-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheax_aes_test.c
134 lines (107 loc) · 3.52 KB
/
eax_aes_test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "eax128.h"
#include "aes128.h"
#include "vectors_eax_aes.h"
static struct
{
uint32_t words[AES128_NREGS];
} aes_regs;
void aes128_streg(int i, uint32_t w)
{
aes_regs.words[i] = w;
}
uint32_t aes128_ldreg(int i)
{
return aes_regs.words[i];
}
void aes_install_key(const uint8_t *key)
{
aes128_set_key(key);
}
void print_dump(const void *data, int len)
{
const uint8_t *p = data;
int col = 0;
const int max_cols = 8;
while (col < len)
{
if (!(col % max_cols))
printf("\n%08x:", col);
printf(" %02x", *p);
p++;
col++;
}
printf("\n");
}
extern void eax128_cipher(void *ctx, uint8_t block[16])
{
aes128_set_data(block);
aes128_encrypt();
aes128_get_data(block);
}
static void test_vector(const testvector_t *v)
{
eax128_t ctx;
aes_install_key(v->key);
eax128_init(&ctx, NULL, v->nonce, v->noncelen);
uint8_t pt[256];
for (int i = 0; i < v->headerlen; i++)
eax128_auth_header(&ctx, v->header[i]);
for (int i = 0; i < v->ctlen; i++)
eax128_auth_data(&ctx, v->ct[i]);
for (int i = 0; i < v->ctlen; i++)
{
pt[i] = eax128_crypt_data(&ctx, i, v->ct[i]);
}
uint8_t local_tag[16];
eax128_digest(&ctx, local_tag);
if (memcmp(pt, v->pt, v->ptlen) != 0)
{
print_dump(pt, v->ptlen);
print_dump(v->pt, v->ptlen);
printf("decrypt fail\n");
exit(-1);
}
if (memcmp(local_tag, v->tag, v->taglen) != 0)
{
print_dump(v->tag, v->taglen);
print_dump(local_tag, v->taglen);
printf("auth fail\n");
exit(-1);
}
}
// special test to be sure the 64 bit nonce addition is running fine
static void test_ctr_ovf(void)
{
uint8_t key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
uint8_t nonce[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfd};
uint8_t pt[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11,
0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24,
0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f};
uint8_t ct[] = {0xfc, 0x55, 0xa7, 0x76, 0xe8, 0xfa, 0x9f, 0x5e, 0x7b, 0x6f, 0xf2, 0xdc, 0xeb, 0x4b, 0xf7, 0xb5, 0x26, 0xda,
0xfa, 0xb4, 0x0d, 0xda, 0xde, 0x1b, 0x69, 0xab, 0x95, 0x8c, 0xbb, 0xa0, 0xa3, 0x1a, 0x19, 0x86, 0xcd, 0x29, 0x2e, 0x7d,
0x74, 0x8f, 0x97, 0xfb, 0x29, 0x08, 0x68, 0x92, 0xba, 0x3d, 0x23, 0x29, 0xa8, 0x59, 0xd0, 0x9e, 0x31, 0x99, 0x48, 0x9a, 0x90, 0x86, 0x0c, 0x83, 0xa7, 0xe1};
eax128_ctr_t ctr;
aes_install_key(key);
eax128_ctr_init(&ctr, NULL, nonce);
for (int i = 0; i < sizeof(pt); i++)
{
int ptb = eax128_ctr_process(&ctr, i, pt[i]);
if (ptb != ct[i])
{
printf("ctr failed\n");
exit(-1);
}
}
}
int main(void)
{
test_ctr_ovf();
for (int i = 0; i < sizeof(testvectors) / sizeof(testvectors[0]); i++)
test_vector(&testvectors[i]);
printf("Ok");
return 0;
}