Mercurial > dropbear
comparison libtomcrypt/src/modes/xts/xts_test.c @ 1471:6dba84798cd5
Update to libtomcrypt 1.18.1, merged with Dropbear changes
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Fri, 09 Feb 2018 21:44:05 +0800 |
parents | f849a5ca2efc |
children |
comparison
equal
deleted
inserted
replaced
1470:8bba51a55704 | 1471:6dba84798cd5 |
---|---|
3 * LibTomCrypt is a library that provides various cryptographic | 3 * LibTomCrypt is a library that provides various cryptographic |
4 * algorithms in a highly modular and flexible manner. | 4 * algorithms in a highly modular and flexible manner. |
5 * | 5 * |
6 * The library is free for all purposes without any express | 6 * The library is free for all purposes without any express |
7 * guarantee it works. | 7 * guarantee it works. |
8 * | |
9 * Tom St Denis, [email protected], http://libtom.org | |
10 */ | 8 */ |
11 #include "tomcrypt.h" | 9 #include "tomcrypt.h" |
12 | 10 |
13 #ifdef LTC_XTS_MODE | 11 #ifdef LTC_XTS_MODE |
14 | 12 |
15 /** | 13 #ifndef LTC_NO_TEST |
14 static int _xts_test_accel_xts_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long blocks, | |
15 unsigned char *tweak, symmetric_key *skey1, symmetric_key *skey2) | |
16 { | |
17 int ret; | |
18 symmetric_xts xts; | |
19 int (*orig)(const unsigned char *, unsigned char *, | |
20 unsigned long , unsigned char *, symmetric_key *, | |
21 symmetric_key *); | |
22 | |
23 /* AES can be under rijndael or aes... try to find it */ | |
24 if ((xts.cipher = find_cipher("aes")) == -1) { | |
25 if ((xts.cipher = find_cipher("rijndael")) == -1) { | |
26 return CRYPT_NOP; | |
27 } | |
28 } | |
29 orig = cipher_descriptor[xts.cipher].accel_xts_encrypt; | |
30 cipher_descriptor[xts.cipher].accel_xts_encrypt = NULL; | |
31 | |
32 XMEMCPY(&xts.key1, skey1, sizeof(symmetric_key)); | |
33 XMEMCPY(&xts.key2, skey2, sizeof(symmetric_key)); | |
34 | |
35 ret = xts_encrypt(pt, blocks << 4, ct, tweak, &xts); | |
36 cipher_descriptor[xts.cipher].accel_xts_encrypt = orig; | |
37 | |
38 return ret; | |
39 } | |
40 | |
41 static int _xts_test_accel_xts_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long blocks, | |
42 unsigned char *tweak, symmetric_key *skey1, symmetric_key *skey2) | |
43 { | |
44 int ret; | |
45 symmetric_xts xts; | |
46 int (*orig)(const unsigned char *, unsigned char *, | |
47 unsigned long , unsigned char *, symmetric_key *, | |
48 symmetric_key *); | |
49 | |
50 /* AES can be under rijndael or aes... try to find it */ | |
51 if ((xts.cipher = find_cipher("aes")) == -1) { | |
52 if ((xts.cipher = find_cipher("rijndael")) == -1) { | |
53 return CRYPT_NOP; | |
54 } | |
55 } | |
56 orig = cipher_descriptor[xts.cipher].accel_xts_decrypt; | |
57 cipher_descriptor[xts.cipher].accel_xts_decrypt = NULL; | |
58 | |
59 XMEMCPY(&xts.key1, skey1, sizeof(symmetric_key)); | |
60 XMEMCPY(&xts.key2, skey2, sizeof(symmetric_key)); | |
61 | |
62 ret = xts_decrypt(ct, blocks << 4, pt, tweak, &xts); | |
63 cipher_descriptor[xts.cipher].accel_xts_decrypt = orig; | |
64 | |
65 return ret; | |
66 } | |
67 #endif | |
68 | |
69 /** | |
16 Source donated by Elliptic Semiconductor Inc (www.ellipticsemi.com) to the LibTom Projects | 70 Source donated by Elliptic Semiconductor Inc (www.ellipticsemi.com) to the LibTom Projects |
71 | |
17 Returns CRYPT_OK upon success. | 72 Returns CRYPT_OK upon success. |
18 */ | 73 */ |
19 int xts_test(void) | 74 int xts_test(void) |
20 { | 75 { |
21 #ifdef LTC_NO_TEST | 76 #ifdef LTC_NO_TEST |
22 return CRYPT_NOP; | 77 return CRYPT_NOP; |
23 #else | 78 #else |
24 static const struct { | 79 static const struct |
80 { | |
25 int keylen; | 81 int keylen; |
26 unsigned char key1[32]; | 82 unsigned char key1[32]; |
27 unsigned char key2[32]; | 83 unsigned char key2[32]; |
28 ulong64 seqnum; | 84 ulong64 seqnum; |
29 unsigned long PTLEN; | 85 unsigned long PTLEN; |
140 { 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 }, | 196 { 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 }, |
141 { 0xd0,0x5b,0xc0,0x90,0xa8,0xe0,0x4f,0x1b,0x3d,0x3e,0xcd,0xd5,0xba,0xec,0x0f,0xd4,0xed,0xbf,0x9d,0xac,0xe4,0x5d,0x6f,0x6a,0x73,0x06,0xe6,0x4b,0xe5,0xdd,0x82 }, | 197 { 0xd0,0x5b,0xc0,0x90,0xa8,0xe0,0x4f,0x1b,0x3d,0x3e,0xcd,0xd5,0xba,0xec,0x0f,0xd4,0xed,0xbf,0x9d,0xac,0xe4,0x5d,0x6f,0x6a,0x73,0x06,0xe6,0x4b,0xe5,0xdd,0x82 }, |
142 }, | 198 }, |
143 | 199 |
144 }; | 200 }; |
145 unsigned char OUT[512], T[16]; | 201 unsigned char OUT[512], Torg[16], T[16]; |
146 ulong64 seq; | 202 ulong64 seq; |
147 symmetric_xts xts; | 203 symmetric_xts xts; |
148 int i, err, idx; | 204 int i, j, k, err, idx; |
149 | 205 unsigned long len; |
150 /* AES can be under rijndael or aes... try to find it */ | 206 |
207 /* AES can be under rijndael or aes... try to find it */ | |
151 if ((idx = find_cipher("aes")) == -1) { | 208 if ((idx = find_cipher("aes")) == -1) { |
152 if ((idx = find_cipher("rijndael")) == -1) { | 209 if ((idx = find_cipher("rijndael")) == -1) { |
153 return CRYPT_NOP; | 210 return CRYPT_NOP; |
154 } | 211 } |
155 } | 212 } |
156 | 213 for (k = 0; k < 4; ++k) { |
157 for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) { | 214 cipher_descriptor[idx].accel_xts_encrypt = NULL; |
158 err = xts_start(idx, tests[i].key1, tests[i].key2, tests[i].keylen/2, 0, &xts); | 215 cipher_descriptor[idx].accel_xts_decrypt = NULL; |
159 if (err != CRYPT_OK) { | 216 if (k & 0x1) { |
160 return err; | 217 cipher_descriptor[idx].accel_xts_encrypt = _xts_test_accel_xts_encrypt; |
161 } | 218 } |
162 | 219 if (k & 0x2) { |
163 seq = tests[i].seqnum; | 220 cipher_descriptor[idx].accel_xts_decrypt = _xts_test_accel_xts_decrypt; |
164 STORE64L(seq,T); | 221 } |
165 XMEMSET(T+8, 0, 8); | 222 for (j = 0; j < 2; j++) { |
166 | 223 for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) { |
167 err = xts_encrypt(tests[i].PTX, tests[i].PTLEN, OUT, T, &xts); | 224 /* skip the cases where |
168 if (err != CRYPT_OK) { | 225 * the length is smaller than 2*blocklen |
169 xts_done(&xts); | 226 * or the length is not a multiple of 32 |
170 return err; | 227 */ |
171 } | 228 if ((j == 1) && ((tests[i].PTLEN < 32) || (tests[i].PTLEN % 32))) { |
172 | 229 continue; |
173 if (XMEMCMP(OUT, tests[i].CTX, tests[i].PTLEN)) { | 230 } |
174 xts_done(&xts); | 231 if ((k > 0) && (j == 1)) { |
175 return CRYPT_FAIL_TESTVECTOR; | 232 continue; |
176 } | 233 } |
177 | 234 len = tests[i].PTLEN / 2; |
178 err = xts_decrypt(tests[i].CTX, tests[i].PTLEN, OUT, T, &xts); | 235 |
179 if (err != CRYPT_OK) { | 236 err = xts_start(idx, tests[i].key1, tests[i].key2, tests[i].keylen / 2, 0, &xts); |
180 xts_done(&xts); | 237 if (err != CRYPT_OK) { |
181 return err; | 238 return err; |
182 } | 239 } |
183 | 240 |
184 if (XMEMCMP(OUT, tests[i].PTX, tests[i].PTLEN)) { | 241 seq = tests[i].seqnum; |
185 xts_done(&xts); | 242 STORE64L(seq, Torg); |
186 return CRYPT_FAIL_TESTVECTOR; | 243 XMEMSET(Torg + 8, 0, 8); |
187 } | 244 |
188 xts_done(&xts); | 245 XMEMCPY(T, Torg, sizeof(T)); |
246 if (j == 0) { | |
247 err = xts_encrypt(tests[i].PTX, tests[i].PTLEN, OUT, T, &xts); | |
248 if (err != CRYPT_OK) { | |
249 xts_done(&xts); | |
250 return err; | |
251 } | |
252 } else { | |
253 err = xts_encrypt(tests[i].PTX, len, OUT, T, &xts); | |
254 if (err != CRYPT_OK) { | |
255 xts_done(&xts); | |
256 return err; | |
257 } | |
258 err = xts_encrypt(&tests[i].PTX[len], len, &OUT[len], T, &xts); | |
259 if (err != CRYPT_OK) { | |
260 xts_done(&xts); | |
261 return err; | |
262 } | |
263 } | |
264 | |
265 if (compare_testvector(OUT, tests[i].PTLEN, tests[i].CTX, tests[i].PTLEN, "XTS encrypt", i)) { | |
266 xts_done(&xts); | |
267 return CRYPT_FAIL_TESTVECTOR; | |
268 } | |
269 | |
270 XMEMCPY(T, Torg, sizeof(T)); | |
271 if (j == 0) { | |
272 err = xts_decrypt(tests[i].CTX, tests[i].PTLEN, OUT, T, &xts); | |
273 if (err != CRYPT_OK) { | |
274 xts_done(&xts); | |
275 return err; | |
276 } | |
277 } else { | |
278 err = xts_decrypt(tests[i].CTX, len, OUT, T, &xts); | |
279 if (err != CRYPT_OK) { | |
280 xts_done(&xts); | |
281 return err; | |
282 } | |
283 err = xts_decrypt(&tests[i].CTX[len], len, &OUT[len], T, &xts); | |
284 if (err != CRYPT_OK) { | |
285 xts_done(&xts); | |
286 return err; | |
287 } | |
288 } | |
289 | |
290 if (compare_testvector(OUT, tests[i].PTLEN, tests[i].PTX, tests[i].PTLEN, "XTS decrypt", i)) { | |
291 xts_done(&xts); | |
292 return CRYPT_FAIL_TESTVECTOR; | |
293 } | |
294 xts_done(&xts); | |
295 } | |
296 } | |
189 } | 297 } |
190 return CRYPT_OK; | 298 return CRYPT_OK; |
191 #endif | 299 #endif |
192 } | 300 } |
193 | 301 |
194 #endif | 302 #endif |
195 | 303 |
196 /* $Source$ */ | 304 /* ref: $Format:%D$ */ |
197 /* $Revision$ */ | 305 /* git commit: $Format:%H$ */ |
198 /* $Date$ */ | 306 /* commit time: $Format:%ai$ */ |
199 |