Coverage Report

Created: 2023-09-25 06:43

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