~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// test.cpp
 
2
// test taocrypt functionality
 
3
 
 
4
#include <string.h>
 
5
#include <stdio.h>
 
6
 
 
7
#include "runtime.hpp"
 
8
#include "sha.hpp"
 
9
#include "md5.hpp"
 
10
#include "md2.hpp"
 
11
#include "md4.hpp"
 
12
#include "ripemd.hpp"
 
13
#include "hmac.hpp"
 
14
#include "arc4.hpp"
 
15
#include "des.hpp"
 
16
#include "rsa.hpp"
 
17
#include "dsa.hpp"
 
18
#include "aes.hpp"
 
19
#include "twofish.hpp"
 
20
#include "blowfish.hpp"
 
21
#include "asn.hpp"
 
22
#include "dh.hpp"
 
23
#include "coding.hpp"
 
24
#include "random.hpp"
 
25
#include "pwdbased.hpp"
 
26
 
 
27
 
 
28
 
 
29
using TaoCrypt::byte;
 
30
using TaoCrypt::word32;
 
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
 
38
using TaoCrypt::MD5;
 
39
using TaoCrypt::MD2;
 
40
using TaoCrypt::MD4;
 
41
using TaoCrypt::RIPEMD160;
 
42
using TaoCrypt::HMAC;
 
43
using TaoCrypt::ARC4;
 
44
using TaoCrypt::DES_EDE3_CBC_Encryption;
 
45
using TaoCrypt::DES_EDE3_CBC_Decryption;
 
46
using TaoCrypt::DES_CBC_Encryption;
 
47
using TaoCrypt::DES_CBC_Decryption;
 
48
using TaoCrypt::DES_ECB_Encryption;
 
49
using TaoCrypt::DES_ECB_Decryption;
 
50
using TaoCrypt::AES_CBC_Encryption;
 
51
using TaoCrypt::AES_CBC_Decryption;
 
52
using TaoCrypt::AES_ECB_Encryption;
 
53
using TaoCrypt::AES_ECB_Decryption;
 
54
using TaoCrypt::Twofish_CBC_Encryption;
 
55
using TaoCrypt::Twofish_CBC_Decryption;
 
56
using TaoCrypt::Twofish_ECB_Encryption;
 
57
using TaoCrypt::Twofish_ECB_Decryption;
 
58
using TaoCrypt::Blowfish_CBC_Encryption;
 
59
using TaoCrypt::Blowfish_CBC_Decryption;
 
60
using TaoCrypt::Blowfish_ECB_Encryption;
 
61
using TaoCrypt::Blowfish_ECB_Decryption;
 
62
using TaoCrypt::RSA_PrivateKey;
 
63
using TaoCrypt::RSA_PublicKey;
 
64
using TaoCrypt::DSA_PrivateKey;
 
65
using TaoCrypt::DSA_PublicKey;
 
66
using TaoCrypt::DSA_Signer;
 
67
using TaoCrypt::DSA_Verifier;
 
68
using TaoCrypt::RSAES_Encryptor;
 
69
using TaoCrypt::RSAES_Decryptor;
 
70
using TaoCrypt::Source;
 
71
using TaoCrypt::FileSource;
 
72
using TaoCrypt::FileSource;
 
73
using TaoCrypt::HexDecoder;
 
74
using TaoCrypt::HexEncoder;
 
75
using TaoCrypt::Base64Decoder;
 
76
using TaoCrypt::Base64Encoder;
 
77
using TaoCrypt::CertDecoder;
 
78
using TaoCrypt::DH;
 
79
using TaoCrypt::EncodeDSA_Signature;
 
80
using TaoCrypt::DecodeDSA_Signature;
 
81
using TaoCrypt::PBKDF2_HMAC;
 
82
using TaoCrypt::tcArrayDelete;
 
83
using TaoCrypt::GetCert;
 
84
using TaoCrypt::GetPKCS_Cert;
 
85
 
 
86
 
 
87
struct testVector {
 
88
    byte*  input_;
 
89
    byte*  output_; 
 
90
    size_t inLen_;
 
91
    size_t outLen_;
 
92
 
 
93
    testVector(const char* in, const char* out) : input_((byte*)in),
 
94
               output_((byte*)out), inLen_(strlen(in)), outLen_(strlen(out)) {}
 
95
};
 
96
 
 
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();
 
104
int  md5_test();
 
105
int  md2_test();
 
106
int  md4_test();
 
107
int  ripemd_test();
 
108
int  hmac_test();
 
109
int  arc4_test();
 
110
int  des_test();
 
111
int  aes_test();
 
112
int  twofish_test();
 
113
int  blowfish_test();
 
114
int  rsa_test();
 
115
int  dsa_test();
 
116
int  dh_test();
 
117
int  pwdbased_test();
 
118
int  pkcs12_test();
 
119
 
 
120
TaoCrypt::RandomNumberGenerator rng;
 
121
 
 
122
 
 
123
void err_sys(const char* msg, int es)
 
124
{
 
125
    printf("%s", msg);
 
126
    exit(es);    
 
127
}
 
128
 
 
129
// func_args from test.hpp, so don't have to pull in other junk
 
130
struct func_args {
 
131
    int    argc;
 
132
    char** argv;
 
133
    int    return_code;
 
134
};
 
135
 
 
136
 
 
137
/* 
 
138
   DES, AES, Blowfish, and Twofish need aligned (4 byte) input/output for
 
139
   processing, can turn this off by setting gpBlock(assumeAligned = false)
 
140
   but would hurt performance.  yaSSL always uses dynamic memory so we have
 
141
   at least 8 byte alignment.  This test tried to force alignment for stack
 
142
   variables (for convenience) but some compiler versions and optimizations
 
143
   seemed to be off.  So we have msgTmp variable which we copy into dynamic
 
144
   memory at runtime to ensure proper alignment, along with plain/cipher.
 
145
   Whew!
 
146
*/
 
