Coverage Report

Created: 2026-10-28 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl35/crypto/bn/bn_intern.c
Line
Count
Source
1
/*
2
 * Copyright 2014-2026 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 "internal/cryptlib.h"
11
#include "bn_local.h"
12
#include "internal/constant_time.h"
13
14
/*
15
 * Determine the modified width-(w+1) Non-Adjacent Form (wNAF) of 'scalar'.
16
 * This is an array  r[]  of values that are either zero or odd with an
17
 * absolute value less than  2^w  satisfying
18
 *     scalar = \sum_j r[j]*2^j
19
 * where at most one of any  w+1  consecutive digits is non-zero
20
 * with the exception that the most significant digit may be only
21
 * w-1 zeros away from that next non-zero digit.
22
 */
23
signed char *bn_compute_wNAF(const BIGNUM *scalar, int w, size_t *ret_len)
24
4.13k
{
25
4.13k
    int window_val;
26
4.13k
    signed char *r = NULL;
27
4.13k
    int sign = 1;
28
4.13k
    int bit, next_bit, mask;
29
4.13k
    size_t len = 0, j;
30
31
4.13k
    if (BN_is_zero(scalar)) {
32
62
        r = OPENSSL_malloc(1);
33
62
        if (r == NULL)
34
0
            goto err;
35
62
        r[0] = 0;
36
62
        *ret_len = 1;
37
62
        return r;
38
62
    }
39
40
4.07k
    if (w <= 0 || w > 7) { /* 'signed char' can represent integers with
41
                            * absolute values less than 2^7 */
42
0
        ERR_raise(ERR_LIB_BN, ERR_R_INTERNAL_ERROR);
43
0
        goto err;
44
0
    }
45
4.07k
    bit = 1 << w; /* at most 128 */
46
4.07k
    next_bit = bit << 1; /* at most 256 */
47
4.07k
    mask = next_bit - 1; /* at most 255 */
48
49
4.07k
    if (BN_is_negative(scalar)) {
50
0
        sign = -1;
51
0
    }
52
53
4.07k
    if (scalar->d == NULL || scalar->top == 0) {
54
0
        ERR_raise(ERR_LIB_BN, ERR_R_INTERNAL_ERROR);
55
0
        goto err;
56
0
    }
57
58
4.07k
    len = BN_num_bits(scalar);
59
4.07k
    r = OPENSSL_malloc(len + 1); /*
60
                                  * Modified wNAF may be one digit longer than binary representation
61
                                  * (*ret_len will be set to the actual length, i.e. at most
62
                                  * BN_num_bits(scalar) + 1)
63
                                  */
64
4.07k
    if (r == NULL)
65
0
        goto err;
66
4.07k
    window_val = scalar->d[0] & mask;
67
4.07k
    j = 0;
68
723k
    while ((window_val != 0) || (j + w + 1 < len)) { /* if j+w+1 >= len,
69
                                                      * window_val will not
70
                                                      * increase */
71
719k
        int digit = 0;
72
73
        /* 0 <= window_val <= 2^(w+1) */
74
75
719k
        if (window_val & 1) {
76
            /* 0 < window_val < 2^(w+1) */
77
78
103k
            if (window_val & bit) {
79
48.0k
                digit = window_val - next_bit; /* -2^w < digit < 0 */
80
81
48.0k
#if 1 /* modified wNAF */
82
48.0k
                if (j + w + 1 >= len) {
83
                    /*
84
                     * Special case for generating modified wNAFs:
85
                     * no new bits will be added into window_val,
86
                     * so using a positive digit here will decrease
87
                     * the total length of the representation
88
                     */
89
90
594
                    digit = window_val & (mask >> 1); /* 0 < digit < 2^w */
91
594
                }
92
48.0k
#endif
93
55.4k
            } else {
94
55.4k
                digit = window_val; /* 0 < digit < 2^w */
95
55.4k
            }
96
97
103k
            if (digit <= -bit || digit >= bit || !(digit & 1)) {
98
0
                ERR_raise(ERR_LIB_BN, ERR_R_INTERNAL_ERROR);
99
0
                goto err;
100
0
            }
101
102
103k
            window_val -= digit;
103
104
            /*
105
             * now window_val is 0 or 2^(w+1) in standard wNAF generation;
106
             * for modified window NAFs, it may also be 2^w
107
             */
108
103k
            if (window_val != 0 && window_val != next_bit
109
594
                && window_val != bit) {
110
0
                ERR_raise(ERR_LIB_BN, ERR_R_INTERNAL_ERROR);
111
0
                goto err;
112
0
            }
113
103k
        }
114
115
719k
        r[j++] = sign * digit;
116
117
719k
        window_val >>= 1;
118
719k
        window_val += bit * BN_is_bit_set(scalar, j + w);
119
120
719k
        if (window_val > next_bit) {
121
0
            ERR_raise(ERR_LIB_BN, ERR_R_INTERNAL_ERROR);
122
0
            goto err;
123
0
        }
124
719k
    }
125
126
4.07k
    if (j > len + 1) {
127
0
        ERR_raise(ERR_LIB_BN, ERR_R_INTERNAL_ERROR);
128
0
        goto err;
129
0
    }
130
4.07k
    *ret_len = j;
131
4.07k
    return r;
132
133
0
err:
134
0
    OPENSSL_free(r);
135
0
    return NULL;
136
4.07k
}
137
138
int bn_get_top(const BIGNUM *a)
139
327k
{
140
327k
    return a->top;
141
327k
}
142
143
int bn_get_dmax(const BIGNUM *a)
144
0
{
145
0
    return a->dmax;
146
0
}
147
148
void bn_set_all_zero(BIGNUM *a)
149
1.05M
{
150
1.05M
    int i;
151
152
1.79M
    for (i = a->top; i < a->dmax; i++)
153
742k
        a->d[i] = 0;
154
1.05M
}
155
156
/*
157
 * Zero-extend |a| so that it occupies exactly |words| words, flag it
158
 * BN_FLG_FIXED_TOP and leave its numeric value unchanged.
159
 *
160
 * This is a companion to bn_correct_top(): where the latter minimises the top
161
 * of a BIGNUM, this one pins the top to a caller-chosen, value-independent
162
 * width.  Constant-time code uses it to make the cost of subsequent word-wise
163
 * operations (e.g. BN_uadd()/BN_add()) independent of the magnitude of a
164
 * secret value.  |words| must be greater than or equal to the current top.
165
 *
166
 * The routine is itself constant time with respect to the current a->top: it
167
 * always sweeps a fixed |words| iterations and selects value-or-zero per word
168
 * with an arithmetic mask, rather than looping over the (possibly secret)
169
 * a->top..words range.  Masking the high words with zero also launders any
170
 * uninitialised padding, so it is safe for the memory sanitiser.
171
 */
