1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 
4 /* ---- NUMBER THEORY ---- */
5 
6 enum public_key_type {
7    /* Refers to the public key */
8    PK_PUBLIC      = 0x0000,
9    /* Refers to the private key */
10    PK_PRIVATE     = 0x0001,
11 
12    /* Indicates standard output formats that can be read e.g. by OpenSSL or GnuTLS */
13    PK_STD         = 0x1000,
14    /* Indicates compressed public ECC key */
15    PK_COMPRESSED  = 0x2000,
16    /* Indicates ECC key with the curve specified by OID */
17    PK_CURVEOID    = 0x4000
18 };
19 
20 int rand_prime(void *N, long len, prng_state *prng, int wprng);
21 
22 /* ---- RSA ---- */
23 #ifdef LTC_MRSA
24 
25 /** RSA PKCS style key */
26 typedef struct Rsa_key {
27     /** Type of key, PK_PRIVATE or PK_PUBLIC */
28     int type;
29     /** The public exponent */
30     void *e;
31     /** The private exponent */
32     void *d;
33     /** The modulus */
34     void *N;
35     /** The p factor of N */
36     void *p;
37     /** The q factor of N */
38     void *q;
39     /** The 1/q mod p CRT param */
40     void *qP;
41     /** The d mod (p - 1) CRT param */
42     void *dP;
43     /** The d mod (q - 1) CRT param */
44     void *dQ;
45 } rsa_key;
46 
47 int rsa_make_key(prng_state *prng, int wprng, int size, long e, rsa_key *key);
48 int rsa_make_key_ubin_e(prng_state *prng, int wprng, int size,
49                         const unsigned char *e, unsigned long elen, rsa_key *key);
50 int rsa_get_size(const rsa_key *key);
51 
52 int rsa_exptmod(const unsigned char *in,   unsigned long inlen,
53                       unsigned char *out,  unsigned long *outlen, int which,
54                 const rsa_key *key);
55 
56 void rsa_free(rsa_key *key);
57 
58 /* These use PKCS #1 v2.0 padding */
59 #define rsa_encrypt_key(in, inlen, out, outlen, lparam, lparamlen, prng, prng_idx, hash_idx, key) \
60   rsa_encrypt_key_ex(in, inlen, out, outlen, lparam, lparamlen, prng, prng_idx, hash_idx, LTC_PKCS_1_OAEP, key)
61 
62 #define rsa_decrypt_key(in, inlen, out, outlen, lparam, lparamlen, hash_idx, stat, key) \
63   rsa_decrypt_key_ex(in, inlen, out, outlen, lparam, lparamlen, hash_idx, LTC_PKCS_1_OAEP, stat, key)
64 
65 #define rsa_sign_hash(in, inlen, out, outlen, prng, prng_idx, hash_idx, saltlen, key) \
66   rsa_sign_hash_ex(in, inlen, out, outlen, LTC_PKCS_1_PSS, prng, prng_idx, hash_idx, saltlen, key)
67 
68 #define rsa_verify_hash(sig, siglen, hash, hashlen, hash_idx, saltlen, stat, key) \
69   rsa_verify_hash_ex(sig, siglen, hash, hashlen, LTC_PKCS_1_PSS, hash_idx, saltlen, stat, key)
70 
71 #define rsa_sign_saltlen_get_max(hash_idx, key) \
72   rsa_sign_saltlen_get_max_ex(LTC_PKCS_1_PSS, hash_idx, key)
73 
74 /* These can be switched between PKCS #1 v2.x and PKCS #1 v1.5 paddings */
75 int rsa_encrypt_key_ex(const unsigned char *in,       unsigned long  inlen,
76                              unsigned char *out,      unsigned long *outlen,
77                        const unsigned char *lparam,   unsigned long  lparamlen,
78                              prng_state    *prng,     int            prng_idx,
79                              int            hash_idx, int            padding,
80                        const rsa_key       *key);
81 
82 int rsa_decrypt_key_ex(const unsigned char *in,             unsigned long  inlen,
83                              unsigned char *out,            unsigned long *outlen,
84                        const unsigned char *lparam,         unsigned long  lparamlen,
85                              int            hash_idx,       int            padding,
86                              int           *stat,     const rsa_key       *key);
87 
88 int rsa_sign_hash_ex(const unsigned char *in,       unsigned long  inlen,
89                            unsigned char *out,      unsigned long *outlen,
90                            int            padding,
91                            prng_state    *prng,     int            prng_idx,
92                            int            hash_idx, unsigned long  saltlen,
93                      const rsa_key       *key);
94 
95 int rsa_verify_hash_ex(const unsigned char *sig,            unsigned long  siglen,
96                        const unsigned char *hash,           unsigned long  hashlen,
97                              int            padding,
98                              int            hash_idx,       unsigned long  saltlen,
99                              int           *stat,     const rsa_key       *key);
100 
101 int rsa_sign_saltlen_get_max_ex(int padding, int hash_idx, const rsa_key *key);
102 
103 /* PKCS #1 import/export */
104 int rsa_export(unsigned char *out, unsigned long *outlen, int type, const rsa_key *key);
105 int rsa_import(const unsigned char *in, unsigned long inlen, rsa_key *key);
106 
107 int rsa_import_x509(const unsigned char *in, unsigned long inlen, rsa_key *key);
108 int rsa_import_pkcs8(const unsigned char *in, unsigned long inlen,
109                      const void *passwd, unsigned long passwdlen, rsa_key *key);
110 
111 int rsa_set_key(const unsigned char *N,  unsigned long Nlen,
112                 const unsigned char *e,  unsigned long elen,
113                 const unsigned char *d,  unsigned long dlen,
114                 rsa_key *key);
115 int rsa_set_factors(const unsigned char *p,  unsigned long plen,
116                     const unsigned char *q,  unsigned long qlen,
117                     rsa_key *key);
118 int rsa_set_crt_params(const unsigned char *dP, unsigned long dPlen,
119                        const unsigned char *dQ, unsigned long dQlen,
120                        const unsigned char *qP, unsigned long qPlen,
121                        rsa_key *key);
122 #endif
123 
124 /* ---- DH Routines ---- */
125 #ifdef LTC_MDH
126 
127 typedef struct {
128     int type;
129     void *x;
130     void *y;
131     void *base;
132     void *prime;
133 } dh_key;
134 
135 int dh_get_groupsize(const dh_key *key);
136 
137 int dh_export(unsigned char *out, unsigned long *outlen, int type, const dh_key *key);
138 int dh_import(const unsigned char *in, unsigned long inlen, dh_key *key);
139 
140 int dh_set_pg(const unsigned char *p, unsigned long plen,
141               const unsigned char *g, unsigned long glen,
142               dh_key *key);
143 int dh_set_pg_dhparam(const unsigned char *dhparam, unsigned long dhparamlen, dh_key *key);
144 int dh_set_pg_groupsize(int groupsize, dh_key *key);
145 
146 int dh_set_key(const unsigned char *in, unsigned long inlen, int type, dh_key *key);
147 int dh_generate_key(prng_state *prng, int wprng, dh_key *key);
148 
149 int dh_shared_secret(const dh_key  *private_key, const dh_key  *public_key,
150                      unsigned char *out,         unsigned long *outlen);
151 
152 void dh_free(dh_key *key);
153 
154 int dh_export_key(void *out, unsigned long *outlen, int type, const dh_key *key);
155 #endif /* LTC_MDH */
156 
157 
158 /* ---- ECC Routines ---- */
159 #ifdef LTC_MECC
160 
161 /* size of our temp buffers for exported keys */
162 #define ECC_BUF_SIZE 256
163 
164 /* max private key size */
165 #define ECC_MAXSIZE  66
166 
167 /** Structure defines a GF(p) curve */
168 typedef struct {
169    /** The prime that defines the field the curve is in (encoded in hex) */
170    const char *prime;
171 
172    /** The fields A param (hex) */
173    const char *A;
174 
175    /** The fields B param (hex) */
176    const char *B;
177 
178    /** The order of the curve (hex) */
179    const char *order;
180 
181    /** The x co-ordinate of the base point on the curve (hex) */
182    const char *Gx;
183 
184    /** The y co-ordinate of the base point on the curve (hex) */
185    const char *Gy;
186 
187    /** The co-factor */
188    unsigned long cofactor;
189 
190    /** The OID */
191    const char *OID;
192 } ltc_ecc_curve;
193 
194 /** 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 */
195 typedef struct {
196     /** The x co-ordinate */
197     void *x;
198 
199     /** The y co-ordinate */
200     void *y;
201 
202     /** The z co-ordinate */
203     void *z;
204 } ecc_point;
205 
206 /** ECC key's domain parameters */
207 typedef struct {
208    /** The size of the curve in octets */
209    int size;
210    /** The prime that defines the field the curve is in */
211    void *prime;
212    /** The fields A param */
213    void *A;
214    /** The fields B param */
215    void *B;
216    /** The order of the curve */
217    void *order;
218    /** The base point G on the curve */
219    ecc_point base;
220    /** The co-factor */
221    unsigned long cofactor;
222    /** The OID */
223    unsigned long oid[16];
224    unsigned long oidlen;
225 } ltc_ecc_dp;
226 
227 /** An ECC key */
228 typedef struct {
229     /** Type of key, PK_PRIVATE or PK_PUBLIC */
230     int type;
231 
232     /** Structure with domain parameters */
233     ltc_ecc_dp dp;
234 
235     /** Structure with the public key */
236     ecc_point pubkey;
237 
238     /** The private key */
239     void *k;
240 } ecc_key;
241 
242 /** Formats of ECC signatures */
243 typedef enum ecc_signature_type_ {
244    /* ASN.1 encoded, ANSI X9.62 */
245    LTC_ECCSIG_ANSIX962   = 0x0,
246    /* raw R, S values */
247    LTC_ECCSIG_RFC7518    = 0x1,
248    /* raw R, S, V (+27) values */
249    LTC_ECCSIG_ETH27      = 0x2,
250    /* SSH + ECDSA signature format defined by RFC5656 */
251    LTC_ECCSIG_RFC5656    = 0x3,
252 } ecc_signature_type;
253 
254 /** the ECC params provided */
255 extern const ltc_ecc_curve ltc_ecc_curves[];
256 
257 void ecc_sizes(int *low, int *high);
258 int  ecc_get_size(const ecc_key *key);
259 
260 int  ecc_find_curve(const char* name_or_oid, const ltc_ecc_curve** cu);
261 int  ecc_set_curve(const ltc_ecc_curve *cu, ecc_key *key);
262 int  ecc_generate_key(prng_state *prng, int wprng, ecc_key *key);
263 int  ecc_set_key(const unsigned char *in, unsigned long inlen, int type, ecc_key *key);
264 int  ecc_get_key(unsigned char *out, unsigned long *outlen, int type, const ecc_key *key);
265 int  ecc_get_oid_str(char *out, unsigned long *outlen, const ecc_key *key);
266 
267 int  ecc_make_key(prng_state *prng, int wprng, int keysize, ecc_key *key);
268 int  ecc_make_key_ex(prng_state *prng, int wprng, ecc_key *key, const ltc_ecc_curve *cu);
269 void ecc_free(ecc_key *key);
270 
271 int  ecc_export(unsigned char *out, unsigned long *outlen, int type, const ecc_key *key);
272 int  ecc_import(const unsigned char *in, unsigned long inlen, ecc_key *key);
273 int  ecc_import_ex(const unsigned char *in, unsigned long inlen, ecc_key *key, const ltc_ecc_curve *cu);
274 
275 int ecc_ansi_x963_export(const ecc_key *key, unsigned char *out, unsigned long *outlen);
276 int ecc_ansi_x963_import(const unsigned char *in, unsigned long inlen, ecc_key *key);
277 int ecc_ansi_x963_import_ex(const unsigned char *in, unsigned long inlen, ecc_key *key, const ltc_ecc_curve *cu);
278 
279 int ecc_export_openssl(unsigned char *out, unsigned long *outlen, int type, const ecc_key *key);
280 int ecc_import_openssl(const unsigned char *in, unsigned long inlen, ecc_key *key);
281 int ecc_import_pkcs8(const unsigned char *in, unsigned long inlen, const void *pwd, unsigned long pwdlen, ecc_key *key);
282 int ecc_import_x509(const unsigned char *in, unsigned long inlen, ecc_key *key);
283 
284 int  ecc_shared_secret(const ecc_key *private_key, const ecc_key *public_key,
285                        unsigned char *out, unsigned long *outlen);
286 
287 int  ecc_encrypt_key(const unsigned char *in,   unsigned long inlen,
288                            unsigned char *out,  unsigned long *outlen,
289                            prng_state *prng, int wprng, int hash,
290                            const ecc_key *key);
291 
292 int  ecc_decrypt_key(const unsigned char *in,  unsigned long  inlen,
293                            unsigned char *out, unsigned long *outlen,
294                            const ecc_key *key);
295 
296 #define ecc_sign_hash_rfc7518(in_, inlen_, out_, outlen_, prng_, wprng_, key_) \
297    ecc_sign_hash_ex(in_, inlen_, out_, outlen_, prng_, wprng_, LTC_ECCSIG_RFC7518, NULL, key_)
298 
299 #define ecc_sign_hash(in_, inlen_, out_, outlen_, prng_, wprng_, key_) \
300    ecc_sign_hash_ex(in_, inlen_, out_, outlen_, prng_, wprng_, LTC_ECCSIG_ANSIX962, NULL, key_)
301 
302 #define ecc_verify_hash_rfc7518(sig_, siglen_, hash_, hashlen_, stat_, key_) \
303    ecc_verify_hash_ex(sig_, siglen_, hash_, hashlen_, LTC_ECCSIG_RFC7518, stat_, key_)
304 
305 #define ecc_verify_hash(sig_, siglen_, hash_, hashlen_, stat_, key_) \
306    ecc_verify_hash_ex(sig_, siglen_, hash_, hashlen_, LTC_ECCSIG_ANSIX962, stat_, key_)
307 
308 int  ecc_sign_hash_ex(const unsigned char *in,  unsigned long inlen,
309                             unsigned char *out, unsigned long *outlen,
310                             prng_state *prng, int wprng, ecc_signature_type sigformat,
311                             int *recid, const ecc_key *key);
312 
313 int  ecc_verify_hash_ex(const unsigned char *sig,  unsigned long siglen,
314                         const unsigned char *hash, unsigned long hashlen,
315                         ecc_signature_type sigformat, int *stat, const ecc_key *key);
316 
317 int  ecc_recover_key(const unsigned char *sig,  unsigned long siglen,
318                      const unsigned char *hash, unsigned long hashlen,
319                      int recid, ecc_signature_type sigformat, ecc_key *key);
320 
321 #endif
322 
323 #ifdef LTC_CURVE25519
324 
325 typedef struct {
326    /** The key type, PK_PRIVATE or PK_PUBLIC */
327    enum public_key_type type;
328 
329    /** The PK-algorithm, PKA_ED25519 or PKA_X25519 */
330    /** This was supposed to be:
331     * enum public_key_algorithms algo;
332     * but that enum is now in tomcrypt_private.h
333     */
334    int algo;
335 
336    /** The private key */
337    unsigned char priv[32];
338 
339    /** The public key */
340    unsigned char pub[32];
341 } curve25519_key;
342 
343 
344 /** Ed25519 Signature API */
345 int ed25519_make_key(prng_state *prng, int wprng, curve25519_key *key);
346 
347 int ed25519_export(       unsigned char *out, unsigned long *outlen,
348                                     int  which,
349                    const curve25519_key *key);
350 
351 int ed25519_import(const unsigned char *in, unsigned long inlen, curve25519_key *key);
352 int ed25519_import_raw(const unsigned char *in, unsigned long inlen, int which, curve25519_key *key);
353 int ed25519_import_x509(const unsigned char *in, unsigned long inlen, curve25519_key *key);
354 int ed25519_import_pkcs8(const unsigned char *in, unsigned long inlen,
355                                   const void *pwd, unsigned long pwdlen,
356                               curve25519_key *key);
357 
358 int ed25519_sign(const unsigned char  *msg, unsigned long msglen,
359                        unsigned char  *sig, unsigned long *siglen,
360                  const curve25519_key *private_key);
361 
362 int ed25519_verify(const  unsigned char *msg, unsigned long msglen,
363                    const  unsigned char *sig, unsigned long siglen,
364                    int *stat, const curve25519_key *public_key);
365 
366 /** X25519 Key-Exchange API */
367 int x25519_make_key(prng_state *prng, int wprng, curve25519_key *key);
368 
369 int x25519_export(       unsigned char *out, unsigned long *outlen,
370                                    int  which,
371                   const curve25519_key *key);
372 
373 int x25519_import(const unsigned char *in, unsigned long inlen, curve25519_key *key);
374 int x25519_import_raw(const unsigned char *in, unsigned long inlen, int which, curve25519_key *key);
375 int x25519_import_x509(const unsigned char *in, unsigned long inlen, curve25519_key *key);
376 int x25519_import_pkcs8(const unsigned char *in, unsigned long inlen,
377                                  const void *pwd, unsigned long pwdlen,
378                              curve25519_key *key);
379 
380 int x25519_shared_secret(const curve25519_key *private_key,
381                          const curve25519_key *public_key,
382                                 unsigned char *out, unsigned long *outlen);
383 
384 #endif /* LTC_CURVE25519 */
385 
386 #ifdef LTC_MDSA
387 
388 /* Max diff between group and modulus size in bytes (max case: L=8192bits, N=256bits) */
389 #define LTC_MDSA_DELTA 992
390 
391 /* Max DSA group size in bytes */
392 #define LTC_MDSA_MAX_GROUP 64
393 
394 /* Max DSA modulus size in bytes (the actual DSA size, max 8192 bits) */
395 #define LTC_MDSA_MAX_MODULUS 1024
396 
397 /** DSA key structure */
398 typedef struct {
399    /** The key type, PK_PRIVATE or PK_PUBLIC */
400    int type;
401 
402    /** The order of the sub-group used in octets */
403    int qord;
404 
405    /** The generator  */
406    void *g;
407 
408    /** The prime used to generate the sub-group */
409    void *q;
410 
411    /** The large prime that generats the field the contains the sub-group */
412    void *p;
413 
414    /** The private key */
415    void *x;
416 
417    /** The public key */
418    void *y;
419 } dsa_key;
420 
421 int dsa_make_key(prng_state *prng, int wprng, int group_size, int modulus_size, dsa_key *key);
422 
423 int dsa_set_pqg(const unsigned char *p,  unsigned long plen,
424                 const unsigned char *q,  unsigned long qlen,
425                 const unsigned char *g,  unsigned long glen,
426                 dsa_key *key);
427 int dsa_set_pqg_dsaparam(const unsigned char *dsaparam, unsigned long dsaparamlen, dsa_key *key);
428 int dsa_generate_pqg(prng_state *prng, int wprng, int group_size, int modulus_size, dsa_key *key);
429 
430 int dsa_set_key(const unsigned char *in, unsigned long inlen, int type, dsa_key *key);
431 int dsa_generate_key(prng_state *prng, int wprng, dsa_key *key);
432 
433 void dsa_free(dsa_key *key);
434 
435 int dsa_sign_hash_raw(const unsigned char *in,  unsigned long inlen,
436                                    void *r,   void *s,
437                                prng_state *prng, int wprng, const dsa_key *key);
438 
439 int dsa_sign_hash(const unsigned char *in,  unsigned long inlen,
440                         unsigned char *out, unsigned long *outlen,
441                         prng_state *prng, int wprng, const dsa_key *key);
442 
443 int dsa_verify_hash_raw(         void *r,          void *s,
444                     const unsigned char *hash, unsigned long hashlen,
445                                     int *stat, const dsa_key *key);
446 
447 int dsa_verify_hash(const unsigned char *sig,        unsigned long  siglen,
448                     const unsigned char *hash,       unsigned long  hashlen,
449                           int           *stat, const dsa_key       *key);
450 
451 int dsa_encrypt_key(const unsigned char *in,   unsigned long inlen,
452                           unsigned char *out,  unsigned long *outlen,
453                           prng_state    *prng, int wprng, int hash,
454                     const dsa_key       *key);
455 
456 int dsa_decrypt_key(const unsigned char *in,  unsigned long  inlen,
457                           unsigned char *out, unsigned long *outlen,
458                     const dsa_key       *key);
459 
460 int dsa_import(const unsigned char *in, unsigned long inlen, dsa_key *key);
461 int dsa_export(unsigned char *out, unsigned long *outlen, int type, const dsa_key *key);
462 int dsa_verify_key(const dsa_key *key, int *stat);
463 int dsa_shared_secret(void          *private_key, void *base,
464                       const dsa_key *public_key,
465                       unsigned char *out,         unsigned long *outlen);
466 #endif /* LTC_MDSA */
467 
468 #ifdef LTC_DER
469 /* DER handling */
470 
471 typedef enum ltc_asn1_type_ {
472  /*  0 */
473  LTC_ASN1_EOL,
474  LTC_ASN1_BOOLEAN,
475  LTC_ASN1_INTEGER,
476  LTC_ASN1_SHORT_INTEGER,
477  LTC_ASN1_BIT_STRING,
478  /*  5 */
479  LTC_ASN1_OCTET_STRING,
480  LTC_ASN1_NULL,
481  LTC_ASN1_OBJECT_IDENTIFIER,
482  LTC_ASN1_IA5_STRING,
483  LTC_ASN1_PRINTABLE_STRING,
484  /* 10 */
485  LTC_ASN1_UTF8_STRING,
486  LTC_ASN1_UTCTIME,
487  LTC_ASN1_CHOICE,
488  LTC_ASN1_SEQUENCE,
489  LTC_ASN1_SET,
490  /* 15 */
491  LTC_ASN1_SETOF,
492  LTC_ASN1_RAW_BIT_STRING,
493  LTC_ASN1_TELETEX_STRING,
494  LTC_ASN1_GENERALIZEDTIME,
495  LTC_ASN1_CUSTOM_TYPE,
496 } ltc_asn1_type;
497 
498 typedef enum {
499    LTC_ASN1_CL_UNIVERSAL = 0x0,
500    LTC_ASN1_CL_APPLICATION = 0x1,
501    LTC_ASN1_CL_CONTEXT_SPECIFIC = 0x2,
502    LTC_ASN1_CL_PRIVATE = 0x3,
503 } ltc_asn1_class;
504 
505 typedef enum {
506    LTC_ASN1_PC_PRIMITIVE = 0x0,
507    LTC_ASN1_PC_CONSTRUCTED = 0x1,
508 } ltc_asn1_pc;
509 
510 /** A LTC ASN.1 list type */
511 typedef struct ltc_asn1_list_ {
512    /** The LTC ASN.1 enumerated type identifier */
513    ltc_asn1_type type;
514    /** The data to encode or place for decoding */
515    void         *data;
516    /** The size of the input or resulting output */
517    unsigned long size;
518    /** The used flag
519     * 1. This is used by the CHOICE ASN.1 type to indicate which choice was made
520     * 2. This is used by the ASN.1 decoder to indicate if an element is used
521     * 3. This is used by the flexi-decoder to indicate the first byte of the identifier */
522    int           used;
523    /** Flag used to indicate optional items in ASN.1 sequences */
524    int           optional;
525    /** ASN.1 identifier */
526    ltc_asn1_class klass;
527    ltc_asn1_pc    pc;
528    ulong64        tag;
529    /** prev/next entry in the list */
530    struct ltc_asn1_list_ *prev, *next, *child, *parent;
531 } ltc_asn1_list;
532 
533 #define LTC_SET_ASN1(list, index, Type, Data, Size)  \
534    do {                                              \
535       int LTC_MACRO_temp            = (index);       \
536       ltc_asn1_list *LTC_MACRO_list = (list);        \
537       LTC_MACRO_list[LTC_MACRO_temp].type = (Type);  \
538       LTC_MACRO_list[LTC_MACRO_temp].data = (void*)(Data);  \
539       LTC_MACRO_list[LTC_MACRO_temp].size = (Size);  \
540       LTC_MACRO_list[LTC_MACRO_temp].used = 0;       \
541       LTC_MACRO_list[LTC_MACRO_temp].optional = 0;   \
542       LTC_MACRO_list[LTC_MACRO_temp].klass = 0;      \
543       LTC_MACRO_list[LTC_MACRO_temp].pc = 0;         \
544       LTC_MACRO_list[LTC_MACRO_temp].tag = 0;        \
545    } while (0)
546 
547 #define LTC_SET_ASN1_IDENTIFIER(list, index, Class, Pc, Tag)      \
548    do {                                                           \
549       int LTC_MACRO_temp            = (index);                    \
550       ltc_asn1_list *LTC_MACRO_list = (list);                     \
551       LTC_MACRO_list[LTC_MACRO_temp].type = LTC_ASN1_CUSTOM_TYPE; \
552       LTC_MACRO_list[LTC_MACRO_temp].klass = (Class);             \
553       LTC_MACRO_list[LTC_MACRO_temp].pc = (Pc);                   \
554       LTC_MACRO_list[LTC_MACRO_temp].tag = (Tag);                 \
555    } while (0)
556 
557 #define LTC_SET_ASN1_CUSTOM_CONSTRUCTED(list, index, Class, Tag, Data)    \
558    do {                                                           \
559       int LTC_MACRO_temp##__LINE__ = (index);                     \
560       LTC_SET_ASN1(list, LTC_MACRO_temp##__LINE__, LTC_ASN1_CUSTOM_TYPE, Data, 1);   \
561       LTC_SET_ASN1_IDENTIFIER(list, LTC_MACRO_temp##__LINE__, Class, LTC_ASN1_PC_CONSTRUCTED, Tag);       \
562    } while (0)
563 
564 #define LTC_SET_ASN1_CUSTOM_PRIMITIVE(list, index, Class, Tag, Type, Data, Size)    \
565    do {                                                           \
566       int LTC_MACRO_temp##__LINE__ = (index);                     \
567       LTC_SET_ASN1(list, LTC_MACRO_temp##__LINE__, LTC_ASN1_CUSTOM_TYPE, Data, Size);   \
568       LTC_SET_ASN1_IDENTIFIER(list, LTC_MACRO_temp##__LINE__, Class, LTC_ASN1_PC_PRIMITIVE, Tag);       \
569       list[LTC_MACRO_temp##__LINE__].used = (int)(Type);       \
570    } while (0)
571 
572 extern const char*          der_asn1_class_to_string_map[];
573 extern const unsigned long  der_asn1_class_to_string_map_sz;
574 
575 extern const char*          der_asn1_pc_to_string_map[];
576 extern const unsigned long  der_asn1_pc_to_string_map_sz;
577 
578 extern const char*          der_asn1_tag_to_string_map[];
579 extern const unsigned long  der_asn1_tag_to_string_map_sz;
580 
581 /* SEQUENCE */
582 int der_encode_sequence_ex(const ltc_asn1_list *list, unsigned long inlen,
583                            unsigned char *out,        unsigned long *outlen, int type_of);
584 
585 #define der_encode_sequence(list, inlen, out, outlen) der_encode_sequence_ex(list, inlen, out, outlen, LTC_ASN1_SEQUENCE)
586 
587 /** The supported bitmap for all the
588  * decoders with a `flags` argument.
589  */
590 enum ltc_der_seq {
591    LTC_DER_SEQ_ZERO = 0x0u,
592 
593    /** Bit0  - [0]=Unordered (SET or SETOF)
594     *          [1]=Ordered (SEQUENCE) */
595    LTC_DER_SEQ_UNORDERED = LTC_DER_SEQ_ZERO,
596    LTC_DER_SEQ_ORDERED = 0x1u,
597 
598    /** Bit1  - [0]=Relaxed
599     *          [1]=Strict */
600    LTC_DER_SEQ_RELAXED = LTC_DER_SEQ_ZERO,
601    LTC_DER_SEQ_STRICT = 0x2u,
602 
603    /** Alternative naming */
604    LTC_DER_SEQ_SET = LTC_DER_SEQ_UNORDERED,
605    LTC_DER_SEQ_SEQUENCE = LTC_DER_SEQ_ORDERED,
606 };
607 
608 int der_decode_sequence_ex(const unsigned char *in, unsigned long  inlen,
609                            ltc_asn1_list *list,     unsigned long  outlen, unsigned int flags);
610 
611 #define der_decode_sequence(in, inlen, list, outlen) der_decode_sequence_ex(in, inlen, list, outlen, LTC_DER_SEQ_SEQUENCE | LTC_DER_SEQ_RELAXED)
612 #define der_decode_sequence_strict(in, inlen, list, outlen) der_decode_sequence_ex(in, inlen, list, outlen, LTC_DER_SEQ_SEQUENCE | LTC_DER_SEQ_STRICT)
613 
614 int der_length_sequence(const ltc_asn1_list *list, unsigned long inlen,
615                         unsigned long *outlen);
616 
617 
618 /* Custom-types */
619 int der_encode_custom_type(const ltc_asn1_list *root,
620                                  unsigned char *out, unsigned long *outlen);
621 
622 int der_decode_custom_type(const unsigned char *in, unsigned long inlen,
623                                  ltc_asn1_list *root);
624 
625 int der_length_custom_type(const ltc_asn1_list *root,
626                                  unsigned long *outlen,
627                                  unsigned long *payloadlen);
628 
629 /* SET */
630 #define der_decode_set(in, inlen, list, outlen) der_decode_sequence_ex(in, inlen, list, outlen, LTC_DER_SEQ_SET)
631 #define der_length_set der_length_sequence
632 int der_encode_set(const ltc_asn1_list *list, unsigned long inlen,
633                    unsigned char *out,        unsigned long *outlen);
634 
635 int der_encode_setof(const ltc_asn1_list *list, unsigned long inlen,
636                      unsigned char *out,        unsigned long *outlen);
637 
638 /* VA list handy helpers with triplets of <type, size, data> */
639 int der_encode_sequence_multi(unsigned char *out, unsigned long *outlen, ...);
640 int der_decode_sequence_multi(const unsigned char *in, unsigned long inlen, ...);
641 
642 /* FLEXI DECODER handle unknown list decoder */
643 int  der_decode_sequence_flexi(const unsigned char *in, unsigned long *inlen, ltc_asn1_list **out);
644 #define der_free_sequence_flexi         der_sequence_free
645 void der_sequence_free(ltc_asn1_list *in);
646 void der_sequence_shrink(ltc_asn1_list *in);
647 
648 /* BOOLEAN */
649 int der_length_boolean(unsigned long *outlen);
650 int der_encode_boolean(int in,
651                        unsigned char *out, unsigned long *outlen);
652 int der_decode_boolean(const unsigned char *in, unsigned long inlen,
653                                        int *out);
654 /* INTEGER */
655 int der_encode_integer(void *num, unsigned char *out, unsigned long *outlen);
656 int der_decode_integer(const unsigned char *in, unsigned long inlen, void *num);
657 int der_length_integer(void *num, unsigned long *outlen);
658 
659 /* INTEGER -- handy for 0..2^32-1 values */
660 int der_decode_short_integer(const unsigned char *in, unsigned long inlen, unsigned long *num);
661 int der_encode_short_integer(unsigned long num, unsigned char *out, unsigned long *outlen);
662 int der_length_short_integer(unsigned long num, unsigned long *outlen);
663 
664 /* BIT STRING */
665 int der_encode_bit_string(const unsigned char *in, unsigned long inlen,
666                                 unsigned char *out, unsigned long *outlen);
667 int der_decode_bit_string(const unsigned char *in, unsigned long inlen,
668                                 unsigned char *out, unsigned long *outlen);
669 int der_encode_raw_bit_string(const unsigned char *in, unsigned long inlen,
670                                 unsigned char *out, unsigned long *outlen);
671 int der_decode_raw_bit_string(const unsigned char *in, unsigned long inlen,
672                                 unsigned char *out, unsigned long *outlen);
673 int der_length_bit_string(unsigned long nbits, unsigned long *outlen);
674 
675 /* OCTET STRING */
676 int der_encode_octet_string(const unsigned char *in, unsigned long inlen,
677                                   unsigned char *out, unsigned long *outlen);
678 int der_decode_octet_string(const unsigned char *in, unsigned long inlen,
679                                   unsigned char *out, unsigned long *outlen);
680 int der_length_octet_string(unsigned long noctets, unsigned long *outlen);
681 
682 /* OBJECT IDENTIFIER */
683 int der_encode_object_identifier(const unsigned long *words, unsigned long  nwords,
684                                        unsigned char *out,   unsigned long *outlen);
685 int der_decode_object_identifier(const unsigned char *in,    unsigned long  inlen,
686                                        unsigned long *words, unsigned long *outlen);
687 int der_length_object_identifier(const unsigned long *words, unsigned long nwords, unsigned long *outlen);
688 unsigned long der_object_identifier_bits(unsigned long x);
689 
690 /* IA5 STRING */
691 int der_encode_ia5_string(const unsigned char *in, unsigned long inlen,
692                                 unsigned char *out, unsigned long *outlen);
693 int der_decode_ia5_string(const unsigned char *in, unsigned long inlen,
694                                 unsigned char *out, unsigned long *outlen);
695 int der_length_ia5_string(const unsigned char *octets, unsigned long noctets, unsigned long *outlen);
696 
697 int der_ia5_char_encode(int c);
698 int der_ia5_value_decode(int v);
699 
700 /* TELETEX STRING */
701 int der_decode_teletex_string(const unsigned char *in, unsigned long inlen,
702                                 unsigned char *out, unsigned long *outlen);
703 int der_length_teletex_string(const unsigned char *octets, unsigned long noctets, unsigned long *outlen);
704 
705 /* PRINTABLE STRING */
706 int der_encode_printable_string(const unsigned char *in, unsigned long inlen,
707                                 unsigned char *out, unsigned long *outlen);
708 int der_decode_printable_string(const unsigned char *in, unsigned long inlen,
709                                 unsigned char *out, unsigned long *outlen);
710 int der_length_printable_string(const unsigned char *octets, unsigned long noctets, unsigned long *outlen);
711 
712 int der_printable_char_encode(int c);
713 int der_printable_value_decode(int v);
714 
715 /* UTF-8 */
716 #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)
717    #if defined(__WCHAR_MAX__)
718       #define LTC_WCHAR_MAX __WCHAR_MAX__
719    #else
720       #include <wchar.h>
721       #define LTC_WCHAR_MAX WCHAR_MAX
722    #endif
723 /* please note that it might happen that LTC_WCHAR_MAX is undefined */
724 #else
725    typedef ulong32 wchar_t;
726    #define LTC_WCHAR_MAX 0xFFFFFFFF
727 #endif
728 
729 int der_encode_utf8_string(const wchar_t *in,  unsigned long inlen,
730                            unsigned char *out, unsigned long *outlen);
731 
732 int der_decode_utf8_string(const unsigned char *in,  unsigned long inlen,
733                                        wchar_t *out, unsigned long *outlen);
734 unsigned long der_utf8_charsize(const wchar_t c);
735 int der_length_utf8_string(const wchar_t *in, unsigned long noctets, unsigned long *outlen);
736 
737 
738 /* CHOICE */
739 int der_decode_choice(const unsigned char *in,   unsigned long *inlen,
740                             ltc_asn1_list *list, unsigned long  outlen);
741 
742 /* UTCTime */
743 typedef struct {
744    unsigned YY, /* year */
745             MM, /* month */
746             DD, /* day */
747             hh, /* hour */
748             mm, /* minute */
749             ss, /* second */
750             off_dir, /* timezone offset direction 0 == +, 1 == - */
751             off_hh, /* timezone offset hours */
752             off_mm; /* timezone offset minutes */
753 } ltc_utctime;
754 
755 int der_encode_utctime(const ltc_utctime   *utctime,
756                              unsigned char *out,   unsigned long *outlen);
757 
758 int der_decode_utctime(const unsigned char *in, unsigned long *inlen,
759                              ltc_utctime   *out);
760 
761 int der_length_utctime(const ltc_utctime *utctime, unsigned long *outlen);
762 
763 /* GeneralizedTime */
764 typedef struct {
765    unsigned YYYY, /* year */
766             MM, /* month */
767             DD, /* day */
768             hh, /* hour */
769             mm, /* minute */
770             ss, /* second */
771             fs, /* fractional seconds */
772             off_dir, /* timezone offset direction 0 == +, 1 == - */
773             off_hh, /* timezone offset hours */
774             off_mm; /* timezone offset minutes */
775 } ltc_generalizedtime;
776 
777 int der_encode_generalizedtime(const ltc_generalizedtime *gtime,
778                                      unsigned char       *out, unsigned long *outlen);
779 
780 int der_decode_generalizedtime(const unsigned char *in, unsigned long *inlen,
781                                ltc_generalizedtime *out);
782 
783 int der_length_generalizedtime(const ltc_generalizedtime *gtime, unsigned long *outlen);
784 
785 #endif
786