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

Fork of MiniTLS-GPL by Donatien Garnier

Committer:
shiyilei
Date:
Fri Feb 06 06:17:33 2015 +0000
Revision:
5:95f70ebfe61f
Parent:
2:527a66d0a1a9
change some parameters in the library to meet the needs of httpbin.org

Who changed what in which revision?

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