]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/WolfSSL/ctaocrypt/test/test.c
Rename the CyaSSL directory to WolfSSL
[freertos] / FreeRTOS-Plus / Source / WolfSSL / ctaocrypt / test / test.c
1 /* test.c
2  *
3  * Copyright (C) 2006-2014 wolfSSL Inc.
4  *
5  * This file is part of CyaSSL.
6  *
7  * CyaSSL is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * CyaSSL is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #ifdef HAVE_CONFIG_H
23     #include <config.h>
24 #endif
25
26 #include <cyassl/ctaocrypt/settings.h>
27
28 #ifdef XMALLOC_USER
29     #include <stdlib.h>  /* we're using malloc / free direct here */
30 #endif
31
32 #ifndef NO_CRYPT_TEST
33
34 #ifdef CYASSL_TEST_CERT
35     #include <cyassl/ctaocrypt/asn.h>
36 #else
37     #include <cyassl/ctaocrypt/asn_public.h>
38 #endif
39 #include <cyassl/ctaocrypt/md2.h>
40 #include <cyassl/ctaocrypt/md5.h>
41 #include <cyassl/ctaocrypt/md4.h>
42 #include <cyassl/ctaocrypt/sha.h>
43 #include <cyassl/ctaocrypt/sha256.h>
44 #include <cyassl/ctaocrypt/sha512.h>
45 #include <cyassl/ctaocrypt/arc4.h>
46 #include <cyassl/ctaocrypt/random.h>
47 #include <cyassl/ctaocrypt/coding.h>
48 #include <cyassl/ctaocrypt/rsa.h>
49 #include <cyassl/ctaocrypt/des3.h>
50 #include <cyassl/ctaocrypt/aes.h>
51 #include <cyassl/ctaocrypt/camellia.h>
52 #include <cyassl/ctaocrypt/hmac.h>
53 #include <cyassl/ctaocrypt/dh.h>
54 #include <cyassl/ctaocrypt/dsa.h>
55 #include <cyassl/ctaocrypt/hc128.h>
56 #include <cyassl/ctaocrypt/rabbit.h>
57 #include <cyassl/ctaocrypt/pwdbased.h>
58 #include <cyassl/ctaocrypt/ripemd.h>
59 #ifdef HAVE_ECC
60     #include <cyassl/ctaocrypt/ecc.h>
61 #endif
62 #ifdef HAVE_BLAKE2
63     #include <cyassl/ctaocrypt/blake2.h>
64 #endif
65 #ifdef HAVE_LIBZ
66     #include <cyassl/ctaocrypt/compress.h>
67 #endif
68 #ifdef HAVE_PKCS7
69     #include <cyassl/ctaocrypt/pkcs7.h>
70 #endif
71
72 #ifdef _MSC_VER
73     /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
74     #pragma warning(disable: 4996)
75 #endif
76
77 #ifdef OPENSSL_EXTRA
78     #include <cyassl/openssl/evp.h>
79     #include <cyassl/openssl/rand.h>
80     #include <cyassl/openssl/hmac.h>
81     #include <cyassl/openssl/des.h>
82 #endif
83
84
85 #if defined(USE_CERT_BUFFERS_1024) || defined(USE_CERT_BUFFERS_2048)
86     /* include test cert and key buffers for use with NO_FILESYSTEM */
87     #if defined(CYASSL_MDK_ARM)
88         #include "cert_data.h"
89                         /* use certs_test.c for initial data, so other
90                                                commands can share the data. */
91     #else
92         #include <cyassl/certs_test.h>
93     #endif
94 #endif
95
96 #if defined(CYASSL_MDK_ARM)
97         #include <stdio.h>
98         #include <stdlib.h>
99     extern FILE * CyaSSL_fopen(const char *fname, const char *mode) ;
100     #define fopen CyaSSL_fopen
101 #endif
102
103 #ifdef HAVE_NTRU
104     #include "ntru_crypto.h"
105 #endif
106 #ifdef HAVE_CAVIUM
107     #include "cavium_sysdep.h"
108     #include "cavium_common.h"
109     #include "cavium_ioctl.h"
110 #endif
111
112 #ifdef FREESCALE_MQX
113     #include <mqx.h>
114     #include <fio.h>
115     #include <stdlib.h>
116 #else
117     #include <stdio.h>
118 #endif
119
120
121 #ifdef THREADX
122     /* since just testing, use THREADX log printf instead */
123     int dc_log_printf(char*, ...);
124         #undef printf
125         #define printf dc_log_printf
126 #endif
127
128 #include "ctaocrypt/test/test.h"
129
130
131 typedef struct testVector {
132     const char*  input;
133     const char*  output;
134     size_t inLen;
135     size_t outLen;
136 } testVector;
137
138 int  md2_test(void);
139 int  md5_test(void);
140 int  md4_test(void);
141 int  sha_test(void);
142 int  sha256_test(void);
143 int  sha512_test(void);
144 int  sha384_test(void);
145 int  hmac_md5_test(void);
146 int  hmac_sha_test(void);
147 int  hmac_sha256_test(void);
148 int  hmac_sha384_test(void);
149 int  hmac_sha512_test(void);
150 int  hmac_blake2b_test(void);
151 int  hkdf_test(void);
152 int  arc4_test(void);
153 int  hc128_test(void);
154 int  rabbit_test(void);
155 int  des_test(void);
156 int  des3_test(void);
157 int  aes_test(void);
158 int  aesgcm_test(void);
159 int  gmac_test(void);
160 int  aesccm_test(void);
161 int  camellia_test(void);
162 int  rsa_test(void);
163 int  dh_test(void);
164 int  dsa_test(void);
165 int  random_test(void);
166 int  pwdbased_test(void);
167 int  ripemd_test(void);
168 int  openssl_test(void);   /* test mini api */
169 int pbkdf1_test(void);
170 int pkcs12_test(void);
171 int pbkdf2_test(void);
172 #ifdef HAVE_ECC
173     int  ecc_test(void);
174     #ifdef HAVE_ECC_ENCRYPT
175         int  ecc_encrypt_test(void);
176     #endif
177 #endif
178 #ifdef HAVE_BLAKE2
179     int  blake2b_test(void);
180 #endif
181 #ifdef HAVE_LIBZ
182     int compress_test(void);
183 #endif
184 #ifdef HAVE_PKCS7
185     int pkcs7enveloped_test(void);
186     int pkcs7signed_test(void);
187 #endif
188
189
190
191 static void err_sys(const char* msg, int es)
192 {
193     printf("%s error = %d\n", msg, es);
194     #if !defined(THREADX) && !defined(CYASSL_MDK_ARM)
195         if (msg)
196         exit(es);
197     #endif
198     return;
199 }
200
201 /* func_args from test.h, so don't have to pull in other junk */
202 typedef struct func_args {
203     int    argc;
204     char** argv;
205     int    return_code;
206 } func_args;
207
208
209
210 void ctaocrypt_test(void* args)
211 {
212     int ret = 0;
213
214     ((func_args*)args)->return_code = -1; /* error state */
215
216 #if !defined(NO_BIG_INT)
217     if (CheckCtcSettings() != 1)
218         err_sys("Build vs runtime math mismatch\n", -1234);
219
220 #ifdef USE_FAST_MATH
221     if (CheckFastMathSettings() != 1)
222         err_sys("Build vs runtime fastmath FP_MAX_BITS mismatch\n", -1235);
223 #endif /* USE_FAST_MATH */
224 #endif /* !NO_BIG_INT */
225
226
227 #ifndef NO_MD5
228     if ( (ret = md5_test()) != 0)
229         err_sys("MD5      test failed!\n", ret);
230     else
231         printf( "MD5      test passed!\n");
232 #endif
233
234 #ifdef CYASSL_MD2
235     if ( (ret = md2_test()) != 0)
236         err_sys("MD2      test failed!\n", ret);
237     else
238         printf( "MD2      test passed!\n");
239 #endif
240
241 #ifndef NO_MD4
242     if ( (ret = md4_test()) != 0)
243         err_sys("MD4      test failed!\n", ret);
244     else
245         printf( "MD4      test passed!\n");
246 #endif
247
248 #ifndef NO_SHA
249     if ( (ret = sha_test()) != 0)
250         err_sys("SHA      test failed!\n", ret);
251     else
252         printf( "SHA      test passed!\n");
253 #endif
254
255 #ifndef NO_SHA256
256     if ( (ret = sha256_test()) != 0)
257         err_sys("SHA-256  test failed!\n", ret);
258     else
259         printf( "SHA-256  test passed!\n");
260 #endif
261
262 #ifdef CYASSL_SHA384
263     if ( (ret = sha384_test()) != 0)
264         err_sys("SHA-384  test failed!\n", ret);
265     else
266         printf( "SHA-384  test passed!\n");
267 #endif
268
269 #ifdef CYASSL_SHA512
270     if ( (ret = sha512_test()) != 0)
271         err_sys("SHA-512  test failed!\n", ret);
272     else
273         printf( "SHA-512  test passed!\n");
274 #endif
275
276 #ifdef CYASSL_RIPEMD
277     if ( (ret = ripemd_test()) != 0)
278         err_sys("RIPEMD   test failed!\n", ret);
279     else
280         printf( "RIPEMD   test passed!\n");
281 #endif
282
283 #ifdef HAVE_BLAKE2
284     if ( (ret = blake2b_test()) != 0)
285         err_sys("BLAKE2b  test failed!\n", ret);
286     else
287         printf( "BLAKE2b  test passed!\n");
288 #endif
289
290 #ifndef NO_HMAC
291     #ifndef NO_MD5
292         if ( (ret = hmac_md5_test()) != 0)
293             err_sys("HMAC-MD5 test failed!\n", ret);
294         else
295             printf( "HMAC-MD5 test passed!\n");
296     #endif
297
298     #ifndef NO_SHA
299     if ( (ret = hmac_sha_test()) != 0)
300         err_sys("HMAC-SHA test failed!\n", ret);
301     else
302         printf( "HMAC-SHA test passed!\n");
303     #endif
304
305     #ifndef NO_SHA256
306         if ( (ret = hmac_sha256_test()) != 0)
307             err_sys("HMAC-SHA256 test failed!\n", ret);
308         else
309             printf( "HMAC-SHA256 test passed!\n");
310     #endif
311
312     #ifdef CYASSL_SHA384
313         if ( (ret = hmac_sha384_test()) != 0)
314             err_sys("HMAC-SHA384 test failed!\n", ret);
315         else
316             printf( "HMAC-SHA384 test passed!\n");
317     #endif
318
319     #ifdef CYASSL_SHA512
320         if ( (ret = hmac_sha512_test()) != 0)
321             err_sys("HMAC-SHA512 test failed!\n", ret);
322         else
323             printf( "HMAC-SHA512 test passed!\n");
324     #endif
325
326     #ifdef HAVE_BLAKE2
327         if ( (ret = hmac_blake2b_test()) != 0)
328             err_sys("HMAC-BLAKE2 test failed!\n", ret);
329         else
330             printf( "HMAC-BLAKE2 test passed!\n");
331     #endif
332
333     #ifdef HAVE_HKDF
334         if ( (ret = hkdf_test()) != 0)
335             err_sys("HMAC-KDF    test failed!\n", ret);
336         else
337             printf( "HMAC-KDF    test passed!\n");
338     #endif
339
340 #endif
341
342 #ifdef HAVE_AESGCM
343     if ( (ret = gmac_test()) != 0)
344         err_sys("GMAC     test passed!\n", ret);
345     else
346         printf( "GMAC     test passed!\n");
347 #endif
348
349 #ifndef NO_RC4
350     if ( (ret = arc4_test()) != 0)
351         err_sys("ARC4     test failed!\n", ret);
352     else
353         printf( "ARC4     test passed!\n");
354 #endif
355
356 #ifndef NO_HC128
357     if ( (ret = hc128_test()) != 0)
358         err_sys("HC-128   test failed!\n", ret);
359     else
360         printf( "HC-128   test passed!\n");
361 #endif
362
363 #ifndef NO_RABBIT
364     if ( (ret = rabbit_test()) != 0)
365         err_sys("Rabbit   test failed!\n", ret);
366     else
367         printf( "Rabbit   test passed!\n");
368 #endif
369
370 #ifndef NO_DES3
371     if ( (ret = des_test()) != 0)
372         err_sys("DES      test failed!\n", ret);
373     else
374         printf( "DES      test passed!\n");
375 #endif
376
377 #ifndef NO_DES3
378     if ( (ret = des3_test()) != 0)
379         err_sys("DES3     test failed!\n", ret);
380     else
381         printf( "DES3     test passed!\n");
382 #endif
383
384 #ifndef NO_AES
385     if ( (ret = aes_test()) != 0)
386         err_sys("AES      test failed!\n", ret);
387     else
388         printf( "AES      test passed!\n");
389
390 #ifdef HAVE_AESGCM
391     if ( (ret = aesgcm_test()) != 0)
392         err_sys("AES-GCM  test failed!\n", ret);
393     else
394         printf( "AES-GCM  test passed!\n");
395 #endif
396
397 #ifdef HAVE_AESCCM
398     if ( (ret = aesccm_test()) != 0)
399         err_sys("AES-CCM  test failed!\n", ret);
400     else
401         printf( "AES-CCM  test passed!\n");
402 #endif
403 #endif
404
405 #ifdef HAVE_CAMELLIA
406     if ( (ret = camellia_test()) != 0)
407         err_sys("CAMELLIA test failed!\n", ret);
408     else
409         printf( "CAMELLIA test passed!\n");
410 #endif
411
412     if ( (ret = random_test()) != 0)
413         err_sys("RANDOM   test failed!\n", ret);
414     else
415         printf( "RANDOM   test passed!\n");
416
417 #ifndef NO_RSA
418     if ( (ret = rsa_test()) != 0)
419         err_sys("RSA      test failed!\n", ret);
420     else
421         printf( "RSA      test passed!\n");
422 #endif
423
424 #ifndef NO_DH
425     if ( (ret = dh_test()) != 0)
426         err_sys("DH       test failed!\n", ret);
427     else
428         printf( "DH       test passed!\n");
429 #endif
430
431 #ifndef NO_DSA
432     if ( (ret = dsa_test()) != 0)
433         err_sys("DSA      test failed!\n", ret);
434     else
435         printf( "DSA      test passed!\n");
436 #endif
437
438 #ifndef NO_PWDBASED
439     if ( (ret = pwdbased_test()) != 0)
440         err_sys("PWDBASED test failed!\n", ret);
441     else
442         printf( "PWDBASED test passed!\n");
443 #endif
444
445 #ifdef OPENSSL_EXTRA
446     if ( (ret = openssl_test()) != 0)
447         err_sys("OPENSSL  test failed!\n", ret);
448     else
449         printf( "OPENSSL  test passed!\n");
450 #endif
451
452 #ifdef HAVE_ECC
453     if ( (ret = ecc_test()) != 0)
454         err_sys("ECC      test failed!\n", ret);
455     else
456         printf( "ECC      test passed!\n");
457     #ifdef HAVE_ECC_ENCRYPT
458         if ( (ret = ecc_encrypt_test()) != 0)
459             err_sys("ECC Enc  test failed!\n", ret);
460         else
461             printf( "ECC Enc  test passed!\n");
462     #endif
463 #endif
464
465 #ifdef HAVE_LIBZ
466     if ( (ret = compress_test()) != 0)
467         err_sys("COMPRESS test failed!\n", ret);
468     else
469         printf( "COMPRESS test passed!\n");
470 #endif
471
472 #ifdef HAVE_PKCS7
473     if ( (ret = pkcs7enveloped_test()) != 0)
474         err_sys("PKCS7enveloped test failed!\n", ret);
475     else
476         printf( "PKCS7enveloped test passed!\n");
477
478     if ( (ret = pkcs7signed_test()) != 0)
479         err_sys("PKCS7signed    test failed!\n", ret);
480     else
481         printf( "PKCS7signed    test passed!\n");
482 #endif
483
484     ((func_args*)args)->return_code = ret;
485 }
486
487
488 #ifndef NO_MAIN_DRIVER
489
490 #ifdef HAVE_CAVIUM
491
492 static int OpenNitroxDevice(int dma_mode,int dev_id)
493 {
494    Csp1CoreAssignment core_assign;
495    Uint32             device;
496
497    if (CspInitialize(CAVIUM_DIRECT,CAVIUM_DEV_ID))
498       return -1;
499    if (Csp1GetDevType(&device))
500       return -1;
501    if (device != NPX_DEVICE) {
502       if (ioctl(gpkpdev_hdlr[CAVIUM_DEV_ID], IOCTL_CSP1_GET_CORE_ASSIGNMENT,
503                 (Uint32 *)&core_assign)!= 0)
504          return -1;
505    }
506    CspShutdown(CAVIUM_DEV_ID);
507
508    return CspInitialize(dma_mode, dev_id);
509 }
510
511 #endif /* HAVE_CAVIUM */
512
513     /* so overall tests can pull in test function */
514
515     int main(int argc, char** argv)
516     {
517
518         func_args args;
519
520
521 #ifdef HAVE_CAVIUM
522         int ret = OpenNitroxDevice(CAVIUM_DIRECT, CAVIUM_DEV_ID);
523         if (ret != 0)
524             err_sys("Cavium OpenNitroxDevice failed", -1236);
525 #endif /* HAVE_CAVIUM */
526
527         args.argc = argc;
528         args.argv = argv;
529
530         ctaocrypt_test(&args);
531
532 #ifdef HAVE_CAVIUM
533         CspShutdown(CAVIUM_DEV_ID);
534 #endif
535
536         return args.return_code;
537     }
538
539 #endif /* NO_MAIN_DRIVER */
540
541
542 #ifdef CYASSL_MD2
543 int md2_test()
544 {
545     Md2  md2;
546     byte hash[MD2_DIGEST_SIZE];
547
548     testVector a, b, c, d, e, f, g;
549     testVector test_md2[7];
550     int times = sizeof(test_md2) / sizeof(testVector), i;
551
552     a.input  = "";
553     a.output = "\x83\x50\xe5\xa3\xe2\x4c\x15\x3d\xf2\x27\x5c\x9f\x80\x69"
554                "\x27\x73";
555     a.inLen  = strlen(a.input);
556     a.outLen = MD2_DIGEST_SIZE;
557
558     b.input  = "a";
559     b.output = "\x32\xec\x01\xec\x4a\x6d\xac\x72\xc0\xab\x96\xfb\x34\xc0"
560                "\xb5\xd1";
561     b.inLen  = strlen(b.input);
562     b.outLen = MD2_DIGEST_SIZE;
563
564     c.input  = "abc";
565     c.output = "\xda\x85\x3b\x0d\x3f\x88\xd9\x9b\x30\x28\x3a\x69\xe6\xde"
566                "\xd6\xbb";
567     c.inLen  = strlen(c.input);
568     c.outLen = MD2_DIGEST_SIZE;
569
570     d.input  = "message digest";
571     d.output = "\xab\x4f\x49\x6b\xfb\x2a\x53\x0b\x21\x9f\xf3\x30\x31\xfe"
572                "\x06\xb0";
573     d.inLen  = strlen(d.input);
574     d.outLen = MD2_DIGEST_SIZE;
575
576     e.input  = "abcdefghijklmnopqrstuvwxyz";
577     e.output = "\x4e\x8d\xdf\xf3\x65\x02\x92\xab\x5a\x41\x08\xc3\xaa\x47"
578                "\x94\x0b";
579     e.inLen  = strlen(e.input);
580     e.outLen = MD2_DIGEST_SIZE;
581
582     f.input  = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
583                "6789";
584     f.output = "\xda\x33\xde\xf2\xa4\x2d\xf1\x39\x75\x35\x28\x46\xc3\x03"
585                "\x38\xcd";
586     f.inLen  = strlen(f.input);
587     f.outLen = MD2_DIGEST_SIZE;
588
589     g.input  = "1234567890123456789012345678901234567890123456789012345678"
590                "9012345678901234567890";
591     g.output = "\xd5\x97\x6f\x79\xd8\x3d\x3a\x0d\xc9\x80\x6c\x3c\x66\xf3"
592                "\xef\xd8";
593     g.inLen  = strlen(g.input);
594     g.outLen = MD2_DIGEST_SIZE;
595
596     test_md2[0] = a;
597     test_md2[1] = b;
598     test_md2[2] = c;
599     test_md2[3] = d;
600     test_md2[4] = e;
601     test_md2[5] = f;
602     test_md2[6] = g;
603
604     InitMd2(&md2);
605
606     for (i = 0; i < times; ++i) {
607         Md2Update(&md2, (byte*)test_md2[i].input, (word32)test_md2[i].inLen);
608         Md2Final(&md2, hash);
609
610         if (memcmp(hash, test_md2[i].output, MD2_DIGEST_SIZE) != 0)
611             return -155 - i;
612     }
613
614     return 0;
615 }
616 #endif
617
618 #ifndef NO_MD5
619 int md5_test(void)
620 {
621     Md5  md5;
622     byte hash[MD5_DIGEST_SIZE];
623
624     testVector a, b, c, d, e;
625     testVector test_md5[5];
626     int times = sizeof(test_md5) / sizeof(testVector), i;
627
628     a.input  = "abc";
629     a.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
630                "\x72";
631     a.inLen  = strlen(a.input);
632     a.outLen = MD5_DIGEST_SIZE;
633
634     b.input  = "message digest";
635     b.output = "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61"
636                "\xd0";
637     b.inLen  = strlen(b.input);
638     b.outLen = MD5_DIGEST_SIZE;
639
640     c.input  = "abcdefghijklmnopqrstuvwxyz";
641     c.output = "\xc3\xfc\xd3\xd7\x61\x92\xe4\x00\x7d\xfb\x49\x6c\xca\x67\xe1"
642                "\x3b";
643     c.inLen  = strlen(c.input);
644     c.outLen = MD5_DIGEST_SIZE;
645
646     d.input  = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
647                "6789";
648     d.output = "\xd1\x74\xab\x98\xd2\x77\xd9\xf5\xa5\x61\x1c\x2c\x9f\x41\x9d"
649                "\x9f";
650     d.inLen  = strlen(d.input);
651     d.outLen = MD5_DIGEST_SIZE;
652
653     e.input  = "1234567890123456789012345678901234567890123456789012345678"
654                "9012345678901234567890";
655     e.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
656                "\x7a";
657     e.inLen  = strlen(e.input);
658     e.outLen = MD5_DIGEST_SIZE;
659
660     test_md5[0] = a;
661     test_md5[1] = b;
662     test_md5[2] = c;
663     test_md5[3] = d;
664     test_md5[4] = e;
665
666     InitMd5(&md5);
667
668     for (i = 0; i < times; ++i) {
669         Md5Update(&md5, (byte*)test_md5[i].input, (word32)test_md5[i].inLen);
670         Md5Final(&md5, hash);
671
672         if (memcmp(hash, test_md5[i].output, MD5_DIGEST_SIZE) != 0)
673             return -5 - i;
674     }
675
676     return 0;
677 }
678 #endif /* NO_MD5 */
679
680
681 #ifndef NO_MD4
682
683 int md4_test(void)
684 {
685     Md4  md4;
686     byte hash[MD4_DIGEST_SIZE];
687
688     testVector a, b, c, d, e, f, g;
689     testVector test_md4[7];
690     int times = sizeof(test_md4) / sizeof(testVector), i;
691
692     a.input  = "";
693     a.output = "\x31\xd6\xcf\xe0\xd1\x6a\xe9\x31\xb7\x3c\x59\xd7\xe0\xc0\x89"
694                "\xc0";
695     a.inLen  = strlen(a.input);
696     a.outLen = MD4_DIGEST_SIZE;
697
698     b.input  = "a";
699     b.output = "\xbd\xe5\x2c\xb3\x1d\xe3\x3e\x46\x24\x5e\x05\xfb\xdb\xd6\xfb"
700                "\x24";
701     b.inLen  = strlen(b.input);
702     b.outLen = MD4_DIGEST_SIZE;
703
704     c.input  = "abc";
705     c.output = "\xa4\x48\x01\x7a\xaf\x21\xd8\x52\x5f\xc1\x0a\xe8\x7a\xa6\x72"
706                "\x9d";
707     c.inLen  = strlen(c.input);
708     c.outLen = MD4_DIGEST_SIZE;
709
710     d.input  = "message digest";
711     d.output = "\xd9\x13\x0a\x81\x64\x54\x9f\xe8\x18\x87\x48\x06\xe1\xc7\x01"
712                "\x4b";
713     d.inLen  = strlen(d.input);
714     d.outLen = MD4_DIGEST_SIZE;
715
716     e.input  = "abcdefghijklmnopqrstuvwxyz";
717     e.output = "\xd7\x9e\x1c\x30\x8a\xa5\xbb\xcd\xee\xa8\xed\x63\xdf\x41\x2d"
718                "\xa9";
719     e.inLen  = strlen(e.input);
720     e.outLen = MD4_DIGEST_SIZE;
721
722     f.input  = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
723                "6789";
724     f.output = "\x04\x3f\x85\x82\xf2\x41\xdb\x35\x1c\xe6\x27\xe1\x53\xe7\xf0"
725                "\xe4";
726     f.inLen  = strlen(f.input);
727     f.outLen = MD4_DIGEST_SIZE;
728
729     g.input  = "1234567890123456789012345678901234567890123456789012345678"
730                "9012345678901234567890";
731     g.output = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f\xcc\x05"
732                "\x36";
733     g.inLen  = strlen(g.input);
734     g.outLen = MD4_DIGEST_SIZE;
735
736     test_md4[0] = a;
737     test_md4[1] = b;
738     test_md4[2] = c;
739     test_md4[3] = d;
740     test_md4[4] = e;
741     test_md4[5] = f;
742     test_md4[6] = g;
743
744     InitMd4(&md4);
745
746     for (i = 0; i < times; ++i) {
747         Md4Update(&md4, (byte*)test_md4[i].input, (word32)test_md4[i].inLen);
748         Md4Final(&md4, hash);
749
750         if (memcmp(hash, test_md4[i].output, MD4_DIGEST_SIZE) != 0)
751             return -205 - i;
752     }
753
754     return 0;
755 }
756
757 #endif /* NO_MD4 */
758
759 #ifndef NO_SHA
760
761 int sha_test(void)
762 {
763     Sha  sha;
764     byte hash[SHA_DIGEST_SIZE];
765
766     testVector a, b, c, d;
767     testVector test_sha[4];
768     int ret;
769     int times = sizeof(test_sha) / sizeof(struct testVector), i;
770
771     a.input  = "abc";
772     a.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
773                "\x6C\x9C\xD0\xD8\x9D";
774     a.inLen  = strlen(a.input);
775     a.outLen = SHA_DIGEST_SIZE;
776
777     b.input  = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
778     b.output = "\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE\x4A\xA1\xF9\x51\x29"
779                "\xE5\xE5\x46\x70\xF1";
780     b.inLen  = strlen(b.input);
781     b.outLen = SHA_DIGEST_SIZE;
782
783     c.input  = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
784                "aaaaaa";
785     c.output = "\x00\x98\xBA\x82\x4B\x5C\x16\x42\x7B\xD7\xA1\x12\x2A\x5A\x44"
786                "\x2A\x25\xEC\x64\x4D";
787     c.inLen  = strlen(c.input);
788     c.outLen = SHA_DIGEST_SIZE;
789
790     d.input  = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
791                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
792                "aaaaaaaaaa";
793     d.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
794                "\x53\x99\x5E\x26\xA0";
795     d.inLen  = strlen(d.input);
796     d.outLen = SHA_DIGEST_SIZE;
797
798     test_sha[0] = a;
799     test_sha[1] = b;
800     test_sha[2] = c;
801     test_sha[3] = d;
802
803     ret = InitSha(&sha);
804     if (ret != 0)
805         return -4001;
806
807     for (i = 0; i < times; ++i) {
808         ShaUpdate(&sha, (byte*)test_sha[i].input, (word32)test_sha[i].inLen);
809         ShaFinal(&sha, hash);
810
811         if (memcmp(hash, test_sha[i].output, SHA_DIGEST_SIZE) != 0)
812             return -10 - i;
813     }
814
815     return 0;
816 }
817
818 #endif /* NO_SHA */
819
820 #ifdef CYASSL_RIPEMD
821 int ripemd_test(void)
822 {
823     RipeMd  ripemd;
824     byte hash[RIPEMD_DIGEST_SIZE];
825
826     testVector a, b, c, d;
827     testVector test_ripemd[4];
828     int times = sizeof(test_ripemd) / sizeof(struct testVector), i;
829
830     a.input  = "abc";
831     a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
832                "\xb0\x87\xf1\x5a\x0b\xfc";
833     a.inLen  = strlen(a.input);
834     a.outLen = RIPEMD_DIGEST_SIZE;
835
836     b.input  = "message digest";
837     b.output = "\x5d\x06\x89\xef\x49\xd2\xfa\xe5\x72\xb8\x81\xb1\x23\xa8"
838                "\x5f\xfa\x21\x59\x5f\x36";
839     b.inLen  = strlen(b.input);
840     b.outLen = RIPEMD_DIGEST_SIZE;
841
842     c.input  = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
843     c.output = "\x12\xa0\x53\x38\x4a\x9c\x0c\x88\xe4\x05\xa0\x6c\x27\xdc"
844                "\xf4\x9a\xda\x62\xeb\x2b";
845     c.inLen  = strlen(c.input);
846     c.outLen = RIPEMD_DIGEST_SIZE;
847
848     d.input  = "12345678901234567890123456789012345678901234567890123456"
849                "789012345678901234567890";
850     d.output = "\x9b\x75\x2e\x45\x57\x3d\x4b\x39\xf4\xdb\xd3\x32\x3c\xab"
851                "\x82\xbf\x63\x32\x6b\xfb";
852     d.inLen  = strlen(d.input);
853     d.outLen = RIPEMD_DIGEST_SIZE;
854
855     test_ripemd[0] = a;
856     test_ripemd[1] = b;
857     test_ripemd[2] = c;
858     test_ripemd[3] = d;
859
860     InitRipeMd(&ripemd);
861
862     for (i = 0; i < times; ++i) {
863         RipeMdUpdate(&ripemd, (byte*)test_ripemd[i].input,
864                      (word32)test_ripemd[i].inLen);
865         RipeMdFinal(&ripemd, hash);
866
867         if (memcmp(hash, test_ripemd[i].output, RIPEMD_DIGEST_SIZE) != 0)
868             return -10 - i;
869     }
870
871     return 0;
872 }
873 #endif /* CYASSL_RIPEMD */
874
875
876 #ifdef HAVE_BLAKE2
877
878
879 #define BLAKE2_TESTS 3
880
881 static const byte blake2b_vec[BLAKE2_TESTS][BLAKE2B_OUTBYTES] =
882 {
883   {
884     0x78, 0x6A, 0x02, 0xF7, 0x42, 0x01, 0x59, 0x03,
885     0xC6, 0xC6, 0xFD, 0x85, 0x25, 0x52, 0xD2, 0x72,
886     0x91, 0x2F, 0x47, 0x40, 0xE1, 0x58, 0x47, 0x61,
887     0x8A, 0x86, 0xE2, 0x17, 0xF7, 0x1F, 0x54, 0x19,
888     0xD2, 0x5E, 0x10, 0x31, 0xAF, 0xEE, 0x58, 0x53,
889     0x13, 0x89, 0x64, 0x44, 0x93, 0x4E, 0xB0, 0x4B,
890     0x90, 0x3A, 0x68, 0x5B, 0x14, 0x48, 0xB7, 0x55,
891     0xD5, 0x6F, 0x70, 0x1A, 0xFE, 0x9B, 0xE2, 0xCE
892   },
893   {
894     0x2F, 0xA3, 0xF6, 0x86, 0xDF, 0x87, 0x69, 0x95,
895     0x16, 0x7E, 0x7C, 0x2E, 0x5D, 0x74, 0xC4, 0xC7,
896     0xB6, 0xE4, 0x8F, 0x80, 0x68, 0xFE, 0x0E, 0x44,
897     0x20, 0x83, 0x44, 0xD4, 0x80, 0xF7, 0x90, 0x4C,
898     0x36, 0x96, 0x3E, 0x44, 0x11, 0x5F, 0xE3, 0xEB,
899     0x2A, 0x3A, 0xC8, 0x69, 0x4C, 0x28, 0xBC, 0xB4,
900     0xF5, 0xA0, 0xF3, 0x27, 0x6F, 0x2E, 0x79, 0x48,
901     0x7D, 0x82, 0x19, 0x05, 0x7A, 0x50, 0x6E, 0x4B
902   },
903   {
904     0x1C, 0x08, 0x79, 0x8D, 0xC6, 0x41, 0xAB, 0xA9,
905     0xDE, 0xE4, 0x35, 0xE2, 0x25, 0x19, 0xA4, 0x72,
906     0x9A, 0x09, 0xB2, 0xBF, 0xE0, 0xFF, 0x00, 0xEF,
907     0x2D, 0xCD, 0x8E, 0xD6, 0xF8, 0xA0, 0x7D, 0x15,
908     0xEA, 0xF4, 0xAE, 0xE5, 0x2B, 0xBF, 0x18, 0xAB,
909     0x56, 0x08, 0xA6, 0x19, 0x0F, 0x70, 0xB9, 0x04,
910     0x86, 0xC8, 0xA7, 0xD4, 0x87, 0x37, 0x10, 0xB1,
911     0x11, 0x5D, 0x3D, 0xEB, 0xBB, 0x43, 0x27, 0xB5
912   }
913 };
914
915
916
917 int blake2b_test(void)
918 {
919     Blake2b b2b;
920     byte    digest[64];
921     byte    input[64];
922     int     i, ret;
923
924     for (i = 0; i < (int)sizeof(input); i++)
925         input[i] = (byte)i;
926
927     for (i = 0; i < BLAKE2_TESTS; i++) {
928         ret = InitBlake2b(&b2b, 64);
929         if (ret != 0)
930             return -4002;
931
932         ret = Blake2bUpdate(&b2b, input, i);
933         if (ret != 0)
934             return -4003;
935
936         ret = Blake2bFinal(&b2b, digest, 64);
937         if (ret != 0)
938             return -4004;
939
940         if (memcmp(digest, blake2b_vec[i], 64) != 0) {
941             return -300 - i;
942         }
943     }
944
945     return 0;
946 }
947 #endif /* HAVE_BLAKE2 */
948
949
950 #ifndef NO_SHA256
951 int sha256_test(void)
952 {
953     Sha256 sha;
954     byte   hash[SHA256_DIGEST_SIZE];
955
956     testVector a, b;
957     testVector test_sha[2];
958     int ret;
959     int times = sizeof(test_sha) / sizeof(struct testVector), i;
960
961     a.input  = "abc";
962     a.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
963                "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
964                "\x15\xAD";
965     a.inLen  = strlen(a.input);
966     a.outLen = SHA256_DIGEST_SIZE;
967
968     b.input  = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
969     b.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
970                "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
971                "\x06\xC1";
972     b.inLen  = strlen(b.input);
973     b.outLen = SHA256_DIGEST_SIZE;
974
975     test_sha[0] = a;
976     test_sha[1] = b;
977
978     ret = InitSha256(&sha);
979     if (ret != 0)
980         return -4005;
981
982     for (i = 0; i < times; ++i) {
983         ret = Sha256Update(&sha, (byte*)test_sha[i].input,(word32)test_sha[i].inLen);
984         if (ret != 0)
985             return -4006;
986         ret = Sha256Final(&sha, hash);
987         if (ret != 0)
988             return -4007;
989
990         if (memcmp(hash, test_sha[i].output, SHA256_DIGEST_SIZE) != 0)
991             return -10 - i;
992     }
993
994     return 0;
995 }
996 #endif
997
998
999 #ifdef CYASSL_SHA512
1000 int sha512_test(void)
1001 {
1002     Sha512 sha;
1003     byte   hash[SHA512_DIGEST_SIZE];
1004     int    ret;
1005
1006     testVector a, b;
1007     testVector test_sha[2];
1008     int times = sizeof(test_sha) / sizeof(struct testVector), i;
1009
1010     a.input  = "abc";
1011     a.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
1012                "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
1013                "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
1014                "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
1015                "\xa5\x4c\xa4\x9f";
1016     a.inLen  = strlen(a.input);
1017     a.outLen = SHA512_DIGEST_SIZE;
1018
1019     b.input  = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
1020                "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
1021     b.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
1022                "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
1023                "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
1024                "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
1025                "\x87\x4b\xe9\x09";
1026     b.inLen  = strlen(b.input);
1027     b.outLen = SHA512_DIGEST_SIZE;
1028
1029     test_sha[0] = a;
1030     test_sha[1] = b;
1031
1032     ret = InitSha512(&sha);
1033     if (ret != 0)
1034         return -4009;
1035
1036     for (i = 0; i < times; ++i) {
1037         ret = Sha512Update(&sha, (byte*)test_sha[i].input,(word32)test_sha[i].inLen);
1038         if (ret != 0)
1039             return -4010;
1040
1041         ret = Sha512Final(&sha, hash);
1042         if (ret != 0)
1043             return -4011;
1044
1045         if (memcmp(hash, test_sha[i].output, SHA512_DIGEST_SIZE) != 0)
1046             return -10 - i;
1047     }
1048
1049     return 0;
1050 }
1051 #endif
1052
1053
1054 #ifdef CYASSL_SHA384
1055 int sha384_test(void)
1056 {
1057     Sha384 sha;
1058     byte   hash[SHA384_DIGEST_SIZE];
1059     int    ret;
1060
1061     testVector a, b;
1062     testVector test_sha[2];
1063     int times = sizeof(test_sha) / sizeof(struct testVector), i;
1064
1065     a.input  = "abc";
1066     a.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
1067                "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
1068                "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
1069                "\xc8\x25\xa7";
1070     a.inLen  = strlen(a.input);
1071     a.outLen = SHA384_DIGEST_SIZE;
1072
1073     b.input  = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
1074                "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
1075     b.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
1076                "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
1077                "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
1078                "\x74\x60\x39";
1079     b.inLen  = strlen(b.input);
1080     b.outLen = SHA384_DIGEST_SIZE;
1081
1082     test_sha[0] = a;
1083     test_sha[1] = b;
1084
1085     ret = InitSha384(&sha);
1086     if (ret != 0)
1087         return -4012;
1088
1089     for (i = 0; i < times; ++i) {
1090         ret = Sha384Update(&sha, (byte*)test_sha[i].input,(word32)test_sha[i].inLen);
1091         if (ret != 0)
1092             return -4013;
1093
1094         ret = Sha384Final(&sha, hash);
1095         if (ret != 0)
1096             return -4014;
1097
1098         if (memcmp(hash, test_sha[i].output, SHA384_DIGEST_SIZE) != 0)
1099             return -10 - i;
1100     }
1101
1102     return 0;
1103 }
1104 #endif /* CYASSL_SHA384 */
1105
1106
1107 #if !defined(NO_HMAC) && !defined(NO_MD5)
1108 int hmac_md5_test(void)
1109 {
1110     Hmac hmac;
1111     byte hash[MD5_DIGEST_SIZE];
1112
1113     const char* keys[]=
1114     {
1115         "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
1116         "Jefe",
1117         "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
1118     };
1119
1120     testVector a, b, c;
1121     testVector test_hmac[3];
1122
1123     int ret;
1124     int times = sizeof(test_hmac) / sizeof(testVector), i;
1125
1126     a.input  = "Hi There";
1127     a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
1128                "\x9d";
1129     a.inLen  = strlen(a.input);
1130     a.outLen = MD5_DIGEST_SIZE;
1131
1132     b.input  = "what do ya want for nothing?";
1133     b.output = "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7"
1134                "\x38";
1135     b.inLen  = strlen(b.input);
1136     b.outLen = MD5_DIGEST_SIZE;
1137
1138     c.input  = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
1139                "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
1140                "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
1141                "\xDD\xDD\xDD\xDD\xDD\xDD";
1142     c.output = "\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3"
1143                "\xf6";
1144     c.inLen  = strlen(c.input);
1145     c.outLen = MD5_DIGEST_SIZE;
1146
1147     test_hmac[0] = a;
1148     test_hmac[1] = b;
1149     test_hmac[2] = c;
1150
1151     for (i = 0; i < times; ++i) {
1152 #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
1153         if (i == 1)
1154             continue; /* cavium can't handle short keys, fips not allowed */
1155 #endif
1156 #ifdef HAVE_CAVIUM
1157         if (HmacInitCavium(&hmac, CAVIUM_DEV_ID) != 0)
1158             return -20009;
1159 #endif
1160         ret = HmacSetKey(&hmac, MD5, (byte*)keys[i], (word32)strlen(keys[i]));
1161         if (ret != 0)
1162             return -4015;
1163         ret = HmacUpdate(&hmac, (byte*)test_hmac[i].input,
1164                    (word32)test_hmac[i].inLen);
1165         if (ret != 0)
1166             return -4016;
1167         ret = HmacFinal(&hmac, hash);
1168         if (ret != 0)
1169             return -4017;
1170
1171         if (memcmp(hash, test_hmac[i].output, MD5_DIGEST_SIZE) != 0)
1172             return -20 - i;
1173 #ifdef HAVE_CAVIUM
1174         HmacFreeCavium(&hmac);
1175 #endif
1176     }
1177
1178     return 0;
1179 }
1180 #endif /* NO_HMAC && NO_MD5 */
1181
1182 #if !defined(NO_HMAC) && !defined(NO_SHA)
1183 int hmac_sha_test(void)
1184 {
1185     Hmac hmac;
1186     byte hash[SHA_DIGEST_SIZE];
1187
1188     const char* keys[]=
1189     {
1190         "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
1191                                                                 "\x0b\x0b\x0b",
1192         "Jefe",
1193         "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
1194                                                                 "\xAA\xAA\xAA"
1195     };
1196
1197     testVector a, b, c;
1198     testVector test_hmac[3];
1199
1200     int ret;
1201     int times = sizeof(test_hmac) / sizeof(testVector), i;
1202
1203     a.input  = "Hi There";
1204     a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
1205                "\x8e\xf1\x46\xbe\x00";
1206     a.inLen  = strlen(a.input);
1207     a.outLen = SHA_DIGEST_SIZE;
1208
1209     b.input  = "what do ya want for nothing?";
1210     b.output = "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf"
1211                "\x9c\x25\x9a\x7c\x79";
1212     b.inLen  = strlen(b.input);
1213     b.outLen = SHA_DIGEST_SIZE;
1214
1215     c.input  = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
1216                "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
1217                "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
1218                "\xDD\xDD\xDD\xDD\xDD\xDD";
1219     c.output = "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b"
1220                "\x4f\x63\xf1\x75\xd3";
1221     c.inLen  = strlen(c.input);
1222     c.outLen = SHA_DIGEST_SIZE;
1223
1224     test_hmac[0] = a;
1225     test_hmac[1] = b;
1226     test_hmac[2] = c;
1227
1228     for (i = 0; i < times; ++i) {
1229 #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
1230         if (i == 1)
1231             continue; /* cavium can't handle short keys, fips not allowed */
1232 #endif
1233 #ifdef HAVE_CAVIUM
1234         if (HmacInitCavium(&hmac, CAVIUM_DEV_ID) != 0)
1235             return -20010;
1236 #endif
1237         ret = HmacSetKey(&hmac, SHA, (byte*)keys[i], (word32)strlen(keys[i]));
1238         if (ret != 0)
1239             return -4018;
1240         ret = HmacUpdate(&hmac, (byte*)test_hmac[i].input,
1241                    (word32)test_hmac[i].inLen);
1242         if (ret != 0)
1243             return -4019;
1244         ret = HmacFinal(&hmac, hash);
1245         if (ret != 0)
1246             return -4020;
1247
1248         if (memcmp(hash, test_hmac[i].output, SHA_DIGEST_SIZE) != 0)
1249             return -20 - i;
1250 #ifdef HAVE_CAVIUM
1251         HmacFreeCavium(&hmac);
1252 #endif
1253     }
1254
1255     return 0;
1256 }
1257 #endif
1258
1259
1260 #if !defined(NO_HMAC) && !defined(NO_SHA256)
1261 int hmac_sha256_test(void)
1262 {
1263     Hmac hmac;
1264     byte hash[SHA256_DIGEST_SIZE];
1265
1266     const char* keys[]=
1267     {
1268         "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
1269                                                                 "\x0b\x0b\x0b",
1270         "Jefe",
1271         "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
1272                                                                 "\xAA\xAA\xAA"
1273     };
1274
1275     testVector a, b, c;
1276     testVector test_hmac[3];
1277
1278     int ret;
1279     int times = sizeof(test_hmac) / sizeof(testVector), i;
1280
1281     a.input  = "Hi There";
1282     a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
1283                "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
1284                "\xcf\xf7";
1285     a.inLen  = strlen(a.input);
1286     a.outLen = SHA256_DIGEST_SIZE;
1287
1288     b.input  = "what do ya want for nothing?";
1289     b.output = "\x5b\xdc\xc1\x46\xbf\x60\x75\x4e\x6a\x04\x24\x26\x08\x95\x75"
1290                "\xc7\x5a\x00\x3f\x08\x9d\x27\x39\x83\x9d\xec\x58\xb9\x64\xec"
1291                "\x38\x43";
1292     b.inLen  = strlen(b.input);
1293     b.outLen = SHA256_DIGEST_SIZE;
1294
1295     c.input  = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
1296                "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
1297                "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
1298                "\xDD\xDD\xDD\xDD\xDD\xDD";
1299     c.output = "\x77\x3e\xa9\x1e\x36\x80\x0e\x46\x85\x4d\xb8\xeb\xd0\x91\x81"
1300                "\xa7\x29\x59\x09\x8b\x3e\xf8\xc1\x22\xd9\x63\x55\x14\xce\xd5"
1301                "\x65\xfe";
1302     c.inLen  = strlen(c.input);
1303     c.outLen = SHA256_DIGEST_SIZE;
1304
1305     test_hmac[0] = a;
1306     test_hmac[1] = b;
1307     test_hmac[2] = c;
1308
1309     for (i = 0; i < times; ++i) {
1310 #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
1311         if (i == 1)
1312             continue; /* cavium can't handle short keys, fips not allowed */
1313 #endif
1314 #ifdef HAVE_CAVIUM
1315         if (HmacInitCavium(&hmac, CAVIUM_DEV_ID) != 0)
1316             return -20011;
1317 #endif
1318         ret = HmacSetKey(&hmac, SHA256, (byte*)keys[i],(word32)strlen(keys[i]));
1319         if (ret != 0)
1320             return -4021;
1321         ret = HmacUpdate(&hmac, (byte*)test_hmac[i].input,
1322                    (word32)test_hmac[i].inLen);
1323         if (ret != 0)
1324             return -4022;
1325         ret = HmacFinal(&hmac, hash);
1326         if (ret != 0)
1327             return -4023;
1328
1329         if (memcmp(hash, test_hmac[i].output, SHA256_DIGEST_SIZE) != 0)
1330             return -20 - i;
1331 #ifdef HAVE_CAVIUM
1332         HmacFreeCavium(&hmac);
1333 #endif
1334     }
1335
1336     return 0;
1337 }
1338 #endif
1339
1340
1341 #if !defined(NO_HMAC) && defined(HAVE_BLAKE2)
1342 int hmac_blake2b_test(void)
1343 {
1344     Hmac hmac;
1345     byte hash[BLAKE2B_256];
1346
1347     const char* keys[]=
1348     {
1349         "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
1350                                                                 "\x0b\x0b\x0b",
1351         "Jefe",
1352         "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
1353                                                                 "\xAA\xAA\xAA"
1354     };
1355
1356     testVector a, b, c;
1357     testVector test_hmac[3];
1358
1359     int ret;
1360     int times = sizeof(test_hmac) / sizeof(testVector), i;
1361
1362     a.input  = "Hi There";
1363     a.output = "\x72\x93\x0d\xdd\xf5\xf7\xe1\x78\x38\x07\x44\x18\x0b\x3f\x51"
1364                "\x37\x25\xb5\x82\xc2\x08\x83\x2f\x1c\x99\xfd\x03\xa0\x16\x75"
1365                "\xac\xfd";
1366     a.inLen  = strlen(a.input);
1367     a.outLen = BLAKE2B_256;
1368
1369     b.input  = "what do ya want for nothing?";
1370     b.output = "\x3d\x20\x50\x71\x05\xc0\x8c\x0c\x38\x44\x1e\xf7\xf9\xd1\x67"
1371                "\x21\xff\x64\xf5\x94\x00\xcf\xf9\x75\x41\xda\x88\x61\x9d\x7c"
1372                "\xda\x2b";
1373     b.inLen  = strlen(b.input);
1374     b.outLen = BLAKE2B_256;
1375
1376     c.input  = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
1377                "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
1378                "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
1379                "\xDD\xDD\xDD\xDD\xDD\xDD";
1380     c.output = "\xda\xfe\x2a\x24\xfc\xe7\xea\x36\x34\xbe\x41\x92\xc7\x11\xa7"
1381                "\x00\xae\x53\x9c\x11\x9c\x80\x74\x55\x22\x25\x4a\xb9\x55\xd3"
1382                "\x0f\x87";
1383     c.inLen  = strlen(c.input);
1384     c.outLen = BLAKE2B_256;
1385
1386     test_hmac[0] = a;
1387     test_hmac[1] = b;
1388     test_hmac[2] = c;
1389
1390     for (i = 0; i < times; ++i) {
1391 #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
1392         if (i == 1)
1393             continue; /* cavium can't handle short keys, fips not allowed */
1394 #endif
1395 #ifdef HAVE_CAVIUM
1396         if (HmacInitCavium(&hmac, CAVIUM_DEV_ID) != 0)
1397             return -20011;
1398 #endif
1399         ret = HmacSetKey(&hmac, BLAKE2B_ID, (byte*)keys[i],
1400                          (word32)strlen(keys[i]));
1401         if (ret != 0)
1402             return -4024;
1403         ret = HmacUpdate(&hmac, (byte*)test_hmac[i].input,
1404                    (word32)test_hmac[i].inLen);
1405         if (ret != 0)
1406             return -4025;
1407         ret = HmacFinal(&hmac, hash);
1408         if (ret != 0)
1409             return -4026;
1410
1411         if (memcmp(hash, test_hmac[i].output, BLAKE2B_256) != 0)
1412             return -20 - i;
1413 #ifdef HAVE_CAVIUM
1414         HmacFreeCavium(&hmac);
1415 #endif
1416     }
1417
1418     return 0;
1419 }
1420 #endif
1421
1422
1423 #if !defined(NO_HMAC) && defined(CYASSL_SHA384)
1424 int hmac_sha384_test(void)
1425 {
1426     Hmac hmac;
1427     byte hash[SHA384_DIGEST_SIZE];
1428
1429     const char* keys[]=
1430     {
1431         "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
1432                                                                 "\x0b\x0b\x0b",
1433         "Jefe",
1434         "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
1435                                                                 "\xAA\xAA\xAA"
1436     };
1437
1438     testVector a, b, c;
1439     testVector test_hmac[3];
1440
1441     int ret;
1442     int times = sizeof(test_hmac) / sizeof(testVector), i;
1443
1444     a.input  = "Hi There";
1445     a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
1446                "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
1447                "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
1448                "\xfa\x9c\xb6";
1449     a.inLen  = strlen(a.input);
1450     a.outLen = SHA384_DIGEST_SIZE;
1451
1452     b.input  = "what do ya want for nothing?";
1453     b.output = "\xaf\x45\xd2\xe3\x76\x48\x40\x31\x61\x7f\x78\xd2\xb5\x8a\x6b"
1454                "\x1b\x9c\x7e\xf4\x64\xf5\xa0\x1b\x47\xe4\x2e\xc3\x73\x63\x22"
1455                "\x44\x5e\x8e\x22\x40\xca\x5e\x69\xe2\xc7\x8b\x32\x39\xec\xfa"
1456                "\xb2\x16\x49";
1457     b.inLen  = strlen(b.input);
1458     b.outLen = SHA384_DIGEST_SIZE;
1459
1460     c.input  = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
1461                "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
1462                "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
1463                "\xDD\xDD\xDD\xDD\xDD\xDD";
1464     c.output = "\x88\x06\x26\x08\xd3\xe6\xad\x8a\x0a\xa2\xac\xe0\x14\xc8\xa8"
1465                "\x6f\x0a\xa6\x35\xd9\x47\xac\x9f\xeb\xe8\x3e\xf4\xe5\x59\x66"
1466                "\x14\x4b\x2a\x5a\xb3\x9d\xc1\x38\x14\xb9\x4e\x3a\xb6\xe1\x01"
1467                "\xa3\x4f\x27";
1468     c.inLen  = strlen(c.input);
1469     c.outLen = SHA384_DIGEST_SIZE;
1470
1471     test_hmac[0] = a;
1472     test_hmac[1] = b;
1473     test_hmac[2] = c;
1474
1475     for (i = 0; i < times; ++i) {
1476 #if defined(HAVE_FIPS)
1477         if (i == 1)
1478             continue; /* fips not allowed */
1479 #endif
1480         ret = HmacSetKey(&hmac, SHA384, (byte*)keys[i],(word32)strlen(keys[i]));
1481         if (ret != 0)
1482             return -4027;
1483         ret = HmacUpdate(&hmac, (byte*)test_hmac[i].input,
1484                    (word32)test_hmac[i].inLen);
1485         if (ret != 0)
1486             return -4028;
1487         ret = HmacFinal(&hmac, hash);
1488         if (ret != 0)
1489             return -4029;
1490
1491         if (memcmp(hash, test_hmac[i].output, SHA384_DIGEST_SIZE) != 0)
1492             return -20 - i;
1493     }
1494
1495     return 0;
1496 }
1497 #endif
1498
1499
1500 #if !defined(NO_HMAC) && defined(CYASSL_SHA512)
1501 int hmac_sha512_test(void)
1502 {
1503     Hmac hmac;
1504     byte hash[SHA512_DIGEST_SIZE];
1505
1506     const char* keys[]=
1507     {
1508         "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
1509                                                                 "\x0b\x0b\x0b",
1510         "Jefe",
1511         "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
1512                                                                 "\xAA\xAA\xAA"
1513     };
1514
1515     testVector a, b, c;
1516     testVector test_hmac[3];
1517
1518     int ret;
1519     int times = sizeof(test_hmac) / sizeof(testVector), i;
1520
1521     a.input  = "Hi There";
1522     a.output = "\x87\xaa\x7c\xde\xa5\xef\x61\x9d\x4f\xf0\xb4\x24\x1a\x1d\x6c"
1523                "\xb0\x23\x79\xf4\xe2\xce\x4e\xc2\x78\x7a\xd0\xb3\x05\x45\xe1"
1524                "\x7c\xde\xda\xa8\x33\xb7\xd6\xb8\xa7\x02\x03\x8b\x27\x4e\xae"
1525                "\xa3\xf4\xe4\xbe\x9d\x91\x4e\xeb\x61\xf1\x70\x2e\x69\x6c\x20"
1526                "\x3a\x12\x68\x54";
1527     a.inLen  = strlen(a.input);
1528     a.outLen = SHA512_DIGEST_SIZE;
1529
1530     b.input  = "what do ya want for nothing?";
1531     b.output = "\x16\x4b\x7a\x7b\xfc\xf8\x19\xe2\xe3\x95\xfb\xe7\x3b\x56\xe0"
1532                "\xa3\x87\xbd\x64\x22\x2e\x83\x1f\xd6\x10\x27\x0c\xd7\xea\x25"
1533                "\x05\x54\x97\x58\xbf\x75\xc0\x5a\x99\x4a\x6d\x03\x4f\x65\xf8"
1534                "\xf0\xe6\xfd\xca\xea\xb1\xa3\x4d\x4a\x6b\x4b\x63\x6e\x07\x0a"
1535                "\x38\xbc\xe7\x37";
1536     b.inLen  = strlen(b.input);
1537     b.outLen = SHA512_DIGEST_SIZE;
1538
1539     c.input  = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
1540                "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
1541                "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
1542                "\xDD\xDD\xDD\xDD\xDD\xDD";
1543     c.output = "\xfa\x73\xb0\x08\x9d\x56\xa2\x84\xef\xb0\xf0\x75\x6c\x89\x0b"
1544                "\xe9\xb1\xb5\xdb\xdd\x8e\xe8\x1a\x36\x55\xf8\x3e\x33\xb2\x27"
1545                "\x9d\x39\xbf\x3e\x84\x82\x79\xa7\x22\xc8\x06\xb4\x85\xa4\x7e"
1546                "\x67\xc8\x07\xb9\x46\xa3\x37\xbe\xe8\x94\x26\x74\x27\x88\x59"
1547                "\xe1\x32\x92\xfb";
1548     c.inLen  = strlen(c.input);
1549     c.outLen = SHA512_DIGEST_SIZE;
1550
1551     test_hmac[0] = a;
1552     test_hmac[1] = b;
1553     test_hmac[2] = c;
1554
1555     for (i = 0; i < times; ++i) {
1556 #if defined(HAVE_FIPS)
1557         if (i == 1)
1558             continue; /* fips not allowed */
1559 #endif
1560         ret = HmacSetKey(&hmac, SHA512, (byte*)keys[i],(word32)strlen(keys[i]));
1561         if (ret != 0)
1562             return -4030;
1563         ret = HmacUpdate(&hmac, (byte*)test_hmac[i].input,
1564                    (word32)test_hmac[i].inLen);
1565         if (ret != 0)
1566             return -4031;
1567         ret = HmacFinal(&hmac, hash);
1568         if (ret != 0)
1569             return -4032;
1570
1571         if (memcmp(hash, test_hmac[i].output, SHA512_DIGEST_SIZE) != 0)
1572             return -20 - i;
1573     }
1574
1575     return 0;
1576 }
1577 #endif
1578
1579
1580 #ifndef NO_RC4
1581 int arc4_test(void)
1582 {
1583     byte cipher[16];
1584     byte plain[16];
1585
1586     const char* keys[] =
1587     {
1588         "\x01\x23\x45\x67\x89\xab\xcd\xef",
1589         "\x01\x23\x45\x67\x89\xab\xcd\xef",
1590         "\x00\x00\x00\x00\x00\x00\x00\x00",
1591         "\xef\x01\x23\x45"
1592     };
1593
1594     testVector a, b, c, d;
1595     testVector test_arc4[4];
1596
1597     int times = sizeof(test_arc4) / sizeof(testVector), i;
1598
1599     a.input  = "\x01\x23\x45\x67\x89\xab\xcd\xef";
1600     a.output = "\x75\xb7\x87\x80\x99\xe0\xc5\x96";
1601     a.inLen  = 8;
1602     a.outLen = 8;
1603
1604     b.input  = "\x00\x00\x00\x00\x00\x00\x00\x00";
1605     b.output = "\x74\x94\xc2\xe7\x10\x4b\x08\x79";
1606     b.inLen  = 8;
1607     b.outLen = 8;
1608
1609     c.input  = "\x00\x00\x00\x00\x00\x00\x00\x00";
1610     c.output = "\xde\x18\x89\x41\xa3\x37\x5d\x3a";
1611     c.inLen  = 8;
1612     c.outLen = 8;
1613
1614     d.input  = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
1615     d.output = "\xd6\xa1\x41\xa7\xec\x3c\x38\xdf\xbd\x61";
1616     d.inLen  = 10;
1617     d.outLen = 10;
1618
1619     test_arc4[0] = a;
1620     test_arc4[1] = b;
1621     test_arc4[2] = c;
1622     test_arc4[3] = d;
1623
1624     for (i = 0; i < times; ++i) {
1625         Arc4 enc;
1626         Arc4 dec;
1627         int  keylen = 8;  /* strlen with key 0x00 not good */
1628         if (i == 3)
1629             keylen = 4;
1630
1631 #ifdef HAVE_CAVIUM
1632         if (Arc4InitCavium(&enc, CAVIUM_DEV_ID) != 0)
1633             return -20001;
1634         if (Arc4InitCavium(&dec, CAVIUM_DEV_ID) != 0)
1635             return -20002;
1636 #endif
1637
1638         Arc4SetKey(&enc, (byte*)keys[i], keylen);
1639         Arc4SetKey(&dec, (byte*)keys[i], keylen);
1640
1641         Arc4Process(&enc, cipher, (byte*)test_arc4[i].input,
1642                     (word32)test_arc4[i].outLen);
1643         Arc4Process(&dec, plain,  cipher, (word32)test_arc4[i].outLen);
1644
1645         if (memcmp(plain, test_arc4[i].input, test_arc4[i].outLen))
1646             return -20 - i;
1647
1648         if (memcmp(cipher, test_arc4[i].output, test_arc4[i].outLen))
1649             return -20 - 5 - i;
1650
1651 #ifdef HAVE_CAVIUM
1652         Arc4FreeCavium(&enc);
1653         Arc4FreeCavium(&dec);
1654 #endif
1655     }
1656
1657     return 0;
1658 }
1659 #endif
1660
1661
1662 int hc128_test(void)
1663 {
1664 #ifdef HAVE_HC128
1665     byte cipher[16];
1666     byte plain[16];
1667
1668     const char* keys[] =
1669     {
1670         "\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
1671         "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
1672         "\x00\x53\xA6\xF9\x4C\x9F\xF2\x45\x98\xEB\x3E\x91\xE4\x37\x8A\xDD",
1673         "\x0F\x62\xB5\x08\x5B\xAE\x01\x54\xA7\xFA\x4D\xA0\xF3\x46\x99\xEC"
1674     };
1675
1676     const char* ivs[] =
1677     {
1678         "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
1679         "\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
1680         "\x0D\x74\xDB\x42\xA9\x10\x77\xDE\x45\xAC\x13\x7A\xE1\x48\xAF\x16",
1681         "\x28\x8F\xF6\x5D\xC4\x2B\x92\xF9\x60\xC7\x2E\x95\xFC\x63\xCA\x31"
1682     };
1683
1684
1685     testVector a, b, c, d;
1686     testVector test_hc128[4];
1687
1688     int times = sizeof(test_hc128) / sizeof(testVector), i;
1689
1690     a.input  = "\x00\x00\x00\x00\x00\x00\x00\x00";
1691     a.output = "\x37\x86\x02\xB9\x8F\x32\xA7\x48";
1692     a.inLen  = 8;
1693     a.outLen = 8;
1694
1695     b.input  = "\x00\x00\x00\x00\x00\x00\x00\x00";
1696     b.output = "\x33\x7F\x86\x11\xC6\xED\x61\x5F";
1697     b.inLen  = 8;
1698     b.outLen = 8;
1699
1700     c.input  = "\x00\x00\x00\x00\x00\x00\x00\x00";
1701     c.output = "\x2E\x1E\xD1\x2A\x85\x51\xC0\x5A";
1702     c.inLen  = 8;
1703     c.outLen = 8;
1704
1705     d.input  = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
1706     d.output = "\x1C\xD8\xAE\xDD\xFE\x52\xE2\x17\xE8\x35\xD0\xB7\xE8\x4E\x29";
1707     d.inLen  = 15;
1708     d.outLen = 15;
1709
1710     test_hc128[0] = a;
1711     test_hc128[1] = b;
1712     test_hc128[2] = c;
1713     test_hc128[3] = d;
1714
1715     for (i = 0; i < times; ++i) {
1716         HC128 enc;
1717         HC128 dec;
1718
1719         /* align keys/ivs in plain/cipher buffers */
1720         memcpy(plain,  keys[i], 16);
1721         memcpy(cipher, ivs[i],  16);
1722
1723         Hc128_SetKey(&enc, plain, cipher);
1724         Hc128_SetKey(&dec, plain, cipher);
1725
1726         /* align input */
1727         memcpy(plain, test_hc128[i].input, test_hc128[i].outLen);
1728         Hc128_Process(&enc, cipher, plain,  (word32)test_hc128[i].outLen);
1729         Hc128_Process(&dec, plain,  cipher, (word32)test_hc128[i].outLen);
1730
1731         if (memcmp(plain, test_hc128[i].input, test_hc128[i].outLen))
1732             return -120 - i;
1733
1734         if (memcmp(cipher, test_hc128[i].output, test_hc128[i].outLen))
1735             return -120 - 5 - i;
1736     }
1737
1738 #endif /* HAVE_HC128 */
1739     return 0;
1740 }
1741
1742
1743 #ifndef NO_RABBIT
1744 int rabbit_test(void)
1745 {
1746     byte cipher[16];
1747     byte plain[16];
1748
1749     const char* keys[] =
1750     {
1751         "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
1752         "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
1753         "\xAC\xC3\x51\xDC\xF1\x62\xFC\x3B\xFE\x36\x3D\x2E\x29\x13\x28\x91"
1754     };
1755
1756     const char* ivs[] =
1757     {
1758         "\x00\x00\x00\x00\x00\x00\x00\x00",
1759         "\x59\x7E\x26\xC1\x75\xF5\x73\xC3",
1760         0
1761     };
1762
1763     testVector a, b, c;
1764     testVector test_rabbit[3];
1765
1766     int times = sizeof(test_rabbit) / sizeof(testVector), i;
1767
1768     a.input  = "\x00\x00\x00\x00\x00\x00\x00\x00";
1769     a.output = "\xED\xB7\x05\x67\x37\x5D\xCD\x7C";
1770     a.inLen  = 8;
1771     a.outLen = 8;
1772
1773     b.input  = "\x00\x00\x00\x00\x00\x00\x00\x00";
1774     b.output = "\x6D\x7D\x01\x22\x92\xCC\xDC\xE0";
1775     b.inLen  = 8;
1776     b.outLen = 8;
1777
1778     c.input  = "\x00\x00\x00\x00\x00\x00\x00\x00";
1779     c.output = "\x04\xCE\xCA\x7A\x1A\x86\x6E\x77";
1780     c.inLen  = 8;
1781     c.outLen = 8;
1782
1783     test_rabbit[0] = a;
1784     test_rabbit[1] = b;
1785     test_rabbit[2] = c;
1786
1787     for (i = 0; i < times; ++i) {
1788         Rabbit enc;
1789         Rabbit dec;
1790         byte*  iv;
1791
1792         /* align keys/ivs in plain/cipher buffers */
1793         memcpy(plain,  keys[i], 16);
1794         if (ivs[i]) {
1795             memcpy(cipher, ivs[i],   8);
1796             iv = cipher;
1797         } else
1798             iv = NULL;
1799         RabbitSetKey(&enc, plain, iv);
1800         RabbitSetKey(&dec, plain, iv);
1801
1802         /* align input */
1803         memcpy(plain, test_rabbit[i].input, test_rabbit[i].outLen);
1804         RabbitProcess(&enc, cipher, plain,  (word32)test_rabbit[i].outLen);
1805         RabbitProcess(&dec, plain,  cipher, (word32)test_rabbit[i].outLen);
1806
1807         if (memcmp(plain, test_rabbit[i].input, test_rabbit[i].outLen))
1808             return -130 - i;
1809
1810         if (memcmp(cipher, test_rabbit[i].output, test_rabbit[i].outLen))
1811             return -130 - 5 - i;
1812     }
1813
1814     return 0;
1815 }
1816 #endif /* NO_RABBIT */
1817
1818
1819 #ifndef NO_DES3
1820 int des_test(void)
1821 {
1822     const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
1823         0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
1824         0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
1825         0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
1826     };
1827
1828     byte plain[24];
1829     byte cipher[24];
1830
1831     Des enc;
1832     Des dec;
1833
1834     const byte key[] =
1835     {
1836         0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
1837     };
1838
1839     const byte iv[] =
1840     {
1841         0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
1842     };
1843
1844     const byte verify[] =
1845     {
1846         0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
1847         0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
1848         0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
1849     };
1850
1851     int ret;
1852
1853     ret = Des_SetKey(&enc, key, iv, DES_ENCRYPTION);
1854     if (ret != 0)
1855         return -31;
1856
1857     Des_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
1858     ret = Des_SetKey(&dec, key, iv, DES_DECRYPTION);
1859     if (ret != 0)
1860         return -32;
1861     Des_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
1862
1863     if (memcmp(plain, vector, sizeof(plain)))
1864         return -33;
1865
1866     if (memcmp(cipher, verify, sizeof(cipher)))
1867         return -34;
1868
1869     return 0;
1870 }
1871 #endif /* NO_DES3 */
1872
1873
1874 #ifndef NO_DES3
1875 int des3_test(void)
1876 {
1877     const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
1878         0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
1879         0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
1880         0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
1881     };
1882
1883     byte plain[24];
1884     byte cipher[24];
1885
1886     Des3 enc;
1887     Des3 dec;
1888
1889     const byte key3[] =
1890     {
1891         0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
1892         0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
1893         0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
1894     };
1895     const byte iv3[] =
1896     {
1897         0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
1898         0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
1899         0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
1900
1901     };
1902
1903     const byte verify3[] =
1904     {
1905         0x43,0xa0,0x29,0x7e,0xd1,0x84,0xf8,0x0e,
1906         0x89,0x64,0x84,0x32,0x12,0xd5,0x08,0x98,
1907         0x18,0x94,0x15,0x74,0x87,0x12,0x7d,0xb0
1908     };
1909
1910     int ret;
1911
1912
1913 #ifdef HAVE_CAVIUM
1914     if (Des3_InitCavium(&enc, CAVIUM_DEV_ID) != 0)
1915         return -20005;
1916     if (Des3_InitCavium(&dec, CAVIUM_DEV_ID) != 0)
1917         return -20006;
1918 #endif
1919     ret = Des3_SetKey(&enc, key3, iv3, DES_ENCRYPTION);
1920     if (ret != 0)
1921         return -31;
1922     ret = Des3_SetKey(&dec, key3, iv3, DES_DECRYPTION);
1923     if (ret != 0)
1924         return -32;
1925     ret = Des3_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
1926     if (ret != 0)
1927         return -33;
1928     ret = Des3_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
1929     if (ret != 0)
1930         return -34;
1931
1932     if (memcmp(plain, vector, sizeof(plain)))
1933         return -35;
1934
1935     if (memcmp(cipher, verify3, sizeof(cipher)))
1936         return -36;
1937
1938 #ifdef HAVE_CAVIUM
1939     Des3_FreeCavium(&enc);
1940     Des3_FreeCavium(&dec);
1941 #endif
1942     return 0;
1943 }
1944 #endif /* NO_DES */
1945
1946
1947 #ifndef NO_AES
1948 int aes_test(void)
1949 {
1950     Aes enc;
1951     Aes dec;
1952
1953     const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
1954         0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
1955         0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
1956         0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
1957     };
1958
1959     const byte verify[] =
1960     {
1961         0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
1962         0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
1963     };
1964
1965     byte key[] = "0123456789abcdef   ";  /* align */
1966     byte iv[]  = "1234567890abcdef   ";  /* align */
1967
1968     byte cipher[AES_BLOCK_SIZE * 4];
1969     byte plain [AES_BLOCK_SIZE * 4];
1970     int  ret;
1971
1972 #ifdef HAVE_CAVIUM
1973         if (AesInitCavium(&enc, CAVIUM_DEV_ID) != 0)
1974             return -20003;
1975         if (AesInitCavium(&dec, CAVIUM_DEV_ID) != 0)
1976             return -20004;
1977 #endif
1978     ret = AesSetKey(&enc, key, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
1979     if (ret != 0)
1980         return -1001;
1981     ret = AesSetKey(&dec, key, AES_BLOCK_SIZE, iv, AES_DECRYPTION);
1982     if (ret != 0)
1983         return -1002;
1984
1985     ret = AesCbcEncrypt(&enc, cipher, msg,   AES_BLOCK_SIZE);
1986     if (ret != 0)
1987         return -1005;
1988     ret = AesCbcDecrypt(&dec, plain, cipher, AES_BLOCK_SIZE);
1989     if (ret != 0)
1990         return -1006;
1991
1992     if (memcmp(plain, msg, AES_BLOCK_SIZE))
1993         return -60;
1994
1995     if (memcmp(cipher, verify, AES_BLOCK_SIZE))
1996         return -61;
1997
1998 #ifdef HAVE_CAVIUM
1999         AesFreeCavium(&enc);
2000         AesFreeCavium(&dec);
2001 #endif
2002 #ifdef CYASSL_AES_COUNTER
2003     {
2004         const byte ctrKey[] =
2005         {
2006             0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
2007             0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
2008         };
2009
2010         const byte ctrIv[] =
2011         {
2012             0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
2013             0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
2014         };
2015
2016
2017         const byte ctrPlain[] =
2018         {
2019             0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
2020             0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
2021             0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
2022             0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
2023             0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
2024             0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
2025             0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
2026             0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
2027         };
2028
2029         const byte ctrCipher[] =
2030         {
2031             0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
2032             0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
2033             0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
2034             0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
2035             0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
2036             0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
2037             0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
2038             0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
2039         };
2040
2041         const byte oddCipher[] =
2042         {
2043             0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
2044             0xc2
2045         };
2046
2047         AesSetKeyDirect(&enc, ctrKey, AES_BLOCK_SIZE, ctrIv, AES_ENCRYPTION);
2048         /* Ctr only uses encrypt, even on key setup */
2049         AesSetKeyDirect(&dec, ctrKey, AES_BLOCK_SIZE, ctrIv, AES_ENCRYPTION);
2050
2051         AesCtrEncrypt(&enc, cipher, ctrPlain, AES_BLOCK_SIZE*4);
2052         AesCtrEncrypt(&dec, plain, cipher, AES_BLOCK_SIZE*4);
2053
2054         if (memcmp(plain, ctrPlain, AES_BLOCK_SIZE*4))
2055             return -66;
2056
2057         if (memcmp(cipher, ctrCipher, AES_BLOCK_SIZE*4))
2058             return -67;
2059
2060         /* let's try with just 9 bytes, non block size test */
2061         AesSetKeyDirect(&enc, ctrKey, AES_BLOCK_SIZE, ctrIv, AES_ENCRYPTION);
2062         /* Ctr only uses encrypt, even on key setup */
2063         AesSetKeyDirect(&dec, ctrKey, AES_BLOCK_SIZE, ctrIv, AES_ENCRYPTION);
2064
2065         AesCtrEncrypt(&enc, cipher, ctrPlain, 9);
2066         AesCtrEncrypt(&dec, plain, cipher, 9);
2067
2068         if (memcmp(plain, ctrPlain, 9))
2069             return -68;
2070
2071         if (memcmp(cipher, ctrCipher, 9))
2072             return -69;
2073
2074         /* and an additional 9 bytes to reuse tmp left buffer */
2075         AesCtrEncrypt(&enc, cipher, ctrPlain, 9);
2076         AesCtrEncrypt(&dec, plain, cipher, 9);
2077
2078         if (memcmp(plain, ctrPlain, 9))
2079             return -70;
2080
2081         if (memcmp(cipher, oddCipher, 9))
2082             return -71;
2083     }
2084 #endif /* CYASSL_AES_COUNTER */
2085
2086 #if defined(CYASSL_AESNI) && defined(CYASSL_AES_DIRECT)
2087     {
2088         const byte niPlain[] =
2089         {
2090             0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
2091             0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
2092         };
2093
2094         const byte niCipher[] =
2095         {
2096             0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
2097             0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
2098         };
2099
2100         const byte niKey[] =
2101         {
2102             0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
2103             0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
2104             0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
2105             0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
2106         };
2107
2108         XMEMSET(cipher, 0, AES_BLOCK_SIZE);
2109         ret = AesSetKey(&enc, niKey, sizeof(niKey), cipher, AES_ENCRYPTION);
2110         if (ret != 0)
2111             return -1003;
2112         AesEncryptDirect(&enc, cipher, niPlain);
2113         if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
2114             return -20006;
2115
2116         XMEMSET(plain, 0, AES_BLOCK_SIZE);
2117         ret = AesSetKey(&dec, niKey, sizeof(niKey), plain, AES_DECRYPTION);
2118         if (ret != 0)
2119             return -1004;
2120         AesDecryptDirect(&dec, plain, niCipher);
2121         if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
2122             return -20007;
2123     }
2124 #endif /* CYASSL_AESNI && CYASSL_AES_DIRECT */
2125
2126     return 0;
2127 }
2128
2129 #ifdef HAVE_AESGCM
2130 int aesgcm_test(void)
2131 {
2132     Aes enc;
2133
2134     /*
2135      * This is Test Case 16 from the document Galois/
2136      * Counter Mode of Operation (GCM) by McGrew and
2137      * Viega.
2138      */
2139     const byte k[] =
2140     {
2141         0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
2142         0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
2143         0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
2144         0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08
2145     };
2146
2147     const byte iv[] =
2148     {
2149         0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad,
2150         0xde, 0xca, 0xf8, 0x88
2151     };
2152
2153     const byte p[] =
2154     {
2155         0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5,
2156         0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a,
2157         0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
2158         0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72,
2159         0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53,
2160         0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25,
2161         0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57,
2162         0xba, 0x63, 0x7b, 0x39
2163     };
2164
2165     const byte a[] =
2166     {
2167         0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
2168         0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
2169         0xab, 0xad, 0xda, 0xd2
2170     };
2171
2172     const byte c[] =
2173     {
2174         0x52, 0x2d, 0xc1, 0xf0, 0x99, 0x56, 0x7d, 0x07,
2175         0xf4, 0x7f, 0x37, 0xa3, 0x2a, 0x84, 0x42, 0x7d,
2176         0x64, 0x3a, 0x8c, 0xdc, 0xbf, 0xe5, 0xc0, 0xc9,
2177         0x75, 0x98, 0xa2, 0xbd, 0x25, 0x55, 0xd1, 0xaa,
2178         0x8c, 0xb0, 0x8e, 0x48, 0x59, 0x0d, 0xbb, 0x3d,
2179         0xa7, 0xb0, 0x8b, 0x10, 0x56, 0x82, 0x88, 0x38,
2180         0xc5, 0xf6, 0x1e, 0x63, 0x93, 0xba, 0x7a, 0x0a,
2181         0xbc, 0xc9, 0xf6, 0x62
2182     };
2183
2184     const byte t[] =
2185     {
2186         0x76, 0xfc, 0x6e, 0xce, 0x0f, 0x4e, 0x17, 0x68,
2187         0xcd, 0xdf, 0x88, 0x53, 0xbb, 0x2d, 0x55, 0x1b
2188     };
2189
2190     byte t2[sizeof(t)];
2191     byte p2[sizeof(c)];
2192     byte c2[sizeof(p)];
2193
2194     int result;
2195
2196     memset(t2, 0, sizeof(t2));
2197     memset(c2, 0, sizeof(c2));
2198     memset(p2, 0, sizeof(p2));
2199
2200     AesGcmSetKey(&enc, k, sizeof(k));
2201     /* AES-GCM encrypt and decrypt both use AES encrypt internally */
2202     AesGcmEncrypt(&enc, c2, p, sizeof(c2), iv, sizeof(iv),
2203                                                  t2, sizeof(t2), a, sizeof(a));
2204     if (memcmp(c, c2, sizeof(c2)))
2205         return -68;
2206     if (memcmp(t, t2, sizeof(t2)))
2207         return -69;
2208
2209     result = AesGcmDecrypt(&enc, p2, c2, sizeof(p2), iv, sizeof(iv),
2210                                                  t2, sizeof(t2), a, sizeof(a));
2211     if (result != 0)
2212         return -70;
2213     if (memcmp(p, p2, sizeof(p2)))
2214         return -71;
2215
2216     return 0;
2217 }
2218
2219 int gmac_test(void)
2220 {
2221     Gmac gmac;
2222
2223     const byte k1[] =
2224     {
2225         0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
2226         0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
2227     };
2228     const byte iv1[] =
2229     {
2230         0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
2231         0xe2, 0x8c, 0x8f, 0x16
2232     };
2233     const byte a1[] =
2234     {
2235        0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
2236        0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
2237     };
2238     const byte t1[] =
2239     {
2240         0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
2241         0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
2242     };
2243
2244     const byte k2[] =
2245     {
2246         0x40, 0xf7, 0xec, 0xb2, 0x52, 0x6d, 0xaa, 0xd4,
2247         0x74, 0x25, 0x1d, 0xf4, 0x88, 0x9e, 0xf6, 0x5b
2248     };
2249     const byte iv2[] =
2250     {
2251         0xee, 0x9c, 0x6e, 0x06, 0x15, 0x45, 0x45, 0x03,
2252         0x1a, 0x60, 0x24, 0xa7
2253     };
2254     const byte a2[] =
2255     {
2256         0x94, 0x81, 0x2c, 0x87, 0x07, 0x4e, 0x15, 0x18,
2257         0x34, 0xb8, 0x35, 0xaf, 0x1c, 0xa5, 0x7e, 0x56
2258     };
2259     const byte t2[] =
2260     {
2261         0xc6, 0x81, 0x79, 0x8e, 0x3d, 0xda, 0xb0, 0x9f,
2262         0x8d, 0x83, 0xb0, 0xbb, 0x14, 0xb6, 0x91
2263     };
2264
2265     const byte k3[] =
2266     {
2267         0xb8, 0xe4, 0x9a, 0x5e, 0x37, 0xf9, 0x98, 0x2b,
2268         0xb9, 0x6d, 0xd0, 0xc9, 0xb6, 0xab, 0x26, 0xac
2269     };
2270     const byte iv3[] =
2271     {
2272         0xe4, 0x4a, 0x42, 0x18, 0x8c, 0xae, 0x94, 0x92,
2273         0x6a, 0x9c, 0x26, 0xb0
2274     };
2275     const byte a3[] =
2276     {
2277         0x9d, 0xb9, 0x61, 0x68, 0xa6, 0x76, 0x7a, 0x31,
2278         0xf8, 0x29, 0xe4, 0x72, 0x61, 0x68, 0x3f, 0x8a
2279     };
2280     const byte t3[] =
2281     {
2282         0x23, 0xe2, 0x9f, 0x66, 0xe4, 0xc6, 0x52, 0x48
2283     };
2284
2285     byte tag[16];
2286
2287     memset(tag, 0, sizeof(tag));
2288     GmacSetKey(&gmac, k1, sizeof(k1));
2289     GmacUpdate(&gmac, iv1, sizeof(iv1), a1, sizeof(a1), tag, sizeof(t1));
2290     if (memcmp(t1, tag, sizeof(t1)) != 0)
2291         return -126;
2292
2293     memset(tag, 0, sizeof(tag));
2294     GmacSetKey(&gmac, k2, sizeof(k2));
2295     GmacUpdate(&gmac, iv2, sizeof(iv2), a2, sizeof(a2), tag, sizeof(t2));
2296     if (memcmp(t2, tag, sizeof(t2)) != 0)
2297         return -127;
2298
2299     memset(tag, 0, sizeof(tag));
2300     GmacSetKey(&gmac, k3, sizeof(k3));
2301     GmacUpdate(&gmac, iv3, sizeof(iv3), a3, sizeof(a3), tag, sizeof(t3));
2302     if (memcmp(t3, tag, sizeof(t3)) != 0)
2303         return -128;
2304
2305     return 0;
2306 }
2307 #endif /* HAVE_AESGCM */
2308
2309 #ifdef HAVE_AESCCM
2310 int aesccm_test(void)
2311 {
2312     Aes enc;
2313
2314     /* key */
2315     const byte k[] =
2316     {
2317         0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
2318         0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
2319     };
2320
2321     /* nonce */
2322     const byte iv[] =
2323     {
2324         0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
2325         0xa1, 0xa2, 0xa3, 0xa4, 0xa5
2326     };
2327
2328     /* plaintext */
2329     const byte p[] =
2330     {
2331         0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
2332         0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
2333         0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
2334     };
2335
2336     const byte a[] =
2337     {
2338         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
2339     };
2340
2341     const byte c[] =
2342     {
2343         0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
2344         0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
2345         0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
2346     };
2347
2348     const byte t[] =
2349     {
2350         0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
2351     };
2352
2353     byte t2[sizeof(t)];
2354     byte p2[sizeof(p)];
2355     byte c2[sizeof(c)];
2356
2357     int result;
2358
2359     memset(t2, 0, sizeof(t2));
2360     memset(c2, 0, sizeof(c2));
2361     memset(p2, 0, sizeof(p2));
2362
2363     AesCcmSetKey(&enc, k, sizeof(k));
2364     /* AES-CCM encrypt and decrypt both use AES encrypt internally */
2365     AesCcmEncrypt(&enc, c2, p, sizeof(c2), iv, sizeof(iv),
2366                                                  t2, sizeof(t2), a, sizeof(a));
2367     if (memcmp(c, c2, sizeof(c2)))
2368         return -107;
2369     if (memcmp(t, t2, sizeof(t2)))
2370         return -108;
2371
2372     result = AesCcmDecrypt(&enc, p2, c2, sizeof(p2), iv, sizeof(iv),
2373                                                  t2, sizeof(t2), a, sizeof(a));
2374     if (result != 0)
2375         return -109;
2376     if (memcmp(p, p2, sizeof(p2)))
2377         return -110;
2378
2379     /* Test the authentication failure */
2380     t2[0]++; /* Corrupt the authentication tag. */
2381     result = AesCcmDecrypt(&enc, p2, c, sizeof(p2), iv, sizeof(iv),
2382                                                  t2, sizeof(t2), a, sizeof(a));
2383     if (result == 0)
2384         return -111;
2385
2386     /* Clear c2 to compare against p2. p2 should be set to zero in case of
2387      * authentication fail. */
2388     memset(c2, 0, sizeof(c2));
2389     if (memcmp(p2, c2, sizeof(p2)))
2390         return -112;
2391
2392     return 0;
2393 }
2394 #endif /* HAVE_AESCCM */
2395
2396
2397 #endif /* NO_AES */
2398
2399
2400 #ifdef HAVE_CAMELLIA
2401
2402 enum {
2403     CAM_ECB_ENC, CAM_ECB_DEC, CAM_CBC_ENC, CAM_CBC_DEC
2404 };
2405
2406 typedef struct {
2407     int type;
2408     const byte* plaintext;
2409     const byte* iv;
2410     const byte* ciphertext;
2411     const byte* key;
2412     word32 keySz;
2413     int errorCode;
2414 } test_vector_t;
2415
2416 int camellia_test(void)
2417 {
2418     /* Camellia ECB Test Plaintext */
2419     static const byte pte[] =
2420     {
2421         0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
2422         0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
2423     };
2424
2425     /* Camellia ECB Test Initialization Vector */
2426     static const byte ive[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
2427
2428     /* Test 1: Camellia ECB 128-bit key */
2429     static const byte k1[] =
2430     {
2431         0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
2432         0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
2433     };
2434     static const byte c1[] =
2435     {
2436         0x67, 0x67, 0x31, 0x38, 0x54, 0x96, 0x69, 0x73,
2437         0x08, 0x57, 0x06, 0x56, 0x48, 0xea, 0xbe, 0x43
2438     };
2439
2440     /* Test 2: Camellia ECB 192-bit key */
2441     static const byte k2[] =
2442     {
2443         0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
2444         0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
2445         0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
2446     };
2447     static const byte c2[] =
2448     {
2449         0xb4, 0x99, 0x34, 0x01, 0xb3, 0xe9, 0x96, 0xf8,
2450         0x4e, 0xe5, 0xce, 0xe7, 0xd7, 0x9b, 0x09, 0xb9
2451     };
2452
2453     /* Test 3: Camellia ECB 256-bit key */
2454     static const byte k3[] =
2455     {
2456         0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
2457         0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
2458         0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
2459         0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
2460     };
2461     static const byte c3[] =
2462     {
2463         0x9a, 0xcc, 0x23, 0x7d, 0xff, 0x16, 0xd7, 0x6c,
2464         0x20, 0xef, 0x7c, 0x91, 0x9e, 0x3a, 0x75, 0x09
2465     };
2466
2467     /* Camellia CBC Test Plaintext */
2468     static const byte ptc[] =
2469     {
2470         0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
2471         0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
2472     };
2473
2474     /* Camellia CBC Test Initialization Vector */
2475     static const byte ivc[] =
2476     {
2477         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
2478         0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
2479     };
2480
2481     /* Test 4: Camellia-CBC 128-bit key */
2482     static const byte k4[] =
2483     {
2484         0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
2485         0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
2486     };
2487     static const byte c4[] =
2488     {
2489         0x16, 0x07, 0xCF, 0x49, 0x4B, 0x36, 0xBB, 0xF0,
2490         0x0D, 0xAE, 0xB0, 0xB5, 0x03, 0xC8, 0x31, 0xAB
2491     };
2492
2493     /* Test 5: Camellia-CBC 192-bit key */
2494     static const byte k5[] =
2495     {
2496         0x8E, 0x73, 0xB0, 0xF7, 0xDA, 0x0E, 0x64, 0x52,
2497         0xC8, 0x10, 0xF3, 0x2B, 0x80, 0x90, 0x79, 0xE5,
2498         0x62, 0xF8, 0xEA, 0xD2, 0x52, 0x2C, 0x6B, 0x7B
2499     };
2500     static const byte c5[] =
2501     {
2502         0x2A, 0x48, 0x30, 0xAB, 0x5A, 0xC4, 0xA1, 0xA2,
2503         0x40, 0x59, 0x55, 0xFD, 0x21, 0x95, 0xCF, 0x93
2504     };
2505
2506     /* Test 6: CBC 256-bit key */
2507     static const byte k6[] =
2508     {
2509         0x60, 0x3D, 0xEB, 0x10, 0x15, 0xCA, 0x71, 0xBE,
2510         0x2B, 0x73, 0xAE, 0xF0, 0x85, 0x7D, 0x77, 0x81,
2511         0x1F, 0x35, 0x2C, 0x07, 0x3B, 0x61, 0x08, 0xD7,
2512         0x2D, 0x98, 0x10, 0xA3, 0x09, 0x14, 0xDF, 0xF4
2513     };
2514     static const byte c6[] =
2515     {
2516         0xE6, 0xCF, 0xA3, 0x5F, 0xC0, 0x2B, 0x13, 0x4A,
2517         0x4D, 0x2C, 0x0B, 0x67, 0x37, 0xAC, 0x3E, 0xDA
2518     };
2519
2520     byte out[CAMELLIA_BLOCK_SIZE];
2521     Camellia cam;
2522     int i, testsSz;
2523     const test_vector_t testVectors[] =
2524     {
2525         {CAM_ECB_ENC, pte, ive, c1, k1, sizeof(k1), -114},
2526         {CAM_ECB_ENC, pte, ive, c2, k2, sizeof(k2), -115},
2527         {CAM_ECB_ENC, pte, ive, c3, k3, sizeof(k3), -116},
2528         {CAM_ECB_DEC, pte, ive, c1, k1, sizeof(k1), -117},
2529         {CAM_ECB_DEC, pte, ive, c2, k2, sizeof(k2), -118},
2530         {CAM_ECB_DEC, pte, ive, c3, k3, sizeof(k3), -119},
2531         {CAM_CBC_ENC, ptc, ivc, c4, k4, sizeof(k4), -120},
2532         {CAM_CBC_ENC, ptc, ivc, c5, k5, sizeof(k5), -121},
2533         {CAM_CBC_ENC, ptc, ivc, c6, k6, sizeof(k6), -122},
2534         {CAM_CBC_DEC, ptc, ivc, c4, k4, sizeof(k4), -123},
2535         {CAM_CBC_DEC, ptc, ivc, c5, k5, sizeof(k5), -124},
2536         {CAM_CBC_DEC, ptc, ivc, c6, k6, sizeof(k6), -125}
2537     };
2538
2539     testsSz = sizeof(testVectors)/sizeof(test_vector_t);
2540     for (i = 0; i < testsSz; i++) {
2541         if (CamelliaSetKey(&cam, testVectors[i].key, testVectors[i].keySz,
2542                                                         testVectors[i].iv) != 0)
2543             return testVectors[i].errorCode;
2544
2545         switch (testVectors[i].type) {
2546             case CAM_ECB_ENC:
2547                 CamelliaEncryptDirect(&cam, out, testVectors[i].plaintext);
2548                 if (memcmp(out, testVectors[i].ciphertext, CAMELLIA_BLOCK_SIZE))
2549                     return testVectors[i].errorCode;
2550                 break;
2551             case CAM_ECB_DEC:
2552                 CamelliaDecryptDirect(&cam, out, testVectors[i].ciphertext);
2553                 if (memcmp(out, testVectors[i].plaintext, CAMELLIA_BLOCK_SIZE))
2554                     return testVectors[i].errorCode;
2555                 break;
2556             case CAM_CBC_ENC:
2557                 CamelliaCbcEncrypt(&cam, out, testVectors[i].plaintext,
2558                                                            CAMELLIA_BLOCK_SIZE);
2559                 if (memcmp(out, testVectors[i].ciphertext, CAMELLIA_BLOCK_SIZE))
2560                     return testVectors[i].errorCode;
2561                 break;
2562             case CAM_CBC_DEC:
2563                 CamelliaCbcDecrypt(&cam, out, testVectors[i].ciphertext,
2564                                                            CAMELLIA_BLOCK_SIZE);
2565                 if (memcmp(out, testVectors[i].plaintext, CAMELLIA_BLOCK_SIZE))
2566                     return testVectors[i].errorCode;
2567                 break;
2568             default:
2569                 break;
2570         }
2571     }
2572
2573     /* Setting the IV and checking it was actually set. */
2574     CamelliaSetIV(&cam, ivc);
2575     if (XMEMCMP(cam.reg, ivc, CAMELLIA_BLOCK_SIZE))
2576         return -1;
2577
2578     /* Setting the IV to NULL should be same as all zeros IV */
2579     if (CamelliaSetIV(&cam, NULL) != 0 ||
2580                                     XMEMCMP(cam.reg, ive, CAMELLIA_BLOCK_SIZE))
2581         return -1;
2582
2583     /* First parameter should never be null */
2584     if (CamelliaSetIV(NULL, NULL) == 0)
2585         return -1;
2586
2587     /* First parameter should never be null, check it fails */
2588     if (CamelliaSetKey(NULL, k1, sizeof(k1), NULL) == 0)
2589         return -1;
2590
2591     /* Key should have a size of 16, 24, or 32 */
2592     if (CamelliaSetKey(&cam, k1, 0, NULL) == 0)
2593         return -1;
2594
2595     return 0;
2596 }
2597 #endif /* HAVE_CAMELLIA */
2598
2599
2600 #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
2601
2602 int random_test(void)
2603 {
2604     const byte test1Entropy[] =
2605     {
2606         0xa6, 0x5a, 0xd0, 0xf3, 0x45, 0xdb, 0x4e, 0x0e, 0xff, 0xe8, 0x75, 0xc3,
2607         0xa2, 0xe7, 0x1f, 0x42, 0xc7, 0x12, 0x9d, 0x62, 0x0f, 0xf5, 0xc1, 0x19,
2608         0xa9, 0xef, 0x55, 0xf0, 0x51, 0x85, 0xe0, 0xfb, 0x85, 0x81, 0xf9, 0x31,
2609         0x75, 0x17, 0x27, 0x6e, 0x06, 0xe9, 0x60, 0x7d, 0xdb, 0xcb, 0xcc, 0x2e
2610     };
2611     const byte test1Output[] =
2612     {
2613         0xd3, 0xe1, 0x60, 0xc3, 0x5b, 0x99, 0xf3, 0x40, 0xb2, 0x62, 0x82, 0x64,
2614         0xd1, 0x75, 0x10, 0x60, 0xe0, 0x04, 0x5d, 0xa3, 0x83, 0xff, 0x57, 0xa5,
2615         0x7d, 0x73, 0xa6, 0x73, 0xd2, 0xb8, 0xd8, 0x0d, 0xaa, 0xf6, 0xa6, 0xc3,
2616         0x5a, 0x91, 0xbb, 0x45, 0x79, 0xd7, 0x3f, 0xd0, 0xc8, 0xfe, 0xd1, 0x11,
2617         0xb0, 0x39, 0x13, 0x06, 0x82, 0x8a, 0xdf, 0xed, 0x52, 0x8f, 0x01, 0x81,
2618         0x21, 0xb3, 0xfe, 0xbd, 0xc3, 0x43, 0xe7, 0x97, 0xb8, 0x7d, 0xbb, 0x63,
2619         0xdb, 0x13, 0x33, 0xde, 0xd9, 0xd1, 0xec, 0xe1, 0x77, 0xcf, 0xa6, 0xb7,
2620         0x1f, 0xe8, 0xab, 0x1d, 0xa4, 0x66, 0x24, 0xed, 0x64, 0x15, 0xe5, 0x1c,
2621         0xcd, 0xe2, 0xc7, 0xca, 0x86, 0xe2, 0x83, 0x99, 0x0e, 0xea, 0xeb, 0x91,
2622         0x12, 0x04, 0x15, 0x52, 0x8b, 0x22, 0x95, 0x91, 0x02, 0x81, 0xb0, 0x2d,
2623         0xd4, 0x31, 0xf4, 0xc9, 0xf7, 0x04, 0x27, 0xdf
2624     };
2625     const byte test2EntropyA[] =
2626     {
2627         0x63, 0x36, 0x33, 0x77, 0xe4, 0x1e, 0x86, 0x46, 0x8d, 0xeb, 0x0a, 0xb4,
2628         0xa8, 0xed, 0x68, 0x3f, 0x6a, 0x13, 0x4e, 0x47, 0xe0, 0x14, 0xc7, 0x00,
2629         0x45, 0x4e, 0x81, 0xe9, 0x53, 0x58, 0xa5, 0x69, 0x80, 0x8a, 0xa3, 0x8f,
2630         0x2a, 0x72, 0xa6, 0x23, 0x59, 0x91, 0x5a, 0x9f, 0x8a, 0x04, 0xca, 0x68
2631     };
2632     const byte test2EntropyB[] =
2633     {
2634         0xe6, 0x2b, 0x8a, 0x8e, 0xe8, 0xf1, 0x41, 0xb6, 0x98, 0x05, 0x66, 0xe3,
2635         0xbf, 0xe3, 0xc0, 0x49, 0x03, 0xda, 0xd4, 0xac, 0x2c, 0xdf, 0x9f, 0x22,
2636         0x80, 0x01, 0x0a, 0x67, 0x39, 0xbc, 0x83, 0xd3
2637     };
2638     const byte test2Output[] =
2639     {
2640         0x04, 0xee, 0xc6, 0x3b, 0xb2, 0x31, 0xdf, 0x2c, 0x63, 0x0a, 0x1a, 0xfb,
2641         0xe7, 0x24, 0x94, 0x9d, 0x00, 0x5a, 0x58, 0x78, 0x51, 0xe1, 0xaa, 0x79,
2642         0x5e, 0x47, 0x73, 0x47, 0xc8, 0xb0, 0x56, 0x62, 0x1c, 0x18, 0xbd, 0xdc,
2643         0xdd, 0x8d, 0x99, 0xfc, 0x5f, 0xc2, 0xb9, 0x20, 0x53, 0xd8, 0xcf, 0xac,
2644         0xfb, 0x0b, 0xb8, 0x83, 0x12, 0x05, 0xfa, 0xd1, 0xdd, 0xd6, 0xc0, 0x71,
2645         0x31, 0x8a, 0x60, 0x18, 0xf0, 0x3b, 0x73, 0xf5, 0xed, 0xe4, 0xd4, 0xd0,
2646         0x71, 0xf9, 0xde, 0x03, 0xfd, 0x7a, 0xea, 0x10, 0x5d, 0x92, 0x99, 0xb8,
2647         0xaf, 0x99, 0xaa, 0x07, 0x5b, 0xdb, 0x4d, 0xb9, 0xaa, 0x28, 0xc1, 0x8d,
2648         0x17, 0x4b, 0x56, 0xee, 0x2a, 0x01, 0x4d, 0x09, 0x88, 0x96, 0xff, 0x22,
2649         0x82, 0xc9, 0x55, 0xa8, 0x19, 0x69, 0xe0, 0x69, 0xfa, 0x8c, 0xe0, 0x07,
2650         0xa1, 0x80, 0x18, 0x3a, 0x07, 0xdf, 0xae, 0x17
2651     };
2652     int ret;
2653
2654     ret = RNG_HealthTest(0, test1Entropy, sizeof(test1Entropy), NULL, 0,
2655                             test1Output, sizeof(test1Output));
2656     if (ret != 0) return -39;
2657
2658     ret = RNG_HealthTest(1, test2EntropyA, sizeof(test2EntropyA),
2659                             test2EntropyB, sizeof(test2EntropyB),
2660                             test2Output, sizeof(test2Output));
2661     if (ret != 0) return -40;
2662
2663     return 0;
2664 }
2665
2666 #else /* HAVE_HASHDRBG || NO_RC4 */
2667
2668 int random_test(void)
2669 {
2670     RNG  rng;
2671     byte block[32];
2672     int ret;
2673
2674 #ifdef HAVE_CAVIUM
2675     ret = InitRngCavium(&rng, CAVIUM_DEV_ID);
2676     if (ret != 0) return -2007;
2677 #endif
2678     ret = InitRng(&rng);
2679     if (ret != 0) return -39;
2680
2681     ret = RNG_GenerateBlock(&rng, block, sizeof(block));
2682     if (ret != 0) return -40;
2683
2684     return 0;
2685 }
2686
2687 #endif /* HAVE_HASHDRBG || NO_RC4 */
2688
2689
2690 #ifdef HAVE_NTRU
2691
2692 byte GetEntropy(ENTROPY_CMD cmd, byte* out);
2693
2694 byte GetEntropy(ENTROPY_CMD cmd, byte* out)
2695 {
2696     static RNG rng;
2697
2698     if (cmd == INIT)
2699         return (InitRng(&rng) == 0) ? 1 : 0;
2700
2701     if (out == NULL)
2702         return 0;
2703
2704     if (cmd == GET_BYTE_OF_ENTROPY)
2705         return (RNG_GenerateBlock(&rng, out, 1) == 0) ? 1 : 0;
2706
2707     if (cmd == GET_NUM_BYTES_PER_BYTE_OF_ENTROPY) {
2708         *out = 1;
2709         return 1;
2710     }
2711
2712     return 0;
2713 }
2714
2715 #endif /* HAVE_NTRU */
2716
2717 #ifndef NO_RSA
2718
2719 #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
2720     #ifdef FREESCALE_MQX
2721         static const char* clientKey  = "a:\\certs\\client-key.der";
2722         static const char* clientCert = "a:\\certs\\client-cert.der";
2723         #ifdef CYASSL_CERT_GEN
2724             static const char* caKeyFile  = "a:\\certs\\ca-key.der";
2725             static const char* caCertFile = "a:\\certs\\ca-cert.pem";
2726             #ifdef HAVE_ECC
2727                 static const char* eccCaKeyFile  = "a:\\certs\\ecc-key.der";
2728                 static const char* eccCaCertFile = "a:\\certs\\server-ecc.pem";
2729             #endif
2730         #endif
2731     #elif defined(CYASSL_MKD_SHELL)
2732         static char* clientKey = "certs/client-key.der";
2733         static char* clientCert = "certs/client-cert.der";
2734         void set_clientKey(char *key) {  clientKey = key ; }
2735         void set_clientCert(char *cert) {  clientCert = cert ; }
2736         #ifdef CYASSL_CERT_GEN
2737             static char* caKeyFile  = "certs/ca-key.der";
2738             static char* caCertFile = "certs/ca-cert.pem";
2739             void set_caKeyFile (char * key)  { caKeyFile   = key ; }
2740             void set_caCertFile(char * cert) { caCertFile = cert ; }
2741             #ifdef HAVE_ECC
2742                 static const char* eccCaKeyFile  = "certs/ecc-key.der";
2743                 static const char* eccCaCertFile = "certs/server-ecc.pem";
2744                 void set_eccCaKeyFile (char * key)  { eccCaKeyFile  = key ; }
2745                 void set_eccCaCertFile(char * cert) { eccCaCertFile = cert ; }
2746             #endif
2747         #endif
2748     #else
2749         static const char* clientKey  = "./certs/client-key.der";
2750         static const char* clientCert = "./certs/client-cert.der";
2751         #ifdef CYASSL_CERT_GEN
2752             static const char* caKeyFile  = "./certs/ca-key.der";
2753             static const char* caCertFile = "./certs/ca-cert.pem";
2754             #ifdef HAVE_ECC
2755                 static const char* eccCaKeyFile  = "./certs/ecc-key.der";
2756                 static const char* eccCaCertFile = "./certs/server-ecc.pem";
2757             #endif
2758         #endif
2759     #endif
2760 #endif
2761
2762
2763
2764 #define FOURK_BUF 4096
2765
2766 int rsa_test(void)
2767 {
2768     byte*   tmp;
2769     size_t bytes;
2770     RsaKey key;
2771     RNG    rng;
2772     word32 idx = 0;
2773     int    ret;
2774     byte   in[] = "Everyone gets Friday off.";
2775     word32 inLen = (word32)strlen((char*)in);
2776     byte   out[256];
2777     byte   plain[256];
2778 #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
2779     FILE*  file, * file2;
2780 #endif
2781 #ifdef CYASSL_TEST_CERT
2782     DecodedCert cert;
2783 #endif
2784
2785     tmp = (byte*)malloc(FOURK_BUF);
2786     if (tmp == NULL)
2787         return -40;
2788
2789 #ifdef USE_CERT_BUFFERS_1024
2790     XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
2791     bytes = sizeof_client_key_der_1024;
2792 #elif defined(USE_CERT_BUFFERS_2048)
2793     XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
2794     bytes = sizeof_client_key_der_2048;
2795 #else
2796     file = fopen(clientKey, "rb");
2797
2798     if (!file)
2799         err_sys("can't open ./certs/client-key.der, "
2800                 "Please run from CyaSSL home dir", -40);
2801
2802     bytes = fread(tmp, 1, FOURK_BUF, file);
2803     fclose(file);
2804 #endif /* USE_CERT_BUFFERS */
2805
2806 #ifdef HAVE_CAVIUM
2807     RsaInitCavium(&key, CAVIUM_DEV_ID);
2808 #endif
2809     ret = InitRsaKey(&key, 0);
2810     if (ret != 0) return -39;
2811     ret = RsaPrivateKeyDecode(tmp, &idx, &key, (word32)bytes);
2812     if (ret != 0) return -41;
2813
2814     ret = InitRng(&rng);
2815     if (ret != 0) return -42;
2816
2817     ret = RsaPublicEncrypt(in, inLen, out, sizeof(out), &key, &rng);
2818     if (ret < 0) return -43;
2819
2820     ret = RsaPrivateDecrypt(out, ret, plain, sizeof(plain), &key);
2821     if (ret < 0) return -44;
2822
2823     if (memcmp(plain, in, inLen)) return -45;
2824
2825     ret = RsaSSL_Sign(in, inLen, out, sizeof(out), &key, &rng);
2826     if (ret < 0) return -46;
2827
2828     memset(plain, 0, sizeof(plain));
2829     ret = RsaSSL_Verify(out, ret, plain, sizeof(plain), &key);
2830     if (ret < 0) return -47;
2831
2832     if (memcmp(plain, in, ret)) return -48;
2833
2834 #if defined(CYASSL_MDK_ARM)
2835     #define sizeof(s) strlen((char *)(s))
2836 #endif
2837
2838 #ifdef USE_CERT_BUFFERS_1024
2839     XMEMCPY(tmp, client_cert_der_1024, sizeof_client_cert_der_1024);
2840     bytes = sizeof_client_cert_der_1024;
2841 #elif defined(USE_CERT_BUFFERS_2048)
2842     XMEMCPY(tmp, client_cert_der_2048, sizeof_client_cert_der_2048);
2843     bytes = sizeof_client_cert_der_2048;
2844 #else
2845     file2 = fopen(clientCert, "rb");
2846     if (!file2)
2847         return -49;
2848
2849     bytes = fread(tmp, 1, FOURK_BUF, file2);
2850     fclose(file2);
2851 #endif
2852
2853 #ifdef sizeof
2854                 #undef sizeof
2855 #endif
2856
2857 #ifdef CYASSL_TEST_CERT
2858     InitDecodedCert(&cert, tmp, (word32)bytes, 0);
2859
2860     ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
2861     if (ret != 0) return -491;
2862
2863     FreeDecodedCert(&cert);
2864 #else
2865     (void)bytes;
2866 #endif
2867
2868
2869 #ifdef CYASSL_KEY_GEN
2870     {
2871         byte*  der;
2872         byte*  pem;
2873         int    derSz = 0;
2874         int    pemSz = 0;
2875         RsaKey derIn;
2876         RsaKey genKey;
2877         FILE*  keyFile;
2878         FILE*  pemFile;
2879
2880         ret = InitRsaKey(&genKey, 0);
2881         if (ret != 0)
2882             return -300;
2883         ret = MakeRsaKey(&genKey, 1024, 65537, &rng);
2884         if (ret != 0)
2885             return -301;
2886
2887         der = (byte*)malloc(FOURK_BUF);
2888         if (der == NULL) {
2889             FreeRsaKey(&genKey);
2890             return -307;
2891         }
2892         pem = (byte*)malloc(FOURK_BUF);
2893         if (pem == NULL) {
2894             free(der);
2895             FreeRsaKey(&genKey);
2896             return -308;
2897         }
2898
2899         derSz = RsaKeyToDer(&genKey, der, FOURK_BUF);
2900         if (derSz < 0) {
2901             free(der);
2902             free(pem);
2903             return -302;
2904         }
2905
2906         keyFile = fopen("./key.der", "wb");
2907         if (!keyFile) {
2908             free(der);
2909             free(pem);
2910             FreeRsaKey(&genKey);
2911             return -303;
2912         }
2913         ret = (int)fwrite(der, 1, derSz, keyFile);
2914         fclose(keyFile);
2915         if (ret != derSz) {
2916             free(der);
2917             free(pem);
2918             FreeRsaKey(&genKey);
2919             return -313;
2920         }
2921
2922         pemSz = DerToPem(der, derSz, pem, FOURK_BUF, PRIVATEKEY_TYPE);
2923         if (pemSz < 0) {
2924             free(der);
2925             free(pem);
2926             FreeRsaKey(&genKey);
2927             return -304;
2928         }
2929
2930         pemFile = fopen("./key.pem", "wb");
2931         if (!pemFile) {
2932             free(der);
2933             free(pem);
2934             FreeRsaKey(&genKey);
2935             return -305;
2936         }
2937         ret = (int)fwrite(pem, 1, pemSz, pemFile);
2938         fclose(pemFile);
2939         if (ret != pemSz) {
2940             free(der);
2941             free(pem);
2942             FreeRsaKey(&genKey);
2943             return -314;
2944         }
2945
2946         ret = InitRsaKey(&derIn, 0);
2947         if (ret != 0) {
2948             free(der);
2949             free(pem);
2950             FreeRsaKey(&genKey);
2951             return -3060;
2952         }
2953         idx = 0;
2954         ret = RsaPrivateKeyDecode(der, &idx, &derIn, derSz);
2955         if (ret != 0) {
2956             free(der);
2957             free(pem);
2958             FreeRsaKey(&derIn);
2959             FreeRsaKey(&genKey);
2960             return -306;
2961         }
2962
2963         FreeRsaKey(&derIn);
2964         FreeRsaKey(&genKey);
2965         free(pem);
2966         free(der);
2967     }
2968 #endif /* CYASSL_KEY_GEN */
2969
2970
2971 #ifdef CYASSL_CERT_GEN
2972     /* self signed */
2973     {
2974         Cert        myCert;
2975         byte*       derCert;
2976         byte*       pem;
2977         FILE*       derFile;
2978         FILE*       pemFile;
2979         int         certSz;
2980         int         pemSz;
2981 #ifdef CYASSL_TEST_CERT
2982         DecodedCert decode;
2983 #endif
2984
2985         derCert = (byte*)malloc(FOURK_BUF);
2986         if (derCert == NULL)
2987             return -309;
2988         pem = (byte*)malloc(FOURK_BUF);
2989         if (pem == NULL) {
2990             free(derCert);
2991             return -310;
2992         }
2993
2994         InitCert(&myCert);
2995
2996         strncpy(myCert.subject.country, "US", CTC_NAME_SIZE);
2997         strncpy(myCert.subject.state, "OR", CTC_NAME_SIZE);
2998         strncpy(myCert.subject.locality, "Portland", CTC_NAME_SIZE);
2999         strncpy(myCert.subject.org, "yaSSL", CTC_NAME_SIZE);
3000         strncpy(myCert.subject.unit, "Development", CTC_NAME_SIZE);
3001         strncpy(myCert.subject.commonName, "www.yassl.com", CTC_NAME_SIZE);
3002         strncpy(myCert.subject.email, "info@yassl.com", CTC_NAME_SIZE);
3003         myCert.isCA    = 1;
3004         myCert.sigType = CTC_SHA256wRSA;
3005
3006         certSz = MakeSelfCert(&myCert, derCert, FOURK_BUF, &key, &rng);
3007         if (certSz < 0) {
3008             free(derCert);
3009             free(pem);
3010             return -401;
3011         }
3012
3013 #ifdef CYASSL_TEST_CERT
3014         InitDecodedCert(&decode, derCert, certSz, 0);
3015         ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
3016         if (ret != 0) {
3017             free(derCert);
3018             free(pem);
3019             return -402;
3020         }
3021         FreeDecodedCert(&decode);
3022 #endif
3023         derFile = fopen("./cert.der", "wb");
3024         if (!derFile) {
3025             free(derCert);
3026             free(pem);
3027             return -403;
3028         }
3029         ret = (int)fwrite(derCert, 1, certSz, derFile);
3030         fclose(derFile);
3031         if (ret != certSz) {
3032             free(derCert);
3033             free(pem);
3034             return -414;
3035         }
3036
3037         pemSz = DerToPem(derCert, certSz, pem, FOURK_BUF, CERT_TYPE);
3038         if (pemSz < 0) {
3039             free(derCert);
3040             free(pem);
3041             return -404;
3042         }
3043
3044         pemFile = fopen("./cert.pem", "wb");
3045         if (!pemFile) {
3046             free(derCert);
3047             free(pem);
3048             return -405;
3049         }
3050         ret = (int)fwrite(pem, 1, pemSz, pemFile);
3051         fclose(pemFile);
3052         if (ret != pemSz) {
3053             free(derCert);
3054             free(pem);
3055             return -406;
3056         }
3057         free(pem);
3058         free(derCert);
3059     }
3060     /* CA style */
3061     {
3062         RsaKey      caKey;
3063         Cert        myCert;
3064         byte*       derCert;
3065         byte*       pem;
3066         FILE*       derFile;
3067         FILE*       pemFile;
3068         int         certSz;
3069         int         pemSz;
3070         size_t      bytes3;
3071         word32      idx3 = 0;
3072         FILE*       file3 ;
3073 #ifdef CYASSL_TEST_CERT
3074         DecodedCert decode;
3075 #endif
3076
3077         derCert = (byte*)malloc(FOURK_BUF);
3078         if (derCert == NULL)
3079             return -311;
3080         pem = (byte*)malloc(FOURK_BUF);
3081         if (pem == NULL) {
3082             free(derCert);
3083             return -312;
3084         }
3085
3086         file3 = fopen(caKeyFile, "rb");
3087
3088         if (!file3) {
3089             free(derCert);
3090             free(pem);
3091             return -412;
3092         }
3093
3094         bytes3 = fread(tmp, 1, FOURK_BUF, file3);
3095         fclose(file3);
3096
3097         ret = InitRsaKey(&caKey, 0);
3098         if (ret != 0) {
3099             free(derCert);
3100             free(pem);
3101             return -411;
3102         }
3103         ret = RsaPrivateKeyDecode(tmp, &idx3, &caKey, (word32)bytes3);
3104         if (ret != 0) {
3105             free(derCert);
3106             free(pem);
3107             FreeRsaKey(&caKey);
3108             return -413;
3109         }
3110
3111         InitCert(&myCert);
3112
3113         strncpy(myCert.subject.country, "US", CTC_NAME_SIZE);
3114         strncpy(myCert.subject.state, "OR", CTC_NAME_SIZE);
3115         strncpy(myCert.subject.locality, "Portland", CTC_NAME_SIZE);
3116         strncpy(myCert.subject.org, "yaSSL", CTC_NAME_SIZE);
3117         strncpy(myCert.subject.unit, "Development", CTC_NAME_SIZE);
3118         strncpy(myCert.subject.commonName, "www.yassl.com", CTC_NAME_SIZE);
3119         strncpy(myCert.subject.email, "info@yassl.com", CTC_NAME_SIZE);
3120
3121         ret = SetIssuer(&myCert, caCertFile);
3122         if (ret < 0) {
3123             free(derCert);
3124             free(pem);
3125             FreeRsaKey(&caKey);
3126             return -405;
3127         }
3128
3129         certSz = MakeCert(&myCert, derCert, FOURK_BUF, &key, NULL, &rng);
3130         if (certSz < 0) {
3131             free(derCert);
3132             free(pem);
3133             FreeRsaKey(&caKey);
3134             return -407;
3135         }
3136
3137         certSz = SignCert(myCert.bodySz, myCert.sigType, derCert, FOURK_BUF,
3138                           &caKey, NULL, &rng);
3139         if (certSz < 0) {
3140             free(derCert);
3141             free(pem);
3142             FreeRsaKey(&caKey);
3143             return -408;
3144         }
3145
3146
3147 #ifdef CYASSL_TEST_CERT
3148         InitDecodedCert(&decode, derCert, certSz, 0);
3149         ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
3150         if (ret != 0) {
3151             free(derCert);
3152             free(pem);
3153             FreeRsaKey(&caKey);
3154             return -409;
3155         }
3156         FreeDecodedCert(&decode);
3157 #endif
3158
3159         derFile = fopen("./othercert.der", "wb");
3160         if (!derFile) {
3161             free(derCert);
3162             free(pem);
3163             FreeRsaKey(&caKey);
3164             return -410;
3165         }
3166         ret = (int)fwrite(derCert, 1, certSz, derFile);
3167         fclose(derFile);
3168         if (ret != certSz) {
3169             free(derCert);
3170             free(pem);
3171             FreeRsaKey(&caKey);
3172             return -416;
3173         }
3174
3175         pemSz = DerToPem(derCert, certSz, pem, FOURK_BUF, CERT_TYPE);
3176         if (pemSz < 0) {
3177             free(derCert);
3178             free(pem);
3179             FreeRsaKey(&caKey);
3180             return -411;
3181         }
3182
3183         pemFile = fopen("./othercert.pem", "wb");
3184         if (!pemFile) {
3185             free(derCert);
3186             free(pem);
3187             FreeRsaKey(&caKey);
3188             return -412;
3189         }
3190         ret = (int)fwrite(pem, 1, pemSz, pemFile);
3191         if (ret != pemSz) {
3192             free(derCert);
3193             free(pem);
3194             FreeRsaKey(&caKey);
3195             return -415;
3196         }
3197         fclose(pemFile);
3198         free(pem);
3199         free(derCert);
3200         FreeRsaKey(&caKey);
3201     }
3202 #ifdef HAVE_ECC
3203     /* ECC CA style */
3204     {
3205         ecc_key     caKey;
3206         Cert        myCert;
3207         byte*       derCert;
3208         byte*       pem;
3209         FILE*       derFile;
3210         FILE*       pemFile;
3211         int         certSz;
3212         int         pemSz;
3213         size_t      bytes3;
3214         word32      idx3 = 0;
3215         FILE*       file3;
3216 #ifdef CYASSL_TEST_CERT
3217         DecodedCert decode;
3218 #endif
3219
3220         derCert = (byte*)malloc(FOURK_BUF);
3221         if (derCert == NULL)
3222             return -5311;
3223         pem = (byte*)malloc(FOURK_BUF);
3224         if (pem == NULL) {
3225             free(derCert);
3226             return -5312;
3227         }
3228
3229         file3 = fopen(eccCaKeyFile, "rb");
3230
3231         if (!file3) {
3232             free(derCert);
3233             free(pem);
3234             return -5412;
3235         }
3236
3237         bytes3 = fread(tmp, 1, FOURK_BUF, file3);
3238         fclose(file3);
3239
3240         ecc_init(&caKey);
3241         ret = EccPrivateKeyDecode(tmp, &idx3, &caKey, (word32)bytes3);
3242         if (ret != 0) {
3243             free(derCert);
3244             free(pem);
3245             return -5413;
3246         }
3247
3248         InitCert(&myCert);
3249         myCert.sigType = CTC_SHA256wECDSA;
3250
3251         strncpy(myCert.subject.country, "US", CTC_NAME_SIZE);
3252         strncpy(myCert.subject.state, "OR", CTC_NAME_SIZE);
3253         strncpy(myCert.subject.locality, "Portland", CTC_NAME_SIZE);
3254         strncpy(myCert.subject.org, "wolfSSL", CTC_NAME_SIZE);
3255         strncpy(myCert.subject.unit, "Development", CTC_NAME_SIZE);
3256         strncpy(myCert.subject.commonName, "www.wolfssl.com", CTC_NAME_SIZE);
3257         strncpy(myCert.subject.email, "info@wolfssl.com", CTC_NAME_SIZE);
3258
3259         ret = SetIssuer(&myCert, eccCaCertFile);
3260         if (ret < 0) {
3261             free(pem);
3262             free(derCert);
3263             ecc_free(&caKey);
3264             return -5405;
3265         }
3266
3267         certSz = MakeCert(&myCert, derCert, FOURK_BUF, NULL, &caKey, &rng);
3268         if (certSz < 0) {
3269             free(pem);
3270             free(derCert);
3271             ecc_free(&caKey);
3272             return -5407;
3273         }
3274
3275         certSz = SignCert(myCert.bodySz, myCert.sigType, derCert, FOURK_BUF,
3276                           NULL, &caKey, &rng);
3277         if (certSz < 0) {
3278             free(pem);
3279             free(derCert);
3280             ecc_free(&caKey);
3281             return -5408;
3282         }
3283
3284 #ifdef CYASSL_TEST_CERT
3285         InitDecodedCert(&decode, derCert, certSz, 0);
3286         ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
3287         if (ret != 0) {
3288             free(pem);
3289             free(derCert);
3290             ecc_free(&caKey);
3291             return -5409;
3292         }
3293         FreeDecodedCert(&decode);
3294 #endif
3295
3296         derFile = fopen("./certecc.der", "wb");
3297         if (!derFile) {
3298             free(pem);
3299             free(derCert);
3300             ecc_free(&caKey);
3301             return -5410;
3302         }
3303         ret = (int)fwrite(derCert, 1, certSz, derFile);
3304         fclose(derFile);
3305         if (ret != certSz) {
3306             free(pem);
3307             free(derCert);
3308             ecc_free(&caKey);
3309             return -5414;
3310         }
3311
3312         pemSz = DerToPem(derCert, certSz, pem, FOURK_BUF, CERT_TYPE);
3313         if (pemSz < 0) {
3314             free(pem);
3315             free(derCert);
3316             ecc_free(&caKey);
3317             return -5411;
3318         }
3319
3320         pemFile = fopen("./certecc.pem", "wb");
3321         if (!pemFile) {
3322             free(pem);
3323             free(derCert);
3324             ecc_free(&caKey);
3325             return -5412;
3326         }
3327         ret = (int)fwrite(pem, 1, pemSz, pemFile);
3328         if (ret != pemSz) {
3329             free(pem);
3330             free(derCert);
3331             ecc_free(&caKey);
3332             return -5415;
3333         }
3334         fclose(pemFile);
3335         free(pem);
3336         free(derCert);
3337         ecc_free(&caKey);
3338     }
3339 #endif /* HAVE_ECC */
3340 #ifdef HAVE_NTRU
3341     {
3342         RsaKey      caKey;
3343         Cert        myCert;
3344         byte*       derCert;
3345         byte*       pem;
3346         FILE*       derFile;
3347         FILE*       pemFile;
3348         FILE*       caFile;
3349         FILE*       ntruPrivFile;
3350         int         certSz;
3351         int         pemSz;
3352         word32      idx3;
3353 #ifdef CYASSL_TEST_CERT
3354         DecodedCert decode;
3355 #endif
3356         derCert = (byte*)malloc(FOURK_BUF);
3357         if (derCert == NULL)
3358             return -311;
3359         pem = (byte*)malloc(FOURK_BUF);
3360         if (pem == NULL) {
3361             free(derCert);
3362             return -312;
3363         }
3364
3365         byte   public_key[557];          /* sized for EES401EP2 */
3366         word16 public_key_len;           /* no. of octets in public key */
3367         byte   private_key[607];         /* sized for EES401EP2 */
3368         word16 private_key_len;          /* no. of octets in private key */
3369         DRBG_HANDLE drbg;
3370         static uint8_t const pers_str[] = {
3371                 'C', 'y', 'a', 'S', 'S', 'L', ' ', 't', 'e', 's', 't'
3372         };
3373         word32 rc = ntru_crypto_drbg_instantiate(112, pers_str,
3374                           sizeof(pers_str), GetEntropy, &drbg);
3375         if (rc != DRBG_OK) {
3376             free(derCert);
3377             free(pem);
3378             return -448;
3379         }
3380
3381         rc = ntru_crypto_ntru_encrypt_keygen(drbg, NTRU_EES401EP2,
3382                                              &public_key_len, NULL,
3383                                              &private_key_len, NULL);
3384         if (rc != NTRU_OK) {
3385             free(derCert);
3386             free(pem);
3387             return -449;
3388         }
3389
3390         rc = ntru_crypto_ntru_encrypt_keygen(drbg, NTRU_EES401EP2,
3391                                              &public_key_len, public_key,
3392                                              &private_key_len, private_key);
3393         if (rc != NTRU_OK) {
3394             free(derCert);
3395             free(pem);
3396             return -450;
3397         }
3398
3399         rc = ntru_crypto_drbg_uninstantiate(drbg);
3400
3401         if (rc != NTRU_OK) {
3402             free(derCert);
3403             free(pem);
3404             return -451;
3405         }
3406
3407         caFile = fopen(caKeyFile, "rb");
3408
3409         if (!caFile) {
3410             free(derCert);
3411             free(pem);
3412             return -452;
3413         }
3414
3415         bytes = fread(tmp, 1, FOURK_BUF, caFile);
3416         fclose(caFile);
3417
3418         ret = InitRsaKey(&caKey, 0);
3419         if (ret != 0) {
3420             free(derCert);
3421             free(pem);
3422             return -453;
3423         }
3424         ret = RsaPrivateKeyDecode(tmp, &idx3, &caKey, (word32)bytes);
3425         if (ret != 0) {
3426             free(derCert);
3427             free(pem);
3428             return -454;
3429         }
3430
3431         InitCert(&myCert);
3432
3433         strncpy(myCert.subject.country, "US", CTC_NAME_SIZE);
3434         strncpy(myCert.subject.state, "OR", CTC_NAME_SIZE);
3435         strncpy(myCert.subject.locality, "Portland", CTC_NAME_SIZE);
3436         strncpy(myCert.subject.org, "yaSSL", CTC_NAME_SIZE);
3437         strncpy(myCert.subject.unit, "Development", CTC_NAME_SIZE);
3438         strncpy(myCert.subject.commonName, "www.yassl.com", CTC_NAME_SIZE);
3439         strncpy(myCert.subject.email, "info@yassl.com", CTC_NAME_SIZE);
3440
3441         ret = SetIssuer(&myCert, caCertFile);
3442         if (ret < 0) {
3443             free(derCert);
3444             free(pem);
3445             FreeRsaKey(&caKey);
3446             return -455;
3447         }
3448
3449         certSz = MakeNtruCert(&myCert, derCert, FOURK_BUF, public_key,
3450                               public_key_len, &rng);
3451         if (certSz < 0) {
3452             free(derCert);
3453             free(pem);
3454             FreeRsaKey(&caKey);
3455             return -456;
3456         }
3457
3458         certSz = SignCert(myCert.bodySz, myCert.sigType, derCert, FOURK_BUF,
3459                           &caKey, NULL, &rng);
3460         FreeRsaKey(&caKey);
3461         if (certSz < 0) {
3462             free(derCert);
3463             free(pem);
3464             return -457;
3465         }
3466
3467
3468 #ifdef CYASSL_TEST_CERT
3469         InitDecodedCert(&decode, derCert, certSz, 0);
3470         ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
3471         if (ret != 0) {
3472             free(derCert);
3473             free(pem);
3474             return -458;
3475         }
3476         FreeDecodedCert(&decode);
3477 #endif
3478         derFile = fopen("./ntru-cert.der", "wb");
3479         if (!derFile) {
3480             free(derCert);
3481             free(pem);
3482             return -459;
3483         }
3484         ret = (int)fwrite(derCert, 1, certSz, derFile);
3485         fclose(derFile);
3486         if (ret != certSz) {
3487             free(derCert);
3488             free(pem);
3489             return -473;
3490         }
3491
3492         pemSz = DerToPem(derCert, certSz, pem, FOURK_BUF, CERT_TYPE);
3493         if (pemSz < 0) {
3494             free(derCert);
3495             free(pem);
3496             return -460;
3497         }
3498
3499         pemFile = fopen("./ntru-cert.pem", "wb");
3500         if (!pemFile) {
3501             free(derCert);
3502             free(pem);
3503             return -461;
3504         }
3505         ret = (int)fwrite(pem, 1, pemSz, pemFile);
3506         fclose(pemFile);
3507         if (ret != pemSz) {
3508             free(derCert);
3509             free(pem);
3510             return -474;
3511         }
3512
3513         ntruPrivFile = fopen("./ntru-key.raw", "wb");
3514         if (!ntruPrivFile) {
3515             free(derCert);
3516             free(pem);
3517             return -462;
3518         }
3519         ret = (int)fwrite(private_key, 1, private_key_len, ntruPrivFile);
3520         fclose(ntruPrivFile);
3521         if (ret != private_key_len) {
3522             free(pem);
3523             free(derCert);
3524             return -475;
3525         }
3526         free(pem);
3527         free(derCert);
3528     }
3529 #endif /* HAVE_NTRU */
3530 #ifdef CYASSL_CERT_REQ
3531     {
3532         Cert        req;
3533         byte*       der;
3534         byte*       pem;
3535         int         derSz;
3536         int         pemSz;
3537         FILE*       reqFile;
3538
3539         der = (byte*)malloc(FOURK_BUF);
3540         if (der == NULL)
3541             return -463;
3542         pem = (byte*)malloc(FOURK_BUF);
3543         if (pem == NULL) {
3544             free(der);
3545             return -464;
3546         }
3547
3548         InitCert(&req);
3549
3550         req.version = 0;
3551         req.isCA    = 1;
3552         strncpy(req.challengePw, "yassl123", CTC_NAME_SIZE);
3553         strncpy(req.subject.country, "US", CTC_NAME_SIZE);
3554         strncpy(req.subject.state, "OR", CTC_NAME_SIZE);
3555         strncpy(req.subject.locality, "Portland", CTC_NAME_SIZE);
3556         strncpy(req.subject.org, "yaSSL", CTC_NAME_SIZE);
3557         strncpy(req.subject.unit, "Development", CTC_NAME_SIZE);
3558         strncpy(req.subject.commonName, "www.yassl.com", CTC_NAME_SIZE);
3559         strncpy(req.subject.email, "info@yassl.com", CTC_NAME_SIZE);
3560         req.sigType = CTC_SHA256wRSA;
3561
3562         derSz = MakeCertReq(&req, der, FOURK_BUF, &key, NULL);
3563         if (derSz < 0) {
3564             free(pem);
3565             free(der);
3566             return -465;
3567         }
3568
3569         derSz = SignCert(req.bodySz, req.sigType, der, FOURK_BUF,
3570                           &key, NULL, &rng);
3571         if (derSz < 0) {
3572             free(pem);
3573             free(der);
3574             return -466;
3575         }
3576
3577         pemSz = DerToPem(der, derSz, pem, FOURK_BUF, CERTREQ_TYPE);
3578         if (pemSz < 0) {
3579             free(pem);
3580             free(der);
3581             return -467;
3582         }
3583
3584         reqFile = fopen("./certreq.der", "wb");
3585         if (!reqFile) {
3586             free(pem);
3587             free(der);
3588             return -468;
3589         }
3590
3591         ret = (int)fwrite(der, 1, derSz, reqFile);
3592         fclose(reqFile);
3593         if (ret != derSz) {
3594             free(pem);
3595             free(der);
3596             return -471;
3597         }
3598
3599         reqFile = fopen("./certreq.pem", "wb");
3600         if (!reqFile) {
3601             free(pem);
3602             free(der);
3603             return -469;
3604         }
3605         ret = (int)fwrite(pem, 1, pemSz, reqFile);
3606         fclose(reqFile);
3607         if (ret != pemSz) {
3608             free(pem);
3609             free(der);
3610             return -470;
3611         }
3612
3613         free(pem);
3614         free(der);
3615     }
3616 #endif /* CYASSL_CERT_REQ */
3617 #endif /* CYASSL_CERT_GEN */
3618
3619     FreeRsaKey(&key);
3620 #ifdef HAVE_CAVIUM
3621     RsaFreeCavium(&key);
3622 #endif
3623     free(tmp);
3624
3625     return 0;
3626 }
3627
3628 #endif
3629
3630
3631 #ifndef NO_DH
3632
3633 #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
3634     #ifdef FREESCALE_MQX
3635         static const char* dhKey = "a:\certs\\dh2048.der";
3636     #else
3637         static const char* dhKey = "./certs/dh2048.der";
3638     #endif
3639 #endif
3640
3641 int dh_test(void)
3642 {
3643     int    ret;
3644     word32 bytes;
3645     word32 idx = 0, privSz, pubSz, privSz2, pubSz2, agreeSz, agreeSz2;
3646     byte   tmp[1024];
3647     byte   priv[256];
3648     byte   pub[256];
3649     byte   priv2[256];
3650     byte   pub2[256];
3651     byte   agree[256];
3652     byte   agree2[256];
3653     DhKey  key;
3654     DhKey  key2;
3655     RNG    rng;
3656
3657
3658 #ifdef USE_CERT_BUFFERS_1024
3659     XMEMCPY(tmp, dh_key_der_1024, sizeof_dh_key_der_1024);
3660     bytes = sizeof_dh_key_der_1024;
3661 #elif defined(USE_CERT_BUFFERS_2048)
3662     XMEMCPY(tmp, dh_key_der_2048, sizeof_dh_key_der_2048);
3663     bytes = sizeof_dh_key_der_2048;
3664 #else
3665     FILE*  file = fopen(dhKey, "rb");
3666
3667     if (!file)
3668         return -50;
3669
3670     bytes = (word32) fread(tmp, 1, sizeof(tmp), file);
3671     fclose(file);
3672 #endif /* USE_CERT_BUFFERS */
3673
3674     InitDhKey(&key);
3675     InitDhKey(&key2);
3676     ret = DhKeyDecode(tmp, &idx, &key, bytes);
3677     if (ret != 0)
3678         return -51;
3679
3680     idx = 0;
3681     ret = DhKeyDecode(tmp, &idx, &key2, bytes);
3682     if (ret != 0)
3683         return -52;
3684
3685     ret = InitRng(&rng);
3686     if (ret != 0)
3687         return -53;
3688
3689     ret =  DhGenerateKeyPair(&key, &rng, priv, &privSz, pub, &pubSz);
3690     ret += DhGenerateKeyPair(&key2, &rng, priv2, &privSz2, pub2, &pubSz2);
3691     if (ret != 0)
3692         return -54;
3693
3694     ret =  DhAgree(&key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
3695     ret += DhAgree(&key2, agree2, &agreeSz2, priv2, privSz2, pub, pubSz);
3696     if (ret != 0)
3697         return -55;
3698
3699     if (memcmp(agree, agree2, agreeSz))
3700         return -56;
3701
3702     FreeDhKey(&key);
3703     FreeDhKey(&key2);
3704
3705     return 0;
3706 }
3707
3708 #endif /* NO_DH */
3709
3710
3711 #ifndef NO_DSA
3712
3713 #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
3714     #ifdef FREESCALE_MQX
3715         static const char* dsaKey = "a:\\certs\\dsa2048.der";
3716     #else
3717         static const char* dsaKey = "./certs/dsa2048.der";
3718     #endif
3719 #endif
3720
3721 int dsa_test(void)
3722 {
3723     int    ret, answer;
3724     word32 bytes;
3725     word32 idx = 0;
3726     byte   tmp[1024];
3727     DsaKey key;
3728     RNG    rng;
3729     Sha    sha;
3730     byte   hash[SHA_DIGEST_SIZE];
3731     byte   signature[40];
3732
3733
3734 #ifdef USE_CERT_BUFFERS_1024
3735     XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
3736     bytes = sizeof_dsa_key_der_1024;
3737 #elif defined(USE_CERT_BUFFERS_2048)
3738     XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
3739     bytes = sizeof_dsa_key_der_2048;
3740 #else
3741     FILE*  file = fopen(dsaKey, "rb");
3742
3743     if (!file)
3744         return -60;
3745
3746     bytes = (word32) fread(tmp, 1, sizeof(tmp), file);
3747     fclose(file);
3748 #endif /* USE_CERT_BUFFERS */
3749
3750     ret = InitSha(&sha);
3751     if (ret != 0)
3752         return -4002;
3753     ShaUpdate(&sha, tmp, bytes);
3754     ShaFinal(&sha, hash);
3755
3756     InitDsaKey(&key);
3757     ret = DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
3758     if (ret != 0) return -61;
3759
3760     ret = InitRng(&rng);
3761     if (ret != 0) return -62;
3762
3763     ret = DsaSign(hash, signature, &key, &rng);
3764     if (ret != 0) return -63;
3765
3766     ret = DsaVerify(hash, signature, &key, &answer);
3767     if (ret != 0) return -64;
3768     if (answer != 1) return -65;
3769
3770     FreeDsaKey(&key);
3771
3772     return 0;
3773 }
3774
3775 #endif /* NO_DSA */
3776
3777
3778 #ifdef OPENSSL_EXTRA
3779
3780 int openssl_test(void)
3781 {
3782     EVP_MD_CTX md_ctx;
3783     testVector a, b, c, d, e, f;
3784     byte       hash[SHA_DIGEST_SIZE*4];  /* max size */
3785
3786     (void)e;
3787     (void)f;
3788
3789     a.input  = "1234567890123456789012345678901234567890123456789012345678"
3790                "9012345678901234567890";
3791     a.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
3792                "\x7a";
3793     a.inLen  = strlen(a.input);
3794     a.outLen = MD5_DIGEST_SIZE;
3795
3796     EVP_MD_CTX_init(&md_ctx);
3797     EVP_DigestInit(&md_ctx, EVP_md5());
3798
3799     EVP_DigestUpdate(&md_ctx, a.input, (unsigned long)a.inLen);
3800     EVP_DigestFinal(&md_ctx, hash, 0);
3801
3802     if (memcmp(hash, a.output, MD5_DIGEST_SIZE) != 0)
3803         return -71;
3804
3805     b.input  = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
3806                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
3807                "aaaaaaaaaa";
3808     b.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
3809                "\x53\x99\x5E\x26\xA0";
3810     b.inLen  = strlen(b.input);
3811     b.outLen = SHA_DIGEST_SIZE;
3812
3813     EVP_MD_CTX_init(&md_ctx);
3814     EVP_DigestInit(&md_ctx, EVP_sha1());
3815
3816     EVP_DigestUpdate(&md_ctx, b.input, (unsigned long)b.inLen);
3817     EVP_DigestFinal(&md_ctx, hash, 0);
3818
3819     if (memcmp(hash, b.output, SHA_DIGEST_SIZE) != 0)
3820         return -72;
3821
3822
3823     d.input  = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
3824     d.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
3825                "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
3826                "\x06\xC1";
3827     d.inLen  = strlen(d.input);
3828     d.outLen = SHA256_DIGEST_SIZE;
3829
3830     EVP_MD_CTX_init(&md_ctx);
3831     EVP_DigestInit(&md_ctx, EVP_sha256());
3832
3833     EVP_DigestUpdate(&md_ctx, d.input, (unsigned long)d.inLen);
3834     EVP_DigestFinal(&md_ctx, hash, 0);
3835
3836     if (memcmp(hash, d.output, SHA256_DIGEST_SIZE) != 0)
3837         return -78;
3838
3839 #ifdef CYASSL_SHA384
3840
3841     e.input  = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
3842                "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
3843     e.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
3844                "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
3845                "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
3846                "\x74\x60\x39";
3847     e.inLen  = strlen(e.input);
3848     e.outLen = SHA384_DIGEST_SIZE;
3849
3850     EVP_MD_CTX_init(&md_ctx);
3851     EVP_DigestInit(&md_ctx, EVP_sha384());
3852
3853     EVP_DigestUpdate(&md_ctx, e.input, e.inLen);
3854     EVP_DigestFinal(&md_ctx, hash, 0);
3855
3856     if (memcmp(hash, e.output, SHA384_DIGEST_SIZE) != 0)
3857         return -79;
3858
3859 #endif /* CYASSL_SHA384 */
3860
3861
3862 #ifdef CYASSL_SHA512
3863
3864     f.input  = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
3865                "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
3866     f.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
3867                "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
3868                "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
3869                "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
3870                "\x87\x4b\xe9\x09";
3871     f.inLen  = strlen(f.input);
3872     f.outLen = SHA512_DIGEST_SIZE;
3873
3874     EVP_MD_CTX_init(&md_ctx);
3875     EVP_DigestInit(&md_ctx, EVP_sha512());
3876
3877     EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
3878     EVP_DigestFinal(&md_ctx, hash, 0);
3879
3880     if (memcmp(hash, f.output, SHA512_DIGEST_SIZE) != 0)
3881         return -80;
3882
3883 #endif /* CYASSL_SHA512 */
3884
3885
3886     if (RAND_bytes(hash, sizeof(hash)) != 1)
3887         return -73;
3888
3889     c.input  = "what do ya want for nothing?";
3890     c.output = "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7"
3891                "\x38";
3892     c.inLen  = strlen(c.input);
3893     c.outLen = MD5_DIGEST_SIZE;
3894
3895     HMAC(EVP_md5(), "Jefe", 4, (byte*)c.input, (int)c.inLen, hash, 0);
3896
3897     if (memcmp(hash, c.output, MD5_DIGEST_SIZE) != 0)
3898         return -74;
3899
3900     { /* des test */
3901     const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
3902         0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
3903         0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
3904         0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
3905     };
3906
3907     byte plain[24];
3908     byte cipher[24];
3909
3910     const_DES_cblock key =
3911     {
3912         0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
3913     };
3914
3915     DES_cblock iv =
3916     {
3917         0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
3918     };
3919
3920     DES_key_schedule sched;
3921
3922     const byte verify[] =
3923     {
3924         0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
3925         0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
3926         0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
3927     };
3928
3929     DES_key_sched(&key, &sched);
3930
3931     DES_cbc_encrypt(vector, cipher, sizeof(vector), &sched, &iv, DES_ENCRYPT);
3932     DES_cbc_encrypt(cipher, plain, sizeof(vector), &sched, &iv, DES_DECRYPT);
3933
3934     if (memcmp(plain, vector, sizeof(vector)) != 0)
3935         return -75;
3936
3937     if (memcmp(cipher, verify, sizeof(verify)) != 0)
3938         return -76;
3939
3940         /* test changing iv */
3941     DES_ncbc_encrypt(vector, cipher, 8, &sched, &iv, DES_ENCRYPT);
3942     DES_ncbc_encrypt(vector + 8, cipher + 8, 16, &sched, &iv, DES_ENCRYPT);
3943
3944     if (memcmp(cipher, verify, sizeof(verify)) != 0)
3945         return -77;
3946
3947     }  /* end des test */
3948
3949     {  /* evp_cipher test */
3950         EVP_CIPHER_CTX ctx;
3951
3952
3953         const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
3954             0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
3955             0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
3956             0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
3957         };
3958
3959         const byte verify[] =
3960         {
3961             0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
3962             0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
3963         };
3964
3965         byte key[] = "0123456789abcdef   ";  /* align */
3966         byte iv[]  = "1234567890abcdef   ";  /* align */
3967
3968         byte cipher[AES_BLOCK_SIZE * 4];
3969         byte plain [AES_BLOCK_SIZE * 4];
3970
3971         EVP_CIPHER_CTX_init(&ctx);
3972         if (EVP_CipherInit(&ctx, EVP_aes_128_cbc(), key, iv, 1) == 0)
3973             return -81;
3974
3975         if (EVP_Cipher(&ctx, cipher, (byte*)msg, 16) == 0)
3976             return -82;
3977
3978         if (memcmp(cipher, verify, AES_BLOCK_SIZE))
3979             return -83;
3980
3981         EVP_CIPHER_CTX_init(&ctx);
3982         if (EVP_CipherInit(&ctx, EVP_aes_128_cbc(), key, iv, 0) == 0)
3983             return -84;
3984
3985         if (EVP_Cipher(&ctx, plain, cipher, 16) == 0)
3986             return -85;
3987
3988         if (memcmp(plain, msg, AES_BLOCK_SIZE))
3989             return -86;
3990
3991
3992     }  /* end evp_cipher test */
3993
3994     return 0;
3995 }
3996
3997 #endif /* OPENSSL_EXTRA */
3998
3999
4000 #ifndef NO_PWDBASED
4001
4002 int pkcs12_test(void)
4003 {
4004     const byte passwd[] = { 0x00, 0x73, 0x00, 0x6d, 0x00, 0x65, 0x00, 0x67,
4005                             0x00, 0x00 };
4006     const byte salt[] =   { 0x0a, 0x58, 0xCF, 0x64, 0x53, 0x0d, 0x82, 0x3f };
4007
4008     const byte passwd2[] = { 0x00, 0x71, 0x00, 0x75, 0x00, 0x65, 0x00, 0x65,
4009                              0x00, 0x67, 0x00, 0x00 };
4010     const byte salt2[] =   { 0x16, 0x82, 0xC0, 0xfC, 0x5b, 0x3f, 0x7e, 0xc5 };
4011     byte  derived[64];
4012
4013     const byte verify[] = {
4014         0x8A, 0xAA, 0xE6, 0x29, 0x7B, 0x6C, 0xB0, 0x46,
4015         0x42, 0xAB, 0x5B, 0x07, 0x78, 0x51, 0x28, 0x4E,
4016         0xB7, 0x12, 0x8F, 0x1A, 0x2A, 0x7F, 0xBC, 0xA3
4017     };
4018
4019     const byte verify2[] = {
4020         0x48, 0x3D, 0xD6, 0xE9, 0x19, 0xD7, 0xDE, 0x2E,
4021         0x8E, 0x64, 0x8B, 0xA8, 0xF8, 0x62, 0xF3, 0xFB,
4022         0xFB, 0xDC, 0x2B, 0xCB, 0x2C, 0x02, 0x95, 0x7F
4023     };
4024
4025     int id         =  1;
4026     int kLen       = 24;
4027     int iterations =  1;
4028     int ret = PKCS12_PBKDF(derived, passwd, sizeof(passwd), salt, 8, iterations,
4029                            kLen, SHA, id);
4030
4031     if (ret < 0)
4032         return -103;
4033
4034     if ( (ret = memcmp(derived, verify, kLen)) != 0)
4035         return -104;
4036
4037     iterations = 1000;
4038     ret = PKCS12_PBKDF(derived, passwd2, sizeof(passwd2), salt2, 8, iterations,
4039                        kLen, SHA, id);
4040     if (ret < 0)
4041         return -105;
4042
4043     if ( (ret = memcmp(derived, verify2, 24)) != 0)
4044         return -106;
4045
4046     return 0;
4047 }
4048
4049
4050 int pbkdf2_test(void)
4051 {
4052     char passwd[] = "password";
4053     const byte salt[] = { 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06 };
4054     int   iterations = 2048;
4055     int   kLen = 24;
4056     byte  derived[64];
4057
4058     const byte verify[] = {
4059         0xBF, 0xDE, 0x6B, 0xE9, 0x4D, 0xF7, 0xE1, 0x1D, 0xD4, 0x09, 0xBC, 0xE2,
4060         0x0A, 0x02, 0x55, 0xEC, 0x32, 0x7C, 0xB9, 0x36, 0xFF, 0xE9, 0x36, 0x43
4061
4062     };
4063
4064     int ret = PBKDF2(derived, (byte*)passwd, (int)strlen(passwd), salt, 8,
4065                                                          iterations, kLen, SHA);
4066     if (ret != 0)
4067         return ret;
4068
4069     if (memcmp(derived, verify, sizeof(verify)) != 0)
4070         return -102;
4071
4072     return 0;
4073 }
4074
4075
4076 int pbkdf1_test(void)
4077 {
4078     char passwd[] = "password";
4079     const byte salt[] = { 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06 };
4080     int   iterations = 1000;
4081     int   kLen = 16;
4082     byte  derived[16];
4083
4084     const byte verify[] = {
4085         0xDC, 0x19, 0x84, 0x7E, 0x05, 0xC6, 0x4D, 0x2F, 0xAF, 0x10, 0xEB, 0xFB,
4086         0x4A, 0x3D, 0x2A, 0x20
4087     };
4088
4089     PBKDF1(derived, (byte*)passwd, (int)strlen(passwd), salt, 8, iterations,
4090            kLen, SHA);
4091
4092     if (memcmp(derived, verify, sizeof(verify)) != 0)
4093         return -101;
4094
4095     return 0;
4096 }
4097
4098
4099 int pwdbased_test(void)
4100 {
4101    int ret =  pbkdf1_test();
4102    ret += pbkdf2_test();
4103
4104    return ret + pkcs12_test();
4105 }
4106
4107 #endif /* NO_PWDBASED */
4108
4109 #if defined(HAVE_HKDF) && (!defined(NO_SHA) || !defined(NO_SHA256))
4110
4111 int hkdf_test(void)
4112 {
4113     int ret;
4114     int L = 42;
4115     byte okm1[42];
4116     byte ikm1[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4117                       0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4118                       0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
4119     byte salt1[13] ={ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
4120                       0x08, 0x09, 0x0a, 0x0b, 0x0c };
4121     byte info1[10] ={ 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
4122                       0xf8, 0xf9 };
4123     byte res1[42] = { 0x0a, 0xc1, 0xaf, 0x70, 0x02, 0xb3, 0xd7, 0x61,
4124                       0xd1, 0xe5, 0x52, 0x98, 0xda, 0x9d, 0x05, 0x06,
4125                       0xb9, 0xae, 0x52, 0x05, 0x72, 0x20, 0xa3, 0x06,
4126                       0xe0, 0x7b, 0x6b, 0x87, 0xe8, 0xdf, 0x21, 0xd0,
4127                       0xea, 0x00, 0x03, 0x3d, 0xe0, 0x39, 0x84, 0xd3,
4128                       0x49, 0x18 };
4129     byte res2[42] = { 0x08, 0x5a, 0x01, 0xea, 0x1b, 0x10, 0xf3, 0x69,
4130                       0x33, 0x06, 0x8b, 0x56, 0xef, 0xa5, 0xad, 0x81,
4131                       0xa4, 0xf1, 0x4b, 0x82, 0x2f, 0x5b, 0x09, 0x15,
4132                       0x68, 0xa9, 0xcd, 0xd4, 0xf1, 0x55, 0xfd, 0xa2,
4133                       0xc2, 0x2e, 0x42, 0x24, 0x78, 0xd3, 0x05, 0xf3,
4134                       0xf8, 0x96 };
4135     byte res3[42] = { 0x8d, 0xa4, 0xe7, 0x75, 0xa5, 0x63, 0xc1, 0x8f,
4136                       0x71, 0x5f, 0x80, 0x2a, 0x06, 0x3c, 0x5a, 0x31,
4137                       0xb8, 0xa1, 0x1f, 0x5c, 0x5e, 0xe1, 0x87, 0x9e,
4138                       0xc3, 0x45, 0x4e, 0x5f, 0x3c, 0x73, 0x8d, 0x2d,
4139                       0x9d, 0x20, 0x13, 0x95, 0xfa, 0xa4, 0xb6, 0x1a,
4140                       0x96, 0xc8 };
4141     byte res4[42] = { 0x3c, 0xb2, 0x5f, 0x25, 0xfa, 0xac, 0xd5, 0x7a,
4142                       0x90, 0x43, 0x4f, 0x64, 0xd0, 0x36, 0x2f, 0x2a,
4143                       0x2d, 0x2d, 0x0a, 0x90, 0xcf, 0x1a, 0x5a, 0x4c,
4144                       0x5d, 0xb0, 0x2d, 0x56, 0xec, 0xc4, 0xc5, 0xbf,
4145                       0x34, 0x00, 0x72, 0x08, 0xd5, 0xb8, 0x87, 0x18,
4146                       0x58, 0x65 };
4147
4148     (void)res1;
4149     (void)res2;
4150     (void)res3;
4151     (void)res4;
4152     (void)salt1;
4153     (void)info1;
4154
4155 #ifndef NO_SHA
4156     ret = HKDF(SHA, ikm1, 22, NULL, 0, NULL, 0, okm1, L);
4157     if (ret != 0)
4158         return -2001;
4159
4160     if (memcmp(okm1, res1, L) != 0)
4161         return -2002;
4162
4163 #ifndef HAVE_FIPS
4164     /* fips can't have key size under 14 bytes, salt is key too */
4165     ret = HKDF(SHA, ikm1, 11, salt1, 13, info1, 10, okm1, L);
4166     if (ret != 0)
4167         return -2003;
4168
4169     if (memcmp(okm1, res2, L) != 0)
4170         return -2004;
4171 #endif /* HAVE_FIPS */
4172 #endif /* NO_SHA */
4173
4174 #ifndef NO_SHA256
4175     ret = HKDF(SHA256, ikm1, 22, NULL, 0, NULL, 0, okm1, L);
4176     if (ret != 0)
4177         return -2005;
4178
4179     if (memcmp(okm1, res3, L) != 0)
4180         return -2006;
4181
4182 #ifndef HAVE_FIPS
4183     /* fips can't have key size under 14 bytes, salt is key too */
4184     ret = HKDF(SHA256, ikm1, 22, salt1, 13, info1, 10, okm1, L);
4185     if (ret != 0)
4186         return -2007;
4187
4188     if (memcmp(okm1, res4, L) != 0)
4189         return -2007;
4190 #endif /* HAVE_FIPS */
4191 #endif /* NO_SHA256 */
4192
4193     return 0;
4194 }
4195
4196 #endif /* HAVE_HKDF */
4197
4198
4199 #ifdef HAVE_ECC
4200
4201 int ecc_test(void)
4202 {
4203     RNG     rng;
4204     byte    sharedA[1024];
4205     byte    sharedB[1024];
4206     byte    sig[1024];
4207     byte    digest[20];
4208     byte    exportBuf[1024];
4209     word32  x, y;
4210     int     i, verify, ret;
4211     ecc_key userA, userB, pubKey;
4212
4213     ret = InitRng(&rng);
4214     if (ret != 0)
4215         return -1001;
4216
4217     ecc_init(&userA);
4218     ecc_init(&userB);
4219     ecc_init(&pubKey);
4220
4221     ret = ecc_make_key(&rng, 32, &userA);
4222
4223     if (ret != 0)
4224         return -1014;
4225
4226     ret = ecc_make_key(&rng, 32, &userB);
4227
4228     if (ret != 0)
4229         return -1002;
4230
4231     x = sizeof(sharedA);
4232     ret = ecc_shared_secret(&userA, &userB, sharedA, &x);
4233
4234     if (ret != 0)
4235         return -1015;
4236
4237     y = sizeof(sharedB);
4238     ret = ecc_shared_secret(&userB, &userA, sharedB, &y);
4239
4240     if (ret != 0)
4241         return -1003;
4242
4243     if (y != x)
4244         return -1004;
4245
4246     if (memcmp(sharedA, sharedB, x))
4247         return -1005;
4248
4249     x = sizeof(exportBuf);
4250     ret = ecc_export_x963(&userA, exportBuf, &x);
4251     if (ret != 0)
4252         return -1006;
4253
4254     ret = ecc_import_x963(exportBuf, x, &pubKey);
4255
4256     if (ret != 0)
4257         return -1007;
4258
4259     y = sizeof(sharedB);
4260     ret = ecc_shared_secret(&userB, &pubKey, sharedB, &y);
4261
4262     if (ret != 0)
4263         return -1008;
4264
4265     if (memcmp(sharedA, sharedB, y))
4266         return -1010;
4267
4268     /* test DSA sign hash */
4269     for (i = 0; i < (int)sizeof(digest); i++)
4270         digest[i] = (byte)i;
4271
4272     x = sizeof(sig);
4273     ret = ecc_sign_hash(digest, sizeof(digest), sig, &x, &rng, &userA);
4274
4275     if (ret != 0)
4276         return -1016;
4277
4278     verify = 0;
4279     ret = ecc_verify_hash(sig, x, digest, sizeof(digest), &verify, &userA);
4280
4281     if (ret != 0)
4282         return -1011;
4283
4284     if (verify != 1)
4285         return -1012;
4286
4287     x = sizeof(exportBuf);
4288     ret = ecc_export_private_only(&userA, exportBuf, &x);
4289     if (ret != 0)
4290         return -1013;
4291
4292     ecc_free(&pubKey);
4293     ecc_free(&userB);
4294     ecc_free(&userA);
4295
4296     return 0;
4297 }
4298
4299 #ifdef HAVE_ECC_ENCRYPT
4300
4301 int ecc_encrypt_test(void)
4302 {
4303     RNG     rng;
4304     int     ret;
4305     ecc_key userA, userB;
4306     byte    msg[48];
4307     byte    plain[48];
4308     byte    out[80];
4309     word32  outSz   = sizeof(out);
4310     word32  plainSz = sizeof(plain);
4311     int     i;
4312
4313     ret = InitRng(&rng);
4314     if (ret != 0)
4315         return -3001;
4316
4317     ecc_init(&userA);
4318     ecc_init(&userB);
4319
4320     ret  = ecc_make_key(&rng, 32, &userA);
4321     ret += ecc_make_key(&rng, 32, &userB);
4322
4323     if (ret != 0)
4324         return -3002;
4325
4326     for (i = 0; i < 48; i++)
4327         msg[i] = i;
4328
4329     /* encrypt msg to B */
4330     ret = ecc_encrypt(&userA, &userB, msg, sizeof(msg), out, &outSz, NULL);
4331     if (ret != 0)
4332         return -3003;
4333
4334     /* decrypt msg from A */
4335     ret = ecc_decrypt(&userB, &userA, out, outSz, plain, &plainSz, NULL);
4336     if (ret != 0)
4337         return -3004;
4338
4339     if (memcmp(plain, msg, sizeof(msg)) != 0)
4340         return -3005;
4341
4342
4343     {  /* let's verify message exchange works, A is client, B is server */
4344         ecEncCtx* cliCtx = ecc_ctx_new(REQ_RESP_CLIENT, &rng);
4345         ecEncCtx* srvCtx = ecc_ctx_new(REQ_RESP_SERVER, &rng);
4346
4347         byte cliSalt[EXCHANGE_SALT_SZ];
4348         byte srvSalt[EXCHANGE_SALT_SZ];
4349         const byte* tmpSalt;
4350
4351         if (cliCtx == NULL || srvCtx == NULL)
4352             return -3006;
4353
4354         /* get salt to send to peer */
4355         tmpSalt = ecc_ctx_get_own_salt(cliCtx);
4356         if (tmpSalt == NULL)
4357             return -3007;
4358         memcpy(cliSalt, tmpSalt, EXCHANGE_SALT_SZ);
4359
4360         tmpSalt = ecc_ctx_get_own_salt(srvCtx);
4361         if (tmpSalt == NULL)
4362             return -3007;
4363         memcpy(srvSalt, tmpSalt, EXCHANGE_SALT_SZ);
4364
4365         /* in actual use, we'd get the peer's salt over the transport */
4366         ret  = ecc_ctx_set_peer_salt(cliCtx, srvSalt);
4367         ret += ecc_ctx_set_peer_salt(srvCtx, cliSalt);
4368
4369         ret += ecc_ctx_set_info(cliCtx, (byte*)"CyaSSL MSGE", 11);
4370         ret += ecc_ctx_set_info(srvCtx, (byte*)"CyaSSL MSGE", 11);
4371
4372         if (ret != 0)
4373             return -3008;
4374
4375         /* get encrypted msg (request) to send to B */
4376         outSz  = sizeof(out);
4377         ret = ecc_encrypt(&userA, &userB, msg, sizeof(msg), out, &outSz,cliCtx);
4378         if (ret != 0)
4379             return -3009;
4380
4381         /* B decrypts msg (request) from A */
4382         plainSz = sizeof(plain);
4383         ret = ecc_decrypt(&userB, &userA, out, outSz, plain, &plainSz, srvCtx);
4384         if (ret != 0)
4385             return -3010;
4386
4387         if (memcmp(plain, msg, sizeof(msg)) != 0)
4388             return -3011;
4389
4390         {
4391             /* msg2 (response) from B to A */
4392             byte    msg2[48];
4393             byte    plain2[48];
4394             byte    out2[80];
4395             word32  outSz2   = sizeof(out2);
4396             word32  plainSz2 = sizeof(plain2);
4397
4398             for (i = 0; i < 48; i++)
4399                 msg2[i] = i+48;
4400
4401             /* get encrypted msg (response) to send to B */
4402             ret = ecc_encrypt(&userB, &userA, msg2, sizeof(msg2), out2,
4403                               &outSz2, srvCtx);
4404             if (ret != 0)
4405                 return -3012;
4406
4407             /* A decrypts msg (response) from B */
4408             ret = ecc_decrypt(&userA, &userB, out2, outSz2, plain2, &plainSz2,
4409                              cliCtx);
4410             if (ret != 0)
4411                 return -3013;
4412
4413             if (memcmp(plain2, msg2, sizeof(msg2)) != 0)
4414                 return -3014;
4415         }
4416
4417         /* cleanup */
4418         ecc_ctx_free(srvCtx);
4419         ecc_ctx_free(cliCtx);
4420     }
4421
4422     /* cleanup */
4423     ecc_free(&userB);
4424     ecc_free(&userA);
4425
4426     return 0;
4427 }
4428
4429 #endif /* HAVE_ECC_ENCRYPT */
4430 #endif /* HAVE_ECC */
4431
4432 #ifdef HAVE_LIBZ
4433
4434 const byte sample_text[] =
4435     "Biodiesel cupidatat marfa, cliche aute put a bird on it incididunt elit\n"
4436     "polaroid. Sunt tattooed bespoke reprehenderit. Sint twee organic id\n"
4437     "marfa. Commodo veniam ad esse gastropub. 3 wolf moon sartorial vero,\n"
4438     "plaid delectus biodiesel squid +1 vice. Post-ironic keffiyeh leggings\n"
4439     "selfies cray fap hoodie, forage anim. Carles cupidatat shoreditch, VHS\n"
4440     "small batch meggings kogi dolore food truck bespoke gastropub.\n"
4441     "\n"
4442     "Terry richardson adipisicing actually typewriter tumblr, twee whatever\n"
4443     "four loko you probably haven't heard of them high life. Messenger bag\n"
4444     "whatever tattooed deep v mlkshk. Brooklyn pinterest assumenda chillwave\n"
4445     "et, banksy ullamco messenger bag umami pariatur direct trade forage.\n"
4446     "Typewriter culpa try-hard, pariatur sint brooklyn meggings. Gentrify\n"
4447     "food truck next level, tousled irony non semiotics PBR ethical anim cred\n"
4448     "readymade. Mumblecore brunch lomo odd future, portland organic terry\n"
4449     "richardson elit leggings adipisicing ennui raw denim banjo hella. Godard\n"
4450     "mixtape polaroid, pork belly readymade organic cray typewriter helvetica\n"
4451     "four loko whatever street art yr farm-to-table.\n"
4452     "\n"
4453     "Vinyl keytar vice tofu. Locavore you probably haven't heard of them pug\n"
4454     "pickled, hella tonx labore truffaut DIY mlkshk elit cosby sweater sint\n"
4455     "et mumblecore. Elit swag semiotics, reprehenderit DIY sartorial nisi ugh\n"
4456     "nesciunt pug pork belly wayfarers selfies delectus. Ethical hoodie\n"
4457     "seitan fingerstache kale chips. Terry richardson artisan williamsburg,\n"
4458     "eiusmod fanny pack irony tonx ennui lo-fi incididunt tofu YOLO\n"
4459     "readymade. 8-bit sed ethnic beard officia. Pour-over iphone DIY butcher,\n"
4460     "ethnic art party qui letterpress nisi proident jean shorts mlkshk\n"
4461     "locavore.\n"
4462     "\n"
4463     "Narwhal flexitarian letterpress, do gluten-free voluptate next level\n"
4464     "banh mi tonx incididunt carles DIY. Odd future nulla 8-bit beard ut\n"
4465     "cillum pickled velit, YOLO officia you probably haven't heard of them\n"
4466     "trust fund gastropub. Nisi adipisicing tattooed, Austin mlkshk 90's\n"
4467     "small batch american apparel. Put a bird on it cosby sweater before they\n"
4468     "sold out pork belly kogi hella. Street art mollit sustainable polaroid,\n"
4469     "DIY ethnic ea pug beard dreamcatcher cosby sweater magna scenester nisi.\n"
4470     "Sed pork belly skateboard mollit, labore proident eiusmod. Sriracha\n"
4471     "excepteur cosby sweater, anim deserunt laborum eu aliquip ethical et\n"
4472     "neutra PBR selvage.\n"
4473     "\n"
4474     "Raw denim pork belly truffaut, irony plaid sustainable put a bird on it\n"
4475     "next level jean shorts exercitation. Hashtag keytar whatever, nihil\n"
4476     "authentic aliquip disrupt laborum. Tattooed selfies deserunt trust fund\n"
4477     "wayfarers. 3 wolf moon synth church-key sartorial, gastropub leggings\n"
4478     "tattooed. Labore high life commodo, meggings raw denim fingerstache pug\n"
4479     "trust fund leggings seitan forage. Nostrud ullamco duis, reprehenderit\n"
4480     "incididunt flannel sustainable helvetica pork belly pug banksy you\n"
4481     "probably haven't heard of them nesciunt farm-to-table. Disrupt nostrud\n"
4482     "mollit magna, sriracha sartorial helvetica.\n"
4483     "\n"
4484     "Nulla kogi reprehenderit, skateboard sustainable duis adipisicing viral\n"
4485     "ad fanny pack salvia. Fanny pack trust fund you probably haven't heard\n"
4486     "of them YOLO vice nihil. Keffiyeh cray lo-fi pinterest cardigan aliqua,\n"
4487     "reprehenderit aute. Culpa tousled williamsburg, marfa lomo actually anim\n"
4488     "skateboard. Iphone aliqua ugh, semiotics pariatur vero readymade\n"
4489     "organic. Marfa squid nulla, in laborum disrupt laboris irure gastropub.\n"
4490     "Veniam sunt food truck leggings, sint vinyl fap.\n"
4491     "\n"
4492     "Hella dolore pork belly, truffaut carles you probably haven't heard of\n"
4493     "them PBR helvetica in sapiente. Fashion axe ugh bushwick american\n"
4494     "apparel. Fingerstache sed iphone, jean shorts blue bottle nisi bushwick\n"
4495     "flexitarian officia veniam plaid bespoke fap YOLO lo-fi. Blog\n"
4496     "letterpress mumblecore, food truck id cray brooklyn cillum ad sed.\n"
4497     "Assumenda chambray wayfarers vinyl mixtape sustainable. VHS vinyl\n"
4498     "delectus, culpa williamsburg polaroid cliche swag church-key synth kogi\n"
4499     "magna pop-up literally. Swag thundercats ennui shoreditch vegan\n"
4500     "pitchfork neutra truffaut etsy, sed single-origin coffee craft beer.\n"
4501     "\n"
4502     "Odio letterpress brooklyn elit. Nulla single-origin coffee in occaecat\n"
4503     "meggings. Irony meggings 8-bit, chillwave lo-fi adipisicing cred\n"
4504     "dreamcatcher veniam. Put a bird on it irony umami, trust fund bushwick\n"
4505     "locavore kale chips. Sriracha swag thundercats, chillwave disrupt\n"
4506     "tousled beard mollit mustache leggings portland next level. Nihil esse\n"
4507     "est, skateboard art party etsy thundercats sed dreamcatcher ut iphone\n"
4508     "swag consectetur et. Irure skateboard banjo, nulla deserunt messenger\n"
4509     "bag dolor terry richardson sapiente.\n";
4510
4511
4512 int compress_test(void)
4513 {
4514     int ret = 0;
4515     word32 dSz = sizeof(sample_text);
4516     word32 cSz = (dSz + (word32)(dSz * 0.001) + 12);
4517     byte *c = NULL;
4518     byte *d = NULL;
4519
4520     c = calloc(cSz, sizeof(byte));
4521     d = calloc(dSz, sizeof(byte));
4522
4523     if (c == NULL || d == NULL)
4524         ret = -300;
4525
4526     if (ret == 0 && (ret = Compress(c, cSz, sample_text, dSz, 0)) < 0)
4527         ret = -301;
4528
4529     if (ret > 0) {
4530         cSz = (word32)ret;
4531         ret = 0;
4532     }
4533
4534     if (ret == 0 && DeCompress(d, dSz, c, cSz) != (int)dSz)
4535         ret = -302;
4536
4537     if (ret == 0 && memcmp(d, sample_text, dSz))
4538         ret = -303;
4539
4540     if (c) free(c);
4541     if (d) free(d);
4542
4543     return ret;
4544 }
4545
4546 #endif /* HAVE_LIBZ */
4547
4548 #ifdef HAVE_PKCS7
4549
4550 int pkcs7enveloped_test(void)
4551 {
4552     int ret = 0;
4553
4554     int cipher = DES3b;
4555     int envelopedSz, decodedSz;
4556     PKCS7 pkcs7;
4557     byte* cert;
4558     byte* privKey;
4559     byte  enveloped[2048];
4560     byte  decoded[2048];
4561
4562     size_t certSz;
4563     size_t privKeySz;
4564     FILE*  certFile;
4565     FILE*  keyFile;
4566     FILE*  pkcs7File;
4567     const char* pkcs7OutFile = "pkcs7envelopedData.der";
4568
4569     const byte data[] = { /* Hello World */
4570         0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
4571         0x72,0x6c,0x64
4572     };
4573
4574     /* read client cert and key in DER format */
4575     cert = (byte*)malloc(FOURK_BUF);
4576     if (cert == NULL)
4577         return -201;
4578
4579     privKey = (byte*)malloc(FOURK_BUF);
4580     if (privKey == NULL) {
4581         free(cert);
4582         return -202;
4583     }
4584
4585     certFile = fopen(clientCert, "rb");
4586     if (!certFile) {
4587         free(cert);
4588         free(privKey);
4589         err_sys("can't open ./certs/client-cert.der, "
4590                 "Please run from CyaSSL home dir", -42);
4591     }
4592
4593     certSz = fread(cert, 1, FOURK_BUF, certFile);
4594     fclose(certFile);
4595
4596     keyFile = fopen(clientKey, "rb");
4597     if (!keyFile) {
4598         free(cert);
4599         free(privKey);
4600         err_sys("can't open ./certs/client-key.der, "
4601                 "Please run from CyaSSL home dir", -43);
4602     }
4603
4604     privKeySz = fread(privKey, 1, FOURK_BUF, keyFile);
4605     fclose(keyFile);
4606
4607     PKCS7_InitWithCert(&pkcs7, cert, (word32)certSz);
4608     pkcs7.content     = (byte*)data;
4609     pkcs7.contentSz   = (word32)sizeof(data);
4610     pkcs7.contentOID  = DATA;
4611     pkcs7.encryptOID  = cipher;
4612     pkcs7.privateKey  = privKey;
4613     pkcs7.privateKeySz = (word32)privKeySz;
4614
4615     /* encode envelopedData */
4616     envelopedSz = PKCS7_EncodeEnvelopedData(&pkcs7, enveloped,
4617                                             sizeof(enveloped));
4618     if (envelopedSz <= 0) {
4619         free(cert);
4620         free(privKey);
4621         return -203;
4622     }
4623
4624     /* decode envelopedData */
4625     decodedSz = PKCS7_DecodeEnvelopedData(&pkcs7, enveloped, envelopedSz,
4626                                           decoded, sizeof(decoded));
4627     if (decodedSz <= 0) {
4628         free(cert);
4629         free(privKey);
4630         return -204;
4631     }
4632
4633     /* test decode result */
4634     if (memcmp(decoded, data, sizeof(data)) != 0) {
4635         free(cert);
4636         free(privKey);
4637         return -205;
4638     }
4639
4640     /* output pkcs7 envelopedData for external testing */
4641     pkcs7File = fopen(pkcs7OutFile, "wb");
4642     if (!pkcs7File) {
4643         free(cert);
4644         free(privKey);
4645         return -206;
4646     }
4647
4648     ret = (int)fwrite(enveloped, envelopedSz, 1, pkcs7File);
4649     fclose(pkcs7File);
4650
4651     free(cert);
4652     free(privKey);
4653     PKCS7_Free(&pkcs7);
4654
4655     if (ret > 0)
4656         return 0;
4657
4658     return ret;
4659 }
4660
4661 int pkcs7signed_test(void)
4662 {
4663     int ret = 0;
4664
4665     FILE* file;
4666     byte* certDer;
4667     byte* keyDer;
4668     byte* out;
4669     char data[] = "Hello World";
4670     word32 dataSz, outSz, certDerSz, keyDerSz;
4671     PKCS7 msg;
4672     RNG rng;
4673
4674     byte transIdOid[] =
4675                { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
4676                  0x09, 0x07 };
4677     byte messageTypeOid[] =
4678                { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
4679                  0x09, 0x02 };
4680     byte senderNonceOid[] =
4681                { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
4682                  0x09, 0x05 };
4683     byte transId[(SHA_DIGEST_SIZE + 1) * 2 + 1];
4684     byte messageType[] = { 0x13, 2, '1', '9' };
4685     byte senderNonce[PKCS7_NONCE_SZ + 2];
4686
4687     PKCS7Attrib attribs[] =
4688     {
4689         { transIdOid, sizeof(transIdOid),
4690                      transId, sizeof(transId) - 1 }, /* take off the null */
4691         { messageTypeOid, sizeof(messageTypeOid),
4692                      messageType, sizeof(messageType) },
4693         { senderNonceOid, sizeof(senderNonceOid),
4694                      senderNonce, sizeof(senderNonce) }
4695     };
4696
4697     dataSz = (word32) strlen(data);
4698     outSz = FOURK_BUF;
4699
4700     certDer = (byte*)malloc(FOURK_BUF);
4701     if (certDer == NULL)
4702         return -207;
4703     keyDer = (byte*)malloc(FOURK_BUF);
4704     if (keyDer == NULL) {
4705         free(certDer);
4706         return -208;
4707     }
4708     out = (byte*)malloc(FOURK_BUF);
4709     if (out == NULL) {
4710         free(certDer);
4711         free(keyDer);
4712         return -209;
4713     }
4714
4715     /* read in DER cert of recipient, into cert of size certSz */
4716     file = fopen(clientCert, "rb");
4717     if (!file) {
4718         free(certDer);
4719         free(keyDer);
4720         free(out);
4721         err_sys("can't open ./certs/client-cert.der, "
4722                 "Please run from CyaSSL home dir", -44);
4723     }
4724     certDerSz = (word32)fread(certDer, 1, FOURK_BUF, file);
4725     fclose(file);
4726
4727     file = fopen(clientKey, "rb");
4728     if (!file) {
4729         free(certDer);
4730         free(keyDer);
4731         free(out);
4732         err_sys("can't open ./certs/client-key.der, "
4733                 "Please run from CyaSSL home dir", -45);
4734     }
4735     keyDerSz = (word32)fread(keyDer, 1, FOURK_BUF, file);
4736     fclose(file);
4737
4738     ret = InitRng(&rng);
4739     if (ret != 0) {
4740         free(certDer);
4741         free(keyDer);
4742         free(out);
4743         return -210;
4744     }
4745
4746     senderNonce[0] = 0x04;
4747     senderNonce[1] = PKCS7_NONCE_SZ;
4748
4749     ret = RNG_GenerateBlock(&rng, &senderNonce[2], PKCS7_NONCE_SZ);
4750     if (ret != 0) {
4751         free(certDer);
4752         free(keyDer);
4753         free(out);
4754         return -211;
4755     }
4756
4757     PKCS7_InitWithCert(&msg, certDer, certDerSz);
4758     msg.privateKey = keyDer;
4759     msg.privateKeySz = keyDerSz;
4760     msg.content = (byte*)data;
4761     msg.contentSz = dataSz;
4762     msg.hashOID = SHAh;
4763     msg.encryptOID = RSAk;
4764     msg.signedAttribs = attribs;
4765     msg.signedAttribsSz = sizeof(attribs)/sizeof(PKCS7Attrib);
4766     msg.rng = &rng;
4767     {
4768         Sha sha;
4769         byte digest[SHA_DIGEST_SIZE];
4770         int i,j;
4771
4772         transId[0] = 0x13;
4773         transId[1] = SHA_DIGEST_SIZE * 2;
4774
4775         ret = InitSha(&sha);
4776         if (ret != 0) {
4777             free(certDer);
4778             free(keyDer);
4779             free(out);
4780             return -4003;
4781         }
4782         ShaUpdate(&sha, msg.publicKey, msg.publicKeySz);
4783         ShaFinal(&sha, digest);
4784
4785         for (i = 0, j = 2; i < SHA_DIGEST_SIZE; i++, j += 2) {
4786             snprintf((char*)&transId[j], 3, "%02x", digest[i]);
4787         }
4788     }
4789     ret = PKCS7_EncodeSignedData(&msg, out, outSz);
4790     if (ret < 0) {
4791         free(certDer);
4792         free(keyDer);
4793         free(out);
4794         PKCS7_Free(&msg);
4795         return -212;
4796     }
4797     else
4798         outSz = ret;
4799
4800     /* write PKCS#7 to output file for more testing */
4801     file = fopen("./pkcs7signedData.der", "wb");
4802     if (!file) {
4803         free(certDer);
4804         free(keyDer);
4805         free(out);
4806         PKCS7_Free(&msg);
4807         return -213;
4808     }
4809     ret = (int)fwrite(out, 1, outSz, file);
4810     fclose(file);
4811     if (ret != (int)outSz) {
4812         free(certDer);
4813         free(keyDer);
4814         free(out);
4815         PKCS7_Free(&msg);
4816         return -218;
4817     }
4818
4819     PKCS7_Free(&msg);
4820     PKCS7_InitWithCert(&msg, NULL, 0);
4821
4822     ret = PKCS7_VerifySignedData(&msg, out, outSz);
4823     if (ret < 0) {
4824         free(certDer);
4825         free(keyDer);
4826         free(out);
4827         PKCS7_Free(&msg);
4828         return -214;
4829     }
4830
4831     if (msg.singleCert == NULL || msg.singleCertSz == 0) {
4832         free(certDer);
4833         free(keyDer);
4834         free(out);
4835         PKCS7_Free(&msg);
4836         return -215;
4837     }
4838
4839     file = fopen("./pkcs7cert.der", "wb");
4840     if (!file) {
4841         free(certDer);
4842         free(keyDer);
4843         free(out);
4844         PKCS7_Free(&msg);
4845         return -216;
4846     }
4847     ret = (int)fwrite(msg.singleCert, 1, msg.singleCertSz, file);
4848     fclose(file);
4849
4850     free(certDer);
4851     free(keyDer);
4852     free(out);
4853     PKCS7_Free(&msg);
4854
4855     if (ret > 0)
4856         return 0;
4857
4858     return ret;
4859 }
4860
4861 #endif /* HAVE_PKCS7 */
4862
4863 #endif /* NO_CRYPT_TEST */