bn_recp.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. #include "internal/cryptlib.h"
  10. #include "bn_local.h"
  11. void BN_RECP_CTX_init(BN_RECP_CTX *recp)
  12. {
  13. memset(recp, 0, sizeof(*recp));
  14. bn_init(&(recp->N));
  15. bn_init(&(recp->Nr));
  16. }
  17. BN_RECP_CTX *BN_RECP_CTX_new(void)
  18. {
  19. BN_RECP_CTX *ret;
  20. if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {
  21. BNerr(BN_F_BN_RECP_CTX_NEW, ERR_R_MALLOC_FAILURE);
  22. return NULL;
  23. }
  24. bn_init(&(ret->N));
  25. bn_init(&(ret->Nr));
  26. ret->flags = BN_FLG_MALLOCED;
  27. return ret;
  28. }
  29. void BN_RECP_CTX_free(BN_RECP_CTX *recp)
  30. {
  31. if (recp == NULL)
  32. return;
  33. BN_free(&recp->N);
  34. BN_free(&recp->Nr);
  35. if (recp->flags & BN_FLG_MALLOCED)
  36. OPENSSL_free(recp);
  37. }
  38. int BN_RECP_CTX_set(BN_RECP_CTX *recp, const BIGNUM *d, BN_CTX *ctx)
  39. {
  40. if (!BN_copy(&(recp->N), d))
  41. return 0;
  42. BN_zero(&(recp->Nr));
  43. recp->num_bits = BN_num_bits(d);
  44. recp->shift = 0;
  45. return 1;
  46. }
  47. int BN_mod_mul_reciprocal(BIGNUM *r, const BIGNUM *x, const BIGNUM *y,
  48. BN_RECP_CTX *recp, BN_CTX *ctx)
  49. {
  50. int ret = 0;
  51. BIGNUM *a;
  52. const BIGNUM *ca;
  53. BN_CTX_start(ctx);
  54. if ((a = BN_CTX_get(ctx)) == NULL)
  55. goto err;
  56. if (y != NULL) {
  57. if (x == y) {
  58. if (!BN_sqr(a, x, ctx))
  59. goto err;
  60. } else {
  61. if (!BN_mul(a, x, y, ctx))
  62. goto err;
  63. }
  64. ca = a;
  65. } else
  66. ca = x; /* Just do the mod */
  67. ret = BN_div_recp(NULL, r, ca, recp, ctx);
  68. err:
  69. BN_CTX_end(ctx);
  70. bn_check_top(r);
  71. return ret;
  72. }
  73. int BN_div_recp(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m,
  74. BN_RECP_CTX *recp, BN_CTX *ctx)
  75. {
  76. int i, j, ret = 0;
  77. BIGNUM *a, *b, *d, *r;
  78. BN_CTX_start(ctx);
  79. d = (dv != NULL) ? dv : BN_CTX_get(ctx);
  80. r = (rem != NULL) ? rem : BN_CTX_get(ctx);
  81. a = BN_CTX_get(ctx);
  82. b = BN_CTX_get(ctx);
  83. if (b == NULL)
  84. goto err;
  85. if (BN_ucmp(m, &(recp->N)) < 0) {
  86. BN_zero(d);
  87. if (!BN_copy(r, m)) {
  88. BN_CTX_end(ctx);
  89. return 0;
  90. }
  91. BN_CTX_end(ctx);
  92. return 1;
  93. }
  94. /*
  95. * We want the remainder Given input of ABCDEF / ab we need multiply
  96. * ABCDEF by 3 digests of the reciprocal of ab
  97. */
  98. /* i := max(BN_num_bits(m), 2*BN_num_bits(N)) */
  99. i = BN_num_bits(m);
  100. j = recp->num_bits << 1;
  101. if (j > i)
  102. i = j;
  103. /* Nr := round(2^i / N) */
  104. if (i != recp->shift)
  105. recp->shift = BN_reciprocal(&(recp->Nr), &(recp->N), i, ctx);
  106. /* BN_reciprocal could have returned -1 for an error */
  107. if (recp->shift == -1)
  108. goto err;
  109. /*-
  110. * d := |round(round(m / 2^BN_num_bits(N)) * recp->Nr / 2^(i - BN_num_bits(N)))|
  111. * = |round(round(m / 2^BN_num_bits(N)) * round(2^i / N) / 2^(i - BN_num_bits(N)))|
  112. * <= |(m / 2^BN_num_bits(N)) * (2^i / N) * (2^BN_num_bits(N) / 2^i)|
  113. * = |m/N|
  114. */
  115. if (!BN_rshift(a, m, recp->num_bits))
  116. goto err;
  117. if (!BN_mul(b, a, &(recp->Nr), ctx))
  118. goto err;
  119. if (!BN_rshift(d, b, i - recp->num_bits))
  120. goto err;
  121. d->neg = 0;
  122. if (!BN_mul(b, &(recp->N), d, ctx))
  123. goto err;
  124. if (!BN_usub(r, m, b))
  125. goto err;
  126. r->neg = 0;
  127. j = 0;
  128. while (BN_ucmp(r, &(recp->N)) >= 0) {
  129. if (j++ > 2) {
  130. BNerr(BN_F_BN_DIV_RECP, BN_R_BAD_RECIPROCAL);
  131. goto err;
  132. }
  133. if (!BN_usub(r, r, &(recp->N)))
  134. goto err;
  135. if (!BN_add_word(d, 1))
  136. goto err;
  137. }
  138. r->neg = BN_is_zero(r) ? 0 : m->neg;
  139. d->neg = m->neg ^ recp->N.neg;
  140. ret = 1;
  141. err:
  142. BN_CTX_end(ctx);
  143. bn_check_top(dv);
  144. bn_check_top(rem);
  145. return ret;
  146. }
  147. /*
  148. * len is the expected size of the result We actually calculate with an extra
  149. * word of precision, so we can do faster division if the remainder is not
  150. * required.
  151. */
  152. /* r := 2^len / m */
  153. int BN_reciprocal(BIGNUM *r, const BIGNUM *m, int len, BN_CTX *ctx)
  154. {
  155. int ret = -1;
  156. BIGNUM *t;
  157. BN_CTX_start(ctx);
  158. if ((t = BN_CTX_get(ctx)) == NULL)
  159. goto err;
  160. if (!BN_set_bit(t, len))
  161. goto err;
  162. if (!BN_div(r, NULL, t, m, ctx))
  163. goto err;
  164. ret = len;
  165. err:
  166. bn_check_top(r);
  167. BN_CTX_end(ctx);
  168. return ret;
  169. }