172
int bn_set_top_fixed(BIGNUM *a, int words)
173
34.7k
{
174
34.7k
    size_t i, n = (size_t)words;
175
34.7k
    BN_ULONG mask;
176
177
34.7k
    if (words < a->top)
178
0
        return 0;
179
34.7k
    if (bn_wexpand(a, words) == NULL) {
180
0
        ERR_raise(ERR_LIB_BN, ERR_R_BN_LIB);
181
0
        return 0;
182
0
    }
183
208k
    for (i = 0; i < n; i++) {
184
        /* mask = all ones iff i < a->top, else all zeros */
185
174k
        mask = value_barrier_bn((BN_ULONG)0 - ((i - a->top) >> (8 * sizeof(i) - 1)));
186
174k
        a->d[i] &= mask;
187
174k
    }
188
34.7k
    a->top = words;
189
34.7k
    a->flags |= BN_FLG_FIXED_TOP;
190
34.7k
    return 1;
191
34.7k
}
192
193
int bn_copy_words(BN_ULONG *out, const BIGNUM *in, int size)
194
309k
{
195
309k
    if (in->top > size)
196
0
        return 0;
197
198
309k
    memset(out, 0, sizeof(*out) * size);
199
309k
    if (in->d != NULL)
200
309k
        memcpy(out, in->d, sizeof(*out) * in->top);
201
309k
    return 1;
202
309k
}
203
204
BN_ULONG *bn_get_words(const BIGNUM *a)
205
404k
{
206
404k
    return a->d;
207
404k
}
208
209
void bn_set_static_words(BIGNUM *a, const BN_ULONG *words, int size)
210
0
{
211
    /*
212
     * |const| qualifier omission is compensated by BN_FLG_STATIC_DATA
213
     * flag, which effectively means "read-only data".
214
     */
215
0
    a->d = (BN_ULONG *)words;
216
0
    a->dmax = a->top = size;
217
0
    a->neg = 0;
218
0
    a->flags |= BN_FLG_STATIC_DATA;
219
0
    bn_correct_top(a);
220
0
}
221
222
int bn_set_words(BIGNUM *a, const BN_ULONG *words, int num_words)
223
247k
{
224
247k
    if (bn_wexpand(a, num_words) == NULL) {
225
0
        ERR_raise(ERR_LIB_BN, ERR_R_BN_LIB);
226
0
        return 0;
227
0
    }
228
229
247k
    memcpy(a->d, words, sizeof(BN_ULONG) * num_words);
230
247k
    a->top = num_words;
231
247k
    bn_correct_top(a);
232
247k
    return 1;
233
247k
}