CyaSSL 3.0.0

Dependents:   HTTPClient-SSL HTTPClient HTTPClient-SSL http_access ... more

Committer:
wolfSSL
Date:
Wed Dec 03 05:24:18 2014 +0000
Revision:
3:64d4f7cb83d5
Parent:
0:1239e9b70ca2
added IGNORE_KEY_EXTENSIONS

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wolfSSL 0:1239e9b70ca2 1 /* tfm.h
wolfSSL 0:1239e9b70ca2 2 *
wolfSSL 0:1239e9b70ca2 3 * Copyright (C) 2006-2014 wolfSSL Inc.
wolfSSL 0:1239e9b70ca2 4 *
wolfSSL 0:1239e9b70ca2 5 * This file is part of CyaSSL.
wolfSSL 0:1239e9b70ca2 6 *
wolfSSL 0:1239e9b70ca2 7 * CyaSSL is free software; you can redistribute it and/or modify
wolfSSL 0:1239e9b70ca2 8 * it under the terms of the GNU General Public License as published by
wolfSSL 0:1239e9b70ca2 9 * the Free Software Foundation; either version 2 of the License, or
wolfSSL 0:1239e9b70ca2 10 * (at your option) any later version.
wolfSSL 0:1239e9b70ca2 11 *
wolfSSL 0:1239e9b70ca2 12 * CyaSSL is distributed in the hope that it will be useful,
wolfSSL 0:1239e9b70ca2 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
wolfSSL 0:1239e9b70ca2 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wolfSSL 0:1239e9b70ca2 15 * GNU General Public License for more details.
wolfSSL 0:1239e9b70ca2 16 *
wolfSSL 0:1239e9b70ca2 17 * You should have received a copy of the GNU General Public License
wolfSSL 0:1239e9b70ca2 18 * along with this program; if not, write to the Free Software
wolfSSL 0:1239e9b70ca2 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
wolfSSL 0:1239e9b70ca2 20 */
wolfSSL 0:1239e9b70ca2 21
wolfSSL 0:1239e9b70ca2 22
wolfSSL 0:1239e9b70ca2 23 /*
wolfSSL 0:1239e9b70ca2 24 * Based on public domain TomsFastMath 0.10 by Tom St Denis, tomstdenis@iahu.ca,
wolfSSL 0:1239e9b70ca2 25 * http://math.libtomcrypt.com
wolfSSL 0:1239e9b70ca2 26 */
wolfSSL 0:1239e9b70ca2 27
wolfSSL 0:1239e9b70ca2 28
wolfSSL 0:1239e9b70ca2 29 /**
wolfSSL 0:1239e9b70ca2 30 * Edited by Moisés Guimarães (moises.guimaraes@phoebus.com.br)
wolfSSL 0:1239e9b70ca2 31 * to fit CyaSSL's needs.
wolfSSL 0:1239e9b70ca2 32 */
wolfSSL 0:1239e9b70ca2 33
wolfSSL 0:1239e9b70ca2 34
wolfSSL 0:1239e9b70ca2 35 #ifndef CTAO_CRYPT_TFM_H
wolfSSL 0:1239e9b70ca2 36 #define CTAO_CRYPT_TFM_H
wolfSSL 0:1239e9b70ca2 37
wolfSSL 0:1239e9b70ca2 38 #include <cyassl/ctaocrypt/types.h>
wolfSSL 0:1239e9b70ca2 39 #ifndef CHAR_BIT
wolfSSL 0:1239e9b70ca2 40 #include <limits.h>
wolfSSL 0:1239e9b70ca2 41 #endif
wolfSSL 0:1239e9b70ca2 42
wolfSSL 0:1239e9b70ca2 43
wolfSSL 0:1239e9b70ca2 44 #ifdef __cplusplus
wolfSSL 0:1239e9b70ca2 45 extern "C" {
wolfSSL 0:1239e9b70ca2 46 #endif
wolfSSL 0:1239e9b70ca2 47
wolfSSL 0:1239e9b70ca2 48 #ifndef MIN
wolfSSL 0:1239e9b70ca2 49 #define MIN(x,y) ((x)<(y)?(x):(y))
wolfSSL 0:1239e9b70ca2 50 #endif
wolfSSL 0:1239e9b70ca2 51
wolfSSL 0:1239e9b70ca2 52 #ifndef MAX
wolfSSL 0:1239e9b70ca2 53 #define MAX(x,y) ((x)>(y)?(x):(y))
wolfSSL 0:1239e9b70ca2 54 #endif
wolfSSL 0:1239e9b70ca2 55
wolfSSL 0:1239e9b70ca2 56
wolfSSL 0:1239e9b70ca2 57 #ifndef NO_64BIT
wolfSSL 0:1239e9b70ca2 58 /* autodetect x86-64 and make sure we are using 64-bit digits with x86-64 asm */
wolfSSL 0:1239e9b70ca2 59 #if defined(__x86_64__)
wolfSSL 0:1239e9b70ca2 60 #if defined(TFM_X86) || defined(TFM_SSE2) || defined(TFM_ARM)
wolfSSL 0:1239e9b70ca2 61 #error x86-64 detected, x86-32/SSE2/ARM optimizations are not valid!
wolfSSL 0:1239e9b70ca2 62 #endif
wolfSSL 0:1239e9b70ca2 63 #if !defined(TFM_X86_64) && !defined(TFM_NO_ASM)
wolfSSL 0:1239e9b70ca2 64 #define TFM_X86_64
wolfSSL 0:1239e9b70ca2 65 #endif
wolfSSL 0:1239e9b70ca2 66 #endif
wolfSSL 0:1239e9b70ca2 67 #if defined(TFM_X86_64)
wolfSSL 0:1239e9b70ca2 68 #if !defined(FP_64BIT)
wolfSSL 0:1239e9b70ca2 69 #define FP_64BIT
wolfSSL 0:1239e9b70ca2 70 #endif
wolfSSL 0:1239e9b70ca2 71 #endif
wolfSSL 0:1239e9b70ca2 72 /* use 64-bit digit even if not using asm on x86_64 */
wolfSSL 0:1239e9b70ca2 73 #if defined(__x86_64__) && !defined(FP_64BIT)
wolfSSL 0:1239e9b70ca2 74 #define FP_64BIT
wolfSSL 0:1239e9b70ca2 75 #endif
wolfSSL 0:1239e9b70ca2 76 #endif /* NO_64BIT */
wolfSSL 0:1239e9b70ca2 77
wolfSSL 0:1239e9b70ca2 78 /* try to detect x86-32 */
wolfSSL 0:1239e9b70ca2 79 #if defined(__i386__) && !defined(TFM_SSE2)
wolfSSL 0:1239e9b70ca2 80 #if defined(TFM_X86_64) || defined(TFM_ARM)
wolfSSL 0:1239e9b70ca2 81 #error x86-32 detected, x86-64/ARM optimizations are not valid!
wolfSSL 0:1239e9b70ca2 82 #endif
wolfSSL 0:1239e9b70ca2 83 #if !defined(TFM_X86) && !defined(TFM_NO_ASM)
wolfSSL 0:1239e9b70ca2 84 #define TFM_X86
wolfSSL 0:1239e9b70ca2 85 #endif
wolfSSL 0:1239e9b70ca2 86 #endif
wolfSSL 0:1239e9b70ca2 87
wolfSSL 0:1239e9b70ca2 88 /* make sure we're 32-bit for x86-32/sse/arm/ppc32 */
wolfSSL 0:1239e9b70ca2 89 #if (defined(TFM_X86) || defined(TFM_SSE2) || defined(TFM_ARM) || defined(TFM_PPC32)) && defined(FP_64BIT)
wolfSSL 0:1239e9b70ca2 90 #warning x86-32, SSE2 and ARM, PPC32 optimizations require 32-bit digits (undefining)
wolfSSL 0:1239e9b70ca2 91 #undef FP_64BIT
wolfSSL 0:1239e9b70ca2 92 #endif
wolfSSL 0:1239e9b70ca2 93
wolfSSL 0:1239e9b70ca2 94 /* multi asms? */
wolfSSL 0:1239e9b70ca2 95 #ifdef TFM_X86
wolfSSL 0:1239e9b70ca2 96 #define TFM_ASM
wolfSSL 0:1239e9b70ca2 97 #endif
wolfSSL 0:1239e9b70ca2 98 #ifdef TFM_X86_64
wolfSSL 0:1239e9b70ca2 99 #ifdef TFM_ASM
wolfSSL 0:1239e9b70ca2 100 #error TFM_ASM already defined!
wolfSSL 0:1239e9b70ca2 101 #endif
wolfSSL 0:1239e9b70ca2 102 #define TFM_ASM
wolfSSL 0:1239e9b70ca2 103 #endif
wolfSSL 0:1239e9b70ca2 104 #ifdef TFM_SSE2
wolfSSL 0:1239e9b70ca2 105 #ifdef TFM_ASM
wolfSSL 0:1239e9b70ca2 106 #error TFM_ASM already defined!
wolfSSL 0:1239e9b70ca2 107 #endif
wolfSSL 0:1239e9b70ca2 108 #define TFM_ASM
wolfSSL 0:1239e9b70ca2 109 #endif
wolfSSL 0:1239e9b70ca2 110 #ifdef TFM_ARM
wolfSSL 0:1239e9b70ca2 111 #ifdef TFM_ASM
wolfSSL 0:1239e9b70ca2 112 #error TFM_ASM already defined!
wolfSSL 0:1239e9b70ca2 113 #endif
wolfSSL 0:1239e9b70ca2 114 #define TFM_ASM
wolfSSL 0:1239e9b70ca2 115 #endif
wolfSSL 0:1239e9b70ca2 116 #ifdef TFM_PPC32
wolfSSL 0:1239e9b70ca2 117 #ifdef TFM_ASM
wolfSSL 0:1239e9b70ca2 118 #error TFM_ASM already defined!
wolfSSL 0:1239e9b70ca2 119 #endif
wolfSSL 0:1239e9b70ca2 120 #define TFM_ASM
wolfSSL 0:1239e9b70ca2 121 #endif
wolfSSL 0:1239e9b70ca2 122 #ifdef TFM_PPC64
wolfSSL 0:1239e9b70ca2 123 #ifdef TFM_ASM
wolfSSL 0:1239e9b70ca2 124 #error TFM_ASM already defined!
wolfSSL 0:1239e9b70ca2 125 #endif
wolfSSL 0:1239e9b70ca2 126 #define TFM_ASM
wolfSSL 0:1239e9b70ca2 127 #endif
wolfSSL 0:1239e9b70ca2 128 #ifdef TFM_AVR32
wolfSSL 0:1239e9b70ca2 129 #ifdef TFM_ASM
wolfSSL 0:1239e9b70ca2 130 #error TFM_ASM already defined!
wolfSSL 0:1239e9b70ca2 131 #endif
wolfSSL 0:1239e9b70ca2 132 #define TFM_ASM
wolfSSL 0:1239e9b70ca2 133 #endif
wolfSSL 0:1239e9b70ca2 134
wolfSSL 0:1239e9b70ca2 135 /* we want no asm? */
wolfSSL 0:1239e9b70ca2 136 #ifdef TFM_NO_ASM
wolfSSL 0:1239e9b70ca2 137 #undef TFM_X86
wolfSSL 0:1239e9b70ca2 138 #undef TFM_X86_64
wolfSSL 0:1239e9b70ca2 139 #undef TFM_SSE2
wolfSSL 0:1239e9b70ca2 140 #undef TFM_ARM
wolfSSL 0:1239e9b70ca2 141 #undef TFM_PPC32
wolfSSL 0:1239e9b70ca2 142 #undef TFM_PPC64
wolfSSL 0:1239e9b70ca2 143 #undef TFM_AVR32
wolfSSL 0:1239e9b70ca2 144 #undef TFM_ASM
wolfSSL 0:1239e9b70ca2 145 #endif
wolfSSL 0:1239e9b70ca2 146
wolfSSL 0:1239e9b70ca2 147 /* ECC helpers */
wolfSSL 0:1239e9b70ca2 148 #ifdef TFM_ECC192
wolfSSL 0:1239e9b70ca2 149 #ifdef FP_64BIT
wolfSSL 0:1239e9b70ca2 150 #define TFM_MUL3
wolfSSL 0:1239e9b70ca2 151 #define TFM_SQR3
wolfSSL 0:1239e9b70ca2 152 #else
wolfSSL 0:1239e9b70ca2 153 #define TFM_MUL6
wolfSSL 0:1239e9b70ca2 154 #define TFM_SQR6
wolfSSL 0:1239e9b70ca2 155 #endif
wolfSSL 0:1239e9b70ca2 156 #endif
wolfSSL 0:1239e9b70ca2 157
wolfSSL 0:1239e9b70ca2 158 #ifdef TFM_ECC224
wolfSSL 0:1239e9b70ca2 159 #ifdef FP_64BIT
wolfSSL 0:1239e9b70ca2 160 #define TFM_MUL4
wolfSSL 0:1239e9b70ca2 161 #define TFM_SQR4
wolfSSL 0:1239e9b70ca2 162 #else
wolfSSL 0:1239e9b70ca2 163 #define TFM_MUL7
wolfSSL 0:1239e9b70ca2 164 #define TFM_SQR7
wolfSSL 0:1239e9b70ca2 165 #endif
wolfSSL 0:1239e9b70ca2 166 #endif
wolfSSL 0:1239e9b70ca2 167
wolfSSL 0:1239e9b70ca2 168 #ifdef TFM_ECC256
wolfSSL 0:1239e9b70ca2 169 #ifdef FP_64BIT
wolfSSL 0:1239e9b70ca2 170 #define TFM_MUL4
wolfSSL 0:1239e9b70ca2 171 #define TFM_SQR4
wolfSSL 0:1239e9b70ca2 172 #else
wolfSSL 0:1239e9b70ca2 173 #define TFM_MUL8
wolfSSL 0:1239e9b70ca2 174 #define TFM_SQR8
wolfSSL 0:1239e9b70ca2 175 #endif
wolfSSL 0:1239e9b70ca2 176 #endif
wolfSSL 0:1239e9b70ca2 177
wolfSSL 0:1239e9b70ca2 178 #ifdef TFM_ECC384
wolfSSL 0:1239e9b70ca2 179 #ifdef FP_64BIT
wolfSSL 0:1239e9b70ca2 180 #define TFM_MUL6
wolfSSL 0:1239e9b70ca2 181 #define TFM_SQR6
wolfSSL 0:1239e9b70ca2 182 #else
wolfSSL 0:1239e9b70ca2 183 #define TFM_MUL12
wolfSSL 0:1239e9b70ca2 184 #define TFM_SQR12
wolfSSL 0:1239e9b70ca2 185 #endif
wolfSSL 0:1239e9b70ca2 186 #endif
wolfSSL 0:1239e9b70ca2 187
wolfSSL 0:1239e9b70ca2 188 #ifdef TFM_ECC521
wolfSSL 0:1239e9b70ca2 189 #ifdef FP_64BIT
wolfSSL 0:1239e9b70ca2 190 #define TFM_MUL9
wolfSSL 0:1239e9b70ca2 191 #define TFM_SQR9
wolfSSL 0:1239e9b70ca2 192 #else
wolfSSL 0:1239e9b70ca2 193 #define TFM_MUL17
wolfSSL 0:1239e9b70ca2 194 #define TFM_SQR17
wolfSSL 0:1239e9b70ca2 195 #endif
wolfSSL 0:1239e9b70ca2 196 #endif
wolfSSL 0:1239e9b70ca2 197
wolfSSL 0:1239e9b70ca2 198
wolfSSL 0:1239e9b70ca2 199 /* some default configurations.
wolfSSL 0:1239e9b70ca2 200 */
wolfSSL 0:1239e9b70ca2 201 #if defined(FP_64BIT)
wolfSSL 0:1239e9b70ca2 202 /* for GCC only on supported platforms */
wolfSSL 0:1239e9b70ca2 203 typedef unsigned long long fp_digit; /* 64bit, 128 uses mode(TI) below */
wolfSSL 0:1239e9b70ca2 204 typedef unsigned long fp_word __attribute__ ((mode(TI)));
wolfSSL 0:1239e9b70ca2 205 #else
wolfSSL 0:1239e9b70ca2 206 #if defined(_MSC_VER) || defined(__BORLANDC__)
wolfSSL 0:1239e9b70ca2 207 typedef unsigned __int64 ulong64;
wolfSSL 0:1239e9b70ca2 208 #else
wolfSSL 0:1239e9b70ca2 209 typedef unsigned long long ulong64;
wolfSSL 0:1239e9b70ca2 210 #endif
wolfSSL 0:1239e9b70ca2 211
wolfSSL 0:1239e9b70ca2 212 #ifndef NO_64BIT
wolfSSL 0:1239e9b70ca2 213 typedef unsigned int fp_digit;
wolfSSL 0:1239e9b70ca2 214 typedef ulong64 fp_word;
wolfSSL 0:1239e9b70ca2 215 #else
wolfSSL 0:1239e9b70ca2 216 /* some procs like coldfire prefer not to place multiply into 64bit type
wolfSSL 0:1239e9b70ca2 217 even though it exists */
wolfSSL 0:1239e9b70ca2 218 typedef unsigned short fp_digit;
wolfSSL 0:1239e9b70ca2 219 typedef unsigned int fp_word;
wolfSSL 0:1239e9b70ca2 220 #endif
wolfSSL 0:1239e9b70ca2 221 #endif
wolfSSL 0:1239e9b70ca2 222
wolfSSL 0:1239e9b70ca2 223 /* # of digits this is */
wolfSSL 0:1239e9b70ca2 224 #define DIGIT_BIT (int)((CHAR_BIT) * sizeof(fp_digit))
wolfSSL 0:1239e9b70ca2 225
wolfSSL 0:1239e9b70ca2 226 /* Max size of any number in bits. Basically the largest size you will be
wolfSSL 0:1239e9b70ca2 227 * multiplying should be half [or smaller] of FP_MAX_SIZE-four_digit
wolfSSL 0:1239e9b70ca2 228 *
wolfSSL 0:1239e9b70ca2 229 * It defaults to 4096-bits [allowing multiplications upto 2048x2048 bits ]
wolfSSL 0:1239e9b70ca2 230 */
wolfSSL 0:1239e9b70ca2 231 #ifndef FP_MAX_BITS
wolfSSL 0:1239e9b70ca2 232 #define FP_MAX_BITS 4096
wolfSSL 0:1239e9b70ca2 233 #endif
wolfSSL 0:1239e9b70ca2 234 #define FP_MAX_SIZE (FP_MAX_BITS+(8*DIGIT_BIT))
wolfSSL 0:1239e9b70ca2 235
wolfSSL 0:1239e9b70ca2 236 /* will this lib work? */
wolfSSL 0:1239e9b70ca2 237 #if (CHAR_BIT & 7)
wolfSSL 0:1239e9b70ca2 238 #error CHAR_BIT must be a multiple of eight.
wolfSSL 0:1239e9b70ca2 239 #endif
wolfSSL 0:1239e9b70ca2 240 #if FP_MAX_BITS % CHAR_BIT
wolfSSL 0:1239e9b70ca2 241 #error FP_MAX_BITS must be a multiple of CHAR_BIT
wolfSSL 0:1239e9b70ca2 242 #endif
wolfSSL 0:1239e9b70ca2 243
wolfSSL 0:1239e9b70ca2 244 #define FP_MASK (fp_digit)(-1)
wolfSSL 0:1239e9b70ca2 245 #define FP_SIZE (FP_MAX_SIZE/DIGIT_BIT)
wolfSSL 0:1239e9b70ca2 246
wolfSSL 0:1239e9b70ca2 247 /* signs */
wolfSSL 0:1239e9b70ca2 248 #define FP_ZPOS 0
wolfSSL 0:1239e9b70ca2 249 #define FP_NEG 1
wolfSSL 0:1239e9b70ca2 250
wolfSSL 0:1239e9b70ca2 251 /* return codes */
wolfSSL 0:1239e9b70ca2 252 #define FP_OKAY 0
wolfSSL 0:1239e9b70ca2 253 #define FP_VAL 1
wolfSSL 0:1239e9b70ca2 254 #define FP_MEM 2
wolfSSL 0:1239e9b70ca2 255
wolfSSL 0:1239e9b70ca2 256 /* equalities */
wolfSSL 0:1239e9b70ca2 257 #define FP_LT -1 /* less than */
wolfSSL 0:1239e9b70ca2 258 #define FP_EQ 0 /* equal to */
wolfSSL 0:1239e9b70ca2 259 #define FP_GT 1 /* greater than */
wolfSSL 0:1239e9b70ca2 260
wolfSSL 0:1239e9b70ca2 261 /* replies */
wolfSSL 0:1239e9b70ca2 262 #define FP_YES 1 /* yes response */
wolfSSL 0:1239e9b70ca2 263 #define FP_NO 0 /* no response */
wolfSSL 0:1239e9b70ca2 264
wolfSSL 0:1239e9b70ca2 265 /* a FP type */
wolfSSL 0:1239e9b70ca2 266 typedef struct {
wolfSSL 0:1239e9b70ca2 267 fp_digit dp[FP_SIZE];
wolfSSL 0:1239e9b70ca2 268 int used,
wolfSSL 0:1239e9b70ca2 269 sign;
wolfSSL 0:1239e9b70ca2 270 } fp_int;
wolfSSL 0:1239e9b70ca2 271
wolfSSL 0:1239e9b70ca2 272 /* externally define this symbol to ignore the default settings, useful for changing the build from the make process */
wolfSSL 0:1239e9b70ca2 273 #ifndef TFM_ALREADY_SET
wolfSSL 0:1239e9b70ca2 274
wolfSSL 0:1239e9b70ca2 275 /* do we want the large set of small multiplications ?
wolfSSL 0:1239e9b70ca2 276 Enable these if you are going to be doing a lot of small (<= 16 digit) multiplications say in ECC
wolfSSL 0:1239e9b70ca2 277 Or if you're on a 64-bit machine doing RSA as a 1024-bit integer == 16 digits ;-)
wolfSSL 0:1239e9b70ca2 278 */
wolfSSL 0:1239e9b70ca2 279 /* need to refactor the function */
wolfSSL 0:1239e9b70ca2 280 /*#define TFM_SMALL_SET */
wolfSSL 0:1239e9b70ca2 281
wolfSSL 0:1239e9b70ca2 282 /* do we want huge code
wolfSSL 0:1239e9b70ca2 283 Enable these if you are doing 20, 24, 28, 32, 48, 64 digit multiplications (useful for RSA)
wolfSSL 0:1239e9b70ca2 284 Less important on 64-bit machines as 32 digits == 2048 bits
wolfSSL 0:1239e9b70ca2 285 */
wolfSSL 0:1239e9b70ca2 286 #if 0
wolfSSL 0:1239e9b70ca2 287 #define TFM_MUL3
wolfSSL 0:1239e9b70ca2 288 #define TFM_MUL4
wolfSSL 0:1239e9b70ca2 289 #define TFM_MUL6
wolfSSL 0:1239e9b70ca2 290 #define TFM_MUL7
wolfSSL 0:1239e9b70ca2 291 #define TFM_MUL8
wolfSSL 0:1239e9b70ca2 292 #define TFM_MUL9
wolfSSL 0:1239e9b70ca2 293 #define TFM_MUL12
wolfSSL 0:1239e9b70ca2 294 #define TFM_MUL17
wolfSSL 0:1239e9b70ca2 295 #endif
wolfSSL 0:1239e9b70ca2 296 #ifdef TFM_HUGE_SET
wolfSSL 0:1239e9b70ca2 297 #define TFM_MUL20
wolfSSL 0:1239e9b70ca2 298 #define TFM_MUL24
wolfSSL 0:1239e9b70ca2 299 #define TFM_MUL28
wolfSSL 0:1239e9b70ca2 300 #define TFM_MUL32
wolfSSL 0:1239e9b70ca2 301 #if (FP_MAX_BITS >= 6144) && defined(FP_64BIT)
wolfSSL 0:1239e9b70ca2 302 #define TFM_MUL48
wolfSSL 0:1239e9b70ca2 303 #endif
wolfSSL 0:1239e9b70ca2 304 #if (FP_MAX_BITS >= 8192) && defined(FP_64BIT)
wolfSSL 0:1239e9b70ca2 305 #define TFM_MUL64
wolfSSL 0:1239e9b70ca2 306 #endif
wolfSSL 0:1239e9b70ca2 307 #endif
wolfSSL 0:1239e9b70ca2 308
wolfSSL 0:1239e9b70ca2 309 #if 0
wolfSSL 0:1239e9b70ca2 310 #define TFM_SQR3
wolfSSL 0:1239e9b70ca2 311 #define TFM_SQR4
wolfSSL 0:1239e9b70ca2 312 #define TFM_SQR6
wolfSSL 0:1239e9b70ca2 313 #define TFM_SQR7
wolfSSL 0:1239e9b70ca2 314 #define TFM_SQR8
wolfSSL 0:1239e9b70ca2 315 #define TFM_SQR9
wolfSSL 0:1239e9b70ca2 316 #define TFM_SQR12
wolfSSL 0:1239e9b70ca2 317 #define TFM_SQR17
wolfSSL 0:1239e9b70ca2 318 #endif
wolfSSL 0:1239e9b70ca2 319 #ifdef TFM_HUGE_SET
wolfSSL 0:1239e9b70ca2 320 #define TFM_SQR20
wolfSSL 0:1239e9b70ca2 321 #define TFM_SQR24
wolfSSL 0:1239e9b70ca2 322 #define TFM_SQR28
wolfSSL 0:1239e9b70ca2 323 #define TFM_SQR32
wolfSSL 0:1239e9b70ca2 324 #define TFM_SQR48
wolfSSL 0:1239e9b70ca2 325 #define TFM_SQR64
wolfSSL 0:1239e9b70ca2 326 #endif
wolfSSL 0:1239e9b70ca2 327
wolfSSL 0:1239e9b70ca2 328 /* do we want some overflow checks
wolfSSL 0:1239e9b70ca2 329 Not required if you make sure your numbers are within range (e.g. by default a modulus for fp_exptmod() can only be upto 2048 bits long)
wolfSSL 0:1239e9b70ca2 330 */
wolfSSL 0:1239e9b70ca2 331 /* #define TFM_CHECK */
wolfSSL 0:1239e9b70ca2 332
wolfSSL 0:1239e9b70ca2 333 /* Is the target a P4 Prescott
wolfSSL 0:1239e9b70ca2 334 */
wolfSSL 0:1239e9b70ca2 335 /* #define TFM_PRESCOTT */
wolfSSL 0:1239e9b70ca2 336
wolfSSL 0:1239e9b70ca2 337 /* Do we want timing resistant fp_exptmod() ?
wolfSSL 0:1239e9b70ca2 338 * This makes it slower but also timing invariant with respect to the exponent
wolfSSL 0:1239e9b70ca2 339 */
wolfSSL 0:1239e9b70ca2 340 /* #define TFM_TIMING_RESISTANT */
wolfSSL 0:1239e9b70ca2 341
wolfSSL 0:1239e9b70ca2 342 #endif /* TFM_ALREADY_SET */
wolfSSL 0:1239e9b70ca2 343
wolfSSL 0:1239e9b70ca2 344 /* functions */
wolfSSL 0:1239e9b70ca2 345
wolfSSL 0:1239e9b70ca2 346 /* returns a TFM ident string useful for debugging... */
wolfSSL 0:1239e9b70ca2 347 /*const char *fp_ident(void);*/
wolfSSL 0:1239e9b70ca2 348
wolfSSL 0:1239e9b70ca2 349 /* initialize [or zero] an fp int */
wolfSSL 0:1239e9b70ca2 350 #define fp_init(a) (void)XMEMSET((a), 0, sizeof(fp_int))
wolfSSL 0:1239e9b70ca2 351 #define fp_zero(a) fp_init(a)
wolfSSL 0:1239e9b70ca2 352
wolfSSL 0:1239e9b70ca2 353 /* zero/even/odd ? */
wolfSSL 0:1239e9b70ca2 354 #define fp_iszero(a) (((a)->used == 0) ? FP_YES : FP_NO)
wolfSSL 0:1239e9b70ca2 355 #define fp_iseven(a) (((a)->used >= 0 && (((a)->dp[0] & 1) == 0)) ? FP_YES : FP_NO)
wolfSSL 0:1239e9b70ca2 356 #define fp_isodd(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 1)) ? FP_YES : FP_NO)
wolfSSL 0:1239e9b70ca2 357
wolfSSL 0:1239e9b70ca2 358 /* set to a small digit */
wolfSSL 0:1239e9b70ca2 359 void fp_set(fp_int *a, fp_digit b);
wolfSSL 0:1239e9b70ca2 360
wolfSSL 0:1239e9b70ca2 361 /* copy from a to b */
wolfSSL 0:1239e9b70ca2 362 #define fp_copy(a, b) (void)(((a) != (b)) ? ((void)XMEMCPY((b), (a), sizeof(fp_int))) : (void)0)
wolfSSL 0:1239e9b70ca2 363 #define fp_init_copy(a, b) fp_copy(b, a)
wolfSSL 0:1239e9b70ca2 364
wolfSSL 0:1239e9b70ca2 365 /* clamp digits */
wolfSSL 0:1239e9b70ca2 366 #define fp_clamp(a) { while ((a)->used && (a)->dp[(a)->used-1] == 0) --((a)->used); (a)->sign = (a)->used ? (a)->sign : FP_ZPOS; }
wolfSSL 0:1239e9b70ca2 367
wolfSSL 0:1239e9b70ca2 368 /* negate and absolute */
wolfSSL 0:1239e9b70ca2 369 #define fp_neg(a, b) { fp_copy(a, b); (b)->sign ^= 1; fp_clamp(b); }
wolfSSL 0:1239e9b70ca2 370 #define fp_abs(a, b) { fp_copy(a, b); (b)->sign = 0; }
wolfSSL 0:1239e9b70ca2 371
wolfSSL 0:1239e9b70ca2 372 /* right shift x digits */
wolfSSL 0:1239e9b70ca2 373 void fp_rshd(fp_int *a, int x);
wolfSSL 0:1239e9b70ca2 374
wolfSSL 0:1239e9b70ca2 375 /* right shift x bits */
wolfSSL 0:1239e9b70ca2 376 void fp_rshb(fp_int *a, int x);
wolfSSL 0:1239e9b70ca2 377
wolfSSL 0:1239e9b70ca2 378 /* left shift x digits */
wolfSSL 0:1239e9b70ca2 379 void fp_lshd(fp_int *a, int x);
wolfSSL 0:1239e9b70ca2 380
wolfSSL 0:1239e9b70ca2 381 /* signed comparison */
wolfSSL 0:1239e9b70ca2 382 int fp_cmp(fp_int *a, fp_int *b);
wolfSSL 0:1239e9b70ca2 383
wolfSSL 0:1239e9b70ca2 384 /* unsigned comparison */
wolfSSL 0:1239e9b70ca2 385 int fp_cmp_mag(fp_int *a, fp_int *b);
wolfSSL 0:1239e9b70ca2 386
wolfSSL 0:1239e9b70ca2 387 /* power of 2 operations */
wolfSSL 0:1239e9b70ca2 388 void fp_div_2d(fp_int *a, int b, fp_int *c, fp_int *d);
wolfSSL 0:1239e9b70ca2 389 void fp_mod_2d(fp_int *a, int b, fp_int *c);
wolfSSL 0:1239e9b70ca2 390 void fp_mul_2d(fp_int *a, int b, fp_int *c);
wolfSSL 0:1239e9b70ca2 391 void fp_2expt (fp_int *a, int b);
wolfSSL 0:1239e9b70ca2 392 void fp_mul_2(fp_int *a, fp_int *c);
wolfSSL 0:1239e9b70ca2 393 void fp_div_2(fp_int *a, fp_int *c);
wolfSSL 0:1239e9b70ca2 394
wolfSSL 0:1239e9b70ca2 395 /* Counts the number of lsbs which are zero before the first zero bit */
wolfSSL 0:1239e9b70ca2 396 /*int fp_cnt_lsb(fp_int *a);*/
wolfSSL 0:1239e9b70ca2 397
wolfSSL 0:1239e9b70ca2 398 /* c = a + b */
wolfSSL 0:1239e9b70ca2 399 void fp_add(fp_int *a, fp_int *b, fp_int *c);
wolfSSL 0:1239e9b70ca2 400
wolfSSL 0:1239e9b70ca2 401 /* c = a - b */
wolfSSL 0:1239e9b70ca2 402 void fp_sub(fp_int *a, fp_int *b, fp_int *c);
wolfSSL 0:1239e9b70ca2 403
wolfSSL 0:1239e9b70ca2 404 /* c = a * b */
wolfSSL 0:1239e9b70ca2 405 void fp_mul(fp_int *a, fp_int *b, fp_int *c);
wolfSSL 0:1239e9b70ca2 406
wolfSSL 0:1239e9b70ca2 407 /* b = a*a */
wolfSSL 0:1239e9b70ca2 408 void fp_sqr(fp_int *a, fp_int *b);
wolfSSL 0:1239e9b70ca2 409
wolfSSL 0:1239e9b70ca2 410 /* a/b => cb + d == a */
wolfSSL 0:1239e9b70ca2 411 int fp_div(fp_int *a, fp_int *b, fp_int *c, fp_int *d);
wolfSSL 0:1239e9b70ca2 412
wolfSSL 0:1239e9b70ca2 413 /* c = a mod b, 0 <= c < b */
wolfSSL 0:1239e9b70ca2 414 int fp_mod(fp_int *a, fp_int *b, fp_int *c);
wolfSSL 0:1239e9b70ca2 415
wolfSSL 0:1239e9b70ca2 416 /* compare against a single digit */
wolfSSL 0:1239e9b70ca2 417 int fp_cmp_d(fp_int *a, fp_digit b);
wolfSSL 0:1239e9b70ca2 418
wolfSSL 0:1239e9b70ca2 419 /* c = a + b */
wolfSSL 0:1239e9b70ca2 420 void fp_add_d(fp_int *a, fp_digit b, fp_int *c);
wolfSSL 0:1239e9b70ca2 421
wolfSSL 0:1239e9b70ca2 422 /* c = a - b */
wolfSSL 0:1239e9b70ca2 423 void fp_sub_d(fp_int *a, fp_digit b, fp_int *c);
wolfSSL 0:1239e9b70ca2 424
wolfSSL 0:1239e9b70ca2 425 /* c = a * b */
wolfSSL 0:1239e9b70ca2 426 void fp_mul_d(fp_int *a, fp_digit b, fp_int *c);
wolfSSL 0:1239e9b70ca2 427
wolfSSL 0:1239e9b70ca2 428 /* a/b => cb + d == a */
wolfSSL 0:1239e9b70ca2 429 /*int fp_div_d(fp_int *a, fp_digit b, fp_int *c, fp_digit *d);*/
wolfSSL 0:1239e9b70ca2 430
wolfSSL 0:1239e9b70ca2 431 /* c = a mod b, 0 <= c < b */
wolfSSL 0:1239e9b70ca2 432 /*int fp_mod_d(fp_int *a, fp_digit b, fp_digit *c);*/
wolfSSL 0:1239e9b70ca2 433
wolfSSL 0:1239e9b70ca2 434 /* ---> number theory <--- */
wolfSSL 0:1239e9b70ca2 435 /* d = a + b (mod c) */
wolfSSL 0:1239e9b70ca2 436 /*int fp_addmod(fp_int *a, fp_int *b, fp_int *c, fp_int *d);*/
wolfSSL 0:1239e9b70ca2 437
wolfSSL 0:1239e9b70ca2 438 /* d = a - b (mod c) */
wolfSSL 0:1239e9b70ca2 439 /*int fp_submod(fp_int *a, fp_int *b, fp_int *c, fp_int *d);*/
wolfSSL 0:1239e9b70ca2 440
wolfSSL 0:1239e9b70ca2 441 /* d = a * b (mod c) */
wolfSSL 0:1239e9b70ca2 442 int fp_mulmod(fp_int *a, fp_int *b, fp_int *c, fp_int *d);
wolfSSL 0:1239e9b70ca2 443
wolfSSL 0:1239e9b70ca2 444 /* c = a * a (mod b) */
wolfSSL 0:1239e9b70ca2 445 int fp_sqrmod(fp_int *a, fp_int *b, fp_int *c);
wolfSSL 0:1239e9b70ca2 446
wolfSSL 0:1239e9b70ca2 447 /* c = 1/a (mod b) */
wolfSSL 0:1239e9b70ca2 448 int fp_invmod(fp_int *a, fp_int *b, fp_int *c);
wolfSSL 0:1239e9b70ca2 449
wolfSSL 0:1239e9b70ca2 450 /* c = (a, b) */
wolfSSL 0:1239e9b70ca2 451 /*void fp_gcd(fp_int *a, fp_int *b, fp_int *c);*/
wolfSSL 0:1239e9b70ca2 452
wolfSSL 0:1239e9b70ca2 453 /* c = [a, b] */
wolfSSL 0:1239e9b70ca2 454 /*void fp_lcm(fp_int *a, fp_int *b, fp_int *c);*/
wolfSSL 0:1239e9b70ca2 455
wolfSSL 0:1239e9b70ca2 456 /* setups the montgomery reduction */
wolfSSL 0:1239e9b70ca2 457 int fp_montgomery_setup(fp_int *a, fp_digit *mp);
wolfSSL 0:1239e9b70ca2 458
wolfSSL 0:1239e9b70ca2 459 /* computes a = B**n mod b without division or multiplication useful for
wolfSSL 0:1239e9b70ca2 460 * normalizing numbers in a Montgomery system.
wolfSSL 0:1239e9b70ca2 461 */
wolfSSL 0:1239e9b70ca2 462 void fp_montgomery_calc_normalization(fp_int *a, fp_int *b);
wolfSSL 0:1239e9b70ca2 463
wolfSSL 0:1239e9b70ca2 464 /* computes x/R == x (mod N) via Montgomery Reduction */
wolfSSL 0:1239e9b70ca2 465 void fp_montgomery_reduce(fp_int *a, fp_int *m, fp_digit mp);
wolfSSL 0:1239e9b70ca2 466
wolfSSL 0:1239e9b70ca2 467 /* d = a**b (mod c) */
wolfSSL 0:1239e9b70ca2 468 int fp_exptmod(fp_int *a, fp_int *b, fp_int *c, fp_int *d);
wolfSSL 0:1239e9b70ca2 469
wolfSSL 0:1239e9b70ca2 470 /* primality stuff */
wolfSSL 0:1239e9b70ca2 471
wolfSSL 0:1239e9b70ca2 472 /* perform a Miller-Rabin test of a to the base b and store result in "result" */
wolfSSL 0:1239e9b70ca2 473 /*void fp_prime_miller_rabin (fp_int * a, fp_int * b, int *result);*/
wolfSSL 0:1239e9b70ca2 474
wolfSSL 0:1239e9b70ca2 475 /* 256 trial divisions + 8 Miller-Rabins, returns FP_YES if probable prime */
wolfSSL 0:1239e9b70ca2 476 /*int fp_isprime(fp_int *a);*/
wolfSSL 0:1239e9b70ca2 477
wolfSSL 0:1239e9b70ca2 478 /* Primality generation flags */
wolfSSL 0:1239e9b70ca2 479 /*#define TFM_PRIME_BBS 0x0001 */ /* BBS style prime */
wolfSSL 0:1239e9b70ca2 480 /*#define TFM_PRIME_SAFE 0x0002 */ /* Safe prime (p-1)/2 == prime */
wolfSSL 0:1239e9b70ca2 481 /*#define TFM_PRIME_2MSB_OFF 0x0004 */ /* force 2nd MSB to 0 */
wolfSSL 0:1239e9b70ca2 482 /*#define TFM_PRIME_2MSB_ON 0x0008 */ /* force 2nd MSB to 1 */
wolfSSL 0:1239e9b70ca2 483
wolfSSL 0:1239e9b70ca2 484 /* callback for fp_prime_random, should fill dst with random bytes and return how many read [upto len] */
wolfSSL 0:1239e9b70ca2 485 /*typedef int tfm_prime_callback(unsigned char *dst, int len, void *dat);*/
wolfSSL 0:1239e9b70ca2 486
wolfSSL 0:1239e9b70ca2 487 /*#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 0:1239e9b70ca2 488
wolfSSL 0:1239e9b70ca2 489 /*int fp_prime_random_ex(fp_int *a, int t, int size, int flags, tfm_prime_callback cb, void *dat);*/
wolfSSL 0:1239e9b70ca2 490
wolfSSL 0:1239e9b70ca2 491 /* radix conersions */
wolfSSL 0:1239e9b70ca2 492 int fp_count_bits(fp_int *a);
wolfSSL 0:1239e9b70ca2 493 int fp_leading_bit(fp_int *a);
wolfSSL 0:1239e9b70ca2 494
wolfSSL 0:1239e9b70ca2 495 int fp_unsigned_bin_size(fp_int *a);
wolfSSL 0:1239e9b70ca2 496 void fp_read_unsigned_bin(fp_int *a, unsigned char *b, int c);
wolfSSL 0:1239e9b70ca2 497 void fp_to_unsigned_bin(fp_int *a, unsigned char *b);
wolfSSL 0:1239e9b70ca2 498
wolfSSL 0:1239e9b70ca2 499 /*int fp_signed_bin_size(fp_int *a);*/
wolfSSL 0:1239e9b70ca2 500 /*void fp_read_signed_bin(fp_int *a, unsigned char *b, int c);*/
wolfSSL 0:1239e9b70ca2 501 /*void fp_to_signed_bin(fp_int *a, unsigned char *b);*/
wolfSSL 0:1239e9b70ca2 502
wolfSSL 0:1239e9b70ca2 503 /*int fp_read_radix(fp_int *a, char *str, int radix);*/
wolfSSL 0:1239e9b70ca2 504 /*int fp_toradix(fp_int *a, char *str, int radix);*/
wolfSSL 0:1239e9b70ca2 505 /*int fp_toradix_n(fp_int * a, char *str, int radix, int maxlen);*/
wolfSSL 0:1239e9b70ca2 506
wolfSSL 0:1239e9b70ca2 507
wolfSSL 0:1239e9b70ca2 508 /* VARIOUS LOW LEVEL STUFFS */
wolfSSL 0:1239e9b70ca2 509 void s_fp_add(fp_int *a, fp_int *b, fp_int *c);
wolfSSL 0:1239e9b70ca2 510 void s_fp_sub(fp_int *a, fp_int *b, fp_int *c);
wolfSSL 0:1239e9b70ca2 511 void fp_reverse(unsigned char *s, int len);
wolfSSL 0:1239e9b70ca2 512
wolfSSL 0:1239e9b70ca2 513 void fp_mul_comba(fp_int *a, fp_int *b, fp_int *c);
wolfSSL 0:1239e9b70ca2 514
wolfSSL 0:1239e9b70ca2 515 #ifdef TFM_SMALL_SET
wolfSSL 0:1239e9b70ca2 516 void fp_mul_comba_small(fp_int *a, fp_int *b, fp_int *c);
wolfSSL 0:1239e9b70ca2 517 #endif
wolfSSL 0:1239e9b70ca2 518
wolfSSL 0:1239e9b70ca2 519 #ifdef TFM_MUL3
wolfSSL 0:1239e9b70ca2 520 void fp_mul_comba3(fp_int *a, fp_int *b, fp_int *c);
wolfSSL 0:1239e9b70ca2 521 #endif
wolfSSL 0:1239e9b70ca2 522 #ifdef TFM_MUL4
wolfSSL 0:1239e9b70ca2 523 void fp_mul_comba4(fp_int *a, fp_int *b, fp_int *c);
wolfSSL 0:1239e9b70ca2 524 #endif
wolfSSL 0:1239e9b70ca2 525 #ifdef TFM_MUL6
wolfSSL 0:1239e9b70ca2 526 void fp_mul_comba6(fp_int *a, fp_int *b, fp_int *c);
wolfSSL 0:1239e9b70ca2 527 #endif
wolfSSL 0:1239e9b70ca2 528 #ifdef TFM_MUL7
wolfSSL 0:1239e9b70ca2 529 void fp_mul_comba7(fp_int *a, fp_int *b, fp_int *c);
wolfSSL 0:1239e9b70ca2 530 #endif
wolfSSL 0:1239e9b70ca2 531 #ifdef TFM_MUL8
wolfSSL 0:1239e9b70ca2 532 void fp_mul_comba8(fp_int *a, fp_int *b, fp_int *c);
wolfSSL 0:1239e9b70ca2 533 #endif
wolfSSL 0:1239e9b70ca2 534 #ifdef TFM_MUL9
wolfSSL 0:1239e9b70ca2 535 void fp_mul_comba9(fp_int *a, fp_int *b, fp_int *c);
wolfSSL 0:1239e9b70ca2 536 #endif
wolfSSL 0:1239e9b70ca2 537 #ifdef TFM_MUL12
wolfSSL 0:1239e9b70ca2 538 void fp_mul_comba12(fp_int *a, fp_int *b, fp_int *c);
wolfSSL 0:1239e9b70ca2 539 #endif
wolfSSL 0:1239e9b70ca2 540 #ifdef TFM_MUL17
wolfSSL 0:1239e9b70ca2 541 void fp_mul_comba17(fp_int *a, fp_int *b, fp_int *c);
wolfSSL 0:1239e9b70ca2 542 #endif
wolfSSL 0:1239e9b70ca2 543
wolfSSL 0:1239e9b70ca2 544 #ifdef TFM_MUL20
wolfSSL 0:1239e9b70ca2 545 void fp_mul_comba20(fp_int *a, fp_int *b, fp_int *c);
wolfSSL 0:1239e9b70ca2 546 #endif
wolfSSL 0:1239e9b70ca2 547 #ifdef TFM_MUL24
wolfSSL 0:1239e9b70ca2 548 void fp_mul_comba24(fp_int *a, fp_int *b, fp_int *c);
wolfSSL 0:1239e9b70ca2 549 #endif
wolfSSL 0:1239e9b70ca2 550 #ifdef TFM_MUL28
wolfSSL 0:1239e9b70ca2 551 void fp_mul_comba28(fp_int *a, fp_int *b, fp_int *c);
wolfSSL 0:1239e9b70ca2 552 #endif
wolfSSL 0:1239e9b70ca2 553 #ifdef TFM_MUL32
wolfSSL 0:1239e9b70ca2 554 void fp_mul_comba32(fp_int *a, fp_int *b, fp_int *c);
wolfSSL 0:1239e9b70ca2 555 #endif
wolfSSL 0:1239e9b70ca2 556 #ifdef TFM_MUL48
wolfSSL 0:1239e9b70ca2 557 void fp_mul_comba48(fp_int *a, fp_int *b, fp_int *c);
wolfSSL 0:1239e9b70ca2 558 #endif
wolfSSL 0:1239e9b70ca2 559 #ifdef TFM_MUL64
wolfSSL 0:1239e9b70ca2 560 void fp_mul_comba64(fp_int *a, fp_int *b, fp_int *c);
wolfSSL 0:1239e9b70ca2 561 #endif
wolfSSL 0:1239e9b70ca2 562
wolfSSL 0:1239e9b70ca2 563 void fp_sqr_comba(fp_int *a, fp_int *b);
wolfSSL 0:1239e9b70ca2 564
wolfSSL 0:1239e9b70ca2 565 #ifdef TFM_SMALL_SET
wolfSSL 0:1239e9b70ca2 566 void fp_sqr_comba_small(fp_int *a, fp_int *b);
wolfSSL 0:1239e9b70ca2 567 #endif
wolfSSL 0:1239e9b70ca2 568
wolfSSL 0:1239e9b70ca2 569 #ifdef TFM_SQR3
wolfSSL 0:1239e9b70ca2 570 void fp_sqr_comba3(fp_int *a, fp_int *b);
wolfSSL 0:1239e9b70ca2 571 #endif
wolfSSL 0:1239e9b70ca2 572 #ifdef TFM_SQR4
wolfSSL 0:1239e9b70ca2 573 void fp_sqr_comba4(fp_int *a, fp_int *b);
wolfSSL 0:1239e9b70ca2 574 #endif
wolfSSL 0:1239e9b70ca2 575 #ifdef TFM_SQR6
wolfSSL 0:1239e9b70ca2 576 void fp_sqr_comba6(fp_int *a, fp_int *b);
wolfSSL 0:1239e9b70ca2 577 #endif
wolfSSL 0:1239e9b70ca2 578 #ifdef TFM_SQR7
wolfSSL 0:1239e9b70ca2 579 void fp_sqr_comba7(fp_int *a, fp_int *b);
wolfSSL 0:1239e9b70ca2 580 #endif
wolfSSL 0:1239e9b70ca2 581 #ifdef TFM_SQR8
wolfSSL 0:1239e9b70ca2 582 void fp_sqr_comba8(fp_int *a, fp_int *b);
wolfSSL 0:1239e9b70ca2 583 #endif
wolfSSL 0:1239e9b70ca2 584 #ifdef TFM_SQR9
wolfSSL 0:1239e9b70ca2 585 void fp_sqr_comba9(fp_int *a, fp_int *b);
wolfSSL 0:1239e9b70ca2 586 #endif
wolfSSL 0:1239e9b70ca2 587 #ifdef TFM_SQR12
wolfSSL 0:1239e9b70ca2 588 void fp_sqr_comba12(fp_int *a, fp_int *b);
wolfSSL 0:1239e9b70ca2 589 #endif
wolfSSL 0:1239e9b70ca2 590 #ifdef TFM_SQR17
wolfSSL 0:1239e9b70ca2 591 void fp_sqr_comba17(fp_int *a, fp_int *b);
wolfSSL 0:1239e9b70ca2 592 #endif
wolfSSL 0:1239e9b70ca2 593
wolfSSL 0:1239e9b70ca2 594 #ifdef TFM_SQR20
wolfSSL 0:1239e9b70ca2 595 void fp_sqr_comba20(fp_int *a, fp_int *b);
wolfSSL 0:1239e9b70ca2 596 #endif
wolfSSL 0:1239e9b70ca2 597 #ifdef TFM_SQR24
wolfSSL 0:1239e9b70ca2 598 void fp_sqr_comba24(fp_int *a, fp_int *b);
wolfSSL 0:1239e9b70ca2 599 #endif
wolfSSL 0:1239e9b70ca2 600 #ifdef TFM_SQR28
wolfSSL 0:1239e9b70ca2 601 void fp_sqr_comba28(fp_int *a, fp_int *b);
wolfSSL 0:1239e9b70ca2 602 #endif
wolfSSL 0:1239e9b70ca2 603 #ifdef TFM_SQR32
wolfSSL 0:1239e9b70ca2 604 void fp_sqr_comba32(fp_int *a, fp_int *b);
wolfSSL 0:1239e9b70ca2 605 #endif
wolfSSL 0:1239e9b70ca2 606 #ifdef TFM_SQR48
wolfSSL 0:1239e9b70ca2 607 void fp_sqr_comba48(fp_int *a, fp_int *b);
wolfSSL 0:1239e9b70ca2 608 #endif
wolfSSL 0:1239e9b70ca2 609 #ifdef TFM_SQR64
wolfSSL 0:1239e9b70ca2 610 void fp_sqr_comba64(fp_int *a, fp_int *b);
wolfSSL 0:1239e9b70ca2 611 #endif
wolfSSL 0:1239e9b70ca2 612 /*extern const char *fp_s_rmap;*/
wolfSSL 0:1239e9b70ca2 613
wolfSSL 0:1239e9b70ca2 614
wolfSSL 0:1239e9b70ca2 615 /**
wolfSSL 0:1239e9b70ca2 616 * Used by CyaSSL
wolfSSL 0:1239e9b70ca2 617 */
wolfSSL 0:1239e9b70ca2 618
wolfSSL 0:1239e9b70ca2 619 /* Types */
wolfSSL 0:1239e9b70ca2 620 typedef fp_digit mp_digit;
wolfSSL 0:1239e9b70ca2 621 typedef fp_word mp_word;
wolfSSL 0:1239e9b70ca2 622 typedef fp_int mp_int;
wolfSSL 0:1239e9b70ca2 623
wolfSSL 0:1239e9b70ca2 624 /* Constants */
wolfSSL 0:1239e9b70ca2 625 #define MP_LT FP_LT /* less than */
wolfSSL 0:1239e9b70ca2 626 #define MP_EQ FP_EQ /* equal to */
wolfSSL 0:1239e9b70ca2 627 #define MP_GT FP_GT /* greater than */
wolfSSL 0:1239e9b70ca2 628 #define MP_OKAY FP_OKAY /* ok result */
wolfSSL 0:1239e9b70ca2 629 #define MP_NO FP_NO /* yes/no result */
wolfSSL 0:1239e9b70ca2 630 #define MP_YES FP_YES /* yes/no result */
wolfSSL 0:1239e9b70ca2 631
wolfSSL 0:1239e9b70ca2 632 /* Prototypes */
wolfSSL 0:1239e9b70ca2 633 int mp_init (mp_int * a);
wolfSSL 0:1239e9b70ca2 634 void mp_clear (mp_int * a);
wolfSSL 0:1239e9b70ca2 635 int mp_init_multi(mp_int* a, mp_int* b, mp_int* c, mp_int* d, mp_int* e, mp_int* f);
wolfSSL 0:1239e9b70ca2 636
wolfSSL 0:1239e9b70ca2 637 int mp_add (mp_int * a, mp_int * b, mp_int * c);
wolfSSL 0:1239e9b70ca2 638 int mp_sub (mp_int * a, mp_int * b, mp_int * c);
wolfSSL 0:1239e9b70ca2 639 int mp_add_d (mp_int * a, mp_digit b, mp_int * c);
wolfSSL 0:1239e9b70ca2 640
wolfSSL 0:1239e9b70ca2 641 int mp_mul (mp_int * a, mp_int * b, mp_int * c);
wolfSSL 0:1239e9b70ca2 642 int mp_mulmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d);
wolfSSL 0:1239e9b70ca2 643 int mp_mod(mp_int *a, mp_int *b, mp_int *c);
wolfSSL 0:1239e9b70ca2 644 int mp_invmod(mp_int *a, mp_int *b, mp_int *c);
wolfSSL 0:1239e9b70ca2 645 int mp_exptmod (mp_int * g, mp_int * x, mp_int * p, mp_int * y);
wolfSSL 0:1239e9b70ca2 646
wolfSSL 0:1239e9b70ca2 647 int mp_cmp(mp_int *a, mp_int *b);
wolfSSL 0:1239e9b70ca2 648 int mp_cmp_d(mp_int *a, mp_digit b);
wolfSSL 0:1239e9b70ca2 649
wolfSSL 0:1239e9b70ca2 650 int mp_unsigned_bin_size(mp_int * a);
wolfSSL 0:1239e9b70ca2 651 int mp_read_unsigned_bin (mp_int * a, const unsigned char *b, int c);
wolfSSL 0:1239e9b70ca2 652 int mp_to_unsigned_bin (mp_int * a, unsigned char *b);
wolfSSL 0:1239e9b70ca2 653
wolfSSL 0:1239e9b70ca2 654 int mp_sub_d(fp_int *a, fp_digit b, fp_int *c);
wolfSSL 0:1239e9b70ca2 655 int mp_copy(fp_int* a, fp_int* b);
wolfSSL 0:1239e9b70ca2 656 int mp_isodd(mp_int* a);
wolfSSL 0:1239e9b70ca2 657 int mp_iszero(mp_int* a);
wolfSSL 0:1239e9b70ca2 658 int mp_count_bits(mp_int *a);
wolfSSL 0:1239e9b70ca2 659 int mp_leading_bit(mp_int *a);
wolfSSL 0:1239e9b70ca2 660 int mp_set_int(fp_int *a, fp_digit b);
wolfSSL 0:1239e9b70ca2 661 void mp_rshb(mp_int *a, int x);
wolfSSL 0:1239e9b70ca2 662
wolfSSL 0:1239e9b70ca2 663 #ifdef HAVE_ECC
wolfSSL 0:1239e9b70ca2 664 int mp_read_radix(mp_int* a, const char* str, int radix);
wolfSSL 0:1239e9b70ca2 665 int mp_set(fp_int *a, fp_digit b);
wolfSSL 0:1239e9b70ca2 666 int mp_sqr(fp_int *a, fp_int *b);
wolfSSL 0:1239e9b70ca2 667 int mp_montgomery_reduce(fp_int *a, fp_int *m, fp_digit mp);
wolfSSL 0:1239e9b70ca2 668 int mp_montgomery_setup(fp_int *a, fp_digit *rho);
wolfSSL 0:1239e9b70ca2 669 int mp_div_2(fp_int * a, fp_int * b);
wolfSSL 0:1239e9b70ca2 670 int mp_init_copy(fp_int * a, fp_int * b);
wolfSSL 0:1239e9b70ca2 671 #endif
wolfSSL 0:1239e9b70ca2 672
wolfSSL 0:1239e9b70ca2 673 #if defined(HAVE_ECC) || defined(CYASSL_KEY_GEN)
wolfSSL 0:1239e9b70ca2 674 int mp_sqrmod(mp_int* a, mp_int* b, mp_int* c);
wolfSSL 0:1239e9b70ca2 675 int mp_montgomery_calc_normalization(mp_int *a, mp_int *b);
wolfSSL 0:1239e9b70ca2 676 #endif
wolfSSL 0:1239e9b70ca2 677
wolfSSL 0:1239e9b70ca2 678 #ifdef CYASSL_KEY_GEN
wolfSSL 0:1239e9b70ca2 679 int mp_gcd(fp_int *a, fp_int *b, fp_int *c);
wolfSSL 0:1239e9b70ca2 680 int mp_lcm(fp_int *a, fp_int *b, fp_int *c);
wolfSSL 0:1239e9b70ca2 681 int mp_prime_is_prime(mp_int* a, int t, int* result);
wolfSSL 0:1239e9b70ca2 682 #endif /* CYASSL_KEY_GEN */
wolfSSL 0:1239e9b70ca2 683
wolfSSL 0:1239e9b70ca2 684 CYASSL_API word32 CheckRunTimeFastMath(void);
wolfSSL 0:1239e9b70ca2 685
wolfSSL 0:1239e9b70ca2 686 /* If user uses RSA, DH, DSA, or ECC math lib directly then fast math FP_SIZE
wolfSSL 0:1239e9b70ca2 687 must match, return 1 if a match otherwise 0 */
wolfSSL 0:1239e9b70ca2 688 #define CheckFastMathSettings() (FP_SIZE == CheckRunTimeFastMath())
wolfSSL 0:1239e9b70ca2 689 #ifdef __cplusplus
wolfSSL 0:1239e9b70ca2 690 }
wolfSSL 0:1239e9b70ca2 691 #endif
wolfSSL 0:1239e9b70ca2 692
wolfSSL 0:1239e9b70ca2 693
wolfSSL 0:1239e9b70ca2 694 #endif /* CTAO_CRYPT_TFM_H */
wolfSSL 0:1239e9b70ca2 695