147
const byte msgTmp[] = { // "now is the time for all " w/o trailing 0
 
148
    0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
 
149
    0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
 
150
    0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
 
151
};
 
152
 
 
153
byte* msg    = 0;   // for block cipher input
 
154
byte* plain  = 0;   // for cipher decrypt comparison 
 
155
byte* cipher = 0;   // block output
 
156
 
 
157
 
 
158
void taocrypt_test(void* args)
 
159
{
 
160
    ((func_args*)args)->return_code = -1; // error state
 
161
    
 
162
    msg    = NEW_TC byte[24];
 
163
    plain  = NEW_TC byte[24];
 
164
    cipher = NEW_TC byte[24];
 
165
 
 
166
    memcpy(msg, msgTmp, 24);
 
167
 
 
168
    int ret = 0;
 
169
    if ( (ret = sha_test()) ) 
 
170
        err_sys("SHA      test failed!\n", ret);
 
171
    else
 
172
        printf( "SHA      test passed!\n");
 
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
 
 
198
    if ( (ret = md5_test()) ) 
 
199
        err_sys("MD5      test failed!\n", ret);
 
200
    else
 
201
        printf( "MD5      test passed!\n");
 
202
 
 
203
    if ( (ret = md2_test()) ) 
 
204
        err_sys("MD2      test failed!\n", ret);
 
205
    else
 
206
        printf( "MD2      test passed!\n");
 
207
 
 
208
    if ( (ret = md4_test()) ) 
 
209
        err_sys("MD4      test failed!\n", ret);
 
210
    else
 
211
        printf( "MD4      test passed!\n");
 
212
 
 
213
    if ( (ret = ripemd_test()) )
 
214
        err_sys("RIPEMD   test failed!\n", ret);
 
215
    else
 
216
        printf( "RIPEMD   test passed!\n");
 
217
 
 
218
    if ( ( ret = hmac_test()) )
 
219
        err_sys("HMAC     test failed!\n", ret);
 
220
    else
 
221
        printf( "HMAC     test passed!\n");
 
222
 
 
223
    if ( (ret = arc4_test()) )
 
224
        err_sys("ARC4     test failed!\n", ret);
 
225
    else
 
226
        printf( "ARC4     test passed!\n");
 
227
 
 
228
    if ( (ret = des_test()) )
 
229
        err_sys("DES      test failed!\n", ret);
 
230
    else
 
231
        printf( "DES      test passed!\n");
 
232
 
 
233
    if ( (ret = aes_test()) )
 
234
        err_sys("AES      test failed!\n", ret);
 
235
    else
 
236
        printf( "AES      test passed!\n");
 
237
 
 
238
    if ( (ret = twofish_test()) )
 
239
        err_sys("Twofish  test failed!\n", ret);
 
240
    else
 
241
        printf( "Twofish  test passed!\n");
 
242
 
 
243
    if ( (ret = blowfish_test()) )
 
244
        err_sys("Blowfish test failed!\n", ret);
 
245
    else
 
246
        printf( "Blowfish test passed!\n");
 
247
 
 
248
    if ( (ret = rsa_test()) )
 
249
        err_sys("RSA      test failed!\n", ret);
 
250
    else
 
251
        printf( "RSA      test passed!\n");
 
252
 
 
253
    if ( (ret = dh_test()) )
 
254
        err_sys("DH       test failed!\n", ret);
 
255
    else
 
256
        printf( "DH       test passed!\n");
 
257
 
 
258
    if ( (ret = dsa_test()) )
 
259
        err_sys("DSA      test failed!\n", ret);
 
260
    else
 
261
        printf( "DSA      test passed!\n");
 
262
 
 
263
    if ( (ret = pwdbased_test()) )
 
264
        err_sys("PBKDF2   test failed!\n", ret);
 
265
    else
 
266
        printf( "PBKDF2   test passed!\n");
 
267
 
 
268
    /* not ready yet
 
269
    if ( (ret = pkcs12_test()) )
 
270
        err_sys("PKCS12   test failed!\n", ret);
 
271
    else
 
272
        printf( "PKCS12   test passed!\n");
 
273
    */
 
274
 
 
275
    tcArrayDelete(cipher);
 
276
    tcArrayDelete(plain);
 
277
    tcArrayDelete(msg);
 
278
 
 
279
    ((func_args*)args)->return_code = ret;
 
280
}
 
281
 
 
282
 
 
283
// so overall tests can pull in test function 
 
284
#ifndef NO_MAIN_DRIVER
 
285
 
 
286
    int main(int argc, char** argv)
 
287
    {
 
288
        func_args args;
 
289
 
 
290
        args.argc = argc;
 
291
        args.argv = argv;
 
292
 
 
293
        taocrypt_test(&args);
 
294
        TaoCrypt::CleanUp();
 
295
 
 
296
        return args.return_code;
 
297
    }
 
298
 
 
299
#endif // NO_MAIN_DRIVER
 
300
 
 
301
 
 
302
void file_test(const char* file, byte* check)
 
