Coverage Report

Created: 2025-12-31 06:58

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