Coverage Report

Created: 2026-10-28 06:28

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