1 | /* wolfmath.c
|
---|
2 | *
|
---|
3 | * Copyright (C) 2006-2017 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 | int get_digit_count(mp_int* a)
|
---|
75 | {
|
---|
76 | if (a == NULL)
|
---|
77 | return 0;
|
---|
78 |
|
---|
79 | return a->used;
|
---|
80 | }
|
---|
81 |
|
---|
82 | mp_digit get_digit(mp_int* a, int n)
|
---|
83 | {
|
---|
84 | if (a == NULL)
|
---|
85 | return 0;
|
---|
86 |
|
---|
87 | return (n >= a->used || n < 0) ? 0 : a->dp[n];
|
---|
88 | }
|
---|
89 |
|
---|
90 | #ifndef WC_NO_RNG
|
---|
91 | int get_rand_digit(WC_RNG* rng, mp_digit* d)
|
---|
92 | {
|
---|
93 | return wc_RNG_GenerateBlock(rng, (byte*)d, sizeof(mp_digit));
|
---|
94 | }
|
---|
95 |
|
---|
96 | #ifdef WC_RSA_BLINDING
|
---|
97 | int mp_rand(mp_int* a, int digits, WC_RNG* rng)
|
---|
98 | {
|
---|
99 | int ret = 0;
|
---|
100 | DECLARE_VAR(d, mp_digit, 1, rng ? rng->heap : NULL);
|
---|
101 |
|
---|
102 | if (rng == NULL) {
|
---|
103 | ret = MISSING_RNG_E; goto exit;
|
---|
104 | }
|
---|
105 |
|
---|
106 | if (a == NULL
|
---|
107 | #ifdef WOLFSSL_ASYNC_CRYPT
|
---|
108 | || d == NULL
|
---|
109 | #endif
|
---|
110 | ) {
|
---|
111 | ret = BAD_FUNC_ARG; goto exit;
|
---|
112 | }
|
---|
113 |
|
---|
114 | mp_zero(a);
|
---|
115 | if (digits <= 0) {
|
---|
116 | ret = MP_OKAY; goto exit;
|
---|
117 | }
|
---|
118 |
|
---|
119 | /* first place a random non-zero digit */
|
---|
120 | do {
|
---|
121 | ret = get_rand_digit(rng, d);
|
---|
122 | if (ret != 0) {
|
---|
123 | goto exit;
|
---|
124 | }
|
---|
125 | } while (*d == 0);
|
---|
126 |
|
---|
127 | if ((ret = mp_add_d(a, *d, a)) != MP_OKAY) {
|
---|
128 | goto exit;
|
---|
129 | }
|
---|
130 |
|
---|
131 | while (--digits > 0) {
|
---|
132 | if ((ret = mp_lshd(a, 1)) != MP_OKAY) {
|
---|
133 | goto exit;
|
---|
134 | }
|
---|
135 | if ((ret = get_rand_digit(rng, d)) != 0) {
|
---|
136 | goto exit;
|
---|
137 | }
|
---|
138 | if ((ret = mp_add_d(a, *d, a)) != MP_OKAY) {
|
---|
139 | goto exit;
|
---|
140 | }
|
---|
141 | }
|
---|
142 |
|
---|
143 | exit:
|
---|
144 | FREE_VAR(d, rng ? rng->heap : NULL);
|
---|
145 |
|
---|
146 | return ret;
|
---|
147 | }
|
---|
148 | #endif /* WC_RSA_BLINDING */
|
---|
149 | #endif
|
---|
150 |
|
---|
151 | /* export an mp_int as unsigned char or hex string
|
---|
152 | * encType is WC_TYPE_UNSIGNED_BIN or WC_TYPE_HEX_STR
|
---|
153 | * return MP_OKAY on success */
|
---|
154 | int wc_export_int(mp_int* mp, byte* buf, word32* len, word32 keySz,
|
---|
155 | int encType)
|
---|
156 | {
|
---|
157 | int err;
|
---|
158 |
|
---|
159 | if (mp == NULL)
|
---|
160 | return BAD_FUNC_ARG;
|
---|
161 |
|
---|
162 | /* check buffer size */
|
---|
163 | if (*len < keySz) {
|
---|
164 | *len = keySz;
|
---|
165 | return BUFFER_E;
|
---|
166 | }
|
---|
167 |
|
---|
168 | *len = keySz;
|
---|
169 | XMEMSET(buf, 0, *len);
|
---|
170 |
|
---|
171 | if (encType == WC_TYPE_HEX_STR) {
|
---|
172 | #ifdef WC_MP_TO_RADIX
|
---|
173 | err = mp_tohex(mp, (char*)buf);
|
---|
174 | #else
|
---|
175 | err = NOT_COMPILED_IN;
|
---|
176 | #endif
|
---|
177 | }
|
---|
178 | else {
|
---|
179 | err = mp_to_unsigned_bin(mp, buf + (keySz - mp_unsigned_bin_size(mp)));
|
---|
180 | }
|
---|
181 |
|
---|
182 | return err;
|
---|
183 | }
|
---|
184 |
|
---|
185 |
|
---|
186 | #ifdef HAVE_WOLF_BIGINT
|
---|
187 | void wc_bigint_init(WC_BIGINT* a)
|
---|
188 | {
|
---|
189 | if (a != NULL) {
|
---|
190 | a->buf = NULL;
|
---|
191 | a->len = 0;
|
---|
192 | a->heap = NULL;
|
---|
193 | }
|
---|
194 | }
|
---|
195 |
|
---|
196 | int wc_bigint_alloc(WC_BIGINT* a, word32 sz)
|
---|
197 | {
|
---|
198 | int err = MP_OKAY;
|
---|
199 |
|
---|
200 | if (a == NULL)
|
---|
201 | return BAD_FUNC_ARG;
|
---|
202 |
|
---|
203 | if (sz > 0) {
|
---|
204 | if (a->buf && sz > a->len) {
|
---|
205 | wc_bigint_free(a);
|
---|
206 | }
|
---|
207 | if (a->buf == NULL) {
|
---|
208 | a->buf = (byte*)XMALLOC(sz, a->heap, DYNAMIC_TYPE_WOLF_BIGINT);
|
---|
209 | }
|
---|
210 | if (a->buf == NULL) {
|
---|
211 | err = MP_MEM;
|
---|
212 | }
|
---|
213 | else {
|
---|
214 | XMEMSET(a->buf, 0, sz);
|
---|
215 | }
|
---|
216 | }
|
---|
217 | a->len = sz;
|
---|
218 |
|
---|
219 | return err;
|
---|
220 | }
|
---|
221 |
|
---|
222 | /* assumes input is big endian format */
|
---|
223 | int wc_bigint_from_unsigned_bin(WC_BIGINT* a, const byte* in, word32 inlen)
|
---|
224 | {
|
---|
225 | int err;
|
---|
226 |
|
---|
227 | if (a == NULL || in == NULL || inlen == 0)
|
---|
228 | return BAD_FUNC_ARG;
|
---|
229 |
|
---|
230 | err = wc_bigint_alloc(a, inlen);
|
---|
231 | if (err == 0) {
|
---|
232 | XMEMCPY(a->buf, in, inlen);
|
---|
233 | }
|
---|
234 |
|
---|
235 | return err;
|
---|
236 | }
|
---|
237 |
|
---|
238 | int wc_bigint_to_unsigned_bin(WC_BIGINT* a, byte* out, word32* outlen)
|
---|
239 | {
|
---|
240 | word32 sz;
|
---|
241 |
|
---|
242 | if (a == NULL || out == NULL || outlen == NULL || *outlen == 0)
|
---|
243 | return BAD_FUNC_ARG;
|
---|
244 |
|
---|
245 | /* trim to fit into output buffer */
|
---|
246 | sz = a->len;
|
---|
247 | if (a->len > *outlen) {
|
---|
248 | WOLFSSL_MSG("wc_bigint_export: Truncating output");
|
---|
249 | sz = *outlen;
|
---|
250 | }
|
---|
251 |
|
---|
252 | if (a->buf) {
|
---|
253 | XMEMCPY(out, a->buf, sz);
|
---|
254 | }
|
---|
255 |
|
---|
256 | *outlen = sz;
|
---|
257 |
|
---|
258 | return MP_OKAY;
|
---|
259 | }
|
---|
260 |
|
---|
261 | void wc_bigint_zero(WC_BIGINT* a)
|
---|
262 | {
|
---|
263 | if (a && a->buf) {
|
---|
264 | ForceZero(a->buf, a->len);
|
---|
265 | }
|
---|
266 | }
|
---|
267 |
|
---|
268 | void wc_bigint_free(WC_BIGINT* a)
|
---|
269 | {
|
---|
270 | if (a) {
|
---|
271 | if (a->buf) {
|
---|
272 | XFREE(a->buf, a->heap, DYNAMIC_TYPE_WOLF_BIGINT);
|
---|
273 | }
|
---|
274 | a->buf = NULL;
|
---|
275 | a->len = 0;
|
---|
276 | }
|
---|
277 | }
|
---|
278 |
|
---|
279 | /* sz: make sure the buffer is at least that size and zero padded.
|
---|
280 | * A `sz == 0` will use the size of `src`.
|
---|
281 | * The calulcates sz is stored into dst->len in `wc_bigint_alloc`.
|
---|
282 | */
|
---|
283 | int wc_mp_to_bigint_sz(mp_int* src, WC_BIGINT* dst, word32 sz)
|
---|
284 | {
|
---|
285 | int err;
|
---|
286 | word32 x, y;
|
---|
287 |
|
---|
288 | if (src == NULL || dst == NULL)
|
---|
289 | return BAD_FUNC_ARG;
|
---|
290 |
|
---|
291 | /* get size of source */
|
---|
292 | x = mp_unsigned_bin_size(src);
|
---|
293 | if (sz < x)
|
---|
294 | sz = x;
|
---|
295 |
|
---|
296 | /* make sure destination is allocated and large enough */
|
---|
297 | err = wc_bigint_alloc(dst, sz);
|
---|
298 | if (err == MP_OKAY) {
|
---|
299 |
|
---|
300 | /* leading zero pad */
|
---|
301 | y = sz - x;
|
---|
302 | XMEMSET(dst->buf, 0, y);
|
---|
303 |
|
---|
304 | /* export src as unsigned bin to destination buf */
|
---|
305 | err = mp_to_unsigned_bin(src, dst->buf + y);
|
---|
306 | }
|
---|
307 |
|
---|
308 | return err;
|
---|
309 | }
|
---|
310 |
|
---|
311 | int wc_mp_to_bigint(mp_int* src, WC_BIGINT* dst)
|
---|
312 | {
|
---|
313 | if (src == NULL || dst == NULL)
|
---|
314 | return BAD_FUNC_ARG;
|
---|
315 |
|
---|
316 | return wc_mp_to_bigint_sz(src, dst, 0);
|
---|
317 | }
|
---|
318 |
|
---|
319 | int wc_bigint_to_mp(WC_BIGINT* src, mp_int* dst)
|
---|
320 | {
|
---|
321 | int err;
|
---|
322 |
|
---|
323 | if (src == NULL || dst == NULL)
|
---|
324 | return BAD_FUNC_ARG;
|
---|
325 |
|
---|
326 | if (src->buf == NULL)
|
---|
327 | return BAD_FUNC_ARG;
|
---|
328 |
|
---|
329 | err = mp_read_unsigned_bin(dst, src->buf, src->len);
|
---|
330 | wc_bigint_free(src);
|
---|
331 |
|
---|
332 | return err;
|
---|
333 | }
|
---|
334 |
|
---|
335 | #endif /* HAVE_WOLF_BIGINT */
|
---|
336 |
|
---|
337 | #endif /* USE_FAST_MATH || !NO_BIG_INT */
|
---|