~ubuntu-branches/ubuntu/hardy/mysql-dfsg-5.0/hardy-updates

« back to all changes in this revision

Viewing changes to extra/yassl/taocrypt/test/test.cpp

  • Committer: Bazaar Package Importer
  • Author(s): sean finney
  • Date: 2007-05-13 12:32:45 UTC
  • mfrom: (1.1.11 upstream)
  • Revision ID: james.westby@ubuntu.com-20070513123245-8c3l187dk34cz2ar
Tags: 5.0.41-2
the previous "translation changes" inadvertently introduced unrelated
changes in the package control file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
using TaoCrypt::byte;
30
30
using TaoCrypt::word32;
31
31
using TaoCrypt::SHA;
 
32
using TaoCrypt::SHA256;
 
33
using TaoCrypt::SHA224;
 
34
#ifdef WORD64_AVAILABLE
 
35
    using TaoCrypt::SHA512;
 
36
    using TaoCrypt::SHA384;
 
37
#endif
32
38
using TaoCrypt::MD5;
33
39
using TaoCrypt::MD2;
34
40
using TaoCrypt::MD4;
88
94
               output_((byte*)out), inLen_(strlen(in)), outLen_(strlen(out)) {}
89
95
};
90
96
 
91
 
void file_test(int, char**);
92
97
int  sha_test();
 
98
int  sha256_test();
 
99
#ifdef WORD64_AVAILABLE
 
100
    int  sha512_test();
 
101
    int  sha384_test();
 
102
#endif
 
103
int  sha224_test();
93
104
int  md5_test();
94
105
int  md2_test();
95
106
int  md4_test();
139
150
    0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
140
151
};
141
152
 
142
 
byte* global_msg    = 0;   // for block cipher input
143
 
byte* global_plain  = 0;   // for cipher decrypt comparison 
144
 
byte* global_cipher = 0;   // block output
 
153
byte* msg    = 0;   // for block cipher input
 
154
byte* plain  = 0;   // for cipher decrypt comparison 
 
155
byte* cipher = 0;   // block output
145
156
 
146
157
 
147
158
void taocrypt_test(void* args)
148
159
{
149
160
    ((func_args*)args)->return_code = -1; // error state
150
161
    
151
 
    global_msg    = NEW_TC byte[24];
152
 
    global_plain  = NEW_TC byte[24];
153
 
    global_cipher = NEW_TC byte[24];
 
162
    msg    = NEW_TC byte[24];
 
163
    plain  = NEW_TC byte[24];
 
164
    cipher = NEW_TC byte[24];
154
165
 
155
 
    memcpy(global_msg, msgTmp, 24);
 
166
    memcpy(msg, msgTmp, 24);
156
167
 
157
168
    int ret = 0;
158
169
    if ( (ret = sha_test()) ) 
160
171
    else
161
172
        printf( "SHA      test passed!\n");
162
173
 
 
174
    if ( (ret = sha256_test()) ) 
 
175
        err_sys("SHA-256  test failed!\n", ret);
 
176
    else
 
177
        printf( "SHA-256  test passed!\n");
 
178
 
 
179
    if ( (ret = sha224_test()) ) 
 
180
        err_sys("SHA-224  test failed!\n", ret);
 
181
    else
 
182
        printf( "SHA-224  test passed!\n");
 
183
 
 
184
#ifdef WORD64_AVAILABLE
 
185
 
 
186
    if ( (ret = sha512_test()) ) 
 
187
        err_sys("SHA-512  test failed!\n", ret);
 
188
    else
 
189
        printf( "SHA-512  test passed!\n");
 
190
 
 
191
    if ( (ret = sha384_test()) ) 
 
192
        err_sys("SHA-384  test failed!\n", ret);
 
193
    else
 
194
        printf( "SHA-384  test passed!\n");
 
195
 
 
196
#endif
 
197
 
163
198
    if ( (ret = md5_test()) ) 
164
199
        err_sys("MD5      test failed!\n", ret);
165
200
    else
237
272
        printf( "PKCS12   test passed!\n");
238
273
    */
239
274
 
240
 
    tcArrayDelete(global_cipher);
241
 
    tcArrayDelete(global_plain);
242
 
    tcArrayDelete(global_msg);
 
275
    tcArrayDelete(cipher);
 
276
    tcArrayDelete(plain);
 
277
    tcArrayDelete(msg);
243
278
 
244
279
    ((func_args*)args)->return_code = ret;
245
280
}
264
299
#endif // NO_MAIN_DRIVER
265
300
 
