Ejemplo n.º 1
0
void
serpent_xts_crypt(struct serpent_xts_ctx *ctx, u_int8_t *data, u_int8_t *iv,
    u_int do_encrypt)
{
	u_int8_t block[SERPENT_XTS_BLOCK_LEN];
	u_int i, carry_in, carry_out;

	for (i = 0; i < SERPENT_XTS_BLOCK_LEN; i++)
		block[i] = data[i] ^ iv[i];

	if (do_encrypt)
		serpent_encrypt(&ctx->key1, block, data);
	else
		serpent_decrypt(&ctx->key1, block, data);

	for (i = 0; i < SERPENT_XTS_BLOCK_LEN; i++)
		data[i] ^= iv[i];

	/* Exponentiate tweak */
	carry_in = 0;
	for (i = 0; i < SERPENT_XTS_BLOCK_LEN; i++) {
		carry_out = iv[i] & 0x80;
		iv[i] = (iv[i] << 1) | (carry_in ? 1 : 0);
		carry_in = carry_out;
	}
	if (carry_in)
		iv[0] ^= AES_XTS_ALPHA;
	bzero(block, sizeof(block));
}
Ejemplo n.º 2
0
int main(void){
	unsigned char plaintext[SERPENT_BLOCK_NB_BYTE] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
	unsigned char key[SERPENT_KEY_MAX_NB_BYTE] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
	unsigned char round_key[SERPENT_ROUND_KEY_NB_BYTE];
	unsigned char ciphertext[SERPENT_BLOCK_NB_BYTE];
	unsigned char deciphertext[SERPENT_BLOCK_NB_BYTE];

	printf("Plaintext:  ");
	fprintBuffer_raw(stdout, (char*)plaintext, SERPENT_BLOCK_NB_BYTE);

	printf("\nKey:        ");
	fprintBuffer_raw(stdout, (char*)key, KEY_SIZE);

	serpent_key_expand((uint32_t*)key, KEY_SIZE * 8, (uint32_t*)round_key);
	serpent_encrypt((uint32_t*)plaintext, (uint32_t*)round_key, (uint32_t*)ciphertext);

	printf("\nCiphertext: ");
	fprintBuffer_raw(stdout, (char*)ciphertext, SERPENT_BLOCK_NB_BYTE);

	serpent_decrypt((uint32_t*)ciphertext, (uint32_t*)round_key, (uint32_t*)deciphertext);

	if (memcmp(deciphertext, plaintext, SERPENT_BLOCK_NB_BYTE) == 0){
		printf("\nRecovery:   OK\n");
	}
	else{
		printf("\nRecovery:   FAIL\n");
	}

	return EXIT_SUCCESS;
}
void EncryptCBC::encrypt_block(const uint8_t src[16], uint8_t dst[16])
{
    if (src != dst) cbc_copy(src, dst);

    cbc_xor(l_cbciv, dst);
    serpent_encrypt((const uint32_t*)dst, (uint32_t*)dst, l_key);
    cbc_copy(dst, l_cbciv);
}
Ejemplo n.º 4
0
int main(void)
{
	uint8_t ct1[32], pt1[32], pt2[32], key[64];
	int klen, plen, clen, i, j;
	serpent_key_t skey;
	serpent_blk ct2;
	uint32_t *p;

	printf("\nserpent-256 test\n");

	for (i = 0; i<sizeof(keys) / sizeof(char*); i++) {
		clen = hex2bin(ct1, cipher[i]);
		plen = hex2bin(pt1, plain[i]);
		klen = hex2bin(key, keys[i]);

		/* set key */
		memset(&skey, 0, sizeof(skey));
		p = (uint32_t*)&skey.x[0][0];

		serpent_set_encrypt_key(&skey, key);
		printf("\nkey=");

		for (j = 0; j<sizeof(skey) / sizeof(serpent_subkey_t) * 4; j++) {
			if ((j % 8) == 0) 
				putchar('\n');
			printf("%08X ", p[j]);
		}

		/* encrypt */
		memcpy(ct2.b, pt1, SERPENT_BLOCK_SIZE);

		printf("\n\n");
		dump_hex("plaintext", ct2.b, 16);

		serpent_encrypt(pt1,ct2.b, &skey);

		dump_hex("ciphertext", ct2.b, 16);

		if (memcmp(ct1, ct2.b, clen) == 0) {
			printf("\nEncryption OK");
			serpent_decrypt(ct2.b,pt1, &skey);
			if (memcmp(pt1, ct2.b, plen) == 0) {
				printf("\nDecryption OK");
				dump_hex("plaintext", ct2.b, 16);
			}
			else {
				printf("\nDecryption failed");
			}
		}
		else {
			printf("\nEncryption failed");
		}
	}
	return 0;
}
Ejemplo n.º 5
0
void EncipherBlock(int cipher, void *data, void *ks)
{
#ifdef GST_WINDOWS_BOOT_AES
	if (IsAesHwCpuSupported())
		aes_hw_cpu_encrypt ((byte *) ks, data);
	else
		aes_encrypt (data, data, ks); 
#elif defined (GST_WINDOWS_BOOT_SERPENT)
	serpent_encrypt (data, data, ks);
#elif defined (GST_WINDOWS_BOOT_TWOFISH)
	twofish_encrypt (ks, data, data);
#endif
}
Ejemplo n.º 6
0
static void tnepres_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
{
	const u32 * const s = (const u32 * const)src;
	u32 * const d = (u32 * const)dst;

	u32 rs[4], rd[4];

	rs[0] = swab32(s[3]);
	rs[1] = swab32(s[2]);
	rs[2] = swab32(s[1]);
	rs[3] = swab32(s[0]);

	serpent_encrypt(tfm, (u8 *)rd, (u8 *)rs);

	d[0] = swab32(rd[3]);
	d[1] = swab32(rd[2]);
	d[2] = swab32(rd[1]);
	d[3] = swab32(rd[0]);
}
Ejemplo n.º 7
0
void EncipherBlock(int cipher, void *data, void *ks)
{
	switch (cipher)
	{
	case AES:	
		// In 32-bit kernel mode, due to KeSaveFloatingPointState() overhead, AES instructions can be used only when processing the whole data unit.
#if (defined (_WIN64) || !defined (TC_WINDOWS_DRIVER)) && !defined (TC_WINDOWS_BOOT)
		if (IsAesHwCpuSupported())
			aes_hw_cpu_encrypt (ks, data);
		else
#endif
			aes_encrypt (data, data, ks);
		break;

	case TWOFISH:		twofish_encrypt (ks, data, data); break;
	case SERPENT:		serpent_encrypt (data, data, ks); break;
	default:			TC_THROW_FATAL_EXCEPTION;	// Unknown/wrong ID
	}
}
Ejemplo n.º 8
0
void
serpent_xts_reinit(caddr_t key, u_int8_t *iv)
{
	struct serpent_xts_ctx *ctx = (struct serpent_xts_ctx *)key;
#if 0
	u_int64_t blocknum;
	u_int i;
#endif

#if 0
	/*
	 * Prepare tweak as E_k2(IV). IV is specified as LE representation
	 * of a 64-bit block number which we allow to be passed in directly.
	 */
	/* XXX: possibly use htole64? */
#endif
	/* Last 64 bits of IV are always zero */
	bzero(iv + SERPENT_XTS_IV_LEN, SERPENT_XTS_IV_LEN);

	serpent_encrypt(&ctx->key2, iv, iv);
}
Ejemplo n.º 9
0
	void CipherSerpent::Encrypt (byte *data) const
	{
		serpent_encrypt (data, data, ScheduledKey);
	}
