rsa.c 14 KB

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