tomcrypt/src/headers/tomcrypt_pk.h

717 lines
26 KiB
C
Raw Normal View History

2017-06-19 14:06:38 +02:00
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
*
* LibTomCrypt is a library that provides various cryptographic
* algorithms in a highly modular and flexible manner.
*
* The library is free for all purposes without any express
* guarantee it works.
*/
2003-03-03 00:59:24 +00:00
/* ---- NUMBER THEORY ---- */
2005-08-01 16:36:47 +00:00
enum {
PK_PUBLIC=0,
PK_PRIVATE=1
};
2003-03-03 00:59:24 +00:00
/* Indicates standard output formats that can be read e.g. by OpenSSL or GnuTLS */
#define PK_STD 0x1000
2017-06-21 13:38:39 +02:00
/* iterations limit for retry-loops */
#define PK_MAX_RETRIES 20
2005-08-01 16:36:47 +00:00
int rand_prime(void *N, long len, prng_state *prng, int wprng);
#ifdef LTC_SOURCE
/* internal helper functions */
int rand_bn_bits(void *N, int bits, prng_state *prng, int wprng);
int rand_bn_range(void *N, void *limit, prng_state *prng, int wprng);
2003-03-03 00:59:24 +00:00
2017-02-28 20:46:12 +01:00
enum public_key_algorithms {
PKA_RSA,
PKA_DSA
};
typedef struct Oid {
unsigned long OID[16];
/** Length of DER encoding */
unsigned long OIDlen;
} oid_st;
int pk_get_oid(int pk, oid_st *st);
#endif /* LTC_SOURCE */
2003-03-03 00:59:24 +00:00
/* ---- RSA ---- */
2007-07-20 17:48:02 +00:00
#ifdef LTC_MRSA
2004-05-12 20:42:16 +00:00
2014-01-03 15:16:59 +01:00
/** RSA PKCS style key */
2003-03-03 00:59:24 +00:00
typedef struct Rsa_key {
2005-08-01 16:36:47 +00:00
/** Type of key, PK_PRIVATE or PK_PUBLIC */
2003-03-03 00:59:24 +00:00
int type;
2005-08-01 16:36:47 +00:00
/** The public exponent */
2012-07-26 14:43:15 +02:00
void *e;
2005-08-01 16:36:47 +00:00
/** The private exponent */
2012-07-26 14:43:15 +02:00
void *d;
2005-08-01 16:36:47 +00:00
/** The modulus */
2012-07-26 14:43:15 +02:00
void *N;
2005-08-01 16:36:47 +00:00
/** The p factor of N */
2012-07-26 14:43:15 +02:00
void *p;
2005-08-01 16:36:47 +00:00
/** The q factor of N */
2012-07-26 14:43:15 +02:00
void *q;
2005-08-01 16:36:47 +00:00
/** The 1/q mod p CRT param */
2012-07-26 14:43:15 +02:00
void *qP;
2005-08-01 16:36:47 +00:00
/** The d mod (p - 1) CRT param */
2012-07-26 14:43:15 +02:00
void *dP;
2005-08-01 16:36:47 +00:00
/** The d mod (q - 1) CRT param */
void *dQ;
2003-03-03 00:59:24 +00:00
} rsa_key;
2004-12-30 23:55:53 +00:00
int rsa_make_key(prng_state *prng, int wprng, int size, long e, rsa_key *key);
2013-09-03 14:24:14 +02:00
int rsa_get_size(rsa_key *key);
2003-03-03 00:59:24 +00:00
2004-12-30 23:55:53 +00:00
int rsa_exptmod(const unsigned char *in, unsigned long inlen,
2004-05-31 02:36:47 +00:00
unsigned char *out, unsigned long *outlen, int which,
rsa_key *key);
2003-03-03 00:59:24 +00:00
2004-12-30 23:55:53 +00:00
void rsa_free(rsa_key *key);
2003-03-03 00:59:24 +00:00
2014-01-03 15:16:59 +01:00
/* These use PKCS #1 v2.0 padding */
2006-11-17 14:21:24 +00:00
#define rsa_encrypt_key(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _prng, _prng_idx, _hash_idx, _key) \
rsa_encrypt_key_ex(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _prng, _prng_idx, _hash_idx, LTC_PKCS_1_OAEP, _key)
2006-11-17 14:21:24 +00:00
#define rsa_decrypt_key(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _hash_idx, _stat, _key) \
rsa_decrypt_key_ex(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _hash_idx, LTC_PKCS_1_OAEP, _stat, _key)
2006-11-17 14:21:24 +00:00
#define rsa_sign_hash(_in, _inlen, _out, _outlen, _prng, _prng_idx, _hash_idx, _saltlen, _key) \
rsa_sign_hash_ex(_in, _inlen, _out, _outlen, LTC_PKCS_1_PSS, _prng, _prng_idx, _hash_idx, _saltlen, _key)
2006-11-17 14:21:24 +00:00
#define rsa_verify_hash(_sig, _siglen, _hash, _hashlen, _hash_idx, _saltlen, _stat, _key) \
rsa_verify_hash_ex(_sig, _siglen, _hash, _hashlen, LTC_PKCS_1_PSS, _hash_idx, _saltlen, _stat, _key)
2006-11-17 14:21:24 +00:00
2013-09-03 14:25:00 +02:00
#define rsa_sign_saltlen_get_max(_hash_idx, _key) \
rsa_sign_saltlen_get_max_ex(LTC_PKCS_1_PSS, _hash_idx, _key)
2014-01-03 15:16:59 +01:00
/* These can be switched between PKCS #1 v2.x and PKCS #1 v1.5 paddings */
2006-11-17 14:21:24 +00:00
int rsa_encrypt_key_ex(const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen,
const unsigned char *lparam, unsigned long lparamlen,
prng_state *prng, int prng_idx, int hash_idx, int padding, rsa_key *key);
int rsa_decrypt_key_ex(const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen,
const unsigned char *lparam, unsigned long lparamlen,
int hash_idx, int padding,
int *stat, rsa_key *key);
int rsa_sign_hash_ex(const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen,
int padding,
prng_state *prng, int prng_idx,
int hash_idx, unsigned long saltlen,
rsa_key *key);
int rsa_verify_hash_ex(const unsigned char *sig, unsigned long siglen,
const unsigned char *hash, unsigned long hashlen,
int padding,
int hash_idx, unsigned long saltlen,
int *stat, rsa_key *key);
2004-05-31 02:36:47 +00:00
2013-09-03 14:25:00 +02:00
int rsa_sign_saltlen_get_max_ex(int padding, int hash_idx, rsa_key *key);
2014-01-03 15:16:59 +01:00
/* PKCS #1 import/export */
2004-05-31 02:36:47 +00:00
int rsa_export(unsigned char *out, unsigned long *outlen, int type, rsa_key *key);
int rsa_import(const unsigned char *in, unsigned long inlen, rsa_key *key);
2012-07-26 14:43:15 +02:00
int rsa_import_x509(const unsigned char *in, unsigned long inlen, rsa_key *key);
2017-04-04 09:08:10 +02:00
int rsa_import_pkcs8(const unsigned char *in, unsigned long inlen,
const void *passwd, unsigned long passwdlen, rsa_key *key);
int rsa_import_radix(int radix, char *N, char *e, char *d, char *p, char *q, char *dP, char *dQ, char *qP, rsa_key *key);
2003-03-03 00:59:24 +00:00
#endif
2005-11-18 05:15:37 +00:00
/* ---- Katja ---- */
2014-07-17 10:49:05 +02:00
#ifdef LTC_MKAT
2005-11-18 05:15:37 +00:00
/* Min and Max KAT key sizes (in bits) */
#define MIN_KAT_SIZE 1024
#define MAX_KAT_SIZE 4096
2014-01-03 15:16:59 +01:00
/** Katja PKCS style key */
2005-11-18 05:15:37 +00:00
typedef struct KAT_key {
/** Type of key, PK_PRIVATE or PK_PUBLIC */
int type;
/** The private exponent */
2012-07-26 14:43:15 +02:00
void *d;
2005-11-18 05:15:37 +00:00
/** The modulus */
2012-07-26 14:43:15 +02:00
void *N;
2005-11-18 05:15:37 +00:00
/** The p factor of N */
2012-07-26 14:43:15 +02:00
void *p;
2005-11-18 05:15:37 +00:00
/** The q factor of N */
2012-07-26 14:43:15 +02:00
void *q;
2005-11-18 05:15:37 +00:00
/** The 1/q mod p CRT param */
2012-07-26 14:43:15 +02:00
void *qP;
2005-11-18 05:15:37 +00:00
/** The d mod (p - 1) CRT param */
2012-07-26 14:43:15 +02:00
void *dP;
2005-11-18 05:15:37 +00:00
/** The d mod (q - 1) CRT param */
void *dQ;
/** The pq param */
void *pq;
} katja_key;
int katja_make_key(prng_state *prng, int wprng, int size, katja_key *key);
int katja_exptmod(const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen, int which,
katja_key *key);
void katja_free(katja_key *key);
2014-01-03 15:16:59 +01:00
/* These use PKCS #1 v2.0 padding */
2005-11-18 05:15:37 +00:00
int katja_encrypt_key(const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen,
const unsigned char *lparam, unsigned long lparamlen,
prng_state *prng, int prng_idx, int hash_idx, katja_key *key);
2012-07-26 14:43:15 +02:00
2005-11-18 05:15:37 +00:00
int katja_decrypt_key(const unsigned char *in, unsigned long inlen,
2012-07-26 14:43:15 +02:00
unsigned char *out, unsigned long *outlen,
2005-11-18 05:15:37 +00:00
const unsigned char *lparam, unsigned long lparamlen,
int hash_idx, int *stat,
katja_key *key);
2014-01-03 15:16:59 +01:00
/* PKCS #1 import/export */
2005-11-18 05:15:37 +00:00
int katja_export(unsigned char *out, unsigned long *outlen, int type, katja_key *key);
int katja_import(const unsigned char *in, unsigned long inlen, katja_key *key);
2012-07-26 14:43:15 +02:00
2005-11-18 05:15:37 +00:00
#endif
/* ---- DH Routines ---- */
2014-02-25 09:59:21 +01:00
#ifdef LTC_MDH
2017-06-22 09:48:10 +02:00
typedef struct {
int size;
char *name, *base, *prime;
} ltc_dh_set_type;
extern const ltc_dh_set_type ltc_dh_sets[];
typedef struct {
int type;
void *x;
void *y;
2017-06-22 09:48:10 +02:00
void *base;
void *prime;
} dh_key;
2017-06-22 09:48:10 +02:00
int dh_get_groupsize(dh_key *key);
2017-06-22 09:48:10 +02:00
int dh_make_key(prng_state *prng, int wprng, int groupsize, dh_key *key);
int dh_make_key_dhparam(prng_state *prng, int wprng, unsigned char *dhparam, unsigned long dhparamlen, dh_key *key);
void dh_free(dh_key *key);
int dh_export(unsigned char *out, unsigned long *outlen, int type, dh_key *key);
int dh_import(const unsigned char *in, unsigned long inlen, dh_key *key);
int dh_shared_secret(dh_key *private_key, dh_key *public_key,
unsigned char *out, unsigned long *outlen);
2017-06-22 09:48:10 +02:00
#ifdef LTC_SOURCE
/* internal helper functions */
2017-06-22 09:48:10 +02:00
int dh_check_pubkey(dh_key *key);
#endif
2017-06-22 09:48:10 +02:00
#endif /* LTC_MDH */
2005-08-01 16:36:47 +00:00
/* ---- ECC Routines ---- */
2007-07-20 17:48:02 +00:00
#ifdef LTC_MECC
2003-03-03 00:59:24 +00:00
2005-11-18 05:15:37 +00:00
/* size of our temp buffers for exported keys */
#define ECC_BUF_SIZE 256
/* max private key size */
#define ECC_MAXSIZE 66
2005-08-01 16:36:47 +00:00
/** Structure defines a NIST GF(p) curve */
typedef struct {
/** The size of the curve in octets */
int size;
2003-03-03 00:59:24 +00:00
2005-08-01 16:36:47 +00:00
/** name of curve */
2012-07-26 14:43:15 +02:00
char *name;
2003-03-03 00:59:24 +00:00
2006-12-16 18:10:04 +00:00
/** The prime that defines the field the curve is in (encoded in hex) */
2005-08-01 16:36:47 +00:00
char *prime;
2003-03-03 00:59:24 +00:00
2006-12-16 18:10:04 +00:00
/** The fields B param (hex) */
2005-08-01 16:36:47 +00:00
char *B;
2003-03-03 00:59:24 +00:00
2006-12-16 18:10:04 +00:00
/** The order of the curve (hex) */
2005-08-01 16:36:47 +00:00
char *order;
2012-07-26 14:43:15 +02:00
2006-12-16 18:10:04 +00:00
/** The x co-ordinate of the base point on the curve (hex) */
2005-08-01 16:36:47 +00:00
char *Gx;
2012-07-26 14:43:15 +02:00
2006-12-16 18:10:04 +00:00
/** The y co-ordinate of the base point on the curve (hex) */
2005-08-01 16:36:47 +00:00
char *Gy;
} ltc_ecc_set_type;
2003-03-03 00:59:24 +00:00
2005-08-01 16:36:47 +00:00
/** 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 */
2003-03-03 00:59:24 +00:00
typedef struct {
2005-08-01 16:36:47 +00:00
/** The x co-ordinate */
void *x;
2005-11-18 05:15:37 +00:00
2005-08-01 16:36:47 +00:00
/** The y co-ordinate */
void *y;
2005-11-18 05:15:37 +00:00
2005-08-01 16:36:47 +00:00
/** The z co-ordinate */
void *z;
2003-03-03 00:59:24 +00:00
} ecc_point;
2005-08-01 16:36:47 +00:00
/** An ECC key */
2003-03-03 00:59:24 +00:00
typedef struct {
2005-08-01 16:36:47 +00:00
/** Type of key, PK_PRIVATE or PK_PUBLIC */
int type;
2005-11-18 05:15:37 +00:00
2006-12-16 18:10:04 +00:00
/** Index into the ltc_ecc_sets[] for the parameters of this curve; if -1, then this key is using user supplied curve in dp */
2005-08-01 16:36:47 +00:00
int idx;
2005-11-18 05:15:37 +00:00
2015-12-20 17:01:18 +01:00
/** pointer to domain parameters; either points to NIST curves (identified by idx >= 0) or user supplied curve */
const ltc_ecc_set_type *dp;
2006-12-16 18:10:04 +00:00
2005-08-01 16:36:47 +00:00
/** The public key */
2003-03-03 00:59:24 +00:00
ecc_point pubkey;
2005-11-18 05:15:37 +00:00
2005-08-01 16:36:47 +00:00
/** The private key */
void *k;
2003-03-03 00:59:24 +00:00
} ecc_key;
2005-08-01 16:36:47 +00:00
/** the ECC params provided */
extern const ltc_ecc_set_type ltc_ecc_sets[];
2005-11-18 05:15:37 +00:00
int ecc_test(void);
2004-12-30 23:55:53 +00:00
void ecc_sizes(int *low, int *high);
2005-11-18 05:15:37 +00:00
int ecc_get_size(ecc_key *key);
2004-12-30 23:55:53 +00:00
2005-11-18 05:15:37 +00:00
int ecc_make_key(prng_state *prng, int wprng, int keysize, ecc_key *key);
2006-12-16 18:10:04 +00:00
int ecc_make_key_ex(prng_state *prng, int wprng, ecc_key *key, const ltc_ecc_set_type *dp);
2004-12-30 23:55:53 +00:00
void ecc_free(ecc_key *key);
2003-03-03 00:59:24 +00:00
2005-11-18 05:15:37 +00:00
int ecc_export(unsigned char *out, unsigned long *outlen, int type, ecc_key *key);
int ecc_import(const unsigned char *in, unsigned long inlen, ecc_key *key);
2006-12-16 18:10:04 +00:00
int ecc_import_ex(const unsigned char *in, unsigned long inlen, ecc_key *key, const ltc_ecc_set_type *dp);
2003-03-03 00:59:24 +00:00
2006-11-17 14:21:24 +00:00
int ecc_ansi_x963_export(ecc_key *key, unsigned char *out, unsigned long *outlen);
int ecc_ansi_x963_import(const unsigned char *in, unsigned long inlen, ecc_key *key);
2006-12-16 18:10:04 +00:00
int ecc_ansi_x963_import_ex(const unsigned char *in, unsigned long inlen, ecc_key *key, ltc_ecc_set_type *dp);
2006-11-17 14:21:24 +00:00
2012-07-26 14:43:15 +02:00
int ecc_shared_secret(ecc_key *private_key, ecc_key *public_key,
2005-11-18 05:15:37 +00:00
unsigned char *out, unsigned long *outlen);
2003-03-03 00:59:24 +00:00
2005-11-18 05:15:37 +00:00
int ecc_encrypt_key(const unsigned char *in, unsigned long inlen,
2012-07-26 14:43:15 +02:00
unsigned char *out, unsigned long *outlen,
prng_state *prng, int wprng, int hash,
2005-11-18 05:15:37 +00:00
ecc_key *key);
2003-03-03 00:59:24 +00:00
2005-11-18 05:15:37 +00:00
int ecc_decrypt_key(const unsigned char *in, unsigned long inlen,
2012-07-26 14:43:15 +02:00
unsigned char *out, unsigned long *outlen,
2005-11-18 05:15:37 +00:00
ecc_key *key);
2003-03-03 00:59:24 +00:00
int ecc_sign_hash_rfc7518(const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen,
prng_state *prng, int wprng, ecc_key *key);
2012-07-26 14:43:15 +02:00
int ecc_sign_hash(const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen,
2005-11-18 05:15:37 +00:00
prng_state *prng, int wprng, ecc_key *key);
2003-03-03 00:59:24 +00:00
int ecc_verify_hash_rfc7518(const unsigned char *sig, unsigned long siglen,
const unsigned char *hash, unsigned long hashlen,
int *stat, ecc_key *key);
2005-11-18 05:15:37 +00:00
int ecc_verify_hash(const unsigned char *sig, unsigned long siglen,
2012-07-26 14:43:15 +02:00
const unsigned char *hash, unsigned long hashlen,
2005-11-18 05:15:37 +00:00
int *stat, ecc_key *key);
2003-03-03 00:59:24 +00:00
2005-08-01 16:36:47 +00:00
/* low level functions */
ecc_point *ltc_ecc_new_point(void);
void ltc_ecc_del_point(ecc_point *p);
2005-11-18 05:15:37 +00:00
int ltc_ecc_is_valid_idx(int n);
2005-08-01 16:36:47 +00:00
/* point ops (mp == montgomery digit) */
2014-03-04 21:29:40 +01:00
#if !defined(LTC_MECC_ACCEL) || defined(LTM_DESC) || defined(GMP_DESC)
2005-08-01 16:36:47 +00:00
/* R = 2P */
2005-11-18 05:15:37 +00:00
int ltc_ecc_projective_dbl_point(ecc_point *P, ecc_point *R, void *modulus, void *mp);
2005-08-01 16:36:47 +00:00
/* R = P + Q */
2005-11-18 05:15:37 +00:00
int ltc_ecc_projective_add_point(ecc_point *P, ecc_point *Q, ecc_point *R, void *modulus, void *mp);
2006-04-06 19:48:32 +00:00
#endif
2005-11-18 05:15:37 +00:00
2007-07-20 17:48:02 +00:00
#if defined(LTC_MECC_FP)
/* optimized point multiplication using fixed point cache (HAC algorithm 14.117) */
2006-05-29 23:12:56 +00:00
int ltc_ecc_fp_mulmod(void *k, ecc_point *G, ecc_point *R, void *modulus, int map);
2007-07-20 17:48:02 +00:00
/* functions for saving/loading/freeing/adding to fixed point cache */
2006-05-29 23:12:56 +00:00
int ltc_ecc_fp_save_state(unsigned char **out, unsigned long *outlen);
int ltc_ecc_fp_restore_state(unsigned char *in, unsigned long inlen);
void ltc_ecc_fp_free(void);
2007-07-20 17:48:02 +00:00
int ltc_ecc_fp_add_point(ecc_point *g, void *modulus, int lock);
/* lock/unlock all points currently in fixed point cache */
void ltc_ecc_fp_tablelock(int lock);
2006-05-29 23:12:56 +00:00
#endif
2005-08-01 16:36:47 +00:00
/* R = kG */
int ltc_ecc_mulmod(void *k, ecc_point *G, ecc_point *R, void *modulus, int map);
2005-11-18 05:15:37 +00:00
2006-12-16 18:10:04 +00:00
#ifdef LTC_ECC_SHAMIR
/* kA*A + kB*B = C */
int ltc_ecc_mul2add(ecc_point *A, void *kA,
ecc_point *B, void *kB,
ecc_point *C,
void *modulus);
2007-07-20 17:48:02 +00:00
#ifdef LTC_MECC_FP
/* Shamir's trick with optimized point multiplication using fixed point cache */
2006-12-16 18:10:04 +00:00
int ltc_ecc_fp_mul2add(ecc_point *A, void *kA,
ecc_point *B, void *kB,
ecc_point *C, void *modulus);
#endif
#endif
2005-08-01 16:36:47 +00:00
/* map P to affine from projective */
int ltc_ecc_map(ecc_point *P, void *modulus, void *mp);
2003-03-03 01:02:10 +00:00
#endif
2003-12-24 18:59:57 +00:00
2007-07-20 17:48:02 +00:00
#ifdef LTC_MDSA
2003-12-24 18:59:57 +00:00
2005-11-24 11:57:58 +00:00
/* Max diff between group and modulus size in bytes */
2007-07-20 17:48:02 +00:00
#define LTC_MDSA_DELTA 512
2005-11-24 11:57:58 +00:00
/* Max DSA group size in bytes (default allows 4k-bit groups) */
2007-07-20 17:48:02 +00:00
#define LTC_MDSA_MAX_GROUP 512
2005-11-24 11:57:58 +00:00
2005-08-01 16:36:47 +00:00
/** DSA key structure */
2003-12-24 18:59:57 +00:00
typedef struct {
2005-08-01 16:36:47 +00:00
/** The key type, PK_PRIVATE or PK_PUBLIC */
2012-07-26 14:43:15 +02:00
int type;
2005-11-18 05:15:37 +00:00
2005-08-01 16:36:47 +00:00
/** The order of the sub-group used in octets */
int qord;
2005-11-18 05:15:37 +00:00
2005-08-01 16:36:47 +00:00
/** The generator */
void *g;
2005-11-18 05:15:37 +00:00
2005-08-01 16:36:47 +00:00
/** The prime used to generate the sub-group */
void *q;
2005-11-18 05:15:37 +00:00
2005-08-01 16:36:47 +00:00
/** The large prime that generats the field the contains the sub-group */
void *p;
2005-11-18 05:15:37 +00:00
2005-08-01 16:36:47 +00:00
/** The private key */
void *x;
2005-11-18 05:15:37 +00:00
2005-08-01 16:36:47 +00:00
/** The public key */
void *y;
2003-12-24 18:59:57 +00:00
} dsa_key;
2004-12-30 23:55:53 +00:00
int dsa_make_key(prng_state *prng, int wprng, int group_size, int modulus_size, dsa_key *key);
2017-02-28 18:05:58 +01:00
int dsa_make_key_ex(prng_state *prng, int wprng, int group_size, int modulus_size, dsa_key *key, char* p_hex, char* q_hex, char* g_hex);
2004-12-30 23:55:53 +00:00
void dsa_free(dsa_key *key);
2003-12-24 18:59:57 +00:00
2005-06-09 00:08:13 +00:00
int dsa_sign_hash_raw(const unsigned char *in, unsigned long inlen,
2005-08-01 16:36:47 +00:00
void *r, void *s,
2005-06-09 00:08:13 +00:00
prng_state *prng, int wprng, dsa_key *key);
2004-12-30 23:55:53 +00:00
int dsa_sign_hash(const unsigned char *in, unsigned long inlen,
2003-12-24 18:59:57 +00:00
unsigned char *out, unsigned long *outlen,
prng_state *prng, int wprng, dsa_key *key);
2005-08-01 16:36:47 +00:00
int dsa_verify_hash_raw( void *r, void *s,
2012-07-26 14:43:15 +02:00
const unsigned char *hash, unsigned long hashlen,
2005-06-09 00:08:13 +00:00
int *stat, dsa_key *key);
2004-12-30 23:55:53 +00:00
int dsa_verify_hash(const unsigned char *sig, unsigned long siglen,
2012-07-26 14:43:15 +02:00
const unsigned char *hash, unsigned long hashlen,
2004-12-30 23:55:53 +00:00
int *stat, dsa_key *key);
2003-12-24 18:59:57 +00:00
2005-11-18 05:15:37 +00:00
int dsa_encrypt_key(const unsigned char *in, unsigned long inlen,
2012-07-26 14:43:15 +02:00
unsigned char *out, unsigned long *outlen,
prng_state *prng, int wprng, int hash,
2005-11-18 05:15:37 +00:00
dsa_key *key);
2012-07-26 14:43:15 +02:00
2005-11-18 05:15:37 +00:00
int dsa_decrypt_key(const unsigned char *in, unsigned long inlen,
2012-07-26 14:43:15 +02:00
unsigned char *out, unsigned long *outlen,
2005-11-18 05:15:37 +00:00
dsa_key *key);
2012-07-26 14:43:15 +02:00
2017-02-21 12:49:20 +01:00
int dsa_import_radix(int radix, char *p, char *q, char *g, char *x, char *y, dsa_key *key);
2004-12-30 23:55:53 +00:00
int dsa_import(const unsigned char *in, unsigned long inlen, dsa_key *key);
int dsa_export(unsigned char *out, unsigned long *outlen, int type, dsa_key *key);
int dsa_verify_key(dsa_key *key, int *stat);
2003-12-24 18:59:57 +00:00
2005-11-18 05:15:37 +00:00
int dsa_shared_secret(void *private_key, void *base,
dsa_key *public_key,
unsigned char *out, unsigned long *outlen);
2003-12-24 18:59:57 +00:00
#endif
2004-10-30 03:00:26 +00:00
2004-12-30 23:55:53 +00:00
#ifdef LTC_DER
2004-10-30 03:00:26 +00:00
/* DER handling */
2005-06-09 00:08:13 +00:00
typedef enum ltc_asn1_type_ {
/* 0 */
2005-06-09 00:08:13 +00:00
LTC_ASN1_EOL,
2006-05-29 23:12:56 +00:00
LTC_ASN1_BOOLEAN,
2005-06-09 00:08:13 +00:00
LTC_ASN1_INTEGER,
LTC_ASN1_SHORT_INTEGER,
LTC_ASN1_BIT_STRING,
/* 5 */
2005-06-09 00:08:13 +00:00
LTC_ASN1_OCTET_STRING,
LTC_ASN1_NULL,
LTC_ASN1_OBJECT_IDENTIFIER,
LTC_ASN1_IA5_STRING,
LTC_ASN1_PRINTABLE_STRING,
/* 10 */
2006-12-16 18:10:04 +00:00
LTC_ASN1_UTF8_STRING,
2005-06-27 11:47:35 +00:00
LTC_ASN1_UTCTIME,
LTC_ASN1_CHOICE,
2005-11-24 11:57:58 +00:00
LTC_ASN1_SEQUENCE,
LTC_ASN1_SET,
/* 15 */
LTC_ASN1_SETOF,
LTC_ASN1_RAW_BIT_STRING,
2013-01-24 11:14:05 -08:00
LTC_ASN1_TELETEX_STRING,
LTC_ASN1_CONSTRUCTED,
LTC_ASN1_CONTEXT_SPECIFIC,
2017-03-30 18:58:21 +02:00
/* 20 */
LTC_ASN1_GENERALIZEDTIME,
} ltc_asn1_type;
2005-06-09 00:08:13 +00:00
2005-08-01 16:36:47 +00:00
/** A LTC ASN.1 list type */
2005-11-18 05:15:37 +00:00
typedef struct ltc_asn1_list_ {
2005-08-01 16:36:47 +00:00
/** The LTC ASN.1 enumerated type identifier */
ltc_asn1_type type;
2005-08-01 16:36:47 +00:00
/** The data to encode or place for decoding */
2005-06-09 00:08:13 +00:00
void *data;
2005-08-01 16:36:47 +00:00
/** The size of the input or resulting output */
2005-06-09 00:08:13 +00:00
unsigned long size;
2005-08-01 16:36:47 +00:00
/** The used flag, this is used by the CHOICE ASN.1 type to indicate which choice was made */
2005-06-27 11:47:35 +00:00
int used;
2005-11-18 05:15:37 +00:00
/** prev/next entry in the list */
struct ltc_asn1_list_ *prev, *next, *child, *parent;
2005-06-09 00:08:13 +00:00
} ltc_asn1_list;
#define LTC_SET_ASN1(list, index, Type, Data, Size) \
do { \
int LTC_MACRO_temp = (index); \
ltc_asn1_list *LTC_MACRO_list = (list); \
LTC_MACRO_list[LTC_MACRO_temp].type = (Type); \
2006-06-18 01:37:50 +00:00
LTC_MACRO_list[LTC_MACRO_temp].data = (void*)(Data); \
2005-06-09 00:08:13 +00:00
LTC_MACRO_list[LTC_MACRO_temp].size = (Size); \
2005-06-27 11:47:35 +00:00
LTC_MACRO_list[LTC_MACRO_temp].used = 0; \
} while (0)
2005-06-09 00:08:13 +00:00
/* SEQUENCE */
2005-11-24 11:57:58 +00:00
int der_encode_sequence_ex(ltc_asn1_list *list, unsigned long inlen,
unsigned char *out, unsigned long *outlen, int type_of);
2012-07-26 14:43:15 +02:00
#define der_encode_sequence(list, inlen, out, outlen) der_encode_sequence_ex(list, inlen, out, outlen, LTC_ASN1_SEQUENCE)
2005-06-09 00:08:13 +00:00
2005-11-24 11:57:58 +00:00
int der_decode_sequence_ex(const unsigned char *in, unsigned long inlen,
ltc_asn1_list *list, unsigned long outlen, int ordered);
2012-07-26 14:43:15 +02:00
2005-11-24 11:57:58 +00:00
#define der_decode_sequence(in, inlen, list, outlen) der_decode_sequence_ex(in, inlen, list, outlen, 1)
2005-06-09 00:08:13 +00:00
int der_length_sequence(ltc_asn1_list *list, unsigned long inlen,
unsigned long *outlen);
#ifdef LTC_SOURCE
/* internal helper functions */
/* SUBJECT PUBLIC KEY INFO */
int der_encode_subject_public_key_info(unsigned char *out, unsigned long *outlen,
unsigned int algorithm, void* public_key, unsigned long public_key_len,
unsigned long parameters_type, void* parameters, unsigned long parameters_len);
int der_decode_subject_public_key_info(const unsigned char *in, unsigned long inlen,
unsigned int algorithm, void* public_key, unsigned long* public_key_len,
unsigned long parameters_type, ltc_asn1_list* parameters, unsigned long parameters_len);
#endif /* LTC_SOURCE */
2005-11-24 11:57:58 +00:00
/* SET */
#define der_decode_set(in, inlen, list, outlen) der_decode_sequence_ex(in, inlen, list, outlen, 0)
#define der_length_set der_length_sequence
int der_encode_set(ltc_asn1_list *list, unsigned long inlen,
unsigned char *out, unsigned long *outlen);
int der_encode_setof(ltc_asn1_list *list, unsigned long inlen,
unsigned char *out, unsigned long *outlen);
2012-07-26 14:43:15 +02:00
2005-11-18 05:15:37 +00:00
/* VA list handy helpers with triplets of <type, size, data> */
2005-06-09 00:08:13 +00:00
int der_encode_sequence_multi(unsigned char *out, unsigned long *outlen, ...);
int der_decode_sequence_multi(const unsigned char *in, unsigned long inlen, ...);
2005-11-24 11:57:58 +00:00
/* FLEXI DECODER handle unknown list decoder */
2005-11-18 05:15:37 +00:00
int der_decode_sequence_flexi(const unsigned char *in, unsigned long *inlen, ltc_asn1_list **out);
#define der_free_sequence_flexi der_sequence_free
2005-11-18 05:15:37 +00:00
void der_sequence_free(ltc_asn1_list *in);
void der_sequence_shrink(ltc_asn1_list *in);
2005-11-18 05:15:37 +00:00
2006-05-29 23:12:56 +00:00
/* BOOLEAN */
int der_length_boolean(unsigned long *outlen);
2012-07-26 14:43:15 +02:00
int der_encode_boolean(int in,
2006-05-29 23:12:56 +00:00
unsigned char *out, unsigned long *outlen);
int der_decode_boolean(const unsigned char *in, unsigned long inlen,
2012-07-26 14:43:15 +02:00
int *out);
2005-06-09 00:08:13 +00:00
/* INTEGER */
2005-08-01 16:36:47 +00:00
int der_encode_integer(void *num, unsigned char *out, unsigned long *outlen);
int der_decode_integer(const unsigned char *in, unsigned long inlen, void *num);
int der_length_integer(void *num, unsigned long *len);
2005-06-09 00:08:13 +00:00
/* INTEGER -- handy for 0..2^32-1 values */
int der_decode_short_integer(const unsigned char *in, unsigned long inlen, unsigned long *num);
int der_encode_short_integer(unsigned long num, unsigned char *out, unsigned long *outlen);
int der_length_short_integer(unsigned long num, unsigned long *outlen);
/* BIT STRING */
int der_encode_bit_string(const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen);
int der_decode_bit_string(const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen);
int der_encode_raw_bit_string(const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen);
int der_decode_raw_bit_string(const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen);
2005-06-09 00:08:13 +00:00
int der_length_bit_string(unsigned long nbits, unsigned long *outlen);
/* OCTET STRING */
int der_encode_octet_string(const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen);
int der_decode_octet_string(const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen);
int der_length_octet_string(unsigned long noctets, unsigned long *outlen);
/* OBJECT IDENTIFIER */
int der_encode_object_identifier(unsigned long *words, unsigned long nwords,
unsigned char *out, unsigned long *outlen);
int der_decode_object_identifier(const unsigned char *in, unsigned long inlen,
unsigned long *words, unsigned long *outlen);
int der_length_object_identifier(unsigned long *words, unsigned long nwords, unsigned long *outlen);
unsigned long der_object_identifier_bits(unsigned long x);
/* IA5 STRING */
int der_encode_ia5_string(const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen);
int der_decode_ia5_string(const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen);
int der_length_ia5_string(const unsigned char *octets, unsigned long noctets, unsigned long *outlen);
int der_ia5_char_encode(int c);
int der_ia5_value_decode(int v);
2013-01-24 11:14:05 -08:00
/* TELETEX STRING */
int der_decode_teletex_string(const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen);
int der_length_teletex_string(const unsigned char *octets, unsigned long noctets, unsigned long *outlen);
#ifdef LTC_SOURCE
/* internal helper functions */
2013-01-24 11:14:05 -08:00
int der_teletex_char_encode(int c);
int der_teletex_value_decode(int v);
#endif /* LTC_SOURCE */
2013-01-24 11:14:05 -08:00
/* PRINTABLE STRING */
2005-06-09 00:08:13 +00:00
int der_encode_printable_string(const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen);
int der_decode_printable_string(const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen);
int der_length_printable_string(const unsigned char *octets, unsigned long noctets, unsigned long *outlen);
int der_printable_char_encode(int c);
int der_printable_value_decode(int v);
2006-12-16 18:10:04 +00:00
/* UTF-8 */
2017-03-09 13:08:58 +01:00
#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)
2006-12-16 18:10:04 +00:00
#include <wchar.h>
2017-03-09 13:08:58 +01:00
#if defined(__WCHAR_MAX__)
#define LTC_WCHAR_MAX __WCHAR_MAX__
#elif defined(WCHAR_MAX)
#define LTC_WCHAR_MAX WCHAR_MAX
#endif
/* please note that it might happen that LTC_WCHAR_MAX is undefined */
2006-12-16 18:10:04 +00:00
#else
typedef ulong32 wchar_t;
2017-03-09 13:08:58 +01:00
#define LTC_WCHAR_MAX 0xFFFFFFFF
2006-12-16 18:10:04 +00:00
#endif
int der_encode_utf8_string(const wchar_t *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen);
int der_decode_utf8_string(const unsigned char *in, unsigned long inlen,
wchar_t *out, unsigned long *outlen);
unsigned long der_utf8_charsize(const wchar_t c);
#ifdef LTC_SOURCE
/* internal helper functions */
int der_utf8_valid_char(const wchar_t c);
#endif /* LTC_SOURCE */
2006-12-16 18:10:04 +00:00
int der_length_utf8_string(const wchar_t *in, unsigned long noctets, unsigned long *outlen);
2005-06-27 11:47:35 +00:00
/* CHOICE */
int der_decode_choice(const unsigned char *in, unsigned long *inlen,
ltc_asn1_list *list, unsigned long outlen);
/* UTCTime */
typedef struct {
unsigned YY, /* year */
MM, /* month */
DD, /* day */
hh, /* hour */
mm, /* minute */
ss, /* second */
off_dir, /* timezone offset direction 0 == +, 1 == - */
off_hh, /* timezone offset hours */
off_mm; /* timezone offset minutes */
} ltc_utctime;
2012-07-26 14:43:15 +02:00
int der_encode_utctime(ltc_utctime *utctime,
2005-06-27 11:47:35 +00:00
unsigned char *out, unsigned long *outlen);
int der_decode_utctime(const unsigned char *in, unsigned long *inlen,
ltc_utctime *out);
int der_length_utctime(ltc_utctime *utctime, unsigned long *outlen);
2017-03-30 18:58:21 +02:00
/* GeneralizedTime */
typedef struct {
unsigned YYYY, /* year */
MM, /* month */
DD, /* day */
hh, /* hour */
mm, /* minute */
ss, /* second */
fs, /* fractional seconds */
off_dir, /* timezone offset direction 0 == +, 1 == - */
off_hh, /* timezone offset hours */
off_mm; /* timezone offset minutes */
2017-03-30 18:58:21 +02:00
} ltc_generalizedtime;
int der_encode_generalizedtime(ltc_generalizedtime *gtime,
unsigned char *out, unsigned long *outlen);
int der_decode_generalizedtime(const unsigned char *in, unsigned long *inlen,
ltc_generalizedtime *out);
int der_length_generalizedtime(ltc_generalizedtime *gtime, unsigned long *outlen);
2005-06-27 11:47:35 +00:00
2004-12-30 23:55:53 +00:00
#endif
2005-06-09 00:08:13 +00:00
2017-06-19 13:43:49 +02:00
/* ref: $Format:%D$ */
/* git commit: $Format:%H$ */
/* commit time: $Format:%ai$ */