Ejemplo n.º 10
0
void SERPENT_encrypt(SERPENT_KEY *serpent_key,char *input, char *output)
{
    serpent_encrypt(serpent_key, (const serpent_word32 *)input, (serpent_word32 *)output);
}
Ejemplo n.º 11
0
int main()
{
	uint32_t expkey[SERPENT_EXPKEY_WORDS];
	uint8_t check[16];
	int i, j;

	/* test serpent with 128key key */
	for (i = 0; i < table128_num; i++) {
		serpent_setkey(expkey, table128[i].key, 16);

		/* encryption test */
		serpent_encrypt(check, table128[i].plain, expkey);
		if (memcmp(check, table128[i].enc, 16) != 0) {
			fprintf(stderr, "serpent-128 encrypt test %d failed\n", i+1);
			return 1;
		}

		/* encrypt 99 times more */
		for (j = 0; j < 99; j++)
			serpent_encrypt(check, check, expkey);

		if (memcmp(check, table128[i].enc100, 16) != 0) {
			fprintf(stderr, "serpent-128 100x encrypt test %d failed\n", i+1);
			return 1;
		}

		/* encrypt 900 times more */
		for (j = 0; j < 900; j++)
			serpent_encrypt(check, check, expkey);

		if (memcmp(check, table128[i].enc1000, 16) != 0) {
			fprintf(stderr, "serpent-128 1000x encrypt test %d failed\n", i+1);
			return 1;
		}
	}

	/* test serpent with 256bit key */
	for (i = 0; i < table256_num; i++) {

		serpent_setkey(expkey, table256[i].key, 32);

		/* encrypt test */
		serpent_encrypt(check, table256[i].plain, expkey);
		if (memcmp(check, table256[i].enc, 16) != 0) {
			fprintf(stderr, "serpent-256 encrypt test %d failed\n", i+1);
			return 1;
		}

		/* 100x encrypt test */
		for (j = 0; j < 99; j++)
			serpent_encrypt(check, check, expkey);
		if (memcmp(check, table256[i].enc100, 16) != 0) {
			fprintf(stderr, "serpent-256 100x encrypt test %d failed\n", i+1);
			return 1;
		}

		/* 1000x encrypt test */
		for (j = 0; j < 900; j++)
			serpent_encrypt(check, check, expkey);
		if (memcmp(check, table256[i].enc1000, 16) != 0) {
			fprintf(stderr, "serpent-256 1000x encrypt test %d failed\n", i+1);
			return 1;
		}
	}

	return 0;
}
Ejemplo n.º 12
0
static void
serpent128_encrypt(caddr_t key, u_int8_t *blk, u_int8_t *iv)
{
	serpent_encrypt((serpent_ctx *) key, blk, blk);
}
Ejemplo n.º 13
0
void EncryptECB::encrypt_block(const uint8_t src[16], uint8_t dst[16]) const
{
    serpent_encrypt((const uint32_t*)src, (uint32_t*)dst, l_key);
}