rsa_gen.c 12 KB

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