crypto.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. /* crypto.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. /* Implements Microchip CRYPTO API layer */
  22. #include "crypto.h"
  23. #include <cyassl/ctaocrypt/settings.h>
  24. #include <cyassl/ctaocrypt/md5.h>
  25. #include <cyassl/ctaocrypt/sha.h>
  26. #include <cyassl/ctaocrypt/sha256.h>
  27. #include <cyassl/ctaocrypt/sha512.h>
  28. #include <cyassl/ctaocrypt/hmac.h>
  29. #include <cyassl/ctaocrypt/compress.h>
  30. #include <cyassl/ctaocrypt/random.h>
  31. #include <cyassl/ctaocrypt/des3.h>
  32. #include <cyassl/ctaocrypt/aes.h>
  33. #include <cyassl/ctaocrypt/rsa.h>
  34. #include <cyassl/ctaocrypt/ecc.h>
  35. #include <cyassl/ctaocrypt/error.h>
  36. /* Initialize MD5 */
  37. int CRYPT_MD5_Initialize(CRYPT_MD5_CTX* md5)
  38. {
  39. typedef char md5_test[sizeof(CRYPT_MD5_CTX) >= sizeof(Md5) ? 1 : -1];
  40. (void)sizeof(md5_test);
  41. if (md5 == NULL)
  42. return BAD_FUNC_ARG;
  43. InitMd5((Md5*)md5);
  44. return 0;
  45. }
  46. /* Add data to MD5 */
  47. int CRYPT_MD5_DataAdd(CRYPT_MD5_CTX* md5, const unsigned char* input,
  48. unsigned int sz)
  49. {
  50. if (md5 == NULL || input == NULL)
  51. return BAD_FUNC_ARG;
  52. Md5Update((Md5*)md5, input, sz);
  53. return 0;
  54. }
  55. /* Get MD5 Final into digest */
  56. int CRYPT_MD5_Finalize(CRYPT_MD5_CTX* md5, unsigned char* digest)
  57. {
  58. if (md5 == NULL || digest == NULL)
  59. return BAD_FUNC_ARG;
  60. Md5Final((Md5*)md5, digest);
  61. return 0;
  62. }
  63. /* Initialize SHA */
  64. int CRYPT_SHA_Initialize(CRYPT_SHA_CTX* sha)
  65. {
  66. typedef char sha_test[sizeof(CRYPT_SHA_CTX) >= sizeof(Sha) ? 1 : -1];
  67. (void)sizeof(sha_test);
  68. if (sha == NULL)
  69. return BAD_FUNC_ARG;
  70. InitSha((Sha*)sha);
  71. return 0;
  72. }
  73. /* Add data to SHA */
  74. int CRYPT_SHA_DataAdd(CRYPT_SHA_CTX* sha, const unsigned char* input,
  75. unsigned int sz)
  76. {
  77. if (sha == NULL || input == NULL)
  78. return BAD_FUNC_ARG;
  79. ShaUpdate((Sha*)sha, input, sz);
  80. return 0;
  81. }
  82. /* Get SHA Final into digest */
  83. int CRYPT_SHA_Finalize(CRYPT_SHA_CTX* sha, unsigned char* digest)
  84. {
  85. if (sha == NULL || digest == NULL)
  86. return BAD_FUNC_ARG;
  87. ShaFinal((Sha*)sha, digest);
  88. return 0;
  89. }
  90. /* Initialize SHA-256 */
  91. int CRYPT_SHA256_Initialize(CRYPT_SHA256_CTX* sha256)
  92. {
  93. typedef char sha_test[sizeof(CRYPT_SHA256_CTX) >= sizeof(Sha256) ? 1 : -1];
  94. (void)sizeof(sha_test);
  95. if (sha256 == NULL)
  96. return BAD_FUNC_ARG;
  97. InitSha256((Sha256*)sha256);
  98. return 0;
  99. }
  100. /* Add data to SHA-256 */
  101. int CRYPT_SHA256_DataAdd(CRYPT_SHA256_CTX* sha256, const unsigned char* input,
  102. unsigned int sz)
  103. {
  104. if (sha256 == NULL || input == NULL)
  105. return BAD_FUNC_ARG;
  106. Sha256Update((Sha256*)sha256, input, sz);
  107. return 0;
  108. }
  109. /* Get SHA-256 Final into digest */
  110. int CRYPT_SHA256_Finalize(CRYPT_SHA256_CTX* sha256, unsigned char* digest)
  111. {
  112. if (sha256 == NULL || digest == NULL)
  113. return BAD_FUNC_ARG;
  114. Sha256Final((Sha256*)sha256, digest);
  115. return 0;
  116. }
  117. /* Initialize SHA-384 */
  118. int CRYPT_SHA384_Initialize(CRYPT_SHA384_CTX* sha384)
  119. {
  120. typedef char sha_test[sizeof(CRYPT_SHA384_CTX) >= sizeof(Sha384) ? 1 : -1];
  121. (void)sizeof(sha_test);
  122. if (sha384 == NULL)
  123. return BAD_FUNC_ARG;
  124. InitSha384((Sha384*)sha384);
  125. return 0;
  126. }
  127. /* Add data to SHA-384 */
  128. int CRYPT_SHA384_DataAdd(CRYPT_SHA384_CTX* sha384, const unsigned char* input,
  129. unsigned int sz)
  130. {
  131. if (sha384 == NULL || input == NULL)
  132. return BAD_FUNC_ARG;
  133. Sha384Update((Sha384*)sha384, input, sz);
  134. return 0;
  135. }
  136. /* Get SHA-384 Final into digest */
  137. int CRYPT_SHA384_Finalize(CRYPT_SHA384_CTX* sha384, unsigned char* digest)
  138. {
  139. if (sha384 == NULL || digest == NULL)
  140. return BAD_FUNC_ARG;
  141. Sha384Final((Sha384*)sha384, digest);
  142. return 0;
  143. }
  144. /* Initialize SHA-512 */
  145. int CRYPT_SHA512_Initialize(CRYPT_SHA512_CTX* sha512)
  146. {
  147. typedef char sha_test[sizeof(CRYPT_SHA512_CTX) >= sizeof(Sha512) ? 1 : -1];
  148. (void)sizeof(sha_test);
  149. if (sha512 == NULL)
  150. return BAD_FUNC_ARG;
  151. InitSha512((Sha512*)sha512);
  152. return 0;
  153. }
  154. /* Add data to SHA-512 */
  155. int CRYPT_SHA512_DataAdd(CRYPT_SHA512_CTX* sha512, const unsigned char* input,
  156. unsigned int sz)
  157. {
  158. if (sha512 == NULL || input == NULL)
  159. return BAD_FUNC_ARG;
  160. Sha512Update((Sha512*)sha512, input, sz);
  161. return 0;
  162. }
  163. /* Get SHA-512 Final into digest */
  164. int CRYPT_SHA512_Finalize(CRYPT_SHA512_CTX* sha512, unsigned char* digest)
  165. {
  166. if (sha512 == NULL || digest == NULL)
  167. return BAD_FUNC_ARG;
  168. Sha512Final((Sha512*)sha512, digest);
  169. return 0;
  170. }
  171. /* Set HMAC key with type */
  172. int CRYPT_HMAC_SetKey(CRYPT_HMAC_CTX* hmac, int type, const unsigned char* key,
  173. unsigned int sz)
  174. {
  175. typedef char hmac_test[sizeof(CRYPT_HMAC_CTX) >= sizeof(Hmac) ? 1 : -1];
  176. (void)sizeof(hmac_test);
  177. if (hmac == NULL || key == NULL)
  178. return BAD_FUNC_ARG;
  179. if (type != CRYPT_HMAC_SHA && type != CRYPT_HMAC_SHA256 &&
  180. type != CRYPT_HMAC_SHA384 && type != CRYPT_HMAC_SHA512) {
  181. return BAD_FUNC_ARG; /* bad hmac type */
  182. }
  183. HmacSetKey((Hmac*)hmac, type, key, sz);
  184. return 0;
  185. }
  186. int CRYPT_HMAC_DataAdd(CRYPT_HMAC_CTX* hmac, const unsigned char* input,
  187. unsigned int sz)
  188. {
  189. if (hmac == NULL || input == NULL)
  190. return BAD_FUNC_ARG;
  191. HmacUpdate((Hmac*)hmac, input, sz);
  192. return 0;
  193. }
  194. /* Get HMAC Final into digest */
  195. int CRYPT_HMAC_Finalize(CRYPT_HMAC_CTX* hmac, unsigned char* digest)
  196. {
  197. if (hmac == NULL || digest == NULL)
  198. return BAD_FUNC_ARG;
  199. HmacFinal((Hmac*)hmac, digest);
  200. return 0;
  201. }
  202. /* Huffman Compression, set flag to do static, otherwise dynamic */
  203. /* return compressed size, otherwise < 0 for error */
  204. int CRYPT_HUFFMAN_Compress(unsigned char* out, unsigned int outSz,
  205. const unsigned char* in, unsigned int inSz,
  206. unsigned int flags)
  207. {
  208. if (out == NULL || in == NULL)
  209. return BAD_FUNC_ARG;
  210. return Compress(out, outSz, in, inSz, flags);
  211. }
  212. /* Huffman DeCompression, self determines type */
  213. /* return decompressed size, otherwise < 0 for error */
  214. int CRYPT_HUFFMAN_DeCompress(unsigned char* out, unsigned int outSz,
  215. const unsigned char* in, unsigned int inSz)
  216. {
  217. if (out == NULL || in == NULL)
  218. return BAD_FUNC_ARG;
  219. return DeCompress(out, outSz, in, inSz);
  220. }
  221. /* RNG Initialize, < 0 on error */
  222. int CRYPT_RNG_Initialize(CRYPT_RNG_CTX* rng)
  223. {
  224. typedef char rng_test[sizeof(CRYPT_RNG_CTX) >= sizeof(RNG) ? 1 : -1];
  225. (void)sizeof(rng_test);
  226. if (rng == NULL)
  227. return BAD_FUNC_ARG;
  228. return InitRng((RNG*)rng);
  229. }
  230. /* RNG Get single bytes, < 0 on error */
  231. int CRYPT_RNG_Get(CRYPT_RNG_CTX* rng, unsigned char* b)
  232. {
  233. if (rng == NULL || b == NULL)
  234. return BAD_FUNC_ARG;
  235. *b = RNG_GenerateByte((RNG*)rng);
  236. return 0;
  237. }
  238. /* RNG Block Generation of sz bytes, < 0 on error */
  239. int CRYPT_RNG_BlockGenerate(CRYPT_RNG_CTX* rng, unsigned char* b,
  240. unsigned int sz)
  241. {
  242. if (rng == NULL || b == NULL)
  243. return BAD_FUNC_ARG;
  244. RNG_GenerateBlock((RNG*)rng, b, sz);
  245. return 0;
  246. }
  247. /* Triple DES Key Set, may have iv, will have direction */
  248. int CRYPT_TDES_KeySet(CRYPT_TDES_CTX* tdes, const unsigned char* key,
  249. const unsigned char* iv, int dir)
  250. {
  251. typedef char tdes_test[sizeof(CRYPT_TDES_CTX) >= sizeof(Des3) ? 1 : -1];
  252. (void)sizeof(tdes_test);
  253. if (tdes == NULL || key == NULL)
  254. return BAD_FUNC_ARG;
  255. Des3_SetKey((Des3*)tdes, key, iv, dir);
  256. return 0;
  257. }
  258. /* Triple DES Iv Set, sometimes added later */
  259. int CRYPT_TDES_IvSet(CRYPT_TDES_CTX* tdes, const unsigned char* iv)
  260. {
  261. if (tdes == NULL || iv == NULL)
  262. return BAD_FUNC_ARG;
  263. Des3_SetIV((Des3*)tdes, iv);
  264. return 0;
  265. }
  266. /* Triple DES CBC Encrypt */
  267. int CRYPT_TDES_CBC_Encrypt(CRYPT_TDES_CTX* tdes, unsigned char* out,
  268. const unsigned char* in, unsigned int inSz)
  269. {
  270. if (tdes == NULL || out == NULL || in == NULL)
  271. return BAD_FUNC_ARG;
  272. Des3_CbcEncrypt((Des3*)tdes, out, in, inSz);
  273. return 0;
  274. }
  275. /* Triple DES CBC Decrypt */
  276. int CRYPT_TDES_CBC_Decrypt(CRYPT_TDES_CTX* tdes, unsigned char* out,
  277. const unsigned char* in, unsigned int inSz)
  278. {
  279. if (tdes == NULL || out == NULL || in == NULL)
  280. return BAD_FUNC_ARG;
  281. Des3_CbcDecrypt((Des3*)tdes, out, in, inSz);
  282. return 0;
  283. }
  284. /* AES Key Set, may have iv, will have direction */
  285. int CRYPT_AES_KeySet(CRYPT_AES_CTX* aes, const unsigned char* key,
  286. unsigned int keyLen, const unsigned char* iv, int dir)
  287. {
  288. typedef char aes_test[sizeof(CRYPT_AES_CTX) >= sizeof(Aes) ? 1 : -1];
  289. (void)sizeof(aes_test);
  290. if (aes == NULL || key == NULL)
  291. return BAD_FUNC_ARG;
  292. return AesSetKey((Aes*)aes, key, keyLen, iv, dir);
  293. }
  294. /* AES Iv Set, sometimes added later */
  295. int CRYPT_AES_IvSet(CRYPT_AES_CTX* aes, const unsigned char* iv)
  296. {
  297. if (aes == NULL || iv == NULL)
  298. return BAD_FUNC_ARG;
  299. AesSetIV((Aes*)aes, iv);
  300. return 0;
  301. }
  302. /* AES CBC Encrypt */
  303. int CRYPT_AES_CBC_Encrypt(CRYPT_AES_CTX* aes, unsigned char* out,
  304. const unsigned char* in, unsigned int inSz)
  305. {
  306. if (aes == NULL || out == NULL || in == NULL)
  307. return BAD_FUNC_ARG;
  308. return AesCbcEncrypt((Aes*)aes, out, in, inSz);
  309. }
  310. /* AES CBC Decrypt */
  311. int CRYPT_AES_CBC_Decrypt(CRYPT_AES_CTX* aes, unsigned char* out,
  312. const unsigned char* in, unsigned int inSz)
  313. {
  314. if (aes == NULL || out == NULL || in == NULL)
  315. return BAD_FUNC_ARG;
  316. return AesCbcDecrypt((Aes*)aes, out, in, inSz);
  317. }
  318. /* AES CTR Encrypt (used for decrypt too, with ENCRYPT key setup) */
  319. int CRYPT_AES_CTR_Encrypt(CRYPT_AES_CTX* aes, unsigned char* out,
  320. const unsigned char* in, unsigned int inSz)
  321. {
  322. if (aes == NULL || out == NULL || in == NULL)
  323. return BAD_FUNC_ARG;
  324. AesCtrEncrypt((Aes*)aes, out, in, inSz);
  325. return 0;
  326. }
  327. /* AES Direct mode encrypt, one block at a time */
  328. int CRYPT_AES_DIRECT_Encrypt(CRYPT_AES_CTX* aes, unsigned char* out,
  329. const unsigned char* in)
  330. {
  331. if (aes == NULL || out == NULL || in == NULL)
  332. return BAD_FUNC_ARG;
  333. AesEncryptDirect((Aes*)aes, out, in);
  334. return 0;
  335. }
  336. /* AES Direct mode decrypt, one block at a time */
  337. int CRYPT_AES_DIRECT_Decrypt(CRYPT_AES_CTX* aes, unsigned char* out,
  338. const unsigned char* in)
  339. {
  340. if (aes == NULL || out == NULL || in == NULL)
  341. return BAD_FUNC_ARG;
  342. AesDecryptDirect((Aes*)aes, out, in);
  343. return 0;
  344. }
  345. /* RSA Initialize */
  346. int CRYPT_RSA_Initialize(CRYPT_RSA_CTX* rsa)
  347. {
  348. if (rsa == NULL)
  349. return BAD_FUNC_ARG;
  350. rsa->holder = (RsaKey*)XMALLOC(sizeof(RsaKey), NULL, DYNAMIC_TYPE_RSA);
  351. if (rsa->holder == NULL)
  352. return -1;
  353. InitRsaKey((RsaKey*)rsa->holder, NULL);
  354. return 0;
  355. }
  356. /* RSA Free resources */
  357. int CRYPT_RSA_Free(CRYPT_RSA_CTX* rsa)
  358. {
  359. if (rsa == NULL)
  360. return BAD_FUNC_ARG;
  361. FreeRsaKey((RsaKey*)rsa->holder);
  362. XFREE(rsa->holder, NULL, DYNAMIC_TYPE_RSA);
  363. rsa->holder = NULL;
  364. return 0;
  365. }
  366. /* RSA Public key decode ASN.1 */
  367. int CRYPT_RSA_PublicKeyDecode(CRYPT_RSA_CTX* rsa, const unsigned char* in,
  368. unsigned int inSz)
  369. {
  370. unsigned int idx = 0;
  371. (void)idx;
  372. if (rsa == NULL || in == NULL)
  373. return BAD_FUNC_ARG;
  374. return RsaPublicKeyDecode(in, &idx, (RsaKey*)rsa->holder, inSz);
  375. }
  376. /* RSA Private key decode ASN.1 */
  377. int CRYPT_RSA_PrivateKeyDecode(CRYPT_RSA_CTX* rsa, const unsigned char* in,
  378. unsigned int inSz)
  379. {
  380. unsigned int idx = 0;
  381. (void)idx;
  382. if (rsa == NULL || in == NULL)
  383. return BAD_FUNC_ARG;
  384. return RsaPrivateKeyDecode(in, &idx, (RsaKey*)rsa->holder, inSz);
  385. }
  386. /* RSA Public Encrypt */
  387. int CRYPT_RSA_PublicEncrypt(CRYPT_RSA_CTX* rsa, unsigned char* out,
  388. unsigned int outSz, const unsigned char* in,
  389. unsigned int inSz, CRYPT_RNG_CTX* rng)
  390. {
  391. if (rsa == NULL || in == NULL || out == NULL || rng == NULL)
  392. return BAD_FUNC_ARG;
  393. return RsaPublicEncrypt(in, inSz, out, outSz, (RsaKey*)rsa->holder,
  394. (RNG*)rng);
  395. }
  396. /* RSA Private Decrypt */
  397. int CRYPT_RSA_PrivateDecrypt(CRYPT_RSA_CTX* rsa, unsigned char* out,
  398. unsigned int outSz, const unsigned char* in,
  399. unsigned int inSz)
  400. {
  401. if (rsa == NULL || in == NULL || out == NULL)
  402. return BAD_FUNC_ARG;
  403. return RsaPrivateDecrypt(in, inSz, out, outSz, (RsaKey*)rsa->holder);
  404. }
  405. /* RSA Get Encrypt size helper */
  406. int CRYPT_RSA_EncryptSizeGet(CRYPT_RSA_CTX* rsa)
  407. {
  408. if (rsa == NULL)
  409. return BAD_FUNC_ARG;
  410. return RsaEncryptSize((RsaKey*)rsa->holder);
  411. }
  412. /* ECC init */
  413. int CRYPT_ECC_Initialize(CRYPT_ECC_CTX* ecc)
  414. {
  415. if (ecc == NULL)
  416. return BAD_FUNC_ARG;
  417. ecc->holder = (ecc_key*)XMALLOC(sizeof(ecc_key), NULL, DYNAMIC_TYPE_ECC);
  418. if (ecc->holder == NULL)
  419. return -1;
  420. ecc_init((ecc_key*)ecc->holder);
  421. return 0;
  422. }
  423. /* ECC free resources */
  424. int CRYPT_ECC_Free(CRYPT_ECC_CTX* ecc)
  425. {
  426. if (ecc == NULL)
  427. return BAD_FUNC_ARG;
  428. ecc_free((ecc_key*)ecc->holder);
  429. XFREE(ecc->holder, NULL, DYNAMIC_TYPE_ECC);
  430. ecc->holder = NULL;
  431. return 0;
  432. }
  433. /* ECC Public x963 Export */
  434. int CRYPT_ECC_PublicExport(CRYPT_ECC_CTX* ecc, unsigned char* out,
  435. unsigned int outSz, unsigned int* usedSz)
  436. {
  437. int ret;
  438. unsigned int inOut = outSz;
  439. if (ecc == NULL || out == NULL)
  440. return BAD_FUNC_ARG;
  441. ret = ecc_export_x963((ecc_key*)ecc->holder, out, &inOut);
  442. *usedSz = inOut;
  443. return ret;
  444. }
  445. /* ECC Public x963 Import */
  446. int CRYPT_ECC_PublicImport(CRYPT_ECC_CTX* ecc, const unsigned char* in,
  447. unsigned int inSz)
  448. {
  449. if (ecc == NULL || in == NULL)
  450. return BAD_FUNC_ARG;
  451. return ecc_import_x963(in, inSz, (ecc_key*)ecc->holder);
  452. }
  453. /* ECC Private x963 Import */
  454. int CRYPT_ECC_PrivateImport(CRYPT_ECC_CTX* ecc, const unsigned char* priv,
  455. unsigned int privSz, const unsigned char* pub, unsigned int pubSz)
  456. {
  457. if (ecc == NULL || priv == NULL || pub == NULL)
  458. return BAD_FUNC_ARG;
  459. return ecc_import_private_key(priv, privSz, pub, pubSz,
  460. (ecc_key*)ecc->holder);
  461. }
  462. /* ECC DHE Make key */
  463. int CRYPT_ECC_DHE_KeyMake(CRYPT_ECC_CTX* ecc, CRYPT_RNG_CTX* rng, int keySz)
  464. {
  465. if (ecc == NULL || rng == NULL)
  466. return BAD_FUNC_ARG;
  467. return ecc_make_key((RNG*)rng, keySz, (ecc_key*)ecc->holder);
  468. }
  469. /* ECC DHE Make shared secret with our private and peer public */
  470. int CRYPT_ECC_DHE_SharedSecretMake(CRYPT_ECC_CTX* priv, CRYPT_ECC_CTX* pub,
  471. unsigned char* out, unsigned int outSz, unsigned int* usedSz)
  472. {
  473. int ret;
  474. unsigned int inOut = outSz;
  475. if (priv == NULL || pub == NULL || out == NULL || usedSz == NULL)
  476. return BAD_FUNC_ARG;
  477. ret = ecc_shared_secret((ecc_key*)priv->holder, (ecc_key*)pub->holder,
  478. out, &inOut);
  479. *usedSz = inOut;
  480. return ret;
  481. }
  482. /* ECC DSA Hash Sign */
  483. int CRYPT_ECC_DSA_HashSign(CRYPT_ECC_CTX* ecc, CRYPT_RNG_CTX* rng,
  484. unsigned char* sig, unsigned int sigSz,
  485. unsigned int* usedSz, const unsigned char* in,
  486. unsigned int inSz)
  487. {
  488. int ret;
  489. unsigned int inOut = sigSz;
  490. if (ecc == NULL || rng == NULL || sig == NULL || usedSz == NULL ||
  491. in == NULL)
  492. return BAD_FUNC_ARG;
  493. ret = ecc_sign_hash(in, inSz, sig, &inOut, (RNG*)rng,
  494. (ecc_key*)ecc->holder);
  495. *usedSz = inOut;
  496. return ret;
  497. }
  498. /* ECC DSA Hash Verify */
  499. int CRYPT_ECC_DSA_HashVerify(CRYPT_ECC_CTX* ecc, const unsigned char* sig,
  500. unsigned int sigSz, unsigned char* hash,
  501. unsigned int hashSz, int* status)
  502. {
  503. if (ecc == NULL || sig == NULL || hash == NULL || status == NULL)
  504. return BAD_FUNC_ARG;
  505. return ecc_verify_hash(sig, sigSz, hash, hashSz, status,
  506. (ecc_key*)ecc->holder);
  507. }
  508. /* ECC get key size helper */
  509. int CRYPT_ECC_KeySizeGet(CRYPT_ECC_CTX* ecc)
  510. {
  511. if (ecc == NULL)
  512. return BAD_FUNC_ARG;
  513. return ecc_size((ecc_key*)ecc->holder);
  514. }
  515. /* ECC get signature size helper */
  516. int CRYPT_ECC_SignatureSizeGet(CRYPT_ECC_CTX* ecc)
  517. {
  518. if (ecc == NULL)
  519. return BAD_FUNC_ARG;
  520. return ecc_sig_size((ecc_key*)ecc->holder);
  521. }
  522. /* Save error string from err to str which needs to be >= 80 chars */
  523. int CRYPT_ERROR_StringGet(int err, char* str)
  524. {
  525. if (str == NULL)
  526. return BAD_FUNC_ARG;
  527. CTaoCryptErrorString(err, str);
  528. return 0;
  529. }