Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/libressl/src/crypto/bn/bn_lib.c |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /* $OpenBSD: bn_lib.c,v 1.45 2018/07/23 18:14:32 tb Exp $ */ | - | ||||||||||||||||||||||||
2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | - | ||||||||||||||||||||||||
3 | * All rights reserved. | - | ||||||||||||||||||||||||
4 | * | - | ||||||||||||||||||||||||
5 | * This package is an SSL implementation written | - | ||||||||||||||||||||||||
6 | * by Eric Young (eay@cryptsoft.com). | - | ||||||||||||||||||||||||
7 | * The implementation was written so as to conform with Netscapes SSL. | - | ||||||||||||||||||||||||
8 | * | - | ||||||||||||||||||||||||
9 | * This library is free for commercial and non-commercial use as long as | - | ||||||||||||||||||||||||
10 | * the following conditions are aheared to. The following conditions | - | ||||||||||||||||||||||||
11 | * apply to all code found in this distribution, be it the RC4, RSA, | - | ||||||||||||||||||||||||
12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | - | ||||||||||||||||||||||||
13 | * included with this distribution is covered by the same copyright terms | - | ||||||||||||||||||||||||
14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | - | ||||||||||||||||||||||||
15 | * | - | ||||||||||||||||||||||||
16 | * Copyright remains Eric Young's, and as such any Copyright notices in | - | ||||||||||||||||||||||||
17 | * the code are not to be removed. | - | ||||||||||||||||||||||||
18 | * If this package is used in a product, Eric Young should be given attribution | - | ||||||||||||||||||||||||
19 | * as the author of the parts of the library used. | - | ||||||||||||||||||||||||
20 | * This can be in the form of a textual message at program startup or | - | ||||||||||||||||||||||||
21 | * in documentation (online or textual) provided with the package. | - | ||||||||||||||||||||||||
22 | * | - | ||||||||||||||||||||||||
23 | * Redistribution and use in source and binary forms, with or without | - | ||||||||||||||||||||||||
24 | * modification, are permitted provided that the following conditions | - | ||||||||||||||||||||||||
25 | * are met: | - | ||||||||||||||||||||||||
26 | * 1. Redistributions of source code must retain the copyright | - | ||||||||||||||||||||||||
27 | * notice, this list of conditions and the following disclaimer. | - | ||||||||||||||||||||||||
28 | * 2. Redistributions in binary form must reproduce the above copyright | - | ||||||||||||||||||||||||
29 | * notice, this list of conditions and the following disclaimer in the | - | ||||||||||||||||||||||||
30 | * documentation and/or other materials provided with the distribution. | - | ||||||||||||||||||||||||
31 | * 3. All advertising materials mentioning features or use of this software | - | ||||||||||||||||||||||||
32 | * must display the following acknowledgement: | - | ||||||||||||||||||||||||
33 | * "This product includes cryptographic software written by | - | ||||||||||||||||||||||||
34 | * Eric Young (eay@cryptsoft.com)" | - | ||||||||||||||||||||||||
35 | * The word 'cryptographic' can be left out if the rouines from the library | - | ||||||||||||||||||||||||
36 | * being used are not cryptographic related :-). | - | ||||||||||||||||||||||||
37 | * 4. If you include any Windows specific code (or a derivative thereof) from | - | ||||||||||||||||||||||||
38 | * the apps directory (application code) you must include an acknowledgement: | - | ||||||||||||||||||||||||
39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | - | ||||||||||||||||||||||||
40 | * | - | ||||||||||||||||||||||||
41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | - | ||||||||||||||||||||||||
42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | - | ||||||||||||||||||||||||
43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | - | ||||||||||||||||||||||||
44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | - | ||||||||||||||||||||||||
45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | - | ||||||||||||||||||||||||
46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | - | ||||||||||||||||||||||||
47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | - | ||||||||||||||||||||||||
48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | - | ||||||||||||||||||||||||
49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | - | ||||||||||||||||||||||||
50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | - | ||||||||||||||||||||||||
51 | * SUCH DAMAGE. | - | ||||||||||||||||||||||||
52 | * | - | ||||||||||||||||||||||||
53 | * The licence and distribution terms for any publically available version or | - | ||||||||||||||||||||||||
54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | - | ||||||||||||||||||||||||
55 | * copied and put under another distribution licence | - | ||||||||||||||||||||||||
56 | * [including the GNU Public Licence.] | - | ||||||||||||||||||||||||
57 | */ | - | ||||||||||||||||||||||||
58 | - | |||||||||||||||||||||||||
59 | #ifndef BN_DEBUG | - | ||||||||||||||||||||||||
60 | # undef NDEBUG /* avoid conflicting definitions */ | - | ||||||||||||||||||||||||
61 | # define NDEBUG | - | ||||||||||||||||||||||||
62 | #endif | - | ||||||||||||||||||||||||
63 | - | |||||||||||||||||||||||||
64 | #include <assert.h> | - | ||||||||||||||||||||||||
65 | #include <limits.h> | - | ||||||||||||||||||||||||
66 | #include <stdio.h> | - | ||||||||||||||||||||||||
67 | #include <string.h> | - | ||||||||||||||||||||||||
68 | - | |||||||||||||||||||||||||
69 | #include <openssl/opensslconf.h> | - | ||||||||||||||||||||||||
70 | - | |||||||||||||||||||||||||
71 | #include <openssl/err.h> | - | ||||||||||||||||||||||||
72 | - | |||||||||||||||||||||||||
73 | #include "bn_lcl.h" | - | ||||||||||||||||||||||||
74 | - | |||||||||||||||||||||||||
75 | /* This stuff appears to be completely unused, so is deprecated */ | - | ||||||||||||||||||||||||
76 | #ifndef OPENSSL_NO_DEPRECATED | - | ||||||||||||||||||||||||
77 | /* For a 32 bit machine | - | ||||||||||||||||||||||||
78 | * 2 - 4 == 128 | - | ||||||||||||||||||||||||
79 | * 3 - 8 == 256 | - | ||||||||||||||||||||||||
80 | * 4 - 16 == 512 | - | ||||||||||||||||||||||||
81 | * 5 - 32 == 1024 | - | ||||||||||||||||||||||||
82 | * 6 - 64 == 2048 | - | ||||||||||||||||||||||||
83 | * 7 - 128 == 4096 | - | ||||||||||||||||||||||||
84 | * 8 - 256 == 8192 | - | ||||||||||||||||||||||||
85 | */ | - | ||||||||||||||||||||||||
86 | static int bn_limit_bits = 0; | - | ||||||||||||||||||||||||
87 | static int bn_limit_num = 8; /* (1<<bn_limit_bits) */ | - | ||||||||||||||||||||||||
88 | static int bn_limit_bits_low = 0; | - | ||||||||||||||||||||||||
89 | static int bn_limit_num_low = 8; /* (1<<bn_limit_bits_low) */ | - | ||||||||||||||||||||||||
90 | static int bn_limit_bits_high = 0; | - | ||||||||||||||||||||||||
91 | static int bn_limit_num_high = 8; /* (1<<bn_limit_bits_high) */ | - | ||||||||||||||||||||||||
92 | static int bn_limit_bits_mont = 0; | - | ||||||||||||||||||||||||
93 | static int bn_limit_num_mont = 8; /* (1<<bn_limit_bits_mont) */ | - | ||||||||||||||||||||||||
94 | - | |||||||||||||||||||||||||
95 | void | - | ||||||||||||||||||||||||
96 | BN_set_params(int mult, int high, int low, int mont) | - | ||||||||||||||||||||||||
97 | { | - | ||||||||||||||||||||||||
98 | if (mult >= 0) {
| 0 | ||||||||||||||||||||||||
99 | if (mult > (int)(sizeof(int) * 8) - 1)
| 0 | ||||||||||||||||||||||||
100 | mult = sizeof(int) * 8 - 1; never executed: mult = sizeof(int) * 8 - 1; | 0 | ||||||||||||||||||||||||
101 | bn_limit_bits = mult; | - | ||||||||||||||||||||||||
102 | bn_limit_num = 1 << mult; | - | ||||||||||||||||||||||||
103 | } never executed: end of block | 0 | ||||||||||||||||||||||||
104 | if (high >= 0) {
| 0 | ||||||||||||||||||||||||
105 | if (high > (int)(sizeof(int) * 8) - 1)
| 0 | ||||||||||||||||||||||||
106 | high = sizeof(int) * 8 - 1; never executed: high = sizeof(int) * 8 - 1; | 0 | ||||||||||||||||||||||||
107 | bn_limit_bits_high = high; | - | ||||||||||||||||||||||||
108 | bn_limit_num_high = 1 << high; | - | ||||||||||||||||||||||||
109 | } never executed: end of block | 0 | ||||||||||||||||||||||||
110 | if (low >= 0) {
| 0 | ||||||||||||||||||||||||
111 | if (low > (int)(sizeof(int) * 8) - 1)
| 0 | ||||||||||||||||||||||||
112 | low = sizeof(int) * 8 - 1; never executed: low = sizeof(int) * 8 - 1; | 0 | ||||||||||||||||||||||||
113 | bn_limit_bits_low = low; | - | ||||||||||||||||||||||||
114 | bn_limit_num_low = 1 << low; | - | ||||||||||||||||||||||||
115 | } never executed: end of block | 0 | ||||||||||||||||||||||||
116 | if (mont >= 0) {
| 0 | ||||||||||||||||||||||||
117 | if (mont > (int)(sizeof(int) * 8) - 1)
| 0 | ||||||||||||||||||||||||
118 | mont = sizeof(int) * 8 - 1; never executed: mont = sizeof(int) * 8 - 1; | 0 | ||||||||||||||||||||||||
119 | bn_limit_bits_mont = mont; | - | ||||||||||||||||||||||||
120 | bn_limit_num_mont = 1 << mont; | - | ||||||||||||||||||||||||
121 | } never executed: end of block | 0 | ||||||||||||||||||||||||
122 | } never executed: end of block | 0 | ||||||||||||||||||||||||
123 | - | |||||||||||||||||||||||||
124 | int | - | ||||||||||||||||||||||||
125 | BN_get_params(int which) | - | ||||||||||||||||||||||||
126 | { | - | ||||||||||||||||||||||||
127 | if (which == 0)
| 0 | ||||||||||||||||||||||||
128 | return (bn_limit_bits); never executed: return (bn_limit_bits); | 0 | ||||||||||||||||||||||||
129 | else if (which == 1)
| 0 | ||||||||||||||||||||||||
130 | return (bn_limit_bits_high); never executed: return (bn_limit_bits_high); | 0 | ||||||||||||||||||||||||
131 | else if (which == 2)
| 0 | ||||||||||||||||||||||||
132 | return (bn_limit_bits_low); never executed: return (bn_limit_bits_low); | 0 | ||||||||||||||||||||||||
133 | else if (which == 3)
| 0 | ||||||||||||||||||||||||
134 | return (bn_limit_bits_mont); never executed: return (bn_limit_bits_mont); | 0 | ||||||||||||||||||||||||
135 | else | - | ||||||||||||||||||||||||
136 | return (0); never executed: return (0); | 0 | ||||||||||||||||||||||||
137 | } | - | ||||||||||||||||||||||||
138 | #endif | - | ||||||||||||||||||||||||
139 | - | |||||||||||||||||||||||||
140 | const BIGNUM * | - | ||||||||||||||||||||||||
141 | BN_value_one(void) | - | ||||||||||||||||||||||||
142 | { | - | ||||||||||||||||||||||||
143 | static const BN_ULONG data_one = 1L; | - | ||||||||||||||||||||||||
144 | static const BIGNUM const_one = { | - | ||||||||||||||||||||||||
145 | (BN_ULONG *)&data_one, 1, 1, 0, BN_FLG_STATIC_DATA | - | ||||||||||||||||||||||||
146 | }; | - | ||||||||||||||||||||||||
147 | - | |||||||||||||||||||||||||
148 | return (&const_one); executed 222554 times by 14 tests: return (&const_one); Executed by:
| 222554 | ||||||||||||||||||||||||
149 | } | - | ||||||||||||||||||||||||
150 | - | |||||||||||||||||||||||||
151 | int | - | ||||||||||||||||||||||||
152 | BN_num_bits_word(BN_ULONG l) | - | ||||||||||||||||||||||||
153 | { | - | ||||||||||||||||||||||||
154 | static const unsigned char bits[256] = { | - | ||||||||||||||||||||||||
155 | 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, | - | ||||||||||||||||||||||||
156 | 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, | - | ||||||||||||||||||||||||
157 | 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, | - | ||||||||||||||||||||||||
158 | 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, | - | ||||||||||||||||||||||||
159 | 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, | - | ||||||||||||||||||||||||
160 | 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, | - | ||||||||||||||||||||||||
161 | 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, | - | ||||||||||||||||||||||||
162 | 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, | - | ||||||||||||||||||||||||
163 | 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, | - | ||||||||||||||||||||||||
164 | 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, | - | ||||||||||||||||||||||||
165 | 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, | - | ||||||||||||||||||||||||
166 | 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, | - | ||||||||||||||||||||||||
167 | 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, | - | ||||||||||||||||||||||||
168 | 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, | - | ||||||||||||||||||||||||
169 | 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, | - | ||||||||||||||||||||||||
170 | 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, | - | ||||||||||||||||||||||||
171 | }; | - | ||||||||||||||||||||||||
172 | - | |||||||||||||||||||||||||
173 | #ifdef _LP64 | - | ||||||||||||||||||||||||
174 | if (l & 0xffffffff00000000L) {
| 6925827-7867498 | ||||||||||||||||||||||||
175 | if (l & 0xffff000000000000L) {
| 3294971-4572527 | ||||||||||||||||||||||||
176 | if (l & 0xff00000000000000L) {
| 1456573-3115954 | ||||||||||||||||||||||||
177 | return (bits[(int)(l >> 56)] + 56); executed 3115954 times by 14 tests: return (bits[(int)(l >> 56)] + 56); Executed by:
| 3115954 | ||||||||||||||||||||||||
178 | } else | - | ||||||||||||||||||||||||
179 | return (bits[(int)(l >> 48)] + 48); executed 1456573 times by 14 tests: return (bits[(int)(l >> 48)] + 48); Executed by:
| 1456573 | ||||||||||||||||||||||||
180 | } else { | - | ||||||||||||||||||||||||
181 | if (l & 0x0000ff0000000000L) {
| 1617700-1677271 | ||||||||||||||||||||||||
182 | return (bits[(int)(l >> 40)] + 40); executed 1677271 times by 13 tests: return (bits[(int)(l >> 40)] + 40); Executed by:
| 1677271 | ||||||||||||||||||||||||
183 | } else | - | ||||||||||||||||||||||||
184 | return (bits[(int)(l >> 32)] + 32); executed 1617700 times by 13 tests: return (bits[(int)(l >> 32)] + 32); Executed by:
| 1617700 | ||||||||||||||||||||||||
185 | } | - | ||||||||||||||||||||||||
186 | } else | - | ||||||||||||||||||||||||
187 | #endif | - | ||||||||||||||||||||||||
188 | { | - | ||||||||||||||||||||||||
189 | if (l & 0xffff0000L) {
| 3353502-3572325 | ||||||||||||||||||||||||
190 | if (l & 0xff000000L)
| 1649135-1923190 | ||||||||||||||||||||||||
191 | return (bits[(int)(l >> 24L)] + 24); executed 1923190 times by 14 tests: return (bits[(int)(l >> 24L)] + 24); Executed by:
| 1923190 | ||||||||||||||||||||||||
192 | else | - | ||||||||||||||||||||||||
193 | return (bits[(int)(l >> 16L)] + 16); executed 1649135 times by 14 tests: return (bits[(int)(l >> 16L)] + 16); Executed by:
| 1649135 | ||||||||||||||||||||||||
194 | } else { | - | ||||||||||||||||||||||||
195 | if (l & 0xff00L)
| 1657899-1695603 | ||||||||||||||||||||||||
196 | return (bits[(int)(l >> 8)] + 8); executed 1695603 times by 14 tests: return (bits[(int)(l >> 8)] + 8); Executed by:
| 1695603 | ||||||||||||||||||||||||
197 | else | - | ||||||||||||||||||||||||
198 | return (bits[(int)(l)]); executed 1657899 times by 14 tests: return (bits[(int)(l)]); Executed by:
| 1657899 | ||||||||||||||||||||||||
199 | } | - | ||||||||||||||||||||||||
200 | } | - | ||||||||||||||||||||||||
201 | } | - | ||||||||||||||||||||||||
202 | - | |||||||||||||||||||||||||
203 | int | - | ||||||||||||||||||||||||
204 | BN_num_bits(const BIGNUM *a) | - | ||||||||||||||||||||||||
205 | { | - | ||||||||||||||||||||||||
206 | int i = a->top - 1; | - | ||||||||||||||||||||||||
207 | - | |||||||||||||||||||||||||
208 | bn_check_top(a); | - | ||||||||||||||||||||||||
209 | - | |||||||||||||||||||||||||
210 | if (BN_is_zero(a))
| 52-3351858 | ||||||||||||||||||||||||
211 | return 0; executed 52 times by 5 tests: return 0; Executed by:
| 52 | ||||||||||||||||||||||||
212 | return ((i * BN_BITS2) + BN_num_bits_word(a->d[i])); executed 3351858 times by 14 tests: return ((i * 64) + BN_num_bits_word(a->d[i])); Executed by:
| 3351858 | ||||||||||||||||||||||||
213 | } | - | ||||||||||||||||||||||||
214 | - | |||||||||||||||||||||||||
215 | void | - | ||||||||||||||||||||||||
216 | BN_clear_free(BIGNUM *a) | - | ||||||||||||||||||||||||
217 | { | - | ||||||||||||||||||||||||
218 | int i; | - | ||||||||||||||||||||||||
219 | - | |||||||||||||||||||||||||
220 | if (a == NULL)
| 3284-80864 | ||||||||||||||||||||||||
221 | return; executed 3284 times by 13 tests: return; Executed by:
| 3284 | ||||||||||||||||||||||||
222 | bn_check_top(a); | - | ||||||||||||||||||||||||
223 | if (a->d != NULL && !(BN_get_flags(a, BN_FLG_STATIC_DATA)))
| 0-77548 | ||||||||||||||||||||||||
224 | freezero(a->d, a->dmax * sizeof(a->d[0])); executed 77548 times by 15 tests: freezero(a->d, a->dmax * sizeof(a->d[0])); Executed by:
| 77548 | ||||||||||||||||||||||||
225 | i = BN_get_flags(a, BN_FLG_MALLOCED); | - | ||||||||||||||||||||||||
226 | explicit_bzero(a, sizeof(BIGNUM)); | - | ||||||||||||||||||||||||
227 | if (i)
| 9442-71422 | ||||||||||||||||||||||||
228 | free(a); executed 9442 times by 14 tests: free(a); Executed by:
| 9442 | ||||||||||||||||||||||||
229 | } executed 80864 times by 15 tests: end of block Executed by:
| 80864 | ||||||||||||||||||||||||
230 | - | |||||||||||||||||||||||||
231 | void | - | ||||||||||||||||||||||||
232 | BN_free(BIGNUM *a) | - | ||||||||||||||||||||||||
233 | { | - | ||||||||||||||||||||||||
234 | BN_clear_free(a); | - | ||||||||||||||||||||||||
235 | } executed 27735 times by 16 tests: end of block Executed by:
| 27735 | ||||||||||||||||||||||||
236 | - | |||||||||||||||||||||||||
237 | void | - | ||||||||||||||||||||||||
238 | BN_init(BIGNUM *a) | - | ||||||||||||||||||||||||
239 | { | - | ||||||||||||||||||||||||
240 | memset(a, 0, sizeof(BIGNUM)); | - | ||||||||||||||||||||||||
241 | bn_check_top(a); | - | ||||||||||||||||||||||||
242 | } executed 93037 times by 14 tests: end of block Executed by:
| 93037 | ||||||||||||||||||||||||
243 | - | |||||||||||||||||||||||||
244 | BIGNUM * | - | ||||||||||||||||||||||||
245 | BN_new(void) | - | ||||||||||||||||||||||||
246 | { | - | ||||||||||||||||||||||||
247 | BIGNUM *ret; | - | ||||||||||||||||||||||||
248 | - | |||||||||||||||||||||||||
249 | if ((ret = malloc(sizeof(BIGNUM))) == NULL) {
| 0-9458 | ||||||||||||||||||||||||
250 | BNerror(ERR_R_MALLOC_FAILURE); | - | ||||||||||||||||||||||||
251 | return (NULL); never executed: return ( ((void *)0) ); | 0 | ||||||||||||||||||||||||
252 | } | - | ||||||||||||||||||||||||
253 | ret->flags = BN_FLG_MALLOCED; | - | ||||||||||||||||||||||||
254 | ret->top = 0; | - | ||||||||||||||||||||||||
255 | ret->neg = 0; | - | ||||||||||||||||||||||||
256 | ret->dmax = 0; | - | ||||||||||||||||||||||||
257 | ret->d = NULL; | - | ||||||||||||||||||||||||
258 | bn_check_top(ret); | - | ||||||||||||||||||||||||
259 | return (ret); executed 9458 times by 15 tests: return (ret); Executed by:
| 9458 | ||||||||||||||||||||||||
260 | } | - | ||||||||||||||||||||||||
261 | - | |||||||||||||||||||||||||
262 | /* This is used both by bn_expand2() and bn_dup_expand() */ | - | ||||||||||||||||||||||||
263 | /* The caller MUST check that words > b->dmax before calling this */ | - | ||||||||||||||||||||||||
264 | static BN_ULONG * | - | ||||||||||||||||||||||||
265 | bn_expand_internal(const BIGNUM *b, int words) | - | ||||||||||||||||||||||||
266 | { | - | ||||||||||||||||||||||||
267 | BN_ULONG *A, *a = NULL; | - | ||||||||||||||||||||||||
268 | const BN_ULONG *B; | - | ||||||||||||||||||||||||
269 | int i; | - | ||||||||||||||||||||||||
270 | - | |||||||||||||||||||||||||
271 | bn_check_top(b); | - | ||||||||||||||||||||||||
272 | - | |||||||||||||||||||||||||
273 | if (words > (INT_MAX/(4*BN_BITS2))) {
| 3-133952 | ||||||||||||||||||||||||
274 | BNerror(BN_R_BIGNUM_TOO_LONG); | - | ||||||||||||||||||||||||
275 | return NULL; executed 3 times by 1 test: return ((void *)0) ; Executed by:
| 3 | ||||||||||||||||||||||||
276 | } | - | ||||||||||||||||||||||||
277 | if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {
| 0-133952 | ||||||||||||||||||||||||
278 | BNerror(BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); | - | ||||||||||||||||||||||||
279 | return (NULL); never executed: return ( ((void *)0) ); | 0 | ||||||||||||||||||||||||
280 | } | - | ||||||||||||||||||||||||
281 | a = A = reallocarray(NULL, words, sizeof(BN_ULONG)); | - | ||||||||||||||||||||||||
282 | if (A == NULL) {
| 0-133952 | ||||||||||||||||||||||||
283 | BNerror(ERR_R_MALLOC_FAILURE); | - | ||||||||||||||||||||||||
284 | return (NULL); never executed: return ( ((void *)0) ); | 0 | ||||||||||||||||||||||||
285 | } | - | ||||||||||||||||||||||||
286 | #if 1 | - | ||||||||||||||||||||||||
287 | B = b->d; | - | ||||||||||||||||||||||||
288 | /* Check if the previous number needs to be copied */ | - | ||||||||||||||||||||||||
289 | if (B != NULL) {
| 56380-77572 | ||||||||||||||||||||||||
290 | for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) {
| 11653-56380 | ||||||||||||||||||||||||
291 | /* | - | ||||||||||||||||||||||||
292 | * The fact that the loop is unrolled | - | ||||||||||||||||||||||||
293 | * 4-wise is a tribute to Intel. It's | - | ||||||||||||||||||||||||
294 | * the one that doesn't have enough | - | ||||||||||||||||||||||||
295 | * registers to accommodate more data. | - | ||||||||||||||||||||||||
296 | * I'd unroll it 8-wise otherwise:-) | - | ||||||||||||||||||||||||
297 | * | - | ||||||||||||||||||||||||
298 | * <appro@fy.chalmers.se> | - | ||||||||||||||||||||||||
299 | */ | - | ||||||||||||||||||||||||
300 | BN_ULONG a0, a1, a2, a3; | - | ||||||||||||||||||||||||
301 | a0 = B[0]; | - | ||||||||||||||||||||||||
302 | a1 = B[1]; | - | ||||||||||||||||||||||||
303 | a2 = B[2]; | - | ||||||||||||||||||||||||
304 | a3 = B[3]; | - | ||||||||||||||||||||||||
305 | A[0] = a0; | - | ||||||||||||||||||||||||
306 | A[1] = a1; | - | ||||||||||||||||||||||||
307 | A[2] = a2; | - | ||||||||||||||||||||||||
308 | A[3] = a3; | - | ||||||||||||||||||||||||
309 | } executed 11653 times by 12 tests: end of block Executed by:
| 11653 | ||||||||||||||||||||||||
310 | switch (b->top & 3) { | - | ||||||||||||||||||||||||
311 | case 3: executed 1676 times by 9 tests: case 3: Executed by:
| 1676 | ||||||||||||||||||||||||
312 | A[2] = B[2]; | - | ||||||||||||||||||||||||
313 | case 2: code before this statement executed 1676 times by 9 tests: case 2: Executed by:
executed 1481 times by 15 tests: case 2: Executed by:
| 1481-1676 | ||||||||||||||||||||||||
314 | A[1] = B[1]; | - | ||||||||||||||||||||||||
315 | case 1: code before this statement executed 3157 times by 15 tests: case 1: Executed by:
executed 10453 times by 15 tests: case 1: Executed by:
| 3157-10453 | ||||||||||||||||||||||||
316 | A[0] = B[0]; | - | ||||||||||||||||||||||||
317 | } executed 10453 times by 15 tests: end of block Executed by:
| 10453 | ||||||||||||||||||||||||
318 | } executed 56380 times by 15 tests: end of block Executed by:
| 56380 | ||||||||||||||||||||||||
319 | - | |||||||||||||||||||||||||
320 | #else | - | ||||||||||||||||||||||||
321 | memset(A, 0, sizeof(BN_ULONG) * words); | - | ||||||||||||||||||||||||
322 | memcpy(A, b->d, sizeof(b->d[0]) * b->top); | - | ||||||||||||||||||||||||
323 | #endif | - | ||||||||||||||||||||||||
324 | - | |||||||||||||||||||||||||
325 | return (a); executed 133952 times by 15 tests: return (a); Executed by:
| 133952 | ||||||||||||||||||||||||
326 | } | - | ||||||||||||||||||||||||
327 | - | |||||||||||||||||||||||||
328 | /* This is an internal function that can be used instead of bn_expand2() | - | ||||||||||||||||||||||||
329 | * when there is a need to copy BIGNUMs instead of only expanding the | - | ||||||||||||||||||||||||
330 | * data part, while still expanding them. | - | ||||||||||||||||||||||||
331 | * Especially useful when needing to expand BIGNUMs that are declared | - | ||||||||||||||||||||||||
332 | * 'const' and should therefore not be changed. | - | ||||||||||||||||||||||||
333 | * The reason to use this instead of a BN_dup() followed by a bn_expand2() | - | ||||||||||||||||||||||||
334 | * is memory allocation overhead. A BN_dup() followed by a bn_expand2() | - | ||||||||||||||||||||||||
335 | * will allocate new memory for the BIGNUM data twice, and free it once, | - | ||||||||||||||||||||||||
336 | * while bn_dup_expand() makes sure allocation is made only once. | - | ||||||||||||||||||||||||
337 | */ | - | ||||||||||||||||||||||||
338 | - | |||||||||||||||||||||||||
339 | #ifndef OPENSSL_NO_DEPRECATED | - | ||||||||||||||||||||||||
340 | BIGNUM * | - | ||||||||||||||||||||||||
341 | bn_dup_expand(const BIGNUM *b, int words) | - | ||||||||||||||||||||||||
342 | { | - | ||||||||||||||||||||||||
343 | BIGNUM *r = NULL; | - | ||||||||||||||||||||||||
344 | - | |||||||||||||||||||||||||
345 | bn_check_top(b); | - | ||||||||||||||||||||||||
346 | - | |||||||||||||||||||||||||
347 | /* This function does not work if | - | ||||||||||||||||||||||||
348 | * words <= b->dmax && top < words | - | ||||||||||||||||||||||||
349 | * because BN_dup() does not preserve 'dmax'! | - | ||||||||||||||||||||||||
350 | * (But bn_dup_expand() is not used anywhere yet.) | - | ||||||||||||||||||||||||
351 | */ | - | ||||||||||||||||||||||||
352 | - | |||||||||||||||||||||||||
353 | if (words > b->dmax) {
| 0 | ||||||||||||||||||||||||
354 | BN_ULONG *a = bn_expand_internal(b, words); | - | ||||||||||||||||||||||||
355 | - | |||||||||||||||||||||||||
356 | if (a) {
| 0 | ||||||||||||||||||||||||
357 | r = BN_new(); | - | ||||||||||||||||||||||||
358 | if (r) {
| 0 | ||||||||||||||||||||||||
359 | r->top = b->top; | - | ||||||||||||||||||||||||
360 | r->dmax = words; | - | ||||||||||||||||||||||||
361 | r->neg = b->neg; | - | ||||||||||||||||||||||||
362 | r->d = a; | - | ||||||||||||||||||||||||
363 | } else { never executed: end of block | 0 | ||||||||||||||||||||||||
364 | /* r == NULL, BN_new failure */ | - | ||||||||||||||||||||||||
365 | free(a); | - | ||||||||||||||||||||||||
366 | } never executed: end of block | 0 | ||||||||||||||||||||||||
367 | } | - | ||||||||||||||||||||||||
368 | /* If a == NULL, there was an error in allocation in | - | ||||||||||||||||||||||||
369 | bn_expand_internal(), and NULL should be returned */ | - | ||||||||||||||||||||||||
370 | } else { never executed: end of block | 0 | ||||||||||||||||||||||||
371 | r = BN_dup(b); | - | ||||||||||||||||||||||||
372 | } never executed: end of block | 0 | ||||||||||||||||||||||||
373 | - | |||||||||||||||||||||||||
374 | bn_check_top(r); | - | ||||||||||||||||||||||||
375 | return r; never executed: return r; | 0 | ||||||||||||||||||||||||
376 | } | - | ||||||||||||||||||||||||
377 | #endif | - | ||||||||||||||||||||||||
378 | - | |||||||||||||||||||||||||
379 | /* This is an internal function that should not be used in applications. | - | ||||||||||||||||||||||||
380 | * It ensures that 'b' has enough room for a 'words' word number | - | ||||||||||||||||||||||||
381 | * and initialises any unused part of b->d with leading zeros. | - | ||||||||||||||||||||||||
382 | * It is mostly used by the various BIGNUM routines. If there is an error, | - | ||||||||||||||||||||||||
383 | * NULL is returned. If not, 'b' is returned. */ | - | ||||||||||||||||||||||||
384 | - | |||||||||||||||||||||||||
385 | BIGNUM * | - | ||||||||||||||||||||||||
386 | bn_expand2(BIGNUM *b, int words) | - | ||||||||||||||||||||||||
387 | { | - | ||||||||||||||||||||||||
388 | bn_check_top(b); | - | ||||||||||||||||||||||||
389 | - | |||||||||||||||||||||||||
390 | if (words > b->dmax) {
| 0-133955 | ||||||||||||||||||||||||
391 | BN_ULONG *a = bn_expand_internal(b, words); | - | ||||||||||||||||||||||||
392 | if (!a)
| 3-133952 | ||||||||||||||||||||||||
393 | return NULL; executed 3 times by 1 test: return ((void *)0) ; Executed by:
| 3 | ||||||||||||||||||||||||
394 | if (b->d)
| 56380-77572 | ||||||||||||||||||||||||
395 | freezero(b->d, b->dmax * sizeof(b->d[0])); executed 56380 times by 15 tests: freezero(b->d, b->dmax * sizeof(b->d[0])); Executed by:
| 56380 | ||||||||||||||||||||||||
396 | b->d = a; | - | ||||||||||||||||||||||||
397 | b->dmax = words; | - | ||||||||||||||||||||||||
398 | } executed 133952 times by 15 tests: end of block Executed by:
| 133952 | ||||||||||||||||||||||||
399 | - | |||||||||||||||||||||||||
400 | /* None of this should be necessary because of what b->top means! */ | - | ||||||||||||||||||||||||
401 | #if 0 | - | ||||||||||||||||||||||||
402 | /* NB: bn_wexpand() calls this only if the BIGNUM really has to grow */ | - | ||||||||||||||||||||||||
403 | if (b->top < b->dmax) { | - | ||||||||||||||||||||||||
404 | int i; | - | ||||||||||||||||||||||||
405 | BN_ULONG *A = &(b->d[b->top]); | - | ||||||||||||||||||||||||
406 | for (i = (b->dmax - b->top) >> 3; i > 0; i--, A += 8) { | - | ||||||||||||||||||||||||
407 | A[0] = 0; | - | ||||||||||||||||||||||||
408 | A[1] = 0; | - | ||||||||||||||||||||||||
409 | A[2] = 0; | - | ||||||||||||||||||||||||
410 | A[3] = 0; | - | ||||||||||||||||||||||||
411 | A[4] = 0; | - | ||||||||||||||||||||||||
412 | A[5] = 0; | - | ||||||||||||||||||||||||
413 | A[6] = 0; | - | ||||||||||||||||||||||||
414 | A[7] = 0; | - | ||||||||||||||||||||||||
415 | } | - | ||||||||||||||||||||||||
416 | for (i = (b->dmax - b->top)&7; i > 0; i--, A++) | - | ||||||||||||||||||||||||
417 | A[0] = 0; | - | ||||||||||||||||||||||||
418 | assert(A == &(b->d[b->dmax])); | - | ||||||||||||||||||||||||
419 | } | - | ||||||||||||||||||||||||
420 | #endif | - | ||||||||||||||||||||||||
421 | bn_check_top(b); | - | ||||||||||||||||||||||||
422 | return b; executed 133952 times by 15 tests: return b; Executed by:
| 133952 | ||||||||||||||||||||||||
423 | } | - | ||||||||||||||||||||||||
424 | - | |||||||||||||||||||||||||
425 | BIGNUM * | - | ||||||||||||||||||||||||
426 | BN_dup(const BIGNUM *a) | - | ||||||||||||||||||||||||
427 | { | - | ||||||||||||||||||||||||
428 | BIGNUM *t; | - | ||||||||||||||||||||||||
429 | - | |||||||||||||||||||||||||
430 | if (a == NULL)
| 0-390 | ||||||||||||||||||||||||
431 | return NULL; never executed: return ((void *)0) ; | 0 | ||||||||||||||||||||||||
432 | bn_check_top(a); | - | ||||||||||||||||||||||||
433 | - | |||||||||||||||||||||||||
434 | t = BN_new(); | - | ||||||||||||||||||||||||
435 | if (t == NULL)
| 0-390 | ||||||||||||||||||||||||
436 | return NULL; never executed: return ((void *)0) ; | 0 | ||||||||||||||||||||||||
437 | if (!BN_copy(t, a)) {
| 0-390 | ||||||||||||||||||||||||
438 | BN_free(t); | - | ||||||||||||||||||||||||
439 | return NULL; never executed: return ((void *)0) ; | 0 | ||||||||||||||||||||||||
440 | } | - | ||||||||||||||||||||||||
441 | bn_check_top(t); | - | ||||||||||||||||||||||||
442 | return t; executed 390 times by 11 tests: return t; Executed by:
| 390 | ||||||||||||||||||||||||
443 | } | - | ||||||||||||||||||||||||
444 | - | |||||||||||||||||||||||||
445 | BIGNUM * | - | ||||||||||||||||||||||||
446 | BN_copy(BIGNUM *a, const BIGNUM *b) | - | ||||||||||||||||||||||||
447 | { | - | ||||||||||||||||||||||||
448 | int i; | - | ||||||||||||||||||||||||
449 | BN_ULONG *A; | - | ||||||||||||||||||||||||
450 | const BN_ULONG *B; | - | ||||||||||||||||||||||||
451 | - | |||||||||||||||||||||||||
452 | bn_check_top(b); | - | ||||||||||||||||||||||||
453 | - | |||||||||||||||||||||||||
454 | if (a == b)
| 188-2213451 | ||||||||||||||||||||||||
455 | return (a); executed 188 times by 1 test: return (a); Executed by:
| 188 | ||||||||||||||||||||||||
456 | if (bn_wexpand(a, b->top) == NULL)
| 0-2213451 | ||||||||||||||||||||||||
457 | return (NULL); never executed: return ( ((void *)0) ); | 0 | ||||||||||||||||||||||||
458 | - | |||||||||||||||||||||||||
459 | #if 1 | - | ||||||||||||||||||||||||
460 | A = a->d; | - | ||||||||||||||||||||||||
461 | B = b->d; | - | ||||||||||||||||||||||||
462 | for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) {
| 1898508-2213451 | ||||||||||||||||||||||||
463 | BN_ULONG a0, a1, a2, a3; | - | ||||||||||||||||||||||||
464 | a0 = B[0]; | - | ||||||||||||||||||||||||
465 | a1 = B[1]; | - | ||||||||||||||||||||||||
466 | a2 = B[2]; | - | ||||||||||||||||||||||||
467 | a3 = B[3]; | - | ||||||||||||||||||||||||
468 | A[0] = a0; | - | ||||||||||||||||||||||||
469 | A[1] = a1; | - | ||||||||||||||||||||||||
470 | A[2] = a2; | - | ||||||||||||||||||||||||
471 | A[3] = a3; | - | ||||||||||||||||||||||||
472 | } executed 1898508 times by 12 tests: end of block Executed by:
| 1898508 | ||||||||||||||||||||||||
473 | switch (b->top & 3) { | - | ||||||||||||||||||||||||
474 | case 3: executed 712769 times by 8 tests: case 3: Executed by:
| 712769 | ||||||||||||||||||||||||
475 | A[2] = B[2]; | - | ||||||||||||||||||||||||
476 | case 2: code before this statement executed 712769 times by 8 tests: case 2: Executed by:
executed 145262 times by 14 tests: case 2: Executed by:
| 145262-712769 | ||||||||||||||||||||||||
477 | A[1] = B[1]; | - | ||||||||||||||||||||||||
478 | case 1: code before this statement executed 858031 times by 14 tests: case 1: Executed by:
executed 1647252 times by 14 tests: case 1: Executed by:
| 858031-1647252 | ||||||||||||||||||||||||
479 | A[0] = B[0]; | - | ||||||||||||||||||||||||
480 | } executed 1647252 times by 14 tests: end of block Executed by:
| 1647252 | ||||||||||||||||||||||||
481 | #else | - | ||||||||||||||||||||||||
482 | memcpy(a->d, b->d, sizeof(b->d[0]) * b->top); | - | ||||||||||||||||||||||||
483 | #endif | - | ||||||||||||||||||||||||
484 | - | |||||||||||||||||||||||||
485 | a->top = b->top; | - | ||||||||||||||||||||||||
486 | a->neg = b->neg; | - | ||||||||||||||||||||||||
487 | bn_check_top(a); | - | ||||||||||||||||||||||||
488 | return (a); executed 2213451 times by 14 tests: return (a); Executed by:
| 2213451 | ||||||||||||||||||||||||
489 | } | - | ||||||||||||||||||||||||
490 | - | |||||||||||||||||||||||||
491 | void | - | ||||||||||||||||||||||||
492 | BN_swap(BIGNUM *a, BIGNUM *b) | - | ||||||||||||||||||||||||
493 | { | - | ||||||||||||||||||||||||
494 | int flags_old_a, flags_old_b; | - | ||||||||||||||||||||||||
495 | BN_ULONG *tmp_d; | - | ||||||||||||||||||||||||
496 | int tmp_top, tmp_dmax, tmp_neg; | - | ||||||||||||||||||||||||
497 | - | |||||||||||||||||||||||||
498 | bn_check_top(a); | - | ||||||||||||||||||||||||
499 | bn_check_top(b); | - | ||||||||||||||||||||||||
500 | - | |||||||||||||||||||||||||
501 | flags_old_a = a->flags; | - | ||||||||||||||||||||||||
502 | flags_old_b = b->flags; | - | ||||||||||||||||||||||||
503 | - | |||||||||||||||||||||||||
504 | tmp_d = a->d; | - | ||||||||||||||||||||||||
505 | tmp_top = a->top; | - | ||||||||||||||||||||||||
506 | tmp_dmax = a->dmax; | - | ||||||||||||||||||||||||
507 | tmp_neg = a->neg; | - | ||||||||||||||||||||||||
508 | - | |||||||||||||||||||||||||
509 | a->d = b->d; | - | ||||||||||||||||||||||||
510 | a->top = b->top; | - | ||||||||||||||||||||||||
511 | a->dmax = b->dmax; | - | ||||||||||||||||||||||||
512 | a->neg = b->neg; | - | ||||||||||||||||||||||||
513 | - | |||||||||||||||||||||||||
514 | b->d = tmp_d; | - | ||||||||||||||||||||||||
515 | b->top = tmp_top; | - | ||||||||||||||||||||||||
516 | b->dmax = tmp_dmax; | - | ||||||||||||||||||||||||
517 | b->neg = tmp_neg; | - | ||||||||||||||||||||||||
518 | - | |||||||||||||||||||||||||
519 | a->flags = (flags_old_a & BN_FLG_MALLOCED) | | - | ||||||||||||||||||||||||
520 | (flags_old_b & BN_FLG_STATIC_DATA); | - | ||||||||||||||||||||||||
521 | b->flags = (flags_old_b & BN_FLG_MALLOCED) | | - | ||||||||||||||||||||||||
522 | (flags_old_a & BN_FLG_STATIC_DATA); | - | ||||||||||||||||||||||||
523 | bn_check_top(a); | - | ||||||||||||||||||||||||
524 | bn_check_top(b); | - | ||||||||||||||||||||||||
525 | } never executed: end of block | 0 | ||||||||||||||||||||||||
526 | - | |||||||||||||||||||||||||
527 | void | - | ||||||||||||||||||||||||
528 | BN_clear(BIGNUM *a) | - | ||||||||||||||||||||||||
529 | { | - | ||||||||||||||||||||||||
530 | bn_check_top(a); | - | ||||||||||||||||||||||||
531 | if (a->d != NULL)
| 0 | ||||||||||||||||||||||||
532 | explicit_bzero(a->d, a->dmax * sizeof(a->d[0])); never executed: explicit_bzero(a->d, a->dmax * sizeof(a->d[0])); | 0 | ||||||||||||||||||||||||
533 | a->top = 0; | - | ||||||||||||||||||||||||
534 | a->neg = 0; | - | ||||||||||||||||||||||||
535 | } never executed: end of block | 0 | ||||||||||||||||||||||||
536 | - | |||||||||||||||||||||||||
537 | BN_ULONG | - | ||||||||||||||||||||||||
538 | BN_get_word(const BIGNUM *a) | - | ||||||||||||||||||||||||
539 | { | - | ||||||||||||||||||||||||
540 | if (a->top > 1)
| 0 | ||||||||||||||||||||||||
541 | return BN_MASK2; never executed: return (0xffffffffffffffffL); | 0 | ||||||||||||||||||||||||
542 | else if (a->top == 1)
| 0 | ||||||||||||||||||||||||
543 | return a->d[0]; never executed: return a->d[0]; | 0 | ||||||||||||||||||||||||
544 | /* a->top == 0 */ | - | ||||||||||||||||||||||||
545 | return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||
546 | } | - | ||||||||||||||||||||||||
547 | - | |||||||||||||||||||||||||
548 | BIGNUM * | - | ||||||||||||||||||||||||
549 | bn_expand(BIGNUM *a, int bits) | - | ||||||||||||||||||||||||
550 | { | - | ||||||||||||||||||||||||
551 | if (bits > (INT_MAX - BN_BITS2 + 1))
| 0-8442108 | ||||||||||||||||||||||||
552 | return (NULL); never executed: return ( ((void *)0) ); | 0 | ||||||||||||||||||||||||
553 | - | |||||||||||||||||||||||||
554 | if (((bits + BN_BITS2 - 1) / BN_BITS2) <= a->dmax)
| 31354-8410754 | ||||||||||||||||||||||||
555 | return (a); executed 8410754 times by 15 tests: return (a); Executed by:
| 8410754 | ||||||||||||||||||||||||
556 | - | |||||||||||||||||||||||||
557 | return bn_expand2(a, (bits + BN_BITS2 - 1) / BN_BITS2); executed 31354 times by 15 tests: return bn_expand2(a, (bits + 64 - 1) / 64); Executed by:
| 31354 | ||||||||||||||||||||||||
558 | } | - | ||||||||||||||||||||||||
559 | - | |||||||||||||||||||||||||
560 | int | - | ||||||||||||||||||||||||
561 | BN_set_word(BIGNUM *a, BN_ULONG w) | - | ||||||||||||||||||||||||
562 | { | - | ||||||||||||||||||||||||
563 | bn_check_top(a); | - | ||||||||||||||||||||||||
564 | if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)
| 0-8441928 | ||||||||||||||||||||||||
565 | return (0); never executed: return (0); | 0 | ||||||||||||||||||||||||
566 | a->neg = 0; | - | ||||||||||||||||||||||||
567 | a->d[0] = w; | - | ||||||||||||||||||||||||
568 | a->top = (w ? 1 : 0);
| 5266-8436662 | ||||||||||||||||||||||||
569 | bn_check_top(a); | - | ||||||||||||||||||||||||
570 | return (1); executed 8441928 times by 15 tests: return (1); Executed by:
| 8441928 | ||||||||||||||||||||||||
571 | } | - | ||||||||||||||||||||||||
572 | - | |||||||||||||||||||||||||
573 | BIGNUM * | - | ||||||||||||||||||||||||
574 | BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret) | - | ||||||||||||||||||||||||
575 | { | - | ||||||||||||||||||||||||
576 | unsigned int i, m; | - | ||||||||||||||||||||||||
577 | unsigned int n; | - | ||||||||||||||||||||||||
578 | BN_ULONG l; | - | ||||||||||||||||||||||||
579 | BIGNUM *bn = NULL; | - | ||||||||||||||||||||||||
580 | - | |||||||||||||||||||||||||
581 | if (ret == NULL)
| 2550-11485 | ||||||||||||||||||||||||
582 | ret = bn = BN_new(); executed 2550 times by 9 tests: ret = bn = BN_new(); Executed by:
| 2550 | ||||||||||||||||||||||||
583 | if (ret == NULL)
| 0-14035 | ||||||||||||||||||||||||
584 | return (NULL); never executed: return ( ((void *)0) ); | 0 | ||||||||||||||||||||||||
585 | bn_check_top(ret); | - | ||||||||||||||||||||||||
586 | l = 0; | - | ||||||||||||||||||||||||
587 | n = len; | - | ||||||||||||||||||||||||
588 | if (n == 0) {
| 3-14032 | ||||||||||||||||||||||||
589 | ret->top = 0; | - | ||||||||||||||||||||||||
590 | return (ret); executed 3 times by 1 test: return (ret); Executed by:
| 3 | ||||||||||||||||||||||||
591 | } | - | ||||||||||||||||||||||||
592 | i = ((n - 1) / BN_BYTES) + 1; | - | ||||||||||||||||||||||||
593 | m = ((n - 1) % (BN_BYTES)); | - | ||||||||||||||||||||||||
594 | if (bn_wexpand(ret, (int)i) == NULL) {
| 3-14029 | ||||||||||||||||||||||||
595 | BN_free(bn); | - | ||||||||||||||||||||||||
596 | return NULL; executed 3 times by 1 test: return ((void *)0) ; Executed by:
| 3 | ||||||||||||||||||||||||
597 | } | - | ||||||||||||||||||||||||
598 | ret->top = i; | - | ||||||||||||||||||||||||
599 | ret->neg = 0; | - | ||||||||||||||||||||||||
600 | while (n--) {
| 14029-777575 | ||||||||||||||||||||||||
601 | l = (l << 8L) | *(s++); | - | ||||||||||||||||||||||||
602 | if (m-- == 0) {
| 101863-675712 | ||||||||||||||||||||||||
603 | ret->d[--i] = l; | - | ||||||||||||||||||||||||
604 | l = 0; | - | ||||||||||||||||||||||||
605 | m = BN_BYTES - 1; | - | ||||||||||||||||||||||||
606 | } executed 101863 times by 14 tests: end of block Executed by:
| 101863 | ||||||||||||||||||||||||
607 | } executed 777575 times by 14 tests: end of block Executed by:
| 777575 | ||||||||||||||||||||||||
608 | /* need to call this due to clear byte at top if avoiding | - | ||||||||||||||||||||||||
609 | * having the top bit set (-ve number) */ | - | ||||||||||||||||||||||||
610 | bn_correct_top(ret); executed 13974 times by 14 tests: break; Executed by:
executed 14029 times by 14 tests: end of block Executed by:
| 0-15663 | ||||||||||||||||||||||||
611 | return (ret); executed 14029 times by 14 tests: return (ret); Executed by:
| 14029 | ||||||||||||||||||||||||
612 | } | - | ||||||||||||||||||||||||
613 | - | |||||||||||||||||||||||||
614 | /* ignore negative */ | - | ||||||||||||||||||||||||
615 | int | - | ||||||||||||||||||||||||
616 | BN_bn2bin(const BIGNUM *a, unsigned char *to) | - | ||||||||||||||||||||||||
617 | { | - | ||||||||||||||||||||||||
618 | int n, i; | - | ||||||||||||||||||||||||
619 | BN_ULONG l; | - | ||||||||||||||||||||||||
620 | - | |||||||||||||||||||||||||
621 | bn_check_top(a); | - | ||||||||||||||||||||||||
622 | n = i=BN_num_bytes(a); | - | ||||||||||||||||||||||||
623 | while (i--) {
| 2728-189801 | ||||||||||||||||||||||||
624 | l = a->d[i / BN_BYTES]; | - | ||||||||||||||||||||||||
625 | *(to++) = (unsigned char)(l >> (8 * (i % BN_BYTES))) & 0xff; | - | ||||||||||||||||||||||||
626 | } executed 189801 times by 12 tests: end of block Executed by:
| 189801 | ||||||||||||||||||||||||
627 | return (n); executed 2728 times by 12 tests: return (n); Executed by:
| 2728 | ||||||||||||||||||||||||
628 | } | - | ||||||||||||||||||||||||
629 | - | |||||||||||||||||||||||||
630 | int | - | ||||||||||||||||||||||||
631 | BN_ucmp(const BIGNUM *a, const BIGNUM *b) | - | ||||||||||||||||||||||||
632 | { | - | ||||||||||||||||||||||||
633 | int i; | - | ||||||||||||||||||||||||
634 | BN_ULONG t1, t2, *ap, *bp; | - | ||||||||||||||||||||||||
635 | - | |||||||||||||||||||||||||
636 | bn_check_top(a); | - | ||||||||||||||||||||||||
637 | bn_check_top(b); | - | ||||||||||||||||||||||||
638 | - | |||||||||||||||||||||||||
639 | i = a->top - b->top; | - | ||||||||||||||||||||||||
640 | if (i != 0)
| 677757-3242824 | ||||||||||||||||||||||||
641 | return (i); executed 677757 times by 15 tests: return (i); Executed by:
| 677757 | ||||||||||||||||||||||||
642 | ap = a->d; | - | ||||||||||||||||||||||||
643 | bp = b->d; | - | ||||||||||||||||||||||||
644 | for (i = a->top - 1; i >= 0; i--) {
| 244196-4319852 | ||||||||||||||||||||||||
645 | t1 = ap[i]; | - | ||||||||||||||||||||||||
646 | t2 = bp[i]; | - | ||||||||||||||||||||||||
647 | if (t1 != t2)
| 1321224-2998628 | ||||||||||||||||||||||||
648 | return ((t1 > t2) ? 1 : -1); executed 2998628 times by 15 tests: return ((t1 > t2) ? 1 : -1); Executed by:
| 1248553-2998628 | ||||||||||||||||||||||||
649 | } executed 1321224 times by 12 tests: end of block Executed by:
| 1321224 | ||||||||||||||||||||||||
650 | return (0); executed 244196 times by 11 tests: return (0); Executed by:
| 244196 | ||||||||||||||||||||||||
651 | } | - | ||||||||||||||||||||||||
652 | - | |||||||||||||||||||||||||
653 | int | - | ||||||||||||||||||||||||
654 | BN_cmp(const BIGNUM *a, const BIGNUM *b) | - | ||||||||||||||||||||||||
655 | { | - | ||||||||||||||||||||||||
656 | int i; | - | ||||||||||||||||||||||||
657 | int gt, lt; | - | ||||||||||||||||||||||||
658 | BN_ULONG t1, t2; | - | ||||||||||||||||||||||||
659 | - | |||||||||||||||||||||||||
660 | if ((a == NULL) || (b == NULL)) {
| 0-1294441 | ||||||||||||||||||||||||
661 | if (a != NULL)
| 0 | ||||||||||||||||||||||||
662 | return (-1); never executed: return (-1); | 0 | ||||||||||||||||||||||||
663 | else if (b != NULL)
| 0 | ||||||||||||||||||||||||
664 | return (1); never executed: return (1); | 0 | ||||||||||||||||||||||||
665 | else | - | ||||||||||||||||||||||||
666 | return (0); never executed: return (0); | 0 | ||||||||||||||||||||||||
667 | } | - | ||||||||||||||||||||||||
668 | - | |||||||||||||||||||||||||
669 | bn_check_top(a); | - | ||||||||||||||||||||||||
670 | bn_check_top(b); | - | ||||||||||||||||||||||||
671 | - | |||||||||||||||||||||||||
672 | if (a->neg != b->neg) {
| 5-1294436 | ||||||||||||||||||||||||
673 | if (a->neg)
| 0-5 | ||||||||||||||||||||||||
674 | return (-1); never executed: return (-1); | 0 | ||||||||||||||||||||||||
675 | else | - | ||||||||||||||||||||||||
676 | return (1); executed 5 times by 1 test: return (1); Executed by:
| 5 | ||||||||||||||||||||||||
677 | } | - | ||||||||||||||||||||||||
678 | if (a->neg == 0) {
| 1-1294435 | ||||||||||||||||||||||||
679 | gt = 1; | - | ||||||||||||||||||||||||
680 | lt = -1; | - | ||||||||||||||||||||||||
681 | } else { executed 1294435 times by 15 tests: end of block Executed by:
| 1294435 | ||||||||||||||||||||||||
682 | gt = -1; | - | ||||||||||||||||||||||||
683 | lt = 1; | - | ||||||||||||||||||||||||
684 | } executed 1 time by 1 test: end of block Executed by:
| 1 | ||||||||||||||||||||||||
685 | - | |||||||||||||||||||||||||
686 | if (a->top > b->top)
| 368125-926311 | ||||||||||||||||||||||||
687 | return (gt); executed 368125 times by 8 tests: return (gt); Executed by:
| 368125 | ||||||||||||||||||||||||
688 | if (a->top < b->top)
| 504-925807 | ||||||||||||||||||||||||
689 | return (lt); executed 504 times by 3 tests: return (lt); Executed by:
| 504 | ||||||||||||||||||||||||
690 | for (i = a->top - 1; i >= 0; i--) {
| 2598-933743 | ||||||||||||||||||||||||
691 | t1 = a->d[i]; | - | ||||||||||||||||||||||||
692 | t2 = b->d[i]; | - | ||||||||||||||||||||||||
693 | if (t1 > t2)
| 351003-582740 | ||||||||||||||||||||||||
694 | return (gt); executed 351003 times by 10 tests: return (gt); Executed by:
| 351003 | ||||||||||||||||||||||||
695 | if (t1 < t2)
| 10534-572206 | ||||||||||||||||||||||||
696 | return (lt); executed 572206 times by 13 tests: return (lt); Executed by:
| 572206 | ||||||||||||||||||||||||
697 | } executed 10534 times by 14 tests: end of block Executed by:
| 10534 | ||||||||||||||||||||||||
698 | return (0); executed 2598 times by 14 tests: return (0); Executed by:
| 2598 | ||||||||||||||||||||||||
699 | } | - | ||||||||||||||||||||||||
700 | - | |||||||||||||||||||||||||
701 | int | - | ||||||||||||||||||||||||
702 | BN_set_bit(BIGNUM *a, int n) | - | ||||||||||||||||||||||||
703 | { | - | ||||||||||||||||||||||||
704 | int i, j, k; | - | ||||||||||||||||||||||||
705 | - | |||||||||||||||||||||||||
706 | if (n < 0)
| 0-6758 | ||||||||||||||||||||||||
707 | return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||
708 | - | |||||||||||||||||||||||||
709 | i = n / BN_BITS2; | - | ||||||||||||||||||||||||
710 | j = n % BN_BITS2; | - | ||||||||||||||||||||||||
711 | if (a->top <= i) {
| 98-6660 | ||||||||||||||||||||||||
712 | if (bn_wexpand(a, i + 1) == NULL)
| 0-6660 | ||||||||||||||||||||||||
713 | return (0); never executed: return (0); | 0 | ||||||||||||||||||||||||
714 | for (k = a->top; k < i + 1; k++)
| 6660-54620 | ||||||||||||||||||||||||
715 | a->d[k] = 0; executed 54620 times by 14 tests: a->d[k] = 0; Executed by:
| 54620 | ||||||||||||||||||||||||
716 | a->top = i + 1; | - | ||||||||||||||||||||||||
717 | } executed 6660 times by 14 tests: end of block Executed by:
| 6660 | ||||||||||||||||||||||||
718 | - | |||||||||||||||||||||||||
719 | a->d[i] |= (((BN_ULONG)1) << j); | - | ||||||||||||||||||||||||
720 | bn_check_top(a); | - | ||||||||||||||||||||||||
721 | return (1); executed 6758 times by 14 tests: return (1); Executed by:
| 6758 | ||||||||||||||||||||||||
722 | } | - | ||||||||||||||||||||||||
723 | - | |||||||||||||||||||||||||
724 | int | - | ||||||||||||||||||||||||
725 | BN_clear_bit(BIGNUM *a, int n) | - | ||||||||||||||||||||||||
726 | { | - | ||||||||||||||||||||||||
727 | int i, j; | - | ||||||||||||||||||||||||
728 | - | |||||||||||||||||||||||||
729 | bn_check_top(a); | - | ||||||||||||||||||||||||
730 | if (n < 0)
| 0 | ||||||||||||||||||||||||
731 | return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||
732 | - | |||||||||||||||||||||||||
733 | i = n / BN_BITS2; | - | ||||||||||||||||||||||||
734 | j = n % BN_BITS2; | - | ||||||||||||||||||||||||
735 | if (a->top <= i)
| 0 | ||||||||||||||||||||||||
736 | return (0); never executed: return (0); | 0 | ||||||||||||||||||||||||
737 | - | |||||||||||||||||||||||||
738 | a->d[i] &= (~(((BN_ULONG)1) << j)); | - | ||||||||||||||||||||||||
739 | bn_correct_top(a); never executed: break; never executed: end of block
| 0 | ||||||||||||||||||||||||
740 | return (1); never executed: return (1); | 0 | ||||||||||||||||||||||||
741 | } | - | ||||||||||||||||||||||||
742 | - | |||||||||||||||||||||||||
743 | int | - | ||||||||||||||||||||||||
744 | BN_is_bit_set(const BIGNUM *a, int n) | - | ||||||||||||||||||||||||
745 | { | - | ||||||||||||||||||||||||
746 | int i, j; | - | ||||||||||||||||||||||||
747 | - | |||||||||||||||||||||||||
748 | bn_check_top(a); | - | ||||||||||||||||||||||||
749 | if (n < 0)
| 2-2219269 | ||||||||||||||||||||||||
750 | return 0; executed 2 times by 1 test: return 0; Executed by:
| 2 | ||||||||||||||||||||||||
751 | i = n / BN_BITS2; | - | ||||||||||||||||||||||||
752 | j = n % BN_BITS2; | - | ||||||||||||||||||||||||
753 | if (a->top <= i)
| 955-2218314 | ||||||||||||||||||||||||
754 | return 0; executed 955 times by 3 tests: return 0; Executed by:
| 955 | ||||||||||||||||||||||||
755 | return (int)(((a->d[i]) >> j) & ((BN_ULONG)1)); executed 2218314 times by 14 tests: return (int)(((a->d[i]) >> j) & ((unsigned long)1)); Executed by:
| 2218314 | ||||||||||||||||||||||||
756 | } | - | ||||||||||||||||||||||||
757 | - | |||||||||||||||||||||||||
758 | int | - | ||||||||||||||||||||||||
759 | BN_mask_bits(BIGNUM *a, int n) | - | ||||||||||||||||||||||||
760 | { | - | ||||||||||||||||||||||||
761 | int b, w; | - | ||||||||||||||||||||||||
762 | - | |||||||||||||||||||||||||
763 | bn_check_top(a); | - | ||||||||||||||||||||||||
764 | if (n < 0)
| 0-154 | ||||||||||||||||||||||||
765 | return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||
766 | - | |||||||||||||||||||||||||
767 | w = n / BN_BITS2; | - | ||||||||||||||||||||||||
768 | b = n % BN_BITS2; | - | ||||||||||||||||||||||||
769 | if (w >= a->top)
| 0-154 | ||||||||||||||||||||||||
770 | return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||
771 | if (b == 0)
| 0-154 | ||||||||||||||||||||||||
772 | a->top = w; never executed: a->top = w; | 0 | ||||||||||||||||||||||||
773 | else { | - | ||||||||||||||||||||||||
774 | a->top = w + 1; | - | ||||||||||||||||||||||||
775 | a->d[w] &= ~(BN_MASK2 << b); | - | ||||||||||||||||||||||||
776 | } executed 154 times by 2 tests: end of block Executed by:
| 154 | ||||||||||||||||||||||||
777 | bn_correct_top(a); executed 154 times by 2 tests: break; Executed by:
executed 154 times by 2 tests: end of block Executed by:
| 0-154 | ||||||||||||||||||||||||
778 | return (1); executed 154 times by 2 tests: return (1); Executed by:
| 154 | ||||||||||||||||||||||||
779 | } | - | ||||||||||||||||||||||||
780 | - | |||||||||||||||||||||||||
781 | void | - | ||||||||||||||||||||||||
782 | BN_set_negative(BIGNUM *a, int b) | - | ||||||||||||||||||||||||
783 | { | - | ||||||||||||||||||||||||
784 | if (b && !BN_is_zero(a))
| 0-645633 | ||||||||||||||||||||||||
785 | a->neg = 1; never executed: a->neg = 1; | 0 | ||||||||||||||||||||||||
786 | else | - | ||||||||||||||||||||||||
787 | a->neg = 0; executed 645633 times by 4 tests: a->neg = 0; Executed by:
| 645633 | ||||||||||||||||||||||||
788 | } | - | ||||||||||||||||||||||||
789 | - | |||||||||||||||||||||||||
790 | int | - | ||||||||||||||||||||||||
791 | bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n) | - | ||||||||||||||||||||||||
792 | { | - | ||||||||||||||||||||||||
793 | int i; | - | ||||||||||||||||||||||||
794 | BN_ULONG aa, bb; | - | ||||||||||||||||||||||||
795 | - | |||||||||||||||||||||||||
796 | aa = a[n - 1]; | - | ||||||||||||||||||||||||
797 | bb = b[n - 1]; | - | ||||||||||||||||||||||||
798 | if (aa != bb)
| 19-7280 | ||||||||||||||||||||||||
799 | return ((aa > bb) ? 1 : -1); executed 7280 times by 7 tests: return ((aa > bb) ? 1 : -1); Executed by:
| 3332-7280 | ||||||||||||||||||||||||
800 | for (i = n - 2; i >= 0; i--) {
| 0-55 | ||||||||||||||||||||||||
801 | aa = a[i]; | - | ||||||||||||||||||||||||
802 | bb = b[i]; | - | ||||||||||||||||||||||||
803 | if (aa != bb)
| 19-36 | ||||||||||||||||||||||||
804 | return ((aa > bb) ? 1 : -1); executed 19 times by 4 tests: return ((aa > bb) ? 1 : -1); Executed by:
| 0-19 | ||||||||||||||||||||||||
805 | } executed 36 times by 3 tests: end of block Executed by:
| 36 | ||||||||||||||||||||||||
806 | return (0); never executed: return (0); | 0 | ||||||||||||||||||||||||
807 | } | - | ||||||||||||||||||||||||
808 | - | |||||||||||||||||||||||||
809 | /* Here follows a specialised variants of bn_cmp_words(). It has the | - | ||||||||||||||||||||||||
810 | property of performing the operation on arrays of different sizes. | - | ||||||||||||||||||||||||
811 | The sizes of those arrays is expressed through cl, which is the | - | ||||||||||||||||||||||||
812 | common length ( basicall, min(len(a),len(b)) ), and dl, which is the | - | ||||||||||||||||||||||||
813 | delta between the two lengths, calculated as len(a)-len(b). | - | ||||||||||||||||||||||||
814 | All lengths are the number of BN_ULONGs... */ | - | ||||||||||||||||||||||||
815 | - | |||||||||||||||||||||||||
816 | int | - | ||||||||||||||||||||||||
817 | bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b, int cl, int dl) | - | ||||||||||||||||||||||||
818 | { | - | ||||||||||||||||||||||||
819 | int n, i; | - | ||||||||||||||||||||||||
820 | - | |||||||||||||||||||||||||
821 | n = cl - 1; | - | ||||||||||||||||||||||||
822 | - | |||||||||||||||||||||||||
823 | if (dl < 0) {
| 336-5248 | ||||||||||||||||||||||||
824 | for (i = dl; i < 0; i++) {
| 0-344 | ||||||||||||||||||||||||
825 | if (b[n - i] != 0)
| 8-336 | ||||||||||||||||||||||||
826 | return -1; /* a < b */ executed 336 times by 2 tests: return -1; Executed by:
| 336 | ||||||||||||||||||||||||
827 | } executed 8 times by 1 test: end of block Executed by:
| 8 | ||||||||||||||||||||||||
828 | } never executed: end of block | 0 | ||||||||||||||||||||||||
829 | if (dl > 0) {
| 514-4734 | ||||||||||||||||||||||||
830 | for (i = dl; i > 0; i--) {
| 0-516 | ||||||||||||||||||||||||
831 | if (a[n + i] != 0)
| 2-514 | ||||||||||||||||||||||||
832 | return 1; /* a > b */ executed 514 times by 2 tests: return 1; Executed by:
| 514 | ||||||||||||||||||||||||
833 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||||||||||||||
834 | } never executed: end of block | 0 | ||||||||||||||||||||||||
835 | return bn_cmp_words(a, b, cl); executed 4734 times by 7 tests: return bn_cmp_words(a, b, cl); Executed by:
| 4734 | ||||||||||||||||||||||||
836 | } | - | ||||||||||||||||||||||||
837 | - | |||||||||||||||||||||||||
838 | /* | - | ||||||||||||||||||||||||
839 | * Constant-time conditional swap of a and b. | - | ||||||||||||||||||||||||
840 | * a and b are swapped if condition is not 0. | - | ||||||||||||||||||||||||
841 | * The code assumes that at most one bit of condition is set. | - | ||||||||||||||||||||||||
842 | * nwords is the number of words to swap. | - | ||||||||||||||||||||||||
843 | * The code assumes that at least nwords are allocated in both a and b, | - | ||||||||||||||||||||||||
844 | * and that no more than nwords are used by either a or b. | - | ||||||||||||||||||||||||
845 | * a and b cannot be the same number | - | ||||||||||||||||||||||||
846 | */ | - | ||||||||||||||||||||||||
847 | void | - | ||||||||||||||||||||||||
848 | BN_consttime_swap(BN_ULONG condition, BIGNUM *a, BIGNUM *b, int nwords) | - | ||||||||||||||||||||||||
849 | { | - | ||||||||||||||||||||||||
850 | BN_ULONG t; | - | ||||||||||||||||||||||||
851 | int i; | - | ||||||||||||||||||||||||
852 | - | |||||||||||||||||||||||||
853 | bn_wcheck_size(a, nwords); | - | ||||||||||||||||||||||||
854 | bn_wcheck_size(b, nwords); | - | ||||||||||||||||||||||||
855 | - | |||||||||||||||||||||||||
856 | assert(a != b); | - | ||||||||||||||||||||||||
857 | assert((condition & (condition - 1)) == 0); | - | ||||||||||||||||||||||||
858 | assert(sizeof(BN_ULONG) >= sizeof(int)); | - | ||||||||||||||||||||||||
859 | - | |||||||||||||||||||||||||
860 | condition = ((condition - 1) >> (BN_BITS2 - 1)) - 1; | - | ||||||||||||||||||||||||
861 | - | |||||||||||||||||||||||||
862 | t = (a->top^b->top) & condition; | - | ||||||||||||||||||||||||
863 | a->top ^= t; | - | ||||||||||||||||||||||||
864 | b->top ^= t; | - | ||||||||||||||||||||||||
865 | - | |||||||||||||||||||||||||
866 | #define BN_CONSTTIME_SWAP(ind) \ | - | ||||||||||||||||||||||||
867 | do { \ | - | ||||||||||||||||||||||||
868 | t = (a->d[ind] ^ b->d[ind]) & condition; \ | - | ||||||||||||||||||||||||
869 | a->d[ind] ^= t; \ | - | ||||||||||||||||||||||||
870 | b->d[ind] ^= t; \ | - | ||||||||||||||||||||||||
871 | } while (0) | - | ||||||||||||||||||||||||
872 | - | |||||||||||||||||||||||||
873 | - | |||||||||||||||||||||||||
874 | switch (nwords) { | - | ||||||||||||||||||||||||
875 | default: never executed: default: | 0 | ||||||||||||||||||||||||
876 | for (i = 10; i < nwords; i++)
| 0 | ||||||||||||||||||||||||
877 | BN_CONSTTIME_SWAP(i); never executed: end of block | 0 | ||||||||||||||||||||||||
878 | /* Fallthrough */ | - | ||||||||||||||||||||||||
879 | case 10: BN_CONSTTIME_SWAP(9); /* Fallthrough */ code before this statement never executed: case 10: never executed: case 10: | 0 | ||||||||||||||||||||||||
880 | case 9: BN_CONSTTIME_SWAP(8); /* Fallthrough */ code before this statement never executed: case 9: never executed: case 9: | 0 | ||||||||||||||||||||||||
881 | case 8: BN_CONSTTIME_SWAP(7); /* Fallthrough */ code before this statement never executed: case 8: never executed: case 8: | 0 | ||||||||||||||||||||||||
882 | case 7: BN_CONSTTIME_SWAP(6); /* Fallthrough */ code before this statement never executed: case 7: never executed: case 7: | 0 | ||||||||||||||||||||||||
883 | case 6: BN_CONSTTIME_SWAP(5); /* Fallthrough */ code before this statement never executed: case 6: never executed: case 6: | 0 | ||||||||||||||||||||||||
884 | case 5: BN_CONSTTIME_SWAP(4); /* Fallthrough */ code before this statement never executed: case 5: never executed: case 5: | 0 | ||||||||||||||||||||||||
885 | case 4: BN_CONSTTIME_SWAP(3); /* Fallthrough */ code before this statement never executed: case 4: never executed: case 4: | 0 | ||||||||||||||||||||||||
886 | case 3: BN_CONSTTIME_SWAP(2); /* Fallthrough */ code before this statement never executed: case 3: never executed: case 3: | 0 | ||||||||||||||||||||||||
887 | case 2: BN_CONSTTIME_SWAP(1); /* Fallthrough */ code before this statement never executed: case 2: never executed: case 2: | 0 | ||||||||||||||||||||||||
888 | case 1: code before this statement never executed: case 1: never executed: case 1: | 0 | ||||||||||||||||||||||||
889 | BN_CONSTTIME_SWAP(0); | - | ||||||||||||||||||||||||
890 | } never executed: end of block | 0 | ||||||||||||||||||||||||
891 | #undef BN_CONSTTIME_SWAP | - | ||||||||||||||||||||||||
892 | } | - | ||||||||||||||||||||||||
893 | - | |||||||||||||||||||||||||
894 | /* | - | ||||||||||||||||||||||||
895 | * Constant-time conditional swap of a and b. | - | ||||||||||||||||||||||||
896 | * a and b are swapped if condition is not 0. | - | ||||||||||||||||||||||||
897 | * nwords is the number of words to swap. | - | ||||||||||||||||||||||||
898 | */ | - | ||||||||||||||||||||||||
899 | int | - | ||||||||||||||||||||||||
900 | BN_swap_ct(BN_ULONG condition, BIGNUM *a, BIGNUM *b, size_t nwords) | - | ||||||||||||||||||||||||
901 | { | - | ||||||||||||||||||||||||
902 | BN_ULONG t; | - | ||||||||||||||||||||||||
903 | int i, words; | - | ||||||||||||||||||||||||
904 | - | |||||||||||||||||||||||||
905 | if (a == b)
| 0-589299 | ||||||||||||||||||||||||
906 | return 1; never executed: return 1; | 0 | ||||||||||||||||||||||||
907 | if (nwords > INT_MAX)
| 0-589299 | ||||||||||||||||||||||||
908 | return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||
909 | words = (int)nwords; | - | ||||||||||||||||||||||||
910 | if (bn_wexpand(a, words) == NULL || bn_wexpand(b, words) == NULL)
| 0-589299 | ||||||||||||||||||||||||
911 | return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||
912 | if (a->top > words || b->top > words) {
| 0-589299 | ||||||||||||||||||||||||
913 | BNerror(BN_R_INVALID_LENGTH); | - | ||||||||||||||||||||||||
914 | return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||
915 | } | - | ||||||||||||||||||||||||
916 | - | |||||||||||||||||||||||||
917 | /* Set condition to 0 (if it was zero) or all 1s otherwise. */ | - | ||||||||||||||||||||||||
918 | condition = ((~condition & (condition - 1)) >> (BN_BITS2 - 1)) - 1; | - | ||||||||||||||||||||||||
919 | - | |||||||||||||||||||||||||
920 | /* swap top field */ | - | ||||||||||||||||||||||||
921 | t = (a->top ^ b->top) & condition; | - | ||||||||||||||||||||||||
922 | a->top ^= t; | - | ||||||||||||||||||||||||
923 | b->top ^= t; | - | ||||||||||||||||||||||||
924 | - | |||||||||||||||||||||||||
925 | /* swap neg field */ | - | ||||||||||||||||||||||||
926 | t = (a->neg ^ b->neg) & condition; | - | ||||||||||||||||||||||||
927 | a->neg ^= t; | - | ||||||||||||||||||||||||
928 | b->neg ^= t; | - | ||||||||||||||||||||||||
929 | - | |||||||||||||||||||||||||
930 | /* swap BN_FLG_CONSTTIME from flag field */ | - | ||||||||||||||||||||||||
931 | t = ((a->flags ^ b->flags) & BN_FLG_CONSTTIME) & condition; | - | ||||||||||||||||||||||||
932 | a->flags ^= t; | - | ||||||||||||||||||||||||
933 | b->flags ^= t; | - | ||||||||||||||||||||||||
934 | - | |||||||||||||||||||||||||
935 | /* swap the data */ | - | ||||||||||||||||||||||||
936 | for (i = 0; i < words; i++) {
| 589299-3081391 | ||||||||||||||||||||||||
937 | t = (a->d[i] ^ b->d[i]) & condition; | - | ||||||||||||||||||||||||
938 | a->d[i] ^= t; | - | ||||||||||||||||||||||||
939 | b->d[i] ^= t; | - | ||||||||||||||||||||||||
940 | } executed 3081391 times by 4 tests: end of block Executed by:
| 3081391 | ||||||||||||||||||||||||
941 | - | |||||||||||||||||||||||||
942 | return 1; executed 589299 times by 4 tests: return 1; Executed by:
| 589299 | ||||||||||||||||||||||||
943 | } | - | ||||||||||||||||||||||||
944 | - | |||||||||||||||||||||||||
945 | BN_GENCB * | - | ||||||||||||||||||||||||
946 | BN_GENCB_new(void) | - | ||||||||||||||||||||||||
947 | { | - | ||||||||||||||||||||||||
948 | BN_GENCB *cb; | - | ||||||||||||||||||||||||
949 | - | |||||||||||||||||||||||||
950 | if ((cb = calloc(1, sizeof(*cb))) == NULL)
| 0 | ||||||||||||||||||||||||
951 | return NULL; never executed: return ((void *)0) ; | 0 | ||||||||||||||||||||||||
952 | - | |||||||||||||||||||||||||
953 | return cb; never executed: return cb; | 0 | ||||||||||||||||||||||||
954 | } | - | ||||||||||||||||||||||||
955 | - | |||||||||||||||||||||||||
956 | void | - | ||||||||||||||||||||||||
957 | BN_GENCB_free(BN_GENCB *cb) | - | ||||||||||||||||||||||||
958 | { | - | ||||||||||||||||||||||||
959 | if (cb == NULL)
| 0-1 | ||||||||||||||||||||||||
960 | return; executed 1 time by 1 test: return; Executed by:
| 1 | ||||||||||||||||||||||||
961 | free(cb); | - | ||||||||||||||||||||||||
962 | } never executed: end of block | 0 | ||||||||||||||||||||||||
963 | - | |||||||||||||||||||||||||
964 | void * | - | ||||||||||||||||||||||||
965 | BN_GENCB_get_arg(BN_GENCB *cb) | - | ||||||||||||||||||||||||
966 | { | - | ||||||||||||||||||||||||
967 | return cb->arg; never executed: return cb->arg; | 0 | ||||||||||||||||||||||||
968 | } | - | ||||||||||||||||||||||||
Source code | Switch to Preprocessed file |