266
301
 
267
 
void file_test(char* file, byte* check)
 
302
void file_test(const char* file, byte* check)
268
303
{
269
304
    FILE* f;
270
305
    int   i(0);
328
363
}
329
364
 
330
365
 
 
366
int sha256_test()
 
367
{
 
368
    SHA256 sha;
 
369
    byte   hash[SHA256::DIGEST_SIZE];
 
370
 
 
371
    testVector test_sha[] =
 
372
    {
 
373
        testVector("abc",
 
374
                 "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
 
375
                 "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
 
376
                 "\x15\xAD"),
 
377
        testVector("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
 
378
                 "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
 
379
                 "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
 
380
                 "\x06\xC1")
 
381
    };
 
382
 
 
383
    int times( sizeof(test_sha) / sizeof(testVector) );
 
384
    for (int i = 0; i < times; ++i) {
 
385
        sha.Update(test_sha[i].input_, test_sha[i].inLen_);
 
386
        sha.Final(hash);
 
387
 
 
388
        if (memcmp(hash, test_sha[i].output_, SHA256::DIGEST_SIZE) != 0)
 
389
            return -1 - i;
 
390
    }
 
391
 
 
392
    return 0;
 
393
}
 
394
 
 
395
 
 
396
#ifdef WORD64_AVAILABLE
 
397
 
 
398
int sha512_test()
 
399
{
 
400
    SHA512 sha;
 
401
    byte   hash[SHA512::DIGEST_SIZE];
 
402
 
 
403
    testVector test_sha[] =
 
404
    {
 
405
        testVector("abc",
 
406
                 "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
 
407
                 "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
 
408
                 "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
 
409
                 "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
 
410
                 "\xa5\x4c\xa4\x9f"),
 
411
        testVector("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
 
412
                   "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", 
 
413
                 "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
 
414
                 "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
 
415
                 "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
 
416
                 "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
 
417
                 "\x87\x4b\xe9\x09")
 
418
    };
 
419
 
 
420
    int times( sizeof(test_sha) / sizeof(testVector) );
 
421
    for (int i = 0; i < times; ++i) {
 
422
        sha.Update(test_sha[i].input_, test_sha[i].inLen_);
 
423
        sha.Final(hash);
 
424
 
 
425
        if (memcmp(hash, test_sha[i].output_, SHA512::DIGEST_SIZE) != 0)
 
426
            return -1 - i;
 
427
    }
 
428
 
 
429
    return 0;
 
430
}
 
431
 
 
432
 
 
433
int sha384_test()
 
434
{
 
435
    SHA384 sha;
 
436
    byte   hash[SHA384::DIGEST_SIZE];
 
437
 
 
438
    testVector test_sha[] =
 
439
    {
 
440
        testVector("abc",
 
441
                 "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
 
442
                 "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
 
443
                 "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
 
444
                 "\xc8\x25\xa7"),
 
445
        testVector("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
 
446
                   "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", 
 
447
                 "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
 
448
                 "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
 
449
                 "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
 
450
                 "\x74\x60\x39")
 
451
    };
 
452
 
 
453
    int times( sizeof(test_sha) / sizeof(testVector) );
 
454
    for (int i = 0; i < times; ++i) {
 
455
        sha.Update(test_sha[i].input_, test_sha[i].inLen_);
 
456
        sha.Final(hash);
 
457
 
 
458
        if (memcmp(hash, test_sha[i].output_, SHA384::DIGEST_SIZE) != 0)
 
459
            return -1 - i;
 
460
    }
 
461
 
 
462
    return 0;
 
463
}
 
464
 
 
465
#endif // WORD64_AVAILABLE
 
466
 
 
467
 
 
468
int sha224_test()
 
469
{
 
470
    SHA224 sha;
 
471
    byte   hash[SHA224::DIGEST_SIZE];
 
472
 
 
473
    testVector test_sha[] =
 
474
    {
 
475
        testVector("abc",
 
476
                 "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2\x55"
 
477
                 "\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7"),
 
478
        testVector("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
 
479
                 "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
 
480
                 "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25")
 
481
    };
 
482
 
 
483
    int times( sizeof(test_sha) / sizeof(testVector) );
 
484
    for (int i = 0; i < times; ++i) {
 
485
        sha.Update(test_sha[i].input_, test_sha[i].inLen_);
 
486
        sha.Final(hash);
 
487
 
 
488
        if (memcmp(hash, test_sha[i].output_, SHA224::DIGEST_SIZE) != 0)
 
489
            return -1 - i;
 
490
    }
 
491
 
 
492
    return 0;
 
493
}
 
