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