303
{
 
304
    FILE* f;
 
305
    int   i(0);
 
306
    MD5   md5;
 
307
    byte  buf[1024];
 
308
    byte  md5sum[MD5::DIGEST_SIZE];
 
309
    
 
310
    if( !( f = fopen( file, "rb" ) )) {
 
311
        printf("Can't open %s\n", file);
 
312
        return;
 
313
    }
 
314
    while( ( i = fread(buf, 1, sizeof(buf), f )) > 0 )
 
315
        md5.Update(buf, i);
 
316
    
 
317
    md5.Final(md5sum);
 
318
    memcpy(check, md5sum, sizeof(md5sum));
 
319
 
 
320
    for(int j = 0; j < MD5::DIGEST_SIZE; ++j ) 
 
321
        printf( "%02x", md5sum[j] );
 
322
   
 
323
    printf("  %s\n", file);
 
324
 
 
325
    fclose(f);
 
326
}
 
327
 
 
328
 
 
329
int sha_test()
 
330
{
 
331
    SHA  sha;
 
332
    byte hash[SHA::DIGEST_SIZE];
 
333
 
 
334
    testVector test_sha[] =
 
335
    {
 
336
        testVector("abc", 
 
337
                 "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
 
338
                 "\x6C\x9C\xD0\xD8\x9D"),
 
339
        testVector("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
 
340
                 "\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE\x4A\xA1\xF9\x51\x29"
 
341
                 "\xE5\xE5\x46\x70\xF1"),
 
342
        testVector("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
 
343
                 "aaaaaa", 
 
344
                 "\x00\x98\xBA\x82\x4B\x5C\x16\x42\x7B\xD7\xA1\x12\x2A\x5A\x44"
 
345
                 "\x2A\x25\xEC\x64\x4D"),
 
346
        testVector("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
 
347
                 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
 
348
                 "aaaaaaaaaa",
 
349
                 "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
 
350
                 "\x53\x99\x5E\x26\xA0")  
 
351
    };
 
352
 
 
353
    int times( sizeof(test_sha) / sizeof(testVector) );
 
354
    for (int i = 0; i < times; ++i) {
 
355
        sha.Update(test_sha[i].input_, test_sha[i].inLen_);
 
356
        sha.Final(hash);
 
357
 
 
358
        if (memcmp(hash, test_sha[i].output_, SHA::DIGEST_SIZE) != 0)
 
359
            return -1 - i;
 
360
    }
 
361
 
 
362
    return 0;
 
363
}
 
364
 
 
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
 
 
496
int md5_test()
 
497
{
 
498
    MD5  md5;
 
499
    byte hash[MD5::DIGEST_SIZE];
 
500
 
 
501
    testVector test_md5[] =
 
502
    {
 
503
        testVector("abc", 
 
504
                 "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
 
505
                 "\x72"),
 
506
        testVector("message digest", 
 
507
                 "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61"
 
508
                 "\xd0"),
 
509
        testVector("abcdefghijklmnopqrstuvwxyz",
 
510
                 "\xc3\xfc\xd3\xd7\x61\x92\xe4\x00\x7d\xfb\x49\x6c\xca\x67\xe1"
 
511
                 "\x3b"),
 
512
        testVector("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
 
513
                 "6789",
 
514
                 "\xd1\x74\xab\x98\xd2\x77\xd9\xf5\xa5\x61\x1c\x2c\x9f\x41\x9d"
 
515
                 "\x9f"),
 
516
        testVector("1234567890123456789012345678901234567890123456789012345678"
 
517
                 "9012345678901234567890",
 
518
                 "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
 
519
                 "\x7a")
 
520
    };
 
521
 
 
522
    int times( sizeof(test_md5) / sizeof(testVector) );
 
523
    for (int i = 0; i < times; ++i) {
 
524
        md5.Update(test_md5[i].input_, test_md5[i].inLen_);
 
525
        md5.Final(hash);
 
526
 
 
527
        if (memcmp(hash, test_md5[i].output_, MD5::DIGEST_SIZE) != 0)
 
528
            return -5 - i;
 
529
    }
 
530
 
 
531
    return 0;
 
532
}
 
533
 
 
534
 
 
535
int md4_test()
 
536
{
 
537
    MD4  md4;
 
538
    byte hash[MD4::DIGEST_SIZE];
 
539
 
 
540
    testVector test_md4[] =
 
541
    {
 
542
        testVector("",
 
543
                 "\x31\xd6\xcf\xe0\xd1\x6a\xe9\x31\xb7\x3c\x59\xd7\xe0\xc0\x89"
 
544
                 "\xc0"),
 
545
        testVector("a",
 
546
                 "\xbd\xe5\x2c\xb3\x1d\xe3\x3e\x46\x24\x5e\x05\xfb\xdb\xd6\xfb"
 
547
                 "\x24"),
 
548
        testVector("abc", 
 
549
                 "\xa4\x48\x01\x7a\xaf\x21\xd8\x52\x5f\xc1\x0a\xe8\x7a\xa6\x72"
 
550
                 "\x9d"),
 
551
        testVector("message digest", 
 
552
                 "\xd9\x13\x0a\x81\x64\x54\x9f\xe8\x18\x87\x48\x06\xe1\xc7\x01"
 
553
                 "\x4b"),
 
554
        testVector("abcdefghijklmnopqrstuvwxyz",
 
555
                 "\xd7\x9e\x1c\x30\x8a\xa5\xbb\xcd\xee\xa8\xed\x63\xdf\x41\x2d"
 
556
                 "\xa9"),
 
557
        testVector("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
 
558
                 "6789",
 
559
                 "\x04\x3f\x85\x82\xf2\x41\xdb\x35\x1c\xe6\x27\xe1\x53\xe7\xf0"
 
560
                 "\xe4"),
 
561
        testVector("1234567890123456789012345678901234567890123456789012345678"
 
562
                 "9012345678901234567890",
 
563
                 "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f\xcc\x05"
 
564
                 "\x36")
 
565
    };
 
566
 
 
567
    int times( sizeof(test_md4) / sizeof(testVector) );
 
568
    for (int i = 0; i < times; ++i) {
 
569
        md4.Update(test_md4[i].input_, test_md4[i].inLen_);
 
570
        md4.Final(hash);
 
571
 
 
572
        if (memcmp(hash, test_md4[i].output_, MD4::DIGEST_SIZE) != 0)
 
573
            return -5 - i;
 
574
    }
 
575
 
 
576
    return 0;
 
577
}
 
