wolfSSL SSL/TLS library, support up to TLS1.3

Dependents:   CyaSSL-Twitter-OAuth4Tw Example-client-tls-cert TwitterReader TweetTest ... more

Committer:
wolfSSL
Date:
Fri Jun 05 00:11:07 2020 +0000
Revision:
17:a5f916481144
Parent:
16:8e0d178b1d1e
wolfSSL 4.4.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wolfSSL 15:117db924cf7c 1 /* tfm.h
wolfSSL 15:117db924cf7c 2 *
wolfSSL 16:8e0d178b1d1e 3 * Copyright (C) 2006-2020 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 /*
wolfSSL 15:117db924cf7c 25 * Based on public domain TomsFastMath 0.10 by Tom St Denis, tomstdenis@iahu.ca,
wolfSSL 15:117db924cf7c 26 * http://math.libtomcrypt.com
wolfSSL 15:117db924cf7c 27 */
wolfSSL 15:117db924cf7c 28
wolfSSL 15:117db924cf7c 29
wolfSSL 15:117db924cf7c 30 /**
wolfSSL 15:117db924cf7c 31 * Edited by Moises Guimaraes (moises.guimaraes@phoebus.com.br)
wolfSSL 15:117db924cf7c 32 * to fit CyaSSL's needs.
wolfSSL 15:117db924cf7c 33 */
wolfSSL 15:117db924cf7c 34
wolfSSL 15:117db924cf7c 35 /*!
wolfSSL 15:117db924cf7c 36 \file wolfssl/wolfcrypt/tfm.h
wolfSSL 15:117db924cf7c 37 */
wolfSSL 15:117db924cf7c 38
wolfSSL 15:117db924cf7c 39 #ifndef WOLF_CRYPT_TFM_H
wolfSSL 15:117db924cf7c 40 #define WOLF_CRYPT_TFM_H
wolfSSL 15:117db924cf7c 41
wolfSSL 15:117db924cf7c 42 #include <wolfssl/wolfcrypt/types.h>
wolfSSL 15:117db924cf7c 43 #ifndef CHAR_BIT
wolfSSL 15:117db924cf7c 44 #include <limits.h>
wolfSSL 15:117db924cf7c 45 #endif
wolfSSL 15:117db924cf7c 46
wolfSSL 15:117db924cf7c 47 #include <wolfssl/wolfcrypt/random.h>
wolfSSL 15:117db924cf7c 48
wolfSSL 15:117db924cf7c 49 #ifdef __cplusplus
wolfSSL 15:117db924cf7c 50 extern "C" {
wolfSSL 15:117db924cf7c 51 #endif
wolfSSL 15:117db924cf7c 52
wolfSSL 15:117db924cf7c 53 #ifdef WOLFSSL_NO_ASM
wolfSSL 15:117db924cf7c 54 #undef TFM_NO_ASM
wolfSSL 15:117db924cf7c 55 #define TFM_NO_ASM
wolfSSL 15:117db924cf7c 56 #endif
wolfSSL 15:117db924cf7c 57
wolfSSL 16:8e0d178b1d1e 58 #ifdef NO_64BIT
wolfSSL 16:8e0d178b1d1e 59 #undef NO_TFM_64BIT
wolfSSL 16:8e0d178b1d1e 60 #define NO_TFM_64BIT
wolfSSL 16:8e0d178b1d1e 61 #endif
wolfSSL 16:8e0d178b1d1e 62
wolfSSL 16:8e0d178b1d1e 63 #ifndef NO_TFM_64BIT
wolfSSL 15:117db924cf7c 64 /* autodetect x86-64 and make sure we are using 64-bit digits with x86-64 asm */
wolfSSL 15:117db924cf7c 65 #if defined(__x86_64__)
wolfSSL 15:117db924cf7c 66 #if defined(TFM_X86) || defined(TFM_SSE2) || defined(TFM_ARM)
wolfSSL 15:117db924cf7c 67 #error x86-64 detected, x86-32/SSE2/ARM optimizations are not valid!
wolfSSL 15:117db924cf7c 68 #endif
wolfSSL 15:117db924cf7c 69 #if !defined(TFM_X86_64) && !defined(TFM_NO_ASM)
wolfSSL 15:117db924cf7c 70 #define TFM_X86_64
wolfSSL 15:117db924cf7c 71 #endif
wolfSSL 15:117db924cf7c 72 #endif
wolfSSL 15:117db924cf7c 73 #if defined(TFM_X86_64)
wolfSSL 15:117db924cf7c 74 #if !defined(FP_64BIT)
wolfSSL 15:117db924cf7c 75 #define FP_64BIT
wolfSSL 15:117db924cf7c 76 #endif
wolfSSL 15:117db924cf7c 77 #endif
wolfSSL 15:117db924cf7c 78 /* use 64-bit digit even if not using asm on x86_64 */
wolfSSL 15:117db924cf7c 79 #if defined(__x86_64__) && !defined(FP_64BIT)
wolfSSL 15:117db924cf7c 80 #define FP_64BIT
wolfSSL 15:117db924cf7c 81 #endif
wolfSSL 15:117db924cf7c 82 /* if intel compiler doesn't provide 128 bit type don't turn on 64bit */
wolfSSL 15:117db924cf7c 83 #if defined(FP_64BIT) && defined(__INTEL_COMPILER) && !defined(HAVE___UINT128_T)
wolfSSL 15:117db924cf7c 84 #undef FP_64BIT
wolfSSL 15:117db924cf7c 85 #undef TFM_X86_64
wolfSSL 15:117db924cf7c 86 #endif
wolfSSL 16:8e0d178b1d1e 87 #endif /* NO_TFM_64BIT */
wolfSSL 15:117db924cf7c 88
wolfSSL 15:117db924cf7c 89 /* try to detect x86-32 */
wolfSSL 15:117db924cf7c 90 #if defined(__i386__) && !defined(TFM_SSE2)
wolfSSL 15:117db924cf7c 91 #if defined(TFM_X86_64) || defined(TFM_ARM)
wolfSSL 15:117db924cf7c 92 #error x86-32 detected, x86-64/ARM optimizations are not valid!
wolfSSL 15:117db924cf7c 93 #endif
wolfSSL 15:117db924cf7c 94 #if !defined(TFM_X86) && !defined(TFM_NO_ASM)
wolfSSL 15:117db924cf7c 95 #define TFM_X86
wolfSSL 15:117db924cf7c 96 #endif
wolfSSL 15:117db924cf7c 97 #endif
wolfSSL 15:117db924cf7c 98
wolfSSL 15:117db924cf7c 99 /* make sure we're 32-bit for x86-32/sse/arm/ppc32 */
wolfSSL 15:117db924cf7c 100 #if (defined(TFM_X86) || defined(TFM_SSE2) || defined(TFM_ARM) || defined(TFM_PPC32)) && defined(FP_64BIT)
wolfSSL 15:117db924cf7c 101 #warning x86-32, SSE2 and ARM, PPC32 optimizations require 32-bit digits (undefining)
wolfSSL 15:117db924cf7c 102 #undef FP_64BIT
wolfSSL 15:117db924cf7c 103 #endif
wolfSSL 15:117db924cf7c 104
wolfSSL 15:117db924cf7c 105 /* multi asms? */
wolfSSL 15:117db924cf7c 106 #ifdef TFM_X86
wolfSSL 15:117db924cf7c 107 #define TFM_ASM
wolfSSL 15:117db924cf7c 108 #endif
wolfSSL 15:117db924cf7c 109 #ifdef TFM_X86_64
wolfSSL 15:117db924cf7c 110 #ifdef TFM_ASM
wolfSSL 15:117db924cf7c 111 #error TFM_ASM already defined!
wolfSSL 15:117db924cf7c 112 #endif
wolfSSL 15:117db924cf7c 113 #define TFM_ASM
wolfSSL 15:117db924cf7c 114 #endif
wolfSSL 15:117db924cf7c 115 #ifdef TFM_SSE2
wolfSSL 15:117db924cf7c 116 #ifdef TFM_ASM
wolfSSL 15:117db924cf7c 117 #error TFM_ASM already defined!
wolfSSL 15:117db924cf7c 118 #endif
wolfSSL 15:117db924cf7c 119 #define TFM_ASM
wolfSSL 15:117db924cf7c 120 #endif
wolfSSL 15:117db924cf7c 121 #ifdef TFM_ARM
wolfSSL 15:117db924cf7c 122 #ifdef TFM_ASM
wolfSSL 15:117db924cf7c 123 #error TFM_ASM already defined!
wolfSSL 15:117db924cf7c 124 #endif
wolfSSL 15:117db924cf7c 125 #define TFM_ASM
wolfSSL 15:117db924cf7c 126 #endif
wolfSSL 15:117db924cf7c 127 #ifdef TFM_PPC32
wolfSSL 15:117db924cf7c 128 #ifdef TFM_ASM
wolfSSL 15:117db924cf7c 129 #error TFM_ASM already defined!
wolfSSL 15:117db924cf7c 130 #endif
wolfSSL 15:117db924cf7c 131 #define TFM_ASM
wolfSSL 15:117db924cf7c 132 #endif
wolfSSL 15:117db924cf7c 133 #ifdef TFM_PPC64
wolfSSL 15:117db924cf7c 134 #ifdef TFM_ASM
wolfSSL 15:117db924cf7c 135 #error TFM_ASM already defined!
wolfSSL 15:117db924cf7c 136 #endif
wolfSSL 15:117db924cf7c 137 #define TFM_ASM
wolfSSL 15:117db924cf7c 138 #endif
wolfSSL 15:117db924cf7c 139 #ifdef TFM_AVR32
wolfSSL 15:117db924cf7c 140 #ifdef TFM_ASM
wolfSSL 15:117db924cf7c 141 #error TFM_ASM already defined!
wolfSSL 15:117db924cf7c 142 #endif
wolfSSL 15:117db924cf7c 143 #define TFM_ASM
wolfSSL 15:117db924cf7c 144 #endif
wolfSSL 15:117db924cf7c 145
wolfSSL 15:117db924cf7c 146 /* we want no asm? */
wolfSSL 15:117db924cf7c 147 #ifdef TFM_NO_ASM
wolfSSL 15:117db924cf7c 148 #undef TFM_X86
wolfSSL 15:117db924cf7c 149 #undef TFM_X86_64
wolfSSL 15:117db924cf7c 150 #undef TFM_SSE2
wolfSSL 15:117db924cf7c 151 #undef TFM_ARM
wolfSSL 15:117db924cf7c 152 #undef TFM_PPC32
wolfSSL 15:117db924cf7c 153 #undef TFM_PPC64
wolfSSL 15:117db924cf7c 154 #undef TFM_AVR32
wolfSSL 15:117db924cf7c 155 #undef TFM_ASM
wolfSSL 15:117db924cf7c 156 #endif
wolfSSL 15:117db924cf7c 157
wolfSSL 15:117db924cf7c 158 /* ECC helpers */
wolfSSL 15:117db924cf7c 159 #ifdef TFM_ECC192
wolfSSL 15:117db924cf7c 160 #ifdef FP_64BIT
wolfSSL 15:117db924cf7c 161 #define TFM_MUL3
wolfSSL 15:117db924cf7c 162 #define TFM_SQR3
wolfSSL 15:117db924cf7c 163 #else
wolfSSL 15:117db924cf7c 164 #define TFM_MUL6
wolfSSL 15:117db924cf7c 165 #define TFM_SQR6
wolfSSL 15:117db924cf7c 166 #endif
wolfSSL 15:117db924cf7c 167 #endif
wolfSSL 15:117db924cf7c 168
wolfSSL 15:117db924cf7c 169 #ifdef TFM_ECC224
wolfSSL 15:117db924cf7c 170 #ifdef FP_64BIT
wolfSSL 15:117db924cf7c 171 #define TFM_MUL4
wolfSSL 15:117db924cf7c 172 #define TFM_SQR4
wolfSSL 15:117db924cf7c 173 #else
wolfSSL 15:117db924cf7c 174 #define TFM_MUL7
wolfSSL 15:117db924cf7c 175 #define TFM_SQR7
wolfSSL 15:117db924cf7c 176 #endif
wolfSSL 15:117db924cf7c 177 #endif
wolfSSL 15:117db924cf7c 178
wolfSSL 15:117db924cf7c 179 #ifdef TFM_ECC256
wolfSSL 15:117db924cf7c 180 #ifdef FP_64BIT
wolfSSL 15:117db924cf7c 181 #define TFM_MUL4
wolfSSL 15:117db924cf7c 182 #define TFM_SQR4
wolfSSL 15:117db924cf7c 183 #else
wolfSSL 15:117db924cf7c 184 #define TFM_MUL8
wolfSSL 15:117db924cf7c 185 #define TFM_SQR8
wolfSSL 15:117db924cf7c 186 #endif
wolfSSL 15:117db924cf7c 187 #endif
wolfSSL 15:117db924cf7c 188
wolfSSL 15:117db924cf7c 189 #ifdef TFM_ECC384
wolfSSL 15:117db924cf7c 190 #ifdef FP_64BIT
wolfSSL 15:117db924cf7c 191 #define TFM_MUL6
wolfSSL 15:117db924cf7c 192 #define TFM_SQR6
wolfSSL 15:117db924cf7c 193 #else
wolfSSL 15:117db924cf7c 194 #define TFM_MUL12
wolfSSL 15:117db924cf7c 195 #define TFM_SQR12
wolfSSL 15:117db924cf7c 196 #endif
wolfSSL 15:117db924cf7c 197 #endif
wolfSSL 15:117db924cf7c 198
wolfSSL 15:117db924cf7c 199 #ifdef TFM_ECC521
wolfSSL 15:117db924cf7c 200 #ifdef FP_64BIT
wolfSSL 15:117db924cf7c 201 #define TFM_MUL9
wolfSSL 15:117db924cf7c 202 #define TFM_SQR9
wolfSSL 15:117db924cf7c 203 #else
wolfSSL 15:117db924cf7c 204 #define TFM_MUL17
wolfSSL 15:117db924cf7c 205 #define TFM_SQR17
wolfSSL 15:117db924cf7c 206 #endif
wolfSSL 15:117db924cf7c 207 #endif
wolfSSL 15:117db924cf7c 208
wolfSSL 15:117db924cf7c 209
wolfSSL 15:117db924cf7c 210 /* allow user to define on fp_digit, fp_word types */
wolfSSL 15:117db924cf7c 211 #ifndef WOLFSSL_BIGINT_TYPES
wolfSSL 15:117db924cf7c 212
wolfSSL 15:117db924cf7c 213 /* some default configurations.
wolfSSL 15:117db924cf7c 214 */
wolfSSL 16:8e0d178b1d1e 215 #if defined(WC_16BIT_CPU)
wolfSSL 16:8e0d178b1d1e 216 typedef unsigned int fp_digit;
wolfSSL 16:8e0d178b1d1e 217 #define SIZEOF_FP_DIGIT 2
wolfSSL 16:8e0d178b1d1e 218 typedef unsigned long fp_word;
wolfSSL 16:8e0d178b1d1e 219 #elif defined(FP_64BIT)
wolfSSL 15:117db924cf7c 220 /* for GCC only on supported platforms */
wolfSSL 15:117db924cf7c 221 typedef unsigned long long fp_digit; /* 64bit, 128 uses mode(TI) below */
wolfSSL 15:117db924cf7c 222 #define SIZEOF_FP_DIGIT 8
wolfSSL 15:117db924cf7c 223 typedef unsigned long fp_word __attribute__ ((mode(TI)));
wolfSSL 15:117db924cf7c 224 #else
wolfSSL 15:117db924cf7c 225
wolfSSL 16:8e0d178b1d1e 226 #ifndef NO_TFM_64BIT
wolfSSL 15:117db924cf7c 227 #if defined(_MSC_VER) || defined(__BORLANDC__)
wolfSSL 15:117db924cf7c 228 typedef unsigned __int64 ulong64;
wolfSSL 15:117db924cf7c 229 #else
wolfSSL 15:117db924cf7c 230 typedef unsigned long long ulong64;
wolfSSL 15:117db924cf7c 231 #endif
wolfSSL 15:117db924cf7c 232 typedef unsigned int fp_digit;
wolfSSL 15:117db924cf7c 233 #define SIZEOF_FP_DIGIT 4
wolfSSL 15:117db924cf7c 234 typedef ulong64 fp_word;
wolfSSL 15:117db924cf7c 235 #define FP_32BIT
wolfSSL 15:117db924cf7c 236 #else
wolfSSL 15:117db924cf7c 237 /* some procs like coldfire prefer not to place multiply into 64bit type
wolfSSL 15:117db924cf7c 238 even though it exists */
wolfSSL 15:117db924cf7c 239 typedef unsigned short fp_digit;
wolfSSL 15:117db924cf7c 240 #define SIZEOF_FP_DIGIT 2
wolfSSL 15:117db924cf7c 241 typedef unsigned int fp_word;
wolfSSL 15:117db924cf7c 242 #endif
wolfSSL 15:117db924cf7c 243 #endif
wolfSSL 15:117db924cf7c 244
wolfSSL 15:117db924cf7c 245 #endif /* WOLFSSL_BIGINT_TYPES */
wolfSSL 15:117db924cf7c 246
wolfSSL 16:8e0d178b1d1e 247
wolfSSL 15:117db924cf7c 248 /* # of digits this is */
wolfSSL 15:117db924cf7c 249 #define DIGIT_BIT ((CHAR_BIT) * SIZEOF_FP_DIGIT)
wolfSSL 15:117db924cf7c 250
wolfSSL 15:117db924cf7c 251 /* Max size of any number in bits. Basically the largest size you will be
wolfSSL 15:117db924cf7c 252 * multiplying should be half [or smaller] of FP_MAX_SIZE-four_digit
wolfSSL 15:117db924cf7c 253 *
wolfSSL 15:117db924cf7c 254 * It defaults to 4096-bits [allowing multiplications up to 2048x2048 bits ]
wolfSSL 15:117db924cf7c 255 */
wolfSSL 15:117db924cf7c 256
wolfSSL 15:117db924cf7c 257
wolfSSL 15:117db924cf7c 258 #ifndef FP_MAX_BITS
wolfSSL 15:117db924cf7c 259 #define FP_MAX_BITS 4096
wolfSSL 15:117db924cf7c 260 #endif
wolfSSL 16:8e0d178b1d1e 261 #ifdef WOLFSSL_OPENSSH
wolfSSL 16:8e0d178b1d1e 262 /* OpenSSH uses some BIG primes so we need to accommodate for that */
wolfSSL 16:8e0d178b1d1e 263 #undef FP_MAX_BITS
wolfSSL 16:8e0d178b1d1e 264 #define FP_MAX_BITS 16384
wolfSSL 16:8e0d178b1d1e 265 #endif
wolfSSL 15:117db924cf7c 266 #define FP_MAX_SIZE (FP_MAX_BITS+(8*DIGIT_BIT))
wolfSSL 15:117db924cf7c 267
wolfSSL 15:117db924cf7c 268 /* will this lib work? */
wolfSSL 15:117db924cf7c 269 #if (CHAR_BIT & 7)
wolfSSL 15:117db924cf7c 270 #error CHAR_BIT must be a multiple of eight.
wolfSSL 15:117db924cf7c 271 #endif
wolfSSL 15:117db924cf7c 272 #if FP_MAX_BITS % CHAR_BIT
wolfSSL 15:117db924cf7c 273 #error FP_MAX_BITS must be a multiple of CHAR_BIT
wolfSSL 15:117db924cf7c 274 #endif
wolfSSL 15:117db924cf7c 275
wolfSSL 15:117db924cf7c 276 #define FP_MASK (fp_digit)(-1)
wolfSSL 15:117db924cf7c 277 #define FP_DIGIT_MAX FP_MASK
wolfSSL 15:117db924cf7c 278 #define FP_SIZE (FP_MAX_SIZE/DIGIT_BIT)
wolfSSL 15:117db924cf7c 279
wolfSSL 16:8e0d178b1d1e 280 #define FP_MAX_PRIME_SIZE (FP_MAX_BITS/(2*CHAR_BIT))
wolfSSL 16:8e0d178b1d1e 281 /* In terms of FP_MAX_BITS, it is double the size possible for a number
wolfSSL 16:8e0d178b1d1e 282 * to allow for multiplication, divide that 2 out. Also divide by CHAR_BIT
wolfSSL 16:8e0d178b1d1e 283 * to convert from bits to bytes. (Note, FP_PRIME_SIZE is the number of
wolfSSL 16:8e0d178b1d1e 284 * values in the canned prime number list.) */
wolfSSL 16:8e0d178b1d1e 285
wolfSSL 15:117db924cf7c 286 /* signs */
wolfSSL 15:117db924cf7c 287 #define FP_ZPOS 0
wolfSSL 15:117db924cf7c 288 #define FP_NEG 1
wolfSSL 15:117db924cf7c 289
wolfSSL 15:117db924cf7c 290 /* return codes */
wolfSSL 15:117db924cf7c 291 #define FP_OKAY 0
wolfSSL 15:117db924cf7c 292 #define FP_VAL -1
wolfSSL 15:117db924cf7c 293 #define FP_MEM -2
wolfSSL 15:117db924cf7c 294 #define FP_NOT_INF -3
wolfSSL 16:8e0d178b1d1e 295 #define FP_WOULDBLOCK -4
wolfSSL 15:117db924cf7c 296
wolfSSL 15:117db924cf7c 297 /* equalities */
wolfSSL 15:117db924cf7c 298 #define FP_LT -1 /* less than */
wolfSSL 15:117db924cf7c 299 #define FP_EQ 0 /* equal to */
wolfSSL 15:117db924cf7c 300 #define FP_GT 1 /* greater than */
wolfSSL 15:117db924cf7c 301
wolfSSL 15:117db924cf7c 302 /* replies */
wolfSSL 15:117db924cf7c 303 #define FP_YES 1 /* yes response */
wolfSSL 15:117db924cf7c 304 #define FP_NO 0 /* no response */
wolfSSL 15:117db924cf7c 305
wolfSSL 15:117db924cf7c 306 #ifdef HAVE_WOLF_BIGINT
wolfSSL 16:8e0d178b1d1e 307 /* raw big integer */
wolfSSL 16:8e0d178b1d1e 308 typedef struct WC_BIGINT {
wolfSSL 16:8e0d178b1d1e 309 byte* buf;
wolfSSL 16:8e0d178b1d1e 310 word32 len;
wolfSSL 16:8e0d178b1d1e 311 void* heap;
wolfSSL 16:8e0d178b1d1e 312 } WC_BIGINT;
wolfSSL 16:8e0d178b1d1e 313 #define WOLF_BIGINT_DEFINED
wolfSSL 15:117db924cf7c 314 #endif
wolfSSL 15:117db924cf7c 315
wolfSSL 15:117db924cf7c 316 /* a FP type */
wolfSSL 15:117db924cf7c 317 typedef struct fp_int {
wolfSSL 15:117db924cf7c 318 int used;
wolfSSL 15:117db924cf7c 319 int sign;
wolfSSL 15:117db924cf7c 320 #if defined(ALT_ECC_SIZE) || defined(HAVE_WOLF_BIGINT)
wolfSSL 15:117db924cf7c 321 int size;
wolfSSL 15:117db924cf7c 322 #endif
wolfSSL 15:117db924cf7c 323 fp_digit dp[FP_SIZE];
wolfSSL 15:117db924cf7c 324
wolfSSL 15:117db924cf7c 325 #ifdef HAVE_WOLF_BIGINT
wolfSSL 15:117db924cf7c 326 struct WC_BIGINT raw; /* unsigned binary (big endian) */
wolfSSL 15:117db924cf7c 327 #endif
wolfSSL 15:117db924cf7c 328 } fp_int;
wolfSSL 15:117db924cf7c 329
wolfSSL 16:8e0d178b1d1e 330 /* Types */
wolfSSL 16:8e0d178b1d1e 331 typedef fp_digit mp_digit;
wolfSSL 16:8e0d178b1d1e 332 typedef fp_word mp_word;
wolfSSL 16:8e0d178b1d1e 333 typedef fp_int mp_int;
wolfSSL 16:8e0d178b1d1e 334
wolfSSL 16:8e0d178b1d1e 335
wolfSSL 16:8e0d178b1d1e 336 /* wolf big int and common functions */
wolfSSL 16:8e0d178b1d1e 337 #include <wolfssl/wolfcrypt/wolfmath.h>
wolfSSL 16:8e0d178b1d1e 338
wolfSSL 16:8e0d178b1d1e 339
wolfSSL 15:117db924cf7c 340 /* externally define this symbol to ignore the default settings, useful for changing the build from the make process */
wolfSSL 15:117db924cf7c 341 #ifndef TFM_ALREADY_SET
wolfSSL 15:117db924cf7c 342
wolfSSL 15:117db924cf7c 343 /* do we want the large set of small multiplications ?
wolfSSL 15:117db924cf7c 344 Enable these if you are going to be doing a lot of small (<= 16 digit) multiplications say in ECC
wolfSSL 15:117db924cf7c 345 Or if you're on a 64-bit machine doing RSA as a 1024-bit integer == 16 digits ;-)
wolfSSL 15:117db924cf7c 346 */
wolfSSL 15:117db924cf7c 347 /* need to refactor the function */
wolfSSL 15:117db924cf7c 348 /*#define TFM_SMALL_SET */
wolfSSL 15:117db924cf7c 349
wolfSSL 15:117db924cf7c 350 /* do we want huge code
wolfSSL 15:117db924cf7c 351 Enable these if you are doing 20, 24, 28, 32, 48, 64 digit multiplications (useful for RSA)
wolfSSL 15:117db924cf7c 352 Less important on 64-bit machines as 32 digits == 2048 bits
wolfSSL 15:117db924cf7c 353 */
wolfSSL 15:117db924cf7c 354 #if 0
wolfSSL 15:117db924cf7c 355 #define TFM_MUL3
wolfSSL 15:117db924cf7c 356 #define TFM_MUL4
wolfSSL 15:117db924cf7c 357 #define TFM_MUL6
wolfSSL 15:117db924cf7c 358 #define TFM_MUL7
wolfSSL 15:117db924cf7c 359 #define TFM_MUL8
wolfSSL 15:117db924cf7c 360 #define TFM_MUL9
wolfSSL 15:117db924cf7c 361 #define TFM_MUL12
wolfSSL 15:117db924cf7c 362 #define TFM_MUL17
wolfSSL 15:117db924cf7c 363 #endif
wolfSSL 15:117db924cf7c 364 #ifdef TFM_HUGE_SET
wolfSSL 15:117db924cf7c 365 #define TFM_MUL20
wolfSSL 15:117db924cf7c 366 #define TFM_MUL24
wolfSSL 15:117db924cf7c 367 #define TFM_MUL28
wolfSSL 15:117db924cf7c 368 #define TFM_MUL32
wolfSSL 15:117db924cf7c 369 #if (FP_MAX_BITS >= 6144) && defined(FP_64BIT)
wolfSSL 15:117db924cf7c 370 #define TFM_MUL48
wolfSSL 15:117db924cf7c 371 #endif
wolfSSL 15:117db924cf7c 372 #if (FP_MAX_BITS >= 8192) && defined(FP_64BIT)
wolfSSL 15:117db924cf7c 373 #define TFM_MUL64
wolfSSL 15:117db924cf7c 374 #endif
wolfSSL 15:117db924cf7c 375 #endif
wolfSSL 15:117db924cf7c 376
wolfSSL 15:117db924cf7c 377 #if 0
wolfSSL 15:117db924cf7c 378 #define TFM_SQR3
wolfSSL 15:117db924cf7c 379 #define TFM_SQR4
wolfSSL 15:117db924cf7c 380 #define TFM_SQR6
wolfSSL 15:117db924cf7c 381 #define TFM_SQR7
wolfSSL 15:117db924cf7c 382 #define TFM_SQR8
wolfSSL 15:117db924cf7c 383 #define TFM_SQR9
wolfSSL 15:117db924cf7c 384 #define TFM_SQR12
wolfSSL 15:117db924cf7c 385 #define TFM_SQR17
wolfSSL 15:117db924cf7c 386 #endif
wolfSSL 15:117db924cf7c 387 #ifdef TFM_HUGE_SET
wolfSSL 15:117db924cf7c 388 #define TFM_SQR20
wolfSSL 15:117db924cf7c 389 #define TFM_SQR24
wolfSSL 15:117db924cf7c 390 #define TFM_SQR28
wolfSSL 15:117db924cf7c 391 #define TFM_SQR32
wolfSSL 15:117db924cf7c 392 #define TFM_SQR48
wolfSSL 15:117db924cf7c 393 #define TFM_SQR64
wolfSSL 15:117db924cf7c 394 #endif
wolfSSL 15:117db924cf7c 395
wolfSSL 15:117db924cf7c 396 /* Optional math checks (enable WOLFSSL_DEBUG_MATH to print info) */
wolfSSL 15:117db924cf7c 397 /* #define TFM_CHECK */
wolfSSL 15:117db924cf7c 398
wolfSSL 15:117db924cf7c 399 /* Is the target a P4 Prescott
wolfSSL 15:117db924cf7c 400 */
wolfSSL 15:117db924cf7c 401 /* #define TFM_PRESCOTT */
wolfSSL 15:117db924cf7c 402
wolfSSL 15:117db924cf7c 403 /* Do we want timing resistant fp_exptmod() ?
wolfSSL 15:117db924cf7c 404 * This makes it slower but also timing invariant with respect to the exponent
wolfSSL 15:117db924cf7c 405 */
wolfSSL 15:117db924cf7c 406 /* #define TFM_TIMING_RESISTANT */
wolfSSL 15:117db924cf7c 407
wolfSSL 15:117db924cf7c 408 #endif /* TFM_ALREADY_SET */
wolfSSL 15:117db924cf7c 409
wolfSSL 15:117db924cf7c 410 /* functions */
wolfSSL 15:117db924cf7c 411
wolfSSL 15:117db924cf7c 412 /* returns a TFM ident string useful for debugging... */
wolfSSL 15:117db924cf7c 413 /*const char *fp_ident(void);*/
wolfSSL 15:117db924cf7c 414
wolfSSL 15:117db924cf7c 415 /* initialize [or zero] an fp int */
wolfSSL 15:117db924cf7c 416 void fp_init(fp_int *a);
wolfSSL 15:117db924cf7c 417 MP_API void fp_zero(fp_int *a);
wolfSSL 15:117db924cf7c 418 MP_API void fp_clear(fp_int *a); /* uses ForceZero to clear sensitive memory */
wolfSSL 15:117db924cf7c 419 MP_API void fp_forcezero (fp_int * a);
wolfSSL 15:117db924cf7c 420 MP_API void fp_free(fp_int* a);
wolfSSL 15:117db924cf7c 421
wolfSSL 16:8e0d178b1d1e 422 /* zero/one/even/odd/neg/word ? */
wolfSSL 15:117db924cf7c 423 #define fp_iszero(a) (((a)->used == 0) ? FP_YES : FP_NO)
wolfSSL 15:117db924cf7c 424 #define fp_isone(a) \
wolfSSL 15:117db924cf7c 425 ((((a)->used == 1) && ((a)->dp[0] == 1)) ? FP_YES : FP_NO)
wolfSSL 15:117db924cf7c 426 #define fp_iseven(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 0)) ? FP_YES : FP_NO)
wolfSSL 15:117db924cf7c 427 #define fp_isodd(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 1)) ? FP_YES : FP_NO)
wolfSSL 15:117db924cf7c 428 #define fp_isneg(a) (((a)->sign != 0) ? FP_YES : FP_NO)
wolfSSL 16:8e0d178b1d1e 429 #define fp_isword(a, w) \
wolfSSL 16:8e0d178b1d1e 430 ((((a)->used == 1) && ((a)->dp[0] == w)) || ((w == 0) && ((a)->used == 0)) \
wolfSSL 16:8e0d178b1d1e 431 ? FP_YES : FP_NO)
wolfSSL 15:117db924cf7c 432
wolfSSL 15:117db924cf7c 433 /* set to a small digit */
wolfSSL 15:117db924cf7c 434 void fp_set(fp_int *a, fp_digit b);
wolfSSL 15:117db924cf7c 435 void fp_set_int(fp_int *a, unsigned long b);
wolfSSL 15:117db924cf7c 436
wolfSSL 15:117db924cf7c 437 /* check if a bit is set */
wolfSSL 15:117db924cf7c 438 int fp_is_bit_set(fp_int *a, fp_digit b);
wolfSSL 15:117db924cf7c 439 /* set the b bit to 1 */
wolfSSL 15:117db924cf7c 440 int fp_set_bit (fp_int * a, fp_digit b);
wolfSSL 15:117db924cf7c 441
wolfSSL 15:117db924cf7c 442 /* copy from a to b */
wolfSSL 15:117db924cf7c 443 void fp_copy(fp_int *a, fp_int *b);
wolfSSL 15:117db924cf7c 444 void fp_init_copy(fp_int *a, fp_int *b);
wolfSSL 15:117db924cf7c 445
wolfSSL 15:117db924cf7c 446 /* clamp digits */
wolfSSL 15:117db924cf7c 447 #define fp_clamp(a) { while ((a)->used && (a)->dp[(a)->used-1] == 0) --((a)->used); (a)->sign = (a)->used ? (a)->sign : FP_ZPOS; }
wolfSSL 15:117db924cf7c 448 #define mp_clamp(a) fp_clamp(a)
wolfSSL 15:117db924cf7c 449 #define mp_grow(a,s) MP_OKAY
wolfSSL 15:117db924cf7c 450
wolfSSL 15:117db924cf7c 451 /* negate and absolute */
wolfSSL 15:117db924cf7c 452 #define fp_neg(a, b) { fp_copy(a, b); (b)->sign ^= 1; fp_clamp(b); }
wolfSSL 15:117db924cf7c 453 #define fp_abs(a, b) { fp_copy(a, b); (b)->sign = 0; }
wolfSSL 15:117db924cf7c 454
wolfSSL 15:117db924cf7c 455 /* right shift x digits */
wolfSSL 15:117db924cf7c 456 void fp_rshd(fp_int *a, int x);
wolfSSL 15:117db924cf7c 457
wolfSSL 15:117db924cf7c 458 /* right shift x bits */
wolfSSL 15:117db924cf7c 459 void fp_rshb(fp_int *a, int x);
wolfSSL 15:117db924cf7c 460
wolfSSL 15:117db924cf7c 461 /* left shift x digits */
wolfSSL 15:117db924cf7c 462 void fp_lshd(fp_int *a, int x);
wolfSSL 15:117db924cf7c 463
wolfSSL 15:117db924cf7c 464 /* signed comparison */
wolfSSL 15:117db924cf7c 465 int fp_cmp(fp_int *a, fp_int *b);
wolfSSL 15:117db924cf7c 466
wolfSSL 15:117db924cf7c 467 /* unsigned comparison */
wolfSSL 15:117db924cf7c 468 int fp_cmp_mag(fp_int *a, fp_int *b);
wolfSSL 15:117db924cf7c 469
wolfSSL 15:117db924cf7c 470 /* power of 2 operations */
wolfSSL 15:117db924cf7c 471 void fp_div_2d(fp_int *a, int b, fp_int *c, fp_int *d);
wolfSSL 15:117db924cf7c 472 void fp_mod_2d(fp_int *a, int b, fp_int *c);
wolfSSL 15:117db924cf7c 473 void fp_mul_2d(fp_int *a, int b, fp_int *c);
wolfSSL 15:117db924cf7c 474 void fp_2expt (fp_int *a, int b);
wolfSSL 15:117db924cf7c 475 void fp_mul_2(fp_int *a, fp_int *c);
wolfSSL 15:117db924cf7c 476 void fp_div_2(fp_int *a, fp_int *c);
wolfSSL 15:117db924cf7c 477
wolfSSL 15:117db924cf7c 478 /* Counts the number of lsbs which are zero before the first zero bit */
wolfSSL 15:117db924cf7c 479 int fp_cnt_lsb(fp_int *a);
wolfSSL 15:117db924cf7c 480
wolfSSL 15:117db924cf7c 481 /* c = a + b */
wolfSSL 15:117db924cf7c 482 void fp_add(fp_int *a, fp_int *b, fp_int *c);
wolfSSL 15:117db924cf7c 483
wolfSSL 15:117db924cf7c 484 /* c = a - b */
wolfSSL 15:117db924cf7c 485 void fp_sub(fp_int *a, fp_int *b, fp_int *c);
wolfSSL 15:117db924cf7c 486
wolfSSL 15:117db924cf7c 487 /* c = a * b */
wolfSSL 16:8e0d178b1d1e 488 int fp_mul(fp_int *a, fp_int *b, fp_int *c);
wolfSSL 15:117db924cf7c 489
wolfSSL 15:117db924cf7c 490 /* b = a*a */
wolfSSL 16:8e0d178b1d1e 491 int fp_sqr(fp_int *a, fp_int *b);
wolfSSL 15:117db924cf7c 492
wolfSSL 15:117db924cf7c 493 /* a/b => cb + d == a */
wolfSSL 15:117db924cf7c 494 int fp_div(fp_int *a, fp_int *b, fp_int *c, fp_int *d);
wolfSSL 15:117db924cf7c 495
wolfSSL 15:117db924cf7c 496 /* c = a mod b, 0 <= c < b */
wolfSSL 15:117db924cf7c 497 int fp_mod(fp_int *a, fp_int *b, fp_int *c);
wolfSSL 15:117db924cf7c 498
wolfSSL 15:117db924cf7c 499 /* compare against a single digit */
wolfSSL 15:117db924cf7c 500 int fp_cmp_d(fp_int *a, fp_digit b);
wolfSSL 15:117db924cf7c 501
wolfSSL 15:117db924cf7c 502 /* c = a + b */
wolfSSL 15:117db924cf7c 503 void fp_add_d(fp_int *a, fp_digit b, fp_int *c);
wolfSSL 15:117db924cf7c 504
wolfSSL 15:117db924cf7c 505 /* c = a - b */
wolfSSL 16:8e0d178b1d1e 506 int fp_sub_d(fp_int *a, fp_digit b, fp_int *c);
wolfSSL 15:117db924cf7c 507
wolfSSL 15:117db924cf7c 508 /* c = a * b */
wolfSSL 15:117db924cf7c 509 void fp_mul_d(fp_int *a, fp_digit b, fp_int *c);
wolfSSL 15:117db924cf7c 510
wolfSSL 15:117db924cf7c 511 /* a/b => cb + d == a */
wolfSSL 15:117db924cf7c 512 /*int fp_div_d(fp_int *a, fp_digit b, fp_int *c, fp_digit *d);*/
wolfSSL 15:117db924cf7c 513
wolfSSL 15:117db924cf7c 514 /* c = a mod b, 0 <= c < b */
wolfSSL 15:117db924cf7c 515 /*int fp_mod_d(fp_int *a, fp_digit b, fp_digit *c);*/
wolfSSL 15:117db924cf7c 516
wolfSSL 15:117db924cf7c 517 /* ---> number theory <--- */
wolfSSL 15:117db924cf7c 518 /* d = a + b (mod c) */
wolfSSL 15:117db924cf7c 519 /*int fp_addmod(fp_int *a, fp_int *b, fp_int *c, fp_int *d);*/
wolfSSL 15:117db924cf7c 520
wolfSSL 15:117db924cf7c 521 /* d = a - b (mod c) */
wolfSSL 15:117db924cf7c 522 /*int fp_submod(fp_int *a, fp_int *b, fp_int *c, fp_int *d);*/
wolfSSL 15:117db924cf7c 523
wolfSSL 15:117db924cf7c 524 /* d = a * b (mod c) */
wolfSSL 15:117db924cf7c 525 int fp_mulmod(fp_int *a, fp_int *b, fp_int *c, fp_int *d);
wolfSSL 15:117db924cf7c 526
wolfSSL 15:117db924cf7c 527 /* d = a - b (mod c) */
wolfSSL 15:117db924cf7c 528 int fp_submod(fp_int *a, fp_int *b, fp_int *c, fp_int *d);
wolfSSL 15:117db924cf7c 529
wolfSSL 15:117db924cf7c 530 /* d = a + b (mod c) */
wolfSSL 15:117db924cf7c 531 int fp_addmod(fp_int *a, fp_int *b, fp_int *c, fp_int *d);
wolfSSL 15:117db924cf7c 532
wolfSSL 15:117db924cf7c 533 /* c = a * a (mod b) */
wolfSSL 15:117db924cf7c 534 int fp_sqrmod(fp_int *a, fp_int *b, fp_int *c);
wolfSSL 15:117db924cf7c 535
wolfSSL 15:117db924cf7c 536 /* c = 1/a (mod b) */
wolfSSL 15:117db924cf7c 537 int fp_invmod(fp_int *a, fp_int *b, fp_int *c);
wolfSSL 16:8e0d178b1d1e 538 int fp_invmod_mont_ct(fp_int *a, fp_int *b, fp_int *c, fp_digit mp);
wolfSSL 15:117db924cf7c 539
wolfSSL 15:117db924cf7c 540 /* c = (a, b) */
wolfSSL 16:8e0d178b1d1e 541 /*int fp_gcd(fp_int *a, fp_int *b, fp_int *c);*/
wolfSSL 15:117db924cf7c 542
wolfSSL 15:117db924cf7c 543 /* c = [a, b] */
wolfSSL 16:8e0d178b1d1e 544 /*int fp_lcm(fp_int *a, fp_int *b, fp_int *c);*/
wolfSSL 15:117db924cf7c 545
wolfSSL 15:117db924cf7c 546 /* setups the montgomery reduction */
wolfSSL 15:117db924cf7c 547 int fp_montgomery_setup(fp_int *a, fp_digit *mp);
wolfSSL 15:117db924cf7c 548
wolfSSL 15:117db924cf7c 549 /* computes a = B**n mod b without division or multiplication useful for
wolfSSL 15:117db924cf7c 550 * normalizing numbers in a Montgomery system.
wolfSSL 15:117db924cf7c 551 */
wolfSSL 15:117db924cf7c 552 void fp_montgomery_calc_normalization(fp_int *a, fp_int *b);
wolfSSL 15:117db924cf7c 553
wolfSSL 15:117db924cf7c 554 /* computes x/R == x (mod N) via Montgomery Reduction */
wolfSSL 16:8e0d178b1d1e 555 int fp_montgomery_reduce(fp_int *a, fp_int *m, fp_digit mp);
wolfSSL 15:117db924cf7c 556
wolfSSL 15:117db924cf7c 557 /* d = a**b (mod c) */
wolfSSL 15:117db924cf7c 558 int fp_exptmod(fp_int *a, fp_int *b, fp_int *c, fp_int *d);
wolfSSL 16:8e0d178b1d1e 559 int fp_exptmod_ex(fp_int *a, fp_int *b, int minDigits, fp_int *c, fp_int *d);
wolfSSL 16:8e0d178b1d1e 560 int fp_exptmod_nct(fp_int *a, fp_int *b, fp_int *c, fp_int *d);
wolfSSL 16:8e0d178b1d1e 561
wolfSSL 16:8e0d178b1d1e 562 #ifdef WC_RSA_NONBLOCK
wolfSSL 16:8e0d178b1d1e 563
wolfSSL 16:8e0d178b1d1e 564 enum tfmExptModNbState {
wolfSSL 16:8e0d178b1d1e 565 TFM_EXPTMOD_NB_INIT = 0,
wolfSSL 16:8e0d178b1d1e 566 TFM_EXPTMOD_NB_MONT,
wolfSSL 16:8e0d178b1d1e 567 TFM_EXPTMOD_NB_MONT_RED,
wolfSSL 16:8e0d178b1d1e 568 TFM_EXPTMOD_NB_MONT_MUL,
wolfSSL 16:8e0d178b1d1e 569 TFM_EXPTMOD_NB_MONT_MOD,
wolfSSL 16:8e0d178b1d1e 570 TFM_EXPTMOD_NB_MONT_MODCHK,
wolfSSL 16:8e0d178b1d1e 571 TFM_EXPTMOD_NB_NEXT,
wolfSSL 16:8e0d178b1d1e 572 TFM_EXPTMOD_NB_MUL,
wolfSSL 16:8e0d178b1d1e 573 TFM_EXPTMOD_NB_MUL_RED,
wolfSSL 16:8e0d178b1d1e 574 TFM_EXPTMOD_NB_SQR,
wolfSSL 16:8e0d178b1d1e 575 TFM_EXPTMOD_NB_SQR_RED,
wolfSSL 16:8e0d178b1d1e 576 TFM_EXPTMOD_NB_RED,
wolfSSL 16:8e0d178b1d1e 577 TFM_EXPTMOD_NB_COUNT /* last item for total state count only */
wolfSSL 16:8e0d178b1d1e 578 };
wolfSSL 16:8e0d178b1d1e 579
wolfSSL 16:8e0d178b1d1e 580 typedef struct {
wolfSSL 16:8e0d178b1d1e 581 #ifndef WC_NO_CACHE_RESISTANT
wolfSSL 16:8e0d178b1d1e 582 fp_int R[3];
wolfSSL 16:8e0d178b1d1e 583 #else
wolfSSL 16:8e0d178b1d1e 584 fp_int R[2];
wolfSSL 16:8e0d178b1d1e 585 #endif
wolfSSL 16:8e0d178b1d1e 586 fp_digit buf;
wolfSSL 16:8e0d178b1d1e 587 fp_digit mp;
wolfSSL 16:8e0d178b1d1e 588 int bitcnt;
wolfSSL 16:8e0d178b1d1e 589 int digidx;
wolfSSL 16:8e0d178b1d1e 590 int y;
wolfSSL 16:8e0d178b1d1e 591 int state; /* tfmExptModNbState */
wolfSSL 16:8e0d178b1d1e 592 #ifdef WC_RSA_NONBLOCK_TIME
wolfSSL 16:8e0d178b1d1e 593 word32 maxBlockInst; /* maximum instructions to block */
wolfSSL 16:8e0d178b1d1e 594 word32 totalInst; /* tracks total instructions */
wolfSSL 16:8e0d178b1d1e 595 #endif
wolfSSL 16:8e0d178b1d1e 596 } exptModNb_t;
wolfSSL 16:8e0d178b1d1e 597
wolfSSL 16:8e0d178b1d1e 598 #ifdef WC_RSA_NONBLOCK_TIME
wolfSSL 16:8e0d178b1d1e 599 enum {
wolfSSL 16:8e0d178b1d1e 600 TFM_EXPTMOD_NB_STOP = 0, /* stop and return FP_WOULDBLOCK */
wolfSSL 16:8e0d178b1d1e 601 TFM_EXPTMOD_NB_CONTINUE = 1, /* keep blocking */
wolfSSL 16:8e0d178b1d1e 602 };
wolfSSL 16:8e0d178b1d1e 603 #endif
wolfSSL 16:8e0d178b1d1e 604
wolfSSL 16:8e0d178b1d1e 605 /* non-blocking version of timing resistant fp_exptmod function */
wolfSSL 16:8e0d178b1d1e 606 /* supports cache resistance */
wolfSSL 16:8e0d178b1d1e 607 int fp_exptmod_nb(exptModNb_t* nb, fp_int* G, fp_int* X, fp_int* P, fp_int* Y);
wolfSSL 16:8e0d178b1d1e 608
wolfSSL 16:8e0d178b1d1e 609 #endif /* WC_RSA_NONBLOCK */
wolfSSL 15:117db924cf7c 610
wolfSSL 15:117db924cf7c 611 /* primality stuff */
wolfSSL 15:117db924cf7c 612
wolfSSL 15:117db924cf7c 613 /* perform a Miller-Rabin test of a to the base b and store result in "result" */
wolfSSL 15:117db924cf7c 614 /*void fp_prime_miller_rabin (fp_int * a, fp_int * b, int *result);*/
wolfSSL 15:117db924cf7c 615
wolfSSL 15:117db924cf7c 616 #define FP_PRIME_SIZE 256
wolfSSL 15:117db924cf7c 617 /* 256 trial divisions + 8 Miller-Rabins, returns FP_YES if probable prime */
wolfSSL 15:117db924cf7c 618 /*int fp_isprime(fp_int *a);*/
wolfSSL 15:117db924cf7c 619 /* extended version of fp_isprime, do 't' Miller-Rabins instead of only 8 */
wolfSSL 16:8e0d178b1d1e 620 /*int fp_isprime_ex(fp_int *a, int t, int* result);*/
wolfSSL 15:117db924cf7c 621
wolfSSL 15:117db924cf7c 622 /* Primality generation flags */
wolfSSL 15:117db924cf7c 623 /*#define TFM_PRIME_BBS 0x0001 */ /* BBS style prime */
wolfSSL 15:117db924cf7c 624 /*#define TFM_PRIME_SAFE 0x0002 */ /* Safe prime (p-1)/2 == prime */
wolfSSL 15:117db924cf7c 625 /*#define TFM_PRIME_2MSB_OFF 0x0004 */ /* force 2nd MSB to 0 */
wolfSSL 15:117db924cf7c 626 /*#define TFM_PRIME_2MSB_ON 0x0008 */ /* force 2nd MSB to 1 */
wolfSSL 15:117db924cf7c 627
wolfSSL 15:117db924cf7c 628 /* callback for fp_prime_random, should fill dst with random bytes and return how many read [up to len] */
wolfSSL 15:117db924cf7c 629 /*typedef int tfm_prime_callback(unsigned char *dst, int len, void *dat);*/
wolfSSL 15:117db924cf7c 630
wolfSSL 15:117db924cf7c 631 /*#define fp_prime_random(a, t, size, bbs, cb, dat) fp_prime_random_ex(a, t, ((size) * 8) + 1, (bbs==1)?TFM_PRIME_BBS:0, cb, dat)*/
wolfSSL 15:117db924cf7c 632
wolfSSL 15:117db924cf7c 633 /*int fp_prime_random_ex(fp_int *a, int t, int size, int flags, tfm_prime_callback cb, void *dat);*/
wolfSSL 15:117db924cf7c 634
wolfSSL 15:117db924cf7c 635 /* radix conversions */
wolfSSL 15:117db924cf7c 636 int fp_count_bits(fp_int *a);
wolfSSL 15:117db924cf7c 637 int fp_leading_bit(fp_int *a);
wolfSSL 15:117db924cf7c 638
wolfSSL 15:117db924cf7c 639 int fp_unsigned_bin_size(fp_int *a);
wolfSSL 15:117db924cf7c 640 void fp_read_unsigned_bin(fp_int *a, const unsigned char *b, int c);
wolfSSL 16:8e0d178b1d1e 641 int fp_to_unsigned_bin(fp_int *a, unsigned char *b);
wolfSSL 16:8e0d178b1d1e 642 int fp_to_unsigned_bin_len(fp_int *a, unsigned char *b, int c);
wolfSSL 15:117db924cf7c 643 int fp_to_unsigned_bin_at_pos(int x, fp_int *t, unsigned char *b);
wolfSSL 15:117db924cf7c 644
wolfSSL 15:117db924cf7c 645 /*int fp_signed_bin_size(fp_int *a);*/
wolfSSL 15:117db924cf7c 646 /*void fp_read_signed_bin(fp_int *a, const unsigned char *b, int c);*/
wolfSSL 15:117db924cf7c 647 /*void fp_to_signed_bin(fp_int *a, unsigned char *b);*/
wolfSSL 15:117db924cf7c 648
wolfSSL 15:117db924cf7c 649 /*int fp_read_radix(fp_int *a, char *str, int radix);*/
wolfSSL 15:117db924cf7c 650 /*int fp_toradix(fp_int *a, char *str, int radix);*/
wolfSSL 15:117db924cf7c 651 /*int fp_toradix_n(fp_int * a, char *str, int radix, int maxlen);*/
wolfSSL 15:117db924cf7c 652
wolfSSL 15:117db924cf7c 653
wolfSSL 15:117db924cf7c 654 /* VARIOUS LOW LEVEL STUFFS */
wolfSSL 15:117db924cf7c 655 void s_fp_add(fp_int *a, fp_int *b, fp_int *c);
wolfSSL 15:117db924cf7c 656 void s_fp_sub(fp_int *a, fp_int *b, fp_int *c);
wolfSSL 15:117db924cf7c 657 void fp_reverse(unsigned char *s, int len);
wolfSSL 15:117db924cf7c 658
wolfSSL 16:8e0d178b1d1e 659 int fp_mul_comba(fp_int *a, fp_int *b, fp_int *c);
wolfSSL 15:117db924cf7c 660
wolfSSL 16:8e0d178b1d1e 661 int fp_mul_comba_small(fp_int *a, fp_int *b, fp_int *c);
wolfSSL 16:8e0d178b1d1e 662 int fp_mul_comba3(fp_int *a, fp_int *b, fp_int *c);
wolfSSL 16:8e0d178b1d1e 663 int fp_mul_comba4(fp_int *a, fp_int *b, fp_int *c);
wolfSSL 16:8e0d178b1d1e 664 int fp_mul_comba6(fp_int *a, fp_int *b, fp_int *c);
wolfSSL 16:8e0d178b1d1e 665 int fp_mul_comba7(fp_int *a, fp_int *b, fp_int *c);
wolfSSL 16:8e0d178b1d1e 666 int fp_mul_comba8(fp_int *a, fp_int *b, fp_int *c);
wolfSSL 16:8e0d178b1d1e 667 int fp_mul_comba9(fp_int *a, fp_int *b, fp_int *c);
wolfSSL 16:8e0d178b1d1e 668 int fp_mul_comba12(fp_int *a, fp_int *b, fp_int *c);
wolfSSL 16:8e0d178b1d1e 669 int fp_mul_comba17(fp_int *a, fp_int *b, fp_int *c);
wolfSSL 16:8e0d178b1d1e 670 int fp_mul_comba20(fp_int *a, fp_int *b, fp_int *c);
wolfSSL 16:8e0d178b1d1e 671 int fp_mul_comba24(fp_int *a, fp_int *b, fp_int *c);
wolfSSL 16:8e0d178b1d1e 672 int fp_mul_comba28(fp_int *a, fp_int *b, fp_int *c);
wolfSSL 16:8e0d178b1d1e 673 int fp_mul_comba32(fp_int *a, fp_int *b, fp_int *c);
wolfSSL 16:8e0d178b1d1e 674 int fp_mul_comba48(fp_int *a, fp_int *b, fp_int *c);
wolfSSL 16:8e0d178b1d1e 675 int fp_mul_comba64(fp_int *a, fp_int *b, fp_int *c);
wolfSSL 16:8e0d178b1d1e 676 int fp_sqr_comba(fp_int *a, fp_int *b);
wolfSSL 16:8e0d178b1d1e 677 int fp_sqr_comba_small(fp_int *a, fp_int *b);
wolfSSL 16:8e0d178b1d1e 678 int fp_sqr_comba3(fp_int *a, fp_int *b);
wolfSSL 16:8e0d178b1d1e 679 int fp_sqr_comba4(fp_int *a, fp_int *b);
wolfSSL 16:8e0d178b1d1e 680 int fp_sqr_comba6(fp_int *a, fp_int *b);
wolfSSL 16:8e0d178b1d1e 681 int fp_sqr_comba7(fp_int *a, fp_int *b);
wolfSSL 16:8e0d178b1d1e 682 int fp_sqr_comba8(fp_int *a, fp_int *b);
wolfSSL 16:8e0d178b1d1e 683 int fp_sqr_comba9(fp_int *a, fp_int *b);
wolfSSL 16:8e0d178b1d1e 684 int fp_sqr_comba12(fp_int *a, fp_int *b);
wolfSSL 16:8e0d178b1d1e 685 int fp_sqr_comba17(fp_int *a, fp_int *b);
wolfSSL 16:8e0d178b1d1e 686 int fp_sqr_comba20(fp_int *a, fp_int *b);
wolfSSL 16:8e0d178b1d1e 687 int fp_sqr_comba24(fp_int *a, fp_int *b);
wolfSSL 16:8e0d178b1d1e 688 int fp_sqr_comba28(fp_int *a, fp_int *b);
wolfSSL 16:8e0d178b1d1e 689 int fp_sqr_comba32(fp_int *a, fp_int *b);
wolfSSL 16:8e0d178b1d1e 690 int fp_sqr_comba48(fp_int *a, fp_int *b);
wolfSSL 16:8e0d178b1d1e 691 int fp_sqr_comba64(fp_int *a, fp_int *b);
wolfSSL 15:117db924cf7c 692
wolfSSL 15:117db924cf7c 693
wolfSSL 15:117db924cf7c 694 /**
wolfSSL 15:117db924cf7c 695 * Used by wolfSSL
wolfSSL 15:117db924cf7c 696 */
wolfSSL 15:117db924cf7c 697
wolfSSL 15:117db924cf7c 698 /* Constants */
wolfSSL 15:117db924cf7c 699 #define MP_LT FP_LT /* less than */
wolfSSL 15:117db924cf7c 700 #define MP_EQ FP_EQ /* equal to */
wolfSSL 15:117db924cf7c 701 #define MP_GT FP_GT /* greater than */
wolfSSL 15:117db924cf7c 702 #define MP_VAL FP_VAL /* invalid */
wolfSSL 15:117db924cf7c 703 #define MP_MEM FP_MEM /* memory error */
wolfSSL 15:117db924cf7c 704 #define MP_NOT_INF FP_NOT_INF /* point not at infinity */
wolfSSL 15:117db924cf7c 705 #define MP_OKAY FP_OKAY /* ok result */
wolfSSL 15:117db924cf7c 706 #define MP_NO FP_NO /* yes/no result */
wolfSSL 15:117db924cf7c 707 #define MP_YES FP_YES /* yes/no result */
wolfSSL 15:117db924cf7c 708 #define MP_ZPOS FP_ZPOS
wolfSSL 15:117db924cf7c 709 #define MP_NEG FP_NEG
wolfSSL 15:117db924cf7c 710 #define MP_MASK FP_MASK
wolfSSL 15:117db924cf7c 711
wolfSSL 15:117db924cf7c 712 /* Prototypes */
wolfSSL 16:8e0d178b1d1e 713 #define mp_zero(a) fp_zero(a)
wolfSSL 16:8e0d178b1d1e 714 #define mp_isone(a) fp_isone(a)
wolfSSL 16:8e0d178b1d1e 715 #define mp_iseven(a) fp_iseven(a)
wolfSSL 16:8e0d178b1d1e 716 #define mp_isneg(a) fp_isneg(a)
wolfSSL 16:8e0d178b1d1e 717 #define mp_isword(a, w) fp_isword(a, w)
wolfSSL 15:117db924cf7c 718
wolfSSL 15:117db924cf7c 719 #define MP_RADIX_BIN 2
wolfSSL 15:117db924cf7c 720 #define MP_RADIX_OCT 8
wolfSSL 15:117db924cf7c 721 #define MP_RADIX_DEC 10
wolfSSL 15:117db924cf7c 722 #define MP_RADIX_HEX 16
wolfSSL 15:117db924cf7c 723 #define MP_RADIX_MAX 64
wolfSSL 15:117db924cf7c 724
wolfSSL 15:117db924cf7c 725 #define mp_tobinary(M, S) mp_toradix((M), (S), MP_RADIX_BIN)
wolfSSL 15:117db924cf7c 726 #define mp_tooctal(M, S) mp_toradix((M), (S), MP_RADIX_OCT)
wolfSSL 15:117db924cf7c 727 #define mp_todecimal(M, S) mp_toradix((M), (S), MP_RADIX_DEC)
wolfSSL 15:117db924cf7c 728 #define mp_tohex(M, S) mp_toradix((M), (S), MP_RADIX_HEX)
wolfSSL 15:117db924cf7c 729
wolfSSL 15:117db924cf7c 730 MP_API int mp_init (mp_int * a);
wolfSSL 15:117db924cf7c 731 MP_API void mp_clear (mp_int * a);
wolfSSL 15:117db924cf7c 732 MP_API void mp_free (mp_int * a);
wolfSSL 15:117db924cf7c 733 MP_API void mp_forcezero (mp_int * a);
wolfSSL 15:117db924cf7c 734 MP_API int mp_init_multi(mp_int* a, mp_int* b, mp_int* c, mp_int* d, mp_int* e,
wolfSSL 15:117db924cf7c 735 mp_int* f);
wolfSSL 15:117db924cf7c 736
wolfSSL 15:117db924cf7c 737 MP_API int mp_add (mp_int * a, mp_int * b, mp_int * c);
wolfSSL 15:117db924cf7c 738 MP_API int mp_sub (mp_int * a, mp_int * b, mp_int * c);
wolfSSL 15:117db924cf7c 739 MP_API int mp_add_d (mp_int * a, mp_digit b, mp_int * c);
wolfSSL 15:117db924cf7c 740
wolfSSL 15:117db924cf7c 741 MP_API int mp_mul (mp_int * a, mp_int * b, mp_int * c);
wolfSSL 15:117db924cf7c 742 MP_API int mp_mul_d (mp_int * a, mp_digit b, mp_int * c);
wolfSSL 15:117db924cf7c 743 MP_API int mp_mulmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d);
wolfSSL 15:117db924cf7c 744 MP_API int mp_submod (mp_int* a, mp_int* b, mp_int* c, mp_int* d);
wolfSSL 15:117db924cf7c 745 MP_API int mp_addmod (mp_int* a, mp_int* b, mp_int* c, mp_int* d);
wolfSSL 15:117db924cf7c 746 MP_API int mp_mod(mp_int *a, mp_int *b, mp_int *c);
wolfSSL 15:117db924cf7c 747 MP_API int mp_invmod(mp_int *a, mp_int *b, mp_int *c);
wolfSSL 16:8e0d178b1d1e 748 MP_API int mp_invmod_mont_ct(mp_int *a, mp_int *b, mp_int *c, fp_digit mp);
wolfSSL 15:117db924cf7c 749 MP_API int mp_exptmod (mp_int * g, mp_int * x, mp_int * p, mp_int * y);
wolfSSL 16:8e0d178b1d1e 750 MP_API int mp_exptmod_ex (mp_int * g, mp_int * x, int minDigits, mp_int * p,
wolfSSL 16:8e0d178b1d1e 751 mp_int * y);
wolfSSL 16:8e0d178b1d1e 752 MP_API int mp_exptmod_nct (mp_int * g, mp_int * x, mp_int * p, mp_int * y);
wolfSSL 15:117db924cf7c 753 MP_API int mp_mul_2d(mp_int *a, int b, mp_int *c);
wolfSSL 15:117db924cf7c 754 MP_API int mp_2expt(mp_int* a, int b);
wolfSSL 15:117db924cf7c 755
wolfSSL 15:117db924cf7c 756 MP_API int mp_div(mp_int * a, mp_int * b, mp_int * c, mp_int * d);
wolfSSL 15:117db924cf7c 757
wolfSSL 15:117db924cf7c 758 MP_API int mp_cmp(mp_int *a, mp_int *b);
wolfSSL 15:117db924cf7c 759 MP_API int mp_cmp_d(mp_int *a, mp_digit b);
wolfSSL 15:117db924cf7c 760
wolfSSL 15:117db924cf7c 761 MP_API int mp_unsigned_bin_size(mp_int * a);
wolfSSL 15:117db924cf7c 762 MP_API int mp_read_unsigned_bin (mp_int * a, const unsigned char *b, int c);
wolfSSL 15:117db924cf7c 763 MP_API int mp_to_unsigned_bin_at_pos(int x, mp_int *t, unsigned char *b);
wolfSSL 15:117db924cf7c 764 MP_API int mp_to_unsigned_bin (mp_int * a, unsigned char *b);
wolfSSL 16:8e0d178b1d1e 765 MP_API int mp_to_unsigned_bin_len(mp_int * a, unsigned char *b, int c);
wolfSSL 15:117db924cf7c 766
wolfSSL 15:117db924cf7c 767 MP_API int mp_sub_d(fp_int *a, fp_digit b, fp_int *c);
wolfSSL 15:117db924cf7c 768 MP_API int mp_copy(fp_int* a, fp_int* b);
wolfSSL 15:117db924cf7c 769 MP_API int mp_isodd(mp_int* a);
wolfSSL 15:117db924cf7c 770 MP_API int mp_iszero(mp_int* a);
wolfSSL 15:117db924cf7c 771 MP_API int mp_count_bits(mp_int *a);
wolfSSL 15:117db924cf7c 772 MP_API int mp_leading_bit(mp_int *a);
wolfSSL 15:117db924cf7c 773 MP_API int mp_set_int(mp_int *a, unsigned long b);
wolfSSL 15:117db924cf7c 774 MP_API int mp_is_bit_set (mp_int * a, mp_digit b);
wolfSSL 15:117db924cf7c 775 MP_API int mp_set_bit (mp_int * a, mp_digit b);
wolfSSL 15:117db924cf7c 776 MP_API void mp_rshb(mp_int *a, int x);
wolfSSL 15:117db924cf7c 777 MP_API void mp_rshd(mp_int *a, int x);
wolfSSL 15:117db924cf7c 778 MP_API int mp_toradix (mp_int *a, char *str, int radix);
wolfSSL 15:117db924cf7c 779 MP_API int mp_radix_size (mp_int * a, int radix, int *size);
wolfSSL 15:117db924cf7c 780
wolfSSL 15:117db924cf7c 781 #ifdef WOLFSSL_DEBUG_MATH
wolfSSL 15:117db924cf7c 782 MP_API void mp_dump(const char* desc, mp_int* a, byte verbose);
wolfSSL 15:117db924cf7c 783 #else
wolfSSL 15:117db924cf7c 784 #define mp_dump(desc, a, verbose)
wolfSSL 15:117db924cf7c 785 #endif
wolfSSL 15:117db924cf7c 786
wolfSSL 15:117db924cf7c 787 #if !defined(NO_DSA) || defined(HAVE_ECC)
wolfSSL 15:117db924cf7c 788 MP_API int mp_read_radix(mp_int* a, const char* str, int radix);
wolfSSL 15:117db924cf7c 789 #endif
wolfSSL 15:117db924cf7c 790
wolfSSL 15:117db924cf7c 791 #ifdef HAVE_ECC
wolfSSL 15:117db924cf7c 792 MP_API int mp_sqr(fp_int *a, fp_int *b);
wolfSSL 15:117db924cf7c 793 MP_API int mp_montgomery_reduce(fp_int *a, fp_int *m, fp_digit mp);
wolfSSL 15:117db924cf7c 794 MP_API int mp_montgomery_setup(fp_int *a, fp_digit *rho);
wolfSSL 15:117db924cf7c 795 MP_API int mp_div_2(fp_int * a, fp_int * b);
wolfSSL 15:117db924cf7c 796 MP_API int mp_init_copy(fp_int * a, fp_int * b);
wolfSSL 15:117db924cf7c 797 #endif
wolfSSL 15:117db924cf7c 798
wolfSSL 16:8e0d178b1d1e 799 #if defined(HAVE_ECC) || !defined(NO_RSA) || !defined(NO_DSA) || \
wolfSSL 16:8e0d178b1d1e 800 defined(WOLFSSL_KEY_GEN)
wolfSSL 15:117db924cf7c 801 MP_API int mp_set(fp_int *a, fp_digit b);
wolfSSL 15:117db924cf7c 802 #endif
wolfSSL 15:117db924cf7c 803
wolfSSL 16:8e0d178b1d1e 804 #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN) || !defined(NO_RSA) || \
wolfSSL 16:8e0d178b1d1e 805 !defined(NO_DSA) || !defined(NO_DH)
wolfSSL 15:117db924cf7c 806 MP_API int mp_sqrmod(mp_int* a, mp_int* b, mp_int* c);
wolfSSL 15:117db924cf7c 807 MP_API int mp_montgomery_calc_normalization(mp_int *a, mp_int *b);
wolfSSL 15:117db924cf7c 808 #endif
wolfSSL 15:117db924cf7c 809
wolfSSL 16:8e0d178b1d1e 810 #if !defined(NO_DH) || !defined(NO_DSA) || !defined(NO_RSA) || defined(WOLFSSL_KEY_GEN)
wolfSSL 16:8e0d178b1d1e 811 MP_API int mp_prime_is_prime(mp_int* a, int t, int* result);
wolfSSL 16:8e0d178b1d1e 812 MP_API int mp_prime_is_prime_ex(mp_int* a, int t, int* result, WC_RNG* rng);
wolfSSL 16:8e0d178b1d1e 813 #endif /* !NO_DH || !NO_DSA || !NO_RSA || WOLFSSL_KEY_GEN */
wolfSSL 15:117db924cf7c 814 #ifdef WOLFSSL_KEY_GEN
wolfSSL 15:117db924cf7c 815 MP_API int mp_gcd(fp_int *a, fp_int *b, fp_int *c);
wolfSSL 15:117db924cf7c 816 MP_API int mp_lcm(fp_int *a, fp_int *b, fp_int *c);
wolfSSL 15:117db924cf7c 817 MP_API int mp_rand_prime(mp_int* N, int len, WC_RNG* rng, void* heap);
wolfSSL 15:117db924cf7c 818 MP_API int mp_exch(mp_int *a, mp_int *b);
wolfSSL 15:117db924cf7c 819 #endif /* WOLFSSL_KEY_GEN */
wolfSSL 15:117db924cf7c 820
wolfSSL 15:117db924cf7c 821 MP_API int mp_cnt_lsb(fp_int *a);
wolfSSL 15:117db924cf7c 822 MP_API int mp_div_2d(fp_int *a, int b, fp_int *c, fp_int *d);
wolfSSL 15:117db924cf7c 823 MP_API int mp_mod_d(fp_int* a, fp_digit b, fp_digit* c);
wolfSSL 15:117db924cf7c 824 MP_API int mp_lshd (mp_int * a, int b);
wolfSSL 15:117db924cf7c 825 MP_API int mp_abs(mp_int* a, mp_int* b);
wolfSSL 15:117db924cf7c 826
wolfSSL 15:117db924cf7c 827 WOLFSSL_API word32 CheckRunTimeFastMath(void);
wolfSSL 15:117db924cf7c 828
wolfSSL 15:117db924cf7c 829 /* If user uses RSA, DH, DSA, or ECC math lib directly then fast math FP_SIZE
wolfSSL 15:117db924cf7c 830 must match, return 1 if a match otherwise 0 */
wolfSSL 15:117db924cf7c 831 #define CheckFastMathSettings() (FP_SIZE == CheckRunTimeFastMath())
wolfSSL 15:117db924cf7c 832
wolfSSL 15:117db924cf7c 833
wolfSSL 15:117db924cf7c 834 #ifdef __cplusplus
wolfSSL 15:117db924cf7c 835 }
wolfSSL 15:117db924cf7c 836 #endif
wolfSSL 15:117db924cf7c 837
wolfSSL 15:117db924cf7c 838 #endif /* WOLF_CRYPT_TFM_H */
wolfSSL 15:117db924cf7c 839
wolfSSL 15:117db924cf7c 840