rsa.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. /* rsa.c
  2. *
  3. * Copyright (C) 2006-2014 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  20. */
  21. #include "rsa.h"
  22. #include "error-crypt.h"
  23. #include "api.h"
  24. #ifdef IN_PAL
  25. int _DkRandomBitsRead (void *buffer, int size);
  26. #define DkRandomBitsRead _DkRandomBitsRead
  27. #else
  28. int DkRandomBitsRead (void *buffer, int size);
  29. #endif
  30. void * malloc (int size);
  31. void free (void * mem);
  32. #define XMALLOC malloc
  33. #define XFREE free
  34. #define XMEMCPY memcpy
  35. #define XMEMSET memset
  36. enum {
  37. RSA_PUBLIC_ENCRYPT = 0,
  38. RSA_PUBLIC_DECRYPT = 1,
  39. RSA_PRIVATE_ENCRYPT = 2,
  40. RSA_PRIVATE_DECRYPT = 3,
  41. RSA_BLOCK_TYPE_1 = 1,
  42. RSA_BLOCK_TYPE_2 = 2,
  43. RSA_MIN_SIZE = 512,
  44. RSA_MAX_SIZE = 4096,
  45. RSA_MIN_PAD_SZ = 11 /* seperator + 0 + pad value + 8 pads */
  46. };
  47. int InitRSAKey(RSAKey *key)
  48. {
  49. key->type = -1; /* haven't decided yet */
  50. key->n.dp = key->e.dp = 0; /* public alloc parts */
  51. key->d.dp = key->p.dp = 0; /* private alloc parts */
  52. key->q.dp = key->dP.dp = 0;
  53. key->u.dp = key->dQ.dp = 0;
  54. return 0;
  55. }
  56. int FreeRSAKey(RSAKey *key)
  57. {
  58. (void)key;
  59. if (key->type == RSA_PRIVATE) {
  60. mp_clear(&key->u);
  61. mp_clear(&key->dQ);
  62. mp_clear(&key->dP);
  63. mp_clear(&key->q);
  64. mp_clear(&key->p);
  65. mp_clear(&key->d);
  66. }
  67. mp_clear(&key->e);
  68. mp_clear(&key->n);
  69. return 0;
  70. }
  71. static int RSAPad(const byte *input, word32 inputLen, byte *pkcsBlock,
  72. word32 pkcsBlockLen, byte padValue)
  73. {
  74. if (inputLen == 0)
  75. return 0;
  76. pkcsBlock[0] = 0x0; /* set first byte to zero and advance */
  77. pkcsBlock++; pkcsBlockLen--;
  78. pkcsBlock[0] = padValue; /* insert padValue */
  79. if (padValue == RSA_BLOCK_TYPE_1)
  80. /* pad with 0xff bytes */
  81. XMEMSET(&pkcsBlock[1], 0xFF, pkcsBlockLen - inputLen - 2);
  82. else {
  83. /* pad with non-zero random bytes */
  84. word32 padLen = pkcsBlockLen - inputLen - 1, i;
  85. int ret = DkRandomBitsRead(&pkcsBlock[1], padLen);
  86. if (ret < 0)
  87. return ret;
  88. /* remove zeros */
  89. for (i = 1; i < padLen; i++)
  90. if (pkcsBlock[i] == 0) pkcsBlock[i] = 0x01;
  91. }
  92. pkcsBlock[pkcsBlockLen-inputLen-1] = 0; /* separator */
  93. XMEMCPY(pkcsBlock+pkcsBlockLen-inputLen, input, inputLen);
  94. return 0;
  95. }
  96. /* UnPad plaintext, set start to *output, return length of plaintext,
  97. * < 0 on error */
  98. static int RSAUnPad(const byte *pkcsBlock, unsigned int pkcsBlockLen,
  99. byte **output, byte padValue)
  100. {
  101. word32 maxOutputLen = (pkcsBlockLen > 10) ? (pkcsBlockLen - 10) : 0,
  102. invalid = 0,
  103. i = 1,
  104. outputLen;
  105. if (pkcsBlock[0] != 0x0) /* skip past zero */
  106. invalid = 1;
  107. pkcsBlock++; pkcsBlockLen--;
  108. /* Require block type padValue */
  109. invalid = (pkcsBlock[0] != padValue) || invalid;
  110. /* verify the padding until we find the separator */
  111. if (padValue == RSA_BLOCK_TYPE_1) {
  112. while (i<pkcsBlockLen && pkcsBlock[i++] == 0xFF) {/* Null body */}
  113. }
  114. else {
  115. while (i<pkcsBlockLen && pkcsBlock[i++]) {/* Null body */}
  116. }
  117. if(!(i==pkcsBlockLen || pkcsBlock[i-1]==0))
  118. return RSA_PAD_E;
  119. outputLen = pkcsBlockLen - i;
  120. invalid = (outputLen > maxOutputLen) || invalid;
  121. if (invalid)
  122. return RSA_PAD_E;
  123. *output = (byte *)(pkcsBlock + i);
  124. return outputLen;
  125. }
  126. static int RSAFunction(const byte *in, word32 inLen, byte *out, word32 *outLen,
  127. int type, RSAKey *key)
  128. {
  129. #define ERROR_OUT(x) { ret = (x); goto done;}
  130. mp_int tmp;
  131. int ret = 0;
  132. word32 keyLen, len;
  133. if (mp_init(&tmp) != MP_OKAY)
  134. return MP_INIT_E;
  135. if (mp_read_unsigned_bin(&tmp, (byte*)in, inLen) != MP_OKAY)
  136. ERROR_OUT(MP_READ_E);
  137. if (type == RSA_PRIVATE_DECRYPT || type == RSA_PRIVATE_ENCRYPT) {
  138. #define INNER_ERROR_OUT(x) { ret = (x); goto inner_done; }
  139. mp_int tmpa, tmpb;
  140. if (mp_init(&tmpa) != MP_OKAY)
  141. ERROR_OUT(MP_INIT_E);
  142. if (mp_init(&tmpb) != MP_OKAY) {
  143. mp_clear(&tmpa);
  144. ERROR_OUT(MP_INIT_E);
  145. }
  146. /* tmpa = tmp^dP mod p */
  147. if (mp_exptmod(&tmp, &key->dP, &key->p, &tmpa) != MP_OKAY)
  148. INNER_ERROR_OUT(MP_EXPTMOD_E);
  149. /* tmpb = tmp^dQ mod q */
  150. if (mp_exptmod(&tmp, &key->dQ, &key->q, &tmpb) != MP_OKAY)
  151. INNER_ERROR_OUT(MP_EXPTMOD_E);
  152. /* tmp = (tmpa - tmpb) *qInv (mod p) */
  153. if (mp_sub(&tmpa, &tmpb, &tmp) != MP_OKAY)
  154. INNER_ERROR_OUT(MP_SUB_E);
  155. if (mp_mulmod(&tmp, &key->u, &key->p, &tmp) != MP_OKAY)
  156. INNER_ERROR_OUT(MP_MULMOD_E);
  157. /* tmp = tmpb + q *tmp */
  158. if (mp_mul(&tmp, &key->q, &tmp) != MP_OKAY)
  159. INNER_ERROR_OUT(MP_MUL_E);
  160. if (mp_add(&tmp, &tmpb, &tmp) != MP_OKAY)
  161. INNER_ERROR_OUT(MP_ADD_E);
  162. inner_done:
  163. mp_clear(&tmpa);
  164. mp_clear(&tmpb);
  165. if (ret != 0) return ret;
  166. }
  167. else if (type == RSA_PUBLIC_ENCRYPT || type == RSA_PUBLIC_DECRYPT) {
  168. if (mp_exptmod(&tmp, &key->e, &key->n, &tmp) != MP_OKAY)
  169. ERROR_OUT(MP_EXPTMOD_E);
  170. }
  171. else
  172. ERROR_OUT(RSA_WRONG_TYPE_E);
  173. keyLen = mp_unsigned_bin_size(&key->n);
  174. if (keyLen > *outLen)
  175. ERROR_OUT(RSA_BUFFER_E);
  176. len = mp_unsigned_bin_size(&tmp);
  177. /* pad front w/ zeros to match key length */
  178. while (len < keyLen) {
  179. *out++ = 0x00;
  180. len++;
  181. }
  182. *outLen = keyLen;
  183. /* convert */
  184. if (mp_to_unsigned_bin(&tmp, out) != MP_OKAY)
  185. ERROR_OUT(MP_TO_E);
  186. done:
  187. mp_clear(&tmp);
  188. return ret;
  189. }
  190. int RSAPublicEncrypt(const byte *in, word32 inLen, byte *out, word32 outLen,
  191. RSAKey *key)
  192. {
  193. int sz, ret;
  194. sz = mp_unsigned_bin_size(&key->n);
  195. if (sz > (int)outLen)
  196. return RSA_BUFFER_E;
  197. if (inLen > (word32)(sz - RSA_MIN_PAD_SZ))
  198. return RSA_BUFFER_E;
  199. ret = RSAPad(in, inLen, out, sz, RSA_BLOCK_TYPE_2);
  200. if (ret != 0)
  201. return ret;
  202. if ((ret = RSAFunction(out, sz, out, &outLen, RSA_PUBLIC_ENCRYPT, key)) < 0)
  203. sz = ret;
  204. return sz;
  205. }
  206. int RSAPrivateDecryptInline(byte *in, word32 inLen, byte* *out, RSAKey *key)
  207. {
  208. int ret;
  209. if ((ret = RSAFunction(in, inLen, in, &inLen, RSA_PRIVATE_DECRYPT, key))
  210. < 0) {
  211. return ret;
  212. }
  213. return RSAUnPad(in, inLen, out, RSA_BLOCK_TYPE_2);
  214. }
  215. int RSAPrivateDecrypt(const byte *in, word32 inLen, byte *out, word32 outLen,
  216. RSAKey *key)
  217. {
  218. int plainLen;
  219. byte *tmp;
  220. byte *pad = 0;
  221. tmp = (byte *)XMALLOC(inLen);
  222. if (tmp == NULL) {
  223. return MEMORY_E;
  224. }
  225. XMEMCPY(tmp, in, inLen);
  226. if ( (plainLen = RSAPrivateDecryptInline(tmp, inLen, &pad, key) ) < 0) {
  227. XFREE(tmp);
  228. return plainLen;
  229. }
  230. if (plainLen > (int)outLen)
  231. plainLen = BAD_FUNC_ARG;
  232. else
  233. XMEMCPY(out, pad, plainLen);
  234. XMEMSET(tmp, 0x00, inLen);
  235. XFREE(tmp);
  236. return plainLen;
  237. }
  238. /* for RSA Verify */
  239. int RSASSL_VerifyInline(byte *in, word32 inLen, byte* *out, RSAKey *key)
  240. {
  241. int ret;
  242. if ((ret = RSAFunction(in, inLen, in, &inLen, RSA_PUBLIC_DECRYPT, key))
  243. < 0) {
  244. return ret;
  245. }
  246. return RSAUnPad(in, inLen, out, RSA_BLOCK_TYPE_1);
  247. }
  248. int RSASSL_Verify(const byte *in, word32 inLen, byte *out, word32 outLen,
  249. RSAKey *key)
  250. {
  251. int plainLen;
  252. byte *tmp;
  253. byte *pad = 0;
  254. tmp = (byte *)XMALLOC(inLen);
  255. if (tmp == NULL) {
  256. return MEMORY_E;
  257. }
  258. XMEMCPY(tmp, in, inLen);
  259. if ( (plainLen = RSASSL_VerifyInline(tmp, inLen, &pad, key) ) < 0) {
  260. XFREE(tmp);
  261. return plainLen;
  262. }
  263. if (plainLen > (int)outLen)
  264. plainLen = BAD_FUNC_ARG;
  265. else
  266. XMEMCPY(out, pad, plainLen);
  267. XMEMSET(tmp, 0x00, inLen);
  268. XFREE(tmp);
  269. return plainLen;
  270. }
  271. /* for RSA Sign */
  272. int RSASSL_Sign(const byte *in, word32 inLen, byte *out, word32 outLen,
  273. RSAKey *key)
  274. {
  275. int sz, ret;
  276. sz = mp_unsigned_bin_size(&key->n);
  277. if (sz > (int)outLen)
  278. return RSA_BUFFER_E;
  279. if (inLen > (word32)(sz - RSA_MIN_PAD_SZ))
  280. return RSA_BUFFER_E;
  281. ret = RSAPad(in, inLen, out, sz, RSA_BLOCK_TYPE_1);
  282. if (ret != 0)
  283. return ret;
  284. if ((ret = RSAFunction(out, sz, out, &outLen, RSA_PRIVATE_ENCRYPT,key)) < 0)
  285. sz = ret;
  286. return sz;
  287. }
  288. int RSAEncryptSize(RSAKey *key)
  289. {
  290. return mp_unsigned_bin_size(&key->n);
  291. }
  292. int RSAPublicKeyDecodeRaw(const byte* n, word32 nSz, const byte* e, word32 eSz,
  293. RSAKey* key)
  294. {
  295. if (n == NULL || e == NULL || key == NULL)
  296. return BAD_FUNC_ARG;
  297. key->type = RSA_PUBLIC;
  298. if (mp_init(&key->n) != MP_OKAY)
  299. return MP_INIT_E;
  300. if (mp_read_unsigned_bin(&key->n, n, nSz) != 0) {
  301. mp_clear(&key->n);
  302. return ASN_GETINT_E;
  303. }
  304. if (mp_init(&key->e) != MP_OKAY) {
  305. mp_clear(&key->n);
  306. return MP_INIT_E;
  307. }
  308. if (mp_read_unsigned_bin(&key->e, e, eSz) != 0) {
  309. mp_clear(&key->n);
  310. mp_clear(&key->e);
  311. return ASN_GETINT_E;
  312. }
  313. return 0;
  314. }
  315. int RSAFlattenPublicKey(RSAKey *key, byte *e, word32 *eSz, byte *n, word32 *nSz)
  316. {
  317. int sz, ret;
  318. if (key == NULL || e == NULL || eSz == NULL || n == NULL || nSz == NULL)
  319. return BAD_FUNC_ARG;
  320. sz = mp_unsigned_bin_size(&key->e);
  321. if ((word32)sz > *nSz)
  322. return RSA_BUFFER_E;
  323. ret = mp_to_unsigned_bin(&key->e, e);
  324. if (ret != MP_OKAY)
  325. return ret;
  326. *eSz = (word32)sz;
  327. sz = mp_unsigned_bin_size(&key->n);
  328. if ((word32)sz > *nSz)
  329. return RSA_BUFFER_E;
  330. ret = mp_to_unsigned_bin(&key->n, n);
  331. if (ret != MP_OKAY)
  332. return ret;
  333. *nSz = (word32)sz;
  334. return 0;
  335. }
  336. static int rand_prime(mp_int *N, int len)
  337. {
  338. int err, res;
  339. byte *buf;
  340. if (N == NULL)
  341. return BAD_FUNC_ARG;
  342. /* allow sizes between 2 and 512 bytes for a prime size */
  343. if (len < 2 || len > 512) {
  344. return BAD_FUNC_ARG;
  345. }
  346. /* allocate buffer to work with */
  347. buf = (byte*)XMALLOC(len);
  348. if (buf == NULL) {
  349. return MEMORY_E;
  350. }
  351. XMEMSET(buf, 0, len);
  352. do {
  353. /* generate value */
  354. err = DkRandomBitsRead(buf, len);
  355. if (err < 0) {
  356. XFREE(buf);
  357. return err;
  358. }
  359. /* munge bits */
  360. buf[0] |= 0x80 | 0x40;
  361. buf[len-1] |= 0x01;
  362. /* load value */
  363. if ((err = mp_read_unsigned_bin(N, buf, len)) != MP_OKAY) {
  364. XFREE(buf);
  365. return err;
  366. }
  367. /* test */
  368. if ((err = mp_prime_is_prime(N, 8, &res)) != MP_OKAY) {
  369. XFREE(buf);
  370. return err;
  371. }
  372. } while (res == MP_NO);
  373. XMEMSET(buf, 0, len);
  374. XFREE(buf);
  375. return 0;
  376. }
  377. /* Make an RSA key for size bits, with e specified, 65537 is a good e */
  378. int MakeRSAKey(RSAKey *key, int size, long e)
  379. {
  380. mp_int p, q, tmp1, tmp2, tmp3;
  381. int err;
  382. if (key == NULL)
  383. return BAD_FUNC_ARG;
  384. if (size < RSA_MIN_SIZE || size > RSA_MAX_SIZE)
  385. return BAD_FUNC_ARG;
  386. if (e < 3 || (e & 1) == 0)
  387. return BAD_FUNC_ARG;
  388. if ((err = mp_init_multi(&p, &q, &tmp1, &tmp2, &tmp3, NULL)) != MP_OKAY)
  389. return err;
  390. err = mp_set_int(&tmp3, e);
  391. /* make p */
  392. if (err == MP_OKAY) {
  393. do {
  394. err = rand_prime(&p, size/16); /* size in bytes/2 */
  395. if (err == MP_OKAY)
  396. err = mp_sub_d(&p, 1, &tmp1); /* tmp1 = p-1 */
  397. if (err == MP_OKAY)
  398. err = mp_gcd(&tmp1, &tmp3, &tmp2); /* tmp2 = gcd(p-1, e) */
  399. } while (err == MP_OKAY && mp_cmp_d(&tmp2, 1) != 0); /* e divdes p-1 */
  400. }
  401. /* make q */
  402. if (err == MP_OKAY) {
  403. do {
  404. err = rand_prime(&q, size/16); /* size in bytes/2 */
  405. if (err == MP_OKAY)
  406. err = mp_sub_d(&q, 1, &tmp1); /* tmp1 = q-1 */
  407. if (err == MP_OKAY)
  408. err = mp_gcd(&tmp1, &tmp3, &tmp2); /* tmp2 = gcd(q-1, e) */
  409. } while (err == MP_OKAY && mp_cmp_d(&tmp2, 1) != 0); /* e divdes q-1 */
  410. }
  411. if (err == MP_OKAY)
  412. err = mp_init_multi(&key->n, &key->e, &key->d, &key->p, &key->q, NULL);
  413. if (err == MP_OKAY)
  414. err = mp_init_multi(&key->dP, &key->dQ, &key->u, NULL, NULL, NULL);
  415. if (err == MP_OKAY)
  416. err = mp_sub_d(&p, 1, &tmp2); /* tmp2 = p-1 */
  417. if (err == MP_OKAY)
  418. err = mp_lcm(&tmp1, &tmp2, &tmp1); /* tmp1 = lcm(p-1, q-1),last loop */
  419. /* make key */
  420. if (err == MP_OKAY)
  421. err = mp_set_int(&key->e, e); /* key->e = e */
  422. if (err == MP_OKAY) /* key->d = 1/e mod lcm(p-1, q-1) */
  423. err = mp_invmod(&key->e, &tmp1, &key->d);
  424. if (err == MP_OKAY)
  425. err = mp_mul(&p, &q, &key->n); /* key->n = pq */
  426. if (err == MP_OKAY)
  427. err = mp_sub_d(&p, 1, &tmp1);
  428. if (err == MP_OKAY)
  429. err = mp_sub_d(&q, 1, &tmp2);
  430. if (err == MP_OKAY)
  431. err = mp_mod(&key->d, &tmp1, &key->dP);
  432. if (err == MP_OKAY)
  433. err = mp_mod(&key->d, &tmp2, &key->dQ);
  434. if (err == MP_OKAY)
  435. err = mp_invmod(&q, &p, &key->u);
  436. if (err == MP_OKAY)
  437. err = mp_copy(&p, &key->p);
  438. if (err == MP_OKAY)
  439. err = mp_copy(&q, &key->q);
  440. if (err == MP_OKAY)
  441. key->type = RSA_PRIVATE;
  442. mp_clear(&tmp3);
  443. mp_clear(&tmp2);
  444. mp_clear(&tmp1);
  445. mp_clear(&q);
  446. mp_clear(&p);
  447. if (err != MP_OKAY) {
  448. FreeRSAKey(key);
  449. return err;
  450. }
  451. return 0;
  452. }