rsa.c 14 KB

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