[457] | 1 | /* wolfmath.h
|
---|
| 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 | #ifndef __WOLFMATH_H__
|
---|
| 23 | #define __WOLFMATH_H__
|
---|
| 24 |
|
---|
| 25 | #ifdef __cplusplus
|
---|
| 26 | extern "C" {
|
---|
| 27 | #endif
|
---|
| 28 |
|
---|
| 29 | #ifdef WOLFSSL_PUBLIC_MP
|
---|
| 30 | #define MP_API WOLFSSL_API
|
---|
| 31 | #else
|
---|
| 32 | #define MP_API WOLFSSL_LOCAL
|
---|
| 33 | #endif
|
---|
| 34 |
|
---|
| 35 | #ifndef MIN
|
---|
| 36 | #define MIN(x,y) ((x)<(y)?(x):(y))
|
---|
| 37 | #endif
|
---|
| 38 |
|
---|
| 39 | #ifndef MAX
|
---|
| 40 | #define MAX(x,y) ((x)>(y)?(x):(y))
|
---|
| 41 | #endif
|
---|
| 42 |
|
---|
| 43 | /* timing resistance array */
|
---|
| 44 | #if !defined(WC_NO_CACHE_RESISTANT) && \
|
---|
| 45 | ((defined(HAVE_ECC) && defined(ECC_TIMING_RESISTANT)) || \
|
---|
| 46 | (defined(USE_FAST_MATH) && defined(TFM_TIMING_RESISTANT)))
|
---|
| 47 |
|
---|
| 48 | extern const wolfssl_word wc_off_on_addr[2];
|
---|
| 49 | #endif
|
---|
| 50 |
|
---|
| 51 |
|
---|
| 52 | /* common math functions */
|
---|
| 53 | MP_API int get_digit_count(mp_int* a);
|
---|
| 54 | MP_API mp_digit get_digit(mp_int* a, int n);
|
---|
| 55 | MP_API int get_rand_digit(WC_RNG* rng, mp_digit* d);
|
---|
| 56 |
|
---|
| 57 | WOLFSSL_API int mp_cond_copy(mp_int* a, int copy, mp_int* b);
|
---|
| 58 | WOLFSSL_API int mp_rand(mp_int* a, int digits, WC_RNG* rng);
|
---|
| 59 |
|
---|
| 60 | enum {
|
---|
| 61 | /* format type */
|
---|
| 62 | WC_TYPE_HEX_STR = 1,
|
---|
| 63 | WC_TYPE_UNSIGNED_BIN = 2,
|
---|
| 64 | };
|
---|
| 65 |
|
---|
| 66 | WOLFSSL_API int wc_export_int(mp_int* mp, byte* buf, word32* len,
|
---|
| 67 | word32 keySz, int encType);
|
---|
| 68 |
|
---|
| 69 | #ifdef HAVE_WOLF_BIGINT
|
---|
| 70 | #if !defined(WOLF_BIGINT_DEFINED)
|
---|
| 71 | /* raw big integer */
|
---|
| 72 | typedef struct WC_BIGINT {
|
---|
| 73 | byte* buf;
|
---|
| 74 | word32 len;
|
---|
| 75 | void* heap;
|
---|
| 76 | } WC_BIGINT;
|
---|
| 77 | #define WOLF_BIGINT_DEFINED
|
---|
| 78 | #endif
|
---|
| 79 |
|
---|
| 80 | WOLFSSL_LOCAL void wc_bigint_init(WC_BIGINT* a);
|
---|
| 81 | WOLFSSL_LOCAL int wc_bigint_alloc(WC_BIGINT* a, word32 sz);
|
---|
| 82 | WOLFSSL_LOCAL int wc_bigint_from_unsigned_bin(WC_BIGINT* a, const byte* in, word32 inlen);
|
---|
| 83 | WOLFSSL_LOCAL int wc_bigint_to_unsigned_bin(WC_BIGINT* a, byte* out, word32* outlen);
|
---|
| 84 | WOLFSSL_LOCAL void wc_bigint_zero(WC_BIGINT* a);
|
---|
| 85 | WOLFSSL_LOCAL void wc_bigint_free(WC_BIGINT* a);
|
---|
| 86 |
|
---|
| 87 | WOLFSSL_LOCAL int wc_mp_to_bigint(mp_int* src, WC_BIGINT* dst);
|
---|
| 88 | WOLFSSL_LOCAL int wc_mp_to_bigint_sz(mp_int* src, WC_BIGINT* dst, word32 sz);
|
---|
| 89 | WOLFSSL_LOCAL int wc_bigint_to_mp(WC_BIGINT* src, mp_int* dst);
|
---|
| 90 | #endif /* HAVE_WOLF_BIGINT */
|
---|
| 91 |
|
---|
| 92 |
|
---|
| 93 | #ifdef __cplusplus
|
---|
| 94 | } /* extern "C" */
|
---|
| 95 | #endif
|
---|
| 96 |
|
---|
| 97 | #endif /* __WOLFMATH_H__ */
|
---|