25 #include "runtime.hpp"
37 #include "twofish.hpp"
38 #include "blowfish.hpp"
43 #include "pwdbased.hpp"
50 using TaoCrypt::word32;
54 #ifdef WORD64_AVAILABLE
55 using TaoCrypt::SHA512;
56 using TaoCrypt::SHA384;
99 using TaoCrypt::EncodeDSA_Signature;
100 using TaoCrypt::DecodeDSA_Signature;
102 using TaoCrypt::tcArrayDelete;
103 using TaoCrypt::GetCert;
104 using TaoCrypt::GetPKCS_Cert;
114 testVector(
const char* in,
const char* out) : input_((byte*)in),
115 output_((byte*)out), inLen_((word32)strlen(in)),
116 outLen_((word32)strlen(out)) {}
121 #ifdef WORD64_AVAILABLE
147 void err_sys(
const char*
msg,
int es)
171 const byte msgTmp[] = {
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
182 void taocrypt_test(
void* args)
186 msg = NEW_TC byte[24];
187 plain = NEW_TC byte[24];
188 cipher = NEW_TC byte[24];
190 memcpy(
msg, msgTmp, 24);
193 if ( (ret = sha_test()) )
194 err_sys(
"SHA test failed!\n", ret);
196 printf(
"SHA test passed!\n");
198 if ( (ret = sha256_test()) )
199 err_sys(
"SHA-256 test failed!\n", ret);
201 printf(
"SHA-256 test passed!\n");
203 if ( (ret = sha224_test()) )
204 err_sys(
"SHA-224 test failed!\n", ret);
206 printf(
"SHA-224 test passed!\n");
208 #ifdef WORD64_AVAILABLE
210 if ( (ret = sha512_test()) )
211 err_sys(
"SHA-512 test failed!\n", ret);
213 printf(
"SHA-512 test passed!\n");
215 if ( (ret = sha384_test()) )
216 err_sys(
"SHA-384 test failed!\n", ret);
218 printf(
"SHA-384 test passed!\n");
222 if ( (ret = md5_test()) )
223 err_sys(
"MD5 test failed!\n", ret);
225 printf(
"MD5 test passed!\n");
227 if ( (ret = md2_test()) )
228 err_sys(
"MD2 test failed!\n", ret);
230 printf(
"MD2 test passed!\n");
232 if ( (ret = md4_test()) )
233 err_sys(
"MD4 test failed!\n", ret);
235 printf(
"MD4 test passed!\n");
237 if ( (ret = ripemd_test()) )
238 err_sys(
"RIPEMD test failed!\n", ret);
240 printf(
"RIPEMD test passed!\n");
242 if ( ( ret = hmac_test()) )
243 err_sys(
"HMAC test failed!\n", ret);
245 printf(
"HMAC test passed!\n");
247 if ( (ret = arc4_test()) )
248 err_sys(
"ARC4 test failed!\n", ret);
250 printf(
"ARC4 test passed!\n");
252 if ( (ret = rabbit_test()) )
253 err_sys(
"Rabbit test failed!\n", ret);
255 printf(
"Rabbit test passed!\n");
257 if ( (ret = hc128_test()) )
258 err_sys(
"HC128 test failed!\n", ret);
260 printf(
"HC128 test passed!\n");
262 if ( (ret = des_test()) )
263 err_sys(
"DES test failed!\n", ret);
265 printf(
"DES test passed!\n");
267 if ( (ret = aes_test()) )
268 err_sys(
"AES test failed!\n", ret);
270 printf(
"AES test passed!\n");
272 if ( (ret = twofish_test()) )
273 err_sys(
"Twofish test failed!\n", ret);
275 printf(
"Twofish test passed!\n");
277 if ( (ret = blowfish_test()) )
278 err_sys(
"Blowfish test failed!\n", ret);
280 printf(
"Blowfish test passed!\n");
282 if ( (ret = rsa_test()) )
283 err_sys(
"RSA test failed!\n", ret);
285 printf(
"RSA test passed!\n");
287 if ( (ret = dh_test()) )
288 err_sys(
"DH test failed!\n", ret);
290 printf(
"DH test passed!\n");
292 if ( (ret = dsa_test()) )
293 err_sys(
"DSA test failed!\n", ret);
295 printf(
"DSA test passed!\n");
297 if ( (ret = pwdbased_test()) )
298 err_sys(
"PBKDF2 test failed!\n", ret);
300 printf(
"PBKDF2 test passed!\n");
309 tcArrayDelete(cipher);
310 tcArrayDelete(plain);
318 #ifndef NO_MAIN_DRIVER
320 int main(
int argc,
char** argv)
327 taocrypt_test(&args);
330 return args.return_code;
333 #endif // NO_MAIN_DRIVER
336 void file_test(
const char*
file, byte* check)
342 byte md5sum[MD5::DIGEST_SIZE];
344 if( !( f = fopen( file,
"rb" ) )) {
345 printf(
"Can't open %s\n", file);
348 while( ( i = (
int)fread(buf, 1,
sizeof(buf), f )) > 0 )
352 memcpy(check, md5sum,
sizeof(md5sum));
354 for(
int j = 0; j < MD5::DIGEST_SIZE; ++j )
355 printf(
"%02x", md5sum[j] );
357 printf(
" %s\n", file);
366 byte hash[SHA::DIGEST_SIZE];
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"
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"
383 "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
384 "\x53\x99\x5E\x26\xA0")
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_);
392 if (memcmp(hash, test_sha[i].output_, SHA::DIGEST_SIZE) != 0)
403 byte hash[SHA256::DIGEST_SIZE];
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"
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"
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_);
422 if (memcmp(hash, test_sha[i].output_, SHA256::DIGEST_SIZE) != 0)
430 #ifdef WORD64_AVAILABLE
435 byte hash[SHA512::DIGEST_SIZE];
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"
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"
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_);
459 if (memcmp(hash, test_sha[i].output_, SHA512::DIGEST_SIZE) != 0)
470 byte hash[SHA384::DIGEST_SIZE];
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"
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"
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_);
492 if (memcmp(hash, test_sha[i].output_, SHA384::DIGEST_SIZE) != 0)
499 #endif // WORD64_AVAILABLE
505 byte hash[SHA224::DIGEST_SIZE];
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")
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_);
522 if (memcmp(hash, test_sha[i].output_, SHA224::DIGEST_SIZE) != 0)
533 byte hash[MD5::DIGEST_SIZE];
538 "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
541 "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61"
544 "\xc3\xfc\xd3\xd7\x61\x92\xe4\x00\x7d\xfb\x49\x6c\xca\x67\xe1"
546 testVector(
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
548 "\xd1\x74\xab\x98\xd2\x77\xd9\xf5\xa5\x61\x1c\x2c\x9f\x41\x9d"
550 testVector(
"1234567890123456789012345678901234567890123456789012345678"
551 "9012345678901234567890",
552 "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
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_);
561 if (memcmp(hash, test_md5[i].output_, MD5::DIGEST_SIZE) != 0)
572 byte hash[MD4::DIGEST_SIZE];
577 "\x31\xd6\xcf\xe0\xd1\x6a\xe9\x31\xb7\x3c\x59\xd7\xe0\xc0\x89"
580 "\xbd\xe5\x2c\xb3\x1d\xe3\x3e\x46\x24\x5e\x05\xfb\xdb\xd6\xfb"
583 "\xa4\x48\x01\x7a\xaf\x21\xd8\x52\x5f\xc1\x0a\xe8\x7a\xa6\x72"
586 "\xd9\x13\x0a\x81\x64\x54\x9f\xe8\x18\x87\x48\x06\xe1\xc7\x01"
589 "\xd7\x9e\x1c\x30\x8a\xa5\xbb\xcd\xee\xa8\xed\x63\xdf\x41\x2d"
591 testVector(
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
593 "\x04\x3f\x85\x82\xf2\x41\xdb\x35\x1c\xe6\x27\xe1\x53\xe7\xf0"
595 testVector(
"1234567890123456789012345678901234567890123456789012345678"
596 "9012345678901234567890",
597 "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f\xcc\x05"
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_);
606 if (memcmp(hash, test_md4[i].output_, MD4::DIGEST_SIZE) != 0)
617 byte hash[MD2::DIGEST_SIZE];
622 "\x83\x50\xe5\xa3\xe2\x4c\x15\x3d\xf2\x27\x5c\x9f\x80\x69"
625 "\x32\xec\x01\xec\x4a\x6d\xac\x72\xc0\xab\x96\xfb\x34\xc0"
628 "\xda\x85\x3b\x0d\x3f\x88\xd9\x9b\x30\x28\x3a\x69\xe6\xde"
631 "\xab\x4f\x49\x6b\xfb\x2a\x53\x0b\x21\x9f\xf3\x30\x31\xfe"
634 "\x4e\x8d\xdf\xf3\x65\x02\x92\xab\x5a\x41\x08\xc3\xaa\x47"
636 testVector(
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
638 "\xda\x33\xde\xf2\xa4\x2d\xf1\x39\x75\x35\x28\x46\xc3\x03"
640 testVector(
"12345678901234567890123456789012345678901234567890123456"
641 "789012345678901234567890",
642 "\xd5\x97\x6f\x79\xd8\x3d\x3a\x0d\xc9\x80\x6c\x3c\x66\xf3"
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_);
651 if (memcmp(hash, test_md2[i].output_, MD2::DIGEST_SIZE) != 0)
662 byte hash[RIPEMD160::DIGEST_SIZE];
667 "\x9c\x11\x85\xa5\xc5\xe9\xfc\x54\x61\x28\x08\x97\x7e\xe8"
668 "\xf5\x48\xb2\x25\x8d\x31"),
670 "\x0b\xdc\x9d\x2d\x25\x6b\x3e\xe9\xda\xae\x34\x7b\xe6\xf4"
671 "\xdc\x83\x5a\x46\x7f\xfe"),
673 "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
674 "\xb0\x87\xf1\x5a\x0b\xfc"),
676 "\x5d\x06\x89\xef\x49\xd2\xfa\xe5\x72\xb8\x81\xb1\x23\xa8"
677 "\x5f\xfa\x21\x59\x5f\x36"),
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"
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"),
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_);
699 if (memcmp(hash, test_ripemd[i].output_, RIPEMD160::DIGEST_SIZE) != 0)
710 byte hash[MD5::DIGEST_SIZE];
714 "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
716 "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
722 "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
725 "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7"
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"
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_);
741 if (memcmp(hash, test_hmacMD5[i].output_, MD5::DIGEST_SIZE) != 0)
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",
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")
775 int times(
sizeof(test_arc4) /
sizeof(
testVector) );
776 for (
int i = 0; i < times; ++
i) {
777 ARC4::Encryption enc;
778 ARC4::Decryption dec;
780 enc.SetKey((byte*)keys[i], (word32)strlen(keys[i]));
781 dec.SetKey((byte*)keys[i], (word32)strlen(keys[i]));
783 enc.Process(cipher, test_arc4[i].input_, test_arc4[i].outLen_);
784 dec.Process(plain, cipher, test_arc4[i].outLen_);
786 if (memcmp(plain, test_arc4[i].input_, test_arc4[i].outLen_))
789 if (memcmp(cipher, test_arc4[i].output_, test_arc4[i].outLen_))
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"
811 "\x00\x00\x00\x00\x00\x00\x00\x00",
812 "\x59\x7E\x26\xC1\x75\xF5\x73\xC3",
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")
828 int times(
sizeof(test_rabbit) /
sizeof(
testVector) );
829 for (
int i = 0; i < times; ++
i) {
830 Rabbit::Encryption enc;
831 Rabbit::Decryption dec;
833 enc.SetKey((byte*)keys[i], (byte*)ivs[i]);
834 dec.SetKey((byte*)keys[i], (byte*)ivs[i]);
836 enc.Process(cipher, test_rabbit[i].input_, test_rabbit[i].outLen_);
837 dec.Process(plain, cipher, test_rabbit[i].outLen_);
839 if (memcmp(plain, test_rabbit[i].input_, test_rabbit[i].outLen_))
842 if (memcmp(cipher, test_rabbit[i].output_, test_rabbit[i].outLen_))
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"
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"
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")
883 int times(
sizeof(test_hc128) /
sizeof(
testVector) );
884 for (
int i = 0; i < times; ++
i) {
885 HC128::Encryption enc;
886 HC128::Decryption dec;
888 enc.SetKey((byte*)keys[i], (byte*)ivs[i]);
889 dec.SetKey((byte*)keys[i], (byte*)ivs[i]);
891 enc.Process(cipher, test_hc128[i].input_, test_hc128[i].outLen_);
892 dec.Process(plain, cipher, test_hc128[i].outLen_);
894 if (memcmp(plain, test_hc128[i].input_, test_hc128[i].outLen_))
897 if (memcmp(cipher, test_hc128[i].output_, test_hc128[i].outLen_))
908 DES_ECB_Encryption enc;
909 DES_ECB_Decryption dec;
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 };
915 enc.SetKey(key,
sizeof(key));
916 enc.Process(cipher,
msg, sz);
917 dec.SetKey(key,
sizeof(key));
918 dec.Process(plain, cipher, sz);
920 if (memcmp(plain,
msg, sz))
923 const byte verify1[] =
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
930 if (memcmp(cipher, verify1, sz))
934 DES_CBC_Encryption enc2;
935 DES_CBC_Decryption dec2;
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);
942 if (memcmp(plain,
msg, sz))
945 const byte verify2[] =
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
952 if (memcmp(cipher, verify2, sz))
956 DES_EDE3_CBC_Encryption enc3;
957 DES_EDE3_CBC_Decryption dec3;
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
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
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);
978 if (memcmp(plain,
msg, sz))
981 const byte verify3[] =
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
988 if (memcmp(cipher, verify3, sz))
997 AES_CBC_Encryption enc;
998 AES_CBC_Decryption dec;
999 const int bs(TaoCrypt::AES::BLOCK_SIZE);
1001 byte key[] =
"0123456789abcdef ";
1002 byte iv[] =
"1234567890abcdef ";
1004 enc.SetKey(key, bs, iv);
1005 dec.SetKey(key, bs, iv);
1007 enc.Process(cipher,
msg, bs);
1008 dec.Process(plain, cipher, bs);
1010 if (memcmp(plain,
msg, bs))
1013 const byte verify[] =
1015 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
1016 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
1019 if (memcmp(cipher, verify, bs))
1022 AES_ECB_Encryption enc2;
1023 AES_ECB_Decryption dec2;
1025 enc2.SetKey(key, bs, iv);
1026 dec2.SetKey(key, bs, iv);
1028 enc2.Process(cipher,
msg, bs);
1029 dec2.Process(plain, cipher, bs);
1031 if (memcmp(plain,
msg, bs))
1034 const byte verify2[] =
1036 0xd0,0xc9,0xd9,0xc9,0x40,0xe8,0x97,0xb6,
1037 0xc8,0x8c,0x33,0x3b,0xb5,0x8f,0x85,0xd1
1040 if (memcmp(cipher, verify2, bs))
1049 Twofish_CBC_Encryption enc;
1050 Twofish_CBC_Decryption dec;
1051 const int bs(TaoCrypt::Twofish::BLOCK_SIZE);
1053 byte key[] =
"0123456789abcdef ";
1054 byte iv[] =
"1234567890abcdef ";
1056 enc.SetKey(key, bs, iv);
1057 dec.SetKey(key, bs, iv);
1059 enc.Process(cipher,
msg, bs);
1060 dec.Process(plain, cipher, bs);
1062 if (memcmp(plain,
msg, bs))
1065 const byte verify[] =
1067 0xD2,0xD7,0x47,0x47,0x4A,0x65,0x4E,0x16,
1068 0x21,0x03,0x58,0x79,0x5F,0x02,0x27,0x2C
1071 if (memcmp(cipher, verify, bs))
1074 Twofish_ECB_Encryption enc2;
1075 Twofish_ECB_Decryption dec2;
1077 enc2.SetKey(key, bs, iv);
1078 dec2.SetKey(key, bs, iv);
1080 enc2.Process(cipher,
msg, bs);
1081 dec2.Process(plain, cipher, bs);
1083 if (memcmp(plain,
msg, bs))
1086 const byte verify2[] =
1088 0x3B,0x6C,0x63,0x10,0x34,0xAB,0xB2,0x87,
1089 0xC4,0xCD,0x6B,0x91,0x14,0xC5,0x3A,0x09
1092 if (memcmp(cipher, verify2, bs))
1101 Blowfish_CBC_Encryption enc;
1102 Blowfish_CBC_Decryption dec;
1103 const int bs(TaoCrypt::Blowfish::BLOCK_SIZE);
1105 byte key[] =
"0123456789abcdef ";
1106 byte iv[] =
"1234567890abcdef ";
1108 enc.SetKey(key, 16, iv);
1109 dec.SetKey(key, 16, iv);
1111 enc.Process(cipher,
msg, bs * 2);
1112 dec.Process(plain, cipher, bs * 2);
1114 if (memcmp(plain,
msg, bs))
1117 const byte verify[] =
1119 0x0E,0x26,0xAA,0x29,0x11,0x25,0xAB,0xB5,
1120 0xBC,0xD9,0x08,0xC4,0x94,0x6C,0x89,0xA3
1123 if (memcmp(cipher, verify, bs))
1126 Blowfish_ECB_Encryption enc2;
1127 Blowfish_ECB_Decryption dec2;
1129 enc2.SetKey(key, 16, iv);
1130 dec2.SetKey(key, 16, iv);
1132 enc2.Process(cipher,
msg, bs * 2);
1133 dec2.Process(plain, cipher, bs * 2);
1135 if (memcmp(plain,
msg, bs))
1138 const byte verify2[] =
1140 0xE7,0x42,0xB9,0x37,0xC8,0x7D,0x93,0xCA,
1141 0x8F,0xCE,0x39,0x32,0xDE,0xD7,0xBC,0x5B
1144 if (memcmp(cipher, verify2, bs))
1154 FileSource(
"../certs/client-key.der", source);
1155 if (source.size() == 0) {
1156 FileSource(
"../../certs/client-key.der", source);
1157 if (source.size() == 0) {
1158 FileSource(
"../../../certs/client-key.der", source);
1159 if (source.size() == 0)
1160 err_sys(
"where's your certs dir?", -79);
1165 RSAES_Encryptor enc(priv);
1166 byte
message[] =
"Everyone gets Friday off.";
1167 const word32 len = (word32)strlen((
char*)message);
1169 enc.Encrypt(message, len, cipher, rng);
1171 RSAES_Decryptor dec(priv);
1173 dec.Decrypt(cipher,
sizeof(plain), plain, rng);
1175 if (memcmp(plain, message, len))
1178 dec.SSL_Sign(message, len, cipher, rng);
1179 if (!enc.SSL_Verify(message, len, cipher))
1185 FileSource(
"../certs/client-cert.der", source2);
1186 if (source2.size() == 0) {
1187 FileSource(
"../../certs/client-cert.der", source2);
1188 if (source2.size() == 0) {
1189 FileSource(
"../../../certs/client-cert.der", source2);
1190 if (source2.size() == 0)
1191 err_sys(
"where's your certs dir?", -79);
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());
1208 if (source.size() == 0) {
1209 FileSource(
"../../certs/dh1024.dat", source);
1210 if (source.size() == 0) {
1211 FileSource(
"../../../certs/dh1024.dat", source);
1212 if (source.size() == 0)
1213 err_sys(
"where's your certs dir?", -79);
1229 dh.GenerateKeyPair(rng, priv, pub);
1230 dh2.GenerateKeyPair(rng, priv2, pub2);
1231 dh.Agree(agree, priv, pub2);
1232 dh2.Agree(agree2, priv2, pub);
1235 if ( memcmp(agree, agree2, dh.GetByteLength()) )
1246 if (source.size() == 0) {
1247 FileSource(
"../../certs/dsa512.der", source);
1248 if (source.size() == 0) {
1249 FileSource(
"../../../certs/dsa512.der", source);
1250 if (source.size() == 0)
1251 err_sys(
"where's your certs dir?", -89);
1255 const char msg[] =
"this is the message";
1262 byte digest[SHA::DIGEST_SIZE];
1263 sha.Update((byte*)msg,
sizeof(msg));
1266 signer.Sign(digest, signature, rng);
1268 byte encoded[
sizeof(signature) + 6];
1271 word32 encSz = EncodeDSA_Signature(signer.GetR(), signer.GetS(), encoded);
1272 DecodeDSA_Signature(decoded, encoded, encSz);
1277 if (!verifier.Verify(digest, decoded))
1289 const byte pwd1[] =
"password ";
1290 const byte salt[] = { 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12 };
1292 pb.DeriveKey(derived, 8, pwd1, 8, salt,
sizeof(salt), 5);
1294 const byte verify1[] = { 0xD1, 0xDA, 0xA7, 0x86, 0x15, 0xF2, 0x87, 0xE6 };
1296 if ( memcmp(derived, verify1,
sizeof(verify1)) )
1300 const byte pwd2[] =
"All n-entities must communicate with other n-entities"
1301 " via n-1 entiteeheehees ";
1303 pb.DeriveKey(derived, 24, pwd2, 76, salt,
sizeof(salt), 500);
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
1310 if ( memcmp(derived, verify2,
sizeof(verify2)) )