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