Shawn Willden | 2c8dd3e | 2014-09-18 15:16:31 -0600 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "ecdsa_key.h" |
| 18 | #include "ecdsa_operation.h" |
| 19 | #include "openssl_utils.h" |
| 20 | #include "unencrypted_key_blob.h" |
| 21 | |
| 22 | namespace keymaster { |
| 23 | |
| 24 | const uint32_t ECDSA_DEFAULT_KEY_SIZE = 224; |
| 25 | |
| 26 | /* static */ |
| 27 | EcdsaKey* EcdsaKey::GenerateKey(const AuthorizationSet& key_description, const Logger& logger, |
| 28 | keymaster_error_t* error) { |
| 29 | if (!error) |
| 30 | return NULL; |
| 31 | |
| 32 | AuthorizationSet authorizations(key_description); |
| 33 | |
| 34 | uint32_t key_size = ECDSA_DEFAULT_KEY_SIZE; |
| 35 | if (!authorizations.GetTagValue(TAG_KEY_SIZE, &key_size)) |
| 36 | authorizations.push_back(Authorization(TAG_KEY_SIZE, key_size)); |
| 37 | |
| 38 | UniquePtr<EC_KEY, ECDSA_Delete> ecdsa_key(EC_KEY_new()); |
| 39 | UniquePtr<EVP_PKEY, EVP_PKEY_Delete> pkey(EVP_PKEY_new()); |
| 40 | if (ecdsa_key.get() == NULL || pkey.get() == NULL) { |
| 41 | *error = KM_ERROR_MEMORY_ALLOCATION_FAILED; |
| 42 | return NULL; |
| 43 | } |
| 44 | |
| 45 | UniquePtr<EC_GROUP, EC_GROUP_Delete> group(choose_group(key_size)); |
| 46 | if (group.get() == NULL) { |
| 47 | // Technically, could also have been a memory allocation problem. |
| 48 | *error = KM_ERROR_UNSUPPORTED_KEY_SIZE; |
| 49 | return NULL; |
| 50 | } |
| 51 | |
| 52 | EC_GROUP_set_point_conversion_form(group.get(), POINT_CONVERSION_UNCOMPRESSED); |
| 53 | EC_GROUP_set_asn1_flag(group.get(), OPENSSL_EC_NAMED_CURVE); |
| 54 | |
| 55 | if (EC_KEY_set_group(ecdsa_key.get(), group.get()) != 1 || |
| 56 | EC_KEY_generate_key(ecdsa_key.get()) != 1 || EC_KEY_check_key(ecdsa_key.get()) < 0) { |
| 57 | *error = KM_ERROR_UNKNOWN_ERROR; |
| 58 | return NULL; |
| 59 | } |
| 60 | |
| 61 | EcdsaKey* new_key = new EcdsaKey(ecdsa_key.release(), authorizations, logger); |
| 62 | *error = new_key ? KM_ERROR_OK : KM_ERROR_MEMORY_ALLOCATION_FAILED; |
| 63 | return new_key; |
| 64 | } |
| 65 | |
| 66 | /* static */ |
| 67 | EcdsaKey* EcdsaKey::ImportKey(const AuthorizationSet& key_description, EVP_PKEY* pkey, |
| 68 | const Logger& logger, keymaster_error_t* error) { |
| 69 | if (!error) |
| 70 | return NULL; |
| 71 | *error = KM_ERROR_UNKNOWN_ERROR; |
| 72 | |
| 73 | UniquePtr<EC_KEY, ECDSA_Delete> ecdsa_key(EVP_PKEY_get1_EC_KEY(pkey)); |
| 74 | if (!ecdsa_key.get()) |
| 75 | return NULL; |
| 76 | |
| 77 | AuthorizationSet authorizations(key_description); |
| 78 | |
| 79 | size_t extracted_key_size_bits; |
| 80 | *error = get_group_size(*EC_KEY_get0_group(ecdsa_key.get()), &extracted_key_size_bits); |
| 81 | if (*error != KM_ERROR_OK) |
| 82 | return NULL; |
| 83 | |
| 84 | uint32_t key_size_bits; |
| 85 | if (authorizations.GetTagValue(TAG_KEY_SIZE, &key_size_bits)) { |
| 86 | // key_size_bits specified, make sure it matches the key. |
| 87 | if (key_size_bits != extracted_key_size_bits) { |
| 88 | *error = KM_ERROR_IMPORT_PARAMETER_MISMATCH; |
| 89 | return NULL; |
| 90 | } |
| 91 | } else { |
| 92 | // key_size_bits not specified, add it. |
| 93 | authorizations.push_back(TAG_KEY_SIZE, extracted_key_size_bits); |
| 94 | } |
| 95 | |
| 96 | keymaster_algorithm_t algorithm; |
| 97 | if (authorizations.GetTagValue(TAG_ALGORITHM, &algorithm)) { |
| 98 | if (algorithm != KM_ALGORITHM_ECDSA) { |
| 99 | *error = KM_ERROR_IMPORT_PARAMETER_MISMATCH; |
| 100 | return NULL; |
| 101 | } |
| 102 | } else { |
| 103 | authorizations.push_back(TAG_ALGORITHM, KM_ALGORITHM_ECDSA); |
| 104 | } |
| 105 | |
| 106 | // Don't bother with the other parameters. If the necessary padding, digest, purpose, etc. are |
| 107 | // missing, the error will be diagnosed when the key is used (when auth checking is |
| 108 | // implemented). |
| 109 | *error = KM_ERROR_OK; |
| 110 | return new EcdsaKey(ecdsa_key.release(), authorizations, logger); |
| 111 | } |
| 112 | |
| 113 | /* static */ |
| 114 | EC_GROUP* EcdsaKey::choose_group(size_t key_size_bits) { |
| 115 | switch (key_size_bits) { |
| 116 | case 192: |
| 117 | return EC_GROUP_new_by_curve_name(NID_X9_62_prime192v1); |
| 118 | break; |
| 119 | case 224: |
| 120 | return EC_GROUP_new_by_curve_name(NID_secp224r1); |
| 121 | break; |
| 122 | case 256: |
| 123 | return EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1); |
| 124 | break; |
| 125 | case 384: |
| 126 | return EC_GROUP_new_by_curve_name(NID_secp384r1); |
| 127 | break; |
| 128 | case 521: |
| 129 | return EC_GROUP_new_by_curve_name(NID_secp521r1); |
| 130 | break; |
| 131 | default: |
| 132 | return NULL; |
| 133 | break; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /* static */ |
| 138 | keymaster_error_t EcdsaKey::get_group_size(const EC_GROUP& group, size_t* key_size_bits) { |
| 139 | switch (EC_GROUP_get_curve_name(&group)) { |
| 140 | case NID_X9_62_prime192v1: |
| 141 | *key_size_bits = 192; |
| 142 | break; |
| 143 | case NID_secp224r1: |
| 144 | *key_size_bits = 224; |
| 145 | break; |
| 146 | case NID_X9_62_prime256v1: |
| 147 | *key_size_bits = 256; |
| 148 | break; |
| 149 | case NID_secp384r1: |
| 150 | *key_size_bits = 384; |
| 151 | break; |
| 152 | case NID_secp521r1: |
| 153 | *key_size_bits = 521; |
| 154 | break; |
| 155 | default: |
| 156 | return KM_ERROR_UNSUPPORTED_EC_FIELD; |
| 157 | } |
| 158 | return KM_ERROR_OK; |
| 159 | } |
| 160 | |
| 161 | EcdsaKey::EcdsaKey(const UnencryptedKeyBlob& blob, const Logger& logger, keymaster_error_t* error) |
| 162 | : AsymmetricKey(blob, logger) { |
| 163 | if (error) |
| 164 | *error = LoadKey(blob); |
| 165 | } |
| 166 | |
| 167 | Operation* EcdsaKey::CreateOperation(keymaster_purpose_t purpose, keymaster_digest_t digest, |
| 168 | keymaster_padding_t padding, keymaster_error_t* error) { |
| 169 | Operation* op; |
| 170 | switch (purpose) { |
| 171 | case KM_PURPOSE_SIGN: |
| 172 | op = new EcdsaSignOperation(purpose, logger_, digest, padding, ecdsa_key_.release()); |
| 173 | break; |
| 174 | case KM_PURPOSE_VERIFY: |
| 175 | op = new EcdsaVerifyOperation(purpose, logger_, digest, padding, ecdsa_key_.release()); |
| 176 | break; |
| 177 | default: |
| 178 | *error = KM_ERROR_UNIMPLEMENTED; |
| 179 | return NULL; |
| 180 | } |
| 181 | *error = op ? KM_ERROR_OK : KM_ERROR_MEMORY_ALLOCATION_FAILED; |
| 182 | return op; |
| 183 | } |
| 184 | |
| 185 | bool EcdsaKey::EvpToInternal(const EVP_PKEY* pkey) { |
| 186 | ecdsa_key_.reset(EVP_PKEY_get1_EC_KEY(const_cast<EVP_PKEY*>(pkey))); |
| 187 | return ecdsa_key_.get() != NULL; |
| 188 | } |
| 189 | |
| 190 | bool EcdsaKey::InternalToEvp(EVP_PKEY* pkey) const { |
| 191 | return EVP_PKEY_set1_EC_KEY(pkey, ecdsa_key_.get()) == 1; |
| 192 | } |
| 193 | |
| 194 | } // namespace keymaster |