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