blob: c8e3a36950a1789b4cd3fd6c57466759d665c16d [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
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060017#include "rsa_key.h"
Shawn Willden567a4a02014-12-31 12:14:46 -070018
Shawn Willden0cb69422015-05-26 08:31:37 -060019#include <keymaster/keymaster_context.h>
20
Shawn Willden567a4a02014-12-31 12:14:46 -070021#include "openssl_err.h"
22#include "openssl_utils.h"
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060023
Shawn Willdend79791b2015-05-09 12:48:36 +000024#if defined(OPENSSL_IS_BORINGSSL)
25typedef size_t openssl_size_t;
26#else
27typedef int openssl_size_t;
28#endif
29
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060030namespace keymaster {
31
Shawn Willden0cb69422015-05-26 08:31:37 -060032keymaster_error_t RsaKeyFactory::GenerateKey(const AuthorizationSet& key_description,
33 KeymasterKeyBlob* key_blob,
34 AuthorizationSet* hw_enforced,
35 AuthorizationSet* sw_enforced) {
36 if (!key_blob || !hw_enforced || !sw_enforced)
37 return KM_ERROR_OUTPUT_PARAMETER_NULL;
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060038
39 AuthorizationSet authorizations(key_description);
40
Shawn Willden3b4e1652015-02-27 13:33:01 -070041 uint64_t public_exponent;
42 if (!authorizations.GetTagValue(TAG_RSA_PUBLIC_EXPONENT, &public_exponent)) {
43 LOG_E("%s", "No public exponent specified for RSA key generation");
Shawn Willden0cb69422015-05-26 08:31:37 -060044 return KM_ERROR_INVALID_ARGUMENT;
Shawn Willden3b4e1652015-02-27 13:33:01 -070045 }
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060046
Shawn Willden3b4e1652015-02-27 13:33:01 -070047 uint32_t key_size;
48 if (!authorizations.GetTagValue(TAG_KEY_SIZE, &key_size)) {
49 LOG_E("%s", "No key size specified for RSA key generation");
Shawn Willden0cb69422015-05-26 08:31:37 -060050 return KM_ERROR_UNSUPPORTED_KEY_SIZE;
Shawn Willden3b4e1652015-02-27 13:33:01 -070051 }
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060052
53 UniquePtr<BIGNUM, BIGNUM_Delete> exponent(BN_new());
Shawn Willdena278f612014-12-23 11:22:21 -070054 UniquePtr<RSA, RsaKey::RSA_Delete> rsa_key(RSA_new());
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060055 UniquePtr<EVP_PKEY, EVP_PKEY_Delete> pkey(EVP_PKEY_new());
Shawn Willden0cb69422015-05-26 08:31:37 -060056 if (exponent.get() == NULL || rsa_key.get() == NULL || pkey.get() == NULL)
57 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060058
59 if (!BN_set_word(exponent.get(), public_exponent) ||
Shawn Willden0cb69422015-05-26 08:31:37 -060060 !RSA_generate_key_ex(rsa_key.get(), key_size, exponent.get(), NULL /* callback */))
61 return TranslateLastOpenSslError();
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060062
Shawn Willden0cb69422015-05-26 08:31:37 -060063 if (EVP_PKEY_set1_RSA(pkey.get(), rsa_key.get()) != 1)
64 return TranslateLastOpenSslError();
65
66 KeymasterKeyBlob key_material;
67 keymaster_error_t error = EvpKeyToKeyMaterial(pkey.get(), &key_material);
68 if (error != KM_ERROR_OK)
69 return error;
70
71 return context_->CreateKeyBlob(authorizations, KM_ORIGIN_GENERATED, key_material, key_blob,
72 hw_enforced, sw_enforced);
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060073}
74
Shawn Willden0cb69422015-05-26 08:31:37 -060075keymaster_error_t RsaKeyFactory::ImportKey(const AuthorizationSet& key_description,
76 keymaster_key_format_t input_key_material_format,
77 const KeymasterKeyBlob& input_key_material,
78 KeymasterKeyBlob* output_key_blob,
79 AuthorizationSet* hw_enforced,
80 AuthorizationSet* sw_enforced) {
81 if (!output_key_blob || !hw_enforced || !sw_enforced)
82 return KM_ERROR_OUTPUT_PARAMETER_NULL;
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060083
Shawn Willden2beb6282015-05-20 16:36:24 -060084 AuthorizationSet authorizations;
85 uint64_t public_exponent;
86 uint32_t key_size;
Shawn Willden0cb69422015-05-26 08:31:37 -060087 keymaster_error_t error =
Shawn Willden2beb6282015-05-20 16:36:24 -060088 UpdateImportKeyDescription(key_description, input_key_material_format, input_key_material,
89 &authorizations, &public_exponent, &key_size);
90 if (error != KM_ERROR_OK)
91 return error;
92 return context_->CreateKeyBlob(authorizations, KM_ORIGIN_IMPORTED, input_key_material,
93 output_key_blob, hw_enforced, sw_enforced);
94}
95
96keymaster_error_t RsaKeyFactory::UpdateImportKeyDescription(const AuthorizationSet& key_description,
97 keymaster_key_format_t key_format,
98 const KeymasterKeyBlob& key_material,
99 AuthorizationSet* updated_description,
100 uint64_t* public_exponent,
101 uint32_t* key_size) {
102 if (!updated_description || !public_exponent || !key_size)
103 return KM_ERROR_OUTPUT_PARAMETER_NULL;
104
105 UniquePtr<EVP_PKEY, EVP_PKEY_Delete> pkey;
106 keymaster_error_t error = KeyMaterialToEvpKey(key_format, key_material, &pkey);
Shawn Willden0cb69422015-05-26 08:31:37 -0600107 if (error != KM_ERROR_OK)
108 return error;
Shawn Willdena278f612014-12-23 11:22:21 -0700109
110 UniquePtr<RSA, RsaKey::RSA_Delete> rsa_key(EVP_PKEY_get1_RSA(pkey.get()));
Shawn Willden0cb69422015-05-26 08:31:37 -0600111 if (!rsa_key.get())
112 return TranslateLastOpenSslError();
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600113
Shawn Willden2beb6282015-05-20 16:36:24 -0600114 updated_description->Reinitialize(key_description);
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600115
Shawn Willden2beb6282015-05-20 16:36:24 -0600116 *public_exponent = BN_get_word(rsa_key->e);
117 if (*public_exponent == 0xffffffffL)
118 return KM_ERROR_INVALID_KEY_BLOB;
119 if (!updated_description->GetTagValue(TAG_RSA_PUBLIC_EXPONENT, public_exponent))
120 updated_description->push_back(TAG_RSA_PUBLIC_EXPONENT, *public_exponent);
121 if (*public_exponent != BN_get_word(rsa_key->e))
122 return KM_ERROR_IMPORT_PARAMETER_MISMATCH;
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600123
Shawn Willden2beb6282015-05-20 16:36:24 -0600124 *key_size = RSA_size(rsa_key.get()) * 8;
125 if (!updated_description->GetTagValue(TAG_KEY_SIZE, key_size))
126 updated_description->push_back(TAG_KEY_SIZE, *key_size);
127 if (RSA_size(rsa_key.get()) * 8 != (openssl_size_t)*key_size)
128 return KM_ERROR_IMPORT_PARAMETER_MISMATCH;
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600129
Shawn Willden2beb6282015-05-20 16:36:24 -0600130 keymaster_algorithm_t algorithm = KM_ALGORITHM_RSA;
131 if (!updated_description->GetTagValue(TAG_ALGORITHM, &algorithm))
132 updated_description->push_back(TAG_ALGORITHM, KM_ALGORITHM_RSA);
133 if (algorithm != KM_ALGORITHM_RSA)
134 return KM_ERROR_IMPORT_PARAMETER_MISMATCH;
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600135
Shawn Willden2beb6282015-05-20 16:36:24 -0600136 return KM_ERROR_OK;
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600137}
138
Shawn Willden0cb69422015-05-26 08:31:37 -0600139keymaster_error_t RsaKeyFactory::CreateEmptyKey(const AuthorizationSet& hw_enforced,
140 const AuthorizationSet& sw_enforced,
141 UniquePtr<AsymmetricKey>* key) {
142 keymaster_error_t error;
143 key->reset(new RsaKey(hw_enforced, sw_enforced, &error));
144 if (!key->get())
145 error = KM_ERROR_MEMORY_ALLOCATION_FAILED;
146 return error;
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600147}
148
149bool RsaKey::EvpToInternal(const EVP_PKEY* pkey) {
150 rsa_key_.reset(EVP_PKEY_get1_RSA(const_cast<EVP_PKEY*>(pkey)));
151 return rsa_key_.get() != NULL;
152}
153
154bool RsaKey::InternalToEvp(EVP_PKEY* pkey) const {
155 return EVP_PKEY_set1_RSA(pkey, rsa_key_.get()) == 1;
156}
157
Shawn Willden4200f212014-12-02 07:01:21 -0700158bool RsaKey::SupportedMode(keymaster_purpose_t purpose, keymaster_padding_t padding) {
159 switch (purpose) {
160 case KM_PURPOSE_SIGN:
161 case KM_PURPOSE_VERIFY:
Shawn Willdenf90f2352014-12-18 23:01:15 -0700162 return padding == KM_PAD_NONE || padding == KM_PAD_RSA_PSS ||
163 padding == KM_PAD_RSA_PKCS1_1_5_SIGN;
Shawn Willden4200f212014-12-02 07:01:21 -0700164 break;
165 case KM_PURPOSE_ENCRYPT:
166 case KM_PURPOSE_DECRYPT:
167 return padding == KM_PAD_RSA_OAEP || padding == KM_PAD_RSA_PKCS1_1_5_ENCRYPT;
168 break;
169 };
170 return false;
171}
172
173bool RsaKey::SupportedMode(keymaster_purpose_t purpose, keymaster_digest_t digest) {
174 switch (purpose) {
175 case KM_PURPOSE_SIGN:
176 case KM_PURPOSE_VERIFY:
Shawn Willden61902362014-12-18 10:33:24 -0700177 return digest == KM_DIGEST_NONE || digest == KM_DIGEST_SHA_2_256;
Shawn Willden4200f212014-12-02 07:01:21 -0700178 break;
179 case KM_PURPOSE_ENCRYPT:
180 case KM_PURPOSE_DECRYPT:
181 /* Don't care */
182 break;
183 };
184 return true;
185}
186
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600187} // namespace keymaster