source: asp3_tinet_ecnl_arm/trunk/wolfssl-3.12.2/wolfcrypt/src/wolfmath.c@ 352

Last change on this file since 352 was 352, checked in by coas-nagasima, 6 years ago

arm向けASP3版ECNLを追加

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
File size: 5.5 KB
Line 
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#ifdef USE_FAST_MATH
33 #include <wolfssl/wolfcrypt/tfm.h>
34#else
35 #include <wolfssl/wolfcrypt/integer.h>
36#endif
37
38#include <wolfssl/wolfcrypt/error-crypt.h>
39#include <wolfssl/wolfcrypt/logging.h>
40
41#if defined(USE_FAST_MATH) || !defined(NO_BIG_INT)
42
43#ifdef WOLFSSL_ASYNC_CRYPT
44 #include <wolfssl/wolfcrypt/async.h>
45#endif
46
47#ifdef NO_INLINE
48 #include <wolfssl/wolfcrypt/misc.h>
49#else
50 #define WOLFSSL_MISC_INCLUDED
51 #include <wolfcrypt/src/misc.c>
52#endif
53
54
55#if !defined(WC_NO_CACHE_RESISTANT) && \
56 ((defined(HAVE_ECC) && defined(ECC_TIMING_RESISTANT)) || \
57 (defined(USE_FAST_MATH) && defined(TFM_TIMING_RESISTANT)))
58
59 /* all off / all on pointer addresses for constant calculations */
60 /* ecc.c uses same table */
61 const wolfssl_word wc_off_on_addr[2] =
62 {
63 #if defined(WC_64BIT_CPU)
64 W64LIT(0x0000000000000000),
65 W64LIT(0xffffffffffffffff)
66 #elif defined(WC_16BIT_CPU)
67 0x0000U,
68 0xffffU
69 #else
70 /* 32 bit */
71 0x00000000U,
72 0xffffffffU
73 #endif
74 };
75#endif
76
77
78int get_digit_count(mp_int* a)
79{
80 if (a == NULL)
81 return 0;
82
83 return a->used;
84}
85
86mp_digit get_digit(mp_int* a, int n)
87{
88 if (a == NULL)
89 return 0;
90
91 return (n >= a->used || n < 0) ? 0 : a->dp[n];
92}
93
94int get_rand_digit(WC_RNG* rng, mp_digit* d)
95{
96 return wc_RNG_GenerateBlock(rng, (byte*)d, sizeof(mp_digit));
97}
98
99#ifdef WC_RSA_BLINDING
100int mp_rand(mp_int* a, int digits, WC_RNG* rng)
101{
102 int ret;
103 mp_digit d;
104
105 if (rng == NULL)
106 return MISSING_RNG_E;
107
108 if (a == NULL)
109 return BAD_FUNC_ARG;
110
111 mp_zero(a);
112 if (digits <= 0) {
113 return MP_OKAY;
114 }
115
116 /* first place a random non-zero digit */
117 do {
118 ret = get_rand_digit(rng, &d);
119 if (ret != 0) {
120 return ret;
121 }
122 } while (d == 0);
123
124 if ((ret = mp_add_d(a, d, a)) != MP_OKAY) {
125 return ret;
126 }
127
128 while (--digits > 0) {
129 if ((ret = mp_lshd(a, 1)) != MP_OKAY) {
130 return ret;
131 }
132 if ((ret = get_rand_digit(rng, &d)) != 0) {
133 return ret;
134 }
135 if ((ret = mp_add_d(a, d, a)) != MP_OKAY) {
136 return ret;
137 }
138 }
139
140 return ret;
141}
142#endif /* WC_RSA_BLINDING */
143
144
145#ifdef HAVE_WOLF_BIGINT
146void wc_bigint_init(WC_BIGINT* a)
147{
148 if (a != NULL) {
149 a->buf = NULL;
150 a->len = 0;
151 a->heap = NULL;
152 }
153}
154
155int wc_bigint_alloc(WC_BIGINT* a, word32 sz)
156{
157 int err = MP_OKAY;
158
159 if (a == NULL)
160 return BAD_FUNC_ARG;
161
162 if (sz > 0) {
163 if (a->buf && sz > a->len) {
164 wc_bigint_free(a);
165 }
166 if (a->buf == NULL) {
167 a->buf = (byte*)XMALLOC(sz, a->heap, DYNAMIC_TYPE_WOLF_BIGINT);
168 }
169 if (a->buf == NULL) {
170 err = MP_MEM;
171 }
172 else {
173 XMEMSET(a->buf, 0, sz);
174 }
175 }
176 a->len = sz;
177
178 return err;
179}
180
181/* assumes input is big endian format */
182int wc_bigint_from_unsigned_bin(WC_BIGINT* a, const byte* in, word32 inlen)
183{
184 int err;
185
186 if (a == NULL || in == NULL || inlen == 0)
187 return BAD_FUNC_ARG;
188
189 err = wc_bigint_alloc(a, inlen);
190 if (err == 0) {
191 XMEMCPY(a->buf, in, inlen);
192 }
193
194 return err;
195}
196
197int wc_bigint_to_unsigned_bin(WC_BIGINT* a, byte* out, word32* outlen)
198{
199 word32 sz;
200
201 if (a == NULL || out == NULL || outlen == NULL || *outlen == 0)
202 return BAD_FUNC_ARG;
203
204 /* trim to fit into output buffer */
205 sz = a->len;
206 if (a->len > *outlen) {
207 WOLFSSL_MSG("wc_bigint_export: Truncating output");
208 sz = *outlen;
209 }
210
211 if (a->buf) {
212 XMEMCPY(out, a->buf, sz);
213 }
214
215 *outlen = sz;
216
217 return MP_OKAY;
218}
219
220void wc_bigint_zero(WC_BIGINT* a)
221{
222 if (a && a->buf) {
223 ForceZero(a->buf, a->len);
224 }
225}
226
227void wc_bigint_free(WC_BIGINT* a)
228{
229 if (a) {
230 if (a->buf) {
231 XFREE(a->buf, a->heap, DYNAMIC_TYPE_WOLF_BIGINT);
232 }
233 a->buf = NULL;
234 a->len = 0;
235 }
236}
237
238int wc_mp_to_bigint(mp_int* src, WC_BIGINT* dst)
239{
240 int err;
241 word32 sz;
242
243 if (src == NULL || dst == NULL)
244 return BAD_FUNC_ARG;
245
246 sz = mp_unsigned_bin_size(src);
247 err = wc_bigint_alloc(dst, sz);
248 if (err == MP_OKAY)
249 err = mp_to_unsigned_bin(src, dst->buf);
250
251 return err;
252}
253
254int wc_bigint_to_mp(WC_BIGINT* src, mp_int* dst)
255{
256 int err;
257
258 if (src == NULL || dst == NULL)
259 return BAD_FUNC_ARG;
260
261 if (src->buf == NULL)
262 return BAD_FUNC_ARG;
263
264 err = mp_read_unsigned_bin(dst, src->buf, src->len);
265 wc_bigint_free(src);
266
267 return err;
268}
269
270#endif /* HAVE_WOLF_BIGINT */
271
272#endif /* USE_FAST_MATH || !NO_BIG_INT */
Note: See TracBrowser for help on using the repository browser.