|
| 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