Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/fipsmodule/bn/cmp.c

raw
1 // Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 #include <openssl/bn.h>
5
6 #include <openssl/mem.h>
7 #include <openssl/type_check.h>
8
9 #include "internal.h"
10 #include "../../internal.h"
11
12
13 static int bn_cmp_words_consttime(const BN_ULONG *a, size_t a_len,
14 const BN_ULONG *b, size_t b_len) {
15 OPENSSL_STATIC_ASSERT(sizeof(BN_ULONG) <= sizeof(crypto_word_t),
16 crypto_word_t_is_too_small)
17 int ret = 0;
18 // Process the common words in little-endian order.
19 size_t min = a_len < b_len ? a_len : b_len;
20 for (size_t i = 0; i < min; i++) {
21 crypto_word_t eq = constant_time_eq_w(a[i], b[i]);
22 crypto_word_t lt = constant_time_lt_w(a[i], b[i]);
23 ret =
24 constant_time_select_int(eq, ret, constant_time_select_int(lt, -1, 1));
25 }
26
27 // If |a| or |b| has non-zero words beyond |min|, they take precedence.
28 if (a_len < b_len) {
29 crypto_word_t mask = 0;
30 for (size_t i = a_len; i < b_len; i++) {
31 mask |= b[i];
32 }
33 ret = constant_time_select_int(constant_time_is_zero_w(mask), ret, -1);
34 } else if (b_len < a_len) {
35 crypto_word_t mask = 0;
36 for (size_t i = b_len; i < a_len; i++) {
37 mask |= a[i];
38 }
39 ret = constant_time_select_int(constant_time_is_zero_w(mask), ret, 1);
40 }
41
42 return ret;
43 }
44
45 int BN_ucmp(const BIGNUM *a, const BIGNUM *b) {
46 return bn_cmp_words_consttime(a->d, a->width, b->d, b->width);
47 }
48
49 int BN_cmp(const BIGNUM *a, const BIGNUM *b) {
50 if ((a == NULL) || (b == NULL)) {
51 if (a != NULL) {
52 return -1;
53 } else if (b != NULL) {
54 return 1;
55 } else {
56 return 0;
57 }
58 }
59
60 // We do not attempt to process the sign bit in constant time. Negative
61 // |BIGNUM|s should never occur in crypto, only calculators.
62 if (a->neg != b->neg) {
63 if (a->neg) {
64 return -1;
65 }
66 return 1;
67 }
68
69 int ret = BN_ucmp(a, b);
70 return a->neg ? -ret : ret;
71 }
72
73 int bn_less_than_words(const BN_ULONG *a, const BN_ULONG *b, size_t len) {
74 return bn_cmp_words_consttime(a, len, b, len) < 0;
75 }
76
77 int BN_abs_is_word(const BIGNUM *bn, BN_ULONG w) {
78 if (bn->width == 0) {
79 return w == 0;
80 }
81 BN_ULONG mask = bn->d[0] ^ w;
82 for (int i = 1; i < bn->width; i++) {
83 mask |= bn->d[i];
84 }
85 return mask == 0;
86 }
87
88 int BN_cmp_word(const BIGNUM *a, BN_ULONG b) {
89 BIGNUM b_bn;
90 BN_init(&b_bn);
91
92 b_bn.d = &b;
93 b_bn.width = b > 0;
94 b_bn.dmax = 1;
95 b_bn.flags = BN_FLG_STATIC_DATA;
96 return BN_cmp(a, &b_bn);
97 }
98
99 int BN_is_zero(const BIGNUM *bn) {
100 return bn_fits_in_words(bn, 0);
101 }
102
103 int BN_is_one(const BIGNUM *bn) {
104 return bn->neg == 0 && BN_abs_is_word(bn, 1);
105 }
106
107 int BN_is_word(const BIGNUM *bn, BN_ULONG w) {
108 return BN_abs_is_word(bn, w) && (w == 0 || bn->neg == 0);
109 }
110
111 int BN_is_odd(const BIGNUM *bn) {
112 return bn->width > 0 && (bn->d[0] & 1) == 1;
113 }
114
115 int BN_is_pow2(const BIGNUM *bn) {
116 int width = bn_minimal_width(bn);
117 if (width == 0 || bn->neg) {
118 return 0;
119 }
120
121 for (int i = 0; i < width - 1; i++) {
122 if (bn->d[i] != 0) {
123 return 0;
124 }
125 }
126
127 return 0 == (bn->d[width-1] & (bn->d[width-1] - 1));
128 }
129
130 int BN_equal_consttime(const BIGNUM *a, const BIGNUM *b) {
131 BN_ULONG mask = 0;
132 // If |a| or |b| has more words than the other, all those words must be zero.
133 for (int i = a->width; i < b->width; i++) {
134 mask |= b->d[i];
135 }
136 for (int i = b->width; i < a->width; i++) {
137 mask |= a->d[i];
138 }
139 // Common words must match.
140 int min = a->width < b->width ? a->width : b->width;
141 for (int i = 0; i < min; i++) {
142 mask |= (a->d[i] ^ b->d[i]);
143 }
144 // The sign bit must match.
145 mask |= (a->neg ^ b->neg);
146 return mask == 0;
147 }