blob: be7d996502186ee915e0571a21026bca4f83ceab [file] [log] [blame]
Ronald Cron00b7bfc2020-11-25 15:25:26 +01001/*
2 * PSA RSA 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_RSA_H
22#define PSA_CRYPTO_RSA_H
23
24#include <psa/crypto.h>
25#include <mbedtls/rsa.h>
26
27/** Load the contents of a key buffer into an internal RSA representation
28 *
29 * \param[in] type The type of key contained in \p data.
30 * \param[in] data The buffer from which to load the representation.
31 * \param[in] data_length The size in bytes of \p data.
32 * \param[out] p_rsa Returns a pointer to an RSA context on success.
33 * The caller is responsible for freeing both the
34 * contents of the context and the context itself
35 * when done.
36 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020037psa_status_t mbedtls_psa_rsa_load_representation(psa_key_type_t type,
38 const uint8_t *data,
39 size_t data_length,
40 mbedtls_rsa_context **p_rsa);
Ronald Crone5ca3d82020-11-26 16:36:16 +010041
Ronald Cronabf2aef2020-11-27 18:13:44 +010042/** Import an RSA key in binary format.
43 *
44 * \note The signature of this function is that of a PSA driver
45 * import_key entry point. This function behaves as an import_key
46 * entry point as defined in the PSA driver interface specification for
47 * transparent drivers.
48 *
49 * \param[in] attributes The attributes for the key to import.
50 * \param[in] data The buffer containing the key data in import
51 * format.
52 * \param[in] data_length Size of the \p data buffer in bytes.
53 * \param[out] key_buffer The buffer containing the key data in output
54 * format.
55 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes. This
56 * size is greater or equal to \p data_length.
57 * \param[out] key_buffer_length The length of the data written in \p
58 * key_buffer in bytes.
59 * \param[out] bits The key size in number of bits.
60 *
61 * \retval #PSA_SUCCESS The RSA key was imported successfully.
62 * \retval #PSA_ERROR_INVALID_ARGUMENT
63 * The key data is not correctly formatted.
64 * \retval #PSA_ERROR_NOT_SUPPORTED
65 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
66 * \retval #PSA_ERROR_CORRUPTION_DETECTED
67 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020068psa_status_t mbedtls_psa_rsa_import_key(const psa_key_attributes_t *attributes,
69 const uint8_t *data,
70 size_t data_length,
71 uint8_t *key_buffer,
72 size_t key_buffer_size,
73 size_t *key_buffer_length,
74 size_t *bits);
Ronald Cronabf2aef2020-11-27 18:13:44 +010075
Ronald Crone5ca3d82020-11-26 16:36:16 +010076/** Export an RSA key to export representation
77 *
78 * \param[in] type The type of key (public/private) to export
79 * \param[in] rsa The internal RSA representation from which to export
80 * \param[out] data The buffer to export to
81 * \param[in] data_size The length of the buffer to export to
82 * \param[out] data_length The amount of bytes written to \p data
83 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020084psa_status_t mbedtls_psa_rsa_export_key(psa_key_type_t type,
85 mbedtls_rsa_context *rsa,
86 uint8_t *data,
87 size_t data_size,
88 size_t *data_length);
Ronald Crone5ca3d82020-11-26 16:36:16 +010089
90/** Export a public RSA key or the public part of an RSA key pair in binary
91 * format.
92 *
93 * \note The signature of this function is that of a PSA driver
94 * export_public_key entry point. This function behaves as an
95 * export_public_key entry point as defined in the PSA driver interface
96 * specification.
97 *
98 * \param[in] attributes The attributes for the key to export.
99 * \param[in] key_buffer Material or context of the key to export.
100 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
101 * \param[out] data Buffer where the key data is to be written.
102 * \param[in] data_size Size of the \p data buffer in bytes.
103 * \param[out] data_length On success, the number of bytes written in
104 * \p data.
105 *
106 * \retval #PSA_SUCCESS The RSA public key was exported successfully.
107 * \retval #PSA_ERROR_NOT_SUPPORTED
108 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
109 * \retval #PSA_ERROR_HARDWARE_FAILURE
110 * \retval #PSA_ERROR_CORRUPTION_DETECTED
111 * \retval #PSA_ERROR_STORAGE_FAILURE
112 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
113 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200114psa_status_t
115mbedtls_psa_rsa_export_public_key(const psa_key_attributes_t *attributes,
116 const uint8_t *key_buffer,
117 size_t key_buffer_size,
118 uint8_t *data,
119 size_t data_size,
120 size_t *data_length);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100121
Ronald Cron9e18fc12020-11-05 17:36:40 +0100122/**
123 * \brief Generate an RSA key.
124 *
125 * \note The signature of the function is that of a PSA driver generate_key
126 * entry point.
127 *
128 * \param[in] attributes The attributes for the RSA key to generate.
129 * \param[out] key_buffer Buffer where the key data is to be written.
130 * \param[in] key_buffer_size Size of \p key_buffer in bytes.
131 * \param[out] key_buffer_length On success, the number of bytes written in
132 * \p key_buffer.
133 *
134 * \retval #PSA_SUCCESS
135 * The key was successfully generated.
136 * \retval #PSA_ERROR_NOT_SUPPORTED
137 * Key length or type not supported.
138 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
139 * The size of \p key_buffer is too small.
140 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200141psa_status_t
142mbedtls_psa_rsa_generate_key(const psa_key_attributes_t *attributes,
143 uint8_t *key_buffer,
144 size_t key_buffer_size,
145 size_t *key_buffer_length);
Ronald Cron9e18fc12020-11-05 17:36:40 +0100146
Ronald Cron7bdbca32020-12-09 13:34:54 +0100147/** Sign an already-calculated hash with an RSA private key.
148 *
149 * \note The signature of this function is that of a PSA driver
150 * sign_hash entry point. This function behaves as a sign_hash
151 * entry point as defined in the PSA driver interface specification for
152 * transparent drivers.
153 *
154 * \param[in] attributes The attributes of the RSA key to use for the
155 * operation.
156 * \param[in] key_buffer The buffer containing the RSA key context.
157 * format.
158 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
159 * \param[in] alg A signature algorithm that is compatible with
160 * an RSA key.
161 * \param[in] hash The hash or message to sign.
162 * \param[in] hash_length Size of the \p hash buffer in bytes.
163 * \param[out] signature Buffer where the signature is to be written.
164 * \param[in] signature_size Size of the \p signature buffer in bytes.
165 * \param[out] signature_length On success, the number of bytes
166 * that make up the returned signature value.
167 *
168 * \retval #PSA_SUCCESS
169 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
170 * The size of the \p signature buffer is too small. You can
171 * determine a sufficient buffer size by calling
172 * #PSA_SIGN_OUTPUT_SIZE(\c PSA_KEY_TYPE_RSA_KEY_PAIR, \c key_bits,
173 * \p alg) where \c key_bits is the bit-size of the RSA key.
174 * \retval #PSA_ERROR_NOT_SUPPORTED
175 * \retval #PSA_ERROR_INVALID_ARGUMENT
176 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
177 * \retval #PSA_ERROR_CORRUPTION_DETECTED
178 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
179 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200180psa_status_t mbedtls_psa_rsa_sign_hash(const psa_key_attributes_t *attributes,
181 const uint8_t *key_buffer,
182 size_t key_buffer_size,
183 psa_algorithm_t alg,
184 const uint8_t *hash,
185 size_t hash_length,
186 uint8_t *signature,
187 size_t signature_size,
188 size_t *signature_length);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100189
190/**
191 * \brief Verify the signature a hash or short message using a public RSA key.
192 *
193 * \note The signature of this function is that of a PSA driver
194 * verify_hash entry point. This function behaves as a verify_hash
195 * entry point as defined in the PSA driver interface specification for
196 * transparent drivers.
197 *
198 * \param[in] attributes The attributes of the RSA key to use for the
199 * operation.
200 * \param[in] key_buffer The buffer containing the RSA key context.
201 * format.
202 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
203 * \param[in] alg A signature algorithm that is compatible with
204 * an RSA key.
205 * \param[in] hash The hash or message whose signature is to be
206 * verified.
207 * \param[in] hash_length Size of the \p hash buffer in bytes.
208 * \param[in] signature Buffer containing the signature to verify.
209 * \param[in] signature_length Size of the \p signature buffer in bytes.
210 *
211 * \retval #PSA_SUCCESS
212 * The signature is valid.
213 * \retval #PSA_ERROR_INVALID_SIGNATURE
214 * The calculation was performed successfully, but the passed
215 * signature is not a valid signature.
216 * \retval #PSA_ERROR_NOT_SUPPORTED
217 * \retval #PSA_ERROR_INVALID_ARGUMENT
218 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
219 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200220psa_status_t mbedtls_psa_rsa_verify_hash(const psa_key_attributes_t *attributes,
221 const uint8_t *key_buffer,
222 size_t key_buffer_size,
223 psa_algorithm_t alg,
224 const uint8_t *hash,
225 size_t hash_length,
226 const uint8_t *signature,
227 size_t signature_length);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100228
Ronald Cronf1057d32020-11-26 19:19:10 +0100229/*
230 * BEYOND THIS POINT, TEST DRIVER ENTRY POINTS ONLY.
231 */
232
233#if defined(PSA_CRYPTO_DRIVER_TEST)
234
Ronald Cron784fb322020-11-30 13:55:05 +0100235psa_status_t mbedtls_transparent_test_driver_rsa_import_key(
236 const psa_key_attributes_t *attributes,
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200237 const uint8_t *data,
238 size_t data_length,
239 uint8_t *key_buffer,
240 size_t key_buffer_size,
241 size_t *key_buffer_length,
242 size_t *bits);
Ronald Cron784fb322020-11-30 13:55:05 +0100243
Ronald Cronf1057d32020-11-26 19:19:10 +0100244psa_status_t mbedtls_transparent_test_driver_rsa_export_public_key(
245 const psa_key_attributes_t *attributes,
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200246 const uint8_t *key_buffer,
247 size_t key_buffer_size,
248 uint8_t *data,
249 size_t data_size,
250 size_t *data_length);
Ronald Cronf1057d32020-11-26 19:19:10 +0100251
Ronald Cron3a9c46b2020-11-06 09:38:35 +0100252psa_status_t mbedtls_transparent_test_driver_rsa_generate_key(
253 const psa_key_attributes_t *attributes,
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200254 uint8_t *key,
255 size_t key_size,
256 size_t *key_length);
Ronald Cron3a9c46b2020-11-06 09:38:35 +0100257
Ronald Crond2fb8542020-12-09 15:18:01 +0100258psa_status_t mbedtls_transparent_test_driver_rsa_sign_hash(
259 const psa_key_attributes_t *attributes,
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200260 const uint8_t *key_buffer,
261 size_t key_buffer_size,
262 psa_algorithm_t alg,
263 const uint8_t *hash,
264 size_t hash_length,
265 uint8_t *signature,
266 size_t signature_size,
267 size_t *signature_length);
Ronald Crond2fb8542020-12-09 15:18:01 +0100268
269psa_status_t mbedtls_transparent_test_driver_rsa_verify_hash(
270 const psa_key_attributes_t *attributes,
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200271 const uint8_t *key_buffer,
272 size_t key_buffer_size,
273 psa_algorithm_t alg,
274 const uint8_t *hash,
275 size_t hash_length,
276 const uint8_t *signature,
277 size_t signature_length);
Ronald Crond2fb8542020-12-09 15:18:01 +0100278
Ronald Cronf1057d32020-11-26 19:19:10 +0100279#endif /* PSA_CRYPTO_DRIVER_TEST */
280
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100281#endif /* PSA_CRYPTO_RSA_H */