578
 
 
579
 
 
580
int md2_test()
 
581
{
 
582
    MD2  md5;
 
583
    byte hash[MD2::DIGEST_SIZE];
 
584
 
 
585
    testVector test_md2[] =
 
586
    {
 
587
        testVector("",
 
588
                   "\x83\x50\xe5\xa3\xe2\x4c\x15\x3d\xf2\x27\x5c\x9f\x80\x69"
 
589
                   "\x27\x73"),
 
590
        testVector("a",
 
591
                   "\x32\xec\x01\xec\x4a\x6d\xac\x72\xc0\xab\x96\xfb\x34\xc0"
 
592
                   "\xb5\xd1"),
 
593
        testVector("abc",
 
594
                   "\xda\x85\x3b\x0d\x3f\x88\xd9\x9b\x30\x28\x3a\x69\xe6\xde"
 
595
                   "\xd6\xbb"),
 
596
        testVector("message digest",
 
597
                   "\xab\x4f\x49\x6b\xfb\x2a\x53\x0b\x21\x9f\xf3\x30\x31\xfe"
 
598
                   "\x06\xb0"),
 
599
        testVector("abcdefghijklmnopqrstuvwxyz",
 
600
                   "\x4e\x8d\xdf\xf3\x65\x02\x92\xab\x5a\x41\x08\xc3\xaa\x47"
 
601
                   "\x94\x0b"),
 
602
        testVector("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
 
603
                   "0123456789",
 
604
                   "\xda\x33\xde\xf2\xa4\x2d\xf1\x39\x75\x35\x28\x46\xc3\x03"
 
605
                   "\x38\xcd"),
 
606
        testVector("12345678901234567890123456789012345678901234567890123456"
 
607
                   "789012345678901234567890",
 
608
                   "\xd5\x97\x6f\x79\xd8\x3d\x3a\x0d\xc9\x80\x6c\x3c\x66\xf3"
 
609
                   "\xef\xd8")
 
610
    };
 
611
 
 
612
    int times( sizeof(test_md2) / sizeof(testVector) );
 
613
    for (int i = 0; i < times; ++i) {
 
614
        md5.Update(test_md2[i].input_, test_md2[i].inLen_);
 
615
        md5.Final(hash);
 
616
 
 
617
        if (memcmp(hash, test_md2[i].output_, MD2::DIGEST_SIZE) != 0)
 
618
            return -10 - i;
 
619
    }
 
620
 
 
621
    return 0;
 
622
}
 
623
 
 
624
 
 
625
int ripemd_test()
 
626
{
 
627
    RIPEMD160  ripe160;
 
628
    byte hash[RIPEMD160::DIGEST_SIZE];
 
629
 
 
630
    testVector test_ripemd[] =
 
631
    {
 
632
        testVector("",
 
633
                   "\x9c\x11\x85\xa5\xc5\xe9\xfc\x54\x61\x28\x08\x97\x7e\xe8"
 
634
                   "\xf5\x48\xb2\x25\x8d\x31"),
 
635
        testVector("a",
 
636
                   "\x0b\xdc\x9d\x2d\x25\x6b\x3e\xe9\xda\xae\x34\x7b\xe6\xf4"
 
637
                   "\xdc\x83\x5a\x46\x7f\xfe"),
 
638
        testVector("abc",
 
639
                   "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
 
640
                   "\xb0\x87\xf1\x5a\x0b\xfc"),
 
641
        testVector("message digest",
 
642
                   "\x5d\x06\x89\xef\x49\xd2\xfa\xe5\x72\xb8\x81\xb1\x23\xa8"
 
643
                   "\x5f\xfa\x21\x59\x5f\x36"),
 
644
        testVector("abcdefghijklmnopqrstuvwxyz",
 
645
                   "\xf7\x1c\x27\x10\x9c\x69\x2c\x1b\x56\xbb\xdc\xeb\x5b\x9d"
 
646
                   "\x28\x65\xb3\x70\x8d\xbc"),
 
647
        testVector("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
 
648
                   "\x12\xa0\x53\x38\x4a\x9c\x0c\x88\xe4\x05\xa0\x6c\x27\xdc"
 
649
                   "\xf4\x9a\xda\x62\xeb\x2b"),
 
650
        testVector("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123"
 
651
                   "456789",
 
652
                   "\xb0\xe2\x0b\x6e\x31\x16\x64\x02\x86\xed\x3a\x87\xa5\x71"
 
653
                   "\x30\x79\xb2\x1f\x51\x89"),
 
654
        testVector("12345678901234567890123456789012345678901234567890123456"
 
655
                   "789012345678901234567890",
 
656
                   "\x9b\x75\x2e\x45\x57\x3d\x4b\x39\xf4\xdb\xd3\x32\x3c\xab"
 
657
                   "\x82\xbf\x63\x32\x6b\xfb"),
 
658
    };
 
659
 
 
660
    int times( sizeof(test_ripemd) / sizeof(testVector) );
 
661
    for (int i = 0; i < times; ++i) {
 
662
        ripe160.Update(test_ripemd[i].input_, test_ripemd[i].inLen_);
 
663
        ripe160.Final(hash);
 
664
 
 
665
        if (memcmp(hash, test_ripemd[i].output_, RIPEMD160::DIGEST_SIZE) != 0)
 
666
            return -100 - i;
 
667
    }
 
668
 
 
669
    return 0;
 
670
}
 
