TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
tomcrypt_pk.h
Go to the documentation of this file.
1 /* ---- NUMBER THEORY ---- */
2 
3 enum {
6 };
7 
8 int rand_prime(void *N, long len, prng_state *prng, int wprng);
9 
10 /* ---- RSA ---- */
11 #ifdef LTC_MRSA
12 
13 /* Min and Max RSA key sizes (in bits) */
14 #define MIN_RSA_SIZE 1024
15 #define MAX_RSA_SIZE 4096
16 
18 typedef struct Rsa_key {
20  int type;
22  void *e;
24  void *d;
26  void *N;
28  void *p;
30  void *q;
32  void *qP;
34  void *dP;
36  void *dQ;
37 } rsa_key;
38 
39 int rsa_make_key(prng_state *prng, int wprng, int size, long e, rsa_key *key);
40 
41 int rsa_exptmod(const unsigned char *in, unsigned long inlen,
42  unsigned char *out, unsigned long *outlen, int which,
43  rsa_key *key);
44 
45 void rsa_free(rsa_key *key);
46 
47 /* These use LTC_PKCS #1 v2.0 padding */
48 #define rsa_encrypt_key(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _prng, _prng_idx, _hash_idx, _key) \
49  rsa_encrypt_key_ex(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _prng, _prng_idx, _hash_idx, LTC_LTC_PKCS_1_OAEP, _key)
50 
51 #define rsa_decrypt_key(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _hash_idx, _stat, _key) \
52  rsa_decrypt_key_ex(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _hash_idx, LTC_LTC_PKCS_1_OAEP, _stat, _key)
53 
54 #define rsa_sign_hash(_in, _inlen, _out, _outlen, _prng, _prng_idx, _hash_idx, _saltlen, _key) \
55  rsa_sign_hash_ex(_in, _inlen, _out, _outlen, LTC_LTC_PKCS_1_PSS, _prng, _prng_idx, _hash_idx, _saltlen, _key)
56 
57 #define rsa_verify_hash(_sig, _siglen, _hash, _hashlen, _hash_idx, _saltlen, _stat, _key) \
58  rsa_verify_hash_ex(_sig, _siglen, _hash, _hashlen, LTC_LTC_PKCS_1_PSS, _hash_idx, _saltlen, _stat, _key)
59 
60 /* These can be switched between LTC_PKCS #1 v2.x and LTC_PKCS #1 v1.5 paddings */
61 int rsa_encrypt_key_ex(const unsigned char *in, unsigned long inlen,
62  unsigned char *out, unsigned long *outlen,
63  const unsigned char *lparam, unsigned long lparamlen,
64  prng_state *prng, int prng_idx, int hash_idx, int padding, rsa_key *key);
65 
66 int rsa_decrypt_key_ex(const unsigned char *in, unsigned long inlen,
67  unsigned char *out, unsigned long *outlen,
68  const unsigned char *lparam, unsigned long lparamlen,
69  int hash_idx, int padding,
70  int *stat, rsa_key *key);
71 
72 int rsa_sign_hash_ex(const unsigned char *in, unsigned long inlen,
73  unsigned char *out, unsigned long *outlen,
74  int padding,
75  prng_state *prng, int prng_idx,
76  int hash_idx, unsigned long saltlen,
77  rsa_key *key);
78 
79 int rsa_verify_hash_ex(const unsigned char *sig, unsigned long siglen,
80  const unsigned char *hash, unsigned long hashlen,
81  int padding,
82  int hash_idx, unsigned long saltlen,
83  int *stat, rsa_key *key);
84 
85 /* LTC_PKCS #1 import/export */
86 int rsa_export(unsigned char *out, unsigned long *outlen, int type, rsa_key *key);
87 int rsa_import(const unsigned char *in, unsigned long inlen, rsa_key *key);
88 
89 /* Ladik: Added for verifying Blizzard strong signature verification */
90 int rsa_verify_simple(const unsigned char *sig, unsigned long siglen,
91  const unsigned char *hash, unsigned long hashlen,
92  int *stat,
93  rsa_key *key);
94 
95 #endif
96 
97 /* ---- Katja ---- */
98 #ifdef MKAT
99 
100 /* Min and Max KAT key sizes (in bits) */
101 #define MIN_KAT_SIZE 1024
102 #define MAX_KAT_SIZE 4096
103 
105 typedef struct KAT_key {
107  int type;
109  void *d;
111  void *N;
113  void *p;
115  void *q;
117  void *qP;
119  void *dP;
121  void *dQ;
123  void *pq;
124 } katja_key;
125 
126 int katja_make_key(prng_state *prng, int wprng, int size, katja_key *key);
127 
128 int katja_exptmod(const unsigned char *in, unsigned long inlen,
129  unsigned char *out, unsigned long *outlen, int which,
130  katja_key *key);
131 
132 void katja_free(katja_key *key);
133 
134 /* These use LTC_PKCS #1 v2.0 padding */
135 int katja_encrypt_key(const unsigned char *in, unsigned long inlen,
136  unsigned char *out, unsigned long *outlen,
137  const unsigned char *lparam, unsigned long lparamlen,
138  prng_state *prng, int prng_idx, int hash_idx, katja_key *key);
139 
140 int katja_decrypt_key(const unsigned char *in, unsigned long inlen,
141  unsigned char *out, unsigned long *outlen,
142  const unsigned char *lparam, unsigned long lparamlen,
143  int hash_idx, int *stat,
144  katja_key *key);
145 
146 /* LTC_PKCS #1 import/export */
147 int katja_export(unsigned char *out, unsigned long *outlen, int type, katja_key *key);
148 int katja_import(const unsigned char *in, unsigned long inlen, katja_key *key);
149 
150 #endif
151 
152 /* ---- ECC Routines ---- */
153 #ifdef LTC_MECC
154 
155 /* size of our temp buffers for exported keys */
156 #define ECC_BUF_SIZE 256
157 
158 /* max private key size */
159 #define ECC_MAXSIZE 66
160 
162 typedef struct {
164  int size;
165 
167  char *name;
168 
170  char *prime;
171 
173  char *B;
174 
176  char *order;
177 
179  char *Gx;
180 
182  char *Gy;
183 } ltc_ecc_set_type;
184 
186 typedef struct {
188  void *x;
189 
191  void *y;
192 
194  void *z;
195 } ecc_point;
196 
198 typedef struct {
200  int type;
201 
203  int idx;
204 
206  const ltc_ecc_set_type *dp;
207 
209  ecc_point pubkey;
210 
212  void *k;
213 } ecc_key;
214 
216 extern const ltc_ecc_set_type ltc_ecc_sets[];
217 
218 int ecc_test(void);
219 void ecc_sizes(int *low, int *high);
220 int ecc_get_size(ecc_key *key);
221 
222 int ecc_make_key(prng_state *prng, int wprng, int keysize, ecc_key *key);
223 int ecc_make_key_ex(prng_state *prng, int wprng, ecc_key *key, const ltc_ecc_set_type *dp);
224 void ecc_free(ecc_key *key);
225 
226 int ecc_export(unsigned char *out, unsigned long *outlen, int type, ecc_key *key);
227 int ecc_import(const unsigned char *in, unsigned long inlen, ecc_key *key);
228 int ecc_import_ex(const unsigned char *in, unsigned long inlen, ecc_key *key, const ltc_ecc_set_type *dp);
229 
230 int ecc_ansi_x963_export(ecc_key *key, unsigned char *out, unsigned long *outlen);
231 int ecc_ansi_x963_import(const unsigned char *in, unsigned long inlen, ecc_key *key);
232 int ecc_ansi_x963_import_ex(const unsigned char *in, unsigned long inlen, ecc_key *key, ltc_ecc_set_type *dp);
233 
234 int ecc_shared_secret(ecc_key *private_key, ecc_key *public_key,
235  unsigned char *out, unsigned long *outlen);
236 
237 int ecc_encrypt_key(const unsigned char *in, unsigned long inlen,
238  unsigned char *out, unsigned long *outlen,
239  prng_state *prng, int wprng, int hash,
240  ecc_key *key);
241 
242 int ecc_decrypt_key(const unsigned char *in, unsigned long inlen,
243  unsigned char *out, unsigned long *outlen,
244  ecc_key *key);
245 
246 int ecc_sign_hash(const unsigned char *in, unsigned long inlen,
247  unsigned char *out, unsigned long *outlen,
248  prng_state *prng, int wprng, ecc_key *key);
249 
250 int ecc_verify_hash(const unsigned char *sig, unsigned long siglen,
251  const unsigned char *hash, unsigned long hashlen,
252  int *stat, ecc_key *key);
253 
254 /* low level functions */
255 ecc_point *ltc_ecc_new_point(void);
256 void ltc_ecc_del_point(ecc_point *p);
257 int ltc_ecc_is_valid_idx(int n);
258 
259 /* point ops (mp == montgomery digit) */
260 #if !defined(LTC_MECC_ACCEL) || defined(LTM_LTC_DESC) || defined(GMP_LTC_DESC)
261 /* R = 2P */
262 int ltc_ecc_projective_dbl_point(ecc_point *P, ecc_point *R, void *modulus, void *mp);
263 
264 /* R = P + Q */
265 int ltc_ecc_projective_add_point(ecc_point *P, ecc_point *Q, ecc_point *R, void *modulus, void *mp);
266 #endif
267 
268 #if defined(LTC_MECC_FP)
269 /* optimized point multiplication using fixed point cache (HAC algorithm 14.117) */
270 int ltc_ecc_fp_mulmod(void *k, ecc_point *G, ecc_point *R, void *modulus, int map);
271 
272 /* functions for saving/loading/freeing/adding to fixed point cache */
273 int ltc_ecc_fp_save_state(unsigned char **out, unsigned long *outlen);
274 int ltc_ecc_fp_restore_state(unsigned char *in, unsigned long inlen);
275 void ltc_ecc_fp_free(void);
276 int ltc_ecc_fp_add_point(ecc_point *g, void *modulus, int lock);
277 
278 /* lock/unlock all points currently in fixed point cache */
279 void ltc_ecc_fp_tablelock(int lock);
280 #endif
281 
282 /* R = kG */
283 int ltc_ecc_mulmod(void *k, ecc_point *G, ecc_point *R, void *modulus, int map);
284 
285 #ifdef LTC_ECC_SHAMIR
286 /* kA*A + kB*B = C */
287 int ltc_ecc_mul2add(ecc_point *A, void *kA,
288  ecc_point *B, void *kB,
289  ecc_point *C,
290  void *modulus);
291 
292 #ifdef LTC_MECC_FP
293 /* Shamir's trick with optimized point multiplication using fixed point cache */
294 int ltc_ecc_fp_mul2add(ecc_point *A, void *kA,
295  ecc_point *B, void *kB,
296  ecc_point *C, void *modulus);
297 #endif
298 
299 #endif
300 
301 
302 /* map P to affine from projective */
303 int ltc_ecc_map(ecc_point *P, void *modulus, void *mp);
304 
305 #endif
306 
307 #ifdef LTC_MDSA
308 
309 /* Max diff between group and modulus size in bytes */
310 #define LTC_MDSA_DELTA 512
311 
312 /* Max DSA group size in bytes (default allows 4k-bit groups) */
313 #define LTC_MDSA_MAX_GROUP 512
314 
316 typedef struct {
318  int type;
319 
321  int qord;
322 
324  void *g;
325 
327  void *q;
328 
330  void *p;
331 
333  void *x;
334 
336  void *y;
337 } dsa_key;
338 
339 int dsa_make_key(prng_state *prng, int wprng, int group_size, int modulus_size, dsa_key *key);
340 void dsa_free(dsa_key *key);
341 
342 int dsa_sign_hash_raw(const unsigned char *in, unsigned long inlen,
343  void *r, void *s,
344  prng_state *prng, int wprng, dsa_key *key);
345 
346 int dsa_sign_hash(const unsigned char *in, unsigned long inlen,
347  unsigned char *out, unsigned long *outlen,
348  prng_state *prng, int wprng, dsa_key *key);
349 
350 int dsa_verify_hash_raw( void *r, void *s,
351  const unsigned char *hash, unsigned long hashlen,
352  int *stat, dsa_key *key);
353 
354 int dsa_verify_hash(const unsigned char *sig, unsigned long siglen,
355  const unsigned char *hash, unsigned long hashlen,
356  int *stat, dsa_key *key);
357 
358 int dsa_encrypt_key(const unsigned char *in, unsigned long inlen,
359  unsigned char *out, unsigned long *outlen,
360  prng_state *prng, int wprng, int hash,
361  dsa_key *key);
362 
363 int dsa_decrypt_key(const unsigned char *in, unsigned long inlen,
364  unsigned char *out, unsigned long *outlen,
365  dsa_key *key);
366 
367 int dsa_import(const unsigned char *in, unsigned long inlen, dsa_key *key);
368 int dsa_export(unsigned char *out, unsigned long *outlen, int type, dsa_key *key);
369 int dsa_verify_key(dsa_key *key, int *stat);
370 
371 int dsa_shared_secret(void *private_key, void *base,
372  dsa_key *public_key,
373  unsigned char *out, unsigned long *outlen);
374 #endif
375 
376 #ifdef LTC_DER
377 /* DER handling */
378 
379 enum {
380  LTC_ASN1_EOL,
381  LTC_ASN1_BOOLEAN,
382  LTC_ASN1_INTEGER,
383  LTC_ASN1_SHORT_INTEGER,
384  LTC_ASN1_BIT_STRING,
385  LTC_ASN1_OCTET_STRING,
386  LTC_ASN1_NULL,
387  LTC_ASN1_OBJECT_IDENTIFIER,
388  LTC_ASN1_IA5_STRING,
389  LTC_ASN1_PRINTABLE_STRING,
390  LTC_ASN1_UTF8_STRING,
391  LTC_ASN1_UTCTIME,
392  LTC_ASN1_CHOICE,
393  LTC_ASN1_SEQUENCE,
394  LTC_ASN1_SET,
395  LTC_ASN1_SETOF
396 };
397 
399 typedef struct ltc_asn1_list_ {
401  int type;
403  void *data;
405  unsigned long size;
407  int used;
409  struct ltc_asn1_list_ *prev, *next, *child, *parent;
410 } ltc_asn1_list;
411 
412 #define LTC_SET_ASN1(list, index, Type, Data, Size) \
413  do { \
414  int LTC_MACRO_temp = (index); \
415  ltc_asn1_list *LTC_MACRO_list = (list); \
416  LTC_MACRO_list[LTC_MACRO_temp].type = (Type); \
417  LTC_MACRO_list[LTC_MACRO_temp].data = (void*)(Data); \
418  LTC_MACRO_list[LTC_MACRO_temp].size = (Size); \
419  LTC_MACRO_list[LTC_MACRO_temp].used = 0; \
420  } while (0);
421 
422 /* SEQUENCE */
423 int der_encode_sequence_ex(ltc_asn1_list *list, unsigned long inlen,
424  unsigned char *out, unsigned long *outlen, int type_of);
425 
426 #define der_encode_sequence(list, inlen, out, outlen) der_encode_sequence_ex(list, inlen, out, outlen, LTC_ASN1_SEQUENCE)
427 
428 int der_decode_sequence_ex(const unsigned char *in, unsigned long inlen,
429  ltc_asn1_list *list, unsigned long outlen, int ordered);
430 
431 #define der_decode_sequence(in, inlen, list, outlen) der_decode_sequence_ex(in, inlen, list, outlen, 1)
432 
433 int der_length_sequence(ltc_asn1_list *list, unsigned long inlen,
434  unsigned long *outlen);
435 
436 /* SET */
437 #define der_decode_set(in, inlen, list, outlen) der_decode_sequence_ex(in, inlen, list, outlen, 0)
438 #define der_length_set der_length_sequence
439 int der_encode_set(ltc_asn1_list *list, unsigned long inlen,
440  unsigned char *out, unsigned long *outlen);
441 
442 int der_encode_setof(ltc_asn1_list *list, unsigned long inlen,
443  unsigned char *out, unsigned long *outlen);
444 
445 /* VA list handy helpers with triplets of <type, size, data> */
446 int der_encode_sequence_multi(unsigned char *out, unsigned long *outlen, ...);
447 int der_decode_sequence_multi(const unsigned char *in, unsigned long inlen, ...);
448 
449 /* FLEXI DECODER handle unknown list decoder */
450 int der_decode_sequence_flexi(const unsigned char *in, unsigned long *inlen, ltc_asn1_list **out);
451 void der_free_sequence_flexi(ltc_asn1_list *list);
452 void der_sequence_free(ltc_asn1_list *in);
453 
454 /* BOOLEAN */
455 int der_length_boolean(unsigned long *outlen);
456 int der_encode_boolean(int in,
457  unsigned char *out, unsigned long *outlen);
458 int der_decode_boolean(const unsigned char *in, unsigned long inlen,
459  int *out);
460 /* INTEGER */
461 int der_encode_integer(void *num, unsigned char *out, unsigned long *outlen);
462 int der_decode_integer(const unsigned char *in, unsigned long inlen, void *num);
463 int der_length_integer(void *num, unsigned long *len);
464 
465 /* INTEGER -- handy for 0..2^32-1 values */
466 int der_decode_short_integer(const unsigned char *in, unsigned long inlen, unsigned long *num);
467 int der_encode_short_integer(unsigned long num, unsigned char *out, unsigned long *outlen);
468 int der_length_short_integer(unsigned long num, unsigned long *outlen);
469 
470 /* BIT STRING */
471 int der_encode_bit_string(const unsigned char *in, unsigned long inlen,
472  unsigned char *out, unsigned long *outlen);
473 int der_decode_bit_string(const unsigned char *in, unsigned long inlen,
474  unsigned char *out, unsigned long *outlen);
475 int der_length_bit_string(unsigned long nbits, unsigned long *outlen);
476 
477 /* OCTET STRING */
478 int der_encode_octet_string(const unsigned char *in, unsigned long inlen,
479  unsigned char *out, unsigned long *outlen);
480 int der_decode_octet_string(const unsigned char *in, unsigned long inlen,
481  unsigned char *out, unsigned long *outlen);
482 int der_length_octet_string(unsigned long noctets, unsigned long *outlen);
483 
484 /* OBJECT IDENTIFIER */
485 int der_encode_object_identifier(unsigned long *words, unsigned long nwords,
486  unsigned char *out, unsigned long *outlen);
487 int der_decode_object_identifier(const unsigned char *in, unsigned long inlen,
488  unsigned long *words, unsigned long *outlen);
489 int der_length_object_identifier(unsigned long *words, unsigned long nwords, unsigned long *outlen);
490 unsigned long der_object_identifier_bits(unsigned long x);
491 
492 /* IA5 STRING */
493 int der_encode_ia5_string(const unsigned char *in, unsigned long inlen,
494  unsigned char *out, unsigned long *outlen);
495 int der_decode_ia5_string(const unsigned char *in, unsigned long inlen,
496  unsigned char *out, unsigned long *outlen);
497 int der_length_ia5_string(const unsigned char *octets, unsigned long noctets, unsigned long *outlen);
498 
499 int der_ia5_char_encode(int c);
500 int der_ia5_value_decode(int v);
501 
502 /* Printable STRING */
503 int der_encode_printable_string(const unsigned char *in, unsigned long inlen,
504  unsigned char *out, unsigned long *outlen);
505 int der_decode_printable_string(const unsigned char *in, unsigned long inlen,
506  unsigned char *out, unsigned long *outlen);
507 int der_length_printable_string(const unsigned char *octets, unsigned long noctets, unsigned long *outlen);
508 
509 int der_printable_char_encode(int c);
510 int der_printable_value_decode(int v);
511 
512 /* UTF-8 */
513 #if (defined(SIZE_MAX) || __STDC_VERSION__ >= 199901L || defined(WCHAR_MAX) || defined(_WCHAR_T) || defined(_WCHAR_T_DEFINED) || defined (__WCHAR_TYPE__)) && !defined(LTC_NO_WCHAR)
514 #include <wchar.h>
515 #else
516 typedef ulong32 wchar_t;
517 #endif
518 
519 int der_encode_utf8_string(const wchar_t *in, unsigned long inlen,
520  unsigned char *out, unsigned long *outlen);
521 
522 int der_decode_utf8_string(const unsigned char *in, unsigned long inlen,
523  wchar_t *out, unsigned long *outlen);
524 unsigned long der_utf8_charsize(const wchar_t c);
525 int der_length_utf8_string(const wchar_t *in, unsigned long noctets, unsigned long *outlen);
526 
527 
528 /* CHOICE */
529 int der_decode_choice(const unsigned char *in, unsigned long *inlen,
530  ltc_asn1_list *list, unsigned long outlen);
531 
532 /* UTCTime */
533 typedef struct {
534  unsigned YY, /* year */
535  MM, /* month */
536  DD, /* day */
537  hh, /* hour */
538  mm, /* minute */
539  ss, /* second */
540  off_dir, /* timezone offset direction 0 == +, 1 == - */
541  off_hh, /* timezone offset hours */
542  off_mm; /* timezone offset minutes */
543 } ltc_utctime;
544 
545 int der_encode_utctime(ltc_utctime *utctime,
546  unsigned char *out, unsigned long *outlen);
547 
548 int der_decode_utctime(const unsigned char *in, unsigned long *inlen,
549  ltc_utctime *out);
550 
551 int der_length_utctime(ltc_utctime *utctime, unsigned long *outlen);
552 
553 
554 #endif
555 
556 /* $Source: /cvs/libtom/libtomcrypt/src/headers/tomcrypt_pk.h,v $ */
557 /* $Revision: 1.81 $ */
558 /* $Date: 2007/05/12 14:32:35 $ */
Definition: tomcrypt_pk.h:5
#define hash
Definition: private_namespace.h:186
int rand_prime(void *N, long len, prng_state *prng, int wprng)
void rsa_key
Definition: tomcrypt_math.h:15
int next(int i, int n)
Definition: RecastContour.cpp:469
unsigned long ulong32
Definition: tomcrypt_macros.h:16
Definition: tomcrypt_pk.h:4
G3D::int16 z
Definition: Vector3int16.h:46
G3D::int16 y
Definition: Vector2int16.h:38
int prev(int i, int n)
Definition: RecastContour.cpp:468
G3D::int16 x
Definition: Vector2int16.h:37
#define G(x, y, z)
void ecc_point
Definition: tomcrypt_math.h:11
uint8 const P[]
Definition: AuthenticationPackets.cpp:225
Definition: tomcrypt_prng.h:50
uint8 const Q[]
Definition: AuthenticationPackets.cpp:237