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