Coverage Report

Created: 2025-08-28 07:07

/src/openssl34/crypto/bn/bn_intern.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2014-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 "internal/cryptlib.h"
11
#include "bn_local.h"
12
13
/*
14
 * Determine the modified width-(w+1) Non-Adjacent Form (wNAF) of 'scalar'.
15
 * This is an array  r[]  of values that are either zero or odd with an
16
 * absolute value less than  2^w  satisfying
17
 *     scalar = \sum_j r[j]*2^j
18
 * where at most one of any  w+1  consecutive digits is non-zero
19
 * with the exception that the most significant digit may be only
20
 * w-1 zeros away from that next non-zero digit.
21
 */
22
signed char *bn_compute_wNAF(const BIGNUM *scalar, int w, size_t *ret_len)
23
5.44k
{
24
5.44k
    int window_val;
25
5.44k
    signed char *r = NULL;
26
5.44k
    int sign = 1;
27
5.44k
    int bit, next_bit, mask;
28
5.44k
    size_t len = 0, j;
29
30
5.44k
    if (BN_is_zero(scalar)) {
31
87
        r = OPENSSL_malloc(1);
32
87
        if (r == NULL)
33
0
            goto err;
34
87
        r[0] = 0;
35
87
        *ret_len = 1;
36
87
        return r;
37
87
    }
38
39
5.35k
    if (w <= 0 || w > 7) {      /* 'signed char' can represent integers with
40
                                 * absolute values less than 2^7 */
41
0
        ERR_raise(ERR_LIB_BN, ERR_R_INTERNAL_ERROR);
42
0
        goto err;
43
0
    }
44
5.35k
    bit = 1 << w;               /* at most 128 */
45
5.35k
    next_bit = bit << 1;        /* at most 256 */
46
5.35k
    mask = next_bit - 1;        /* at most 255 */
47
48
5.35k
    if (BN_is_negative(scalar)) {
49
0
        sign = -1;
50
0
    }
51
52
5.35k
    if (scalar->d == NULL || scalar->top == 0) {
53
0
        ERR_raise(ERR_LIB_BN, ERR_R_INTERNAL_ERROR);
54
0
        goto err;
55
0
    }
56
57
5.35k
    len = BN_num_bits(scalar);
58
5.35k
    r = OPENSSL_malloc(len + 1); /*
59
                                  * Modified wNAF may be one digit longer than binary representation
60
                                  * (*ret_len will be set to the actual length, i.e. at most
61
                                  * BN_num_bits(scalar) + 1)
62
                                  */
63
5.35k
    if (r == NULL)
64
0
        goto err;
65
5.35k
    window_val = scalar->d[0] & mask;
66
5.35k
    j = 0;
67
947k
    while ((window_val != 0) || (j + w + 1 < len)) { /* if j+w+1 >= len,
68
                                                      * window_val will not
69
                                                      * increase */
70
941k
        int digit = 0;
71
72
        /* 0 <= window_val <= 2^(w+1) */
73
74
941k
        if (window_val & 1) {
75
            /* 0 < window_val < 2^(w+1) */
76
77
120k
            if (window_val & bit) {
78
55.6k
                digit = window_val - next_bit; /* -2^w < digit < 0 */
79
80
55.6k
#if 1                           /* modified wNAF */
81
55.6k
                if (j + w + 1 >= len) {
82
                    /*
83
                     * Special case for generating modified wNAFs:
84
                     * no new bits will be added into window_val,
85
                     * so using a positive digit here will decrease
86
                     * the total length of the representation
87
                     */
88
89
922
                    digit = window_val & (mask >> 1); /* 0 < digit < 2^w */
90
922
                }
91
55.6k
#endif
92
64.3k
            } else {
93
64.3k
                digit = window_val; /* 0 < digit < 2^w */
94
64.3k
            }
95
96
120k
            if (digit <= -bit || digit >= bit || !(digit & 1)) {
97
0
                ERR_raise(ERR_LIB_BN, ERR_R_INTERNAL_ERROR);
98
0
                goto err;
99
0
            }
100
101
120k
            window_val -= digit;
102
103
            /*
104
             * now window_val is 0 or 2^(w+1) in standard wNAF generation;
105
             * for modified window NAFs, it may also be 2^w
106
             */
107
120k
            if (window_val != 0 && window_val != next_bit
108
120k
                && window_val != bit) {
109
0
                ERR_raise(ERR_LIB_BN, ERR_R_INTERNAL_ERROR);
110
0
                goto err;
111
0
            }
112
120k
        }
113
114
941k
        r[j++] = sign * digit;
115
116
941k
        window_val >>= 1;
117
941k
        window_val += bit * BN_is_bit_set(scalar, j + w);
118
119
941k
        if (window_val > next_bit) {
120
0
            ERR_raise(ERR_LIB_BN, ERR_R_INTERNAL_ERROR);
121
0
            goto err;
122
0
        }
123
941k
    }
124
125
5.35k
    if (j > len + 1) {
126
0
        ERR_raise(ERR_LIB_BN, ERR_R_INTERNAL_ERROR);
127
0
        goto err;
128
0
    }
129
5.35k
    *ret_len = j;
130
5.35k
    return r;
131
132
0
 err:
133
0
    OPENSSL_free(r);
134
0
    return NULL;
135
5.35k
}
136
137
int bn_get_top(const BIGNUM *a)
138
292k
{
139
292k
    return a->top;
140
292k
}
141
142
int bn_get_dmax(const BIGNUM *a)
143
0
{
144
0
    return a->dmax;
145
0
}
146
147
void bn_set_all_zero(BIGNUM *a)
148
851k
{
149
851k
    int i;
150
151
1.55M
    for (i = a->top; i < a->dmax; i++)
152
702k
        a->d[i] = 0;
153
851k
}
154
155
int bn_copy_words(BN_ULONG *out, const BIGNUM *in, int size)
156
308k
{
157
308k
    if (in->top > size)
158
0
        return 0;
159
160
308k
    memset(out, 0, sizeof(*out) * size);
161
308k
    if (in->d != NULL)
162
308k
        memcpy(out, in->d, sizeof(*out) * in->top);
163
308k
    return 1;
164
308k
}
165
166
BN_ULONG *bn_get_words(const BIGNUM *a)
167
440k
{
168
440k
    return a->d;
169
440k
}
170
171
void bn_set_static_words(BIGNUM *a, const BN_ULONG *words, int size)
172
0
{
173
    /*
174
     * |const| qualifier omission is compensated by BN_FLG_STATIC_DATA
175
     * flag, which effectively means "read-only data".
176
     */
177
0
    a->d = (BN_ULONG *)words;
178
0
    a->dmax = a->top = size;
179
0
    a->neg = 0;
180
0
    a->flags |= BN_FLG_STATIC_DATA;
181
0
    bn_correct_top(a);
182
0
}
183
184
int bn_set_words(BIGNUM *a, const BN_ULONG *words, int num_words)
185
226k
{
186
226k
    if (bn_wexpand(a, num_words) == NULL) {
187
0
        ERR_raise(ERR_LIB_BN, ERR_R_BN_LIB);
188
0
        return 0;
189
0
    }
190
191
226k
    memcpy(a->d, words, sizeof(BN_ULONG) * num_words);
192
226k
    a->top = num_words;
193
226k
    bn_correct_top(a);
194
226k
    return 1;
195
226k
}