Coverage Report

Created: 2025-06-13 06:56

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