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