Coverage Report

Created: 2024-11-21 07:03

/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
266
{
24
266
    int window_val;
25
266
    signed char *r = NULL;
26
266
    int sign = 1;
27
266
    int bit, next_bit, mask;
28
266
    size_t len = 0, j;
29
30
266
    if (BN_is_zero(scalar)) {
31
4
        r = OPENSSL_malloc(1);
32
4
        if (r == NULL)
33
0
            goto err;
34
4
        r[0] = 0;
35
4
        *ret_len = 1;
36
4
        return r;
37
4
    }
38
39
262
    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
262
    bit = 1 << w;               /* at most 128 */
45
262
    next_bit = bit << 1;        /* at most 256 */
46
262
    mask = next_bit - 1;        /* at most 255 */
47
48
262
    if (BN_is_negative(scalar)) {
49
0
        sign = -1;
50
0
    }
51
52
262
    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
262
    len = BN_num_bits(scalar);
58
262
    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
262
    if (r == NULL)
64
0
        goto err;
65
262
    window_val = scalar->d[0] & mask;
66
262
    j = 0;
67
85.2k
    while ((window_val != 0) || (j + w + 1 < len)) { /* if j+w+1 >= len,
68
                                                      * window_val will not
69
                                                      * increase */
70
84.9k
        int digit = 0;
71
72
        /* 0 <= window_val <= 2^(w+1) */
73
74
84.9k
        if (window_val & 1) {
75
            /* 0 < window_val < 2^(w+1) */
76
77
15.1k
            if (window_val & bit) {
78
7.49k
                digit = window_val - next_bit; /* -2^w < digit < 0 */
79
80
7.49k
#if 1                           /* modified wNAF */
81
7.49k
                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
55
                    digit = window_val & (mask >> 1); /* 0 < digit < 2^w */
90
55
                }
91
7.49k
#endif
92
7.65k
            } else {
93
7.65k
                digit = window_val; /* 0 < digit < 2^w */
94
7.65k
            }
95
96
15.1k
            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
15.1k
            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
15.1k
            if (window_val != 0 && window_val != next_bit
108
15.1k
                && window_val != bit) {
109
0
                ERR_raise(ERR_LIB_BN, ERR_R_INTERNAL_ERROR);
110
0
                goto err;
111
0
            }
112
15.1k
        }
113
114
84.9k
        r[j++] = sign * digit;
115
116
84.9k
        window_val >>= 1;
117
84.9k
        window_val += bit * BN_is_bit_set(scalar, j + w);
118
119
84.9k
        if (window_val > next_bit) {
120
0
            ERR_raise(ERR_LIB_BN, ERR_R_INTERNAL_ERROR);
121
0
            goto err;
122
0
        }
123
84.9k
    }
124
125
262
    if (j > len + 1) {
126
0
        ERR_raise(ERR_LIB_BN, ERR_R_INTERNAL_ERROR);
127
0
        goto err;
128
0
    }
129
262
    *ret_len = j;
130
262
    return r;
131
132
0
 err:
133
0
    OPENSSL_free(r);
134
0
    return NULL;
135
262
}
136
137
int bn_get_top(const BIGNUM *a)
138
7.03k
{
139
7.03k
    return a->top;
140
7.03k
}
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
1.03k
{
149
1.03k
    int i;
150
151
2.18k
    for (i = a->top; i < a->dmax; i++)
152
1.15k
        a->d[i] = 0;
153
1.03k
}
154
155
int bn_copy_words(BN_ULONG *out, const BIGNUM *in, int size)
156
1.56k
{
157
1.56k
    if (in->top > size)
158
0
        return 0;
159
160
1.56k
    memset(out, 0, sizeof(*out) * size);
161
1.56k
    if (in->d != NULL)
162
1.56k
        memcpy(out, in->d, sizeof(*out) * in->top);
163
1.56k
    return 1;
164
1.56k
}
165
166
BN_ULONG *bn_get_words(const BIGNUM *a)
167
3.05k
{
168
3.05k
    return a->d;
169
3.05k
}
170
171
void bn_set_static_words(BIGNUM *a, const BN_ULONG *words, int size)
172
{
173
    /*
174
     * |const| qualifier omission is compensated by BN_FLG_STATIC_DATA
175
     * flag, which effectively means "read-only data".
176
     */
177
    a->d = (BN_ULONG *)words;
178
    a->dmax = a->top = size;
179
    a->neg = 0;
180
    a->flags |= BN_FLG_STATIC_DATA;
181
    bn_correct_top(a);
182
}
183
184
int bn_set_words(BIGNUM *a, const BN_ULONG *words, int num_words)
185
1.72k
{
186
1.72k
    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
1.72k
    memcpy(a->d, words, sizeof(BN_ULONG) * num_words);
192
1.72k
    a->top = num_words;
193
1.72k
    bn_correct_top(a);
194
1.72k
    return 1;
195
1.72k
}