Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added CMAC SPEC rfc4493.txt.pdf
Binary file not shown.
37 changes: 34 additions & 3 deletions aes_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,41 @@ void aes_cmac_test()
print_hex(mac, 16);
}

void aes_cmac_can_test()
{
unsigned char key_auth[16] = {0x30, 0xC1, 0x37, 0xAA, 0x33, 0x85, 0xDE, 0x39, 0x07, 0xB3, 0x09, 0x4B, 0x03, 0x0C, 0xFD, 0x30};

unsigned char out_mac[16];
unsigned char pdu_in[18] = {0x03, 0x51, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x00, 0x16, 0x00, 0x01, 0x00, 0x00, 0x35};
aes_cmac(pdu_in, sizeof(pdu_in), key_auth, out_mac);
print_hex(out_mac, 16);
}

void aes_cbc_test()
{
unsigned char aes_key[16] = {0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01};
unsigned char aes_iv[16] = {0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02};
char in[128] = "123456789agdugyasdgfyagsdvsdayfadfakdfggyu";
unsigned char out[128];
char tmp[128];
int out_len;
int tmp_len;

memset(out, 0, sizeof(out));
memset(tmp, 0, sizeof(tmp));

out_len = AES_CBC_encrypt(in, out, strlen(in), aes_key, 16, aes_iv);
print_hex(out, out_len);
AES_CBC_decrypt(out, tmp, out_len, aes_key, 16, aes_iv);
printf("%s\n", tmp);
}

int main()
{
aes_128_test();
//aes_128_test();
// aes_256_test();
aes_128_cbc_test();
aes_cmac_test();
//aes_128_cbc_test();
//aes_cmac_test();
aes_cbc_test();
aes_cmac_can_test();
}
18 changes: 9 additions & 9 deletions aes_cmac.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void aes_cmac(uint8_t *input, unsigned long length, uint8_t *key, uint8_t *mac_v
{
uint8_t subkey_1[AES_BLOCKSIZE];
uint8_t subkey_2[AES_BLOCKSIZE];
uint8_t previous_block_ciphertext[AES_BLOCKSIZE] = {};
uint8_t previous_block_ciphertext[AES_BLOCKSIZE] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
uint8_t temp[AES_BLOCKSIZE];
unsigned long i;
aes_ctx_t *aes_ctx;
Expand All @@ -30,29 +30,29 @@ void aes_cmac(uint8_t *input, unsigned long length, uint8_t *key, uint8_t *mac_v

#ifdef DEBUG
printf("Position %lx\n", i);
printf("M:\n");
print_block(input);
printf("IV:\n");
print_block(previous_block_ciphertext);
#endif
block_xor_triple(input, previous_block_ciphertext, temp);

#ifdef DEBUG
printf("xored with IV:\n");
print_block(temp);
#endif

if(i + AES_BLOCKSIZE == length)
{
//the last block if full, xor with subkey_1
memcpy(temp, input, AES_BLOCKSIZE);
block_xor_triple(temp, subkey_1, temp);
}
else if(i + AES_BLOCKSIZE > length)
{
//last block is not full, add padding
memcpy(temp, input, length-i);
add_padding(temp, length - i);
//print_block(temp);
block_xor_triple(temp, subkey_2, temp);
}
else
{
memcpy(temp, input, AES_BLOCKSIZE);
}
block_xor_triple(temp, previous_block_ciphertext, temp);

#ifdef DEBUG
printf("xored with key:\n");
Expand Down