3 * Copyright (C) 2006-2014 wolfSSL Inc.
5 * This file is part of CyaSSL.
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.
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.
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
23 /* Tests Microchip CRYPTO API layer */
30 #include <cyassl/ctaocrypt/settings.h>
32 /* sanity test against our default implementation, cyassl headers */
33 #include <cyassl/ctaocrypt/md5.h>
34 #include <cyassl/ctaocrypt/sha.h>
35 #include <cyassl/ctaocrypt/sha256.h>
36 #include <cyassl/ctaocrypt/sha512.h>
37 #include <cyassl/ctaocrypt/hmac.h>
38 #include <cyassl/ctaocrypt/compress.h>
39 #include <cyassl/ctaocrypt/random.h>
40 #include <cyassl/ctaocrypt/des3.h>
41 #include <cyassl/ctaocrypt/aes.h>
42 #include <cyassl/ctaocrypt/ecc.h>
43 #include <cyassl/ctaocrypt/rsa.h>
44 #define USE_CERT_BUFFERS_1024
45 #include <cyassl/certs_test.h>
47 #if defined(CYASSL_MICROCHIP_PIC32MZ)
48 #define MICROCHIP_PIC32
50 #pragma config ICESEL = ICS_PGx2
51 /* ICE/ICD Comm Channel Select (Communicate on PGEC2/PGED2) */
54 #include "PIC32MZ-serial.h"
55 #define SYSTEMConfigPerformance /* void out SYSTEMConfigPerformance(); */
56 #elif defined(MICROCHIP_PIC32)
57 #define PIC32_STARTER_KIT
62 #include <sys/appio.h>
63 #define init_serial() /* void out init_serial() */
65 #include <stdio.h> /* order matters above ? */
67 #define OUR_DATA_SIZE 1024
68 static byte ourData[OUR_DATA_SIZE];
69 static byte* key = NULL;
70 static byte* iv = NULL;
71 static CRYPT_RNG_CTX mcRng;
74 static int check_md5(void);
75 static int check_sha(void);
76 static int check_sha256(void);
77 static int check_sha384(void);
78 static int check_sha512(void);
79 static int check_hmac(void);
80 static int check_compress(void);
81 static int check_rng(void);
82 static int check_des3(void);
83 static int check_aescbc(void);
84 static int check_aesctr(void);
85 static int check_aesdirect(void);
86 static int check_rsa(void);
87 static int check_ecc(void);
90 int main(int argc, char** argv)
98 #if defined(MICROCHIP_PIC32)
99 init_serial() ; /* initialize PIC32MZ serial I/O */
100 SYSTEMConfigPerformance(80000000);
104 /* align key, iv pointers */
105 key = (byte*)XMALLOC(32, NULL, DYNAMIC_TYPE_KEY);
107 printf("mcapi key alloc failed\n");
111 iv = (byte*)XMALLOC(16, NULL, DYNAMIC_TYPE_KEY);
113 printf("mcapi iv alloc failed\n");
117 for (i = 0; i < OUR_DATA_SIZE; i++)
118 ourData[i] = (byte)i;
122 printf("mcapi check_md5 failed\n");
128 printf("mcapi check_sha failed\n");
132 ret = check_sha256();
134 printf("mcapi check_sha256 failed\n");
138 ret = check_sha384();
140 printf("mcapi check_sha384 failed\n");
144 ret = check_sha512();
146 printf("mcapi check_sha512 failed\n");
152 printf("mcapi check_hmac failed\n");
156 ret = check_compress();
158 printf("mcapi check_compress failed\n");
164 printf("mcapi check_rng failed\n");
170 printf("mcapi check_des3 failed\n");
174 ret = check_aescbc();
176 printf("mcapi check_aes cbc failed\n");
180 ret = check_aesctr();
182 printf("mcapi check_aes ctr failed\n");
186 ret = check_aesdirect();
188 printf("mcapi check_aes direct failed\n");
194 printf("mcapi check_rsa failed\n");
200 printf("mcapi check_ecc failed\n");
204 XFREE(iv, NULL, DYNAMIC_TYPE_KEY);
205 XFREE(key, NULL, DYNAMIC_TYPE_KEY);
211 /* check mcapi md5 against internal */
212 static int check_md5(void)
216 byte mcDigest[CRYPT_MD5_DIGEST_SIZE];
217 byte defDigest[MD5_DIGEST_SIZE];
219 CRYPT_MD5_Initialize(&mcMd5);
222 CRYPT_MD5_DataAdd(&mcMd5, ourData, OUR_DATA_SIZE);
223 Md5Update(&defMd5, ourData, OUR_DATA_SIZE);
225 CRYPT_MD5_Finalize(&mcMd5, mcDigest);
226 Md5Final(&defMd5, defDigest);
228 if (memcmp(mcDigest, defDigest, CRYPT_MD5_DIGEST_SIZE) != 0) {
229 printf("md5 final memcmp fialed\n");
232 printf("md5 mcapi test passed\n");
238 /* check mcapi sha against internal */
239 static int check_sha(void)
244 byte mcDigest[CRYPT_SHA_DIGEST_SIZE];
245 byte defDigest[SHA_DIGEST_SIZE];
247 CRYPT_SHA_Initialize(&mcSha);
248 ret = InitSha(&defSha);
250 printf("sha init default failed\n");
254 CRYPT_SHA_DataAdd(&mcSha, ourData, OUR_DATA_SIZE);
255 ShaUpdate(&defSha, ourData, OUR_DATA_SIZE);
257 CRYPT_SHA_Finalize(&mcSha, mcDigest);
258 ShaFinal(&defSha, defDigest);
260 if (memcmp(mcDigest, defDigest, CRYPT_SHA_DIGEST_SIZE) != 0) {
261 printf("sha final memcmp failed\n");
264 printf("sha mcapi test passed\n");
270 /* check mcapi sha256 against internal */
271 static int check_sha256(void)
273 CRYPT_SHA256_CTX mcSha256;
276 byte mcDigest[CRYPT_SHA256_DIGEST_SIZE];
277 byte defDigest[SHA256_DIGEST_SIZE];
279 CRYPT_SHA256_Initialize(&mcSha256);
280 ret = InitSha256(&defSha256);
282 printf("sha256 init default failed\n");
286 CRYPT_SHA256_DataAdd(&mcSha256, ourData, OUR_DATA_SIZE);
287 ret = Sha256Update(&defSha256, ourData, OUR_DATA_SIZE);
289 printf("sha256 update default failed\n");
293 CRYPT_SHA256_Finalize(&mcSha256, mcDigest);
294 ret = Sha256Final(&defSha256, defDigest);
296 printf("sha256 final default failed\n");
300 if (memcmp(mcDigest, defDigest, CRYPT_SHA256_DIGEST_SIZE) != 0) {
301 printf("sha256 final memcmp fialed\n");
304 printf("sha256 mcapi test passed\n");
310 /* check mcapi sha384 against internal */
311 static int check_sha384(void)
313 CRYPT_SHA384_CTX mcSha384;
316 byte mcDigest[CRYPT_SHA384_DIGEST_SIZE];
317 byte defDigest[SHA384_DIGEST_SIZE];
319 CRYPT_SHA384_Initialize(&mcSha384);
320 ret = InitSha384(&defSha384);
322 printf("sha384 init default failed\n");
326 CRYPT_SHA384_DataAdd(&mcSha384, ourData, OUR_DATA_SIZE);
327 ret = Sha384Update(&defSha384, ourData, OUR_DATA_SIZE);
329 printf("sha384 update default failed\n");
333 CRYPT_SHA384_Finalize(&mcSha384, mcDigest);
334 ret = Sha384Final(&defSha384, defDigest);
336 printf("sha384 final default failed\n");
340 if (memcmp(mcDigest, defDigest, CRYPT_SHA384_DIGEST_SIZE) != 0) {
341 printf("sha384 final memcmp fialed\n");
344 printf("sha384 mcapi test passed\n");
350 /* check mcapi sha512 against internal */
351 static int check_sha512(void)
353 CRYPT_SHA512_CTX mcSha512;
356 byte mcDigest[CRYPT_SHA512_DIGEST_SIZE];
357 byte defDigest[SHA512_DIGEST_SIZE];
359 CRYPT_SHA512_Initialize(&mcSha512);
360 ret = InitSha512(&defSha512);
362 printf("sha512 init default failed\n");
366 CRYPT_SHA512_DataAdd(&mcSha512, ourData, OUR_DATA_SIZE);
367 ret = Sha512Update(&defSha512, ourData, OUR_DATA_SIZE);
369 printf("sha512 update default failed\n");
373 CRYPT_SHA512_Finalize(&mcSha512, mcDigest);
374 ret = Sha512Final(&defSha512, defDigest);
376 printf("sha512 final default failed\n");
380 if (memcmp(mcDigest, defDigest, CRYPT_SHA512_DIGEST_SIZE) != 0) {
381 printf("sha512 final memcmp fialed\n");
384 printf("sha512 mcapi test passed\n");
390 /* check mcapi hmac against internal */
391 static int check_hmac(void)
393 CRYPT_HMAC_CTX mcHmac;
396 byte mcDigest[CRYPT_SHA512_DIGEST_SIZE];
397 byte defDigest[SHA512_DIGEST_SIZE];
399 strncpy((char*)key, "Jefe", 4);
402 CRYPT_HMAC_SetKey(&mcHmac, CRYPT_HMAC_SHA, key, 4);
403 ret = HmacSetKey(&defHmac, SHA, key, 4);
405 printf("hmac sha setkey default failed\n");
409 CRYPT_HMAC_DataAdd(&mcHmac, ourData, OUR_DATA_SIZE);
410 ret = HmacUpdate(&defHmac, ourData, OUR_DATA_SIZE);
412 printf("hmac sha update default failed\n");
416 CRYPT_HMAC_Finalize(&mcHmac, mcDigest);
417 ret = HmacFinal(&defHmac, defDigest);
419 printf("hmac sha final default failed\n");
423 if (memcmp(mcDigest, defDigest, CRYPT_SHA_DIGEST_SIZE) != 0) {
424 printf("hmac sha final memcmp fialed\n");
427 printf("hmac sha mcapi test passed\n");
430 CRYPT_HMAC_SetKey(&mcHmac, CRYPT_HMAC_SHA256, key, 4);
431 ret = HmacSetKey(&defHmac, SHA256, key, 4);
433 printf("hmac sha256 setkey default failed\n");
437 CRYPT_HMAC_DataAdd(&mcHmac, ourData, OUR_DATA_SIZE);
438 ret = HmacUpdate(&defHmac, ourData, OUR_DATA_SIZE);
440 printf("hmac sha256 update default failed\n");
444 CRYPT_HMAC_Finalize(&mcHmac, mcDigest);
445 ret = HmacFinal(&defHmac, defDigest);
447 printf("hmac sha256 final default failed\n");
451 if (memcmp(mcDigest, defDigest, CRYPT_SHA256_DIGEST_SIZE) != 0) {
452 printf("hmac sha256 final memcmp fialed\n");
455 printf("hmac sha256 mcapi test passed\n");
458 CRYPT_HMAC_SetKey(&mcHmac, CRYPT_HMAC_SHA384, key, 4);
459 ret = HmacSetKey(&defHmac, SHA384, key, 4);
461 printf("hmac sha384 setkey default failed\n");
465 CRYPT_HMAC_DataAdd(&mcHmac, ourData, OUR_DATA_SIZE);
466 ret = HmacUpdate(&defHmac, ourData, OUR_DATA_SIZE);
468 printf("hmac sha384 update default failed\n");
472 CRYPT_HMAC_Finalize(&mcHmac, mcDigest);
473 ret = HmacFinal(&defHmac, defDigest);
475 printf("hmac sha384 final default failed\n");
479 if (memcmp(mcDigest, defDigest, CRYPT_SHA384_DIGEST_SIZE) != 0) {
480 printf("hmac sha384 final memcmp fialed\n");
483 printf("hmac sha384 mcapi test passed\n");
486 CRYPT_HMAC_SetKey(&mcHmac, CRYPT_HMAC_SHA512, key, 4);
487 ret = HmacSetKey(&defHmac, SHA512, key, 4);
489 printf("hmac sha512 setkey default failed\n");
493 CRYPT_HMAC_DataAdd(&mcHmac, ourData, OUR_DATA_SIZE);
494 ret = HmacUpdate(&defHmac, ourData, OUR_DATA_SIZE);
496 printf("hmac sha512 update default failed\n");
500 CRYPT_HMAC_Finalize(&mcHmac, mcDigest);
501 ret = HmacFinal(&defHmac, defDigest);
503 printf("hmac sha512 final default failed\n");
507 if (memcmp(mcDigest, defDigest, CRYPT_SHA512_DIGEST_SIZE) != 0) {
508 printf("hmac sha512 final memcmp fialed\n");
511 printf("hmac sha512 mcapi test passed\n");
517 /* check mcapi compress against internal */
518 static int check_compress(void)
520 const unsigned char text[] =
521 "Biodiesel cupidatat marfa, cliche aute put a bird on it incididunt elit\n"
522 "polaroid. Sunt tattooed bespoke reprehenderit. Sint twee organic id\n"
523 "marfa. Commodo veniam ad esse gastropub. 3 wolf moon sartorial vero,\n"
524 "plaid delectus biodiesel squid +1 vice. Post-ironic keffiyeh leggings\n"
525 "selfies cray fap hoodie, forage anim. Carles cupidatat shoreditch, VHS\n"
526 "small batch meggings kogi dolore food truck bespoke gastropub.\n"
528 "Terry richardson adipisicing actually typewriter tumblr, twee whatever\n"
529 "four loko you probably haven't heard of them high life. Messenger bag\n"
530 "whatever tattooed deep v mlkshk. Brooklyn pinterest assumenda chillwave\n"
531 "et, banksy ullamco messenger bag umami pariatur direct trade forage.\n"
532 "Typewriter culpa try-hard, pariatur sint brooklyn meggings. Gentrify\n"
533 "food truck next level, tousled irony non semiotics PBR ethical anim cred\n"
534 "readymade. Mumblecore brunch lomo odd future, portland organic terry\n"
535 "four loko whatever street art yr farm-to-table.\n";
537 unsigned int inSz = sizeof(text);
539 unsigned char cBuffer[1024];
540 unsigned char dBuffer[1024];
545 ret1 = CRYPT_HUFFMAN_Compress(cBuffer, sizeof(cBuffer), text, inSz, 0);
546 ret2 = Compress(dBuffer, sizeof(dBuffer), text, inSz, 0);
548 if (ret1 != ret2 || ret1 < 0) {
549 printf("compress dynamic ret failed\n");
553 if (memcmp(cBuffer, dBuffer, ret1) != 0) {
554 printf("compress dynamic cmp failed\n");
560 ret1 = CRYPT_HUFFMAN_DeCompress(dBuffer, sizeof(dBuffer), cBuffer, outSz);
562 if (memcmp(dBuffer, text, inSz) != 0) {
563 printf("mcapi decompress dynamic cmp failed\n");
567 memset(dBuffer, 0, sizeof(dBuffer));
569 ret1 = DeCompress(dBuffer, sizeof(dBuffer), cBuffer, outSz);
571 if (memcmp(dBuffer, text, inSz) != 0) {
572 printf("decompress dynamic cmp failed\n");
576 memset(cBuffer, 0, sizeof(cBuffer));
577 memset(dBuffer, 0, sizeof(dBuffer));
580 ret1 = CRYPT_HUFFMAN_Compress(cBuffer, sizeof(cBuffer), text, inSz, 1);
581 ret2 = Compress(dBuffer, sizeof(dBuffer), text, inSz, 1);
583 if (ret1 != ret2 || ret1 < 0) {
584 printf("compress static ret failed\n");
588 if (memcmp(cBuffer, dBuffer, ret1) != 0) {
589 printf("compress static cmp failed\n");
595 ret1 = CRYPT_HUFFMAN_DeCompress(dBuffer, sizeof(dBuffer), cBuffer, outSz);
597 if (memcmp(dBuffer, text, inSz) != 0) {
598 printf("mcapi decompress static cmp failed\n");
602 memset(dBuffer, 0, sizeof(dBuffer));
604 ret1 = DeCompress(dBuffer, sizeof(dBuffer), cBuffer, outSz);
606 if (memcmp(dBuffer, text, inSz) != 0) {
607 printf("decompress static cmp failed\n");
611 printf("huffman mcapi test passed\n");
617 #define RANDOM_BYTE_SZ 32
619 /* check mcapi rng */
620 static int check_rng(void)
624 byte in[RANDOM_BYTE_SZ];
625 byte out[RANDOM_BYTE_SZ];
627 for (i = 0; i < RANDOM_BYTE_SZ; i++)
630 for (i = 0; i < RANDOM_BYTE_SZ; i++)
633 ret = InitRng(&defRng);
635 printf("default rng init failed\n");
639 ret = CRYPT_RNG_Initialize(&mcRng);
641 printf("mcapi rng init failed\n");
645 ret = CRYPT_RNG_Get(&mcRng, &out[0]);
647 printf("mcapi rng get failed\n");
651 ret = CRYPT_RNG_BlockGenerate(&mcRng, out, RANDOM_BYTE_SZ);
653 printf("mcapi rng block gen failed\n");
657 if (memcmp(in, out, RANDOM_BYTE_SZ) == 0) {
658 printf("mcapi rng block gen output failed\n");
662 printf("rng mcapi test passed\n");
668 #define TDES_TEST_SIZE 32
670 /* check mcapi des3 */
671 static int check_des3(void)
673 CRYPT_TDES_CTX mcDes3;
676 byte out1[TDES_TEST_SIZE];
677 byte out2[TDES_TEST_SIZE];
679 strncpy((char*)key, "1234567890abcdefghijklmn", 24);
680 strncpy((char*)iv, "12345678", 8);
683 ret = CRYPT_TDES_KeySet(&mcDes3, key, iv, CRYPT_TDES_ENCRYPTION);
685 printf("mcapi tdes key set failed\n");
688 ret = Des3_SetKey(&defDes3, key, iv, DES_ENCRYPTION);
690 printf("default des3 key set failed\n");
694 ret = CRYPT_TDES_CBC_Encrypt(&mcDes3, out1, ourData, TDES_TEST_SIZE);
696 printf("mcapi tdes cbc encrypt failed\n");
699 ret = Des3_CbcEncrypt(&defDes3, out2, ourData, TDES_TEST_SIZE);
701 printf("mcapi default tdes cbc encrypt failed\n");
705 if (memcmp(out1, out2, TDES_TEST_SIZE) != 0) {
706 printf("mcapi tdes cbc encrypt cmp failed\n");
711 ret = CRYPT_TDES_KeySet(&mcDes3, key, iv, CRYPT_TDES_DECRYPTION);
713 printf("mcapi tdes key set failed\n");
716 ret = Des3_SetKey(&defDes3, key, iv, DES_DECRYPTION);
718 printf("default des3 key set failed\n");
722 ret = CRYPT_TDES_CBC_Decrypt(&mcDes3, out2, out1, TDES_TEST_SIZE);
724 printf("mcapi tdes cbc decrypt failed\n");
727 ret = Des3_CbcDecrypt(&defDes3, out1, out1, TDES_TEST_SIZE);
729 printf("mcapi default tdes cbc decrypt failed\n");
733 if (memcmp(out1, out2, TDES_TEST_SIZE) != 0) {
734 printf("mcapi tdes cbc decrypt cmp failed\n");
738 if (memcmp(out1, ourData, TDES_TEST_SIZE) != 0) {
739 printf("mcapi tdes cbc decrypt orig cmp failed\n");
743 printf("tdes mcapi test passed\n");
749 #define AES_TEST_SIZE 32
751 /* check mcapi aes cbc */
752 static int check_aescbc(void)
757 byte out1[AES_TEST_SIZE];
758 byte out2[AES_TEST_SIZE];
760 strncpy((char*)key, "1234567890abcdefghijklmnopqrstuv", 32);
761 strncpy((char*)iv, "1234567890abcdef", 16);
763 /* 128 cbc encrypt */
764 ret = CRYPT_AES_KeySet(&mcAes, key, 16, iv, CRYPT_AES_ENCRYPTION);
766 printf("mcapi aes-128 key set failed\n");
769 ret = AesSetKey(&defAes, key, 16, iv, AES_ENCRYPTION);
771 printf("default aes-128 key set failed\n");
775 ret = CRYPT_AES_CBC_Encrypt(&mcAes, out1, ourData, AES_TEST_SIZE);
777 printf("mcapi aes-128 cbc encrypt failed\n");
780 AesCbcEncrypt(&defAes, out2, ourData, AES_TEST_SIZE);
782 if (memcmp(out1, out2, AES_TEST_SIZE) != 0) {
783 printf("mcapi aes-128 cbc encrypt cmp failed\n");
787 /* 128 cbc decrypt */
788 ret = CRYPT_AES_KeySet(&mcAes, key, 16, iv, CRYPT_AES_DECRYPTION);
790 printf("mcapi aes-128 key set failed\n");
793 ret = AesSetKey(&defAes, key, 16, iv, DES_DECRYPTION);
795 printf("default aes-128 key set failed\n");
799 ret = CRYPT_AES_CBC_Decrypt(&mcAes, out2, out1, AES_TEST_SIZE);
801 printf("mcapi aes-128 cbc decrypt failed\n");
804 AesCbcDecrypt(&defAes, out1, out1, AES_TEST_SIZE);
806 if (memcmp(out1, out2, AES_TEST_SIZE) != 0) {
807 printf("mcapi aes-128 cbc decrypt cmp failed\n");
811 if (memcmp(out1, ourData, AES_TEST_SIZE) != 0) {
812 printf("mcapi aes-128 cbc decrypt orig cmp failed\n");
816 /* 192 cbc encrypt */
817 ret = CRYPT_AES_KeySet(&mcAes, key, 24, iv, CRYPT_AES_ENCRYPTION);
819 printf("mcapi aes-192 key set failed\n");
822 ret = AesSetKey(&defAes, key, 24, iv, AES_ENCRYPTION);
824 printf("default aes-192 key set failed\n");
828 ret = CRYPT_AES_CBC_Encrypt(&mcAes, out1, ourData, AES_TEST_SIZE);
830 printf("mcapi aes-192 cbc encrypt failed\n");
833 AesCbcEncrypt(&defAes, out2, ourData, AES_TEST_SIZE);
835 if (memcmp(out1, out2, AES_TEST_SIZE) != 0) {
836 printf("mcapi aes-192 cbc encrypt cmp failed\n");
840 /* 192 cbc decrypt */
841 ret = CRYPT_AES_KeySet(&mcAes, key, 24, iv, CRYPT_AES_DECRYPTION);
843 printf("mcapi aes-192 key set failed\n");
846 ret = AesSetKey(&defAes, key, 24, iv, AES_DECRYPTION);
848 printf("default aes-192 key set failed\n");
852 ret = CRYPT_AES_CBC_Decrypt(&mcAes, out2, out1, AES_TEST_SIZE);
854 printf("mcapi aes-192 cbc decrypt failed\n");
857 AesCbcDecrypt(&defAes, out1, out1, AES_TEST_SIZE);
859 if (memcmp(out1, out2, AES_TEST_SIZE) != 0) {
860 printf("mcapi aes-192 cbc decrypt cmp failed\n");
864 if (memcmp(out1, ourData, AES_TEST_SIZE) != 0) {
865 printf("mcapi aes-192 cbc decrypt orig cmp failed\n");
869 /* 256 cbc encrypt */
870 ret = CRYPT_AES_KeySet(&mcAes, key, 32, iv, CRYPT_AES_ENCRYPTION);
872 printf("mcapi aes-256 key set failed\n");
875 ret = AesSetKey(&defAes, key, 32, iv, AES_ENCRYPTION);
877 printf("default aes-256 key set failed\n");
881 ret = CRYPT_AES_CBC_Encrypt(&mcAes, out1, ourData, AES_TEST_SIZE);
883 printf("mcapi aes-256 cbc encrypt failed\n");
886 AesCbcEncrypt(&defAes, out2, ourData, AES_TEST_SIZE);
888 if (memcmp(out1, out2, AES_TEST_SIZE) != 0) {
889 printf("mcapi aes-256 cbc encrypt cmp failed\n");
893 /* 256 cbc decrypt */
894 ret = CRYPT_AES_KeySet(&mcAes, key, 32, iv, CRYPT_AES_DECRYPTION);
896 printf("mcapi aes-256 key set failed\n");
899 ret = AesSetKey(&defAes, key, 32, iv, AES_DECRYPTION);
901 printf("default aes-256 key set failed\n");
905 ret = CRYPT_AES_CBC_Decrypt(&mcAes, out2, out1, AES_TEST_SIZE);
907 printf("mcapi aes-256 cbc decrypt failed\n");
910 AesCbcDecrypt(&defAes, out1, out1, AES_TEST_SIZE);
912 if (memcmp(out1, out2, AES_TEST_SIZE) != 0) {
913 printf("mcapi aes-256 cbc decrypt cmp failed\n");
917 if (memcmp(out1, ourData, AES_TEST_SIZE) != 0) {
918 printf("mcapi aes-256 cbc decrypt orig cmp failed\n");
922 printf("aes-cbc mcapi test passed\n");
928 /* check mcapi aes ctr */
929 static int check_aesctr(void)
934 byte out1[AES_TEST_SIZE];
935 byte out2[AES_TEST_SIZE];
937 strncpy((char*)key, "1234567890abcdefghijklmnopqrstuv", 32);
938 strncpy((char*)iv, "1234567890abcdef", 16);
940 /* 128 ctr encrypt */
941 ret = CRYPT_AES_KeySet(&mcAes, key, 16, iv, CRYPT_AES_ENCRYPTION);
943 printf("mcapi aes-128 key set failed\n");
946 ret = AesSetKey(&defAes, key, 16, iv, AES_ENCRYPTION);
948 printf("default aes-128 key set failed\n");
952 ret = CRYPT_AES_CTR_Encrypt(&mcAes, out1, ourData, AES_TEST_SIZE);
954 printf("mcapi aes-128 ctr encrypt failed\n");
957 AesCtrEncrypt(&defAes, out2, ourData, AES_TEST_SIZE);
959 if (memcmp(out1, out2, AES_TEST_SIZE) != 0) {
960 printf("mcapi aes-128 ctr encrypt cmp failed\n");
964 /* 128 ctr decrypt */
965 ret = CRYPT_AES_KeySet(&mcAes, key, 16, iv, CRYPT_AES_ENCRYPTION);
967 printf("mcapi aes-128 key set failed\n");
970 ret = AesSetKey(&defAes, key, 16, iv, AES_ENCRYPTION);
972 printf("default aes-128 key set failed\n");
976 ret = CRYPT_AES_CTR_Encrypt(&mcAes, out2, out1, AES_TEST_SIZE);
978 printf("mcapi aes-128 ctr decrypt failed\n");
982 if (memcmp(out2, ourData, AES_TEST_SIZE) != 0) {
983 printf("mcapi aes-128 ctr decrypt orig cmp failed\n");
987 /* 192 ctr encrypt */
988 ret = CRYPT_AES_KeySet(&mcAes, key, 24, iv, CRYPT_AES_ENCRYPTION);
990 printf("mcapi aes-192 key set failed\n");
993 ret = AesSetKey(&defAes, key, 24, iv, AES_ENCRYPTION);
995 printf("default aes-192 key set failed\n");
999 ret = CRYPT_AES_CTR_Encrypt(&mcAes, out1, ourData, AES_TEST_SIZE);
1001 printf("mcapi aes-192 ctr encrypt failed\n");
1004 AesCtrEncrypt(&defAes, out2, ourData, AES_TEST_SIZE);
1006 if (memcmp(out1, out2, AES_TEST_SIZE) != 0) {
1007 printf("mcapi aes-192 ctr encrypt cmp failed\n");
1011 /* 192 ctr decrypt */
1012 ret = CRYPT_AES_KeySet(&mcAes, key, 24, iv, CRYPT_AES_ENCRYPTION);
1014 printf("mcapi aes-192 key set failed\n");
1017 ret = AesSetKey(&defAes, key, 24, iv, AES_DECRYPTION);
1019 printf("default aes-192 key set failed\n");
1023 ret = CRYPT_AES_CTR_Encrypt(&mcAes, out2, out1, AES_TEST_SIZE);
1025 printf("mcapi aes-192 ctr decrypt failed\n");
1029 if (memcmp(out2, ourData, AES_TEST_SIZE) != 0) {
1030 printf("mcapi aes-192 ctr decrypt orig cmp failed\n");
1034 /* 256 ctr encrypt */
1035 ret = CRYPT_AES_KeySet(&mcAes, key, 32, iv, CRYPT_AES_ENCRYPTION);
1037 printf("mcapi aes-256 key set failed\n");
1040 ret = AesSetKey(&defAes, key, 32, iv, AES_ENCRYPTION);
1042 printf("default aes-256 key set failed\n");
1046 ret = CRYPT_AES_CTR_Encrypt(&mcAes, out1, ourData, AES_TEST_SIZE);
1048 printf("mcapi aes-256 ctr encrypt failed\n");
1051 AesCtrEncrypt(&defAes, out2, ourData, AES_TEST_SIZE);
1053 if (memcmp(out1, out2, AES_TEST_SIZE) != 0) {
1054 printf("mcapi aes-256 ctr encrypt cmp failed\n");
1058 /* 256 ctr decrypt */
1059 ret = CRYPT_AES_KeySet(&mcAes, key, 32, iv, CRYPT_AES_ENCRYPTION);
1061 printf("mcapi aes-256 key set failed\n");
1064 ret = AesSetKey(&defAes, key, 32, iv, AES_ENCRYPTION);
1066 printf("default aes-256 key set failed\n");
1070 ret = CRYPT_AES_CTR_Encrypt(&mcAes, out2, out1, AES_TEST_SIZE);
1072 printf("mcapi aes-256 ctr decrypt failed\n");
1076 if (memcmp(out2, ourData, AES_TEST_SIZE) != 0) {
1077 printf("mcapi aes-256 ctr decrypt orig cmp failed\n");
1081 printf("aes-ctr mcapi test passed\n");
1087 /* check mcapi aes direct */
1088 static int check_aesdirect(void)
1090 CRYPT_AES_CTX mcAes;
1093 byte out1[CRYPT_AES_BLOCK_SIZE];
1094 byte out2[16]; /* one block at a time */
1096 strncpy((char*)key, "1234567890abcdefghijklmnopqrstuv", 32);
1097 strncpy((char*)iv, "1234567890abcdef", 16);
1099 /* 128 direct encrypt */
1100 ret = CRYPT_AES_KeySet(&mcAes, key, 16, iv, CRYPT_AES_ENCRYPTION);
1102 printf("mcapi aes-128 key set failed\n");
1105 ret = AesSetKey(&defAes, key, 16, iv, AES_ENCRYPTION);
1107 printf("default aes-128 key set failed\n");
1111 ret = CRYPT_AES_DIRECT_Encrypt(&mcAes, out1, ourData);
1113 printf("mcapi aes-128 direct encrypt failed\n");
1116 AesEncryptDirect(&defAes, out2, ourData);
1118 if (memcmp(out1, out2, CRYPT_AES_BLOCK_SIZE) != 0) {
1119 printf("mcapi aes-128 direct encrypt cmp failed\n");
1123 /* 128 direct decrypt */
1124 ret = CRYPT_AES_KeySet(&mcAes, key, 16, iv, CRYPT_AES_DECRYPTION);
1126 printf("mcapi aes-128 key set failed\n");
1129 ret = AesSetKey(&defAes, key, 16, iv, DES_DECRYPTION);
1131 printf("default aes-128 key set failed\n");
1135 ret = CRYPT_AES_DIRECT_Decrypt(&mcAes, out2, out1);
1137 printf("mcapi aes-128 direct decrypt failed\n");
1140 AesDecryptDirect(&defAes, out1, out1);
1142 if (memcmp(out1, out2, CRYPT_AES_BLOCK_SIZE) != 0) {
1143 printf("mcapi aes-128 direct decrypt cmp failed\n");
1147 if (memcmp(out1, ourData, CRYPT_AES_BLOCK_SIZE) != 0) {
1148 printf("mcapi aes-128 direct decrypt orig cmp failed\n");
1152 /* 192 direct encrypt */
1153 ret = CRYPT_AES_KeySet(&mcAes, key, 24, iv, CRYPT_AES_ENCRYPTION);
1155 printf("mcapi aes-192 key set failed\n");
1158 ret = AesSetKey(&defAes, key, 24, iv, AES_ENCRYPTION);
1160 printf("default aes-192 key set failed\n");
1164 ret = CRYPT_AES_DIRECT_Encrypt(&mcAes, out1, ourData);
1166 printf("mcapi aes-192 direct encrypt failed\n");
1169 AesEncryptDirect(&defAes, out2, ourData);
1171 if (memcmp(out1, out2, CRYPT_AES_BLOCK_SIZE) != 0) {
1172 printf("mcapi aes-192 direct encrypt cmp failed\n");
1176 /* 192 direct decrypt */
1177 ret = CRYPT_AES_KeySet(&mcAes, key, 24, iv, CRYPT_AES_DECRYPTION);
1179 printf("mcapi aes-192 key set failed\n");
1182 ret = AesSetKey(&defAes, key, 24, iv, AES_DECRYPTION);
1184 printf("default aes-192 key set failed\n");
1188 ret = CRYPT_AES_DIRECT_Decrypt(&mcAes, out2, out1);
1190 printf("mcapi aes-192 direct decrypt failed\n");
1193 AesDecryptDirect(&defAes, out1, out1);
1195 if (memcmp(out1, out2, CRYPT_AES_BLOCK_SIZE) != 0) {
1196 printf("mcapi aes-192 direct decrypt cmp failed\n");
1200 if (memcmp(out1, ourData, CRYPT_AES_BLOCK_SIZE) != 0) {
1201 printf("mcapi aes-192 direct decrypt orig cmp failed\n");
1205 /* 256 direct encrypt */
1206 ret = CRYPT_AES_KeySet(&mcAes, key, 32, iv, CRYPT_AES_ENCRYPTION);
1208 printf("mcapi aes-256 key set failed\n");
1211 ret = AesSetKey(&defAes, key, 32, iv, AES_ENCRYPTION);
1213 printf("default aes-256 key set failed\n");
1217 ret = CRYPT_AES_DIRECT_Encrypt(&mcAes, out1, ourData);
1219 printf("mcapi aes-256 direct encrypt failed\n");
1222 AesEncryptDirect(&defAes, out2, ourData);
1224 if (memcmp(out1, out2, CRYPT_AES_BLOCK_SIZE) != 0) {
1225 printf("mcapi aes-256 direct encrypt cmp failed\n");
1229 /* 256 direct decrypt */
1230 ret = CRYPT_AES_KeySet(&mcAes, key, 32, iv, CRYPT_AES_DECRYPTION);
1232 printf("mcapi aes-256 key set failed\n");
1235 ret = AesSetKey(&defAes, key, 32, iv, AES_DECRYPTION);
1237 printf("default aes-256 key set failed\n");
1241 ret = CRYPT_AES_DIRECT_Decrypt(&mcAes, out2, out1);
1243 printf("mcapi aes-256 direct decrypt failed\n");
1246 AesDecryptDirect(&defAes, out1, out1);
1248 if (memcmp(out1, out2, CRYPT_AES_BLOCK_SIZE) != 0) {
1249 printf("mcapi aes-256 direct decrypt cmp failed\n");
1253 if (memcmp(out1, ourData, CRYPT_AES_BLOCK_SIZE) != 0) {
1254 printf("mcapi aes-256 direct decrypt orig cmp failed\n");
1258 printf("aes-direct mcapi test passed\n");
1264 #define RSA_TEST_SIZE 64
1266 /* check mcapi rsa */
1267 static int check_rsa(void)
1269 CRYPT_RSA_CTX mcRsa;
1273 unsigned int keySz = (unsigned int)sizeof(client_key_der_1024);
1274 unsigned int idx = 0;
1278 ret = InitRsaKey(&defRsa, NULL);
1280 ret = CRYPT_RSA_Initialize(&mcRsa);
1282 printf("mcapi rsa init failed\n");
1286 ret = CRYPT_RSA_PrivateKeyDecode(&mcRsa, client_key_der_1024, keySz);
1288 printf("mcapi rsa private key decode failed\n");
1292 ret = RsaPrivateKeyDecode(client_key_der_1024, &idx, &defRsa, keySz);
1294 printf("default rsa private key decode failed\n");
1298 ret = CRYPT_RSA_PublicEncrypt(&mcRsa, out1, sizeof(out1), ourData,
1299 RSA_TEST_SIZE, &mcRng);
1301 printf("mcapi rsa public encrypt failed\n");
1305 ret2 = RsaPublicEncrypt(ourData, RSA_TEST_SIZE, out2, sizeof(out2),
1308 printf("default rsa public encrypt failed\n");
1313 printf("default rsa public encrypt sz != mcapi sz\n");
1317 if (ret != CRYPT_RSA_EncryptSizeGet(&mcRsa)) {
1318 printf("mcapi encrypt sz get != mcapi sz\n");
1322 ret = CRYPT_RSA_PrivateDecrypt(&mcRsa, out2, sizeof(out2), out1, ret);
1324 printf("mcapi rsa private derypt failed\n");
1328 if (ret != RSA_TEST_SIZE) {
1329 printf("mcapi rsa private derypt plain size wrong\n");
1333 if (memcmp(out2, ourData, ret) != 0) {
1334 printf("mcapi rsa private derypt plain text bad\n");
1338 FreeRsaKey(&defRsa);
1339 ret = CRYPT_RSA_Free(&mcRsa);
1341 printf("mcapi rsa free failed\n");
1345 printf("rsa mcapi test passed\n");
1351 /* check mcapi ecc */
1352 static int check_ecc(void)
1354 CRYPT_ECC_CTX userA;
1355 CRYPT_ECC_CTX userB;
1360 unsigned int aSz = (unsigned int)sizeof(sharedA);
1361 unsigned int bSz = (unsigned int)sizeof(sharedB);
1362 unsigned int sigSz = (unsigned int)sizeof(sig);
1363 unsigned int usedA = 0;
1364 unsigned int usedB = 0;
1365 int verifyStatus = 0;
1368 ret = CRYPT_ECC_Initialize(&userA);
1370 printf("mcapi ecc init failed\n");
1374 ret = CRYPT_ECC_Initialize(&userB);
1376 printf("mcapi ecc init b failed\n");
1381 ret = CRYPT_ECC_DHE_KeyMake(&userA, &mcRng, 32);
1383 printf("mcapi ecc make key failed\n");
1387 ret = CRYPT_ECC_DHE_KeyMake(&userB, &mcRng, 32);
1389 printf("mcapi ecc make key b failed\n");
1393 ret = CRYPT_ECC_KeySizeGet(&userA);
1395 printf("mcapi ecc key size get failed\n");
1399 ret = CRYPT_ECC_SignatureSizeGet(&userA);
1401 printf("mcapi ecc signature size get failed\n");
1405 ret = CRYPT_ECC_DHE_SharedSecretMake(&userA, &userB, sharedA, aSz, &usedA);
1407 printf("mcapi ecc make shared secret failed\n");
1411 ret = CRYPT_ECC_DHE_SharedSecretMake(&userB, &userA, sharedB, bSz, &usedB);
1413 printf("mcapi ecc make shared secret failed\n");
1417 if (usedA != usedB || usedA <= 0) {
1418 printf("mcapi ecc make shared secret output size match failed\n");
1422 if (memcmp(sharedA, sharedB, usedA) != 0) {
1423 printf("mcapi ecc make shared secret output match cmp failed\n");
1428 ret = CRYPT_ECC_DSA_HashSign(&userA, &mcRng, sig, sigSz, &usedA, ourData,
1429 CRYPT_SHA_DIGEST_SIZE);
1431 printf("mcapi ecc sign hash failed\n");
1437 printf("mcapi ecc sign hash bad sig size\n");
1441 ret = CRYPT_ECC_DSA_HashVerify(&userA, sig, sigSz, ourData,
1442 CRYPT_SHA_DIGEST_SIZE, &verifyStatus);
1444 printf("mcapi ecc verify hash failed\n");
1447 if (verifyStatus != 1) {
1448 printf("mcapi ecc verify hash status failed\n");
1452 /* import / export */
1454 ret = CRYPT_ECC_PublicExport(&userA, sharedA, aSz, &usedA);
1456 printf("mcapi ecc public export failed\n");
1460 ret = CRYPT_ECC_PublicImport(&userB, sharedA, usedA);
1462 printf("mcapi ecc public import failed\n");
1466 ret = CRYPT_ECC_Free(&userA);
1468 printf("mcapi ecc free failed\n");
1472 ret = CRYPT_ECC_Free(&userB);
1474 printf("mcapi ecc free b failed\n");
1478 printf("ecc mcapi test passed\n");