wolfmath.h (5179B)
1 /* wolfmath.h 2 * 3 * Copyright (C) 2006-2025 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 3 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 DESCRIPTION 24 This library provides big integer math functions. 25 26 */ 27 #ifndef __WOLFMATH_H__ 28 #define __WOLFMATH_H__ 29 30 #ifdef __cplusplus 31 extern "C" { 32 #endif 33 34 #include <wolfssl/wolfcrypt/types.h> 35 36 #ifdef WOLFSSL_PUBLIC_MP 37 #define MP_API WOLFSSL_API 38 #else 39 #define MP_API WOLFSSL_LOCAL 40 #endif 41 42 43 #if defined(NO_BIG_INT) 44 /* MPI globally disabled -- no PK algorithms supported. */ 45 #if defined(USE_FAST_MATH) || defined(USE_INTEGER_HEAP_MATH) || \ 46 defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_SP_MATH) || \ 47 defined(HAVE_WOLF_BIGINT) || defined(WOLFSSL_EXPORT_INT) 48 #error Conflicting MPI settings. 49 #endif 50 #elif defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_SP_MATH) 51 #include <wolfssl/wolfcrypt/sp_int.h> 52 #elif defined(USE_FAST_MATH) 53 #include <wolfssl/wolfcrypt/tfm.h> 54 #elif defined(USE_INTEGER_HEAP_MATH) 55 #include <wolfssl/wolfcrypt/integer.h> 56 #else 57 #include <wolfssl/wolfcrypt/sp_int.h> 58 #endif 59 60 #if !defined(NO_BIG_INT) 61 #include <wolfssl/wolfcrypt/random.h> 62 #endif 63 64 #if defined(WOLFSSL_MAX3266X) || defined(WOLFSSL_MAX3266X_OLD) 65 #include <wolfssl/wolfcrypt/port/maxim/max3266x.h> 66 #endif 67 68 #ifndef MIN 69 #define MIN(x,y) ((x)<(y)?(x):(y)) 70 #endif 71 72 #ifndef MAX 73 #define MAX(x,y) ((x)>(y)?(x):(y)) 74 #endif 75 76 /* timing resistance array */ 77 #if !defined(WC_NO_CACHE_RESISTANT) && \ 78 ((defined(HAVE_ECC) && defined(ECC_TIMING_RESISTANT)) || \ 79 (defined(USE_FAST_MATH) && defined(TFM_TIMING_RESISTANT))) 80 81 extern const wc_ptr_t wc_off_on_addr[2]; 82 #endif 83 84 #if !defined(NO_BIG_INT) 85 /* common math functions */ 86 MP_API int mp_get_digit_count(const mp_int* a); 87 MP_API mp_digit mp_get_digit(const mp_int* a, int n); 88 MP_API int mp_get_rand_digit(WC_RNG* rng, mp_digit* d); 89 WOLFSSL_LOCAL void mp_reverse(unsigned char *s, int len); 90 91 #if defined(HAVE_FIPS) || defined(HAVE_SELFTEST) 92 #define get_digit_count mp_get_digit_count 93 #define get_digit mp_get_digit 94 #define get_rand_digit mp_get_rand_digit 95 #endif 96 97 WOLFSSL_API int mp_cond_copy(mp_int* a, int copy, mp_int* b); 98 WOLFSSL_API int mp_rand(mp_int* a, int digits, WC_RNG* rng); 99 #endif 100 101 #define WC_TYPE_HEX_STR 1 102 #define WC_TYPE_UNSIGNED_BIN 2 103 #if defined(WOLFSSL_QNX_CAAM) || defined(WOLFSSL_IMXRT1170_CAAM) 104 #define WC_TYPE_BLACK_KEY 3 105 #endif 106 107 #if defined(HAVE_ECC) || defined(WOLFSSL_EXPORT_INT) 108 WOLFSSL_API int wc_export_int(mp_int* mp, byte* buf, word32* len, 109 word32 keySz, int encType); 110 #endif 111 112 #ifdef HAVE_WOLF_BIGINT 113 #if !defined(WOLF_BIGINT_DEFINED) 114 /* raw big integer */ 115 typedef struct WC_BIGINT { 116 byte* buf; 117 word32 len; 118 void* heap; 119 } WC_BIGINT; 120 #define WOLF_BIGINT_DEFINED 121 #endif 122 123 WOLFSSL_LOCAL void wc_bigint_init(WC_BIGINT* a); 124 WOLFSSL_LOCAL int wc_bigint_alloc(WC_BIGINT* a, word32 sz); 125 WOLFSSL_LOCAL int wc_bigint_from_unsigned_bin(WC_BIGINT* a, const byte* in, word32 inlen); 126 WOLFSSL_LOCAL int wc_bigint_to_unsigned_bin(WC_BIGINT* a, byte* out, word32* outlen); 127 WOLFSSL_LOCAL void wc_bigint_zero(WC_BIGINT* a); 128 WOLFSSL_LOCAL void wc_bigint_free(WC_BIGINT* a); 129 130 WOLFSSL_LOCAL int wc_mp_to_bigint(mp_int* src, WC_BIGINT* dst); 131 WOLFSSL_LOCAL int wc_mp_to_bigint_sz(mp_int* src, WC_BIGINT* dst, word32 sz); 132 WOLFSSL_LOCAL int wc_bigint_to_mp(WC_BIGINT* src, mp_int* dst); 133 #endif /* HAVE_WOLF_BIGINT */ 134 135 136 #ifdef HAVE_WC_INTROSPECTION 137 WOLFSSL_API const char *wc_GetMathInfo(void); 138 #endif 139 140 /* Support for generic Hardware based Math Functions */ 141 #ifdef WOLFSSL_USE_HW_MP 142 143 WOLFSSL_LOCAL int hw_mod(mp_int* multiplier, mp_int* mod, mp_int* result); 144 WOLFSSL_LOCAL int hw_mulmod(mp_int* multiplier, mp_int* multiplicand, 145 mp_int* mod, mp_int* result); 146 WOLFSSL_LOCAL int hw_addmod(mp_int* a, mp_int* b, mp_int* mod, mp_int* result); 147 WOLFSSL_LOCAL int hw_submod(mp_int* a, mp_int* b, mp_int* mod, mp_int* result); 148 WOLFSSL_LOCAL int hw_exptmod(mp_int* base, mp_int* exp, mp_int* mod, 149 mp_int* result); 150 WOLFSSL_LOCAL int hw_sqrmod(mp_int* base, mp_int* mod, mp_int* result); 151 152 /* One to one mappings */ 153 #define mp_mod hw_mod 154 #define mp_addmod hw_addmod 155 #define mp_submod hw_submod 156 #define mp_mulmod hw_mulmod 157 #define mp_exptmod hw_exptmod 158 #define mp_sqrmod hw_sqrmod 159 160 #endif 161 162 #ifdef __cplusplus 163 } /* extern "C" */ 164 #endif 165 166 #endif /* __WOLFMATH_H__ */