2
0

rsa_gen.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. BN_set_flags(prime, BN_FLG_CONSTTIME);
  142. for (;;) {
  143. redo:
  144. if (!BN_generate_prime_ex(prime, bitsr[i] + adj, 0, NULL, NULL, cb))
  145. goto err;
  146. /*
  147. * prime should not be equal to p, q, r_3...
  148. * (those primes prior to this one)
  149. */
  150. {
  151. int j;
  152. for (j = 0; j < i; j++) {
  153. BIGNUM *prev_prime;
  154. if (j == 0)
  155. prev_prime = rsa->p;
  156. else if (j == 1)
  157. prev_prime = rsa->q;
  158. else
  159. prev_prime = sk_RSA_PRIME_INFO_value(prime_infos,
  160. j - 2)->r;
  161. if (!BN_cmp(prime, prev_prime)) {
  162. goto redo;
  163. }
  164. }
  165. }
  166. if (!BN_sub(r2, prime, BN_value_one()))
  167. goto err;
  168. ERR_set_mark();
  169. BN_set_flags(r2, BN_FLG_CONSTTIME);
  170. if (BN_mod_inverse(r1, r2, rsa->e, ctx) != NULL) {
  171. /* GCD == 1 since inverse exists */
  172. break;
  173. }
  174. error = ERR_peek_last_error();
  175. if (ERR_GET_LIB(error) == ERR_LIB_BN
  176. && ERR_GET_REASON(error) == BN_R_NO_INVERSE) {
  177. /* GCD != 1 */
  178. ERR_pop_to_mark();
  179. } else {
  180. goto err;
  181. }
  182. if (!BN_GENCB_call(cb, 2, n++))
  183. goto err;
  184. }
  185. bitse += bitsr[i];
  186. /* calculate n immediately to see if it's sufficient */
  187. if (i == 1) {
  188. /* we get at least 2 primes */
  189. if (!BN_mul(r1, rsa->p, rsa->q, ctx))
  190. goto err;
  191. } else if (i != 0) {
  192. /* modulus n = p * q * r_3 * r_4 ... */
  193. if (!BN_mul(r1, rsa->n, prime, ctx))
  194. goto err;
  195. } else {
  196. /* i == 0, do nothing */
  197. if (!BN_GENCB_call(cb, 3, i))
  198. goto err;
  199. continue;
  200. }
  201. /*
  202. * if |r1|, product of factors so far, is not as long as expected
  203. * (by checking the first 4 bits are less than 0x9 or greater than
  204. * 0xF). If so, re-generate the last prime.
  205. *
  206. * NOTE: This actually can't happen in two-prime case, because of
  207. * the way factors are generated.
  208. *
  209. * Besides, another consideration is, for multi-prime case, even the
  210. * length modulus is as long as expected, the modulus could start at
  211. * 0x8, which could be utilized to distinguish a multi-prime private
  212. * key by using the modulus in a certificate. This is also covered
  213. * by checking the length should not be less than 0x9.
  214. */
  215. if (!BN_rshift(r2, r1, bitse - 4))
  216. goto err;
  217. bitst = BN_get_word(r2);
  218. if (bitst < 0x9 || bitst > 0xF) {
  219. /*
  220. * For keys with more than 4 primes, we attempt longer factor to
  221. * meet length requirement.
  222. *
  223. * Otherwise, we just re-generate the prime with the same length.
  224. *
  225. * This strategy has the following goals:
  226. *
  227. * 1. 1024-bit factors are effcient when using 3072 and 4096-bit key
  228. * 2. stay the same logic with normal 2-prime key
  229. */
  230. bitse -= bitsr[i];
  231. if (!BN_GENCB_call(cb, 2, n++))
  232. goto err;
  233. if (primes > 4) {
  234. if (bitst < 0x9)
  235. adj++;
  236. else
  237. adj--;
  238. } else if (retries == 4) {
  239. /*
  240. * re-generate all primes from scratch, mainly used
  241. * in 4 prime case to avoid long loop. Max retry times
  242. * is set to 4.
  243. */
  244. i = -1;
  245. bitse = 0;
  246. continue;
  247. }
  248. retries++;
  249. goto redo;
  250. }
  251. /* save product of primes for further use, for multi-prime only */
  252. if (i > 1 && BN_copy(pinfo->pp, rsa->n) == NULL)
  253. goto err;
  254. if (BN_copy(rsa->n, r1) == NULL)
  255. goto err;
  256. if (!BN_GENCB_call(cb, 3, i))
  257. goto err;
  258. }
  259. if (BN_cmp(rsa->p, rsa->q) < 0) {
  260. tmp = rsa->p;
  261. rsa->p = rsa->q;
  262. rsa->q = tmp;
  263. }
  264. /* calculate d */
  265. /* p - 1 */
  266. if (!BN_sub(r1, rsa->p, BN_value_one()))
  267. goto err;
  268. /* q - 1 */
  269. if (!BN_sub(r2, rsa->q, BN_value_one()))
  270. goto err;
  271. /* (p - 1)(q - 1) */
  272. if (!BN_mul(r0, r1, r2, ctx))
  273. goto err;
  274. /* multi-prime */
  275. for (i = 2; i < primes; i++) {
  276. pinfo = sk_RSA_PRIME_INFO_value(prime_infos, i - 2);
  277. /* save r_i - 1 to pinfo->d temporarily */
  278. if (!BN_sub(pinfo->d, pinfo->r, BN_value_one()))
  279. goto err;
  280. if (!BN_mul(r0, r0, pinfo->d, ctx))
  281. goto err;
  282. }
  283. {
  284. BIGNUM *pr0 = BN_new();
  285. if (pr0 == NULL)
  286. goto err;
  287. BN_with_flags(pr0, r0, BN_FLG_CONSTTIME);
  288. if (!BN_mod_inverse(rsa->d, rsa->e, pr0, ctx)) {
  289. BN_free(pr0);
  290. goto err; /* d */
  291. }
  292. /* We MUST free pr0 before any further use of r0 */
  293. BN_free(pr0);
  294. }
  295. {
  296. BIGNUM *d = BN_new();
  297. if (d == NULL)
  298. goto err;
  299. BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
  300. /* calculate d mod (p-1) and d mod (q - 1) */
  301. if (!BN_mod(rsa->dmp1, d, r1, ctx)
  302. || !BN_mod(rsa->dmq1, d, r2, ctx)) {
  303. BN_free(d);
  304. goto err;
  305. }
  306. /* calculate CRT exponents */
  307. for (i = 2; i < primes; i++) {
  308. pinfo = sk_RSA_PRIME_INFO_value(prime_infos, i - 2);
  309. /* pinfo->d == r_i - 1 */
  310. if (!BN_mod(pinfo->d, d, pinfo->d, ctx)) {
  311. BN_free(d);
  312. goto err;
  313. }
  314. }
  315. /* We MUST free d before any further use of rsa->d */
  316. BN_free(d);
  317. }
  318. {
  319. BIGNUM *p = BN_new();
  320. if (p == NULL)
  321. goto err;
  322. BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME);
  323. /* calculate inverse of q mod p */
  324. if (!BN_mod_inverse(rsa->iqmp, rsa->q, p, ctx)) {
  325. BN_free(p);
  326. goto err;
  327. }
  328. /* calculate CRT coefficient for other primes */
  329. for (i = 2; i < primes; i++) {
  330. pinfo = sk_RSA_PRIME_INFO_value(prime_infos, i - 2);
  331. BN_with_flags(p, pinfo->r, BN_FLG_CONSTTIME);
  332. if (!BN_mod_inverse(pinfo->t, pinfo->pp, p, ctx)) {
  333. BN_free(p);
  334. goto err;
  335. }
  336. }
  337. /* We MUST free p before any further use of rsa->p */
  338. BN_free(p);
  339. }
  340. ok = 1;
  341. err:
  342. if (ok == -1) {
  343. RSAerr(RSA_F_RSA_BUILTIN_KEYGEN, ERR_LIB_BN);
  344. ok = 0;
  345. }
  346. if (ctx != NULL)
  347. BN_CTX_end(ctx);
  348. BN_CTX_free(ctx);
  349. return ok;
  350. }