blob: b79ba5d04dd5439e2c81bc5b5fd108fb93368e5a [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>
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +010026#include "psa/crypto_values.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010027#include "psa_crypto_core.h"
Ronald Cron9e18fc12020-11-05 17:36:40 +010028#include "psa_crypto_random_impl.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010029#include "psa_crypto_rsa.h"
Steven Cooreman5f88e772021-03-15 11:07:12 +010030#include "psa_crypto_hash.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010031
32#include <stdlib.h>
33#include <string.h>
34#include "mbedtls/platform.h"
35#if !defined(MBEDTLS_PLATFORM_C)
36#define mbedtls_calloc calloc
37#define mbedtls_free free
38#endif
39
40#include <mbedtls/rsa.h>
41#include <mbedtls/error.h>
42#include <mbedtls/pk.h>
Dave Rodgman3b5e6f02021-04-06 17:58:16 +010043#include "pk_wrap.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010044
45#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Ronald Cron00b7bfc2020-11-25 15:25:26 +010046 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
Ronald Cron0266cfe2021-03-13 18:50:11 +010047 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
48 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
49 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
50 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Cron00b7bfc2020-11-25 15:25:26 +010051
52/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
53 * that are not a multiple of 8) well. For example, there is only
54 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
55 * way to return the exact bit size of a key.
56 * To keep things simple, reject non-byte-aligned key sizes. */
57static psa_status_t psa_check_rsa_key_byte_aligned(
58 const mbedtls_rsa_context *rsa )
59{
60 mbedtls_mpi n;
61 psa_status_t status;
62 mbedtls_mpi_init( &n );
63 status = mbedtls_to_psa_error(
64 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
65 if( status == PSA_SUCCESS )
66 {
67 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
68 status = PSA_ERROR_NOT_SUPPORTED;
69 }
70 mbedtls_mpi_free( &n );
71 return( status );
72}
73
74psa_status_t mbedtls_psa_rsa_load_representation(
75 psa_key_type_t type, const uint8_t *data, size_t data_length,
76 mbedtls_rsa_context **p_rsa )
77{
78 psa_status_t status;
79 mbedtls_pk_context ctx;
80 size_t bits;
81 mbedtls_pk_init( &ctx );
82
83 /* Parse the data. */
84 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
85 status = mbedtls_to_psa_error(
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +020086 mbedtls_pk_parse_key( &ctx, data, data_length, NULL, 0,
87 mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) );
Ronald Cron00b7bfc2020-11-25 15:25:26 +010088 else
89 status = mbedtls_to_psa_error(
90 mbedtls_pk_parse_public_key( &ctx, data, data_length ) );
91 if( status != PSA_SUCCESS )
92 goto exit;
93
94 /* We have something that the pkparse module recognizes. If it is a
95 * valid RSA key, store it. */
96 if( mbedtls_pk_get_type( &ctx ) != MBEDTLS_PK_RSA )
97 {
98 status = PSA_ERROR_INVALID_ARGUMENT;
99 goto exit;
100 }
101
102 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
103 * supports non-byte-aligned key sizes, but not well. For example,
104 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
105 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( mbedtls_pk_rsa( ctx ) ) );
106 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
107 {
108 status = PSA_ERROR_NOT_SUPPORTED;
109 goto exit;
110 }
111 status = psa_check_rsa_key_byte_aligned( mbedtls_pk_rsa( ctx ) );
112 if( status != PSA_SUCCESS )
113 goto exit;
114
115 /* Copy out the pointer to the RSA context, and reset the PK context
116 * such that pk_free doesn't free the RSA context we just grabbed. */
117 *p_rsa = mbedtls_pk_rsa( ctx );
118 ctx.pk_info = NULL;
119
120exit:
121 mbedtls_pk_free( &ctx );
122 return( status );
123}
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100124#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100125 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
Ronald Cron0266cfe2021-03-13 18:50:11 +0100126 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
127 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
128 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
129 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100130
Ronald Cron0266cfe2021-03-13 18:50:11 +0100131#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
132 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Cronabf2aef2020-11-27 18:13:44 +0100133
Ronald Cron0266cfe2021-03-13 18:50:11 +0100134psa_status_t mbedtls_psa_rsa_import_key(
Ronald Cronabf2aef2020-11-27 18:13:44 +0100135 const psa_key_attributes_t *attributes,
136 const uint8_t *data, size_t data_length,
137 uint8_t *key_buffer, size_t key_buffer_size,
138 size_t *key_buffer_length, size_t *bits )
139{
140 psa_status_t status;
141 mbedtls_rsa_context *rsa = NULL;
142
143 /* Parse input */
144 status = mbedtls_psa_rsa_load_representation( attributes->core.type,
145 data,
146 data_length,
147 &rsa );
148 if( status != PSA_SUCCESS )
149 goto exit;
150
151 *bits = (psa_key_bits_t) PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
152
153 /* Re-export the data to PSA export format, such that we can store export
154 * representation in the key slot. Export representation in case of RSA is
155 * the smallest representation that's allowed as input, so a straight-up
156 * allocation of the same size as the input buffer will be large enough. */
157 status = mbedtls_psa_rsa_export_key( attributes->core.type,
158 rsa,
159 key_buffer,
160 key_buffer_size,
161 key_buffer_length );
162exit:
163 /* Always free the RSA object */
164 mbedtls_rsa_free( rsa );
165 mbedtls_free( rsa );
166
167 return( status );
168}
169
Ronald Crone5ca3d82020-11-26 16:36:16 +0100170psa_status_t mbedtls_psa_rsa_export_key( psa_key_type_t type,
171 mbedtls_rsa_context *rsa,
172 uint8_t *data,
173 size_t data_size,
174 size_t *data_length )
175{
176#if defined(MBEDTLS_PK_WRITE_C)
177 int ret;
178 mbedtls_pk_context pk;
179 uint8_t *pos = data + data_size;
180
181 mbedtls_pk_init( &pk );
182 pk.pk_info = &mbedtls_rsa_info;
183 pk.pk_ctx = rsa;
184
185 /* PSA Crypto API defines the format of an RSA key as a DER-encoded
186 * representation of the non-encrypted PKCS#1 RSAPrivateKey for a
187 * private key and of the RFC3279 RSAPublicKey for a public key. */
188 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
189 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
190 else
191 ret = mbedtls_pk_write_pubkey( &pos, data, &pk );
192
193 if( ret < 0 )
194 {
195 /* Clean up in case pk_write failed halfway through. */
196 memset( data, 0, data_size );
197 return( mbedtls_to_psa_error( ret ) );
198 }
199
200 /* The mbedtls_pk_xxx functions write to the end of the buffer.
201 * Move the data to the beginning and erase remaining data
202 * at the original location. */
203 if( 2 * (size_t) ret <= data_size )
204 {
205 memcpy( data, data + data_size - ret, ret );
206 memset( data + data_size - ret, 0, ret );
207 }
208 else if( (size_t) ret < data_size )
209 {
210 memmove( data, data + data_size - ret, ret );
211 memset( data + ret, 0, data_size - ret );
212 }
213
214 *data_length = ret;
215 return( PSA_SUCCESS );
216#else
217 (void) type;
218 (void) rsa;
219 (void) data;
220 (void) data_size;
221 (void) data_length;
222 return( PSA_ERROR_NOT_SUPPORTED );
223#endif /* MBEDTLS_PK_WRITE_C */
224}
225
Ronald Cron0266cfe2021-03-13 18:50:11 +0100226psa_status_t mbedtls_psa_rsa_export_public_key(
Ronald Crone5ca3d82020-11-26 16:36:16 +0100227 const psa_key_attributes_t *attributes,
228 const uint8_t *key_buffer, size_t key_buffer_size,
229 uint8_t *data, size_t data_size, size_t *data_length )
230{
231 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
232 mbedtls_rsa_context *rsa = NULL;
233
234 status = mbedtls_psa_rsa_load_representation(
235 attributes->core.type, key_buffer, key_buffer_size, &rsa );
236 if( status != PSA_SUCCESS )
237 return( status );
238
239 status = mbedtls_psa_rsa_export_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY,
240 rsa,
241 data,
242 data_size,
243 data_length );
244
245 mbedtls_rsa_free( rsa );
246 mbedtls_free( rsa );
247
248 return( status );
249}
Ronald Cron0266cfe2021-03-13 18:50:11 +0100250#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
251 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cronf1057d32020-11-26 19:19:10 +0100252
Ronald Cron0266cfe2021-03-13 18:50:11 +0100253#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
Jaeden Amero424fa932021-05-14 08:34:32 +0100254 defined(MBEDTLS_GENPRIME)
Ronald Cron2365fde2021-02-08 09:52:24 +0100255static psa_status_t psa_rsa_read_exponent( const uint8_t *domain_parameters,
Ronald Cron9e18fc12020-11-05 17:36:40 +0100256 size_t domain_parameters_size,
257 int *exponent )
258{
259 size_t i;
260 uint32_t acc = 0;
261
262 if( domain_parameters_size == 0 )
263 {
264 *exponent = 65537;
265 return( PSA_SUCCESS );
266 }
267
268 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
269 * support values that fit in a 32-bit integer, which is larger than
270 * int on just about every platform anyway. */
271 if( domain_parameters_size > sizeof( acc ) )
272 return( PSA_ERROR_NOT_SUPPORTED );
273 for( i = 0; i < domain_parameters_size; i++ )
274 acc = ( acc << 8 ) | domain_parameters[i];
275 if( acc > INT_MAX )
276 return( PSA_ERROR_NOT_SUPPORTED );
277 *exponent = acc;
278 return( PSA_SUCCESS );
279}
280
Ronald Cron0266cfe2021-03-13 18:50:11 +0100281psa_status_t mbedtls_psa_rsa_generate_key(
Ronald Cron9e18fc12020-11-05 17:36:40 +0100282 const psa_key_attributes_t *attributes,
283 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
284{
285 psa_status_t status;
286 mbedtls_rsa_context rsa;
287 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
288 int exponent;
289
Ronald Cron2365fde2021-02-08 09:52:24 +0100290 status = psa_rsa_read_exponent( attributes->domain_parameters,
Ronald Cron9e18fc12020-11-05 17:36:40 +0100291 attributes->domain_parameters_size,
292 &exponent );
293 if( status != PSA_SUCCESS )
294 return( status );
295
Ronald Cronc1905a12021-06-05 11:11:14 +0200296 mbedtls_rsa_init( &rsa );
Ronald Cron9e18fc12020-11-05 17:36:40 +0100297 ret = mbedtls_rsa_gen_key( &rsa,
298 mbedtls_psa_get_random,
299 MBEDTLS_PSA_RANDOM_STATE,
300 (unsigned int)attributes->core.bits,
301 exponent );
302 if( ret != 0 )
303 return( mbedtls_to_psa_error( ret ) );
304
305 status = mbedtls_psa_rsa_export_key( attributes->core.type,
306 &rsa, key_buffer, key_buffer_size,
307 key_buffer_length );
308 mbedtls_rsa_free( &rsa );
309
310 return( status );
311}
Ronald Cron0266cfe2021-03-13 18:50:11 +0100312#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Jaeden Amero424fa932021-05-14 08:34:32 +0100313 * defined(MBEDTLS_GENPRIME) */
Ronald Cron9e18fc12020-11-05 17:36:40 +0100314
Ronald Cron7bdbca32020-12-09 13:34:54 +0100315/****************************************************************/
316/* Sign/verify hashes */
317/****************************************************************/
318
Ronald Cron0266cfe2021-03-13 18:50:11 +0100319#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
320 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100321
322/* Decode the hash algorithm from alg and store the mbedtls encoding in
323 * md_alg. Verify that the hash length is acceptable. */
324static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
325 size_t hash_length,
326 mbedtls_md_type_t *md_alg )
327{
328 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
329 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
330 *md_alg = mbedtls_md_get_type( md_info );
331
332 /* The Mbed TLS RSA module uses an unsigned int for hash length
333 * parameters. Validate that it fits so that we don't risk an
334 * overflow later. */
335#if SIZE_MAX > UINT_MAX
336 if( hash_length > UINT_MAX )
337 return( PSA_ERROR_INVALID_ARGUMENT );
338#endif
339
Janos Follath0af093b2021-06-07 14:34:10 +0100340 /* For signatures using a hash, the hash length must be correct. */
341 if( alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Ronald Cron7bdbca32020-12-09 13:34:54 +0100342 {
343 if( md_info == NULL )
344 return( PSA_ERROR_NOT_SUPPORTED );
345 if( mbedtls_md_get_size( md_info ) != hash_length )
346 return( PSA_ERROR_INVALID_ARGUMENT );
347 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100348
349 return( PSA_SUCCESS );
350}
351
Ronald Cron0266cfe2021-03-13 18:50:11 +0100352psa_status_t mbedtls_psa_rsa_sign_hash(
Ronald Cron7bdbca32020-12-09 13:34:54 +0100353 const psa_key_attributes_t *attributes,
354 const uint8_t *key_buffer, size_t key_buffer_size,
355 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
356 uint8_t *signature, size_t signature_size, size_t *signature_length )
357{
358 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
359 mbedtls_rsa_context *rsa = NULL;
360 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
361 mbedtls_md_type_t md_alg;
362
363 status = mbedtls_psa_rsa_load_representation( attributes->core.type,
364 key_buffer,
365 key_buffer_size,
366 &rsa );
367 if( status != PSA_SUCCESS )
368 return( status );
369
370 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
371 if( status != PSA_SUCCESS )
372 goto exit;
373
374 if( signature_size < mbedtls_rsa_get_len( rsa ) )
375 {
376 status = PSA_ERROR_BUFFER_TOO_SMALL;
377 goto exit;
378 }
379
Ronald Cron0266cfe2021-03-13 18:50:11 +0100380#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100381 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
382 {
Ronald Cronea7631b2021-06-03 18:51:59 +0200383 ret = mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
384 MBEDTLS_MD_NONE );
385 if( ret == 0 )
386 {
387 ret = mbedtls_rsa_pkcs1_sign( rsa,
388 mbedtls_psa_get_random,
389 MBEDTLS_PSA_RANDOM_STATE,
390 md_alg,
391 (unsigned int) hash_length,
392 hash,
393 signature );
394 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100395 }
396 else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100397#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
398#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100399 if( PSA_ALG_IS_RSA_PSS( alg ) )
400 {
Ronald Cronea7631b2021-06-03 18:51:59 +0200401 ret = mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
402
403 if( ret == 0 )
404 {
405 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
406 mbedtls_psa_get_random,
407 MBEDTLS_PSA_RANDOM_STATE,
408 MBEDTLS_MD_NONE,
409 (unsigned int) hash_length,
410 hash,
411 signature );
412 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100413 }
414 else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100415#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Ronald Cron7bdbca32020-12-09 13:34:54 +0100416 {
417 status = PSA_ERROR_INVALID_ARGUMENT;
418 goto exit;
419 }
420
421 if( ret == 0 )
422 *signature_length = mbedtls_rsa_get_len( rsa );
423 status = mbedtls_to_psa_error( ret );
424
425exit:
426 mbedtls_rsa_free( rsa );
427 mbedtls_free( rsa );
428
429 return( status );
430}
431
Ronald Cron0266cfe2021-03-13 18:50:11 +0100432#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200433static int rsa_pss_expected_salt_len( psa_algorithm_t alg,
434 const mbedtls_rsa_context *rsa,
435 size_t hash_length )
436{
437 if( PSA_ALG_IS_RSA_PSS_ANY_SALT( alg ) )
438 return( MBEDTLS_RSA_SALT_LEN_ANY );
439 /* Otherwise: standard salt length, i.e. largest possible salt length
440 * up to the hash length. */
Gilles Peskinef6892de2021-10-08 16:28:32 +0200441 int klen = (int) mbedtls_rsa_get_len( rsa ); // known to fit
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200442 int hlen = (int) hash_length; // known to fit
443 int room = klen - 2 - hlen;
444 if( room < 0 )
445 return( 0 ); // there is no valid signature in this case anyway
446 else if( room > hlen )
447 return( hlen );
448 else
449 return( room );
450}
Ronald Cron0266cfe2021-03-13 18:50:11 +0100451#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200452
Ronald Cron0266cfe2021-03-13 18:50:11 +0100453psa_status_t mbedtls_psa_rsa_verify_hash(
Ronald Cron7bdbca32020-12-09 13:34:54 +0100454 const psa_key_attributes_t *attributes,
455 const uint8_t *key_buffer, size_t key_buffer_size,
456 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
457 const uint8_t *signature, size_t signature_length )
458{
459 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
460 mbedtls_rsa_context *rsa = NULL;
461 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
462 mbedtls_md_type_t md_alg;
463
464 status = mbedtls_psa_rsa_load_representation( attributes->core.type,
465 key_buffer,
466 key_buffer_size,
467 &rsa );
468 if( status != PSA_SUCCESS )
469 goto exit;
470
471 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
472 if( status != PSA_SUCCESS )
473 goto exit;
474
475 if( signature_length != mbedtls_rsa_get_len( rsa ) )
476 {
477 status = PSA_ERROR_INVALID_SIGNATURE;
478 goto exit;
479 }
480
Ronald Cron0266cfe2021-03-13 18:50:11 +0100481#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100482 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
483 {
Ronald Cronea7631b2021-06-03 18:51:59 +0200484 ret = mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
485 MBEDTLS_MD_NONE );
486 if( ret == 0 )
487 {
488 ret = mbedtls_rsa_pkcs1_verify( rsa,
489 md_alg,
490 (unsigned int) hash_length,
491 hash,
492 signature );
493 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100494 }
495 else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100496#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
497#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100498 if( PSA_ALG_IS_RSA_PSS( alg ) )
499 {
Ronald Cronea7631b2021-06-03 18:51:59 +0200500 ret = mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
501 if( ret == 0 )
502 {
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200503 int slen = rsa_pss_expected_salt_len( alg, rsa, hash_length );
504 ret = mbedtls_rsa_rsassa_pss_verify_ext( rsa,
505 md_alg,
506 (unsigned) hash_length,
507 hash,
508 md_alg,
509 slen,
510 signature );
Ronald Cronea7631b2021-06-03 18:51:59 +0200511 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100512 }
513 else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100514#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Ronald Cron7bdbca32020-12-09 13:34:54 +0100515 {
516 status = PSA_ERROR_INVALID_ARGUMENT;
517 goto exit;
518 }
519
520 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
521 * the rest of the signature is invalid". This has little use in
522 * practice and PSA doesn't report this distinction. */
523 status = ( ret == MBEDTLS_ERR_RSA_INVALID_PADDING ) ?
524 PSA_ERROR_INVALID_SIGNATURE :
525 mbedtls_to_psa_error( ret );
526
527exit:
528 mbedtls_rsa_free( rsa );
529 mbedtls_free( rsa );
530
531 return( status );
532}
533
Ronald Crond2fb8542020-12-09 15:18:01 +0100534#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
535 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
536
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100537/****************************************************************/
538/* Asymmetric cryptography */
539/****************************************************************/
540
541#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
542static int psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
543 mbedtls_rsa_context *rsa )
544{
545 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
546 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
547 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
548
549 return( mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ) );
550}
551#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
552
553psa_status_t mbedtls_psa_asymmetric_encrypt( const psa_key_attributes_t *attributes,
554 const uint8_t *key_buffer,
555 size_t key_buffer_size,
556 psa_algorithm_t alg,
557 const uint8_t *input,
558 size_t input_length,
559 const uint8_t *salt,
560 size_t salt_length,
561 uint8_t *output,
562 size_t output_size,
563 size_t *output_length )
564{
565 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
566 (void) key_buffer;
567 (void) key_buffer_size;
568 (void) input;
569 (void) input_length;
570 (void) salt;
571 (void) salt_length;
572 (void) output;
573 (void) output_size;
574 (void) output_length;
575
576 if( PSA_KEY_TYPE_IS_RSA( attributes->core.type ) )
577 {
578#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
579 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
580 mbedtls_rsa_context *rsa = NULL;
581 status = mbedtls_psa_rsa_load_representation( attributes->core.type,
582 key_buffer,
583 key_buffer_size,
584 &rsa );
585 if( status != PSA_SUCCESS )
586 goto rsa_exit;
587
588 if( output_size < mbedtls_rsa_get_len( rsa ) )
589 {
590 status = PSA_ERROR_BUFFER_TOO_SMALL;
591 goto rsa_exit;
592 }
593#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
594 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
595 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
596 {
597#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
598 status = mbedtls_to_psa_error(
599 mbedtls_rsa_pkcs1_encrypt( rsa,
600 mbedtls_psa_get_random,
601 MBEDTLS_PSA_RANDOM_STATE,
602 input_length,
603 input,
604 output ) );
605#else
606 status = PSA_ERROR_NOT_SUPPORTED;
607#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
608 }
609 else
610 if( PSA_ALG_IS_RSA_OAEP( alg ) )
611 {
612#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
613 status = mbedtls_to_psa_error(
614 psa_rsa_oaep_set_padding_mode( alg, rsa ) );
615 if( status != PSA_SUCCESS )
616 goto rsa_exit;
617
618 status = mbedtls_to_psa_error(
619 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
620 mbedtls_psa_get_random,
621 MBEDTLS_PSA_RANDOM_STATE,
622 salt, salt_length,
623 input_length,
624 input,
625 output ) );
626#else
627 status = PSA_ERROR_NOT_SUPPORTED;
628#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
629 }
630 else
631 {
632 status = PSA_ERROR_INVALID_ARGUMENT;
633 }
634#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
635 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
636rsa_exit:
637 if( status == PSA_SUCCESS )
638 *output_length = mbedtls_rsa_get_len( rsa );
639
640 mbedtls_rsa_free( rsa );
641 mbedtls_free( rsa );
642#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
643 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
644 }
645 else
646 {
647 status = PSA_ERROR_NOT_SUPPORTED;
648 }
649
650 return status;
651}
652
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100653#endif /* MBEDTLS_PSA_CRYPTO_C */