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