671
 
 
672
 
 
673
int hmac_test()
 
674
{
 
675
    HMAC<MD5> hmacMD5;
 
676
    byte hash[MD5::DIGEST_SIZE];
 
677
 
 
678
    const char* keys[]=
 
679
    {
 
680
        "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
 
681
        "Jefe",
 
682
        "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
 
683
    };
 
684
 
 
685
    testVector test_hmacMD5[] = 
 
686
    {
 
687
        testVector("Hi There",
 
688
                 "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
 
689
                 "\x9d"),
 
690
        testVector("what do ya want for nothing?",
 
691
                 "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7"
 
692
                 "\x38"),
 
693
        testVector("\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
 
694
                 "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
 
695
                 "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
 
696
                 "\xDD\xDD\xDD\xDD\xDD\xDD",
 
697
                 "\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3"
 
698
                 "\xf6")
 
699
    };
 
700
 
 
701
    int times( sizeof(test_hmacMD5) / sizeof(testVector) );
 
702
    for (int i = 0; i < times; ++i) {
 
703
        hmacMD5.SetKey((byte*)keys[i], strlen(keys[i]));
 
704
        hmacMD5.Update(test_hmacMD5[i].input_, test_hmacMD5[i].inLen_);
 
705
        hmacMD5.Final(hash);
 
706
 
 
707
        if (memcmp(hash, test_hmacMD5[i].output_, MD5::DIGEST_SIZE) != 0)
 
708
            return -20 - i;
 
709
    }
 
710
 
 
711
    return 0;
 
712
}
 
713
 
 
714
 
 
715
int arc4_test()
 
716
{
 
717
    byte cipher[16];
 
718
    byte plain[16];
 
719
 
 
720
    const char* keys[] = 
 
721
    {           
 
722
        "\x01\x23\x45\x67\x89\xab\xcd\xef",
 
723
        "\x01\x23\x45\x67\x89\xab\xcd\xef",
 
724
        "\x00\x00\x00\x00\x00\x00\x00\x00",
 
725
        "\xef\x01\x23\x45"
 
726
    };
 
727
 
 
728
    testVector test_arc4[] =
 
729
    {
 
730
        testVector("\x01\x23\x45\x67\x89\xab\xcd\xef",
 
731
                   "\x75\xb7\x87\x80\x99\xe0\xc5\x96"),
 
732
        testVector("\x00\x00\x00\x00\x00\x00\x00\x00",
 
733
                   "\x74\x94\xc2\xe7\x10\x4b\x08\x79"),
 
734
        testVector("\x00\x00\x00\x00\x00\x00\x00\x00",
 
735
                   "\xde\x18\x89\x41\xa3\x37\x5d\x3a"),
 
736
        testVector("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
 
737
                   "\xd6\xa1\x41\xa7\xec\x3c\x38\xdf\xbd\x61")
 
738
    };
 
739
 
 
740
 
 
741
    int times( sizeof(test_arc4) / sizeof(testVector) );
 
742
    for (int i = 0; i < times; ++i) {
 
743
        ARC4::Encryption enc;
 
744
        ARC4::Decryption dec;
 
745
 
 
746
        enc.SetKey((byte*)keys[i], strlen(keys[i]));
 
747
        dec.SetKey((byte*)keys[i], strlen(keys[i]));
 
748
 
 
749
        enc.Process(cipher, test_arc4[i].input_, test_arc4[i].outLen_);
 
750
        dec.Process(plain,  cipher, test_arc4[i].outLen_);
 
751
 
 
752
        if (memcmp(plain, test_arc4[i].input_, test_arc4[i].outLen_))
 
753
            return -30 - i;
 
754
 
 
755
        if (memcmp(cipher, test_arc4[i].output_, test_arc4[i].outLen_))
 
756
            return -40 - i;
 
757
    }
 
758
 
 
759
    return 0;
 
760
}
 
761
 
 
762
 
 
763
int des_test()
 
