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