Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygentomcrypt.h
Go to the documentation of this file.
00001 /* 00002 * dlls/rsaenh/tomcrypt.h 00003 * Function prototypes, type definitions and constant definitions 00004 * for LibTomCrypt code. 00005 * 00006 * Copyright 2004 Michael Jung 00007 * Based on public domain code by Tom St Denis (tomstdenis@iahu.ca) 00008 * 00009 * This library is free software; you can redistribute it and/or 00010 * modify it under the terms of the GNU Lesser General Public 00011 * License as published by the Free Software Foundation; either 00012 * version 2.1 of the License, or (at your option) any later version. 00013 * 00014 * This library is distributed in the hope that it will be useful, 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00017 * Lesser General Public License for more details. 00018 * 00019 * You should have received a copy of the GNU Lesser General Public 00020 * License along with this library; if not, write to the Free Software 00021 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA 00022 */ 00023 00024 /* 00025 * This file contains code from the LibTomCrypt cryptographic 00026 * library written by Tom St Denis (tomstdenis@iahu.ca). LibTomCrypt 00027 * is in the public domain. The code in this file is tailored to 00028 * special requirements. Take a look at http://libtomcrypt.org for the 00029 * original version. 00030 */ 00031 00032 #ifndef __WINE_TOMCRYPT_H_ 00033 #define __WINE_TOMCRYPT_H_ 00034 00035 #include <stdio.h> 00036 #include <string.h> 00037 #include <stdlib.h> 00038 #include <limits.h> 00039 #include "basetsd.h" 00040 00041 /* error codes [will be expanded in future releases] */ 00042 enum { 00043 CRYPT_OK=0, /* Result OK */ 00044 CRYPT_ERROR, /* Generic Error */ 00045 CRYPT_NOP, /* Not a failure but no operation was performed */ 00046 00047 CRYPT_INVALID_KEYSIZE, /* Invalid key size given */ 00048 CRYPT_INVALID_ROUNDS, /* Invalid number of rounds */ 00049 CRYPT_FAIL_TESTVECTOR, /* Algorithm failed test vectors */ 00050 00051 CRYPT_BUFFER_OVERFLOW, /* Not enough space for output */ 00052 CRYPT_INVALID_PACKET, /* Invalid input packet given */ 00053 00054 CRYPT_INVALID_PRNGSIZE, /* Invalid number of bits for a PRNG */ 00055 CRYPT_ERROR_READPRNG, /* Could not read enough from PRNG */ 00056 00057 CRYPT_INVALID_CIPHER, /* Invalid cipher specified */ 00058 CRYPT_INVALID_HASH, /* Invalid hash specified */ 00059 CRYPT_INVALID_PRNG, /* Invalid PRNG specified */ 00060 00061 CRYPT_MEM, /* Out of memory */ 00062 00063 CRYPT_PK_TYPE_MISMATCH, /* Not equivalent types of PK keys */ 00064 CRYPT_PK_NOT_PRIVATE, /* Requires a private PK key */ 00065 00066 CRYPT_INVALID_ARG, /* Generic invalid argument */ 00067 CRYPT_FILE_NOTFOUND, /* File Not Found */ 00068 00069 CRYPT_PK_INVALID_TYPE, /* Invalid type of PK key */ 00070 CRYPT_PK_INVALID_SYSTEM,/* Invalid PK system specified */ 00071 CRYPT_PK_DUP, /* Duplicate key already in key ring */ 00072 CRYPT_PK_NOT_FOUND, /* Key not found in keyring */ 00073 CRYPT_PK_INVALID_SIZE, /* Invalid size input for PK parameters */ 00074 00075 CRYPT_INVALID_PRIME_SIZE/* Invalid size of prime requested */ 00076 }; 00077 00078 #define CONST64(a,b) ((((ULONG64)(a)) << 32) | (b)) 00079 typedef ULONG64 ulong64; 00080 00081 /* this is the "32-bit at least" data type 00082 * Re-define it to suit your platform but it must be at least 32-bits 00083 */ 00084 typedef ULONG32 ulong32; 00085 00086 /* ---- HELPER MACROS ---- */ 00087 #define STORE32H(x, y) \ 00088 { (y)[0] = (unsigned char)(((x)>>24)&255); (y)[1] = (unsigned char)(((x)>>16)&255); \ 00089 (y)[2] = (unsigned char)(((x)>>8)&255); (y)[3] = (unsigned char)((x)&255); } 00090 00091 #define LOAD32H(x, y) \ 00092 { x = ((unsigned long)((y)[0] & 255)<<24) | \ 00093 ((unsigned long)((y)[1] & 255)<<16) | \ 00094 ((unsigned long)((y)[2] & 255)<<8) | \ 00095 ((unsigned long)((y)[3] & 255)); } 00096 00097 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) && !defined(INTEL_CC) 00098 00099 static inline unsigned ROR(unsigned word, int i) 00100 { 00101 __asm__("rorl %%cl,%0" 00102 :"=r" (word) 00103 :"0" (word),"c" (i)); 00104 return word; 00105 } 00106 00107 #else 00108 00109 /* rotates the hard way */ 00110 #define ROR(x, y) ( ((((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)((y)&31)) | \ 00111 ((unsigned long)(x)<<(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL) 00112 00113 #endif 00114 00115 #undef MIN 00116 #define MIN(x, y) ( ((x)<(y))?(x):(y) ) 00117 00118 #define byte(x, n) (((x) >> (8 * (n))) & 255) 00119 00120 typedef struct tag_rc2_key { 00121 unsigned xkey[64]; 00122 } rc2_key; 00123 00124 typedef struct tag_des_key { 00125 ulong32 ek[32], dk[32]; 00126 } des_key; 00127 00128 typedef struct tag_des3_key { 00129 ulong32 ek[3][32], dk[3][32]; 00130 } des3_key; 00131 00132 typedef struct tag_aes_key { 00133 ulong32 eK[64], dK[64]; 00134 int Nr; 00135 } aes_key; 00136 00137 int rc2_setup(const unsigned char *key, int keylen, int bits, int num_rounds, rc2_key *skey); 00138 void rc2_ecb_encrypt(const unsigned char *pt, unsigned char *ct, rc2_key *key); 00139 void rc2_ecb_decrypt(const unsigned char *ct, unsigned char *pt, rc2_key *key); 00140 00141 int des_setup(const unsigned char *key, int keylen, int num_rounds, des_key *skey); 00142 void des_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const des_key *key); 00143 void des_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const des_key *key); 00144 00145 int des3_setup(const unsigned char *key, int keylen, int num_rounds, des3_key *skey); 00146 void des3_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const des3_key *key); 00147 void des3_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const des3_key *key); 00148 00149 int aes_setup(const unsigned char *key, int keylen, int rounds, aes_key *skey); 00150 void aes_ecb_encrypt(const unsigned char *pt, unsigned char *ct, aes_key *skey); 00151 void aes_ecb_decrypt(const unsigned char *ct, unsigned char *pt, aes_key *skey); 00152 00153 typedef struct tag_md2_state { 00154 unsigned char chksum[16], X[48], buf[16]; 00155 unsigned long curlen; 00156 } md2_state; 00157 00158 int md2_init(md2_state * md); 00159 int md2_process(md2_state * md, const unsigned char *buf, unsigned long len); 00160 int md2_done(md2_state * md, unsigned char *hash); 00161 00162 struct rc4_prng { 00163 int x, y; 00164 unsigned char buf[256]; 00165 }; 00166 00167 typedef union Prng_state { 00168 struct rc4_prng rc4; 00169 } prng_state; 00170 00171 int rc4_start(prng_state *prng); 00172 int rc4_add_entropy(const unsigned char *buf, unsigned long len, prng_state *prng); 00173 int rc4_ready(prng_state *prng); 00174 unsigned long rc4_read(unsigned char *buf, unsigned long len, prng_state *prng); 00175 00176 /* some default configurations. 00177 * 00178 * A "mp_digit" must be able to hold DIGIT_BIT + 1 bits 00179 * A "mp_word" must be able to hold 2*DIGIT_BIT + 1 bits 00180 * 00181 * At the very least a mp_digit must be able to hold 7 bits 00182 * [any size beyond that is ok provided it doesn't overflow the data type] 00183 */ 00184 typedef unsigned long mp_digit; 00185 typedef ulong64 mp_word; 00186 #define DIGIT_BIT 28 00187 00188 #define MP_DIGIT_BIT DIGIT_BIT 00189 #define MP_MASK ((((mp_digit)1)<<((mp_digit)DIGIT_BIT))-((mp_digit)1)) 00190 #define MP_DIGIT_MAX MP_MASK 00191 00192 /* equalities */ 00193 #define MP_LT -1 /* less than */ 00194 #define MP_EQ 0 /* equal to */ 00195 #define MP_GT 1 /* greater than */ 00196 00197 #define MP_ZPOS 0 /* positive integer */ 00198 #define MP_NEG 1 /* negative */ 00199 00200 #define MP_OKAY 0 /* ok result */ 00201 #define MP_MEM -2 /* out of mem */ 00202 #define MP_VAL -3 /* invalid input */ 00203 #define MP_RANGE MP_VAL 00204 00205 #define MP_YES 1 /* yes response */ 00206 #define MP_NO 0 /* no response */ 00207 00208 /* Primality generation flags */ 00209 #define LTM_PRIME_BBS 0x0001 /* BBS style prime */ 00210 #define LTM_PRIME_SAFE 0x0002 /* Safe prime (p-1)/2 == prime */ 00211 #define LTM_PRIME_2MSB_OFF 0x0004 /* force 2nd MSB to 0 */ 00212 #define LTM_PRIME_2MSB_ON 0x0008 /* force 2nd MSB to 1 */ 00213 00214 typedef int mp_err; 00215 00216 /* define this to use lower memory usage routines (exptmods mostly) */ 00217 /* #define MP_LOW_MEM */ 00218 00219 #define MP_PREC 64 /* default digits of precision */ 00220 00221 /* size of comba arrays, should be at least 2 * 2**(BITS_PER_WORD - BITS_PER_DIGIT*2) */ 00222 #define MP_WARRAY (1 << (sizeof(mp_word) * CHAR_BIT - 2 * DIGIT_BIT + 1)) 00223 00224 /* the infamous mp_int structure */ 00225 typedef struct { 00226 int used, alloc, sign; 00227 mp_digit *dp; 00228 } mp_int; 00229 00230 /* callback for mp_prime_random, should fill dst with random bytes and return how many read [up to len] */ 00231 typedef int ltm_prime_callback(unsigned char *dst, int len, void *dat); 00232 00233 #define DIGIT(m,k) ((m)->dp[(k)]) 00234 00235 /* error code to char* string */ 00236 char *mp_error_to_string(int code); 00237 00238 /* init a null terminated series of arguments */ 00239 int mp_init_multi(mp_int *mp, ...); 00240 00241 /* clear a null terminated series of arguments */ 00242 void mp_clear_multi(mp_int *mp, ...); 00243 00244 /* shrink ram required for a bignum */ 00245 int mp_shrink(mp_int *a); 00246 00247 /* ---> Basic Manipulations <--- */ 00248 #define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO) 00249 #define mp_iseven(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 0)) ? MP_YES : MP_NO) 00250 #define mp_isodd(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 1)) ? MP_YES : MP_NO) 00251 00252 /* set a 32-bit const */ 00253 int mp_set_int(mp_int *a, unsigned long b); 00254 00255 /* get a 32-bit value */ 00256 unsigned long mp_get_int(const mp_int * a); 00257 00258 /* initialize and set a digit */ 00259 int mp_init_set (mp_int * a, mp_digit b); 00260 00261 /* initialize and set 32-bit value */ 00262 int mp_init_set_int (mp_int * a, unsigned long b); 00263 00264 /* copy, b = a */ 00265 int mp_copy(const mp_int *a, mp_int *b); 00266 00267 /* inits and copies, a = b */ 00268 int mp_init_copy(mp_int *a, const mp_int *b); 00269 00270 /* ---> digit manipulation <--- */ 00271 00272 /* I Love Earth! */ 00273 00274 /* makes a pseudo-random int of a given size */ 00275 int mp_rand(mp_int *a, int digits); 00276 00277 /* ---> binary operations <--- */ 00278 /* c = a XOR b */ 00279 int mp_xor(mp_int *a, mp_int *b, mp_int *c); 00280 00281 /* c = a OR b */ 00282 int mp_or(mp_int *a, mp_int *b, mp_int *c); 00283 00284 /* c = a AND b */ 00285 int mp_and(mp_int *a, mp_int *b, mp_int *c); 00286 00287 /* ---> Basic arithmetic <--- */ 00288 00289 /* b = -a */ 00290 int mp_neg(mp_int *a, mp_int *b); 00291 00292 /* compare a to b */ 00293 int mp_cmp(const mp_int *a, const mp_int *b); 00294 00295 /* c = a + b */ 00296 int mp_add(mp_int *a, mp_int *b, mp_int *c); 00297 00298 /* c = a - b */ 00299 int mp_sub(mp_int *a, mp_int *b, mp_int *c); 00300 00301 /* c = a * b */ 00302 int mp_mul(const mp_int *a, const mp_int *b, mp_int *c); 00303 00304 /* c = a mod b, 0 <= c < b */ 00305 int mp_mod(const mp_int *a, mp_int *b, mp_int *c); 00306 00307 /* ---> single digit functions <--- */ 00308 00309 /* compare against a single digit */ 00310 int mp_cmp_d(const mp_int *a, mp_digit b); 00311 00312 /* c = a - b */ 00313 int mp_sub_d(mp_int *a, mp_digit b, mp_int *c); 00314 00315 /* a/3 => 3c + d == a */ 00316 int mp_div_3(mp_int *a, mp_int *c, mp_digit *d); 00317 00318 /* c = a**b */ 00319 int mp_expt_d(mp_int *a, mp_digit b, mp_int *c); 00320 00321 /* ---> number theory <--- */ 00322 00323 /* d = a + b (mod c) */ 00324 int mp_addmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d); 00325 00326 /* d = a - b (mod c) */ 00327 int mp_submod(mp_int *a, mp_int *b, mp_int *c, mp_int *d); 00328 00329 /* d = a * b (mod c) */ 00330 int mp_mulmod(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d); 00331 00332 /* c = 1/a (mod b) */ 00333 int mp_invmod(const mp_int *a, mp_int *b, mp_int *c); 00334 00335 /* c = (a, b) */ 00336 int mp_gcd(const mp_int *a, const mp_int *b, mp_int *c); 00337 00338 /* produces value such that U1*a + U2*b = U3 */ 00339 int mp_exteuclid(mp_int *a, mp_int *b, mp_int *U1, mp_int *U2, mp_int *U3); 00340 00341 /* c = [a, b] or (a*b)/(a, b) */ 00342 int mp_lcm(const mp_int *a, const mp_int *b, mp_int *c); 00343 00344 /* finds one of the b'th root of a, such that |c|**b <= |a| 00345 * 00346 * returns error if a < 0 and b is even 00347 */ 00348 int mp_n_root(mp_int *a, mp_digit b, mp_int *c); 00349 00350 /* special sqrt algo */ 00351 int mp_sqrt(mp_int *arg, mp_int *ret); 00352 00353 /* is number a square? */ 00354 int mp_is_square(mp_int *arg, int *ret); 00355 00356 /* computes the jacobi c = (a | n) (or Legendre if b is prime) */ 00357 int mp_jacobi(mp_int *a, mp_int *n, int *c); 00358 00359 /* returns 1 if a is a valid DR modulus */ 00360 int mp_dr_is_modulus(mp_int *a); 00361 00362 /* returns true if a can be reduced with mp_reduce_2k */ 00363 int mp_reduce_is_2k(mp_int *a); 00364 00365 /* d = a**b (mod c) */ 00366 int mp_exptmod(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d); 00367 00368 /* ---> Primes <--- */ 00369 00370 /* number of primes */ 00371 #define PRIME_SIZE 256 00372 00373 /* performs one Fermat test of "a" using base "b". 00374 * Sets result to 0 if composite or 1 if probable prime 00375 */ 00376 int mp_prime_fermat(mp_int *a, mp_int *b, int *result); 00377 00378 /* This gives [for a given bit size] the number of trials required 00379 * such that Miller-Rabin gives a prob of failure lower than 2^-96 00380 */ 00381 int mp_prime_rabin_miller_trials(int size); 00382 00383 /* finds the next prime after the number "a" using "t" trials 00384 * of Miller-Rabin. 00385 * 00386 * bbs_style = 1 means the prime must be congruent to 3 mod 4 00387 */ 00388 int mp_prime_next_prime(mp_int *a, int t, int bbs_style); 00389 00390 /* makes a truly random prime of a given size (bytes), 00391 * call with bbs = 1 if you want it to be congruent to 3 mod 4 00392 * 00393 * You have to supply a callback which fills in a buffer with random bytes. "dat" is a parameter you can 00394 * have passed to the callback (e.g. a state or something). This function doesn't use "dat" itself 00395 * so it can be NULL 00396 * 00397 * The prime generated will be larger than 2^(8*size). 00398 */ 00399 #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) 00400 00401 /* makes a truly random prime of a given size (bits), 00402 * 00403 * Flags are as follows: 00404 * 00405 * LTM_PRIME_BBS - make prime congruent to 3 mod 4 00406 * LTM_PRIME_SAFE - make sure (p-1)/2 is prime as well (implies LTM_PRIME_BBS) 00407 * LTM_PRIME_2MSB_OFF - make the 2nd highest bit zero 00408 * LTM_PRIME_2MSB_ON - make the 2nd highest bit one 00409 * 00410 * You have to supply a callback which fills in a buffer with random bytes. "dat" is a parameter you can 00411 * have passed to the callback (e.g. a state or something). This function doesn't use "dat" itself 00412 * so it can be NULL 00413 * 00414 */ 00415 int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback cb, void *dat); 00416 00417 /* ---> radix conversion <--- */ 00418 int mp_count_bits(const mp_int *a); 00419 00420 int mp_unsigned_bin_size(const mp_int *a); 00421 int mp_read_unsigned_bin(mp_int *a, const unsigned char *b, int c); 00422 int mp_to_unsigned_bin(const mp_int *a, unsigned char *b); 00423 00424 int mp_read_signed_bin(mp_int *a, unsigned char *b, int c); 00425 int mp_to_signed_bin(mp_int *a, unsigned char *b); 00426 00427 int mp_read_radix(mp_int *a, char *str, int radix); 00428 int mp_toradix(mp_int *a, char *str, int radix); 00429 int mp_toradix_n(mp_int * a, char *str, int radix, int maxlen); 00430 int mp_radix_size(mp_int *a, int radix, int *size); 00431 00432 int mp_fread(mp_int *a, int radix, FILE *stream); 00433 int mp_fwrite(mp_int *a, int radix, FILE *stream); 00434 00435 #define mp_read_raw(mp, str, len) mp_read_signed_bin((mp), (str), (len)) 00436 #define mp_raw_size(mp) mp_signed_bin_size(mp) 00437 #define mp_toraw(mp, str) mp_to_signed_bin((mp), (str)) 00438 #define mp_read_mag(mp, str, len) mp_read_unsigned_bin((mp), (str), (len)) 00439 #define mp_mag_size(mp) mp_unsigned_bin_size(mp) 00440 #define mp_tomag(mp, str) mp_to_unsigned_bin((mp), (str)) 00441 00442 #define mp_tobinary(M, S) mp_toradix((M), (S), 2) 00443 #define mp_tooctal(M, S) mp_toradix((M), (S), 8) 00444 #define mp_todecimal(M, S) mp_toradix((M), (S), 10) 00445 #define mp_tohex(M, S) mp_toradix((M), (S), 16) 00446 00447 extern const char *mp_s_rmap; 00448 00449 #define PK_PRIVATE 0 /* PK private keys */ 00450 #define PK_PUBLIC 1 /* PK public keys */ 00451 00452 /* Min and Max RSA key sizes (in bits) */ 00453 #define MIN_RSA_SIZE 384 00454 #define MAX_RSA_SIZE 16384 00455 00456 typedef struct Rsa_key { 00457 int type; 00458 mp_int e, d, N, p, q, qP, dP, dQ; 00459 } rsa_key; 00460 00461 int rsa_make_key(int size, long e, rsa_key *key); 00462 00463 int rsa_exptmod(const unsigned char *in, unsigned long inlen, 00464 unsigned char *out, unsigned long *outlen, int which, 00465 rsa_key *key); 00466 00467 void rsa_free(rsa_key *key); 00468 00469 #endif /* __WINE_TOMCRYPT_H_ */ Generated on Mon May 28 2012 04:25:48 for ReactOS by
1.7.6.1
|