| 1 | /* | 
|---|
| 2 | * dlls/rsaenh/tomcrypt.h | 
|---|
| 3 | * Function prototypes, type definitions and constant definitions | 
|---|
| 4 | * for LibTomCrypt code. | 
|---|
| 5 | * | 
|---|
| 6 | * Copyright 2004 Michael Jung | 
|---|
| 7 | * Based on public domain code by Tom St Denis (tomstdenis@iahu.ca) | 
|---|
| 8 | * | 
|---|
| 9 | * This library is free software; you can redistribute it and/or | 
|---|
| 10 | * modify it under the terms of the GNU Lesser General Public | 
|---|
| 11 | * License as published by the Free Software Foundation; either | 
|---|
| 12 | * version 2.1 of the License, or (at your option) any later version. | 
|---|
| 13 | * | 
|---|
| 14 | * This library is distributed in the hope that it will be useful, | 
|---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
|---|
| 17 | * Lesser General Public License for more details. | 
|---|
| 18 | * | 
|---|
| 19 | * You should have received a copy of the GNU Lesser General Public | 
|---|
| 20 | * License along with this library; if not, write to the Free Software | 
|---|
| 21 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA | 
|---|
| 22 | */ | 
|---|
| 23 |  | 
|---|
| 24 | /* | 
|---|
| 25 | * This file contains code from the LibTomCrypt cryptographic | 
|---|
| 26 | * library written by Tom St Denis (tomstdenis@iahu.ca). LibTomCrypt | 
|---|
| 27 | * is in the public domain. The code in this file is tailored to | 
|---|
| 28 | * special requirements. Take a look at http://libtomcrypt.org for the | 
|---|
| 29 | * original version. | 
|---|
| 30 | */ | 
|---|
| 31 |  | 
|---|
| 32 | #ifndef __WINE_TOMCRYPT_H_ | 
|---|
| 33 | #define __WINE_TOMCRYPT_H_ | 
|---|
| 34 |  | 
|---|
| 35 | #include <stdio.h> | 
|---|
| 36 | #include <string.h> | 
|---|
| 37 | #include <stdlib.h> | 
|---|
| 38 | #include <limits.h> | 
|---|
| 39 |  | 
|---|
| 40 | #if defined(__WIN32OS2__) && (__IBMC__ < 400) && (__IBMCPP__ < 360) && !defined(__WATCOMC__) && !defined(__EMX__) | 
|---|
| 41 |  | 
|---|
| 42 | /* provide a better 64-bit integer storage than double */ | 
|---|
| 43 | typedef struct | 
|---|
| 44 | { | 
|---|
| 45 | int lo; | 
|---|
| 46 | int hi; | 
|---|
| 47 | } __long_long; | 
|---|
| 48 | #define __int64 __long_long | 
|---|
| 49 |  | 
|---|
| 50 | #define XOR_int64(a, b)     ((a).hi ^= (b).hi, (a).lo ^= (b).lo) | 
|---|
| 51 | #define HI_int64(a)         ((a).hi) | 
|---|
| 52 | #define LO_int64(a)         ((a).lo) | 
|---|
| 53 | #define ASSIGN_int64(a, b)  ((a).hi = 0, (a).lo = (int)(b)) | 
|---|
| 54 |  | 
|---|
| 55 | #else | 
|---|
| 56 |  | 
|---|
| 57 | #define XOR_int64(a, b)     ((a) ^= (b)) | 
|---|
| 58 | #define HI_int64(a)         ((a) >> 32) | 
|---|
| 59 | #define LO_int64(a)         ((a) & 0xFFFFFFFFUL) | 
|---|
| 60 | #define ASSIGN_int64(a, b)  ((a) = (b)) | 
|---|
| 61 |  | 
|---|
| 62 | #endif | 
|---|
| 63 |  | 
|---|
| 64 | #include "basetsd.h" | 
|---|
| 65 |  | 
|---|
| 66 | /* error codes [will be expanded in future releases] */ | 
|---|
| 67 | enum { | 
|---|
| 68 | CRYPT_OK=0,             /* Result OK */ | 
|---|
| 69 | CRYPT_ERROR,            /* Generic Error */ | 
|---|
| 70 | CRYPT_NOP,              /* Not a failure but no operation was performed */ | 
|---|
| 71 |  | 
|---|
| 72 | CRYPT_INVALID_KEYSIZE,  /* Invalid key size given */ | 
|---|
| 73 | CRYPT_INVALID_ROUNDS,   /* Invalid number of rounds */ | 
|---|
| 74 | CRYPT_FAIL_TESTVECTOR,  /* Algorithm failed test vectors */ | 
|---|
| 75 |  | 
|---|
| 76 | CRYPT_BUFFER_OVERFLOW,  /* Not enough space for output */ | 
|---|
| 77 | CRYPT_INVALID_PACKET,   /* Invalid input packet given */ | 
|---|
| 78 |  | 
|---|
| 79 | CRYPT_INVALID_PRNGSIZE, /* Invalid number of bits for a PRNG */ | 
|---|
| 80 | CRYPT_ERROR_READPRNG,   /* Could not read enough from PRNG */ | 
|---|
| 81 |  | 
|---|
| 82 | CRYPT_INVALID_CIPHER,   /* Invalid cipher specified */ | 
|---|
| 83 | CRYPT_INVALID_HASH,     /* Invalid hash specified */ | 
|---|
| 84 | CRYPT_INVALID_PRNG,     /* Invalid PRNG specified */ | 
|---|
| 85 |  | 
|---|
| 86 | CRYPT_MEM,              /* Out of memory */ | 
|---|
| 87 |  | 
|---|
| 88 | CRYPT_PK_TYPE_MISMATCH, /* Not equivalent types of PK keys */ | 
|---|
| 89 | CRYPT_PK_NOT_PRIVATE,   /* Requires a private PK key */ | 
|---|
| 90 |  | 
|---|
| 91 | CRYPT_INVALID_ARG,      /* Generic invalid argument */ | 
|---|
| 92 | CRYPT_FILE_NOTFOUND,    /* File Not Found */ | 
|---|
| 93 |  | 
|---|
| 94 | CRYPT_PK_INVALID_TYPE,  /* Invalid type of PK key */ | 
|---|
| 95 | CRYPT_PK_INVALID_SYSTEM,/* Invalid PK system specified */ | 
|---|
| 96 | CRYPT_PK_DUP,           /* Duplicate key already in key ring */ | 
|---|
| 97 | CRYPT_PK_NOT_FOUND,     /* Key not found in keyring */ | 
|---|
| 98 | CRYPT_PK_INVALID_SIZE,  /* Invalid size input for PK parameters */ | 
|---|
| 99 |  | 
|---|
| 100 | CRYPT_INVALID_PRIME_SIZE/* Invalid size of prime requested */ | 
|---|
| 101 | }; | 
|---|
| 102 |  | 
|---|
| 103 | #if defined(__WIN32OS2__) && (__IBMC__ < 400) && (__IBMCPP__ < 360) && !defined(__WATCOMC__) && !defined(__EMX__) | 
|---|
| 104 | #define CONST64(a,b) { (b), (a) } | 
|---|
| 105 | #else | 
|---|
| 106 | #define CONST64(a,b) ((((ULONG64)(a)) << 32) | (b)) | 
|---|
| 107 | #endif | 
|---|
| 108 | typedef ULONG64 ulong64; | 
|---|
| 109 |  | 
|---|
| 110 | /* this is the "32-bit at least" data type | 
|---|
| 111 | * Re-define it to suit your platform but it must be at least 32-bits | 
|---|
| 112 | */ | 
|---|
| 113 | typedef ULONG32 ulong32; | 
|---|
| 114 |  | 
|---|
| 115 | /* ---- HELPER MACROS ---- */ | 
|---|
| 116 | #define STORE32H(x, y)                                                                     \ | 
|---|
| 117 | { (y)[0] = (unsigned char)(((x)>>24)&255); (y)[1] = (unsigned char)(((x)>>16)&255);   \ | 
|---|
| 118 | (y)[2] = (unsigned char)(((x)>>8)&255); (y)[3] = (unsigned char)((x)&255); } | 
|---|
| 119 |  | 
|---|
| 120 | #define LOAD32H(x, y)                            \ | 
|---|
| 121 | { x = ((unsigned long)((y)[0] & 255)<<24) | \ | 
|---|
| 122 | ((unsigned long)((y)[1] & 255)<<16) | \ | 
|---|
| 123 | ((unsigned long)((y)[2] & 255)<<8)  | \ | 
|---|
| 124 | ((unsigned long)((y)[3] & 255)); } | 
|---|
| 125 |  | 
|---|
| 126 | #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) && !defined(INTEL_CC) | 
|---|
| 127 |  | 
|---|
| 128 | static inline unsigned ROR(unsigned word, int i) | 
|---|
| 129 | { | 
|---|
| 130 | __asm__("rorl %%cl,%0" | 
|---|
| 131 | :"=r" (word) | 
|---|
| 132 | :"0" (word),"c" (i)); | 
|---|
| 133 | return word; | 
|---|
| 134 | } | 
|---|
| 135 |  | 
|---|
| 136 | #else | 
|---|
| 137 |  | 
|---|
| 138 | /* rotates the hard way */ | 
|---|
| 139 | #define ROR(x, y) ( ((((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)((y)&31)) | \ | 
|---|
| 140 | ((unsigned long)(x)<<(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL) | 
|---|
| 141 |  | 
|---|
| 142 | #endif | 
|---|
| 143 |  | 
|---|
| 144 | #undef MIN | 
|---|
| 145 | #define MIN(x, y) ( ((x)<(y))?(x):(y) ) | 
|---|
| 146 |  | 
|---|
| 147 | #define byte(x, n) (((x) >> (8 * (n))) & 255) | 
|---|
| 148 |  | 
|---|
| 149 | typedef struct tag_rc2_key { | 
|---|
| 150 | unsigned xkey[64]; | 
|---|
| 151 | } rc2_key; | 
|---|
| 152 |  | 
|---|
| 153 | typedef struct tag_des_key { | 
|---|
| 154 | ulong32 ek[32], dk[32]; | 
|---|
| 155 | } des_key; | 
|---|
| 156 |  | 
|---|
| 157 | typedef struct tag_des3_key { | 
|---|
| 158 | ulong32 ek[3][32], dk[3][32]; | 
|---|
| 159 | } des3_key; | 
|---|
| 160 |  | 
|---|
| 161 | typedef struct tag_aes_key { | 
|---|
| 162 | ulong32 eK[64], dK[64]; | 
|---|
| 163 | int Nr; | 
|---|
| 164 | } aes_key; | 
|---|
| 165 |  | 
|---|
| 166 | int rc2_setup(const unsigned char *key, int keylen, int bits, int num_rounds, rc2_key *skey); | 
|---|
| 167 | void rc2_ecb_encrypt(const unsigned char *pt, unsigned char *ct, rc2_key *key); | 
|---|
| 168 | void rc2_ecb_decrypt(const unsigned char *ct, unsigned char *pt, rc2_key *key); | 
|---|
| 169 |  | 
|---|
| 170 | int des_setup(const unsigned char *key, int keylen, int num_rounds, des_key *skey); | 
|---|
| 171 | void des_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const des_key *key); | 
|---|
| 172 | void des_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const des_key *key); | 
|---|
| 173 |  | 
|---|
| 174 | int des3_setup(const unsigned char *key, int keylen, int num_rounds, des3_key *skey); | 
|---|
| 175 | void des3_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const des3_key *key); | 
|---|
| 176 | void des3_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const des3_key *key); | 
|---|
| 177 |  | 
|---|
| 178 | int aes_setup(const unsigned char *key, int keylen, int rounds, aes_key *skey); | 
|---|
| 179 | void aes_ecb_encrypt(const unsigned char *pt, unsigned char *ct, aes_key *skey); | 
|---|
| 180 | void aes_ecb_decrypt(const unsigned char *ct, unsigned char *pt, aes_key *skey); | 
|---|
| 181 |  | 
|---|
| 182 | typedef struct tag_md2_state { | 
|---|
| 183 | unsigned char chksum[16], X[48], buf[16]; | 
|---|
| 184 | unsigned long curlen; | 
|---|
| 185 | } md2_state; | 
|---|
| 186 |  | 
|---|
| 187 | int md2_init(md2_state * md); | 
|---|
| 188 | int md2_process(md2_state * md, const unsigned char *buf, unsigned long len); | 
|---|
| 189 | int md2_done(md2_state * md, unsigned char *hash); | 
|---|
| 190 |  | 
|---|
| 191 | struct rc4_prng { | 
|---|
| 192 | int x, y; | 
|---|
| 193 | unsigned char buf[256]; | 
|---|
| 194 | }; | 
|---|
| 195 |  | 
|---|
| 196 | typedef union Prng_state { | 
|---|
| 197 | struct rc4_prng       rc4; | 
|---|
| 198 | } prng_state; | 
|---|
| 199 |  | 
|---|
| 200 | int rc4_start(prng_state *prng); | 
|---|
| 201 | int rc4_add_entropy(const unsigned char *buf, unsigned long len, prng_state *prng); | 
|---|
| 202 | int rc4_ready(prng_state *prng); | 
|---|
| 203 | unsigned long rc4_read(unsigned char *buf, unsigned long len, prng_state *prng); | 
|---|
| 204 |  | 
|---|
| 205 | /* some default configurations. | 
|---|
| 206 | * | 
|---|
| 207 | * A "mp_digit" must be able to hold DIGIT_BIT + 1 bits | 
|---|
| 208 | * A "mp_word" must be able to hold 2*DIGIT_BIT + 1 bits | 
|---|
| 209 | * | 
|---|
| 210 | * At the very least a mp_digit must be able to hold 7 bits | 
|---|
| 211 | * [any size beyond that is ok provided it doesn't overflow the data type] | 
|---|
| 212 | */ | 
|---|
| 213 | #if defined(__WIN32OS2__) && (__IBMC__ < 400) && (__IBMCPP__ < 360) && !defined(__WATCOMC__) && !defined(__EMX__) | 
|---|
| 214 | typedef unsigned short     mp_digit; | 
|---|
| 215 | typedef unsigned long      mp_word; | 
|---|
| 216 | #define DIGIT_BIT 14 | 
|---|
| 217 | #else | 
|---|
| 218 | typedef unsigned long      mp_digit; | 
|---|
| 219 | typedef ulong64            mp_word; | 
|---|
| 220 | #define DIGIT_BIT 28 | 
|---|
| 221 | #endif | 
|---|
| 222 |  | 
|---|
| 223 | #define MP_DIGIT_BIT     DIGIT_BIT | 
|---|
| 224 | #define MP_MASK          ((((mp_digit)1)<<((mp_digit)DIGIT_BIT))-((mp_digit)1)) | 
|---|
| 225 | #define MP_DIGIT_MAX     MP_MASK | 
|---|
| 226 |  | 
|---|
| 227 | /* equalities */ | 
|---|
| 228 | #define MP_LT        -1   /* less than */ | 
|---|
| 229 | #define MP_EQ         0   /* equal to */ | 
|---|
| 230 | #define MP_GT         1   /* greater than */ | 
|---|
| 231 |  | 
|---|
| 232 | #define MP_ZPOS       0   /* positive integer */ | 
|---|
| 233 | #define MP_NEG        1   /* negative */ | 
|---|
| 234 |  | 
|---|
| 235 | #define MP_OKAY       0   /* ok result */ | 
|---|
| 236 | #define MP_MEM        -2  /* out of mem */ | 
|---|
| 237 | #define MP_VAL        -3  /* invalid input */ | 
|---|
| 238 | #define MP_RANGE      MP_VAL | 
|---|
| 239 |  | 
|---|
| 240 | #define MP_YES        1   /* yes response */ | 
|---|
| 241 | #define MP_NO         0   /* no response */ | 
|---|
| 242 |  | 
|---|
| 243 | /* Primality generation flags */ | 
|---|
| 244 | #define LTM_PRIME_BBS      0x0001 /* BBS style prime */ | 
|---|
| 245 | #define LTM_PRIME_SAFE     0x0002 /* Safe prime (p-1)/2 == prime */ | 
|---|
| 246 | #define LTM_PRIME_2MSB_OFF 0x0004 /* force 2nd MSB to 0 */ | 
|---|
| 247 | #define LTM_PRIME_2MSB_ON  0x0008 /* force 2nd MSB to 1 */ | 
|---|
| 248 |  | 
|---|
| 249 | typedef int           mp_err; | 
|---|
| 250 |  | 
|---|
| 251 | /* define this to use lower memory usage routines (exptmods mostly) */ | 
|---|
| 252 | /* #define MP_LOW_MEM */ | 
|---|
| 253 |  | 
|---|
| 254 | #if defined(__WIN32OS2__) && (__IBMC__ < 400) && (__IBMCPP__ < 360) && !defined(__WATCOMC__) && !defined(__EMX__) | 
|---|
| 255 | #define MP_PREC                 128    /* default digits of precision */ | 
|---|
| 256 | #else | 
|---|
| 257 | #define MP_PREC                 64     /* default digits of precision */ | 
|---|
| 258 | #endif | 
|---|
| 259 |  | 
|---|
| 260 | /* size of comba arrays, should be at least 2 * 2**(BITS_PER_WORD - BITS_PER_DIGIT*2) */ | 
|---|
| 261 | #define MP_WARRAY               (1 << (sizeof(mp_word) * CHAR_BIT - 2 * DIGIT_BIT + 1)) | 
|---|
| 262 |  | 
|---|
| 263 | /* the infamous mp_int structure */ | 
|---|
| 264 | typedef struct  { | 
|---|
| 265 | int used, alloc, sign; | 
|---|
| 266 | mp_digit *dp; | 
|---|
| 267 | } mp_int; | 
|---|
| 268 |  | 
|---|
| 269 | /* callback for mp_prime_random, should fill dst with random bytes and return how many read [up to len] */ | 
|---|
| 270 | typedef int ltm_prime_callback(unsigned char *dst, int len, void *dat); | 
|---|
| 271 |  | 
|---|
| 272 | #define DIGIT(m,k) ((m)->dp[(k)]) | 
|---|
| 273 |  | 
|---|
| 274 | /* error code to char* string */ | 
|---|
| 275 | char *mp_error_to_string(int code); | 
|---|
| 276 |  | 
|---|
| 277 | /* ---> init and deinit bignum functions <--- */ | 
|---|
| 278 | /* init a bignum */ | 
|---|
| 279 | int mp_init(mp_int *a); | 
|---|
| 280 |  | 
|---|
| 281 | /* free a bignum */ | 
|---|
| 282 | void mp_clear(mp_int *a); | 
|---|
| 283 |  | 
|---|
| 284 | /* init a null terminated series of arguments */ | 
|---|
| 285 | int mp_init_multi(mp_int *mp, ...); | 
|---|
| 286 |  | 
|---|
| 287 | /* clear a null terminated series of arguments */ | 
|---|
| 288 | void mp_clear_multi(mp_int *mp, ...); | 
|---|
| 289 |  | 
|---|
| 290 | /* exchange two ints */ | 
|---|
| 291 | void mp_exch(mp_int *a, mp_int *b); | 
|---|
| 292 |  | 
|---|
| 293 | /* shrink ram required for a bignum */ | 
|---|
| 294 | int mp_shrink(mp_int *a); | 
|---|
| 295 |  | 
|---|
| 296 | /* grow an int to a given size */ | 
|---|
| 297 | int mp_grow(mp_int *a, int size); | 
|---|
| 298 |  | 
|---|
| 299 | /* init to a given number of digits */ | 
|---|
| 300 | int mp_init_size(mp_int *a, int size); | 
|---|
| 301 |  | 
|---|
| 302 | /* ---> Basic Manipulations <--- */ | 
|---|
| 303 | #define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO) | 
|---|
| 304 | #define mp_iseven(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 0)) ? MP_YES : MP_NO) | 
|---|
| 305 | #define mp_isodd(a)  (((a)->used > 0 && (((a)->dp[0] & 1) == 1)) ? MP_YES : MP_NO) | 
|---|
| 306 |  | 
|---|
| 307 | /* set to zero */ | 
|---|
| 308 | void mp_zero(mp_int *a); | 
|---|
| 309 |  | 
|---|
| 310 | /* set to a digit */ | 
|---|
| 311 | void mp_set(mp_int *a, mp_digit b); | 
|---|
| 312 |  | 
|---|
| 313 | /* set a 32-bit const */ | 
|---|
| 314 | int mp_set_int(mp_int *a, unsigned long b); | 
|---|
| 315 |  | 
|---|
| 316 | /* get a 32-bit value */ | 
|---|
| 317 | unsigned long mp_get_int(const mp_int * a); | 
|---|
| 318 |  | 
|---|
| 319 | /* initialize and set a digit */ | 
|---|
| 320 | int mp_init_set (mp_int * a, mp_digit b); | 
|---|
| 321 |  | 
|---|
| 322 | /* initialize and set 32-bit value */ | 
|---|
| 323 | int mp_init_set_int (mp_int * a, unsigned long b); | 
|---|
| 324 |  | 
|---|
| 325 | /* copy, b = a */ | 
|---|
| 326 | int mp_copy(const mp_int *a, mp_int *b); | 
|---|
| 327 |  | 
|---|
| 328 | /* inits and copies, a = b */ | 
|---|
| 329 | int mp_init_copy(mp_int *a, const mp_int *b); | 
|---|
| 330 |  | 
|---|
| 331 | /* trim unused digits */ | 
|---|
| 332 | void mp_clamp(mp_int *a); | 
|---|
| 333 |  | 
|---|
| 334 | /* ---> digit manipulation <--- */ | 
|---|
| 335 |  | 
|---|
| 336 | /* right shift by "b" digits */ | 
|---|
| 337 | void mp_rshd(mp_int *a, int b); | 
|---|
| 338 |  | 
|---|
| 339 | /* left shift by "b" digits */ | 
|---|
| 340 | int mp_lshd(mp_int *a, int b); | 
|---|
| 341 |  | 
|---|
| 342 | /* c = a / 2**b */ | 
|---|
| 343 | int mp_div_2d(const mp_int *a, int b, mp_int *c, mp_int *d); | 
|---|
| 344 |  | 
|---|
| 345 | /* b = a/2 */ | 
|---|
| 346 | int mp_div_2(const mp_int *a, mp_int *b); | 
|---|
| 347 |  | 
|---|
| 348 | /* c = a * 2**b */ | 
|---|
| 349 | int mp_mul_2d(const mp_int *a, int b, mp_int *c); | 
|---|
| 350 |  | 
|---|
| 351 | /* b = a*2 */ | 
|---|
| 352 | int mp_mul_2(const mp_int *a, mp_int *b); | 
|---|
| 353 |  | 
|---|
| 354 | /* c = a mod 2**d */ | 
|---|
| 355 | int mp_mod_2d(const mp_int *a, int b, mp_int *c); | 
|---|
| 356 |  | 
|---|
| 357 | /* computes a = 2**b */ | 
|---|
| 358 | int mp_2expt(mp_int *a, int b); | 
|---|
| 359 |  | 
|---|
| 360 | /* Counts the number of lsbs which are zero before the first zero bit */ | 
|---|
| 361 | int mp_cnt_lsb(const mp_int *a); | 
|---|
| 362 |  | 
|---|
| 363 | /* I Love Earth! */ | 
|---|
| 364 |  | 
|---|
| 365 | /* makes a pseudo-random int of a given size */ | 
|---|
| 366 | int mp_rand(mp_int *a, int digits); | 
|---|
| 367 |  | 
|---|
| 368 | /* ---> binary operations <--- */ | 
|---|
| 369 | /* c = a XOR b  */ | 
|---|
| 370 | int mp_xor(mp_int *a, mp_int *b, mp_int *c); | 
|---|
| 371 |  | 
|---|
| 372 | /* c = a OR b */ | 
|---|
| 373 | int mp_or(mp_int *a, mp_int *b, mp_int *c); | 
|---|
| 374 |  | 
|---|
| 375 | /* c = a AND b */ | 
|---|
| 376 | int mp_and(mp_int *a, mp_int *b, mp_int *c); | 
|---|
| 377 |  | 
|---|
| 378 | /* ---> Basic arithmetic <--- */ | 
|---|
| 379 |  | 
|---|
| 380 | /* b = -a */ | 
|---|
| 381 | int mp_neg(mp_int *a, mp_int *b); | 
|---|
| 382 |  | 
|---|
| 383 | /* b = |a| */ | 
|---|
| 384 | int mp_abs(const mp_int *a, mp_int *b); | 
|---|
| 385 |  | 
|---|
| 386 | /* compare a to b */ | 
|---|
| 387 | int mp_cmp(const mp_int *a, const mp_int *b); | 
|---|
| 388 |  | 
|---|
| 389 | /* compare |a| to |b| */ | 
|---|
| 390 | int mp_cmp_mag(const mp_int *a, const mp_int *b); | 
|---|
| 391 |  | 
|---|
| 392 | /* c = a + b */ | 
|---|
| 393 | int mp_add(mp_int *a, mp_int *b, mp_int *c); | 
|---|
| 394 |  | 
|---|
| 395 | /* c = a - b */ | 
|---|
| 396 | int mp_sub(mp_int *a, mp_int *b, mp_int *c); | 
|---|
| 397 |  | 
|---|
| 398 | /* c = a * b */ | 
|---|
| 399 | int mp_mul(const mp_int *a, const mp_int *b, mp_int *c); | 
|---|
| 400 |  | 
|---|
| 401 | /* b = a*a  */ | 
|---|
| 402 | int mp_sqr(const mp_int *a, mp_int *b); | 
|---|
| 403 |  | 
|---|
| 404 | /* a/b => cb + d == a */ | 
|---|
| 405 | int mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d); | 
|---|
| 406 |  | 
|---|
| 407 | /* c = a mod b, 0 <= c < b  */ | 
|---|
| 408 | int mp_mod(const mp_int *a, mp_int *b, mp_int *c); | 
|---|
| 409 |  | 
|---|
| 410 | /* ---> single digit functions <--- */ | 
|---|
| 411 |  | 
|---|
| 412 | /* compare against a single digit */ | 
|---|
| 413 | int mp_cmp_d(const mp_int *a, mp_digit b); | 
|---|
| 414 |  | 
|---|
| 415 | /* c = a + b */ | 
|---|
| 416 | int mp_add_d(mp_int *a, mp_digit b, mp_int *c); | 
|---|
| 417 |  | 
|---|
| 418 | /* c = a - b */ | 
|---|
| 419 | int mp_sub_d(mp_int *a, mp_digit b, mp_int *c); | 
|---|
| 420 |  | 
|---|
| 421 | /* c = a * b */ | 
|---|
| 422 | int mp_mul_d(const mp_int *a, mp_digit b, mp_int *c); | 
|---|
| 423 |  | 
|---|
| 424 | /* a/b => cb + d == a */ | 
|---|
| 425 | int mp_div_d(const mp_int *a, mp_digit b, mp_int *c, mp_digit *d); | 
|---|
| 426 |  | 
|---|
| 427 | /* a/3 => 3c + d == a */ | 
|---|
| 428 | int mp_div_3(mp_int *a, mp_int *c, mp_digit *d); | 
|---|
| 429 |  | 
|---|
| 430 | /* c = a**b */ | 
|---|
| 431 | int mp_expt_d(mp_int *a, mp_digit b, mp_int *c); | 
|---|
| 432 |  | 
|---|
| 433 | /* c = a mod b, 0 <= c < b  */ | 
|---|
| 434 | int mp_mod_d(const mp_int *a, mp_digit b, mp_digit *c); | 
|---|
| 435 |  | 
|---|
| 436 | /* ---> number theory <--- */ | 
|---|
| 437 |  | 
|---|
| 438 | /* d = a + b (mod c) */ | 
|---|
| 439 | int mp_addmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d); | 
|---|
| 440 |  | 
|---|
| 441 | /* d = a - b (mod c) */ | 
|---|
| 442 | int mp_submod(mp_int *a, mp_int *b, mp_int *c, mp_int *d); | 
|---|
| 443 |  | 
|---|
| 444 | /* d = a * b (mod c) */ | 
|---|
| 445 | int mp_mulmod(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d); | 
|---|
| 446 |  | 
|---|
| 447 | /* c = a * a (mod b) */ | 
|---|
| 448 | int mp_sqrmod(const mp_int *a, mp_int *b, mp_int *c); | 
|---|
| 449 |  | 
|---|
| 450 | /* c = 1/a (mod b) */ | 
|---|
| 451 | int mp_invmod(const mp_int *a, mp_int *b, mp_int *c); | 
|---|
| 452 |  | 
|---|
| 453 | /* c = (a, b) */ | 
|---|
| 454 | int mp_gcd(const mp_int *a, const mp_int *b, mp_int *c); | 
|---|
| 455 |  | 
|---|
| 456 | /* produces value such that U1*a + U2*b = U3 */ | 
|---|
| 457 | int mp_exteuclid(mp_int *a, mp_int *b, mp_int *U1, mp_int *U2, mp_int *U3); | 
|---|
| 458 |  | 
|---|
| 459 | /* c = [a, b] or (a*b)/(a, b) */ | 
|---|
| 460 | int mp_lcm(const mp_int *a, const mp_int *b, mp_int *c); | 
|---|
| 461 |  | 
|---|
| 462 | /* finds one of the b'th root of a, such that |c|**b <= |a| | 
|---|
| 463 | * | 
|---|
| 464 | * returns error if a < 0 and b is even | 
|---|
| 465 | */ | 
|---|
| 466 | int mp_n_root(mp_int *a, mp_digit b, mp_int *c); | 
|---|
| 467 |  | 
|---|
| 468 | /* special sqrt algo */ | 
|---|
| 469 | int mp_sqrt(mp_int *arg, mp_int *ret); | 
|---|
| 470 |  | 
|---|
| 471 | /* is number a square? */ | 
|---|
| 472 | int mp_is_square(mp_int *arg, int *ret); | 
|---|
| 473 |  | 
|---|
| 474 | /* computes the jacobi c = (a | n) (or Legendre if b is prime)  */ | 
|---|
| 475 | int mp_jacobi(mp_int *a, mp_int *n, int *c); | 
|---|
| 476 |  | 
|---|
| 477 | /* used to setup the Barrett reduction for a given modulus b */ | 
|---|
| 478 | int mp_reduce_setup(mp_int *a, const mp_int *b); | 
|---|
| 479 |  | 
|---|
| 480 | /* Barrett Reduction, computes a (mod b) with a precomputed value c | 
|---|
| 481 | * | 
|---|
| 482 | * Assumes that 0 < a <= b*b, note if 0 > a > -(b*b) then you can merely | 
|---|
| 483 | * compute the reduction as -1 * mp_reduce(mp_abs(a)) [pseudo code]. | 
|---|
| 484 | */ | 
|---|
| 485 | int mp_reduce(mp_int *a, const mp_int *b, const mp_int *c); | 
|---|
| 486 |  | 
|---|
| 487 | /* setups the montgomery reduction */ | 
|---|
| 488 | int mp_montgomery_setup(const mp_int *a, mp_digit *mp); | 
|---|
| 489 |  | 
|---|
| 490 | /* computes a = B**n mod b without division or multiplication useful for | 
|---|
| 491 | * normalizing numbers in a Montgomery system. | 
|---|
| 492 | */ | 
|---|
| 493 | int mp_montgomery_calc_normalization(mp_int *a, const mp_int *b); | 
|---|
| 494 |  | 
|---|
| 495 | /* computes x/R == x (mod N) via Montgomery Reduction */ | 
|---|
| 496 | int mp_montgomery_reduce(mp_int *a, const mp_int *m, mp_digit mp); | 
|---|
| 497 |  | 
|---|
| 498 | /* returns 1 if a is a valid DR modulus */ | 
|---|
| 499 | int mp_dr_is_modulus(mp_int *a); | 
|---|
| 500 |  | 
|---|
| 501 | /* sets the value of "d" required for mp_dr_reduce */ | 
|---|
| 502 | void mp_dr_setup(const mp_int *a, mp_digit *d); | 
|---|
| 503 |  | 
|---|
| 504 | /* reduces a modulo b using the Diminished Radix method */ | 
|---|
| 505 | int mp_dr_reduce(mp_int *a, const mp_int *b, mp_digit mp); | 
|---|
| 506 |  | 
|---|
| 507 | /* returns true if a can be reduced with mp_reduce_2k */ | 
|---|
| 508 | int mp_reduce_is_2k(mp_int *a); | 
|---|
| 509 |  | 
|---|
| 510 | /* determines k value for 2k reduction */ | 
|---|
| 511 | int mp_reduce_2k_setup(const mp_int *a, mp_digit *d); | 
|---|
| 512 |  | 
|---|
| 513 | /* reduces a modulo b where b is of the form 2**p - k [0 <= a] */ | 
|---|
| 514 | int mp_reduce_2k(mp_int *a, const mp_int *n, mp_digit d); | 
|---|
| 515 |  | 
|---|
| 516 | /* d = a**b (mod c) */ | 
|---|
| 517 | int mp_exptmod(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d); | 
|---|
| 518 |  | 
|---|
| 519 | /* ---> Primes <--- */ | 
|---|
| 520 |  | 
|---|
| 521 | /* number of primes */ | 
|---|
| 522 | #define PRIME_SIZE      256 | 
|---|
| 523 |  | 
|---|
| 524 | /* result=1 if a is divisible by one of the first PRIME_SIZE primes */ | 
|---|
| 525 | int mp_prime_is_divisible(const mp_int *a, int *result); | 
|---|
| 526 |  | 
|---|
| 527 | /* performs one Fermat test of "a" using base "b". | 
|---|
| 528 | * Sets result to 0 if composite or 1 if probable prime | 
|---|
| 529 | */ | 
|---|
| 530 | int mp_prime_fermat(mp_int *a, mp_int *b, int *result); | 
|---|
| 531 |  | 
|---|
| 532 | /* performs one Miller-Rabin test of "a" using base "b". | 
|---|
| 533 | * Sets result to 0 if composite or 1 if probable prime | 
|---|
| 534 | */ | 
|---|
| 535 | int mp_prime_miller_rabin(mp_int *a, const mp_int *b, int *result); | 
|---|
| 536 |  | 
|---|
| 537 | /* This gives [for a given bit size] the number of trials required | 
|---|
| 538 | * such that Miller-Rabin gives a prob of failure lower than 2^-96 | 
|---|
| 539 | */ | 
|---|
| 540 | int mp_prime_rabin_miller_trials(int size); | 
|---|
| 541 |  | 
|---|
| 542 | /* performs t rounds of Miller-Rabin on "a" using the first | 
|---|
| 543 | * t prime bases.  Also performs an initial sieve of trial | 
|---|
| 544 | * division.  Determines if "a" is prime with probability | 
|---|
| 545 | * of error no more than (1/4)**t. | 
|---|
| 546 | * | 
|---|
| 547 | * Sets result to 1 if probably prime, 0 otherwise | 
|---|
| 548 | */ | 
|---|
| 549 | int mp_prime_is_prime(mp_int *a, int t, int *result); | 
|---|
| 550 |  | 
|---|
| 551 | /* finds the next prime after the number "a" using "t" trials | 
|---|
| 552 | * of Miller-Rabin. | 
|---|
| 553 | * | 
|---|
| 554 | * bbs_style = 1 means the prime must be congruent to 3 mod 4 | 
|---|
| 555 | */ | 
|---|
| 556 | int mp_prime_next_prime(mp_int *a, int t, int bbs_style); | 
|---|
| 557 |  | 
|---|
| 558 | /* makes a truly random prime of a given size (bytes), | 
|---|
| 559 | * call with bbs = 1 if you want it to be congruent to 3 mod 4 | 
|---|
| 560 | * | 
|---|
| 561 | * You have to supply a callback which fills in a buffer with random bytes.  "dat" is a parameter you can | 
|---|
| 562 | * have passed to the callback (e.g. a state or something).  This function doesn't use "dat" itself | 
|---|
| 563 | * so it can be NULL | 
|---|
| 564 | * | 
|---|
| 565 | * The prime generated will be larger than 2^(8*size). | 
|---|
| 566 | */ | 
|---|
| 567 | #define mp_prime_random(a, t, size, bbs, cb, dat) mp_prime_random_ex(a, t, ((size) * 8) + 1, (bbs==1)?LTM_PRIME_BBS:0, cb, dat) | 
|---|
| 568 |  | 
|---|
| 569 | /* makes a truly random prime of a given size (bits), | 
|---|
| 570 | * | 
|---|
| 571 | * Flags are as follows: | 
|---|
| 572 | * | 
|---|
| 573 | *   LTM_PRIME_BBS      - make prime congruent to 3 mod 4 | 
|---|
| 574 | *   LTM_PRIME_SAFE     - make sure (p-1)/2 is prime as well (implies LTM_PRIME_BBS) | 
|---|
| 575 | *   LTM_PRIME_2MSB_OFF - make the 2nd highest bit zero | 
|---|
| 576 | *   LTM_PRIME_2MSB_ON  - make the 2nd highest bit one | 
|---|
| 577 | * | 
|---|
| 578 | * You have to supply a callback which fills in a buffer with random bytes.  "dat" is a parameter you can | 
|---|
| 579 | * have passed to the callback (e.g. a state or something).  This function doesn't use "dat" itself | 
|---|
| 580 | * so it can be NULL | 
|---|
| 581 | * | 
|---|
| 582 | */ | 
|---|
| 583 | int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback cb, void *dat); | 
|---|
| 584 |  | 
|---|
| 585 | /* ---> radix conversion <--- */ | 
|---|
| 586 | int mp_count_bits(const mp_int *a); | 
|---|
| 587 |  | 
|---|
| 588 | int mp_unsigned_bin_size(const mp_int *a); | 
|---|
| 589 | int mp_read_unsigned_bin(mp_int *a, const unsigned char *b, int c); | 
|---|
| 590 | int mp_to_unsigned_bin(const mp_int *a, unsigned char *b); | 
|---|
| 591 |  | 
|---|
| 592 | int mp_signed_bin_size(const mp_int *a); | 
|---|
| 593 | int mp_read_signed_bin(mp_int *a, unsigned char *b, int c); | 
|---|
| 594 | int mp_to_signed_bin(mp_int *a, unsigned char *b); | 
|---|
| 595 |  | 
|---|
| 596 | int mp_read_radix(mp_int *a, char *str, int radix); | 
|---|
| 597 | int mp_toradix(mp_int *a, char *str, int radix); | 
|---|
| 598 | int mp_toradix_n(mp_int * a, char *str, int radix, int maxlen); | 
|---|
| 599 | int mp_radix_size(mp_int *a, int radix, int *size); | 
|---|
| 600 |  | 
|---|
| 601 | int mp_fread(mp_int *a, int radix, FILE *stream); | 
|---|
| 602 | int mp_fwrite(mp_int *a, int radix, FILE *stream); | 
|---|
| 603 |  | 
|---|
| 604 | #define mp_read_raw(mp, str, len) mp_read_signed_bin((mp), (str), (len)) | 
|---|
| 605 | #define mp_raw_size(mp)           mp_signed_bin_size(mp) | 
|---|
| 606 | #define mp_toraw(mp, str)         mp_to_signed_bin((mp), (str)) | 
|---|
| 607 | #define mp_read_mag(mp, str, len) mp_read_unsigned_bin((mp), (str), (len)) | 
|---|
| 608 | #define mp_mag_size(mp)           mp_unsigned_bin_size(mp) | 
|---|
| 609 | #define mp_tomag(mp, str)         mp_to_unsigned_bin((mp), (str)) | 
|---|
| 610 |  | 
|---|
| 611 | #define mp_tobinary(M, S)  mp_toradix((M), (S), 2) | 
|---|
| 612 | #define mp_tooctal(M, S)   mp_toradix((M), (S), 8) | 
|---|
| 613 | #define mp_todecimal(M, S) mp_toradix((M), (S), 10) | 
|---|
| 614 | #define mp_tohex(M, S)     mp_toradix((M), (S), 16) | 
|---|
| 615 |  | 
|---|
| 616 | extern const char *mp_s_rmap; | 
|---|
| 617 |  | 
|---|
| 618 | #define PK_PRIVATE            0        /* PK private keys */ | 
|---|
| 619 | #define PK_PUBLIC             1        /* PK public keys */ | 
|---|
| 620 |  | 
|---|
| 621 | /* Min and Max RSA key sizes (in bits) */ | 
|---|
| 622 | #define MIN_RSA_SIZE 384 | 
|---|
| 623 | #define MAX_RSA_SIZE 16384 | 
|---|
| 624 |  | 
|---|
| 625 | typedef struct Rsa_key { | 
|---|
| 626 | int type; | 
|---|
| 627 | mp_int e, d, N, p, q, qP, dP, dQ; | 
|---|
| 628 | } rsa_key; | 
|---|
| 629 |  | 
|---|
| 630 | int rsa_make_key(int size, long e, rsa_key *key); | 
|---|
| 631 |  | 
|---|
| 632 | int rsa_exptmod(const unsigned char *in,   unsigned long inlen, | 
|---|
| 633 | unsigned char *out,  unsigned long *outlen, int which, | 
|---|
| 634 | rsa_key *key); | 
|---|
| 635 |  | 
|---|
| 636 | void rsa_free(rsa_key *key); | 
|---|
| 637 |  | 
|---|
| 638 | #endif /* __WINE_TOMCRYPT_H_ */ | 
|---|