aboutsummaryrefslogtreecommitdiff
path: root/components/service/crypto/client/cpp/crypto_client.h
blob: 68d80ee58a8f62cf1a1a88b3b116ecd860737a3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 * Copyright (c) 2020-2021, Arm Limited and Contributors. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#ifndef CRYPTO_CLIENT_H
#define CRYPTO_CLIENT_H

#include <cstdint>
#include <psa/crypto.h>
#include <service/common/client/service_client.h>

/*
 * Provides a client interface for accessing an instance of the Crypto service
 * using a C++ version of the PSA Crypto API.
 */
class crypto_client
{
public:
	virtual ~crypto_client();

	int err_rpc_status() const;
	struct service_info get_service_info() const;

	/* Key lifecycle methods */
	virtual psa_status_t generate_key(
		const psa_key_attributes_t *attributes,
		psa_key_id_t *id) = 0;

	virtual psa_status_t destroy_key(
		psa_key_id_t id) = 0;

	virtual psa_status_t import_key(
		const psa_key_attributes_t *attributes,
		const uint8_t *data, size_t data_length,
		psa_key_id_t *id) = 0;

	virtual psa_status_t copy_key(
		psa_key_id_t source_key,
		const psa_key_attributes_t *attributes,
		psa_key_id_t *target_key) = 0;

	virtual psa_status_t purge_key(
		psa_key_id_t id) = 0;

	virtual psa_status_t get_key_attributes(
		psa_key_id_t id,
		psa_key_attributes_t *attributes) = 0;

	/* Key export methods */
	virtual psa_status_t export_key(
		psa_key_id_t id,
		uint8_t *data, size_t data_size, size_t *data_length) = 0;

	virtual psa_status_t export_public_key(
		psa_key_id_t id,
		uint8_t *data, size_t data_size, size_t *data_length) = 0;

	/* Sign/verify methods */
	virtual psa_status_t sign_hash(
		psa_key_id_t id,
		psa_algorithm_t alg,
		const uint8_t *hash, size_t hash_length,
		uint8_t *signature, size_t signature_size, size_t *signature_length) = 0;

	virtual psa_status_t verify_hash(
		psa_key_id_t id,
		psa_algorithm_t alg,
		const uint8_t *hash, size_t hash_length,
		const uint8_t *signature, size_t signature_length) = 0;

	/* Asymmetric encrypt/decrypt */
	virtual psa_status_t asymmetric_encrypt(
		psa_key_id_t id,
		psa_algorithm_t alg,
		const uint8_t *input, size_t input_length,
		const uint8_t *salt, size_t salt_length,
		uint8_t *output, size_t output_size, size_t *output_length) = 0;

	virtual psa_status_t asymmetric_decrypt(
		psa_key_id_t id,
		psa_algorithm_t alg,
		const uint8_t *input, size_t input_length,
		const uint8_t *salt, size_t salt_length,
		uint8_t *output, size_t output_size, size_t *output_length) = 0;

	/* Random number generation */
	virtual psa_status_t generate_random(
		uint8_t *output, size_t output_size) = 0;

	/* Hash methods */
	virtual size_t hash_max_update_size() const = 0;

	virtual psa_status_t hash_setup(
		uint32_t *op_handle,
		psa_algorithm_t alg) = 0;

	virtual psa_status_t hash_update(
		uint32_t op_handle,
		const uint8_t *input, size_t input_length) = 0;

	virtual psa_status_t hash_finish(
		uint32_t op_handle,
		uint8_t *hash, size_t hash_size, size_t *hash_length) = 0;

	virtual psa_status_t hash_abort(
		uint32_t op_handle) = 0;

	virtual psa_status_t hash_verify(
		uint32_t op_handle,
		const uint8_t *hash, size_t hash_length) = 0;

	virtual psa_status_t hash_clone(
		uint32_t source_op_handle,
		uint32_t *target_op_handle) = 0;

protected:
	crypto_client();
	crypto_client(struct rpc_caller *caller);
	void set_caller(struct rpc_caller *caller);

	struct service_client m_client;
};

#endif /* CRYPTO_CLIENT_H */