blob: df524e1bcbbccc9f9bf1e3f965a324b0acee968c [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#include "common.h"
22
23#if defined(MBEDTLS_PSA_CRYPTO_C)
24
25#include <psa/crypto.h>
26#include "psa_crypto_core.h"
Ronald Cron9e18fc12020-11-05 17:36:40 +010027#include "psa_crypto_random_impl.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010028#include "psa_crypto_rsa.h"
Steven Cooreman5f88e772021-03-15 11:07:12 +010029#include "psa_crypto_hash.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010030
31#include <stdlib.h>
32#include <string.h>
33#include "mbedtls/platform.h"
34#if !defined(MBEDTLS_PLATFORM_C)
35#define mbedtls_calloc calloc
36#define mbedtls_free free
37#endif
38
39#include <mbedtls/rsa.h>
40#include <mbedtls/error.h>
41#include <mbedtls/pk.h>
Dave Rodgman3b5e6f02021-04-06 17:58:16 +010042#include "pk_wrap.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010043
44#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Ronald Cron00b7bfc2020-11-25 15:25:26 +010045 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
Ronald Cron0266cfe2021-03-13 18:50:11 +010046 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
47 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
48 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
49 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Cron00b7bfc2020-11-25 15:25:26 +010050
51/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
52 * that are not a multiple of 8) well. For example, there is only
53 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
54 * way to return the exact bit size of a key.
55 * To keep things simple, reject non-byte-aligned key sizes. */
56static psa_status_t psa_check_rsa_key_byte_aligned(
57 const mbedtls_rsa_context *rsa )
58{
59 mbedtls_mpi n;
60 psa_status_t status;
61 mbedtls_mpi_init( &n );
62 status = mbedtls_to_psa_error(
63 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
64 if( status == PSA_SUCCESS )
65 {
66 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
67 status = PSA_ERROR_NOT_SUPPORTED;
68 }
69 mbedtls_mpi_free( &n );
70 return( status );
71}
72
73psa_status_t mbedtls_psa_rsa_load_representation(
74 psa_key_type_t type, const uint8_t *data, size_t data_length,
75 mbedtls_rsa_context **p_rsa )
76{
77 psa_status_t status;
78 mbedtls_pk_context ctx;
79 size_t bits;
80 mbedtls_pk_init( &ctx );
81
82 /* Parse the data. */
83 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
84 status = mbedtls_to_psa_error(
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +020085 mbedtls_pk_parse_key( &ctx, data, data_length, NULL, 0,
86 mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) );
Ronald Cron00b7bfc2020-11-25 15:25:26 +010087 else
88 status = mbedtls_to_psa_error(
89 mbedtls_pk_parse_public_key( &ctx, data, data_length ) );
90 if( status != PSA_SUCCESS )
91 goto exit;
92
93 /* We have something that the pkparse module recognizes. If it is a
94 * valid RSA key, store it. */
95 if( mbedtls_pk_get_type( &ctx ) != MBEDTLS_PK_RSA )
96 {
97 status = PSA_ERROR_INVALID_ARGUMENT;
98 goto exit;
99 }
100
101 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
102 * supports non-byte-aligned key sizes, but not well. For example,
103 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
104 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( mbedtls_pk_rsa( ctx ) ) );
105 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
106 {
107 status = PSA_ERROR_NOT_SUPPORTED;
108 goto exit;
109 }
110 status = psa_check_rsa_key_byte_aligned( mbedtls_pk_rsa( ctx ) );
111 if( status != PSA_SUCCESS )
112 goto exit;
113
114 /* Copy out the pointer to the RSA context, and reset the PK context
115 * such that pk_free doesn't free the RSA context we just grabbed. */
116 *p_rsa = mbedtls_pk_rsa( ctx );
117 ctx.pk_info = NULL;
118
119exit:
120 mbedtls_pk_free( &ctx );
121 return( status );
122}
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100123#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100124 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
Ronald Cron0266cfe2021-03-13 18:50:11 +0100125 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
126 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
127 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
128 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100129
Ronald Cron0266cfe2021-03-13 18:50:11 +0100130#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
131 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Cronabf2aef2020-11-27 18:13:44 +0100132
Ronald Cron0266cfe2021-03-13 18:50:11 +0100133psa_status_t mbedtls_psa_rsa_import_key(
Ronald Cronabf2aef2020-11-27 18:13:44 +0100134 const psa_key_attributes_t *attributes,
135 const uint8_t *data, size_t data_length,
136 uint8_t *key_buffer, size_t key_buffer_size,
137 size_t *key_buffer_length, size_t *bits )
138{
139 psa_status_t status;
140 mbedtls_rsa_context *rsa = NULL;
141
142 /* Parse input */
143 status = mbedtls_psa_rsa_load_representation( attributes->core.type,
144 data,
145 data_length,
146 &rsa );
147 if( status != PSA_SUCCESS )
148 goto exit;
149
150 *bits = (psa_key_bits_t) PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
151
152 /* Re-export the data to PSA export format, such that we can store export
153 * representation in the key slot. Export representation in case of RSA is
154 * the smallest representation that's allowed as input, so a straight-up
155 * allocation of the same size as the input buffer will be large enough. */
156 status = mbedtls_psa_rsa_export_key( attributes->core.type,
157 rsa,
158 key_buffer,
159 key_buffer_size,
160 key_buffer_length );
161exit:
162 /* Always free the RSA object */
163 mbedtls_rsa_free( rsa );
164 mbedtls_free( rsa );
165
166 return( status );
167}
168
Ronald Crone5ca3d82020-11-26 16:36:16 +0100169psa_status_t mbedtls_psa_rsa_export_key( psa_key_type_t type,
170 mbedtls_rsa_context *rsa,
171 uint8_t *data,
172 size_t data_size,
173 size_t *data_length )
174{
175#if defined(MBEDTLS_PK_WRITE_C)
176 int ret;
177 mbedtls_pk_context pk;
178 uint8_t *pos = data + data_size;
179
180 mbedtls_pk_init( &pk );
181 pk.pk_info = &mbedtls_rsa_info;
182 pk.pk_ctx = rsa;
183
184 /* PSA Crypto API defines the format of an RSA key as a DER-encoded
185 * representation of the non-encrypted PKCS#1 RSAPrivateKey for a
186 * private key and of the RFC3279 RSAPublicKey for a public key. */
187 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
188 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
189 else
190 ret = mbedtls_pk_write_pubkey( &pos, data, &pk );
191
192 if( ret < 0 )
193 {
194 /* Clean up in case pk_write failed halfway through. */
195 memset( data, 0, data_size );
196 return( mbedtls_to_psa_error( ret ) );
197 }
198
199 /* The mbedtls_pk_xxx functions write to the end of the buffer.
200 * Move the data to the beginning and erase remaining data
201 * at the original location. */
202 if( 2 * (size_t) ret <= data_size )
203 {
204 memcpy( data, data + data_size - ret, ret );
205 memset( data + data_size - ret, 0, ret );
206 }
207 else if( (size_t) ret < data_size )
208 {
209 memmove( data, data + data_size - ret, ret );
210 memset( data + ret, 0, data_size - ret );
211 }
212
213 *data_length = ret;
214 return( PSA_SUCCESS );
215#else
216 (void) type;
217 (void) rsa;
218 (void) data;
219 (void) data_size;
220 (void) data_length;
221 return( PSA_ERROR_NOT_SUPPORTED );
222#endif /* MBEDTLS_PK_WRITE_C */
223}
224
Ronald Cron0266cfe2021-03-13 18:50:11 +0100225psa_status_t mbedtls_psa_rsa_export_public_key(
Ronald Crone5ca3d82020-11-26 16:36:16 +0100226 const psa_key_attributes_t *attributes,
227 const uint8_t *key_buffer, size_t key_buffer_size,
228 uint8_t *data, size_t data_size, size_t *data_length )
229{
230 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
231 mbedtls_rsa_context *rsa = NULL;
232
233 status = mbedtls_psa_rsa_load_representation(
234 attributes->core.type, key_buffer, key_buffer_size, &rsa );
235 if( status != PSA_SUCCESS )
236 return( status );
237
238 status = mbedtls_psa_rsa_export_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY,
239 rsa,
240 data,
241 data_size,
242 data_length );
243
244 mbedtls_rsa_free( rsa );
245 mbedtls_free( rsa );
246
247 return( status );
248}
Ronald Cron0266cfe2021-03-13 18:50:11 +0100249#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
250 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cronf1057d32020-11-26 19:19:10 +0100251
Ronald Cron0266cfe2021-03-13 18:50:11 +0100252#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
Jaeden Amero424fa932021-05-14 08:34:32 +0100253 defined(MBEDTLS_GENPRIME)
Ronald Cron2365fde2021-02-08 09:52:24 +0100254static psa_status_t psa_rsa_read_exponent( const uint8_t *domain_parameters,
Ronald Cron9e18fc12020-11-05 17:36:40 +0100255 size_t domain_parameters_size,
256 int *exponent )
257{
258 size_t i;
259 uint32_t acc = 0;
260
261 if( domain_parameters_size == 0 )
262 {
263 *exponent = 65537;
264 return( PSA_SUCCESS );
265 }
266
267 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
268 * support values that fit in a 32-bit integer, which is larger than
269 * int on just about every platform anyway. */
270 if( domain_parameters_size > sizeof( acc ) )
271 return( PSA_ERROR_NOT_SUPPORTED );
272 for( i = 0; i < domain_parameters_size; i++ )
273 acc = ( acc << 8 ) | domain_parameters[i];
274 if( acc > INT_MAX )
275 return( PSA_ERROR_NOT_SUPPORTED );
276 *exponent = acc;
277 return( PSA_SUCCESS );
278}
279
Ronald Cron0266cfe2021-03-13 18:50:11 +0100280psa_status_t mbedtls_psa_rsa_generate_key(
Ronald Cron9e18fc12020-11-05 17:36:40 +0100281 const psa_key_attributes_t *attributes,
282 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
283{
284 psa_status_t status;
285 mbedtls_rsa_context rsa;
286 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
287 int exponent;
288
Ronald Cron2365fde2021-02-08 09:52:24 +0100289 status = psa_rsa_read_exponent( attributes->domain_parameters,
Ronald Cron9e18fc12020-11-05 17:36:40 +0100290 attributes->domain_parameters_size,
291 &exponent );
292 if( status != PSA_SUCCESS )
293 return( status );
294
Ronald Cronc1905a12021-06-05 11:11:14 +0200295 mbedtls_rsa_init( &rsa );
Ronald Cron9e18fc12020-11-05 17:36:40 +0100296 ret = mbedtls_rsa_gen_key( &rsa,
297 mbedtls_psa_get_random,
298 MBEDTLS_PSA_RANDOM_STATE,
299 (unsigned int)attributes->core.bits,
300 exponent );
301 if( ret != 0 )
302 return( mbedtls_to_psa_error( ret ) );
303
304 status = mbedtls_psa_rsa_export_key( attributes->core.type,
305 &rsa, key_buffer, key_buffer_size,
306 key_buffer_length );
307 mbedtls_rsa_free( &rsa );
308
309 return( status );
310}
Ronald Cron0266cfe2021-03-13 18:50:11 +0100311#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Jaeden Amero424fa932021-05-14 08:34:32 +0100312 * defined(MBEDTLS_GENPRIME) */
Ronald Cron9e18fc12020-11-05 17:36:40 +0100313
Ronald Cron7bdbca32020-12-09 13:34:54 +0100314/****************************************************************/
315/* Sign/verify hashes */
316/****************************************************************/
317
Ronald Cron0266cfe2021-03-13 18:50:11 +0100318#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
319 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100320
321/* Decode the hash algorithm from alg and store the mbedtls encoding in
322 * md_alg. Verify that the hash length is acceptable. */
323static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
324 size_t hash_length,
325 mbedtls_md_type_t *md_alg )
326{
327 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
328 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
329 *md_alg = mbedtls_md_get_type( md_info );
330
331 /* The Mbed TLS RSA module uses an unsigned int for hash length
332 * parameters. Validate that it fits so that we don't risk an
333 * overflow later. */
334#if SIZE_MAX > UINT_MAX
335 if( hash_length > UINT_MAX )
336 return( PSA_ERROR_INVALID_ARGUMENT );
337#endif
338
Janos Follath0af093b2021-06-07 14:34:10 +0100339 /* For signatures using a hash, the hash length must be correct. */
340 if( alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Ronald Cron7bdbca32020-12-09 13:34:54 +0100341 {
342 if( md_info == NULL )
343 return( PSA_ERROR_NOT_SUPPORTED );
344 if( mbedtls_md_get_size( md_info ) != hash_length )
345 return( PSA_ERROR_INVALID_ARGUMENT );
346 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100347
348 return( PSA_SUCCESS );
349}
350
Ronald Cron0266cfe2021-03-13 18:50:11 +0100351psa_status_t mbedtls_psa_rsa_sign_hash(
Ronald Cron7bdbca32020-12-09 13:34:54 +0100352 const psa_key_attributes_t *attributes,
353 const uint8_t *key_buffer, size_t key_buffer_size,
354 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
355 uint8_t *signature, size_t signature_size, size_t *signature_length )
356{
357 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
358 mbedtls_rsa_context *rsa = NULL;
359 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
360 mbedtls_md_type_t md_alg;
361
362 status = mbedtls_psa_rsa_load_representation( attributes->core.type,
363 key_buffer,
364 key_buffer_size,
365 &rsa );
366 if( status != PSA_SUCCESS )
367 return( status );
368
369 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
370 if( status != PSA_SUCCESS )
371 goto exit;
372
373 if( signature_size < mbedtls_rsa_get_len( rsa ) )
374 {
375 status = PSA_ERROR_BUFFER_TOO_SMALL;
376 goto exit;
377 }
378
Ronald Cron0266cfe2021-03-13 18:50:11 +0100379#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100380 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
381 {
Ronald Cronea7631b2021-06-03 18:51:59 +0200382 ret = mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
383 MBEDTLS_MD_NONE );
384 if( ret == 0 )
385 {
386 ret = mbedtls_rsa_pkcs1_sign( rsa,
387 mbedtls_psa_get_random,
388 MBEDTLS_PSA_RANDOM_STATE,
389 md_alg,
390 (unsigned int) hash_length,
391 hash,
392 signature );
393 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100394 }
395 else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100396#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
397#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100398 if( PSA_ALG_IS_RSA_PSS( alg ) )
399 {
Ronald Cronea7631b2021-06-03 18:51:59 +0200400 ret = mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
401
402 if( ret == 0 )
403 {
404 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
405 mbedtls_psa_get_random,
406 MBEDTLS_PSA_RANDOM_STATE,
407 MBEDTLS_MD_NONE,
408 (unsigned int) hash_length,
409 hash,
410 signature );
411 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100412 }
413 else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100414#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Ronald Cron7bdbca32020-12-09 13:34:54 +0100415 {
416 status = PSA_ERROR_INVALID_ARGUMENT;
417 goto exit;
418 }
419
420 if( ret == 0 )
421 *signature_length = mbedtls_rsa_get_len( rsa );
422 status = mbedtls_to_psa_error( ret );
423
424exit:
425 mbedtls_rsa_free( rsa );
426 mbedtls_free( rsa );
427
428 return( status );
429}
430
Ronald Cron0266cfe2021-03-13 18:50:11 +0100431#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200432static int rsa_pss_expected_salt_len( psa_algorithm_t alg,
433 const mbedtls_rsa_context *rsa,
434 size_t hash_length )
435{
436 if( PSA_ALG_IS_RSA_PSS_ANY_SALT( alg ) )
437 return( MBEDTLS_RSA_SALT_LEN_ANY );
438 /* Otherwise: standard salt length, i.e. largest possible salt length
439 * up to the hash length. */
Gilles Peskinef6892de2021-10-08 16:28:32 +0200440 int klen = (int) mbedtls_rsa_get_len( rsa ); // known to fit
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200441 int hlen = (int) hash_length; // known to fit
442 int room = klen - 2 - hlen;
443 if( room < 0 )
444 return( 0 ); // there is no valid signature in this case anyway
445 else if( room > hlen )
446 return( hlen );
447 else
448 return( room );
449}
Ronald Cron0266cfe2021-03-13 18:50:11 +0100450#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200451
Ronald Cron0266cfe2021-03-13 18:50:11 +0100452psa_status_t mbedtls_psa_rsa_verify_hash(
Ronald Cron7bdbca32020-12-09 13:34:54 +0100453 const psa_key_attributes_t *attributes,
454 const uint8_t *key_buffer, size_t key_buffer_size,
455 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
456 const uint8_t *signature, size_t signature_length )
457{
458 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
459 mbedtls_rsa_context *rsa = NULL;
460 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
461 mbedtls_md_type_t md_alg;
462
463 status = mbedtls_psa_rsa_load_representation( attributes->core.type,
464 key_buffer,
465 key_buffer_size,
466 &rsa );
467 if( status != PSA_SUCCESS )
468 goto exit;
469
470 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
471 if( status != PSA_SUCCESS )
472 goto exit;
473
474 if( signature_length != mbedtls_rsa_get_len( rsa ) )
475 {
476 status = PSA_ERROR_INVALID_SIGNATURE;
477 goto exit;
478 }
479
Ronald Cron0266cfe2021-03-13 18:50:11 +0100480#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100481 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
482 {
Ronald Cronea7631b2021-06-03 18:51:59 +0200483 ret = mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
484 MBEDTLS_MD_NONE );
485 if( ret == 0 )
486 {
487 ret = mbedtls_rsa_pkcs1_verify( rsa,
488 md_alg,
489 (unsigned int) hash_length,
490 hash,
491 signature );
492 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100493 }
494 else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100495#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
496#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100497 if( PSA_ALG_IS_RSA_PSS( alg ) )
498 {
Ronald Cronea7631b2021-06-03 18:51:59 +0200499 ret = mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
500 if( ret == 0 )
501 {
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200502 int slen = rsa_pss_expected_salt_len( alg, rsa, hash_length );
503 ret = mbedtls_rsa_rsassa_pss_verify_ext( rsa,
504 md_alg,
505 (unsigned) hash_length,
506 hash,
507 md_alg,
508 slen,
509 signature );
Ronald Cronea7631b2021-06-03 18:51:59 +0200510 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100511 }
512 else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100513#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Ronald Cron7bdbca32020-12-09 13:34:54 +0100514 {
515 status = PSA_ERROR_INVALID_ARGUMENT;
516 goto exit;
517 }
518
519 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
520 * the rest of the signature is invalid". This has little use in
521 * practice and PSA doesn't report this distinction. */
522 status = ( ret == MBEDTLS_ERR_RSA_INVALID_PADDING ) ?
523 PSA_ERROR_INVALID_SIGNATURE :
524 mbedtls_to_psa_error( ret );
525
526exit:
527 mbedtls_rsa_free( rsa );
528 mbedtls_free( rsa );
529
530 return( status );
531}
532
Ronald Crond2fb8542020-12-09 15:18:01 +0100533#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
534 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
535
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100536#endif /* MBEDTLS_PSA_CRYPTO_C */