Codebase list libcryptx-perl / 2753b39
ltc sync Karel Miko 5 years ago
3 changed file(s) with 144 addition(s) and 86 deletion(s). Raw diff Collapse all Expand all
194194 int ecc_set_dp_from_mpis(void *a, void *b, void *prime, void *order, void *gx, void *gy, unsigned long cofactor, ecc_key *key);
195195 int ecc_copy_dp(const ecc_key *srckey, ecc_key *key);
196196 int ecc_set_dp_by_size(int size, ecc_key *key);
197 int ecc_import_subject_public_key_info(const unsigned char *in, unsigned long inlen, ecc_key *key);
197198
198199 /* low level functions */
199200 ecc_point *ltc_ecc_new_point(void);
1010
1111 #ifdef LTC_MECC
1212
13 int ecc_import_openssl(const unsigned char *in, unsigned long inlen, ecc_key *key)
13 static int _ecc_import_private_with_oid(const unsigned char *in, unsigned long inlen, ecc_key *key)
1414 {
15 void *prime, *order, *a, *b, *gx, *gy;
16 ltc_asn1_list seq_fieldid[2], seq_curve[3], seq_ecparams[6], seq_priv[4], custom[2];
17 unsigned char bin_a[ECC_MAXSIZE], bin_b[ECC_MAXSIZE], bin_k[ECC_MAXSIZE];
18 unsigned char bin_g[2*ECC_MAXSIZE+1], bin_xy[2*ECC_MAXSIZE+2], bin_seed[128];
19 unsigned long len_a, len_b, len_k, len_g, len_xy, len_oid, len;
20 unsigned long cofactor = 0, ecver = 0, pkver = 0, tmpoid[16], curveoid[16];
15 ltc_asn1_list seq_priv[4], custom[2];
16 unsigned char bin_xy[2*ECC_MAXSIZE+2], bin_k[ECC_MAXSIZE];
17 unsigned long len, pkver = 0, curveoid[16];
2118 char OID[256];
2219 const ltc_ecc_curve *curve;
2320 int err;
2421
25 if ((err = mp_init_multi(&prime, &order, &a, &b, &gx, &gy, NULL)) != CRYPT_OK) {
26 return err;
27 }
28
29 /* ### 1. try to load public key - no curve parameters just curve OID */
30
31 len_xy = sizeof(bin_xy);
32 len_oid = 16;
33 err = x509_decode_subject_public_key_info(in, inlen, PKA_EC, bin_xy, &len_xy,
34 LTC_ASN1_OBJECT_IDENTIFIER, (void *)curveoid, &len_oid);
35 if (err == CRYPT_OK) {
36 /* load curve parameters for given curve OID */
37 len = sizeof(OID);
38 if ((err = pk_oid_num_to_str(curveoid, len_oid, OID, &len)) != CRYPT_OK) { goto error; }
39 if ((err = ecc_get_curve(OID, &curve)) != CRYPT_OK) { goto error; }
40 if ((err = ecc_set_dp(curve, key)) != CRYPT_OK) { goto error; }
41 /* load public key */
42 if ((err = ecc_set_key(bin_xy, len_xy, PK_PUBLIC, key)) != CRYPT_OK) { goto error; }
43 goto success;
44 }
45
46 /* ### 2. try to load public key - curve parameters included */
47
48 /* ECParameters SEQUENCE */
49 LTC_SET_ASN1(seq_ecparams, 0, LTC_ASN1_SHORT_INTEGER, &ecver, 1UL);
50 LTC_SET_ASN1(seq_ecparams, 1, LTC_ASN1_SEQUENCE, seq_fieldid, 2UL);
51 LTC_SET_ASN1(seq_ecparams, 2, LTC_ASN1_SEQUENCE, seq_curve, 3UL);
52 LTC_SET_ASN1(seq_ecparams, 3, LTC_ASN1_OCTET_STRING, bin_g, (unsigned long)2*ECC_MAXSIZE+1);
53 LTC_SET_ASN1(seq_ecparams, 4, LTC_ASN1_INTEGER, order, 1UL);
54 LTC_SET_ASN1(seq_ecparams, 5, LTC_ASN1_SHORT_INTEGER, &cofactor, 1UL);
55 seq_ecparams[5].optional = 1;
56 /* FieldID SEQUENCE */
57 LTC_SET_ASN1(seq_fieldid, 0, LTC_ASN1_OBJECT_IDENTIFIER, tmpoid, 16UL);
58 LTC_SET_ASN1(seq_fieldid, 1, LTC_ASN1_INTEGER, prime, 1UL);
59 /* Curve SEQUENCE */
60 LTC_SET_ASN1(seq_curve, 0, LTC_ASN1_OCTET_STRING, bin_a, (unsigned long)ECC_MAXSIZE);
61 LTC_SET_ASN1(seq_curve, 1, LTC_ASN1_OCTET_STRING, bin_b, (unsigned long)ECC_MAXSIZE);
62 LTC_SET_ASN1(seq_curve, 2, LTC_ASN1_RAW_BIT_STRING, bin_seed, (unsigned long)8*128);
63 seq_curve[2].optional = 1;
64 /* try to load public key */
65 len_xy = sizeof(bin_xy);
66 len = 6;
67 err = x509_decode_subject_public_key_info(in, inlen, PKA_EC, bin_xy, &len_xy, LTC_ASN1_SEQUENCE, seq_ecparams, &len);
68
69 if (err == CRYPT_OK) {
70 len_a = seq_curve[0].size;
71 len_b = seq_curve[1].size;
72 len_g = seq_ecparams[3].size;
73 /* create bignums */
74 if ((err = mp_read_unsigned_bin(a, bin_a, len_a)) != CRYPT_OK) { goto error; }
75 if ((err = mp_read_unsigned_bin(b, bin_b, len_b)) != CRYPT_OK) { goto error; }
76 if ((err = ltc_ecc_import_point(bin_g, len_g, prime, a, b, gx, gy)) != CRYPT_OK) { goto error; }
77 /* load curve parameters */
78 if ((err = ecc_set_dp_from_mpis(a, b, prime, order, gx, gy, cofactor, key)) != CRYPT_OK) { goto error; }
79 /* load public key */
80 if ((err = ecc_set_key(bin_xy, len_xy, PK_PUBLIC, key)) != CRYPT_OK) { goto error; }
81 goto success;
82 }
83
84 /* ### 3. try to load private key - no curve parameters just curve OID */
22 /* ### try to load private key - no curve parameters just curve OID */
8523
8624 /* ECPrivateKey SEQUENCE */
8725 LTC_SET_ASN1(custom, 0, LTC_ASN1_OBJECT_IDENTIFIER, curveoid, 16UL);
88 LTC_SET_ASN1(custom, 1, LTC_ASN1_RAW_BIT_STRING, bin_xy, (unsigned long)8*(2*ECC_MAXSIZE+2));
26 LTC_SET_ASN1(custom, 1, LTC_ASN1_RAW_BIT_STRING, bin_xy, 8u*sizeof(bin_xy));
8927 LTC_SET_ASN1(seq_priv, 0, LTC_ASN1_SHORT_INTEGER, &pkver, 1UL);
90 LTC_SET_ASN1(seq_priv, 1, LTC_ASN1_OCTET_STRING, bin_k, (unsigned long)ECC_MAXSIZE);
28 LTC_SET_ASN1(seq_priv, 1, LTC_ASN1_OCTET_STRING, bin_k, sizeof(bin_k));
9129 LTC_SET_ASN1_CUSTOM_CONSTRUCTED(seq_priv, 2, LTC_ASN1_CL_CONTEXT_SPECIFIC, 0, custom); /* context specific 0 */
9230 LTC_SET_ASN1_CUSTOM_CONSTRUCTED(seq_priv, 3, LTC_ASN1_CL_CONTEXT_SPECIFIC, 1, custom + 1); /* context specific 1 */
9331
10038 if ((err = ecc_get_curve(OID, &curve)) != CRYPT_OK) { goto error; }
10139 if ((err = ecc_set_dp(curve, key)) != CRYPT_OK) { goto error; }
10240 /* load private+public key */
103 if ((err = ecc_set_key(bin_k, seq_priv[1].size, PK_PRIVATE, key)) != CRYPT_OK) { goto error; }
104 goto success;
41 err = ecc_set_key(bin_k, seq_priv[1].size, PK_PRIVATE, key);
42 }
43 error:
44 return err;
45 }
46
47 static int _ecc_import_private_with_curve(const unsigned char *in, unsigned long inlen, ecc_key *key)
48 {
49 void *prime, *order, *a, *b, *gx, *gy;
50 ltc_asn1_list seq_fieldid[2], seq_curve[3], seq_ecparams[6], seq_priv[4], custom[2];
51 unsigned char bin_a[ECC_MAXSIZE], bin_b[ECC_MAXSIZE], bin_k[ECC_MAXSIZE];
52 unsigned char bin_g[2*ECC_MAXSIZE+1], bin_xy[2*ECC_MAXSIZE+2], bin_seed[128];
53 unsigned long len_a, len_b, len_k, len_g;
54 unsigned long cofactor = 0, ecver = 0, pkver = 0, tmpoid[16];
55 int err;
56
57 if ((err = mp_init_multi(&prime, &order, &a, &b, &gx, &gy, NULL)) != CRYPT_OK) {
58 return err;
10559 }
10660
107 /* ### 4. try to load private key - curve parameters included */
61 /* ### try to load private key - curve parameters included */
10862
10963 /* ECPrivateKey SEQUENCE */
11064 LTC_SET_ASN1(custom, 0, LTC_ASN1_SEQUENCE, seq_ecparams, 6UL);
111 LTC_SET_ASN1(custom, 1, LTC_ASN1_RAW_BIT_STRING, bin_xy, (unsigned long)8*(2*ECC_MAXSIZE+2));
65 LTC_SET_ASN1(custom, 1, LTC_ASN1_RAW_BIT_STRING, bin_xy, 8u*sizeof(bin_xy));
11266 LTC_SET_ASN1(seq_priv, 0, LTC_ASN1_SHORT_INTEGER, &pkver, 1UL);
113 LTC_SET_ASN1(seq_priv, 1, LTC_ASN1_OCTET_STRING, bin_k, (unsigned long)ECC_MAXSIZE);
67 LTC_SET_ASN1(seq_priv, 1, LTC_ASN1_OCTET_STRING, bin_k, sizeof(bin_k));
11468 LTC_SET_ASN1_CUSTOM_CONSTRUCTED(seq_priv, 2, LTC_ASN1_CL_CONTEXT_SPECIFIC, 0, custom); /* context specific 0 */
11569 LTC_SET_ASN1_CUSTOM_CONSTRUCTED(seq_priv, 3, LTC_ASN1_CL_CONTEXT_SPECIFIC, 1, custom + 1); /* context specific 1 */
11670 /* ECParameters SEQUENCE */
11771 LTC_SET_ASN1(seq_ecparams, 0, LTC_ASN1_SHORT_INTEGER, &ecver, 1UL);
11872 LTC_SET_ASN1(seq_ecparams, 1, LTC_ASN1_SEQUENCE, seq_fieldid, 2UL);
11973 LTC_SET_ASN1(seq_ecparams, 2, LTC_ASN1_SEQUENCE, seq_curve, 3UL);
120 LTC_SET_ASN1(seq_ecparams, 3, LTC_ASN1_OCTET_STRING, bin_g, (unsigned long)2*ECC_MAXSIZE+1);
74 LTC_SET_ASN1(seq_ecparams, 3, LTC_ASN1_OCTET_STRING, bin_g, sizeof(bin_g));
12175 LTC_SET_ASN1(seq_ecparams, 4, LTC_ASN1_INTEGER, order, 1UL);
12276 LTC_SET_ASN1(seq_ecparams, 5, LTC_ASN1_SHORT_INTEGER, &cofactor, 1UL);
12377 seq_ecparams[5].optional = 1;
12579 LTC_SET_ASN1(seq_fieldid, 0, LTC_ASN1_OBJECT_IDENTIFIER, tmpoid, 16UL);
12680 LTC_SET_ASN1(seq_fieldid, 1, LTC_ASN1_INTEGER, prime, 1UL);
12781 /* Curve SEQUENCE */
128 LTC_SET_ASN1(seq_curve, 0, LTC_ASN1_OCTET_STRING, bin_a, (unsigned long)ECC_MAXSIZE);
129 LTC_SET_ASN1(seq_curve, 1, LTC_ASN1_OCTET_STRING, bin_b, (unsigned long)ECC_MAXSIZE);
130 LTC_SET_ASN1(seq_curve, 2, LTC_ASN1_RAW_BIT_STRING, bin_seed, (unsigned long)8*128);
82 LTC_SET_ASN1(seq_curve, 0, LTC_ASN1_OCTET_STRING, bin_a, sizeof(bin_a));
83 LTC_SET_ASN1(seq_curve, 1, LTC_ASN1_OCTET_STRING, bin_b, sizeof(bin_b));
84 LTC_SET_ASN1(seq_curve, 2, LTC_ASN1_RAW_BIT_STRING, bin_seed, sizeof(bin_seed));
13185 seq_curve[2].optional = 1;
13286 /* try to load private key */
13387 err = der_decode_sequence(in, inlen, seq_priv, 4);
13488 if (err == CRYPT_OK) {
135 len_xy = custom[1].size;
13689 len_k = seq_priv[1].size;
13790 len_a = seq_curve[0].size;
13891 len_b = seq_curve[1].size;
14497 /* load curve parameters */
14598 if ((err = ecc_set_dp_from_mpis(a, b, prime, order, gx, gy, cofactor, key)) != CRYPT_OK) { goto error; }
14699 /* load private+public key */
147 if ((err = ecc_set_key(bin_k, len_k, PK_PRIVATE, key)) != CRYPT_OK) { goto error; }
100 err = ecc_set_key(bin_k, len_k, PK_PRIVATE, key);
101 }
102 error:
103 mp_clear_multi(prime, order, a, b, gx, gy, NULL);
104 return err;
105 }
106
107 int ecc_import_openssl(const unsigned char *in, unsigned long inlen, ecc_key *key)
108 {
109 int err;
110
111 if ((err = ecc_import_subject_public_key_info(in, inlen, key)) == CRYPT_OK) {
148112 goto success;
149113 }
150114
151 /* ### 5. all attempts failed */
152 goto error;
115 if ((err = _ecc_import_private_with_oid(in, inlen, key)) == CRYPT_OK) {
116 goto success;
117 }
118
119 err = _ecc_import_private_with_curve(in, inlen, key);
153120
154121 success:
155 err = CRYPT_OK;
156 error:
157 mp_clear_multi(prime, order, a, b, gx, gy, NULL);
158122 return err;
159123 }
160124
88 #include "tomcrypt_private.h"
99
1010 #ifdef LTC_MECC
11
12 static int _ecc_import_x509_with_oid(const unsigned char *in, unsigned long inlen, ecc_key *key)
13 {
14 unsigned char bin_xy[2*ECC_MAXSIZE+2];
15 unsigned long curveoid[16];
16 unsigned long len_xy, len_oid, len;
17 char OID[256];
18 const ltc_ecc_curve *curve;
19 int err;
20
21 len_xy = sizeof(bin_xy);
22 len_oid = 16;
23 err = x509_decode_subject_public_key_info(in, inlen, PKA_EC, bin_xy, &len_xy,
24 LTC_ASN1_OBJECT_IDENTIFIER, (void *)curveoid, &len_oid);
25 if (err == CRYPT_OK) {
26 /* load curve parameters for given curve OID */
27 len = sizeof(OID);
28 if ((err = pk_oid_num_to_str(curveoid, len_oid, OID, &len)) != CRYPT_OK) { goto error; }
29 if ((err = ecc_get_curve(OID, &curve)) != CRYPT_OK) { goto error; }
30 if ((err = ecc_set_dp(curve, key)) != CRYPT_OK) { goto error; }
31 /* load public key */
32 err = ecc_set_key(bin_xy, len_xy, PK_PUBLIC, key);
33 }
34 error:
35 return err;
36 }
37
38 static int _ecc_import_x509_with_curve(const unsigned char *in, unsigned long inlen, ecc_key *key)
39 {
40 void *prime, *order, *a, *b, *gx, *gy;
41 ltc_asn1_list seq_fieldid[2], seq_curve[3], seq_ecparams[6];
42 unsigned char bin_a[ECC_MAXSIZE], bin_b[ECC_MAXSIZE];
43 unsigned char bin_g[2*ECC_MAXSIZE+1], bin_xy[2*ECC_MAXSIZE+2], bin_seed[128];
44 unsigned long len_a, len_b, len_g, len_xy, len;
45 unsigned long cofactor = 0, ecver = 0, tmpoid[16];
46 int err;
47
48 if ((err = mp_init_multi(&prime, &order, &a, &b, &gx, &gy, NULL)) != CRYPT_OK) {
49 return err;
50 }
51
52 /* ECParameters SEQUENCE */
53 LTC_SET_ASN1(seq_ecparams, 0, LTC_ASN1_SHORT_INTEGER, &ecver, 1UL);
54 LTC_SET_ASN1(seq_ecparams, 1, LTC_ASN1_SEQUENCE, seq_fieldid, 2UL);
55 LTC_SET_ASN1(seq_ecparams, 2, LTC_ASN1_SEQUENCE, seq_curve, 3UL);
56 LTC_SET_ASN1(seq_ecparams, 3, LTC_ASN1_OCTET_STRING, bin_g, sizeof(bin_g));
57 LTC_SET_ASN1(seq_ecparams, 4, LTC_ASN1_INTEGER, order, 1UL);
58 LTC_SET_ASN1(seq_ecparams, 5, LTC_ASN1_SHORT_INTEGER, &cofactor, 1UL);
59 seq_ecparams[5].optional = 1;
60 /* FieldID SEQUENCE */
61 LTC_SET_ASN1(seq_fieldid, 0, LTC_ASN1_OBJECT_IDENTIFIER, tmpoid, 16UL);
62 LTC_SET_ASN1(seq_fieldid, 1, LTC_ASN1_INTEGER, prime, 1UL);
63 /* Curve SEQUENCE */
64 LTC_SET_ASN1(seq_curve, 0, LTC_ASN1_OCTET_STRING, bin_a, sizeof(bin_a));
65 LTC_SET_ASN1(seq_curve, 1, LTC_ASN1_OCTET_STRING, bin_b, sizeof(bin_b));
66 LTC_SET_ASN1(seq_curve, 2, LTC_ASN1_RAW_BIT_STRING, bin_seed, 8u*sizeof(bin_seed));
67 seq_curve[2].optional = 1;
68 /* try to load public key */
69 len_xy = sizeof(bin_xy);
70 len = 6;
71 err = x509_decode_subject_public_key_info(in, inlen, PKA_EC, bin_xy, &len_xy, LTC_ASN1_SEQUENCE, seq_ecparams, &len);
72
73 if (err == CRYPT_OK) {
74 len_a = seq_curve[0].size;
75 len_b = seq_curve[1].size;
76 len_g = seq_ecparams[3].size;
77 /* create bignums */
78 if ((err = mp_read_unsigned_bin(a, bin_a, len_a)) != CRYPT_OK) { goto error; }
79 if ((err = mp_read_unsigned_bin(b, bin_b, len_b)) != CRYPT_OK) { goto error; }
80 if ((err = ltc_ecc_import_point(bin_g, len_g, prime, a, b, gx, gy)) != CRYPT_OK) { goto error; }
81 /* load curve parameters */
82 if ((err = ecc_set_dp_from_mpis(a, b, prime, order, gx, gy, cofactor, key)) != CRYPT_OK) { goto error; }
83 /* load public key */
84 err = ecc_set_key(bin_xy, len_xy, PK_PUBLIC, key);
85 }
86 error:
87 mp_clear_multi(prime, order, a, b, gx, gy, NULL);
88 return err;
89 }
90
91 int ecc_import_subject_public_key_info(const unsigned char *in, unsigned long inlen, ecc_key *key)
92 {
93 int err;
94
95 if ((err = _ecc_import_x509_with_oid(in, inlen, key)) == CRYPT_OK) {
96 goto success;
97 }
98
99 err = _ecc_import_x509_with_curve(in, inlen, key);
100
101 success:
102 return err;
103 }
11104
12105 /**
13106 Import an ECC key from a X.509 certificate
37130 l->child && l->child->type == LTC_ASN1_SEQUENCE &&
38131 l->child->child && l->child->child->type == LTC_ASN1_OBJECT_IDENTIFIER &&
39132 l->child->next && l->child->next->type == LTC_ASN1_BIT_STRING) {
40 err = ecc_import_openssl(l->data, l->size, key);
133 err = ecc_import_subject_public_key_info(l->data, l->size, key);
41134 goto LBL_DONE;
42135 }
43136 l = l->next;