diff --git a/src/ltc/hashes/md4.c b/src/ltc/hashes/md4.c index 9550275..e3bc0c5 100644 --- a/src/ltc/hashes/md4.c +++ b/src/ltc/hashes/md4.c @@ -71,7 +71,7 @@ } #ifdef LTC_CLEAN_STACK -static int _md4_compress(hash_state *md, unsigned char *buf) +static int _md4_compress(hash_state *md, const unsigned char *buf) #else static int md4_compress(hash_state *md, const unsigned char *buf) #endif @@ -155,7 +155,7 @@ } #ifdef LTC_CLEAN_STACK -static int md4_compress(hash_state *md, unsigned char *buf) +static int md4_compress(hash_state *md, const unsigned char *buf) { int err; err = _md4_compress(md, buf); diff --git a/src/ltc/hashes/md5.c b/src/ltc/hashes/md5.c index 354c376..a417970 100644 --- a/src/ltc/hashes/md5.c +++ b/src/ltc/hashes/md5.c @@ -96,7 +96,7 @@ #endif #ifdef LTC_CLEAN_STACK -static int _md5_compress(hash_state *md, unsigned char *buf) +static int _md5_compress(hash_state *md, const unsigned char *buf) #else static int md5_compress(hash_state *md, const unsigned char *buf) #endif @@ -214,7 +214,7 @@ } #ifdef LTC_CLEAN_STACK -static int md5_compress(hash_state *md, unsigned char *buf) +static int md5_compress(hash_state *md, const unsigned char *buf) { int err; err = _md5_compress(md, buf); diff --git a/src/ltc/hashes/rmd128.c b/src/ltc/hashes/rmd128.c index 4ddc0c2..b60cb0a 100644 --- a/src/ltc/hashes/rmd128.c +++ b/src/ltc/hashes/rmd128.c @@ -79,7 +79,7 @@ (a) = ROLc((a), (s)); #ifdef LTC_CLEAN_STACK -static int _rmd128_compress(hash_state *md, unsigned char *buf) +static int _rmd128_compress(hash_state *md, const unsigned char *buf) #else static int rmd128_compress(hash_state *md, const unsigned char *buf) #endif @@ -253,7 +253,7 @@ } #ifdef LTC_CLEAN_STACK -static int rmd128_compress(hash_state *md, unsigned char *buf) +static int rmd128_compress(hash_state *md, const unsigned char *buf) { int err; err = _rmd128_compress(md, buf); diff --git a/src/ltc/hashes/rmd160.c b/src/ltc/hashes/rmd160.c index a23de82..d38d2be 100644 --- a/src/ltc/hashes/rmd160.c +++ b/src/ltc/hashes/rmd160.c @@ -99,7 +99,7 @@ #ifdef LTC_CLEAN_STACK -static int _rmd160_compress(hash_state *md, unsigned char *buf) +static int _rmd160_compress(hash_state *md, const unsigned char *buf) #else static int rmd160_compress(hash_state *md, const unsigned char *buf) #endif @@ -311,7 +311,7 @@ } #ifdef LTC_CLEAN_STACK -static int rmd160_compress(hash_state *md, unsigned char *buf) +static int rmd160_compress(hash_state *md, const unsigned char *buf) { int err; err = _rmd160_compress(md, buf); diff --git a/src/ltc/hashes/rmd256.c b/src/ltc/hashes/rmd256.c index 3bec56c..da51a1d 100644 --- a/src/ltc/hashes/rmd256.c +++ b/src/ltc/hashes/rmd256.c @@ -73,7 +73,7 @@ (a) = ROLc((a), (s)); #ifdef LTC_CLEAN_STACK -static int _rmd256_compress(hash_state *md, unsigned char *buf) +static int _rmd256_compress(hash_state *md, const unsigned char *buf) #else static int rmd256_compress(hash_state *md, const unsigned char *buf) #endif @@ -262,7 +262,7 @@ } #ifdef LTC_CLEAN_STACK -static int rmd256_compress(hash_state *md, unsigned char *buf) +static int rmd256_compress(hash_state *md, const unsigned char *buf) { int err; err = _rmd256_compress(md, buf); diff --git a/src/ltc/hashes/rmd320.c b/src/ltc/hashes/rmd320.c index a7d81e7..ba6ba9e 100644 --- a/src/ltc/hashes/rmd320.c +++ b/src/ltc/hashes/rmd320.c @@ -94,7 +94,7 @@ #ifdef LTC_CLEAN_STACK -static int _rmd320_compress(hash_state *md, unsigned char *buf) +static int _rmd320_compress(hash_state *md, const unsigned char *buf) #else static int rmd320_compress(hash_state *md, const unsigned char *buf) #endif @@ -325,7 +325,7 @@ } #ifdef LTC_CLEAN_STACK -static int rmd320_compress(hash_state *md, unsigned char *buf) +static int rmd320_compress(hash_state *md, const unsigned char *buf) { int err; err = _rmd320_compress(md, buf); diff --git a/src/ltc/hashes/sha1.c b/src/ltc/hashes/sha1.c index 824d3eb..f78ef75 100644 --- a/src/ltc/hashes/sha1.c +++ b/src/ltc/hashes/sha1.c @@ -40,7 +40,7 @@ #define F3(x,y,z) (x ^ y ^ z) #ifdef LTC_CLEAN_STACK -static int _sha1_compress(hash_state *md, unsigned char *buf) +static int _sha1_compress(hash_state *md, const unsigned char *buf) #else static int sha1_compress(hash_state *md, const unsigned char *buf) #endif @@ -146,7 +146,7 @@ } #ifdef LTC_CLEAN_STACK -static int sha1_compress(hash_state *md, unsigned char *buf) +static int sha1_compress(hash_state *md, const unsigned char *buf) { int err; err = _sha1_compress(md, buf); diff --git a/src/ltc/hashes/sha2/sha256.c b/src/ltc/hashes/sha2/sha256.c index fbdcc89..3b9c4e2 100644 --- a/src/ltc/hashes/sha2/sha256.c +++ b/src/ltc/hashes/sha2/sha256.c @@ -64,7 +64,7 @@ /* compress 512-bits */ #ifdef LTC_CLEAN_STACK -static int _sha256_compress(hash_state * md, unsigned char *buf) +static int _sha256_compress(hash_state * md, const unsigned char *buf) #else static int sha256_compress(hash_state * md, const unsigned char *buf) #endif @@ -187,7 +187,7 @@ } #ifdef LTC_CLEAN_STACK -static int sha256_compress(hash_state * md, unsigned char *buf) +static int sha256_compress(hash_state * md, const unsigned char *buf) { int err; err = _sha256_compress(md, buf); diff --git a/src/ltc/hashes/sha2/sha512.c b/src/ltc/hashes/sha2/sha512.c index 5d00fe6..c7bffd1 100644 --- a/src/ltc/hashes/sha2/sha512.c +++ b/src/ltc/hashes/sha2/sha512.c @@ -89,7 +89,7 @@ /* compress 1024-bits */ #ifdef LTC_CLEAN_STACK -static int _sha512_compress(hash_state * md, unsigned char *buf) +static int _sha512_compress(hash_state * md, const unsigned char *buf) #else static int sha512_compress(hash_state * md, const unsigned char *buf) #endif @@ -156,7 +156,7 @@ /* compress 1024-bits */ #ifdef LTC_CLEAN_STACK -static int sha512_compress(hash_state * md, unsigned char *buf) +static int sha512_compress(hash_state * md, const unsigned char *buf) { int err; err = _sha512_compress(md, buf); diff --git a/src/ltc/hashes/tiger.c b/src/ltc/hashes/tiger.c index 460a573..c6dce7c 100644 --- a/src/ltc/hashes/tiger.c +++ b/src/ltc/hashes/tiger.c @@ -639,7 +639,7 @@ } #ifdef LTC_CLEAN_STACK -static int tiger_compress(hash_state *md, unsigned char *buf) +static int tiger_compress(hash_state *md, const unsigned char *buf) { int err; err = _tiger_compress(md, buf); diff --git a/src/ltc/hashes/whirl/whirl.c b/src/ltc/hashes/whirl/whirl.c index f864e48..636f03a 100644 --- a/src/ltc/hashes/whirl/whirl.c +++ b/src/ltc/hashes/whirl/whirl.c @@ -109,7 +109,7 @@ #ifdef LTC_CLEAN_STACK -static int whirlpool_compress(hash_state *md, unsigned char *buf) +static int whirlpool_compress(hash_state *md, const unsigned char *buf) { int err; err = _whirlpool_compress(md, buf); diff --git a/src/ltc/headers/tomcrypt_custom.h b/src/ltc/headers/tomcrypt_custom.h index d5d61d0..ab45e76 100644 --- a/src/ltc/headers/tomcrypt_custom.h +++ b/src/ltc/headers/tomcrypt_custom.h @@ -528,16 +528,14 @@ #define LTC_ECC_SECP256R1 #define LTC_ECC_SECP384R1 #define LTC_ECC_SECP521R1 - /* OLD deprecated (but still working) defines */ - #define LTC_ECC112 - #define LTC_ECC128 - #define LTC_ECC160 - #define LTC_ECC192 - #define LTC_ECC224 - #define LTC_ECC256 - #define LTC_ECC384 - #define LTC_ECC521 -#endif +#endif +#endif + +#if defined(LTC_DER) + #ifndef LTC_DER_MAX_RECURSION + /* Maximum recursion limit when processing nested ASN.1 types. */ + #define LTC_DER_MAX_RECURSION 30 + #endif #endif #if defined(LTC_MECC) || defined(LTC_MRSA) || defined(LTC_MDSA) || defined(LTC_MKAT) @@ -646,6 +644,40 @@ #endif #endif +/* ECC backwards compatibility */ +#if !defined(LTC_ECC_SECP112R1) && defined(LTC_ECC112) +#define LTC_ECC_SECP112R1 +#undef LTC_ECC112 +#endif +#if !defined(LTC_ECC_SECP128R1) && defined(LTC_ECC128) +#define LTC_ECC_SECP128R1 +#undef LTC_ECC128 +#endif +#if !defined(LTC_ECC_SECP160R1) && defined(LTC_ECC160) +#define LTC_ECC_SECP160R1 +#undef LTC_ECC160 +#endif +#if !defined(LTC_ECC_SECP192R1) && defined(LTC_ECC192) +#define LTC_ECC_SECP192R1 +#undef LTC_ECC192 +#endif +#if !defined(LTC_ECC_SECP224R1) && defined(LTC_ECC224) +#define LTC_ECC_SECP224R1 +#undef LTC_ECC224 +#endif +#if !defined(LTC_ECC_SECP256R1) && defined(LTC_ECC256) +#define LTC_ECC_SECP256R1 +#undef LTC_ECC256 +#endif +#if !defined(LTC_ECC_SECP384R1) && defined(LTC_ECC384) +#define LTC_ECC_SECP384R1 +#undef LTC_ECC384 +#endif +#if !defined(LTC_ECC_SECP512R1) && defined(LTC_ECC521) +#define LTC_ECC_SECP521R1 +#undef LTC_ECC521 +#endif + /* ref: $Format:%D$ */ /* git commit: $Format:%H$ */ /* commit time: $Format:%ai$ */ diff --git a/src/ltc/headers/tomcrypt_hash.h b/src/ltc/headers/tomcrypt_hash.h index 60f0cb5..ea2077e 100644 --- a/src/ltc/headers/tomcrypt_hash.h +++ b/src/ltc/headers/tomcrypt_hash.h @@ -523,7 +523,7 @@ } \ while (inlen > 0) { \ if (md-> state_var .curlen == 0 && inlen >= block_size) { \ - if ((err = compress_name (md, (unsigned char *)in)) != CRYPT_OK) { \ + if ((err = compress_name (md, in)) != CRYPT_OK) { \ return err; \ } \ md-> state_var .length += block_size * 8; \ diff --git a/src/ltc/headers/tomcrypt_pk.h b/src/ltc/headers/tomcrypt_pk.h index e059f47..c3d05da 100644 --- a/src/ltc/headers/tomcrypt_pk.h +++ b/src/ltc/headers/tomcrypt_pk.h @@ -340,58 +340,58 @@ int ecc_test(void); void ecc_sizes(int *low, int *high); -int ecc_get_size(ecc_key *key); +int ecc_get_size(const ecc_key *key); int ecc_get_curve(const char* name_or_oid, const ltc_ecc_curve** cu); int ecc_set_dp(const ltc_ecc_curve *cu, ecc_key *key); int ecc_generate_key(prng_state *prng, int wprng, ecc_key *key); int ecc_set_key(const unsigned char *in, unsigned long inlen, int type, ecc_key *key); -int ecc_get_key(unsigned char *out, unsigned long *outlen, int type, ecc_key *key); +int ecc_get_key(unsigned char *out, unsigned long *outlen, int type, const ecc_key *key); int ecc_make_key(prng_state *prng, int wprng, int keysize, ecc_key *key); int ecc_make_key_ex(prng_state *prng, int wprng, ecc_key *key, const ltc_ecc_curve *cu); void ecc_free(ecc_key *key); -int ecc_export(unsigned char *out, unsigned long *outlen, int type, ecc_key *key); +int ecc_export(unsigned char *out, unsigned long *outlen, int type, const ecc_key *key); int ecc_import(const unsigned char *in, unsigned long inlen, ecc_key *key); int ecc_import_ex(const unsigned char *in, unsigned long inlen, ecc_key *key, const ltc_ecc_curve *cu); -int ecc_ansi_x963_export(ecc_key *key, unsigned char *out, unsigned long *outlen); +int ecc_ansi_x963_export(const ecc_key *key, unsigned char *out, unsigned long *outlen); int ecc_ansi_x963_import(const unsigned char *in, unsigned long inlen, ecc_key *key); int ecc_ansi_x963_import_ex(const unsigned char *in, unsigned long inlen, ecc_key *key, const ltc_ecc_curve *cu); -int ecc_export_openssl(unsigned char *out, unsigned long *outlen, int type, ecc_key *key); +int ecc_export_openssl(unsigned char *out, unsigned long *outlen, int type, const ecc_key *key); int ecc_import_openssl(const unsigned char *in, unsigned long inlen, ecc_key *key); int ecc_import_pkcs8(const unsigned char *in, unsigned long inlen, const void *pwd, unsigned long pwdlen, ecc_key *key); int ecc_import_x509(const unsigned char *in, unsigned long inlen, ecc_key *key); -int ecc_shared_secret(ecc_key *private_key, ecc_key *public_key, +int ecc_shared_secret(const ecc_key *private_key, const ecc_key *public_key, unsigned char *out, unsigned long *outlen); 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); + const ecc_key *key); int ecc_decrypt_key(const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen, - ecc_key *key); + const ecc_key *key); 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); + prng_state *prng, int wprng, const ecc_key *key); 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); + prng_state *prng, int wprng, const ecc_key *key); int ecc_verify_hash_rfc7518(const unsigned char *sig, unsigned long siglen, const unsigned char *hash, unsigned long hashlen, - int *stat, ecc_key *key); + int *stat, const ecc_key *key); int ecc_verify_hash(const unsigned char *sig, unsigned long siglen, const unsigned char *hash, unsigned long hashlen, - int *stat, ecc_key *key); + int *stat, const ecc_key *key); #ifdef LTC_SOURCE @@ -410,7 +410,7 @@ int ltc_ecc_is_point_at_infinity(const ecc_point *P, void *modulus, int *retval); int ltc_ecc_import_point(const unsigned char *in, unsigned long inlen, void *prime, void *a, void *b, void *x, void *y); int ltc_ecc_export_point(unsigned char *out, unsigned long *outlen, void *x, void *y, unsigned long size, int compressed); -int ltc_ecc_verify_key(ecc_key *key); +int ltc_ecc_verify_key(const ecc_key *key); /* point ops (mp == montgomery digit) */ #if !defined(LTC_MECC_ACCEL) || defined(LTM_DESC) || defined(GMP_DESC) diff --git a/src/ltc/misc/crypt/crypt.c b/src/ltc/misc/crypt/crypt.c index 6705bea..d8eaa73 100644 --- a/src/ltc/misc/crypt/crypt.c +++ b/src/ltc/misc/crypt/crypt.c @@ -428,6 +428,7 @@ #endif #if defined(LTC_DER) " DER " + " " NAME_VALUE(LTC_DER_MAX_RECURSION) " " #endif #if defined(LTC_PKCS_1) " PKCS#1 " diff --git a/src/ltc/misc/crypt/crypt_constants.c b/src/ltc/misc/crypt/crypt_constants.c index c551769..902b774 100644 --- a/src/ltc/misc/crypt/crypt_constants.c +++ b/src/ltc/misc/crypt/crypt_constants.c @@ -126,6 +126,7 @@ #ifdef LTC_DER /* DER handling */ + {"LTC_DER", 1}, _C_STRINGIFY(LTC_ASN1_EOL), _C_STRINGIFY(LTC_ASN1_BOOLEAN), _C_STRINGIFY(LTC_ASN1_INTEGER), @@ -146,6 +147,9 @@ _C_STRINGIFY(LTC_ASN1_TELETEX_STRING), _C_STRINGIFY(LTC_ASN1_GENERALIZEDTIME), _C_STRINGIFY(LTC_ASN1_CUSTOM_TYPE), + _C_STRINGIFY(LTC_DER_MAX_RECURSION), +#else + {"LTC_DER", 0}, #endif #ifdef LTC_CTR_MODE diff --git a/src/ltc/pk/asn1/der/sequence/der_decode_sequence_flexi.c b/src/ltc/pk/asn1/der/sequence/der_decode_sequence_flexi.c index 756a6f9..a6c3cf7 100644 --- a/src/ltc/pk/asn1/der/sequence/der_decode_sequence_flexi.c +++ b/src/ltc/pk/asn1/der/sequence/der_decode_sequence_flexi.c @@ -43,7 +43,7 @@ */ int der_decode_sequence_flexi(const unsigned char *in, unsigned long *inlen, ltc_asn1_list **out) { - ltc_asn1_list *l; + ltc_asn1_list *l, *t; unsigned long err, identifier, len, totlen, data_offset, id_len, len_len; void *realloc_tmp; @@ -463,6 +463,17 @@ l->child->parent = l; } + t = l; + len_len = 0; + while((t != NULL) && (t->child != NULL)) { + len_len++; + t = t->child; + } + if (len_len > LTC_DER_MAX_RECURSION) { + err = CRYPT_PK_ASN1_ERROR; + goto error; + } + break; case 0x80: /* Context-specific */ diff --git a/src/ltc/pk/ecc/ecc.c b/src/ltc/pk/ecc/ecc.c index ea0a0f6..802097f 100644 --- a/src/ltc/pk/ecc/ecc.c +++ b/src/ltc/pk/ecc/ecc.c @@ -24,7 +24,7 @@ * - http://www.ecc-brainpool.org/download/Domain-parameters.pdf (named: BRAINPOOLP*) */ const ltc_ecc_curve ltc_ecc_curves[] = { -#if defined(LTC_ECC_SECP112R1) || defined(LTC_ECC112) +#ifdef LTC_ECC_SECP112R1 { /* prime */ "DB7C2ABF62E35E668076BEAD208B", /* A */ "DB7C2ABF62E35E668076BEAD2088", @@ -48,7 +48,7 @@ /* OID */ "1.3.132.0.7" }, #endif -#if defined(LTC_ECC_SECP128R1) || defined(LTC_ECC128) +#ifdef LTC_ECC_SECP128R1 { /* prime */ "FFFFFFFDFFFFFFFFFFFFFFFFFFFFFFFF", /* A */ "FFFFFFFDFFFFFFFFFFFFFFFFFFFFFFFC", @@ -72,7 +72,7 @@ /* OID */ "1.3.132.0.29" }, #endif -#if defined(LTC_ECC_SECP160R1) || defined(LTC_ECC160) +#ifdef LTC_ECC_SECP160R1 { /* prime */ "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFF", /* A */ "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFC", @@ -108,7 +108,7 @@ /* OID */ "1.3.132.0.9" }, #endif -#if defined(LTC_ECC_SECP192R1) || defined(LTC_ECC192) +#ifdef LTC_ECC_SECP192R1 { /* prime */ "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF", /* A */ "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFC", @@ -156,7 +156,7 @@ /* OID */ "1.3.132.0.31" }, #endif -#if defined(LTC_ECC_SECP224R1) || defined(LTC_ECC224) +#ifdef LTC_ECC_SECP224R1 { /* prime */ "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001", /* A */ "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE", @@ -180,7 +180,7 @@ /* OID */ "1.3.132.0.32" }, #endif -#if defined(LTC_ECC_SECP256R1) || defined(LTC_ECC256) +#ifdef LTC_ECC_SECP256R1 { /* prime */ "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF", /* A */ "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC", @@ -204,7 +204,7 @@ /* OID */ "1.3.132.0.10" }, #endif -#if defined(LTC_ECC_SECP384R1) || defined(LTC_ECC384) +#ifdef LTC_ECC_SECP384R1 { /* prime */ "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF", /* A */ "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFC", @@ -216,7 +216,7 @@ /* OID */ "1.3.132.0.34" }, #endif -#if defined(LTC_ECC_SECP521R1) || defined(LTC_ECC521) +#ifdef LTC_ECC_SECP521R1 { /* prime */ "01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", /* A */ "01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC", diff --git a/src/ltc/pk/ecc/ecc_ansi_x963_export.c b/src/ltc/pk/ecc/ecc_ansi_x963_export.c index 528dcd8..b752b6b 100644 --- a/src/ltc/pk/ecc/ecc_ansi_x963_export.c +++ b/src/ltc/pk/ecc/ecc_ansi_x963_export.c @@ -22,7 +22,7 @@ @param outlen [in/out] Length of destination and final output size Return CRYPT_OK on success */ -int ecc_ansi_x963_export(ecc_key *key, unsigned char *out, unsigned long *outlen) +int ecc_ansi_x963_export(const ecc_key *key, unsigned char *out, unsigned long *outlen) { unsigned char buf[ECC_BUF_SIZE]; unsigned long numlen, xlen, ylen; diff --git a/src/ltc/pk/ecc/ecc_decrypt_key.c b/src/ltc/pk/ecc/ecc_decrypt_key.c index b5298d2..6ce93d5 100644 --- a/src/ltc/pk/ecc/ecc_decrypt_key.c +++ b/src/ltc/pk/ecc/ecc_decrypt_key.c @@ -27,7 +27,7 @@ */ int ecc_decrypt_key(const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen, - ecc_key *key) + const ecc_key *key) { unsigned char *ecc_shared, *skey, *pub_expt; unsigned long x, y; diff --git a/src/ltc/pk/ecc/ecc_encrypt_key.c b/src/ltc/pk/ecc/ecc_encrypt_key.c index 1ccfd71..e0beb69 100644 --- a/src/ltc/pk/ecc/ecc_encrypt_key.c +++ b/src/ltc/pk/ecc/ecc_encrypt_key.c @@ -31,7 +31,7 @@ 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) + const ecc_key *key) { unsigned char *pub_expt, *ecc_shared, *skey; ecc_key pubkey; diff --git a/src/ltc/pk/ecc/ecc_export.c b/src/ltc/pk/ecc/ecc_export.c index a095e9a..eab854a 100644 --- a/src/ltc/pk/ecc/ecc_export.c +++ b/src/ltc/pk/ecc/ecc_export.c @@ -24,7 +24,7 @@ @param key The key to export @return CRYPT_OK if successful */ -int ecc_export(unsigned char *out, unsigned long *outlen, int type, ecc_key *key) +int ecc_export(unsigned char *out, unsigned long *outlen, int type, const ecc_key *key) { int err; unsigned char flags[1]; diff --git a/src/ltc/pk/ecc/ecc_export_openssl.c b/src/ltc/pk/ecc/ecc_export_openssl.c index c918137..4f47dd3 100644 --- a/src/ltc/pk/ecc/ecc_export_openssl.c +++ b/src/ltc/pk/ecc/ecc_export_openssl.c @@ -20,89 +20,86 @@ @return CRYPT_OK if successful */ -int ecc_export_openssl(unsigned char *out, unsigned long *outlen, int type, ecc_key *key) +int ecc_export_openssl(unsigned char *out, unsigned long *outlen, int type, const ecc_key *key) { - int err; - void *prime, *order, *a, *b, *gx, *gy; - unsigned char bin_a[256], bin_b[256], bin_k[256], bin_g[512], bin_xy[512]; - unsigned long len_a, len_b, len_k, len_g, len_xy; - unsigned long cofactor, one = 1; - oid_st oid; - ltc_asn1_list seq_fieldid[2], seq_curve[2], seq_ecparams[6], seq_priv[4], pub_xy, ecparams; - int flag_oid = type & PK_CURVEOID ? 1 : 0; - int flag_com = type & PK_COMPRESSED ? 1 : 0; - int flag_pri = type & PK_PRIVATE ? 1 : 0; + int err; + void *prime, *order, *a, *b, *gx, *gy; + unsigned char bin_a[256], bin_b[256], bin_k[256], bin_g[512], bin_xy[512]; + unsigned long len_a, len_b, len_k, len_g, len_xy; + unsigned long cofactor, one = 1; + oid_st oid; + ltc_asn1_list seq_fieldid[2], seq_curve[2], seq_ecparams[6], seq_priv[4], pub_xy, ecparams; + int flag_oid = type & PK_CURVEOID ? 1 : 0; + int flag_com = type & PK_COMPRESSED ? 1 : 0; + int flag_pri = type & PK_PRIVATE ? 1 : 0; - LTC_ARGCHK(out != NULL); - LTC_ARGCHK(outlen != NULL); - LTC_ARGCHK(key != NULL); + LTC_ARGCHK(out != NULL); + LTC_ARGCHK(outlen != NULL); + LTC_ARGCHK(key != NULL); - if (key->type != PK_PRIVATE && flag_pri) return CRYPT_PK_TYPE_MISMATCH; + if (key->type != PK_PRIVATE && flag_pri) return CRYPT_PK_TYPE_MISMATCH; - prime = key->dp.prime; - order = key->dp.order; - b = key->dp.B; - a = key->dp.A; - gx = key->dp.base.x; - gy = key->dp.base.y; + prime = key->dp.prime; + order = key->dp.order; + b = key->dp.B; + a = key->dp.A; + gx = key->dp.base.x; + gy = key->dp.base.y; - /* curve param a */ - len_a = mp_unsigned_bin_size(a); - if (len_a > sizeof(bin_a)) { err = CRYPT_BUFFER_OVERFLOW; goto error; } - if ((err = mp_to_unsigned_bin(a, bin_a)) != CRYPT_OK) goto error; - if (len_a == 0) { len_a = 1; bin_a[0] = 0; } /* XXX-TODO hack to handle case a == 0 */ + /* curve param a */ + len_a = mp_unsigned_bin_size(a); + if (len_a > sizeof(bin_a)) { err = CRYPT_BUFFER_OVERFLOW; goto error; } + if ((err = mp_to_unsigned_bin(a, bin_a)) != CRYPT_OK) { goto error; } + if (len_a == 0) { len_a = 1; bin_a[0] = 0; } /* handle case a == 0 */ - /* curve param b */ - len_b = mp_unsigned_bin_size(b); - if (len_b > sizeof(bin_b)) { err = CRYPT_BUFFER_OVERFLOW; goto error; } - if ((err = mp_to_unsigned_bin(b, bin_b)) != CRYPT_OK) goto error; - if (len_b == 0) { len_b = 1; bin_b[0] = 0; } /* XXX-TODO hack to handle case b == 0 */ + /* curve param b */ + len_b = mp_unsigned_bin_size(b); + if (len_b > sizeof(bin_b)) { err = CRYPT_BUFFER_OVERFLOW; goto error; } + if ((err = mp_to_unsigned_bin(b, bin_b)) != CRYPT_OK) { goto error; } + if (len_b == 0) { len_b = 1; bin_b[0] = 0; } /* handle case b == 0 */ - /* base point - (un)compressed based on flag_com */ - len_g = sizeof(bin_g); - if ((err = ltc_ecc_export_point(bin_g, &len_g, gx, gy, key->dp.size, flag_com)) != CRYPT_OK) goto error; + /* base point - (un)compressed based on flag_com */ + len_g = sizeof(bin_g); + err = ltc_ecc_export_point(bin_g, &len_g, gx, gy, key->dp.size, flag_com); + if (err != CRYPT_OK) { goto error; } - /* public key - (un)compressed based on flag_com */ - len_xy = sizeof(bin_xy); - if ((err = ltc_ecc_export_point(bin_xy, &len_xy, key->pubkey.x, key->pubkey.y, key->dp.size, flag_com)) != CRYPT_OK) goto error; + /* public key - (un)compressed based on flag_com */ + len_xy = sizeof(bin_xy); + err = ltc_ecc_export_point(bin_xy, &len_xy, key->pubkey.x, key->pubkey.y, key->dp.size, flag_com); + if (err != CRYPT_OK) { goto error; } - /* co-factor */ - cofactor = key->dp.cofactor; + /* co-factor */ + cofactor = key->dp.cofactor; - /* we support only prime-field EC */ - if ((err = pk_get_oid(PKA_EC_PRIMEF, &oid)) != CRYPT_OK) goto error; + /* we support only prime-field EC */ + if ((err = pk_get_oid(PKA_EC_PRIMEF, &oid)) != CRYPT_OK) { goto error; } - if (flag_oid) { - /* from http://tools.ietf.org/html/rfc5912 - - ECParameters ::= CHOICE { - namedCurve CURVE.&id({NamedCurve}) # OBJECT - } + if (flag_oid) { + /* http://tools.ietf.org/html/rfc5912 + ECParameters ::= CHOICE { + namedCurve CURVE.&id({NamedCurve}) # OBJECT + } */ - if (key->dp.oidlen == 0) { - err = CRYPT_INVALID_ARG; - goto error; - } + if (key->dp.oidlen == 0) { err = CRYPT_INVALID_ARG; goto error; } LTC_SET_ASN1(&ecparams, 0, LTC_ASN1_OBJECT_IDENTIFIER, key->dp.oid, key->dp.oidlen); - } - else { - /* from http://tools.ietf.org/html/rfc3279 - - ECParameters ::= SEQUENCE { # SEQUENCE - version INTEGER { ecpVer1(1) } (ecpVer1), # INTEGER :01 - FieldID ::= SEQUENCE { # SEQUENCE - fieldType FIELD-ID.&id({IOSet}), # OBJECT :prime-field - parameters FIELD-ID.&Type({IOSet}{@fieldType}) # INTEGER - } - Curve ::= SEQUENCE { # SEQUENCE - a FieldElement ::= OCTET STRING # OCTET STRING - b FieldElement ::= OCTET STRING # OCTET STRING - seed BIT STRING OPTIONAL - } - base ECPoint ::= OCTET STRING # OCTET STRING - order INTEGER, # INTEGER - cofactor INTEGER OPTIONAL # INTEGER - } + } + else { + /* http://tools.ietf.org/html/rfc3279 + ECParameters ::= SEQUENCE { # SEQUENCE + version INTEGER { ecpVer1(1) } (ecpVer1) # INTEGER :01 + FieldID ::= SEQUENCE { # SEQUENCE + fieldType FIELD-ID.&id({IOSet}), # OBJECT :prime-field + parameters FIELD-ID.&Type({IOSet}{@fieldType}) # INTEGER + } + Curve ::= SEQUENCE { # SEQUENCE + a FieldElement ::= OCTET STRING # OCTET STRING + b FieldElement ::= OCTET STRING # OCTET STRING + seed BIT STRING OPTIONAL + } + base ECPoint ::= OCTET STRING # OCTET STRING + order INTEGER, # INTEGER + cofactor INTEGER OPTIONAL # INTEGER + } */ /* FieldID SEQUENCE */ @@ -123,50 +120,47 @@ /* ECParameters used by ECPrivateKey or SubjectPublicKeyInfo below */ LTC_SET_ASN1(&ecparams, 0, LTC_ASN1_SEQUENCE, seq_ecparams, 6UL); - } + } - if (flag_pri) { - /* private key format: http://tools.ietf.org/html/rfc5915 - - ECPrivateKey ::= SEQUENCE { # SEQUENCE - version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1), # INTEGER :01 - privateKey OCTET STRING, # OCTET STRING - [0] ECParameters # see above - [1] publicKey # BIT STRING - } + if (flag_pri) { + /* http://tools.ietf.org/html/rfc5915 + ECPrivateKey ::= SEQUENCE { # SEQUENCE + version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1) # INTEGER :01 + privateKey OCTET STRING, # OCTET STRING + [0] ECParameters # see above + [1] publicKey # BIT STRING + } */ /* private key */ len_k = mp_unsigned_bin_size(key->k); - if (len_k > sizeof(bin_k)) { err = CRYPT_BUFFER_OVERFLOW; goto error; } - if ((err = mp_to_unsigned_bin(key->k, bin_k)) != CRYPT_OK) goto error; + if (len_k > sizeof(bin_k)) { err = CRYPT_BUFFER_OVERFLOW; goto error; } + if ((err = mp_to_unsigned_bin(key->k, bin_k)) != CRYPT_OK) { goto error; } - LTC_SET_ASN1(&pub_xy, 0, LTC_ASN1_RAW_BIT_STRING, bin_xy, 8*len_xy); - LTC_SET_ASN1(seq_priv, 0, LTC_ASN1_SHORT_INTEGER, &one, 1); - LTC_SET_ASN1(seq_priv, 1, LTC_ASN1_OCTET_STRING, bin_k, len_k); + LTC_SET_ASN1(&pub_xy, 0, LTC_ASN1_RAW_BIT_STRING, bin_xy, 8*len_xy); + LTC_SET_ASN1(seq_priv, 0, LTC_ASN1_SHORT_INTEGER, &one, 1); + LTC_SET_ASN1(seq_priv, 1, LTC_ASN1_OCTET_STRING, bin_k, len_k); LTC_SET_ASN1_CUSTOM_CONSTRUCTED(seq_priv, 2, LTC_ASN1_CL_CONTEXT_SPECIFIC, 0, &ecparams); /* context specific 0 */ LTC_SET_ASN1_CUSTOM_CONSTRUCTED(seq_priv, 3, LTC_ASN1_CL_CONTEXT_SPECIFIC, 1, &pub_xy); /* context specific 1 */ err = der_encode_sequence(seq_priv, 4, out, outlen); - } - else { - /* public key format: http://tools.ietf.org/html/rfc5480 - - SubjectPublicKeyInfo ::= SEQUENCE { # SEQUENCE - AlgorithmIdentifier ::= SEQUENCE { # SEQUENCE - algorithm OBJECT IDENTIFIER # OBJECT :id-ecPublicKey - ECParameters # see above - } - subjectPublicKey BIT STRING # BIT STRING - } + } + else { + /* http://tools.ietf.org/html/rfc5480 + SubjectPublicKeyInfo ::= SEQUENCE { # SEQUENCE + AlgorithmIdentifier ::= SEQUENCE { # SEQUENCE + algorithm OBJECT IDENTIFIER # OBJECT :id-ecPublicKey + ECParameters # see above + } + subjectPublicKey BIT STRING # BIT STRING + } */ - err = x509_encode_subject_public_key_info( out, outlen, - PKA_EC, bin_xy, len_xy, - ecparams.type, ecparams.data, ecparams.size ); - } + err = x509_encode_subject_public_key_info( out, outlen, PKA_EC, bin_xy, len_xy, + ecparams.type, ecparams.data, ecparams.size ); + } error: - return err; + return err; } #endif diff --git a/src/ltc/pk/ecc/ecc_get_curve.c b/src/ltc/pk/ecc/ecc_get_curve.c index 7e47b49..7f03862 100644 --- a/src/ltc/pk/ecc/ecc_get_curve.c +++ b/src/ltc/pk/ecc/ecc_get_curve.c @@ -11,11 +11,11 @@ #ifdef LTC_MECC -const struct { +static const struct { const char *OID; const char *names[6]; } _curve_names[] = { -#if defined(LTC_ECC_SECP112R1) || defined(LTC_ECC112) +#ifdef LTC_ECC_SECP112R1 { "1.3.132.0.6", { "SECP112R1", "ECC-112", NULL } }, @@ -25,7 +25,7 @@ "1.3.132.0.7", { "SECP112R2", NULL } }, #endif -#if defined(LTC_ECC_SECP128R1) || defined(LTC_ECC128) +#ifdef LTC_ECC_SECP128R1 { "1.3.132.0.28", { "SECP128R1", "ECC-128", NULL } }, @@ -35,7 +35,7 @@ "1.3.132.0.29", { "SECP128R2", NULL } }, #endif -#if defined(LTC_ECC_SECP160R1) || defined(LTC_ECC160) +#ifdef LTC_ECC_SECP160R1 { "1.3.132.0.8", { "SECP160R1", "ECC-160", NULL } }, @@ -50,7 +50,7 @@ "1.3.132.0.9", { "SECP160K1", NULL } }, #endif -#if defined(LTC_ECC_SECP192R1) || defined(LTC_ECC192) +#ifdef LTC_ECC_SECP192R1 { "1.2.840.10045.3.1.1", { "SECP192R1", "NISTP192", "PRIME192V1", "ECC-192", "P-192", NULL } }, @@ -70,7 +70,7 @@ "1.3.132.0.31", { "SECP192K1", NULL } }, #endif -#if defined(LTC_ECC_SECP224R1) || defined(LTC_ECC224) +#ifdef LTC_ECC_SECP224R1 { "1.3.132.0.33", { "SECP224R1", "NISTP224", "ECC-224", "P-224", NULL } }, @@ -80,7 +80,7 @@ "1.3.132.0.32", { "SECP224K1", NULL } }, #endif -#if defined(LTC_ECC_SECP256R1) || defined(LTC_ECC256) +#ifdef LTC_ECC_SECP256R1 { "1.2.840.10045.3.1.7", { "SECP256R1", "NISTP256", "PRIME256V1", "ECC-256", "P-256", NULL } }, @@ -90,12 +90,12 @@ "1.3.132.0.10", { "SECP256K1", NULL } }, #endif -#if defined(LTC_ECC_SECP384R1) || defined(LTC_ECC384) +#ifdef LTC_ECC_SECP384R1 { "1.3.132.0.34", { "SECP384R1", "NISTP384", "ECC-384", "P-384", NULL } }, #endif -#if defined(LTC_ECC_SECP521R1) || defined(LTC_ECC521) +#ifdef LTC_ECC_SECP521R1 { "1.3.132.0.35", { "SECP521R1", "NISTP521", "ECC-521", "P-521", NULL } }, diff --git a/src/ltc/pk/ecc/ecc_get_key.c b/src/ltc/pk/ecc/ecc_get_key.c index 2b4e857..2154e7f 100644 --- a/src/ltc/pk/ecc/ecc_get_key.c +++ b/src/ltc/pk/ecc/ecc_get_key.c @@ -19,7 +19,7 @@ Return CRYPT_OK on success */ -int ecc_get_key(unsigned char *out, unsigned long *outlen, int type, ecc_key *key) +int ecc_get_key(unsigned char *out, unsigned long *outlen, int type, const ecc_key *key) { unsigned long size, ksize; int err, compressed; diff --git a/src/ltc/pk/ecc/ecc_get_size.c b/src/ltc/pk/ecc/ecc_get_size.c index 8d1c936..3f5810c 100644 --- a/src/ltc/pk/ecc/ecc_get_size.c +++ b/src/ltc/pk/ecc/ecc_get_size.c @@ -21,7 +21,7 @@ @param key The key to get the size of @return The size (octets) of the key or INT_MAX on error */ -int ecc_get_size(ecc_key *key) +int ecc_get_size(const ecc_key *key) { if (key == NULL) { return INT_MAX; diff --git a/src/ltc/pk/ecc/ecc_import_openssl.c b/src/ltc/pk/ecc/ecc_import_openssl.c index 143e022..a9e25a6 100644 --- a/src/ltc/pk/ecc/ecc_import_openssl.c +++ b/src/ltc/pk/ecc/ecc_import_openssl.c @@ -15,7 +15,8 @@ { void *prime, *order, *a, *b, *gx, *gy; ltc_asn1_list seq_fieldid[2], seq_curve[3], seq_ecparams[6], seq_priv[4], custom[2]; - unsigned char bin_a[ECC_MAXSIZE], bin_b[ECC_MAXSIZE], bin_k[ECC_MAXSIZE], bin_g[2*ECC_MAXSIZE+1], bin_xy[2*ECC_MAXSIZE+2], bin_seed[128]; + unsigned char bin_a[ECC_MAXSIZE], bin_b[ECC_MAXSIZE], bin_k[ECC_MAXSIZE]; + unsigned char bin_g[2*ECC_MAXSIZE+1], bin_xy[2*ECC_MAXSIZE+2], bin_seed[128]; unsigned long len_a, len_b, len_k, len_g, len_xy, len_oid, len; unsigned long cofactor = 0, ecver = 0, pkver = 0, tmpoid[16], curveoid[16]; char OID[256]; @@ -30,7 +31,8 @@ len_xy = sizeof(bin_xy); len_oid = 16; - err = x509_decode_subject_public_key_info(in, inlen, PKA_EC, bin_xy, &len_xy, LTC_ASN1_OBJECT_IDENTIFIER, (void *)curveoid, &len_oid); + err = x509_decode_subject_public_key_info(in, inlen, PKA_EC, bin_xy, &len_xy, + LTC_ASN1_OBJECT_IDENTIFIER, (void *)curveoid, &len_oid); if (err == CRYPT_OK) { /* load curve parameters for given curve OID */ len = sizeof(OID); @@ -45,20 +47,20 @@ /* ### 2. try to load public key - curve parameters included */ /* ECParameters SEQUENCE */ - LTC_SET_ASN1(seq_ecparams, 0, LTC_ASN1_SHORT_INTEGER, &ecver, 1UL); - LTC_SET_ASN1(seq_ecparams, 1, LTC_ASN1_SEQUENCE, seq_fieldid, 2UL); - LTC_SET_ASN1(seq_ecparams, 2, LTC_ASN1_SEQUENCE, seq_curve, 3UL); - LTC_SET_ASN1(seq_ecparams, 3, LTC_ASN1_OCTET_STRING, bin_g, (unsigned long)2*ECC_MAXSIZE+1); - LTC_SET_ASN1(seq_ecparams, 4, LTC_ASN1_INTEGER, order, 1UL); - LTC_SET_ASN1(seq_ecparams, 5, LTC_ASN1_SHORT_INTEGER, &cofactor, 1UL); + LTC_SET_ASN1(seq_ecparams, 0, LTC_ASN1_SHORT_INTEGER, &ecver, 1UL); + LTC_SET_ASN1(seq_ecparams, 1, LTC_ASN1_SEQUENCE, seq_fieldid, 2UL); + LTC_SET_ASN1(seq_ecparams, 2, LTC_ASN1_SEQUENCE, seq_curve, 3UL); + LTC_SET_ASN1(seq_ecparams, 3, LTC_ASN1_OCTET_STRING, bin_g, (unsigned long)2*ECC_MAXSIZE+1); + LTC_SET_ASN1(seq_ecparams, 4, LTC_ASN1_INTEGER, order, 1UL); + LTC_SET_ASN1(seq_ecparams, 5, LTC_ASN1_SHORT_INTEGER, &cofactor, 1UL); seq_ecparams[5].optional = 1; /* FieldID SEQUENCE */ - LTC_SET_ASN1(seq_fieldid, 0, LTC_ASN1_OBJECT_IDENTIFIER, tmpoid, 16UL); - LTC_SET_ASN1(seq_fieldid, 1, LTC_ASN1_INTEGER, prime, 1UL); + LTC_SET_ASN1(seq_fieldid, 0, LTC_ASN1_OBJECT_IDENTIFIER, tmpoid, 16UL); + LTC_SET_ASN1(seq_fieldid, 1, LTC_ASN1_INTEGER, prime, 1UL); /* Curve SEQUENCE */ - LTC_SET_ASN1(seq_curve, 0, LTC_ASN1_OCTET_STRING, bin_a, (unsigned long)ECC_MAXSIZE); - LTC_SET_ASN1(seq_curve, 1, LTC_ASN1_OCTET_STRING, bin_b, (unsigned long)ECC_MAXSIZE); - LTC_SET_ASN1(seq_curve, 2, LTC_ASN1_RAW_BIT_STRING, bin_seed, (unsigned long)8*128); + LTC_SET_ASN1(seq_curve, 0, LTC_ASN1_OCTET_STRING, bin_a, (unsigned long)ECC_MAXSIZE); + LTC_SET_ASN1(seq_curve, 1, LTC_ASN1_OCTET_STRING, bin_b, (unsigned long)ECC_MAXSIZE); + LTC_SET_ASN1(seq_curve, 2, LTC_ASN1_RAW_BIT_STRING, bin_seed, (unsigned long)8*128); seq_curve[2].optional = 1; /* try to load public key */ len_xy = sizeof(bin_xy); @@ -70,23 +72,23 @@ len_b = seq_curve[1].size; len_g = seq_ecparams[3].size; /* create bignums */ - if ((err = mp_read_unsigned_bin(a, bin_a, len_a)) != CRYPT_OK) { goto error; } - if ((err = mp_read_unsigned_bin(b, bin_b, len_b)) != CRYPT_OK) { goto error; } - if ((err = ltc_ecc_import_point(bin_g, len_g, prime, a, b, gx, gy)) != CRYPT_OK) { goto error; } + if ((err = mp_read_unsigned_bin(a, bin_a, len_a)) != CRYPT_OK) { goto error; } + if ((err = mp_read_unsigned_bin(b, bin_b, len_b)) != CRYPT_OK) { goto error; } + if ((err = ltc_ecc_import_point(bin_g, len_g, prime, a, b, gx, gy)) != CRYPT_OK) { goto error; } /* load curve parameters */ if ((err = ecc_set_dp_from_mpis(a, b, prime, order, gx, gy, cofactor, key)) != CRYPT_OK) { goto error; } /* load public key */ - if ((err = ecc_set_key(bin_xy, len_xy, PK_PUBLIC, key)) != CRYPT_OK) { goto error; } + if ((err = ecc_set_key(bin_xy, len_xy, PK_PUBLIC, key)) != CRYPT_OK) { goto error; } goto success; } /* ### 3. try to load private key - no curve parameters just curve OID */ /* ECPrivateKey SEQUENCE */ - LTC_SET_ASN1(custom, 0, LTC_ASN1_OBJECT_IDENTIFIER, curveoid, 16UL); - LTC_SET_ASN1(custom, 1, LTC_ASN1_RAW_BIT_STRING, bin_xy, (unsigned long)8*(2*ECC_MAXSIZE+2)); - LTC_SET_ASN1(seq_priv, 0, LTC_ASN1_SHORT_INTEGER, &pkver, 1UL); - LTC_SET_ASN1(seq_priv, 1, LTC_ASN1_OCTET_STRING, bin_k, (unsigned long)ECC_MAXSIZE); + LTC_SET_ASN1(custom, 0, LTC_ASN1_OBJECT_IDENTIFIER, curveoid, 16UL); + LTC_SET_ASN1(custom, 1, LTC_ASN1_RAW_BIT_STRING, bin_xy, (unsigned long)8*(2*ECC_MAXSIZE+2)); + LTC_SET_ASN1(seq_priv, 0, LTC_ASN1_SHORT_INTEGER, &pkver, 1UL); + LTC_SET_ASN1(seq_priv, 1, LTC_ASN1_OCTET_STRING, bin_k, (unsigned long)ECC_MAXSIZE); LTC_SET_ASN1_CUSTOM_CONSTRUCTED(seq_priv, 2, LTC_ASN1_CL_CONTEXT_SPECIFIC, 0, custom); /* context specific 0 */ LTC_SET_ASN1_CUSTOM_CONSTRUCTED(seq_priv, 3, LTC_ASN1_CL_CONTEXT_SPECIFIC, 1, custom + 1); /* context specific 1 */ @@ -121,12 +123,12 @@ LTC_SET_ASN1(seq_ecparams, 5, LTC_ASN1_SHORT_INTEGER, &cofactor, 1UL); seq_ecparams[5].optional = 1; /* FieldID SEQUENCE */ - LTC_SET_ASN1(seq_fieldid, 0, LTC_ASN1_OBJECT_IDENTIFIER, tmpoid, 16UL); - LTC_SET_ASN1(seq_fieldid, 1, LTC_ASN1_INTEGER, prime, 1UL); + LTC_SET_ASN1(seq_fieldid, 0, LTC_ASN1_OBJECT_IDENTIFIER, tmpoid, 16UL); + LTC_SET_ASN1(seq_fieldid, 1, LTC_ASN1_INTEGER, prime, 1UL); /* Curve SEQUENCE */ - LTC_SET_ASN1(seq_curve, 0, LTC_ASN1_OCTET_STRING, bin_a, (unsigned long)ECC_MAXSIZE); - LTC_SET_ASN1(seq_curve, 1, LTC_ASN1_OCTET_STRING, bin_b, (unsigned long)ECC_MAXSIZE); - LTC_SET_ASN1(seq_curve, 2, LTC_ASN1_RAW_BIT_STRING, bin_seed, (unsigned long)8*128); + LTC_SET_ASN1(seq_curve, 0, LTC_ASN1_OCTET_STRING, bin_a, (unsigned long)ECC_MAXSIZE); + LTC_SET_ASN1(seq_curve, 1, LTC_ASN1_OCTET_STRING, bin_b, (unsigned long)ECC_MAXSIZE); + LTC_SET_ASN1(seq_curve, 2, LTC_ASN1_RAW_BIT_STRING, bin_seed, (unsigned long)8*128); seq_curve[2].optional = 1; /* try to load private key */ err = der_decode_sequence(in, inlen, seq_priv, 4); @@ -137,19 +139,19 @@ len_b = seq_curve[1].size; len_g = seq_ecparams[3].size; /* create bignums */ - if ((err = mp_read_unsigned_bin(a, bin_a, len_a)) != CRYPT_OK) { goto error; } - if ((err = mp_read_unsigned_bin(b, bin_b, len_b)) != CRYPT_OK) { goto error; } - if ((err = ltc_ecc_import_point(bin_g, len_g, prime, a, b, gx, gy)) != CRYPT_OK) { goto error; } + if ((err = mp_read_unsigned_bin(a, bin_a, len_a)) != CRYPT_OK) { goto error; } + if ((err = mp_read_unsigned_bin(b, bin_b, len_b)) != CRYPT_OK) { goto error; } + if ((err = ltc_ecc_import_point(bin_g, len_g, prime, a, b, gx, gy)) != CRYPT_OK) { goto error; } /* load curve parameters */ if ((err = ecc_set_dp_from_mpis(a, b, prime, order, gx, gy, cofactor, key)) != CRYPT_OK) { goto error; } /* load private+public key */ - if ((err = ecc_set_key(bin_k, len_k, PK_PRIVATE, key)) != CRYPT_OK) { goto error; } + if ((err = ecc_set_key(bin_k, len_k, PK_PRIVATE, key)) != CRYPT_OK) { goto error; } goto success; } /* ### 5. backward compatibility - try to load old-DER format */ - if ((err = ecc_import(in, inlen, key)) != CRYPT_OK) { goto error; } + if ((err = ecc_import(in, inlen, key)) != CRYPT_OK) { goto error; } success: err = CRYPT_OK; diff --git a/src/ltc/pk/ecc/ecc_shared_secret.c b/src/ltc/pk/ecc/ecc_shared_secret.c index 92917cc..afb8691 100644 --- a/src/ltc/pk/ecc/ecc_shared_secret.c +++ b/src/ltc/pk/ecc/ecc_shared_secret.c @@ -24,7 +24,7 @@ @param outlen [in/out] The max size and resulting size of the shared secret @return CRYPT_OK if successful */ -int ecc_shared_secret(ecc_key *private_key, ecc_key *public_key, +int ecc_shared_secret(const ecc_key *private_key, const ecc_key *public_key, unsigned char *out, unsigned long *outlen) { unsigned long x; diff --git a/src/ltc/pk/ecc/ecc_sign_hash.c b/src/ltc/pk/ecc/ecc_sign_hash.c index f17e96c..027ad7a 100644 --- a/src/ltc/pk/ecc/ecc_sign_hash.c +++ b/src/ltc/pk/ecc/ecc_sign_hash.c @@ -18,7 +18,7 @@ static 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, int sigformat) + prng_state *prng, int wprng, const ecc_key *key, int sigformat) { ecc_key pubkey; void *r, *s, *e, *p; @@ -132,7 +132,7 @@ */ 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) + prng_state *prng, int wprng, const ecc_key *key) { return _ecc_sign_hash(in, inlen, out, outlen, prng, wprng, key, 0); } @@ -150,7 +150,7 @@ */ 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) + prng_state *prng, int wprng, const ecc_key *key) { return _ecc_sign_hash(in, inlen, out, outlen, prng, wprng, key, 1); } diff --git a/src/ltc/pk/ecc/ecc_verify_hash.c b/src/ltc/pk/ecc/ecc_verify_hash.c index 3dd2abb..5127953 100644 --- a/src/ltc/pk/ecc/ecc_verify_hash.c +++ b/src/ltc/pk/ecc/ecc_verify_hash.c @@ -18,7 +18,7 @@ static int _ecc_verify_hash(const unsigned char *sig, unsigned long siglen, const unsigned char *hash, unsigned long hashlen, - int *stat, ecc_key *key, int sigformat) + int *stat, const ecc_key *key, int sigformat) { ecc_point *mG = NULL, *mQ = NULL; void *r, *s, *v, *w, *u1, *u2, *e, *p, *m, *a, *a_plus3 = NULL, *mu = NULL, *ma = NULL; @@ -37,7 +37,7 @@ /* allocate ints */ if ((err = mp_init_multi(&r, &s, &v, &w, &u1, &u2, &e, &a_plus3, NULL)) != CRYPT_OK) { - return CRYPT_MEM; + return err; } p = key->dp.order; @@ -171,7 +171,7 @@ */ int ecc_verify_hash(const unsigned char *sig, unsigned long siglen, const unsigned char *hash, unsigned long hashlen, - int *stat, ecc_key *key) + int *stat, const ecc_key *key) { return _ecc_verify_hash(sig, siglen, hash, hashlen, stat, key, 0); } @@ -188,7 +188,7 @@ */ int ecc_verify_hash_rfc7518(const unsigned char *sig, unsigned long siglen, const unsigned char *hash, unsigned long hashlen, - int *stat, ecc_key *key) + int *stat, const ecc_key *key) { return _ecc_verify_hash(sig, siglen, hash, hashlen, stat, key, 1); } diff --git a/src/ltc/pk/ecc/ltc_ecc_verify_key.c b/src/ltc/pk/ecc/ltc_ecc_verify_key.c index b04df5d..3d76341 100644 --- a/src/ltc/pk/ecc/ltc_ecc_verify_key.c +++ b/src/ltc/pk/ecc/ltc_ecc_verify_key.c @@ -19,17 +19,13 @@ @return CRYPT_OK if successful */ -int ltc_ecc_verify_key(ecc_key *key) +int ltc_ecc_verify_key(const ecc_key *key) { int err, inf; - void *prime = NULL; - void *order = NULL; - void *a = NULL; ecc_point *point; - - prime = key->dp.prime; - order = key->dp.order; - a = key->dp.A; + void *prime = key->dp.prime; + void *order = key->dp.order; + void *a = key->dp.A; /* Test 1: Are the x and y points of the public key in the field? */ if (ltc_mp.compare_d(key->pubkey.z, 1) == LTC_MP_EQ) {