2003-03-03 00:59:24 +00:00
|
|
|
/* ---- NUMBER THEORY ---- */
|
|
|
|
#ifdef MPI
|
|
|
|
|
2004-05-31 02:36:47 +00:00
|
|
|
#include "ltc_tommath.h"
|
2003-03-03 01:03:50 +00:00
|
|
|
|
2004-01-25 17:40:34 +00:00
|
|
|
/* in/out macros */
|
2004-02-20 20:03:32 +00:00
|
|
|
#define OUTPUT_BIGNUM(num, out, y, z) \
|
|
|
|
{ \
|
|
|
|
if ((y + 4) > *outlen) { return CRYPT_BUFFER_OVERFLOW; } \
|
|
|
|
z = (unsigned long)mp_unsigned_bin_size(num); \
|
|
|
|
STORE32L(z, out+y); \
|
|
|
|
y += 4; \
|
|
|
|
if ((y + z) > *outlen) { return CRYPT_BUFFER_OVERFLOW; } \
|
|
|
|
if ((err = mp_to_unsigned_bin(num, out+y)) != MP_OKAY) { return mpi_to_ltc_error(err); } \
|
|
|
|
y += z; \
|
2004-01-25 17:40:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-05-12 20:42:16 +00:00
|
|
|
#define INPUT_BIGNUM(num, in, x, y, inlen) \
|
2004-01-25 17:40:34 +00:00
|
|
|
{ \
|
|
|
|
/* load value */ \
|
2004-02-20 20:03:32 +00:00
|
|
|
if ((y + 4) > inlen) { \
|
|
|
|
err = CRYPT_INVALID_PACKET; \
|
2004-01-25 17:40:34 +00:00
|
|
|
goto error; \
|
|
|
|
} \
|
|
|
|
LOAD32L(x, in+y); \
|
|
|
|
y += 4; \
|
|
|
|
\
|
|
|
|
/* sanity check... */ \
|
2004-02-20 20:03:32 +00:00
|
|
|
if ((x+y) > inlen) { \
|
|
|
|
err = CRYPT_INVALID_PACKET; \
|
2004-01-25 17:40:34 +00:00
|
|
|
goto error; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
/* load it */ \
|
|
|
|
if ((err = mp_read_unsigned_bin(num, (unsigned char *)in+y, (int)x)) != MP_OKAY) {\
|
2004-02-20 20:03:32 +00:00
|
|
|
err = mpi_to_ltc_error(err); \
|
2004-01-25 17:40:34 +00:00
|
|
|
goto error; \
|
|
|
|
} \
|
|
|
|
y += x; \
|
2004-02-20 20:03:32 +00:00
|
|
|
if ((err = mp_shrink(num)) != MP_OKAY) { \
|
|
|
|
err = mpi_to_ltc_error(err); \
|
2004-01-25 17:40:34 +00:00
|
|
|
goto error; \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2004-06-20 02:41:49 +00:00
|
|
|
int is_prime(mp_int *, int *);
|
|
|
|
int rand_prime(mp_int *N, long len, prng_state *prng, int wprng);
|
2003-03-03 00:59:24 +00:00
|
|
|
|
2003-03-03 01:03:50 +00:00
|
|
|
#else
|
|
|
|
#ifdef MRSA
|
|
|
|
#error RSA requires the big int library
|
|
|
|
#endif
|
|
|
|
#ifdef MECC
|
|
|
|
#error ECC requires the big int library
|
|
|
|
#endif
|
|
|
|
#ifdef MDH
|
|
|
|
#error DH requires the big int library
|
|
|
|
#endif
|
|
|
|
#ifdef MDSA
|
|
|
|
#error DSA requires the big int library
|
|
|
|
#endif
|
|
|
|
#endif /* MPI */
|
|
|
|
|
2003-03-03 00:59:24 +00:00
|
|
|
|
|
|
|
/* ---- PUBLIC KEY CRYPTO ---- */
|
|
|
|
|
|
|
|
#define PK_PRIVATE 0 /* PK private keys */
|
|
|
|
#define PK_PUBLIC 1 /* PK public keys */
|
|
|
|
|
|
|
|
/* ---- PACKET ---- */
|
|
|
|
#ifdef PACKET
|
|
|
|
|
2004-12-30 23:55:53 +00:00
|
|
|
void packet_store_header(unsigned char *dst, int section, int subsection);
|
|
|
|
int packet_valid_header(unsigned char *src, int section, int subsection);
|
2003-03-03 00:59:24 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/* ---- RSA ---- */
|
|
|
|
#ifdef MRSA
|
2004-05-12 20:42:16 +00:00
|
|
|
|
|
|
|
/* Min and Max RSA key sizes (in bits) */
|
|
|
|
#define MIN_RSA_SIZE 1024
|
|
|
|
#define MAX_RSA_SIZE 4096
|
|
|
|
|
2003-03-03 00:59:24 +00:00
|
|
|
typedef struct Rsa_key {
|
|
|
|
int type;
|
2004-10-30 03:00:26 +00:00
|
|
|
mp_int e, d, N, p, q, qP, dP, 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);
|
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
|
|
|
|
2004-07-23 15:40:22 +00:00
|
|
|
/* These use PKCS #1 v2.0 padding */
|
2004-12-30 23:55:53 +00:00
|
|
|
int rsa_encrypt_key(const unsigned char *in, unsigned long inlen,
|
|
|
|
unsigned char *out, unsigned long *outlen,
|
2004-05-31 02:36:47 +00:00
|
|
|
const unsigned char *lparam, unsigned long lparamlen,
|
|
|
|
prng_state *prng, int prng_idx, int hash_idx, rsa_key *key);
|
|
|
|
|
2004-12-30 23:55:53 +00:00
|
|
|
int rsa_decrypt_key(const unsigned char *in, unsigned long inlen,
|
|
|
|
unsigned char *out, unsigned long *outlen,
|
|
|
|
const unsigned char *lparam, unsigned long lparamlen,
|
|
|
|
int hash_idx, int *stat,
|
2004-05-31 02:36:47 +00:00
|
|
|
rsa_key *key);
|
|
|
|
|
2004-12-30 23:55:53 +00:00
|
|
|
int rsa_sign_hash(const unsigned char *in, unsigned long inlen,
|
|
|
|
unsigned char *out, unsigned long *outlen,
|
2004-05-31 02:36:47 +00:00
|
|
|
prng_state *prng, int prng_idx,
|
|
|
|
int hash_idx, unsigned long saltlen,
|
|
|
|
rsa_key *key);
|
|
|
|
|
|
|
|
int rsa_verify_hash(const unsigned char *sig, unsigned long siglen,
|
2004-12-30 23:55:53 +00:00
|
|
|
const unsigned char *hash, unsigned long hashlen,
|
2004-05-31 02:36:47 +00:00
|
|
|
int hash_idx, unsigned long saltlen,
|
|
|
|
int *stat, rsa_key *key);
|
|
|
|
|
2004-07-23 15:40:22 +00: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);
|
|
|
|
|
2003-03-03 00:59:24 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* ---- DH Routines ---- */
|
|
|
|
#ifdef MDH
|
|
|
|
|
|
|
|
typedef struct Dh_key {
|
|
|
|
int idx, type;
|
|
|
|
mp_int x, y;
|
|
|
|
} dh_key;
|
|
|
|
|
2004-12-30 23:55:53 +00:00
|
|
|
int dh_test(void);
|
|
|
|
void dh_sizes(int *low, int *high);
|
|
|
|
int dh_get_size(dh_key *key);
|
2003-03-03 00:59:24 +00:00
|
|
|
|
2004-12-30 23:55:53 +00:00
|
|
|
int dh_make_key(prng_state *prng, int wprng, int keysize, dh_key *key);
|
|
|
|
void dh_free(dh_key *key);
|
2003-03-03 00:59:24 +00:00
|
|
|
|
2004-12-30 23:55:53 +00:00
|
|
|
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);
|
2003-03-03 00:59:24 +00:00
|
|
|
|
2004-12-30 23:55:53 +00:00
|
|
|
int dh_shared_secret(dh_key *private_key, dh_key *public_key,
|
|
|
|
unsigned char *out, unsigned long *outlen);
|
2003-03-03 00:59:24 +00:00
|
|
|
|
2004-12-30 23:55:53 +00:00
|
|
|
int dh_encrypt_key(const unsigned char *in, unsigned long keylen,
|
|
|
|
unsigned char *out, unsigned long *outlen,
|
|
|
|
prng_state *prng, int wprng, int hash,
|
|
|
|
dh_key *key);
|
2003-03-03 00:59:24 +00:00
|
|
|
|
2004-12-30 23:55:53 +00:00
|
|
|
int dh_decrypt_key(const unsigned char *in, unsigned long inlen,
|
|
|
|
unsigned char *out, unsigned long *outlen,
|
|
|
|
dh_key *key);
|
2003-03-03 00:59:24 +00:00
|
|
|
|
2004-12-30 23:55:53 +00:00
|
|
|
int dh_sign_hash(const unsigned char *in, unsigned long inlen,
|
|
|
|
unsigned char *out, unsigned long *outlen,
|
|
|
|
prng_state *prng, int wprng, dh_key *key);
|
2003-03-03 00:59:24 +00:00
|
|
|
|
2004-12-30 23:55:53 +00:00
|
|
|
int dh_verify_hash(const unsigned char *sig, unsigned long siglen,
|
|
|
|
const unsigned char *hash, unsigned long hashlen,
|
|
|
|
int *stat, dh_key *key);
|
2003-03-03 00:59:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* ---- ECC Routines ---- */
|
|
|
|
#ifdef MECC
|
|
|
|
typedef struct {
|
2005-04-17 11:37:13 +00:00
|
|
|
mp_int x, y, z;
|
2003-03-03 00:59:24 +00:00
|
|
|
} ecc_point;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int type, idx;
|
|
|
|
ecc_point pubkey;
|
|
|
|
mp_int k;
|
|
|
|
} ecc_key;
|
|
|
|
|
2004-12-30 23:55:53 +00:00
|
|
|
int ecc_test(void);
|
|
|
|
void ecc_sizes(int *low, int *high);
|
|
|
|
int ecc_get_size(ecc_key *key);
|
|
|
|
|
|
|
|
int ecc_make_key(prng_state *prng, int wprng, int keysize, ecc_key *key);
|
|
|
|
void ecc_free(ecc_key *key);
|
2003-03-03 00:59:24 +00:00
|
|
|
|
2004-12-30 23:55:53 +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);
|
2003-03-03 00:59:24 +00:00
|
|
|
|
2004-12-30 23:55:53 +00:00
|
|
|
int ecc_shared_secret(ecc_key *private_key, ecc_key *public_key,
|
|
|
|
unsigned char *out, unsigned long *outlen);
|
2003-03-03 00:59:24 +00:00
|
|
|
|
2004-12-30 23:55:53 +00:00
|
|
|
int ecc_encrypt_key(const unsigned char *in, unsigned long inlen,
|
|
|
|
unsigned char *out, unsigned long *outlen,
|
|
|
|
prng_state *prng, int wprng, int hash,
|
|
|
|
ecc_key *key);
|
2003-03-03 00:59:24 +00:00
|
|
|
|
2004-12-30 23:55:53 +00:00
|
|
|
int ecc_decrypt_key(const unsigned char *in, unsigned long inlen,
|
|
|
|
unsigned char *out, unsigned long *outlen,
|
|
|
|
ecc_key *key);
|
2003-03-03 00:59:24 +00:00
|
|
|
|
2004-12-30 23:55:53 +00:00
|
|
|
int ecc_sign_hash(const unsigned char *in, unsigned long inlen,
|
|
|
|
unsigned char *out, unsigned long *outlen,
|
|
|
|
prng_state *prng, int wprng, ecc_key *key);
|
2003-03-03 00:59:24 +00:00
|
|
|
|
2004-12-30 23:55:53 +00:00
|
|
|
int ecc_verify_hash(const unsigned char *sig, unsigned long siglen,
|
|
|
|
const unsigned char *hash, unsigned long hashlen,
|
|
|
|
int *stat, ecc_key *key);
|
2003-03-03 00:59:24 +00:00
|
|
|
|
2003-03-03 01:02:10 +00:00
|
|
|
#endif
|
2003-12-24 18:59:57 +00:00
|
|
|
|
|
|
|
#ifdef MDSA
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int type, qord;
|
|
|
|
mp_int g, q, p, x, y;
|
|
|
|
} 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);
|
|
|
|
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,
|
|
|
|
mp_int *r, mp_int *s,
|
|
|
|
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-06-09 00:08:13 +00:00
|
|
|
int dsa_verify_hash_raw( mp_int *r, mp_int *s,
|
|
|
|
const unsigned char *hash, unsigned long hashlen,
|
|
|
|
int *stat, dsa_key *key);
|
|
|
|
|
2004-12-30 23:55:53 +00:00
|
|
|
int dsa_verify_hash(const unsigned char *sig, unsigned long siglen,
|
|
|
|
const unsigned char *hash, unsigned long hashlen,
|
|
|
|
int *stat, dsa_key *key);
|
2003-12-24 18:59:57 +00:00
|
|
|
|
2004-12-30 23:55:53 +00:00
|
|
|
int dsa_import(const unsigned char *in, unsigned long inlen, dsa_key *key);
|
2003-12-24 18:59:57 +00:00
|
|
|
|
2004-12-30 23:55:53 +00:00
|
|
|
int dsa_export(unsigned char *out, unsigned long *outlen, int type, dsa_key *key);
|
2003-12-24 18:59:57 +00:00
|
|
|
|
2004-12-30 23:55:53 +00:00
|
|
|
int dsa_verify_key(dsa_key *key, int *stat);
|
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
|
|
|
|
|
|
|
enum {
|
|
|
|
LTC_ASN1_EOL,
|
|
|
|
LTC_ASN1_INTEGER,
|
|
|
|
LTC_ASN1_SHORT_INTEGER,
|
|
|
|
LTC_ASN1_BIT_STRING,
|
|
|
|
LTC_ASN1_OCTET_STRING,
|
|
|
|
LTC_ASN1_NULL,
|
|
|
|
LTC_ASN1_OBJECT_IDENTIFIER,
|
|
|
|
LTC_ASN1_IA5_STRING,
|
|
|
|
LTC_ASN1_PRINTABLE_STRING,
|
|
|
|
|
|
|
|
LTC_ASN1_SEQUENCE
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int type;
|
|
|
|
void *data;
|
|
|
|
unsigned long size;
|
|
|
|
} 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); \
|
|
|
|
LTC_MACRO_list[LTC_MACRO_temp].data = (Data); \
|
|
|
|
LTC_MACRO_list[LTC_MACRO_temp].size = (Size); \
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
/* SEQUENCE */
|
|
|
|
int der_encode_sequence(ltc_asn1_list *list, unsigned long inlen,
|
|
|
|
unsigned char *out, unsigned long *outlen);
|
|
|
|
|
|
|
|
int der_decode_sequence(const unsigned char *in, unsigned long inlen,
|
|
|
|
ltc_asn1_list *list, unsigned long outlen);
|
|
|
|
|
|
|
|
int der_length_sequence(ltc_asn1_list *list, unsigned long inlen,
|
|
|
|
unsigned long *outlen);
|
|
|
|
|
|
|
|
/* VA list handy helpers */
|
|
|
|
int der_encode_sequence_multi(unsigned char *out, unsigned long *outlen, ...);
|
|
|
|
int der_decode_sequence_multi(const unsigned char *in, unsigned long inlen, ...);
|
|
|
|
|
|
|
|
/* INTEGER */
|
2004-10-30 03:00:26 +00:00
|
|
|
int der_encode_integer(mp_int *num, unsigned char *out, unsigned long *outlen);
|
2005-06-09 00:08:13 +00:00
|
|
|
int der_decode_integer(const unsigned char *in, unsigned long inlen, mp_int *num);
|
2004-10-30 03:00:26 +00:00
|
|
|
int der_length_integer(mp_int *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_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);
|
|
|
|
|
|
|
|
/* Printable STRING */
|
|
|
|
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);
|
|
|
|
|
2004-12-30 23:55:53 +00:00
|
|
|
#endif
|
2005-06-09 00:08:13 +00:00
|
|
|
|
|
|
|
/* $Source$ */
|
|
|
|
/* $Revision$ */
|
|
|
|
/* $Date$ */
|