Files
micro-AES/testvectors/aes_testvectors_CCM.h
polfosol 7956dce4c1 fixed a couple of minor issues.
announcing the first release of simple-ASCON
2025-10-04 15:37:27 +03:30

97 lines
2.8 KiB
C

/*
==============================================================================
Name : aes_testvectors_CCM.h
Author : polfosol
Version : 1.2.0.0
Copyright : copyright © 2024 - polfosol
Description : checking the test vectors for AES-CCM
==============================================================================
*/
#include "aes_testvectors.h"
#if defined(_TESTING_CCM_H_) ^ defined(CCM_TEST_FILE)
#define _TESTING_CCM_H_
int verifyccm(uint8_t* key, uint8_t* i, uint8_t* p, uint8_t* a, uint8_t* c,
size_t np, size_t na, char* r)
{
char sk[2 * AES_KEYLENGTH + 1], si[30], sp[80], sc[96], sa[80], msg[30];
uint8_t tmp[64], v = 0;
strcpy(msg, "passed the test");
AES_CCM_encrypt(key, i, a, na, p, np, tmp);
if (memcmp(c, tmp, np + CCM_TAG_LEN))
{
strcpy(msg, "encrypt failure");
v = 1;
}
memset(tmp, 0xcc, sizeof tmp);
*r = AES_CCM_decrypt(key, i, a, na, c, np, tmp);
if (*r || memcmp(p, tmp, np))
{
strcat(strcpy(msg, v ? "encrypt & " : ""), "decrypt failure");
v |= 2;
}
bytes2str(key, sk, AES_KEYLENGTH);
bytes2str(i, si, CCM_NONCE_LEN);
bytes2str(p, sp, np);
bytes2str(a, sa, na);
bytes2str(c, sc, np + CCM_TAG_LEN);
sprintf(r, "%s\nK: %s\ni: %s\nP: %s\nA: %s\nC: %s", msg, sk, si, sp, sa, sc);
return v;
}
void aes_ccm_test(FILE** files, unsigned* count)
{
const char* head[] = CCM_HEADLINES;
char buffer[0x800], *value = NULL;
size_t s[5] = { 0 };
uint8_t h, e = 0, key[32], iv[15], p[64], c[80], a[64];
while (fgets(buffer, sizeof buffer, *files) != NULL)
{
buffer[ strcspn(buffer, "\n") ] = 0;
for (h = strlen(buffer) < 4 ? 5 : 0; h < 5; ++h)
{
if (strncmp(buffer, head[h], strlen(head[h])) != 0) continue;
value = strrchr(buffer, ' ') + 1;
s[h] = strlen(value) / 2 - CCM_TAG_LEN * (h == 4);
switch (h)
{
case 0:
str2bytes(value, key);
break;
case 1:
str2bytes(value, iv);
break;
case 2:
str2bytes(value, a);
break;
case 3:
str2bytes(value, p);
break;
case 4:
str2bytes(value, c);
break;
}
e += (h == 3 || h == 4);
}
if (e == 2)
{
if (AES_KEYLENGTH == *s && CCM_NONCE_LEN == s[1] && s[3] == s[4])
{
e = verifyccm(key, iv, p, a, c, s[3], s[2], buffer);
fprintf(files[2 - !e], "%s\n", buffer); /* save the log */
++count[0];
if (e & 1) ++count[1];
if (e & 2) ++count[2];
}
e = 0;
}
}
}
#endif /* header guard */