test.c 36 KB

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