CyaSSL is an SSL library for devices like mbed.

Dependents:   cyassl-client Sync

Committer:
toddouska
Date:
Sat Feb 05 01:09:17 2011 +0000
Revision:
0:5045d2638c29
Beta Version

Who changed what in which revision?

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