#include <stdio.h>
#include <string.h>
#include "api.h"
#include "secure_types.h"
extern KEY_TABLE key_table[15];
#define RED "\x1b[31m"
#define GREEN "\x1b[32m"
#define NORMAL "\x1b[0m"
////////////////////////////////////////////////////////////////////////////////
void dump(const char *label,void *d,int len)
{
int i;
uint8 *dt = (uint8*)d;
printf("%s",label);
for(i=0;i<len;i++)
{
printf("%02x",*dt++);
}
printf("\n");
}
#define BS 4
u128 buf[BS];
u128 enc[BS];
u128 ret[BS];
int fail=0;
int ntest=0;
#define CMP(p,q,s) { ntest++; if (!memcmp(p, q, s)){\
fprintf(stderr, GREEN "CHECK OK\n" NORMAL);} else {\
fail++; fprintf(stderr, RED "!!CHECK NG\n" NORMAL);}}
void AESTEST() {
u128 iv = {
0x55, 0x55, 0x55, 0x55, 0xa5, 0x55, 0xa5, 0x55,
0x55, 0x55, 0x55, 0x55, 0xa5, 0x55, 0xa5, 0x55,
};
for (int i = 0; i< BS; i++) {
buf[i].ul[0] = 0x00010203 + i*0x10101010;
buf[i].ul[1] = 0x04050607 + i*0x10101010;
buf[i].ul[2] = 0x08090a0b + i*0x10101010;
buf[i].ul[3] = 0x0c0d0e0f + i*0x10101010; // making message
}
dump("key:", &key_table[RAMKEY].key, sizeof(u128));
dump("msg:", buf, BS*sizeof(u128));
printf("** AES ECB test\n");
memcpy(enc, (uint8*)SB_B_ENC_ECB(BS,RAMKEY, buf), BS*sizeof(u128));
memcpy(ret, (uint8*)SB_B_DEC_ECB(BS,RAMKEY, enc), BS*sizeof(u128));
dump("ECB crypt:::", enc, BS*sizeof(u128));
dump("ECB decrypt:", ret, BS*sizeof(u128));
CMP(buf, ret, BS*sizeof(u128))
printf("** AES CBC test\n");
memcpy(enc, (uint8*)SB_B_ENC_CBC(BS,RAMKEY, iv, buf), BS*sizeof(u128));
memcpy(ret, (uint8*)SB_B_DEC_CBC(BS,RAMKEY, iv, enc), BS*sizeof(u128));
dump("CBC crypt:::", enc, BS*sizeof(u128));
dump("CBC decrypt:", ret, BS*sizeof(u128));
CMP(buf, ret, BS*sizeof(u128))
}
void CMACTEST(){
printf("** AES-CMAC test\n");
u128 expected = {
0x7f,0xc4,0x8d,0x88,0xd4,0xfd,0x4a,0x13,
0xf9,0xec,0x24,0xd9,0x95,0x71,0xe1,0x01,
};
for (int i = 0; i< BS; i++) {
buf[i].ul[0] = 0x00010203 + i*0x10101010;
buf[i].ul[1] = 0x04050607 + i*0x10101010;
buf[i].ul[2] = 0x08090a0b + i*0x10101010;
buf[i].ul[3] = 0x0c0d0e0f + i*0x10101010; // making message
}
memcpy(ret, SB_B_GENERATE_MAC(BS*sizeof(u128), RAMKEY, buf), sizeof(u128));
dump("mac ret:", ret, sizeof(u128));
CMP(ret, &expected, sizeof(u128))
// ========only 125bit is valid
expected.uc[15] ^= 0x07; // b0000 0111 (3bits reversed)
printf("----- 125bit test: verify will success\n");
uint result = SB_B_VERIFY_MAC(BS*sizeof(u128), 128-3, RAMKEY, buf, &expected);
uint one = 1, zero = 0;
CMP(&result, &zero, sizeof(uint));
printf("------126bit test: verify will failed\n");
result = SB_B_VERIFY_MAC(BS*sizeof(u128), 128-2, RAMKEY, buf, &expected);
CMP(&result, &one, sizeof(uint));
}
int main(int argc, char const* argv[])
{
KEY128 key = {
0x10, 0x55, 0x55, 0x55, 0xa5, 0x55, 0xa5, 0x55,
0xaa, 0x55, 0x55, 0x55, 0xa5, 0x55, 0xa5, 0x55,
};
SB_B_LOAD_PLAIN_KEY(key);
AESTEST();
CMACTEST();
printf("passed %d/%d\n", ntest-fail, ntest);
return 0;
}
// vi:expandtab:foldmethod=marker sw=4 ts=4