summaryrefslogtreecommitdiff
path: root/src/ltc/pk/ecc/ecc_import.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/ltc/pk/ecc/ecc_import.c')
-rw-r--r--src/ltc/pk/ecc/ecc_import.c42
1 files changed, 14 insertions, 28 deletions
diff --git a/src/ltc/pk/ecc/ecc_import.c b/src/ltc/pk/ecc/ecc_import.c
index 38465b12..3a1dcc57 100644
--- a/src/ltc/pk/ecc/ecc_import.c
+++ b/src/ltc/pk/ecc/ecc_import.c
@@ -7,9 +7,6 @@
* guarantee it works.
*/
-/* Implements ECC over Z/pZ for curve y^2 = x^3 + a*x + b
- *
- */
#include "tomcrypt.h"
/**
@@ -49,18 +46,20 @@ int ecc_import_ex(const unsigned char *in, unsigned long inlen, ecc_key *key, co
LTC_ARGCHK(key != NULL);
LTC_ARGCHK(ltc_mp.name != NULL);
- /* init key */
- if (mp_init_multi(&key->pubkey.x, &key->pubkey.y, &key->pubkey.z, &key->k, NULL) != CRYPT_OK) {
- return CRYPT_MEM;
- }
-
/* find out what type of key it is */
- err = der_decode_sequence_multi(in, inlen, LTC_ASN1_BIT_STRING, 1UL, flags,
- LTC_ASN1_EOL, 0UL, NULL);
+ err = der_decode_sequence_multi(in, inlen, LTC_ASN1_BIT_STRING, 1UL, flags,
+ LTC_ASN1_SHORT_INTEGER, 1UL, &key_size,
+ LTC_ASN1_EOL, 0UL, NULL);
if (err != CRYPT_OK && err != CRYPT_INPUT_TOO_LONG) {
- goto done;
+ return err;
}
+ /* allocate & initialize the key */
+ if (dp == NULL) {
+ if ((err = ecc_set_dp_size(key_size, key)) != CRYPT_OK) { goto done; }
+ } else {
+ if ((err = ecc_set_dp(dp, key)) != CRYPT_OK) { goto done; }
+ }
if (flags[0] == 1) {
/* private key */
@@ -91,30 +90,17 @@ int ecc_import_ex(const unsigned char *in, unsigned long inlen, ecc_key *key, co
goto done;
}
- if (dp == NULL) {
- /* find the idx */
- for (key->idx = 0; ltc_ecc_sets[key->idx].size && (unsigned long)ltc_ecc_sets[key->idx].size != key_size; ++key->idx);
- if (ltc_ecc_sets[key->idx].size == 0) {
- err = CRYPT_INVALID_PACKET;
- goto done;
- }
- key->dp = &ltc_ecc_sets[key->idx];
- } else {
- key->idx = -1;
- key->dp = dp;
- }
/* set z */
if ((err = mp_set(key->pubkey.z, 1)) != CRYPT_OK) { goto done; }
- /* is it a point on the curve? */
- if ((err = ltc_ecc_is_point(key->dp, key->pubkey.x, key->pubkey.y)) != CRYPT_OK) {
- goto done;
- }
+ /* point on the curve + other checks */
+ if ((err = ltc_ecc_verify_key(key)) != CRYPT_OK) { goto done; }
/* we're good */
return CRYPT_OK;
+
done:
- mp_clear_multi(key->pubkey.x, key->pubkey.y, key->pubkey.z, key->k, NULL);
+ ecc_free(key);
return err;
}
#endif