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 #ifndef SYSTEM_KEYMASTER_ECDSA_KEYMASTER1_OPERATION_H_ 18 #define SYSTEM_KEYMASTER_ECDSA_KEYMASTER1_OPERATION_H_ 19 20 #include <openssl/evp.h> 21 22 #include <hardware/keymaster1.h> 23 24 #include <keymaster/android_keymaster_utils.h> 25 #include <keymaster/km_openssl/ecdsa_operation.h> 26 #include <keymaster/legacy_support/keymaster1_engine.h> 27 28 namespace keymaster { 29 30 class EcdsaKeymaster1WrappedOperation { 31 public: EcdsaKeymaster1WrappedOperation(keymaster_purpose_t purpose,const Keymaster1Engine * engine)32 EcdsaKeymaster1WrappedOperation(keymaster_purpose_t purpose, const Keymaster1Engine* engine) 33 : purpose_(purpose), operation_handle_(0), engine_(engine) {} ~EcdsaKeymaster1WrappedOperation()34 ~EcdsaKeymaster1WrappedOperation() { 35 if (operation_handle_) 36 Abort(); 37 } 38 39 keymaster_error_t Begin(EVP_PKEY* ecdsa_key, const AuthorizationSet& input_params); 40 keymaster_error_t PrepareFinish(EVP_PKEY* ecdsa_key, const AuthorizationSet& input_params); 41 keymaster_error_t Abort(); 42 43 keymaster_error_t GetError(EVP_PKEY* ecdsa_key); 44 GetOperationHandle()45 keymaster_operation_handle_t GetOperationHandle() const { return operation_handle_; } 46 protected: 47 keymaster_purpose_t purpose_; 48 keymaster_operation_handle_t operation_handle_; 49 const Keymaster1Engine* engine_; 50 }; 51 52 template <typename BaseOperation> class EcdsaKeymaster1Operation : public BaseOperation { 53 typedef BaseOperation super; 54 55 public: EcdsaKeymaster1Operation(AuthorizationSet && hw_enforced,AuthorizationSet && sw_enforced,keymaster_digest_t digest,EVP_PKEY * key,const Keymaster1Engine * engine)56 EcdsaKeymaster1Operation(AuthorizationSet&& hw_enforced, AuthorizationSet&& sw_enforced, 57 keymaster_digest_t digest, EVP_PKEY* key, 58 const Keymaster1Engine* engine) 59 : BaseOperation(move(hw_enforced), move(sw_enforced), digest, key), 60 wrapped_operation_(super::purpose(), engine) { 61 // Shouldn't be instantiated for public key operations. 62 assert(super::purpose() != KM_PURPOSE_VERIFY); 63 assert(super::purpose() != KM_PURPOSE_ENCRYPT); 64 } 65 Begin(const AuthorizationSet & input_params,AuthorizationSet * output_params)66 keymaster_error_t Begin(const AuthorizationSet& input_params, 67 AuthorizationSet* output_params) override { 68 keymaster_error_t error = wrapped_operation_.Begin(super::ecdsa_key_, input_params); 69 if (error != KM_ERROR_OK) 70 return error; 71 return super::Begin(input_params, output_params); 72 } 73 Finish(const AuthorizationSet & input_params,const Buffer & input,const Buffer & signature,AuthorizationSet * output_params,Buffer * output)74 keymaster_error_t Finish(const AuthorizationSet& input_params, const Buffer& input, 75 const Buffer& signature, AuthorizationSet* output_params, 76 Buffer* output) override { 77 keymaster_error_t error = wrapped_operation_.PrepareFinish(super::ecdsa_key_, input_params); 78 if (error != KM_ERROR_OK) 79 return error; 80 error = super::Finish(input_params, input, signature, output_params, output); 81 if (wrapped_operation_.GetError(super::ecdsa_key_) != KM_ERROR_OK) 82 error = wrapped_operation_.GetError(super::ecdsa_key_); 83 return error; 84 } 85 Abort()86 keymaster_error_t Abort() override { 87 keymaster_error_t error = wrapped_operation_.Abort(); 88 if (error != KM_ERROR_OK) 89 return error; 90 return super::Abort(); 91 } 92 operation_handle()93 keymaster_operation_handle_t operation_handle() const override { 94 return wrapped_operation_.GetOperationHandle(); 95 } 96 private: 97 EcdsaKeymaster1WrappedOperation wrapped_operation_; 98 }; 99 100 /** 101 * Factory that produces EcdsaKeymaster1Operations. This is instantiated and 102 * provided by EcdsaKeymaster1KeyFactory. 103 */ 104 class EcdsaKeymaster1OperationFactory : public OperationFactory { 105 public: EcdsaKeymaster1OperationFactory(keymaster_purpose_t purpose,const Keymaster1Engine * engine)106 EcdsaKeymaster1OperationFactory(keymaster_purpose_t purpose, const Keymaster1Engine* engine) 107 : purpose_(purpose), engine_(engine) {} registry_key()108 KeyType registry_key() const override { return KeyType(KM_ALGORITHM_EC, purpose_); } 109 110 OperationPtr CreateOperation(Key&& key, const AuthorizationSet& begin_params, 111 keymaster_error_t* error) override; 112 113 const keymaster_digest_t* SupportedDigests(size_t* digest_count) const override; 114 const keymaster_padding_t* SupportedPaddingModes(size_t* padding_mode_count) const override; 115 116 private: 117 keymaster_purpose_t purpose_; 118 const Keymaster1Engine* engine_; 119 }; 120 121 } // namespace keymaster 122 123 #endif // SYSTEM_KEYMASTER_ECDSA_KEYMASTER1_OPERATION_H_ 124