Skip to content

Commit 49ed020

Browse files
committed
Refactor some of the ECC import internals
Signed-off-by: Steffen Jaeckel <[email protected]>
1 parent e707197 commit 49ed020

File tree

3 files changed

+65
-103
lines changed

3 files changed

+65
-103
lines changed

src/headers/tomcrypt_private.h

+2
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,8 @@ int ecc_copy_curve(const ecc_key *srckey, ecc_key *key);
382382
int ecc_set_curve_by_size(int size, ecc_key *key);
383383
int ecc_import_subject_public_key_info(const unsigned char *in, unsigned long inlen, ecc_key *key);
384384
int ecc_import_pkcs8_asn1(ltc_asn1_list *alg_id, ltc_asn1_list *priv_key, ecc_key *key);
385+
int ecc_import_with_curve(const unsigned char *in, unsigned long inlen, int type, ecc_key *key);
386+
int ecc_import_with_oid(const unsigned char *in, unsigned long inlen, unsigned long *oid, unsigned long oid_len, int type, ecc_key *key);
385387

386388
#ifdef LTC_SSH
387389
int ecc_ssh_ecdsa_encode_name(char *buffer, unsigned long *buflen, const ecc_key *key);

src/pk/ecc/ecc_import_openssl.c

+59-37
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ static int s_ecc_import_private_with_oid(const unsigned char *in, unsigned long
99
{
1010
ltc_asn1_list seq_priv[4], custom[2];
1111
unsigned char bin_xy[2*ECC_MAXSIZE+2], bin_k[ECC_MAXSIZE];
12-
unsigned long len, pkver = 0, curveoid[16];
13-
char OID[256];
14-
const ltc_ecc_curve *curve;
12+
unsigned long pkver = 0, curveoid[16];
1513
int err;
1614

1715
/* ### try to load private key - no curve parameters just curve OID */
@@ -26,50 +24,44 @@ static int s_ecc_import_private_with_oid(const unsigned char *in, unsigned long
2624

2725
/* try to load private key */
2826
err = der_decode_sequence(in, inlen, seq_priv, 4);
29-
if (err == CRYPT_OK) {
30-
/* load curve parameters for given curve OID */
31-
len = sizeof(OID);
32-
if ((err = pk_oid_num_to_str(curveoid, custom[0].size, OID, &len)) != CRYPT_OK) { goto error; }
33-
if ((err = ecc_find_curve(OID, &curve)) != CRYPT_OK) { goto error; }
34-
if ((err = ecc_set_curve(curve, key)) != CRYPT_OK) { goto error; }
35-
/* load private+public key */
36-
err = ecc_set_key(bin_k, seq_priv[1].size, PK_PRIVATE, key);
37-
}
27+
if (err != CRYPT_OK) { goto error; }
28+
err = ecc_import_with_oid(bin_k, seq_priv[1].size, curveoid, custom[0].size, PK_PRIVATE, key);
3829
error:
3930
return err;
4031
}
4132

42-
static int s_ecc_import_private_with_curve(const unsigned char *in, unsigned long inlen, ecc_key *key)
33+
int ecc_import_with_oid(const unsigned char *in, unsigned long inlen, unsigned long *oid, unsigned long oid_len, int type, ecc_key *key)
34+
{
35+
char OID[256];
36+
unsigned long len;
37+
const ltc_ecc_curve *curve;
38+
int err;
39+
40+
/* load curve parameters for given curve OID */
41+
len = sizeof(OID);
42+
if ((err = pk_oid_num_to_str(oid, oid_len, OID, &len)) != CRYPT_OK) { goto error; }
43+
if ((err = ecc_find_curve(OID, &curve)) != CRYPT_OK) { goto error; }
44+
if ((err = ecc_set_curve(curve, key)) != CRYPT_OK) { goto error; }
45+
/* load public key */
46+
err = ecc_set_key(in, inlen, type, key);
47+
error:
48+
return err;
49+
}
50+
51+
int ecc_import_with_curve(const unsigned char *in, unsigned long inlen, int type, ecc_key *key)
4352
{
4453
void *prime, *order, *a, *b, *gx, *gy;
4554
ltc_asn1_list seq_fieldid[2], seq_curve[3], seq_ecparams[6], seq_priv[4], custom[2];
4655
unsigned char bin_a[ECC_MAXSIZE], bin_b[ECC_MAXSIZE], bin_k[ECC_MAXSIZE];
4756
unsigned char bin_g[2*ECC_MAXSIZE+1], bin_xy[2*ECC_MAXSIZE+2], bin_seed[128];
48-
unsigned long len_a, len_b, len_k, len_g;
57+
unsigned long len_a, len_b, len_k, len_g, len_xy, len;
4958
unsigned long cofactor = 0, ecver = 0, pkver = 0, tmpoid[16];
5059
int err;
5160

5261
if ((err = mp_init_multi(&prime, &order, &a, &b, &gx, &gy, LTC_NULL)) != CRYPT_OK) {
5362
return err;
5463
}
5564

56-
/* ### try to load private key - curve parameters included */
57-
58-
/* ECPrivateKey SEQUENCE */
59-
LTC_SET_ASN1(custom, 0, LTC_ASN1_SEQUENCE, seq_ecparams, 6UL);
60-
LTC_SET_ASN1(custom, 1, LTC_ASN1_RAW_BIT_STRING, bin_xy, 8UL*sizeof(bin_xy));
61-
LTC_SET_ASN1(seq_priv, 0, LTC_ASN1_SHORT_INTEGER, &pkver, 1UL);
62-
LTC_SET_ASN1(seq_priv, 1, LTC_ASN1_OCTET_STRING, bin_k, sizeof(bin_k));
63-
LTC_SET_ASN1_CUSTOM_CONSTRUCTED(seq_priv, 2, LTC_ASN1_CL_CONTEXT_SPECIFIC, 0, custom); /* context specific 0 */
64-
LTC_SET_ASN1_CUSTOM_CONSTRUCTED(seq_priv, 3, LTC_ASN1_CL_CONTEXT_SPECIFIC, 1, custom + 1); /* context specific 1 */
65-
/* ECParameters SEQUENCE */
66-
LTC_SET_ASN1(seq_ecparams, 0, LTC_ASN1_SHORT_INTEGER, &ecver, 1UL);
67-
LTC_SET_ASN1(seq_ecparams, 1, LTC_ASN1_SEQUENCE, seq_fieldid, 2UL);
68-
LTC_SET_ASN1(seq_ecparams, 2, LTC_ASN1_SEQUENCE, seq_curve, 3UL);
69-
LTC_SET_ASN1(seq_ecparams, 3, LTC_ASN1_OCTET_STRING, bin_g, sizeof(bin_g));
70-
LTC_SET_ASN1(seq_ecparams, 4, LTC_ASN1_INTEGER, order, 1UL);
71-
LTC_SET_ASN1(seq_ecparams, 5, LTC_ASN1_SHORT_INTEGER, &cofactor, 1UL);
72-
seq_ecparams[5].optional = 1;
7365
/* FieldID SEQUENCE */
7466
LTC_SET_ASN1(seq_fieldid, 0, LTC_ASN1_OBJECT_IDENTIFIER, tmpoid, 16UL);
7567
LTC_SET_ASN1(seq_fieldid, 1, LTC_ASN1_INTEGER, prime, 1UL);
@@ -78,10 +70,33 @@ static int s_ecc_import_private_with_curve(const unsigned char *in, unsigned lon
7870
LTC_SET_ASN1(seq_curve, 1, LTC_ASN1_OCTET_STRING, bin_b, sizeof(bin_b));
7971
LTC_SET_ASN1(seq_curve, 2, LTC_ASN1_RAW_BIT_STRING, bin_seed, 8UL*sizeof(bin_seed));
8072
seq_curve[2].optional = 1;
81-
/* try to load private key */
82-
err = der_decode_sequence(in, inlen, seq_priv, 4);
73+
/* ECParameters SEQUENCE */
74+
LTC_SET_ASN1(seq_ecparams, 0, LTC_ASN1_SHORT_INTEGER, &ecver, 1UL);
75+
LTC_SET_ASN1(seq_ecparams, 1, LTC_ASN1_SEQUENCE, seq_fieldid, 2UL);
76+
LTC_SET_ASN1(seq_ecparams, 2, LTC_ASN1_SEQUENCE, seq_curve, 3UL);
77+
LTC_SET_ASN1(seq_ecparams, 3, LTC_ASN1_OCTET_STRING, bin_g, sizeof(bin_g));
78+
LTC_SET_ASN1(seq_ecparams, 4, LTC_ASN1_INTEGER, order, 1UL);
79+
LTC_SET_ASN1(seq_ecparams, 5, LTC_ASN1_SHORT_INTEGER, &cofactor, 1UL);
80+
seq_ecparams[5].optional = 1;
81+
if (type == PK_PRIVATE) {
82+
/* ECPrivateKey SEQUENCE */
83+
LTC_SET_ASN1(custom, 0, LTC_ASN1_SEQUENCE, seq_ecparams, 6UL);
84+
LTC_SET_ASN1(custom, 1, LTC_ASN1_RAW_BIT_STRING, bin_xy, 8UL*sizeof(bin_xy));
85+
LTC_SET_ASN1(seq_priv, 0, LTC_ASN1_SHORT_INTEGER, &pkver, 1UL);
86+
LTC_SET_ASN1(seq_priv, 1, LTC_ASN1_OCTET_STRING, bin_k, sizeof(bin_k));
87+
LTC_SET_ASN1_CUSTOM_CONSTRUCTED(seq_priv, 2, LTC_ASN1_CL_CONTEXT_SPECIFIC, 0, custom); /* context specific 0 */
88+
LTC_SET_ASN1_CUSTOM_CONSTRUCTED(seq_priv, 3, LTC_ASN1_CL_CONTEXT_SPECIFIC, 1, custom + 1); /* context specific 1 */
89+
/* try to load private key */
90+
err = der_decode_sequence(in, inlen, seq_priv, 4);
91+
} else if (type == PK_PUBLIC) {
92+
/* try to load public key */
93+
len_xy = sizeof(bin_xy);
94+
len = 6;
95+
err = x509_decode_subject_public_key_info(in, inlen, LTC_OID_EC, bin_xy, &len_xy, LTC_ASN1_SEQUENCE, seq_ecparams, &len);
96+
} else {
97+
err = CRYPT_PK_INVALID_TYPE;
98+
}
8399
if (err == CRYPT_OK) {
84-
len_k = seq_priv[1].size;
85100
len_a = seq_curve[0].size;
86101
len_b = seq_curve[1].size;
87102
len_g = seq_ecparams[3].size;
@@ -91,8 +106,15 @@ static int s_ecc_import_private_with_curve(const unsigned char *in, unsigned lon
91106
if ((err = ltc_ecc_import_point(bin_g, len_g, prime, a, b, gx, gy)) != CRYPT_OK) { goto error; }
92107
/* load curve parameters */
93108
if ((err = ecc_set_curve_from_mpis(a, b, prime, order, gx, gy, cofactor, key)) != CRYPT_OK) { goto error; }
94-
/* load private+public key */
95-
err = ecc_set_key(bin_k, len_k, PK_PRIVATE, key);
109+
110+
if (type == PK_PRIVATE) {
111+
len_k = seq_priv[1].size;
112+
/* load private+public key */
113+
err = ecc_set_key(bin_k, len_k, PK_PRIVATE, key);
114+
} else {
115+
/* load public key */
116+
err = ecc_set_key(bin_xy, len_xy, PK_PUBLIC, key);
117+
}
96118
}
97119
error:
98120
mp_clear_multi(prime, order, a, b, gx, gy, LTC_NULL);
@@ -111,7 +133,7 @@ int ecc_import_openssl(const unsigned char *in, unsigned long inlen, ecc_key *ke
111133
goto success;
112134
}
113135

114-
err = s_ecc_import_private_with_curve(in, inlen, key);
136+
err = ecc_import_with_curve(in, inlen, PK_PRIVATE, key);
115137

116138
success:
117139
return err;

src/pk/ecc/ecc_import_x509.c

+4-66
Original file line numberDiff line numberDiff line change
@@ -8,78 +8,16 @@ static int s_ecc_import_x509_with_oid(const unsigned char *in, unsigned long inl
88
{
99
unsigned char bin_xy[2*ECC_MAXSIZE+2];
1010
unsigned long curveoid[16];
11-
unsigned long len_xy, len_oid, len;
12-
char OID[256];
13-
const ltc_ecc_curve *curve;
11+
unsigned long len_xy, len_oid;
1412
int err;
1513

1614
len_xy = sizeof(bin_xy);
1715
len_oid = 16;
1816
err = x509_decode_subject_public_key_info(in, inlen, LTC_OID_EC, bin_xy, &len_xy,
1917
LTC_ASN1_OBJECT_IDENTIFIER, (void *)curveoid, &len_oid);
20-
if (err == CRYPT_OK) {
21-
/* load curve parameters for given curve OID */
22-
len = sizeof(OID);
23-
if ((err = pk_oid_num_to_str(curveoid, len_oid, OID, &len)) != CRYPT_OK) { goto error; }
24-
if ((err = ecc_find_curve(OID, &curve)) != CRYPT_OK) { goto error; }
25-
if ((err = ecc_set_curve(curve, key)) != CRYPT_OK) { goto error; }
26-
/* load public key */
27-
err = ecc_set_key(bin_xy, len_xy, PK_PUBLIC, key);
28-
}
29-
error:
30-
return err;
31-
}
32-
33-
static int s_ecc_import_x509_with_curve(const unsigned char *in, unsigned long inlen, ecc_key *key)
34-
{
35-
void *prime, *order, *a, *b, *gx, *gy;
36-
ltc_asn1_list seq_fieldid[2], seq_curve[3], seq_ecparams[6];
37-
unsigned char bin_a[ECC_MAXSIZE], bin_b[ECC_MAXSIZE];
38-
unsigned char bin_g[2*ECC_MAXSIZE+1], bin_xy[2*ECC_MAXSIZE+2], bin_seed[128];
39-
unsigned long len_a, len_b, len_g, len_xy, len;
40-
unsigned long cofactor = 0, ecver = 0, tmpoid[16];
41-
int err;
42-
43-
if ((err = mp_init_multi(&prime, &order, &a, &b, &gx, &gy, LTC_NULL)) != CRYPT_OK) {
44-
return err;
45-
}
46-
47-
/* ECParameters SEQUENCE */
48-
LTC_SET_ASN1(seq_ecparams, 0, LTC_ASN1_SHORT_INTEGER, &ecver, 1UL);
49-
LTC_SET_ASN1(seq_ecparams, 1, LTC_ASN1_SEQUENCE, seq_fieldid, 2UL);
50-
LTC_SET_ASN1(seq_ecparams, 2, LTC_ASN1_SEQUENCE, seq_curve, 3UL);
51-
LTC_SET_ASN1(seq_ecparams, 3, LTC_ASN1_OCTET_STRING, bin_g, sizeof(bin_g));
52-
LTC_SET_ASN1(seq_ecparams, 4, LTC_ASN1_INTEGER, order, 1UL);
53-
LTC_SET_ASN1(seq_ecparams, 5, LTC_ASN1_SHORT_INTEGER, &cofactor, 1UL);
54-
seq_ecparams[5].optional = 1;
55-
/* FieldID SEQUENCE */
56-
LTC_SET_ASN1(seq_fieldid, 0, LTC_ASN1_OBJECT_IDENTIFIER, tmpoid, 16UL);
57-
LTC_SET_ASN1(seq_fieldid, 1, LTC_ASN1_INTEGER, prime, 1UL);
58-
/* Curve SEQUENCE */
59-
LTC_SET_ASN1(seq_curve, 0, LTC_ASN1_OCTET_STRING, bin_a, sizeof(bin_a));
60-
LTC_SET_ASN1(seq_curve, 1, LTC_ASN1_OCTET_STRING, bin_b, sizeof(bin_b));
61-
LTC_SET_ASN1(seq_curve, 2, LTC_ASN1_RAW_BIT_STRING, bin_seed, 8u*sizeof(bin_seed));
62-
seq_curve[2].optional = 1;
63-
/* try to load public key */
64-
len_xy = sizeof(bin_xy);
65-
len = 6;
66-
err = x509_decode_subject_public_key_info(in, inlen, LTC_OID_EC, bin_xy, &len_xy, LTC_ASN1_SEQUENCE, seq_ecparams, &len);
67-
68-
if (err == CRYPT_OK) {
69-
len_a = seq_curve[0].size;
70-
len_b = seq_curve[1].size;
71-
len_g = seq_ecparams[3].size;
72-
/* create bignums */
73-
if ((err = mp_read_unsigned_bin(a, bin_a, len_a)) != CRYPT_OK) { goto error; }
74-
if ((err = mp_read_unsigned_bin(b, bin_b, len_b)) != CRYPT_OK) { goto error; }
75-
if ((err = ltc_ecc_import_point(bin_g, len_g, prime, a, b, gx, gy)) != CRYPT_OK) { goto error; }
76-
/* load curve parameters */
77-
if ((err = ecc_set_curve_from_mpis(a, b, prime, order, gx, gy, cofactor, key)) != CRYPT_OK) { goto error; }
78-
/* load public key */
79-
err = ecc_set_key(bin_xy, len_xy, PK_PUBLIC, key);
80-
}
18+
if (err != CRYPT_OK) { goto error; }
19+
err = ecc_import_with_oid(bin_xy, len_xy, curveoid, len_oid, PK_PUBLIC, key);
8120
error:
82-
mp_clear_multi(prime, order, a, b, gx, gy, LTC_NULL);
8321
return err;
8422
}
8523

@@ -91,7 +29,7 @@ int ecc_import_subject_public_key_info(const unsigned char *in, unsigned long in
9129
goto success;
9230
}
9331

94-
err = s_ecc_import_x509_with_curve(in, inlen, key);
32+
err = ecc_import_with_curve(in, inlen, PK_PUBLIC, key);
9533

9634
success:
9735
return err;

0 commit comments

Comments
 (0)