1 /*
2 Copyright (c) 2006, 2014, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; version 2 of the License.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program; see the file COPYING. If not, write to the
15 Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
16 MA 02110-1335 USA.
17 */
18
19 // test.cpp
20 // test taocrypt functionality
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <string.h>
27 #include <stdio.h>
28
29 #include "runtime.hpp"
30 #include "sha.hpp"
31 #include "md5.hpp"
32 #include "md2.hpp"
33 #include "md4.hpp"
34 #include "ripemd.hpp"
35 #include "hmac.hpp"
36 #include "arc4.hpp"
37 #include "des.hpp"
38 #include "rsa.hpp"
39 #include "dsa.hpp"
40 #include "aes.hpp"
41 #include "twofish.hpp"
42 #include "blowfish.hpp"
43 #include "asn.hpp"
44 #include "dh.hpp"
45 #include "coding.hpp"
46 #include "random.hpp"
47 #include "pwdbased.hpp"
48 #include "rabbit.hpp"
49 #include "hc128.hpp"
50
51
52
53 using TaoCrypt::byte;
54 using TaoCrypt::word32;
55 using TaoCrypt::SHA;
56 using TaoCrypt::SHA256;
57 using TaoCrypt::SHA224;
58 #ifdef WORD64_AVAILABLE
59 using TaoCrypt::SHA512;
60 using TaoCrypt::SHA384;
61 #endif
62 using TaoCrypt::MD5;
63 using TaoCrypt::MD2;
64 using TaoCrypt::MD4;
65 using TaoCrypt::RIPEMD160;
66 using TaoCrypt::HMAC;
67 using TaoCrypt::ARC4;
68 using TaoCrypt::DES_EDE3_CBC_Encryption;
69 using TaoCrypt::DES_EDE3_CBC_Decryption;
70 using TaoCrypt::DES_CBC_Encryption;
71 using TaoCrypt::DES_CBC_Decryption;
72 using TaoCrypt::DES_ECB_Encryption;
73 using TaoCrypt::DES_ECB_Decryption;
74 using TaoCrypt::AES_CBC_Encryption;
75 using TaoCrypt::AES_CBC_Decryption;
76 using TaoCrypt::AES_ECB_Encryption;
77 using TaoCrypt::AES_ECB_Decryption;
78 using TaoCrypt::Twofish_CBC_Encryption;
79 using TaoCrypt::Twofish_CBC_Decryption;
80 using TaoCrypt::Twofish_ECB_Encryption;
81 using TaoCrypt::Twofish_ECB_Decryption;
82 using TaoCrypt::Blowfish_CBC_Encryption;
83 using TaoCrypt::Blowfish_CBC_Decryption;
84 using TaoCrypt::Blowfish_ECB_Encryption;
85 using TaoCrypt::Blowfish_ECB_Decryption;
86 using TaoCrypt::RSA_PrivateKey;
87 using TaoCrypt::RSA_PublicKey;
88 using TaoCrypt::DSA_PrivateKey;
89 using TaoCrypt::DSA_PublicKey;
90 using TaoCrypt::DSA_Signer;
91 using TaoCrypt::DSA_Verifier;
92 using TaoCrypt::RSAES_Encryptor;
93 using TaoCrypt::RSAES_Decryptor;
94 using TaoCrypt::Source;
95 using TaoCrypt::FileSource;
96 using TaoCrypt::FileSource;
97 using TaoCrypt::HexDecoder;
98 using TaoCrypt::HexEncoder;
99 using TaoCrypt::Base64Decoder;
100 using TaoCrypt::Base64Encoder;
101 using TaoCrypt::CertDecoder;
102 using TaoCrypt::DH;
103 using TaoCrypt::EncodeDSA_Signature;
104 using TaoCrypt::DecodeDSA_Signature;
105 using TaoCrypt::PBKDF2_HMAC;
106 using TaoCrypt::tcArrayDelete;
107 using TaoCrypt::GetCert;
108 using TaoCrypt::GetPKCS_Cert;
109 using TaoCrypt::Rabbit;
110 using TaoCrypt::HC128;
111
112 struct testVector {
113 byte* input_;
114 byte* output_;
115 word32 inLen_;
116 word32 outLen_;
117
testVectortestVector118 testVector(const char* in, const char* out) : input_((byte*)in),
119 output_((byte*)out), inLen_((word32)strlen(in)),
120 outLen_((word32)strlen(out)) {}
121 };
122
123 int sha_test();
124 int sha256_test();
125 #ifdef WORD64_AVAILABLE
126 int sha512_test();
127 int sha384_test();
128 #endif
129 int sha224_test();
130 int md5_test();
131 int md2_test();
132 int md4_test();
133 int ripemd_test();
134 int hmac_test();
135 int arc4_test();
136 int des_test();
137 int aes_test();
138 int twofish_test();
139 int blowfish_test();
140 int rsa_test();
141 int dsa_test();
142 int dh_test();
143 int pwdbased_test();
144 int pkcs12_test();
145 int rabbit_test();
146 int hc128_test();
147
148 TaoCrypt::RandomNumberGenerator rng;
149
150
err_sys(const char * msg,int es)151 void err_sys(const char* msg, int es)
152 {
153 printf("%s\n", msg);
154 exit(es);
155 }
156
157 // func_args from test.hpp, so don't have to pull in other junk
158 struct func_args {
159 int argc;
160 char** argv;
161 int return_code;
162 };
163
164
165 /*
166 DES, AES, Blowfish, and Twofish need aligned (4 byte) input/output for
167 processing, can turn this off by setting gpBlock(assumeAligned = false)
168 but would hurt performance. yaSSL always uses dynamic memory so we have
169 at least 8 byte alignment. This test tried to force alignment for stack
170 variables (for convenience) but some compiler versions and optimizations
171 seemed to be off. So we have msgTmp variable which we copy into dynamic
172 memory at runtime to ensure proper alignment, along with plain/cipher.
173 Whew!
174 */
175 const byte msgTmp[] = { // "now is the time for all " w/o trailing 0
176 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
177 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
178 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
179 };
180
181 byte* msg = 0; // for block cipher input
182 byte* plain = 0; // for cipher decrypt comparison
183 byte* cipher = 0; // block output
184
185
taocrypt_test(void * args)186 void taocrypt_test(void* args)
187 {
188 ((func_args*)args)->return_code = -1; // error state
189
190 msg = NEW_TC byte[24];
191 plain = NEW_TC byte[24];
192 cipher = NEW_TC byte[24];
193
194 memcpy(msg, msgTmp, 24);
195
196 int ret = 0;
197 if ( (ret = sha_test()) )
198 err_sys("SHA test failed!\n", ret);
199 else
200 printf( "SHA test passed!\n");
201
202 if ( (ret = sha256_test()) )
203 err_sys("SHA-256 test failed!\n", ret);
204 else
205 printf( "SHA-256 test passed!\n");
206
207 if ( (ret = sha224_test()) )
208 err_sys("SHA-224 test failed!\n", ret);
209 else
210 printf( "SHA-224 test passed!\n");
211
212 #ifdef WORD64_AVAILABLE
213
214 if ( (ret = sha512_test()) )
215 err_sys("SHA-512 test failed!\n", ret);
216 else
217 printf( "SHA-512 test passed!\n");
218
219 if ( (ret = sha384_test()) )
220 err_sys("SHA-384 test failed!\n", ret);
221 else
222 printf( "SHA-384 test passed!\n");
223
224 #endif
225
226 if ( (ret = md5_test()) )
227 err_sys("MD5 test failed!\n", ret);
228 else
229 printf( "MD5 test passed!\n");
230
231 if ( (ret = md2_test()) )
232 err_sys("MD2 test failed!\n", ret);
233 else
234 printf( "MD2 test passed!\n");
235
236 if ( (ret = md4_test()) )
237 err_sys("MD4 test failed!\n", ret);
238 else
239 printf( "MD4 test passed!\n");
240
241 if ( (ret = ripemd_test()) )
242 err_sys("RIPEMD test failed!\n", ret);
243 else
244 printf( "RIPEMD test passed!\n");
245
246 if ( ( ret = hmac_test()) )
247 err_sys("HMAC test failed!\n", ret);
248 else
249 printf( "HMAC test passed!\n");
250
251 if ( (ret = arc4_test()) )
252 err_sys("ARC4 test failed!\n", ret);
253 else
254 printf( "ARC4 test passed!\n");
255
256 if ( (ret = rabbit_test()) )
257 err_sys("Rabbit test failed!\n", ret);
258 else
259 printf( "Rabbit test passed!\n");
260
261 if ( (ret = hc128_test()) )
262 err_sys("HC128 test failed!\n", ret);
263 else
264 printf( "HC128 test passed!\n");
265
266 if ( (ret = des_test()) )
267 err_sys("DES test failed!\n", ret);
268 else
269 printf( "DES test passed!\n");
270
271 if ( (ret = aes_test()) )
272 err_sys("AES test failed!\n", ret);
273 else
274 printf( "AES test passed!\n");
275
276 if ( (ret = twofish_test()) )
277 err_sys("Twofish test failed!\n", ret);
278 else
279 printf( "Twofish test passed!\n");
280
281 if ( (ret = blowfish_test()) )
282 err_sys("Blowfish test failed!\n", ret);
283 else
284 printf( "Blowfish test passed!\n");
285
286 if ( (ret = rsa_test()) )
287 err_sys("RSA test failed!\n", ret);
288 else
289 printf( "RSA test passed!\n");
290
291 if ( (ret = dh_test()) )
292 err_sys("DH test failed!\n", ret);
293 else
294 printf( "DH test passed!\n");
295
296 if ( (ret = dsa_test()) )
297 err_sys("DSA test failed!\n", ret);
298 else
299 printf( "DSA test passed!\n");
300
301 if ( (ret = pwdbased_test()) )
302 err_sys("PBKDF2 test failed!\n", ret);
303 else
304 printf( "PBKDF2 test passed!\n");
305
306 /* not ready yet
307 if ( (ret = pkcs12_test()) )
308 err_sys("PKCS12 test failed!\n", ret);
309 else
310 printf( "PKCS12 test passed!\n");
311 */
312
313 tcArrayDelete(cipher);
314 tcArrayDelete(plain);
315 tcArrayDelete(msg);
316
317 ((func_args*)args)->return_code = ret;
318 }
319
320
321 // so overall tests can pull in test function
322 #ifndef NO_MAIN_DRIVER
323
main(int argc,char ** argv)324 int main(int argc, char** argv)
325 {
326 func_args args;
327
328 args.argc = argc;
329 args.argv = argv;
330
331 taocrypt_test(&args);
332 TaoCrypt::CleanUp();
333
334 return args.return_code;
335 }
336
337 #endif // NO_MAIN_DRIVER
338
339
file_test(const char * file,byte * check)340 void file_test(const char* file, byte* check)
341 {
342 FILE* f;
343 int i = 0;
344 MD5 md5;
345 byte buf[1024];
346 byte md5sum[MD5::DIGEST_SIZE];
347
348 if( !( f = fopen( file, "rb" ) )) {
349 printf("Can't open %s\n", file);
350 return;
351 }
352 while( ( i = (int)fread(buf, 1, sizeof(buf), f )) > 0 )
353 md5.Update(buf, i);
354
355 md5.Final(md5sum);
356 memcpy(check, md5sum, sizeof(md5sum));
357
358 for(int j = 0; j < MD5::DIGEST_SIZE; ++j )
359 printf( "%02x", md5sum[j] );
360
361 printf(" %s\n", file);
362
363 fclose(f);
364 }
365
366
sha_test()367 int sha_test()
368 {
369 SHA sha;
370 byte hash[SHA::DIGEST_SIZE];
371
372 testVector test_sha[] =
373 {
374 testVector("abc",
375 "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
376 "\x6C\x9C\xD0\xD8\x9D"),
377 testVector("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
378 "\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE\x4A\xA1\xF9\x51\x29"
379 "\xE5\xE5\x46\x70\xF1"),
380 testVector("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
381 "aaaaaa",
382 "\x00\x98\xBA\x82\x4B\x5C\x16\x42\x7B\xD7\xA1\x12\x2A\x5A\x44"
383 "\x2A\x25\xEC\x64\x4D"),
384 testVector("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
385 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
386 "aaaaaaaaaa",
387 "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
388 "\x53\x99\x5E\x26\xA0")
389 };
390
391 int times( sizeof(test_sha) / sizeof(testVector) );
392 for (int i = 0; i < times; ++i) {
393 sha.Update(test_sha[i].input_, test_sha[i].inLen_);
394 sha.Final(hash);
395
396 if (memcmp(hash, test_sha[i].output_, SHA::DIGEST_SIZE) != 0)
397 return -1 - i;
398 }
399
400 return 0;
401 }
402
403
sha256_test()404 int sha256_test()
405 {
406 SHA256 sha;
407 byte hash[SHA256::DIGEST_SIZE];
408
409 testVector test_sha[] =
410 {
411 testVector("abc",
412 "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
413 "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
414 "\x15\xAD"),
415 testVector("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
416 "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
417 "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
418 "\x06\xC1")
419 };
420
421 int times( sizeof(test_sha) / sizeof(testVector) );
422 for (int i = 0; i < times; ++i) {
423 sha.Update(test_sha[i].input_, test_sha[i].inLen_);
424 sha.Final(hash);
425
426 if (memcmp(hash, test_sha[i].output_, SHA256::DIGEST_SIZE) != 0)
427 return -1 - i;
428 }
429
430 return 0;
431 }
432
433
434 #ifdef WORD64_AVAILABLE
435
sha512_test()436 int sha512_test()
437 {
438 SHA512 sha;
439 byte hash[SHA512::DIGEST_SIZE];
440
441 testVector test_sha[] =
442 {
443 testVector("abc",
444 "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
445 "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
446 "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
447 "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
448 "\xa5\x4c\xa4\x9f"),
449 testVector("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
450 "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
451 "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
452 "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
453 "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
454 "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
455 "\x87\x4b\xe9\x09")
456 };
457
458 int times( sizeof(test_sha) / sizeof(testVector) );
459 for (int i = 0; i < times; ++i) {
460 sha.Update(test_sha[i].input_, test_sha[i].inLen_);
461 sha.Final(hash);
462
463 if (memcmp(hash, test_sha[i].output_, SHA512::DIGEST_SIZE) != 0)
464 return -1 - i;
465 }
466
467 return 0;
468 }
469
470
sha384_test()471 int sha384_test()
472 {
473 SHA384 sha;
474 byte hash[SHA384::DIGEST_SIZE];
475
476 testVector test_sha[] =
477 {
478 testVector("abc",
479 "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
480 "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
481 "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
482 "\xc8\x25\xa7"),
483 testVector("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
484 "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
485 "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
486 "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
487 "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
488 "\x74\x60\x39")
489 };
490
491 int times( sizeof(test_sha) / sizeof(testVector) );
492 for (int i = 0; i < times; ++i) {
493 sha.Update(test_sha[i].input_, test_sha[i].inLen_);
494 sha.Final(hash);
495
496 if (memcmp(hash, test_sha[i].output_, SHA384::DIGEST_SIZE) != 0)
497 return -1 - i;
498 }
499
500 return 0;
501 }
502
503 #endif // WORD64_AVAILABLE
504
505
sha224_test()506 int sha224_test()
507 {
508 SHA224 sha;
509 byte hash[SHA224::DIGEST_SIZE];
510
511 testVector test_sha[] =
512 {
513 testVector("abc",
514 "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2\x55"
515 "\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7"),
516 testVector("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
517 "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
518 "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25")
519 };
520
521 int times( sizeof(test_sha) / sizeof(testVector) );
522 for (int i = 0; i < times; ++i) {
523 sha.Update(test_sha[i].input_, test_sha[i].inLen_);
524 sha.Final(hash);
525
526 if (memcmp(hash, test_sha[i].output_, SHA224::DIGEST_SIZE) != 0)
527 return -1 - i;
528 }
529
530 return 0;
531 }
532
533
md5_test()534 int md5_test()
535 {
536 MD5 md5;
537 byte hash[MD5::DIGEST_SIZE];
538
539 testVector test_md5[] =
540 {
541 testVector("abc",
542 "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
543 "\x72"),
544 testVector("message digest",
545 "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61"
546 "\xd0"),
547 testVector("abcdefghijklmnopqrstuvwxyz",
548 "\xc3\xfc\xd3\xd7\x61\x92\xe4\x00\x7d\xfb\x49\x6c\xca\x67\xe1"
549 "\x3b"),
550 testVector("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
551 "6789",
552 "\xd1\x74\xab\x98\xd2\x77\xd9\xf5\xa5\x61\x1c\x2c\x9f\x41\x9d"
553 "\x9f"),
554 testVector("1234567890123456789012345678901234567890123456789012345678"
555 "9012345678901234567890",
556 "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
557 "\x7a")
558 };
559
560 int times( sizeof(test_md5) / sizeof(testVector) );
561 for (int i = 0; i < times; ++i) {
562 md5.Update(test_md5[i].input_, test_md5[i].inLen_);
563 md5.Final(hash);
564
565 if (memcmp(hash, test_md5[i].output_, MD5::DIGEST_SIZE) != 0)
566 return -5 - i;
567 }
568
569 return 0;
570 }
571
572
md4_test()573 int md4_test()
574 {
575 MD4 md4;
576 byte hash[MD4::DIGEST_SIZE];
577
578 testVector test_md4[] =
579 {
580 testVector("",
581 "\x31\xd6\xcf\xe0\xd1\x6a\xe9\x31\xb7\x3c\x59\xd7\xe0\xc0\x89"
582 "\xc0"),
583 testVector("a",
584 "\xbd\xe5\x2c\xb3\x1d\xe3\x3e\x46\x24\x5e\x05\xfb\xdb\xd6\xfb"
585 "\x24"),
586 testVector("abc",
587 "\xa4\x48\x01\x7a\xaf\x21\xd8\x52\x5f\xc1\x0a\xe8\x7a\xa6\x72"
588 "\x9d"),
589 testVector("message digest",
590 "\xd9\x13\x0a\x81\x64\x54\x9f\xe8\x18\x87\x48\x06\xe1\xc7\x01"
591 "\x4b"),
592 testVector("abcdefghijklmnopqrstuvwxyz",
593 "\xd7\x9e\x1c\x30\x8a\xa5\xbb\xcd\xee\xa8\xed\x63\xdf\x41\x2d"
594 "\xa9"),
595 testVector("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
596 "6789",
597 "\x04\x3f\x85\x82\xf2\x41\xdb\x35\x1c\xe6\x27\xe1\x53\xe7\xf0"
598 "\xe4"),
599 testVector("1234567890123456789012345678901234567890123456789012345678"
600 "9012345678901234567890",
601 "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f\xcc\x05"
602 "\x36")
603 };
604
605 int times( sizeof(test_md4) / sizeof(testVector) );
606 for (int i = 0; i < times; ++i) {
607 md4.Update(test_md4[i].input_, test_md4[i].inLen_);
608 md4.Final(hash);
609
610 if (memcmp(hash, test_md4[i].output_, MD4::DIGEST_SIZE) != 0)
611 return -5 - i;
612 }
613
614 return 0;
615 }
616
617
md2_test()618 int md2_test()
619 {
620 MD2 md5;
621 byte hash[MD2::DIGEST_SIZE];
622
623 testVector test_md2[] =
624 {
625 testVector("",
626 "\x83\x50\xe5\xa3\xe2\x4c\x15\x3d\xf2\x27\x5c\x9f\x80\x69"
627 "\x27\x73"),
628 testVector("a",
629 "\x32\xec\x01\xec\x4a\x6d\xac\x72\xc0\xab\x96\xfb\x34\xc0"
630 "\xb5\xd1"),
631 testVector("abc",
632 "\xda\x85\x3b\x0d\x3f\x88\xd9\x9b\x30\x28\x3a\x69\xe6\xde"
633 "\xd6\xbb"),
634 testVector("message digest",
635 "\xab\x4f\x49\x6b\xfb\x2a\x53\x0b\x21\x9f\xf3\x30\x31\xfe"
636 "\x06\xb0"),
637 testVector("abcdefghijklmnopqrstuvwxyz",
638 "\x4e\x8d\xdf\xf3\x65\x02\x92\xab\x5a\x41\x08\xc3\xaa\x47"
639 "\x94\x0b"),
640 testVector("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
641 "0123456789",
642 "\xda\x33\xde\xf2\xa4\x2d\xf1\x39\x75\x35\x28\x46\xc3\x03"
643 "\x38\xcd"),
644 testVector("12345678901234567890123456789012345678901234567890123456"
645 "789012345678901234567890",
646 "\xd5\x97\x6f\x79\xd8\x3d\x3a\x0d\xc9\x80\x6c\x3c\x66\xf3"
647 "\xef\xd8")
648 };
649
650 int times( sizeof(test_md2) / sizeof(testVector) );
651 for (int i = 0; i < times; ++i) {
652 md5.Update(test_md2[i].input_, test_md2[i].inLen_);
653 md5.Final(hash);
654
655 if (memcmp(hash, test_md2[i].output_, MD2::DIGEST_SIZE) != 0)
656 return -10 - i;
657 }
658
659 return 0;
660 }
661
662
ripemd_test()663 int ripemd_test()
664 {
665 RIPEMD160 ripe160;
666 byte hash[RIPEMD160::DIGEST_SIZE];
667
668 testVector test_ripemd[] =
669 {
670 testVector("",
671 "\x9c\x11\x85\xa5\xc5\xe9\xfc\x54\x61\x28\x08\x97\x7e\xe8"
672 "\xf5\x48\xb2\x25\x8d\x31"),
673 testVector("a",
674 "\x0b\xdc\x9d\x2d\x25\x6b\x3e\xe9\xda\xae\x34\x7b\xe6\xf4"
675 "\xdc\x83\x5a\x46\x7f\xfe"),
676 testVector("abc",
677 "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
678 "\xb0\x87\xf1\x5a\x0b\xfc"),
679 testVector("message digest",
680 "\x5d\x06\x89\xef\x49\xd2\xfa\xe5\x72\xb8\x81\xb1\x23\xa8"
681 "\x5f\xfa\x21\x59\x5f\x36"),
682 testVector("abcdefghijklmnopqrstuvwxyz",
683 "\xf7\x1c\x27\x10\x9c\x69\x2c\x1b\x56\xbb\xdc\xeb\x5b\x9d"
684 "\x28\x65\xb3\x70\x8d\xbc"),
685 testVector("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
686 "\x12\xa0\x53\x38\x4a\x9c\x0c\x88\xe4\x05\xa0\x6c\x27\xdc"
687 "\xf4\x9a\xda\x62\xeb\x2b"),
688 testVector("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123"
689 "456789",
690 "\xb0\xe2\x0b\x6e\x31\x16\x64\x02\x86\xed\x3a\x87\xa5\x71"
691 "\x30\x79\xb2\x1f\x51\x89"),
692 testVector("12345678901234567890123456789012345678901234567890123456"
693 "789012345678901234567890",
694 "\x9b\x75\x2e\x45\x57\x3d\x4b\x39\xf4\xdb\xd3\x32\x3c\xab"
695 "\x82\xbf\x63\x32\x6b\xfb"),
696 };
697
698 int times( sizeof(test_ripemd) / sizeof(testVector) );
699 for (int i = 0; i < times; ++i) {
700 ripe160.Update(test_ripemd[i].input_, test_ripemd[i].inLen_);
701 ripe160.Final(hash);
702
703 if (memcmp(hash, test_ripemd[i].output_, RIPEMD160::DIGEST_SIZE) != 0)
704 return -100 - i;
705 }
706
707 return 0;
708 }
709
710
hmac_test()711 int hmac_test()
712 {
713 HMAC<MD5> hmacMD5;
714 byte hash[MD5::DIGEST_SIZE];
715
716 const char* keys[]=
717 {
718 "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
719 "Jefe",
720 "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
721 };
722
723 testVector test_hmacMD5[] =
724 {
725 testVector("Hi There",
726 "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
727 "\x9d"),
728 testVector("what do ya want for nothing?",
729 "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7"
730 "\x38"),
731 testVector("\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
732 "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
733 "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
734 "\xDD\xDD\xDD\xDD\xDD\xDD",
735 "\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3"
736 "\xf6")
737 };
738
739 int times( sizeof(test_hmacMD5) / sizeof(testVector) );
740 for (int i = 0; i < times; ++i) {
741 hmacMD5.SetKey((byte*)keys[i], (word32)strlen(keys[i]));
742 hmacMD5.Update(test_hmacMD5[i].input_, test_hmacMD5[i].inLen_);
743 hmacMD5.Final(hash);
744
745 if (memcmp(hash, test_hmacMD5[i].output_, MD5::DIGEST_SIZE) != 0)
746 return -20 - i;
747 }
748
749 return 0;
750 }
751
752
arc4_test()753 int arc4_test()
754 {
755 byte cipher[16];
756 byte plain[16];
757
758 const char* keys[] =
759 {
760 "\x01\x23\x45\x67\x89\xab\xcd\xef",
761 "\x01\x23\x45\x67\x89\xab\xcd\xef",
762 "\x00\x00\x00\x00\x00\x00\x00\x00",
763 "\xef\x01\x23\x45"
764 };
765
766 testVector test_arc4[] =
767 {
768 testVector("\x01\x23\x45\x67\x89\xab\xcd\xef",
769 "\x75\xb7\x87\x80\x99\xe0\xc5\x96"),
770 testVector("\x00\x00\x00\x00\x00\x00\x00\x00",
771 "\x74\x94\xc2\xe7\x10\x4b\x08\x79"),
772 testVector("\x00\x00\x00\x00\x00\x00\x00\x00",
773 "\xde\x18\x89\x41\xa3\x37\x5d\x3a"),
774 testVector("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
775 "\xd6\xa1\x41\xa7\xec\x3c\x38\xdf\xbd\x61")
776 };
777
778
779 int times( sizeof(test_arc4) / sizeof(testVector) );
780 for (int i = 0; i < times; ++i) {
781 ARC4::Encryption enc;
782 ARC4::Decryption dec;
783
784 enc.SetKey((byte*)keys[i], (word32)strlen(keys[i]));
785 dec.SetKey((byte*)keys[i], (word32)strlen(keys[i]));
786
787 enc.Process(cipher, test_arc4[i].input_, test_arc4[i].outLen_);
788 dec.Process(plain, cipher, test_arc4[i].outLen_);
789
790 if (memcmp(plain, test_arc4[i].input_, test_arc4[i].outLen_))
791 return -30 - i;
792
793 if (memcmp(cipher, test_arc4[i].output_, test_arc4[i].outLen_))
794 return -40 - i;
795 }
796
797 return 0;
798 }
799
800
rabbit_test()801 int rabbit_test()
802 {
803 byte cipher[16];
804 byte plain[16];
805
806 const char* keys[] =
807 {
808 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
809 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
810 "\xAC\xC3\x51\xDC\xF1\x62\xFC\x3B\xFE\x36\x3D\x2E\x29\x13\x28\x91"
811 };
812
813 const char* ivs[] =
814 {
815 "\x00\x00\x00\x00\x00\x00\x00\x00",
816 "\x59\x7E\x26\xC1\x75\xF5\x73\xC3",
817 0
818 };
819
820
821 testVector test_rabbit[] =
822 {
823 testVector("\x00\x00\x00\x00\x00\x00\x00\x00",
824 "\xED\xB7\x05\x67\x37\x5D\xCD\x7C"),
825 testVector("\x00\x00\x00\x00\x00\x00\x00\x00",
826 "\x6D\x7D\x01\x22\x92\xCC\xDC\xE0"),
827 testVector("\x00\x00\x00\x00\x00\x00\x00\x00",
828 "\x9C\x51\xE2\x87\x84\xC3\x7F\xE9")
829 };
830
831
832 int times( sizeof(test_rabbit) / sizeof(testVector) );
833 for (int i = 0; i < times; ++i) {
834 Rabbit::Encryption enc;
835 Rabbit::Decryption dec;
836
837 enc.SetKey((byte*)keys[i], (byte*)ivs[i]);
838 dec.SetKey((byte*)keys[i], (byte*)ivs[i]);
839
840 enc.Process(cipher, test_rabbit[i].input_, test_rabbit[i].outLen_);
841 dec.Process(plain, cipher, test_rabbit[i].outLen_);
842
843 if (memcmp(plain, test_rabbit[i].input_, test_rabbit[i].outLen_))
844 return -230 - i;
845
846 if (memcmp(cipher, test_rabbit[i].output_, test_rabbit[i].outLen_))
847 return -240 - i;
848 }
849
850 return 0;
851 }
852
853
hc128_test()854 int hc128_test()
855 {
856 byte cipher[16];
857 byte plain[16];
858
859 const char* keys[] =
860 {
861 "\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
862 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
863 "\x00\x53\xA6\xF9\x4C\x9F\xF2\x45\x98\xEB\x3E\x91\xE4\x37\x8A\xDD",
864 "\x0F\x62\xB5\x08\x5B\xAE\x01\x54\xA7\xFA\x4D\xA0\xF3\x46\x99\xEC"
865 };
866
867 const char* ivs[] =
868 {
869 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
870 "\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
871 "\x0D\x74\xDB\x42\xA9\x10\x77\xDE\x45\xAC\x13\x7A\xE1\x48\xAF\x16",
872 "\x28\x8F\xF6\x5D\xC4\x2B\x92\xF9\x60\xC7\x2E\x95\xFC\x63\xCA\x31"
873 };
874
875 testVector test_hc128[] =
876 {
877 testVector("\x00\x00\x00\x00\x00\x00\x00\x00",
878 "\x37\x86\x02\xB9\x8F\x32\xA7\x48"),
879 testVector("\x00\x00\x00\x00\x00\x00\x00\x00",
880 "\x33\x7F\x86\x11\xC6\xED\x61\x5F"),
881 testVector("\x00\x00\x00\x00\x00\x00\x00\x00",
882 "\x2E\x1E\xD1\x2A\x85\x51\xC0\x5A"),
883 testVector("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
884 "\x1C\xD8\xAE\xDD\xFE\x52\xE2\x17\xE8\x35\xD0\xB7\xE8\x4E\x29")
885 };
886
887 int times( sizeof(test_hc128) / sizeof(testVector) );
888 for (int i = 0; i < times; ++i) {
889 HC128::Encryption enc;
890 HC128::Decryption dec;
891
892 enc.SetKey((byte*)keys[i], (byte*)ivs[i]);
893 dec.SetKey((byte*)keys[i], (byte*)ivs[i]);
894
895 enc.Process(cipher, test_hc128[i].input_, test_hc128[i].outLen_);
896 dec.Process(plain, cipher, test_hc128[i].outLen_);
897
898 if (memcmp(plain, test_hc128[i].input_, test_hc128[i].outLen_))
899 return -330 - i;
900
901 if (memcmp(cipher, test_hc128[i].output_, test_hc128[i].outLen_))
902 return -340 - i;
903 }
904
905 return 0;
906 }
907
908
des_test()909 int des_test()
910 {
911 //ECB mode
912 DES_ECB_Encryption enc;
913 DES_ECB_Decryption dec;
914
915 const int sz = TaoCrypt::DES_BLOCK_SIZE * 3;
916 const byte key[] = { 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef };
917 const byte iv[] = { 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef };
918
919 enc.SetKey(key, sizeof(key));
920 enc.Process(cipher, msg, sz);
921 dec.SetKey(key, sizeof(key));
922 dec.Process(plain, cipher, sz);
923
924 if (memcmp(plain, msg, sz))
925 return -50;
926
927 const byte verify1[] =
928 {
929 0xf9,0x99,0xb8,0x8e,0xaf,0xea,0x71,0x53,
930 0x6a,0x27,0x17,0x87,0xab,0x88,0x83,0xf9,
931 0x89,0x3d,0x51,0xec,0x4b,0x56,0x3b,0x53
932 };
933
934 if (memcmp(cipher, verify1, sz))
935 return -51;
936
937 // CBC mode
938 DES_CBC_Encryption enc2;
939 DES_CBC_Decryption dec2;
940
941 enc2.SetKey(key, sizeof(key), iv);
942 enc2.Process(cipher, msg, sz);
943 dec2.SetKey(key, sizeof(key), iv);
944 dec2.Process(plain, cipher, sz);
945
946 if (memcmp(plain, msg, sz))
947 return -52;
948
949 const byte verify2[] =
950 {
951 0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
952 0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
953 0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
954 };
955
956 if (memcmp(cipher, verify2, sz))
957 return -53;
958
959 // EDE3 CBC mode
960 DES_EDE3_CBC_Encryption enc3;
961 DES_EDE3_CBC_Decryption dec3;
962
963 const byte key3[] =
964 {
965 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
966 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
967 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
968 };
969 const byte iv3[] =
970 {
971 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
972 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
973 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
974
975 };
976
977 enc3.SetKey(key3, sizeof(key3), iv3);
978 enc3.Process(cipher, msg, sz);
979 dec3.SetKey(key3, sizeof(key3), iv3);
980 dec3.Process(plain, cipher, sz);
981
982 if (memcmp(plain, msg, sz))
983 return -54;
984
985 const byte verify3[] =
986 {
987 0x08,0x8a,0xae,0xe6,0x9a,0xa9,0xc1,0x13,
988 0x93,0x7d,0xf7,0x3a,0x11,0x56,0x66,0xb3,
989 0x18,0xbc,0xbb,0x6d,0xd2,0xb1,0x16,0xda
990 };
991
992 if (memcmp(cipher, verify3, sz))
993 return -55;
994
995 return 0;
996 }
997
998
aes_test()999 int aes_test()
1000 {
1001 AES_CBC_Encryption enc;
1002 AES_CBC_Decryption dec;
1003 const int bs(TaoCrypt::AES::BLOCK_SIZE);
1004
1005 byte key[] = "0123456789abcdef "; // align
1006 byte iv[] = "1234567890abcdef "; // align
1007
1008 enc.SetKey(key, bs, iv);
1009 dec.SetKey(key, bs, iv);
1010
1011 enc.Process(cipher, msg, bs);
1012 dec.Process(plain, cipher, bs);
1013
1014 if (memcmp(plain, msg, bs))
1015 return -60;
1016
1017 const byte verify[] =
1018 {
1019 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
1020 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
1021 };
1022
1023 if (memcmp(cipher, verify, bs))
1024 return -61;
1025
1026 AES_ECB_Encryption enc2;
1027 AES_ECB_Decryption dec2;
1028
1029 enc2.SetKey(key, bs, iv);
1030 dec2.SetKey(key, bs, iv);
1031
1032 enc2.Process(cipher, msg, bs);
1033 dec2.Process(plain, cipher, bs);
1034
1035 if (memcmp(plain, msg, bs))
1036 return -62;
1037
1038 const byte verify2[] =
1039 {
1040 0xd0,0xc9,0xd9,0xc9,0x40,0xe8,0x97,0xb6,
1041 0xc8,0x8c,0x33,0x3b,0xb5,0x8f,0x85,0xd1
1042 };
1043
1044 if (memcmp(cipher, verify2, bs))
1045 return -63;
1046
1047 return 0;
1048 }
1049
1050
twofish_test()1051 int twofish_test()
1052 {
1053 Twofish_CBC_Encryption enc;
1054 Twofish_CBC_Decryption dec;
1055 const int bs(TaoCrypt::Twofish::BLOCK_SIZE);
1056
1057 byte key[] = "0123456789abcdef "; // align
1058 byte iv[] = "1234567890abcdef "; // align
1059
1060 enc.SetKey(key, bs, iv);
1061 dec.SetKey(key, bs, iv);
1062
1063 enc.Process(cipher, msg, bs);
1064 dec.Process(plain, cipher, bs);
1065
1066 if (memcmp(plain, msg, bs))
1067 return -60;
1068
1069 const byte verify[] =
1070 {
1071 0xD2,0xD7,0x47,0x47,0x4A,0x65,0x4E,0x16,
1072 0x21,0x03,0x58,0x79,0x5F,0x02,0x27,0x2C
1073 };
1074
1075 if (memcmp(cipher, verify, bs))
1076 return -61;
1077
1078 Twofish_ECB_Encryption enc2;
1079 Twofish_ECB_Decryption dec2;
1080
1081 enc2.SetKey(key, bs, iv);
1082 dec2.SetKey(key, bs, iv);
1083
1084 enc2.Process(cipher, msg, bs);
1085 dec2.Process(plain, cipher, bs);
1086
1087 if (memcmp(plain, msg, bs))
1088 return -62;
1089
1090 const byte verify2[] =
1091 {
1092 0x3B,0x6C,0x63,0x10,0x34,0xAB,0xB2,0x87,
1093 0xC4,0xCD,0x6B,0x91,0x14,0xC5,0x3A,0x09
1094 };
1095
1096 if (memcmp(cipher, verify2, bs))
1097 return -63;
1098
1099 return 0;
1100 }
1101
1102
blowfish_test()1103 int blowfish_test()
1104 {
1105 Blowfish_CBC_Encryption enc;
1106 Blowfish_CBC_Decryption dec;
1107 const int bs(TaoCrypt::Blowfish::BLOCK_SIZE);
1108
1109 byte key[] = "0123456789abcdef "; // align
1110 byte iv[] = "1234567890abcdef "; // align
1111
1112 enc.SetKey(key, 16, iv);
1113 dec.SetKey(key, 16, iv);
1114
1115 enc.Process(cipher, msg, bs * 2);
1116 dec.Process(plain, cipher, bs * 2);
1117
1118 if (memcmp(plain, msg, bs))
1119 return -60;
1120
1121 const byte verify[] =
1122 {
1123 0x0E,0x26,0xAA,0x29,0x11,0x25,0xAB,0xB5,
1124 0xBC,0xD9,0x08,0xC4,0x94,0x6C,0x89,0xA3
1125 };
1126
1127 if (memcmp(cipher, verify, bs))
1128 return -61;
1129
1130 Blowfish_ECB_Encryption enc2;
1131 Blowfish_ECB_Decryption dec2;
1132
1133 enc2.SetKey(key, 16, iv);
1134 dec2.SetKey(key, 16, iv);
1135
1136 enc2.Process(cipher, msg, bs * 2);
1137 dec2.Process(plain, cipher, bs * 2);
1138
1139 if (memcmp(plain, msg, bs))
1140 return -62;
1141
1142 const byte verify2[] =
1143 {
1144 0xE7,0x42,0xB9,0x37,0xC8,0x7D,0x93,0xCA,
1145 0x8F,0xCE,0x39,0x32,0xDE,0xD7,0xBC,0x5B
1146 };
1147
1148 if (memcmp(cipher, verify2, bs))
1149 return -63;
1150
1151 return 0;
1152 }
1153
1154
rsa_test()1155 int rsa_test()
1156 {
1157 Source source;
1158 FileSource("../certs/client-key.der", source);
1159 if (source.size() == 0) {
1160 FileSource("../../certs/client-key.der", source); // for testsuite
1161 if (source.size() == 0) {
1162 FileSource("../../../certs/client-key.der", source); // Debug dir
1163 if (source.size() == 0)
1164 err_sys("where's your certs dir?", -79);
1165 }
1166 }
1167 RSA_PrivateKey priv(source);
1168
1169 RSAES_Encryptor enc(priv);
1170 byte message[] = "Everyone gets Friday off.";
1171 const word32 len = (word32)strlen((char*)message);
1172 byte cipher[512];
1173 enc.Encrypt(message, len, cipher, rng);
1174
1175 RSAES_Decryptor dec(priv);
1176 byte plain[512];
1177 dec.Decrypt(cipher, priv.FixedCiphertextLength(), plain, rng);
1178
1179 if (memcmp(plain, message, len))
1180 return -70;
1181
1182 dec.SSL_Sign(message, len, cipher, rng);
1183 if (!enc.SSL_Verify(message, len, cipher))
1184 return -71;
1185
1186
1187 // test decode
1188 Source source2;
1189 FileSource("../certs/client-cert.der", source2);
1190 if (source2.size() == 0) {
1191 FileSource("../../certs/client-cert.der", source2); // for testsuite
1192 if (source2.size() == 0) {
1193 FileSource("../../../certs/client-cert.der", source2); // Debug dir
1194 if (source2.size() == 0)
1195 err_sys("where's your certs dir?", -79);
1196 }
1197 }
1198 CertDecoder cd(source2, true, 0, false, CertDecoder::CA);
1199 if (cd.GetError().What())
1200 err_sys("cert error", -80);
1201 Source source3(cd.GetPublicKey().GetKey(), cd.GetPublicKey().size());
1202 RSA_PublicKey pub(source3);
1203
1204 return 0;
1205 }
1206
1207
dh_test()1208 int dh_test()
1209 {
1210 Source source;
1211 FileSource("../certs/dh1024.dat", source);
1212 if (source.size() == 0) {
1213 FileSource("../../certs/dh1024.dat", source); // for testsuite
1214 if (source.size() == 0) {
1215 FileSource("../../../certs/dh1024.dat", source); // win32 Debug dir
1216 if (source.size() == 0)
1217 err_sys("where's your certs dir?", -79);
1218 }
1219 }
1220 HexDecoder hDec(source);
1221
1222 DH dh(source);
1223
1224 byte pub[128];
1225 byte priv[128];
1226 byte agree[128];
1227 byte pub2[128];
1228 byte priv2[128];
1229 byte agree2[128];
1230
1231 DH dh2(dh);
1232
1233 dh.GenerateKeyPair(rng, priv, pub);
1234 dh2.GenerateKeyPair(rng, priv2, pub2);
1235 dh.Agree(agree, priv, pub2);
1236 dh2.Agree(agree2, priv2, pub);
1237
1238
1239 if ( memcmp(agree, agree2, dh.GetByteLength()) )
1240 return -80;
1241
1242 return 0;
1243 }
1244
1245
dsa_test()1246 int dsa_test()
1247 {
1248 Source source;
1249 FileSource("../certs/dsa1024.der", source);
1250 if (source.size() == 0) {
1251 FileSource("../../certs/dsa1024.der", source); // for testsuite
1252 if (source.size() == 0) {
1253 FileSource("../../../certs/dsa1024.der", source); // win32 Debug dir
1254 if (source.size() == 0)
1255 err_sys("where's your certs dir?", -89);
1256 }
1257 }
1258
1259 const char msg[] = "this is the message";
1260 byte signature[40];
1261
1262 DSA_PrivateKey priv(source);
1263 DSA_Signer signer(priv);
1264
1265 SHA sha;
1266 byte digest[SHA::DIGEST_SIZE];
1267 sha.Update((byte*)msg, sizeof(msg));
1268 sha.Final(digest);
1269
1270 signer.Sign(digest, signature, rng);
1271
1272 byte encoded[sizeof(signature) + 6];
1273 byte decoded[40];
1274
1275 word32 encSz = EncodeDSA_Signature(signer.GetR(), signer.GetS(), encoded);
1276 DecodeDSA_Signature(decoded, encoded, encSz);
1277
1278 DSA_PublicKey pub(priv);
1279 DSA_Verifier verifier(pub);
1280
1281 if (!verifier.Verify(digest, decoded))
1282 return -90;
1283
1284 if (!verifier.Verify(digest, signature))
1285 return -91;
1286
1287 return 0;
1288 }
1289
1290
pwdbased_test()1291 int pwdbased_test()
1292 {
1293 PBKDF2_HMAC<SHA> pb;
1294
1295 byte derived[32];
1296 const byte pwd1[] = "password "; // align
1297 const byte salt[] = { 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12 };
1298
1299 pb.DeriveKey(derived, 8, pwd1, 8, salt, sizeof(salt), 5);
1300
1301 const byte verify1[] = { 0xD1, 0xDA, 0xA7, 0x86, 0x15, 0xF2, 0x87, 0xE6 };
1302
1303 if ( memcmp(derived, verify1, sizeof(verify1)) )
1304 return -101;
1305
1306
1307 const byte pwd2[] = "All n-entities must communicate with other n-entities"
1308 " via n-1 entiteeheehees "; // align
1309
1310 pb.DeriveKey(derived, 24, pwd2, 76, salt, sizeof(salt), 500);
1311
1312 const byte verify2[] = { 0x6A, 0x89, 0x70, 0xBF, 0x68, 0xC9, 0x2C, 0xAE,
1313 0xA8, 0x4A, 0x8D, 0xF2, 0x85, 0x10, 0x85, 0x86,
1314 0x07, 0x12, 0x63, 0x80, 0xCC, 0x47, 0xAB, 0x2D
1315 };
1316
1317 if ( memcmp(derived, verify2, sizeof(verify2)) )
1318 return -102;
1319
1320 return 0;
1321 }
1322
1323
1324 /*
1325 int pkcs12_test()
1326 {
1327 Source cert;
1328 FileSource("../certs/server-cert.pem", cert);
1329 if (cert.size() == 0) {
1330 FileSource("../../certs/server-cert.pem", cert); // for testsuite
1331 if (cert.size() == 0) {
1332 FileSource("../../../certs/server-cert.pem", cert); // Debug dir
1333 if (cert.size() == 0)
1334 err_sys("where's your certs dir?", -109);
1335 }
1336 }
1337
1338 if (GetCert(cert) != 0)
1339 return -110;
1340
1341 Source source;
1342 FileSource("../certs/server.p12", source);
1343 if (source.size() == 0) {
1344 FileSource("../../certs/server.p12", source); // for testsuite
1345 if (source.size() == 0) {
1346 FileSource("../../../certs/server.p12", source); // Debug dir
1347 if (source.size() == 0)
1348 err_sys("where's your certs dir?", -111);
1349 }
1350 }
1351
1352 if (GetPKCS_Cert("password", source) != 0)
1353 return -112;
1354
1355 return 0;
1356 }
1357 */
1358
1359