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