Exemplo n.º 1
0
/*
 * Decrypt a ciphertext message using the mt19937 generator as keystream
 */
unsigned int decrypt( uint8_t *ptext[], const  uint8_t ctext[], const size_t len)
{
	size_t i;
	uint8_t *keystream;

	// MT PRNG not init
	if (!token_init)
		return 0x01;

	keystream = malloc(len*sizeof(uint8_t));
	if (NULL == keystream)	
		return 0x01;

	*ptext = malloc((len - random_prefix_len)*sizeof(uint8_t));
	if (NULL == ptext)
	{
		free(keystream);
		return 0x01;
	}

	mt19937_init( &cipher_mt, cipher_mt.seed);
	for (i = 0; i < len; i++)
		keystream[i] = mt19937_get_value(&cipher_mt);

	xor_encode(*ptext , ctext + random_prefix_len, len, keystream, len);

	free(keystream);

	return 0x00;
}
Exemplo n.º 2
0
/*
 * Encrypt a plaintext message using the mt19937 generator as keystream
 */
size_t encrypt( uint8_t *ctext[], const  uint8_t ptext[], const size_t len)
{
	size_t i;
	uint8_t *keystream;

	if (!random_prefix_len)
		random_prefix_len = 1 + (get_random_value() % 15);

	// Init MT19937 prng to a new seed
	password_token_reset();		
		
	keystream = malloc(len*sizeof(uint8_t));
	if (NULL == keystream)	
		return 0x00;

	*ctext = malloc((random_prefix_len + len)*sizeof(uint8_t));
	if (NULL == *ctext)
	{
		free(keystream);
		return 0x00;
	}	

	for (i = 0; i < random_prefix_len; i++)
		(*ctext)[i] = (uint8_t) get_random_value();

	mt19937_init( &cipher_mt, cipher_mt.seed);
	for (i = 0; i < len; i++)
		keystream[i] = mt19937_get_value(&cipher_mt);

	xor_encode(&((*ctext)[random_prefix_len]), ptext, len, keystream, len);

	free(keystream);

	return random_prefix_len + len;
}
Exemplo n.º 3
0
/*
 * Equivalent of rand(), using the newly implemented MT19937 prng.
 */
uint32_t get_random_value()
{
	struct mt19937_t mt_single_shot;
	mt19937_init(&mt_single_shot, time(NULL));

	return mt19937_get_value(&mt_single_shot);
}
Exemplo n.º 4
0
/*
 *  Reset the prng seed to the current timestamp (aka token reset).
 */
void password_token_reset()
{
	sleep(1 + (get_random_value() % 15));

	mt19937_init(&cipher_mt, time(NULL));
	token_init = 0x01;
	
	sleep(1 +(get_random_value() % 15));
}
Exemplo n.º 5
0
/* return a 128-bit random value using the MT19937. 
 * I'm pretty sure it's not truly random (small numbers must have are slightl less likely to appear)
 */
unsigned int main_get_128_bit_random_value(mpz_t *rvalue)
{
	size_t i, char_bit_val = 0x8;
	uint32_t tmp; 
	struct mt19937_t mt_single_shot;
	char r_hexstr[8*4 + 1] = {0};

	mt19937_init(&mt_single_shot, time(NULL));

	for (i = 0; i < 4; i++)
	{
		tmp = mt19937_get_value(&mt_single_shot);
		snprintf(r_hexstr + i*char_bit_val, 1 + char_bit_val, "%08x", tmp);
	}

	r_hexstr[8*4] = 0x00;
	mpz_init_set_str(*rvalue, r_hexstr, 16);
	return 0x00;
}
Exemplo n.º 6
0
/*
 * 	Generate a #PKCS1.5 padding with non-zeros random values (for block type 2)
 * 	The output string buffer should have enough memory to incorporate the padding.
 */
int pkcs1_v1_5_gen_rpadding(unsigned char *padding, const size_t padlen)
{
	int non_zero;
	size_t i, j;
	struct mt19937_t r_gen;

	mt19937_init(&r_gen, time(NULL));
	
	i = 0x00;
	while (4*i < padlen)
	{
		((uint32_t*) padding)[i] = mt19937_get_value(&r_gen);

		non_zero = 0x01;
		for (j = 4*i; j < 4*(i+1); j++)
			non_zero &= (padding[j] != 0x00);

		if (non_zero)
			i++;
	}

	return 0x00;
}
Exemplo n.º 7
0
uint32_t first_output_given_seed(uint32_t seed)
{
  mt19937 mt = mt19937_init(seed);
  return mt19937_extract(&mt);
}