Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2020, Arm Limited and Contributors. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #ifndef CRYPTO_CLIENT_H |
| 8 | #define CRYPTO_CLIENT_H |
| 9 | |
| 10 | #include <cstdint> |
| 11 | #include <psa/crypto.h> |
| 12 | #include <service/crypto/protobuf/key_attributes.pb.h> |
| 13 | |
| 14 | |
| 15 | struct rpc_caller; |
| 16 | |
| 17 | /** Provides a client interface for accessing an instance of the PSA Crypto service. |
| 18 | **/ |
| 19 | class crypto_client |
| 20 | { |
| 21 | public: |
| 22 | crypto_client(struct rpc_caller *caller); |
| 23 | virtual ~crypto_client(); |
| 24 | |
| 25 | int err_rpc_status() const; |
| 26 | |
| 27 | /* Key lifecycle methods */ |
| 28 | psa_status_t generate_key(const psa_key_attributes_t *attributes, psa_key_handle_t *handle); |
| 29 | psa_status_t destroy_key(psa_key_handle_t handle); |
| 30 | psa_status_t open_key(psa_key_id_t id, psa_key_handle_t *handle); |
| 31 | psa_status_t close_key(psa_key_handle_t handle); |
| 32 | psa_status_t import_key(const psa_key_attributes_t *attributes, |
| 33 | const uint8_t *data, size_t data_length, psa_key_handle_t *handle); |
| 34 | |
| 35 | /* Key export methods */ |
| 36 | psa_status_t export_key(psa_key_handle_t handle, |
| 37 | uint8_t *data, size_t data_size, |
| 38 | size_t *data_length); |
| 39 | psa_status_t export_public_key(psa_key_handle_t handle, |
| 40 | uint8_t *data, size_t data_size, size_t *data_length); |
| 41 | |
| 42 | /* Sign/verify methods */ |
| 43 | psa_status_t sign_hash(psa_key_handle_t handle, psa_algorithm_t alg, |
| 44 | const uint8_t *hash, size_t hash_length, |
| 45 | uint8_t *signature, size_t signature_size, size_t *signature_length); |
| 46 | psa_status_t verify_hash(psa_key_handle_t handle, psa_algorithm_t alg, |
| 47 | const uint8_t *hash, size_t hash_length, |
| 48 | const uint8_t *signature, size_t signature_length); |
| 49 | |
| 50 | /* Asymmetric encrypt/decrypt */ |
| 51 | psa_status_t asymmetric_encrypt(psa_key_handle_t handle, psa_algorithm_t alg, |
| 52 | const uint8_t *input, size_t input_length, |
| 53 | const uint8_t *salt, size_t salt_length, |
| 54 | uint8_t *output, size_t output_size, size_t *output_length); |
| 55 | psa_status_t asymmetric_decrypt(psa_key_handle_t handle, psa_algorithm_t alg, |
| 56 | const uint8_t *input, size_t input_length, |
| 57 | const uint8_t *salt, size_t salt_length, |
| 58 | uint8_t *output, size_t output_size, size_t *output_length); |
| 59 | |
| 60 | /* Random number generation */ |
| 61 | psa_status_t generate_random(uint8_t *output, size_t output_size); |
| 62 | |
| 63 | protected: |
| 64 | crypto_client(); |
| 65 | void set_caller(struct rpc_caller *caller) {m_caller = caller;} |
| 66 | |
| 67 | private: |
| 68 | |
| 69 | void translate_key_attributes(ts_crypto_KeyAttributes &proto_attributes, |
| 70 | const psa_key_attributes_t &psa_attributes); |
| 71 | |
| 72 | struct rpc_caller *m_caller; |
| 73 | int m_err_rpc_status; |
| 74 | }; |
| 75 | |
| 76 | #endif /* CRYPTO_CLIENT_H */ |