You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

215 lines
5.7 KiB

  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.] */
  56. #include <openssl/bn.h>
  57. #include <openssl/mem.h>
  58. #include "internal.h"
  59. int BN_ucmp(const BIGNUM *a, const BIGNUM *b) {
  60. int i;
  61. BN_ULONG t1, t2, *ap, *bp;
  62. i = a->top - b->top;
  63. if (i != 0) {
  64. return i;
  65. }
  66. ap = a->d;
  67. bp = b->d;
  68. for (i = a->top - 1; i >= 0; i--) {
  69. t1 = ap[i];
  70. t2 = bp[i];
  71. if (t1 != t2) {
  72. return (t1 > t2) ? 1 : -1;
  73. }
  74. }
  75. return 0;
  76. }
  77. int BN_cmp(const BIGNUM *a, const BIGNUM *b) {
  78. int i;
  79. int gt, lt;
  80. BN_ULONG t1, t2;
  81. if ((a == NULL) || (b == NULL)) {
  82. if (a != NULL) {
  83. return -1;
  84. } else if (b != NULL) {
  85. return 1;
  86. } else {
  87. return 0;
  88. }
  89. }
  90. if (a->neg != b->neg) {
  91. if (a->neg) {
  92. return -1;
  93. }
  94. return 1;
  95. }
  96. if (a->neg == 0) {
  97. gt = 1;
  98. lt = -1;
  99. } else {
  100. gt = -1;
  101. lt = 1;
  102. }
  103. if (a->top > b->top) {
  104. return gt;
  105. }
  106. if (a->top < b->top) {
  107. return lt;
  108. }
  109. for (i = a->top - 1; i >= 0; i--) {
  110. t1 = a->d[i];
  111. t2 = b->d[i];
  112. if (t1 > t2) {
  113. return gt;
  114. } if (t1 < t2) {
  115. return lt;
  116. }
  117. }
  118. return 0;
  119. }
  120. int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n) {
  121. int i;
  122. BN_ULONG aa, bb;
  123. aa = a[n - 1];
  124. bb = b[n - 1];
  125. if (aa != bb) {
  126. return (aa > bb) ? 1 : -1;
  127. }
  128. for (i = n - 2; i >= 0; i--) {
  129. aa = a[i];
  130. bb = b[i];
  131. if (aa != bb) {
  132. return (aa > bb) ? 1 : -1;
  133. }
  134. }
  135. return 0;
  136. }
  137. int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b, int cl, int dl) {
  138. int n, i;
  139. n = cl - 1;
  140. if (dl < 0) {
  141. for (i = dl; i < 0; i++) {
  142. if (b[n - i] != 0) {
  143. return -1; /* a < b */
  144. }
  145. }
  146. }
  147. if (dl > 0) {
  148. for (i = dl; i > 0; i--) {
  149. if (a[n + i] != 0) {
  150. return 1; /* a > b */
  151. }
  152. }
  153. }
  154. return bn_cmp_words(a, b, cl);
  155. }
  156. int BN_abs_is_word(const BIGNUM *bn, BN_ULONG w) {
  157. switch (bn->top) {
  158. case 1:
  159. return bn->d[0] == w;
  160. case 0:
  161. return w == 0;
  162. default:
  163. return 0;
  164. }
  165. }
  166. int BN_is_zero(const BIGNUM *bn) {
  167. return bn->top == 0;
  168. }
  169. int BN_is_one(const BIGNUM *bn) {
  170. return bn->neg == 0 && BN_abs_is_word(bn, 1);
  171. }
  172. int BN_is_word(const BIGNUM *bn, BN_ULONG w) {
  173. return BN_abs_is_word(bn, w) && (w == 0 || bn->neg == 0);
  174. }
  175. int BN_is_odd(const BIGNUM *bn) {
  176. return bn->top > 0 && (bn->d[0] & 1) == 1;
  177. }
  178. int BN_equal_consttime(const BIGNUM *a, const BIGNUM *b) {
  179. if (a->top != b->top) {
  180. return 0;
  181. }
  182. int limbs_are_equal =
  183. CRYPTO_memcmp(a->d, b->d, (size_t)a->top * sizeof(a->d[0])) == 0;
  184. return constant_time_select_int(constant_time_eq_int(a->neg, b->neg),
  185. limbs_are_equal, 0);
  186. }