Skip to content

Commit 9992a2f

Browse files
committed
modify demo to use multiple test vectors
1 parent 93d0cad commit 9992a2f

File tree

1 file changed

+95
-28
lines changed

1 file changed

+95
-28
lines changed

demo.c

Lines changed: 95 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Byte-oriented AES-256 implementation.
33
* All lookup tables replaced with 'on the fly' calculations.
44
*
5-
* Copyright (c) 2007 Ilya O. Levin, http://www.literatecode.com
5+
* Copyright (c) 2007, 2022 Ilia Levin (ilia@levin.sg)
66
*
77
* Permission to use, copy, modify, and distribute this software for any
88
* purpose with or without fee is hereby granted, provided that the above
@@ -16,45 +16,112 @@
1616
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1717
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1818
*/
19-
#include <stdlib.h>
2019
#include <stdio.h>
20+
#include <string.h>
2121
#include "aes256.h"
2222

23-
#define DUMP(s, i, buf, sz) {printf(s); \
24-
for (i = 0; i < (sz);i++) \
25-
printf("%02x ", buf[i]); \
26-
printf("\n");}
23+
/*
24+
* AES tests from FIPS-197 Appendix C.3, NIST documents SP800-38A and AESAVS
25+
*/
26+
27+
static struct {
28+
uint8_t key[32];
29+
uint8_t pt[16];
30+
uint8_t ct[16];
31+
} test[] = {
32+
{
33+
.key = {
34+
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
35+
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
36+
},
37+
.pt = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff},
38+
.ct = {0x8e, 0xa2, 0xb7, 0xca, 0x51, 0x67, 0x45, 0xbf, 0xea, 0xfc, 0x49, 0x90, 0x4b, 0x49, 0x60, 0x89},
39+
},
40+
{
41+
.key = {
42+
0x60, 0x3D, 0xEB, 0x10, 0x15, 0xCA, 0x71, 0xBE, 0x2B, 0x73, 0xAE, 0xF0, 0x85, 0x7D, 0x77, 0x81,
43+
0x1F, 0x35, 0x2C, 0x07, 0x3B, 0x61, 0x08, 0xD7, 0x2D, 0x98, 0x10, 0xA3, 0x09, 0x14, 0xDF, 0xF4
44+
},
45+
.pt = {0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96, 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A},
46+
.ct = {0xF3, 0xEE, 0xD1, 0xBD, 0xB5, 0xD2, 0xA0, 0x3C, 0x06, 0x4B, 0x5A, 0x7E, 0x3D, 0xB1, 0x81, 0xF8},
47+
},
48+
{
49+
.key = {
50+
0x60, 0x3D, 0xEB, 0x10, 0x15, 0xCA, 0x71, 0xBE, 0x2B, 0x73, 0xAE, 0xF0, 0x85, 0x7D, 0x77, 0x81,
51+
0x1F, 0x35, 0x2C, 0x07, 0x3B, 0x61, 0x08, 0xD7, 0x2D, 0x98, 0x10, 0xA3, 0x09, 0x14, 0xDF, 0xF4
52+
},
53+
.pt = {0xAE, 0x2D, 0x8A, 0x57, 0x1E, 0x03, 0xAC, 0x9C, 0x9E, 0xB7, 0x6F, 0xAC, 0x45, 0xAF, 0x8E, 0x51},
54+
.ct = {0x59, 0x1C, 0xCB, 0x10, 0xD4, 0x10, 0xED, 0x26, 0xDC, 0x5B, 0xA7, 0x4A, 0x31, 0x36, 0x28, 0x70},
55+
},
56+
{
57+
.key = {
58+
0x60, 0x3D, 0xEB, 0x10, 0x15, 0xCA, 0x71, 0xBE, 0x2B, 0x73, 0xAE, 0xF0, 0x85, 0x7D, 0x77, 0x81,
59+
0x1F, 0x35, 0x2C, 0x07, 0x3B, 0x61, 0x08, 0xD7, 0x2D, 0x98, 0x10, 0xA3, 0x09, 0x14, 0xDF, 0xF4
60+
},
61+
.pt = {0x30, 0xC8, 0x1C, 0x46, 0xA3, 0x5C, 0xE4, 0x11, 0xE5, 0xFB, 0xC1, 0x19, 0x1A, 0x0A, 0x52, 0xEF},
62+
.ct = {0xB6, 0xED, 0x21, 0xB9, 0x9C, 0xA6, 0xF4, 0xF9, 0xF1, 0x53, 0xE7, 0xB1, 0xBE, 0xAF, 0xED, 0x1D},
63+
},
64+
{
65+
.key = {
66+
0x60, 0x3D, 0xEB, 0x10, 0x15, 0xCA, 0x71, 0xBE, 0x2B, 0x73, 0xAE, 0xF0, 0x85, 0x7D, 0x77, 0x81,
67+
0x1F, 0x35, 0x2C, 0x07, 0x3B, 0x61, 0x08, 0xD7, 0x2D, 0x98, 0x10, 0xA3, 0x09, 0x14, 0xDF, 0xF4
68+
},
69+
.pt = {0xF6, 0x9F, 0x24, 0x45, 0xDF, 0x4F, 0x9B, 0x17, 0xAD, 0x2B, 0x41, 0x7B, 0xE6, 0x6C, 0x37, 0x10},
70+
.ct = {0x23, 0x30, 0x4B, 0x7A, 0x39, 0xF9, 0xF3, 0xFF, 0x06, 0x7D, 0x8D, 0x8F, 0x9E, 0x24, 0xEC, 0xC7},
71+
},
72+
{
73+
.key = {
74+
0xc4, 0x7b, 0x02, 0x94, 0xdb, 0xbb, 0xee, 0x0f, 0xec, 0x47, 0x57, 0xf2, 0x2f, 0xfe, 0xee, 0x35,
75+
0x87, 0xca, 0x47, 0x30, 0xc3, 0xd3, 0x3b, 0x69, 0x1d, 0xf3, 0x8b, 0xab, 0x07, 0x6b, 0xc5, 0x58
76+
},
77+
.pt = {0},
78+
.ct = {0x46, 0xf2, 0xfb, 0x34, 0x2d, 0x6f, 0x0a, 0xb4, 0x77, 0x47, 0x6f, 0xc5, 0x01, 0x24, 0x2c, 0x5f},
79+
},
80+
{
81+
.key = {
82+
0xfc, 0xa0, 0x2f, 0x3d, 0x50, 0x11, 0xcf, 0xc5, 0xc1, 0xe2, 0x31, 0x65, 0xd4, 0x13, 0xa0, 0x49,
83+
0xd4, 0x52, 0x6a, 0x99, 0x18, 0x27, 0x42, 0x4d, 0x89, 0x6f, 0xe3, 0x43, 0x5e, 0x0b, 0xf6, 0x8e
84+
},
85+
.pt = {0},
86+
.ct = {0x17, 0x9a, 0x49, 0xc7, 0x12, 0x15, 0x4b, 0xbf, 0xfb, 0xe6, 0xe7, 0xa8, 0x4a, 0x18, 0xe2, 0x20},
87+
},
88+
{
89+
.key = {
90+
0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
91+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
92+
},
93+
.pt = {0},
94+
.ct = {0x9c, 0xf4, 0x89, 0x3e, 0xca, 0xfa, 0x0a, 0x02, 0x47, 0xa8, 0x98, 0xe0, 0x40, 0x69, 0x15, 0x59},
95+
},
96+
};
97+
98+
static const uint8_t total_tests = 0x7f & (sizeof(test) / sizeof(test[0]));
99+
27100

