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