blob: 293d51ec12ce282ad45f5ecf8f50d207c121771d [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 Willden0cb69422015-05-26 08:31:37 -060084 UniquePtr<EVP_PKEY, EVP_PKEY_Delete> pkey;
85 keymaster_error_t error =
86 KeyMaterialToEvpKey(input_key_material_format, input_key_material, &pkey);
87 if (error != KM_ERROR_OK)
88 return error;
Shawn Willdena278f612014-12-23 11:22:21 -070089
90 UniquePtr<RSA, RsaKey::RSA_Delete> rsa_key(EVP_PKEY_get1_RSA(pkey.get()));
Shawn Willden0cb69422015-05-26 08:31:37 -060091 if (!rsa_key.get())
92 return TranslateLastOpenSslError();
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060093
94 AuthorizationSet authorizations(key_description);
95
96 uint64_t public_exponent;
97 if (authorizations.GetTagValue(TAG_RSA_PUBLIC_EXPONENT, &public_exponent)) {
98 // public_exponent specified, make sure it matches the key
99 UniquePtr<BIGNUM, BIGNUM_Delete> public_exponent_bn(BN_new());
100 if (!BN_set_word(public_exponent_bn.get(), public_exponent))
Shawn Willden0cb69422015-05-26 08:31:37 -0600101 return KM_ERROR_UNKNOWN_ERROR;
102 if (BN_cmp(public_exponent_bn.get(), rsa_key->e) != 0)
103 return KM_ERROR_IMPORT_PARAMETER_MISMATCH;
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600104 } else {
105 // public_exponent not specified, use the one from the key.
106 public_exponent = BN_get_word(rsa_key->e);
Shawn Willden0cb69422015-05-26 08:31:37 -0600107 if (public_exponent == 0xffffffffL)
108 return KM_ERROR_IMPORT_PARAMETER_MISMATCH;
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600109 authorizations.push_back(TAG_RSA_PUBLIC_EXPONENT, public_exponent);
110 }
111
112 uint32_t key_size;
113 if (authorizations.GetTagValue(TAG_KEY_SIZE, &key_size)) {
114 // key_size specified, make sure it matches the key.
Shawn Willden0cb69422015-05-26 08:31:37 -0600115 if (RSA_size(rsa_key.get()) * 8 != (openssl_size_t)key_size)
116 return KM_ERROR_IMPORT_PARAMETER_MISMATCH;
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600117 } else {
118 key_size = RSA_size(rsa_key.get()) * 8;
119 authorizations.push_back(TAG_KEY_SIZE, key_size);
120 }
121
122 keymaster_algorithm_t algorithm;
123 if (authorizations.GetTagValue(TAG_ALGORITHM, &algorithm)) {
Shawn Willden0cb69422015-05-26 08:31:37 -0600124 if (algorithm != KM_ALGORITHM_RSA)
125 return KM_ERROR_IMPORT_PARAMETER_MISMATCH;
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600126 } else {
127 authorizations.push_back(TAG_ALGORITHM, KM_ALGORITHM_RSA);
128 }
129
Shawn Willden0cb69422015-05-26 08:31:37 -0600130 return context_->CreateKeyBlob(authorizations, KM_ORIGIN_IMPORTED, input_key_material,
131 output_key_blob, hw_enforced, sw_enforced);
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600132}
133
Shawn Willden0cb69422015-05-26 08:31:37 -0600134keymaster_error_t RsaKeyFactory::CreateEmptyKey(const AuthorizationSet& hw_enforced,
135 const AuthorizationSet& sw_enforced,
136 UniquePtr<AsymmetricKey>* key) {
137 keymaster_error_t error;
138 key->reset(new RsaKey(hw_enforced, sw_enforced, &error));
139 if (!key->get())
140 error = KM_ERROR_MEMORY_ALLOCATION_FAILED;
141 return error;
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600142}
143
144bool RsaKey::EvpToInternal(const EVP_PKEY* pkey) {
145 rsa_key_.reset(EVP_PKEY_get1_RSA(const_cast<EVP_PKEY*>(pkey)));
146 return rsa_key_.get() != NULL;
147}
148
149bool RsaKey::InternalToEvp(EVP_PKEY* pkey) const {
150 return EVP_PKEY_set1_RSA(pkey, rsa_key_.get()) == 1;
151}
152
Shawn Willden4200f212014-12-02 07:01:21 -0700153bool RsaKey::SupportedMode(keymaster_purpose_t purpose, keymaster_padding_t padding) {
154 switch (purpose) {
155 case KM_PURPOSE_SIGN:
156 case KM_PURPOSE_VERIFY:
Shawn Willdenf90f2352014-12-18 23:01:15 -0700157 return padding == KM_PAD_NONE || padding == KM_PAD_RSA_PSS ||
158 padding == KM_PAD_RSA_PKCS1_1_5_SIGN;
Shawn Willden4200f212014-12-02 07:01:21 -0700159 break;
160 case KM_PURPOSE_ENCRYPT:
161 case KM_PURPOSE_DECRYPT:
162 return padding == KM_PAD_RSA_OAEP || padding == KM_PAD_RSA_PKCS1_1_5_ENCRYPT;
163 break;
164 };
165 return false;
166}
167
168bool RsaKey::SupportedMode(keymaster_purpose_t purpose, keymaster_digest_t digest) {
169 switch (purpose) {
170 case KM_PURPOSE_SIGN:
171 case KM_PURPOSE_VERIFY:
Shawn Willden61902362014-12-18 10:33:24 -0700172 return digest == KM_DIGEST_NONE || digest == KM_DIGEST_SHA_2_256;
Shawn Willden4200f212014-12-02 07:01:21 -0700173 break;
174 case KM_PURPOSE_ENCRYPT:
175 case KM_PURPOSE_DECRYPT:
176 /* Don't care */
177 break;
178 };
179 return true;
180}
181
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600182} // namespace keymaster