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