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