1 /* 2 * Copyright 2021 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_ECDH_OPERATION_H_ 18 #define SYSTEM_KEYMASTER_ECDH_OPERATION_H_ 19 20 #include <utility> 21 22 #include <openssl/ec.h> 23 #include <openssl/evp.h> 24 25 #include <keymaster/UniquePtr.h> 26 27 #include <keymaster/key.h> 28 #include <keymaster/km_openssl/openssl_utils.h> 29 #include <keymaster/operation.h> 30 31 namespace keymaster { 32 33 class EcdhOperation : public Operation { 34 public: EcdhOperation(AuthorizationSet && hw_enforced,AuthorizationSet && sw_enforced,EVP_PKEY * key)35 EcdhOperation(AuthorizationSet&& hw_enforced, AuthorizationSet&& sw_enforced, EVP_PKEY* key) 36 : Operation(KM_PURPOSE_AGREE_KEY, std::move(hw_enforced), std::move(sw_enforced)), 37 ecdh_key_(key) {} 38 Abort()39 keymaster_error_t Abort() override { return KM_ERROR_OK; } 40 41 keymaster_error_t Begin(const AuthorizationSet& input_params, 42 AuthorizationSet* output_params) override; 43 keymaster_error_t Update(const AuthorizationSet& additional_params, const Buffer& input, 44 AuthorizationSet* output_params, Buffer* output, 45 size_t* input_consumed) override; 46 keymaster_error_t Finish(const AuthorizationSet& additional_params, const Buffer& input, 47 const Buffer& signature, AuthorizationSet* output_params, 48 Buffer* output) override; 49 50 protected: 51 EVP_PKEY_Ptr ecdh_key_; 52 }; 53 54 class X25519Operation : public EcdhOperation { 55 public: X25519Operation(AuthorizationSet && hw_enforced,AuthorizationSet && sw_enforced,EVP_PKEY * key)56 X25519Operation(AuthorizationSet&& hw_enforced, AuthorizationSet&& sw_enforced, EVP_PKEY* key) 57 : EcdhOperation(std::move(hw_enforced), std::move(sw_enforced), key) {} 58 59 keymaster_error_t Finish(const AuthorizationSet& additional_params, const Buffer& input, 60 const Buffer& signature, AuthorizationSet* output_params, 61 Buffer* output) override; 62 }; 63 64 class EcdhOperationFactory : public OperationFactory { 65 private: registry_key()66 KeyType registry_key() const override { return KeyType(KM_ALGORITHM_EC, KM_PURPOSE_AGREE_KEY); } 67 OperationPtr CreateOperation(Key&& key, const AuthorizationSet& begin_params, 68 keymaster_error_t* error) override; 69 }; 70 71 } // namespace keymaster 72 73 #endif // SYSTEM_KEYMASTER_ECDH_OPERATION_H_ 74