2
0

bn_x931p.c 5.7 KB

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