blob: 31dcc2ebb460388a469ffc4d15757ecf8d67840b [file] [log] [blame]
Ronald Cron00b7bfc2020-11-25 15:25:26 +01001/*
2 * PSA ECP layer on top of Mbed TLS crypto
3 */
4/*
5 * Copyright The Mbed TLS Contributors
6 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21#ifndef PSA_CRYPTO_ECP_H
22#define PSA_CRYPTO_ECP_H
23
24#include <psa/crypto.h>
25#include <mbedtls/ecp.h>
26
27/** Load the contents of a key buffer into an internal ECP representation
28 *
29 * \param[in] type The type of key contained in \p data.
Gilles Peskined88ccae2021-02-08 18:39:18 +010030 * \param[in] curve_bits The nominal bit-size of the curve.
31 * It must be consistent with the representation
32 * passed in \p data.
33 * This can be 0, in which case the bit-size
34 * is inferred from \p data_length (which is possible
35 * for all key types and representation formats
36 * formats that are currently supported or will
37 * be in the foreseeable future).
Ronald Cron00b7bfc2020-11-25 15:25:26 +010038 * \param[in] data The buffer from which to load the representation.
39 * \param[in] data_length The size in bytes of \p data.
40 * \param[out] p_ecp Returns a pointer to an ECP context on success.
41 * The caller is responsible for freeing both the
42 * contents of the context and the context itself
43 * when done.
44 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020045psa_status_t mbedtls_psa_ecp_load_representation(psa_key_type_t type,
46 size_t curve_bits,
47 const uint8_t *data,
48 size_t data_length,
49 mbedtls_ecp_keypair **p_ecp);
Ronald Crone5ca3d82020-11-26 16:36:16 +010050
Ronald Crond6ec3032020-11-27 18:54:57 +010051/** Import an ECP key in binary format.
52 *
53 * \note The signature of this function is that of a PSA driver
54 * import_key entry point. This function behaves as an import_key
55 * entry point as defined in the PSA driver interface specification for
56 * transparent drivers.
57 *
58 * \param[in] attributes The attributes for the key to import.
59 * \param[in] data The buffer containing the key data in import
60 * format.
61 * \param[in] data_length Size of the \p data buffer in bytes.
62 * \param[out] key_buffer The buffer containing the key data in output
63 * format.
64 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes. This
65 * size is greater or equal to \p data_length.
66 * \param[out] key_buffer_length The length of the data written in \p
67 * key_buffer in bytes.
68 * \param[out] bits The key size in number of bits.
69 *
70 * \retval #PSA_SUCCESS The ECP key was imported successfully.
71 * \retval #PSA_ERROR_INVALID_ARGUMENT
72 * The key data is not correctly formatted.
73 * \retval #PSA_ERROR_NOT_SUPPORTED
74 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
75 * \retval #PSA_ERROR_CORRUPTION_DETECTED
76 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020077psa_status_t mbedtls_psa_ecp_import_key(const psa_key_attributes_t *attributes,
78 const uint8_t *data,
79 size_t data_length,
80 uint8_t *key_buffer,
81 size_t key_buffer_size,
82 size_t *key_buffer_length,
83 size_t *bits);
Ronald Crond6ec3032020-11-27 18:54:57 +010084
Ronald Crone5ca3d82020-11-26 16:36:16 +010085/** Export an ECP key to export representation
86 *
87 * \param[in] type The type of key (public/private) to export
88 * \param[in] ecp The internal ECP representation from which to export
89 * \param[out] data The buffer to export to
90 * \param[in] data_size The length of the buffer to export to
91 * \param[out] data_length The amount of bytes written to \p data
92 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020093psa_status_t mbedtls_psa_ecp_export_key(psa_key_type_t type,
94 mbedtls_ecp_keypair *ecp,
95 uint8_t *data,
96 size_t data_size,
97 size_t *data_length);
Ronald Crone5ca3d82020-11-26 16:36:16 +010098
99/** Export an ECP public key or the public part of an ECP key pair in binary
100 * format.
101 *
102 * \note The signature of this function is that of a PSA driver
103 * export_public_key entry point. This function behaves as an
104 * export_public_key entry point as defined in the PSA driver interface
105 * specification.
106 *
107 * \param[in] attributes The attributes for the key to export.
108 * \param[in] key_buffer Material or context of the key to export.
109 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
110 * \param[out] data Buffer where the key data is to be written.
111 * \param[in] data_size Size of the \p data buffer in bytes.
112 * \param[out] data_length On success, the number of bytes written in
113 * \p data
114 *
115 * \retval #PSA_SUCCESS The ECP public key was exported successfully.
116 * \retval #PSA_ERROR_NOT_SUPPORTED
117 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
118 * \retval #PSA_ERROR_HARDWARE_FAILURE
119 * \retval #PSA_ERROR_CORRUPTION_DETECTED
120 * \retval #PSA_ERROR_STORAGE_FAILURE
121 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
122 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200123psa_status_t
124mbedtls_psa_ecp_export_public_key(const psa_key_attributes_t *attributes,
125 const uint8_t *key_buffer,
126 size_t key_buffer_size,
127 uint8_t *data,
128 size_t data_size,
129 size_t *data_length);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100130
Ronald Cron7023db52020-11-20 18:17:42 +0100131/**
132 * \brief Generate an ECP key.
133 *
134 * \note The signature of the function is that of a PSA driver generate_key
135 * entry point.
136 *
137 * \param[in] attributes The attributes for the ECP key to generate.
138 * \param[out] key_buffer Buffer where the key data is to be written.
139 * \param[in] key_buffer_size Size of \p key_buffer in bytes.
140 * \param[out] key_buffer_length On success, the number of bytes written in
141 * \p key_buffer.
142 *
143 * \retval #PSA_SUCCESS
144 * The key was successfully generated.
145 * \retval #PSA_ERROR_NOT_SUPPORTED
146 * Key length or type not supported.
147 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
148 * The size of \p key_buffer is too small.
149 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200150psa_status_t
151mbedtls_psa_ecp_generate_key(const psa_key_attributes_t *attributes,
152 uint8_t *key_buffer,
153 size_t key_buffer_size,
154 size_t *key_buffer_length);
Ronald Cron7023db52020-11-20 18:17:42 +0100155
Ronald Cron072722c2020-12-09 16:36:19 +0100156/** Sign an already-calculated hash with ECDSA.
157 *
158 * \note The signature of this function is that of a PSA driver
159 * sign_hash entry point. This function behaves as a sign_hash
160 * entry point as defined in the PSA driver interface specification for
161 * transparent drivers.
162 *
163 * \param[in] attributes The attributes of the ECC key to use for the
164 * operation.
165 * \param[in] key_buffer The buffer containing the ECC key context.
166 * format.
167 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
168 * \param[in] alg Randomized or deterministic ECDSA algorithm.
169 * \param[in] hash The hash or message to sign.
170 * \param[in] hash_length Size of the \p hash buffer in bytes.
171 * \param[out] signature Buffer where the signature is to be written.
172 * \param[in] signature_size Size of the \p signature buffer in bytes.
173 * \param[out] signature_length On success, the number of bytes
174 * that make up the returned signature value.
175 *
176 * \retval #PSA_SUCCESS
177 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
178 * The size of the \p signature buffer is too small. You can
179 * determine a sufficient buffer size by calling
180 * #PSA_SIGN_OUTPUT_SIZE(\c PSA_KEY_TYPE_ECC_KEY_PAIR, \c key_bits,
181 * \p alg) where \c key_bits is the bit-size of the ECC key.
182 * \retval #PSA_ERROR_NOT_SUPPORTED
183 * \retval #PSA_ERROR_INVALID_ARGUMENT
184 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
185 * \retval #PSA_ERROR_CORRUPTION_DETECTED
186 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
187 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200188psa_status_t mbedtls_psa_ecdsa_sign_hash(const psa_key_attributes_t *attributes,
189 const uint8_t *key_buffer,
190 size_t key_buffer_size,
191 psa_algorithm_t alg,
192 const uint8_t *hash,
193 size_t hash_length,
194 uint8_t *signature,
195 size_t signature_size,
196 size_t *signature_length);
Ronald Cron072722c2020-12-09 16:36:19 +0100197
198/**
199 * \brief Verify an ECDSA hash or short message signature.
200 *
201 * \note The signature of this function is that of a PSA driver
202 * verify_hash entry point. This function behaves as a verify_hash
203 * entry point as defined in the PSA driver interface specification for
204 * transparent drivers.
205 *
206 * \param[in] attributes The attributes of the ECC key to use for the
207 * operation.
208 * \param[in] key_buffer The buffer containing the ECC key context.
209 * format.
210 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
211 * \param[in] alg Randomized or deterministic ECDSA algorithm.
212 * \param[in] hash The hash or message whose signature is to be
213 * verified.
214 * \param[in] hash_length Size of the \p hash buffer in bytes.
215 * \param[in] signature Buffer containing the signature to verify.
216 * \param[in] signature_length Size of the \p signature buffer in bytes.
217 *
218 * \retval #PSA_SUCCESS
219 * The signature is valid.
220 * \retval #PSA_ERROR_INVALID_SIGNATURE
221 * The calculation was performed successfully, but the passed
222 * signature is not a valid signature.
223 * \retval #PSA_ERROR_NOT_SUPPORTED
224 * \retval #PSA_ERROR_INVALID_ARGUMENT
225 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
226 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200227psa_status_t
228mbedtls_psa_ecdsa_verify_hash(const psa_key_attributes_t *attributes,
229 const uint8_t *key_buffer,
230 size_t key_buffer_size,
231 psa_algorithm_t alg,
232 const uint8_t *hash,
233 size_t hash_length,
234 const uint8_t *signature,
235 size_t signature_length);
Ronald Cronf1057d32020-11-26 19:19:10 +0100236/*
237 * BEYOND THIS POINT, TEST DRIVER ENTRY POINTS ONLY.
238 */
239
240#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronbbe5cbb2020-11-20 19:42:24 +0100241
Ronald Cron784fb322020-11-30 13:55:05 +0100242psa_status_t mbedtls_transparent_test_driver_ecp_import_key(
243 const psa_key_attributes_t *attributes,
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200244 const uint8_t *data,
245 size_t data_length,
246 uint8_t *key_buffer,
247 size_t key_buffer_size,
248 size_t *key_buffer_length,
249 size_t *bits);
Ronald Cron784fb322020-11-30 13:55:05 +0100250
Ronald Cronf1057d32020-11-26 19:19:10 +0100251psa_status_t mbedtls_transparent_test_driver_ecp_export_public_key(
252 const psa_key_attributes_t *attributes,
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200253 const uint8_t *key_buffer,
254 size_t key_buffer_size,
255 uint8_t *data,
256 size_t data_size,
257 size_t *data_length);
Ronald Cron784fb322020-11-30 13:55:05 +0100258
Ronald Cronbbe5cbb2020-11-20 19:42:24 +0100259psa_status_t mbedtls_transparent_test_driver_ecp_generate_key(
260 const psa_key_attributes_t *attributes,
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200261 uint8_t *key_buffer,
262 size_t key_buffer_size,
263 size_t *key_buffer_length);
Ronald Cronbbe5cbb2020-11-20 19:42:24 +0100264
Ronald Cronb5399a82020-12-10 09:35:33 +0100265psa_status_t mbedtls_transparent_test_driver_ecdsa_sign_hash(
266 const psa_key_attributes_t *attributes,
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200267 const uint8_t *key_buffer,
268 size_t key_buffer_size,
269 psa_algorithm_t alg,
270 const uint8_t *hash,
271 size_t hash_length,
272 uint8_t *signature,
273 size_t signature_size,
274 size_t *signature_length);
Ronald Cronb5399a82020-12-10 09:35:33 +0100275
276psa_status_t mbedtls_transparent_test_driver_ecdsa_verify_hash(
277 const psa_key_attributes_t *attributes,
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200278 const uint8_t *key_buffer,
279 size_t key_buffer_size,
280 psa_algorithm_t alg,
281 const uint8_t *hash,
282 size_t hash_length,
283 const uint8_t *signature,
284 size_t signature_length);
Ronald Cronb5399a82020-12-10 09:35:33 +0100285
Ronald Cronf1057d32020-11-26 19:19:10 +0100286#endif /* PSA_CRYPTO_DRIVER_TEST */
287
Ronald Crone5ca3d82020-11-26 16:36:16 +0100288#endif /* PSA_CRYPTO_ECP_H */