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