/src/openssl30/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 | 439k | { |
15 | 439k | #ifndef BN_LLONG |
16 | 439k | BN_ULONG ret = 0; |
17 | | #else |
18 | | BN_ULLONG ret = 0; |
19 | | #endif |
20 | 439k | int i; |
21 | | |
22 | 439k | if (w == 0) |
23 | 0 | return (BN_ULONG)-1; |
24 | | |
25 | 439k | #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 | 439k | 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 | 439k | #endif |
41 | | |
42 | 439k | bn_check_top(a); |
43 | 439k | w &= BN_MASK2; |
44 | 10.7M | for (i = a->top - 1; i >= 0; i--) { |
45 | 10.2M | #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 | 10.2M | ret = ((ret << BN_BITS4) | ((a->d[i] >> BN_BITS4) & BN_MASK2l)) % w; |
52 | 10.2M | 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 | 10.2M | } |
57 | 439k | return (BN_ULONG)ret; |
58 | 439k | } |
59 | | |
60 | | BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w) |
61 | 3.40M | { |
62 | 3.40M | BN_ULONG ret = 0; |
63 | 3.40M | int i, j; |
64 | | |
65 | 3.40M | bn_check_top(a); |
66 | 3.40M | w &= BN_MASK2; |
67 | | |
68 | 3.40M | if (!w) |
69 | | /* actually this an error (division by zero) */ |
70 | 0 | return (BN_ULONG)-1; |
71 | 3.40M | if (a->top == 0) |
72 | 0 | return 0; |
73 | | |
74 | | /* normalize input (so bn_div_words doesn't complain) */ |
75 | 3.40M | j = BN_BITS2 - BN_num_bits_word(w); |
76 | 3.40M | w <<= j; |
77 | 3.40M | if (!BN_lshift(a, a, j)) |
78 | 0 | return (BN_ULONG)-1; |
79 | | |
80 | 8.16M | for (i = a->top - 1; i >= 0; i--) { |
81 | 4.75M | BN_ULONG l, d; |
82 | | |
83 | 4.75M | l = a->d[i]; |
84 | 4.75M | d = bn_div_words(ret, l, w); |
85 | 4.75M | ret = (l - ((d * w) & BN_MASK2)) & BN_MASK2; |
86 | 4.75M | a->d[i] = d; |
87 | 4.75M | } |
88 | 3.40M | if ((a->top > 0) && (a->d[a->top - 1] == 0)) |
89 | 3.38M | a->top--; |
90 | 3.40M | ret >>= j; |
91 | 3.40M | if (!a->top) |
92 | 3.05M | a->neg = 0; /* don't allow negative zero */ |
93 | 3.40M | bn_check_top(a); |
94 | 3.40M | return ret; |
95 | 3.40M | } |
96 | | |
97 | | int BN_add_word(BIGNUM *a, BN_ULONG w) |
98 | 3.63M | { |
99 | 3.63M | BN_ULONG l; |
100 | 3.63M | int i; |
101 | | |
102 | 3.63M | bn_check_top(a); |
103 | 3.63M | w &= BN_MASK2; |
104 | | |
105 | | /* degenerate case: w is zero */ |
106 | 3.63M | if (!w) |
107 | 497k | return 1; |
108 | | /* degenerate case: a is zero */ |
109 | 3.13M | if (BN_is_zero(a)) |
110 | 42.1k | return BN_set_word(a, w); |
111 | | /* handle 'a' when negative */ |
112 | 3.09M | 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.45M | for (i = 0; w != 0 && i < a->top; i++) { |
120 | 3.36M | a->d[i] = l = (a->d[i] + w) & BN_MASK2; |
121 | 3.36M | w = (w > l) ? 1 : 0; |
122 | 3.36M | } |
123 | 3.09M | if (w && i == a->top) { |
124 | 1.70k | if (bn_wexpand(a, a->top + 1) == NULL) |
125 | 0 | return 0; |
126 | 1.70k | a->top++; |
127 | 1.70k | a->d[i] = w; |
128 | 1.70k | } |
129 | 3.09M | bn_check_top(a); |
130 | 3.09M | return 1; |
131 | 3.09M | } |
132 | | |
133 | | int BN_sub_word(BIGNUM *a, BN_ULONG w) |
134 | 1.13M | { |
135 | 1.13M | int i; |
136 | | |
137 | 1.13M | bn_check_top(a); |
138 | 1.13M | w &= BN_MASK2; |
139 | | |
140 | | /* degenerate case: w is zero */ |
141 | 1.13M | if (!w) |
142 | 0 | return 1; |
143 | | /* degenerate case: a is zero */ |
144 | 1.13M | if (BN_is_zero(a)) { |
145 | 715 | i = BN_set_word(a, w); |
146 | 715 | if (i != 0) |
147 | 715 | BN_set_negative(a, 1); |
148 | 715 | return i; |
149 | 715 | } |
150 | | /* handle 'a' when negative */ |
151 | 1.13M | 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.13M | if ((a->top == 1) && (a->d[0] < w)) { |
159 | 22 | a->d[0] = w - a->d[0]; |
160 | 22 | a->neg = 1; |
161 | 22 | return 1; |
162 | 22 | } |
163 | 1.13M | i = 0; |
164 | 2.20M | for (;;) { |
165 | 2.20M | if (a->d[i] >= w) { |
166 | 1.13M | a->d[i] -= w; |
167 | 1.13M | break; |
168 | 1.13M | } else { |
169 | 1.06M | a->d[i] = (a->d[i] - w) & BN_MASK2; |
170 | 1.06M | i++; |
171 | 1.06M | w = 1; |
172 | 1.06M | } |
173 | 2.20M | } |
174 | 1.13M | if ((a->d[i] == 0) && (i == (a->top - 1))) |
175 | 191k | a->top--; |
176 | 1.13M | bn_check_top(a); |
177 | 1.13M | return 1; |
178 | 1.13M | } |
179 | | |
180 | | int BN_mul_word(BIGNUM *a, BN_ULONG w) |
181 | 4.86M | { |
182 | 4.86M | BN_ULONG ll; |
183 | | |
184 | 4.86M | bn_check_top(a); |
185 | 4.86M | w &= BN_MASK2; |
186 | 4.86M | if (a->top) { |
187 | 4.86M | if (w == 0) |
188 | 0 | BN_zero(a); |
189 | 4.86M | else { |
190 | 4.86M | ll = bn_mul_words(a->d, a->d, a->top, w); |
191 | 4.86M | if (ll) { |
192 | 4.70M | if (bn_wexpand(a, a->top + 1) == NULL) |
193 | 0 | return 0; |
194 | 4.70M | a->d[a->top++] = ll; |
195 | 4.70M | } |
196 | 4.86M | } |
197 | 4.86M | } |
198 | 4.86M | bn_check_top(a); |
199 | 4.86M | return 1; |
200 | 4.86M | } |