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