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