2
0

crypto.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. /* crypto.c
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. /* Implements Microchip CRYPTO API layer */
  22. #ifdef HAVE_CONFIG_H
  23. #include "config.h"
  24. #endif
  25. #include <wolfssl/wolfcrypt/settings.h>
  26. #ifdef MICROCHIP_MPLAB_HARMONY
  27. #include "system_config.h"
  28. #include "crypto/crypto.h"
  29. #else
  30. #include "crypto.h"
  31. #endif
  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/rsa.h>
  42. #include <wolfssl/wolfcrypt/ecc.h>
  43. #include <wolfssl/wolfcrypt/error-crypt.h>
  44. #ifndef NO_MD5
  45. /* Initialize MD5 */
  46. int CRYPT_MD5_Initialize(CRYPT_MD5_CTX* md5)
  47. {
  48. typedef char md5_test[sizeof(CRYPT_MD5_CTX) >= sizeof(Md5) ? 1 : -1];
  49. (void)sizeof(md5_test);
  50. if (md5 == NULL)
  51. return BAD_FUNC_ARG;
  52. return wc_InitMd5((Md5*)md5);
  53. }
  54. int CRYPT_MD5_DataSizeSet(CRYPT_MD5_CTX* md5, unsigned int sz)
  55. {
  56. if (md5 == NULL)
  57. return BAD_FUNC_ARG;
  58. #ifdef WOLFSSL_PIC32MZ_HASH
  59. wc_Md5SizeSet((Md5*)md5, sz);
  60. #else
  61. (void)sz;
  62. #endif
  63. return 0;
  64. }
  65. /* Add data to MD5 */
  66. int CRYPT_MD5_DataAdd(CRYPT_MD5_CTX* md5, const unsigned char* input,
  67. unsigned int sz)
  68. {
  69. if (md5 == NULL || input == NULL)
  70. return BAD_FUNC_ARG;
  71. return wc_Md5Update((Md5*)md5, input, sz);
  72. }
  73. /* Get MD5 Final into digest */
  74. int CRYPT_MD5_Finalize(CRYPT_MD5_CTX* md5, unsigned char* digest)
  75. {
  76. if (md5 == NULL || digest == NULL)
  77. return BAD_FUNC_ARG;
  78. return wc_Md5Final((Md5*)md5, digest);
  79. }
  80. #endif
  81. #ifndef NO_SHA
  82. /* Initialize SHA */
  83. int CRYPT_SHA_Initialize(CRYPT_SHA_CTX* sha)
  84. {
  85. typedef char sha_test[sizeof(CRYPT_SHA_CTX) >= sizeof(Sha) ? 1 : -1];
  86. (void)sizeof(sha_test);
  87. if (sha == NULL)
  88. return BAD_FUNC_ARG;
  89. return wc_InitSha((wc_Sha*)sha);
  90. }
  91. int CRYPT_SHA_DataSizeSet(CRYPT_SHA_CTX* sha, unsigned int sz)
  92. {
  93. if (sha == NULL)
  94. return BAD_FUNC_ARG;
  95. #ifdef WOLFSSL_PIC32MZ_HASH
  96. wc_ShaSizeSet((wc_Sha*)sha, sz);
  97. #else
  98. (void)sz;
  99. #endif
  100. return 0;
  101. }
  102. /* Add data to SHA */
  103. int CRYPT_SHA_DataAdd(CRYPT_SHA_CTX* sha, const unsigned char* input,
  104. unsigned int sz)
  105. {
  106. if (sha == NULL || input == NULL)
  107. return BAD_FUNC_ARG;
  108. return wc_ShaUpdate((wc_Sha*)sha, input, sz);
  109. }
  110. /* Get SHA Final into digest */
  111. int CRYPT_SHA_Finalize(CRYPT_SHA_CTX* sha, unsigned char* digest)
  112. {
  113. if (sha == NULL || digest == NULL)
  114. return BAD_FUNC_ARG;
  115. return wc_ShaFinal((wc_Sha*)sha, digest);
  116. }
  117. #endif
  118. #ifndef NO_SHA256
  119. /* Initialize SHA-256 */
  120. int CRYPT_SHA256_Initialize(CRYPT_SHA256_CTX* sha256)
  121. {
  122. typedef char sha_test[sizeof(CRYPT_SHA256_CTX) >= sizeof(wc_Sha256) ? 1 : -1];
  123. (void)sizeof(sha_test);
  124. if (sha256 == NULL)
  125. return BAD_FUNC_ARG;
  126. return wc_InitSha256((wc_Sha256*)sha256);
  127. }
  128. int CRYPT_SHA256_DataSizeSet(CRYPT_SHA256_CTX* sha256, unsigned int sz)
  129. {
  130. if (sha256 == NULL)
  131. return BAD_FUNC_ARG;
  132. #ifdef WOLFSSL_PIC32MZ_HASH
  133. wc_Sha256SizeSet((wc_Sha256*)sha256, sz);
  134. #else
  135. (void)sz;
  136. #endif
  137. return 0;
  138. }
  139. /* Add data to SHA-256 */
  140. int CRYPT_SHA256_DataAdd(CRYPT_SHA256_CTX* sha256, const unsigned char* input,
  141. unsigned int sz)
  142. {
  143. if (sha256 == NULL || input == NULL)
  144. return BAD_FUNC_ARG;
  145. return wc_Sha256Update((wc_Sha256*)sha256, input, sz);
  146. }
  147. /* Get SHA-256 Final into digest */
  148. int CRYPT_SHA256_Finalize(CRYPT_SHA256_CTX* sha256, unsigned char* digest)
  149. {
  150. if (sha256 == NULL || digest == NULL)
  151. return BAD_FUNC_ARG;
  152. return wc_Sha256Final((wc_Sha256*)sha256, digest);
  153. }
  154. #endif
  155. #ifdef WOLFSSL_SHA512
  156. #ifdef WOLFSSL_SHA384
  157. /* Initialize SHA-384 */
  158. int CRYPT_SHA384_Initialize(CRYPT_SHA384_CTX* sha384)
  159. {
  160. typedef char sha_test[sizeof(CRYPT_SHA384_CTX) >= sizeof(wc_Sha384) ? 1 : -1];
  161. (void)sizeof(sha_test);
  162. if (sha384 == NULL)
  163. return BAD_FUNC_ARG;
  164. return wc_InitSha384((wc_Sha384*)sha384);
  165. }
  166. /* Add data to SHA-384 */
  167. int CRYPT_SHA384_DataAdd(CRYPT_SHA384_CTX* sha384, const unsigned char* input,
  168. unsigned int sz)
  169. {
  170. if (sha384 == NULL || input == NULL)
  171. return BAD_FUNC_ARG;
  172. return wc_Sha384Update((wc_Sha384*)sha384, input, sz);
  173. }
  174. /* Get SHA-384 Final into digest */
  175. int CRYPT_SHA384_Finalize(CRYPT_SHA384_CTX* sha384, unsigned char* digest)
  176. {
  177. if (sha384 == NULL || digest == NULL)
  178. return BAD_FUNC_ARG;
  179. return wc_Sha384Final((wc_Sha384*)sha384, digest);
  180. }
  181. #endif
  182. /* Initialize SHA-512 */
  183. int CRYPT_SHA512_Initialize(CRYPT_SHA512_CTX* sha512)
  184. {
  185. typedef char sha_test[sizeof(CRYPT_SHA512_CTX) >= sizeof(wc_Sha512) ? 1 : -1];
  186. (void)sizeof(sha_test);
  187. if (sha512 == NULL)
  188. return BAD_FUNC_ARG;
  189. return wc_InitSha512((wc_Sha512*)sha512);
  190. }
  191. /* Add data to SHA-512 */
  192. int CRYPT_SHA512_DataAdd(CRYPT_SHA512_CTX* sha512, const unsigned char* input,
  193. unsigned int sz)
  194. {
  195. if (sha512 == NULL || input == NULL)
  196. return BAD_FUNC_ARG;
  197. return wc_Sha512Update((wc_Sha512*)sha512, input, sz);
  198. }
  199. /* Get SHA-512 Final into digest */
  200. int CRYPT_SHA512_Finalize(CRYPT_SHA512_CTX* sha512, unsigned char* digest)
  201. {
  202. if (sha512 == NULL || digest == NULL)
  203. return BAD_FUNC_ARG;
  204. return wc_Sha512Final((wc_Sha512*)sha512, digest);
  205. }
  206. #endif
  207. #ifndef NO_HMAC
  208. /* Set HMAC key with type */
  209. int CRYPT_HMAC_SetKey(CRYPT_HMAC_CTX* hmac, int type, const unsigned char* key,
  210. unsigned int sz)
  211. {
  212. /* compile-time check to verify CRYPT_HMAC_CTX is large enough to hold Hmac */
  213. typedef char hmac_test[sizeof(CRYPT_HMAC_CTX) >= sizeof(Hmac) ? 1 : -1];
  214. (void)sizeof(hmac_test);
  215. if (hmac == NULL || key == NULL)
  216. return BAD_FUNC_ARG;
  217. if (type != CRYPT_HMAC_SHA && type != CRYPT_HMAC_SHA256 &&
  218. type != CRYPT_HMAC_SHA384 && type != CRYPT_HMAC_SHA512) {
  219. return BAD_FUNC_ARG; /* bad hmac type */
  220. }
  221. return wc_HmacSetKey((Hmac*)hmac, type, key, sz);
  222. }
  223. int CRYPT_HMAC_DataAdd(CRYPT_HMAC_CTX* hmac, const unsigned char* input,
  224. unsigned int sz)
  225. {
  226. if (hmac == NULL || input == NULL)
  227. return BAD_FUNC_ARG;
  228. return wc_HmacUpdate((Hmac*)hmac, input, sz);
  229. }
  230. /* Get HMAC Final into digest */
  231. int CRYPT_HMAC_Finalize(CRYPT_HMAC_CTX* hmac, unsigned char* digest)
  232. {
  233. if (hmac == NULL || digest == NULL)
  234. return BAD_FUNC_ARG;
  235. return wc_HmacFinal((Hmac*)hmac, digest);
  236. }
  237. #endif
  238. #ifdef HAVE_LIBZ
  239. /* Huffman Compression, set flag to do static, otherwise dynamic */
  240. /* return compressed size, otherwise < 0 for error */
  241. int CRYPT_HUFFMAN_Compress(unsigned char* out, unsigned int outSz,
  242. const unsigned char* in, unsigned int inSz,
  243. unsigned int flags)
  244. {
  245. if (out == NULL || in == NULL)
  246. return BAD_FUNC_ARG;
  247. return wc_Compress(out, outSz, in, inSz, flags);
  248. }
  249. /* Huffman DeCompression, self determines type */
  250. /* return decompressed size, otherwise < 0 for error */
  251. int CRYPT_HUFFMAN_DeCompress(unsigned char* out, unsigned int outSz,
  252. const unsigned char* in, unsigned int inSz)
  253. {
  254. if (out == NULL || in == NULL)
  255. return BAD_FUNC_ARG;
  256. return wc_DeCompress(out, outSz, in, inSz);
  257. }
  258. #endif
  259. #ifndef NO_RNG
  260. /* RNG Initialize, < 0 on error */
  261. int CRYPT_RNG_Initialize(CRYPT_RNG_CTX* rng)
  262. {
  263. typedef char rng_test[sizeof(CRYPT_RNG_CTX) >= sizeof(WC_RNG) ? 1 : -1];
  264. (void)sizeof(rng_test);
  265. if (rng == NULL)
  266. return BAD_FUNC_ARG;
  267. return wc_InitRng((WC_RNG*)rng);
  268. }
  269. /* RNG Get single bytes, < 0 on error */
  270. int CRYPT_RNG_Get(CRYPT_RNG_CTX* rng, unsigned char* b)
  271. {
  272. if (rng == NULL || b == NULL)
  273. return BAD_FUNC_ARG;
  274. return wc_RNG_GenerateByte((WC_RNG*)rng, (byte*)b);
  275. }
  276. /* RNG Block Generation of sz bytes, < 0 on error */
  277. int CRYPT_RNG_BlockGenerate(CRYPT_RNG_CTX* rng, unsigned char* b,
  278. unsigned int sz)
  279. {
  280. if (rng == NULL || b == NULL)
  281. return BAD_FUNC_ARG;
  282. return wc_RNG_GenerateBlock((WC_RNG*)rng, b, sz);
  283. }
  284. #endif
  285. #ifndef NO_DES3
  286. /* Triple DES Key Set, may have iv, will have direction */
  287. int CRYPT_TDES_KeySet(CRYPT_TDES_CTX* tdes, const unsigned char* key,
  288. const unsigned char* iv, int dir)
  289. {
  290. typedef char tdes_test[sizeof(CRYPT_TDES_CTX) >= sizeof(Des3) ? 1 : -1];
  291. (void)sizeof(tdes_test);
  292. if (tdes == NULL || key == NULL)
  293. return BAD_FUNC_ARG;
  294. return wc_Des3_SetKey((Des3*)tdes, key, iv, dir);
  295. }
  296. /* Triple DES Iv Set, sometimes added later */
  297. int CRYPT_TDES_IvSet(CRYPT_TDES_CTX* tdes, const unsigned char* iv)
  298. {
  299. if (tdes == NULL || iv == NULL)
  300. return BAD_FUNC_ARG;
  301. return wc_Des3_SetIV((Des3*)tdes, iv);
  302. }
  303. /* Triple DES CBC Encrypt */
  304. int CRYPT_TDES_CBC_Encrypt(CRYPT_TDES_CTX* tdes, unsigned char* out,
  305. const unsigned char* in, unsigned int inSz)
  306. {
  307. if (tdes == NULL || out == NULL || in == NULL)
  308. return BAD_FUNC_ARG;
  309. return wc_Des3_CbcEncrypt((Des3*)tdes, out, in, inSz);
  310. }
  311. /* Triple DES CBC Decrypt */
  312. int CRYPT_TDES_CBC_Decrypt(CRYPT_TDES_CTX* tdes, unsigned char* out,
  313. const unsigned char* in, unsigned int inSz)
  314. {
  315. if (tdes == NULL || out == NULL || in == NULL)
  316. return BAD_FUNC_ARG;
  317. return wc_Des3_CbcDecrypt((Des3*)tdes, out, in, inSz);
  318. }
  319. #endif
  320. #ifndef NO_AES
  321. /* AES Key Set, may have iv, will have direction */
  322. int CRYPT_AES_KeySet(CRYPT_AES_CTX* aes, const unsigned char* key,
  323. unsigned int keyLen, const unsigned char* iv, int dir)
  324. {
  325. typedef char aes_test[sizeof(CRYPT_AES_CTX) >= sizeof(Aes) ? 1 : -1];
  326. (void)sizeof(aes_test);
  327. if (aes == NULL || key == NULL)
  328. return BAD_FUNC_ARG;
  329. return wc_AesSetKey((Aes*)aes, key, keyLen, iv, dir);
  330. }
  331. /* AES Iv Set, sometimes added later */
  332. int CRYPT_AES_IvSet(CRYPT_AES_CTX* aes, const unsigned char* iv)
  333. {
  334. if (aes == NULL || iv == NULL)
  335. return BAD_FUNC_ARG;
  336. return wc_AesSetIV((Aes*)aes, iv);
  337. }
  338. /* AES CBC Encrypt */
  339. int CRYPT_AES_CBC_Encrypt(CRYPT_AES_CTX* aes, unsigned char* out,
  340. const unsigned char* in, unsigned int inSz)
  341. {
  342. if (aes == NULL || out == NULL || in == NULL)
  343. return BAD_FUNC_ARG;
  344. return wc_AesCbcEncrypt((Aes*)aes, out, in, inSz);
  345. }
  346. /* AES CBC Decrypt */
  347. int CRYPT_AES_CBC_Decrypt(CRYPT_AES_CTX* aes, unsigned char* out,
  348. const unsigned char* in, unsigned int inSz)
  349. {
  350. if (aes == NULL || out == NULL || in == NULL)
  351. return BAD_FUNC_ARG;
  352. return wc_AesCbcDecrypt((Aes*)aes, out, in, inSz);
  353. }
  354. #endif
  355. #ifdef WOLFSSL_AES_COUNTER
  356. /* AES CTR Encrypt (used for decrypt too, with ENCRYPT key setup) */
  357. int CRYPT_AES_CTR_Encrypt(CRYPT_AES_CTX* aes, unsigned char* out,
  358. const unsigned char* in, unsigned int inSz)
  359. {
  360. if (aes == NULL || out == NULL || in == NULL)
  361. return BAD_FUNC_ARG;
  362. return wc_AesCtrEncrypt((Aes*)aes, out, in, inSz);
  363. }
  364. #endif
  365. #ifdef WOLFSSL_AES_DIRECT
  366. /* AES Direct mode encrypt, one block at a time */
  367. int CRYPT_AES_DIRECT_Encrypt(CRYPT_AES_CTX* aes, unsigned char* out,
  368. const unsigned char* in)
  369. {
  370. if (aes == NULL || out == NULL || in == NULL)
  371. return BAD_FUNC_ARG;
  372. wc_AesEncryptDirect((Aes*)aes, out, in);
  373. return 0;
  374. }
  375. /* AES Direct mode decrypt, one block at a time */
  376. int CRYPT_AES_DIRECT_Decrypt(CRYPT_AES_CTX* aes, unsigned char* out,
  377. const unsigned char* in)
  378. {
  379. if (aes == NULL || out == NULL || in == NULL)
  380. return BAD_FUNC_ARG;
  381. wc_AesDecryptDirect((Aes*)aes, out, in);
  382. return 0;
  383. }
  384. #endif
  385. #ifndef NO_RSA
  386. /* RSA Initialize */
  387. int CRYPT_RSA_Initialize(CRYPT_RSA_CTX* rsa)
  388. {
  389. if (rsa == NULL)
  390. return BAD_FUNC_ARG;
  391. rsa->holder = (RsaKey*)XMALLOC(sizeof(RsaKey), NULL, DYNAMIC_TYPE_RSA);
  392. if (rsa->holder == NULL)
  393. return -1;
  394. return wc_InitRsaKey((RsaKey*)rsa->holder, NULL);
  395. }
  396. /* RSA Free resources */
  397. int CRYPT_RSA_Free(CRYPT_RSA_CTX* rsa)
  398. {
  399. if (rsa == NULL)
  400. return BAD_FUNC_ARG;
  401. wc_FreeRsaKey((RsaKey*)rsa->holder);
  402. XFREE(rsa->holder, NULL, DYNAMIC_TYPE_RSA);
  403. rsa->holder = NULL;
  404. return 0;
  405. }
  406. /* RSA Public key decode ASN.1 */
  407. int CRYPT_RSA_PublicKeyDecode(CRYPT_RSA_CTX* rsa, const unsigned char* in,
  408. unsigned int inSz)
  409. {
  410. unsigned int idx = 0;
  411. (void)idx;
  412. if (rsa == NULL || in == NULL)
  413. return BAD_FUNC_ARG;
  414. return wc_RsaPublicKeyDecode(in, &idx, (RsaKey*)rsa->holder, inSz);
  415. }
  416. /* RSA Private key decode ASN.1 */
  417. int CRYPT_RSA_PrivateKeyDecode(CRYPT_RSA_CTX* rsa, const unsigned char* in,
  418. unsigned int inSz)
  419. {
  420. unsigned int idx = 0;
  421. (void)idx;
  422. if (rsa == NULL || in == NULL)
  423. return BAD_FUNC_ARG;
  424. return wc_RsaPrivateKeyDecode(in, &idx, (RsaKey*)rsa->holder, inSz);
  425. }
  426. /* RSA Public Encrypt */
  427. int CRYPT_RSA_PublicEncrypt(CRYPT_RSA_CTX* rsa, unsigned char* out,
  428. unsigned int outSz, const unsigned char* in,
  429. unsigned int inSz, CRYPT_RNG_CTX* rng)
  430. {
  431. if (rsa == NULL || in == NULL || out == NULL || rng == NULL)
  432. return BAD_FUNC_ARG;
  433. return wc_RsaPublicEncrypt(in, inSz, out, outSz, (RsaKey*)rsa->holder,
  434. (WC_RNG*)rng);
  435. }
  436. /* RSA Private Decrypt */
  437. int CRYPT_RSA_PrivateDecrypt(CRYPT_RSA_CTX* rsa, unsigned char* out,
  438. unsigned int outSz, const unsigned char* in,
  439. unsigned int inSz)
  440. {
  441. if (rsa == NULL || in == NULL || out == NULL)
  442. return BAD_FUNC_ARG;
  443. return wc_RsaPrivateDecrypt(in, inSz, out, outSz, (RsaKey*)rsa->holder);
  444. }
  445. /* RSA Get Encrypt size helper */
  446. int CRYPT_RSA_EncryptSizeGet(CRYPT_RSA_CTX* rsa)
  447. {
  448. if (rsa == NULL)
  449. return BAD_FUNC_ARG;
  450. return wc_RsaEncryptSize((RsaKey*)rsa->holder);
  451. }
  452. int CRYPT_RSA_SetRng(CRYPT_RSA_CTX* rsa, CRYPT_RNG_CTX* rng)
  453. {
  454. if (rsa == NULL)
  455. return BAD_FUNC_ARG;
  456. #ifdef WC_RSA_BLINDING
  457. return wc_RsaSetRNG((RsaKey*)rsa->holder, (WC_RNG*)rng);
  458. #else
  459. (void)rng;
  460. return 0;
  461. #endif
  462. }
  463. #endif
  464. #ifdef HAVE_ECC
  465. /* ECC init */
  466. int CRYPT_ECC_Initialize(CRYPT_ECC_CTX* ecc)
  467. {
  468. if (ecc == NULL)
  469. return BAD_FUNC_ARG;
  470. ecc->holder = (ecc_key*)XMALLOC(sizeof(ecc_key), NULL, DYNAMIC_TYPE_ECC);
  471. if (ecc->holder == NULL)
  472. return -1;
  473. wc_ecc_init((ecc_key*)ecc->holder);
  474. return 0;
  475. }
  476. /* ECC free resources */
  477. int CRYPT_ECC_Free(CRYPT_ECC_CTX* ecc)
  478. {
  479. if (ecc == NULL)
  480. return BAD_FUNC_ARG;
  481. wc_ecc_free((ecc_key*)ecc->holder);
  482. XFREE(ecc->holder, NULL, DYNAMIC_TYPE_ECC);
  483. ecc->holder = NULL;
  484. return 0;
  485. }
  486. /* ECC Public x963 Export */
  487. int CRYPT_ECC_PublicExport(CRYPT_ECC_CTX* ecc, unsigned char* out,
  488. unsigned int outSz, unsigned int* usedSz)
  489. {
  490. int ret;
  491. unsigned int inOut = outSz;
  492. if (ecc == NULL || out == NULL)
  493. return BAD_FUNC_ARG;
  494. ret = wc_ecc_export_x963((ecc_key*)ecc->holder, out, &inOut);
  495. *usedSz = inOut;
  496. return ret;
  497. }
  498. /* ECC Public x963 Import */
  499. int CRYPT_ECC_PublicImport(CRYPT_ECC_CTX* ecc, const unsigned char* in,
  500. unsigned int inSz)
  501. {
  502. if (ecc == NULL || in == NULL)
  503. return BAD_FUNC_ARG;
  504. return wc_ecc_import_x963(in, inSz, (ecc_key*)ecc->holder);
  505. }
  506. /* ECC Private x963 Import */
  507. int CRYPT_ECC_PrivateImport(CRYPT_ECC_CTX* ecc, const unsigned char* priv,
  508. unsigned int privSz, const unsigned char* pub, unsigned int pubSz)
  509. {
  510. if (ecc == NULL || priv == NULL || pub == NULL)
  511. return BAD_FUNC_ARG;
  512. return wc_ecc_import_private_key(priv, privSz, pub, pubSz,
  513. (ecc_key*)ecc->holder);
  514. }
  515. /* ECC DHE Make key */
  516. int CRYPT_ECC_DHE_KeyMake(CRYPT_ECC_CTX* ecc, CRYPT_RNG_CTX* rng, int keySz)
  517. {
  518. if (ecc == NULL || rng == NULL)
  519. return BAD_FUNC_ARG;
  520. return wc_ecc_make_key((WC_RNG*)rng, keySz, (ecc_key*)ecc->holder);
  521. }
  522. /* ECC DHE Make shared secret with our private and peer public */
  523. int CRYPT_ECC_DHE_SharedSecretMake(CRYPT_ECC_CTX* priv, CRYPT_ECC_CTX* pub,
  524. unsigned char* out, unsigned int outSz, unsigned int* usedSz)
  525. {
  526. int ret;
  527. unsigned int inOut = outSz;
  528. #if defined(ECC_TIMING_RESISTANT)
  529. WC_RNG rng;
  530. #endif
  531. if (priv == NULL || pub == NULL || out == NULL || usedSz == NULL)
  532. return BAD_FUNC_ARG;
  533. #if defined(ECC_TIMING_RESISTANT)
  534. ret = wc_InitRng(&rng);
  535. if (ret != 0)
  536. return ret;
  537. ret = wc_ecc_set_rng((ecc_key*)priv->holder, &rng);
  538. if (ret != 0) {
  539. wc_FreeRng(&rng);
  540. return ret;
  541. }
  542. #endif
  543. ret = wc_ecc_shared_secret((ecc_key*)priv->holder, (ecc_key*)pub->holder,
  544. out, &inOut);
  545. *usedSz = inOut;
  546. #if defined(ECC_TIMING_RESISTANT)
  547. wc_FreeRng(&rng);
  548. #endif
  549. return ret;
  550. }
  551. /* ECC DSA Hash Sign */
  552. int CRYPT_ECC_DSA_HashSign(CRYPT_ECC_CTX* ecc, CRYPT_RNG_CTX* rng,
  553. unsigned char* sig, unsigned int sigSz,
  554. unsigned int* usedSz, const unsigned char* in,
  555. unsigned int inSz)
  556. {
  557. int ret;
  558. unsigned int inOut = sigSz;
  559. if (ecc == NULL || rng == NULL || sig == NULL || usedSz == NULL ||
  560. in == NULL)
  561. return BAD_FUNC_ARG;
  562. ret = wc_ecc_sign_hash(in, inSz, sig, &inOut, (WC_RNG*)rng,
  563. (ecc_key*)ecc->holder);
  564. *usedSz = inOut;
  565. return ret;
  566. }
  567. /* ECC DSA Hash Verify */
  568. int CRYPT_ECC_DSA_HashVerify(CRYPT_ECC_CTX* ecc, const unsigned char* sig,
  569. unsigned int sigSz, unsigned char* hash,
  570. unsigned int hashSz, int* status)
  571. {
  572. if (ecc == NULL || sig == NULL || hash == NULL || status == NULL)
  573. return BAD_FUNC_ARG;
  574. return wc_ecc_verify_hash(sig, sigSz, hash, hashSz, status,
  575. (ecc_key*)ecc->holder);
  576. }
  577. /* ECC get key size helper */
  578. int CRYPT_ECC_KeySizeGet(CRYPT_ECC_CTX* ecc)
  579. {
  580. if (ecc == NULL)
  581. return BAD_FUNC_ARG;
  582. return wc_ecc_size((ecc_key*)ecc->holder);
  583. }
  584. /* ECC get signature size helper */
  585. int CRYPT_ECC_SignatureSizeGet(CRYPT_ECC_CTX* ecc)
  586. {
  587. if (ecc == NULL)
  588. return BAD_FUNC_ARG;
  589. return wc_ecc_sig_size((ecc_key*)ecc->holder);
  590. }
  591. #endif
  592. /* Save error string from err to str which needs to be >= 80 chars */
  593. int CRYPT_ERROR_StringGet(int err, char* str)
  594. {
  595. if (str == NULL)
  596. return BAD_FUNC_ARG;
  597. wc_ErrorString(err, str);
  598. return 0;
  599. }