494
 
 
495
 
331
496
int md5_test()
332
497
{
333
498
    MD5  md5;
606
771
    const byte iv[] =  { 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef };
607
772
 
608
773
    enc.SetKey(key, sizeof(key));
609
 
    enc.Process(global_cipher, global_msg, sz);
 
774
    enc.Process(cipher, msg, sz);
610
775
    dec.SetKey(key, sizeof(key));
611
 
    dec.Process(global_plain, global_cipher, sz);
 
776
    dec.Process(plain, cipher, sz);
612
777
 
613
 
    if (memcmp(global_plain, global_msg, sz))
 
778
    if (memcmp(plain, msg, sz))
614
779
        return -50;
615
780
 
616
781
    const byte verify1[] = 
620
785
        0x89,0x3d,0x51,0xec,0x4b,0x56,0x3b,0x53
621
786
    };
622
787
 
623
 
    if (memcmp(global_cipher, verify1, sz))
 
788
    if (memcmp(cipher, verify1, sz))
624
789
        return -51;
625
790
 
626
791
    // CBC mode
628
793
    DES_CBC_Decryption dec2;
629
794
 
630
795
    enc2.SetKey(key, sizeof(key), iv);
631
 
    enc2.Process(global_cipher, global_msg, sz);
 
796
    enc2.Process(cipher, msg, sz);
632
797
    dec2.SetKey(key, sizeof(key), iv);
633
 
    dec2.Process(global_plain, global_cipher, sz);
 
798
    dec2.Process(plain, cipher, sz);
634
799
 
635
 
    if (memcmp(global_plain, global_msg, sz))
 
800
    if (memcmp(plain, msg, sz))
636
801
        return -52;
637
802
 
638
803
    const byte verify2[] = 
642
807
        0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
643
808
    };
644
809
 
645
 
    if (memcmp(global_cipher, verify2, sz))
 
810
    if (memcmp(cipher, verify2, sz))
646
811
        return -53;
647
812
 
648
813
    // EDE3 CBC mode
664
829
    };
665
830
 
666
831
    enc3.SetKey(key3, sizeof(key3), iv3);
667
 
    enc3.Process(global_cipher, global_msg, sz);
 
832
    enc3.Process(cipher, msg, sz);
668
833
    dec3.SetKey(key3, sizeof(key3), iv3);
669
 
    dec3.Process(global_plain, global_cipher, sz);
 
834
    dec3.Process(plain, cipher, sz);
670
835
 
671
 
    if (memcmp(global_plain, global_msg, sz))
 
836
    if (memcmp(plain, msg, sz))
672
837
        return -54;
673
838
 
674
839
    const byte verify3[] = 
678
843
        0x18,0xbc,0xbb,0x6d,0xd2,0xb1,0x16,0xda
679
844
    };
680
845
 
681
 
    if (memcmp(global_cipher, verify3, sz))
 
846
    if (memcmp(cipher, verify3, sz))
682
847
        return -55;
683
848
 
684
849
    return 0;
697
862
    enc.SetKey(key, bs, iv);
698
863
    dec.SetKey(key, bs, iv);
699
864
 
700
 
    enc.Process(global_cipher, global_msg, bs);
701
 
    dec.Process(global_plain, global_cipher, bs);
 
865
    enc.Process(cipher, msg, bs);
 
866
    dec.Process(plain, cipher, bs);
702
867
 
703
 
    if (memcmp(global_plain, global_msg, bs))
 
868
    if (memcmp(plain, msg, bs))
704
869
        return -60;
705
870
 
706
871
    const byte verify[] = 
709
874
        0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
710
875
    };
711
876
 
712
 
    if (memcmp(global_cipher, verify, bs))
 
877
    if (memcmp(cipher, verify, bs))
713
878
        return -61;
714
879
 
715
880
    AES_ECB_Encryption enc2;
718
883
    enc2.SetKey(key, bs, iv);
719
884
    dec2.SetKey(key, bs, iv);
720
885
 
721
 
    enc2.Process(global_cipher, global_msg, bs);
722
 
    dec2.Process(global_plain, global_cipher, bs);
 
886
    enc2.Process(cipher, msg, bs);
 
