PageRenderTime 55ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/ctaocrypt/src/rsa.c

https://github.com/andersmalm/cyassl
C | 807 lines | 567 code | 190 blank | 50 comment | 225 complexity | 5b2d35d6bbb1ebb0b5fd1a9e0e849f90 MD5 | raw file
Possible License(s): GPL-2.0
  1. /* rsa.c
  2. *
  3. * Copyright (C) 2006-2012 Sawtooth Consulting Ltd.
  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. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #ifndef NO_RSA
  25. #include <cyassl/ctaocrypt/rsa.h>
  26. #include <cyassl/ctaocrypt/random.h>
  27. #include <cyassl/ctaocrypt/error.h>
  28. #include <cyassl/ctaocrypt/logging.h>
  29. #ifdef SHOW_GEN
  30. #ifdef FREESCALE_MQX
  31. #include <fio.h>
  32. #else
  33. #include <stdio.h>
  34. #endif
  35. #endif
  36. #ifdef HAVE_CAVIUM
  37. static void InitCaviumRsaKey(RsaKey* key, void* heap);
  38. static void FreeCaviumRsaKey(RsaKey* key);
  39. static int CaviumRsaPublicEncrypt(const byte* in, word32 inLen, byte* out,
  40. word32 outLen, RsaKey* key);
  41. static int CaviumRsaPrivateDecrypt(const byte* in, word32 inLen, byte* out,
  42. word32 outLen, RsaKey* key);
  43. static int CaviumRsaSSL_Sign(const byte* in, word32 inLen, byte* out,
  44. word32 outLen, RsaKey* key);
  45. static int CaviumRsaSSL_Verify(const byte* in, word32 inLen, byte* out,
  46. word32 outLen, RsaKey* key);
  47. #endif
  48. enum {
  49. RSA_PUBLIC_ENCRYPT = 0,
  50. RSA_PUBLIC_DECRYPT = 1,
  51. RSA_PRIVATE_ENCRYPT = 2,
  52. RSA_PRIVATE_DECRYPT = 3,
  53. RSA_BLOCK_TYPE_1 = 1,
  54. RSA_BLOCK_TYPE_2 = 2,
  55. RSA_MIN_SIZE = 512,
  56. RSA_MAX_SIZE = 4096,
  57. RSA_MIN_PAD_SZ = 11 /* seperator + 0 + pad value + 8 pads */
  58. };
  59. void InitRsaKey(RsaKey* key, void* heap)
  60. {
  61. #ifdef HAVE_CAVIUM
  62. if (key->magic == CYASSL_RSA_CAVIUM_MAGIC)
  63. return InitCaviumRsaKey(key, heap);
  64. #endif
  65. key->type = -1; /* haven't decided yet */
  66. key->heap = heap;
  67. /* TomsFastMath doesn't use memory allocation */
  68. #ifndef USE_FAST_MATH
  69. key->n.dp = key->e.dp = 0; /* public alloc parts */
  70. key->d.dp = key->p.dp = 0; /* private alloc parts */
  71. key->q.dp = key->dP.dp = 0;
  72. key->u.dp = key->dQ.dp = 0;
  73. #endif
  74. }
  75. void FreeRsaKey(RsaKey* key)
  76. {
  77. (void)key;
  78. #ifdef HAVE_CAVIUM
  79. if (key->magic == CYASSL_RSA_CAVIUM_MAGIC)
  80. return FreeCaviumRsaKey(key);
  81. #endif
  82. /* TomsFastMath doesn't use memory allocation */
  83. #ifndef USE_FAST_MATH
  84. if (key->type == RSA_PRIVATE) {
  85. mp_clear(&key->u);
  86. mp_clear(&key->dQ);
  87. mp_clear(&key->dP);
  88. mp_clear(&key->q);
  89. mp_clear(&key->p);
  90. mp_clear(&key->d);
  91. }
  92. mp_clear(&key->e);
  93. mp_clear(&key->n);
  94. #endif
  95. }
  96. static void RsaPad(const byte* input, word32 inputLen, byte* pkcsBlock,
  97. word32 pkcsBlockLen, byte padValue, RNG* rng)
  98. {
  99. if (inputLen == 0) return;
  100. pkcsBlock[0] = 0x0; /* set first byte to zero and advance */
  101. pkcsBlock++; pkcsBlockLen--;
  102. pkcsBlock[0] = padValue; /* insert padValue */
  103. if (padValue == RSA_BLOCK_TYPE_1)
  104. /* pad with 0xff bytes */
  105. XMEMSET(&pkcsBlock[1], 0xFF, pkcsBlockLen - inputLen - 2);
  106. else {
  107. /* pad with non-zero random bytes */
  108. word32 padLen = pkcsBlockLen - inputLen - 1, i;
  109. RNG_GenerateBlock(rng, &pkcsBlock[1], padLen);
  110. /* remove zeros */
  111. for (i = 1; i < padLen; i++)
  112. if (pkcsBlock[i] == 0) pkcsBlock[i] = 0x01;
  113. }
  114. pkcsBlock[pkcsBlockLen-inputLen-1] = 0; /* separator */
  115. XMEMCPY(pkcsBlock+pkcsBlockLen-inputLen, input, inputLen);
  116. }
  117. static word32 RsaUnPad(const byte *pkcsBlock, unsigned int pkcsBlockLen,
  118. byte **output, byte padValue)
  119. {
  120. word32 maxOutputLen = (pkcsBlockLen > 10) ? (pkcsBlockLen - 10) : 0,
  121. invalid = 0,
  122. i = 1,
  123. outputLen;
  124. if (pkcsBlock[0] != 0x0) /* skip past zero */
  125. invalid = 1;
  126. pkcsBlock++; pkcsBlockLen--;
  127. /* Require block type padValue */
  128. invalid = (pkcsBlock[0] != padValue) || invalid;
  129. /* skip past the padding until we find the separator */
  130. while (i<pkcsBlockLen && pkcsBlock[i++]) { /* null body */
  131. }
  132. if(!(i==pkcsBlockLen || pkcsBlock[i-1]==0)) {
  133. CYASSL_MSG("RsaUnPad error, bad formatting");
  134. return 0;
  135. }
  136. outputLen = pkcsBlockLen - i;
  137. invalid = (outputLen > maxOutputLen) || invalid;
  138. if (invalid) {
  139. CYASSL_MSG("RsaUnPad error, bad formatting");
  140. return 0;
  141. }
  142. *output = (byte *)(pkcsBlock + i);
  143. return outputLen;
  144. }
  145. static int RsaFunction(const byte* in, word32 inLen, byte* out, word32* outLen,
  146. int type, RsaKey* key)
  147. {
  148. #define ERROR_OUT(x) { ret = x; goto done;}
  149. mp_int tmp;
  150. int ret = 0;
  151. word32 keyLen, len;
  152. if (mp_init(&tmp) != MP_OKAY)
  153. return MP_INIT_E;
  154. if (mp_read_unsigned_bin(&tmp, (byte*)in, inLen) != MP_OKAY)
  155. ERROR_OUT(MP_READ_E);
  156. if (type == RSA_PRIVATE_DECRYPT || type == RSA_PRIVATE_ENCRYPT) {
  157. #ifdef RSA_LOW_MEM /* half as much memory but twice as slow */
  158. if (mp_exptmod(&tmp, &key->d, &key->n, &tmp) != MP_OKAY)
  159. ERROR_OUT(MP_EXPTMOD_E);
  160. #else
  161. #define INNER_ERROR_OUT(x) { ret = x; goto inner_done; }
  162. mp_int tmpa, tmpb;
  163. if (mp_init(&tmpa) != MP_OKAY)
  164. ERROR_OUT(MP_INIT_E);
  165. if (mp_init(&tmpb) != MP_OKAY) {
  166. mp_clear(&tmpa);
  167. ERROR_OUT(MP_INIT_E);
  168. }
  169. /* tmpa = tmp^dP mod p */
  170. if (mp_exptmod(&tmp, &key->dP, &key->p, &tmpa) != MP_OKAY)
  171. INNER_ERROR_OUT(MP_EXPTMOD_E);
  172. /* tmpb = tmp^dQ mod q */
  173. if (mp_exptmod(&tmp, &key->dQ, &key->q, &tmpb) != MP_OKAY)
  174. INNER_ERROR_OUT(MP_EXPTMOD_E);
  175. /* tmp = (tmpa - tmpb) * qInv (mod p) */
  176. if (mp_sub(&tmpa, &tmpb, &tmp) != MP_OKAY)
  177. INNER_ERROR_OUT(MP_SUB_E);
  178. if (mp_mulmod(&tmp, &key->u, &key->p, &tmp) != MP_OKAY)
  179. INNER_ERROR_OUT(MP_MULMOD_E);
  180. /* tmp = tmpb + q * tmp */
  181. if (mp_mul(&tmp, &key->q, &tmp) != MP_OKAY)
  182. INNER_ERROR_OUT(MP_MUL_E);
  183. if (mp_add(&tmp, &tmpb, &tmp) != MP_OKAY)
  184. INNER_ERROR_OUT(MP_ADD_E);
  185. inner_done:
  186. mp_clear(&tmpa);
  187. mp_clear(&tmpb);
  188. if (ret != 0) return ret;
  189. #endif /* RSA_LOW_MEM */
  190. }
  191. else if (type == RSA_PUBLIC_ENCRYPT || type == RSA_PUBLIC_DECRYPT) {
  192. if (mp_exptmod(&tmp, &key->e, &key->n, &tmp) != MP_OKAY)
  193. ERROR_OUT(MP_EXPTMOD_E);
  194. }
  195. else
  196. ERROR_OUT(RSA_WRONG_TYPE_E);
  197. keyLen = mp_unsigned_bin_size(&key->n);
  198. if (keyLen > *outLen)
  199. ERROR_OUT(RSA_BUFFER_E);
  200. len = mp_unsigned_bin_size(&tmp);
  201. /* pad front w/ zeros to match key length */
  202. while (len < keyLen) {
  203. *out++ = 0x00;
  204. len++;
  205. }
  206. *outLen = keyLen;
  207. /* convert */
  208. if (mp_to_unsigned_bin(&tmp, out) != MP_OKAY)
  209. ERROR_OUT(MP_TO_E);
  210. done:
  211. mp_clear(&tmp);
  212. return ret;
  213. }
  214. int RsaPublicEncrypt(const byte* in, word32 inLen, byte* out, word32 outLen,
  215. RsaKey* key, RNG* rng)
  216. {
  217. int sz, ret;
  218. #ifdef HAVE_CAVIUM
  219. if (key->magic == CYASSL_RSA_CAVIUM_MAGIC)
  220. return CaviumRsaPublicEncrypt(in, inLen, out, outLen, key);
  221. #endif
  222. sz = mp_unsigned_bin_size(&key->n);
  223. if (sz > (int)outLen)
  224. return RSA_BUFFER_E;
  225. if (inLen > (word32)(sz - RSA_MIN_PAD_SZ))
  226. return RSA_BUFFER_E;
  227. RsaPad(in, inLen, out, sz, RSA_BLOCK_TYPE_2, rng);
  228. if ((ret = RsaFunction(out, sz, out, &outLen, RSA_PUBLIC_ENCRYPT, key)) < 0)
  229. sz = ret;
  230. return sz;
  231. }
  232. int RsaPrivateDecryptInline(byte* in, word32 inLen, byte** out, RsaKey* key)
  233. {
  234. int plainLen, ret;
  235. #ifdef HAVE_CAVIUM
  236. if (key->magic == CYASSL_RSA_CAVIUM_MAGIC) {
  237. ret = CaviumRsaPrivateDecrypt(in, inLen, in, inLen, key);
  238. if (ret > 0)
  239. *out = in;
  240. return ret;
  241. }
  242. #endif
  243. if ((ret = RsaFunction(in, inLen, in, &inLen, RSA_PRIVATE_DECRYPT, key))
  244. < 0) {
  245. return ret;
  246. }
  247. plainLen = RsaUnPad(in, inLen, out, RSA_BLOCK_TYPE_2);
  248. return plainLen;
  249. }
  250. int RsaPrivateDecrypt(const byte* in, word32 inLen, byte* out, word32 outLen,
  251. RsaKey* key)
  252. {
  253. int plainLen, ret;
  254. byte* tmp;
  255. byte* pad = 0;
  256. #ifdef HAVE_CAVIUM
  257. if (key->magic == CYASSL_RSA_CAVIUM_MAGIC)
  258. return CaviumRsaPrivateDecrypt(in, inLen, out, outLen, key);
  259. #endif
  260. tmp = (byte*)XMALLOC(inLen, key->heap, DYNAMIC_TYPE_RSA);
  261. if (tmp == NULL) {
  262. return MEMORY_E;
  263. }
  264. XMEMCPY(tmp, in, inLen);
  265. if ((ret = plainLen = RsaPrivateDecryptInline(tmp, inLen, &pad, key))
  266. < 0) {
  267. XFREE(tmp, key->heap, DYNAMIC_TYPE_RSA);
  268. return ret;
  269. }
  270. if (plainLen > (int)outLen)
  271. plainLen = BAD_FUNC_ARG;
  272. else
  273. XMEMCPY(out, pad, plainLen);
  274. XMEMSET(tmp, 0x00, inLen);
  275. XFREE(tmp, key->heap, DYNAMIC_TYPE_RSA);
  276. return plainLen;
  277. }
  278. /* for Rsa Verify */
  279. int RsaSSL_VerifyInline(byte* in, word32 inLen, byte** out, RsaKey* key)
  280. {
  281. int plainLen, ret;
  282. #ifdef HAVE_CAVIUM
  283. if (key->magic == CYASSL_RSA_CAVIUM_MAGIC) {
  284. ret = CaviumRsaSSL_Verify(in, inLen, in, inLen, key);
  285. if (ret > 0)
  286. *out = in;
  287. return ret;
  288. }
  289. #endif
  290. if ((ret = RsaFunction(in, inLen, in, &inLen, RSA_PUBLIC_DECRYPT, key))
  291. < 0) {
  292. return ret;
  293. }
  294. plainLen = RsaUnPad(in, inLen, out, RSA_BLOCK_TYPE_1);
  295. return plainLen;
  296. }
  297. int RsaSSL_Verify(const byte* in, word32 inLen, byte* out, word32 outLen,
  298. RsaKey* key)
  299. {
  300. int plainLen, ret;
  301. byte* tmp;
  302. byte* pad = 0;
  303. #ifdef HAVE_CAVIUM
  304. if (key->magic == CYASSL_RSA_CAVIUM_MAGIC)
  305. return CaviumRsaSSL_Verify(in, inLen, out, outLen, key);
  306. #endif
  307. tmp = (byte*)XMALLOC(inLen, key->heap, DYNAMIC_TYPE_RSA);
  308. if (tmp == NULL) {
  309. return MEMORY_E;
  310. }
  311. XMEMCPY(tmp, in, inLen);
  312. if ((ret = plainLen = RsaSSL_VerifyInline(tmp, inLen, &pad, key))
  313. < 0) {
  314. XFREE(tmp, key->heap, DYNAMIC_TYPE_RSA);
  315. return ret;
  316. }
  317. if (plainLen > (int)outLen)
  318. plainLen = BAD_FUNC_ARG;
  319. else
  320. XMEMCPY(out, pad, plainLen);
  321. XMEMSET(tmp, 0x00, inLen);
  322. XFREE(tmp, key->heap, DYNAMIC_TYPE_RSA);
  323. return plainLen;
  324. }
  325. /* for Rsa Sign */
  326. int RsaSSL_Sign(const byte* in, word32 inLen, byte* out, word32 outLen,
  327. RsaKey* key, RNG* rng)
  328. {
  329. int sz, ret;
  330. #ifdef HAVE_CAVIUM
  331. if (key->magic == CYASSL_RSA_CAVIUM_MAGIC)
  332. return CaviumRsaSSL_Sign(in, inLen, out, outLen, key);
  333. #endif
  334. sz = mp_unsigned_bin_size(&key->n);
  335. if (sz > (int)outLen)
  336. return RSA_BUFFER_E;
  337. if (inLen > (word32)(sz - RSA_MIN_PAD_SZ))
  338. return RSA_BUFFER_E;
  339. RsaPad(in, inLen, out, sz, RSA_BLOCK_TYPE_1, rng);
  340. if ((ret = RsaFunction(out, sz, out, &outLen, RSA_PRIVATE_ENCRYPT,key)) < 0)
  341. sz = ret;
  342. return sz;
  343. }
  344. int RsaEncryptSize(RsaKey* key)
  345. {
  346. #ifdef HAVE_CAVIUM
  347. if (key->magic == CYASSL_RSA_CAVIUM_MAGIC)
  348. return key->c_nSz;
  349. #endif
  350. return mp_unsigned_bin_size(&key->n);
  351. }
  352. #ifdef CYASSL_KEY_GEN
  353. static const int USE_BBS = 1;
  354. static int rand_prime(mp_int* N, int len, RNG* rng, void* heap)
  355. {
  356. int err, res, type;
  357. byte* buf;
  358. (void)heap;
  359. if (N == NULL || rng == NULL)
  360. return BAD_FUNC_ARG;
  361. /* get type */
  362. if (len < 0) {
  363. type = USE_BBS;
  364. len = -len;
  365. } else {
  366. type = 0;
  367. }
  368. /* allow sizes between 2 and 512 bytes for a prime size */
  369. if (len < 2 || len > 512) {
  370. return BAD_FUNC_ARG;
  371. }
  372. /* allocate buffer to work with */
  373. buf = (byte*)XMALLOC(len, heap, DYNAMIC_TYPE_RSA);
  374. if (buf == NULL) {
  375. return MEMORY_E;
  376. }
  377. XMEMSET(buf, 0, len);
  378. do {
  379. #ifdef SHOW_GEN
  380. printf(".");
  381. fflush(stdout);
  382. #endif
  383. /* generate value */
  384. RNG_GenerateBlock(rng, buf, len);
  385. /* munge bits */
  386. buf[0] |= 0x80 | 0x40;
  387. buf[len-1] |= 0x01 | ((type & USE_BBS) ? 0x02 : 0x00);
  388. /* load value */
  389. if ((err = mp_read_unsigned_bin(N, buf, len)) != MP_OKAY) {
  390. XFREE(buf, heap, DYNAMIC_TYPE_RSA);
  391. return err;
  392. }
  393. /* test */
  394. if ((err = mp_prime_is_prime(N, 8, &res)) != MP_OKAY) {
  395. XFREE(buf, heap, DYNAMIC_TYPE_RSA);
  396. return err;
  397. }
  398. } while (res == MP_NO);
  399. #ifdef LTC_CLEAN_STACK
  400. XMEMSET(buf, 0, len);
  401. #endif
  402. XFREE(buf, heap, DYNAMIC_TYPE_RSA);
  403. return 0;
  404. }
  405. /* Make an RSA key for size bits, with e specified, 65537 is a good e */
  406. int MakeRsaKey(RsaKey* key, int size, long e, RNG* rng)
  407. {
  408. mp_int p, q, tmp1, tmp2, tmp3;
  409. int err;
  410. if (key == NULL || rng == NULL)
  411. return BAD_FUNC_ARG;
  412. if (size < RSA_MIN_SIZE || size > RSA_MAX_SIZE)
  413. return BAD_FUNC_ARG;
  414. if (e < 3 || (e & 1) == 0)
  415. return BAD_FUNC_ARG;
  416. if ((err = mp_init_multi(&p, &q, &tmp1, &tmp2, &tmp3, NULL)) != MP_OKAY)
  417. return err;
  418. err = mp_set_int(&tmp3, e);
  419. /* make p */
  420. if (err == MP_OKAY) {
  421. do {
  422. err = rand_prime(&p, size/16, rng, key->heap); /* size in bytes/2 */
  423. if (err == MP_OKAY)
  424. err = mp_sub_d(&p, 1, &tmp1); /* tmp1 = p-1 */
  425. if (err == MP_OKAY)
  426. err = mp_gcd(&tmp1, &tmp3, &tmp2); /* tmp2 = gcd(p-1, e) */
  427. } while (err == MP_OKAY && mp_cmp_d(&tmp2, 1) != 0); /* e divdes p-1 */
  428. }
  429. /* make q */
  430. if (err == MP_OKAY) {
  431. do {
  432. err = rand_prime(&q, size/16, rng, key->heap); /* size in bytes/2 */
  433. if (err == MP_OKAY)
  434. err = mp_sub_d(&q, 1, &tmp1); /* tmp1 = q-1 */
  435. if (err == MP_OKAY)
  436. err = mp_gcd(&tmp1, &tmp3, &tmp2); /* tmp2 = gcd(q-1, e) */
  437. } while (err == MP_OKAY && mp_cmp_d(&tmp2, 1) != 0); /* e divdes q-1 */
  438. }
  439. if (err == MP_OKAY)
  440. err = mp_init_multi(&key->n, &key->e, &key->d, &key->p, &key->q, NULL);
  441. if (err == MP_OKAY)
  442. err = mp_init_multi(&key->dP, &key->dQ, &key->u, NULL, NULL, NULL);
  443. if (err == MP_OKAY)
  444. err = mp_sub_d(&p, 1, &tmp2); /* tmp2 = p-1 */
  445. if (err == MP_OKAY)
  446. err = mp_lcm(&tmp1, &tmp2, &tmp1); /* tmp1 = lcm(p-1, q-1),last loop */
  447. /* make key */
  448. if (err == MP_OKAY)
  449. err = mp_set_int(&key->e, e); /* key->e = e */
  450. if (err == MP_OKAY) /* key->d = 1/e mod lcm(p-1, q-1) */
  451. err = mp_invmod(&key->e, &tmp1, &key->d);
  452. if (err == MP_OKAY)
  453. err = mp_mul(&p, &q, &key->n); /* key->n = pq */
  454. if (err == MP_OKAY)
  455. err = mp_sub_d(&p, 1, &tmp1);
  456. if (err == MP_OKAY)
  457. err = mp_sub_d(&q, 1, &tmp2);
  458. if (err == MP_OKAY)
  459. err = mp_mod(&key->d, &tmp1, &key->dP);
  460. if (err == MP_OKAY)
  461. err = mp_mod(&key->d, &tmp2, &key->dQ);
  462. if (err == MP_OKAY)
  463. err = mp_invmod(&q, &p, &key->u);
  464. if (err == MP_OKAY)
  465. err = mp_copy(&p, &key->p);
  466. if (err == MP_OKAY)
  467. err = mp_copy(&q, &key->q);
  468. if (err == MP_OKAY)
  469. key->type = RSA_PRIVATE;
  470. mp_clear(&tmp3);
  471. mp_clear(&tmp2);
  472. mp_clear(&tmp1);
  473. mp_clear(&q);
  474. mp_clear(&p);
  475. if (err != MP_OKAY) {
  476. FreeRsaKey(key);
  477. return err;
  478. }
  479. return 0;
  480. }
  481. #endif /* CYASSL_KEY_GEN */
  482. #ifdef HAVE_CAVIUM
  483. #include <cyassl/ctaocrypt/logging.h>
  484. #include "cavium_common.h"
  485. /* Initiliaze RSA for use with Nitrox device */
  486. int RsaInitCavium(RsaKey* rsa, int devId)
  487. {
  488. if (rsa == NULL)
  489. return -1;
  490. if (CspAllocContext(CONTEXT_SSL, &rsa->contextHandle, devId) != 0)
  491. return -1;
  492. rsa->devId = devId;
  493. rsa->magic = CYASSL_RSA_CAVIUM_MAGIC;
  494. return 0;
  495. }
  496. /* Free RSA from use with Nitrox device */
  497. void RsaFreeCavium(RsaKey* rsa)
  498. {
  499. if (rsa == NULL)
  500. return;
  501. CspFreeContext(CONTEXT_SSL, rsa->contextHandle, rsa->devId);
  502. rsa->magic = 0;
  503. }
  504. /* Initialize cavium RSA key */
  505. static void InitCaviumRsaKey(RsaKey* key, void* heap)
  506. {
  507. if (key == NULL)
  508. return;
  509. key->heap = heap;
  510. key->type = -1; /* don't know yet */
  511. key->c_n = NULL;
  512. key->c_e = NULL;
  513. key->c_d = NULL;
  514. key->c_p = NULL;
  515. key->c_q = NULL;
  516. key->c_dP = NULL;
  517. key->c_dQ = NULL;
  518. key->c_u = NULL;
  519. key->c_nSz = 0;
  520. key->c_eSz = 0;
  521. key->c_dSz = 0;
  522. key->c_pSz = 0;
  523. key->c_qSz = 0;
  524. key->c_dP_Sz = 0;
  525. key->c_dQ_Sz = 0;
  526. key->c_uSz = 0;
  527. }
  528. /* Free cavium RSA key */
  529. static void FreeCaviumRsaKey(RsaKey* key)
  530. {
  531. if (key == NULL)
  532. return;
  533. XFREE(key->c_n, key->heap, DYNAMIC_TYPE_CAVIUM_TMP);
  534. XFREE(key->c_e, key->heap, DYNAMIC_TYPE_CAVIUM_TMP);
  535. XFREE(key->c_d, key->heap, DYNAMIC_TYPE_CAVIUM_TMP);
  536. XFREE(key->c_p, key->heap, DYNAMIC_TYPE_CAVIUM_TMP);
  537. XFREE(key->c_q, key->heap, DYNAMIC_TYPE_CAVIUM_TMP);
  538. XFREE(key->c_dP, key->heap, DYNAMIC_TYPE_CAVIUM_TMP);
  539. XFREE(key->c_dQ, key->heap, DYNAMIC_TYPE_CAVIUM_TMP);
  540. XFREE(key->c_u, key->heap, DYNAMIC_TYPE_CAVIUM_TMP);
  541. InitCaviumRsaKey(key, key->heap); /* reset pointers */
  542. }
  543. static int CaviumRsaPublicEncrypt(const byte* in, word32 inLen, byte* out,
  544. word32 outLen, RsaKey* key)
  545. {
  546. word32 requestId;
  547. word32 ret;
  548. if (key == NULL || in == NULL || out == NULL || outLen < (word32)key->c_nSz)
  549. return -1;
  550. ret = CspPkcs1v15Enc(CAVIUM_BLOCKING, BT2, key->c_nSz, key->c_eSz,
  551. (word16)inLen, key->c_n, key->c_e, (byte*)in, out,
  552. &requestId, key->devId);
  553. if (ret != 0) {
  554. CYASSL_MSG("Cavium Enc BT2 failed");
  555. return -1;
  556. }
  557. return key->c_nSz;
  558. }
  559. static INLINE void ato16(const byte* c, word16* u16)
  560. {
  561. *u16 = (c[0] << 8) | (c[1]);
  562. }
  563. static int CaviumRsaPrivateDecrypt(const byte* in, word32 inLen, byte* out,
  564. word32 outLen, RsaKey* key)
  565. {
  566. word32 requestId;
  567. word32 ret;
  568. word16 outSz = (word16)outLen;
  569. if (key == NULL || in == NULL || out == NULL || inLen != (word32)key->c_nSz)
  570. return -1;
  571. ret = CspPkcs1v15CrtDec(CAVIUM_BLOCKING, BT2, key->c_nSz, key->c_q,
  572. key->c_dQ, key->c_p, key->c_dP, key->c_u,
  573. (byte*)in, &outSz, out, &requestId, key->devId);
  574. if (ret != 0) {
  575. CYASSL_MSG("Cavium CRT Dec BT2 failed");
  576. return -1;
  577. }
  578. ato16((const byte*)&outSz, &outSz);
  579. return outSz;
  580. }
  581. static int CaviumRsaSSL_Sign(const byte* in, word32 inLen, byte* out,
  582. word32 outLen, RsaKey* key)
  583. {
  584. word32 requestId;
  585. word32 ret;
  586. if (key == NULL || in == NULL || out == NULL || inLen == 0 || outLen <
  587. (word32)key->c_nSz)
  588. return -1;
  589. ret = CspPkcs1v15CrtEnc(CAVIUM_BLOCKING, BT1, key->c_nSz, (word16)inLen,
  590. key->c_q, key->c_dQ, key->c_p, key->c_dP, key->c_u,
  591. (byte*)in, out, &requestId, key->devId);
  592. if (ret != 0) {
  593. CYASSL_MSG("Cavium CRT Enc BT1 failed");
  594. return -1;
  595. }
  596. return key->c_nSz;
  597. }
  598. static int CaviumRsaSSL_Verify(const byte* in, word32 inLen, byte* out,
  599. word32 outLen, RsaKey* key)
  600. {
  601. word32 requestId;
  602. word32 ret;
  603. word16 outSz = (word16)outLen;
  604. if (key == NULL || in == NULL || out == NULL || inLen != (word32)key->c_nSz)
  605. return -1;
  606. ret = CspPkcs1v15Dec(CAVIUM_BLOCKING, BT1, key->c_nSz, key->c_eSz,
  607. key->c_n, key->c_e, (byte*)in, &outSz, out,
  608. &requestId, key->devId);
  609. if (ret != 0) {
  610. CYASSL_MSG("Cavium Dec BT1 failed");
  611. return -1;
  612. }
  613. outSz = ntohs(outSz);
  614. return outSz;
  615. }
  616. #endif /* HAVE_CAVIUM */
  617. #endif /* NO_RSA */