コード例 #1
0
ファイル: tea.c プロジェクト: luciomarinelli/gtk-gnutella
/**
 * Test implementation.
 */
void G_COLD
tea_test(void)
{
	tea_key_t key;
	tea_block_t value;
	tea_block_t encrypted;
	tea_block_t decrypted;
	int i;
	char in[80];
	char out[80];
	char recovered[80];

	STATIC_ASSERT(sizeof(key.v) == TEA_KEY_SIZE);
	STATIC_ASSERT(sizeof(value.v) == TEA_BLOCK_SIZE);

	for (i = 0; i < 10; i++) {
		int j;
		bool randomized = FALSE;

		for (j = 0; j < 10; j++) {
			random_bytes(key.v, TEA_KEY_SIZE);
			random_bytes(value.v, TEA_BLOCK_SIZE);

			t_encrypt(&encrypted, &key, &value);
			if (0 != memcmp(value.v, encrypted.v, TEA_BLOCK_SIZE)) {
				randomized = TRUE;
				break;
			}
		}

		if (!randomized)
			g_error("no luck with random numbers in tea_test()");

		t_decrypt(&decrypted, &key, &encrypted);
		if (0 != memcmp(value.v, decrypted.v, TEA_BLOCK_SIZE)) {
			g_error("TEA implementation tests FAILED");
			return;
		}
	}

	STATIC_ASSERT(sizeof in == sizeof out);
	STATIC_ASSERT(sizeof in == sizeof recovered);

	random_bytes(key.v, TEA_KEY_SIZE);
	random_bytes(in, sizeof in);
	tea_encrypt(&key, out, in, sizeof in);
	tea_decrypt(&key, recovered, out, sizeof out);

	if (0 != memcmp(in, recovered, sizeof in))
		g_error("TEA implementation tests FAILED");
}
コード例 #2
0
ファイル: tea_crypt.c プロジェクト: chenfangxin/vsec_disp
int nm_decrypt(unsigned int *val, int len)
{
	int i=0;
#if defined(NM_TEA_CRYPT)
	int crypt_len = len/sizeof(unsigned int);
	for(i=0;i<crypt_len;i+=2){
		tea_decrypt(val+i, tea_key);
	}

	return crypt_len*sizeof(unsigned int);
#else
	unsigned char *ptr=(unsigned char *)val;
	for(i=0;i<len;i++){
		if(*(ptr+i) & 0x80){
			*(ptr+i) &= 0x7f;
		}else{
			*(ptr+i) |= 0x80;
		}
	}
	return len;
#endif
}