rsa_gen.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /*
  10. * NB: these functions have been "upgraded", the deprecated versions (which
  11. * are compatibility wrappers using these functions) are in rsa_depr.c. -
  12. * Geoff
  13. */
  14. #include <stdio.h>
  15. #include <time.h>
  16. #include "internal/cryptlib.h"
  17. #include <openssl/bn.h>
  18. #include "rsa_locl.h"
  19. static int rsa_builtin_keygen(RSA *rsa, int bits, int primes, BIGNUM *e_value,
  20. BN_GENCB *cb);
  21. /*
  22. * NB: this wrapper would normally be placed in rsa_lib.c and the static
  23. * implementation would probably be in rsa_eay.c. Nonetheless, is kept here
  24. * so that we don't introduce a new linker dependency. Eg. any application
  25. * that wasn't previously linking object code related to key-generation won't
  26. * have to now just because key-generation is part of RSA_METHOD.
  27. */
  28. int RSA_generate_key_ex(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb)
  29. {
  30. if (rsa->meth->rsa_keygen != NULL)
  31. return rsa->meth->rsa_keygen(rsa, bits, e_value, cb);
  32. return RSA_generate_multi_prime_key(rsa, bits, RSA_DEFAULT_PRIME_NUM,
  33. e_value, cb);
  34. }
  35. int RSA_generate_multi_prime_key(RSA *rsa, int bits, int primes,
  36. BIGNUM *e_value, BN_GENCB *cb)
  37. {
  38. /* multi-prime is only supported with the builtin key generation */
  39. if (rsa->meth->rsa_multi_prime_keygen != NULL) {
  40. return rsa->meth->rsa_multi_prime_keygen(rsa, bits, primes,
  41. e_value, cb);
  42. } else if (rsa->meth->rsa_keygen != NULL) {
  43. /*
  44. * However, if rsa->meth implements only rsa_keygen, then we
  45. * have to honour it in 2-prime case and assume that it wouldn't
  46. * know what to do with multi-prime key generated by builtin
  47. * subroutine...
  48. */
  49. if (primes == 2)
  50. return rsa->meth->rsa_keygen(rsa, bits, e_value, cb);
  51. else
  52. return 0;
  53. }
  54. return rsa_builtin_keygen(rsa, bits, primes, e_value, cb);
  55. }
  56. static int rsa_builtin_keygen(RSA *rsa, int bits, int primes, BIGNUM *e_value,
  57. BN_GENCB *cb)
  58. {
  59. BIGNUM *r0 = NULL, *r1 = NULL, *r2 = NULL, *tmp, *prime;
  60. int ok = -1, n = 0, bitsr[RSA_MAX_PRIME_NUM], bitse = 0;
  61. int i = 0, quo = 0, rmd = 0, adj = 0, retries = 0;
  62. RSA_PRIME_INFO *pinfo = NULL;
  63. STACK_OF(RSA_PRIME_INFO) *prime_infos = NULL;
  64. BN_CTX *ctx = NULL;
  65. BN_ULONG bitst = 0;
  66. unsigned long error = 0;
  67. if (bits < RSA_MIN_MODULUS_BITS) {
  68. ok = 0; /* we set our own err */
  69. RSAerr(RSA_F_RSA_BUILTIN_KEYGEN, RSA_R_KEY_SIZE_TOO_SMALL);
  70. goto err;
  71. }
  72. if (primes < RSA_DEFAULT_PRIME_NUM || primes > rsa_multip_cap(bits)) {
  73. ok = 0; /* we set our own err */
  74. RSAerr(RSA_F_RSA_BUILTIN_KEYGEN, RSA_R_KEY_PRIME_NUM_INVALID);
  75. goto err;
  76. }
  77. ctx = BN_CTX_new();
  78. if (ctx == NULL)
  79. goto err;
  80. BN_CTX_start(ctx);
  81. r0 = BN_CTX_get(ctx);
  82. r1 = BN_CTX_get(ctx);
  83. r2 = BN_CTX_get(ctx);
  84. if (r2 == NULL)
  85. goto err;
  86. /* divide bits into 'primes' pieces evenly */
  87. quo = bits / primes;
  88. rmd = bits % primes;
  89. for (i = 0; i < primes; i++)
  90. bitsr[i] = (i < rmd) ? quo + 1 : quo;
  91. /* We need the RSA components non-NULL */
  92. if (!rsa->n && ((rsa->n = BN_new()) == NULL))
  93. goto err;
  94. if (!rsa->d && ((rsa->d = BN_secure_new()) == NULL))
  95. goto err;
  96. if (!rsa->e && ((rsa->e = BN_new()) == NULL))
  97. goto err;
  98. if (!rsa->p && ((rsa->p = BN_secure_new()) == NULL))
  99. goto err;
  100. if (!rsa->q && ((rsa->q = BN_secure_new()) == NULL))
  101. goto err;
  102. if (!rsa->dmp1 && ((rsa->dmp1 = BN_secure_new()) == NULL))
  103. goto err;
  104. if (!rsa->dmq1 && ((rsa->dmq1 = BN_secure_new()) == NULL))
  105. goto err;
  106. if (!rsa->iqmp && ((rsa->iqmp = BN_secure_new()) == NULL))
  107. goto err;
  108. /* initialize multi-prime components */
  109. if (primes > RSA_DEFAULT_PRIME_NUM) {
  110. rsa->version = RSA_ASN1_VERSION_MULTI;
  111. prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, primes - 2);
  112. if (prime_infos == NULL)
  113. goto err;
  114. if (rsa->prime_infos != NULL) {
  115. /* could this happen? */
  116. sk_RSA_PRIME_INFO_pop_free(rsa->prime_infos, rsa_multip_info_free);
  117. }
  118. rsa->prime_infos = prime_infos;
  119. /* prime_info from 2 to |primes| -1 */
  120. for (i = 2; i < primes; i++) {
  121. pinfo = rsa_multip_info_new();
  122. if (pinfo == NULL)
  123. goto err;
  124. (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo);
  125. }
  126. }
  127. if (BN_copy(rsa->e, e_value) == NULL)
  128. goto err;
  129. /* generate p, q and other primes (if any) */
  130. for (i = 0; i < primes; i++) {
  131. adj = 0;
  132. retries = 0;
  133. if (i == 0) {
  134. prime = rsa->p;
  135. } else if (i == 1) {
  136. prime = rsa->q;
  137. } else {
  138. pinfo = sk_RSA_PRIME_INFO_value(prime_infos, i - 2);
  139. prime = pinfo->r;
  140. }
  141. for (;;) {
  142. redo:
  143. if (!BN_generate_prime_ex(prime, bitsr[i] + adj, 0, NULL, NULL, cb))
  144. goto err;
  145. /*
  146. * prime should not be equal to p, q, r_3...
  147. * (those primes prior to this one)
  148. */
  149. {
  150. int j;
  151. for (j = 0; j < i; j++) {
  152. BIGNUM *prev_prime;
  153. if (j == 0)
  154. prev_prime = rsa->p;
  155. else if (j == 1)
  156. prev_prime = rsa->q;
  157. else
  158. prev_prime = sk_RSA_PRIME_INFO_value(prime_infos,
  159. j - 2)->r;
  160. if (!BN_cmp(prime, prev_prime)) {
  161. goto redo;
  162. }
  163. }
  164. }
  165. if (!BN_sub(r2, prime, BN_value_one()))
  166. goto err;
  167. ERR_set_mark();
  168. BN_set_flags(r2, BN_FLG_CONSTTIME);
  169. if (BN_mod_inverse(r1, r2, rsa->e, ctx) != NULL) {
  170. /* GCD == 1 since inverse exists */
  171. break;
  172. }
  173. error = ERR_peek_last_error();
  174. if (ERR_GET_LIB(error) == ERR_LIB_BN
  175. && ERR_GET_REASON(error) == BN_R_NO_INVERSE) {
  176. /* GCD != 1 */
  177. ERR_pop_to_mark();
  178. } else {
  179. goto err;
  180. }
  181. if (!BN_GENCB_call(cb, 2, n++))
  182. goto err;
  183. }
  184. bitse += bitsr[i];
  185. /* calculate n immediately to see if it's sufficient */
  186. if (i == 1) {
  187. /* we get at least 2 primes */
  188. if (!BN_mul(r1, rsa->p, rsa->q, ctx))
  189. goto err;
  190. } else if (i != 0) {
  191. /* modulus n = p * q * r_3 * r_4 ... */
  192. if (!BN_mul(r1, rsa->n, prime, ctx))
  193. goto err;
  194. } else {
  195. /* i == 0, do nothing */
  196. if (!BN_GENCB_call(cb, 3, i))
  197. goto err;
  198. continue;
  199. }
  200. /*
  201. * if |r1|, product of factors so far, is not as long as expected
  202. * (by checking the first 4 bits are less than 0x9 or greater than
  203. * 0xF). If so, re-generate the last prime.
  204. *
  205. * NOTE: This actually can't happen in two-prime case, because of
  206. * the way factors are generated.
  207. *
  208. * Besides, another consideration is, for multi-prime case, even the
  209. * length modulus is as long as expected, the modulus could start at
  210. * 0x8, which could be utilized to distinguish a multi-prime private
  211. * key by using the modulus in a certificate. This is also covered
  212. * by checking the length should not be less than 0x9.
  213. */
  214. if (!BN_rshift(r2, r1, bitse - 4))
  215. goto err;
  216. bitst = BN_get_word(r2);
  217. if (bitst < 0x9 || bitst > 0xF) {
  218. /*
  219. * For keys with more than 4 primes, we attempt longer factor to
  220. * meet length requirement.
  221. *
  222. * Otherwise, we just re-generate the prime with the same length.
  223. *
  224. * This strategy has the following goals:
  225. *
  226. * 1. 1024-bit factors are effcient when using 3072 and 4096-bit key
  227. * 2. stay the same logic with normal 2-prime key
  228. */
  229. bitse -= bitsr[i];
  230. if (!BN_GENCB_call(cb, 2, n++))
  231. goto err;
  232. if (primes > 4) {
  233. if (bitst < 0x9)
  234. adj++;
  235. else
  236. adj--;
  237. } else if (retries == 4) {
  238. /*
  239. * re-generate all primes from scratch, mainly used
  240. * in 4 prime case to avoid long loop. Max retry times
  241. * is set to 4.
  242. */
  243. i = -1;
  244. bitse = 0;
  245. continue;
  246. }
  247. retries++;
  248. goto redo;
  249. }
  250. /* save product of primes for further use, for multi-prime only */
  251. if (i > 1 && BN_copy(pinfo->pp, rsa->n) == NULL)
  252. goto err;
  253. if (BN_copy(rsa->n, r1) == NULL)
  254. goto err;
  255. if (!BN_GENCB_call(cb, 3, i))
  256. goto err;
  257. }
  258. if (BN_cmp(rsa->p, rsa->q) < 0) {
  259. tmp = rsa->p;
  260. rsa->p = rsa->q;
  261. rsa->q = tmp;
  262. }
  263. /* calculate d */
  264. /* p - 1 */
  265. if (!BN_sub(r1, rsa->p, BN_value_one()))
  266. goto err;
  267. /* q - 1 */
  268. if (!BN_sub(r2, rsa->q, BN_value_one()))
  269. goto err;
  270. /* (p - 1)(q - 1) */
  271. if (!BN_mul(r0, r1, r2, ctx))
  272. goto err;
  273. /* multi-prime */
  274. for (i = 2; i < primes; i++) {
  275. pinfo = sk_RSA_PRIME_INFO_value(prime_infos, i - 2);
  276. /* save r_i - 1 to pinfo->d temporarily */
  277. if (!BN_sub(pinfo->d, pinfo->r, BN_value_one()))
  278. goto err;
  279. if (!BN_mul(r0, r0, pinfo->d, ctx))
  280. goto err;
  281. }
  282. {
  283. BIGNUM *pr0 = BN_new();
  284. if (pr0 == NULL)
  285. goto err;
  286. BN_with_flags(pr0, r0, BN_FLG_CONSTTIME);
  287. if (!BN_mod_inverse(rsa->d, rsa->e, pr0, ctx)) {
  288. BN_free(pr0);
  289. goto err; /* d */
  290. }
  291. /* We MUST free pr0 before any further use of r0 */
  292. BN_free(pr0);
  293. }
  294. {
  295. BIGNUM *d = BN_new();
  296. if (d == NULL)
  297. goto err;
  298. BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
  299. /* calculate d mod (p-1) and d mod (q - 1) */
  300. if (!BN_mod(rsa->dmp1, d, r1, ctx)
  301. || !BN_mod(rsa->dmq1, d, r2, ctx)) {
  302. BN_free(d);
  303. goto err;
  304. }
  305. /* calculate CRT exponents */
  306. for (i = 2; i < primes; i++) {
  307. pinfo = sk_RSA_PRIME_INFO_value(prime_infos, i - 2);
  308. /* pinfo->d == r_i - 1 */
  309. if (!BN_mod(pinfo->d, d, pinfo->d, ctx)) {
  310. BN_free(d);
  311. goto err;
  312. }
  313. }
  314. /* We MUST free d before any further use of rsa->d */
  315. BN_free(d);
  316. }
  317. {
  318. BIGNUM *p = BN_new();
  319. if (p == NULL)
  320. goto err;
  321. BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME);
  322. /* calculate inverse of q mod p */
  323. if (!BN_mod_inverse(rsa->iqmp, rsa->q, p, ctx)) {
  324. BN_free(p);
  325. goto err;
  326. }
  327. /* calculate CRT coefficient for other primes */
  328. for (i = 2; i < primes; i++) {
  329. pinfo = sk_RSA_PRIME_INFO_value(prime_infos, i - 2);
  330. BN_with_flags(p, pinfo->r, BN_FLG_CONSTTIME);
  331. if (!BN_mod_inverse(pinfo->t, pinfo->pp, p, ctx)) {
  332. BN_free(p);
  333. goto err;
  334. }
  335. }
  336. /* We MUST free p before any further use of rsa->p */
  337. BN_free(p);
  338. }
  339. ok = 1;
  340. err:
  341. if (ok == -1) {
  342. RSAerr(RSA_F_RSA_BUILTIN_KEYGEN, ERR_LIB_BN);
  343. ok = 0;
  344. }
  345. if (ctx != NULL)
  346. BN_CTX_end(ctx);
  347. BN_CTX_free(ctx);
  348. return ok;
  349. }