Coverage Report

Created: 2024-07-27 06:39

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