blob: 5c9b63c39da1773af55e785b723ae562c5f0dc79 [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 */
45psa_status_t mbedtls_psa_ecp_load_representation( psa_key_type_t type,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +010046 size_t curve_bits,
Ronald Cron00b7bfc2020-11-25 15:25:26 +010047 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 */
77psa_status_t mbedtls_psa_ecp_import_key(
78 const psa_key_attributes_t *attributes,
79 const uint8_t *data, size_t data_length,
80 uint8_t *key_buffer, size_t key_buffer_size,
81 size_t *key_buffer_length, size_t *bits );
82
Ronald Crone5ca3d82020-11-26 16:36:16 +010083/** Export an ECP key to export representation
84 *
85 * \param[in] type The type of key (public/private) to export
86 * \param[in] ecp The internal ECP representation from which to export
87 * \param[out] data The buffer to export to
88 * \param[in] data_size The length of the buffer to export to
89 * \param[out] data_length The amount of bytes written to \p data
90 */
91psa_status_t mbedtls_psa_ecp_export_key( psa_key_type_t type,
92 mbedtls_ecp_keypair *ecp,
93 uint8_t *data,
94 size_t data_size,
95 size_t *data_length );
96
97/** Export an ECP public key or the public part of an ECP key pair in binary
98 * format.
99 *
100 * \note The signature of this function is that of a PSA driver
101 * export_public_key entry point. This function behaves as an
102 * export_public_key entry point as defined in the PSA driver interface
103 * specification.
104 *
105 * \param[in] attributes The attributes for the key to export.
106 * \param[in] key_buffer Material or context of the key to export.
107 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
108 * \param[out] data Buffer where the key data is to be written.
109 * \param[in] data_size Size of the \p data buffer in bytes.
110 * \param[out] data_length On success, the number of bytes written in
111 * \p data
112 *
113 * \retval #PSA_SUCCESS The ECP public key was exported successfully.
114 * \retval #PSA_ERROR_NOT_SUPPORTED
115 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
116 * \retval #PSA_ERROR_HARDWARE_FAILURE
117 * \retval #PSA_ERROR_CORRUPTION_DETECTED
118 * \retval #PSA_ERROR_STORAGE_FAILURE
119 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
120 */
121psa_status_t mbedtls_psa_ecp_export_public_key(
122 const psa_key_attributes_t *attributes,
123 const uint8_t *key_buffer, size_t key_buffer_size,
124 uint8_t *data, size_t data_size, size_t *data_length );
125
Ronald Cron7023db52020-11-20 18:17:42 +0100126/**
127 * \brief Generate an ECP key.
128 *
129 * \note The signature of the function is that of a PSA driver generate_key
130 * entry point.
131 *
132 * \param[in] attributes The attributes for the ECP key to generate.
133 * \param[out] key_buffer Buffer where the key data is to be written.
134 * \param[in] key_buffer_size Size of \p key_buffer in bytes.
135 * \param[out] key_buffer_length On success, the number of bytes written in
136 * \p key_buffer.
137 *
138 * \retval #PSA_SUCCESS
139 * The key was successfully generated.
140 * \retval #PSA_ERROR_NOT_SUPPORTED
141 * Key length or type not supported.
142 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
143 * The size of \p key_buffer is too small.
144 */
145psa_status_t mbedtls_psa_ecp_generate_key(
146 const psa_key_attributes_t *attributes,
147 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length );
148
Ronald Cronf1057d32020-11-26 19:19:10 +0100149/*
150 * BEYOND THIS POINT, TEST DRIVER ENTRY POINTS ONLY.
151 */
152
153#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronbbe5cbb2020-11-20 19:42:24 +0100154
Ronald Cron784fb322020-11-30 13:55:05 +0100155psa_status_t mbedtls_transparent_test_driver_ecp_import_key(
156 const psa_key_attributes_t *attributes,
157 const uint8_t *data, size_t data_length,
158 uint8_t *key_buffer, size_t key_buffer_size,
159 size_t *key_buffer_length, size_t *bits );
160
Ronald Cronf1057d32020-11-26 19:19:10 +0100161psa_status_t mbedtls_transparent_test_driver_ecp_export_public_key(
162 const psa_key_attributes_t *attributes,
163 const uint8_t *key_buffer, size_t key_buffer_size,
164 uint8_t *data, size_t data_size, size_t *data_length );
Ronald Cron784fb322020-11-30 13:55:05 +0100165
Ronald Cronbbe5cbb2020-11-20 19:42:24 +0100166psa_status_t mbedtls_transparent_test_driver_ecp_generate_key(
167 const psa_key_attributes_t *attributes,
168 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length );
169
Ronald Cronf1057d32020-11-26 19:19:10 +0100170#endif /* PSA_CRYPTO_DRIVER_TEST */
171
Ronald Crone5ca3d82020-11-26 16:36:16 +0100172#endif /* PSA_CRYPTO_ECP_H */