1 | /* wolfmath.c
|
---|
2 | *
|
---|
3 | * Copyright (C) 2006-2020 wolfSSL Inc.
|
---|
4 | *
|
---|
5 | * This file is part of wolfSSL.
|
---|
6 | *
|
---|
7 | * wolfSSL is free software; you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation; either version 2 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * wolfSSL is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with this program; if not, write to the Free Software
|
---|
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
|
---|
20 | */
|
---|
21 |
|
---|
22 |
|
---|
23 | /* common functions for either math library */
|
---|
24 |
|
---|
25 | #ifdef HAVE_CONFIG_H
|
---|
26 | #include <config.h>
|
---|
27 | #endif
|
---|
28 |
|
---|
29 | /* in case user set USE_FAST_MATH there */
|
---|
30 | #include <wolfssl/wolfcrypt/settings.h>
|
---|
31 |
|
---|
32 | #include <wolfssl/wolfcrypt/integer.h>
|
---|
33 |
|
---|
34 | #include <wolfssl/wolfcrypt/error-crypt.h>
|
---|
35 | #include <wolfssl/wolfcrypt/logging.h>
|
---|
36 |
|
---|
37 | #if defined(USE_FAST_MATH) || !defined(NO_BIG_INT)
|
---|
38 |
|
---|
39 | #ifdef WOLFSSL_ASYNC_CRYPT
|
---|
40 | #include <wolfssl/wolfcrypt/async.h>
|
---|
41 | #endif
|
---|
42 |
|
---|
43 | #ifdef NO_INLINE
|
---|
44 | #include <wolfssl/wolfcrypt/misc.h>
|
---|
45 | #else
|
---|
46 | #define WOLFSSL_MISC_INCLUDED
|
---|
47 | #include <wolfcrypt/src/misc.c>
|
---|
48 | #endif
|
---|
49 |
|
---|
50 |
|
---|
51 | #if !defined(WC_NO_CACHE_RESISTANT) && \
|
---|
52 | ((defined(HAVE_ECC) && defined(ECC_TIMING_RESISTANT)) || \
|
---|
53 | (defined(USE_FAST_MATH) && defined(TFM_TIMING_RESISTANT)))
|
---|
54 |
|
---|
55 | /* all off / all on pointer addresses for constant calculations */
|
---|
56 | /* ecc.c uses same table */
|
---|
57 | const wolfssl_word wc_off_on_addr[2] =
|
---|
58 | {
|
---|
59 | #if defined(WC_64BIT_CPU)
|
---|
60 | W64LIT(0x0000000000000000),
|
---|
61 | W64LIT(0xffffffffffffffff)
|
---|
62 | #elif defined(WC_16BIT_CPU)
|
---|
63 | 0x0000U,
|
---|
64 | 0xffffU
|
---|
65 | #else
|
---|
66 | /* 32 bit */
|
---|
67 | 0x00000000U,
|
---|
68 | 0xffffffffU
|
---|
69 | #endif
|
---|
70 | };
|
---|
71 | #endif
|
---|
72 |
|
---|
73 |
|
---|
74 | #if !defined(WOLFSSL_SP_MATH)
|
---|
75 | int get_digit_count(mp_int* a)
|
---|
76 | {
|
---|
77 | if (a == NULL)
|
---|
78 | return 0;
|
---|
79 |
|
---|
80 | return a->used;
|
---|
81 | }
|
---|
82 | #endif
|
---|
83 |
|
---|
84 | mp_digit get_digit(mp_int* a, int n)
|
---|
85 | {
|
---|
86 | if (a == NULL)
|
---|
87 | return 0;
|
---|
88 |
|
---|
89 | return (n >= a->used || n < 0) ? 0 : a->dp[n];
|
---|
90 | }
|
---|
91 |
|
---|
92 | /* Conditionally copy a into b. Performed in constant time.
|
---|
93 | *
|
---|
94 | * a MP integer to copy.
|
---|
95 | * copy On 1, copy a into b. on 0 leave b unchanged.
|
---|
96 | * b MP integer to copy into.
|
---|
97 | * returns BAD_FUNC_ARG when a or b is NULL, MEMORY_E when growing b fails and
|
---|
98 | * MP_OKAY otherwise.
|
---|
99 | */
|
---|
100 | int mp_cond_copy(mp_int* a, int copy, mp_int* b)
|
---|
101 | {
|
---|
102 | int err = MP_OKAY;
|
---|
103 | int i;
|
---|
104 | mp_digit mask = (mp_digit)0 - copy;
|
---|
105 |
|
---|
106 | if (a == NULL || b == NULL)
|
---|
107 | err = BAD_FUNC_ARG;
|
---|
108 |
|
---|
109 | /* Ensure b has enough space to copy a into */
|
---|
110 | if (err == MP_OKAY)
|
---|
111 | err = mp_grow(b, a->used + 1);
|
---|
112 | if (err == MP_OKAY) {
|
---|
113 | /* When mask 0, b is unchanged2
|
---|
114 | * When mask all set, b ^ b ^ a = a
|
---|
115 | */
|
---|
116 | /* Conditionaly copy all digits and then number of used diigits.
|
---|
117 | * get_digit() returns 0 when index greater than available digit.
|
---|
118 | */
|
---|
119 | for (i = 0; i < a->used; i++) {
|
---|
120 | b->dp[i] ^= (get_digit(a, i) ^ get_digit(b, i)) & mask;
|
---|
121 | }
|
---|
122 | for (; i < b->used; i++) {
|
---|
123 | b->dp[i] ^= (get_digit(a, i) ^ get_digit(b, i)) & mask;
|
---|
124 | }
|
---|
125 | b->used ^= (a->used ^ b->used) & (int)mask;
|
---|
126 | }
|
---|
127 |
|
---|
128 | return err;
|
---|
129 | }
|
---|
130 |
|
---|
131 | #ifndef WC_NO_RNG
|
---|
132 | int get_rand_digit(WC_RNG* rng, mp_digit* d)
|
---|
133 | {
|
---|
134 | return wc_RNG_GenerateBlock(rng, (byte*)d, sizeof(mp_digit));
|
---|
135 | }
|
---|
136 |
|
---|
137 | #ifdef WC_RSA_BLINDING
|
---|
138 | int mp_rand(mp_int* a, int digits, WC_RNG* rng)
|
---|
139 | {
|
---|
140 | int ret = 0;
|
---|
141 | int cnt = digits * sizeof(mp_digit);
|
---|
142 | #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
|
---|
143 | int i;
|
---|
144 | #endif
|
---|
145 |
|
---|
146 | if (rng == NULL) {
|
---|
147 | ret = MISSING_RNG_E;
|
---|
148 | }
|
---|
149 | else if (a == NULL) {
|
---|
150 | ret = BAD_FUNC_ARG;
|
---|
151 | }
|
---|
152 |
|
---|
153 | #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
|
---|
154 | /* allocate space for digits */
|
---|
155 | if (ret == MP_OKAY) {
|
---|
156 | ret = mp_set_bit(a, digits * DIGIT_BIT - 1);
|
---|
157 | }
|
---|
158 | #else
|
---|
159 | #if defined(WOLFSSL_SP_MATH)
|
---|
160 | if ((ret == MP_OKAY) && (digits > SP_INT_DIGITS))
|
---|
161 | #else
|
---|
162 | if ((ret == MP_OKAY) && (digits > FP_SIZE))
|
---|
163 | #endif
|
---|
164 | {
|
---|
165 | ret = BAD_FUNC_ARG;
|
---|
166 | }
|
---|
167 | if (ret == MP_OKAY) {
|
---|
168 | a->used = digits;
|
---|
169 | }
|
---|
170 | #endif
|
---|
171 | /* fill the data with random bytes */
|
---|
172 | if (ret == MP_OKAY) {
|
---|
173 | ret = wc_RNG_GenerateBlock(rng, (byte*)a->dp, cnt);
|
---|
174 | }
|
---|
175 | if (ret == MP_OKAY) {
|
---|
176 | #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
|
---|
177 | /* Mask down each digit to only bits used */
|
---|
178 | for (i = 0; i < a->used; i++) {
|
---|
179 | a->dp[i] &= MP_MASK;
|
---|
180 | }
|
---|
181 | #endif
|
---|
182 | /* ensure top digit is not zero */
|
---|
183 | while ((ret == MP_OKAY) && (a->dp[a->used - 1] == 0)) {
|
---|
184 | ret = get_rand_digit(rng, &a->dp[a->used - 1]);
|
---|
185 | #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
|
---|
186 | a->dp[a->used - 1] &= MP_MASK;
|
---|
187 | #endif
|
---|
188 | }
|
---|
189 | }
|
---|
190 |
|
---|
191 | return ret;
|
---|
192 | }
|
---|
193 | #endif /* WC_RSA_BLINDING */
|
---|
194 | #endif
|
---|
195 |
|
---|
196 | /* export an mp_int as unsigned char or hex string
|
---|
197 | * encType is WC_TYPE_UNSIGNED_BIN or WC_TYPE_HEX_STR
|
---|
198 | * return MP_OKAY on success */
|
---|
199 | int wc_export_int(mp_int* mp, byte* buf, word32* len, word32 keySz,
|
---|
200 | int encType)
|
---|
201 | {
|
---|
202 | int err;
|
---|
203 |
|
---|
204 | if (mp == NULL)
|
---|
205 | return BAD_FUNC_ARG;
|
---|
206 |
|
---|
207 | /* check buffer size */
|
---|
208 | if (*len < keySz) {
|
---|
209 | *len = keySz;
|
---|
210 | return BUFFER_E;
|
---|
211 | }
|
---|
212 |
|
---|
213 | *len = keySz;
|
---|
214 | XMEMSET(buf, 0, *len);
|
---|
215 |
|
---|
216 | if (encType == WC_TYPE_HEX_STR) {
|
---|
217 | #ifdef WC_MP_TO_RADIX
|
---|
218 | err = mp_tohex(mp, (char*)buf);
|
---|
219 | #else
|
---|
220 | err = NOT_COMPILED_IN;
|
---|
221 | #endif
|
---|
222 | }
|
---|
223 | else {
|
---|
224 | err = mp_to_unsigned_bin(mp, buf + (keySz - mp_unsigned_bin_size(mp)));
|
---|
225 | }
|
---|
226 |
|
---|
227 | return err;
|
---|
228 | }
|
---|
229 |
|
---|
230 |
|
---|
231 | #ifdef HAVE_WOLF_BIGINT
|
---|
232 | void wc_bigint_init(WC_BIGINT* a)
|
---|
233 | {
|
---|
234 | if (a != NULL) {
|
---|
235 | a->buf = NULL;
|
---|
236 | a->len = 0;
|
---|
237 | a->heap = NULL;
|
---|
238 | }
|
---|
239 | }
|
---|
240 |
|
---|
241 | int wc_bigint_alloc(WC_BIGINT* a, word32 sz)
|
---|
242 | {
|
---|
243 | int err = MP_OKAY;
|
---|
244 |
|
---|
245 | if (a == NULL)
|
---|
246 | return BAD_FUNC_ARG;
|
---|
247 |
|
---|
248 | if (sz > 0) {
|
---|
249 | if (a->buf && sz > a->len) {
|
---|
250 | wc_bigint_free(a);
|
---|
251 | }
|
---|
252 | if (a->buf == NULL) {
|
---|
253 | a->buf = (byte*)XMALLOC(sz, a->heap, DYNAMIC_TYPE_WOLF_BIGINT);
|
---|
254 | if (a->buf == NULL) {
|
---|
255 | err = MP_MEM;
|
---|
256 | }
|
---|
257 | }
|
---|
258 | else {
|
---|
259 | XMEMSET(a->buf, 0, sz);
|
---|
260 | }
|
---|
261 | }
|
---|
262 | a->len = sz;
|
---|
263 |
|
---|
264 | return err;
|
---|
265 | }
|
---|
266 |
|
---|
267 | /* assumes input is big endian format */
|
---|
268 | int wc_bigint_from_unsigned_bin(WC_BIGINT* a, const byte* in, word32 inlen)
|
---|
269 | {
|
---|
270 | int err;
|
---|
271 |
|
---|
272 | if (a == NULL || in == NULL || inlen == 0)
|
---|
273 | return BAD_FUNC_ARG;
|
---|
274 |
|
---|
275 | err = wc_bigint_alloc(a, inlen);
|
---|
276 | if (err == 0) {
|
---|
277 | XMEMCPY(a->buf, in, inlen);
|
---|
278 | }
|
---|
279 |
|
---|
280 | return err;
|
---|
281 | }
|
---|
282 |
|
---|
283 | int wc_bigint_to_unsigned_bin(WC_BIGINT* a, byte* out, word32* outlen)
|
---|
284 | {
|
---|
285 | word32 sz;
|
---|
286 |
|
---|
287 | if (a == NULL || out == NULL || outlen == NULL || *outlen == 0)
|
---|
288 | return BAD_FUNC_ARG;
|
---|
289 |
|
---|
290 | /* trim to fit into output buffer */
|
---|
291 | sz = a->len;
|
---|
292 | if (a->len > *outlen) {
|
---|
293 | WOLFSSL_MSG("wc_bigint_export: Truncating output");
|
---|
294 | sz = *outlen;
|
---|
295 | }
|
---|
296 |
|
---|
297 | if (a->buf) {
|
---|
298 | XMEMCPY(out, a->buf, sz);
|
---|
299 | }
|
---|
300 |
|
---|
301 | *outlen = sz;
|
---|
302 |
|
---|
303 | return MP_OKAY;
|
---|
304 | }
|
---|
305 |
|
---|
306 | void wc_bigint_zero(WC_BIGINT* a)
|
---|
307 | {
|
---|
308 | if (a && a->buf) {
|
---|
309 | ForceZero(a->buf, a->len);
|
---|
310 | }
|
---|
311 | }
|
---|
312 |
|
---|
313 | void wc_bigint_free(WC_BIGINT* a)
|
---|
314 | {
|
---|
315 | if (a) {
|
---|
316 | if (a->buf) {
|
---|
317 | XFREE(a->buf, a->heap, DYNAMIC_TYPE_WOLF_BIGINT);
|
---|
318 | }
|
---|
319 | a->buf = NULL;
|
---|
320 | a->len = 0;
|
---|
321 | }
|
---|
322 | }
|
---|
323 |
|
---|
324 | /* sz: make sure the buffer is at least that size and zero padded.
|
---|
325 | * A `sz == 0` will use the size of `src`.
|
---|
326 | * The calulcates sz is stored into dst->len in `wc_bigint_alloc`.
|
---|
327 | */
|
---|
328 | int wc_mp_to_bigint_sz(mp_int* src, WC_BIGINT* dst, word32 sz)
|
---|
329 | {
|
---|
330 | int err;
|
---|
331 | word32 x, y;
|
---|
332 |
|
---|
333 | if (src == NULL || dst == NULL)
|
---|
334 | return BAD_FUNC_ARG;
|
---|
335 |
|
---|
336 | /* get size of source */
|
---|
337 | x = mp_unsigned_bin_size(src);
|
---|
338 | if (sz < x)
|
---|
339 | sz = x;
|
---|
340 |
|
---|
341 | /* make sure destination is allocated and large enough */
|
---|
342 | err = wc_bigint_alloc(dst, sz);
|
---|
343 | if (err == MP_OKAY) {
|
---|
344 |
|
---|
345 | /* leading zero pad */
|
---|
346 | y = sz - x;
|
---|
347 | XMEMSET(dst->buf, 0, y);
|
---|
348 |
|
---|
349 | /* export src as unsigned bin to destination buf */
|
---|
350 | err = mp_to_unsigned_bin(src, dst->buf + y);
|
---|
351 | }
|
---|
352 |
|
---|
353 | return err;
|
---|
354 | }
|
---|
355 |
|
---|
356 | int wc_mp_to_bigint(mp_int* src, WC_BIGINT* dst)
|
---|
357 | {
|
---|
358 | if (src == NULL || dst == NULL)
|
---|
359 | return BAD_FUNC_ARG;
|
---|
360 |
|
---|
361 | return wc_mp_to_bigint_sz(src, dst, 0);
|
---|
362 | }
|
---|
363 |
|
---|
364 | int wc_bigint_to_mp(WC_BIGINT* src, mp_int* dst)
|
---|
365 | {
|
---|
366 | int err;
|
---|
367 |
|
---|
368 | if (src == NULL || dst == NULL)
|
---|
369 | return BAD_FUNC_ARG;
|
---|
370 |
|
---|
371 | if (src->buf == NULL)
|
---|
372 | return BAD_FUNC_ARG;
|
---|
373 |
|
---|
374 | err = mp_read_unsigned_bin(dst, src->buf, src->len);
|
---|
375 | wc_bigint_free(src);
|
---|
376 |
|
---|
377 | return err;
|
---|
378 | }
|
---|
379 | #endif /* HAVE_WOLF_BIGINT */
|
---|
380 |
|
---|
381 | #endif /* USE_FAST_MATH || !NO_BIG_INT */
|
---|