764
{
 
765
    //ECB mode
 
766
    DES_ECB_Encryption enc;
 
767
    DES_ECB_Decryption dec;
 
768
 
 
769
    const int sz = TaoCrypt::DES_BLOCK_SIZE * 3;
 
770
    const byte key[] = { 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef };
 
771
    const byte iv[] =  { 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef };
 
772
 
 
773
    enc.SetKey(key, sizeof(key));
 
774
    enc.Process(cipher, msg, sz);
 
775
    dec.SetKey(key, sizeof(key));
 
776
    dec.Process(plain, cipher, sz);
 
777
 
 
778
    if (memcmp(plain, msg, sz))
 
779
        return -50;
 
780
 
 
781
    const byte verify1[] = 
 
782
    {
 
783
        0xf9,0x99,0xb8,0x8e,0xaf,0xea,0x71,0x53,
 
784
        0x6a,0x27,0x17,0x87,0xab,0x88,0x83,0xf9,
 
785
        0x89,0x3d,0x51,0xec,0x4b,0x56,0x3b,0x53
 
786
    };
 
787
 
 
788
    if (memcmp(cipher, verify1, sz))
 
789
        return -51;
 
790
 
 
791
    // CBC mode
 
792
    DES_CBC_Encryption enc2;
 
793
    DES_CBC_Decryption dec2;
 
794
 
 
795
    enc2.SetKey(key, sizeof(key), iv);
 
796
    enc2.Process(cipher, msg, sz);
 
797
    dec2.SetKey(key, sizeof(key), iv);
 
798
    dec2.Process(plain, cipher, sz);
 
799
 
 
800
    if (memcmp(plain, msg, sz))
 
801
        return -52;
 
802
 
 
803
    const byte verify2[] = 
 
804
    {
 
805
        0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
 
806
        0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
 
807
        0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
 
808
    };
 
809
 
 
810
    if (memcmp(cipher, verify2, sz))
 
811
        return -53;
 
812
 
 
813
    // EDE3 CBC mode
 
814
    DES_EDE3_CBC_Encryption enc3;
 
815
    DES_EDE3_CBC_Decryption dec3;
 
816
 
 
817
    const byte key3[] = 
 
818
    {
 
819
        0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
 
820
        0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
 
821
        0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
 
822
    };
 
823
    const byte iv3[] = 
 
824
    {
 
825
        0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
 
826
        0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
 
827
        0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
 
828
        
 
829
    };
 
830
 
 
831
    enc3.SetKey(key3, sizeof(key3), iv3);
 
832
    enc3.Process(cipher, msg, sz);
 
833
    dec3.SetKey(key3, sizeof(key3), iv3);
 
834
    dec3.Process(plain, cipher, sz);
 
835
 
 
836
    if (memcmp(plain, msg, sz))
 
837
        return -54;
 
838
 
 
839
    const byte verify3[] = 
 
840
    {
 
841
        0x08,0x8a,0xae,0xe6,0x9a,0xa9,0xc1,0x13,
 
842
        0x93,0x7d,0xf7,0x3a,0x11,0x56,0x66,0xb3,
 
843
        0x18,0xbc,0xbb,0x6d,0xd2,0xb1,0x16,0xda
 
844
    };
 
845
 
 
846
    if (memcmp(cipher, verify3, sz))
 
847
        return -55;
 
848
 
 
849
    return 0;
 
850
}
 
851
 
 
852
 
 
853
int aes_test()
 
854
{
 
855
    AES_CBC_Encryption enc;
 
856
    AES_CBC_Decryption dec;
 
857
    const int bs(TaoCrypt::AES::BLOCK_SIZE);
 
858
 
 
859
    byte key[] = "0123456789abcdef   ";  // align
 
860
    byte iv[]  = "1234567890abcdef   ";  // align
 
861
 
 
862
    enc.SetKey(key, bs, iv);
 
863
    dec.SetKey(key, bs, iv);
 
864
 
 
865
    enc.Process(cipher, msg, bs);
 
866
    dec.Process(plain, cipher, bs);
 
867
 
 
868
    if (memcmp(plain, msg, bs))
 
869
        return -60;
 
870
 
 
871
    const byte verify[] = 
 
872
    {
 
873
        0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
 
874
        0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
 
875
    };
 
876
 
 
877
    if (memcmp(cipher, verify, bs))
 
878
        return -61;
 
879
 
 
880
    AES_ECB_Encryption enc2;
 
881
    AES_ECB_Decryption dec2;
 
882
 
 
883
    enc2.SetKey(key, bs, iv);
 
884
    dec2.SetKey(key, bs, iv);
 
885
 
 
886
    enc2.Process(cipher, msg, bs);
 
887
    dec2.Process(plain, cipher, bs);
 
888
 
 
889
    if (memcmp(plain, msg, bs))
 
890
        return -62;
 
891
 
 
892
    const byte verify2[] = 
 
893
    {
 
894
        0xd0,0xc9,0xd9,0xc9,0x40,0xe8,0x97,0xb6,
 
895
        0xc8,0x8c,0x33,0x3b,0xb5,0x8f,0x85,0xd1
 
896
    };
 
897
 
 
898
    if (memcmp(cipher, verify2, bs))
 
899
        return -63;
 
900
 
 
901
    return 0;
 
902
}
 
903
 
 
904
 
 
905
int twofish_test()
 
906
{
 
907
    Twofish_CBC_Encryption enc;
 
908
    Twofish_CBC_Decryption dec;
 
909
    const int bs(TaoCrypt::Twofish::BLOCK_SIZE);
 
910
 
 
911
    byte key[] = "0123456789abcdef   ";  // align
 
912
    byte iv[]  = "1234567890abcdef   ";  // align
 
913
 
 
914
    enc.SetKey(key, bs, iv);
 
915
    dec.SetKey(key, bs, iv);
 
916
 
 
917
    enc.Process(cipher, msg, bs);
 
918
    dec.Process(plain, cipher, bs);
 
919
 
 
920
    if (memcmp(plain, msg, bs))
 
921
        return -60;
 
922
 
 
923
    const byte verify[] = 
 
924
    {
 
925
        0xD2,0xD7,0x47,0x47,0x4A,0x65,0x4E,0x16,
 
926
        0x21,0x03,0x58,0x79,0x5F,0x02,0x27,0x2C
 
927
    };
 
928
 
 
929
    if (memcmp(cipher, verify, bs))
 
930
        return -61;
 
931
 
 
932
    Twofish_ECB_Encryption enc2;
 
933
    Twofish_ECB_Decryption dec2;
 
934
 
 
935
    enc2.SetKey(key, bs, iv);
 
936
    dec2.SetKey(key, bs, iv);
 
937
 
 
938
    enc2.Process(cipher, msg, bs);
 
939
    dec2.Process(plain, cipher, bs);
 
940
 
 
941
    if (memcmp(plain, msg, bs))
 
942
        return -62;
 
943
 
 
944
    const byte verify2[] = 
 
945
    {
 
946
        0x3B,0x6C,0x63,0x10,0x34,0xAB,0xB2,0x87,
 
947
        0xC4,0xCD,0x6B,0x91,0x14,0xC5,0x3A,0x09
 
948
    };
 
949
 
 
950
    if (memcmp(cipher, verify2, bs))
 
951
        return -63;
 
952
 
 
953
    return 0;
 
954
}
 
