Xuyi Wang / wolfSSL

Dependents:   OS

Committer:
sPymbed
Date:
Tue Nov 19 14:32:16 2019 +0000
Revision:
16:048e5e270a58
Parent:
15:117db924cf7c
working ssl

Who changed what in which revision?

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