]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/CyaSSL/IDE/MDK5-ARM/Projects/CyaSSL-Full/test.c
Update CyaSSL to latest version.
[freertos] / FreeRTOS-Plus / Source / CyaSSL / IDE / MDK5-ARM / Projects / CyaSSL-Full / 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 "crypto_ntru.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 #ifdef HAVE_CAVIUM
1153         if (i == 1)
1154             continue; /* driver can't handle keys <= bytes */
1155         if (HmacInitCavium(&hmac, CAVIUM_DEV_ID) != 0)
1156             return -20009;
1157 #endif
1158         ret = HmacSetKey(&hmac, MD5, (byte*)keys[i], (word32)strlen(keys[i]));
1159         if (ret != 0)
1160             return -4015;
1161         ret = HmacUpdate(&hmac, (byte*)test_hmac[i].input,
1162                    (word32)test_hmac[i].inLen);
1163         if (ret != 0)
1164             return -4016;
1165         ret = HmacFinal(&hmac, hash);
1166         if (ret != 0)
1167             return -4017;
1168
1169         if (memcmp(hash, test_hmac[i].output, MD5_DIGEST_SIZE) != 0)
1170             return -20 - i;
1171 #ifdef HAVE_CAVIUM
1172         HmacFreeCavium(&hmac);
1173 #endif
1174     }
1175
1176     return 0;
1177 }
1178 #endif /* NO_HMAC && NO_MD5 */
1179
1180 #if !defined(NO_HMAC) && !defined(NO_SHA)
1181 int hmac_sha_test(void)
1182 {
1183     Hmac hmac;
1184     byte hash[SHA_DIGEST_SIZE];
1185
1186     const char* keys[]=
1187     {
1188         "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
1189                                                                 "\x0b\x0b\x0b",
1190         "Jefe",
1191         "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
1192                                                                 "\xAA\xAA\xAA"
1193     };
1194
1195     testVector a, b, c;
1196     testVector test_hmac[3];
1197
1198     int ret;
1199     int times = sizeof(test_hmac) / sizeof(testVector), i;
1200
1201     a.input  = "Hi There";
1202     a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
1203                "\x8e\xf1\x46\xbe\x00";
1204     a.inLen  = strlen(a.input);
1205     a.outLen = SHA_DIGEST_SIZE;
1206
1207     b.input  = "what do ya want for nothing?";
1208     b.output = "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf"
1209                "\x9c\x25\x9a\x7c\x79";
1210     b.inLen  = strlen(b.input);
1211     b.outLen = SHA_DIGEST_SIZE;
1212
1213     c.input  = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
1214                "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
1215                "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
1216                "\xDD\xDD\xDD\xDD\xDD\xDD";
1217     c.output = "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b"
1218                "\x4f\x63\xf1\x75\xd3";
1219     c.inLen  = strlen(c.input);
1220     c.outLen = SHA_DIGEST_SIZE;
1221
1222     test_hmac[0] = a;
1223     test_hmac[1] = b;
1224     test_hmac[2] = c;
1225
1226     for (i = 0; i < times; ++i) {
1227 #ifdef HAVE_CAVIUM
1228         if (i == 1)
1229             continue; /* driver can't handle keys <= bytes */
1230         if (HmacInitCavium(&hmac, CAVIUM_DEV_ID) != 0)
1231             return -20010;
1232 #endif
1233         ret = HmacSetKey(&hmac, SHA, (byte*)keys[i], (word32)strlen(keys[i]));
1234         if (ret != 0)
1235             return -4018;
1236         ret = HmacUpdate(&hmac, (byte*)test_hmac[i].input,
1237                    (word32)test_hmac[i].inLen);
1238         if (ret != 0)
1239             return -4019;
1240         ret = HmacFinal(&hmac, hash);
1241         if (ret != 0)
1242             return -4020;
1243
1244         if (memcmp(hash, test_hmac[i].output, SHA_DIGEST_SIZE) != 0)
1245             return -20 - i;
1246 #ifdef HAVE_CAVIUM
1247         HmacFreeCavium(&hmac);
1248 #endif
1249     }
1250
1251     return 0;
1252 }
1253 #endif
1254
1255
1256 #if !defined(NO_HMAC) && !defined(NO_SHA256)
1257 int hmac_sha256_test(void)
1258 {
1259     Hmac hmac;
1260     byte hash[SHA256_DIGEST_SIZE];
1261
1262     const char* keys[]=
1263     {
1264         "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
1265                                                                 "\x0b\x0b\x0b",
1266         "Jefe",
1267         "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
1268                                                                 "\xAA\xAA\xAA"
1269     };
1270
1271     testVector a, b, c;
1272     testVector test_hmac[3];
1273
1274     int ret;
1275     int times = sizeof(test_hmac) / sizeof(testVector), i;
1276
1277     a.input  = "Hi There";
1278     a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
1279                "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
1280                "\xcf\xf7";
1281     a.inLen  = strlen(a.input);
1282     a.outLen = SHA256_DIGEST_SIZE;
1283
1284     b.input  = "what do ya want for nothing?";
1285     b.output = "\x5b\xdc\xc1\x46\xbf\x60\x75\x4e\x6a\x04\x24\x26\x08\x95\x75"
1286                "\xc7\x5a\x00\x3f\x08\x9d\x27\x39\x83\x9d\xec\x58\xb9\x64\xec"
1287                "\x38\x43";
1288     b.inLen  = strlen(b.input);
1289     b.outLen = SHA256_DIGEST_SIZE;
1290
1291     c.input  = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
1292                "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
1293                "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
1294                "\xDD\xDD\xDD\xDD\xDD\xDD";
1295     c.output = "\x77\x3e\xa9\x1e\x36\x80\x0e\x46\x85\x4d\xb8\xeb\xd0\x91\x81"
1296                "\xa7\x29\x59\x09\x8b\x3e\xf8\xc1\x22\xd9\x63\x55\x14\xce\xd5"
1297                "\x65\xfe";
1298     c.inLen  = strlen(c.input);
1299     c.outLen = SHA256_DIGEST_SIZE;
1300
1301     test_hmac[0] = a;
1302     test_hmac[1] = b;
1303     test_hmac[2] = c;
1304
1305     for (i = 0; i < times; ++i) {
1306 #ifdef HAVE_CAVIUM
1307         if (i == 1)
1308             continue; /* driver can't handle keys <= bytes */
1309         if (HmacInitCavium(&hmac, CAVIUM_DEV_ID) != 0)
1310             return -20011;
1311 #endif
1312         ret = HmacSetKey(&hmac, SHA256, (byte*)keys[i],(word32)strlen(keys[i]));
1313         if (ret != 0)
1314             return -4021;
1315         ret = HmacUpdate(&hmac, (byte*)test_hmac[i].input,
1316                    (word32)test_hmac[i].inLen);
1317         if (ret != 0)
1318             return -4022;
1319         ret = HmacFinal(&hmac, hash);
1320         if (ret != 0)
1321             return -4023;
1322
1323         if (memcmp(hash, test_hmac[i].output, SHA256_DIGEST_SIZE) != 0)
1324             return -20 - i;
1325 #ifdef HAVE_CAVIUM
1326         HmacFreeCavium(&hmac);
1327 #endif
1328     }
1329
1330     return 0;
1331 }
1332 #endif
1333
1334
1335 #if !defined(NO_HMAC) && defined(HAVE_BLAKE2)
1336 int hmac_blake2b_test(void)
1337 {
1338     Hmac hmac;
1339     byte hash[BLAKE2B_256];
1340
1341     const char* keys[]=
1342     {
1343         "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
1344                                                                 "\x0b\x0b\x0b",
1345         "Jefe",
1346         "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
1347                                                                 "\xAA\xAA\xAA"
1348     };
1349
1350     testVector a, b, c;
1351     testVector test_hmac[3];
1352
1353     int ret;
1354     int times = sizeof(test_hmac) / sizeof(testVector), i;
1355
1356     a.input  = "Hi There";
1357     a.output = "\x72\x93\x0d\xdd\xf5\xf7\xe1\x78\x38\x07\x44\x18\x0b\x3f\x51"
1358                "\x37\x25\xb5\x82\xc2\x08\x83\x2f\x1c\x99\xfd\x03\xa0\x16\x75"
1359                "\xac\xfd";
1360     a.inLen  = strlen(a.input);
1361     a.outLen = BLAKE2B_256;
1362
1363     b.input  = "what do ya want for nothing?";
1364     b.output = "\x3d\x20\x50\x71\x05\xc0\x8c\x0c\x38\x44\x1e\xf7\xf9\xd1\x67"
1365                "\x21\xff\x64\xf5\x94\x00\xcf\xf9\x75\x41\xda\x88\x61\x9d\x7c"
1366                "\xda\x2b";
1367     b.inLen  = strlen(b.input);
1368     b.outLen = BLAKE2B_256;
1369
1370     c.input  = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
1371                "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
1372                "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
1373                "\xDD\xDD\xDD\xDD\xDD\xDD";
1374     c.output = "\xda\xfe\x2a\x24\xfc\xe7\xea\x36\x34\xbe\x41\x92\xc7\x11\xa7"
1375                "\x00\xae\x53\x9c\x11\x9c\x80\x74\x55\x22\x25\x4a\xb9\x55\xd3"
1376                "\x0f\x87";
1377     c.inLen  = strlen(c.input);
1378     c.outLen = BLAKE2B_256;
1379
1380     test_hmac[0] = a;
1381     test_hmac[1] = b;
1382     test_hmac[2] = c;
1383
1384     for (i = 0; i < times; ++i) {
1385 #ifdef HAVE_CAVIUM
1386         if (i == 1)
1387             continue; /* driver can't handle keys <= bytes */
1388         if (HmacInitCavium(&hmac, CAVIUM_DEV_ID) != 0)
1389             return -20011;
1390 #endif
1391         ret = HmacSetKey(&hmac, BLAKE2B_ID, (byte*)keys[i],
1392                          (word32)strlen(keys[i]));
1393         if (ret != 0)
1394             return -4024;
1395         ret = HmacUpdate(&hmac, (byte*)test_hmac[i].input,
1396                    (word32)test_hmac[i].inLen);
1397         if (ret != 0)
1398             return -4025;
1399         ret = HmacFinal(&hmac, hash);
1400         if (ret != 0)
1401             return -4026;
1402
1403         if (memcmp(hash, test_hmac[i].output, BLAKE2B_256) != 0)
1404             return -20 - i;
1405 #ifdef HAVE_CAVIUM
1406         HmacFreeCavium(&hmac);
1407 #endif
1408     }
1409
1410     return 0;
1411 }
1412 #endif
1413
1414
1415 #if !defined(NO_HMAC) && defined(CYASSL_SHA384)
1416 int hmac_sha384_test(void)
1417 {
1418     Hmac hmac;
1419     byte hash[SHA384_DIGEST_SIZE];
1420
1421     const char* keys[]=
1422     {
1423         "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
1424                                                                 "\x0b\x0b\x0b",
1425         "Jefe",
1426         "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
1427                                                                 "\xAA\xAA\xAA"
1428     };
1429
1430     testVector a, b, c;
1431     testVector test_hmac[3];
1432
1433     int ret;
1434     int times = sizeof(test_hmac) / sizeof(testVector), i;
1435
1436     a.input  = "Hi There";
1437     a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
1438                "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
1439                "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
1440                "\xfa\x9c\xb6";
1441     a.inLen  = strlen(a.input);
1442     a.outLen = SHA384_DIGEST_SIZE;
1443
1444     b.input  = "what do ya want for nothing?";
1445     b.output = "\xaf\x45\xd2\xe3\x76\x48\x40\x31\x61\x7f\x78\xd2\xb5\x8a\x6b"
1446                "\x1b\x9c\x7e\xf4\x64\xf5\xa0\x1b\x47\xe4\x2e\xc3\x73\x63\x22"
1447                "\x44\x5e\x8e\x22\x40\xca\x5e\x69\xe2\xc7\x8b\x32\x39\xec\xfa"
1448                "\xb2\x16\x49";
1449     b.inLen  = strlen(b.input);
1450     b.outLen = SHA384_DIGEST_SIZE;
1451
1452     c.input  = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
1453                "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
1454                "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
1455                "\xDD\xDD\xDD\xDD\xDD\xDD";
1456     c.output = "\x88\x06\x26\x08\xd3\xe6\xad\x8a\x0a\xa2\xac\xe0\x14\xc8\xa8"
1457                "\x6f\x0a\xa6\x35\xd9\x47\xac\x9f\xeb\xe8\x3e\xf4\xe5\x59\x66"
1458                "\x14\x4b\x2a\x5a\xb3\x9d\xc1\x38\x14\xb9\x4e\x3a\xb6\xe1\x01"
1459                "\xa3\x4f\x27";
1460     c.inLen  = strlen(c.input);
1461     c.outLen = SHA384_DIGEST_SIZE;
1462
1463     test_hmac[0] = a;
1464     test_hmac[1] = b;
1465     test_hmac[2] = c;
1466
1467     for (i = 0; i < times; ++i) {
1468         ret = HmacSetKey(&hmac, SHA384, (byte*)keys[i],(word32)strlen(keys[i]));
1469         if (ret != 0)
1470             return -4027;
1471         ret = HmacUpdate(&hmac, (byte*)test_hmac[i].input,
1472                    (word32)test_hmac[i].inLen);
1473         if (ret != 0)
1474             return -4028;
1475         ret = HmacFinal(&hmac, hash);
1476         if (ret != 0)
1477             return -4029;
1478
1479         if (memcmp(hash, test_hmac[i].output, SHA384_DIGEST_SIZE) != 0)
1480             return -20 - i;
1481     }
1482
1483     return 0;
1484 }
1485 #endif
1486
1487
1488 #if !defined(NO_HMAC) && defined(CYASSL_SHA512)
1489 int hmac_sha512_test(void)
1490 {
1491     Hmac hmac;
1492     byte hash[SHA512_DIGEST_SIZE];
1493
1494     const char* keys[]=
1495     {
1496         "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
1497                                                                 "\x0b\x0b\x0b",
1498         "Jefe",
1499         "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
1500                                                                 "\xAA\xAA\xAA"
1501     };
1502
1503     testVector a, b, c;
1504     testVector test_hmac[3];
1505
1506     int ret;
1507     int times = sizeof(test_hmac) / sizeof(testVector), i;
1508
1509     a.input  = "Hi There";
1510     a.output = "\x87\xaa\x7c\xde\xa5\xef\x61\x9d\x4f\xf0\xb4\x24\x1a\x1d\x6c"
1511                "\xb0\x23\x79\xf4\xe2\xce\x4e\xc2\x78\x7a\xd0\xb3\x05\x45\xe1"
1512                "\x7c\xde\xda\xa8\x33\xb7\xd6\xb8\xa7\x02\x03\x8b\x27\x4e\xae"
1513                "\xa3\xf4\xe4\xbe\x9d\x91\x4e\xeb\x61\xf1\x70\x2e\x69\x6c\x20"
1514                "\x3a\x12\x68\x54";
1515     a.inLen  = strlen(a.input);
1516     a.outLen = SHA512_DIGEST_SIZE;
1517
1518     b.input  = "what do ya want for nothing?";
1519     b.output = "\x16\x4b\x7a\x7b\xfc\xf8\x19\xe2\xe3\x95\xfb\xe7\x3b\x56\xe0"
1520                "\xa3\x87\xbd\x64\x22\x2e\x83\x1f\xd6\x10\x27\x0c\xd7\xea\x25"
1521                "\x05\x54\x97\x58\xbf\x75\xc0\x5a\x99\x4a\x6d\x03\x4f\x65\xf8"
1522                "\xf0\xe6\xfd\xca\xea\xb1\xa3\x4d\x4a\x6b\x4b\x63\x6e\x07\x0a"
1523                "\x38\xbc\xe7\x37";
1524     b.inLen  = strlen(b.input);
1525     b.outLen = SHA512_DIGEST_SIZE;
1526
1527     c.input  = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
1528                "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
1529                "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
1530                "\xDD\xDD\xDD\xDD\xDD\xDD";
1531     c.output = "\xfa\x73\xb0\x08\x9d\x56\xa2\x84\xef\xb0\xf0\x75\x6c\x89\x0b"
1532                "\xe9\xb1\xb5\xdb\xdd\x8e\xe8\x1a\x36\x55\xf8\x3e\x33\xb2\x27"
1533                "\x9d\x39\xbf\x3e\x84\x82\x79\xa7\x22\xc8\x06\xb4\x85\xa4\x7e"
1534                "\x67\xc8\x07\xb9\x46\xa3\x37\xbe\xe8\x94\x26\x74\x27\x88\x59"
1535                "\xe1\x32\x92\xfb";
1536     c.inLen  = strlen(c.input);
1537     c.outLen = SHA512_DIGEST_SIZE;
1538
1539     test_hmac[0] = a;
1540     test_hmac[1] = b;
1541     test_hmac[2] = c;
1542
1543     for (i = 0; i < times; ++i) {
1544         ret = HmacSetKey(&hmac, SHA512, (byte*)keys[i],(word32)strlen(keys[i]));
1545         if (ret != 0)
1546             return -4030;
1547         ret = HmacUpdate(&hmac, (byte*)test_hmac[i].input,
1548                    (word32)test_hmac[i].inLen);
1549         if (ret != 0)
1550             return -4031;
1551         ret = HmacFinal(&hmac, hash);
1552         if (ret != 0)
1553             return -4032;
1554
1555         if (memcmp(hash, test_hmac[i].output, SHA512_DIGEST_SIZE) != 0)
1556             return -20 - i;
1557     }
1558
1559     return 0;
1560 }
1561 #endif
1562
1563
1564 #ifndef NO_RC4
1565 int arc4_test(void)
1566 {
1567     byte cipher[16];
1568     byte plain[16];
1569
1570     const char* keys[] =
1571     {
1572         "\x01\x23\x45\x67\x89\xab\xcd\xef",
1573         "\x01\x23\x45\x67\x89\xab\xcd\xef",
1574         "\x00\x00\x00\x00\x00\x00\x00\x00",
1575         "\xef\x01\x23\x45"
1576     };
1577
1578     testVector a, b, c, d;
1579     testVector test_arc4[4];
1580
1581     int times = sizeof(test_arc4) / sizeof(testVector), i;
1582
1583     a.input  = "\x01\x23\x45\x67\x89\xab\xcd\xef";
1584     a.output = "\x75\xb7\x87\x80\x99\xe0\xc5\x96";
1585     a.inLen  = 8;
1586     a.outLen = 8;
1587
1588     b.input  = "\x00\x00\x00\x00\x00\x00\x00\x00";
1589     b.output = "\x74\x94\xc2\xe7\x10\x4b\x08\x79";
1590     b.inLen  = 8;
1591     b.outLen = 8;
1592
1593     c.input  = "\x00\x00\x00\x00\x00\x00\x00\x00";
1594     c.output = "\xde\x18\x89\x41\xa3\x37\x5d\x3a";
1595     c.inLen  = 8;
1596     c.outLen = 8;
1597
1598     d.input  = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
1599     d.output = "\xd6\xa1\x41\xa7\xec\x3c\x38\xdf\xbd\x61";
1600     d.inLen  = 10;
1601     d.outLen = 10;
1602
1603     test_arc4[0] = a;
1604     test_arc4[1] = b;
1605     test_arc4[2] = c;
1606     test_arc4[3] = d;
1607
1608     for (i = 0; i < times; ++i) {
1609         Arc4 enc;
1610         Arc4 dec;
1611         int  keylen = 8;  /* strlen with key 0x00 not good */
1612         if (i == 3)
1613             keylen = 4;
1614
1615 #ifdef HAVE_CAVIUM
1616         if (Arc4InitCavium(&enc, CAVIUM_DEV_ID) != 0)
1617             return -20001;
1618         if (Arc4InitCavium(&dec, CAVIUM_DEV_ID) != 0)
1619             return -20002;
1620 #endif
1621
1622         Arc4SetKey(&enc, (byte*)keys[i], keylen);
1623         Arc4SetKey(&dec, (byte*)keys[i], keylen);
1624
1625         Arc4Process(&enc, cipher, (byte*)test_arc4[i].input,
1626                     (word32)test_arc4[i].outLen);
1627         Arc4Process(&dec, plain,  cipher, (word32)test_arc4[i].outLen);
1628
1629         if (memcmp(plain, test_arc4[i].input, test_arc4[i].outLen))
1630             return -20 - i;
1631
1632         if (memcmp(cipher, test_arc4[i].output, test_arc4[i].outLen))
1633             return -20 - 5 - i;
1634
1635 #ifdef HAVE_CAVIUM
1636         Arc4FreeCavium(&enc);
1637         Arc4FreeCavium(&dec);
1638 #endif
1639     }
1640
1641     return 0;
1642 }
1643 #endif
1644
1645
1646 int hc128_test(void)
1647 {
1648 #ifdef HAVE_HC128
1649     byte cipher[16];
1650     byte plain[16];
1651
1652     const char* keys[] =
1653     {
1654         "\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
1655         "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
1656         "\x00\x53\xA6\xF9\x4C\x9F\xF2\x45\x98\xEB\x3E\x91\xE4\x37\x8A\xDD",
1657         "\x0F\x62\xB5\x08\x5B\xAE\x01\x54\xA7\xFA\x4D\xA0\xF3\x46\x99\xEC"
1658     };
1659
1660     const char* ivs[] =
1661     {
1662         "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
1663         "\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
1664         "\x0D\x74\xDB\x42\xA9\x10\x77\xDE\x45\xAC\x13\x7A\xE1\x48\xAF\x16",
1665         "\x28\x8F\xF6\x5D\xC4\x2B\x92\xF9\x60\xC7\x2E\x95\xFC\x63\xCA\x31"
1666     };
1667
1668
1669     testVector a, b, c, d;
1670     testVector test_hc128[4];
1671
1672     int times = sizeof(test_hc128) / sizeof(testVector), i;
1673
1674     a.input  = "\x00\x00\x00\x00\x00\x00\x00\x00";
1675     a.output = "\x37\x86\x02\xB9\x8F\x32\xA7\x48";
1676     a.inLen  = 8;
1677     a.outLen = 8;
1678
1679     b.input  = "\x00\x00\x00\x00\x00\x00\x00\x00";
1680     b.output = "\x33\x7F\x86\x11\xC6\xED\x61\x5F";
1681     b.inLen  = 8;
1682     b.outLen = 8;
1683
1684     c.input  = "\x00\x00\x00\x00\x00\x00\x00\x00";
1685     c.output = "\x2E\x1E\xD1\x2A\x85\x51\xC0\x5A";
1686     c.inLen  = 8;
1687     c.outLen = 8;
1688
1689     d.input  = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
1690     d.output = "\x1C\xD8\xAE\xDD\xFE\x52\xE2\x17\xE8\x35\xD0\xB7\xE8\x4E\x29";
1691     d.inLen  = 15;
1692     d.outLen = 15;
1693
1694     test_hc128[0] = a;
1695     test_hc128[1] = b;
1696     test_hc128[2] = c;
1697     test_hc128[3] = d;
1698
1699     for (i = 0; i < times; ++i) {
1700         HC128 enc;
1701         HC128 dec;
1702
1703         /* align keys/ivs in plain/cipher buffers */
1704         memcpy(plain,  keys[i], 16);
1705         memcpy(cipher, ivs[i],  16);
1706
1707         Hc128_SetKey(&enc, plain, cipher);
1708         Hc128_SetKey(&dec, plain, cipher);
1709
1710         /* align input */
1711         memcpy(plain, test_hc128[i].input, test_hc128[i].outLen);
1712         Hc128_Process(&enc, cipher, plain,  (word32)test_hc128[i].outLen);
1713         Hc128_Process(&dec, plain,  cipher, (word32)test_hc128[i].outLen);
1714
1715         if (memcmp(plain, test_hc128[i].input, test_hc128[i].outLen))
1716             return -120 - i;
1717
1718         if (memcmp(cipher, test_hc128[i].output, test_hc128[i].outLen))
1719             return -120 - 5 - i;
1720     }
1721
1722 #endif /* HAVE_HC128 */
1723     return 0;
1724 }
1725
1726
1727 #ifndef NO_RABBIT
1728 int rabbit_test(void)
1729 {
1730     byte cipher[16];
1731     byte plain[16];
1732
1733     const char* keys[] =
1734     {
1735         "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
1736         "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
1737         "\xAC\xC3\x51\xDC\xF1\x62\xFC\x3B\xFE\x36\x3D\x2E\x29\x13\x28\x91"
1738     };
1739
1740     const char* ivs[] =
1741     {
1742         "\x00\x00\x00\x00\x00\x00\x00\x00",
1743         "\x59\x7E\x26\xC1\x75\xF5\x73\xC3",
1744         0
1745     };
1746
1747     testVector a, b, c;
1748     testVector test_rabbit[3];
1749
1750     int times = sizeof(test_rabbit) / sizeof(testVector), i;
1751
1752     a.input  = "\x00\x00\x00\x00\x00\x00\x00\x00";
1753     a.output = "\xED\xB7\x05\x67\x37\x5D\xCD\x7C";
1754     a.inLen  = 8;
1755     a.outLen = 8;
1756
1757     b.input  = "\x00\x00\x00\x00\x00\x00\x00\x00";
1758     b.output = "\x6D\x7D\x01\x22\x92\xCC\xDC\xE0";
1759     b.inLen  = 8;
1760     b.outLen = 8;
1761
1762     c.input  = "\x00\x00\x00\x00\x00\x00\x00\x00";
1763     c.output = "\x04\xCE\xCA\x7A\x1A\x86\x6E\x77";
1764     c.inLen  = 8;
1765     c.outLen = 8;
1766
1767     test_rabbit[0] = a;
1768     test_rabbit[1] = b;
1769     test_rabbit[2] = c;
1770
1771     for (i = 0; i < times; ++i) {
1772         Rabbit enc;
1773         Rabbit dec;
1774         byte*  iv;
1775
1776         /* align keys/ivs in plain/cipher buffers */
1777         memcpy(plain,  keys[i], 16);
1778         if (ivs[i]) {
1779             memcpy(cipher, ivs[i],   8);
1780             iv = cipher;
1781         } else
1782             iv = NULL;
1783         RabbitSetKey(&enc, plain, iv);
1784         RabbitSetKey(&dec, plain, iv);
1785
1786         /* align input */
1787         memcpy(plain, test_rabbit[i].input, test_rabbit[i].outLen);
1788         RabbitProcess(&enc, cipher, plain,  (word32)test_rabbit[i].outLen);
1789         RabbitProcess(&dec, plain,  cipher, (word32)test_rabbit[i].outLen);
1790
1791         if (memcmp(plain, test_rabbit[i].input, test_rabbit[i].outLen))
1792             return -130 - i;
1793
1794         if (memcmp(cipher, test_rabbit[i].output, test_rabbit[i].outLen))
1795             return -130 - 5 - i;
1796     }
1797
1798     return 0;
1799 }
1800 #endif /* NO_RABBIT */
1801
1802
1803 #ifndef NO_DES3
1804 int des_test(void)
1805 {
1806     const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
1807         0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
1808         0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
1809         0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
1810     };
1811
1812     byte plain[24];
1813     byte cipher[24];
1814
1815     Des enc;
1816     Des dec;
1817
1818     const byte key[] =
1819     {
1820         0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
1821     };
1822
1823     const byte iv[] =
1824     {
1825         0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
1826     };
1827
1828     const byte verify[] =
1829     {
1830         0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
1831         0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
1832         0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
1833     };
1834
1835     int ret;
1836
1837     ret = Des_SetKey(&enc, key, iv, DES_ENCRYPTION);
1838     if (ret != 0)
1839         return -31;
1840
1841     Des_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
1842     ret = Des_SetKey(&dec, key, iv, DES_DECRYPTION);
1843     if (ret != 0)
1844         return -32;
1845     Des_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
1846
1847     if (memcmp(plain, vector, sizeof(plain)))
1848         return -33;
1849
1850     if (memcmp(cipher, verify, sizeof(cipher)))
1851         return -34;
1852
1853     return 0;
1854 }
1855 #endif /* NO_DES3 */
1856
1857
1858 #ifndef NO_DES3
1859 int des3_test(void)
1860 {
1861     const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
1862         0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
1863         0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
1864         0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
1865     };
1866
1867     byte plain[24];
1868     byte cipher[24];
1869
1870     Des3 enc;
1871     Des3 dec;
1872
1873     const byte key3[] =
1874     {
1875         0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
1876         0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
1877         0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
1878     };
1879     const byte iv3[] =
1880     {
1881         0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
1882         0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
1883         0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
1884
1885     };
1886
1887     const byte verify3[] =
1888     {
1889         0x43,0xa0,0x29,0x7e,0xd1,0x84,0xf8,0x0e,
1890         0x89,0x64,0x84,0x32,0x12,0xd5,0x08,0x98,
1891         0x18,0x94,0x15,0x74,0x87,0x12,0x7d,0xb0
1892     };
1893
1894     int ret;
1895
1896
1897 #ifdef HAVE_CAVIUM
1898     if (Des3_InitCavium(&enc, CAVIUM_DEV_ID) != 0)
1899         return -20005;
1900     if (Des3_InitCavium(&dec, CAVIUM_DEV_ID) != 0)
1901         return -20006;
1902 #endif
1903     ret = Des3_SetKey(&enc, key3, iv3, DES_ENCRYPTION);
1904     if (ret != 0)
1905         return -31;
1906     ret = Des3_SetKey(&dec, key3, iv3, DES_DECRYPTION);
1907     if (ret != 0)
1908         return -32;
1909     ret = Des3_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
1910     if (ret != 0)
1911         return -33;
1912     ret = Des3_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
1913     if (ret != 0)
1914         return -34;
1915
1916     if (memcmp(plain, vector, sizeof(plain)))
1917         return -35;
1918
1919     if (memcmp(cipher, verify3, sizeof(cipher)))
1920         return -36;
1921
1922 #ifdef HAVE_CAVIUM
1923     Des3_FreeCavium(&enc);
1924     Des3_FreeCavium(&dec);
1925 #endif
1926     return 0;
1927 }
1928 #endif /* NO_DES */
1929
1930
1931 #ifndef NO_AES
1932 int aes_test(void)
1933 {
1934     Aes enc;
1935     Aes dec;
1936
1937     const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
1938         0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
1939         0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
1940         0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
1941     };
1942
1943     const byte verify[] =
1944     {
1945         0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
1946         0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
1947     };
1948
1949     byte key[] = "0123456789abcdef   ";  /* align */
1950     byte iv[]  = "1234567890abcdef   ";  /* align */
1951
1952     byte cipher[AES_BLOCK_SIZE * 4];
1953     byte plain [AES_BLOCK_SIZE * 4];
1954     int  ret;
1955
1956 #ifdef HAVE_CAVIUM
1957         if (AesInitCavium(&enc, CAVIUM_DEV_ID) != 0)
1958             return -20003;
1959         if (AesInitCavium(&dec, CAVIUM_DEV_ID) != 0)
1960             return -20004;
1961 #endif
1962     ret = AesSetKey(&enc, key, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
1963     if (ret != 0)
1964         return -1001;
1965     ret = AesSetKey(&dec, key, AES_BLOCK_SIZE, iv, AES_DECRYPTION);
1966     if (ret != 0)
1967         return -1002;
1968
1969     ret = AesCbcEncrypt(&enc, cipher, msg,   AES_BLOCK_SIZE);
1970     if (ret != 0)
1971         return -1005;
1972     ret = AesCbcDecrypt(&dec, plain, cipher, AES_BLOCK_SIZE);
1973     if (ret != 0)
1974         return -1006;
1975
1976     if (memcmp(plain, msg, AES_BLOCK_SIZE))
1977         return -60;
1978
1979     if (memcmp(cipher, verify, AES_BLOCK_SIZE))
1980         return -61;
1981
1982 #ifdef HAVE_CAVIUM
1983         AesFreeCavium(&enc);
1984         AesFreeCavium(&dec);
1985 #endif
1986 #ifdef CYASSL_AES_COUNTER
1987     {
1988         const byte ctrKey[] =
1989         {
1990             0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
1991             0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
1992         };
1993
1994         const byte ctrIv[] =
1995         {
1996             0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
1997             0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
1998         };
1999
2000
2001         const byte ctrPlain[] =
2002         {
2003             0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
2004             0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
2005             0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
2006             0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
2007             0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
2008             0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
2009             0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
2010             0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
2011         };
2012
2013         const byte ctrCipher[] =
2014         {
2015             0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
2016             0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
2017             0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
2018             0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
2019             0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
2020             0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
2021             0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
2022             0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
2023         };
2024
2025         const byte oddCipher[] =
2026         {
2027             0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
2028             0xc2
2029         };
2030
2031         AesSetKeyDirect(&enc, ctrKey, AES_BLOCK_SIZE, ctrIv, AES_ENCRYPTION);
2032         /* Ctr only uses encrypt, even on key setup */
2033         AesSetKeyDirect(&dec, ctrKey, AES_BLOCK_SIZE, ctrIv, AES_ENCRYPTION);
2034
2035         AesCtrEncrypt(&enc, cipher, ctrPlain, AES_BLOCK_SIZE*4);
2036         AesCtrEncrypt(&dec, plain, cipher, AES_BLOCK_SIZE*4);
2037
2038         if (memcmp(plain, ctrPlain, AES_BLOCK_SIZE*4))
2039             return -66;
2040
2041         if (memcmp(cipher, ctrCipher, AES_BLOCK_SIZE*4))
2042             return -67;
2043
2044         /* let's try with just 9 bytes, non block size test */
2045         AesSetKeyDirect(&enc, ctrKey, AES_BLOCK_SIZE, ctrIv, AES_ENCRYPTION);
2046         /* Ctr only uses encrypt, even on key setup */
2047         AesSetKeyDirect(&dec, ctrKey, AES_BLOCK_SIZE, ctrIv, AES_ENCRYPTION);
2048
2049         AesCtrEncrypt(&enc, cipher, ctrPlain, 9);
2050         AesCtrEncrypt(&dec, plain, cipher, 9);
2051
2052         if (memcmp(plain, ctrPlain, 9))
2053             return -68;
2054
2055         if (memcmp(cipher, ctrCipher, 9))
2056             return -69;
2057
2058         /* and an additional 9 bytes to reuse tmp left buffer */
2059         AesCtrEncrypt(&enc, cipher, ctrPlain, 9);
2060         AesCtrEncrypt(&dec, plain, cipher, 9);
2061
2062         if (memcmp(plain, ctrPlain, 9))
2063             return -70;
2064
2065         if (memcmp(cipher, oddCipher, 9))
2066             return -71;
2067     }
2068 #endif /* CYASSL_AES_COUNTER */
2069
2070 #if defined(CYASSL_AESNI) && defined(CYASSL_AES_DIRECT)
2071     {
2072         const byte niPlain[] =
2073         {
2074             0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
2075             0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
2076         };
2077
2078         const byte niCipher[] =
2079         {
2080             0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
2081             0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
2082         };
2083
2084         const byte niKey[] =
2085         {
2086             0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
2087             0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
2088             0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
2089             0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
2090         };
2091
2092         XMEMSET(cipher, 0, AES_BLOCK_SIZE);
2093         ret = AesSetKey(&enc, niKey, sizeof(niKey), cipher, AES_ENCRYPTION);
2094         if (ret != 0)
2095             return -1003;
2096         AesEncryptDirect(&enc, cipher, niPlain);
2097         if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
2098             return -20006;
2099
2100         XMEMSET(plain, 0, AES_BLOCK_SIZE);
2101         ret = AesSetKey(&dec, niKey, sizeof(niKey), plain, AES_DECRYPTION);
2102         if (ret != 0)
2103             return -1004;
2104         AesDecryptDirect(&dec, plain, niCipher);
2105         if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
2106             return -20007;
2107     }
2108 #endif /* CYASSL_AESNI && CYASSL_AES_DIRECT */
2109
2110     return 0;
2111 }
2112
2113 #ifdef HAVE_AESGCM
2114 int aesgcm_test(void)
2115 {
2116     Aes enc;
2117
2118     /*
2119      * This is Test Case 16 from the document Galois/
2120      * Counter Mode of Operation (GCM) by McGrew and
2121      * Viega.
2122      */
2123     const byte k[] =
2124     {
2125         0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
2126         0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
2127         0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
2128         0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08
2129     };
2130
2131     const byte iv[] =
2132     {
2133         0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad,
2134         0xde, 0xca, 0xf8, 0x88
2135     };
2136
2137     const byte p[] =
2138     {
2139         0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5,
2140         0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a,
2141         0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
2142         0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72,
2143         0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53,
2144         0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25,
2145         0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57,
2146         0xba, 0x63, 0x7b, 0x39
2147     };
2148
2149     const byte a[] =
2150     {
2151         0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
2152         0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
2153         0xab, 0xad, 0xda, 0xd2
2154     };
2155
2156     const byte c[] =
2157     {
2158         0x52, 0x2d, 0xc1, 0xf0, 0x99, 0x56, 0x7d, 0x07,
2159         0xf4, 0x7f, 0x37, 0xa3, 0x2a, 0x84, 0x42, 0x7d,
2160         0x64, 0x3a, 0x8c, 0xdc, 0xbf, 0xe5, 0xc0, 0xc9,
2161         0x75, 0x98, 0xa2, 0xbd, 0x25, 0x55, 0xd1, 0xaa,
2162         0x8c, 0xb0, 0x8e, 0x48, 0x59, 0x0d, 0xbb, 0x3d,
2163         0xa7, 0xb0, 0x8b, 0x10, 0x56, 0x82, 0x88, 0x38,
2164         0xc5, 0xf6, 0x1e, 0x63, 0x93, 0xba, 0x7a, 0x0a,
2165         0xbc, 0xc9, 0xf6, 0x62
2166     };
2167
2168     const byte t[] =
2169     {
2170         0x76, 0xfc, 0x6e, 0xce, 0x0f, 0x4e, 0x17, 0x68,
2171         0xcd, 0xdf, 0x88, 0x53, 0xbb, 0x2d, 0x55, 0x1b
2172     };
2173
2174     byte t2[sizeof(t)];
2175     byte p2[sizeof(c)];
2176     byte c2[sizeof(p)];
2177
2178     int result;
2179
2180     memset(t2, 0, sizeof(t2));
2181     memset(c2, 0, sizeof(c2));
2182     memset(p2, 0, sizeof(p2));
2183
2184     AesGcmSetKey(&enc, k, sizeof(k));
2185     /* AES-GCM encrypt and decrypt both use AES encrypt internally */
2186     AesGcmEncrypt(&enc, c2, p, sizeof(c2), iv, sizeof(iv),
2187                                                  t2, sizeof(t2), a, sizeof(a));
2188     if (memcmp(c, c2, sizeof(c2)))
2189         return -68;
2190     if (memcmp(t, t2, sizeof(t2)))
2191         return -69;
2192
2193     result = AesGcmDecrypt(&enc, p2, c2, sizeof(p2), iv, sizeof(iv),
2194                                                  t2, sizeof(t2), a, sizeof(a));
2195     if (result != 0)
2196         return -70;
2197     if (memcmp(p, p2, sizeof(p2)))
2198         return -71;
2199
2200     return 0;
2201 }
2202
2203 int gmac_test(void)
2204 {
2205     Gmac gmac;
2206
2207     const byte k1[] =
2208     {
2209         0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
2210         0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
2211     };
2212     const byte iv1[] =
2213     {
2214         0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
2215         0xe2, 0x8c, 0x8f, 0x16
2216     };
2217     const byte a1[] =
2218     {
2219        0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
2220        0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
2221     };
2222     const byte t1[] =
2223     {
2224         0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
2225         0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
2226     };
2227
2228     const byte k2[] =
2229     {
2230         0x40, 0xf7, 0xec, 0xb2, 0x52, 0x6d, 0xaa, 0xd4,
2231         0x74, 0x25, 0x1d, 0xf4, 0x88, 0x9e, 0xf6, 0x5b
2232     };
2233     const byte iv2[] =
2234     {
2235         0xee, 0x9c, 0x6e, 0x06, 0x15, 0x45, 0x45, 0x03,
2236         0x1a, 0x60, 0x24, 0xa7
2237     };
2238     const byte a2[] =
2239     {
2240         0x94, 0x81, 0x2c, 0x87, 0x07, 0x4e, 0x15, 0x18,
2241         0x34, 0xb8, 0x35, 0xaf, 0x1c, 0xa5, 0x7e, 0x56
2242     };
2243     const byte t2[] =
2244     {
2245         0xc6, 0x81, 0x79, 0x8e, 0x3d, 0xda, 0xb0, 0x9f,
2246         0x8d, 0x83, 0xb0, 0xbb, 0x14, 0xb6, 0x91
2247     };
2248
2249     const byte k3[] =
2250     {
2251         0xb8, 0xe4, 0x9a, 0x5e, 0x37, 0xf9, 0x98, 0x2b,
2252         0xb9, 0x6d, 0xd0, 0xc9, 0xb6, 0xab, 0x26, 0xac
2253     };
2254     const byte iv3[] =
2255     {
2256         0xe4, 0x4a, 0x42, 0x18, 0x8c, 0xae, 0x94, 0x92,
2257         0x6a, 0x9c, 0x26, 0xb0
2258     };
2259     const byte a3[] =
2260     {
2261         0x9d, 0xb9, 0x61, 0x68, 0xa6, 0x76, 0x7a, 0x31,
2262         0xf8, 0x29, 0xe4, 0x72, 0x61, 0x68, 0x3f, 0x8a
2263     };
2264     const byte t3[] =
2265     {
2266         0x23, 0xe2, 0x9f, 0x66, 0xe4, 0xc6, 0x52, 0x48
2267     };
2268
2269     byte tag[16];
2270
2271     memset(tag, 0, sizeof(tag));
2272     GmacSetKey(&gmac, k1, sizeof(k1));
2273     GmacUpdate(&gmac, iv1, sizeof(iv1), a1, sizeof(a1), tag, sizeof(t1));
2274     if (memcmp(t1, tag, sizeof(t1)) != 0)
2275         return -126;
2276
2277     memset(tag, 0, sizeof(tag));
2278     GmacSetKey(&gmac, k2, sizeof(k2));
2279     GmacUpdate(&gmac, iv2, sizeof(iv2), a2, sizeof(a2), tag, sizeof(t2));
2280     if (memcmp(t2, tag, sizeof(t2)) != 0)
2281         return -127;
2282
2283     memset(tag, 0, sizeof(tag));
2284     GmacSetKey(&gmac, k3, sizeof(k3));
2285     GmacUpdate(&gmac, iv3, sizeof(iv3), a3, sizeof(a3), tag, sizeof(t3));
2286     if (memcmp(t3, tag, sizeof(t3)) != 0)
2287         return -128;
2288
2289     return 0;
2290 }
2291 #endif /* HAVE_AESGCM */
2292
2293 #ifdef HAVE_AESCCM
2294 int aesccm_test(void)
2295 {
2296     Aes enc;
2297
2298     /* key */
2299     const byte k[] =
2300     {
2301         0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
2302         0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
2303     };
2304
2305     /* nonce */
2306     const byte iv[] =
2307     {
2308         0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
2309         0xa1, 0xa2, 0xa3, 0xa4, 0xa5
2310     };
2311
2312     /* plaintext */
2313     const byte p[] =
2314     {
2315         0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
2316         0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
2317         0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
2318     };
2319
2320     const byte a[] =
2321     {
2322         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
2323     };
2324
2325     const byte c[] =
2326     {
2327         0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
2328         0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
2329         0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
2330     };
2331
2332     const byte t[] =
2333     {
2334         0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
2335     };
2336
2337     byte t2[sizeof(t)];
2338     byte p2[sizeof(p)];
2339     byte c2[sizeof(c)];
2340
2341     int result;
2342
2343     memset(t2, 0, sizeof(t2));
2344     memset(c2, 0, sizeof(c2));
2345     memset(p2, 0, sizeof(p2));
2346
2347     AesCcmSetKey(&enc, k, sizeof(k));
2348     /* AES-CCM encrypt and decrypt both use AES encrypt internally */
2349     AesCcmEncrypt(&enc, c2, p, sizeof(c2), iv, sizeof(iv),
2350                                                  t2, sizeof(t2), a, sizeof(a));
2351     if (memcmp(c, c2, sizeof(c2)))
2352         return -107;
2353     if (memcmp(t, t2, sizeof(t2)))
2354         return -108;
2355
2356     result = AesCcmDecrypt(&enc, p2, c2, sizeof(p2), iv, sizeof(iv),
2357                                                  t2, sizeof(t2), a, sizeof(a));
2358     if (result != 0)
2359         return -109;
2360     if (memcmp(p, p2, sizeof(p2)))
2361         return -110;
2362
2363     /* Test the authentication failure */
2364     t2[0]++; /* Corrupt the authentication tag. */
2365     result = AesCcmDecrypt(&enc, p2, c, sizeof(p2), iv, sizeof(iv),
2366                                                  t2, sizeof(t2), a, sizeof(a));
2367     if (result == 0)
2368         return -111;
2369
2370     /* Clear c2 to compare against p2. p2 should be set to zero in case of
2371      * authentication fail. */
2372     memset(c2, 0, sizeof(c2));
2373     if (memcmp(p2, c2, sizeof(p2)))
2374         return -112;
2375
2376     return 0;
2377 }
2378 #endif /* HAVE_AESCCM */
2379
2380
2381 #endif /* NO_AES */
2382
2383
2384 #ifdef HAVE_CAMELLIA
2385
2386 enum {
2387     CAM_ECB_ENC, CAM_ECB_DEC, CAM_CBC_ENC, CAM_CBC_DEC
2388 };
2389
2390 typedef struct {
2391     int type;
2392     const byte* plaintext;
2393     const byte* iv;
2394     const byte* ciphertext;
2395     const byte* key;
2396     word32 keySz;
2397     int errorCode;
2398 } test_vector_t;
2399
2400 int camellia_test(void)
2401 {
2402     /* Camellia ECB Test Plaintext */
2403     static const byte pte[] =
2404     {
2405         0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
2406         0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
2407     };
2408
2409     /* Camellia ECB Test Initialization Vector */
2410     static const byte ive[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
2411
2412     /* Test 1: Camellia ECB 128-bit key */
2413     static const byte k1[] =
2414     {
2415         0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
2416         0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
2417     };
2418     static const byte c1[] =
2419     {
2420         0x67, 0x67, 0x31, 0x38, 0x54, 0x96, 0x69, 0x73,
2421         0x08, 0x57, 0x06, 0x56, 0x48, 0xea, 0xbe, 0x43
2422     };
2423
2424     /* Test 2: Camellia ECB 192-bit key */
2425     static const byte k2[] =
2426     {
2427         0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
2428         0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
2429         0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
2430     };
2431     static const byte c2[] =
2432     {
2433         0xb4, 0x99, 0x34, 0x01, 0xb3, 0xe9, 0x96, 0xf8,
2434         0x4e, 0xe5, 0xce, 0xe7, 0xd7, 0x9b, 0x09, 0xb9
2435     };
2436
2437     /* Test 3: Camellia ECB 256-bit key */
2438     static const byte k3[] =
2439     {
2440         0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
2441         0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
2442         0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
2443         0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
2444     };
2445     static const byte c3[] =
2446     {
2447         0x9a, 0xcc, 0x23, 0x7d, 0xff, 0x16, 0xd7, 0x6c,
2448         0x20, 0xef, 0x7c, 0x91, 0x9e, 0x3a, 0x75, 0x09
2449     };
2450
2451     /* Camellia CBC Test Plaintext */
2452     static const byte ptc[] =
2453     {
2454         0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
2455         0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
2456     };
2457
2458     /* Camellia CBC Test Initialization Vector */
2459     static const byte ivc[] =
2460     {
2461         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
2462         0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
2463     };
2464
2465     /* Test 4: Camellia-CBC 128-bit key */
2466     static const byte k4[] =
2467     {
2468         0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
2469         0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
2470     };
2471     static const byte c4[] =
2472     {
2473         0x16, 0x07, 0xCF, 0x49, 0x4B, 0x36, 0xBB, 0xF0,
2474         0x0D, 0xAE, 0xB0, 0xB5, 0x03, 0xC8, 0x31, 0xAB
2475     };
2476
2477     /* Test 5: Camellia-CBC 192-bit key */
2478     static const byte k5[] =
2479     {
2480         0x8E, 0x73, 0xB0, 0xF7, 0xDA, 0x0E, 0x64, 0x52,
2481         0xC8, 0x10, 0xF3, 0x2B, 0x80, 0x90, 0x79, 0xE5,
2482         0x62, 0xF8, 0xEA, 0xD2, 0x52, 0x2C, 0x6B, 0x7B
2483     };
2484     static const byte c5[] =
2485     {
2486         0x2A, 0x48, 0x30, 0xAB, 0x5A, 0xC4, 0xA1, 0xA2,
2487         0x40, 0x59, 0x55, 0xFD, 0x21, 0x95, 0xCF, 0x93
2488     };
2489
2490     /* Test 6: CBC 256-bit key */
2491     static const byte k6[] =
2492     {
2493         0x60, 0x3D, 0xEB, 0x10, 0x15, 0xCA, 0x71, 0xBE,
2494         0x2B, 0x73, 0xAE, 0xF0, 0x85, 0x7D, 0x77, 0x81,
2495         0x1F, 0x35, 0x2C, 0x07, 0x3B, 0x61, 0x08, 0xD7,
2496         0x2D, 0x98, 0x10, 0xA3, 0x09, 0x14, 0xDF, 0xF4
2497     };
2498     static const byte c6[] =
2499     {
2500         0xE6, 0xCF, 0xA3, 0x5F, 0xC0, 0x2B, 0x13, 0x4A,
2501         0x4D, 0x2C, 0x0B, 0x67, 0x37, 0xAC, 0x3E, 0xDA
2502     };
2503
2504     byte out[CAMELLIA_BLOCK_SIZE];
2505     Camellia cam;
2506     int i, testsSz;
2507     const test_vector_t testVectors[] =
2508     {
2509         {CAM_ECB_ENC, pte, ive, c1, k1, sizeof(k1), -114},
2510         {CAM_ECB_ENC, pte, ive, c2, k2, sizeof(k2), -115},
2511         {CAM_ECB_ENC, pte, ive, c3, k3, sizeof(k3), -116},
2512         {CAM_ECB_DEC, pte, ive, c1, k1, sizeof(k1), -117},
2513         {CAM_ECB_DEC, pte, ive, c2, k2, sizeof(k2), -118},
2514         {CAM_ECB_DEC, pte, ive, c3, k3, sizeof(k3), -119},
2515         {CAM_CBC_ENC, ptc, ivc, c4, k4, sizeof(k4), -120},
2516         {CAM_CBC_ENC, ptc, ivc, c5, k5, sizeof(k5), -121},
2517         {CAM_CBC_ENC, ptc, ivc, c6, k6, sizeof(k6), -122},
2518         {CAM_CBC_DEC, ptc, ivc, c4, k4, sizeof(k4), -123},
2519         {CAM_CBC_DEC, ptc, ivc, c5, k5, sizeof(k5), -124},
2520         {CAM_CBC_DEC, ptc, ivc, c6, k6, sizeof(k6), -125}
2521     };
2522
2523     testsSz = sizeof(testVectors)/sizeof(test_vector_t);
2524     for (i = 0; i < testsSz; i++) {
2525         if (CamelliaSetKey(&cam, testVectors[i].key, testVectors[i].keySz,
2526                                                         testVectors[i].iv) != 0)
2527             return testVectors[i].errorCode;
2528
2529         switch (testVectors[i].type) {
2530             case CAM_ECB_ENC:
2531                 CamelliaEncryptDirect(&cam, out, testVectors[i].plaintext);
2532                 if (memcmp(out, testVectors[i].ciphertext, CAMELLIA_BLOCK_SIZE))
2533                     return testVectors[i].errorCode;
2534                 break;
2535             case CAM_ECB_DEC:
2536                 CamelliaDecryptDirect(&cam, out, testVectors[i].ciphertext);
2537                 if (memcmp(out, testVectors[i].plaintext, CAMELLIA_BLOCK_SIZE))
2538                     return testVectors[i].errorCode;
2539                 break;
2540             case CAM_CBC_ENC:
2541                 CamelliaCbcEncrypt(&cam, out, testVectors[i].plaintext,
2542                                                            CAMELLIA_BLOCK_SIZE);
2543                 if (memcmp(out, testVectors[i].ciphertext, CAMELLIA_BLOCK_SIZE))
2544                     return testVectors[i].errorCode;
2545                 break;
2546             case CAM_CBC_DEC:
2547                 CamelliaCbcDecrypt(&cam, out, testVectors[i].ciphertext,
2548                                                            CAMELLIA_BLOCK_SIZE);
2549                 if (memcmp(out, testVectors[i].plaintext, CAMELLIA_BLOCK_SIZE))
2550                     return testVectors[i].errorCode;
2551                 break;
2552             default:
2553                 break;
2554         }
2555     }
2556
2557     /* Setting the IV and checking it was actually set. */
2558     CamelliaSetIV(&cam, ivc);
2559     if (XMEMCMP(cam.reg, ivc, CAMELLIA_BLOCK_SIZE))
2560         return -1;
2561
2562     /* Setting the IV to NULL should be same as all zeros IV */
2563     if (CamelliaSetIV(&cam, NULL) != 0 ||
2564                                     XMEMCMP(cam.reg, ive, CAMELLIA_BLOCK_SIZE))
2565         return -1;
2566
2567     /* First parameter should never be null */
2568     if (CamelliaSetIV(NULL, NULL) == 0)
2569         return -1;
2570
2571     /* First parameter should never be null, check it fails */
2572     if (CamelliaSetKey(NULL, k1, sizeof(k1), NULL) == 0)
2573         return -1;
2574
2575     /* Key should have a size of 16, 24, or 32 */
2576     if (CamelliaSetKey(&cam, k1, 0, NULL) == 0)
2577         return -1;
2578
2579     return 0;
2580 }
2581 #endif /* HAVE_CAMELLIA */
2582
2583
2584 int random_test(void)
2585 {
2586     RNG  rng;
2587     byte block[32];
2588     int ret;
2589
2590 #ifdef HAVE_CAVIUM
2591     ret = InitRngCavium(&rng, CAVIUM_DEV_ID);
2592     if (ret != 0) return -2007;
2593 #endif
2594     ret = InitRng(&rng);
2595     if (ret != 0) return -39;
2596
2597     ret = RNG_GenerateBlock(&rng, block, sizeof(block));
2598     if (ret != 0) return -40;
2599
2600     return 0;
2601 }
2602
2603
2604 #ifdef HAVE_NTRU
2605
2606 byte GetEntropy(ENTROPY_CMD cmd, byte* out);
2607
2608 byte GetEntropy(ENTROPY_CMD cmd, byte* out)
2609 {
2610     static RNG rng;
2611
2612     if (cmd == INIT)
2613         return (InitRng(&rng) == 0) ? 1 : 0;
2614
2615     if (out == NULL)
2616         return 0;
2617
2618     if (cmd == GET_BYTE_OF_ENTROPY)
2619         return (RNG_GenerateBlock(&rng, out, 1) == 0) ? 1 : 0;
2620
2621     if (cmd == GET_NUM_BYTES_PER_BYTE_OF_ENTROPY) {
2622         *out = 1;
2623         return 1;
2624     }
2625
2626     return 0;
2627 }
2628
2629 #endif /* HAVE_NTRU */
2630
2631 #ifndef NO_RSA
2632
2633 #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
2634     #ifdef FREESCALE_MQX
2635         static const char* clientKey  = "a:\\certs\\client-key.der";
2636         static const char* clientCert = "a:\\certs\\client-cert.der";
2637         #ifdef CYASSL_CERT_GEN
2638             static const char* caKeyFile  = "a:\\certs\\ca-key.der";
2639             static const char* caCertFile = "a:\\certs\\ca-cert.pem";
2640             #ifdef HAVE_ECC
2641                 static const char* eccCaKeyFile  = "a:\\certs\\ecc-key.der";
2642                 static const char* eccCaCertFile = "a:\\certs\\server-ecc.pem";
2643             #endif
2644         #endif
2645     #elif defined(CYASSL_MKD_SHELL)
2646         static char* clientKey = "certs/client-key.der";
2647         static char* clientCert = "certs/client-cert.der";
2648         void set_clientKey(char *key) {  clientKey = key ; }
2649         void set_clientCert(char *cert) {  clientCert = cert ; }
2650         #ifdef CYASSL_CERT_GEN
2651             static char* caKeyFile  = "certs/ca-key.der";
2652             static char* caCertFile = "certs/ca-cert.pem";
2653             void set_caKeyFile (char * key)  { caKeyFile   = key ; }
2654             void set_caCertFile(char * cert) { caCertFile = cert ; }
2655             #ifdef HAVE_ECC
2656                 static const char* eccCaKeyFile  = "certs/ecc-key.der";
2657                 static const char* eccCaCertFile = "certs/server-ecc.pem";
2658                 void set_eccCaKeyFile (char * key)  { eccCaKeyFile  = key ; }
2659                 void set_eccCaCertFile(char * cert) { eccCaCertFile = cert ; }
2660             #endif
2661         #endif
2662     #else
2663         static const char* clientKey  = "./certs/client-key.der";
2664         static const char* clientCert = "./certs/client-cert.der";
2665         #ifdef CYASSL_CERT_GEN
2666             static const char* caKeyFile  = "./certs/ca-key.der";
2667             static const char* caCertFile = "./certs/ca-cert.pem";
2668             #ifdef HAVE_ECC
2669                 static const char* eccCaKeyFile  = "./certs/ecc-key.der";
2670                 static const char* eccCaCertFile = "./certs/server-ecc.pem";
2671             #endif
2672         #endif
2673     #endif
2674 #endif
2675
2676
2677
2678 #define FOURK_BUF 4096
2679
2680 int rsa_test(void)
2681 {
2682     byte*   tmp;
2683     size_t bytes;
2684     RsaKey key;
2685     RNG    rng;
2686     word32 idx = 0;
2687     int    ret;
2688     byte   in[] = "Everyone gets Friday off.";
2689     word32 inLen = (word32)strlen((char*)in);
2690     byte   out[256];
2691     byte   plain[256];
2692 #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
2693     FILE*  file, * file2;
2694 #endif
2695 #ifdef CYASSL_TEST_CERT
2696     DecodedCert cert;
2697 #endif
2698
2699     tmp = (byte*)malloc(FOURK_BUF);
2700     if (tmp == NULL)
2701         return -40;
2702
2703 #ifdef USE_CERT_BUFFERS_1024
2704     XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
2705     bytes = sizeof_client_key_der_1024;
2706 #elif defined(USE_CERT_BUFFERS_2048)
2707     XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
2708     bytes = sizeof_client_key_der_2048;
2709 #else
2710     file = fopen(clientKey, "rb");
2711
2712     if (!file)
2713         err_sys("can't open ./certs/client-key.der, "
2714                 "Please run from CyaSSL home dir", -40);
2715
2716     bytes = fread(tmp, 1, FOURK_BUF, file);
2717     fclose(file);
2718 #endif /* USE_CERT_BUFFERS */
2719
2720 #ifdef HAVE_CAVIUM
2721     RsaInitCavium(&key, CAVIUM_DEV_ID);
2722 #endif
2723     ret = InitRsaKey(&key, 0);
2724     if (ret != 0) return -39;
2725     ret = RsaPrivateKeyDecode(tmp, &idx, &key, (word32)bytes);
2726     if (ret != 0) return -41;
2727
2728     ret = InitRng(&rng);
2729     if (ret != 0) return -42;
2730
2731     ret = RsaPublicEncrypt(in, inLen, out, sizeof(out), &key, &rng);
2732     if (ret < 0) return -43;
2733
2734     ret = RsaPrivateDecrypt(out, ret, plain, sizeof(plain), &key);
2735     if (ret < 0) return -44;
2736
2737     if (memcmp(plain, in, inLen)) return -45;
2738
2739     ret = RsaSSL_Sign(in, inLen, out, sizeof(out), &key, &rng);
2740     if (ret < 0) return -46;
2741
2742     memset(plain, 0, sizeof(plain));
2743     ret = RsaSSL_Verify(out, ret, plain, sizeof(plain), &key);
2744     if (ret < 0) return -47;
2745
2746     if (memcmp(plain, in, ret)) return -48;
2747
2748 #if defined(CYASSL_MDK_ARM)
2749     #define sizeof(s) strlen((char *)(s))
2750 #endif
2751
2752 #ifdef USE_CERT_BUFFERS_1024
2753     XMEMCPY(tmp, client_cert_der_1024, sizeof_client_cert_der_1024);
2754     bytes = sizeof_client_cert_der_1024;
2755 #elif defined(USE_CERT_BUFFERS_2048)
2756     XMEMCPY(tmp, client_cert_der_2048, sizeof_client_cert_der_2048);
2757     bytes = sizeof_client_cert_der_2048;
2758 #else
2759     file2 = fopen(clientCert, "rb");
2760     if (!file2)
2761         return -49;
2762
2763     bytes = fread(tmp, 1, FOURK_BUF, file2);
2764     fclose(file2);
2765 #endif
2766
2767 #ifdef sizeof
2768                 #undef sizeof
2769 #endif
2770
2771 #ifdef CYASSL_TEST_CERT
2772     InitDecodedCert(&cert, tmp, (word32)bytes, 0);
2773
2774     ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
2775     if (ret != 0) return -491;
2776
2777     FreeDecodedCert(&cert);
2778 #else
2779     (void)bytes;
2780 #endif
2781
2782
2783 #ifdef CYASSL_KEY_GEN
2784     {
2785         byte*  der;
2786         byte*  pem;
2787         int    derSz = 0;
2788         int    pemSz = 0;
2789         RsaKey derIn;
2790         RsaKey genKey;
2791         FILE* keyFile;
2792         FILE* pemFile;
2793
2794         ret = InitRsaKey(&genKey, 0);
2795         if (ret != 0)
2796             return -300;
2797         ret = MakeRsaKey(&genKey, 1024, 65537, &rng);
2798         if (ret != 0)
2799             return -301;
2800
2801         der = (byte*)malloc(FOURK_BUF);
2802         if (der == NULL) {
2803             FreeRsaKey(&genKey);
2804             return -307;
2805         }
2806         pem = (byte*)malloc(FOURK_BUF);
2807         if (pem == NULL) {
2808             free(der);
2809             FreeRsaKey(&genKey);
2810             return -308;
2811         }
2812
2813         derSz = RsaKeyToDer(&genKey, der, FOURK_BUF);
2814         if (derSz < 0) {
2815             free(der);
2816             free(pem);
2817             return -302;
2818         }
2819
2820         keyFile = fopen("./key.der", "wb");
2821         if (!keyFile) {
2822             free(der);
2823             free(pem);
2824             FreeRsaKey(&genKey);
2825             return -303;
2826         }
2827         ret = (int)fwrite(der, 1, derSz, keyFile);
2828         fclose(keyFile);
2829         if (ret != derSz) {
2830             free(der);
2831             free(pem);
2832             FreeRsaKey(&genKey);
2833             return -313;
2834         }
2835
2836         pemSz = DerToPem(der, derSz, pem, FOURK_BUF, PRIVATEKEY_TYPE);
2837         if (pemSz < 0) {
2838             free(der);
2839             free(pem);
2840             FreeRsaKey(&genKey);
2841             return -304;
2842         }
2843
2844         pemFile = fopen("./key.pem", "wb");
2845         if (!pemFile) {
2846             free(der);
2847             free(pem);
2848             FreeRsaKey(&genKey);
2849             return -305;
2850         }
2851         ret = (int)fwrite(pem, 1, pemSz, pemFile);
2852         fclose(pemFile);
2853         if (ret != pemSz) {
2854             free(der);
2855             free(pem);
2856             FreeRsaKey(&genKey);
2857             return -314;
2858         }
2859
2860         ret = InitRsaKey(&derIn, 0);
2861         if (ret != 0) {
2862             free(der);
2863             free(pem);
2864             FreeRsaKey(&genKey);
2865             return -3060;
2866         }
2867         idx = 0;
2868         ret = RsaPrivateKeyDecode(der, &idx, &derIn, derSz);
2869         if (ret != 0) {
2870             free(der);
2871             free(pem);
2872             FreeRsaKey(&derIn);
2873             FreeRsaKey(&genKey);
2874             return -306;
2875         }
2876
2877         FreeRsaKey(&derIn);
2878         FreeRsaKey(&genKey);
2879         free(pem);
2880         free(der);
2881     }
2882 #endif /* CYASSL_KEY_GEN */
2883
2884
2885 #ifdef CYASSL_CERT_GEN
2886     /* self signed */
2887     {
2888         Cert        myCert;
2889         byte*       derCert;
2890         byte*       pem;
2891         FILE*       derFile;
2892         FILE*       pemFile;
2893         int         certSz;
2894         int         pemSz;
2895 #ifdef CYASSL_TEST_CERT
2896         DecodedCert decode;
2897 #endif
2898
2899         derCert = (byte*)malloc(FOURK_BUF);
2900         if (derCert == NULL)
2901             return -309;
2902         pem = (byte*)malloc(FOURK_BUF);
2903         if (pem == NULL) {
2904             free(derCert);
2905             return -310;
2906         }
2907
2908         InitCert(&myCert);
2909
2910         strncpy(myCert.subject.country, "US", CTC_NAME_SIZE);
2911         strncpy(myCert.subject.state, "OR", CTC_NAME_SIZE);
2912         strncpy(myCert.subject.locality, "Portland", CTC_NAME_SIZE);
2913         strncpy(myCert.subject.org, "yaSSL", CTC_NAME_SIZE);
2914         strncpy(myCert.subject.unit, "Development", CTC_NAME_SIZE);
2915         strncpy(myCert.subject.commonName, "www.yassl.com", CTC_NAME_SIZE);
2916         strncpy(myCert.subject.email, "info@yassl.com", CTC_NAME_SIZE);
2917         myCert.isCA    = 1;
2918         myCert.sigType = CTC_SHA256wRSA;
2919
2920         certSz = MakeSelfCert(&myCert, derCert, FOURK_BUF, &key, &rng);
2921         if (certSz < 0) {
2922             free(derCert);
2923             free(pem);
2924             return -401;
2925         }
2926
2927 #ifdef CYASSL_TEST_CERT
2928         InitDecodedCert(&decode, derCert, certSz, 0);
2929         ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
2930         if (ret != 0) {
2931             free(derCert);
2932             free(pem);
2933             return -402;
2934         }
2935         FreeDecodedCert(&decode);
2936 #endif
2937         derFile = fopen("./cert.der", "wb");
2938         if (!derFile) {
2939             free(derCert);
2940             free(pem);
2941             return -403;
2942         }
2943         ret = (int)fwrite(derCert, 1, certSz, derFile);
2944         fclose(derFile);
2945         if (ret != certSz) {
2946             free(derCert);
2947             free(pem);
2948             return -414;
2949         }
2950
2951         pemSz = DerToPem(derCert, certSz, pem, FOURK_BUF, CERT_TYPE);
2952         if (pemSz < 0) {
2953             free(derCert);
2954             free(pem);
2955             return -404;
2956         }
2957
2958         pemFile = fopen("./cert.pem", "wb");
2959         if (!pemFile) {
2960             free(derCert);
2961             free(pem);
2962             return -405;
2963         }
2964         ret = (int)fwrite(pem, 1, pemSz, pemFile);
2965         fclose(pemFile);
2966         if (ret != pemSz) {
2967             free(derCert);
2968             free(pem);
2969             return -406;
2970         }
2971         free(pem);
2972         free(derCert);
2973     }
2974     /* CA style */
2975     {
2976         RsaKey      caKey;
2977         Cert        myCert;
2978         byte*       derCert;
2979         byte*       pem;
2980         FILE*       derFile;
2981         FILE*       pemFile;
2982         int         certSz;
2983         int         pemSz;
2984         size_t      bytes3;
2985         word32      idx3 = 0;
2986                           FILE* file3 ;
2987 #ifdef CYASSL_TEST_CERT
2988         DecodedCert decode;
2989 #endif
2990
2991         derCert = (byte*)malloc(FOURK_BUF);
2992         if (derCert == NULL)
2993             return -311;
2994         pem = (byte*)malloc(FOURK_BUF);
2995         if (pem == NULL) {
2996             free(derCert);
2997             return -312;
2998         }
2999
3000         file3 = fopen(caKeyFile, "rb");
3001
3002         if (!file3) {
3003             free(derCert);
3004             free(pem);
3005             return -412;
3006         }
3007
3008         bytes3 = fread(tmp, 1, FOURK_BUF, file3);
3009         fclose(file3);
3010
3011         ret = InitRsaKey(&caKey, 0);
3012         if (ret != 0) {
3013             free(derCert);
3014             free(pem);
3015             return -411;
3016         }
3017         ret = RsaPrivateKeyDecode(tmp, &idx3, &caKey, (word32)bytes3);
3018         if (ret != 0) {
3019             free(derCert);
3020             free(pem);
3021             FreeRsaKey(&caKey);
3022             return -413;
3023         }
3024
3025         InitCert(&myCert);
3026
3027         strncpy(myCert.subject.country, "US", CTC_NAME_SIZE);
3028         strncpy(myCert.subject.state, "OR", CTC_NAME_SIZE);
3029         strncpy(myCert.subject.locality, "Portland", CTC_NAME_SIZE);
3030         strncpy(myCert.subject.org, "yaSSL", CTC_NAME_SIZE);
3031         strncpy(myCert.subject.unit, "Development", CTC_NAME_SIZE);
3032         strncpy(myCert.subject.commonName, "www.yassl.com", CTC_NAME_SIZE);
3033         strncpy(myCert.subject.email, "info@yassl.com", CTC_NAME_SIZE);
3034
3035         ret = SetIssuer(&myCert, caCertFile);
3036         if (ret < 0) {
3037             free(derCert);
3038             free(pem);
3039             FreeRsaKey(&caKey);
3040             return -405;
3041         }
3042
3043         certSz = MakeCert(&myCert, derCert, FOURK_BUF, &key, NULL, &rng);
3044         if (certSz < 0) {
3045             free(derCert);
3046             free(pem);
3047             FreeRsaKey(&caKey);
3048             return -407;
3049         }
3050
3051         certSz = SignCert(myCert.bodySz, myCert.sigType, derCert, FOURK_BUF,
3052                           &caKey, NULL, &rng);
3053         if (certSz < 0) {
3054             free(derCert);
3055             free(pem);
3056             FreeRsaKey(&caKey);
3057             return -408;
3058         }
3059
3060
3061 #ifdef CYASSL_TEST_CERT
3062         InitDecodedCert(&decode, derCert, certSz, 0);
3063         ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
3064         if (ret != 0) {
3065             free(derCert);
3066             free(pem);
3067             FreeRsaKey(&caKey);
3068             return -409;
3069         }
3070         FreeDecodedCert(&decode);
3071 #endif
3072
3073         derFile = fopen("./othercert.der", "wb");
3074         if (!derFile) {
3075             free(derCert);
3076             free(pem);
3077             FreeRsaKey(&caKey);
3078             return -410;
3079         }
3080         ret = (int)fwrite(derCert, 1, certSz, derFile);
3081         fclose(derFile);
3082         if (ret != certSz) {
3083             free(derCert);
3084             free(pem);
3085             FreeRsaKey(&caKey);
3086             return -416;
3087         }
3088
3089         pemSz = DerToPem(derCert, certSz, pem, FOURK_BUF, CERT_TYPE);
3090         if (pemSz < 0) {
3091             free(derCert);
3092             free(pem);
3093             FreeRsaKey(&caKey);
3094             return -411;
3095         }
3096
3097         pemFile = fopen("./othercert.pem", "wb");
3098         if (!pemFile) {
3099             free(derCert);
3100             free(pem);
3101             FreeRsaKey(&caKey);
3102             return -412;
3103         }
3104         ret = (int)fwrite(pem, 1, pemSz, pemFile);
3105         if (ret != pemSz) {
3106             free(derCert);
3107             free(pem);
3108             FreeRsaKey(&caKey);
3109             return -415;
3110         }
3111         fclose(pemFile);
3112         free(pem);
3113         free(derCert);
3114         FreeRsaKey(&caKey);
3115     }
3116 #ifdef HAVE_ECC
3117     /* ECC CA style */
3118     {
3119         ecc_key     caKey;
3120         Cert        myCert;
3121         byte*       derCert;
3122         byte*       pem;
3123         FILE*       derFile;
3124         FILE*       pemFile;
3125         int         certSz;
3126         int         pemSz;
3127         size_t      bytes3;
3128         word32      idx3 = 0;
3129         FILE*       file3;
3130 #ifdef CYASSL_TEST_CERT
3131         DecodedCert decode;
3132 #endif
3133
3134         derCert = (byte*)malloc(FOURK_BUF);
3135         if (derCert == NULL)
3136             return -5311;
3137         pem = (byte*)malloc(FOURK_BUF);
3138         if (pem == NULL) {
3139             free(derCert);
3140             return -5312;
3141         }
3142
3143         file3 = fopen(eccCaKeyFile, "rb");
3144
3145         if (!file3) {
3146             free(derCert);
3147             free(pem);
3148             return -5412;
3149         }
3150
3151         bytes3 = fread(tmp, 1, FOURK_BUF, file3);
3152         fclose(file3);
3153
3154         ecc_init(&caKey);
3155         ret = EccPrivateKeyDecode(tmp, &idx3, &caKey, (word32)bytes3);
3156         if (ret != 0) {
3157             free(derCert);
3158             free(pem);
3159             return -5413;
3160         }
3161
3162         InitCert(&myCert);
3163         myCert.sigType = CTC_SHA256wECDSA;
3164
3165         strncpy(myCert.subject.country, "US", CTC_NAME_SIZE);
3166         strncpy(myCert.subject.state, "OR", CTC_NAME_SIZE);
3167         strncpy(myCert.subject.locality, "Portland", CTC_NAME_SIZE);
3168         strncpy(myCert.subject.org, "wolfSSL", CTC_NAME_SIZE);
3169         strncpy(myCert.subject.unit, "Development", CTC_NAME_SIZE);
3170         strncpy(myCert.subject.commonName, "www.wolfssl.com", CTC_NAME_SIZE);
3171         strncpy(myCert.subject.email, "info@wolfssl.com", CTC_NAME_SIZE);
3172
3173         ret = SetIssuer(&myCert, eccCaCertFile);
3174         if (ret < 0) {
3175             free(pem);
3176             free(derCert);
3177             ecc_free(&caKey);
3178             return -5405;
3179         }
3180
3181         certSz = MakeCert(&myCert, derCert, FOURK_BUF, NULL, &caKey, &rng);
3182         if (certSz < 0) {
3183             free(pem);
3184             free(derCert);
3185             ecc_free(&caKey);
3186             return -5407;
3187         }
3188
3189         certSz = SignCert(myCert.bodySz, myCert.sigType, derCert, FOURK_BUF,
3190                           NULL, &caKey, &rng);
3191         if (certSz < 0) {
3192             free(pem);
3193             free(derCert);
3194             ecc_free(&caKey);
3195             return -5408;
3196         }
3197
3198 #ifdef CYASSL_TEST_CERT
3199         InitDecodedCert(&decode, derCert, certSz, 0);
3200         ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
3201         if (ret != 0) {
3202             free(pem);
3203             free(derCert);
3204             ecc_free(&caKey);
3205             return -5409;
3206         }
3207         FreeDecodedCert(&decode);
3208 #endif
3209
3210         derFile = fopen("./certecc.der", "wb");
3211         if (!derFile) {
3212             free(pem);
3213             free(derCert);
3214             ecc_free(&caKey);
3215             return -5410;
3216         }
3217         ret = (int)fwrite(derCert, 1, certSz, derFile);
3218         fclose(derFile);
3219         if (ret != certSz) {
3220             free(pem);
3221             free(derCert);
3222             ecc_free(&caKey);
3223             return -5414;
3224         }
3225
3226         pemSz = DerToPem(derCert, certSz, pem, FOURK_BUF, CERT_TYPE);
3227         if (pemSz < 0) {
3228             free(pem);
3229             free(derCert);
3230             ecc_free(&caKey);
3231             return -5411;
3232         }
3233
3234         pemFile = fopen("./certecc.pem", "wb");
3235         if (!pemFile) {
3236             free(pem);
3237             free(derCert);
3238             ecc_free(&caKey);
3239             return -5412;
3240         }
3241         ret = (int)fwrite(pem, 1, pemSz, pemFile);
3242         if (ret != pemSz) {
3243             free(pem);
3244             free(derCert);
3245             ecc_free(&caKey);
3246             return -5415;
3247         }
3248         fclose(pemFile);
3249         free(pem);
3250         free(derCert);
3251         ecc_free(&caKey);
3252     }
3253 #endif /* HAVE_ECC */
3254 #ifdef HAVE_NTRU
3255     {
3256         RsaKey      caKey;
3257         Cert        myCert;
3258         byte*       derCert;
3259         byte*       pem;
3260         FILE*       derFile;
3261         FILE*       pemFile;
3262         FILE*       caFile;
3263         FILE*       ntruPrivFile;
3264         int         certSz;
3265         int         pemSz;
3266         word32      idx3;
3267 #ifdef CYASSL_TEST_CERT
3268         DecodedCert decode;
3269 #endif
3270         derCert = (byte*)malloc(FOURK_BUF);
3271         if (derCert == NULL)
3272             return -311;
3273         pem = (byte*)malloc(FOURK_BUF);
3274         if (pem == NULL) {
3275             free(derCert);
3276             return -312;
3277         }
3278
3279         byte   public_key[557];          /* sized for EES401EP2 */
3280         word16 public_key_len;           /* no. of octets in public key */
3281         byte   private_key[607];         /* sized for EES401EP2 */
3282         word16 private_key_len;          /* no. of octets in private key */
3283         DRBG_HANDLE drbg;
3284         static uint8_t const pers_str[] = {
3285                 'C', 'y', 'a', 'S', 'S', 'L', ' ', 't', 'e', 's', 't'
3286         };
3287         word32 rc = crypto_drbg_instantiate(112, pers_str, sizeof(pers_str),
3288                                             GetEntropy, &drbg);
3289         if (rc != DRBG_OK) {
3290             free(derCert);
3291             free(pem);
3292             return -450;
3293         }
3294
3295         rc = crypto_ntru_encrypt_keygen(drbg, NTRU_EES401EP2, &public_key_len,
3296                                         NULL, &private_key_len, NULL);
3297         if (rc != NTRU_OK) {
3298             free(derCert);
3299             free(pem);
3300             return -451;
3301         }
3302
3303         rc = crypto_ntru_encrypt_keygen(drbg, NTRU_EES401EP2, &public_key_len,
3304                                      public_key, &private_key_len, private_key);
3305         crypto_drbg_uninstantiate(drbg);
3306
3307         if (rc != NTRU_OK) {
3308             free(derCert);
3309             free(pem);
3310             return -452;
3311         }
3312
3313         caFile = fopen(caKeyFile, "rb");
3314
3315         if (!caFile) {
3316             free(derCert);
3317             free(pem);
3318             return -453;
3319         }
3320
3321         bytes = fread(tmp, 1, FOURK_BUF, caFile);
3322         fclose(caFile);
3323
3324         ret = InitRsaKey(&caKey, 0);
3325         if (ret != 0) {
3326             free(derCert);
3327             free(pem);
3328             return -459;
3329         }
3330         ret = RsaPrivateKeyDecode(tmp, &idx3, &caKey, (word32)bytes);
3331         if (ret != 0) {
3332             free(derCert);
3333             free(pem);
3334             return -454;
3335         }
3336
3337         InitCert(&myCert);
3338
3339         strncpy(myCert.subject.country, "US", CTC_NAME_SIZE);
3340         strncpy(myCert.subject.state, "OR", CTC_NAME_SIZE);
3341         strncpy(myCert.subject.locality, "Portland", CTC_NAME_SIZE);
3342         strncpy(myCert.subject.org, "yaSSL", CTC_NAME_SIZE);
3343         strncpy(myCert.subject.unit, "Development", CTC_NAME_SIZE);
3344         strncpy(myCert.subject.commonName, "www.yassl.com", CTC_NAME_SIZE);
3345         strncpy(myCert.subject.email, "info@yassl.com", CTC_NAME_SIZE);
3346
3347         ret = SetIssuer(&myCert, caCertFile);
3348         if (ret < 0) {
3349             free(derCert);
3350             free(pem);
3351             FreeRsaKey(&caKey);
3352             return -455;
3353         }
3354
3355         certSz = MakeNtruCert(&myCert, derCert, FOURK_BUF, public_key,
3356                               public_key_len, &rng);
3357         if (certSz < 0) {
3358             free(derCert);
3359             free(pem);
3360             FreeRsaKey(&caKey);
3361             return -456;
3362         }
3363
3364         certSz = SignCert(myCert.bodySz, myCert.sigType, derCert, FOURK_BUF,
3365                           &caKey, NULL, &rng);
3366         FreeRsaKey(&caKey);
3367         if (certSz < 0) {
3368             free(derCert);
3369             free(pem);
3370             return -457;
3371         }
3372
3373
3374 #ifdef CYASSL_TEST_CERT
3375         InitDecodedCert(&decode, derCert, certSz, 0);
3376         ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
3377         if (ret != 0) {
3378             free(derCert);
3379             free(pem);
3380             return -458;
3381         }
3382         FreeDecodedCert(&decode);
3383 #endif
3384         derFile = fopen("./ntru-cert.der", "wb");
3385         if (!derFile) {
3386             free(derCert);
3387             free(pem);
3388             return -459;
3389         }
3390         ret = (int)fwrite(derCert, 1, certSz, derFile);
3391         fclose(derFile);
3392         if (ret != certSz) {
3393             free(derCert);
3394             free(pem);
3395             return -473;
3396         }
3397
3398         pemSz = DerToPem(derCert, certSz, pem, FOURK_BUF, CERT_TYPE);
3399         if (pemSz < 0) {
3400             free(derCert);
3401             free(pem);
3402             return -460;
3403         }
3404
3405         pemFile = fopen("./ntru-cert.pem", "wb");
3406         if (!pemFile) {
3407             free(derCert);
3408             free(pem);
3409             return -461;
3410         }
3411         ret = (int)fwrite(pem, 1, pemSz, pemFile);
3412         fclose(pemFile);
3413         if (ret != pemSz) {
3414             free(derCert);
3415             free(pem);
3416             return -474;
3417         }
3418
3419         ntruPrivFile = fopen("./ntru-key.raw", "wb");
3420         if (!ntruPrivFile) {
3421             free(derCert);
3422             free(pem);
3423             return -462;
3424         }
3425         ret = (int)fwrite(private_key, 1, private_key_len, ntruPrivFile);
3426         fclose(ntruPrivFile);
3427         if (ret != private_key_len) {
3428             free(pem);
3429             free(derCert);
3430             return -475;
3431         }
3432         free(pem);
3433         free(derCert);
3434     }
3435 #endif /* HAVE_NTRU */
3436 #ifdef CYASSL_CERT_REQ
3437     {
3438         Cert        req;
3439         byte*       der;
3440         byte*       pem;
3441         int         derSz;
3442         int         pemSz;
3443         FILE*       reqFile;
3444
3445         der = (byte*)malloc(FOURK_BUF);
3446         if (der == NULL)
3447             return -463;
3448         pem = (byte*)malloc(FOURK_BUF);
3449         if (pem == NULL) {
3450             free(der);
3451             return -464;
3452         }
3453
3454         InitCert(&req);
3455
3456         req.version = 0;
3457         req.isCA    = 1;
3458         strncpy(req.challengePw, "yassl123", CTC_NAME_SIZE);
3459         strncpy(req.subject.country, "US", CTC_NAME_SIZE);
3460         strncpy(req.subject.state, "OR", CTC_NAME_SIZE);
3461         strncpy(req.subject.locality, "Portland", CTC_NAME_SIZE);
3462         strncpy(req.subject.org, "yaSSL", CTC_NAME_SIZE);
3463         strncpy(req.subject.unit, "Development", CTC_NAME_SIZE);
3464         strncpy(req.subject.commonName, "www.yassl.com", CTC_NAME_SIZE);
3465         strncpy(req.subject.email, "info@yassl.com", CTC_NAME_SIZE);
3466         req.sigType = CTC_SHA256wRSA;
3467
3468         derSz = MakeCertReq(&req, der, FOURK_BUF, &key, NULL);
3469         if (derSz < 0) {
3470             free(pem);
3471             free(der);
3472             return -465;
3473         }
3474
3475         derSz = SignCert(req.bodySz, req.sigType, der, FOURK_BUF,
3476                           &key, NULL, &rng);
3477         if (derSz < 0) {
3478             free(pem);
3479             free(der);
3480             return -466;
3481         }
3482
3483         pemSz = DerToPem(der, derSz, pem, FOURK_BUF, CERTREQ_TYPE);
3484         if (pemSz < 0) {
3485             free(pem);
3486             free(der);
3487             return -467;
3488         }
3489
3490         reqFile = fopen("./certreq.der", "wb");
3491         if (!reqFile) {
3492             free(pem);
3493             free(der);
3494             return -468;
3495         }
3496
3497         ret = (int)fwrite(der, 1, derSz, reqFile);
3498         fclose(reqFile);
3499         if (ret != derSz) {
3500             free(pem);
3501             free(der);
3502             return -471;
3503         }
3504
3505         reqFile = fopen("./certreq.pem", "wb");
3506         if (!reqFile) {
3507             free(pem);
3508             free(der);
3509             return -469;
3510         }
3511         ret = (int)fwrite(pem, 1, pemSz, reqFile);
3512         fclose(reqFile);
3513         if (ret != pemSz) {
3514             free(pem);
3515             free(der);
3516             return -470;
3517         }
3518
3519         free(pem);
3520         free(der);
3521     }
3522 #endif /* CYASSL_CERT_REQ */
3523 #endif /* CYASSL_CERT_GEN */
3524
3525     FreeRsaKey(&key);
3526 #ifdef HAVE_CAVIUM
3527     RsaFreeCavium(&key);
3528 #endif
3529     free(tmp);
3530
3531     return 0;
3532 }
3533
3534 #endif
3535
3536
3537 #ifndef NO_DH
3538
3539 #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
3540     #ifdef FREESCALE_MQX
3541         static const char* dhKey = "a:\certs\\dh2048.der";
3542     #else
3543         static const char* dhKey = "./certs/dh2048.der";
3544     #endif
3545 #endif
3546
3547 int dh_test(void)
3548 {
3549     int    ret;
3550     word32 bytes;
3551     word32 idx = 0, privSz, pubSz, privSz2, pubSz2, agreeSz, agreeSz2;
3552     byte   tmp[1024];
3553     byte   priv[256];
3554     byte   pub[256];
3555     byte   priv2[256];
3556     byte   pub2[256];
3557     byte   agree[256];
3558     byte   agree2[256];
3559     DhKey  key;
3560     DhKey  key2;
3561     RNG    rng;
3562
3563
3564 #ifdef USE_CERT_BUFFERS_1024
3565     XMEMCPY(tmp, dh_key_der_1024, sizeof_dh_key_der_1024);
3566     bytes = sizeof_dh_key_der_1024;
3567 #elif defined(USE_CERT_BUFFERS_2048)
3568     XMEMCPY(tmp, dh_key_der_2048, sizeof_dh_key_der_2048);
3569     bytes = sizeof_dh_key_der_2048;
3570 #else
3571     FILE*  file = fopen(dhKey, "rb");
3572
3573     if (!file)
3574         return -50;
3575
3576     bytes = (word32) fread(tmp, 1, sizeof(tmp), file);
3577     fclose(file);
3578 #endif /* USE_CERT_BUFFERS */
3579
3580     InitDhKey(&key);
3581     InitDhKey(&key2);
3582     ret = DhKeyDecode(tmp, &idx, &key, bytes);
3583     if (ret != 0)
3584         return -51;
3585
3586     idx = 0;
3587     ret = DhKeyDecode(tmp, &idx, &key2, bytes);
3588     if (ret != 0)
3589         return -52;
3590
3591     ret = InitRng(&rng);
3592     if (ret != 0)
3593         return -53;
3594
3595     ret =  DhGenerateKeyPair(&key, &rng, priv, &privSz, pub, &pubSz);
3596     ret += DhGenerateKeyPair(&key2, &rng, priv2, &privSz2, pub2, &pubSz2);
3597     if (ret != 0)
3598         return -54;
3599
3600     ret =  DhAgree(&key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
3601     ret += DhAgree(&key2, agree2, &agreeSz2, priv2, privSz2, pub, pubSz);
3602     if (ret != 0)
3603         return -55;
3604
3605     if (memcmp(agree, agree2, agreeSz))
3606         return -56;
3607
3608     FreeDhKey(&key);
3609     FreeDhKey(&key2);
3610
3611     return 0;
3612 }
3613
3614 #endif /* NO_DH */
3615
3616
3617 #ifndef NO_DSA
3618
3619 #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
3620     #ifdef FREESCALE_MQX
3621         static const char* dsaKey = "a:\\certs\\dsa2048.der";
3622     #else
3623         static const char* dsaKey = "./certs/dsa2048.der";
3624     #endif
3625 #endif
3626
3627 int dsa_test(void)
3628 {
3629     int    ret, answer;
3630     word32 bytes;
3631     word32 idx = 0;
3632     byte   tmp[1024];
3633     DsaKey key;
3634     RNG    rng;
3635     Sha    sha;
3636     byte   hash[SHA_DIGEST_SIZE];
3637     byte   signature[40];
3638
3639
3640 #ifdef USE_CERT_BUFFERS_1024
3641     XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
3642     bytes = sizeof_dsa_key_der_1024;
3643 #elif defined(USE_CERT_BUFFERS_2048)
3644     XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
3645     bytes = sizeof_dsa_key_der_2048;
3646 #else
3647     FILE*  file = fopen(dsaKey, "rb");
3648
3649     if (!file)
3650         return -60;
3651
3652     bytes = (word32) fread(tmp, 1, sizeof(tmp), file);
3653     fclose(file);
3654 #endif /* USE_CERT_BUFFERS */
3655
3656     ret = InitSha(&sha);
3657     if (ret != 0)
3658         return -4002;
3659     ShaUpdate(&sha, tmp, bytes);
3660     ShaFinal(&sha, hash);
3661
3662     InitDsaKey(&key);
3663     ret = DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
3664     if (ret != 0) return -61;
3665
3666     ret = InitRng(&rng);
3667     if (ret != 0) return -62;
3668
3669     ret = DsaSign(hash, signature, &key, &rng);
3670     if (ret != 0) return -63;
3671
3672     ret = DsaVerify(hash, signature, &key, &answer);
3673     if (ret != 0) return -64;
3674     if (answer != 1) return -65;
3675
3676     FreeDsaKey(&key);
3677
3678     return 0;
3679 }
3680
3681 #endif /* NO_DSA */
3682
3683
3684 #ifdef OPENSSL_EXTRA
3685
3686 int openssl_test(void)
3687 {
3688     EVP_MD_CTX md_ctx;
3689     testVector a, b, c, d, e, f;
3690     byte       hash[SHA_DIGEST_SIZE*4];  /* max size */
3691
3692     (void)e;
3693     (void)f;
3694
3695     a.input  = "1234567890123456789012345678901234567890123456789012345678"
3696                "9012345678901234567890";
3697     a.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
3698                "\x7a";
3699     a.inLen  = strlen(a.input);
3700     a.outLen = MD5_DIGEST_SIZE;
3701
3702     EVP_MD_CTX_init(&md_ctx);
3703     EVP_DigestInit(&md_ctx, EVP_md5());
3704
3705     EVP_DigestUpdate(&md_ctx, a.input, a.inLen);
3706     EVP_DigestFinal(&md_ctx, hash, 0);
3707
3708     if (memcmp(hash, a.output, MD5_DIGEST_SIZE) != 0)
3709         return -71;
3710
3711     b.input  = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
3712                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
3713                "aaaaaaaaaa";
3714     b.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
3715                "\x53\x99\x5E\x26\xA0";
3716     b.inLen  = strlen(b.input);
3717     b.outLen = SHA_DIGEST_SIZE;
3718
3719     EVP_MD_CTX_init(&md_ctx);
3720     EVP_DigestInit(&md_ctx, EVP_sha1());
3721
3722     EVP_DigestUpdate(&md_ctx, b.input, b.inLen);
3723     EVP_DigestFinal(&md_ctx, hash, 0);
3724
3725     if (memcmp(hash, b.output, SHA_DIGEST_SIZE) != 0)
3726         return -72;
3727
3728
3729     d.input  = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
3730     d.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
3731                "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
3732                "\x06\xC1";
3733     d.inLen  = strlen(d.input);
3734     d.outLen = SHA256_DIGEST_SIZE;
3735
3736     EVP_MD_CTX_init(&md_ctx);
3737     EVP_DigestInit(&md_ctx, EVP_sha256());
3738
3739     EVP_DigestUpdate(&md_ctx, d.input, d.inLen);
3740     EVP_DigestFinal(&md_ctx, hash, 0);
3741
3742     if (memcmp(hash, d.output, SHA256_DIGEST_SIZE) != 0)
3743         return -78;
3744
3745 #ifdef CYASSL_SHA384
3746
3747     e.input  = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
3748                "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
3749     e.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
3750                "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
3751                "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
3752                "\x74\x60\x39";
3753     e.inLen  = strlen(e.input);
3754     e.outLen = SHA384_DIGEST_SIZE;
3755
3756     EVP_MD_CTX_init(&md_ctx);
3757     EVP_DigestInit(&md_ctx, EVP_sha384());
3758
3759     EVP_DigestUpdate(&md_ctx, e.input, e.inLen);
3760     EVP_DigestFinal(&md_ctx, hash, 0);
3761
3762     if (memcmp(hash, e.output, SHA384_DIGEST_SIZE) != 0)
3763         return -79;
3764
3765 #endif /* CYASSL_SHA384 */
3766
3767
3768 #ifdef CYASSL_SHA512
3769
3770     f.input  = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
3771                "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
3772     f.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
3773                "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
3774                "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
3775                "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
3776                "\x87\x4b\xe9\x09";
3777     f.inLen  = strlen(f.input);
3778     f.outLen = SHA512_DIGEST_SIZE;
3779
3780     EVP_MD_CTX_init(&md_ctx);
3781     EVP_DigestInit(&md_ctx, EVP_sha512());
3782
3783     EVP_DigestUpdate(&md_ctx, f.input, f.inLen);
3784     EVP_DigestFinal(&md_ctx, hash, 0);
3785
3786     if (memcmp(hash, f.output, SHA512_DIGEST_SIZE) != 0)
3787         return -80;
3788
3789 #endif /* CYASSL_SHA512 */
3790
3791
3792     if (RAND_bytes(hash, sizeof(hash)) != 1)
3793         return -73;
3794
3795     c.input  = "what do ya want for nothing?";
3796     c.output = "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7"
3797                "\x38";
3798     c.inLen  = strlen(c.input);
3799     c.outLen = MD5_DIGEST_SIZE;
3800
3801     HMAC(EVP_md5(), "Jefe", 4, (byte*)c.input, (int)c.inLen, hash, 0);
3802
3803     if (memcmp(hash, c.output, MD5_DIGEST_SIZE) != 0)
3804         return -74;
3805
3806     { /* des test */
3807     const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
3808         0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
3809         0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
3810         0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
3811     };
3812
3813     byte plain[24];
3814     byte cipher[24];
3815
3816     const_DES_cblock key =
3817     {
3818         0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
3819     };
3820
3821     DES_cblock iv =
3822     {
3823         0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
3824     };
3825
3826     DES_key_schedule sched;
3827
3828     const byte verify[] =
3829     {
3830         0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
3831         0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
3832         0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
3833     };
3834
3835     DES_key_sched(&key, &sched);
3836
3837     DES_cbc_encrypt(vector, cipher, sizeof(vector), &sched, &iv, DES_ENCRYPT);
3838     DES_cbc_encrypt(cipher, plain, sizeof(vector), &sched, &iv, DES_DECRYPT);
3839
3840     if (memcmp(plain, vector, sizeof(vector)) != 0)
3841         return -75;
3842
3843     if (memcmp(cipher, verify, sizeof(verify)) != 0)
3844         return -76;
3845
3846         /* test changing iv */
3847     DES_ncbc_encrypt(vector, cipher, 8, &sched, &iv, DES_ENCRYPT);
3848     DES_ncbc_encrypt(vector + 8, cipher + 8, 16, &sched, &iv, DES_ENCRYPT);
3849
3850     if (memcmp(cipher, verify, sizeof(verify)) != 0)
3851         return -77;
3852
3853     }  /* end des test */
3854
3855     {  /* evp_cipher test */
3856         EVP_CIPHER_CTX ctx;
3857
3858
3859         const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
3860             0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
3861             0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
3862             0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
3863         };
3864
3865         const byte verify[] =
3866         {
3867             0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
3868             0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
3869         };
3870
3871         byte key[] = "0123456789abcdef   ";  /* align */
3872         byte iv[]  = "1234567890abcdef   ";  /* align */
3873
3874         byte cipher[AES_BLOCK_SIZE * 4];
3875         byte plain [AES_BLOCK_SIZE * 4];
3876
3877         EVP_CIPHER_CTX_init(&ctx);
3878         if (EVP_CipherInit(&ctx, EVP_aes_128_cbc(), key, iv, 1) == 0)
3879             return -81;
3880
3881         if (EVP_Cipher(&ctx, cipher, (byte*)msg, 16) == 0)
3882             return -82;
3883
3884         if (memcmp(cipher, verify, AES_BLOCK_SIZE))
3885             return -83;
3886
3887         EVP_CIPHER_CTX_init(&ctx);
3888         if (EVP_CipherInit(&ctx, EVP_aes_128_cbc(), key, iv, 0) == 0)
3889             return -84;
3890
3891         if (EVP_Cipher(&ctx, plain, cipher, 16) == 0)
3892             return -85;
3893
3894         if (memcmp(plain, msg, AES_BLOCK_SIZE))
3895             return -86;
3896
3897
3898     }  /* end evp_cipher test */
3899
3900     return 0;
3901 }
3902
3903 #endif /* OPENSSL_EXTRA */
3904
3905
3906 #ifndef NO_PWDBASED
3907
3908 int pkcs12_test(void)
3909 {
3910     const byte passwd[] = { 0x00, 0x73, 0x00, 0x6d, 0x00, 0x65, 0x00, 0x67,
3911                             0x00, 0x00 };
3912     const byte salt[] =   { 0x0a, 0x58, 0xCF, 0x64, 0x53, 0x0d, 0x82, 0x3f };
3913
3914     const byte passwd2[] = { 0x00, 0x71, 0x00, 0x75, 0x00, 0x65, 0x00, 0x65,
3915                              0x00, 0x67, 0x00, 0x00 };
3916     const byte salt2[] =   { 0x16, 0x82, 0xC0, 0xfC, 0x5b, 0x3f, 0x7e, 0xc5 };
3917     byte  derived[64];
3918
3919     const byte verify[] = {
3920         0x8A, 0xAA, 0xE6, 0x29, 0x7B, 0x6C, 0xB0, 0x46,
3921         0x42, 0xAB, 0x5B, 0x07, 0x78, 0x51, 0x28, 0x4E,
3922         0xB7, 0x12, 0x8F, 0x1A, 0x2A, 0x7F, 0xBC, 0xA3
3923     };
3924
3925     const byte verify2[] = {
3926         0x48, 0x3D, 0xD6, 0xE9, 0x19, 0xD7, 0xDE, 0x2E,
3927         0x8E, 0x64, 0x8B, 0xA8, 0xF8, 0x62, 0xF3, 0xFB,
3928         0xFB, 0xDC, 0x2B, 0xCB, 0x2C, 0x02, 0x95, 0x7F
3929     };
3930
3931     int id         =  1;
3932     int kLen       = 24;
3933     int iterations =  1;
3934     int ret = PKCS12_PBKDF(derived, passwd, sizeof(passwd), salt, 8, iterations,
3935                            kLen, SHA, id);
3936
3937     if (ret < 0)
3938         return -103;
3939
3940     if ( (ret = memcmp(derived, verify, kLen)) != 0)
3941         return -104;
3942
3943     iterations = 1000;
3944     ret = PKCS12_PBKDF(derived, passwd2, sizeof(passwd2), salt2, 8, iterations,
3945                        kLen, SHA, id);
3946     if (ret < 0)
3947         return -105;
3948
3949     if ( (ret = memcmp(derived, verify2, 24)) != 0)
3950         return -106;
3951
3952     return 0;
3953 }
3954
3955
3956 int pbkdf2_test(void)
3957 {
3958     char passwd[] = "password";
3959     const byte salt[] = { 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06 };
3960     int   iterations = 2048;
3961     int   kLen = 24;
3962     byte  derived[64];
3963
3964     const byte verify[] = {
3965         0xBF, 0xDE, 0x6B, 0xE9, 0x4D, 0xF7, 0xE1, 0x1D, 0xD4, 0x09, 0xBC, 0xE2,
3966         0x0A, 0x02, 0x55, 0xEC, 0x32, 0x7C, 0xB9, 0x36, 0xFF, 0xE9, 0x36, 0x43
3967
3968     };
3969
3970     int ret = PBKDF2(derived, (byte*)passwd, (int)strlen(passwd), salt, 8,
3971                                                          iterations, kLen, SHA);
3972     if (ret != 0)
3973         return ret;
3974
3975     if (memcmp(derived, verify, sizeof(verify)) != 0)
3976         return -102;
3977
3978     return 0;
3979 }
3980
3981
3982 int pbkdf1_test(void)
3983 {
3984     char passwd[] = "password";
3985     const byte salt[] = { 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06 };
3986     int   iterations = 1000;
3987     int   kLen = 16;
3988     byte  derived[16];
3989
3990     const byte verify[] = {
3991         0xDC, 0x19, 0x84, 0x7E, 0x05, 0xC6, 0x4D, 0x2F, 0xAF, 0x10, 0xEB, 0xFB,
3992         0x4A, 0x3D, 0x2A, 0x20
3993     };
3994
3995     PBKDF1(derived, (byte*)passwd, (int)strlen(passwd), salt, 8, iterations,
3996            kLen, SHA);
3997
3998     if (memcmp(derived, verify, sizeof(verify)) != 0)
3999         return -101;
4000
4001     return 0;
4002 }
4003
4004
4005 int pwdbased_test(void)
4006 {
4007    int ret =  pbkdf1_test();
4008    ret += pbkdf2_test();
4009
4010    return ret + pkcs12_test();
4011 }
4012
4013 #endif /* NO_PWDBASED */
4014
4015 #if defined(HAVE_HKDF) && (!defined(NO_SHA) || !defined(NO_SHA256))
4016
4017 int hkdf_test(void)
4018 {
4019     int ret;
4020     int L = 42;
4021     byte okm1[42];
4022     byte ikm1[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4023                       0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4024                       0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
4025     byte salt1[13] ={ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
4026                       0x08, 0x09, 0x0a, 0x0b, 0x0c };
4027     byte info1[10] ={ 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
4028                       0xf8, 0xf9 };
4029     byte res1[42] = { 0x0a, 0xc1, 0xaf, 0x70, 0x02, 0xb3, 0xd7, 0x61,
4030                       0xd1, 0xe5, 0x52, 0x98, 0xda, 0x9d, 0x05, 0x06,
4031                       0xb9, 0xae, 0x52, 0x05, 0x72, 0x20, 0xa3, 0x06,
4032                       0xe0, 0x7b, 0x6b, 0x87, 0xe8, 0xdf, 0x21, 0xd0,
4033                       0xea, 0x00, 0x03, 0x3d, 0xe0, 0x39, 0x84, 0xd3,
4034                       0x49, 0x18 };
4035     byte res2[42] = { 0x08, 0x5a, 0x01, 0xea, 0x1b, 0x10, 0xf3, 0x69,
4036                       0x33, 0x06, 0x8b, 0x56, 0xef, 0xa5, 0xad, 0x81,
4037                       0xa4, 0xf1, 0x4b, 0x82, 0x2f, 0x5b, 0x09, 0x15,
4038                       0x68, 0xa9, 0xcd, 0xd4, 0xf1, 0x55, 0xfd, 0xa2,
4039                       0xc2, 0x2e, 0x42, 0x24, 0x78, 0xd3, 0x05, 0xf3,
4040                       0xf8, 0x96 };
4041     byte res3[42] = { 0x8d, 0xa4, 0xe7, 0x75, 0xa5, 0x63, 0xc1, 0x8f,
4042                       0x71, 0x5f, 0x80, 0x2a, 0x06, 0x3c, 0x5a, 0x31,
4043                       0xb8, 0xa1, 0x1f, 0x5c, 0x5e, 0xe1, 0x87, 0x9e,
4044                       0xc3, 0x45, 0x4e, 0x5f, 0x3c, 0x73, 0x8d, 0x2d,
4045                       0x9d, 0x20, 0x13, 0x95, 0xfa, 0xa4, 0xb6, 0x1a,
4046                       0x96, 0xc8 };
4047     byte res4[42] = { 0x3c, 0xb2, 0x5f, 0x25, 0xfa, 0xac, 0xd5, 0x7a,
4048                       0x90, 0x43, 0x4f, 0x64, 0xd0, 0x36, 0x2f, 0x2a,
4049                       0x2d, 0x2d, 0x0a, 0x90, 0xcf, 0x1a, 0x5a, 0x4c,
4050                       0x5d, 0xb0, 0x2d, 0x56, 0xec, 0xc4, 0xc5, 0xbf,
4051                       0x34, 0x00, 0x72, 0x08, 0xd5, 0xb8, 0x87, 0x18,
4052                       0x58, 0x65 };
4053
4054     (void)res1;
4055     (void)res2;
4056     (void)res3;
4057     (void)res4;
4058
4059 #ifndef NO_SHA
4060     ret = HKDF(SHA, ikm1, 22, NULL, 0, NULL, 0, okm1, L);
4061     if (ret != 0)
4062         return -2001;
4063
4064     if (memcmp(okm1, res1, L) != 0)
4065         return -2002;
4066
4067     ret = HKDF(SHA, ikm1, 11, salt1, 13, info1, 10, okm1, L);
4068     if (ret != 0)
4069         return -2003;
4070
4071     if (memcmp(okm1, res2, L) != 0)
4072         return -2004;
4073 #endif /* NO_SHA */
4074
4075 #ifndef NO_SHA256
4076     ret = HKDF(SHA256, ikm1, 22, NULL, 0, NULL, 0, okm1, L);
4077     if (ret != 0)
4078         return -2005;
4079
4080     if (memcmp(okm1, res3, L) != 0)
4081         return -2006;
4082
4083     ret = HKDF(SHA256, ikm1, 22, salt1, 13, info1, 10, okm1, L);
4084     if (ret != 0)
4085         return -2007;
4086
4087     if (memcmp(okm1, res4, L) != 0)
4088         return -2007;
4089 #endif /* NO_SHA256 */
4090
4091     return 0;
4092 }
4093
4094 #endif /* HAVE_HKDF */
4095
4096
4097 #ifdef HAVE_ECC
4098
4099 int ecc_test(void)
4100 {
4101     RNG     rng;
4102     byte    sharedA[1024];
4103     byte    sharedB[1024];
4104     byte    sig[1024];
4105     byte    digest[20];
4106     byte    exportBuf[1024];
4107     word32  x, y;
4108     int     i, verify, ret;
4109     ecc_key userA, userB, pubKey;
4110
4111     ret = InitRng(&rng);
4112     if (ret != 0)
4113         return -1001;
4114
4115     ecc_init(&userA);
4116     ecc_init(&userB);
4117     ecc_init(&pubKey);
4118
4119     ret = ecc_make_key(&rng, 32, &userA);
4120
4121     if (ret != 0)
4122         return -1014;
4123
4124     ret = ecc_make_key(&rng, 32, &userB);
4125
4126     if (ret != 0)
4127         return -1002;
4128
4129     x = sizeof(sharedA);
4130     ret = ecc_shared_secret(&userA, &userB, sharedA, &x);
4131
4132     if (ret != 0)
4133         return -1015;
4134
4135     y = sizeof(sharedB);
4136     ret = ecc_shared_secret(&userB, &userA, sharedB, &y);
4137
4138     if (ret != 0)
4139         return -1003;
4140
4141     if (y != x)
4142         return -1004;
4143
4144     if (memcmp(sharedA, sharedB, x))
4145         return -1005;
4146
4147     x = sizeof(exportBuf);
4148     ret = ecc_export_x963(&userA, exportBuf, &x);
4149     if (ret != 0)
4150         return -1006;
4151
4152     ret = ecc_import_x963(exportBuf, x, &pubKey);
4153
4154     if (ret != 0)
4155         return -1007;
4156
4157     y = sizeof(sharedB);
4158     ret = ecc_shared_secret(&userB, &pubKey, sharedB, &y);
4159
4160     if (ret != 0)
4161         return -1008;
4162
4163     if (memcmp(sharedA, sharedB, y))
4164         return -1010;
4165
4166     /* test DSA sign hash */
4167     for (i = 0; i < (int)sizeof(digest); i++)
4168         digest[i] = (byte)i;
4169
4170     x = sizeof(sig);
4171     ret = ecc_sign_hash(digest, sizeof(digest), sig, &x, &rng, &userA);
4172
4173     if (ret != 0)
4174         return -1016;
4175
4176     verify = 0;
4177     ret = ecc_verify_hash(sig, x, digest, sizeof(digest), &verify, &userA);
4178
4179     if (ret != 0)
4180         return -1011;
4181
4182     if (verify != 1)
4183         return -1012;
4184
4185     x = sizeof(exportBuf);
4186     ret = ecc_export_private_only(&userA, exportBuf, &x);
4187     if (ret != 0)
4188         return -1013;
4189
4190     ecc_free(&pubKey);
4191     ecc_free(&userB);
4192     ecc_free(&userA);
4193
4194     return 0;
4195 }
4196
4197 #ifdef HAVE_ECC_ENCRYPT
4198
4199 int ecc_encrypt_test(void)
4200 {
4201     RNG     rng;
4202     int     ret;
4203     ecc_key userA, userB;
4204     byte    msg[48];
4205     byte    plain[48];
4206     byte    out[80];
4207     word32  outSz   = sizeof(out);
4208     word32  plainSz = sizeof(plain);
4209     int     i;
4210
4211     ret = InitRng(&rng);
4212     if (ret != 0)
4213         return -3001;
4214
4215     ecc_init(&userA);
4216     ecc_init(&userB);
4217
4218     ret  = ecc_make_key(&rng, 32, &userA);
4219     ret += ecc_make_key(&rng, 32, &userB);
4220
4221     if (ret != 0)
4222         return -3002;
4223
4224     for (i = 0; i < 48; i++)
4225         msg[i] = i;
4226
4227     /* encrypt msg to B */
4228     ret = ecc_encrypt(&userA, &userB, msg, sizeof(msg), out, &outSz, NULL);
4229     if (ret != 0)
4230         return -3003;
4231
4232     /* decrypt msg from A */
4233     ret = ecc_decrypt(&userB, &userA, out, outSz, plain, &plainSz, NULL);
4234     if (ret != 0)
4235         return -3004;
4236
4237     if (memcmp(plain, msg, sizeof(msg)) != 0)
4238         return -3005;
4239
4240
4241     {  /* let's verify message exchange works, A is client, B is server */
4242         ecEncCtx* cliCtx = ecc_ctx_new(REQ_RESP_CLIENT, &rng);
4243         ecEncCtx* srvCtx = ecc_ctx_new(REQ_RESP_SERVER, &rng);
4244
4245         byte cliSalt[EXCHANGE_SALT_SZ];
4246         byte srvSalt[EXCHANGE_SALT_SZ];
4247         const byte* tmpSalt;
4248
4249         if (cliCtx == NULL || srvCtx == NULL)
4250             return -3006;
4251
4252         /* get salt to send to peer */
4253         tmpSalt = ecc_ctx_get_own_salt(cliCtx);
4254         if (tmpSalt == NULL)
4255             return -3007;
4256         memcpy(cliSalt, tmpSalt, EXCHANGE_SALT_SZ);
4257
4258         tmpSalt = ecc_ctx_get_own_salt(srvCtx);
4259         if (tmpSalt == NULL)
4260             return -3007;
4261         memcpy(srvSalt, tmpSalt, EXCHANGE_SALT_SZ);
4262
4263         /* in actual use, we'd get the peer's salt over the transport */
4264         ret  = ecc_ctx_set_peer_salt(cliCtx, srvSalt);
4265         ret += ecc_ctx_set_peer_salt(srvCtx, cliSalt);
4266
4267         if (ret != 0)
4268             return -3008;
4269
4270         /* get encrypted msg (request) to send to B */
4271         outSz  = sizeof(out);
4272         ret = ecc_encrypt(&userA, &userB, msg, sizeof(msg), out, &outSz,cliCtx);
4273         if (ret != 0)
4274             return -3009;
4275
4276         /* B decrypts msg (request) from A */
4277         plainSz = sizeof(plain);
4278         ret = ecc_decrypt(&userB, &userA, out, outSz, plain, &plainSz, srvCtx);
4279         if (ret != 0)
4280             return -3010;
4281
4282         if (memcmp(plain, msg, sizeof(msg)) != 0)
4283             return -3011;
4284
4285         {
4286             /* msg2 (response) from B to A */
4287             byte    msg2[48];
4288             byte    plain2[48];
4289             byte    out2[80];
4290             word32  outSz2   = sizeof(out2);
4291             word32  plainSz2 = sizeof(plain2);
4292
4293             for (i = 0; i < 48; i++)
4294                 msg2[i] = i+48;
4295
4296             /* get encrypted msg (response) to send to B */
4297             ret = ecc_encrypt(&userB, &userA, msg2, sizeof(msg2), out2,
4298                               &outSz2, srvCtx);
4299             if (ret != 0)
4300                 return -3012;
4301
4302             /* A decrypts msg (response) from B */
4303             ret = ecc_decrypt(&userA, &userB, out2, outSz2, plain2, &plainSz2,
4304                              cliCtx);
4305             if (ret != 0)
4306                 return -3013;
4307
4308             if (memcmp(plain2, msg2, sizeof(msg2)) != 0)
4309                 return -3014;
4310         }
4311
4312         /* cleanup */
4313         ecc_ctx_free(srvCtx);
4314         ecc_ctx_free(cliCtx);
4315     }
4316
4317     /* cleanup */
4318     ecc_free(&userB);
4319     ecc_free(&userA);
4320
4321     return 0;
4322 }
4323
4324 #endif /* HAVE_ECC_ENCRYPT */
4325 #endif /* HAVE_ECC */
4326
4327 #ifdef HAVE_LIBZ
4328
4329 const byte sample_text[] =
4330     "Biodiesel cupidatat marfa, cliche aute put a bird on it incididunt elit\n"
4331     "polaroid. Sunt tattooed bespoke reprehenderit. Sint twee organic id\n"
4332     "marfa. Commodo veniam ad esse gastropub. 3 wolf moon sartorial vero,\n"
4333     "plaid delectus biodiesel squid +1 vice. Post-ironic keffiyeh leggings\n"
4334     "selfies cray fap hoodie, forage anim. Carles cupidatat shoreditch, VHS\n"
4335     "small batch meggings kogi dolore food truck bespoke gastropub.\n"
4336     "\n"
4337     "Terry richardson adipisicing actually typewriter tumblr, twee whatever\n"
4338     "four loko you probably haven't heard of them high life. Messenger bag\n"
4339     "whatever tattooed deep v mlkshk. Brooklyn pinterest assumenda chillwave\n"
4340     "et, banksy ullamco messenger bag umami pariatur direct trade forage.\n"
4341     "Typewriter culpa try-hard, pariatur sint brooklyn meggings. Gentrify\n"
4342     "food truck next level, tousled irony non semiotics PBR ethical anim cred\n"
4343     "readymade. Mumblecore brunch lomo odd future, portland organic terry\n"
4344     "richardson elit leggings adipisicing ennui raw denim banjo hella. Godard\n"
4345     "mixtape polaroid, pork belly readymade organic cray typewriter helvetica\n"
4346     "four loko whatever street art yr farm-to-table.\n"
4347     "\n"
4348     "Vinyl keytar vice tofu. Locavore you probably haven't heard of them pug\n"
4349     "pickled, hella tonx labore truffaut DIY mlkshk elit cosby sweater sint\n"
4350     "et mumblecore. Elit swag semiotics, reprehenderit DIY sartorial nisi ugh\n"
4351     "nesciunt pug pork belly wayfarers selfies delectus. Ethical hoodie\n"
4352     "seitan fingerstache kale chips. Terry richardson artisan williamsburg,\n"
4353     "eiusmod fanny pack irony tonx ennui lo-fi incididunt tofu YOLO\n"
4354     "readymade. 8-bit sed ethnic beard officia. Pour-over iphone DIY butcher,\n"
4355     "ethnic art party qui letterpress nisi proident jean shorts mlkshk\n"
4356     "locavore.\n"
4357     "\n"
4358     "Narwhal flexitarian letterpress, do gluten-free voluptate next level\n"
4359     "banh mi tonx incididunt carles DIY. Odd future nulla 8-bit beard ut\n"
4360     "cillum pickled velit, YOLO officia you probably haven't heard of them\n"
4361     "trust fund gastropub. Nisi adipisicing tattooed, Austin mlkshk 90's\n"
4362     "small batch american apparel. Put a bird on it cosby sweater before they\n"
4363     "sold out pork belly kogi hella. Street art mollit sustainable polaroid,\n"
4364     "DIY ethnic ea pug beard dreamcatcher cosby sweater magna scenester nisi.\n"
4365     "Sed pork belly skateboard mollit, labore proident eiusmod. Sriracha\n"
4366     "excepteur cosby sweater, anim deserunt laborum eu aliquip ethical et\n"
4367     "neutra PBR selvage.\n"
4368     "\n"
4369     "Raw denim pork belly truffaut, irony plaid sustainable put a bird on it\n"
4370     "next level jean shorts exercitation. Hashtag keytar whatever, nihil\n"
4371     "authentic aliquip disrupt laborum. Tattooed selfies deserunt trust fund\n"
4372     "wayfarers. 3 wolf moon synth church-key sartorial, gastropub leggings\n"
4373     "tattooed. Labore high life commodo, meggings raw denim fingerstache pug\n"
4374     "trust fund leggings seitan forage. Nostrud ullamco duis, reprehenderit\n"
4375     "incididunt flannel sustainable helvetica pork belly pug banksy you\n"
4376     "probably haven't heard of them nesciunt farm-to-table. Disrupt nostrud\n"
4377     "mollit magna, sriracha sartorial helvetica.\n"
4378     "\n"
4379     "Nulla kogi reprehenderit, skateboard sustainable duis adipisicing viral\n"
4380     "ad fanny pack salvia. Fanny pack trust fund you probably haven't heard\n"
4381     "of them YOLO vice nihil. Keffiyeh cray lo-fi pinterest cardigan aliqua,\n"
4382     "reprehenderit aute. Culpa tousled williamsburg, marfa lomo actually anim\n"
4383     "skateboard. Iphone aliqua ugh, semiotics pariatur vero readymade\n"
4384     "organic. Marfa squid nulla, in laborum disrupt laboris irure gastropub.\n"
4385     "Veniam sunt food truck leggings, sint vinyl fap.\n"
4386     "\n"
4387     "Hella dolore pork belly, truffaut carles you probably haven't heard of\n"
4388     "them PBR helvetica in sapiente. Fashion axe ugh bushwick american\n"
4389     "apparel. Fingerstache sed iphone, jean shorts blue bottle nisi bushwick\n"
4390     "flexitarian officia veniam plaid bespoke fap YOLO lo-fi. Blog\n"
4391     "letterpress mumblecore, food truck id cray brooklyn cillum ad sed.\n"
4392     "Assumenda chambray wayfarers vinyl mixtape sustainable. VHS vinyl\n"
4393     "delectus, culpa williamsburg polaroid cliche swag church-key synth kogi\n"
4394     "magna pop-up literally. Swag thundercats ennui shoreditch vegan\n"
4395     "pitchfork neutra truffaut etsy, sed single-origin coffee craft beer.\n"
4396     "\n"
4397     "Odio letterpress brooklyn elit. Nulla single-origin coffee in occaecat\n"
4398     "meggings. Irony meggings 8-bit, chillwave lo-fi adipisicing cred\n"
4399     "dreamcatcher veniam. Put a bird on it irony umami, trust fund bushwick\n"
4400     "locavore kale chips. Sriracha swag thundercats, chillwave disrupt\n"
4401     "tousled beard mollit mustache leggings portland next level. Nihil esse\n"
4402     "est, skateboard art party etsy thundercats sed dreamcatcher ut iphone\n"
4403     "swag consectetur et. Irure skateboard banjo, nulla deserunt messenger\n"
4404     "bag dolor terry richardson sapiente.\n";
4405
4406
4407 int compress_test(void)
4408 {
4409     int ret = 0;
4410     word32 dSz = sizeof(sample_text);
4411     word32 cSz = (dSz + (word32)(dSz * 0.001) + 12);
4412     byte *c = NULL;
4413     byte *d = NULL;
4414
4415     c = calloc(cSz, sizeof(byte));
4416     d = calloc(dSz, sizeof(byte));
4417
4418     if (c == NULL || d == NULL)
4419         ret = -300;
4420
4421     if (ret == 0 && (ret = Compress(c, cSz, sample_text, dSz, 0)) < 0)
4422         ret = -301;
4423
4424     if (ret > 0) {
4425         cSz = (word32)ret;
4426         ret = 0;
4427     }
4428
4429     if (ret == 0 && DeCompress(d, dSz, c, cSz) != (int)dSz)
4430         ret = -302;
4431
4432     if (ret == 0 && memcmp(d, sample_text, dSz))
4433         ret = -303;
4434
4435     if (c) free(c);
4436     if (d) free(d);
4437
4438     return ret;
4439 }
4440
4441 #endif /* HAVE_LIBZ */
4442
4443 #ifdef HAVE_PKCS7
4444
4445 int pkcs7enveloped_test(void)
4446 {
4447     int ret = 0;
4448
4449     int cipher = DES3b;
4450     int envelopedSz, decodedSz;
4451     PKCS7 pkcs7;
4452     byte* cert;
4453     byte* privKey;
4454     byte  enveloped[2048];
4455     byte  decoded[2048];
4456
4457     size_t certSz;
4458     size_t privKeySz;
4459     FILE*  certFile;
4460     FILE*  keyFile;
4461     FILE*  pkcs7File;
4462     const char* pkcs7OutFile = "pkcs7envelopedData.der";
4463
4464     const byte data[] = { /* Hello World */
4465         0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
4466         0x72,0x6c,0x64
4467     };
4468
4469     /* read client cert and key in DER format */
4470     cert = (byte*)malloc(FOURK_BUF);
4471     if (cert == NULL)
4472         return -201;
4473
4474     privKey = (byte*)malloc(FOURK_BUF);
4475     if (privKey == NULL) {
4476         free(cert);
4477         return -202;
4478     }
4479
4480     certFile = fopen(clientCert, "rb");
4481     if (!certFile) {
4482         free(cert);
4483         free(privKey);
4484         err_sys("can't open ./certs/client-cert.der, "
4485                 "Please run from CyaSSL home dir", -42);
4486     }
4487
4488     certSz = fread(cert, 1, FOURK_BUF, certFile);
4489     fclose(certFile);
4490
4491     keyFile = fopen(clientKey, "rb");
4492     if (!keyFile) {
4493         free(cert);
4494         free(privKey);
4495         err_sys("can't open ./certs/client-key.der, "
4496                 "Please run from CyaSSL home dir", -43);
4497     }
4498
4499     privKeySz = fread(privKey, 1, FOURK_BUF, keyFile);
4500     fclose(keyFile);
4501
4502     PKCS7_InitWithCert(&pkcs7, cert, (word32)certSz);
4503     pkcs7.content     = (byte*)data;
4504     pkcs7.contentSz   = (word32)sizeof(data);
4505     pkcs7.contentOID  = DATA;
4506     pkcs7.encryptOID  = cipher;
4507     pkcs7.privateKey  = privKey;
4508     pkcs7.privateKeySz = (word32)privKeySz;
4509
4510     /* encode envelopedData */
4511     envelopedSz = PKCS7_EncodeEnvelopedData(&pkcs7, enveloped,
4512                                             sizeof(enveloped));
4513     if (envelopedSz <= 0) {
4514         free(cert);
4515         free(privKey);
4516         return -203;
4517     }
4518
4519     /* decode envelopedData */
4520     decodedSz = PKCS7_DecodeEnvelopedData(&pkcs7, enveloped, envelopedSz,
4521                                           decoded, sizeof(decoded));
4522     if (decodedSz <= 0) {
4523         free(cert);
4524         free(privKey);
4525         return -204;
4526     }
4527
4528     /* test decode result */
4529     if (memcmp(decoded, data, sizeof(data)) != 0) {
4530         free(cert);
4531         free(privKey);
4532         return -205;
4533     }
4534
4535     /* output pkcs7 envelopedData for external testing */
4536     pkcs7File = fopen(pkcs7OutFile, "wb");
4537     if (!pkcs7File) {
4538         free(cert);
4539         free(privKey);
4540         return -206;
4541     }
4542
4543     ret = (int)fwrite(enveloped, envelopedSz, 1, pkcs7File);
4544     fclose(pkcs7File);
4545
4546     free(cert);
4547     free(privKey);
4548     PKCS7_Free(&pkcs7);
4549
4550     if (ret > 0)
4551         return 0;
4552
4553     return ret;
4554 }
4555
4556 int pkcs7signed_test(void)
4557 {
4558     int ret = 0;
4559
4560     FILE* file;
4561     byte* certDer;
4562     byte* keyDer;
4563     byte* out;
4564     char data[] = "Hello World";
4565     word32 dataSz, outSz, certDerSz, keyDerSz;
4566     PKCS7 msg;
4567     RNG rng;
4568
4569     byte transIdOid[] =
4570                { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
4571                  0x09, 0x07 };
4572     byte messageTypeOid[] =
4573                { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
4574                  0x09, 0x02 };
4575     byte senderNonceOid[] =
4576                { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
4577                  0x09, 0x05 };
4578     byte transId[(SHA_DIGEST_SIZE + 1) * 2 + 1];
4579     byte messageType[] = { 0x13, 2, '1', '9' };
4580     byte senderNonce[PKCS7_NONCE_SZ + 2];
4581
4582     PKCS7Attrib attribs[] =
4583     {
4584         { transIdOid, sizeof(transIdOid),
4585                      transId, sizeof(transId) - 1 }, /* take off the null */
4586         { messageTypeOid, sizeof(messageTypeOid),
4587                      messageType, sizeof(messageType) },
4588         { senderNonceOid, sizeof(senderNonceOid),
4589                      senderNonce, sizeof(senderNonce) }
4590     };
4591
4592     dataSz = (word32) strlen(data);
4593     outSz = FOURK_BUF;
4594
4595     certDer = (byte*)malloc(FOURK_BUF);
4596     if (certDer == NULL)
4597         return -207;
4598     keyDer = (byte*)malloc(FOURK_BUF);
4599     if (keyDer == NULL) {
4600         free(certDer);
4601         return -208;
4602     }
4603     out = (byte*)malloc(FOURK_BUF);
4604     if (out == NULL) {
4605         free(certDer);
4606         free(keyDer);
4607         return -209;
4608     }
4609
4610     /* read in DER cert of recipient, into cert of size certSz */
4611     file = fopen(clientCert, "rb");
4612     if (!file) {
4613         free(certDer);
4614         free(keyDer);
4615         free(out);
4616         err_sys("can't open ./certs/client-cert.der, "
4617                 "Please run from CyaSSL home dir", -44);
4618     }
4619     certDerSz = (word32)fread(certDer, 1, FOURK_BUF, file);
4620     fclose(file);
4621
4622     file = fopen(clientKey, "rb");
4623     if (!file) {
4624         free(certDer);
4625         free(keyDer);
4626         free(out);
4627         err_sys("can't open ./certs/client-key.der, "
4628                 "Please run from CyaSSL home dir", -45);
4629     }
4630     keyDerSz = (word32)fread(keyDer, 1, FOURK_BUF, file);
4631     fclose(file);
4632
4633     ret = InitRng(&rng);
4634     if (ret != 0) {
4635         free(certDer);
4636         free(keyDer);
4637         free(out);
4638         return -210;
4639     }
4640
4641     senderNonce[0] = 0x04;
4642     senderNonce[1] = PKCS7_NONCE_SZ;
4643
4644     ret = RNG_GenerateBlock(&rng, &senderNonce[2], PKCS7_NONCE_SZ);
4645     if (ret != 0) {
4646         free(certDer);
4647         free(keyDer);
4648         free(out);
4649         return -211;
4650     }
4651
4652     PKCS7_InitWithCert(&msg, certDer, certDerSz);
4653     msg.privateKey = keyDer;
4654     msg.privateKeySz = keyDerSz;
4655     msg.content = (byte*)data;
4656     msg.contentSz = dataSz;
4657     msg.hashOID = SHAh;
4658     msg.encryptOID = RSAk;
4659     msg.signedAttribs = attribs;
4660     msg.signedAttribsSz = sizeof(attribs)/sizeof(PKCS7Attrib);
4661     msg.rng = &rng;
4662     {
4663         Sha sha;
4664         byte digest[SHA_DIGEST_SIZE];
4665         int i,j;
4666
4667         transId[0] = 0x13;
4668         transId[1] = SHA_DIGEST_SIZE * 2;
4669
4670         ret = InitSha(&sha);
4671         if (ret != 0) {
4672             free(certDer);
4673             free(keyDer);
4674             free(out);
4675             return -4003;
4676         }
4677         ShaUpdate(&sha, msg.publicKey, msg.publicKeySz);
4678         ShaFinal(&sha, digest);
4679
4680         for (i = 0, j = 2; i < SHA_DIGEST_SIZE; i++, j += 2) {
4681             snprintf((char*)&transId[j], 3, "%02x", digest[i]);
4682         }
4683     }
4684     ret = PKCS7_EncodeSignedData(&msg, out, outSz);
4685     if (ret < 0) {
4686         free(certDer);
4687         free(keyDer);
4688         free(out);
4689         PKCS7_Free(&msg);
4690         return -212;
4691     }
4692     else
4693         outSz = ret;
4694
4695     /* write PKCS#7 to output file for more testing */
4696     file = fopen("./pkcs7signedData.der", "wb");
4697     if (!file) {
4698         free(certDer);
4699         free(keyDer);
4700         free(out);
4701         PKCS7_Free(&msg);
4702         return -213;
4703     }
4704     ret = (int)fwrite(out, 1, outSz, file);
4705     fclose(file);
4706     if (ret != (int)outSz) {
4707         free(certDer);
4708         free(keyDer);
4709         free(out);
4710         PKCS7_Free(&msg);
4711         return -218;
4712     }
4713
4714     PKCS7_Free(&msg);
4715     PKCS7_InitWithCert(&msg, NULL, 0);
4716
4717     ret = PKCS7_VerifySignedData(&msg, out, outSz);
4718     if (ret < 0) {
4719         free(certDer);
4720         free(keyDer);
4721         free(out);
4722         PKCS7_Free(&msg);
4723         return -214;
4724     }
4725
4726     if (msg.singleCert == NULL || msg.singleCertSz == 0) {
4727         free(certDer);
4728         free(keyDer);
4729         free(out);
4730         PKCS7_Free(&msg);
4731         return -215;
4732     }
4733
4734     file = fopen("./pkcs7cert.der", "wb");
4735     if (!file) {
4736         free(certDer);
4737         free(keyDer);
4738         free(out);
4739         PKCS7_Free(&msg);
4740         return -216;
4741     }
4742     ret = (int)fwrite(msg.singleCert, 1, msg.singleCertSz, file);
4743     fclose(file);
4744
4745     free(certDer);
4746     free(keyDer);
4747     free(out);
4748     PKCS7_Free(&msg);
4749
4750     if (ret > 0)
4751         return 0;
4752
4753     return ret;
4754 }
4755
4756 #endif /* HAVE_PKCS7 */
4757
4758 #endif /* NO_CRYPT_TEST */