3
|
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis |
|
2 * |
|
3 * LibTomCrypt is a library that provides various cryptographic |
|
4 * algorithms in a highly modular and flexible manner. |
|
5 * |
|
6 * The library is free for all purposes without any express |
|
7 * guarantee it works. |
|
8 * |
|
9 * Tom St Denis, [email protected], http://libtomcrypt.org |
|
10 */ |
|
11 |
|
12 /* EAX Implementation by Tom St Denis */ |
|
13 #include "mycrypt.h" |
|
14 |
|
15 #ifdef EAX_MODE |
|
16 |
|
17 int eax_test(void) |
|
18 { |
|
19 #ifndef LTC_TEST |
|
20 return CRYPT_NOP; |
|
21 #else |
|
22 static const struct { |
|
23 int keylen, |
|
24 noncelen, |
|
25 headerlen, |
|
26 msglen; |
|
27 |
|
28 unsigned char key[MAXBLOCKSIZE], |
|
29 nonce[MAXBLOCKSIZE], |
|
30 header[MAXBLOCKSIZE], |
|
31 plaintext[MAXBLOCKSIZE], |
|
32 ciphertext[MAXBLOCKSIZE], |
|
33 tag[MAXBLOCKSIZE]; |
|
34 } tests[] = { |
|
35 |
|
36 /* NULL message */ |
|
37 { |
|
38 16, 0, 0, 0, |
|
39 /* key */ |
|
40 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
|
41 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }, |
|
42 /* nonce */ |
|
43 { 0 }, |
|
44 /* header */ |
|
45 { 0 }, |
|
46 /* plaintext */ |
|
47 { 0 }, |
|
48 /* ciphertext */ |
|
49 { 0 }, |
|
50 /* tag */ |
|
51 { 0x9a, 0xd0, 0x7e, 0x7d, 0xbf, 0xf3, 0x01, 0xf5, |
|
52 0x05, 0xde, 0x59, 0x6b, 0x96, 0x15, 0xdf, 0xff } |
|
53 }, |
|
54 |
|
55 /* test with nonce */ |
|
56 { |
|
57 16, 16, 0, 0, |
|
58 /* key */ |
|
59 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
|
60 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }, |
|
61 /* nonce */ |
|
62 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
|
63 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }, |
|
64 /* header */ |
|
65 { 0 }, |
|
66 /* plaintext */ |
|
67 { 0 }, |
|
68 /* ciphertext */ |
|
69 { 0 }, |
|
70 /* tag */ |
|
71 { 0x1c, 0xe1, 0x0d, 0x3e, 0xff, 0xd4, 0xca, 0xdb, |
|
72 0xe2, 0xe4, 0x4b, 0x58, 0xd6, 0x0a, 0xb9, 0xec } |
|
73 }, |
|
74 |
|
75 /* test with header [no nonce] */ |
|
76 { |
|
77 16, 0, 16, 0, |
|
78 /* key */ |
|
79 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
|
80 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }, |
|
81 /* nonce */ |
|
82 { 0 }, |
|
83 /* header */ |
|
84 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
|
85 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }, |
|
86 /* plaintext */ |
|
87 { 0 }, |
|
88 /* ciphertext */ |
|
89 { 0 }, |
|
90 /* tag */ |
|
91 { 0x3a, 0x69, 0x8f, 0x7a, 0x27, 0x0e, 0x51, 0xb0, |
|
92 0xf6, 0x5b, 0x3d, 0x3e, 0x47, 0x19, 0x3c, 0xff } |
|
93 }, |
|
94 |
|
95 /* test with header + nonce + plaintext */ |
|
96 { |
|
97 16, 16, 16, 32, |
|
98 /* key */ |
|
99 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
|
100 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }, |
|
101 /* nonce */ |
|
102 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
|
103 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }, |
|
104 /* header */ |
|
105 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
|
106 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }, |
|
107 /* plaintext */ |
|
108 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
|
109 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
|
110 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, |
|
111 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f }, |
|
112 /* ciphertext */ |
|
113 { 0x29, 0xd8, 0x78, 0xd1, 0xa3, 0xbe, 0x85, 0x7b, |
|
114 0x6f, 0xb8, 0xc8, 0xea, 0x59, 0x50, 0xa7, 0x78, |
|
115 0x33, 0x1f, 0xbf, 0x2c, 0xcf, 0x33, 0x98, 0x6f, |
|
116 0x35, 0xe8, 0xcf, 0x12, 0x1d, 0xcb, 0x30, 0xbc }, |
|
117 /* tag */ |
|
118 { 0x4f, 0xbe, 0x03, 0x38, 0xbe, 0x1c, 0x8c, 0x7e, |
|
119 0x1d, 0x7a, 0xe7, 0xe4, 0x5b, 0x92, 0xc5, 0x87 } |
|
120 }, |
|
121 |
|
122 /* test with header + nonce + plaintext [not even sizes!] */ |
|
123 { |
|
124 16, 15, 14, 29, |
|
125 /* key */ |
|
126 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
|
127 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }, |
|
128 /* nonce */ |
|
129 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
|
130 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e }, |
|
131 /* header */ |
|
132 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
|
133 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d }, |
|
134 /* plaintext */ |
|
135 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
|
136 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
|
137 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, |
|
138 0x18, 0x19, 0x1a, 0x1b, 0x1c }, |
|
139 /* ciphertext */ |
|
140 { 0xdd, 0x25, 0xc7, 0x54, 0xc5, 0xb1, 0x7c, 0x59, |
|
141 0x28, 0xb6, 0x9b, 0x73, 0x15, 0x5f, 0x7b, 0xb8, |
|
142 0x88, 0x8f, 0xaf, 0x37, 0x09, 0x1a, 0xd9, 0x2c, |
|
143 0x8a, 0x24, 0xdb, 0x86, 0x8b }, |
|
144 /* tag */ |
|
145 { 0x0d, 0x1a, 0x14, 0xe5, 0x22, 0x24, 0xff, 0xd2, |
|
146 0x3a, 0x05, 0xfa, 0x02, 0xcd, 0xef, 0x52, 0xda } |
|
147 }, |
|
148 |
|
149 /* Vectors from Brian Gladman */ |
|
150 |
|
151 { |
|
152 16, 16, 8, 0, |
|
153 /* key */ |
|
154 { 0x23, 0x39, 0x52, 0xde, 0xe4, 0xd5, 0xed, 0x5f, |
|
155 0x9b, 0x9c, 0x6d, 0x6f, 0xf8, 0x0f, 0xf4, 0x78 }, |
|
156 /* nonce */ |
|
157 { 0x62, 0xec, 0x67, 0xf9, 0xc3, 0xa4, 0xa4, 0x07, |
|
158 0xfc, 0xb2, 0xa8, 0xc4, 0x90, 0x31, 0xa8, 0xb3 }, |
|
159 /* header */ |
|
160 { 0x6b, 0xfb, 0x91, 0x4f, 0xd0, 0x7e, 0xae, 0x6b }, |
|
161 /* PT */ |
|
162 { 0x00 }, |
|
163 /* CT */ |
|
164 { 0x00 }, |
|
165 /* tag */ |
|
166 { 0xe0, 0x37, 0x83, 0x0e, 0x83, 0x89, 0xf2, 0x7b, |
|
167 0x02, 0x5a, 0x2d, 0x65, 0x27, 0xe7, 0x9d, 0x01 } |
|
168 }, |
|
169 |
|
170 { |
|
171 16, 16, 8, 2, |
|
172 /* key */ |
|
173 { 0x91, 0x94, 0x5d, 0x3f, 0x4d, 0xcb, 0xee, 0x0b, |
|
174 0xf4, 0x5e, 0xf5, 0x22, 0x55, 0xf0, 0x95, 0xa4 }, |
|
175 /* nonce */ |
|
176 { 0xbe, 0xca, 0xf0, 0x43, 0xb0, 0xa2, 0x3d, 0x84, |
|
177 0x31, 0x94, 0xba, 0x97, 0x2c, 0x66, 0xde, 0xbd }, |
|
178 /* header */ |
|
179 { 0xfa, 0x3b, 0xfd, 0x48, 0x06, 0xeb, 0x53, 0xfa }, |
|
180 /* PT */ |
|
181 { 0xf7, 0xfb }, |
|
182 /* CT */ |
|
183 { 0x19, 0xdd }, |
|
184 /* tag */ |
|
185 { 0x5c, 0x4c, 0x93, 0x31, 0x04, 0x9d, 0x0b, 0xda, |
|
186 0xb0, 0x27, 0x74, 0x08, 0xf6, 0x79, 0x67, 0xe5 } |
|
187 }, |
|
188 |
|
189 { |
|
190 16, 16, 8, 5, |
|
191 /* key */ |
|
192 { 0x01, 0xf7, 0x4a, 0xd6, 0x40, 0x77, 0xf2, 0xe7, |
|
193 0x04, 0xc0, 0xf6, 0x0a, 0xda, 0x3d, 0xd5, 0x23 }, |
|
194 /* nonce */ |
|
195 { 0x70, 0xc3, 0xdb, 0x4f, 0x0d, 0x26, 0x36, 0x84, |
|
196 0x00, 0xa1, 0x0e, 0xd0, 0x5d, 0x2b, 0xff, 0x5e }, |
|
197 /* header */ |
|
198 { 0x23, 0x4a, 0x34, 0x63, 0xc1, 0x26, 0x4a, 0xc6 }, |
|
199 /* PT */ |
|
200 { 0x1a, 0x47, 0xcb, 0x49, 0x33 }, |
|
201 /* CT */ |
|
202 { 0xd8, 0x51, 0xd5, 0xba, 0xe0 }, |
|
203 /* Tag */ |
|
204 { 0x3a, 0x59, 0xf2, 0x38, 0xa2, 0x3e, 0x39, 0x19, |
|
205 0x9d, 0xc9, 0x26, 0x66, 0x26, 0xc4, 0x0f, 0x80 } |
|
206 } |
|
207 |
|
208 }; |
|
209 int err, x, idx, res; |
|
210 unsigned long len; |
|
211 unsigned char outct[MAXBLOCKSIZE], outtag[MAXBLOCKSIZE]; |
|
212 |
|
213 /* AES can be under rijndael or aes... try to find it */ |
|
214 if ((idx = find_cipher("aes")) == -1) { |
|
215 if ((idx = find_cipher("rijndael")) == -1) { |
|
216 return CRYPT_NOP; |
|
217 } |
|
218 } |
|
219 |
|
220 for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) { |
|
221 len = sizeof(outtag); |
|
222 if ((err = eax_encrypt_authenticate_memory(idx, tests[x].key, tests[x].keylen, |
|
223 tests[x].nonce, tests[x].noncelen, tests[x].header, tests[x].headerlen, |
|
224 tests[x].plaintext, tests[x].msglen, outct, outtag, &len)) != CRYPT_OK) { |
|
225 return err; |
|
226 } |
|
227 if (memcmp(outct, tests[x].ciphertext, tests[x].msglen) || memcmp(outtag, tests[x].tag, len)) { |
|
228 #if 0 |
|
229 unsigned long y; |
|
230 printf("\n\nFailure: \nCT:\n"); |
|
231 for (y = 0; y < (unsigned long)tests[x].msglen; ) { |
|
232 printf("0x%02x", outct[y]); |
|
233 if (y < (unsigned long)(tests[x].msglen-1)) printf(", "); |
|
234 if (!(++y % 8)) printf("\n"); |
|
235 } |
|
236 printf("\nTAG:\n"); |
|
237 for (y = 0; y < len; ) { |
|
238 printf("0x%02x", outtag[y]); |
|
239 if (y < len-1) printf(", "); |
|
240 if (!(++y % 8)) printf("\n"); |
|
241 } |
|
242 #endif |
|
243 return CRYPT_FAIL_TESTVECTOR; |
|
244 } |
|
245 |
|
246 /* test decrypt */ |
|
247 if ((err = eax_decrypt_verify_memory(idx, tests[x].key, tests[x].keylen, |
|
248 tests[x].nonce, tests[x].noncelen, tests[x].header, tests[x].headerlen, |
|
249 outct, tests[x].msglen, outct, outtag, len, &res)) != CRYPT_OK) { |
|
250 return err; |
|
251 } |
|
252 if ((res != 1) || memcmp(outct, tests[x].plaintext, tests[x].msglen)) { |
|
253 #if 0 |
|
254 unsigned long y; |
|
255 printf("\n\nFailure (res == %d): \nPT:\n", res); |
|
256 for (y = 0; y < (unsigned long)tests[x].msglen; ) { |
|
257 printf("0x%02x", outct[y]); |
|
258 if (y < (unsigned long)(tests[x].msglen-1)) printf(", "); |
|
259 if (!(++y % 8)) printf("\n"); |
|
260 } |
|
261 printf("\n\n"); |
|
262 #endif |
|
263 return CRYPT_FAIL_TESTVECTOR; |
|
264 } |
|
265 |
|
266 } |
|
267 return CRYPT_OK; |
|
268 #endif /* LTC_TEST */ |
|
269 } |
|
270 |
|
271 #endif /* EAX_MODE */ |