void aes_decrypt_cbc(uint8_t *output, aes_key *key, aes_block *ivini, uint8_t *input, uint32_t nb_blocks) { aes_block block,blocko; aes_block iv; if (!nb_blocks) return; #if defined(ARCH_X86) && defined(WITH_AESNI) if (have_aesni() && key->nbr == 10) { return aes_ni_decrypt_cbc(output, key, (uint8_t *) ivini, input, nb_blocks); } #endif /* preload IV in block */ block128_copy(&iv, ivini); aes_decrypt_block(&block, key, &block); for ( ;nb_blocks-- > 0; input += 16, output += 16) { block128_copy(&block, (block128 *) input); aes_decrypt_block(&blocko, key, &block); block128_vxor((block128 *) output, &blocko, &iv); block128_copy(&iv, &block); } }
void test_aes_decrypt_block() { uint8_t ret_text[16] = {0}; uint8_t text[16] = { 0x01,0x23,0x45,0x67, 0x89,0xab,0xcd,0xef, 0xfe,0xdc,0xba,0x98, 0x76,0x54,0x32,0x10 }; uint8_t cipher_text[16] = { 0xff,0x0b,0x84,0x4a, 0x08,0x53,0xbf,0x7c, 0x69,0x34,0xab,0x43, 0x64,0x14,0x8f,0xb9 }; uint8_t key[16] = { 0x0f,0x15,0x71,0xc9, 0x47,0xd9,0xe8,0x59, 0x0c,0xb7,0xad,0xd6, 0xaf,0x7f,0x67,0x98 }; aes_context ctx; CU_ASSERT_EQUAL(aes_set_key(&ctx, key, sizeof(key)*8), SUCCESS); CU_ASSERT_EQUAL(aes_decrypt_block(&ctx, ret_text, cipher_text), SUCCESS); int ret = memcmp(ret_text, text, 16); CU_ASSERT_EQUAL(memcmp(ret_text, text, 16), 0); }
void aes_decrypt_ecb(uint8_t *output, aes_key *key, uint8_t *input, uint32_t nb_blocks) { if (!nb_blocks) return; #if defined(ARCH_X86) && defined(WITH_AESNI) if (have_aesni() && key->nbr == 10) return aes_ni_decrypt_ecb(output, key, input, nb_blocks); #endif for ( ; nb_blocks-- > 0; input += 16, output += 16) { aes_decrypt_block((block128 *) output, key, (block128 *) input); } }
int main() { uint8_t ret_text[16] = {0}; uint8_t text[16] = { 0x01,0x23,0x45,0x67, 0x89,0xab,0xcd,0xef, 0xfe,0xdc,0xba,0x98, 0x76,0x54,0x32,0x10 }; uint8_t cipher_text[16] = {0}; uint8_t key[32] = { 0x0f,0x15,0x71,0xc9, 0x47,0xd9,0xe8,0x59, 0x0c,0xb7,0xad,0xd6, 0xaf,0x7f,0x67,0x98, 0x0f,0x15,0x71,0xc9, 0x47,0xd9,0xe8,0x59, 0x0c,0xb7,0xad,0xd6, 0xaf,0x7f,0x67,0x98 }; uint32_t key_bit[3] = {128, 192, 256}; aes_context ctx; int i; for (i = 0; i < sizeof(key_bit)/sizeof(key_bit[0]); ++i) { if (aes_set_key(&ctx, key, key_bit[i]) != SUCCESS) { perror("aes_set_key error."); return -1; } if(aes_encrypt_block(&ctx, cipher_text, text) != SUCCESS) { perror("aes_encrypt_block error."); return -1; } if(aes_decrypt_block(&ctx, ret_text, cipher_text) != SUCCESS) { perror("aes_decrypt_block error."); return -1; } printf("key_bit %d: \n", key_bit[i]); print("\tinput : ", text); print("\tencrypt: ", cipher_text); print("\tdecrypt: ", ret_text); } return 0; }
void tmd_aes_generic_decrypt_xts(aes_block *output, const aes_key *k1, aes_key *k2, aes_block *dataunit, uint32_t spoint, aes_block *input, uint32_t nb_blocks) { aes_block block, tweak; /* load IV and encrypt it using k2 as the tweak */ block128_copy(&tweak, dataunit); aes_encrypt_block(&tweak, k2, &tweak); /* TO OPTIMISE: this is really inefficient way to do that */ while (spoint-- > 0) tmd_gf_mulx(&tweak); for ( ; nb_blocks-- > 0; input++, output++, tmd_gf_mulx(&tweak)) { block128_vxor(&block, input, &tweak); aes_decrypt_block(&block, k1, &block); block128_vxor(output, &block, &tweak); } }
void aes_decrypt_xts(uint8_t *output, aes_key *k1, aes_key *k2, aes_block *dataunit, uint32_t spoint, uint8_t *input, uint32_t nb_blocks) { aes_block block, tweak; if (!nb_blocks) return; /* load IV and encrypt it using k2 as the tweak */ block128_copy(&tweak, dataunit); aes_encrypt_block(&tweak, k2, &tweak); /* TO OPTIMISE: this is really inefficient way to do that */ while (spoint-- > 0) gf_mulx(&tweak); for ( ; nb_blocks-- > 0; input += 16, output += 16, gf_mulx(&tweak)) { block128_vxor(&block, (block128 *) input, &tweak); aes_decrypt_block(&block, k1, &block); block128_vxor((block128 *) output, &block, &tweak); } }