28101
int
29102
main(void)
30103
{
31104
aes256_context ctx;
32-
uint8_t key[32];
33-
uint8_t buf[16], i;
34-
35-
/* put a test vector */
36-
for (i = 0; i < sizeof(buf); i++) {
37-
buf[i] = i * 16 + i;
38-
}
39-
for (i = 0; i < sizeof(key); i++) {
40-
key[i] = i;
41-
}
42-
43-
DUMP("txt: ", i, buf, sizeof(buf));
44-
DUMP("key: ", i, key, sizeof(key));
45-
printf("---\n");
105+
uint8_t buf[16];
106+
uint8_t n;
46107

47-
aes256_init(&ctx, key);
48-
aes256_encrypt_ecb(&ctx, buf);
108+
for (n = 0; n < total_tests; n++) {
109+
aes256_init(&ctx, test[n].key);
110+
memcpy(buf, test[n].pt, sizeof(buf));
49111

50-
DUMP("enc: ", i, buf, sizeof(buf));
51-
printf("tst: 8e a2 b7 ca 51 67 45 bf ea fc 49 90 4b 49 60 89\n");
112+
aes256_encrypt_ecb(&ctx, buf);
113+
if (0 != memcmp(buf, test[n].ct, sizeof(buf))) {
114+
return printf("FAILED (case %d/encrypt)\n", n);
115+
}
52116

53-
aes256_init(&ctx, key);
54-
aes256_decrypt_ecb(&ctx, buf);
55-
DUMP("dec: ", i, buf, sizeof(buf));
117+
aes256_decrypt_ecb(&ctx, buf);
118+
if (0 != memcmp(buf, test[n].pt, sizeof(buf))) {
119+
return printf("FAILED (case %d/decrypt)\n", n);
120+
}
56121

57-
aes256_done(&ctx);
122+
aes256_done(&ctx);
123+
}
58124

125+
printf("Success\n");
59126
return 0;
60127
} /* main */

0 commit comments

Comments
 (0)