2
0

crypto.c 19 KB

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