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