mcapi_test.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510
  1. /* mcapi_test.c
  2. *
  3. * Copyright (C) 2006-2021 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 "crypto.h"
  24. #include <wolfssl/wolfcrypt/settings.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 fialed\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 fialed\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 fialed\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 fialed\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 fialed\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 fialed\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 fialed\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. if (memcmp(cBuffer, dBuffer, ret1) != 0) {
  471. printf("compress dynamic cmp failed\n");
  472. return -1;
  473. }
  474. outSz = ret1;
  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. ret1 = wc_DeCompress(dBuffer, sizeof(dBuffer), cBuffer, outSz);
  482. if (memcmp(dBuffer, text, inSz) != 0) {
  483. printf("decompress dynamic cmp failed\n");
  484. return -1;
  485. }
  486. memset(cBuffer, 0, sizeof(cBuffer));
  487. memset(dBuffer, 0, sizeof(dBuffer));
  488. /* static */
  489. ret1 = CRYPT_HUFFMAN_Compress(cBuffer, sizeof(cBuffer), text, inSz, 1);
  490. ret2 = wc_Compress(dBuffer, sizeof(dBuffer), text, inSz, 1);
  491. if (ret1 != ret2 || ret1 < 0) {
  492. printf("compress static ret failed\n");
  493. return -1;
  494. }
  495. if (memcmp(cBuffer, dBuffer, ret1) != 0) {
  496. printf("compress static cmp failed\n");
  497. return -1;
  498. }
  499. outSz = ret1;
  500. ret1 = CRYPT_HUFFMAN_DeCompress(dBuffer, sizeof(dBuffer), cBuffer, outSz);
  501. if (memcmp(dBuffer, text, inSz) != 0) {
  502. printf("mcapi decompress static cmp failed\n");
  503. return -1;
  504. }
  505. memset(dBuffer, 0, sizeof(dBuffer));
  506. ret1 = wc_DeCompress(dBuffer, sizeof(dBuffer), cBuffer, outSz);
  507. if (memcmp(dBuffer, text, inSz) != 0) {
  508. printf("decompress static cmp failed\n");
  509. return -1;
  510. }
  511. printf("huffman mcapi test passed\n");
  512. return 0;
  513. }
  514. #define RANDOM_BYTE_SZ 32
  515. /* check mcapi rng */
  516. static int check_rng(void)
  517. {
  518. int ret;
  519. int i;
  520. byte in[RANDOM_BYTE_SZ];
  521. byte out[RANDOM_BYTE_SZ];
  522. for (i = 0; i < RANDOM_BYTE_SZ; i++)
  523. in[i] = (byte)i;
  524. for (i = 0; i < RANDOM_BYTE_SZ; i++)
  525. out[i] = (byte)i;
  526. ret = wc_InitRng(&defRng);
  527. if (ret != 0) {
  528. printf("default rng init failed\n");
  529. return -1;
  530. }
  531. ret = CRYPT_RNG_Initialize(&mcRng);
  532. if (ret != 0) {
  533. printf("mcapi rng init failed\n");
  534. return -1;
  535. }
  536. ret = CRYPT_RNG_Get(&mcRng, &out[0]);
  537. if (ret != 0) {
  538. printf("mcapi rng get failed\n");
  539. return -1;
  540. }
  541. ret = CRYPT_RNG_BlockGenerate(&mcRng, out, RANDOM_BYTE_SZ);
  542. if (ret != 0) {
  543. printf("mcapi rng block gen failed\n");
  544. return -1;
  545. }
  546. if (memcmp(in, out, RANDOM_BYTE_SZ) == 0) {
  547. printf("mcapi rng block gen output failed\n");
  548. return -1;
  549. }
  550. printf("rng mcapi test passed\n");
  551. return 0;
  552. }
  553. #define TDES_TEST_SIZE 32
  554. /* check mcapi des3 */
  555. static int check_des3(void)
  556. {
  557. CRYPT_TDES_CTX mcDes3;
  558. Des3 defDes3;
  559. int ret;
  560. byte out1[TDES_TEST_SIZE];
  561. byte out2[TDES_TEST_SIZE];
  562. memcpy((char*)key, "1234567890abcdefghijklmn", 24);
  563. memcpy((char*)iv, "12345678", 8);
  564. /* cbc encrypt */
  565. ret = CRYPT_TDES_KeySet(&mcDes3, key, iv, CRYPT_TDES_ENCRYPTION);
  566. if (ret != 0) {
  567. printf("mcapi tdes key set failed\n");
  568. return -1;
  569. }
  570. ret = wc_Des3_SetKey(&defDes3, key, iv, DES_ENCRYPTION);
  571. if (ret != 0) {
  572. printf("default des3 key set failed\n");
  573. return -1;
  574. }
  575. ret = CRYPT_TDES_CBC_Encrypt(&mcDes3, out1, ourData, TDES_TEST_SIZE);
  576. if (ret != 0) {
  577. printf("mcapi tdes cbc encrypt failed\n");
  578. return -1;
  579. }
  580. ret = wc_Des3_CbcEncrypt(&defDes3, out2, ourData, TDES_TEST_SIZE);
  581. if (ret != 0) {
  582. printf("mcapi default tdes cbc encrypt failed\n");
  583. return -1;
  584. }
  585. if (memcmp(out1, out2, TDES_TEST_SIZE) != 0) {
  586. printf("mcapi tdes cbc encrypt cmp failed\n");
  587. return -1;
  588. }
  589. /* cbc decrypt */
  590. ret = CRYPT_TDES_KeySet(&mcDes3, key, iv, CRYPT_TDES_DECRYPTION);
  591. if (ret != 0) {
  592. printf("mcapi tdes key set failed\n");
  593. return -1;
  594. }
  595. ret = wc_Des3_SetKey(&defDes3, key, iv, DES_DECRYPTION);
  596. if (ret != 0) {
  597. printf("default des3 key set failed\n");
  598. return -1;
  599. }
  600. ret = CRYPT_TDES_CBC_Decrypt(&mcDes3, out2, out1, TDES_TEST_SIZE);
  601. if (ret != 0) {
  602. printf("mcapi tdes cbc decrypt failed\n");
  603. return -1;
  604. }
  605. ret = wc_Des3_CbcDecrypt(&defDes3, out1, out1, TDES_TEST_SIZE);
  606. if (ret != 0) {
  607. printf("mcapi default tdes cbc decrypt failed\n");
  608. return -1;
  609. }
  610. if (memcmp(out1, out2, TDES_TEST_SIZE) != 0) {
  611. printf("mcapi tdes cbc decrypt cmp failed\n");
  612. return -1;
  613. }
  614. if (memcmp(out1, ourData, TDES_TEST_SIZE) != 0) {
  615. printf("mcapi tdes cbc decrypt orig cmp failed\n");
  616. return -1;
  617. }
  618. printf("tdes mcapi test passed\n");
  619. return 0;
  620. }
  621. #define AES_TEST_SIZE 32
  622. /* check mcapi aes cbc */
  623. static int check_aescbc(void)
  624. {
  625. CRYPT_AES_CTX mcAes;
  626. Aes defAes;
  627. int ret;
  628. byte out1[AES_TEST_SIZE];
  629. byte out2[AES_TEST_SIZE];
  630. memcpy((char*)key, "1234567890abcdefghijklmnopqrstuv", 32);
  631. memcpy((char*)iv, "1234567890abcdef", 16);
  632. /* 128 cbc encrypt */
  633. ret = CRYPT_AES_KeySet(&mcAes, key, 16, iv, CRYPT_AES_ENCRYPTION);
  634. if (ret != 0) {
  635. printf("mcapi aes-128 key set failed\n");
  636. return -1;
  637. }
  638. ret = wc_AesSetKey(&defAes, key, 16, iv, AES_ENCRYPTION);
  639. if (ret != 0) {
  640. printf("default aes-128 key set failed\n");
  641. return -1;
  642. }
  643. ret = CRYPT_AES_CBC_Encrypt(&mcAes, out1, ourData, AES_TEST_SIZE);
  644. if (ret != 0) {
  645. printf("mcapi aes-128 cbc encrypt failed\n");
  646. return -1;
  647. }
  648. wc_AesCbcEncrypt(&defAes, out2, ourData, AES_TEST_SIZE);
  649. if (memcmp(out1, out2, AES_TEST_SIZE) != 0) {
  650. printf("mcapi aes-128 cbc encrypt cmp failed\n");
  651. return -1;
  652. }
  653. /* 128 cbc decrypt */
  654. ret = CRYPT_AES_KeySet(&mcAes, key, 16, iv, CRYPT_AES_DECRYPTION);
  655. if (ret != 0) {
  656. printf("mcapi aes-128 key set failed\n");
  657. return -1;
  658. }
  659. ret = wc_AesSetKey(&defAes, key, 16, iv, DES_DECRYPTION);
  660. if (ret != 0) {
  661. printf("default aes-128 key set failed\n");
  662. return -1;
  663. }
  664. ret = CRYPT_AES_CBC_Decrypt(&mcAes, out2, out1, AES_TEST_SIZE);
  665. if (ret != 0) {
  666. printf("mcapi aes-128 cbc decrypt failed\n");
  667. return -1;
  668. }
  669. wc_AesCbcDecrypt(&defAes, out1, out1, AES_TEST_SIZE);
  670. if (memcmp(out1, out2, AES_TEST_SIZE) != 0) {
  671. printf("mcapi aes-128 cbc decrypt cmp failed\n");
  672. return -1;
  673. }
  674. if (memcmp(out1, ourData, AES_TEST_SIZE) != 0) {
  675. printf("mcapi aes-128 cbc decrypt orig cmp failed\n");
  676. return -1;
  677. }
  678. /* 192 cbc encrypt */
  679. ret = CRYPT_AES_KeySet(&mcAes, key, 24, iv, CRYPT_AES_ENCRYPTION);
  680. if (ret != 0) {
  681. printf("mcapi aes-192 key set failed\n");
  682. return -1;
  683. }
  684. ret = wc_AesSetKey(&defAes, key, 24, iv, AES_ENCRYPTION);
  685. if (ret != 0) {
  686. printf("default aes-192 key set failed\n");
  687. return -1;
  688. }
  689. ret = CRYPT_AES_CBC_Encrypt(&mcAes, out1, ourData, AES_TEST_SIZE);
  690. if (ret != 0) {
  691. printf("mcapi aes-192 cbc encrypt failed\n");
  692. return -1;
  693. }
  694. wc_AesCbcEncrypt(&defAes, out2, ourData, AES_TEST_SIZE);
  695. if (memcmp(out1, out2, AES_TEST_SIZE) != 0) {
  696. printf("mcapi aes-192 cbc encrypt cmp failed\n");
  697. return -1;
  698. }
  699. /* 192 cbc decrypt */
  700. ret = CRYPT_AES_KeySet(&mcAes, key, 24, iv, CRYPT_AES_DECRYPTION);
  701. if (ret != 0) {
  702. printf("mcapi aes-192 key set failed\n");
  703. return -1;
  704. }
  705. ret = wc_AesSetKey(&defAes, key, 24, iv, AES_DECRYPTION);
  706. if (ret != 0) {
  707. printf("default aes-192 key set failed\n");
  708. return -1;
  709. }
  710. ret = CRYPT_AES_CBC_Decrypt(&mcAes, out2, out1, AES_TEST_SIZE);
  711. if (ret != 0) {
  712. printf("mcapi aes-192 cbc decrypt failed\n");
  713. return -1;
  714. }
  715. wc_AesCbcDecrypt(&defAes, out1, out1, AES_TEST_SIZE);
  716. if (memcmp(out1, out2, AES_TEST_SIZE) != 0) {
  717. printf("mcapi aes-192 cbc decrypt cmp failed\n");
  718. return -1;
  719. }
  720. if (memcmp(out1, ourData, AES_TEST_SIZE) != 0) {
  721. printf("mcapi aes-192 cbc decrypt orig cmp failed\n");
  722. return -1;
  723. }
  724. /* 256 cbc encrypt */
  725. ret = CRYPT_AES_KeySet(&mcAes, key, 32, iv, CRYPT_AES_ENCRYPTION);
  726. if (ret != 0) {
  727. printf("mcapi aes-256 key set failed\n");
  728. return -1;
  729. }
  730. ret = wc_AesSetKey(&defAes, key, 32, iv, AES_ENCRYPTION);
  731. if (ret != 0) {
  732. printf("default aes-256 key set failed\n");
  733. return -1;
  734. }
  735. ret = CRYPT_AES_CBC_Encrypt(&mcAes, out1, ourData, AES_TEST_SIZE);
  736. if (ret != 0) {
  737. printf("mcapi aes-256 cbc encrypt failed\n");
  738. return -1;
  739. }
  740. wc_AesCbcEncrypt(&defAes, out2, ourData, AES_TEST_SIZE);
  741. if (memcmp(out1, out2, AES_TEST_SIZE) != 0) {
  742. printf("mcapi aes-256 cbc encrypt cmp failed\n");
  743. return -1;
  744. }
  745. /* 256 cbc decrypt */
  746. ret = CRYPT_AES_KeySet(&mcAes, key, 32, iv, CRYPT_AES_DECRYPTION);
  747. if (ret != 0) {
  748. printf("mcapi aes-256 key set failed\n");
  749. return -1;
  750. }
  751. ret = wc_AesSetKey(&defAes, key, 32, iv, AES_DECRYPTION);
  752. if (ret != 0) {
  753. printf("default aes-256 key set failed\n");
  754. return -1;
  755. }
  756. ret = CRYPT_AES_CBC_Decrypt(&mcAes, out2, out1, AES_TEST_SIZE);
  757. if (ret != 0) {
  758. printf("mcapi aes-256 cbc decrypt failed\n");
  759. return -1;
  760. }
  761. wc_AesCbcDecrypt(&defAes, out1, out1, AES_TEST_SIZE);
  762. if (memcmp(out1, out2, AES_TEST_SIZE) != 0) {
  763. printf("mcapi aes-256 cbc decrypt cmp failed\n");
  764. return -1;
  765. }
  766. if (memcmp(out1, ourData, AES_TEST_SIZE) != 0) {
  767. printf("mcapi aes-256 cbc decrypt orig cmp failed\n");
  768. return -1;
  769. }
  770. printf("aes-cbc mcapi test passed\n");
  771. return 0;
  772. }
  773. /* check mcapi aes ctr */
  774. static int check_aesctr(void)
  775. {
  776. CRYPT_AES_CTX mcAes;
  777. Aes defAes;
  778. int ret;
  779. byte out1[AES_TEST_SIZE];
  780. byte out2[AES_TEST_SIZE];
  781. memcpy((char*)key, "1234567890abcdefghijklmnopqrstuv", 32);
  782. memcpy((char*)iv, "1234567890abcdef", 16);
  783. /* 128 ctr encrypt */
  784. ret = CRYPT_AES_KeySet(&mcAes, key, 16, iv, CRYPT_AES_ENCRYPTION);
  785. if (ret != 0) {
  786. printf("mcapi aes-128 key set failed\n");
  787. return -1;
  788. }
  789. ret = wc_AesSetKey(&defAes, key, 16, iv, AES_ENCRYPTION);
  790. if (ret != 0) {
  791. printf("default aes-128 key set failed\n");
  792. return -1;
  793. }
  794. ret = CRYPT_AES_CTR_Encrypt(&mcAes, out1, ourData, AES_TEST_SIZE);
  795. if (ret != 0) {
  796. printf("mcapi aes-128 ctr encrypt failed\n");
  797. return -1;
  798. }
  799. ret = wc_AesCtrEncrypt(&defAes, out2, ourData, AES_TEST_SIZE);
  800. if (ret != 0) {
  801. printf("mcapi aes-128 ctr encrypt set failed\n");
  802. return -1;
  803. }
  804. if (memcmp(out1, out2, AES_TEST_SIZE) != 0) {
  805. printf("mcapi aes-128 ctr encrypt cmp failed\n");
  806. return -1;
  807. }
  808. /* 128 ctr decrypt */
  809. ret = CRYPT_AES_KeySet(&mcAes, key, 16, iv, CRYPT_AES_ENCRYPTION);
  810. if (ret != 0) {
  811. printf("mcapi aes-128 key set failed\n");
  812. return -1;
  813. }
  814. ret = wc_AesSetKey(&defAes, key, 16, iv, AES_ENCRYPTION);
  815. if (ret != 0) {
  816. printf("default aes-128 key set failed\n");
  817. return -1;
  818. }
  819. ret = CRYPT_AES_CTR_Encrypt(&mcAes, out2, out1, AES_TEST_SIZE);
  820. if (ret != 0) {
  821. printf("mcapi aes-128 ctr decrypt failed\n");
  822. return -1;
  823. }
  824. if (memcmp(out2, ourData, AES_TEST_SIZE) != 0) {
  825. printf("mcapi aes-128 ctr decrypt orig cmp failed\n");
  826. return -1;
  827. }
  828. /* 192 ctr encrypt */
  829. ret = CRYPT_AES_KeySet(&mcAes, key, 24, iv, CRYPT_AES_ENCRYPTION);
  830. if (ret != 0) {
  831. printf("mcapi aes-192 key set failed\n");
  832. return -1;
  833. }
  834. ret = wc_AesSetKey(&defAes, key, 24, iv, AES_ENCRYPTION);
  835. if (ret != 0) {
  836. printf("default aes-192 key set failed\n");
  837. return -1;
  838. }
  839. ret = CRYPT_AES_CTR_Encrypt(&mcAes, out1, ourData, AES_TEST_SIZE);
  840. if (ret != 0) {
  841. printf("mcapi aes-192 ctr encrypt failed\n");
  842. return -1;
  843. }
  844. ret = wc_AesCtrEncrypt(&defAes, out2, ourData, AES_TEST_SIZE);
  845. if (ret != 0) {
  846. printf("mcapi aes-192 ctr encrypt set failed\n");
  847. return -1;
  848. }
  849. if (memcmp(out1, out2, AES_TEST_SIZE) != 0) {
  850. printf("mcapi aes-192 ctr encrypt cmp failed\n");
  851. return -1;
  852. }
  853. /* 192 ctr decrypt */
  854. ret = CRYPT_AES_KeySet(&mcAes, key, 24, iv, CRYPT_AES_ENCRYPTION);
  855. if (ret != 0) {
  856. printf("mcapi aes-192 key set failed\n");
  857. return -1;
  858. }
  859. ret = wc_AesSetKey(&defAes, key, 24, iv, AES_DECRYPTION);
  860. if (ret != 0) {
  861. printf("default aes-192 key set failed\n");
  862. return -1;
  863. }
  864. ret = CRYPT_AES_CTR_Encrypt(&mcAes, out2, out1, AES_TEST_SIZE);
  865. if (ret != 0) {
  866. printf("mcapi aes-192 ctr decrypt failed\n");
  867. return -1;
  868. }
  869. if (memcmp(out2, ourData, AES_TEST_SIZE) != 0) {
  870. printf("mcapi aes-192 ctr decrypt orig cmp failed\n");
  871. return -1;
  872. }
  873. /* 256 ctr encrypt */
  874. ret = CRYPT_AES_KeySet(&mcAes, key, 32, iv, CRYPT_AES_ENCRYPTION);
  875. if (ret != 0) {
  876. printf("mcapi aes-256 key set failed\n");
  877. return -1;
  878. }
  879. ret = wc_AesSetKey(&defAes, key, 32, iv, AES_ENCRYPTION);
  880. if (ret != 0) {
  881. printf("default aes-256 key set failed\n");
  882. return -1;
  883. }
  884. ret = CRYPT_AES_CTR_Encrypt(&mcAes, out1, ourData, AES_TEST_SIZE);
  885. if (ret != 0) {
  886. printf("mcapi aes-256 ctr encrypt failed\n");
  887. return -1;
  888. }
  889. ret = wc_AesCtrEncrypt(&defAes, out2, ourData, AES_TEST_SIZE);
  890. if (ret != 0) {
  891. printf("mcapi aes-256 ctr encrypt set failed\n");
  892. return -1;
  893. }
  894. if (memcmp(out1, out2, AES_TEST_SIZE) != 0) {
  895. printf("mcapi aes-256 ctr encrypt cmp failed\n");
  896. return -1;
  897. }
  898. /* 256 ctr decrypt */
  899. ret = CRYPT_AES_KeySet(&mcAes, key, 32, iv, CRYPT_AES_ENCRYPTION);
  900. if (ret != 0) {
  901. printf("mcapi aes-256 key set failed\n");
  902. return -1;
  903. }
  904. ret = wc_AesSetKey(&defAes, key, 32, iv, AES_ENCRYPTION);
  905. if (ret != 0) {
  906. printf("default aes-256 key set failed\n");
  907. return -1;
  908. }
  909. ret = CRYPT_AES_CTR_Encrypt(&mcAes, out2, out1, AES_TEST_SIZE);
  910. if (ret != 0) {
  911. printf("mcapi aes-256 ctr decrypt failed\n");
  912. return -1;
  913. }
  914. if (memcmp(out2, ourData, AES_TEST_SIZE) != 0) {
  915. printf("mcapi aes-256 ctr decrypt orig cmp failed\n");
  916. return -1;
  917. }
  918. printf("aes-ctr mcapi test passed\n");
  919. return 0;
  920. }
  921. /* check mcapi aes direct */
  922. static int check_aesdirect(void)
  923. {
  924. CRYPT_AES_CTX mcAes;
  925. Aes defAes;
  926. int ret;
  927. byte out1[CRYPT_AES_BLOCK_SIZE];
  928. byte out2[16]; /* one block at a time */
  929. memcpy((char*)key, "1234567890abcdefghijklmnopqrstuv", 32);
  930. memcpy((char*)iv, "1234567890abcdef", 16);
  931. /* 128 direct encrypt */
  932. ret = CRYPT_AES_KeySet(&mcAes, key, 16, iv, CRYPT_AES_ENCRYPTION);
  933. if (ret != 0) {
  934. printf("mcapi aes-128 key set failed\n");
  935. return -1;
  936. }
  937. ret = wc_AesSetKey(&defAes, key, 16, iv, AES_ENCRYPTION);
  938. if (ret != 0) {
  939. printf("default aes-128 key set failed\n");
  940. return -1;
  941. }
  942. ret = CRYPT_AES_DIRECT_Encrypt(&mcAes, out1, ourData);
  943. if (ret != 0) {
  944. printf("mcapi aes-128 direct encrypt failed\n");
  945. return -1;
  946. }
  947. wc_AesEncryptDirect(&defAes, out2, ourData);
  948. if (memcmp(out1, out2, CRYPT_AES_BLOCK_SIZE) != 0) {
  949. printf("mcapi aes-128 direct encrypt cmp failed\n");
  950. return -1;
  951. }
  952. /* 128 direct decrypt */
  953. ret = CRYPT_AES_KeySet(&mcAes, key, 16, iv, CRYPT_AES_DECRYPTION);
  954. if (ret != 0) {
  955. printf("mcapi aes-128 key set failed\n");
  956. return -1;
  957. }
  958. ret = wc_AesSetKey(&defAes, key, 16, iv, DES_DECRYPTION);
  959. if (ret != 0) {
  960. printf("default aes-128 key set failed\n");
  961. return -1;
  962. }
  963. ret = CRYPT_AES_DIRECT_Decrypt(&mcAes, out2, out1);
  964. if (ret != 0) {
  965. printf("mcapi aes-128 direct decrypt failed\n");
  966. return -1;
  967. }
  968. wc_AesDecryptDirect(&defAes, out1, out1);
  969. if (memcmp(out1, out2, CRYPT_AES_BLOCK_SIZE) != 0) {
  970. printf("mcapi aes-128 direct decrypt cmp failed\n");
  971. return -1;
  972. }
  973. if (memcmp(out1, ourData, CRYPT_AES_BLOCK_SIZE) != 0) {
  974. printf("mcapi aes-128 direct decrypt orig cmp failed\n");
  975. return -1;
  976. }
  977. /* 192 direct encrypt */
  978. ret = CRYPT_AES_KeySet(&mcAes, key, 24, iv, CRYPT_AES_ENCRYPTION);
  979. if (ret != 0) {
  980. printf("mcapi aes-192 key set failed\n");
  981. return -1;
  982. }
  983. ret = wc_AesSetKey(&defAes, key, 24, iv, AES_ENCRYPTION);
  984. if (ret != 0) {
  985. printf("default aes-192 key set failed\n");
  986. return -1;
  987. }
  988. ret = CRYPT_AES_DIRECT_Encrypt(&mcAes, out1, ourData);
  989. if (ret != 0) {
  990. printf("mcapi aes-192 direct encrypt failed\n");
  991. return -1;
  992. }
  993. wc_AesEncryptDirect(&defAes, out2, ourData);
  994. if (memcmp(out1, out2, CRYPT_AES_BLOCK_SIZE) != 0) {
  995. printf("mcapi aes-192 direct encrypt cmp failed\n");
  996. return -1;
  997. }
  998. /* 192 direct decrypt */
  999. ret = CRYPT_AES_KeySet(&mcAes, key, 24, iv, CRYPT_AES_DECRYPTION);
  1000. if (ret != 0) {
  1001. printf("mcapi aes-192 key set failed\n");
  1002. return -1;
  1003. }
  1004. ret = wc_AesSetKey(&defAes, key, 24, iv, AES_DECRYPTION);
  1005. if (ret != 0) {
  1006. printf("default aes-192 key set failed\n");
  1007. return -1;
  1008. }
  1009. ret = CRYPT_AES_DIRECT_Decrypt(&mcAes, out2, out1);
  1010. if (ret != 0) {
  1011. printf("mcapi aes-192 direct decrypt failed\n");
  1012. return -1;
  1013. }
  1014. wc_AesDecryptDirect(&defAes, out1, out1);
  1015. if (memcmp(out1, out2, CRYPT_AES_BLOCK_SIZE) != 0) {
  1016. printf("mcapi aes-192 direct decrypt cmp failed\n");
  1017. return -1;
  1018. }
  1019. if (memcmp(out1, ourData, CRYPT_AES_BLOCK_SIZE) != 0) {
  1020. printf("mcapi aes-192 direct decrypt orig cmp failed\n");
  1021. return -1;
  1022. }
  1023. /* 256 direct encrypt */
  1024. ret = CRYPT_AES_KeySet(&mcAes, key, 32, iv, CRYPT_AES_ENCRYPTION);
  1025. if (ret != 0) {
  1026. printf("mcapi aes-256 key set failed\n");
  1027. return -1;
  1028. }
  1029. ret = wc_AesSetKey(&defAes, key, 32, iv, AES_ENCRYPTION);
  1030. if (ret != 0) {
  1031. printf("default aes-256 key set failed\n");
  1032. return -1;
  1033. }
  1034. ret = CRYPT_AES_DIRECT_Encrypt(&mcAes, out1, ourData);
  1035. if (ret != 0) {
  1036. printf("mcapi aes-256 direct encrypt failed\n");
  1037. return -1;
  1038. }
  1039. wc_AesEncryptDirect(&defAes, out2, ourData);
  1040. if (memcmp(out1, out2, CRYPT_AES_BLOCK_SIZE) != 0) {
  1041. printf("mcapi aes-256 direct encrypt cmp failed\n");
  1042. return -1;
  1043. }
  1044. /* 256 direct decrypt */
  1045. ret = CRYPT_AES_KeySet(&mcAes, key, 32, iv, CRYPT_AES_DECRYPTION);
  1046. if (ret != 0) {
  1047. printf("mcapi aes-256 key set failed\n");
  1048. return -1;
  1049. }
  1050. ret = wc_AesSetKey(&defAes, key, 32, iv, AES_DECRYPTION);
  1051. if (ret != 0) {
  1052. printf("default aes-256 key set failed\n");
  1053. return -1;
  1054. }
  1055. ret = CRYPT_AES_DIRECT_Decrypt(&mcAes, out2, out1);
  1056. if (ret != 0) {
  1057. printf("mcapi aes-256 direct decrypt failed\n");
  1058. return -1;
  1059. }
  1060. wc_AesDecryptDirect(&defAes, out1, out1);
  1061. if (memcmp(out1, out2, CRYPT_AES_BLOCK_SIZE) != 0) {
  1062. printf("mcapi aes-256 direct decrypt cmp failed\n");
  1063. return -1;
  1064. }
  1065. if (memcmp(out1, ourData, CRYPT_AES_BLOCK_SIZE) != 0) {
  1066. printf("mcapi aes-256 direct decrypt orig cmp failed\n");
  1067. return -1;
  1068. }
  1069. printf("aes-direct mcapi test passed\n");
  1070. return 0;
  1071. }
  1072. #define RSA_TEST_SIZE 64
  1073. /* check mcapi rsa */
  1074. static int check_rsa(void)
  1075. {
  1076. CRYPT_RSA_CTX mcRsa;
  1077. RsaKey defRsa;
  1078. int ret;
  1079. int ret2;
  1080. unsigned int keySz = (unsigned int)sizeof(client_key_der_1024);
  1081. unsigned int idx = 0;
  1082. byte out1[256];
  1083. byte out2[256];
  1084. ret = wc_InitRsaKey(&defRsa, NULL);
  1085. if (ret == 0)
  1086. ret = CRYPT_RSA_Initialize(&mcRsa);
  1087. if (ret != 0) {
  1088. printf("mcapi rsa init failed\n");
  1089. return -1;
  1090. }
  1091. ret = CRYPT_RSA_PrivateKeyDecode(&mcRsa, client_key_der_1024, keySz);
  1092. if (ret != 0) {
  1093. printf("mcapi rsa private key decode failed\n");
  1094. return -1;
  1095. }
  1096. ret = wc_RsaPrivateKeyDecode(client_key_der_1024, &idx, &defRsa, keySz);
  1097. if (ret != 0) {
  1098. printf("default rsa private key decode failed\n");
  1099. return -1;
  1100. }
  1101. ret = CRYPT_RSA_SetRng(&mcRsa, &mcRng);
  1102. if (ret != 0) {
  1103. printf("mcapi rsa set rng failed\n");
  1104. return -1;
  1105. }
  1106. ret = CRYPT_RSA_PublicEncrypt(&mcRsa, out1, sizeof(out1), ourData,
  1107. RSA_TEST_SIZE, &mcRng);
  1108. if (ret < 0) {
  1109. printf("mcapi rsa public encrypt failed\n");
  1110. return -1;
  1111. }
  1112. ret2 = wc_RsaPublicEncrypt(ourData, RSA_TEST_SIZE, out2, sizeof(out2),
  1113. &defRsa, &defRng);
  1114. if (ret2 < 0) {
  1115. printf("default rsa public encrypt failed\n");
  1116. return -1;
  1117. }
  1118. if (ret != ret2) {
  1119. printf("default rsa public encrypt sz != mcapi sz\n");
  1120. return -1;
  1121. }
  1122. if (ret != CRYPT_RSA_EncryptSizeGet(&mcRsa)) {
  1123. printf("mcapi encrypt sz get != mcapi sz\n");
  1124. return -1;
  1125. }
  1126. ret = CRYPT_RSA_PrivateDecrypt(&mcRsa, out2, sizeof(out2), out1, ret);
  1127. if (ret < 0) {
  1128. printf("mcapi rsa private derypt failed\n");
  1129. return -1;
  1130. }
  1131. if (ret != RSA_TEST_SIZE) {
  1132. printf("mcapi rsa private derypt plain size wrong\n");
  1133. return -1;
  1134. }
  1135. if (memcmp(out2, ourData, ret) != 0) {
  1136. printf("mcapi rsa private derypt plain text bad\n");
  1137. return -1;
  1138. }
  1139. wc_FreeRsaKey(&defRsa);
  1140. ret = CRYPT_RSA_Free(&mcRsa);
  1141. if (ret != 0) {
  1142. printf("mcapi rsa free failed\n");
  1143. return -1;
  1144. }
  1145. printf("rsa mcapi test passed\n");
  1146. return 0;
  1147. }
  1148. /* check mcapi ecc */
  1149. static int check_ecc(void)
  1150. {
  1151. CRYPT_ECC_CTX userA;
  1152. CRYPT_ECC_CTX userB;
  1153. int ret;
  1154. byte sharedA[100];
  1155. byte sharedB[100];
  1156. byte sig[100];
  1157. unsigned int aSz = (unsigned int)sizeof(sharedA);
  1158. unsigned int bSz = (unsigned int)sizeof(sharedB);
  1159. unsigned int sigSz = (unsigned int)sizeof(sig);
  1160. unsigned int usedA = 0;
  1161. unsigned int usedB = 0;
  1162. int verifyStatus = 0;
  1163. /* init */
  1164. ret = CRYPT_ECC_Initialize(&userA);
  1165. if (ret != 0) {
  1166. printf("mcapi ecc init failed\n");
  1167. return -1;
  1168. }
  1169. ret = CRYPT_ECC_Initialize(&userB);
  1170. if (ret != 0) {
  1171. printf("mcapi ecc init b failed\n");
  1172. return -1;
  1173. }
  1174. /* dhe + helpers */
  1175. ret = CRYPT_ECC_DHE_KeyMake(&userA, &mcRng, 32);
  1176. if (ret != 0) {
  1177. printf("mcapi ecc make key failed\n");
  1178. return -1;
  1179. }
  1180. ret = CRYPT_ECC_DHE_KeyMake(&userB, &mcRng, 32);
  1181. if (ret != 0) {
  1182. printf("mcapi ecc make key b failed\n");
  1183. return -1;
  1184. }
  1185. ret = CRYPT_ECC_KeySizeGet(&userA);
  1186. if (ret <= 0) {
  1187. printf("mcapi ecc key size get failed\n");
  1188. return -1;
  1189. }
  1190. ret = CRYPT_ECC_SignatureSizeGet(&userA);
  1191. if (ret <= 0) {
  1192. printf("mcapi ecc signature size get failed\n");
  1193. return -1;
  1194. }
  1195. ret = CRYPT_ECC_DHE_SharedSecretMake(&userA, &userB, sharedA, aSz, &usedA);
  1196. if (ret != 0) {
  1197. printf("mcapi ecc make shared secret failed\n");
  1198. return -1;
  1199. }
  1200. ret = CRYPT_ECC_DHE_SharedSecretMake(&userB, &userA, sharedB, bSz, &usedB);
  1201. if (ret != 0) {
  1202. printf("mcapi ecc make shared secret failed\n");
  1203. return -1;
  1204. }
  1205. if (usedA != usedB || usedA == 0) {
  1206. printf("mcapi ecc make shared secret output size match failed\n");
  1207. return -1;
  1208. }
  1209. if (memcmp(sharedA, sharedB, usedA) != 0) {
  1210. printf("mcapi ecc make shared secret output match cmp failed\n");
  1211. return -1;
  1212. }
  1213. /* dsa */
  1214. ret = CRYPT_ECC_DSA_HashSign(&userA, &mcRng, sig, sigSz, &usedA, ourData,
  1215. CRYPT_SHA_DIGEST_SIZE);
  1216. if (ret != 0) {
  1217. printf("mcapi ecc sign hash failed\n");
  1218. return -1;
  1219. }
  1220. sigSz = usedA;
  1221. if (sigSz == 0) {
  1222. printf("mcapi ecc sign hash bad sig size\n");
  1223. return -1;
  1224. }
  1225. ret = CRYPT_ECC_DSA_HashVerify(&userA, sig, sigSz, ourData,
  1226. CRYPT_SHA_DIGEST_SIZE, &verifyStatus);
  1227. if (ret != 0) {
  1228. printf("mcapi ecc verify hash failed\n");
  1229. return -1;
  1230. }
  1231. if (verifyStatus != 1) {
  1232. printf("mcapi ecc verify hash status failed\n");
  1233. return -1;
  1234. }
  1235. /* import / export */
  1236. usedA = 0;
  1237. ret = CRYPT_ECC_PublicExport(&userA, sharedA, aSz, &usedA);
  1238. if (ret != 0) {
  1239. printf("mcapi ecc public export failed\n");
  1240. return -1;
  1241. }
  1242. ret = CRYPT_ECC_PublicImport(&userB, sharedA, usedA);
  1243. if (ret != 0) {
  1244. printf("mcapi ecc public import failed\n");
  1245. return -1;
  1246. }
  1247. ret = CRYPT_ECC_Free(&userA);
  1248. if (ret != 0) {
  1249. printf("mcapi ecc free failed\n");
  1250. return -1;
  1251. }
  1252. ret = CRYPT_ECC_Free(&userB);
  1253. if (ret != 0) {
  1254. printf("mcapi ecc free b failed\n");
  1255. return -1;
  1256. }
  1257. printf("ecc mcapi test passed\n");
  1258. return 0;
  1259. }