ssh lib
wolfcrypt/integer.h@1:e4ea39eba2fb, 2019-11-25 (annotated)
- Committer:
- sPymbed
- Date:
- Mon Nov 25 14:23:49 2019 +0000
- Revision:
- 1:e4ea39eba2fb
- Parent:
- 0:1387ff3eed4a
improved
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sPymbed | 0:1387ff3eed4a | 1 | /* integer.h |
sPymbed | 0:1387ff3eed4a | 2 | * |
sPymbed | 0:1387ff3eed4a | 3 | * Copyright (C) 2006-2017 wolfSSL Inc. |
sPymbed | 0:1387ff3eed4a | 4 | * |
sPymbed | 0:1387ff3eed4a | 5 | * This file is part of wolfSSL. |
sPymbed | 0:1387ff3eed4a | 6 | * |
sPymbed | 0:1387ff3eed4a | 7 | * wolfSSL is free software; you can redistribute it and/or modify |
sPymbed | 0:1387ff3eed4a | 8 | * it under the terms of the GNU General Public License as published by |
sPymbed | 0:1387ff3eed4a | 9 | * the Free Software Foundation; either version 2 of the License, or |
sPymbed | 0:1387ff3eed4a | 10 | * (at your option) any later version. |
sPymbed | 0:1387ff3eed4a | 11 | * |
sPymbed | 0:1387ff3eed4a | 12 | * wolfSSL is distributed in the hope that it will be useful, |
sPymbed | 0:1387ff3eed4a | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
sPymbed | 0:1387ff3eed4a | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
sPymbed | 0:1387ff3eed4a | 15 | * GNU General Public License for more details. |
sPymbed | 0:1387ff3eed4a | 16 | * |
sPymbed | 0:1387ff3eed4a | 17 | * You should have received a copy of the GNU General Public License |
sPymbed | 0:1387ff3eed4a | 18 | * along with this program; if not, write to the Free Software |
sPymbed | 0:1387ff3eed4a | 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
sPymbed | 0:1387ff3eed4a | 20 | */ |
sPymbed | 0:1387ff3eed4a | 21 | |
sPymbed | 0:1387ff3eed4a | 22 | |
sPymbed | 0:1387ff3eed4a | 23 | /* |
sPymbed | 0:1387ff3eed4a | 24 | * Based on public domain LibTomMath 0.38 by Tom St Denis, tomstdenis@iahu.ca, |
sPymbed | 0:1387ff3eed4a | 25 | * http://math.libtomcrypt.com |
sPymbed | 0:1387ff3eed4a | 26 | */ |
sPymbed | 0:1387ff3eed4a | 27 | |
sPymbed | 0:1387ff3eed4a | 28 | |
sPymbed | 0:1387ff3eed4a | 29 | #ifndef WOLF_CRYPT_INTEGER_H |
sPymbed | 0:1387ff3eed4a | 30 | #define WOLF_CRYPT_INTEGER_H |
sPymbed | 0:1387ff3eed4a | 31 | |
sPymbed | 0:1387ff3eed4a | 32 | /* may optionally use fast math instead, not yet supported on all platforms and |
sPymbed | 0:1387ff3eed4a | 33 | may not be faster on all |
sPymbed | 0:1387ff3eed4a | 34 | */ |
sPymbed | 0:1387ff3eed4a | 35 | #include <wolfcrypt/types.h> /* will set MP_xxBIT if not default */ |
sPymbed | 0:1387ff3eed4a | 36 | #ifdef WOLFSSL_SP_MATH |
sPymbed | 0:1387ff3eed4a | 37 | #include <wolfcrypt/sp_int.h> |
sPymbed | 0:1387ff3eed4a | 38 | #elif defined(USE_FAST_MATH) |
sPymbed | 0:1387ff3eed4a | 39 | #include <wolfcrypt/tfm.h> |
sPymbed | 0:1387ff3eed4a | 40 | #else |
sPymbed | 0:1387ff3eed4a | 41 | |
sPymbed | 0:1387ff3eed4a | 42 | #include <wolfcrypt/random.h> |
sPymbed | 0:1387ff3eed4a | 43 | |
sPymbed | 0:1387ff3eed4a | 44 | #ifndef CHAR_BIT |
sPymbed | 0:1387ff3eed4a | 45 | #include <limits.h> |
sPymbed | 0:1387ff3eed4a | 46 | #endif |
sPymbed | 0:1387ff3eed4a | 47 | |
sPymbed | 0:1387ff3eed4a | 48 | #include <wolfcrypt/mpi_class.h> |
sPymbed | 0:1387ff3eed4a | 49 | |
sPymbed | 0:1387ff3eed4a | 50 | /* wolf big int and common functions */ |
sPymbed | 0:1387ff3eed4a | 51 | #include <wolfcrypt/wolfmath.h> |
sPymbed | 0:1387ff3eed4a | 52 | |
sPymbed | 0:1387ff3eed4a | 53 | |
sPymbed | 0:1387ff3eed4a | 54 | #ifdef WOLFSSL_PUBLIC_MP |
sPymbed | 0:1387ff3eed4a | 55 | #define MP_API WOLFSSL_API |
sPymbed | 0:1387ff3eed4a | 56 | #else |
sPymbed | 0:1387ff3eed4a | 57 | #define MP_API |
sPymbed | 0:1387ff3eed4a | 58 | #endif |
sPymbed | 0:1387ff3eed4a | 59 | |
sPymbed | 0:1387ff3eed4a | 60 | #ifndef MIN |
sPymbed | 0:1387ff3eed4a | 61 | #define MIN(x,y) ((x)<(y)?(x):(y)) |
sPymbed | 0:1387ff3eed4a | 62 | #endif |
sPymbed | 0:1387ff3eed4a | 63 | |
sPymbed | 0:1387ff3eed4a | 64 | #ifndef MAX |
sPymbed | 0:1387ff3eed4a | 65 | #define MAX(x,y) ((x)>(y)?(x):(y)) |
sPymbed | 0:1387ff3eed4a | 66 | #endif |
sPymbed | 0:1387ff3eed4a | 67 | |
sPymbed | 0:1387ff3eed4a | 68 | #ifdef __cplusplus |
sPymbed | 0:1387ff3eed4a | 69 | extern "C" { |
sPymbed | 0:1387ff3eed4a | 70 | |
sPymbed | 0:1387ff3eed4a | 71 | /* C++ compilers don't like assigning void * to mp_digit * */ |
sPymbed | 0:1387ff3eed4a | 72 | #define OPT_CAST(x) (x *) |
sPymbed | 0:1387ff3eed4a | 73 | |
sPymbed | 0:1387ff3eed4a | 74 | #elif defined(_SH3) |
sPymbed | 0:1387ff3eed4a | 75 | |
sPymbed | 0:1387ff3eed4a | 76 | /* SuperH SH3 compiler doesn't like assigning voi* to mp_digit* */ |
sPymbed | 0:1387ff3eed4a | 77 | #define OPT_CAST(x) (x *) |
sPymbed | 0:1387ff3eed4a | 78 | |
sPymbed | 0:1387ff3eed4a | 79 | #else |
sPymbed | 0:1387ff3eed4a | 80 | |
sPymbed | 0:1387ff3eed4a | 81 | /* C on the other hand doesn't care */ |
sPymbed | 0:1387ff3eed4a | 82 | #define OPT_CAST(x) |
sPymbed | 0:1387ff3eed4a | 83 | |
sPymbed | 0:1387ff3eed4a | 84 | #endif /* __cplusplus */ |
sPymbed | 0:1387ff3eed4a | 85 | |
sPymbed | 0:1387ff3eed4a | 86 | |
sPymbed | 0:1387ff3eed4a | 87 | /* detect 64-bit mode if possible */ |
sPymbed | 0:1387ff3eed4a | 88 | #if defined(__x86_64__) |
sPymbed | 0:1387ff3eed4a | 89 | #if !(defined(MP_64BIT) && defined(MP_16BIT) && defined(MP_8BIT)) |
sPymbed | 0:1387ff3eed4a | 90 | #define MP_64BIT |
sPymbed | 0:1387ff3eed4a | 91 | #endif |
sPymbed | 0:1387ff3eed4a | 92 | #endif |
sPymbed | 0:1387ff3eed4a | 93 | /* if intel compiler doesn't provide 128 bit type don't turn on 64bit */ |
sPymbed | 0:1387ff3eed4a | 94 | #if defined(MP_64BIT) && defined(__INTEL_COMPILER) && !defined(HAVE___UINT128_T) |
sPymbed | 0:1387ff3eed4a | 95 | #undef MP_64BIT |
sPymbed | 0:1387ff3eed4a | 96 | #endif |
sPymbed | 0:1387ff3eed4a | 97 | |
sPymbed | 0:1387ff3eed4a | 98 | |
sPymbed | 0:1387ff3eed4a | 99 | /* allow user to define on mp_digit, mp_word, DIGIT_BIT types */ |
sPymbed | 0:1387ff3eed4a | 100 | #ifndef WOLFSSL_BIGINT_TYPES |
sPymbed | 0:1387ff3eed4a | 101 | |
sPymbed | 0:1387ff3eed4a | 102 | /* some default configurations. |
sPymbed | 0:1387ff3eed4a | 103 | * |
sPymbed | 0:1387ff3eed4a | 104 | * A "mp_digit" must be able to hold DIGIT_BIT + 1 bits |
sPymbed | 0:1387ff3eed4a | 105 | * A "mp_word" must be able to hold 2*DIGIT_BIT + 1 bits |
sPymbed | 0:1387ff3eed4a | 106 | * |
sPymbed | 0:1387ff3eed4a | 107 | * At the very least a mp_digit must be able to hold 7 bits |
sPymbed | 0:1387ff3eed4a | 108 | * [any size beyond that is ok provided it doesn't overflow the data type] |
sPymbed | 0:1387ff3eed4a | 109 | */ |
sPymbed | 0:1387ff3eed4a | 110 | #ifdef MP_8BIT |
sPymbed | 0:1387ff3eed4a | 111 | typedef unsigned char mp_digit; |
sPymbed | 0:1387ff3eed4a | 112 | typedef unsigned short mp_word; |
sPymbed | 0:1387ff3eed4a | 113 | #elif defined(MP_16BIT) || defined(NO_64BIT) |
sPymbed | 0:1387ff3eed4a | 114 | typedef unsigned short mp_digit; |
sPymbed | 0:1387ff3eed4a | 115 | typedef unsigned int mp_word; |
sPymbed | 0:1387ff3eed4a | 116 | #define DIGIT_BIT 12 |
sPymbed | 0:1387ff3eed4a | 117 | #elif defined(MP_64BIT) |
sPymbed | 0:1387ff3eed4a | 118 | /* for GCC only on supported platforms */ |
sPymbed | 0:1387ff3eed4a | 119 | typedef unsigned long long mp_digit; /* 64 bit type, 128 uses mode(TI) */ |
sPymbed | 0:1387ff3eed4a | 120 | typedef unsigned long mp_word __attribute__ ((mode(TI))); |
sPymbed | 0:1387ff3eed4a | 121 | |
sPymbed | 0:1387ff3eed4a | 122 | #define DIGIT_BIT 60 |
sPymbed | 0:1387ff3eed4a | 123 | #else |
sPymbed | 0:1387ff3eed4a | 124 | /* this is the default case, 28-bit digits */ |
sPymbed | 0:1387ff3eed4a | 125 | |
sPymbed | 0:1387ff3eed4a | 126 | #if defined(_MSC_VER) || defined(__BORLANDC__) |
sPymbed | 0:1387ff3eed4a | 127 | typedef unsigned __int64 ulong64; |
sPymbed | 0:1387ff3eed4a | 128 | #else |
sPymbed | 0:1387ff3eed4a | 129 | typedef unsigned long long ulong64; |
sPymbed | 0:1387ff3eed4a | 130 | #endif |
sPymbed | 0:1387ff3eed4a | 131 | |
sPymbed | 0:1387ff3eed4a | 132 | typedef unsigned int mp_digit; /* long could be 64 now, changed TAO */ |
sPymbed | 0:1387ff3eed4a | 133 | typedef ulong64 mp_word; |
sPymbed | 0:1387ff3eed4a | 134 | |
sPymbed | 0:1387ff3eed4a | 135 | #ifdef MP_31BIT |
sPymbed | 0:1387ff3eed4a | 136 | /* this is an extension that uses 31-bit digits */ |
sPymbed | 0:1387ff3eed4a | 137 | #define DIGIT_BIT 31 |
sPymbed | 0:1387ff3eed4a | 138 | #else |
sPymbed | 0:1387ff3eed4a | 139 | /* default case is 28-bit digits, defines MP_28BIT as a handy test macro */ |
sPymbed | 0:1387ff3eed4a | 140 | #define DIGIT_BIT 28 |
sPymbed | 0:1387ff3eed4a | 141 | #define MP_28BIT |
sPymbed | 0:1387ff3eed4a | 142 | #endif |
sPymbed | 0:1387ff3eed4a | 143 | #endif |
sPymbed | 0:1387ff3eed4a | 144 | |
sPymbed | 0:1387ff3eed4a | 145 | #endif /* WOLFSSL_BIGINT_TYPES */ |
sPymbed | 0:1387ff3eed4a | 146 | |
sPymbed | 0:1387ff3eed4a | 147 | /* otherwise the bits per digit is calculated automatically from the size of |
sPymbed | 0:1387ff3eed4a | 148 | a mp_digit */ |
sPymbed | 0:1387ff3eed4a | 149 | #ifndef DIGIT_BIT |
sPymbed | 0:1387ff3eed4a | 150 | #define DIGIT_BIT ((int)((CHAR_BIT * sizeof(mp_digit) - 1))) |
sPymbed | 0:1387ff3eed4a | 151 | /* bits per digit */ |
sPymbed | 0:1387ff3eed4a | 152 | #endif |
sPymbed | 0:1387ff3eed4a | 153 | |
sPymbed | 0:1387ff3eed4a | 154 | #define MP_DIGIT_BIT DIGIT_BIT |
sPymbed | 0:1387ff3eed4a | 155 | #define MP_MASK ((((mp_digit)1)<<((mp_digit)DIGIT_BIT))-((mp_digit)1)) |
sPymbed | 0:1387ff3eed4a | 156 | #define MP_DIGIT_MAX MP_MASK |
sPymbed | 0:1387ff3eed4a | 157 | |
sPymbed | 0:1387ff3eed4a | 158 | /* equalities */ |
sPymbed | 0:1387ff3eed4a | 159 | #define MP_LT -1 /* less than */ |
sPymbed | 0:1387ff3eed4a | 160 | #define MP_EQ 0 /* equal to */ |
sPymbed | 0:1387ff3eed4a | 161 | #define MP_GT 1 /* greater than */ |
sPymbed | 0:1387ff3eed4a | 162 | |
sPymbed | 0:1387ff3eed4a | 163 | #define MP_ZPOS 0 /* positive integer */ |
sPymbed | 0:1387ff3eed4a | 164 | #define MP_NEG 1 /* negative */ |
sPymbed | 0:1387ff3eed4a | 165 | |
sPymbed | 0:1387ff3eed4a | 166 | #define MP_OKAY 0 /* ok result */ |
sPymbed | 0:1387ff3eed4a | 167 | #define MP_MEM -2 /* out of mem */ |
sPymbed | 0:1387ff3eed4a | 168 | #define MP_VAL -3 /* invalid input */ |
sPymbed | 0:1387ff3eed4a | 169 | #define MP_NOT_INF -4 /* point not at infinity */ |
sPymbed | 0:1387ff3eed4a | 170 | #define MP_RANGE MP_NOT_INF |
sPymbed | 0:1387ff3eed4a | 171 | |
sPymbed | 0:1387ff3eed4a | 172 | #define MP_YES 1 /* yes response */ |
sPymbed | 0:1387ff3eed4a | 173 | #define MP_NO 0 /* no response */ |
sPymbed | 0:1387ff3eed4a | 174 | |
sPymbed | 0:1387ff3eed4a | 175 | /* Primality generation flags */ |
sPymbed | 0:1387ff3eed4a | 176 | #define LTM_PRIME_BBS 0x0001 /* BBS style prime */ |
sPymbed | 0:1387ff3eed4a | 177 | #define LTM_PRIME_SAFE 0x0002 /* Safe prime (p-1)/2 == prime */ |
sPymbed | 0:1387ff3eed4a | 178 | #define LTM_PRIME_2MSB_ON 0x0008 /* force 2nd MSB to 1 */ |
sPymbed | 0:1387ff3eed4a | 179 | |
sPymbed | 0:1387ff3eed4a | 180 | typedef int mp_err; |
sPymbed | 0:1387ff3eed4a | 181 | |
sPymbed | 0:1387ff3eed4a | 182 | /* define this to use lower memory usage routines (exptmods mostly) */ |
sPymbed | 0:1387ff3eed4a | 183 | #define MP_LOW_MEM |
sPymbed | 0:1387ff3eed4a | 184 | |
sPymbed | 0:1387ff3eed4a | 185 | /* default precision */ |
sPymbed | 0:1387ff3eed4a | 186 | #ifndef MP_PREC |
sPymbed | 0:1387ff3eed4a | 187 | #ifndef MP_LOW_MEM |
sPymbed | 0:1387ff3eed4a | 188 | #define MP_PREC 32 /* default digits of precision */ |
sPymbed | 0:1387ff3eed4a | 189 | #else |
sPymbed | 0:1387ff3eed4a | 190 | #define MP_PREC 1 /* default digits of precision */ |
sPymbed | 0:1387ff3eed4a | 191 | #endif |
sPymbed | 0:1387ff3eed4a | 192 | #endif |
sPymbed | 0:1387ff3eed4a | 193 | |
sPymbed | 0:1387ff3eed4a | 194 | /* size of comba arrays, should be at least 2 * 2**(BITS_PER_WORD - |
sPymbed | 0:1387ff3eed4a | 195 | BITS_PER_DIGIT*2) */ |
sPymbed | 0:1387ff3eed4a | 196 | #define MP_WARRAY (1 << (sizeof(mp_word) * CHAR_BIT - 2 * DIGIT_BIT + 1)) |
sPymbed | 0:1387ff3eed4a | 197 | |
sPymbed | 0:1387ff3eed4a | 198 | #ifdef HAVE_WOLF_BIGINT |
sPymbed | 0:1387ff3eed4a | 199 | struct WC_BIGINT; |
sPymbed | 0:1387ff3eed4a | 200 | #endif |
sPymbed | 0:1387ff3eed4a | 201 | |
sPymbed | 0:1387ff3eed4a | 202 | /* the mp_int structure */ |
sPymbed | 0:1387ff3eed4a | 203 | typedef struct mp_int { |
sPymbed | 0:1387ff3eed4a | 204 | int used, alloc, sign; |
sPymbed | 0:1387ff3eed4a | 205 | mp_digit *dp; |
sPymbed | 0:1387ff3eed4a | 206 | |
sPymbed | 0:1387ff3eed4a | 207 | #ifdef HAVE_WOLF_BIGINT |
sPymbed | 0:1387ff3eed4a | 208 | struct WC_BIGINT raw; /* unsigned binary (big endian) */ |
sPymbed | 0:1387ff3eed4a | 209 | #endif |
sPymbed | 0:1387ff3eed4a | 210 | } mp_int; |
sPymbed | 0:1387ff3eed4a | 211 | #define MP_INT_DEFINED |
sPymbed | 0:1387ff3eed4a | 212 | |
sPymbed | 0:1387ff3eed4a | 213 | /* callback for mp_prime_random, should fill dst with random bytes and return |
sPymbed | 0:1387ff3eed4a | 214 | how many read [up to len] */ |
sPymbed | 0:1387ff3eed4a | 215 | typedef int ltm_prime_callback(unsigned char *dst, int len, void *dat); |
sPymbed | 0:1387ff3eed4a | 216 | |
sPymbed | 0:1387ff3eed4a | 217 | |
sPymbed | 0:1387ff3eed4a | 218 | #define USED(m) ((m)->used) |
sPymbed | 0:1387ff3eed4a | 219 | #define DIGIT(m,k) ((m)->dp[(k)]) |
sPymbed | 0:1387ff3eed4a | 220 | #define SIGN(m) ((m)->sign) |
sPymbed | 0:1387ff3eed4a | 221 | |
sPymbed | 0:1387ff3eed4a | 222 | |
sPymbed | 0:1387ff3eed4a | 223 | /* ---> Basic Manipulations <--- */ |
sPymbed | 0:1387ff3eed4a | 224 | #define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO) |
sPymbed | 0:1387ff3eed4a | 225 | #define mp_isone(a) \ |
sPymbed | 0:1387ff3eed4a | 226 | (((((a)->used == 1)) && ((a)->dp[0] == 1u)) ? MP_YES : MP_NO) |
sPymbed | 0:1387ff3eed4a | 227 | #define mp_iseven(a) \ |
sPymbed | 0:1387ff3eed4a | 228 | (((a)->used > 0 && (((a)->dp[0] & 1u) == 0u)) ? MP_YES : MP_NO) |
sPymbed | 0:1387ff3eed4a | 229 | #define mp_isodd(a) \ |
sPymbed | 0:1387ff3eed4a | 230 | (((a)->used > 0 && (((a)->dp[0] & 1u) == 1u)) ? MP_YES : MP_NO) |
sPymbed | 0:1387ff3eed4a | 231 | #define mp_isneg(a) (((a)->sign != MP_ZPOS) ? MP_YES : MP_NO) |
sPymbed | 0:1387ff3eed4a | 232 | |
sPymbed | 0:1387ff3eed4a | 233 | /* number of primes */ |
sPymbed | 0:1387ff3eed4a | 234 | #ifdef MP_8BIT |
sPymbed | 0:1387ff3eed4a | 235 | #define PRIME_SIZE 31 |
sPymbed | 0:1387ff3eed4a | 236 | #else |
sPymbed | 0:1387ff3eed4a | 237 | #define PRIME_SIZE 256 |
sPymbed | 0:1387ff3eed4a | 238 | #endif |
sPymbed | 0:1387ff3eed4a | 239 | |
sPymbed | 0:1387ff3eed4a | 240 | #ifndef MAX_INVMOD_SZ |
sPymbed | 0:1387ff3eed4a | 241 | #if defined(WOLFSSL_MYSQL_COMPATIBLE) |
sPymbed | 0:1387ff3eed4a | 242 | #define MAX_INVMOD_SZ 8192 |
sPymbed | 0:1387ff3eed4a | 243 | #else |
sPymbed | 0:1387ff3eed4a | 244 | #define MAX_INVMOD_SZ 4096 |
sPymbed | 0:1387ff3eed4a | 245 | #endif |
sPymbed | 0:1387ff3eed4a | 246 | #endif |
sPymbed | 0:1387ff3eed4a | 247 | |
sPymbed | 0:1387ff3eed4a | 248 | #define mp_prime_random(a, t, size, bbs, cb, dat) \ |
sPymbed | 0:1387ff3eed4a | 249 | mp_prime_random_ex(a, t, ((size) * 8) + 1, (bbs==1)?LTM_PRIME_BBS:0, cb, dat) |
sPymbed | 0:1387ff3eed4a | 250 | |
sPymbed | 0:1387ff3eed4a | 251 | #define mp_read_raw(mp, str, len) mp_read_signed_bin((mp), (str), (len)) |
sPymbed | 0:1387ff3eed4a | 252 | #define mp_raw_size(mp) mp_signed_bin_size(mp) |
sPymbed | 0:1387ff3eed4a | 253 | #define mp_toraw(mp, str) mp_to_signed_bin((mp), (str)) |
sPymbed | 0:1387ff3eed4a | 254 | #define mp_read_mag(mp, str, len) mp_read_unsigned_bin((mp), (str), (len)) |
sPymbed | 0:1387ff3eed4a | 255 | #define mp_mag_size(mp) mp_unsigned_bin_size(mp) |
sPymbed | 0:1387ff3eed4a | 256 | #define mp_tomag(mp, str) mp_to_unsigned_bin((mp), (str)) |
sPymbed | 0:1387ff3eed4a | 257 | |
sPymbed | 0:1387ff3eed4a | 258 | #define MP_RADIX_BIN 2 |
sPymbed | 0:1387ff3eed4a | 259 | #define MP_RADIX_OCT 8 |
sPymbed | 0:1387ff3eed4a | 260 | #define MP_RADIX_DEC 10 |
sPymbed | 0:1387ff3eed4a | 261 | #define MP_RADIX_HEX 16 |
sPymbed | 0:1387ff3eed4a | 262 | #define MP_RADIX_MAX 64 |
sPymbed | 0:1387ff3eed4a | 263 | |
sPymbed | 0:1387ff3eed4a | 264 | #define mp_tobinary(M, S) mp_toradix((M), (S), MP_RADIX_BIN) |
sPymbed | 0:1387ff3eed4a | 265 | #define mp_tooctal(M, S) mp_toradix((M), (S), MP_RADIX_OCT) |
sPymbed | 0:1387ff3eed4a | 266 | #define mp_todecimal(M, S) mp_toradix((M), (S), MP_RADIX_DEC) |
sPymbed | 0:1387ff3eed4a | 267 | #define mp_tohex(M, S) mp_toradix((M), (S), MP_RADIX_HEX) |
sPymbed | 0:1387ff3eed4a | 268 | |
sPymbed | 0:1387ff3eed4a | 269 | #define s_mp_mul(a, b, c) s_mp_mul_digs(a, b, c, (a)->used + (b)->used + 1) |
sPymbed | 0:1387ff3eed4a | 270 | |
sPymbed | 0:1387ff3eed4a | 271 | #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY) || \ |
sPymbed | 0:1387ff3eed4a | 272 | defined(WOLFSSL_DEBUG_MATH) || defined(DEBUG_WOLFSSL) |
sPymbed | 0:1387ff3eed4a | 273 | extern const char *mp_s_rmap; |
sPymbed | 0:1387ff3eed4a | 274 | #endif |
sPymbed | 0:1387ff3eed4a | 275 | |
sPymbed | 0:1387ff3eed4a | 276 | /* 6 functions needed by Rsa */ |
sPymbed | 0:1387ff3eed4a | 277 | MP_API int mp_init (mp_int * a); |
sPymbed | 0:1387ff3eed4a | 278 | MP_API void mp_clear (mp_int * a); |
sPymbed | 0:1387ff3eed4a | 279 | MP_API void mp_free (mp_int * a); |
sPymbed | 0:1387ff3eed4a | 280 | MP_API void mp_forcezero(mp_int * a); |
sPymbed | 0:1387ff3eed4a | 281 | MP_API int mp_unsigned_bin_size(mp_int * a); |
sPymbed | 0:1387ff3eed4a | 282 | MP_API int mp_read_unsigned_bin (mp_int * a, const unsigned char *b, int c); |
sPymbed | 0:1387ff3eed4a | 283 | MP_API int mp_to_unsigned_bin_at_pos(int x, mp_int *t, unsigned char *b); |
sPymbed | 0:1387ff3eed4a | 284 | MP_API int mp_to_unsigned_bin (mp_int * a, unsigned char *b); |
sPymbed | 0:1387ff3eed4a | 285 | MP_API int mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y); |
sPymbed | 0:1387ff3eed4a | 286 | /* end functions needed by Rsa */ |
sPymbed | 0:1387ff3eed4a | 287 | |
sPymbed | 0:1387ff3eed4a | 288 | /* functions added to support above needed, removed TOOM and KARATSUBA */ |
sPymbed | 0:1387ff3eed4a | 289 | MP_API int mp_count_bits (mp_int * a); |
sPymbed | 0:1387ff3eed4a | 290 | MP_API int mp_leading_bit (mp_int * a); |
sPymbed | 0:1387ff3eed4a | 291 | MP_API int mp_init_copy (mp_int * a, mp_int * b); |
sPymbed | 0:1387ff3eed4a | 292 | MP_API int mp_copy (mp_int * a, mp_int * b); |
sPymbed | 0:1387ff3eed4a | 293 | MP_API int mp_grow (mp_int * a, int size); |
sPymbed | 0:1387ff3eed4a | 294 | MP_API int mp_div_2d (mp_int * a, int b, mp_int * c, mp_int * d); |
sPymbed | 0:1387ff3eed4a | 295 | MP_API void mp_zero (mp_int * a); |
sPymbed | 0:1387ff3eed4a | 296 | MP_API void mp_clamp (mp_int * a); |
sPymbed | 0:1387ff3eed4a | 297 | MP_API void mp_exch (mp_int * a, mp_int * b); |
sPymbed | 0:1387ff3eed4a | 298 | MP_API void mp_rshd (mp_int * a, int b); |
sPymbed | 0:1387ff3eed4a | 299 | MP_API void mp_rshb (mp_int * a, int b); |
sPymbed | 0:1387ff3eed4a | 300 | MP_API int mp_mod_2d (mp_int * a, int b, mp_int * c); |
sPymbed | 0:1387ff3eed4a | 301 | MP_API int mp_mul_2d (mp_int * a, int b, mp_int * c); |
sPymbed | 0:1387ff3eed4a | 302 | MP_API int mp_lshd (mp_int * a, int b); |
sPymbed | 0:1387ff3eed4a | 303 | MP_API int mp_abs (mp_int * a, mp_int * b); |
sPymbed | 0:1387ff3eed4a | 304 | MP_API int mp_invmod (mp_int * a, mp_int * b, mp_int * c); |
sPymbed | 0:1387ff3eed4a | 305 | int fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c); |
sPymbed | 0:1387ff3eed4a | 306 | MP_API int mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c); |
sPymbed | 0:1387ff3eed4a | 307 | MP_API int mp_cmp_mag (mp_int * a, mp_int * b); |
sPymbed | 0:1387ff3eed4a | 308 | MP_API int mp_cmp (mp_int * a, mp_int * b); |
sPymbed | 0:1387ff3eed4a | 309 | MP_API int mp_cmp_d(mp_int * a, mp_digit b); |
sPymbed | 0:1387ff3eed4a | 310 | MP_API int mp_set (mp_int * a, mp_digit b); |
sPymbed | 0:1387ff3eed4a | 311 | MP_API int mp_is_bit_set (mp_int * a, mp_digit b); |
sPymbed | 0:1387ff3eed4a | 312 | MP_API int mp_mod (mp_int * a, mp_int * b, mp_int * c); |
sPymbed | 0:1387ff3eed4a | 313 | MP_API int mp_div(mp_int * a, mp_int * b, mp_int * c, mp_int * d); |
sPymbed | 0:1387ff3eed4a | 314 | MP_API int mp_div_2(mp_int * a, mp_int * b); |
sPymbed | 0:1387ff3eed4a | 315 | MP_API int mp_add (mp_int * a, mp_int * b, mp_int * c); |
sPymbed | 0:1387ff3eed4a | 316 | int s_mp_add (mp_int * a, mp_int * b, mp_int * c); |
sPymbed | 0:1387ff3eed4a | 317 | int s_mp_sub (mp_int * a, mp_int * b, mp_int * c); |
sPymbed | 0:1387ff3eed4a | 318 | MP_API int mp_sub (mp_int * a, mp_int * b, mp_int * c); |
sPymbed | 0:1387ff3eed4a | 319 | MP_API int mp_reduce_is_2k_l(mp_int *a); |
sPymbed | 0:1387ff3eed4a | 320 | MP_API int mp_reduce_is_2k(mp_int *a); |
sPymbed | 0:1387ff3eed4a | 321 | MP_API int mp_dr_is_modulus(mp_int *a); |
sPymbed | 0:1387ff3eed4a | 322 | MP_API int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, |
sPymbed | 0:1387ff3eed4a | 323 | int); |
sPymbed | 0:1387ff3eed4a | 324 | MP_API int mp_montgomery_setup (mp_int * n, mp_digit * rho); |
sPymbed | 0:1387ff3eed4a | 325 | int fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho); |
sPymbed | 0:1387ff3eed4a | 326 | MP_API int mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho); |
sPymbed | 0:1387ff3eed4a | 327 | MP_API void mp_dr_setup(mp_int *a, mp_digit *d); |
sPymbed | 0:1387ff3eed4a | 328 | MP_API int mp_dr_reduce (mp_int * x, mp_int * n, mp_digit k); |
sPymbed | 0:1387ff3eed4a | 329 | MP_API int mp_reduce_2k(mp_int *a, mp_int *n, mp_digit d); |
sPymbed | 0:1387ff3eed4a | 330 | int fast_s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs); |
sPymbed | 0:1387ff3eed4a | 331 | int s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs); |
sPymbed | 0:1387ff3eed4a | 332 | MP_API int mp_reduce_2k_setup_l(mp_int *a, mp_int *d); |
sPymbed | 0:1387ff3eed4a | 333 | MP_API int mp_reduce_2k_l(mp_int *a, mp_int *n, mp_int *d); |
sPymbed | 0:1387ff3eed4a | 334 | MP_API int mp_reduce (mp_int * x, mp_int * m, mp_int * mu); |
sPymbed | 0:1387ff3eed4a | 335 | MP_API int mp_reduce_setup (mp_int * a, mp_int * b); |
sPymbed | 0:1387ff3eed4a | 336 | int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode); |
sPymbed | 0:1387ff3eed4a | 337 | MP_API int mp_montgomery_calc_normalization (mp_int * a, mp_int * b); |
sPymbed | 0:1387ff3eed4a | 338 | int s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs); |
sPymbed | 0:1387ff3eed4a | 339 | int s_mp_sqr (mp_int * a, mp_int * b); |
sPymbed | 0:1387ff3eed4a | 340 | int fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs); |
sPymbed | 0:1387ff3eed4a | 341 | int fast_s_mp_sqr (mp_int * a, mp_int * b); |
sPymbed | 0:1387ff3eed4a | 342 | MP_API int mp_init_size (mp_int * a, int size); |
sPymbed | 0:1387ff3eed4a | 343 | MP_API int mp_div_3 (mp_int * a, mp_int *c, mp_digit * d); |
sPymbed | 0:1387ff3eed4a | 344 | MP_API int mp_mul_2(mp_int * a, mp_int * b); |
sPymbed | 0:1387ff3eed4a | 345 | MP_API int mp_mul (mp_int * a, mp_int * b, mp_int * c); |
sPymbed | 0:1387ff3eed4a | 346 | MP_API int mp_sqr (mp_int * a, mp_int * b); |
sPymbed | 0:1387ff3eed4a | 347 | MP_API int mp_mulmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d); |
sPymbed | 0:1387ff3eed4a | 348 | MP_API int mp_submod (mp_int* a, mp_int* b, mp_int* c, mp_int* d); |
sPymbed | 0:1387ff3eed4a | 349 | MP_API int mp_addmod (mp_int* a, mp_int* b, mp_int* c, mp_int* d); |
sPymbed | 0:1387ff3eed4a | 350 | MP_API int mp_mul_d (mp_int * a, mp_digit b, mp_int * c); |
sPymbed | 0:1387ff3eed4a | 351 | MP_API int mp_2expt (mp_int * a, int b); |
sPymbed | 0:1387ff3eed4a | 352 | MP_API int mp_set_bit (mp_int * a, int b); |
sPymbed | 0:1387ff3eed4a | 353 | MP_API int mp_reduce_2k_setup(mp_int *a, mp_digit *d); |
sPymbed | 0:1387ff3eed4a | 354 | MP_API int mp_add_d (mp_int* a, mp_digit b, mp_int* c); |
sPymbed | 0:1387ff3eed4a | 355 | MP_API int mp_set_int (mp_int * a, unsigned long b); |
sPymbed | 0:1387ff3eed4a | 356 | MP_API int mp_sub_d (mp_int * a, mp_digit b, mp_int * c); |
sPymbed | 0:1387ff3eed4a | 357 | /* end support added functions */ |
sPymbed | 0:1387ff3eed4a | 358 | |
sPymbed | 0:1387ff3eed4a | 359 | /* added */ |
sPymbed | 0:1387ff3eed4a | 360 | MP_API int mp_init_multi(mp_int* a, mp_int* b, mp_int* c, mp_int* d, mp_int* e, |
sPymbed | 0:1387ff3eed4a | 361 | mp_int* f); |
sPymbed | 0:1387ff3eed4a | 362 | MP_API int mp_toradix (mp_int *a, char *str, int radix); |
sPymbed | 0:1387ff3eed4a | 363 | MP_API int mp_radix_size (mp_int * a, int radix, int *size); |
sPymbed | 0:1387ff3eed4a | 364 | |
sPymbed | 0:1387ff3eed4a | 365 | #ifdef WOLFSSL_DEBUG_MATH |
sPymbed | 0:1387ff3eed4a | 366 | MP_API void mp_dump(const char* desc, mp_int* a, byte verbose); |
sPymbed | 0:1387ff3eed4a | 367 | #else |
sPymbed | 0:1387ff3eed4a | 368 | #define mp_dump(desc, a, verbose) |
sPymbed | 0:1387ff3eed4a | 369 | #endif |
sPymbed | 0:1387ff3eed4a | 370 | |
sPymbed | 0:1387ff3eed4a | 371 | #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN) |
sPymbed | 0:1387ff3eed4a | 372 | MP_API int mp_sqrmod(mp_int* a, mp_int* b, mp_int* c); |
sPymbed | 0:1387ff3eed4a | 373 | #endif |
sPymbed | 0:1387ff3eed4a | 374 | #if !defined(NO_DSA) || defined(HAVE_ECC) |
sPymbed | 0:1387ff3eed4a | 375 | MP_API int mp_read_radix(mp_int* a, const char* str, int radix); |
sPymbed | 0:1387ff3eed4a | 376 | #endif |
sPymbed | 0:1387ff3eed4a | 377 | |
sPymbed | 0:1387ff3eed4a | 378 | #ifdef WOLFSSL_KEY_GEN |
sPymbed | 0:1387ff3eed4a | 379 | MP_API int mp_prime_is_prime (mp_int * a, int t, int *result); |
sPymbed | 0:1387ff3eed4a | 380 | MP_API int mp_gcd (mp_int * a, mp_int * b, mp_int * c); |
sPymbed | 0:1387ff3eed4a | 381 | MP_API int mp_lcm (mp_int * a, mp_int * b, mp_int * c); |
sPymbed | 0:1387ff3eed4a | 382 | MP_API int mp_rand_prime(mp_int* N, int len, WC_RNG* rng, void* heap); |
sPymbed | 0:1387ff3eed4a | 383 | #endif |
sPymbed | 0:1387ff3eed4a | 384 | |
sPymbed | 0:1387ff3eed4a | 385 | MP_API int mp_cnt_lsb(mp_int *a); |
sPymbed | 0:1387ff3eed4a | 386 | MP_API int mp_mod_d(mp_int* a, mp_digit b, mp_digit* c); |
sPymbed | 0:1387ff3eed4a | 387 | |
sPymbed | 0:1387ff3eed4a | 388 | |
sPymbed | 0:1387ff3eed4a | 389 | /* wolf big int and common functions */ |
sPymbed | 0:1387ff3eed4a | 390 | #include <wolfcrypt/wolfmath.h> |
sPymbed | 0:1387ff3eed4a | 391 | |
sPymbed | 0:1387ff3eed4a | 392 | |
sPymbed | 0:1387ff3eed4a | 393 | #ifdef __cplusplus |
sPymbed | 0:1387ff3eed4a | 394 | } |
sPymbed | 0:1387ff3eed4a | 395 | #endif |
sPymbed | 0:1387ff3eed4a | 396 | |
sPymbed | 0:1387ff3eed4a | 397 | |
sPymbed | 0:1387ff3eed4a | 398 | #endif /* USE_FAST_MATH */ |
sPymbed | 0:1387ff3eed4a | 399 | |
sPymbed | 0:1387ff3eed4a | 400 | #endif /* WOLF_CRYPT_INTEGER_H */ |
sPymbed | 0:1387ff3eed4a | 401 | |
sPymbed | 0:1387ff3eed4a | 402 |