@@ -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 );
3829error :
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 }
97119error :
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
116138success :
117139 return err ;
0 commit comments