1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
4 /* ---- NUMBER THEORY ---- */
7 /* Refers to the public key */
9 /* Refers to the private key */
12 /* Indicates standard output formats that can be read e.g. by OpenSSL or GnuTLS */
14 /* Indicates compressed public ECC key */
15 PK_COMPRESSED
= 0x2000,
16 /* Indicates ECC key with the curve specified by OID */
20 int rand_prime(void *N
, long len
, prng_state
*prng
, int wprng
);
25 /** RSA PKCS style key */
26 typedef struct Rsa_key
{
27 /** Type of key, PK_PRIVATE or PK_PUBLIC */
29 /** The public exponent */
31 /** The private exponent */
35 /** The p factor of N */
37 /** The q factor of N */
39 /** The 1/q mod p CRT param */
41 /** The d mod (p - 1) CRT param */
43 /** The d mod (q - 1) CRT param */
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
);
52 int rsa_exptmod(const unsigned char *in
, unsigned long inlen
,
53 unsigned char *out
, unsigned long *outlen
, int which
,
56 void rsa_free(rsa_key
*key
);
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)
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)
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)
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)
71 #define rsa_sign_saltlen_get_max(hash_idx, key) \
72 rsa_sign_saltlen_get_max_ex(LTC_PKCS_1_PSS, hash_idx, key)
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
,
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
);
88 int rsa_sign_hash_ex(const unsigned char *in
, unsigned long inlen
,
89 unsigned char *out
, unsigned long *outlen
,
91 prng_state
*prng
, int prng_idx
,
92 int hash_idx
, unsigned long saltlen
,
95 int rsa_verify_hash_ex(const unsigned char *sig
, unsigned long siglen
,
96 const unsigned char *hash
, unsigned long hashlen
,
98 int hash_idx
, unsigned long saltlen
,
99 int *stat
, const rsa_key
*key
);
101 int rsa_sign_saltlen_get_max_ex(int padding
, int hash_idx
, const rsa_key
*key
);
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
);
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
);
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
,
115 int rsa_set_factors(const unsigned char *p
, unsigned long plen
,
116 const unsigned char *q
, unsigned long qlen
,
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
,
124 /* ---- DH Routines ---- */
135 int dh_get_groupsize(const dh_key
*key
);
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
);
140 int dh_set_pg(const unsigned char *p
, unsigned long plen
,
141 const unsigned char *g
, unsigned long glen
,
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
);
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
);
149 int dh_shared_secret(const dh_key
*private_key
, const dh_key
*public_key
,
150 unsigned char *out
, unsigned long *outlen
);
152 void dh_free(dh_key
*key
);
154 int dh_export_key(void *out
, unsigned long *outlen
, int type
, const dh_key
*key
);
158 /* ---- ECC Routines ---- */
161 /* size of our temp buffers for exported keys */
162 #define ECC_BUF_SIZE 256
164 /* max private key size */
165 #define ECC_MAXSIZE 66
167 /** Structure defines a GF(p) curve */
169 /** The prime that defines the field the curve is in (encoded in hex) */
172 /** The fields A param (hex) */
175 /** The fields B param (hex) */
178 /** The order of the curve (hex) */
181 /** The x co-ordinate of the base point on the curve (hex) */
184 /** The y co-ordinate of the base point on the curve (hex) */
188 unsigned long cofactor
;
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 */
196 /** The x co-ordinate */
199 /** The y co-ordinate */
202 /** The z co-ordinate */
206 /** ECC key's domain parameters */
208 /** The size of the curve in octets */
210 /** The prime that defines the field the curve is in */
212 /** The fields A param */
214 /** The fields B param */
216 /** The order of the curve */
218 /** The base point G on the curve */
221 unsigned long cofactor
;
223 unsigned long oid
[16];
224 unsigned long oidlen
;
229 /** Type of key, PK_PRIVATE or PK_PUBLIC */
232 /** Structure with domain parameters */
235 /** Structure with the public key */
238 /** The private key */
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
;
254 /** the ECC params provided */
255 extern const ltc_ecc_curve ltc_ecc_curves
[];
257 void ecc_sizes(int *low
, int *high
);
258 int ecc_get_size(const ecc_key
*key
);
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
);
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
);
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
);
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
);
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
);
284 int ecc_shared_secret(const ecc_key
*private_key
, const ecc_key
*public_key
,
285 unsigned char *out
, unsigned long *outlen
);
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
,
292 int ecc_decrypt_key(const unsigned char *in
, unsigned long inlen
,
293 unsigned char *out
, unsigned long *outlen
,
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_)
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_)
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_)
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_)
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
);
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
);
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
);
323 #ifdef LTC_CURVE25519
326 /** The key type, PK_PRIVATE or PK_PUBLIC */
327 enum public_key_type type
;
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
336 /** The private key */
337 unsigned char priv
[32];
339 /** The public key */
340 unsigned char pub
[32];
344 /** Ed25519 Signature API */
345 int ed25519_make_key(prng_state
*prng
, int wprng
, curve25519_key
*key
);
347 int ed25519_export( unsigned char *out
, unsigned long *outlen
,
349 const curve25519_key
*key
);
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
);
358 int ed25519_sign(const unsigned char *msg
, unsigned long msglen
,
359 unsigned char *sig
, unsigned long *siglen
,
360 const curve25519_key
*private_key
);
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
);
366 /** X25519 Key-Exchange API */
367 int x25519_make_key(prng_state
*prng
, int wprng
, curve25519_key
*key
);
369 int x25519_export( unsigned char *out
, unsigned long *outlen
,
371 const curve25519_key
*key
);
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
);
380 int x25519_shared_secret(const curve25519_key
*private_key
,
381 const curve25519_key
*public_key
,
382 unsigned char *out
, unsigned long *outlen
);
384 #endif /* LTC_CURVE25519 */
388 /* Max diff between group and modulus size in bytes (max case: L=8192bits, N=256bits) */
389 #define LTC_MDSA_DELTA 992
391 /* Max DSA group size in bytes */
392 #define LTC_MDSA_MAX_GROUP 64
394 /* Max DSA modulus size in bytes (the actual DSA size, max 8192 bits) */
395 #define LTC_MDSA_MAX_MODULUS 1024
397 /** DSA key structure */
399 /** The key type, PK_PRIVATE or PK_PUBLIC */
402 /** The order of the sub-group used in octets */
408 /** The prime used to generate the sub-group */
411 /** The large prime that generats the field the contains the sub-group */
414 /** The private key */
417 /** The public key */
421 int dsa_make_key(prng_state
*prng
, int wprng
, int group_size
, int modulus_size
, dsa_key
*key
);
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
,
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
);
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
);
433 void dsa_free(dsa_key
*key
);
435 int dsa_sign_hash_raw(const unsigned char *in
, unsigned long inlen
,
437 prng_state
*prng
, int wprng
, const dsa_key
*key
);
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
);
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
);
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
);
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
,
456 int dsa_decrypt_key(const unsigned char *in
, unsigned long inlen
,
457 unsigned char *out
, unsigned long *outlen
,
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 */
471 typedef enum ltc_asn1_type_
{
476 LTC_ASN1_SHORT_INTEGER
,
479 LTC_ASN1_OCTET_STRING
,
481 LTC_ASN1_OBJECT_IDENTIFIER
,
483 LTC_ASN1_PRINTABLE_STRING
,
485 LTC_ASN1_UTF8_STRING
,
492 LTC_ASN1_RAW_BIT_STRING
,
493 LTC_ASN1_TELETEX_STRING
,
494 LTC_ASN1_GENERALIZEDTIME
,
495 LTC_ASN1_CUSTOM_TYPE
,
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,
506 LTC_ASN1_PC_PRIMITIVE
= 0x0,
507 LTC_ASN1_PC_CONSTRUCTED
= 0x1,
510 /** A LTC ASN.1 list type */
511 typedef struct ltc_asn1_list_
{
512 /** The LTC ASN.1 enumerated type identifier */
514 /** The data to encode or place for decoding */
516 /** The size of the input or resulting output */
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 */
523 /** Flag used to indicate optional items in ASN.1 sequences */
525 /** ASN.1 identifier */
526 ltc_asn1_class klass
;
529 /** prev/next entry in the list */
530 struct ltc_asn1_list_
*prev
, *next
, *child
, *parent
;
533 #define LTC_SET_ASN1(list, index, Type, Data, Size) \
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; \
547 #define LTC_SET_ASN1_IDENTIFIER(list, index, Class, Pc, Tag) \
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); \
557 #define LTC_SET_ASN1_CUSTOM_CONSTRUCTED(list, index, Class, Tag, Data) \
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); \
564 #define LTC_SET_ASN1_CUSTOM_PRIMITIVE(list, index, Class, Tag, Type, Data, Size) \
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); \
572 extern const char* der_asn1_class_to_string_map
[];
573 extern const unsigned long der_asn1_class_to_string_map_sz
;
575 extern const char* der_asn1_pc_to_string_map
[];
576 extern const unsigned long der_asn1_pc_to_string_map_sz
;
578 extern const char* der_asn1_tag_to_string_map
[];
579 extern const unsigned long der_asn1_tag_to_string_map_sz
;
582 int der_encode_sequence_ex(const ltc_asn1_list
*list
, unsigned long inlen
,
583 unsigned char *out
, unsigned long *outlen
, int type_of
);
585 #define der_encode_sequence(list, inlen, out, outlen) der_encode_sequence_ex(list, inlen, out, outlen, LTC_ASN1_SEQUENCE)
587 /** The supported bitmap for all the
588 * decoders with a `flags` argument.
591 LTC_DER_SEQ_ZERO
= 0x0u
,
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
,
598 /** Bit1 - [0]=Relaxed
600 LTC_DER_SEQ_RELAXED
= LTC_DER_SEQ_ZERO
,
601 LTC_DER_SEQ_STRICT
= 0x2u
,
603 /** Alternative naming */
604 LTC_DER_SEQ_SET
= LTC_DER_SEQ_UNORDERED
,
605 LTC_DER_SEQ_SEQUENCE
= LTC_DER_SEQ_ORDERED
,
608 int der_decode_sequence_ex(const unsigned char *in
, unsigned long inlen
,
609 ltc_asn1_list
*list
, unsigned long outlen
, unsigned int flags
);
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)
614 int der_length_sequence(const ltc_asn1_list
*list
, unsigned long inlen
,
615 unsigned long *outlen
);
619 int der_encode_custom_type(const ltc_asn1_list
*root
,
620 unsigned char *out
, unsigned long *outlen
);
622 int der_decode_custom_type(const unsigned char *in
, unsigned long inlen
,
623 ltc_asn1_list
*root
);
625 int der_length_custom_type(const ltc_asn1_list
*root
,
626 unsigned long *outlen
,
627 unsigned long *payloadlen
);
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
);
635 int der_encode_setof(const ltc_asn1_list
*list
, unsigned long inlen
,
636 unsigned char *out
, unsigned long *outlen
);
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
, ...);
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
);
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
,
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
);
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
);
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
);
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
);
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
);
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
);
697 int der_ia5_char_encode(int c
);
698 int der_ia5_value_decode(int v
);
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
);
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
);
712 int der_printable_char_encode(int c
);
713 int der_printable_value_decode(int v
);
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__
721 #define LTC_WCHAR_MAX WCHAR_MAX
723 /* please note that it might happen that LTC_WCHAR_MAX is undefined */
725 typedef ulong32
wchar_t;
726 #define LTC_WCHAR_MAX 0xFFFFFFFF
729 int der_encode_utf8_string(const wchar_t *in
, unsigned long inlen
,
730 unsigned char *out
, unsigned long *outlen
);
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
);
739 int der_decode_choice(const unsigned char *in
, unsigned long *inlen
,
740 ltc_asn1_list
*list
, unsigned long outlen
);
744 unsigned YY
, /* year */
750 off_dir
, /* timezone offset direction 0 == +, 1 == - */
751 off_hh
, /* timezone offset hours */
752 off_mm
; /* timezone offset minutes */
755 int der_encode_utctime(const ltc_utctime
*utctime
,
756 unsigned char *out
, unsigned long *outlen
);
758 int der_decode_utctime(const unsigned char *in
, unsigned long *inlen
,
761 int der_length_utctime(const ltc_utctime
*utctime
, unsigned long *outlen
);
763 /* GeneralizedTime */
765 unsigned YYYY
, /* year */
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
;
777 int der_encode_generalizedtime(const ltc_generalizedtime
*gtime
,
778 unsigned char *out
, unsigned long *outlen
);
780 int der_decode_generalizedtime(const unsigned char *in
, unsigned long *inlen
,
781 ltc_generalizedtime
*out
);
783 int der_length_generalizedtime(const ltc_generalizedtime
*gtime
, unsigned long *outlen
);