mcapi_test.c 40 KB

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