blob: bd159ac7da10131c1a8a8aa89e44ed823bebac48 [file] [log] [blame]
Shawn Willden2c8dd3e2014-09-18 15:16:31 -06001/*
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
Thai Duongf862a762015-03-18 14:10:56 -070017#include "ec_key.h"
18#include "operation.h"
Shawn Willden567a4a02014-12-31 12:14:46 -070019#include "openssl_err.h"
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060020#include "openssl_utils.h"
21#include "unencrypted_key_blob.h"
22
23namespace keymaster {
24
Shawn Willdena278f612014-12-23 11:22:21 -070025static KeyFactoryRegistry::Registration<EcdsaKeyFactory> registration;
26
Thai Duongf862a762015-03-18 14:10:56 -070027Key* EcKeyFactory::LoadKey(const UnencryptedKeyBlob& blob, keymaster_error_t* error) {
28 return new EcKey(blob, error);
Shawn Willden13011cc2015-03-04 09:41:54 -070029}
30
Thai Duongf862a762015-03-18 14:10:56 -070031Key* EcKeyFactory::GenerateKey(const AuthorizationSet& key_description,
32 keymaster_error_t* error) {
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060033 if (!error)
34 return NULL;
35
36 AuthorizationSet authorizations(key_description);
37
Shawn Willden3b4e1652015-02-27 13:33:01 -070038 uint32_t key_size;
Thai Duongf862a762015-03-18 14:10:56 -070039
Shawn Willden3b4e1652015-02-27 13:33:01 -070040 if (!authorizations.GetTagValue(TAG_KEY_SIZE, &key_size)) {
Thai Duongf862a762015-03-18 14:10:56 -070041 LOG_E("%s", "No key size specified for EC key generation");
Shawn Willden3b4e1652015-02-27 13:33:01 -070042 *error = KM_ERROR_UNSUPPORTED_KEY_SIZE;
43 }
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060044
Thai Duongf862a762015-03-18 14:10:56 -070045 UniquePtr<EC_KEY, EcKey::EC_Delete> ec_key(EC_KEY_new());
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060046 UniquePtr<EVP_PKEY, EVP_PKEY_Delete> pkey(EVP_PKEY_new());
Thai Duongf862a762015-03-18 14:10:56 -070047 if (ec_key.get() == NULL || pkey.get() == NULL) {
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060048 *error = KM_ERROR_MEMORY_ALLOCATION_FAILED;
49 return NULL;
50 }
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060051 UniquePtr<EC_GROUP, EC_GROUP_Delete> group(choose_group(key_size));
52 if (group.get() == NULL) {
53 // Technically, could also have been a memory allocation problem.
Shawn Willdenf01329d2015-03-11 21:51:38 -060054 LOG_E("Unable to get EC group for key of size %d", key_size);
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060055 *error = KM_ERROR_UNSUPPORTED_KEY_SIZE;
56 return NULL;
57 }
58
Shawn Willdenbaf99b62014-09-26 09:38:22 -060059#if !defined(OPENSSL_IS_BORINGSSL)
Adam Langleya550fba2015-02-13 14:44:14 -080060 EC_GROUP_set_point_conversion_form(group.get(), POINT_CONVERSION_UNCOMPRESSED);
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060061 EC_GROUP_set_asn1_flag(group.get(), OPENSSL_EC_NAMED_CURVE);
Shawn Willdenbaf99b62014-09-26 09:38:22 -060062#endif
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060063
Thai Duongf862a762015-03-18 14:10:56 -070064 if (EC_KEY_set_group(ec_key.get(), group.get()) != 1 ||
65 EC_KEY_generate_key(ec_key.get()) != 1 || EC_KEY_check_key(ec_key.get()) < 0) {
Shawn Willden567a4a02014-12-31 12:14:46 -070066 *error = TranslateLastOpenSslError();
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060067 return NULL;
68 }
69
Thai Duongf862a762015-03-18 14:10:56 -070070 EcKey* new_key = new EcKey(ec_key.release(), authorizations);
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060071 *error = new_key ? KM_ERROR_OK : KM_ERROR_MEMORY_ALLOCATION_FAILED;
72 return new_key;
73}
74
Thai Duongf862a762015-03-18 14:10:56 -070075Key* EcKeyFactory::ImportKey(const AuthorizationSet& key_description,
76 keymaster_key_format_t key_format, const uint8_t* key_data,
77 size_t key_data_length, keymaster_error_t* error) {
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060078 if (!error)
79 return NULL;
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060080
Shawn Willdena278f612014-12-23 11:22:21 -070081 UniquePtr<EVP_PKEY, EVP_PKEY_Delete> pkey(
Thai Duongf862a762015-03-18 14:10:56 -070082 ExtractEvpKey(key_format, registry_key(), key_data, key_data_length, error));
Shawn Willdena278f612014-12-23 11:22:21 -070083 if (*error != KM_ERROR_OK)
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060084 return NULL;
Shawn Willdena278f612014-12-23 11:22:21 -070085 assert(pkey.get());
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060086
Thai Duongf862a762015-03-18 14:10:56 -070087 UniquePtr<EC_KEY, EcKey::EC_Delete> ec_key(EVP_PKEY_get1_EC_KEY(pkey.get()));
88 if (!ec_key.get()) {
Shawn Willden567a4a02014-12-31 12:14:46 -070089 *error = TranslateLastOpenSslError();
Shawn Willdena278f612014-12-23 11:22:21 -070090 return NULL;
91 }
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060092
93 size_t extracted_key_size_bits;
Thai Duongf862a762015-03-18 14:10:56 -070094 *error = get_group_size(*EC_KEY_get0_group(ec_key.get()), &extracted_key_size_bits);
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060095 if (*error != KM_ERROR_OK)
96 return NULL;
97
Shawn Willdena278f612014-12-23 11:22:21 -070098 AuthorizationSet authorizations(key_description);
99
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600100 uint32_t key_size_bits;
101 if (authorizations.GetTagValue(TAG_KEY_SIZE, &key_size_bits)) {
102 // key_size_bits specified, make sure it matches the key.
103 if (key_size_bits != extracted_key_size_bits) {
104 *error = KM_ERROR_IMPORT_PARAMETER_MISMATCH;
105 return NULL;
106 }
107 } else {
108 // key_size_bits not specified, add it.
109 authorizations.push_back(TAG_KEY_SIZE, extracted_key_size_bits);
110 }
111
112 keymaster_algorithm_t algorithm;
113 if (authorizations.GetTagValue(TAG_ALGORITHM, &algorithm)) {
Thai Duongf862a762015-03-18 14:10:56 -0700114 if (algorithm != registry_key()) {
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600115 *error = KM_ERROR_IMPORT_PARAMETER_MISMATCH;
116 return NULL;
117 }
118 } else {
Thai Duongf862a762015-03-18 14:10:56 -0700119 authorizations.push_back(TAG_ALGORITHM, registry_key());
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600120 }
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600121 // Don't bother with the other parameters. If the necessary padding, digest, purpose, etc. are
122 // missing, the error will be diagnosed when the key is used (when auth checking is
123 // implemented).
124 *error = KM_ERROR_OK;
Thai Duongf862a762015-03-18 14:10:56 -0700125 return new EcKey(ec_key.release(), authorizations);
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600126}
127
Thai Duongf862a762015-03-18 14:10:56 -0700128Key* EcKeyFactory::RescopeKey(const UnencryptedKeyBlob& blob,
129 const AuthorizationSet& new_authorizations,
130 keymaster_error_t* error) {
Shawn Willden5fad7852015-01-26 16:10:56 -0700131 if (!error)
132 return NULL;
133
Thai Duongf862a762015-03-18 14:10:56 -0700134 EcKey original_key(blob, error);
Shawn Willden5fad7852015-01-26 16:10:56 -0700135 if (*error != KM_ERROR_OK)
136 return NULL;
137
Thai Duongf862a762015-03-18 14:10:56 -0700138 EcKey* new_key = new EcKey(original_key.ec_key_.release(), new_authorizations);
Shawn Willden5fad7852015-01-26 16:10:56 -0700139 *error = new_key ? KM_ERROR_OK : KM_ERROR_MEMORY_ALLOCATION_FAILED;
140 return new_key;
141}
142
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600143/* static */
Thai Duongf862a762015-03-18 14:10:56 -0700144EC_GROUP* EcKeyFactory::choose_group(size_t key_size_bits) {
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600145 switch (key_size_bits) {
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600146 case 224:
147 return EC_GROUP_new_by_curve_name(NID_secp224r1);
148 break;
149 case 256:
150 return EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
151 break;
152 case 384:
153 return EC_GROUP_new_by_curve_name(NID_secp384r1);
154 break;
155 case 521:
156 return EC_GROUP_new_by_curve_name(NID_secp521r1);
157 break;
158 default:
159 return NULL;
160 break;
161 }
162}
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600163/* static */
Thai Duongf862a762015-03-18 14:10:56 -0700164keymaster_error_t EcKeyFactory::get_group_size(const EC_GROUP& group, size_t* key_size_bits) {
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600165 switch (EC_GROUP_get_curve_name(&group)) {
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600166 case NID_secp224r1:
167 *key_size_bits = 224;
168 break;
169 case NID_X9_62_prime256v1:
170 *key_size_bits = 256;
171 break;
172 case NID_secp384r1:
173 *key_size_bits = 384;
174 break;
175 case NID_secp521r1:
176 *key_size_bits = 521;
177 break;
178 default:
179 return KM_ERROR_UNSUPPORTED_EC_FIELD;
180 }
181 return KM_ERROR_OK;
182}
183
Thai Duongf862a762015-03-18 14:10:56 -0700184EcKey::EcKey(const UnencryptedKeyBlob& blob, keymaster_error_t* error) : AsymmetricKey(blob) {
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600185 if (error)
186 *error = LoadKey(blob);
187}
188
Thai Duongf862a762015-03-18 14:10:56 -0700189bool EcKey::EvpToInternal(const EVP_PKEY* pkey) {
190 ec_key_.reset(EVP_PKEY_get1_EC_KEY(const_cast<EVP_PKEY*>(pkey)));
191 return ec_key_.get() != NULL;
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600192}
193
Thai Duongf862a762015-03-18 14:10:56 -0700194bool EcKey::InternalToEvp(EVP_PKEY* pkey) const {
195 return EVP_PKEY_set1_EC_KEY(pkey, ec_key_.get()) == 1;
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600196}
197
198} // namespace keymaster