blob: c58140c3bb762b3e810fd49745e2621f6bc2c330 [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.
54 *error = KM_ERROR_UNSUPPORTED_KEY_SIZE;
55 return NULL;
56 }
57
Shawn Willdenbaf99b62014-09-26 09:38:22 -060058#if !defined(OPENSSL_IS_BORINGSSL)
Adam Langleya550fba2015-02-13 14:44:14 -080059 EC_GROUP_set_point_conversion_form(group.get(), POINT_CONVERSION_UNCOMPRESSED);
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060060 EC_GROUP_set_asn1_flag(group.get(), OPENSSL_EC_NAMED_CURVE);
Shawn Willdenbaf99b62014-09-26 09:38:22 -060061#endif
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060062
Thai Duongf862a762015-03-18 14:10:56 -070063 if (EC_KEY_set_group(ec_key.get(), group.get()) != 1 ||
64 EC_KEY_generate_key(ec_key.get()) != 1 || EC_KEY_check_key(ec_key.get()) < 0) {
Shawn Willden567a4a02014-12-31 12:14:46 -070065 *error = TranslateLastOpenSslError();
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060066 return NULL;
67 }
68
Thai Duongf862a762015-03-18 14:10:56 -070069 EcKey* new_key = new EcKey(ec_key.release(), authorizations);
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060070 *error = new_key ? KM_ERROR_OK : KM_ERROR_MEMORY_ALLOCATION_FAILED;
71 return new_key;
72}
73
Thai Duongf862a762015-03-18 14:10:56 -070074Key* EcKeyFactory::ImportKey(const AuthorizationSet& key_description,
75 keymaster_key_format_t key_format, const uint8_t* key_data,
76 size_t key_data_length, keymaster_error_t* error) {
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060077 if (!error)
78 return NULL;
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060079
Shawn Willdena278f612014-12-23 11:22:21 -070080 UniquePtr<EVP_PKEY, EVP_PKEY_Delete> pkey(
Thai Duongf862a762015-03-18 14:10:56 -070081 ExtractEvpKey(key_format, registry_key(), key_data, key_data_length, error));
Shawn Willdena278f612014-12-23 11:22:21 -070082 if (*error != KM_ERROR_OK)
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060083 return NULL;
Shawn Willdena278f612014-12-23 11:22:21 -070084 assert(pkey.get());
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060085
Thai Duongf862a762015-03-18 14:10:56 -070086 UniquePtr<EC_KEY, EcKey::EC_Delete> ec_key(EVP_PKEY_get1_EC_KEY(pkey.get()));
87 if (!ec_key.get()) {
Shawn Willden567a4a02014-12-31 12:14:46 -070088 *error = TranslateLastOpenSslError();
Shawn Willdena278f612014-12-23 11:22:21 -070089 return NULL;
90 }
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060091
92 size_t extracted_key_size_bits;
Thai Duongf862a762015-03-18 14:10:56 -070093 *error = get_group_size(*EC_KEY_get0_group(ec_key.get()), &extracted_key_size_bits);
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060094 if (*error != KM_ERROR_OK)
95 return NULL;
96
Shawn Willdena278f612014-12-23 11:22:21 -070097 AuthorizationSet authorizations(key_description);
98
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060099 uint32_t key_size_bits;
100 if (authorizations.GetTagValue(TAG_KEY_SIZE, &key_size_bits)) {
101 // key_size_bits specified, make sure it matches the key.
102 if (key_size_bits != extracted_key_size_bits) {
103 *error = KM_ERROR_IMPORT_PARAMETER_MISMATCH;
104 return NULL;
105 }
106 } else {
107 // key_size_bits not specified, add it.
108 authorizations.push_back(TAG_KEY_SIZE, extracted_key_size_bits);
109 }
110
111 keymaster_algorithm_t algorithm;
112 if (authorizations.GetTagValue(TAG_ALGORITHM, &algorithm)) {
Thai Duongf862a762015-03-18 14:10:56 -0700113 if (algorithm != registry_key()) {
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600114 *error = KM_ERROR_IMPORT_PARAMETER_MISMATCH;
115 return NULL;
116 }
117 } else {
Thai Duongf862a762015-03-18 14:10:56 -0700118 authorizations.push_back(TAG_ALGORITHM, registry_key());
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600119 }
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600120 // Don't bother with the other parameters. If the necessary padding, digest, purpose, etc. are
121 // missing, the error will be diagnosed when the key is used (when auth checking is
122 // implemented).
123 *error = KM_ERROR_OK;
Thai Duongf862a762015-03-18 14:10:56 -0700124 return new EcKey(ec_key.release(), authorizations);
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600125}
126
Thai Duongf862a762015-03-18 14:10:56 -0700127Key* EcKeyFactory::RescopeKey(const UnencryptedKeyBlob& blob,
128 const AuthorizationSet& new_authorizations,
129 keymaster_error_t* error) {
Shawn Willden5fad7852015-01-26 16:10:56 -0700130 if (!error)
131 return NULL;
132
Thai Duongf862a762015-03-18 14:10:56 -0700133 EcKey original_key(blob, error);
Shawn Willden5fad7852015-01-26 16:10:56 -0700134 if (*error != KM_ERROR_OK)
135 return NULL;
136
Thai Duongf862a762015-03-18 14:10:56 -0700137 EcKey* new_key = new EcKey(original_key.ec_key_.release(), new_authorizations);
Shawn Willden5fad7852015-01-26 16:10:56 -0700138 *error = new_key ? KM_ERROR_OK : KM_ERROR_MEMORY_ALLOCATION_FAILED;
139 return new_key;
140}
141
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600142/* static */
Thai Duongf862a762015-03-18 14:10:56 -0700143EC_GROUP* EcKeyFactory::choose_group(size_t key_size_bits) {
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600144 switch (key_size_bits) {
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600145 case 224:
146 return EC_GROUP_new_by_curve_name(NID_secp224r1);
147 break;
148 case 256:
149 return EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
150 break;
151 case 384:
152 return EC_GROUP_new_by_curve_name(NID_secp384r1);
153 break;
154 case 521:
155 return EC_GROUP_new_by_curve_name(NID_secp521r1);
156 break;
157 default:
158 return NULL;
159 break;
160 }
161}
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600162/* static */
Thai Duongf862a762015-03-18 14:10:56 -0700163keymaster_error_t EcKeyFactory::get_group_size(const EC_GROUP& group, size_t* key_size_bits) {
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600164 switch (EC_GROUP_get_curve_name(&group)) {
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600165 case NID_secp224r1:
166 *key_size_bits = 224;
167 break;
168 case NID_X9_62_prime256v1:
169 *key_size_bits = 256;
170 break;
171 case NID_secp384r1:
172 *key_size_bits = 384;
173 break;
174 case NID_secp521r1:
175 *key_size_bits = 521;
176 break;
177 default:
178 return KM_ERROR_UNSUPPORTED_EC_FIELD;
179 }
180 return KM_ERROR_OK;
181}
182
Thai Duongf862a762015-03-18 14:10:56 -0700183EcKey::EcKey(const UnencryptedKeyBlob& blob, keymaster_error_t* error) : AsymmetricKey(blob) {
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600184 if (error)
185 *error = LoadKey(blob);
186}
187
Thai Duongf862a762015-03-18 14:10:56 -0700188bool EcKey::EvpToInternal(const EVP_PKEY* pkey) {
189 ec_key_.reset(EVP_PKEY_get1_EC_KEY(const_cast<EVP_PKEY*>(pkey)));
190 return ec_key_.get() != NULL;
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600191}
192
Thai Duongf862a762015-03-18 14:10:56 -0700193bool EcKey::InternalToEvp(EVP_PKEY* pkey) const {
194 return EVP_PKEY_set1_EC_KEY(pkey, ec_key_.get()) == 1;
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600195}
196
197} // namespace keymaster