2
0

bn_exp2.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Copyright 1995-2017 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 "internal/cryptlib.h"
  11. #include "bn_lcl.h"
  12. #define TABLE_SIZE 32
  13. int BN_mod_exp2_mont(BIGNUM *rr, const BIGNUM *a1, const BIGNUM *p1,
  14. const BIGNUM *a2, const BIGNUM *p2, const BIGNUM *m,
  15. BN_CTX *ctx, BN_MONT_CTX *in_mont)
  16. {
  17. int i, j, bits, b, bits1, bits2, ret =
  18. 0, wpos1, wpos2, window1, window2, wvalue1, wvalue2;
  19. int r_is_one = 1;
  20. BIGNUM *d, *r;
  21. const BIGNUM *a_mod_m;
  22. /* Tables of variables obtained from 'ctx' */
  23. BIGNUM *val1[TABLE_SIZE], *val2[TABLE_SIZE];
  24. BN_MONT_CTX *mont = NULL;
  25. bn_check_top(a1);
  26. bn_check_top(p1);
  27. bn_check_top(a2);
  28. bn_check_top(p2);
  29. bn_check_top(m);
  30. if (!(m->d[0] & 1)) {
  31. BNerr(BN_F_BN_MOD_EXP2_MONT, BN_R_CALLED_WITH_EVEN_MODULUS);
  32. return 0;
  33. }
  34. bits1 = BN_num_bits(p1);
  35. bits2 = BN_num_bits(p2);
  36. if ((bits1 == 0) && (bits2 == 0)) {
  37. ret = BN_one(rr);
  38. return ret;
  39. }
  40. bits = (bits1 > bits2) ? bits1 : bits2;
  41. BN_CTX_start(ctx);
  42. d = BN_CTX_get(ctx);
  43. r = BN_CTX_get(ctx);
  44. val1[0] = BN_CTX_get(ctx);
  45. val2[0] = BN_CTX_get(ctx);
  46. if (val2[0] == NULL)
  47. goto err;
  48. if (in_mont != NULL)
  49. mont = in_mont;
  50. else {
  51. if ((mont = BN_MONT_CTX_new()) == NULL)
  52. goto err;
  53. if (!BN_MONT_CTX_set(mont, m, ctx))
  54. goto err;
  55. }
  56. window1 = BN_window_bits_for_exponent_size(bits1);
  57. window2 = BN_window_bits_for_exponent_size(bits2);
  58. /*
  59. * Build table for a1: val1[i] := a1^(2*i + 1) mod m for i = 0 .. 2^(window1-1)
  60. */
  61. if (a1->neg || BN_ucmp(a1, m) >= 0) {
  62. if (!BN_mod(val1[0], a1, m, ctx))
  63. goto err;
  64. a_mod_m = val1[0];
  65. } else
  66. a_mod_m = a1;
  67. if (BN_is_zero(a_mod_m)) {
  68. BN_zero(rr);
  69. ret = 1;
  70. goto err;
  71. }
  72. if (!BN_to_montgomery(val1[0], a_mod_m, mont, ctx))
  73. goto err;
  74. if (window1 > 1) {
  75. if (!BN_mod_mul_montgomery(d, val1[0], val1[0], mont, ctx))
  76. goto err;
  77. j = 1 << (window1 - 1);
  78. for (i = 1; i < j; i++) {
  79. if (((val1[i] = BN_CTX_get(ctx)) == NULL) ||
  80. !BN_mod_mul_montgomery(val1[i], val1[i - 1], d, mont, ctx))
  81. goto err;
  82. }
  83. }
  84. /*
  85. * Build table for a2: val2[i] := a2^(2*i + 1) mod m for i = 0 .. 2^(window2-1)
  86. */
  87. if (a2->neg || BN_ucmp(a2, m) >= 0) {
  88. if (!BN_mod(val2[0], a2, m, ctx))
  89. goto err;
  90. a_mod_m = val2[0];
  91. } else
  92. a_mod_m = a2;
  93. if (BN_is_zero(a_mod_m)) {
  94. BN_zero(rr);
  95. ret = 1;
  96. goto err;
  97. }
  98. if (!BN_to_montgomery(val2[0], a_mod_m, mont, ctx))
  99. goto err;
  100. if (window2 > 1) {
  101. if (!BN_mod_mul_montgomery(d, val2[0], val2[0], mont, ctx))
  102. goto err;
  103. j = 1 << (window2 - 1);
  104. for (i = 1; i < j; i++) {
  105. if (((val2[i] = BN_CTX_get(ctx)) == NULL) ||
  106. !BN_mod_mul_montgomery(val2[i], val2[i - 1], d, mont, ctx))
  107. goto err;
  108. }
  109. }
  110. /* Now compute the power product, using independent windows. */
  111. r_is_one = 1;
  112. wvalue1 = 0; /* The 'value' of the first window */
  113. wvalue2 = 0; /* The 'value' of the second window */
  114. wpos1 = 0; /* If wvalue1 > 0, the bottom bit of the
  115. * first window */
  116. wpos2 = 0; /* If wvalue2 > 0, the bottom bit of the
  117. * second window */
  118. if (!BN_to_montgomery(r, BN_value_one(), mont, ctx))
  119. goto err;
  120. for (b = bits - 1; b >= 0; b--) {
  121. if (!r_is_one) {
  122. if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))
  123. goto err;
  124. }
  125. if (!wvalue1)
  126. if (BN_is_bit_set(p1, b)) {
  127. /*
  128. * consider bits b-window1+1 .. b for this window
  129. */
  130. i = b - window1 + 1;
  131. while (!BN_is_bit_set(p1, i)) /* works for i<0 */
  132. i++;
  133. wpos1 = i;
  134. wvalue1 = 1;
  135. for (i = b - 1; i >= wpos1; i--) {
  136. wvalue1 <<= 1;
  137. if (BN_is_bit_set(p1, i))
  138. wvalue1++;
  139. }
  140. }
  141. if (!wvalue2)
  142. if (BN_is_bit_set(p2, b)) {
  143. /*
  144. * consider bits b-window2+1 .. b for this window
  145. */
  146. i = b - window2 + 1;
  147. while (!BN_is_bit_set(p2, i))
  148. i++;
  149. wpos2 = i;
  150. wvalue2 = 1;
  151. for (i = b - 1; i >= wpos2; i--) {
  152. wvalue2 <<= 1;
  153. if (BN_is_bit_set(p2, i))
  154. wvalue2++;
  155. }
  156. }
  157. if (wvalue1 && b == wpos1) {
  158. /* wvalue1 is odd and < 2^window1 */
  159. if (!BN_mod_mul_montgomery(r, r, val1[wvalue1 >> 1], mont, ctx))
  160. goto err;
  161. wvalue1 = 0;
  162. r_is_one = 0;
  163. }
  164. if (wvalue2 && b == wpos2) {
  165. /* wvalue2 is odd and < 2^window2 */
  166. if (!BN_mod_mul_montgomery(r, r, val2[wvalue2 >> 1], mont, ctx))
  167. goto err;
  168. wvalue2 = 0;
  169. r_is_one = 0;
  170. }
  171. }
  172. if (!BN_from_montgomery(rr, r, mont, ctx))
  173. goto err;
  174. ret = 1;
  175. err:
  176. if (in_mont == NULL)
  177. BN_MONT_CTX_free(mont);
  178. BN_CTX_end(ctx);
  179. bn_check_top(rr);
  180. return ret;
  181. }