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 int dh_make_key(prng_state *prng, int wprng, void *q, int xbits, dh_key *key); /* OP-TEE */
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 int ed25519ctx_sign(const  unsigned char *msg, unsigned long  msglen,
362                            unsigned char *sig, unsigned long *siglen,
363                     const  unsigned char *ctx, unsigned long  ctxlen,
364                     const curve25519_key *private_key);
365 int ed25519ph_sign(const  unsigned char *msg, unsigned long  msglen,
366                           unsigned char *sig, unsigned long *siglen,
367                    const  unsigned char *ctx, unsigned long  ctxlen,
368                    const curve25519_key *private_key);
369 int ed25519_verify(const  unsigned char *msg, unsigned long msglen,
370                    const  unsigned char *sig, unsigned long siglen,
371                                     int *stat,
372                    const curve25519_key *public_key);
373 int ed25519ctx_verify(const  unsigned char *msg, unsigned long msglen,
374                       const  unsigned char *sig, unsigned long siglen,
375                       const  unsigned char *ctx, unsigned long ctxlen,
376                                        int *stat,
377                       const curve25519_key *public_key);
378 int ed25519ph_verify(const  unsigned char *msg, unsigned long msglen,
379                      const  unsigned char *sig, unsigned long siglen,
380                      const  unsigned char *ctx, unsigned long ctxlen,
381                                       int *stat,
382                      const curve25519_key *public_key);
383 
384 /** X25519 Key-Exchange API */
385 int x25519_make_key(prng_state *prng, int wprng, curve25519_key *key);
386 
387 int x25519_export(       unsigned char *out, unsigned long *outlen,
388                                    int  which,
389                   const curve25519_key *key);
390 
391 int x25519_import(const unsigned char *in, unsigned long inlen, curve25519_key *key);
392 int x25519_import_raw(const unsigned char *in, unsigned long inlen, int which, curve25519_key *key);
393 int x25519_import_x509(const unsigned char *in, unsigned long inlen, curve25519_key *key);
394 int x25519_import_pkcs8(const unsigned char *in, unsigned long inlen,
395                                  const void *pwd, unsigned long pwdlen,
396                              curve25519_key *key);
397 
398 int x25519_shared_secret(const curve25519_key *private_key,
399                          const curve25519_key *public_key,
400                                 unsigned char *out, unsigned long *outlen);
401 
402 #endif /* LTC_CURVE25519 */
403 
404 #ifdef LTC_MDSA
405 
406 /* Max diff between group and modulus size in bytes (max case: L=8192bits, N=256bits) */
407 #define LTC_MDSA_DELTA 992
408 
409 /* Max DSA group size in bytes */
410 #define LTC_MDSA_MAX_GROUP 64
411 
412 /* Max DSA modulus size in bytes (the actual DSA size, max 8192 bits) */
413 #define LTC_MDSA_MAX_MODULUS 1024
414 
415 /** DSA key structure */
416 typedef struct {
417    /** The key type, PK_PRIVATE or PK_PUBLIC */
418    int type;
419 
420    /** The order of the sub-group used in octets */
421    int qord;
422 
423    /** The generator  */
424    void *g;
425 
426    /** The prime used to generate the sub-group */
427    void *q;
428 
429    /** The large prime that generats the field the contains the sub-group */
430    void *p;
431 
432    /** The private key */
433    void *x;
434 
435    /** The public key */
436    void *y;
437 } dsa_key;
438 
439 int dsa_make_key(prng_state *prng, int wprng, int group_size, int modulus_size, dsa_key *key);
440 
441 int dsa_set_pqg(const unsigned char *p,  unsigned long plen,
442                 const unsigned char *q,  unsigned long qlen,
443                 const unsigned char *g,  unsigned long glen,
444                 dsa_key *key);
445 int dsa_set_pqg_dsaparam(const unsigned char *dsaparam, unsigned long dsaparamlen, dsa_key *key);
446 int dsa_generate_pqg(prng_state *prng, int wprng, int group_size, int modulus_size, dsa_key *key);
447 
448 int dsa_set_key(const unsigned char *in, unsigned long inlen, int type, dsa_key *key);
449 int dsa_generate_key(prng_state *prng, int wprng, dsa_key *key);
450 
451 void dsa_free(dsa_key *key);
452 
453 int dsa_sign_hash_raw(const unsigned char *in,  unsigned long inlen,
454                                    void *r,   void *s,
455                                prng_state *prng, int wprng, const dsa_key *key);
456 
457 int dsa_sign_hash(const unsigned char *in,  unsigned long inlen,
458                         unsigned char *out, unsigned long *outlen,
459                         prng_state *prng, int wprng, const dsa_key *key);
460 
461 int dsa_verify_hash_raw(         void *r,          void *s,
462                     const unsigned char *hash, unsigned long hashlen,
463                                     int *stat, const dsa_key *key);
464 
465 int dsa_verify_hash(const unsigned char *sig,        unsigned long  siglen,
466                     const unsigned char *hash,       unsigned long  hashlen,
467                           int           *stat, const dsa_key       *key);
468 
469 int dsa_encrypt_key(const unsigned char *in,   unsigned long inlen,
470                           unsigned char *out,  unsigned long *outlen,
471                           prng_state    *prng, int wprng, int hash,
472                     const dsa_key       *key);
473 
474 int dsa_decrypt_key(const unsigned char *in,  unsigned long  inlen,
475                           unsigned char *out, unsigned long *outlen,
476                     const dsa_key       *key);
477 
478 int dsa_import(const unsigned char *in, unsigned long inlen, dsa_key *key);
479 int dsa_export(unsigned char *out, unsigned long *outlen, int type, const dsa_key *key);
480 int dsa_verify_key(const dsa_key *key, int *stat);
481 int dsa_shared_secret(void          *private_key, void *base,
482                       const dsa_key *public_key,
483                       unsigned char *out,         unsigned long *outlen);
484 #endif /* LTC_MDSA */
485 
486 #ifdef LTC_DER
487 /* DER handling */
488 
489 typedef enum ltc_asn1_type_ {
490  /*  0 */
491  LTC_ASN1_EOL,
492  LTC_ASN1_BOOLEAN,
493  LTC_ASN1_INTEGER,
494  LTC_ASN1_SHORT_INTEGER,
495  LTC_ASN1_BIT_STRING,
496  /*  5 */
497  LTC_ASN1_OCTET_STRING,
498  LTC_ASN1_NULL,
499  LTC_ASN1_OBJECT_IDENTIFIER,
500  LTC_ASN1_IA5_STRING,
501  LTC_ASN1_PRINTABLE_STRING,
502  /* 10 */
503  LTC_ASN1_UTF8_STRING,
504  LTC_ASN1_UTCTIME,
505  LTC_ASN1_CHOICE,
506  LTC_ASN1_SEQUENCE,
507  LTC_ASN1_SET,
508  /* 15 */
509  LTC_ASN1_SETOF,
510  LTC_ASN1_RAW_BIT_STRING,
511  LTC_ASN1_TELETEX_STRING,
512  LTC_ASN1_GENERALIZEDTIME,
513  LTC_ASN1_CUSTOM_TYPE,
514 } ltc_asn1_type;
515 
516 typedef enum {
517    LTC_ASN1_CL_UNIVERSAL = 0x0,
518    LTC_ASN1_CL_APPLICATION = 0x1,
519    LTC_ASN1_CL_CONTEXT_SPECIFIC = 0x2,
520    LTC_ASN1_CL_PRIVATE = 0x3,
521 } ltc_asn1_class;
522 
523 typedef enum {
524    LTC_ASN1_PC_PRIMITIVE = 0x0,
525    LTC_ASN1_PC_CONSTRUCTED = 0x1,
526 } ltc_asn1_pc;
527 
528 /** A LTC ASN.1 list type */
529 typedef struct ltc_asn1_list_ {
530    /** The LTC ASN.1 enumerated type identifier */
531    ltc_asn1_type type;
532    /** The data to encode or place for decoding */
533    void         *data;
534    /** The size of the input or resulting output */
535    unsigned long size;
536    /** The used flag
537     * 1. This is used by the CHOICE ASN.1 type to indicate which choice was made
538     * 2. This is used by the ASN.1 decoder to indicate if an element is used
539     * 3. This is used by the flexi-decoder to indicate the first byte of the identifier */
540    int           used;
541    /** Flag used to indicate optional items in ASN.1 sequences */
542    int           optional;
543    /** ASN.1 identifier */
544    ltc_asn1_class klass;
545    ltc_asn1_pc    pc;
546    ulong64        tag;
547    /** prev/next entry in the list */
548    struct ltc_asn1_list_ *prev, *next, *child, *parent;
549 } ltc_asn1_list;
550 
551 #define LTC_SET_ASN1(list, index, Type, Data, Size)  \
552    do {                                              \
553       int LTC_MACRO_temp            = (index);       \
554       ltc_asn1_list *LTC_MACRO_list = (list);        \
555       LTC_MACRO_list[LTC_MACRO_temp].type = (Type);  \
556       LTC_MACRO_list[LTC_MACRO_temp].data = (void*)(Data);  \
557       LTC_MACRO_list[LTC_MACRO_temp].size = (Size);  \
558       LTC_MACRO_list[LTC_MACRO_temp].used = 0;       \
559       LTC_MACRO_list[LTC_MACRO_temp].optional = 0;   \
560       LTC_MACRO_list[LTC_MACRO_temp].klass = 0;      \
561       LTC_MACRO_list[LTC_MACRO_temp].pc = 0;         \
562       LTC_MACRO_list[LTC_MACRO_temp].tag = 0;        \
563    } while (0)
564 
565 #define LTC_SET_ASN1_IDENTIFIER(list, index, Class, Pc, Tag)      \
566    do {                                                           \
567       int LTC_MACRO_temp            = (index);                    \
568       ltc_asn1_list *LTC_MACRO_list = (list);                     \
569       LTC_MACRO_list[LTC_MACRO_temp].type = LTC_ASN1_CUSTOM_TYPE; \
570       LTC_MACRO_list[LTC_MACRO_temp].klass = (Class);             \
571       LTC_MACRO_list[LTC_MACRO_temp].pc = (Pc);                   \
572       LTC_MACRO_list[LTC_MACRO_temp].tag = (Tag);                 \
573    } while (0)
574 
575 #define LTC_SET_ASN1_CUSTOM_CONSTRUCTED(list, index, Class, Tag, Data)    \
576    do {                                                           \
577       int LTC_MACRO_temp##__LINE__ = (index);                     \
578       LTC_SET_ASN1(list, LTC_MACRO_temp##__LINE__, LTC_ASN1_CUSTOM_TYPE, Data, 1);   \
579       LTC_SET_ASN1_IDENTIFIER(list, LTC_MACRO_temp##__LINE__, Class, LTC_ASN1_PC_CONSTRUCTED, Tag);       \
580    } while (0)
581 
582 #define LTC_SET_ASN1_CUSTOM_PRIMITIVE(list, index, Class, Tag, Type, Data, Size)    \
583    do {                                                           \
584       int LTC_MACRO_temp##__LINE__ = (index);                     \
585       LTC_SET_ASN1(list, LTC_MACRO_temp##__LINE__, LTC_ASN1_CUSTOM_TYPE, Data, Size);   \
586       LTC_SET_ASN1_IDENTIFIER(list, LTC_MACRO_temp##__LINE__, Class, LTC_ASN1_PC_PRIMITIVE, Tag);       \
587       list[LTC_MACRO_temp##__LINE__].used = (int)(Type);       \
588    } while (0)
589 
590 extern const char*          der_asn1_class_to_string_map[];
591 extern const unsigned long  der_asn1_class_to_string_map_sz;
592 
593 extern const char*          der_asn1_pc_to_string_map[];
594 extern const unsigned long  der_asn1_pc_to_string_map_sz;
595 
596 extern const char*          der_asn1_tag_to_string_map[];
597 extern const unsigned long  der_asn1_tag_to_string_map_sz;
598 
599 /* SEQUENCE */
600 int der_encode_sequence_ex(const ltc_asn1_list *list, unsigned long inlen,
601                            unsigned char *out,        unsigned long *outlen, int type_of);
602 
603 #define der_encode_sequence(list, inlen, out, outlen) der_encode_sequence_ex(list, inlen, out, outlen, LTC_ASN1_SEQUENCE)
604 
605 /** The supported bitmap for all the
606  * decoders with a `flags` argument.
607  */
608 enum ltc_der_seq {
609    LTC_DER_SEQ_ZERO = 0x0u,
610 
611    /** Bit0  - [0]=Unordered (SET or SETOF)
612     *          [1]=Ordered (SEQUENCE) */
613    LTC_DER_SEQ_UNORDERED = LTC_DER_SEQ_ZERO,
614    LTC_DER_SEQ_ORDERED = 0x1u,
615 
616    /** Bit1  - [0]=Relaxed
617     *          [1]=Strict */
618    LTC_DER_SEQ_RELAXED = LTC_DER_SEQ_ZERO,
619    LTC_DER_SEQ_STRICT = 0x2u,
620 
621    /** Alternative naming */
622    LTC_DER_SEQ_SET = LTC_DER_SEQ_UNORDERED,
623    LTC_DER_SEQ_SEQUENCE = LTC_DER_SEQ_ORDERED,
624 };
625 
626 int der_decode_sequence_ex(const unsigned char *in, unsigned long  inlen,
627                            ltc_asn1_list *list,     unsigned long  outlen, unsigned int flags);
628 
629 #define der_decode_sequence(in, inlen, list, outlen) der_decode_sequence_ex(in, inlen, list, outlen, LTC_DER_SEQ_SEQUENCE | LTC_DER_SEQ_RELAXED)
630 #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)
631 
632 int der_length_sequence(const ltc_asn1_list *list, unsigned long inlen,
633                         unsigned long *outlen);
634 
635 
636 /* Custom-types */
637 int der_encode_custom_type(const ltc_asn1_list *root,
638                                  unsigned char *out, unsigned long *outlen);
639 
640 int der_decode_custom_type(const unsigned char *in, unsigned long inlen,
641                                  ltc_asn1_list *root);
642 
643 int der_length_custom_type(const ltc_asn1_list *root,
644                                  unsigned long *outlen,
645                                  unsigned long *payloadlen);
646 
647 /* SET */
648 #define der_decode_set(in, inlen, list, outlen) der_decode_sequence_ex(in, inlen, list, outlen, LTC_DER_SEQ_SET)
649 #define der_length_set der_length_sequence
650 int der_encode_set(const ltc_asn1_list *list, unsigned long inlen,
651                    unsigned char *out,        unsigned long *outlen);
652 
653 int der_encode_setof(const ltc_asn1_list *list, unsigned long inlen,
654                      unsigned char *out,        unsigned long *outlen);
655 
656 /* VA list handy helpers with triplets of <type, size, data> */
657 int der_encode_sequence_multi(unsigned char *out, unsigned long *outlen, ...) LTC_NULL_TERMINATED;
658 int der_decode_sequence_multi(const unsigned char *in, unsigned long inlen, ...) LTC_NULL_TERMINATED;
659 
660 /* FLEXI DECODER handle unknown list decoder */
661 int  der_decode_sequence_flexi(const unsigned char *in, unsigned long *inlen, ltc_asn1_list **out);
662 #define der_free_sequence_flexi         der_sequence_free
663 void der_sequence_free(ltc_asn1_list *in);
664 void der_sequence_shrink(ltc_asn1_list *in);
665 
666 /* BOOLEAN */
667 int der_length_boolean(unsigned long *outlen);
668 int der_encode_boolean(int in,
669                        unsigned char *out, unsigned long *outlen);
670 int der_decode_boolean(const unsigned char *in, unsigned long inlen,
671                                        int *out);
672 /* INTEGER */
673 int der_encode_integer(void *num, unsigned char *out, unsigned long *outlen);
674 int der_decode_integer(const unsigned char *in, unsigned long inlen, void *num);
675 int der_length_integer(void *num, unsigned long *outlen);
676 
677 /* INTEGER -- handy for 0..2^32-1 values */
678 int der_decode_short_integer(const unsigned char *in, unsigned long inlen, unsigned long *num);
679 int der_encode_short_integer(unsigned long num, unsigned char *out, unsigned long *outlen);
680 int der_length_short_integer(unsigned long num, unsigned long *outlen);
681 
682 /* BIT STRING */
683 int der_encode_bit_string(const unsigned char *in, unsigned long inlen,
684                                 unsigned char *out, unsigned long *outlen);
685 int der_decode_bit_string(const unsigned char *in, unsigned long inlen,
686                                 unsigned char *out, unsigned long *outlen);
687 int der_encode_raw_bit_string(const unsigned char *in, unsigned long inlen,
688                                 unsigned char *out, unsigned long *outlen);
689 int der_decode_raw_bit_string(const unsigned char *in, unsigned long inlen,
690                                 unsigned char *out, unsigned long *outlen);
691 int der_length_bit_string(unsigned long nbits, unsigned long *outlen);
692 
693 /* OCTET STRING */
694 int der_encode_octet_string(const unsigned char *in, unsigned long inlen,
695                                   unsigned char *out, unsigned long *outlen);
696 int der_decode_octet_string(const unsigned char *in, unsigned long inlen,
697                                   unsigned char *out, unsigned long *outlen);
698 int der_length_octet_string(unsigned long noctets, unsigned long *outlen);
699 
700 /* OBJECT IDENTIFIER */
701 int der_encode_object_identifier(const unsigned long *words, unsigned long  nwords,
702                                        unsigned char *out,   unsigned long *outlen);
703 int der_decode_object_identifier(const unsigned char *in,    unsigned long  inlen,
704                                        unsigned long *words, unsigned long *outlen);
705 int der_length_object_identifier(const unsigned long *words, unsigned long nwords, unsigned long *outlen);
706 unsigned long der_object_identifier_bits(unsigned long x);
707 
708 /* IA5 STRING */
709 int der_encode_ia5_string(const unsigned char *in, unsigned long inlen,
710                                 unsigned char *out, unsigned long *outlen);
711 int der_decode_ia5_string(const unsigned char *in, unsigned long inlen,
712                                 unsigned char *out, unsigned long *outlen);
713 int der_length_ia5_string(const unsigned char *octets, unsigned long noctets, unsigned long *outlen);
714 
715 int der_ia5_char_encode(int c);
716 int der_ia5_value_decode(int v);
717 
718 /* TELETEX STRING */
719 int der_decode_teletex_string(const unsigned char *in, unsigned long inlen,
720                                 unsigned char *out, unsigned long *outlen);
721 int der_length_teletex_string(const unsigned char *octets, unsigned long noctets, unsigned long *outlen);
722 
723 /* PRINTABLE STRING */
724 int der_encode_printable_string(const unsigned char *in, unsigned long inlen,
725                                 unsigned char *out, unsigned long *outlen);
726 int der_decode_printable_string(const unsigned char *in, unsigned long inlen,
727                                 unsigned char *out, unsigned long *outlen);
728 int der_length_printable_string(const unsigned char *octets, unsigned long noctets, unsigned long *outlen);
729 
730 int der_printable_char_encode(int c);
731 int der_printable_value_decode(int v);
732 
733 /* UTF-8 */
734 #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)
735    #if defined(__WCHAR_MAX__)
736       #define LTC_WCHAR_MAX __WCHAR_MAX__
737    #else
738       #include <wchar.h>
739       #define LTC_WCHAR_MAX WCHAR_MAX
740    #endif
741 /* please note that it might happen that LTC_WCHAR_MAX is undefined */
742 #else
743    typedef ulong32 wchar_t;
744    #define LTC_WCHAR_MAX 0xFFFFFFFF
745 #endif
746 
747 int der_encode_utf8_string(const wchar_t *in,  unsigned long inlen,
748                            unsigned char *out, unsigned long *outlen);
749 
750 int der_decode_utf8_string(const unsigned char *in,  unsigned long inlen,
751                                        wchar_t *out, unsigned long *outlen);
752 unsigned long der_utf8_charsize(const wchar_t c);
753 int der_length_utf8_string(const wchar_t *in, unsigned long noctets, unsigned long *outlen);
754 
755 
756 /* CHOICE */
757 int der_decode_choice(const unsigned char *in,   unsigned long *inlen,
758                             ltc_asn1_list *list, unsigned long  outlen);
759 
760 /* UTCTime */
761 typedef struct {
762    unsigned YY, /* year */
763             MM, /* month */
764             DD, /* day */
765             hh, /* hour */
766             mm, /* minute */
767             ss, /* second */
768             off_dir, /* timezone offset direction 0 == +, 1 == - */
769             off_hh, /* timezone offset hours */
770             off_mm; /* timezone offset minutes */
771 } ltc_utctime;
772 
773 int der_encode_utctime(const ltc_utctime   *utctime,
774                              unsigned char *out,   unsigned long *outlen);
775 
776 int der_decode_utctime(const unsigned char *in, unsigned long *inlen,
777                              ltc_utctime   *out);
778 
779 int der_length_utctime(const ltc_utctime *utctime, unsigned long *outlen);
780 
781 /* GeneralizedTime */
782 typedef struct {
783    unsigned YYYY, /* year */
784             MM, /* month */
785             DD, /* day */
786             hh, /* hour */
787             mm, /* minute */
788             ss, /* second */
789             fs, /* fractional seconds */
790             off_dir, /* timezone offset direction 0 == +, 1 == - */
791             off_hh, /* timezone offset hours */
792             off_mm; /* timezone offset minutes */
793 } ltc_generalizedtime;
794 
795 int der_encode_generalizedtime(const ltc_generalizedtime *gtime,
796                                      unsigned char       *out, unsigned long *outlen);
797 
798 int der_decode_generalizedtime(const unsigned char *in, unsigned long *inlen,
799                                ltc_generalizedtime *out);
800 
801 int der_length_generalizedtime(const ltc_generalizedtime *gtime, unsigned long *outlen);
802 
803 #endif
804