]> pd.if.org Git - zpackage/blob - libtomcrypt/src/headers/tomcrypt_pk.h
commit files needed for zpm-fetchurl
[zpackage] / libtomcrypt / src / headers / tomcrypt_pk.h
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
2  *
3  * LibTomCrypt is a library that provides various cryptographic
4  * algorithms in a highly modular and flexible manner.
5  *
6  * The library is free for all purposes without any express
7  * guarantee it works.
8  */
9
10 /* ---- NUMBER THEORY ---- */
11
12 enum {
13    PK_PUBLIC=0,
14    PK_PRIVATE=1
15 };
16
17 /* Indicates standard output formats that can be read e.g. by OpenSSL or GnuTLS */
18 #define PK_STD          0x1000
19
20 int rand_prime(void *N, long len, prng_state *prng, int wprng);
21
22 #ifdef LTC_SOURCE
23 /* internal helper functions */
24 int rand_bn_bits(void *N, int bits, prng_state *prng, int wprng);
25 int rand_bn_upto(void *N, void *limit, prng_state *prng, int wprng);
26
27 enum public_key_algorithms {
28    PKA_RSA,
29    PKA_DSA
30 };
31
32 typedef struct Oid {
33     unsigned long OID[16];
34     /** Number of OID digits in use */
35     unsigned long OIDlen;
36 } oid_st;
37
38 int pk_get_oid(int pk, oid_st *st);
39 #endif /* LTC_SOURCE */
40
41 /* ---- RSA ---- */
42 #ifdef LTC_MRSA
43
44 /** RSA PKCS style key */
45 typedef struct Rsa_key {
46     /** Type of key, PK_PRIVATE or PK_PUBLIC */
47     int type;
48     /** The public exponent */
49     void *e;
50     /** The private exponent */
51     void *d;
52     /** The modulus */
53     void *N;
54     /** The p factor of N */
55     void *p;
56     /** The q factor of N */
57     void *q;
58     /** The 1/q mod p CRT param */
59     void *qP;
60     /** The d mod (p - 1) CRT param */
61     void *dP;
62     /** The d mod (q - 1) CRT param */
63     void *dQ;
64 } rsa_key;
65
66 int rsa_make_key(prng_state *prng, int wprng, int size, long e, rsa_key *key);
67
68 int rsa_get_size(rsa_key *key);
69
70 int rsa_exptmod(const unsigned char *in,   unsigned long inlen,
71                       unsigned char *out,  unsigned long *outlen, int which,
72                       rsa_key *key);
73
74 void rsa_free(rsa_key *key);
75
76 /* These use PKCS #1 v2.0 padding */
77 #define rsa_encrypt_key(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _prng, _prng_idx, _hash_idx, _key) \
78   rsa_encrypt_key_ex(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _prng, _prng_idx, _hash_idx, LTC_PKCS_1_OAEP, _key)
79
80 #define rsa_decrypt_key(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _hash_idx, _stat, _key) \
81   rsa_decrypt_key_ex(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _hash_idx, LTC_PKCS_1_OAEP, _stat, _key)
82
83 #define rsa_sign_hash(_in, _inlen, _out, _outlen, _prng, _prng_idx, _hash_idx, _saltlen, _key) \
84   rsa_sign_hash_ex(_in, _inlen, _out, _outlen, LTC_PKCS_1_PSS, _prng, _prng_idx, _hash_idx, _saltlen, _key)
85
86 #define rsa_verify_hash(_sig, _siglen, _hash, _hashlen, _hash_idx, _saltlen, _stat, _key) \
87   rsa_verify_hash_ex(_sig, _siglen, _hash, _hashlen, LTC_PKCS_1_PSS, _hash_idx, _saltlen, _stat, _key)
88
89 #define rsa_sign_saltlen_get_max(_hash_idx, _key) \
90   rsa_sign_saltlen_get_max_ex(LTC_PKCS_1_PSS, _hash_idx, _key)
91
92 /* These can be switched between PKCS #1 v2.x and PKCS #1 v1.5 paddings */
93 int rsa_encrypt_key_ex(const unsigned char *in,     unsigned long inlen,
94                              unsigned char *out,    unsigned long *outlen,
95                        const unsigned char *lparam, unsigned long lparamlen,
96                        prng_state *prng, int prng_idx, int hash_idx, int padding, rsa_key *key);
97
98 int rsa_decrypt_key_ex(const unsigned char *in,       unsigned long  inlen,
99                              unsigned char *out,      unsigned long *outlen,
100                        const unsigned char *lparam,   unsigned long  lparamlen,
101                              int            hash_idx, int            padding,
102                              int           *stat,     rsa_key       *key);
103
104 int rsa_sign_hash_ex(const unsigned char *in,       unsigned long  inlen,
105                            unsigned char *out,      unsigned long *outlen,
106                            int            padding,
107                            prng_state    *prng,     int            prng_idx,
108                            int            hash_idx, unsigned long  saltlen,
109                            rsa_key *key);
110
111 int rsa_verify_hash_ex(const unsigned char *sig,      unsigned long siglen,
112                        const unsigned char *hash,     unsigned long hashlen,
113                              int            padding,
114                              int            hash_idx, unsigned long saltlen,
115                              int           *stat,     rsa_key      *key);
116
117 int rsa_sign_saltlen_get_max_ex(int padding, int hash_idx, rsa_key *key);
118
119 /* PKCS #1 import/export */
120 int rsa_export(unsigned char *out, unsigned long *outlen, int type, rsa_key *key);
121 int rsa_import(const unsigned char *in, unsigned long inlen, rsa_key *key);
122
123 int rsa_import_x509(const unsigned char *in, unsigned long inlen, rsa_key *key);
124 int rsa_import_pkcs8(const unsigned char *in, unsigned long inlen,
125                      const void *passwd, unsigned long passwdlen, rsa_key *key);
126
127 int rsa_set_key(const unsigned char *N,  unsigned long Nlen,
128                 const unsigned char *e,  unsigned long elen,
129                 const unsigned char *d,  unsigned long dlen,
130                 rsa_key *key);
131 int rsa_set_factors(const unsigned char *p,  unsigned long plen,
132                     const unsigned char *q,  unsigned long qlen,
133                     rsa_key *key);
134 int rsa_set_crt_params(const unsigned char *dP, unsigned long dPlen,
135                        const unsigned char *dQ, unsigned long dQlen,
136                        const unsigned char *qP, unsigned long qPlen,
137                        rsa_key *key);
138 #endif
139
140 /* ---- Katja ---- */
141 #ifdef LTC_MKAT
142
143 /* Min and Max KAT key sizes (in bits) */
144 #define MIN_KAT_SIZE 1024
145 #define MAX_KAT_SIZE 4096
146
147 /** Katja PKCS style key */
148 typedef struct KAT_key {
149     /** Type of key, PK_PRIVATE or PK_PUBLIC */
150     int type;
151     /** The private exponent */
152     void *d;
153     /** The modulus */
154     void *N;
155     /** The p factor of N */
156     void *p;
157     /** The q factor of N */
158     void *q;
159     /** The 1/q mod p CRT param */
160     void *qP;
161     /** The d mod (p - 1) CRT param */
162     void *dP;
163     /** The d mod (q - 1) CRT param */
164     void *dQ;
165     /** The pq param */
166     void *pq;
167 } katja_key;
168
169 int katja_make_key(prng_state *prng, int wprng, int size, katja_key *key);
170
171 int katja_exptmod(const unsigned char *in,   unsigned long inlen,
172                         unsigned char *out,  unsigned long *outlen, int which,
173                         katja_key *key);
174
175 void katja_free(katja_key *key);
176
177 /* These use PKCS #1 v2.0 padding */
178 int katja_encrypt_key(const unsigned char *in,     unsigned long inlen,
179                             unsigned char *out,    unsigned long *outlen,
180                       const unsigned char *lparam, unsigned long lparamlen,
181                       prng_state *prng, int prng_idx, int hash_idx, katja_key *key);
182
183 int katja_decrypt_key(const unsigned char *in,       unsigned long inlen,
184                             unsigned char *out,      unsigned long *outlen,
185                       const unsigned char *lparam,   unsigned long lparamlen,
186                             int            hash_idx, int *stat,
187                             katja_key       *key);
188
189 /* PKCS #1 import/export */
190 int katja_export(unsigned char *out, unsigned long *outlen, int type, katja_key *key);
191 int katja_import(const unsigned char *in, unsigned long inlen, katja_key *key);
192
193 #endif
194
195 /* ---- DH Routines ---- */
196 #ifdef LTC_MDH
197
198 typedef struct {
199     int type;
200     void *x;
201     void *y;
202     void *base;
203     void *prime;
204 } dh_key;
205
206 int dh_get_groupsize(dh_key *key);
207
208 int dh_export(unsigned char *out, unsigned long *outlen, int type, dh_key *key);
209 int dh_import(const unsigned char *in, unsigned long inlen, dh_key *key);
210
211 int dh_set_pg(const unsigned char *p, unsigned long plen,
212               const unsigned char *g, unsigned long glen,
213               dh_key *key);
214 int dh_set_pg_dhparam(const unsigned char *dhparam, unsigned long dhparamlen, dh_key *key);
215 int dh_set_pg_groupsize(int groupsize, dh_key *key);
216
217 int dh_set_key(const unsigned char *in, unsigned long inlen, int type, dh_key *key);
218 int dh_generate_key(prng_state *prng, int wprng, dh_key *key);
219
220 int dh_shared_secret(dh_key        *private_key, dh_key        *public_key,
221                      unsigned char *out,         unsigned long *outlen);
222
223 void dh_free(dh_key *key);
224
225 int dh_export_key(void *out, unsigned long *outlen, int type, dh_key *key);
226
227 #ifdef LTC_SOURCE
228 typedef struct {
229   int size;
230   const char *name, *base, *prime;
231 } ltc_dh_set_type;
232
233 extern const ltc_dh_set_type ltc_dh_sets[];
234
235 /* internal helper functions */
236 int dh_check_pubkey(dh_key *key);
237 #endif
238
239 #endif /* LTC_MDH */
240
241
242 /* ---- ECC Routines ---- */
243 #ifdef LTC_MECC
244
245 /* size of our temp buffers for exported keys */
246 #define ECC_BUF_SIZE 256
247
248 /* max private key size */
249 #define ECC_MAXSIZE  66
250
251 /** Structure defines a NIST GF(p) curve */
252 typedef struct {
253    /** The size of the curve in octets */
254    int size;
255
256    /** name of curve */
257    const char *name;
258
259    /** The prime that defines the field the curve is in (encoded in hex) */
260    const char *prime;
261
262    /** The fields B param (hex) */
263    const char *B;
264
265    /** The order of the curve (hex) */
266    const char *order;
267
268    /** The x co-ordinate of the base point on the curve (hex) */
269    const char *Gx;
270
271    /** The y co-ordinate of the base point on the curve (hex) */
272    const char *Gy;
273 } ltc_ecc_set_type;
274
275 /** A point on a ECC curve, stored in Jacbobian format such that (x,y,z) => (x/z^2, y/z^3, 1) when interpretted as affine */
276 typedef struct {
277     /** The x co-ordinate */
278     void *x;
279
280     /** The y co-ordinate */
281     void *y;
282
283     /** The z co-ordinate */
284     void *z;
285 } ecc_point;
286
287 /** An ECC key */
288 typedef struct {
289     /** Type of key, PK_PRIVATE or PK_PUBLIC */
290     int type;
291
292     /** Index into the ltc_ecc_sets[] for the parameters of this curve; if -1, then this key is using user supplied curve in dp */
293     int idx;
294
295     /** pointer to domain parameters; either points to NIST curves (identified by idx >= 0) or user supplied curve */
296     const ltc_ecc_set_type *dp;
297
298     /** The public key */
299     ecc_point pubkey;
300
301     /** The private key */
302     void *k;
303 } ecc_key;
304
305 /** the ECC params provided */
306 extern const ltc_ecc_set_type ltc_ecc_sets[];
307
308 int  ecc_test(void);
309 void ecc_sizes(int *low, int *high);
310 int  ecc_get_size(ecc_key *key);
311
312 int  ecc_make_key(prng_state *prng, int wprng, int keysize, ecc_key *key);
313 int  ecc_make_key_ex(prng_state *prng, int wprng, ecc_key *key, const ltc_ecc_set_type *dp);
314 void ecc_free(ecc_key *key);
315
316 int  ecc_export(unsigned char *out, unsigned long *outlen, int type, ecc_key *key);
317 int  ecc_import(const unsigned char *in, unsigned long inlen, ecc_key *key);
318 int  ecc_import_ex(const unsigned char *in, unsigned long inlen, ecc_key *key, const ltc_ecc_set_type *dp);
319
320 int ecc_ansi_x963_export(ecc_key *key, unsigned char *out, unsigned long *outlen);
321 int ecc_ansi_x963_import(const unsigned char *in, unsigned long inlen, ecc_key *key);
322 int ecc_ansi_x963_import_ex(const unsigned char *in, unsigned long inlen, ecc_key *key, ltc_ecc_set_type *dp);
323
324 int  ecc_shared_secret(ecc_key *private_key, ecc_key *public_key,
325                        unsigned char *out, unsigned long *outlen);
326
327 int  ecc_encrypt_key(const unsigned char *in,   unsigned long inlen,
328                            unsigned char *out,  unsigned long *outlen,
329                            prng_state *prng, int wprng, int hash,
330                            ecc_key *key);
331
332 int  ecc_decrypt_key(const unsigned char *in,  unsigned long  inlen,
333                            unsigned char *out, unsigned long *outlen,
334                            ecc_key *key);
335
336 int ecc_sign_hash_rfc7518(const unsigned char *in,  unsigned long inlen,
337                                 unsigned char *out, unsigned long *outlen,
338                                 prng_state *prng, int wprng, ecc_key *key);
339
340 int  ecc_sign_hash(const unsigned char *in,  unsigned long inlen,
341                          unsigned char *out, unsigned long *outlen,
342                          prng_state *prng, int wprng, ecc_key *key);
343
344 int ecc_verify_hash_rfc7518(const unsigned char *sig,  unsigned long siglen,
345                             const unsigned char *hash, unsigned long hashlen,
346                             int *stat, ecc_key *key);
347
348 int  ecc_verify_hash(const unsigned char *sig,  unsigned long siglen,
349                      const unsigned char *hash, unsigned long hashlen,
350                      int *stat, ecc_key *key);
351
352 /* low level functions */
353 ecc_point *ltc_ecc_new_point(void);
354 void       ltc_ecc_del_point(ecc_point *p);
355 int        ltc_ecc_is_valid_idx(int n);
356
357 /* point ops (mp == montgomery digit) */
358 #if !defined(LTC_MECC_ACCEL) || defined(LTM_DESC) || defined(GMP_DESC)
359 /* R = 2P */
360 int ltc_ecc_projective_dbl_point(ecc_point *P, ecc_point *R, void *modulus, void *mp);
361
362 /* R = P + Q */
363 int ltc_ecc_projective_add_point(ecc_point *P, ecc_point *Q, ecc_point *R, void *modulus, void *mp);
364 #endif
365
366 #if defined(LTC_MECC_FP)
367 /* optimized point multiplication using fixed point cache (HAC algorithm 14.117) */
368 int ltc_ecc_fp_mulmod(void *k, ecc_point *G, ecc_point *R, void *modulus, int map);
369
370 /* functions for saving/loading/freeing/adding to fixed point cache */
371 int ltc_ecc_fp_save_state(unsigned char **out, unsigned long *outlen);
372 int ltc_ecc_fp_restore_state(unsigned char *in, unsigned long inlen);
373 void ltc_ecc_fp_free(void);
374 int ltc_ecc_fp_add_point(ecc_point *g, void *modulus, int lock);
375
376 /* lock/unlock all points currently in fixed point cache */
377 void ltc_ecc_fp_tablelock(int lock);
378 #endif
379
380 /* R = kG */
381 int ltc_ecc_mulmod(void *k, ecc_point *G, ecc_point *R, void *modulus, int map);
382
383 #ifdef LTC_ECC_SHAMIR
384 /* kA*A + kB*B = C */
385 int ltc_ecc_mul2add(ecc_point *A, void *kA,
386                     ecc_point *B, void *kB,
387                     ecc_point *C,
388                          void *modulus);
389
390 #ifdef LTC_MECC_FP
391 /* Shamir's trick with optimized point multiplication using fixed point cache */
392 int ltc_ecc_fp_mul2add(ecc_point *A, void *kA,
393                        ecc_point *B, void *kB,
394                        ecc_point *C, void *modulus);
395 #endif
396
397 #endif
398
399
400 /* map P to affine from projective */
401 int ltc_ecc_map(ecc_point *P, void *modulus, void *mp);
402
403 #endif
404
405 #ifdef LTC_MDSA
406
407 /* Max diff between group and modulus size in bytes */
408 #define LTC_MDSA_DELTA     512
409
410 /* Max DSA group size in bytes (default allows 4k-bit groups) */
411 #define LTC_MDSA_MAX_GROUP 512
412
413 /** DSA key structure */
414 typedef struct {
415    /** The key type, PK_PRIVATE or PK_PUBLIC */
416    int type;
417
418    /** The order of the sub-group used in octets */
419    int qord;
420
421    /** The generator  */
422    void *g;
423
424    /** The prime used to generate the sub-group */
425    void *q;
426
427    /** The large prime that generats the field the contains the sub-group */
428    void *p;
429
430    /** The private key */
431    void *x;
432
433    /** The public key */
434    void *y;
435 } dsa_key;
436
437 int dsa_make_key(prng_state *prng, int wprng, int group_size, int modulus_size, dsa_key *key);
438
439 int dsa_set_pqg(const unsigned char *p,  unsigned long plen,
440                 const unsigned char *q,  unsigned long qlen,
441                 const unsigned char *g,  unsigned long glen,
442                 dsa_key *key);
443 int dsa_set_pqg_dsaparam(const unsigned char *dsaparam, unsigned long dsaparamlen, dsa_key *key);
444 int dsa_generate_pqg(prng_state *prng, int wprng, int group_size, int modulus_size, dsa_key *key);
445
446 int dsa_set_key(const unsigned char *in, unsigned long inlen, int type, dsa_key *key);
447 int dsa_generate_key(prng_state *prng, int wprng, dsa_key *key);
448
449 void dsa_free(dsa_key *key);
450
451 int dsa_sign_hash_raw(const unsigned char *in,  unsigned long inlen,
452                                    void *r,   void *s,
453                                prng_state *prng, int wprng, dsa_key *key);
454
455 int dsa_sign_hash(const unsigned char *in,  unsigned long inlen,
456                         unsigned char *out, unsigned long *outlen,
457                         prng_state *prng, int wprng, dsa_key *key);
458
459 int dsa_verify_hash_raw(         void *r,          void *s,
460                     const unsigned char *hash, unsigned long hashlen,
461                                     int *stat,      dsa_key *key);
462
463 int dsa_verify_hash(const unsigned char *sig,  unsigned long siglen,
464                     const unsigned char *hash, unsigned long hashlen,
465                           int           *stat, dsa_key       *key);
466
467 int dsa_encrypt_key(const unsigned char *in,   unsigned long inlen,
468                           unsigned char *out,  unsigned long *outlen,
469                           prng_state *prng, int wprng, int hash,
470                           dsa_key *key);
471
472 int dsa_decrypt_key(const unsigned char *in,  unsigned long  inlen,
473                           unsigned char *out, unsigned long *outlen,
474                           dsa_key *key);
475
476 int dsa_import(const unsigned char *in, unsigned long inlen, dsa_key *key);
477 int dsa_export(unsigned char *out, unsigned long *outlen, int type, dsa_key *key);
478 int dsa_verify_key(dsa_key *key, int *stat);
479 #ifdef LTC_SOURCE
480 /* internal helper functions */
481 int dsa_int_validate_xy(dsa_key *key, int *stat);
482 int dsa_int_validate_pqg(dsa_key *key, int *stat);
483 int dsa_int_validate_primes(dsa_key *key, int *stat);
484 #endif
485 int dsa_shared_secret(void          *private_key, void *base,
486                       dsa_key       *public_key,
487                       unsigned char *out,         unsigned long *outlen);
488 #endif
489
490 #ifdef LTC_DER
491 /* DER handling */
492
493 typedef enum ltc_asn1_type_ {
494  /*  0 */
495  LTC_ASN1_EOL,
496  LTC_ASN1_BOOLEAN,
497  LTC_ASN1_INTEGER,
498  LTC_ASN1_SHORT_INTEGER,
499  LTC_ASN1_BIT_STRING,
500  /*  5 */
501  LTC_ASN1_OCTET_STRING,
502  LTC_ASN1_NULL,
503  LTC_ASN1_OBJECT_IDENTIFIER,
504  LTC_ASN1_IA5_STRING,
505  LTC_ASN1_PRINTABLE_STRING,
506  /* 10 */
507  LTC_ASN1_UTF8_STRING,
508  LTC_ASN1_UTCTIME,
509  LTC_ASN1_CHOICE,
510  LTC_ASN1_SEQUENCE,
511  LTC_ASN1_SET,
512  /* 15 */
513  LTC_ASN1_SETOF,
514  LTC_ASN1_RAW_BIT_STRING,
515  LTC_ASN1_TELETEX_STRING,
516  LTC_ASN1_CONSTRUCTED,
517  LTC_ASN1_CONTEXT_SPECIFIC,
518  /* 20 */
519  LTC_ASN1_GENERALIZEDTIME,
520 } ltc_asn1_type;
521
522 /** A LTC ASN.1 list type */
523 typedef struct ltc_asn1_list_ {
524    /** The LTC ASN.1 enumerated type identifier */
525    ltc_asn1_type type;
526    /** The data to encode or place for decoding */
527    void         *data;
528    /** The size of the input or resulting output */
529    unsigned long size;
530    /** The used flag, this is used by the CHOICE ASN.1 type to indicate which choice was made */
531    int           used;
532    /** prev/next entry in the list */
533    struct ltc_asn1_list_ *prev, *next, *child, *parent;
534 } ltc_asn1_list;
535
536 #define LTC_SET_ASN1(list, index, Type, Data, Size)  \
537    do {                                              \
538       int LTC_MACRO_temp            = (index);       \
539       ltc_asn1_list *LTC_MACRO_list = (list);        \
540       LTC_MACRO_list[LTC_MACRO_temp].type = (Type);  \
541       LTC_MACRO_list[LTC_MACRO_temp].data = (void*)(Data);  \
542       LTC_MACRO_list[LTC_MACRO_temp].size = (Size);  \
543       LTC_MACRO_list[LTC_MACRO_temp].used = 0;       \
544    } while (0)
545
546 /* SEQUENCE */
547 int der_encode_sequence_ex(ltc_asn1_list *list, unsigned long inlen,
548                            unsigned char *out,  unsigned long *outlen, int type_of);
549
550 #define der_encode_sequence(list, inlen, out, outlen) der_encode_sequence_ex(list, inlen, out, outlen, LTC_ASN1_SEQUENCE)
551
552 int der_decode_sequence_ex(const unsigned char *in, unsigned long  inlen,
553                            ltc_asn1_list *list,     unsigned long  outlen, int ordered);
554
555 #define der_decode_sequence(in, inlen, list, outlen) der_decode_sequence_ex(in, inlen, list, outlen, 1)
556
557 int der_length_sequence(ltc_asn1_list *list, unsigned long inlen,
558                         unsigned long *outlen);
559
560
561 #ifdef LTC_SOURCE
562 /* internal helper functions */
563 int der_length_sequence_ex(ltc_asn1_list *list, unsigned long inlen,
564                            unsigned long *outlen, unsigned long *payloadlen);
565 /* SUBJECT PUBLIC KEY INFO */
566 int der_encode_subject_public_key_info(unsigned char *out, unsigned long *outlen,
567         unsigned int algorithm, void* public_key, unsigned long public_key_len,
568         unsigned long parameters_type, void* parameters, unsigned long parameters_len);
569
570 int der_decode_subject_public_key_info(const unsigned char *in, unsigned long inlen,
571         unsigned int algorithm, void* public_key, unsigned long* public_key_len,
572         unsigned long parameters_type, ltc_asn1_list* parameters, unsigned long parameters_len);
573 #endif /* LTC_SOURCE */
574
575 /* SET */
576 #define der_decode_set(in, inlen, list, outlen) der_decode_sequence_ex(in, inlen, list, outlen, 0)
577 #define der_length_set der_length_sequence
578 int der_encode_set(ltc_asn1_list *list, unsigned long inlen,
579                    unsigned char *out,  unsigned long *outlen);
580
581 int der_encode_setof(ltc_asn1_list *list, unsigned long inlen,
582                      unsigned char *out,  unsigned long *outlen);
583
584 /* VA list handy helpers with triplets of <type, size, data> */
585 int der_encode_sequence_multi(unsigned char *out, unsigned long *outlen, ...);
586 int der_decode_sequence_multi(const unsigned char *in, unsigned long inlen, ...);
587
588 /* FLEXI DECODER handle unknown list decoder */
589 int  der_decode_sequence_flexi(const unsigned char *in, unsigned long *inlen, ltc_asn1_list **out);
590 #define der_free_sequence_flexi         der_sequence_free
591 void der_sequence_free(ltc_asn1_list *in);
592 void der_sequence_shrink(ltc_asn1_list *in);
593
594 /* BOOLEAN */
595 int der_length_boolean(unsigned long *outlen);
596 int der_encode_boolean(int in,
597                        unsigned char *out, unsigned long *outlen);
598 int der_decode_boolean(const unsigned char *in, unsigned long inlen,
599                                        int *out);
600 /* INTEGER */
601 int der_encode_integer(void *num, unsigned char *out, unsigned long *outlen);
602 int der_decode_integer(const unsigned char *in, unsigned long inlen, void *num);
603 int der_length_integer(void *num, unsigned long *len);
604
605 /* INTEGER -- handy for 0..2^32-1 values */
606 int der_decode_short_integer(const unsigned char *in, unsigned long inlen, unsigned long *num);
607 int der_encode_short_integer(unsigned long num, unsigned char *out, unsigned long *outlen);
608 int der_length_short_integer(unsigned long num, unsigned long *outlen);
609
610 /* BIT STRING */
611 int der_encode_bit_string(const unsigned char *in, unsigned long inlen,
612                                 unsigned char *out, unsigned long *outlen);
613 int der_decode_bit_string(const unsigned char *in, unsigned long inlen,
614                                 unsigned char *out, unsigned long *outlen);
615 int der_encode_raw_bit_string(const unsigned char *in, unsigned long inlen,
616                                 unsigned char *out, unsigned long *outlen);
617 int der_decode_raw_bit_string(const unsigned char *in, unsigned long inlen,
618                                 unsigned char *out, unsigned long *outlen);
619 int der_length_bit_string(unsigned long nbits, unsigned long *outlen);
620
621 /* OCTET STRING */
622 int der_encode_octet_string(const unsigned char *in, unsigned long inlen,
623                                   unsigned char *out, unsigned long *outlen);
624 int der_decode_octet_string(const unsigned char *in, unsigned long inlen,
625                                   unsigned char *out, unsigned long *outlen);
626 int der_length_octet_string(unsigned long noctets, unsigned long *outlen);
627
628 /* OBJECT IDENTIFIER */
629 int der_encode_object_identifier(unsigned long *words, unsigned long  nwords,
630                                  unsigned char *out,   unsigned long *outlen);
631 int der_decode_object_identifier(const unsigned char *in,    unsigned long  inlen,
632                                        unsigned long *words, unsigned long *outlen);
633 int der_length_object_identifier(unsigned long *words, unsigned long nwords, unsigned long *outlen);
634 unsigned long der_object_identifier_bits(unsigned long x);
635
636 /* IA5 STRING */
637 int der_encode_ia5_string(const unsigned char *in, unsigned long inlen,
638                                 unsigned char *out, unsigned long *outlen);
639 int der_decode_ia5_string(const unsigned char *in, unsigned long inlen,
640                                 unsigned char *out, unsigned long *outlen);
641 int der_length_ia5_string(const unsigned char *octets, unsigned long noctets, unsigned long *outlen);
642
643 int der_ia5_char_encode(int c);
644 int der_ia5_value_decode(int v);
645
646 /* TELETEX STRING */
647 int der_decode_teletex_string(const unsigned char *in, unsigned long inlen,
648                                 unsigned char *out, unsigned long *outlen);
649 int der_length_teletex_string(const unsigned char *octets, unsigned long noctets, unsigned long *outlen);
650
651 #ifdef LTC_SOURCE
652 /* internal helper functions */
653 int der_teletex_char_encode(int c);
654 int der_teletex_value_decode(int v);
655 #endif /* LTC_SOURCE */
656
657
658 /* PRINTABLE STRING */
659 int der_encode_printable_string(const unsigned char *in, unsigned long inlen,
660                                 unsigned char *out, unsigned long *outlen);
661 int der_decode_printable_string(const unsigned char *in, unsigned long inlen,
662                                 unsigned char *out, unsigned long *outlen);
663 int der_length_printable_string(const unsigned char *octets, unsigned long noctets, unsigned long *outlen);
664
665 int der_printable_char_encode(int c);
666 int der_printable_value_decode(int v);
667
668 /* UTF-8 */
669 #if (defined(SIZE_MAX) || __STDC_VERSION__ >= 199901L || defined(WCHAR_MAX) || defined(__WCHAR_MAX__) || defined(_WCHAR_T) || defined(_WCHAR_T_DEFINED) || defined (__WCHAR_TYPE__)) && !defined(LTC_NO_WCHAR)
670    #if defined(__WCHAR_MAX__)
671       #define LTC_WCHAR_MAX __WCHAR_MAX__
672    #else
673       #include <wchar.h>
674       #define LTC_WCHAR_MAX WCHAR_MAX
675    #endif
676 /* please note that it might happen that LTC_WCHAR_MAX is undefined */
677 #else
678    typedef ulong32 wchar_t;
679    #define LTC_WCHAR_MAX 0xFFFFFFFF
680 #endif
681
682 int der_encode_utf8_string(const wchar_t *in,  unsigned long inlen,
683                            unsigned char *out, unsigned long *outlen);
684
685 int der_decode_utf8_string(const unsigned char *in,  unsigned long inlen,
686                                        wchar_t *out, unsigned long *outlen);
687 unsigned long der_utf8_charsize(const wchar_t c);
688 #ifdef LTC_SOURCE
689 /* internal helper functions */
690 int der_utf8_valid_char(const wchar_t c);
691 #endif /* LTC_SOURCE */
692 int der_length_utf8_string(const wchar_t *in, unsigned long noctets, unsigned long *outlen);
693
694
695 /* CHOICE */
696 int der_decode_choice(const unsigned char *in,   unsigned long *inlen,
697                             ltc_asn1_list *list, unsigned long  outlen);
698
699 /* UTCTime */
700 typedef struct {
701    unsigned YY, /* year */
702             MM, /* month */
703             DD, /* day */
704             hh, /* hour */
705             mm, /* minute */
706             ss, /* second */
707             off_dir, /* timezone offset direction 0 == +, 1 == - */
708             off_hh, /* timezone offset hours */
709             off_mm; /* timezone offset minutes */
710 } ltc_utctime;
711
712 int der_encode_utctime(ltc_utctime *utctime,
713                        unsigned char *out,   unsigned long *outlen);
714
715 int der_decode_utctime(const unsigned char *in, unsigned long *inlen,
716                              ltc_utctime   *out);
717
718 int der_length_utctime(ltc_utctime *utctime, unsigned long *outlen);
719
720 /* GeneralizedTime */
721 typedef struct {
722    unsigned YYYY, /* year */
723             MM, /* month */
724             DD, /* day */
725             hh, /* hour */
726             mm, /* minute */
727             ss, /* second */
728             fs, /* fractional seconds */
729             off_dir, /* timezone offset direction 0 == +, 1 == - */
730             off_hh, /* timezone offset hours */
731             off_mm; /* timezone offset minutes */
732 } ltc_generalizedtime;
733
734 int der_encode_generalizedtime(ltc_generalizedtime *gtime,
735                                unsigned char       *out, unsigned long *outlen);
736
737 int der_decode_generalizedtime(const unsigned char *in, unsigned long *inlen,
738                                ltc_generalizedtime *out);
739
740 int der_length_generalizedtime(ltc_generalizedtime *gtime, unsigned long *outlen);
741
742
743 #endif
744
745 /* ref:         $Format:%D$ */
746 /* git commit:  $Format:%H$ */
747 /* commit time: $Format:%ai$ */