cmp.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 <openssl/type_check.h>
  59. #include "internal.h"
  60. #include "../../internal.h"
  61. int BN_ucmp(const BIGNUM *a, const BIGNUM *b) {
  62. int i;
  63. BN_ULONG t1, t2, *ap, *bp;
  64. i = a->top - b->top;
  65. if (i != 0) {
  66. return i;
  67. }
  68. ap = a->d;
  69. bp = b->d;
  70. for (i = a->top - 1; i >= 0; i--) {
  71. t1 = ap[i];
  72. t2 = bp[i];
  73. if (t1 != t2) {
  74. return (t1 > t2) ? 1 : -1;
  75. }
  76. }
  77. return 0;
  78. }
  79. int BN_cmp(const BIGNUM *a, const BIGNUM *b) {
  80. int i;
  81. int gt, lt;
  82. BN_ULONG t1, t2;
  83. if ((a == NULL) || (b == NULL)) {
  84. if (a != NULL) {
  85. return -1;
  86. } else if (b != NULL) {
  87. return 1;
  88. } else {
  89. return 0;
  90. }
  91. }
  92. if (a->neg != b->neg) {
  93. if (a->neg) {
  94. return -1;
  95. }
  96. return 1;
  97. }
  98. if (a->neg == 0) {
  99. gt = 1;
  100. lt = -1;
  101. } else {
  102. gt = -1;
  103. lt = 1;
  104. }
  105. if (a->top > b->top) {
  106. return gt;
  107. }
  108. if (a->top < b->top) {
  109. return lt;
  110. }
  111. for (i = a->top - 1; i >= 0; i--) {
  112. t1 = a->d[i];
  113. t2 = b->d[i];
  114. if (t1 > t2) {
  115. return gt;
  116. } if (t1 < t2) {
  117. return lt;
  118. }
  119. }
  120. return 0;
  121. }
  122. int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n) {
  123. int i;
  124. BN_ULONG aa, bb;
  125. aa = a[n - 1];
  126. bb = b[n - 1];
  127. if (aa != bb) {
  128. return (aa > bb) ? 1 : -1;
  129. }
  130. for (i = n - 2; i >= 0; i--) {
  131. aa = a[i];
  132. bb = b[i];
  133. if (aa != bb) {
  134. return (aa > bb) ? 1 : -1;
  135. }
  136. }
  137. return 0;
  138. }
  139. int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b, int cl, int dl) {
  140. int n, i;
  141. n = cl - 1;
  142. if (dl < 0) {
  143. for (i = dl; i < 0; i++) {
  144. if (b[n - i] != 0) {
  145. return -1; // a < b
  146. }
  147. }
  148. }
  149. if (dl > 0) {
  150. for (i = dl; i > 0; i--) {
  151. if (a[n + i] != 0) {
  152. return 1; // a > b
  153. }
  154. }
  155. }
  156. return bn_cmp_words(a, b, cl);
  157. }
  158. int bn_less_than_words(const BN_ULONG *a, const BN_ULONG *b, size_t len) {
  159. OPENSSL_COMPILE_ASSERT(sizeof(BN_ULONG) <= sizeof(crypto_word_t),
  160. crypto_word_t_too_small);
  161. int ret = 0;
  162. // Process the words in little-endian order.
  163. for (size_t i = 0; i < len; i++) {
  164. crypto_word_t eq = constant_time_eq_w(a[i], b[i]);
  165. crypto_word_t lt = constant_time_lt_w(a[i], b[i]);
  166. ret = constant_time_select_int(eq, ret, constant_time_select_int(lt, 1, 0));
  167. }
  168. return ret;
  169. }
  170. int BN_abs_is_word(const BIGNUM *bn, BN_ULONG w) {
  171. switch (bn->top) {
  172. case 1:
  173. return bn->d[0] == w;
  174. case 0:
  175. return w == 0;
  176. default:
  177. return 0;
  178. }
  179. }
  180. int BN_cmp_word(const BIGNUM *a, BN_ULONG b) {
  181. BIGNUM b_bn;
  182. BN_init(&b_bn);
  183. b_bn.d = &b;
  184. b_bn.top = b > 0;
  185. b_bn.dmax = 1;
  186. b_bn.flags = BN_FLG_STATIC_DATA;
  187. return BN_cmp(a, &b_bn);
  188. }
  189. int BN_is_zero(const BIGNUM *bn) {
  190. return bn->top == 0;
  191. }
  192. int BN_is_one(const BIGNUM *bn) {
  193. return bn->neg == 0 && BN_abs_is_word(bn, 1);
  194. }
  195. int BN_is_word(const BIGNUM *bn, BN_ULONG w) {
  196. return BN_abs_is_word(bn, w) && (w == 0 || bn->neg == 0);
  197. }
  198. int BN_is_odd(const BIGNUM *bn) {
  199. return bn->top > 0 && (bn->d[0] & 1) == 1;
  200. }
  201. int BN_is_pow2(const BIGNUM *bn) {
  202. if (bn->top == 0 || bn->neg) {
  203. return 0;
  204. }
  205. for (int i = 0; i < bn->top - 1; i++) {
  206. if (bn->d[i] != 0) {
  207. return 0;
  208. }
  209. }
  210. return 0 == (bn->d[bn->top-1] & (bn->d[bn->top-1] - 1));
  211. }
  212. int BN_equal_consttime(const BIGNUM *a, const BIGNUM *b) {
  213. if (a->top != b->top) {
  214. return 0;
  215. }
  216. int limbs_are_equal =
  217. CRYPTO_memcmp(a->d, b->d, (size_t)a->top * sizeof(a->d[0])) == 0;
  218. return constant_time_select_int(constant_time_eq_int(a->neg, b->neg),
  219. limbs_are_equal, 0);
  220. }