Coverage Report

Created: 2023-09-25 06:41

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