blob: 19011a4e6adaea0b90020da923384be0bb1ef096 [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"
Manuel Pégourié-Gonnard47728842022-07-18 13:00:40 +020044#include "hash_info.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010045
46#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Ronald Cron00b7bfc2020-11-25 15:25:26 +010047 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
Ronald Cron0266cfe2021-03-13 18:50:11 +010048 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
49 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
50 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
51 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Cron00b7bfc2020-11-25 15:25:26 +010052
53/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
54 * that are not a multiple of 8) well. For example, there is only
55 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
56 * way to return the exact bit size of a key.
57 * To keep things simple, reject non-byte-aligned key sizes. */
58static psa_status_t psa_check_rsa_key_byte_aligned(
59 const mbedtls_rsa_context *rsa )
60{
61 mbedtls_mpi n;
62 psa_status_t status;
63 mbedtls_mpi_init( &n );
64 status = mbedtls_to_psa_error(
65 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
66 if( status == PSA_SUCCESS )
67 {
68 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
69 status = PSA_ERROR_NOT_SUPPORTED;
70 }
71 mbedtls_mpi_free( &n );
72 return( status );
73}
74
75psa_status_t mbedtls_psa_rsa_load_representation(
76 psa_key_type_t type, const uint8_t *data, size_t data_length,
77 mbedtls_rsa_context **p_rsa )
78{
79 psa_status_t status;
80 mbedtls_pk_context ctx;
81 size_t bits;
82 mbedtls_pk_init( &ctx );
83
84 /* Parse the data. */
85 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
86 status = mbedtls_to_psa_error(
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +020087 mbedtls_pk_parse_key( &ctx, data, data_length, NULL, 0,
88 mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) );
Ronald Cron00b7bfc2020-11-25 15:25:26 +010089 else
90 status = mbedtls_to_psa_error(
91 mbedtls_pk_parse_public_key( &ctx, data, data_length ) );
92 if( status != PSA_SUCCESS )
93 goto exit;
94
95 /* We have something that the pkparse module recognizes. If it is a
96 * valid RSA key, store it. */
97 if( mbedtls_pk_get_type( &ctx ) != MBEDTLS_PK_RSA )
98 {
99 status = PSA_ERROR_INVALID_ARGUMENT;
100 goto exit;
101 }
102
103 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
104 * supports non-byte-aligned key sizes, but not well. For example,
105 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
106 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( mbedtls_pk_rsa( ctx ) ) );
107 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
108 {
109 status = PSA_ERROR_NOT_SUPPORTED;
110 goto exit;
111 }
112 status = psa_check_rsa_key_byte_aligned( mbedtls_pk_rsa( ctx ) );
113 if( status != PSA_SUCCESS )
114 goto exit;
115
116 /* Copy out the pointer to the RSA context, and reset the PK context
117 * such that pk_free doesn't free the RSA context we just grabbed. */
118 *p_rsa = mbedtls_pk_rsa( ctx );
119 ctx.pk_info = NULL;
120
121exit:
122 mbedtls_pk_free( &ctx );
123 return( status );
124}
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100125#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100126 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
Ronald Cron0266cfe2021-03-13 18:50:11 +0100127 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
128 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
129 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
130 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100131
Ronald Cron0266cfe2021-03-13 18:50:11 +0100132#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
133 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Cronabf2aef2020-11-27 18:13:44 +0100134
Ronald Cron0266cfe2021-03-13 18:50:11 +0100135psa_status_t mbedtls_psa_rsa_import_key(
Ronald Cronabf2aef2020-11-27 18:13:44 +0100136 const psa_key_attributes_t *attributes,
137 const uint8_t *data, size_t data_length,
138 uint8_t *key_buffer, size_t key_buffer_size,
139 size_t *key_buffer_length, size_t *bits )
140{
141 psa_status_t status;
142 mbedtls_rsa_context *rsa = NULL;
143
144 /* Parse input */
145 status = mbedtls_psa_rsa_load_representation( attributes->core.type,
146 data,
147 data_length,
148 &rsa );
149 if( status != PSA_SUCCESS )
150 goto exit;
151
152 *bits = (psa_key_bits_t) PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
153
154 /* Re-export the data to PSA export format, such that we can store export
155 * representation in the key slot. Export representation in case of RSA is
156 * the smallest representation that's allowed as input, so a straight-up
157 * allocation of the same size as the input buffer will be large enough. */
158 status = mbedtls_psa_rsa_export_key( attributes->core.type,
159 rsa,
160 key_buffer,
161 key_buffer_size,
162 key_buffer_length );
163exit:
164 /* Always free the RSA object */
165 mbedtls_rsa_free( rsa );
166 mbedtls_free( rsa );
167
168 return( status );
169}
170
Ronald Crone5ca3d82020-11-26 16:36:16 +0100171psa_status_t mbedtls_psa_rsa_export_key( psa_key_type_t type,
172 mbedtls_rsa_context *rsa,
173 uint8_t *data,
174 size_t data_size,
175 size_t *data_length )
176{
177#if defined(MBEDTLS_PK_WRITE_C)
178 int ret;
179 mbedtls_pk_context pk;
180 uint8_t *pos = data + data_size;
181
182 mbedtls_pk_init( &pk );
183 pk.pk_info = &mbedtls_rsa_info;
184 pk.pk_ctx = rsa;
185
186 /* PSA Crypto API defines the format of an RSA key as a DER-encoded
187 * representation of the non-encrypted PKCS#1 RSAPrivateKey for a
188 * private key and of the RFC3279 RSAPublicKey for a public key. */
189 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
190 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
191 else
192 ret = mbedtls_pk_write_pubkey( &pos, data, &pk );
193
194 if( ret < 0 )
195 {
196 /* Clean up in case pk_write failed halfway through. */
197 memset( data, 0, data_size );
198 return( mbedtls_to_psa_error( ret ) );
199 }
200
201 /* The mbedtls_pk_xxx functions write to the end of the buffer.
202 * Move the data to the beginning and erase remaining data
203 * at the original location. */
204 if( 2 * (size_t) ret <= data_size )
205 {
206 memcpy( data, data + data_size - ret, ret );
207 memset( data + data_size - ret, 0, ret );
208 }
209 else if( (size_t) ret < data_size )
210 {
211 memmove( data, data + data_size - ret, ret );
212 memset( data + ret, 0, data_size - ret );
213 }
214
215 *data_length = ret;
216 return( PSA_SUCCESS );
217#else
218 (void) type;
219 (void) rsa;
220 (void) data;
221 (void) data_size;
222 (void) data_length;
223 return( PSA_ERROR_NOT_SUPPORTED );
224#endif /* MBEDTLS_PK_WRITE_C */
225}
226
Ronald Cron0266cfe2021-03-13 18:50:11 +0100227psa_status_t mbedtls_psa_rsa_export_public_key(
Ronald Crone5ca3d82020-11-26 16:36:16 +0100228 const psa_key_attributes_t *attributes,
229 const uint8_t *key_buffer, size_t key_buffer_size,
230 uint8_t *data, size_t data_size, size_t *data_length )
231{
232 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
233 mbedtls_rsa_context *rsa = NULL;
234
235 status = mbedtls_psa_rsa_load_representation(
236 attributes->core.type, key_buffer, key_buffer_size, &rsa );
237 if( status != PSA_SUCCESS )
238 return( status );
239
240 status = mbedtls_psa_rsa_export_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY,
241 rsa,
242 data,
243 data_size,
244 data_length );
245
246 mbedtls_rsa_free( rsa );
247 mbedtls_free( rsa );
248
249 return( status );
250}
Ronald Cron0266cfe2021-03-13 18:50:11 +0100251#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
252 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cronf1057d32020-11-26 19:19:10 +0100253
Ronald Cron0266cfe2021-03-13 18:50:11 +0100254#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
Jaeden Amero424fa932021-05-14 08:34:32 +0100255 defined(MBEDTLS_GENPRIME)
Ronald Cron2365fde2021-02-08 09:52:24 +0100256static psa_status_t psa_rsa_read_exponent( const uint8_t *domain_parameters,
Ronald Cron9e18fc12020-11-05 17:36:40 +0100257 size_t domain_parameters_size,
258 int *exponent )
259{
260 size_t i;
261 uint32_t acc = 0;
262
263 if( domain_parameters_size == 0 )
264 {
265 *exponent = 65537;
266 return( PSA_SUCCESS );
267 }
268
269 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
270 * support values that fit in a 32-bit integer, which is larger than
271 * int on just about every platform anyway. */
272 if( domain_parameters_size > sizeof( acc ) )
273 return( PSA_ERROR_NOT_SUPPORTED );
274 for( i = 0; i < domain_parameters_size; i++ )
275 acc = ( acc << 8 ) | domain_parameters[i];
276 if( acc > INT_MAX )
277 return( PSA_ERROR_NOT_SUPPORTED );
278 *exponent = acc;
279 return( PSA_SUCCESS );
280}
281
Ronald Cron0266cfe2021-03-13 18:50:11 +0100282psa_status_t mbedtls_psa_rsa_generate_key(
Ronald Cron9e18fc12020-11-05 17:36:40 +0100283 const psa_key_attributes_t *attributes,
284 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
285{
286 psa_status_t status;
287 mbedtls_rsa_context rsa;
288 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
289 int exponent;
290
Ronald Cron2365fde2021-02-08 09:52:24 +0100291 status = psa_rsa_read_exponent( attributes->domain_parameters,
Ronald Cron9e18fc12020-11-05 17:36:40 +0100292 attributes->domain_parameters_size,
293 &exponent );
294 if( status != PSA_SUCCESS )
295 return( status );
296
Ronald Cronc1905a12021-06-05 11:11:14 +0200297 mbedtls_rsa_init( &rsa );
Ronald Cron9e18fc12020-11-05 17:36:40 +0100298 ret = mbedtls_rsa_gen_key( &rsa,
299 mbedtls_psa_get_random,
300 MBEDTLS_PSA_RANDOM_STATE,
301 (unsigned int)attributes->core.bits,
302 exponent );
303 if( ret != 0 )
304 return( mbedtls_to_psa_error( ret ) );
305
306 status = mbedtls_psa_rsa_export_key( attributes->core.type,
307 &rsa, key_buffer, key_buffer_size,
308 key_buffer_length );
309 mbedtls_rsa_free( &rsa );
310
311 return( status );
312}
Ronald Cron0266cfe2021-03-13 18:50:11 +0100313#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Jaeden Amero424fa932021-05-14 08:34:32 +0100314 * defined(MBEDTLS_GENPRIME) */
Ronald Cron9e18fc12020-11-05 17:36:40 +0100315
Ronald Cron7bdbca32020-12-09 13:34:54 +0100316/****************************************************************/
317/* Sign/verify hashes */
318/****************************************************************/
319
Ronald Cron0266cfe2021-03-13 18:50:11 +0100320#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
321 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100322
323/* Decode the hash algorithm from alg and store the mbedtls encoding in
324 * md_alg. Verify that the hash length is acceptable. */
325static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
326 size_t hash_length,
327 mbedtls_md_type_t *md_alg )
328{
329 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Manuel Pégourié-Gonnard130fa4d2022-07-18 15:12:48 +0200330 *md_alg = mbedtls_hash_info_md_from_psa( hash_alg );
Ronald Cron7bdbca32020-12-09 13:34:54 +0100331
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 {
Manuel Pégourié-Gonnard3f477892022-07-05 11:30:31 +0200343 if( *md_alg == MBEDTLS_MD_NONE )
Ronald Cron7bdbca32020-12-09 13:34:54 +0100344 return( PSA_ERROR_NOT_SUPPORTED );
Manuel Pégourié-Gonnard47728842022-07-18 13:00:40 +0200345 if( mbedtls_hash_info_get_size( *md_alg ) != hash_length )
Ronald Cron7bdbca32020-12-09 13:34:54 +0100346 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
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100653psa_status_t mbedtls_psa_asymmetric_decrypt( const psa_key_attributes_t *attributes,
654 const uint8_t *key_buffer,
655 size_t key_buffer_size,
656 psa_algorithm_t alg,
657 const uint8_t *input,
658 size_t input_length,
659 const uint8_t *salt,
660 size_t salt_length,
661 uint8_t *output,
662 size_t output_size,
663 size_t *output_length )
664{
665 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
666 (void) key_buffer;
667 (void) key_buffer_size;
668 (void) input;
669 (void) input_length;
670 (void) salt;
671 (void) salt_length;
672 (void) output;
673 (void) output_size;
674 (void) output_length;
675
676 *output_length = 0;
677
678 if( attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
679 {
680#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
681 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
682 mbedtls_rsa_context *rsa = NULL;
683 status = mbedtls_psa_rsa_load_representation( attributes->core.type,
684 key_buffer,
685 key_buffer_size,
686 &rsa );
687 if( status != PSA_SUCCESS )
688 goto rsa_exit;
689
690 if( input_length != mbedtls_rsa_get_len( rsa ) )
691 {
692 status = PSA_ERROR_INVALID_ARGUMENT;
693 goto rsa_exit;
694 }
695#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
696 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
697
698 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
699 {
700#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
701 status = mbedtls_to_psa_error(
702 mbedtls_rsa_pkcs1_decrypt( rsa,
703 mbedtls_psa_get_random,
704 MBEDTLS_PSA_RANDOM_STATE,
705 output_length,
706 input,
707 output,
708 output_size ) );
709#else
710 status = PSA_ERROR_NOT_SUPPORTED;
711#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
712 }
713 else
714 if( PSA_ALG_IS_RSA_OAEP( alg ) )
715 {
716#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
717 status = mbedtls_to_psa_error(
718 psa_rsa_oaep_set_padding_mode( alg, rsa ) );
719 if( status != PSA_SUCCESS )
720 goto rsa_exit;
721
722 status = mbedtls_to_psa_error(
723 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
724 mbedtls_psa_get_random,
725 MBEDTLS_PSA_RANDOM_STATE,
726 salt, salt_length,
727 output_length,
728 input,
729 output,
730 output_size ) );
731#else
732 status = PSA_ERROR_NOT_SUPPORTED;
733#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
734 }
735 else
736 {
737 status = PSA_ERROR_INVALID_ARGUMENT;
738 }
739
740#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
741 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
742rsa_exit:
743 mbedtls_rsa_free( rsa );
744 mbedtls_free( rsa );
745#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
746 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
747 }
748 else
749 {
750 status = PSA_ERROR_NOT_SUPPORTED;
751 }
752
753 return status;
754}
755
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100756#endif /* MBEDTLS_PSA_CRYPTO_C */