1d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden/* 2d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden * Copyright 2015 The Android Open Source Project 3d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden * 4d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden * Licensed under the Apache License, Version 2.0 (the "License"); 5d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden * you may not use this file except in compliance with the License. 6d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden * You may obtain a copy of the License at 7d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden * 8d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden * http://www.apache.org/licenses/LICENSE-2.0 9d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden * 10d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden * Unless required by applicable law or agreed to in writing, software 11d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden * distributed under the License is distributed on an "AS IS" BASIS, 12d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden * See the License for the specific language governing permissions and 14d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden * limitations under the License. 15d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden */ 16d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden 17d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden#ifndef SYSTEM_KEYMASTER_ECDSA_KEYMASTER1_OPERATION_H_ 18d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden#define SYSTEM_KEYMASTER_ECDSA_KEYMASTER1_OPERATION_H_ 19d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden 20d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden#include <openssl/evp.h> 21d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden 22d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden#include <hardware/keymaster1.h> 23d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden#include <keymaster/android_keymaster_utils.h> 24d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden 25d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden#include "ecdsa_operation.h" 26d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden#include "keymaster1_engine.h" 27d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden 28d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willdennamespace keymaster { 29d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden 30d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willdenclass EcdsaKeymaster1WrappedOperation { 31d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden public: 32d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden EcdsaKeymaster1WrappedOperation(keymaster_purpose_t purpose, const Keymaster1Engine* engine) 33d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden : purpose_(purpose), operation_handle_(0), engine_(engine) {} 34d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden ~EcdsaKeymaster1WrappedOperation() { 35d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden if (operation_handle_) 36d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden Abort(); 37d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden } 38d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden 39d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden keymaster_error_t Begin(EVP_PKEY* ecdsa_key, const AuthorizationSet& input_params); 40d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden keymaster_error_t PrepareFinish(EVP_PKEY* ecdsa_key, const AuthorizationSet& input_params); 41d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden void Finish() { operation_handle_ = 0; } 42d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden keymaster_error_t Abort(); 43d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden 44d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden keymaster_error_t GetError(EVP_PKEY* ecdsa_key); 45d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden 46d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden protected: 47d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden keymaster_purpose_t purpose_; 48d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden keymaster_operation_handle_t operation_handle_; 49d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden const Keymaster1Engine* engine_; 50d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden}; 51d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden 52d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willdentemplate <typename BaseOperation> class EcdsaKeymaster1Operation : public BaseOperation { 53d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden typedef BaseOperation super; 54d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden 55d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden public: 56d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden EcdsaKeymaster1Operation(keymaster_digest_t digest, EVP_PKEY* key, 57d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden const Keymaster1Engine* engine) 58d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden : BaseOperation(digest, key), wrapped_operation_(super::purpose(), engine) { 59d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden // Shouldn't be instantiated for public key operations. 60d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden assert(super::purpose() != KM_PURPOSE_VERIFY); 61d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden assert(super::purpose() != KM_PURPOSE_ENCRYPT); 62d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden } 63d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden 64d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden keymaster_error_t Begin(const AuthorizationSet& input_params, 65d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden AuthorizationSet* output_params) override { 66d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden keymaster_error_t error = wrapped_operation_.Begin(super::ecdsa_key_, input_params); 67d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden if (error != KM_ERROR_OK) 68d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden return error; 69d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden return super::Begin(input_params, output_params); 70d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden } 71d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden 72d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden keymaster_error_t Finish(const AuthorizationSet& input_params, const Buffer& signature, 73d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden AuthorizationSet* output_params, Buffer* output) override { 74d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden keymaster_error_t error = wrapped_operation_.PrepareFinish(super::ecdsa_key_, input_params); 75d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden if (error != KM_ERROR_OK) 76d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden return error; 77d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden error = super::Finish(input_params, signature, output_params, output); 78d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden if (wrapped_operation_.GetError(super::ecdsa_key_) != KM_ERROR_OK) 79d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden error = wrapped_operation_.GetError(super::ecdsa_key_); 80d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden if (error == KM_ERROR_OK) 81d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden wrapped_operation_.Finish(); 82d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden return error; 83d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden } 84d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden 85d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden keymaster_error_t Abort() override { 86d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden keymaster_error_t error = wrapped_operation_.Abort(); 87d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden if (error != KM_ERROR_OK) 88d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden return error; 89d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden return super::Abort(); 90d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden } 91d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden 92d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden private: 93d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden EcdsaKeymaster1WrappedOperation wrapped_operation_; 94d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden}; 95d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden 96d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden/** 97d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden * Factory that produces EcdsaKeymaster1Operations. This is instantiated and 98d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden * provided by EcdsaKeymaster1KeyFactory. 99d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden */ 100d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willdenclass EcdsaKeymaster1OperationFactory : public OperationFactory { 101d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden public: 102d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden EcdsaKeymaster1OperationFactory(keymaster_purpose_t purpose, const Keymaster1Engine* engine) 103d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden : purpose_(purpose), engine_(engine) {} 104d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden KeyType registry_key() const override { return KeyType(KM_ALGORITHM_EC, purpose_); } 105d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden 106d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden Operation* CreateOperation(const Key& key, const AuthorizationSet& begin_params, 107d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden keymaster_error_t* error) override; 108d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden 109d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden const keymaster_digest_t* SupportedDigests(size_t* digest_count) const override; 110d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden const keymaster_padding_t* SupportedPaddingModes(size_t* padding_mode_count) const override; 111d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden 112d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden private: 113d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden keymaster_purpose_t purpose_; 114d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden const Keymaster1Engine* engine_; 115d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden}; 116d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden 117d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden} // namespace keymaster 118d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden 119d599b15c0693950bdc72fb867872044fdc484ef5Shawn Willden#endif // SYSTEM_KEYMASTER_ECDSA_KEYMASTER1_OPERATION_H_ 120