Coverage Report

Created: 2025-06-13 06:55

/src/openssl/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
0
{
24
0
    int window_val;
25
0
    signed char *r = NULL;
26
0
    int sign = 1;
27
0
    int bit, next_bit, mask;
28
0
    size_t len = 0, j;
29
30
0
    if (BN_is_zero(scalar)) {
31
0
        r = OPENSSL_malloc(1);
32
0
        if (r == NULL)
33
0
            goto err;
34
0
        r[0] = 0;
35
0
        *ret_len = 1;
36
0
        return r;
37
0
    }
38
39
0
    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
0
    bit = 1 << w;               /* at most 128 */
45
0
    next_bit = bit << 1;        /* at most 256 */
46
0
    mask = next_bit - 1;        /* at most 255 */
47
48
0
    if (BN_is_negative(scalar)) {
49
0
        sign = -1;
50
0
    }
51
52
0
    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
0
    len = BN_num_bits(scalar);
58
0
    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
0
    if (r == NULL)
64
0
        goto err;
65
0
    window_val = scalar->d[0] & mask;
66
0
    j = 0;
67
0
    while ((window_val != 0) || (j + w + 1 < len)) { /* if j+w+1 >= len,
68
                                                      * window_val will not
69
                                                      * increase */
70
0
        int digit = 0;
71
72
        /* 0 <= window_val <= 2^(w+1) */
73
74
0
        if (window_val & 1) {
75
            /* 0 < window_val < 2^(w+1) */
76
77
0
            if (window_val & bit) {
78
0
                digit = window_val - next_bit; /* -2^w < digit < 0 */
79
80
0
#if 1                           /* modified wNAF */
81
0
                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
0
                    digit = window_val & (mask >> 1); /* 0 < digit < 2^w */
90
0
                }
91
0
#endif
92
0
            } else {
93
0
                digit = window_val; /* 0 < digit < 2^w */
94
0
            }
95
96
0
            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
0
            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
0
            if (window_val != 0 && window_val != next_bit
108
0
                && window_val != bit) {
109
0
                ERR_raise(ERR_LIB_BN, ERR_R_INTERNAL_ERROR);
110
0
                goto err;
111
0
            }
112
0
        }
113
114
0
        r[j++] = sign * digit;
115
116
0
        window_val >>= 1;
117
0
        window_val += bit * BN_is_bit_set(scalar, j + w);
118
119
0
        if (window_val > next_bit) {
120
0
            ERR_raise(ERR_LIB_BN, ERR_R_INTERNAL_ERROR);
121
0
            goto err;
122
0
        }
123
0
    }
124
125
0
    if (j > len + 1) {
126
0
        ERR_raise(ERR_LIB_BN, ERR_R_INTERNAL_ERROR);
127
0
        goto err;
128
0
    }
129
0
    *ret_len = j;
130
0
    return r;
131
132
0
 err:
133
0
    OPENSSL_free(r);
134
0
    return NULL;
135
0
}
136
137
int bn_get_top(const BIGNUM *a)
138
0
{
139
0
    return a->top;
140
0
}
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
0
{
149
0
    int i;
150
151
0
    for (i = a->top; i < a->dmax; i++)
152
0
        a->d[i] = 0;
153
0
}
154
155
int bn_copy_words(BN_ULONG *out, const BIGNUM *in, int size)
156
0
{
157
0
    if (in->top > size)
158
0
        return 0;
159
160
0
    memset(out, 0, sizeof(*out) * size);
161
0
    if (in->d != NULL)
162
0
        memcpy(out, in->d, sizeof(*out) * in->top);
163
0
    return 1;
164
0
}
165
166
BN_ULONG *bn_get_words(const BIGNUM *a)
167
0
{
168
0
    return a->d;
169
0
}
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
0
{
186
0
    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
0
    memcpy(a->d, words, sizeof(BN_ULONG) * num_words);
192
0
    a->top = num_words;
193
0
    bn_correct_top(a);
194
0
    return 1;
195
0
}