1 /*
2 * Copyright 2015 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_keymaster1_operation.h"
18
19 #include <memory>
20
21 #include <keymaster/android_keymaster_utils.h>
22
23 #include "openssl_err.h"
24 #include "openssl_utils.h"
25 #include "ec_keymaster1_key.h"
26
27 using std::unique_ptr;
28
29 namespace keymaster {
30
Begin(EVP_PKEY * ecdsa_key,const AuthorizationSet & input_params)31 keymaster_error_t EcdsaKeymaster1WrappedOperation::Begin(EVP_PKEY* ecdsa_key,
32 const AuthorizationSet& input_params) {
33 Keymaster1Engine::KeyData* key_data = engine_->GetData(ecdsa_key);
34 if (!key_data)
35 return KM_ERROR_UNKNOWN_ERROR;
36
37 // Copy the input params and substitute KM_DIGEST_NONE for whatever was specified. Also change
38 // KM_PAD_ECDSA_PSS and KM_PAD_OAEP to KM_PAD_NONE, if necessary. These are the params we'll
39 // pass
40 // to the hardware module. The regular Ecdsa*Operation classes will do software digesting and
41 // padding where we've told the HW not to.
42 //
43 // The reason we don't change KM_PAD_ECDSA_PKCS1_1_5_SIGN or KM_PAD_ECDSA_PKCS1_1_5_ENCRYPT to
44 // KM_PAD_NONE is because the hardware can to those padding modes, since they don't involve
45 // digesting.
46 //
47 // We also cache in the key the padding value that we expect to be passed to the engine crypto
48 // operation. This just allows us to double-check that the correct padding value is reaching
49 // that layer.
50 AuthorizationSet begin_params(input_params);
51 int pos = begin_params.find(TAG_DIGEST);
52 if (pos == -1)
53 return KM_ERROR_UNSUPPORTED_DIGEST;
54 begin_params[pos].enumerated = KM_DIGEST_NONE;
55
56 return engine_->device()->begin(engine_->device(), purpose_, &key_data->key_material,
57 &begin_params, nullptr /* out_params */, &operation_handle_);
58 }
59
60 keymaster_error_t
PrepareFinish(EVP_PKEY * ecdsa_key,const AuthorizationSet & input_params)61 EcdsaKeymaster1WrappedOperation::PrepareFinish(EVP_PKEY* ecdsa_key,
62 const AuthorizationSet& input_params) {
63 Keymaster1Engine::KeyData* key_data = engine_->GetData(ecdsa_key);
64 if (!key_data) {
65 LOG_E("Could not get extended key data... not a Keymaster1Engine key?", 0);
66 return KM_ERROR_UNKNOWN_ERROR;
67 }
68 key_data->op_handle = operation_handle_;
69 key_data->finish_params.Reinitialize(input_params);
70
71 return KM_ERROR_OK;
72 }
73
Abort()74 keymaster_error_t EcdsaKeymaster1WrappedOperation::Abort() {
75 return engine_->device()->abort(engine_->device(), operation_handle_);
76 }
77
GetError(EVP_PKEY * ecdsa_key)78 keymaster_error_t EcdsaKeymaster1WrappedOperation::GetError(EVP_PKEY* ecdsa_key) {
79 Keymaster1Engine::KeyData* key_data = engine_->GetData(ecdsa_key);
80 if (!key_data)
81 return KM_ERROR_UNKNOWN_ERROR;
82 return key_data->error;
83 }
84
GetEvpKey(const EcdsaKeymaster1Key & key,keymaster_error_t * error)85 static EVP_PKEY* GetEvpKey(const EcdsaKeymaster1Key& key, keymaster_error_t* error) {
86 if (!key.key()) {
87 *error = KM_ERROR_UNKNOWN_ERROR;
88 return nullptr;
89 }
90
91 UniquePtr<EVP_PKEY, EVP_PKEY_Delete> pkey(EVP_PKEY_new());
92 if (!key.InternalToEvp(pkey.get())) {
93 *error = KM_ERROR_UNKNOWN_ERROR;
94 return nullptr;
95 }
96 return pkey.release();
97 }
98
CreateOperation(const Key & key,const AuthorizationSet & begin_params,keymaster_error_t * error)99 Operation* EcdsaKeymaster1OperationFactory::CreateOperation(const Key& key,
100 const AuthorizationSet& begin_params,
101 keymaster_error_t* error) {
102 keymaster_digest_t digest;
103 if (!GetAndValidateDigest(begin_params, key, &digest, error))
104 return nullptr;
105
106 const EcdsaKeymaster1Key& ecdsa_km1_key(static_cast<const EcdsaKeymaster1Key&>(key));
107 unique_ptr<EVP_PKEY, EVP_PKEY_Delete> ecdsa(GetEvpKey(ecdsa_km1_key, error));
108 if (!ecdsa)
109 return nullptr;
110
111 switch (purpose_) {
112 case KM_PURPOSE_SIGN:
113 return new EcdsaKeymaster1Operation<EcdsaSignOperation>(digest, ecdsa.release(), engine_);
114 default:
115 LOG_E(
116 "Bug: Pubkey operation requested. Those should be handled by normal ECDSA operations.",
117 0);
118 *error = KM_ERROR_UNSUPPORTED_PURPOSE;
119 return nullptr;
120 }
121 }
122
123 static const keymaster_digest_t supported_digests[] = {
124 KM_DIGEST_NONE, KM_DIGEST_MD5, KM_DIGEST_SHA1, KM_DIGEST_SHA_2_224,
125 KM_DIGEST_SHA_2_256, KM_DIGEST_SHA_2_384, KM_DIGEST_SHA_2_512};
126
127 const keymaster_digest_t*
SupportedDigests(size_t * digest_count) const128 EcdsaKeymaster1OperationFactory::SupportedDigests(size_t* digest_count) const {
129 *digest_count = array_length(supported_digests);
130 return supported_digests;
131 }
132
133 const keymaster_padding_t*
SupportedPaddingModes(size_t * padding_mode_count) const134 EcdsaKeymaster1OperationFactory::SupportedPaddingModes(size_t* padding_mode_count) const {
135 *padding_mode_count = 0;
136 return nullptr;
137 }
138
139 } // namespace keymaster
140