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