blob: c740c4522dab78e3cf316af4e4846d7fb2c26d77 [file] [log] [blame]
Aditya Deshpande045b3702023-02-20 17:08:30 +00001/*
2 * Driver entry points for p256-m
3 */
4/*
5 * Copyright The Mbed TLS Contributors
Dave Rodgmanfffeae82023-11-03 09:28:10 +00006 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Aditya Deshpande045b3702023-02-20 17:08:30 +00007 */
8
Aditya Deshpandee41f7e42023-01-12 16:29:02 +00009#ifndef P256M_DRIVER_ENTRYPOINTS_H
10#define P256M_DRIVER_ENTRYPOINTS_H
11
Gilles Peskineefaee9a2023-09-20 20:49:47 +020012#if defined(MBEDTLS_PSA_P256M_DRIVER_ENABLED)
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000013#ifndef PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT
14#define PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT
15#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Gilles Peskineefaee9a2023-09-20 20:49:47 +020016#endif /* MBEDTLS_PSA_P256M_DRIVER_ENABLED */
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000017
18#include "psa/crypto_types.h"
19
Manuel Pégourié-Gonnard5424cf22023-08-07 10:56:12 +020020/** Import SECP256R1 key.
21 *
22 * \param[in] attributes The attributes of the key to use for the
23 * operation.
24 * \param[in] data The raw key material. For private keys
25 * this must be a big-endian integer of 32
26 * bytes; for public key this must be an
27 * uncompressed ECPoint (65 bytes).
28 * \param[in] data_length The size of the raw key material.
29 * \param[out] key_buffer The buffer to contain the key data in
30 * output format upon successful return.
31 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
32 * \param[out] key_buffer_length The length of the data written in \p
33 * key_buffer in bytes.
34 * \param[out] bits The bitsize of the key.
35 *
36 * \retval #PSA_SUCCESS
37 * Success. Keypair generated and stored in buffer.
38 * \retval #PSA_ERROR_NOT_SUPPORTED
39 * The input is not supported by this driver (not SECP256R1).
40 * \retval #PSA_ERROR_INVALID_ARGUMENT
41 * The input is invalid.
42 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
43 * \p key_buffer_size is too small.
44 */
45psa_status_t p256_transparent_import_key(const psa_key_attributes_t *attributes,
46 const uint8_t *data,
47 size_t data_length,
48 uint8_t *key_buffer,
49 size_t key_buffer_size,
50 size_t *key_buffer_length,
51 size_t *bits);
52
Manuel Pégourié-Gonnard18d71422023-08-07 11:18:05 +020053/** Export SECP256R1 public key, from the private key.
54 *
55 * \param[in] attributes The attributes of the key to use for the
56 * operation.
57 * \param[in] key_buffer The private key in the export format.
58 * \param[in] key_buffer_size The size of the private key in bytes.
59 * \param[out] data The buffer to contain the public key in
60 * the export format upon successful return.
61 * \param[in] data_size The size of the \p data buffer in bytes.
62 * \param[out] data_length The length written to \p data in bytes.
63 *
64 * \retval #PSA_SUCCESS
65 * Success. Keypair generated and stored in buffer.
66 * \retval #PSA_ERROR_NOT_SUPPORTED
67 * The input is not supported by this driver (not SECP256R1).
68 * \retval #PSA_ERROR_INVALID_ARGUMENT
69 * The input is invalid.
70 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
71 * \p key_buffer_size is too small.
72 */
73psa_status_t p256_transparent_export_public_key(const psa_key_attributes_t *attributes,
74 const uint8_t *key_buffer,
75 size_t key_buffer_size,
76 uint8_t *data,
77 size_t data_size,
78 size_t *data_length);
79
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000080/** Generate SECP256R1 ECC Key Pair.
81 * Interface function which calls the p256-m key generation function and
Gilles Peskinee820c0a2023-08-03 17:45:20 +020082 * places it in the key buffer provided by the caller (Mbed TLS) in the
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000083 * correct format. For a SECP256R1 curve this is the 32 bit private key.
84 *
85 * \param[in] attributes The attributes of the key to use for the
86 * operation.
87 * \param[out] key_buffer The buffer to contain the key data in
88 * output format upon successful return.
89 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
90 * \param[out] key_buffer_length The length of the data written in \p
91 * key_buffer in bytes.
92 *
93 * \retval #PSA_SUCCESS
94 * Success. Keypair generated and stored in buffer.
Manuel Pégourié-Gonnardf2518942023-09-20 09:42:55 +020095 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
96 * \p key_buffer_size is too small.
97 * \retval #PSA_ERROR_GENERIC_ERROR
98 * The internal RNG failed.
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000099 */
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000100psa_status_t p256_transparent_generate_key(
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000101 const psa_key_attributes_t *attributes,
102 uint8_t *key_buffer,
103 size_t key_buffer_size,
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000104 size_t *key_buffer_length);
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000105
106/** Perform raw key agreement using p256-m's ECDH implementation
107 * \param[in] attributes The attributes of the key to use for the
108 * operation.
109 * \param[in] key_buffer The buffer containing the private key
110 * in the format specified by PSA.
111 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
112 * \param[in] alg A key agreement algorithm that is
113 * compatible with the type of the key.
114 * \param[in] peer_key The buffer containing the peer's public
115 * key in format specified by PSA.
116 * \param[in] peer_key_length Size of the \p peer_key buffer in
117 * bytes.
118 * \param[out] shared_secret The buffer to which the shared secret
119 * is to be written.
120 * \param[in] shared_secret_size Size of the \p shared_secret buffer in
121 * bytes.
122 * \param[out] shared_secret_length On success, the number of bytes that
123 * make up the returned shared secret.
Manuel Pégourié-Gonnardf2518942023-09-20 09:42:55 +0200124 * \retval #PSA_SUCCESS
125 * Success. Shared secret successfully calculated.
126 * \retval #PSA_ERROR_INVALID_ARGUMENT
127 * The input is invalid.
128 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
129 * \p shared_secret_size is too small.
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000130 */
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000131psa_status_t p256_transparent_key_agreement(
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000132 const psa_key_attributes_t *attributes,
133 const uint8_t *key_buffer,
134 size_t key_buffer_size,
135 psa_algorithm_t alg,
136 const uint8_t *peer_key,
137 size_t peer_key_length,
138 uint8_t *shared_secret,
139 size_t shared_secret_size,
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000140 size_t *shared_secret_length);
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000141
142/** Sign an already-calculated hash with a private key using p256-m's ECDSA
143 * implementation
144 * \param[in] attributes The attributes of the key to use for the
145 * operation.
146 * \param[in] key_buffer The buffer containing the private key
147 * in the format specified by PSA.
148 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
149 * \param[in] alg A signature algorithm that is compatible
150 * with the type of the key.
151 * \param[in] hash The hash to sign.
152 * \param[in] hash_length Size of the \p hash buffer in bytes.
153 * \param[out] signature Buffer where signature is to be written.
154 * \param[in] signature_size Size of the \p signature buffer in bytes.
155 * \param[out] signature_length On success, the number of bytes
156 * that make up the returned signature value.
157 *
Manuel Pégourié-Gonnardf2518942023-09-20 09:42:55 +0200158 * \retval #PSA_SUCCESS
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000159 * Success. Hash was signed successfully.
Manuel Pégourié-Gonnardf2518942023-09-20 09:42:55 +0200160 * \retval #PSA_ERROR_INVALID_ARGUMENT
161 * The input is invalid.
162 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
163 * \p signature_size is too small.
164 * \retval #PSA_ERROR_GENERIC_ERROR
165 * The internal RNG failed.
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000166 */
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000167psa_status_t p256_transparent_sign_hash(
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000168 const psa_key_attributes_t *attributes,
169 const uint8_t *key_buffer,
170 size_t key_buffer_size,
171 psa_algorithm_t alg,
172 const uint8_t *hash,
173 size_t hash_length,
174 uint8_t *signature,
175 size_t signature_size,
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000176 size_t *signature_length);
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000177
178/** Verify the signature of a hash using a SECP256R1 public key using p256-m's
179 * ECDSA implementation.
180 *
181 * \note p256-m expects a 64 byte public key, but the contents of the key
182 buffer may be the 32 byte keypair representation or the 65 byte
183 public key representation. As a result, this function calls
184 psa_driver_wrapper_export_public_key() to ensure the public key
185 can be passed to p256-m.
186 *
187 * \param[in] attributes The attributes of the key to use for the
188 * operation.
189 *
190 * \param[in] key_buffer The buffer containing the key
191 * in the format specified by PSA.
192 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
193 * \param[in] alg A signature algorithm that is compatible with
194 * the type of the key.
195 * \param[in] hash The hash whose signature is to be
196 * verified.
197 * \param[in] hash_length Size of the \p hash buffer in bytes.
198 * \param[in] signature Buffer containing the signature to verify.
199 * \param[in] signature_length Size of the \p signature buffer in bytes.
200 *
Manuel Pégourié-Gonnardf2518942023-09-20 09:42:55 +0200201 * \retval #PSA_SUCCESS
202 * The signature is valid.
203 * \retval #PSA_ERROR_INVALID_SIGNATURE
204 * The calculation was performed successfully, but the passed
205 * signature is not a valid signature.
206 * \retval #PSA_ERROR_INVALID_ARGUMENT
207 * The input is invalid.
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000208 */
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000209psa_status_t p256_transparent_verify_hash(
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000210 const psa_key_attributes_t *attributes,
211 const uint8_t *key_buffer,
212 size_t key_buffer_size,
213 psa_algorithm_t alg,
214 const uint8_t *hash,
215 size_t hash_length,
216 const uint8_t *signature,
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000217 size_t signature_length);
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000218
219#endif /* P256M_DRIVER_ENTRYPOINTS_H */