crypto.c 17 KB

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