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