955
 
 
956
 
 
957
int blowfish_test()
 
958
{
 
959
    Blowfish_CBC_Encryption enc;
 
960
    Blowfish_CBC_Decryption dec;
 
961
    const int bs(TaoCrypt::Blowfish::BLOCK_SIZE);
 
962
 
 
963
    byte key[] = "0123456789abcdef   ";  // align
 
964
    byte iv[]  = "1234567890abcdef   ";  // align
 
965
 
 
966
    enc.SetKey(key, 16, iv);
 
967
    dec.SetKey(key, 16, iv);
 
968
 
 
969
    enc.Process(cipher, msg, bs * 2);
 
970
    dec.Process(plain, cipher, bs * 2);
 
971
 
 
972
    if (memcmp(plain, msg, bs))
 
973
        return -60;
 
974
 
 
975
    const byte verify[] = 
 
976
    {
 
977
        0x0E,0x26,0xAA,0x29,0x11,0x25,0xAB,0xB5,
 
978
        0xBC,0xD9,0x08,0xC4,0x94,0x6C,0x89,0xA3
 
979
    };
 
980
 
 
981
    if (memcmp(cipher, verify, bs))
 
982
        return -61;
 
983
 
 
984
    Blowfish_ECB_Encryption enc2;
 
985
    Blowfish_ECB_Decryption dec2;
 
986
 
 
987
    enc2.SetKey(key, 16, iv);
 
988
    dec2.SetKey(key, 16, iv);
 
989
 
 
990
    enc2.Process(cipher, msg, bs * 2);
 
991
    dec2.Process(plain, cipher, bs * 2);
 
992
 
 
993
    if (memcmp(plain, msg, bs))
 
994
        return -62;
 
995
 
 
996
    const byte verify2[] = 
 
997
    {
 
998
        0xE7,0x42,0xB9,0x37,0xC8,0x7D,0x93,0xCA,
 
999
        0x8F,0xCE,0x39,0x32,0xDE,0xD7,0xBC,0x5B
 
1000
    };
 
1001
 
 
1002
    if (memcmp(cipher, verify2, bs))
 
1003
        return -63;
 
1004
 
 
1005
    return 0;
 
1006
}
 
1007
 
 
1008
 
 
1009
int rsa_test()
 
1010
{
 
1011
    Source source;
 
1012
    FileSource("../certs/client-key.der", source);
 
1013
    if (source.size() == 0) {
 
1014
        FileSource("../../certs/client-key.der", source);  // for testsuite
 
1015
        if (source.size() == 0) {
 
1016
            FileSource("../../../certs/client-key.der", source); // Debug dir
 
1017
            if (source.size() == 0)
 
1018
                err_sys("where's your certs dir?", -79);
 
1019
        }
 
1020
    }
 
1021
    RSA_PrivateKey priv(source);
 
1022
 
 
1023
    RSAES_Encryptor enc(priv);
 
1024
    byte message[] = "Everyone gets Friday off.";
 
1025
    const int len(strlen((char*)message));
 
1026
    byte cipher[64];
 
1027
    enc.Encrypt(message, len, cipher, rng);
 
1028
 
 
1029
    RSAES_Decryptor dec(priv);
 
1030
    byte plain[64];
 
1031
    dec.Decrypt(cipher, sizeof(plain), plain, rng);
 
1032
 
 
1033
    if (memcmp(plain, message, len))
 
1034
        return -70;
 
1035
 
 
1036
    dec.SSL_Sign(message, len, cipher, rng);
 
1037
    if (!enc.SSL_Verify(message, len, cipher))
 
1038
        return -71;
 
1039
 
 
1040
 
 
1041
    // test decode   
 
1042
    Source source2;
 
1043
    FileSource("../certs/client-cert.der", source2);
 
1044
    if (source2.size() == 0) {
 
1045
        FileSource("../../certs/client-cert.der", source2);  // for testsuite
 
1046
        if (source2.size() == 0) {
 
1047
            FileSource("../../../certs/client-cert.der", source2); // Debug dir
 
1048
            if (source2.size() == 0)
 
1049
                err_sys("where's your certs dir?", -79);
 
1050
        }
 
1051
    }
 
1052
    CertDecoder cd(source2, true, 0, false, CertDecoder::CA);
 
1053
    Source source3(cd.GetPublicKey().GetKey(), cd.GetPublicKey().size());
 
1054
    RSA_PublicKey pub(source3);
 
1055
 
 
1056
    return 0;
 
1057
}
 
1058
 
 
1059
 
 
1060
int dh_test()
 
