Coverage Report

Created: 2023-06-08 06:41

/src/openssl111/crypto/bn/bn_shift.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the OpenSSL license (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
1.85k
{
16
1.85k
    register BN_ULONG *ap, *rp, t, c;
17
1.85k
    int i;
18
19
1.85k
    bn_check_top(r);
20
1.85k
    bn_check_top(a);
21
22
1.85k
    if (r != a) {
23
1.85k
        r->neg = a->neg;
24
1.85k
        if (bn_wexpand(r, a->top + 1) == NULL)
25
0
            return 0;
26
1.85k
        r->top = a->top;
27
1.85k
    } else {
28
0
        if (bn_wexpand(r, a->top + 1) == NULL)
29
0
            return 0;
30
0
    }
31
1.85k
    ap = a->d;
32
1.85k
    rp = r->d;
33
1.85k
    c = 0;
34
4.62k
    for (i = 0; i < a->top; i++) {
35
2.77k
        t = *(ap++);
36
2.77k
        *(rp++) = ((t << 1) | c) & BN_MASK2;
37
2.77k
        c = t >> (BN_BITS2 - 1);
38
2.77k
    }
39
1.85k
    *rp = c;
40
1.85k
    r->top += c;
41
1.85k
    bn_check_top(r);
42
1.85k
    return 1;
43
1.85k
}
44
45
int BN_rshift1(BIGNUM *r, const BIGNUM *a)
46
221k
{
47
221k
    BN_ULONG *ap, *rp, t, c;
48
221k
    int i;
49
50
221k
    bn_check_top(r);
51
221k
    bn_check_top(a);
52
53
221k
    if (BN_is_zero(a)) {
54
0
        BN_zero(r);
55
0
        return 1;
56
0
    }
57
221k
    i = a->top;
58
221k
    ap = a->d;
59
221k
    if (a != r) {
60
331
        if (bn_wexpand(r, i) == NULL)
61
0
            return 0;
62
331
        r->neg = a->neg;
63
331
    }
64
221k
    rp = r->d;
65
221k
    r->top = i;
66
221k
    t = ap[--i];
67
221k
    rp[i] = t >> 1;
68
221k
    c = t << (BN_BITS2 - 1);
69
221k
    r->top -= (t == 1);
70
361k
    while (i > 0) {
71
139k
        t = ap[--i];
72
139k
        rp[i] = ((t >> 1) & BN_MASK2) | c;
73
139k
        c = t << (BN_BITS2 - 1);
74
139k
    }
75
221k
    if (!r->top)
76
0
        r->neg = 0; /* don't allow negative zero */
77
221k
    bn_check_top(r);
78
221k
    return 1;
79
221k
}
80
81
int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)
82
65.8k
{
83
65.8k
    int ret;
84
85
65.8k
    if (n < 0) {
86
0
        BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT);
87
0
        return 0;
88
0
    }
89
90
65.8k
    ret = bn_lshift_fixed_top(r, a, n);
91
92
65.8k
    bn_correct_top(r);
93
65.8k
    bn_check_top(r);
94
95
65.8k
    return ret;
96
65.8k
}
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
467k
{
106
467k
    int i, nw;
107
467k
    unsigned int lb, rb;
108
467k
    BN_ULONG *t, *f;
109
467k
    BN_ULONG l, m, rmask = 0;
110
111
467k
    assert(n >= 0);
112
113
467k
    bn_check_top(r);
114
467k
    bn_check_top(a);
115
116
467k
    nw = n / BN_BITS2;
117
467k
    if (bn_wexpand(r, a->top + nw + 1) == NULL)
118
0
        return 0;
119
120
467k
    if (a->top != 0) {
121
466k
        lb = (unsigned int)n % BN_BITS2;
122
466k
        rb = BN_BITS2 - lb;
123
466k
        rb %= BN_BITS2;            /* say no to undefined behaviour */
124
466k
        rmask = (BN_ULONG)0 - rb;  /* rmask = 0 - (rb != 0) */
125
466k
        rmask |= rmask >> 8;
126
466k
        f = &(a->d[0]);
127
466k
        t = &(r->d[nw]);
128
466k
        l = f[a->top - 1];
129
466k
        t[a->top] = (l >> rb) & rmask;
130
2.96M
        for (i = a->top - 1; i > 0; i--) {
131
2.49M
            m = l << lb;
132
2.49M
            l = f[i - 1];
133
2.49M
            t[i] = (m | ((l >> rb) & rmask)) & BN_MASK2;
134
2.49M
        }
135
466k
        t[0] = (l << lb) & BN_MASK2;
136
466k
    } else {
137
        /* shouldn't happen, but formally required */
138
1.32k
        r->d[nw] = 0;
139
1.32k
    }
140
467k
    if (nw != 0)
141
3.99k
        memset(r->d, 0, sizeof(*t) * nw);
142
143
467k
    r->neg = a->neg;
144
467k
    r->top = a->top + nw + 1;
145
467k
    r->flags |= BN_FLG_FIXED_TOP;
146
147
467k
    return 1;
148
467k
}
149
150
int BN_rshift(BIGNUM *r, const BIGNUM *a, int n)
151
219k
{
152
219k
    int ret = 0;
153
154
219k
    if (n < 0) {
155
0
        BNerr(BN_F_BN_RSHIFT, BN_R_INVALID_SHIFT);
156
0
        return 0;
157
0
    }
158
159
219k
    ret = bn_rshift_fixed_top(r, a, n);
160
161
219k
    bn_correct_top(r);
162
219k
    bn_check_top(r);
163
164
219k
    return ret;
165
219k
}
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
617k
{
175
617k
    int i, top, nw;
176
617k
    unsigned int lb, rb;
177
617k
    BN_ULONG *t, *f;
178
617k
    BN_ULONG l, m, mask;
179
180
617k
    bn_check_top(r);
181
617k
    bn_check_top(a);
182
183
617k
    assert(n >= 0);
184
185
617k
    nw = n / BN_BITS2;
186
617k
    if (nw >= a->top) {
187
        /* shouldn't happen, but formally required */
188
0
        BN_zero(r);
189
0
        return 1;
190
0
    }
191
192
617k
    rb = (unsigned int)n % BN_BITS2;
193
617k
    lb = BN_BITS2 - rb;
194
617k
    lb %= BN_BITS2;            /* say no to undefined behaviour */
195
617k
    mask = (BN_ULONG)0 - lb;   /* mask = 0 - (lb != 0) */
196
617k
    mask |= mask >> 8;
197
617k
    top = a->top - nw;
198
617k
    if (r != a && bn_wexpand(r, top) == NULL)
199
0
        return 0;
200
201
617k
    t = &(r->d[0]);
202
617k
    f = &(a->d[nw]);
203
617k
    l = f[0];
204
1.74M
    for (i = 0; i < top - 1; i++) {
205
1.12M
        m = f[i + 1];
206
1.12M
        t[i] = (l >> rb) | ((m << lb) & mask);
207
1.12M
        l = m;
208
1.12M
    }
209
617k
    t[i] = l >> rb;
210
211
617k
    r->neg = a->neg;
212
617k
    r->top = top;
213
617k
    r->flags |= BN_FLG_FIXED_TOP;
214
215
617k
    return 1;
216
617k
}