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