mcapi_test.c 40 KB

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