A library for setting up Secure Socket Layer (SSL) connections and verifying remote hosts using certificates. Contains only the source files for mbed platform implementation of the library.

Dependents:   HTTPClient-SSL HTTPClient-SSL HTTPClient-SSL HTTPClient-SSL

Committer:
Mike Fiore
Date:
Mon Mar 23 16:51:07 2015 -0500
Revision:
6:cf58d49e1a86
Parent:
0:b86d15c6ba29
fix whitespace in sha512.c

Who changed what in which revision?

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