Coverage Report

Created: 2025-08-11 07:04

/src/openssl34/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
10.4M
{
16
10.4M
    register BN_ULONG *ap, *rp, t, c;
17
10.4M
    int i;
18
19
10.4M
    bn_check_top(r);
20
10.4M
    bn_check_top(a);
21
22
10.4M
    if (r != a) {
23
2.78M
        r->neg = a->neg;
24
2.78M
        if (bn_wexpand(r, a->top + 1) == NULL)
25
0
            return 0;
26
2.78M
        r->top = a->top;
27
7.70M
    } else {
28
7.70M
        if (bn_wexpand(r, a->top + 1) == NULL)
29
0
            return 0;
30
7.70M
    }
31
10.4M
    ap = a->d;
32
10.4M
    rp = r->d;
33
10.4M
    c = 0;
34
53.0M
    for (i = 0; i < a->top; i++) {
35
42.5M
        t = *(ap++);
36
42.5M
        *(rp++) = ((t << 1) | c) & BN_MASK2;
37
42.5M
        c = t >> (BN_BITS2 - 1);
38
42.5M
    }
39
10.4M
    *rp = c;
40
10.4M
    r->top += c;
41
10.4M
    bn_check_top(r);
42
10.4M
    return 1;
43
10.4M
}
44
45
int BN_rshift1(BIGNUM *r, const BIGNUM *a)
46
87.7M
{
47
87.7M
    BN_ULONG *ap, *rp, t, c;
48
87.7M
    int i;
49
50
87.7M
    bn_check_top(r);
51
87.7M
    bn_check_top(a);
52
53
87.7M
    if (BN_is_zero(a)) {
54
12.7M
        BN_zero(r);
55
12.7M
        return 1;
56
12.7M
    }
57
75.0M
    i = a->top;
58
75.0M
    ap = a->d;
59
75.0M
    if (a != r) {
60
156k
        if (bn_wexpand(r, i) == NULL)
61
0
            return 0;
62
156k
        r->neg = a->neg;
63
156k
    }
64
75.0M
    rp = r->d;
65
75.0M
    r->top = i;
66
75.0M
    t = ap[--i];
67
75.0M
    rp[i] = t >> 1;
68
75.0M
    c = t << (BN_BITS2 - 1);
69
75.0M
    r->top -= (t == 1);
70
2.32G
    while (i > 0) {
71
2.25G
        t = ap[--i];
72
2.25G
        rp[i] = ((t >> 1) & BN_MASK2) | c;
73
2.25G
        c = t << (BN_BITS2 - 1);
74
2.25G
    }
75
75.0M
    if (!r->top)
76
83
        r->neg = 0; /* don't allow negative zero */
77
75.0M
    bn_check_top(r);
78
75.0M
    return 1;
79
75.0M
}
80
81
int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)
82
9.09M
{
83
9.09M
    int ret;
84
85
9.09M
    if (n < 0) {
86
0
        ERR_raise(ERR_LIB_BN, BN_R_INVALID_SHIFT);
87
0
        return 0;
88
0
    }
89
90
9.09M
    ret = bn_lshift_fixed_top(r, a, n);
91
92
9.09M
    bn_correct_top(r);
93
9.09M
    bn_check_top(r);
94
95
9.09M
    return ret;
96
9.09M
}
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
98.9M
{
106
98.9M
    int i, nw;
107
98.9M
    unsigned int lb, rb;
108
98.9M
    BN_ULONG *t, *f;
109
98.9M
    BN_ULONG l, m, rmask = 0;
110
111
98.9M
    assert(n >= 0);
112
113
98.9M
    bn_check_top(r);
114
98.9M
    bn_check_top(a);
115
116
98.9M
    nw = n / BN_BITS2;
117
98.9M
    if (bn_wexpand(r, a->top + nw + 1) == NULL)
118
0
        return 0;
119
120
98.9M
    if (a->top != 0) {
121
98.6M
        lb = (unsigned int)n % BN_BITS2;
122
98.6M
        rb = BN_BITS2 - lb;
123
98.6M
        rb %= BN_BITS2;            /* say no to undefined behaviour */
124
98.6M
        rmask = (BN_ULONG)0 - rb;  /* rmask = 0 - (rb != 0) */
125
98.6M
        rmask |= rmask >> 8;
126
98.6M
        f = &(a->d[0]);
127
98.6M
        t = &(r->d[nw]);
128
98.6M
        l = f[a->top - 1];
129
98.6M
        t[a->top] = (l >> rb) & rmask;
130
978M
        for (i = a->top - 1; i > 0; i--) {
131
880M
            m = l << lb;
132
880M
            l = f[i - 1];
133
880M
            t[i] = (m | ((l >> rb) & rmask)) & BN_MASK2;
134
880M
        }
135
98.6M
        t[0] = (l << lb) & BN_MASK2;
136
98.6M
    } else {
137
        /* shouldn't happen, but formally required */
138
329k
        r->d[nw] = 0;
139
329k
    }
140
98.9M
    if (nw != 0)
141
853k
        memset(r->d, 0, sizeof(*t) * nw);
142
143
98.9M
    r->neg = a->neg;
144
98.9M
    r->top = a->top + nw + 1;
145
98.9M
    r->flags |= BN_FLG_FIXED_TOP;
146
147
98.9M
    return 1;
148
98.9M
}
149
150
int BN_rshift(BIGNUM *r, const BIGNUM *a, int n)
151
49.9M
{
152
49.9M
    int ret = 0;
153
154
49.9M
    if (n < 0) {
155
0
        ERR_raise(ERR_LIB_BN, BN_R_INVALID_SHIFT);
156
0
        return 0;
157
0
    }
158
159
49.9M
    bn_check_top(r);
160
49.9M
    bn_check_top(a);
161
162
49.9M
    ret = bn_rshift_fixed_top(r, a, n);
163
164
49.9M
    bn_correct_top(r);
165
49.9M
    bn_check_top(r);
166
167
49.9M
    return ret;
168
49.9M
}
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
138M
{
178
138M
    int i, top, nw;
179
138M
    unsigned int lb, rb;
180
138M
    BN_ULONG *t, *f;
181
138M
    BN_ULONG l, m, mask;
182
183
138M
    assert(n >= 0);
184
185
138M
    nw = n / BN_BITS2;
186
138M
    if (nw >= a->top) {
187
        /* shouldn't happen, but formally required */
188
9.55k
        BN_zero(r);
189
9.55k
        return 1;
190
9.55k
    }
191
192
138M
    rb = (unsigned int)n % BN_BITS2;
193
138M
    lb = BN_BITS2 - rb;
194
138M
    lb %= BN_BITS2;            /* say no to undefined behaviour */
195
138M
    mask = (BN_ULONG)0 - lb;   /* mask = 0 - (lb != 0) */
196
138M
    mask |= mask >> 8;
197
138M
    top = a->top - nw;
198
138M
    if (r != a && bn_wexpand(r, top) == NULL)
199
0
        return 0;
200
201
138M
    t = &(r->d[0]);
202
138M
    f = &(a->d[nw]);
203
138M
    l = f[0];
204
847M
    for (i = 0; i < top - 1; i++) {
205
708M
        m = f[i + 1];
206
708M
        t[i] = (l >> rb) | ((m << lb) & mask);
207
708M
        l = m;
208
708M
    }
209
138M
    t[i] = l >> rb;
210
211
138M
    r->neg = a->neg;
212
138M
    r->top = top;
213
138M
    r->flags |= BN_FLG_FIXED_TOP;
214
215
138M
    return 1;
216
138M
}