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