|
| 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$ */ |
0 commit comments