/src/openssl30/crypto/bn/bn_shift.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2020 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 <assert.h> |
11 | | #include "internal/cryptlib.h" |
12 | | #include "bn_local.h" |
13 | | |
14 | | int BN_lshift1(BIGNUM *r, const BIGNUM *a) |
15 | 2.89k | { |
16 | 2.89k | register BN_ULONG *ap, *rp, t, c; |
17 | 2.89k | int i; |
18 | | |
19 | 2.89k | bn_check_top(r); |
20 | 2.89k | bn_check_top(a); |
21 | | |
22 | 2.89k | if (r != a) { |
23 | 2.89k | r->neg = a->neg; |
24 | 2.89k | if (bn_wexpand(r, a->top + 1) == NULL) |
25 | 0 | return 0; |
26 | 2.89k | r->top = a->top; |
27 | 2.89k | } else { |
28 | 0 | if (bn_wexpand(r, a->top + 1) == NULL) |
29 | 0 | return 0; |
30 | 0 | } |
31 | 2.89k | ap = a->d; |
32 | 2.89k | rp = r->d; |
33 | 2.89k | c = 0; |
34 | 7.29k | for (i = 0; i < a->top; i++) { |
35 | 4.40k | t = *(ap++); |
36 | 4.40k | *(rp++) = ((t << 1) | c) & BN_MASK2; |
37 | 4.40k | c = t >> (BN_BITS2 - 1); |
38 | 4.40k | } |
39 | 2.89k | *rp = c; |
40 | 2.89k | r->top += c; |
41 | 2.89k | bn_check_top(r); |
42 | 2.89k | return 1; |
43 | 2.89k | } |
44 | | |
45 | | int BN_rshift1(BIGNUM *r, const BIGNUM *a) |
46 | 311k | { |
47 | 311k | BN_ULONG *ap, *rp, t, c; |
48 | 311k | int i; |
49 | | |
50 | 311k | bn_check_top(r); |
51 | 311k | bn_check_top(a); |
52 | | |
53 | 311k | if (BN_is_zero(a)) { |
54 | 0 | BN_zero(r); |
55 | 0 | return 1; |
56 | 0 | } |
57 | 311k | i = a->top; |
58 | 311k | ap = a->d; |
59 | 311k | if (a != r) { |
60 | 794 | if (bn_wexpand(r, i) == NULL) |
61 | 0 | return 0; |
62 | 794 | r->neg = a->neg; |
63 | 794 | } |
64 | 311k | rp = r->d; |
65 | 311k | r->top = i; |
66 | 311k | t = ap[--i]; |
67 | 311k | rp[i] = t >> 1; |
68 | 311k | c = t << (BN_BITS2 - 1); |
69 | 311k | r->top -= (t == 1); |
70 | 500k | while (i > 0) { |
71 | 189k | t = ap[--i]; |
72 | 189k | rp[i] = ((t >> 1) & BN_MASK2) | c; |
73 | 189k | c = t << (BN_BITS2 - 1); |
74 | 189k | } |
75 | 311k | if (!r->top) |
76 | 0 | r->neg = 0; /* don't allow negative zero */ |
77 | 311k | bn_check_top(r); |
78 | 311k | return 1; |
79 | 311k | } |
80 | | |
81 | | int BN_lshift(BIGNUM *r, const BIGNUM *a, int n) |
82 | 570k | { |
83 | 570k | int ret; |
84 | | |
85 | 570k | if (n < 0) { |
86 | 0 | ERR_raise(ERR_LIB_BN, BN_R_INVALID_SHIFT); |
87 | 0 | return 0; |
88 | 0 | } |
89 | | |
90 | 570k | ret = bn_lshift_fixed_top(r, a, n); |
91 | | |
92 | 570k | bn_correct_top(r); |
93 | 570k | bn_check_top(r); |
94 | | |
95 | 570k | return ret; |
96 | 570k | } |
97 | | |
98 | | /* |
99 | | * In respect to shift factor the execution time is invariant of |
100 | | * |n % BN_BITS2|, but not |n / BN_BITS2|. Or in other words pre-condition |
101 | | * for constant-time-ness is |n < BN_BITS2| or |n / BN_BITS2| being |
102 | | * non-secret. |
103 | | */ |
104 | | int bn_lshift_fixed_top(BIGNUM *r, const BIGNUM *a, int n) |
105 | 1.29M | { |
106 | 1.29M | int i, nw; |
107 | 1.29M | unsigned int lb, rb; |
108 | 1.29M | BN_ULONG *t, *f; |
109 | 1.29M | BN_ULONG l, m, rmask = 0; |
110 | | |
111 | 1.29M | assert(n >= 0); |
112 | | |
113 | 1.29M | bn_check_top(r); |
114 | 1.29M | bn_check_top(a); |
115 | | |
116 | 1.29M | nw = n / BN_BITS2; |
117 | 1.29M | if (bn_wexpand(r, a->top + nw + 1) == NULL) |
118 | 0 | return 0; |
119 | | |
120 | 1.29M | if (a->top != 0) { |
121 | 1.28M | lb = (unsigned int)n % BN_BITS2; |
122 | 1.28M | rb = BN_BITS2 - lb; |
123 | 1.28M | rb %= BN_BITS2; /* say no to undefined behaviour */ |
124 | 1.28M | rmask = (BN_ULONG)0 - rb; /* rmask = 0 - (rb != 0) */ |
125 | 1.28M | rmask |= rmask >> 8; |
126 | 1.28M | f = &(a->d[0]); |
127 | 1.28M | t = &(r->d[nw]); |
128 | 1.28M | l = f[a->top - 1]; |
129 | 1.28M | t[a->top] = (l >> rb) & rmask; |
130 | 6.73M | for (i = a->top - 1; i > 0; i--) { |
131 | 5.44M | m = l << lb; |
132 | 5.44M | l = f[i - 1]; |
133 | 5.44M | t[i] = (m | ((l >> rb) & rmask)) & BN_MASK2; |
134 | 5.44M | } |
135 | 1.28M | t[0] = (l << lb) & BN_MASK2; |
136 | 1.28M | } else { |
137 | | /* shouldn't happen, but formally required */ |
138 | 3.14k | r->d[nw] = 0; |
139 | 3.14k | } |
140 | 1.29M | if (nw != 0) |
141 | 5.81k | memset(r->d, 0, sizeof(*t) * nw); |
142 | | |
143 | 1.29M | r->neg = a->neg; |
144 | 1.29M | r->top = a->top + nw + 1; |
145 | 1.29M | r->flags |= BN_FLG_FIXED_TOP; |
146 | | |
147 | 1.29M | return 1; |
148 | 1.29M | } |
149 | | |
150 | | int BN_rshift(BIGNUM *r, const BIGNUM *a, int n) |
151 | 304k | { |
152 | 304k | int ret = 0; |
153 | | |
154 | 304k | if (n < 0) { |
155 | 0 | ERR_raise(ERR_LIB_BN, BN_R_INVALID_SHIFT); |
156 | 0 | return 0; |
157 | 0 | } |
158 | | |
159 | 304k | ret = bn_rshift_fixed_top(r, a, n); |
160 | | |
161 | 304k | bn_correct_top(r); |
162 | 304k | bn_check_top(r); |
163 | | |
164 | 304k | return ret; |
165 | 304k | } |
166 | | |
167 | | /* |
168 | | * In respect to shift factor the execution time is invariant of |
169 | | * |n % BN_BITS2|, but not |n / BN_BITS2|. Or in other words pre-condition |
170 | | * for constant-time-ness for sufficiently[!] zero-padded inputs is |
171 | | * |n < BN_BITS2| or |n / BN_BITS2| being non-secret. |
172 | | */ |
173 | | int bn_rshift_fixed_top(BIGNUM *r, const BIGNUM *a, int n) |
174 | 1.01M | { |
175 | 1.01M | int i, top, nw; |
176 | 1.01M | unsigned int lb, rb; |
177 | 1.01M | BN_ULONG *t, *f; |
178 | 1.01M | BN_ULONG l, m, mask; |
179 | | |
180 | 1.01M | bn_check_top(r); |
181 | 1.01M | bn_check_top(a); |
182 | | |
183 | 1.01M | assert(n >= 0); |
184 | | |
185 | 1.01M | nw = n / BN_BITS2; |
186 | 1.01M | if (nw >= a->top) { |
187 | | /* shouldn't happen, but formally required */ |
188 | 0 | BN_zero(r); |
189 | 0 | return 1; |
190 | 0 | } |
191 | | |
192 | 1.01M | rb = (unsigned int)n % BN_BITS2; |
193 | 1.01M | lb = BN_BITS2 - rb; |
194 | 1.01M | lb %= BN_BITS2; /* say no to undefined behaviour */ |
195 | 1.01M | mask = (BN_ULONG)0 - lb; /* mask = 0 - (lb != 0) */ |
196 | 1.01M | mask |= mask >> 8; |
197 | 1.01M | top = a->top - nw; |
198 | 1.01M | if (r != a && bn_wexpand(r, top) == NULL) |
199 | 0 | return 0; |
200 | | |
201 | 1.01M | t = &(r->d[0]); |
202 | 1.01M | f = &(a->d[nw]); |
203 | 1.01M | l = f[0]; |
204 | 3.09M | for (i = 0; i < top - 1; i++) { |
205 | 2.07M | m = f[i + 1]; |
206 | 2.07M | t[i] = (l >> rb) | ((m << lb) & mask); |
207 | 2.07M | l = m; |
208 | 2.07M | } |
209 | 1.01M | t[i] = l >> rb; |
210 | | |
211 | 1.01M | r->neg = a->neg; |
212 | 1.01M | r->top = top; |
213 | 1.01M | r->flags |= BN_FLG_FIXED_TOP; |
214 | | |
215 | 1.01M | return 1; |
216 | 1.01M | } |