Skip to content

Commit 05d397d

Browse files
karel-msjaeckel
authored andcommitted
ECC improved import/export
1 parent abedfa1 commit 05d397d

File tree

6 files changed

+795
-0
lines changed

6 files changed

+795
-0
lines changed

src/headers/tomcrypt_pk.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,10 @@ int ecc_ansi_x963_export(const ecc_key *key, unsigned char *out, unsigned long *
270270
int ecc_ansi_x963_import(const unsigned char *in, unsigned long inlen, ecc_key *key);
271271
int ecc_ansi_x963_import_ex(const unsigned char *in, unsigned long inlen, ecc_key *key, const ltc_ecc_curve *cu);
272272

273+
int ecc_export_openssl(unsigned char *out, unsigned long *outlen, int type, const ecc_key *key);
274+
int ecc_import_openssl(const unsigned char *in, unsigned long inlen, ecc_key *key);
275+
int ecc_import_x509(const unsigned char *in, unsigned long inlen, ecc_key *key);
276+
273277
int ecc_shared_secret(const ecc_key *private_key, const ecc_key *public_key,
274278
unsigned char *out, unsigned long *outlen);
275279

src/misc/pk_get_oid.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ static const oid_st dsa_oid = {
1919
6,
2020
};
2121

22+
static const oid_st ec_oid = {
23+
{ 1, 2, 840, 10045, 2, 1 },
24+
6,
25+
};
26+
27+
static const oid_st ec_primef = {
28+
{ 1, 2, 840, 10045, 1, 1 },
29+
6,
30+
};
31+
2232
/*
2333
Returns the OID of the public key algorithm.
2434
@return CRYPT_OK if valid
@@ -32,6 +42,12 @@ int pk_get_oid(int pk, oid_st *st)
3242
case PKA_DSA:
3343
XMEMCPY(st, &dsa_oid, sizeof(*st));
3444
break;
45+
case PKA_EC:
46+
XMEMCPY(st, &ec_oid, sizeof(*st));
47+
break;
48+
case PKA_EC_PRIMEF:
49+
XMEMCPY(st, &ec_primef, sizeof(*st));
50+
break;
3551
default:
3652
return CRYPT_INVALID_ARG;
3753
}

src/pk/ecc/ecc_export_openssl.c

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
2+
*
3+
* LibTomCrypt is a library that provides various cryptographic
4+
* algorithms in a highly modular and flexible manner.
5+
*
6+
* The library is free for all purposes without any express
7+
* guarantee it works.
8+
*/
9+
10+
#include "tomcrypt_private.h"
11+
12+
#ifdef LTC_MECC
13+
14+
/**
15+
Export an ECC key as a binary packet
16+
@param out [out] Destination for the key
17+
@param outlen [in/out] Max size and resulting size of the exported key
18+
@param type The type of key you want to export (PK_PRIVATE or PK_PUBLIC)
19+
@param key The key to export
20+
@return CRYPT_OK if successful
21+
*/
22+
23+
int ecc_export_openssl(unsigned char *out, unsigned long *outlen, int type, const ecc_key *key)
24+
{
25+
int err;
26+
void *prime, *order, *a, *b, *gx, *gy;
27+
unsigned char bin_a[256], bin_b[256], bin_k[256], bin_g[512], bin_xy[512];
28+
unsigned long len_a, len_b, len_k, len_g, len_xy;
29+
unsigned long cofactor, one = 1;
30+
oid_st oid;
31+
ltc_asn1_list seq_fieldid[2], seq_curve[2], seq_ecparams[6], seq_priv[4], pub_xy, ecparams;
32+
int flag_oid = type & PK_CURVEOID ? 1 : 0;
33+
int flag_com = type & PK_COMPRESSED ? 1 : 0;
34+
int flag_pri = type & PK_PRIVATE ? 1 : 0;
35+
36+
LTC_ARGCHK(out != NULL);
37+
LTC_ARGCHK(outlen != NULL);
38+
LTC_ARGCHK(key != NULL);
39+
40+
if (key->type != PK_PRIVATE && flag_pri) return CRYPT_PK_TYPE_MISMATCH;
41+
42+
prime = key->dp.prime;
43+
order = key->dp.order;
44+
b = key->dp.B;
45+
a = key->dp.A;
46+
gx = key->dp.base.x;
47+
gy = key->dp.base.y;
48+
49+
/* curve param a */
50+
len_a = mp_unsigned_bin_size(a);
51+
if (len_a > sizeof(bin_a)) { err = CRYPT_BUFFER_OVERFLOW; goto error; }
52+
if ((err = mp_to_unsigned_bin(a, bin_a)) != CRYPT_OK) { goto error; }
53+
if (len_a == 0) { len_a = 1; bin_a[0] = 0; } /* handle case a == 0 */
54+
55+
/* curve param b */
56+
len_b = mp_unsigned_bin_size(b);
57+
if (len_b > sizeof(bin_b)) { err = CRYPT_BUFFER_OVERFLOW; goto error; }
58+
if ((err = mp_to_unsigned_bin(b, bin_b)) != CRYPT_OK) { goto error; }
59+
if (len_b == 0) { len_b = 1; bin_b[0] = 0; } /* handle case b == 0 */
60+
61+
/* base point - (un)compressed based on flag_com */
62+
len_g = sizeof(bin_g);
63+
err = ltc_ecc_export_point(bin_g, &len_g, gx, gy, key->dp.size, flag_com);
64+
if (err != CRYPT_OK) { goto error; }
65+
66+
/* public key - (un)compressed based on flag_com */
67+
len_xy = sizeof(bin_xy);
68+
err = ltc_ecc_export_point(bin_xy, &len_xy, key->pubkey.x, key->pubkey.y, key->dp.size, flag_com);
69+
if (err != CRYPT_OK) { goto error; }
70+
71+
/* co-factor */
72+
cofactor = key->dp.cofactor;
73+
74+
/* we support only prime-field EC */
75+
if ((err = pk_get_oid(PKA_EC_PRIMEF, &oid)) != CRYPT_OK) { goto error; }
76+
77+
if (flag_oid) {
78+
/* http://tools.ietf.org/html/rfc5912
79+
ECParameters ::= CHOICE {
80+
namedCurve CURVE.&id({NamedCurve}) # OBJECT
81+
}
82+
*/
83+
if (key->dp.oidlen == 0) { err = CRYPT_INVALID_ARG; goto error; }
84+
LTC_SET_ASN1(&ecparams, 0, LTC_ASN1_OBJECT_IDENTIFIER, key->dp.oid, key->dp.oidlen);
85+
}
86+
else {
87+
/* http://tools.ietf.org/html/rfc3279
88+
ECParameters ::= SEQUENCE { # SEQUENCE
89+
version INTEGER { ecpVer1(1) } (ecpVer1) # INTEGER :01
90+
FieldID ::= SEQUENCE { # SEQUENCE
91+
fieldType FIELD-ID.&id({IOSet}), # OBJECT :prime-field
92+
parameters FIELD-ID.&Type({IOSet}{@fieldType}) # INTEGER
93+
}
94+
Curve ::= SEQUENCE { # SEQUENCE
95+
a FieldElement ::= OCTET STRING # OCTET STRING
96+
b FieldElement ::= OCTET STRING # OCTET STRING
97+
seed BIT STRING OPTIONAL
98+
}
99+
base ECPoint ::= OCTET STRING # OCTET STRING
100+
order INTEGER, # INTEGER
101+
cofactor INTEGER OPTIONAL # INTEGER
102+
}
103+
*/
104+
105+
/* FieldID SEQUENCE */
106+
LTC_SET_ASN1(seq_fieldid, 0, LTC_ASN1_OBJECT_IDENTIFIER, oid.OID, oid.OIDlen);
107+
LTC_SET_ASN1(seq_fieldid, 1, LTC_ASN1_INTEGER, prime, 1UL);
108+
109+
/* Curve SEQUENCE */
110+
LTC_SET_ASN1(seq_curve, 0, LTC_ASN1_OCTET_STRING, bin_a, len_a);
111+
LTC_SET_ASN1(seq_curve, 1, LTC_ASN1_OCTET_STRING, bin_b, len_b);
112+
113+
/* ECParameters SEQUENCE */
114+
LTC_SET_ASN1(seq_ecparams, 0, LTC_ASN1_SHORT_INTEGER, &one, 1UL);
115+
LTC_SET_ASN1(seq_ecparams, 1, LTC_ASN1_SEQUENCE, seq_fieldid, 2UL);
116+
LTC_SET_ASN1(seq_ecparams, 2, LTC_ASN1_SEQUENCE, seq_curve, 2UL);
117+
LTC_SET_ASN1(seq_ecparams, 3, LTC_ASN1_OCTET_STRING, bin_g, len_g);
118+
LTC_SET_ASN1(seq_ecparams, 4, LTC_ASN1_INTEGER, order, 1UL);
119+
LTC_SET_ASN1(seq_ecparams, 5, LTC_ASN1_SHORT_INTEGER, &cofactor, 1UL);
120+
121+
/* ECParameters used by ECPrivateKey or SubjectPublicKeyInfo below */
122+
LTC_SET_ASN1(&ecparams, 0, LTC_ASN1_SEQUENCE, seq_ecparams, 6UL);
123+
}
124+
125+
if (flag_pri) {
126+
/* http://tools.ietf.org/html/rfc5915
127+
ECPrivateKey ::= SEQUENCE { # SEQUENCE
128+
version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1) # INTEGER :01
129+
privateKey OCTET STRING, # OCTET STRING
130+
[0] ECParameters # see above
131+
[1] publicKey # BIT STRING
132+
}
133+
*/
134+
135+
/* private key */
136+
len_k = mp_unsigned_bin_size(key->k);
137+
if (len_k > sizeof(bin_k)) { err = CRYPT_BUFFER_OVERFLOW; goto error; }
138+
if ((err = mp_to_unsigned_bin(key->k, bin_k)) != CRYPT_OK) { goto error; }
139+
140+
LTC_SET_ASN1(&pub_xy, 0, LTC_ASN1_RAW_BIT_STRING, bin_xy, 8*len_xy);
141+
LTC_SET_ASN1(seq_priv, 0, LTC_ASN1_SHORT_INTEGER, &one, 1);
142+
LTC_SET_ASN1(seq_priv, 1, LTC_ASN1_OCTET_STRING, bin_k, len_k);
143+
LTC_SET_ASN1_CUSTOM_CONSTRUCTED(seq_priv, 2, LTC_ASN1_CL_CONTEXT_SPECIFIC, 0, &ecparams); /* context specific 0 */
144+
LTC_SET_ASN1_CUSTOM_CONSTRUCTED(seq_priv, 3, LTC_ASN1_CL_CONTEXT_SPECIFIC, 1, &pub_xy); /* context specific 1 */
145+
146+
err = der_encode_sequence(seq_priv, 4, out, outlen);
147+
}
148+
else {
149+
/* http://tools.ietf.org/html/rfc5480
150+
SubjectPublicKeyInfo ::= SEQUENCE { # SEQUENCE
151+
AlgorithmIdentifier ::= SEQUENCE { # SEQUENCE
152+
algorithm OBJECT IDENTIFIER # OBJECT :id-ecPublicKey
153+
ECParameters # see above
154+
}
155+
subjectPublicKey BIT STRING # BIT STRING
156+
}
157+
*/
158+
err = x509_encode_subject_public_key_info( out, outlen, PKA_EC, bin_xy, len_xy,
159+
ecparams.type, ecparams.data, ecparams.size );
160+
}
161+
162+
error:
163+
return err;
164+
}
165+
166+
#endif
167+
168+
/* ref: $Format:%D$ */
169+
/* git commit: $Format:%H$ */
170+
/* commit time: $Format:%ai$ */

src/pk/ecc/ecc_import_openssl.c

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
2+
*
3+
* LibTomCrypt is a library that provides various cryptographic
4+
* algorithms in a highly modular and flexible manner.
5+
*
6+
* The library is free for all purposes without any express
7+
* guarantee it works.
8+
*/
9+
10+
#include "tomcrypt_private.h"
11+
12+
#ifdef LTC_MECC
13+
14+
int ecc_import_openssl(const unsigned char *in, unsigned long inlen, ecc_key *key)
15+
{
16+
void *prime, *order, *a, *b, *gx, *gy;
17+
ltc_asn1_list seq_fieldid[2], seq_curve[3], seq_ecparams[6], seq_priv[4], custom[2];
18+
unsigned char bin_a[ECC_MAXSIZE], bin_b[ECC_MAXSIZE], bin_k[ECC_MAXSIZE];
19+
unsigned char bin_g[2*ECC_MAXSIZE+1], bin_xy[2*ECC_MAXSIZE+2], bin_seed[128];
20+
unsigned long len_a, len_b, len_k, len_g, len_xy, len_oid, len;
21+
unsigned long cofactor = 0, ecver = 0, pkver = 0, tmpoid[16], curveoid[16];
22+
char OID[256];
23+
const ltc_ecc_curve *curve;
24+
int err;
25+
26+
if ((err = mp_init_multi(&prime, &order, &a, &b, &gx, &gy, NULL)) != CRYPT_OK) {
27+
return err;
28+
}
29+
30+
/* ### 1. try to load public key - no curve parameters just curve OID */
31+
32+
len_xy = sizeof(bin_xy);
33+
len_oid = 16;
34+
err = x509_decode_subject_public_key_info(in, inlen, PKA_EC, bin_xy, &len_xy,
35+
LTC_ASN1_OBJECT_IDENTIFIER, (void *)curveoid, &len_oid);
36+
if (err == CRYPT_OK) {
37+
/* load curve parameters for given curve OID */
38+
len = sizeof(OID);
39+
if ((err = pk_oid_num_to_str(curveoid, len_oid, OID, &len)) != CRYPT_OK) { goto error; }
40+
if ((err = ecc_get_curve(OID, &curve)) != CRYPT_OK) { goto error; }
41+
if ((err = ecc_set_dp(curve, key)) != CRYPT_OK) { goto error; }
42+
/* load public key */
43+
if ((err = ecc_set_key(bin_xy, len_xy, PK_PUBLIC, key)) != CRYPT_OK) { goto error; }
44+
goto success;
45+
}
46+
47+
/* ### 2. try to load public key - curve parameters included */
48+
49+
/* ECParameters SEQUENCE */
50+
LTC_SET_ASN1(seq_ecparams, 0, LTC_ASN1_SHORT_INTEGER, &ecver, 1UL);
51+
LTC_SET_ASN1(seq_ecparams, 1, LTC_ASN1_SEQUENCE, seq_fieldid, 2UL);
52+
LTC_SET_ASN1(seq_ecparams, 2, LTC_ASN1_SEQUENCE, seq_curve, 3UL);
53+
LTC_SET_ASN1(seq_ecparams, 3, LTC_ASN1_OCTET_STRING, bin_g, (unsigned long)2*ECC_MAXSIZE+1);
54+
LTC_SET_ASN1(seq_ecparams, 4, LTC_ASN1_INTEGER, order, 1UL);
55+
LTC_SET_ASN1(seq_ecparams, 5, LTC_ASN1_SHORT_INTEGER, &cofactor, 1UL);
56+
seq_ecparams[5].optional = 1;
57+
/* FieldID SEQUENCE */
58+
LTC_SET_ASN1(seq_fieldid, 0, LTC_ASN1_OBJECT_IDENTIFIER, tmpoid, 16UL);
59+
LTC_SET_ASN1(seq_fieldid, 1, LTC_ASN1_INTEGER, prime, 1UL);
60+
/* Curve SEQUENCE */
61+
LTC_SET_ASN1(seq_curve, 0, LTC_ASN1_OCTET_STRING, bin_a, (unsigned long)ECC_MAXSIZE);
62+
LTC_SET_ASN1(seq_curve, 1, LTC_ASN1_OCTET_STRING, bin_b, (unsigned long)ECC_MAXSIZE);
63+
LTC_SET_ASN1(seq_curve, 2, LTC_ASN1_RAW_BIT_STRING, bin_seed, (unsigned long)8*128);
64+
seq_curve[2].optional = 1;
65+
/* try to load public key */
66+
len_xy = sizeof(bin_xy);
67+
len = 6;
68+
err = x509_decode_subject_public_key_info(in, inlen, PKA_EC, bin_xy, &len_xy, LTC_ASN1_SEQUENCE, seq_ecparams, &len);
69+
70+
if (err == CRYPT_OK) {
71+
len_a = seq_curve[0].size;
72+
len_b = seq_curve[1].size;
73+
len_g = seq_ecparams[3].size;
74+
/* create bignums */
75+
if ((err = mp_read_unsigned_bin(a, bin_a, len_a)) != CRYPT_OK) { goto error; }
76+
if ((err = mp_read_unsigned_bin(b, bin_b, len_b)) != CRYPT_OK) { goto error; }
77+
if ((err = ltc_ecc_import_point(bin_g, len_g, prime, a, b, gx, gy)) != CRYPT_OK) { goto error; }
78+
/* load curve parameters */
79+
if ((err = ecc_set_dp_from_mpis(a, b, prime, order, gx, gy, cofactor, key)) != CRYPT_OK) { goto error; }
80+
/* load public key */
81+
if ((err = ecc_set_key(bin_xy, len_xy, PK_PUBLIC, key)) != CRYPT_OK) { goto error; }
82+
goto success;
83+
}
84+
85+
/* ### 3. try to load private key - no curve parameters just curve OID */
86+
87+
/* ECPrivateKey SEQUENCE */
88+
LTC_SET_ASN1(custom, 0, LTC_ASN1_OBJECT_IDENTIFIER, curveoid, 16UL);
89+
LTC_SET_ASN1(custom, 1, LTC_ASN1_RAW_BIT_STRING, bin_xy, (unsigned long)8*(2*ECC_MAXSIZE+2));
90+
LTC_SET_ASN1(seq_priv, 0, LTC_ASN1_SHORT_INTEGER, &pkver, 1UL);
91+
LTC_SET_ASN1(seq_priv, 1, LTC_ASN1_OCTET_STRING, bin_k, (unsigned long)ECC_MAXSIZE);
92+
LTC_SET_ASN1_CUSTOM_CONSTRUCTED(seq_priv, 2, LTC_ASN1_CL_CONTEXT_SPECIFIC, 0, custom); /* context specific 0 */
93+
LTC_SET_ASN1_CUSTOM_CONSTRUCTED(seq_priv, 3, LTC_ASN1_CL_CONTEXT_SPECIFIC, 1, custom + 1); /* context specific 1 */
94+
95+
/* try to load private key */
96+
err = der_decode_sequence(in, inlen, seq_priv, 4);
97+
if (err == CRYPT_OK) {
98+
/* load curve parameters for given curve OID */
99+
len = sizeof(OID);
100+
if ((err = pk_oid_num_to_str(curveoid, custom[0].size, OID, &len)) != CRYPT_OK) { goto error; }
101+
if ((err = ecc_get_curve(OID, &curve)) != CRYPT_OK) { goto error; }
102+
if ((err = ecc_set_dp(curve, key)) != CRYPT_OK) { goto error; }
103+
/* load private+public key */
104+
if ((err = ecc_set_key(bin_k, seq_priv[1].size, PK_PRIVATE, key)) != CRYPT_OK) { goto error; }
105+
goto success;
106+
}
107+
108+
/* ### 4. try to load private key - curve parameters included */
109+
110+
/* ECPrivateKey SEQUENCE */
111+
LTC_SET_ASN1(custom, 0, LTC_ASN1_SEQUENCE, seq_ecparams, 6UL);
112+
LTC_SET_ASN1(custom, 1, LTC_ASN1_RAW_BIT_STRING, bin_xy, (unsigned long)8*(2*ECC_MAXSIZE+2));
113+
LTC_SET_ASN1(seq_priv, 0, LTC_ASN1_SHORT_INTEGER, &pkver, 1UL);
114+
LTC_SET_ASN1(seq_priv, 1, LTC_ASN1_OCTET_STRING, bin_k, (unsigned long)ECC_MAXSIZE);
115+
LTC_SET_ASN1_CUSTOM_CONSTRUCTED(seq_priv, 2, LTC_ASN1_CL_CONTEXT_SPECIFIC, 0, custom); /* context specific 0 */
116+
LTC_SET_ASN1_CUSTOM_CONSTRUCTED(seq_priv, 3, LTC_ASN1_CL_CONTEXT_SPECIFIC, 1, custom + 1); /* context specific 1 */
117+
/* ECParameters SEQUENCE */
118+
LTC_SET_ASN1(seq_ecparams, 0, LTC_ASN1_SHORT_INTEGER, &ecver, 1UL);
119+
LTC_SET_ASN1(seq_ecparams, 1, LTC_ASN1_SEQUENCE, seq_fieldid, 2UL);
120+
LTC_SET_ASN1(seq_ecparams, 2, LTC_ASN1_SEQUENCE, seq_curve, 3UL);
121+
LTC_SET_ASN1(seq_ecparams, 3, LTC_ASN1_OCTET_STRING, bin_g, (unsigned long)2*ECC_MAXSIZE+1);
122+
LTC_SET_ASN1(seq_ecparams, 4, LTC_ASN1_INTEGER, order, 1UL);
123+
LTC_SET_ASN1(seq_ecparams, 5, LTC_ASN1_SHORT_INTEGER, &cofactor, 1UL);
124+
seq_ecparams[5].optional = 1;
125+
/* FieldID SEQUENCE */
126+
LTC_SET_ASN1(seq_fieldid, 0, LTC_ASN1_OBJECT_IDENTIFIER, tmpoid, 16UL);
127+
LTC_SET_ASN1(seq_fieldid, 1, LTC_ASN1_INTEGER, prime, 1UL);
128+
/* Curve SEQUENCE */
129+
LTC_SET_ASN1(seq_curve, 0, LTC_ASN1_OCTET_STRING, bin_a, (unsigned long)ECC_MAXSIZE);
130+
LTC_SET_ASN1(seq_curve, 1, LTC_ASN1_OCTET_STRING, bin_b, (unsigned long)ECC_MAXSIZE);
131+
LTC_SET_ASN1(seq_curve, 2, LTC_ASN1_RAW_BIT_STRING, bin_seed, (unsigned long)8*128);
132+
seq_curve[2].optional = 1;
133+
/* try to load private key */
134+
err = der_decode_sequence(in, inlen, seq_priv, 4);
135+
if (err == CRYPT_OK) {
136+
len_xy = custom[1].size;
137+
len_k = seq_priv[1].size;
138+
len_a = seq_curve[0].size;
139+
len_b = seq_curve[1].size;
140+
len_g = seq_ecparams[3].size;
141+
/* create bignums */
142+
if ((err = mp_read_unsigned_bin(a, bin_a, len_a)) != CRYPT_OK) { goto error; }
143+
if ((err = mp_read_unsigned_bin(b, bin_b, len_b)) != CRYPT_OK) { goto error; }
144+
if ((err = ltc_ecc_import_point(bin_g, len_g, prime, a, b, gx, gy)) != CRYPT_OK) { goto error; }
145+
/* load curve parameters */
146+
if ((err = ecc_set_dp_from_mpis(a, b, prime, order, gx, gy, cofactor, key)) != CRYPT_OK) { goto error; }
147+
/* load private+public key */
148+
if ((err = ecc_set_key(bin_k, len_k, PK_PRIVATE, key)) != CRYPT_OK) { goto error; }
149+
goto success;
150+
}
151+
152+
/* ### 5. all attempts failed */
153+
goto error;
154+
155+
success:
156+
err = CRYPT_OK;
157+
error:
158+
mp_clear_multi(prime, order, a, b, gx, gy, NULL);
159+
return err;
160+
}
161+
162+
#endif
163+
164+
/* ref: $Format:%D$ */
165+
/* git commit: $Format:%H$ */
166+
/* commit time: $Format:%ai$ */

0 commit comments

Comments
 (0)