Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 1 | /* |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 2 | * Copyright (c) 2020-2021, Arm Limited and Contributors. All rights reserved. |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 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> |
Julian Hall | 99a57e3 | 2021-07-28 14:18:50 +0100 | [diff] [blame] | 12 | #include <service/common/client/service_client.h> |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 13 | |
julhal01 | 734dbad | 2020-12-21 10:27:41 +0000 | [diff] [blame] | 14 | /* |
| 15 | * Provides a client interface for accessing an instance of the Crypto service |
| 16 | * using a C++ version of the PSA Crypto API. |
| 17 | */ |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 18 | class crypto_client |
| 19 | { |
| 20 | public: |
Julian Hall | 7a70340 | 2021-08-04 09:20:43 +0100 | [diff] [blame] | 21 | virtual ~crypto_client(); |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 22 | |
Julian Hall | 7a70340 | 2021-08-04 09:20:43 +0100 | [diff] [blame] | 23 | int err_rpc_status() const; |
Julian Hall | c6e7a8a | 2021-08-09 14:28:13 +0100 | [diff] [blame] | 24 | struct service_info get_service_info() const; |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 25 | |
Julian Hall | 7a70340 | 2021-08-04 09:20:43 +0100 | [diff] [blame] | 26 | /* Key lifecycle methods */ |
| 27 | virtual psa_status_t generate_key( |
| 28 | const psa_key_attributes_t *attributes, |
| 29 | psa_key_id_t *id) = 0; |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 30 | |
Julian Hall | 7a70340 | 2021-08-04 09:20:43 +0100 | [diff] [blame] | 31 | virtual psa_status_t destroy_key( |
| 32 | psa_key_id_t id) = 0; |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 33 | |
Julian Hall | 7a70340 | 2021-08-04 09:20:43 +0100 | [diff] [blame] | 34 | virtual psa_status_t import_key( |
| 35 | const psa_key_attributes_t *attributes, |
| 36 | const uint8_t *data, size_t data_length, |
| 37 | psa_key_id_t *id) = 0; |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 38 | |
Julian Hall | 7a70340 | 2021-08-04 09:20:43 +0100 | [diff] [blame] | 39 | virtual psa_status_t copy_key( |
| 40 | psa_key_id_t source_key, |
| 41 | const psa_key_attributes_t *attributes, |
| 42 | psa_key_id_t *target_key) = 0; |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 43 | |
Julian Hall | 7a70340 | 2021-08-04 09:20:43 +0100 | [diff] [blame] | 44 | virtual psa_status_t purge_key( |
| 45 | psa_key_id_t id) = 0; |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 46 | |
Julian Hall | 7a70340 | 2021-08-04 09:20:43 +0100 | [diff] [blame] | 47 | virtual psa_status_t get_key_attributes( |
| 48 | psa_key_id_t id, |
| 49 | psa_key_attributes_t *attributes) = 0; |
| 50 | |
| 51 | /* Key export methods */ |
| 52 | virtual psa_status_t export_key( |
| 53 | psa_key_id_t id, |
| 54 | uint8_t *data, size_t data_size, size_t *data_length) = 0; |
| 55 | |
| 56 | virtual psa_status_t export_public_key( |
| 57 | psa_key_id_t id, |
| 58 | uint8_t *data, size_t data_size, size_t *data_length) = 0; |
| 59 | |
| 60 | /* Sign/verify methods */ |
| 61 | virtual psa_status_t sign_hash( |
| 62 | psa_key_id_t id, |
| 63 | psa_algorithm_t alg, |
| 64 | const uint8_t *hash, size_t hash_length, |
| 65 | uint8_t *signature, size_t signature_size, size_t *signature_length) = 0; |
| 66 | |
| 67 | virtual psa_status_t verify_hash( |
| 68 | psa_key_id_t id, |
| 69 | psa_algorithm_t alg, |
| 70 | const uint8_t *hash, size_t hash_length, |
| 71 | const uint8_t *signature, size_t signature_length) = 0; |
| 72 | |
| 73 | /* Asymmetric encrypt/decrypt */ |
| 74 | virtual psa_status_t asymmetric_encrypt( |
| 75 | psa_key_id_t id, |
| 76 | psa_algorithm_t alg, |
| 77 | const uint8_t *input, size_t input_length, |
| 78 | const uint8_t *salt, size_t salt_length, |
| 79 | uint8_t *output, size_t output_size, size_t *output_length) = 0; |
| 80 | |
| 81 | virtual psa_status_t asymmetric_decrypt( |
| 82 | psa_key_id_t id, |
| 83 | psa_algorithm_t alg, |
| 84 | const uint8_t *input, size_t input_length, |
| 85 | const uint8_t *salt, size_t salt_length, |
| 86 | uint8_t *output, size_t output_size, size_t *output_length) = 0; |
| 87 | |
| 88 | /* Random number generation */ |
| 89 | virtual psa_status_t generate_random( |
| 90 | uint8_t *output, size_t output_size) = 0; |
| 91 | |
| 92 | /* Hash methods */ |
Julian Hall | c6e7a8a | 2021-08-09 14:28:13 +0100 | [diff] [blame] | 93 | virtual size_t hash_max_update_size() const = 0; |
| 94 | |
Julian Hall | 7a70340 | 2021-08-04 09:20:43 +0100 | [diff] [blame] | 95 | virtual psa_status_t hash_setup( |
| 96 | uint32_t *op_handle, |
| 97 | psa_algorithm_t alg) = 0; |
| 98 | |
| 99 | virtual psa_status_t hash_update( |
| 100 | uint32_t op_handle, |
| 101 | const uint8_t *input, size_t input_length) = 0; |
| 102 | |
| 103 | virtual psa_status_t hash_finish( |
| 104 | uint32_t op_handle, |
| 105 | uint8_t *hash, size_t hash_size, size_t *hash_length) = 0; |
Julian Hall | f572896 | 2021-06-24 09:40:23 +0100 | [diff] [blame] | 106 | |
Julian Hall | c6e7a8a | 2021-08-09 14:28:13 +0100 | [diff] [blame] | 107 | virtual psa_status_t hash_abort( |
| 108 | uint32_t op_handle) = 0; |
| 109 | |
| 110 | virtual psa_status_t hash_verify( |
| 111 | uint32_t op_handle, |
| 112 | const uint8_t *hash, size_t hash_length) = 0; |
| 113 | |
| 114 | virtual psa_status_t hash_clone( |
| 115 | uint32_t source_op_handle, |
| 116 | uint32_t *target_op_handle) = 0; |
| 117 | |
Julian Hall | a652ad6 | 2021-08-10 12:05:46 +0100 | [diff] [blame^] | 118 | /* Cipher methods */ |
| 119 | virtual size_t cipher_max_update_size() const = 0; |
| 120 | |
| 121 | virtual psa_status_t cipher_encrypt_setup( |
| 122 | uint32_t *op_handle, |
| 123 | psa_key_id_t key, |
| 124 | psa_algorithm_t alg) = 0; |
| 125 | |
| 126 | virtual psa_status_t cipher_decrypt_setup( |
| 127 | uint32_t *op_handle, |
| 128 | psa_key_id_t key, |
| 129 | psa_algorithm_t alg) = 0; |
| 130 | |
| 131 | virtual psa_status_t cipher_generate_iv( |
| 132 | uint32_t op_handle, |
| 133 | uint8_t *iv, size_t iv_size, size_t *iv_length) = 0; |
| 134 | |
| 135 | virtual psa_status_t cipher_set_iv( |
| 136 | uint32_t op_handle, |
| 137 | const uint8_t *iv, size_t iv_length) = 0; |
| 138 | |
| 139 | virtual psa_status_t cipher_update( |
| 140 | uint32_t op_handle, |
| 141 | const uint8_t *input, size_t input_length, |
| 142 | uint8_t *output, size_t output_size, size_t *output_length) = 0; |
| 143 | |
| 144 | virtual psa_status_t cipher_finish( |
| 145 | uint32_t op_handle, |
| 146 | uint8_t *output, size_t output_size, size_t *output_length) = 0; |
| 147 | |
| 148 | virtual psa_status_t cipher_abort( |
| 149 | uint32_t op_handle) = 0; |
| 150 | |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 151 | protected: |
Julian Hall | 7a70340 | 2021-08-04 09:20:43 +0100 | [diff] [blame] | 152 | crypto_client(); |
| 153 | crypto_client(struct rpc_caller *caller); |
| 154 | void set_caller(struct rpc_caller *caller); |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 155 | |
Julian Hall | 7a70340 | 2021-08-04 09:20:43 +0100 | [diff] [blame] | 156 | struct service_client m_client; |
Julian Hall | c02fffb | 2020-11-23 18:22:06 +0100 | [diff] [blame] | 157 | }; |
| 158 | |
| 159 | #endif /* CRYPTO_CLIENT_H */ |