/src/openssl33/crypto/bn/bn_word.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 1995-2016 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 | | |
10 | | #include "internal/cryptlib.h" |
11 | | #include "bn_local.h" |
12 | | |
13 | | BN_ULONG BN_mod_word(const BIGNUM *a, BN_ULONG w) |
14 | 486k | { |
15 | 486k | #ifndef BN_LLONG |
16 | 486k | BN_ULONG ret = 0; |
17 | | #else |
18 | | BN_ULLONG ret = 0; |
19 | | #endif |
20 | 486k | int i; |
21 | | |
22 | 486k | if (w == 0) |
23 | 0 | return (BN_ULONG)-1; |
24 | | |
25 | 486k | #ifndef BN_LLONG |
26 | | /* |
27 | | * If |w| is too long and we don't have BN_ULLONG then we need to fall |
28 | | * back to using BN_div_word |
29 | | */ |
30 | 486k | if (w > ((BN_ULONG)1 << BN_BITS4)) { |
31 | 0 | BIGNUM *tmp = BN_dup(a); |
32 | 0 | if (tmp == NULL) |
33 | 0 | return (BN_ULONG)-1; |
34 | | |
35 | 0 | ret = BN_div_word(tmp, w); |
36 | 0 | BN_free(tmp); |
37 | |
|
38 | 0 | return ret; |
39 | 0 | } |
40 | 486k | #endif |
41 | | |
42 | 486k | bn_check_top(a); |
43 | 486k | w &= BN_MASK2; |
44 | 11.6M | for (i = a->top - 1; i >= 0; i--) { |
45 | 11.1M | #ifndef BN_LLONG |
46 | | /* |
47 | | * We can assume here that | w <= ((BN_ULONG)1 << BN_BITS4) | and so |
48 | | * | ret < ((BN_ULONG)1 << BN_BITS4) | and therefore the shifts here are |
49 | | * safe and will not overflow |
50 | | */ |
51 | 11.1M | ret = ((ret << BN_BITS4) | ((a->d[i] >> BN_BITS4) & BN_MASK2l)) % w; |
52 | 11.1M | ret = ((ret << BN_BITS4) | (a->d[i] & BN_MASK2l)) % w; |
53 | | #else |
54 | | ret = (BN_ULLONG)(((ret << (BN_ULLONG)BN_BITS2) | a->d[i]) % (BN_ULLONG)w); |
55 | | #endif |
56 | 11.1M | } |
57 | 486k | return (BN_ULONG)ret; |
58 | 486k | } |
59 | | |
60 | | BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w) |
61 | 2.57M | { |
62 | 2.57M | BN_ULONG ret = 0; |
63 | 2.57M | int i, j; |
64 | | |
65 | 2.57M | bn_check_top(a); |
66 | 2.57M | w &= BN_MASK2; |
67 | | |
68 | 2.57M | if (!w) |
69 | | /* actually this an error (division by zero) */ |
70 | 0 | return (BN_ULONG)-1; |
71 | 2.57M | if (a->top == 0) |
72 | 0 | return 0; |
73 | | |
74 | | /* normalize input (so bn_div_words doesn't complain) */ |
75 | 2.57M | j = BN_BITS2 - BN_num_bits_word(w); |
76 | 2.57M | w <<= j; |
77 | 2.57M | if (!BN_lshift(a, a, j)) |
78 | 0 | return (BN_ULONG)-1; |
79 | | |
80 | 6.45M | for (i = a->top - 1; i >= 0; i--) { |
81 | 3.88M | BN_ULONG l, d; |
82 | | |
83 | 3.88M | l = a->d[i]; |
84 | 3.88M | d = bn_div_words(ret, l, w); |
85 | 3.88M | ret = (l - ((d * w) & BN_MASK2)) & BN_MASK2; |
86 | 3.88M | a->d[i] = d; |
87 | 3.88M | } |
88 | 2.57M | if ((a->top > 0) && (a->d[a->top - 1] == 0)) |
89 | 2.55M | a->top--; |
90 | 2.57M | ret >>= j; |
91 | 2.57M | if (!a->top) |
92 | 2.22M | a->neg = 0; /* don't allow negative zero */ |
93 | 2.57M | bn_check_top(a); |
94 | 2.57M | return ret; |
95 | 2.57M | } |
96 | | |
97 | | int BN_add_word(BIGNUM *a, BN_ULONG w) |
98 | 3.43M | { |
99 | 3.43M | BN_ULONG l; |
100 | 3.43M | int i; |
101 | | |
102 | 3.43M | bn_check_top(a); |
103 | 3.43M | w &= BN_MASK2; |
104 | | |
105 | | /* degenerate case: w is zero */ |
106 | 3.43M | if (!w) |
107 | 441k | return 1; |
108 | | /* degenerate case: a is zero */ |
109 | 2.99M | if (BN_is_zero(a)) |
110 | 48.2k | return BN_set_word(a, w); |
111 | | /* handle 'a' when negative */ |
112 | 2.94M | if (a->neg) { |
113 | 0 | a->neg = 0; |
114 | 0 | i = BN_sub_word(a, w); |
115 | 0 | if (!BN_is_zero(a)) |
116 | 0 | a->neg = !(a->neg); |
117 | 0 | return i; |
118 | 0 | } |
119 | 6.16M | for (i = 0; w != 0 && i < a->top; i++) { |
120 | 3.22M | a->d[i] = l = (a->d[i] + w) & BN_MASK2; |
121 | 3.22M | w = (w > l) ? 1 : 0; |
122 | 3.22M | } |
123 | 2.94M | if (w && i == a->top) { |
124 | 2.16k | if (bn_wexpand(a, a->top + 1) == NULL) |
125 | 0 | return 0; |
126 | 2.16k | a->top++; |
127 | 2.16k | a->d[i] = w; |
128 | 2.16k | } |
129 | 2.94M | bn_check_top(a); |
130 | 2.94M | return 1; |
131 | 2.94M | } |
132 | | |
133 | | int BN_sub_word(BIGNUM *a, BN_ULONG w) |
134 | 1.18M | { |
135 | 1.18M | int i; |
136 | | |
137 | 1.18M | bn_check_top(a); |
138 | 1.18M | w &= BN_MASK2; |
139 | | |
140 | | /* degenerate case: w is zero */ |
141 | 1.18M | if (!w) |
142 | 0 | return 1; |
143 | | /* degenerate case: a is zero */ |
144 | 1.18M | if (BN_is_zero(a)) { |
145 | 811 | i = BN_set_word(a, w); |
146 | 811 | if (i != 0) |
147 | 811 | BN_set_negative(a, 1); |
148 | 811 | return i; |
149 | 811 | } |
150 | | /* handle 'a' when negative */ |
151 | 1.18M | if (a->neg) { |
152 | 0 | a->neg = 0; |
153 | 0 | i = BN_add_word(a, w); |
154 | 0 | a->neg = 1; |
155 | 0 | return i; |
156 | 0 | } |
157 | | |
158 | 1.18M | if ((a->top == 1) && (a->d[0] < w)) { |
159 | 15 | a->d[0] = w - a->d[0]; |
160 | 15 | a->neg = 1; |
161 | 15 | return 1; |
162 | 15 | } |
163 | 1.18M | i = 0; |
164 | 2.29M | for (;;) { |
165 | 2.29M | if (a->d[i] >= w) { |
166 | 1.18M | a->d[i] -= w; |
167 | 1.18M | break; |
168 | 1.18M | } else { |
169 | 1.11M | a->d[i] = (a->d[i] - w) & BN_MASK2; |
170 | 1.11M | i++; |
171 | 1.11M | w = 1; |
172 | 1.11M | } |
173 | 2.29M | } |
174 | 1.18M | if ((a->d[i] == 0) && (i == (a->top - 1))) |
175 | 189k | a->top--; |
176 | 1.18M | bn_check_top(a); |
177 | 1.18M | return 1; |
178 | 1.18M | } |
179 | | |
180 | | int BN_mul_word(BIGNUM *a, BN_ULONG w) |
181 | 5.23M | { |
182 | 5.23M | BN_ULONG ll; |
183 | | |
184 | 5.23M | bn_check_top(a); |
185 | 5.23M | w &= BN_MASK2; |
186 | 5.23M | if (a->top) { |
187 | 5.23M | if (w == 0) |
188 | 0 | BN_zero(a); |
189 | 5.23M | else { |
190 | 5.23M | ll = bn_mul_words(a->d, a->d, a->top, w); |
191 | 5.23M | if (ll) { |
192 | 5.07M | if (bn_wexpand(a, a->top + 1) == NULL) |
193 | 0 | return 0; |
194 | 5.07M | a->d[a->top++] = ll; |
195 | 5.07M | } |
196 | 5.23M | } |
197 | 5.23M | } |
198 | 5.23M | bn_check_top(a); |
199 | 5.23M | return 1; |
200 | 5.23M | } |