mcapi_test.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512
  1. /* mcapi_test.c
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL 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. * wolfSSL 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-1335, USA
  20. */
  21. /* Tests Microchip CRYPTO API layer */
  22. /* mc api header */
  23. #include <wolfssl/wolfcrypt/settings.h>
  24. #include "crypto.h"
  25. /* sanity test against our default implementation, wolfssl headers */
  26. #include <wolfssl/wolfcrypt/md5.h>
  27. #include <wolfssl/wolfcrypt/sha.h>
  28. #include <wolfssl/wolfcrypt/sha256.h>
  29. #include <wolfssl/wolfcrypt/sha512.h>
  30. #include <wolfssl/wolfcrypt/hmac.h>
  31. #include <wolfssl/wolfcrypt/compress.h>
  32. #include <wolfssl/wolfcrypt/random.h>
  33. #include <wolfssl/wolfcrypt/des3.h>
  34. #include <wolfssl/wolfcrypt/aes.h>
  35. #include <wolfssl/wolfcrypt/ecc.h>
  36. #include <wolfssl/wolfcrypt/rsa.h>
  37. #define USE_CERT_BUFFERS_1024
  38. #include <wolfssl/certs_test.h>
  39. #if defined(WOLFSSL_MICROCHIP_PIC32MZ)
  40. #define MICROCHIP_PIC32
  41. #include <xc.h>
  42. #pragma config ICESEL = ICS_PGx2
  43. /* ICE/ICD Comm Channel Select (Communicate on PGEC2/PGED2) */
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46. #include "PIC32MZ-serial.h"
  47. #define SYSTEMConfigPerformance(n) /* void out SYSTEMConfigPerformance(); */
  48. #elif defined(MICROCHIP_PIC32)
  49. #define PIC32_STARTER_KIT
  50. #include <stdio.h>
  51. #include <stdlib.h>
  52. #include <p32xxxx.h>
  53. #define _SUPPRESS_PLIB_WARNING
  54. #define _DISABLE_OPENADC10_CONFIGPORT_WARNING
  55. #include <plib.h>
  56. #include <sys/appio.h>
  57. #define init_serial() /* void out init_serial() */
  58. #else
  59. #include <stdio.h> /* order matters above ? */
  60. #endif
  61. #define OUR_DATA_SIZE 1024
  62. static byte ourData[OUR_DATA_SIZE];
  63. static byte* key = NULL;
  64. static byte* iv = NULL;
  65. static CRYPT_RNG_CTX mcRng;
  66. static WC_RNG defRng;
  67. static int check_md5(void);
  68. static int check_sha(void);
  69. static int check_sha256(void);
  70. static int check_sha384(void);
  71. static int check_sha512(void);
  72. static int check_hmac(void);
  73. static int check_compress(void);
  74. static int check_rng(void);
  75. static int check_des3(void);
  76. static int check_aescbc(void);
  77. static int check_aesctr(void);
  78. static int check_aesdirect(void);
  79. static int check_rsa(void);
  80. static int check_ecc(void);
  81. int main(int argc, char** argv)
  82. {
  83. int ret;
  84. int i;
  85. (void)argc;
  86. (void)argv;
  87. #if defined(MICROCHIP_PIC32)
  88. init_serial() ; /* initialize PIC32MZ serial I/O */
  89. SYSTEMConfigPerformance(80000000);
  90. DBINIT();
  91. #endif
  92. /* align key, iv pointers */
  93. key = (byte*)XMALLOC(32, NULL, DYNAMIC_TYPE_KEY);
  94. if (key == NULL) {
  95. printf("mcapi key alloc failed\n");
  96. return -1;
  97. }
  98. iv = (byte*)XMALLOC(16, NULL, DYNAMIC_TYPE_KEY);
  99. if (iv == NULL) {
  100. printf("mcapi iv alloc failed\n");
  101. return -1;
  102. }
  103. for (i = 0; i < OUR_DATA_SIZE; i++)
  104. ourData[i] = (byte)i;
  105. ret = check_md5();
  106. if (ret != 0) {
  107. printf("mcapi check_md5 failed\n");
  108. return -1;
  109. }
  110. ret = check_sha();
  111. if (ret != 0) {
  112. printf("mcapi check_sha failed\n");
  113. return -1;
  114. }
  115. ret = check_sha256();
  116. if (ret != 0) {
  117. printf("mcapi check_sha256 failed\n");
  118. return -1;
  119. }
  120. ret = check_sha384();
  121. if (ret != 0) {
  122. printf("mcapi check_sha384 failed\n");
  123. return -1;
  124. }
  125. ret = check_sha512();
  126. if (ret != 0) {
  127. printf("mcapi check_sha512 failed\n");
  128. return -1;
  129. }
  130. ret = check_hmac();
  131. if (ret != 0) {
  132. printf("mcapi check_hmac failed\n");
  133. return -1;
  134. }
  135. ret = check_compress();
  136. if (ret != 0) {
  137. printf("mcapi check_compress failed\n");
  138. return -1;
  139. }
  140. ret = check_rng();
  141. if (ret != 0) {
  142. printf("mcapi check_rng failed\n");
  143. return -1;
  144. }
  145. ret = check_des3();
  146. if (ret != 0) {
  147. printf("mcapi check_des3 failed\n");
  148. return -1;
  149. }
  150. ret = check_aescbc();
  151. if (ret != 0) {
  152. printf("mcapi check_aes cbc failed\n");
  153. return -1;
  154. }
  155. ret = check_aesctr();
  156. if (ret != 0) {
  157. printf("mcapi check_aes ctr failed\n");
  158. return -1;
  159. }
  160. ret = check_aesdirect();
  161. if (ret != 0) {
  162. printf("mcapi check_aes direct failed\n");
  163. return -1;
  164. }
  165. ret = check_rsa();
  166. if (ret != 0) {
  167. printf("mcapi check_rsa failed\n");
  168. return -1;
  169. }
  170. ret = check_ecc();
  171. if (ret != 0) {
  172. printf("mcapi check_ecc failed\n");
  173. return -1;
  174. }
  175. XFREE(iv, NULL, DYNAMIC_TYPE_KEY);
  176. XFREE(key, NULL, DYNAMIC_TYPE_KEY);
  177. return 0;
  178. }
  179. /* check mcapi md5 against internal */
  180. static int check_md5(void)
  181. {
  182. CRYPT_MD5_CTX mcMd5;
  183. Md5 defMd5;
  184. int ret;
  185. byte mcDigest[CRYPT_MD5_DIGEST_SIZE];
  186. byte defDigest[MD5_DIGEST_SIZE];
  187. CRYPT_MD5_Initialize(&mcMd5);
  188. ret = wc_InitMd5(&defMd5);
  189. if (ret == 0) {
  190. CRYPT_MD5_DataAdd(&mcMd5, ourData, OUR_DATA_SIZE);
  191. ret = wc_Md5Update(&defMd5, ourData, OUR_DATA_SIZE);
  192. }
  193. if (ret == 0) {
  194. CRYPT_MD5_Finalize(&mcMd5, mcDigest);
  195. ret = wc_Md5Final(&defMd5, defDigest);
  196. }
  197. if (ret != 0) {
  198. printf("md5 failed\n");
  199. return -1;
  200. }
  201. if (memcmp(mcDigest, defDigest, CRYPT_MD5_DIGEST_SIZE) != 0) {
  202. printf("md5 final memcmp failed\n");
  203. return -1;
  204. }
  205. printf("md5 mcapi test passed\n");
  206. return ret;
  207. }
  208. /* check mcapi sha against internal */
  209. static int check_sha(void)
  210. {
  211. CRYPT_SHA_CTX mcSha;
  212. Sha defSha;
  213. int ret = 0;
  214. byte mcDigest[CRYPT_SHA_DIGEST_SIZE];
  215. byte defDigest[SHA_DIGEST_SIZE];
  216. CRYPT_SHA_Initialize(&mcSha);
  217. ret = wc_InitSha(&defSha);
  218. if (ret != 0) {
  219. printf("sha init default failed\n");
  220. return -1;
  221. }
  222. CRYPT_SHA_DataAdd(&mcSha, ourData, OUR_DATA_SIZE);
  223. wc_ShaUpdate(&defSha, ourData, OUR_DATA_SIZE);
  224. CRYPT_SHA_Finalize(&mcSha, mcDigest);
  225. wc_ShaFinal(&defSha, defDigest);
  226. if (memcmp(mcDigest, defDigest, CRYPT_SHA_DIGEST_SIZE) != 0) {
  227. printf("sha final memcmp failed\n");
  228. return -1;
  229. }
  230. printf("sha mcapi test passed\n");
  231. return 0;
  232. }
  233. /* check mcapi sha256 against internal */
  234. static int check_sha256(void)
  235. {
  236. CRYPT_SHA256_CTX mcSha256;
  237. wc_Sha256 defSha256;
  238. int ret;
  239. byte mcDigest[CRYPT_SHA256_DIGEST_SIZE];
  240. byte defDigest[WC_SHA256_DIGEST_SIZE];
  241. CRYPT_SHA256_Initialize(&mcSha256);
  242. ret = wc_InitSha256(&defSha256);
  243. if (ret != 0) {
  244. printf("sha256 init default failed\n");
  245. return -1;
  246. }
  247. CRYPT_SHA256_DataAdd(&mcSha256, ourData, OUR_DATA_SIZE);
  248. ret = wc_Sha256Update(&defSha256, ourData, OUR_DATA_SIZE);
  249. if (ret != 0) {
  250. printf("sha256 update default failed\n");
  251. return -1;
  252. }
  253. CRYPT_SHA256_Finalize(&mcSha256, mcDigest);
  254. ret = wc_Sha256Final(&defSha256, defDigest);
  255. if (ret != 0) {
  256. printf("sha256 final default failed\n");
  257. return -1;
  258. }
  259. if (memcmp(mcDigest, defDigest, CRYPT_SHA256_DIGEST_SIZE) != 0) {
  260. printf("sha256 final memcmp failed\n");
  261. return -1;
  262. }
  263. printf("sha256 mcapi test passed\n");
  264. return 0;
  265. }
  266. /* check mcapi sha384 against internal */
  267. static int check_sha384(void)
  268. {
  269. CRYPT_SHA384_CTX mcSha384;
  270. wc_Sha384 defSha384;
  271. int ret;
  272. byte mcDigest[CRYPT_SHA384_DIGEST_SIZE];
  273. byte defDigest[WC_SHA384_DIGEST_SIZE];
  274. CRYPT_SHA384_Initialize(&mcSha384);
  275. ret = wc_InitSha384(&defSha384);
  276. if (ret != 0) {
  277. printf("sha384 init default failed\n");
  278. return -1;
  279. }
  280. CRYPT_SHA384_DataAdd(&mcSha384, ourData, OUR_DATA_SIZE);
  281. ret = wc_Sha384Update(&defSha384, ourData, OUR_DATA_SIZE);
  282. if (ret != 0) {
  283. printf("sha384 update default failed\n");
  284. return -1;
  285. }
  286. CRYPT_SHA384_Finalize(&mcSha384, mcDigest);
  287. ret = wc_Sha384Final(&defSha384, defDigest);
  288. if (ret != 0) {
  289. printf("sha384 final default failed\n");
  290. return -1;
  291. }
  292. if (memcmp(mcDigest, defDigest, CRYPT_SHA384_DIGEST_SIZE) != 0) {
  293. printf("sha384 final memcmp failed\n");
  294. return -1;
  295. }
  296. printf("sha384 mcapi test passed\n");
  297. return 0;
  298. }
  299. /* check mcapi sha512 against internal */
  300. static int check_sha512(void)
  301. {
  302. CRYPT_SHA512_CTX mcSha512;
  303. wc_Sha512 defSha512;
  304. int ret;
  305. byte mcDigest[CRYPT_SHA512_DIGEST_SIZE];
  306. byte defDigest[WC_SHA512_DIGEST_SIZE];
  307. CRYPT_SHA512_Initialize(&mcSha512);
  308. ret = wc_InitSha512(&defSha512);
  309. if (ret != 0) {
  310. printf("sha512 init default failed\n");
  311. return -1;
  312. }
  313. CRYPT_SHA512_DataAdd(&mcSha512, ourData, OUR_DATA_SIZE);
  314. ret = wc_Sha512Update(&defSha512, ourData, OUR_DATA_SIZE);
  315. if (ret != 0) {
  316. printf("sha512 update default failed\n");
  317. return -1;
  318. }
  319. CRYPT_SHA512_Finalize(&mcSha512, mcDigest);
  320. ret = wc_Sha512Final(&defSha512, defDigest);
  321. if (ret != 0) {
  322. printf("sha512 final default failed\n");
  323. return -1;
  324. }
  325. if (memcmp(mcDigest, defDigest, CRYPT_SHA512_DIGEST_SIZE) != 0) {
  326. printf("sha512 final memcmp failed\n");
  327. return -1;
  328. }
  329. printf("sha512 mcapi test passed\n");
  330. return 0;
  331. }
  332. /* check mcapi hmac against internal */
  333. static int check_hmac(void)
  334. {
  335. CRYPT_HMAC_CTX mcHmac;
  336. Hmac defHmac;
  337. int ret;
  338. byte mcDigest[CRYPT_SHA512_DIGEST_SIZE];
  339. byte defDigest[WC_SHA512_DIGEST_SIZE];
  340. memcpy((char*)key, "Jefe", 4);
  341. /* SHA1 */
  342. CRYPT_HMAC_SetKey(&mcHmac, CRYPT_HMAC_SHA, key, 4);
  343. ret = wc_HmacSetKey(&defHmac, WC_SHA, key, 4);
  344. if (ret != 0) {
  345. printf("hmac sha setkey default failed\n");
  346. return -1;
  347. }
  348. CRYPT_HMAC_DataAdd(&mcHmac, ourData, OUR_DATA_SIZE);
  349. ret = wc_HmacUpdate(&defHmac, ourData, OUR_DATA_SIZE);
  350. if (ret != 0) {
  351. printf("hmac sha update default failed\n");
  352. return -1;
  353. }
  354. CRYPT_HMAC_Finalize(&mcHmac, mcDigest);
  355. ret = wc_HmacFinal(&defHmac, defDigest);
  356. if (ret != 0) {
  357. printf("hmac sha final default failed\n");
  358. return -1;
  359. }
  360. if (memcmp(mcDigest, defDigest, CRYPT_SHA_DIGEST_SIZE) != 0) {
  361. printf("hmac sha final memcmp failed\n");
  362. return -1;
  363. }
  364. printf("hmac sha mcapi test passed\n");
  365. /* SHA-256 */
  366. CRYPT_HMAC_SetKey(&mcHmac, CRYPT_HMAC_SHA256, key, 4);
  367. ret = wc_HmacSetKey(&defHmac, WC_SHA256, key, 4);
  368. if (ret != 0) {
  369. printf("hmac sha256 setkey default failed\n");
  370. return -1;
  371. }
  372. CRYPT_HMAC_DataAdd(&mcHmac, ourData, OUR_DATA_SIZE);
  373. ret = wc_HmacUpdate(&defHmac, ourData, OUR_DATA_SIZE);
  374. if (ret != 0) {
  375. printf("hmac sha256 update default failed\n");
  376. return -1;
  377. }
  378. CRYPT_HMAC_Finalize(&mcHmac, mcDigest);
  379. ret = wc_HmacFinal(&defHmac, defDigest);
  380. if (ret != 0) {
  381. printf("hmac sha256 final default failed\n");
  382. return -1;
  383. }
  384. if (memcmp(mcDigest, defDigest, CRYPT_SHA256_DIGEST_SIZE) != 0) {
  385. printf("hmac sha256 final memcmp failed\n");
  386. return -1;
  387. }
  388. printf("hmac sha256 mcapi test passed\n");
  389. /* SHA-384 */
  390. CRYPT_HMAC_SetKey(&mcHmac, CRYPT_HMAC_SHA384, key, 4);
  391. ret = wc_HmacSetKey(&defHmac, WC_SHA384, key, 4);
  392. if (ret != 0) {
  393. printf("hmac sha384 setkey default failed\n");
  394. return -1;
  395. }
  396. CRYPT_HMAC_DataAdd(&mcHmac, ourData, OUR_DATA_SIZE);
  397. ret = wc_HmacUpdate(&defHmac, ourData, OUR_DATA_SIZE);
  398. if (ret != 0) {
  399. printf("hmac sha384 update default failed\n");
  400. return -1;
  401. }
  402. CRYPT_HMAC_Finalize(&mcHmac, mcDigest);
  403. ret = wc_HmacFinal(&defHmac, defDigest);
  404. if (ret != 0) {
  405. printf("hmac sha384 final default failed\n");
  406. return -1;
  407. }
  408. if (memcmp(mcDigest, defDigest, CRYPT_SHA384_DIGEST_SIZE) != 0) {
  409. printf("hmac sha384 final memcmp failed\n");
  410. return -1;
  411. }
  412. printf("hmac sha384 mcapi test passed\n");
  413. /* SHA-512 */
  414. CRYPT_HMAC_SetKey(&mcHmac, CRYPT_HMAC_SHA512, key, 4);
  415. ret = wc_HmacSetKey(&defHmac, WC_SHA512, key, 4);
  416. if (ret != 0) {
  417. printf("hmac sha512 setkey default failed\n");
  418. return -1;
  419. }
  420. CRYPT_HMAC_DataAdd(&mcHmac, ourData, OUR_DATA_SIZE);
  421. ret = wc_HmacUpdate(&defHmac, ourData, OUR_DATA_SIZE);
  422. if (ret != 0) {
  423. printf("hmac sha512 update default failed\n");
  424. return -1;
  425. }
  426. CRYPT_HMAC_Finalize(&mcHmac, mcDigest);
  427. ret = wc_HmacFinal(&defHmac, defDigest);
  428. if (ret != 0) {
  429. printf("hmac sha512 final default failed\n");
  430. return -1;
  431. }
  432. if (memcmp(mcDigest, defDigest, CRYPT_SHA512_DIGEST_SIZE) != 0) {
  433. printf("hmac sha512 final memcmp failed\n");
  434. return -1;
  435. }
  436. printf("hmac sha512 mcapi test passed\n");
  437. return 0;
  438. }
  439. /* check mcapi compress against internal */
  440. static int check_compress(void)
  441. {
  442. const unsigned char text[] =
  443. "Biodiesel cupidatat marfa, cliche aute put a bird on it incididunt elit\n"
  444. "polaroid. Sunt tattooed bespoke reprehenderit. Sint twee organic id\n"
  445. "marfa. Commodo veniam ad esse gastropub. 3 wolf moon sartorial vero,\n"
  446. "plaid delectus biodiesel squid +1 vice. Post-ironic keffiyeh leggings\n"
  447. "selfies cray fap hoodie, forage anim. Carles cupidatat shoreditch, VHS\n"
  448. "small batch meggings kogi dolore food truck bespoke gastropub.\n"
  449. "\n"
  450. "Terry richardson adipisicing actually typewriter tumblr, twee whatever\n"
  451. "four loko you probably haven't heard of them high life. Messenger bag\n"
  452. "whatever tattooed deep v mlkshk. Brooklyn pinterest assumenda chillwave\n"
  453. "et, banksy ullamco messenger bag umami pariatur direct trade forage.\n"
  454. "Typewriter culpa try-hard, pariatur sint brooklyn meggings. Gentrify\n"
  455. "food truck next level, tousled irony non semiotics PBR ethical anim cred\n"
  456. "readymade. Mumblecore brunch lomo odd future, portland organic terry\n"
  457. "four loko whatever street art yr farm-to-table.\n";
  458. unsigned int inSz = sizeof(text);
  459. unsigned int outSz;
  460. unsigned char cBuffer[1024];
  461. unsigned char dBuffer[1024];
  462. int ret1, ret2;
  463. /* dynamic */
  464. ret1 = CRYPT_HUFFMAN_Compress(cBuffer, sizeof(cBuffer), text, inSz, 0);
  465. ret2 = wc_Compress(dBuffer, sizeof(dBuffer), text, inSz, 0);
  466. if (ret1 != ret2 || ret1 < 0) {
  467. printf("compress dynamic ret failed\n");
  468. return -1;
  469. }
  470. outSz = ret1;
  471. if (memcmp(cBuffer, dBuffer, outSz) != 0) {
  472. printf("compress dynamic cmp failed\n");
  473. return -1;
  474. }
  475. ret1 = CRYPT_HUFFMAN_DeCompress(dBuffer, sizeof(dBuffer), cBuffer, outSz);
  476. if (memcmp(dBuffer, text, inSz) != 0) {
  477. printf("mcapi decompress dynamic cmp failed\n");
  478. return -1;
  479. }
  480. memset(dBuffer, 0, sizeof(dBuffer));
  481. ret2 = wc_DeCompress(dBuffer, sizeof(dBuffer), cBuffer, outSz);
  482. if (ret1 != ret2 || ret2 < 0) {
  483. printf("decompress dynamic ret failed\n");
  484. }
  485. if (memcmp(dBuffer, text, inSz) != 0) {
  486. printf("decompress dynamic cmp failed\n");
  487. return -1;
  488. }
  489. memset(cBuffer, 0, sizeof(cBuffer));
  490. memset(dBuffer, 0, sizeof(dBuffer));
  491. /* static */
  492. ret1 = CRYPT_HUFFMAN_Compress(cBuffer, sizeof(cBuffer), text, inSz, 1);
  493. ret2 = wc_Compress(dBuffer, sizeof(dBuffer), text, inSz, 1);
  494. if (ret1 != ret2 || ret1 < 0) {
  495. printf("compress static ret failed\n");
  496. return -1;
  497. }
  498. outSz = ret1;
  499. if (memcmp(cBuffer, dBuffer, outSz) != 0) {
  500. printf("compress static cmp failed\n");
  501. return -1;
  502. }
  503. ret1 = CRYPT_HUFFMAN_DeCompress(dBuffer, sizeof(dBuffer), cBuffer, outSz);
  504. if (memcmp(dBuffer, text, inSz) != 0) {
  505. printf("mcapi decompress static cmp failed\n");
  506. return -1;
  507. }
  508. memset(dBuffer, 0, sizeof(dBuffer));
  509. ret2 = wc_DeCompress(dBuffer, sizeof(dBuffer), cBuffer, outSz);
  510. if (ret1 != ret2 || ret2 < 0) {
  511. printf("decompress static ret failed\n");
  512. }
  513. if (memcmp(dBuffer, text, inSz) != 0) {
  514. printf("decompress static cmp failed\n");
  515. return -1;
  516. }
  517. printf("huffman mcapi test passed\n");
  518. return 0;
  519. }
  520. #define RANDOM_BYTE_SZ 32
  521. /* check mcapi rng */
  522. static int check_rng(void)
  523. {
  524. int ret;
  525. int i;
  526. byte in[RANDOM_BYTE_SZ];
  527. byte out[RANDOM_BYTE_SZ];
  528. for (i = 0; i < RANDOM_BYTE_SZ; i++)
  529. in[i] = (byte)i;
  530. for (i = 0; i < RANDOM_BYTE_SZ; i++)
  531. out[i] = (byte)i;
  532. ret = wc_InitRng(&defRng);
  533. if (ret != 0) {
  534. printf("default rng init failed\n");
  535. return -1;
  536. }
  537. ret = CRYPT_RNG_Initialize(&mcRng);
  538. if (ret != 0) {
  539. printf("mcapi rng init failed\n");
  540. return -1;
  541. }
  542. ret = CRYPT_RNG_Get(&mcRng, &out[0]);
  543. if (ret != 0) {
  544. printf("mcapi rng get failed\n");
  545. return -1;
  546. }
  547. ret = CRYPT_RNG_BlockGenerate(&mcRng, out, RANDOM_BYTE_SZ);
  548. if (ret != 0) {
  549. printf("mcapi rng block gen failed\n");
  550. return -1;
  551. }
  552. if (memcmp(in, out, RANDOM_BYTE_SZ) == 0) {
  553. printf("mcapi rng block gen output failed\n");
  554. return -1;
  555. }
  556. printf("rng mcapi test passed\n");
  557. return 0;
  558. }
  559. #define TDES_TEST_SIZE 32
  560. /* check mcapi des3 */
  561. static int check_des3(void)
  562. {
  563. CRYPT_TDES_CTX mcDes3;
  564. Des3 defDes3;
  565. int ret;
  566. byte out1[TDES_TEST_SIZE];
  567. byte out2[TDES_TEST_SIZE];
  568. memcpy((char*)key, "1234567890abcdefghijklmn", 24);
  569. memcpy((char*)iv, "12345678", 8);
  570. /* cbc encrypt */
  571. ret = CRYPT_TDES_KeySet(&mcDes3, key, iv, CRYPT_TDES_ENCRYPTION);
  572. if (ret != 0) {
  573. printf("mcapi tdes key set failed\n");
  574. return -1;
  575. }
  576. ret = wc_Des3_SetKey(&defDes3, key, iv, DES_ENCRYPTION);
  577. if (ret != 0) {
  578. printf("default des3 key set failed\n");
  579. return -1;
  580. }
  581. ret = CRYPT_TDES_CBC_Encrypt(&mcDes3, out1, ourData, TDES_TEST_SIZE);
  582. if (ret != 0) {
  583. printf("mcapi tdes cbc encrypt failed\n");
  584. return -1;
  585. }
  586. ret = wc_Des3_CbcEncrypt(&defDes3, out2, ourData, TDES_TEST_SIZE);
  587. if (ret != 0) {
  588. printf("mcapi default tdes cbc encrypt failed\n");
  589. return -1;
  590. }
  591. if (memcmp(out1, out2, TDES_TEST_SIZE) != 0) {
  592. printf("mcapi tdes cbc encrypt cmp failed\n");
  593. return -1;
  594. }
  595. /* cbc decrypt */
  596. ret = CRYPT_TDES_KeySet(&mcDes3, key, iv, CRYPT_TDES_DECRYPTION);
  597. if (ret != 0) {
  598. printf("mcapi tdes key set failed\n");
  599. return -1;
  600. }
  601. ret = wc_Des3_SetKey(&defDes3, key, iv, DES_DECRYPTION);
  602. if (ret != 0) {
  603. printf("default des3 key set failed\n");
  604. return -1;
  605. }
  606. ret = CRYPT_TDES_CBC_Decrypt(&mcDes3, out2, out1, TDES_TEST_SIZE);
  607. if (ret != 0) {
  608. printf("mcapi tdes cbc decrypt failed\n");
  609. return -1;
  610. }
  611. ret = wc_Des3_CbcDecrypt(&defDes3, out1, out1, TDES_TEST_SIZE);
  612. if (ret != 0) {
  613. printf("mcapi default tdes cbc decrypt failed\n");
  614. return -1;
  615. }
  616. if (memcmp(out1, out2, TDES_TEST_SIZE) != 0) {
  617. printf("mcapi tdes cbc decrypt cmp failed\n");
  618. return -1;
  619. }
  620. if (memcmp(out1, ourData, TDES_TEST_SIZE) != 0) {
  621. printf("mcapi tdes cbc decrypt orig cmp failed\n");
  622. return -1;
  623. }
  624. printf("tdes mcapi test passed\n");
  625. return 0;
  626. }
  627. #define AES_TEST_SIZE 32
  628. /* check mcapi aes cbc */
  629. static int check_aescbc(void)
  630. {
  631. CRYPT_AES_CTX mcAes;
  632. Aes defAes;
  633. int ret;
  634. byte out1[AES_TEST_SIZE];
  635. byte out2[AES_TEST_SIZE];
  636. memcpy((char*)key, "1234567890abcdefghijklmnopqrstuv", 32);
  637. memcpy((char*)iv, "1234567890abcdef", 16);
  638. /* 128 cbc encrypt */
  639. ret = CRYPT_AES_KeySet(&mcAes, key, 16, iv, CRYPT_AES_ENCRYPTION);
  640. if (ret != 0) {
  641. printf("mcapi aes-128 key set failed\n");
  642. return -1;
  643. }
  644. ret = wc_AesSetKey(&defAes, key, 16, iv, AES_ENCRYPTION);
  645. if (ret != 0) {
  646. printf("default aes-128 key set failed\n");
  647. return -1;
  648. }
  649. ret = CRYPT_AES_CBC_Encrypt(&mcAes, out1, ourData, AES_TEST_SIZE);
  650. if (ret != 0) {
  651. printf("mcapi aes-128 cbc encrypt failed\n");
  652. return -1;
  653. }
  654. wc_AesCbcEncrypt(&defAes, out2, ourData, AES_TEST_SIZE);
  655. if (memcmp(out1, out2, AES_TEST_SIZE) != 0) {
  656. printf("mcapi aes-128 cbc encrypt cmp failed\n");
  657. return -1;
  658. }
  659. /* 128 cbc decrypt */
  660. ret = CRYPT_AES_KeySet(&mcAes, key, 16, iv, CRYPT_AES_DECRYPTION);
  661. if (ret != 0) {
  662. printf("mcapi aes-128 key set failed\n");
  663. return -1;
  664. }
  665. ret = wc_AesSetKey(&defAes, key, 16, iv, AES_DECRYPTION);
  666. if (ret != 0) {
  667. printf("default aes-128 key set failed\n");
  668. return -1;
  669. }
  670. ret = CRYPT_AES_CBC_Decrypt(&mcAes, out2, out1, AES_TEST_SIZE);
  671. if (ret != 0) {
  672. printf("mcapi aes-128 cbc decrypt failed\n");
  673. return -1;
  674. }
  675. wc_AesCbcDecrypt(&defAes, out1, out1, AES_TEST_SIZE);
  676. if (memcmp(out1, out2, AES_TEST_SIZE) != 0) {
  677. printf("mcapi aes-128 cbc decrypt cmp failed\n");
  678. return -1;
  679. }
  680. if (memcmp(out1, ourData, AES_TEST_SIZE) != 0) {
  681. printf("mcapi aes-128 cbc decrypt orig cmp failed\n");
  682. return -1;
  683. }
  684. /* 192 cbc encrypt */
  685. ret = CRYPT_AES_KeySet(&mcAes, key, 24, iv, CRYPT_AES_ENCRYPTION);
  686. if (ret != 0) {
  687. printf("mcapi aes-192 key set failed\n");
  688. return -1;
  689. }
  690. ret = wc_AesSetKey(&defAes, key, 24, iv, AES_ENCRYPTION);
  691. if (ret != 0) {
  692. printf("default aes-192 key set failed\n");
  693. return -1;
  694. }
  695. ret = CRYPT_AES_CBC_Encrypt(&mcAes, out1, ourData, AES_TEST_SIZE);
  696. if (ret != 0) {
  697. printf("mcapi aes-192 cbc encrypt failed\n");
  698. return -1;
  699. }
  700. wc_AesCbcEncrypt(&defAes, out2, ourData, AES_TEST_SIZE);
  701. if (memcmp(out1, out2, AES_TEST_SIZE) != 0) {
  702. printf("mcapi aes-192 cbc encrypt cmp failed\n");
  703. return -1;
  704. }
  705. /* 192 cbc decrypt */
  706. ret = CRYPT_AES_KeySet(&mcAes, key, 24, iv, CRYPT_AES_DECRYPTION);
  707. if (ret != 0) {
  708. printf("mcapi aes-192 key set failed\n");
  709. return -1;
  710. }
  711. ret = wc_AesSetKey(&defAes, key, 24, iv, AES_DECRYPTION);
  712. if (ret != 0) {
  713. printf("default aes-192 key set failed\n");
  714. return -1;
  715. }
  716. ret = CRYPT_AES_CBC_Decrypt(&mcAes, out2, out1, AES_TEST_SIZE);
  717. if (ret != 0) {
  718. printf("mcapi aes-192 cbc decrypt failed\n");
  719. return -1;
  720. }
  721. wc_AesCbcDecrypt(&defAes, out1, out1, AES_TEST_SIZE);
  722. if (memcmp(out1, out2, AES_TEST_SIZE) != 0) {
  723. printf("mcapi aes-192 cbc decrypt cmp failed\n");
  724. return -1;
  725. }
  726. if (memcmp(out1, ourData, AES_TEST_SIZE) != 0) {
  727. printf("mcapi aes-192 cbc decrypt orig cmp failed\n");
  728. return -1;
  729. }
  730. /* 256 cbc encrypt */
  731. ret = CRYPT_AES_KeySet(&mcAes, key, 32, iv, CRYPT_AES_ENCRYPTION);
  732. if (ret != 0) {
  733. printf("mcapi aes-256 key set failed\n");
  734. return -1;
  735. }
  736. ret = wc_AesSetKey(&defAes, key, 32, iv, AES_ENCRYPTION);
  737. if (ret != 0) {
  738. printf("default aes-256 key set failed\n");
  739. return -1;
  740. }
  741. ret = CRYPT_AES_CBC_Encrypt(&mcAes, out1, ourData, AES_TEST_SIZE);
  742. if (ret != 0) {
  743. printf("mcapi aes-256 cbc encrypt failed\n");
  744. return -1;
  745. }
  746. wc_AesCbcEncrypt(&defAes, out2, ourData, AES_TEST_SIZE);
  747. if (memcmp(out1, out2, AES_TEST_SIZE) != 0) {
  748. printf("mcapi aes-256 cbc encrypt cmp failed\n");
  749. return -1;
  750. }
  751. /* 256 cbc decrypt */
  752. ret = CRYPT_AES_KeySet(&mcAes, key, 32, iv, CRYPT_AES_DECRYPTION);
  753. if (ret != 0) {
  754. printf("mcapi aes-256 key set failed\n");
  755. return -1;
  756. }
  757. ret = wc_AesSetKey(&defAes, key, 32, iv, AES_DECRYPTION);
  758. if (ret != 0) {
  759. printf("default aes-256 key set failed\n");
  760. return -1;
  761. }
  762. ret = CRYPT_AES_CBC_Decrypt(&mcAes, out2, out1, AES_TEST_SIZE);
  763. if (ret != 0) {
  764. printf("mcapi aes-256 cbc decrypt failed\n");
  765. return -1;
  766. }
  767. wc_AesCbcDecrypt(&defAes, out1, out1, AES_TEST_SIZE);
  768. if (memcmp(out1, out2, AES_TEST_SIZE) != 0) {
  769. printf("mcapi aes-256 cbc decrypt cmp failed\n");
  770. return -1;
  771. }
  772. if (memcmp(out1, ourData, AES_TEST_SIZE) != 0) {
  773. printf("mcapi aes-256 cbc decrypt orig cmp failed\n");
  774. return -1;
  775. }
  776. printf("aes-cbc mcapi test passed\n");
  777. return 0;
  778. }
  779. /* check mcapi aes ctr */
  780. static int check_aesctr(void)
  781. {
  782. CRYPT_AES_CTX mcAes;
  783. Aes defAes;
  784. int ret;
  785. byte out1[AES_TEST_SIZE];
  786. byte out2[AES_TEST_SIZE];
  787. memcpy((char*)key, "1234567890abcdefghijklmnopqrstuv", 32);
  788. memcpy((char*)iv, "1234567890abcdef", 16);
  789. /* 128 ctr encrypt */
  790. ret = CRYPT_AES_KeySet(&mcAes, key, 16, iv, CRYPT_AES_ENCRYPTION);
  791. if (ret != 0) {
  792. printf("mcapi aes-128 key set failed\n");
  793. return -1;
  794. }
  795. ret = wc_AesSetKey(&defAes, key, 16, iv, AES_ENCRYPTION);
  796. if (ret != 0) {
  797. printf("default aes-128 key set failed\n");
  798. return -1;
  799. }
  800. ret = CRYPT_AES_CTR_Encrypt(&mcAes, out1, ourData, AES_TEST_SIZE);
  801. if (ret != 0) {
  802. printf("mcapi aes-128 ctr encrypt failed\n");
  803. return -1;
  804. }
  805. ret = wc_AesCtrEncrypt(&defAes, out2, ourData, AES_TEST_SIZE);
  806. if (ret != 0) {
  807. printf("mcapi aes-128 ctr encrypt set failed\n");
  808. return -1;
  809. }
  810. if (memcmp(out1, out2, AES_TEST_SIZE) != 0) {
  811. printf("mcapi aes-128 ctr encrypt cmp failed\n");
  812. return -1;
  813. }
  814. /* 128 ctr decrypt */
  815. ret = CRYPT_AES_KeySet(&mcAes, key, 16, iv, CRYPT_AES_ENCRYPTION);
  816. if (ret != 0) {
  817. printf("mcapi aes-128 key set failed\n");
  818. return -1;
  819. }
  820. ret = wc_AesSetKey(&defAes, key, 16, iv, AES_ENCRYPTION);
  821. if (ret != 0) {
  822. printf("default aes-128 key set failed\n");
  823. return -1;
  824. }
  825. ret = CRYPT_AES_CTR_Encrypt(&mcAes, out2, out1, AES_TEST_SIZE);
  826. if (ret != 0) {
  827. printf("mcapi aes-128 ctr decrypt failed\n");
  828. return -1;
  829. }
  830. if (memcmp(out2, ourData, AES_TEST_SIZE) != 0) {
  831. printf("mcapi aes-128 ctr decrypt orig cmp failed\n");
  832. return -1;
  833. }
  834. /* 192 ctr encrypt */
  835. ret = CRYPT_AES_KeySet(&mcAes, key, 24, iv, CRYPT_AES_ENCRYPTION);
  836. if (ret != 0) {
  837. printf("mcapi aes-192 key set failed\n");
  838. return -1;
  839. }
  840. ret = wc_AesSetKey(&defAes, key, 24, iv, AES_ENCRYPTION);
  841. if (ret != 0) {
  842. printf("default aes-192 key set failed\n");
  843. return -1;
  844. }
  845. ret = CRYPT_AES_CTR_Encrypt(&mcAes, out1, ourData, AES_TEST_SIZE);
  846. if (ret != 0) {
  847. printf("mcapi aes-192 ctr encrypt failed\n");
  848. return -1;
  849. }
  850. ret = wc_AesCtrEncrypt(&defAes, out2, ourData, AES_TEST_SIZE);
  851. if (ret != 0) {
  852. printf("mcapi aes-192 ctr encrypt set failed\n");
  853. return -1;
  854. }
  855. if (memcmp(out1, out2, AES_TEST_SIZE) != 0) {
  856. printf("mcapi aes-192 ctr encrypt cmp failed\n");
  857. return -1;
  858. }
  859. /* 192 ctr decrypt */
  860. ret = CRYPT_AES_KeySet(&mcAes, key, 24, iv, CRYPT_AES_ENCRYPTION);
  861. if (ret != 0) {
  862. printf("mcapi aes-192 key set failed\n");
  863. return -1;
  864. }
  865. ret = wc_AesSetKey(&defAes, key, 24, iv, AES_DECRYPTION);
  866. if (ret != 0) {
  867. printf("default aes-192 key set failed\n");
  868. return -1;
  869. }
  870. ret = CRYPT_AES_CTR_Encrypt(&mcAes, out2, out1, AES_TEST_SIZE);
  871. if (ret != 0) {
  872. printf("mcapi aes-192 ctr decrypt failed\n");
  873. return -1;
  874. }
  875. if (memcmp(out2, ourData, AES_TEST_SIZE) != 0) {
  876. printf("mcapi aes-192 ctr decrypt orig cmp failed\n");
  877. return -1;
  878. }
  879. /* 256 ctr encrypt */
  880. ret = CRYPT_AES_KeySet(&mcAes, key, 32, iv, CRYPT_AES_ENCRYPTION);
  881. if (ret != 0) {
  882. printf("mcapi aes-256 key set failed\n");
  883. return -1;
  884. }
  885. ret = wc_AesSetKey(&defAes, key, 32, iv, AES_ENCRYPTION);
  886. if (ret != 0) {
  887. printf("default aes-256 key set failed\n");
  888. return -1;
  889. }
  890. ret = CRYPT_AES_CTR_Encrypt(&mcAes, out1, ourData, AES_TEST_SIZE);
  891. if (ret != 0) {
  892. printf("mcapi aes-256 ctr encrypt failed\n");
  893. return -1;
  894. }
  895. ret = wc_AesCtrEncrypt(&defAes, out2, ourData, AES_TEST_SIZE);
  896. if (ret != 0) {
  897. printf("mcapi aes-256 ctr encrypt set failed\n");
  898. return -1;
  899. }
  900. if (memcmp(out1, out2, AES_TEST_SIZE) != 0) {
  901. printf("mcapi aes-256 ctr encrypt cmp failed\n");
  902. return -1;
  903. }
  904. /* 256 ctr decrypt */
  905. ret = CRYPT_AES_KeySet(&mcAes, key, 32, iv, CRYPT_AES_ENCRYPTION);
  906. if (ret != 0) {
  907. printf("mcapi aes-256 key set failed\n");
  908. return -1;
  909. }
  910. ret = wc_AesSetKey(&defAes, key, 32, iv, AES_ENCRYPTION);
  911. if (ret != 0) {
  912. printf("default aes-256 key set failed\n");
  913. return -1;
  914. }
  915. ret = CRYPT_AES_CTR_Encrypt(&mcAes, out2, out1, AES_TEST_SIZE);
  916. if (ret != 0) {
  917. printf("mcapi aes-256 ctr decrypt failed\n");
  918. return -1;
  919. }
  920. if (memcmp(out2, ourData, AES_TEST_SIZE) != 0) {
  921. printf("mcapi aes-256 ctr decrypt orig cmp failed\n");
  922. return -1;
  923. }
  924. printf("aes-ctr mcapi test passed\n");
  925. return 0;
  926. }
  927. /* check mcapi aes direct */
  928. static int check_aesdirect(void)
  929. {
  930. CRYPT_AES_CTX mcAes;
  931. Aes defAes;
  932. int ret;
  933. byte out1[CRYPT_AES_BLOCK_SIZE];
  934. byte out2[16]; /* one block at a time */
  935. memcpy((char*)key, "1234567890abcdefghijklmnopqrstuv", 32);
  936. memcpy((char*)iv, "1234567890abcdef", 16);
  937. /* 128 direct encrypt */
  938. ret = CRYPT_AES_KeySet(&mcAes, key, 16, iv, CRYPT_AES_ENCRYPTION);
  939. if (ret != 0) {
  940. printf("mcapi aes-128 key set failed\n");
  941. return -1;
  942. }
  943. ret = wc_AesSetKey(&defAes, key, 16, iv, AES_ENCRYPTION);
  944. if (ret != 0) {
  945. printf("default aes-128 key set failed\n");
  946. return -1;
  947. }
  948. ret = CRYPT_AES_DIRECT_Encrypt(&mcAes, out1, ourData);
  949. if (ret != 0) {
  950. printf("mcapi aes-128 direct encrypt failed\n");
  951. return -1;
  952. }
  953. wc_AesEncryptDirect(&defAes, out2, ourData);
  954. if (memcmp(out1, out2, CRYPT_AES_BLOCK_SIZE) != 0) {
  955. printf("mcapi aes-128 direct encrypt cmp failed\n");
  956. return -1;
  957. }
  958. /* 128 direct decrypt */
  959. ret = CRYPT_AES_KeySet(&mcAes, key, 16, iv, CRYPT_AES_DECRYPTION);
  960. if (ret != 0) {
  961. printf("mcapi aes-128 key set failed\n");
  962. return -1;
  963. }
  964. ret = wc_AesSetKey(&defAes, key, 16, iv, AES_DECRYPTION);
  965. if (ret != 0) {
  966. printf("default aes-128 key set failed\n");
  967. return -1;
  968. }
  969. ret = CRYPT_AES_DIRECT_Decrypt(&mcAes, out2, out1);
  970. if (ret != 0) {
  971. printf("mcapi aes-128 direct decrypt failed\n");
  972. return -1;
  973. }
  974. wc_AesDecryptDirect(&defAes, out1, out1);
  975. if (memcmp(out1, out2, CRYPT_AES_BLOCK_SIZE) != 0) {
  976. printf("mcapi aes-128 direct decrypt cmp failed\n");
  977. return -1;
  978. }
  979. if (memcmp(out1, ourData, CRYPT_AES_BLOCK_SIZE) != 0) {
  980. printf("mcapi aes-128 direct decrypt orig cmp failed\n");
  981. return -1;
  982. }
  983. /* 192 direct encrypt */
  984. ret = CRYPT_AES_KeySet(&mcAes, key, 24, iv, CRYPT_AES_ENCRYPTION);
  985. if (ret != 0) {
  986. printf("mcapi aes-192 key set failed\n");
  987. return -1;
  988. }
  989. ret = wc_AesSetKey(&defAes, key, 24, iv, AES_ENCRYPTION);
  990. if (ret != 0) {
  991. printf("default aes-192 key set failed\n");
  992. return -1;
  993. }
  994. ret = CRYPT_AES_DIRECT_Encrypt(&mcAes, out1, ourData);
  995. if (ret != 0) {
  996. printf("mcapi aes-192 direct encrypt failed\n");
  997. return -1;
  998. }
  999. wc_AesEncryptDirect(&defAes, out2, ourData);
  1000. if (memcmp(out1, out2, CRYPT_AES_BLOCK_SIZE) != 0) {
  1001. printf("mcapi aes-192 direct encrypt cmp failed\n");
  1002. return -1;
  1003. }
  1004. /* 192 direct decrypt */
  1005. ret = CRYPT_AES_KeySet(&mcAes, key, 24, iv, CRYPT_AES_DECRYPTION);
  1006. if (ret != 0) {
  1007. printf("mcapi aes-192 key set failed\n");
  1008. return -1;
  1009. }
  1010. ret = wc_AesSetKey(&defAes, key, 24, iv, AES_DECRYPTION);
  1011. if (ret != 0) {
  1012. printf("default aes-192 key set failed\n");
  1013. return -1;
  1014. }
  1015. ret = CRYPT_AES_DIRECT_Decrypt(&mcAes, out2, out1);
  1016. if (ret != 0) {
  1017. printf("mcapi aes-192 direct decrypt failed\n");
  1018. return -1;
  1019. }
  1020. wc_AesDecryptDirect(&defAes, out1, out1);
  1021. if (memcmp(out1, out2, CRYPT_AES_BLOCK_SIZE) != 0) {
  1022. printf("mcapi aes-192 direct decrypt cmp failed\n");
  1023. return -1;
  1024. }
  1025. if (memcmp(out1, ourData, CRYPT_AES_BLOCK_SIZE) != 0) {
  1026. printf("mcapi aes-192 direct decrypt orig cmp failed\n");
  1027. return -1;
  1028. }
  1029. /* 256 direct encrypt */
  1030. ret = CRYPT_AES_KeySet(&mcAes, key, 32, iv, CRYPT_AES_ENCRYPTION);
  1031. if (ret != 0) {
  1032. printf("mcapi aes-256 key set failed\n");
  1033. return -1;
  1034. }
  1035. ret = wc_AesSetKey(&defAes, key, 32, iv, AES_ENCRYPTION);
  1036. if (ret != 0) {
  1037. printf("default aes-256 key set failed\n");
  1038. return -1;
  1039. }
  1040. ret = CRYPT_AES_DIRECT_Encrypt(&mcAes, out1, ourData);
  1041. if (ret != 0) {
  1042. printf("mcapi aes-256 direct encrypt failed\n");
  1043. return -1;
  1044. }
  1045. wc_AesEncryptDirect(&defAes, out2, ourData);
  1046. if (memcmp(out1, out2, CRYPT_AES_BLOCK_SIZE) != 0) {
  1047. printf("mcapi aes-256 direct encrypt cmp failed\n");
  1048. return -1;
  1049. }
  1050. /* 256 direct decrypt */
  1051. ret = CRYPT_AES_KeySet(&mcAes, key, 32, iv, CRYPT_AES_DECRYPTION);
  1052. if (ret != 0) {
  1053. printf("mcapi aes-256 key set failed\n");
  1054. return -1;
  1055. }
  1056. ret = wc_AesSetKey(&defAes, key, 32, iv, AES_DECRYPTION);
  1057. if (ret != 0) {
  1058. printf("default aes-256 key set failed\n");
  1059. return -1;
  1060. }
  1061. ret = CRYPT_AES_DIRECT_Decrypt(&mcAes, out2, out1);
  1062. if (ret != 0) {
  1063. printf("mcapi aes-256 direct decrypt failed\n");
  1064. return -1;
  1065. }
  1066. wc_AesDecryptDirect(&defAes, out1, out1);
  1067. if (memcmp(out1, out2, CRYPT_AES_BLOCK_SIZE) != 0) {
  1068. printf("mcapi aes-256 direct decrypt cmp failed\n");
  1069. return -1;
  1070. }
  1071. if (memcmp(out1, ourData, CRYPT_AES_BLOCK_SIZE) != 0) {
  1072. printf("mcapi aes-256 direct decrypt orig cmp failed\n");
  1073. return -1;
  1074. }
  1075. printf("aes-direct mcapi test passed\n");
  1076. return 0;
  1077. }
  1078. #define RSA_TEST_SIZE 64
  1079. /* check mcapi rsa */
  1080. static int check_rsa(void)
  1081. {
  1082. CRYPT_RSA_CTX mcRsa;
  1083. RsaKey defRsa;
  1084. int ret;
  1085. int ret2;
  1086. unsigned int keySz = (unsigned int)sizeof(client_key_der_1024);
  1087. unsigned int idx = 0;
  1088. byte out1[256];
  1089. byte out2[256];
  1090. ret = wc_InitRsaKey(&defRsa, NULL);
  1091. if (ret == 0)
  1092. ret = CRYPT_RSA_Initialize(&mcRsa);
  1093. if (ret != 0) {
  1094. printf("mcapi rsa init failed\n");
  1095. return -1;
  1096. }
  1097. ret = CRYPT_RSA_PrivateKeyDecode(&mcRsa, client_key_der_1024, keySz);
  1098. if (ret != 0) {
  1099. printf("mcapi rsa private key decode failed\n");
  1100. return -1;
  1101. }
  1102. ret = wc_RsaPrivateKeyDecode(client_key_der_1024, &idx, &defRsa, keySz);
  1103. if (ret != 0) {
  1104. printf("default rsa private key decode failed\n");
  1105. return -1;
  1106. }
  1107. ret = CRYPT_RSA_SetRng(&mcRsa, &mcRng);
  1108. if (ret != 0) {
  1109. printf("mcapi rsa set rng failed\n");
  1110. return -1;
  1111. }
  1112. ret = CRYPT_RSA_PublicEncrypt(&mcRsa, out1, sizeof(out1), ourData,
  1113. RSA_TEST_SIZE, &mcRng);
  1114. if (ret < 0) {
  1115. printf("mcapi rsa public encrypt failed\n");
  1116. return -1;
  1117. }
  1118. ret2 = wc_RsaPublicEncrypt(ourData, RSA_TEST_SIZE, out2, sizeof(out2),
  1119. &defRsa, &defRng);
  1120. if (ret2 < 0) {
  1121. printf("default rsa public encrypt failed\n");
  1122. return -1;
  1123. }
  1124. if (ret != ret2) {
  1125. printf("default rsa public encrypt sz != mcapi sz\n");
  1126. return -1;
  1127. }
  1128. if (ret != CRYPT_RSA_EncryptSizeGet(&mcRsa)) {
  1129. printf("mcapi encrypt sz get != mcapi sz\n");
  1130. return -1;
  1131. }
  1132. ret = CRYPT_RSA_PrivateDecrypt(&mcRsa, out2, sizeof(out2), out1, ret);
  1133. if (ret < 0) {
  1134. printf("mcapi rsa private derypt failed\n");
  1135. return -1;
  1136. }
  1137. if (ret != RSA_TEST_SIZE) {
  1138. printf("mcapi rsa private derypt plain size wrong\n");
  1139. return -1;
  1140. }
  1141. if (memcmp(out2, ourData, ret) != 0) {
  1142. printf("mcapi rsa private derypt plain text bad\n");
  1143. return -1;
  1144. }
  1145. wc_FreeRsaKey(&defRsa);
  1146. ret = CRYPT_RSA_Free(&mcRsa);
  1147. if (ret != 0) {
  1148. printf("mcapi rsa free failed\n");
  1149. return -1;
  1150. }
  1151. printf("rsa mcapi test passed\n");
  1152. return 0;
  1153. }
  1154. /* check mcapi ecc */
  1155. static int check_ecc(void)
  1156. {
  1157. CRYPT_ECC_CTX userA;
  1158. CRYPT_ECC_CTX userB;
  1159. int ret;
  1160. byte sharedA[100];
  1161. byte sharedB[100];
  1162. byte sig[100];
  1163. unsigned int aSz = (unsigned int)sizeof(sharedA);
  1164. unsigned int bSz = (unsigned int)sizeof(sharedB);
  1165. unsigned int sigSz = (unsigned int)sizeof(sig);
  1166. unsigned int usedA = 0;
  1167. unsigned int usedB = 0;
  1168. int verifyStatus = 0;
  1169. /* init */
  1170. ret = CRYPT_ECC_Initialize(&userA);
  1171. if (ret != 0) {
  1172. printf("mcapi ecc init failed\n");
  1173. return -1;
  1174. }
  1175. ret = CRYPT_ECC_Initialize(&userB);
  1176. if (ret != 0) {
  1177. printf("mcapi ecc init b failed\n");
  1178. return -1;
  1179. }
  1180. /* dhe + helpers */
  1181. ret = CRYPT_ECC_DHE_KeyMake(&userA, &mcRng, 32);
  1182. if (ret != 0) {
  1183. printf("mcapi ecc make key failed\n");
  1184. return -1;
  1185. }
  1186. ret = CRYPT_ECC_DHE_KeyMake(&userB, &mcRng, 32);
  1187. if (ret != 0) {
  1188. printf("mcapi ecc make key b failed\n");
  1189. return -1;
  1190. }
  1191. ret = CRYPT_ECC_KeySizeGet(&userA);
  1192. if (ret <= 0) {
  1193. printf("mcapi ecc key size get failed\n");
  1194. return -1;
  1195. }
  1196. ret = CRYPT_ECC_SignatureSizeGet(&userA);
  1197. if (ret <= 0) {
  1198. printf("mcapi ecc signature size get failed\n");
  1199. return -1;
  1200. }
  1201. ret = CRYPT_ECC_DHE_SharedSecretMake(&userA, &userB, sharedA, aSz, &usedA);
  1202. if (ret != 0) {
  1203. printf("mcapi ecc make shared secret failed\n");
  1204. return -1;
  1205. }
  1206. ret = CRYPT_ECC_DHE_SharedSecretMake(&userB, &userA, sharedB, bSz, &usedB);
  1207. if (ret != 0) {
  1208. printf("mcapi ecc make shared secret failed\n");
  1209. return -1;
  1210. }
  1211. if (usedA != usedB || usedA == 0) {
  1212. printf("mcapi ecc make shared secret output size match failed\n");
  1213. return -1;
  1214. }
  1215. if (memcmp(sharedA, sharedB, usedA) != 0) {
  1216. printf("mcapi ecc make shared secret output match cmp failed\n");
  1217. return -1;
  1218. }
  1219. /* dsa */
  1220. ret = CRYPT_ECC_DSA_HashSign(&userA, &mcRng, sig, sigSz, &usedA, ourData,
  1221. CRYPT_SHA_DIGEST_SIZE);
  1222. if (ret != 0) {
  1223. printf("mcapi ecc sign hash failed\n");
  1224. return -1;
  1225. }
  1226. sigSz = usedA;
  1227. if (sigSz == 0) {
  1228. printf("mcapi ecc sign hash bad sig size\n");
  1229. return -1;
  1230. }
  1231. ret = CRYPT_ECC_DSA_HashVerify(&userA, sig, sigSz, ourData,
  1232. CRYPT_SHA_DIGEST_SIZE, &verifyStatus);
  1233. if (ret != 0) {
  1234. printf("mcapi ecc verify hash failed\n");
  1235. return -1;
  1236. }
  1237. if (verifyStatus != 1) {
  1238. printf("mcapi ecc verify hash status failed\n");
  1239. return -1;
  1240. }
  1241. /* import / export */
  1242. usedA = 0;
  1243. ret = CRYPT_ECC_PublicExport(&userA, sharedA, aSz, &usedA);
  1244. if (ret != 0) {
  1245. printf("mcapi ecc public export failed\n");
  1246. return -1;
  1247. }
  1248. ret = CRYPT_ECC_PublicImport(&userB, sharedA, usedA);
  1249. if (ret != 0) {
  1250. printf("mcapi ecc public import failed\n");
  1251. return -1;
  1252. }
  1253. ret = CRYPT_ECC_Free(&userA);
  1254. if (ret != 0) {
  1255. printf("mcapi ecc free failed\n");
  1256. return -1;
  1257. }
  1258. ret = CRYPT_ECC_Free(&userB);
  1259. if (ret != 0) {
  1260. printf("mcapi ecc free b failed\n");
  1261. return -1;
  1262. }
  1263. printf("ecc mcapi test passed\n");
  1264. return 0;
  1265. }