update atrs list
[RRG-proxmark3.git] / common / mbedtls / psa_crypto_rsa.c
blob54c44b0ecc38a983fed6431a9d07b6267a0a4149
1 /*
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
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include "common.h"
23 #if defined(MBEDTLS_PSA_CRYPTO_C)
25 #include <psa/crypto.h>
26 #include "psa_crypto_core.h"
27 #include "psa_crypto_random_impl.h"
28 #include "psa_crypto_rsa.h"
30 #include <stdlib.h>
31 #include <string.h>
32 #include "mbedtls/platform.h"
33 #if !defined(MBEDTLS_PLATFORM_C)
34 #define mbedtls_calloc calloc
35 #define mbedtls_free free
36 #endif
38 #include <mbedtls/rsa.h>
39 #include <mbedtls/error.h>
40 #include <mbedtls/pk.h>
41 #include <mbedtls/pk_internal.h>
43 #if ( defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
44 ( defined(PSA_CRYPTO_DRIVER_TEST) && \
45 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_RSA_KEY_PAIR) ) )
46 #define BUILTIN_KEY_TYPE_RSA_KEY_PAIR 1
47 #endif
49 #if ( defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) || \
50 ( defined(PSA_CRYPTO_DRIVER_TEST) && \
51 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_RSA_PUBLIC_KEY) ) )
52 #define BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY 1
53 #endif
55 #if ( defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
56 ( defined(PSA_CRYPTO_DRIVER_TEST) && \
57 defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PKCS1V15_SIGN) && \
58 defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V15) ) )
59 #define BUILTIN_ALG_RSA_PKCS1V15_SIGN 1
60 #endif
62 #if ( defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
63 ( defined(PSA_CRYPTO_DRIVER_TEST) && \
64 defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PSS) && \
65 defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) ) )
66 #define BUILTIN_ALG_RSA_PSS 1
67 #endif
69 #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
70 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
71 defined(BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
72 defined(BUILTIN_ALG_RSA_PSS) || \
73 defined(BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
74 defined(BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
76 /* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
77 * that are not a multiple of 8) well. For example, there is only
78 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
79 * way to return the exact bit size of a key.
80 * To keep things simple, reject non-byte-aligned key sizes. */
81 static psa_status_t psa_check_rsa_key_byte_aligned(
82 const mbedtls_rsa_context *rsa) {
83 mbedtls_mpi n;
84 psa_status_t status;
85 mbedtls_mpi_init(&n);
86 status = mbedtls_to_psa_error(
87 mbedtls_rsa_export(rsa, &n, NULL, NULL, NULL, NULL));
88 if (status == PSA_SUCCESS) {
89 if (mbedtls_mpi_bitlen(&n) % 8 != 0)
90 status = PSA_ERROR_NOT_SUPPORTED;
92 mbedtls_mpi_free(&n);
93 return (status);
96 psa_status_t mbedtls_psa_rsa_load_representation(
97 psa_key_type_t type, const uint8_t *data, size_t data_length,
98 mbedtls_rsa_context **p_rsa) {
99 psa_status_t status;
100 mbedtls_pk_context ctx;
101 size_t bits;
102 mbedtls_pk_init(&ctx);
104 /* Parse the data. */
105 if (PSA_KEY_TYPE_IS_KEY_PAIR(type))
106 status = mbedtls_to_psa_error(
107 mbedtls_pk_parse_key(&ctx, data, data_length, NULL, 0));
108 else
109 status = mbedtls_to_psa_error(
110 mbedtls_pk_parse_public_key(&ctx, data, data_length));
111 if (status != PSA_SUCCESS)
112 goto exit;
114 /* We have something that the pkparse module recognizes. If it is a
115 * valid RSA key, store it. */
116 if (mbedtls_pk_get_type(&ctx) != MBEDTLS_PK_RSA) {
117 status = PSA_ERROR_INVALID_ARGUMENT;
118 goto exit;
121 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
122 * supports non-byte-aligned key sizes, but not well. For example,
123 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
124 bits = PSA_BYTES_TO_BITS(mbedtls_rsa_get_len(mbedtls_pk_rsa(ctx)));
125 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
126 status = PSA_ERROR_NOT_SUPPORTED;
127 goto exit;
129 status = psa_check_rsa_key_byte_aligned(mbedtls_pk_rsa(ctx));
130 if (status != PSA_SUCCESS)
131 goto exit;
133 /* Copy out the pointer to the RSA context, and reset the PK context
134 * such that pk_free doesn't free the RSA context we just grabbed. */
135 *p_rsa = mbedtls_pk_rsa(ctx);
136 ctx.pk_info = NULL;
138 exit:
139 mbedtls_pk_free(&ctx);
140 return (status);
142 #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
143 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
144 * defined(BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
145 * defined(BUILTIN_ALG_RSA_PSS) ||
146 * defined(BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
147 * defined(BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
149 #if defined(BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
150 defined(BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
152 static psa_status_t rsa_import_key(
153 const psa_key_attributes_t *attributes,
154 const uint8_t *data, size_t data_length,
155 uint8_t *key_buffer, size_t key_buffer_size,
156 size_t *key_buffer_length, size_t *bits) {
157 psa_status_t status;
158 mbedtls_rsa_context *rsa = NULL;
160 /* Parse input */
161 status = mbedtls_psa_rsa_load_representation(attributes->core.type,
162 data,
163 data_length,
164 &rsa);
165 if (status != PSA_SUCCESS)
166 goto exit;
168 *bits = (psa_key_bits_t) PSA_BYTES_TO_BITS(mbedtls_rsa_get_len(rsa));
170 /* Re-export the data to PSA export format, such that we can store export
171 * representation in the key slot. Export representation in case of RSA is
172 * the smallest representation that's allowed as input, so a straight-up
173 * allocation of the same size as the input buffer will be large enough. */
174 status = mbedtls_psa_rsa_export_key(attributes->core.type,
175 rsa,
176 key_buffer,
177 key_buffer_size,
178 key_buffer_length);
179 exit:
180 /* Always free the RSA object */
181 mbedtls_rsa_free(rsa);
182 mbedtls_free(rsa);
184 return (status);
187 psa_status_t mbedtls_psa_rsa_export_key(psa_key_type_t type,
188 mbedtls_rsa_context *rsa,
189 uint8_t *data,
190 size_t data_size,
191 size_t *data_length) {
192 #if defined(MBEDTLS_PK_WRITE_C)
193 int ret;
194 mbedtls_pk_context pk;
195 uint8_t *pos = data + data_size;
197 mbedtls_pk_init(&pk);
198 pk.pk_info = &mbedtls_rsa_info;
199 pk.pk_ctx = rsa;
201 /* PSA Crypto API defines the format of an RSA key as a DER-encoded
202 * representation of the non-encrypted PKCS#1 RSAPrivateKey for a
203 * private key and of the RFC3279 RSAPublicKey for a public key. */
204 if (PSA_KEY_TYPE_IS_KEY_PAIR(type))
205 ret = mbedtls_pk_write_key_der(&pk, data, data_size);
206 else
207 ret = mbedtls_pk_write_pubkey(&pos, data, &pk);
209 if (ret < 0) {
210 /* Clean up in case pk_write failed halfway through. */
211 memset(data, 0, data_size);
212 return (mbedtls_to_psa_error(ret));
215 /* The mbedtls_pk_xxx functions write to the end of the buffer.
216 * Move the data to the beginning and erase remaining data
217 * at the original location. */
218 if (2 * (size_t) ret <= data_size) {
219 memcpy(data, data + data_size - ret, ret);
220 memset(data + data_size - ret, 0, ret);
221 } else if ((size_t) ret < data_size) {
222 memmove(data, data + data_size - ret, ret);
223 memset(data + ret, 0, data_size - ret);
226 *data_length = ret;
227 return (PSA_SUCCESS);
228 #else
229 (void) type;
230 (void) rsa;
231 (void) data;
232 (void) data_size;
233 (void) data_length;
234 return (PSA_ERROR_NOT_SUPPORTED);
235 #endif /* MBEDTLS_PK_WRITE_C */
238 static psa_status_t rsa_export_public_key(
239 const psa_key_attributes_t *attributes,
240 const uint8_t *key_buffer, size_t key_buffer_size,
241 uint8_t *data, size_t data_size, size_t *data_length) {
242 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
243 mbedtls_rsa_context *rsa = NULL;
245 status = mbedtls_psa_rsa_load_representation(
246 attributes->core.type, key_buffer, key_buffer_size, &rsa);
247 if (status != PSA_SUCCESS)
248 return (status);
250 status = mbedtls_psa_rsa_export_key(PSA_KEY_TYPE_RSA_PUBLIC_KEY,
251 rsa,
252 data,
253 data_size,
254 data_length);
256 mbedtls_rsa_free(rsa);
257 mbedtls_free(rsa);
259 return (status);
261 #endif /* defined(BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
262 * defined(BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
264 #if defined(BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
265 static psa_status_t psa_rsa_read_exponent(const uint8_t *domain_parameters,
266 size_t domain_parameters_size,
267 int *exponent) {
268 size_t i;
269 uint32_t acc = 0;
271 if (domain_parameters_size == 0) {
272 *exponent = 65537;
273 return (PSA_SUCCESS);
276 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
277 * support values that fit in a 32-bit integer, which is larger than
278 * int on just about every platform anyway. */
279 if (domain_parameters_size > sizeof(acc))
280 return (PSA_ERROR_NOT_SUPPORTED);
281 for (i = 0; i < domain_parameters_size; i++)
282 acc = (acc << 8) | domain_parameters[i];
283 if (acc > INT_MAX)
284 return (PSA_ERROR_NOT_SUPPORTED);
285 *exponent = acc;
286 return (PSA_SUCCESS);
289 static psa_status_t rsa_generate_key(
290 const psa_key_attributes_t *attributes,
291 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length) {
292 psa_status_t status;
293 mbedtls_rsa_context rsa;
294 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
295 int exponent;
297 status = psa_rsa_read_exponent(attributes->domain_parameters,
298 attributes->domain_parameters_size,
299 &exponent);
300 if (status != PSA_SUCCESS)
301 return (status);
303 mbedtls_rsa_init(&rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE);
304 ret = mbedtls_rsa_gen_key(&rsa,
305 mbedtls_psa_get_random,
306 MBEDTLS_PSA_RANDOM_STATE,
307 (unsigned int)attributes->core.bits,
308 exponent);
309 if (ret != 0)
310 return (mbedtls_to_psa_error(ret));
312 status = mbedtls_psa_rsa_export_key(attributes->core.type,
313 &rsa, key_buffer, key_buffer_size,
314 key_buffer_length);
315 mbedtls_rsa_free(&rsa);
317 return (status);
319 #endif /* defined(BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
321 /****************************************************************/
322 /* Sign/verify hashes */
323 /****************************************************************/
325 #if defined(BUILTIN_ALG_RSA_PKCS1V15_SIGN) || defined(BUILTIN_ALG_RSA_PSS)
327 /* Decode the hash algorithm from alg and store the mbedtls encoding in
328 * md_alg. Verify that the hash length is acceptable. */
329 static psa_status_t psa_rsa_decode_md_type(psa_algorithm_t alg,
330 size_t hash_length,
331 mbedtls_md_type_t *md_alg) {
332 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
333 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa(hash_alg);
334 *md_alg = mbedtls_md_get_type(md_info);
336 /* The Mbed TLS RSA module uses an unsigned int for hash length
337 * parameters. Validate that it fits so that we don't risk an
338 * overflow later. */
339 #if SIZE_MAX > UINT_MAX
340 if (hash_length > UINT_MAX)
341 return (PSA_ERROR_INVALID_ARGUMENT);
342 #endif
344 #if defined(BUILTIN_ALG_RSA_PKCS1V15_SIGN)
345 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
346 * must be correct. */
347 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) &&
348 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW) {
349 if (md_info == NULL)
350 return (PSA_ERROR_NOT_SUPPORTED);
351 if (mbedtls_md_get_size(md_info) != hash_length)
352 return (PSA_ERROR_INVALID_ARGUMENT);
354 #endif /* BUILTIN_ALG_RSA_PKCS1V15_SIGN */
356 #if defined(BUILTIN_ALG_RSA_PSS)
357 /* PSS requires a hash internally. */
358 if (PSA_ALG_IS_RSA_PSS(alg)) {
359 if (md_info == NULL)
360 return (PSA_ERROR_NOT_SUPPORTED);
362 #endif /* BUILTIN_ALG_RSA_PSS */
364 return (PSA_SUCCESS);
367 static psa_status_t rsa_sign_hash(
368 const psa_key_attributes_t *attributes,
369 const uint8_t *key_buffer, size_t key_buffer_size,
370 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
371 uint8_t *signature, size_t signature_size, size_t *signature_length) {
372 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
373 mbedtls_rsa_context *rsa = NULL;
374 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
375 mbedtls_md_type_t md_alg;
377 status = mbedtls_psa_rsa_load_representation(attributes->core.type,
378 key_buffer,
379 key_buffer_size,
380 &rsa);
381 if (status != PSA_SUCCESS)
382 return (status);
384 status = psa_rsa_decode_md_type(alg, hash_length, &md_alg);
385 if (status != PSA_SUCCESS)
386 goto exit;
388 if (signature_size < mbedtls_rsa_get_len(rsa)) {
389 status = PSA_ERROR_BUFFER_TOO_SMALL;
390 goto exit;
393 #if defined(BUILTIN_ALG_RSA_PKCS1V15_SIGN)
394 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg)) {
395 mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V15,
396 MBEDTLS_MD_NONE);
397 ret = mbedtls_rsa_pkcs1_sign(rsa,
398 mbedtls_psa_get_random,
399 MBEDTLS_PSA_RANDOM_STATE,
400 MBEDTLS_RSA_PRIVATE,
401 md_alg,
402 (unsigned int) hash_length,
403 hash,
404 signature);
405 } else
406 #endif /* BUILTIN_ALG_RSA_PKCS1V15_SIGN */
407 #if defined(BUILTIN_ALG_RSA_PSS)
408 if (PSA_ALG_IS_RSA_PSS(alg)) {
409 mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
410 ret = mbedtls_rsa_rsassa_pss_sign(rsa,
411 mbedtls_psa_get_random,
412 MBEDTLS_PSA_RANDOM_STATE,
413 MBEDTLS_RSA_PRIVATE,
414 MBEDTLS_MD_NONE,
415 (unsigned int) hash_length,
416 hash,
417 signature);
418 } else
419 #endif /* BUILTIN_ALG_RSA_PSS */
421 status = PSA_ERROR_INVALID_ARGUMENT;
422 goto exit;
425 if (ret == 0)
426 *signature_length = mbedtls_rsa_get_len(rsa);
427 status = mbedtls_to_psa_error(ret);
429 exit:
430 mbedtls_rsa_free(rsa);
431 mbedtls_free(rsa);
433 return (status);
436 static psa_status_t rsa_verify_hash(
437 const psa_key_attributes_t *attributes,
438 const uint8_t *key_buffer, size_t key_buffer_size,
439 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
440 const uint8_t *signature, size_t signature_length) {
441 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
442 mbedtls_rsa_context *rsa = NULL;
443 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
444 mbedtls_md_type_t md_alg;
446 status = mbedtls_psa_rsa_load_representation(attributes->core.type,
447 key_buffer,
448 key_buffer_size,
449 &rsa);
450 if (status != PSA_SUCCESS)
451 goto exit;
453 status = psa_rsa_decode_md_type(alg, hash_length, &md_alg);
454 if (status != PSA_SUCCESS)
455 goto exit;
457 if (signature_length != mbedtls_rsa_get_len(rsa)) {
458 status = PSA_ERROR_INVALID_SIGNATURE;
459 goto exit;
462 #if defined(BUILTIN_ALG_RSA_PKCS1V15_SIGN)
463 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg)) {
464 mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V15,
465 MBEDTLS_MD_NONE);
466 ret = mbedtls_rsa_pkcs1_verify(rsa,
467 mbedtls_psa_get_random,
468 MBEDTLS_PSA_RANDOM_STATE,
469 MBEDTLS_RSA_PUBLIC,
470 md_alg,
471 (unsigned int) hash_length,
472 hash,
473 signature);
474 } else
475 #endif /* BUILTIN_ALG_RSA_PKCS1V15_SIGN */
476 #if defined(BUILTIN_ALG_RSA_PSS)
477 if (PSA_ALG_IS_RSA_PSS(alg)) {
478 mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
479 ret = mbedtls_rsa_rsassa_pss_verify(rsa,
480 mbedtls_psa_get_random,
481 MBEDTLS_PSA_RANDOM_STATE,
482 MBEDTLS_RSA_PUBLIC,
483 MBEDTLS_MD_NONE,
484 (unsigned int) hash_length,
485 hash,
486 signature);
487 } else
488 #endif /* BUILTIN_ALG_RSA_PSS */
490 status = PSA_ERROR_INVALID_ARGUMENT;
491 goto exit;
494 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
495 * the rest of the signature is invalid". This has little use in
496 * practice and PSA doesn't report this distinction. */
497 status = (ret == MBEDTLS_ERR_RSA_INVALID_PADDING) ?
498 PSA_ERROR_INVALID_SIGNATURE :
499 mbedtls_to_psa_error(ret);
501 exit:
502 mbedtls_rsa_free(rsa);
503 mbedtls_free(rsa);
505 return (status);
508 #endif /* defined(BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
509 * defined(BUILTIN_ALG_RSA_PSS) */
511 #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
512 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
514 psa_status_t mbedtls_psa_rsa_import_key(
515 const psa_key_attributes_t *attributes,
516 const uint8_t *data, size_t data_length,
517 uint8_t *key_buffer, size_t key_buffer_size,
518 size_t *key_buffer_length, size_t *bits) {
519 return (rsa_import_key(attributes, data, data_length,
520 key_buffer, key_buffer_size,
521 key_buffer_length, bits));
524 psa_status_t mbedtls_psa_rsa_export_public_key(
525 const psa_key_attributes_t *attributes,
526 const uint8_t *key_buffer, size_t key_buffer_size,
527 uint8_t *data, size_t data_size, size_t *data_length) {
528 return (rsa_export_public_key(attributes, key_buffer, key_buffer_size,
529 data, data_size, data_length));
532 #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
533 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
535 #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
536 psa_status_t mbedtls_psa_rsa_generate_key(
537 const psa_key_attributes_t *attributes,
538 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length) {
539 return (rsa_generate_key(attributes, key_buffer, key_buffer_size,
540 key_buffer_length));
542 #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
544 #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
545 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
546 psa_status_t mbedtls_psa_rsa_sign_hash(
547 const psa_key_attributes_t *attributes,
548 const uint8_t *key_buffer, size_t key_buffer_size,
549 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
550 uint8_t *signature, size_t signature_size, size_t *signature_length) {
551 return (rsa_sign_hash(
552 attributes,
553 key_buffer, key_buffer_size,
554 alg, hash, hash_length,
555 signature, signature_size, signature_length));
558 psa_status_t mbedtls_psa_rsa_verify_hash(
559 const psa_key_attributes_t *attributes,
560 const uint8_t *key_buffer, size_t key_buffer_size,
561 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
562 const uint8_t *signature, size_t signature_length) {
563 return (rsa_verify_hash(
564 attributes,
565 key_buffer, key_buffer_size,
566 alg, hash, hash_length,
567 signature, signature_length));
569 #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
570 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
573 * BEYOND THIS POINT, TEST DRIVER ENTRY POINTS ONLY.
576 #if defined(PSA_CRYPTO_DRIVER_TEST)
578 #if defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_RSA_KEY_PAIR) || \
579 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_RSA_PUBLIC_KEY)
581 psa_status_t mbedtls_transparent_test_driver_rsa_import_key(
582 const psa_key_attributes_t *attributes,
583 const uint8_t *data, size_t data_length,
584 uint8_t *key_buffer, size_t key_buffer_size,
585 size_t *key_buffer_length, size_t *bits) {
586 return (rsa_import_key(attributes, data, data_length,
587 key_buffer, key_buffer_size,
588 key_buffer_length, bits));
591 psa_status_t mbedtls_transparent_test_driver_rsa_export_public_key(
592 const psa_key_attributes_t *attributes,
593 const uint8_t *key_buffer, size_t key_buffer_size,
594 uint8_t *data, size_t data_size, size_t *data_length) {
595 return (rsa_export_public_key(attributes, key_buffer, key_buffer_size,
596 data, data_size, data_length));
599 #endif /* defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_RSA_KEY_PAIR) ||
600 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_RSA_PUBLIC_KEY) */
602 #if defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_RSA_KEY_PAIR)
603 psa_status_t mbedtls_transparent_test_driver_rsa_generate_key(
604 const psa_key_attributes_t *attributes,
605 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length) {
606 return (rsa_generate_key(attributes, key_buffer, key_buffer_size,
607 key_buffer_length));
609 #endif /* defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_RSA_KEY_PAIR) */
611 #if defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PKCS1V15_SIGN) || \
612 defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PSS)
613 psa_status_t mbedtls_transparent_test_driver_rsa_sign_hash(
614 const psa_key_attributes_t *attributes,
615 const uint8_t *key_buffer, size_t key_buffer_size,
616 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
617 uint8_t *signature, size_t signature_size, size_t *signature_length) {
618 #if defined(MBEDTLS_RSA_C) && \
619 (defined(MBEDTLS_PKCS1_V15) || defined(MBEDTLS_PKCS1_V21))
620 return (rsa_sign_hash(
621 attributes,
622 key_buffer, key_buffer_size,
623 alg, hash, hash_length,
624 signature, signature_size, signature_length));
625 #else
626 (void)attributes;
627 (void)key_buffer;
628 (void)key_buffer_size;
629 (void)alg;
630 (void)hash;
631 (void)hash_length;
632 (void)signature;
633 (void)signature_size;
634 (void)signature_length;
635 return (PSA_ERROR_NOT_SUPPORTED);
636 #endif
639 psa_status_t mbedtls_transparent_test_driver_rsa_verify_hash(
640 const psa_key_attributes_t *attributes,
641 const uint8_t *key_buffer, size_t key_buffer_size,
642 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
643 const uint8_t *signature, size_t signature_length) {
644 #if defined(MBEDTLS_RSA_C) && \
645 (defined(MBEDTLS_PKCS1_V15) || defined(MBEDTLS_PKCS1_V21))
646 return (rsa_verify_hash(
647 attributes,
648 key_buffer, key_buffer_size,
649 alg, hash, hash_length,
650 signature, signature_length));
651 #else
652 (void)attributes;
653 (void)key_buffer;
654 (void)key_buffer_size;
655 (void)alg;
656 (void)hash;
657 (void)hash_length;
658 (void)signature;
659 (void)signature_length;
660 return (PSA_ERROR_NOT_SUPPORTED);
661 #endif
663 #endif /* defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PKCS1V15_SIGN) ||
664 * defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PSS) */
666 #endif /* PSA_CRYPTO_DRIVER_TEST */
668 #endif /* MBEDTLS_PSA_CRYPTO_C */