1061
{
 
1062
    Source source;
 
1063
    FileSource("../certs/dh1024.dat", source);
 
1064
    if (source.size() == 0) {
 
1065
        FileSource("../../certs/dh1024.dat", source);  // for testsuite
 
1066
        if (source.size() == 0) {
 
1067
            FileSource("../../../certs/dh1024.dat", source); // win32 Debug dir
 
1068
            if (source.size() == 0)
 
1069
                err_sys("where's your certs dir?", -79);
 
1070
        }
 
1071
    }
 
1072
    HexDecoder hDec(source);
 
1073
 
 
1074
    DH dh(source);
 
1075
 
 
1076
    byte pub[128];
 
1077
    byte priv[128];
 
1078
    byte agree[128];
 
1079
    byte pub2[128];
 
1080
    byte priv2[128];
 
1081
    byte agree2[128];
 
1082
 
 
1083
    DH dh2(dh);
 
1084
 
 
1085
    dh.GenerateKeyPair(rng, priv, pub);
 
1086
    dh2.GenerateKeyPair(rng, priv2, pub2);
 
1087
    dh.Agree(agree, priv, pub2); 
 
1088
    dh2.Agree(agree2, priv2, pub);
 
1089
 
 
1090
    
 
1091
    if ( memcmp(agree, agree2, dh.GetByteLength()) )
 
1092
        return -80;
 
1093
 
 
1094
    return 0;
 
1095
}
 
1096
 
 
1097
 
 
1098
int dsa_test()
 
1099
{
 
1100
    Source source;
 
1101
    FileSource("../certs/dsa512.der", source);
 
1102
    if (source.size() == 0) {
 
1103
        FileSource("../../certs/dsa512.der", source);  // for testsuite
 
1104
        if (source.size() == 0) {
 
1105
            FileSource("../../../certs/dsa512.der", source); // win32 Debug dir
 
1106
            if (source.size() == 0)
 
1107
                err_sys("where's your certs dir?", -89);
 
1108
        }
 
1109
    }
 
1110
 
 
1111
    const char msg[] = "this is the message";
 
1112
    byte signature[40];
 
1113
 
 
1114
    DSA_PrivateKey priv(source);
 
1115
    DSA_Signer signer(priv);
 
1116
 
 
1117
    SHA sha;
 
1118
    byte digest[SHA::DIGEST_SIZE];
 
1119
    sha.Update((byte*)msg, sizeof(msg));
 
1120
    sha.Final(digest);
 
1121
 
 
1122
    signer.Sign(digest, signature, rng);
 
1123
 
 
1124
    byte encoded[sizeof(signature) + 6];
 
1125
    byte decoded[40];
 
1126
 
 
1127
    word32 encSz = EncodeDSA_Signature(signer.GetR(), signer.GetS(), encoded);
 
1128
    DecodeDSA_Signature(decoded, encoded, encSz);
 
1129
 
 
1130
    DSA_PublicKey pub(priv);
 
1131
    DSA_Verifier verifier(pub);
 
1132
 
 
1133
    if (!verifier.Verify(digest, decoded))
 
1134
        return -90;
 
1135
 
 
1136
    return 0;
 
1137
}
 
1138
 
 
1139
 
 
1140
int pwdbased_test()
 
1141
{
 
1142
    PBKDF2_HMAC<SHA> pb;
 
1143
 
 
1144
    byte derived[32];
 
1145
    const byte pwd1[] = "password   ";  // align
 
1146
    const byte salt[]  = { 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12 };
 
1147
    
 
1148
    pb.DeriveKey(derived, 8, pwd1, 8, salt, sizeof(salt), 5);
 
1149
 
 
1150
    const byte verify1[] = { 0xD1, 0xDA, 0xA7, 0x86, 0x15, 0xF2, 0x87, 0xE6 };
 
1151
 
 
1152
    if ( memcmp(derived, verify1, sizeof(verify1)) )
 
1153
        return -101;
 
1154
 
 
1155
 
 
1156
    const byte pwd2[] = "All n-entities must communicate with other n-entities"
 
1157
                        " via n-1 entiteeheehees   ";  // align
 
1158
 
 
1159
    pb.DeriveKey(derived, 24, pwd2, 76, salt, sizeof(salt), 500);
 
1160
 
 
1161
    const byte verify2[] = { 0x6A, 0x89, 0x70, 0xBF, 0x68, 0xC9, 0x2C, 0xAE,
 
1162
                             0xA8, 0x4A, 0x8D, 0xF2, 0x85, 0x10, 0x85, 0x86,
 
1163
                             0x07, 0x12, 0x63, 0x80, 0xCC, 0x47, 0xAB, 0x2D
 
1164
    };
 
1165
 
 
1166
    if ( memcmp(derived, verify2, sizeof(verify2)) )
 
1167
        return -102;
 
1168
 
 
1169
    return 0;
 
1170
}
 
1171
 
 
1172
 
 
1173
int pkcs12_test()
 
1174
{
 
1175
    Source cert;
 
1176
    FileSource("../certs/server-cert.pem", cert);
 
1177
    if (cert.size() == 0) {
 
1178
        FileSource("../../certs/server-cert.pem", cert);  // for testsuite
 
1179
        if (cert.size() == 0) {
 
1180
            FileSource("../../../certs/server-cert.pem", cert); // Debug dir
 
1181
            if (cert.size() == 0)
 
1182
                err_sys("where's your certs dir?", -109);
 
1183
        }
 
1184
    }
 
1185
 
 
1186
    if (GetCert(cert) != 0)
 
1187
        return -110;
 
1188
 
 
1189
    Source source;
 
1190
    FileSource("../certs/server.p12", source);
 
1191
    if (source.size() == 0) {
 
1192
        FileSource("../../certs/server.p12", source);  // for testsuite
 
1193
        if (source.size() == 0) {
 
1194
            FileSource("../../../certs/server.p12", source); // Debug dir
 
1195
            if (source.size() == 0)
 
1196
                err_sys("where's your certs dir?", -111);
 
1197
        }
 
1198
    }
 
1199
 
 
1200
    if (GetPKCS_Cert("password", source) != 0)
 
1201
        return -112;
 
1202
 
 
1203
    return 0;
 
1204
}
 
1205