change some parameters in the library to meet the needs of the website httpbin.org

Fork of MiniTLS-GPL by Donatien Garnier

Committer:
MiniTLS
Date:
Fri Jun 06 10:49:02 2014 +0000
Revision:
0:35aa5be3b78d
Initial commit

Who changed what in which revision?

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