bn_x931p.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Copyright 2011-2021 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. #define OPENSSL_SUPPRESS_DEPRECATED
  10. #include <stdio.h>
  11. #include <openssl/bn.h>
  12. #include "bn_local.h"
  13. /* X9.31 routines for prime derivation */
  14. /*
  15. * X9.31 prime derivation. This is used to generate the primes pi (p1, p2,
  16. * q1, q2) from a parameter Xpi by checking successive odd integers.
  17. */
  18. static int bn_x931_derive_pi(BIGNUM *pi, const BIGNUM *Xpi, BN_CTX *ctx,
  19. BN_GENCB *cb)
  20. {
  21. int i = 0, is_prime;
  22. if (!BN_copy(pi, Xpi))
  23. return 0;
  24. if (!BN_is_odd(pi) && !BN_add_word(pi, 1))
  25. return 0;
  26. for (;;) {
  27. i++;
  28. BN_GENCB_call(cb, 0, i);
  29. /* NB 27 MR is specified in X9.31 */
  30. is_prime = BN_check_prime(pi, ctx, cb);
  31. if (is_prime < 0)
  32. return 0;
  33. if (is_prime)
  34. break;
  35. if (!BN_add_word(pi, 2))
  36. return 0;
  37. }
  38. BN_GENCB_call(cb, 2, i);
  39. return 1;
  40. }
  41. /*
  42. * This is the main X9.31 prime derivation function. From parameters Xp1, Xp2
  43. * and Xp derive the prime p. If the parameters p1 or p2 are not NULL they
  44. * will be returned too: this is needed for testing.
  45. */
  46. int BN_X931_derive_prime_ex(BIGNUM *p, BIGNUM *p1, BIGNUM *p2,
  47. const BIGNUM *Xp, const BIGNUM *Xp1,
  48. const BIGNUM *Xp2, const BIGNUM *e, BN_CTX *ctx,
  49. BN_GENCB *cb)
  50. {
  51. int ret = 0;
  52. BIGNUM *t, *p1p2, *pm1;
  53. /* Only even e supported */
  54. if (!BN_is_odd(e))
  55. return 0;
  56. BN_CTX_start(ctx);
  57. if (p1 == NULL)
  58. p1 = BN_CTX_get(ctx);
  59. if (p2 == NULL)
  60. p2 = BN_CTX_get(ctx);
  61. t = BN_CTX_get(ctx);
  62. p1p2 = BN_CTX_get(ctx);
  63. pm1 = BN_CTX_get(ctx);
  64. if (pm1 == NULL)
  65. goto err;
  66. if (!bn_x931_derive_pi(p1, Xp1, ctx, cb))
  67. goto err;
  68. if (!bn_x931_derive_pi(p2, Xp2, ctx, cb))
  69. goto err;
  70. if (!BN_mul(p1p2, p1, p2, ctx))
  71. goto err;
  72. /* First set p to value of Rp */
  73. if (!BN_mod_inverse(p, p2, p1, ctx))
  74. goto err;
  75. if (!BN_mul(p, p, p2, ctx))
  76. goto err;
  77. if (!BN_mod_inverse(t, p1, p2, ctx))
  78. goto err;
  79. if (!BN_mul(t, t, p1, ctx))
  80. goto err;
  81. if (!BN_sub(p, p, t))
  82. goto err;
  83. if (p->neg && !BN_add(p, p, p1p2))
  84. goto err;
  85. /* p now equals Rp */
  86. if (!BN_mod_sub(p, p, Xp, p1p2, ctx))
  87. goto err;
  88. if (!BN_add(p, p, Xp))
  89. goto err;
  90. /* p now equals Yp0 */
  91. for (;;) {
  92. int i = 1;
  93. BN_GENCB_call(cb, 0, i++);
  94. if (!BN_copy(pm1, p))
  95. goto err;
  96. if (!BN_sub_word(pm1, 1))
  97. goto err;
  98. if (!BN_gcd(t, pm1, e, ctx))
  99. goto err;
  100. if (BN_is_one(t)) {
  101. /*
  102. * X9.31 specifies 8 MR and 1 Lucas test or any prime test
  103. * offering similar or better guarantees 50 MR is considerably
  104. * better.
  105. */
  106. int r = BN_check_prime(p, ctx, cb);
  107. if (r < 0)
  108. goto err;
  109. if (r)
  110. break;
  111. }
  112. if (!BN_add(p, p, p1p2))
  113. goto err;
  114. }
  115. BN_GENCB_call(cb, 3, 0);
  116. ret = 1;
  117. err:
  118. BN_CTX_end(ctx);
  119. return ret;
  120. }
  121. /*
  122. * Generate pair of parameters Xp, Xq for X9.31 prime generation. Note: nbits
  123. * parameter is sum of number of bits in both.
  124. */
  125. int BN_X931_generate_Xpq(BIGNUM *Xp, BIGNUM *Xq, int nbits, BN_CTX *ctx)
  126. {
  127. BIGNUM *t;
  128. int i;
  129. /*
  130. * Number of bits for each prime is of the form 512+128s for s = 0, 1,
  131. * ...
  132. */
  133. if ((nbits < 1024) || (nbits & 0xff))
  134. return 0;
  135. nbits >>= 1;
  136. /*
  137. * The random value Xp must be between sqrt(2) * 2^(nbits-1) and 2^nbits
  138. * - 1. By setting the top two bits we ensure that the lower bound is
  139. * exceeded.
  140. */
  141. if (!BN_priv_rand_ex(Xp, nbits, BN_RAND_TOP_TWO, BN_RAND_BOTTOM_ANY, 0,
  142. ctx))
  143. return 0;
  144. BN_CTX_start(ctx);
  145. t = BN_CTX_get(ctx);
  146. if (t == NULL)
  147. goto err;
  148. for (i = 0; i < 1000; i++) {
  149. if (!BN_priv_rand_ex(Xq, nbits, BN_RAND_TOP_TWO, BN_RAND_BOTTOM_ANY, 0,
  150. ctx))
  151. goto err;
  152. /* Check that |Xp - Xq| > 2^(nbits - 100) */
  153. if (!BN_sub(t, Xp, Xq))
  154. goto err;
  155. if (BN_num_bits(t) > (nbits - 100))
  156. break;
  157. }
  158. BN_CTX_end(ctx);
  159. if (i < 1000)
  160. return 1;
  161. return 0;
  162. err:
  163. BN_CTX_end(ctx);
  164. return 0;
  165. }
  166. /*
  167. * Generate primes using X9.31 algorithm. Of the values p, p1, p2, Xp1 and
  168. * Xp2 only 'p' needs to be non-NULL. If any of the others are not NULL the
  169. * relevant parameter will be stored in it. Due to the fact that |Xp - Xq| >
  170. * 2^(nbits - 100) must be satisfied Xp and Xq are generated using the
  171. * previous function and supplied as input.
  172. */
  173. int BN_X931_generate_prime_ex(BIGNUM *p, BIGNUM *p1, BIGNUM *p2,
  174. BIGNUM *Xp1, BIGNUM *Xp2,
  175. const BIGNUM *Xp,
  176. const BIGNUM *e, BN_CTX *ctx, BN_GENCB *cb)
  177. {
  178. int ret = 0;
  179. BN_CTX_start(ctx);
  180. if (Xp1 == NULL)
  181. Xp1 = BN_CTX_get(ctx);
  182. if (Xp2 == NULL)
  183. Xp2 = BN_CTX_get(ctx);
  184. if (Xp1 == NULL || Xp2 == NULL)
  185. goto error;
  186. if (!BN_priv_rand_ex(Xp1, 101, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY, 0, ctx))
  187. goto error;
  188. if (!BN_priv_rand_ex(Xp2, 101, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY, 0, ctx))
  189. goto error;
  190. if (!BN_X931_derive_prime_ex(p, p1, p2, Xp, Xp1, Xp2, e, ctx, cb))
  191. goto error;
  192. ret = 1;
  193. error:
  194. BN_CTX_end(ctx);
  195. return ret;
  196. }