887
    dec2.Process(plain, cipher, bs);
723
888
 
724
 
    if (memcmp(global_plain, global_msg, bs))
 
889
    if (memcmp(plain, msg, bs))
725
890
        return -62;
726
891
 
727
892
    const byte verify2[] = 
730
895
        0xc8,0x8c,0x33,0x3b,0xb5,0x8f,0x85,0xd1
731
896
    };
732
897
 
733
 
    if (memcmp(global_cipher, verify2, bs))
 
898
    if (memcmp(cipher, verify2, bs))
734
899
        return -63;
735
900
 
736
901
    return 0;
749
914
    enc.SetKey(key, bs, iv);
750
915
    dec.SetKey(key, bs, iv);
751
916
 
752
 
    enc.Process(global_cipher, global_msg, bs);
753
 
    dec.Process(global_plain, global_cipher, bs);
 
917
    enc.Process(cipher, msg, bs);
 
918
    dec.Process(plain, cipher, bs);
754
919
 
755
 
    if (memcmp(global_plain, global_msg, bs))
 
920
    if (memcmp(plain, msg, bs))
756
921
        return -60;
757
922
 
758
923
    const byte verify[] = 
761
926
        0x21,0x03,0x58,0x79,0x5F,0x02,0x27,0x2C
762
927
    };
763
928
 
764
 
    if (memcmp(global_cipher, verify, bs))
 
929
    if (memcmp(cipher, verify, bs))
765
930
        return -61;
766
931
 
767
932
    Twofish_ECB_Encryption enc2;
770
935
    enc2.SetKey(key, bs, iv);
771
936
    dec2.SetKey(key, bs, iv);
772
937
 
773
 
    enc2.Process(global_cipher, global_msg, bs);
774
 
    dec2.Process(global_plain, global_cipher, bs);
 
938
    enc2.Process(cipher, msg, bs);
 
939
    dec2.Process(plain, cipher, bs);
775
940
 
776
 
    if (memcmp(global_plain, global_msg, bs))
 
941
    if (memcmp(plain, msg, bs))
777
942
        return -62;
778
943
 
779
944
    const byte verify2[] = 
782
947
        0xC4,0xCD,0x6B,0x91,0x14,0xC5,0x3A,0x09
783
948
    };
784
949
 
785
 
    if (memcmp(global_cipher, verify2, bs))
 
950
    if (memcmp(cipher, verify2, bs))
786
951
        return -63;
787
952
 
788
953
    return 0;
801
966
    enc.SetKey(key, 16, iv);
802
967
    dec.SetKey(key, 16, iv);
803
968
 
804
 
    enc.Process(global_cipher, global_msg, bs * 2);
805
 
    dec.Process(global_plain, global_cipher, bs * 2);
 
969
    enc.Process(cipher, msg, bs * 2);
 
970
    dec.Process(plain, cipher, bs * 2);
806
971
 
807
 
    if (memcmp(global_plain, global_msg, bs))
 
972
    if (memcmp(plain, msg, bs))
808
973
        return -60;
809
974
 
810
975
    const byte verify[] = 
813
978
        0xBC,0xD9,0x08,0xC4,0x94,0x6C,0x89,0xA3
814
979
    };
815
980
 
816
 
    if (memcmp(global_cipher, verify, bs))
 
981
    if (memcmp(cipher, verify, bs))
817
982
        return -61;
818
983
 
819
984
    Blowfish_ECB_Encryption enc2;
822
987
    enc2.SetKey(key, 16, iv);
823
988
    dec2.SetKey(key, 16, iv);
824
989
 
825
 
    enc2.Process(global_cipher, global_msg, bs * 2);
826
 
    dec2.Process(global_plain, global_cipher, bs * 2);
 
990
    enc2.Process(cipher, msg, bs * 2);
 
991
    dec2.Process(plain, cipher, bs * 2);
827
992
 
828
 
    if (memcmp(global_plain, global_msg, bs))
 
993
    if (memcmp(plain, msg, bs))
829
994
        return -62;
830
995
 
831
996
    const byte verify2[] = 
834
999
        0x8F,0xCE,0x39,0x32,0xDE,0xD7,0xBC,0x5B
835
1000
    };
836
1001
 
837
 
    if (memcmp(global_cipher, verify2, bs))
 
1002
    if (memcmp(cipher, verify2, bs))
838
1003
        return -63;
839
1004
 
840
1005
    return 0;