blob: 4a3944e1830d5f340cff668db72fa48ff91816a3 [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
Manuel Pégourié-Gonnard3f477892022-07-05 11:30:31 +0200323/* Convert a hash algorithm from PSA to MD identifier */
324static inline mbedtls_md_type_t get_md_alg_from_psa( psa_algorithm_t psa_alg )
325{
326 switch( psa_alg )
327 {
328 case PSA_ALG_MD5:
329 return( MBEDTLS_MD_MD5 );
330 case PSA_ALG_RIPEMD160:
331 return( MBEDTLS_MD_RIPEMD160 );
332 case PSA_ALG_SHA_1:
333 return( MBEDTLS_MD_SHA1 );
334 case PSA_ALG_SHA_224:
335 return( MBEDTLS_MD_SHA224 );
336 case PSA_ALG_SHA_256:
337 return( MBEDTLS_MD_SHA256 );
338 case PSA_ALG_SHA_384:
339 return( MBEDTLS_MD_SHA384 );
340 case PSA_ALG_SHA_512:
341 return( MBEDTLS_MD_SHA512 );
342 default:
343 return( MBEDTLS_MD_NONE );
344 }
345}
346
Ronald Cron7bdbca32020-12-09 13:34:54 +0100347/* Decode the hash algorithm from alg and store the mbedtls encoding in
348 * md_alg. Verify that the hash length is acceptable. */
349static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
350 size_t hash_length,
351 mbedtls_md_type_t *md_alg )
352{
353 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Manuel Pégourié-Gonnard3f477892022-07-05 11:30:31 +0200354 *md_alg = get_md_alg_from_psa( hash_alg );
Ronald Cron7bdbca32020-12-09 13:34:54 +0100355
356 /* The Mbed TLS RSA module uses an unsigned int for hash length
357 * parameters. Validate that it fits so that we don't risk an
358 * overflow later. */
359#if SIZE_MAX > UINT_MAX
360 if( hash_length > UINT_MAX )
361 return( PSA_ERROR_INVALID_ARGUMENT );
362#endif
363
Janos Follath0af093b2021-06-07 14:34:10 +0100364 /* For signatures using a hash, the hash length must be correct. */
365 if( alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Ronald Cron7bdbca32020-12-09 13:34:54 +0100366 {
Manuel Pégourié-Gonnard3f477892022-07-05 11:30:31 +0200367 if( *md_alg == MBEDTLS_MD_NONE )
Ronald Cron7bdbca32020-12-09 13:34:54 +0100368 return( PSA_ERROR_NOT_SUPPORTED );
Manuel Pégourié-Gonnard47728842022-07-18 13:00:40 +0200369 if( mbedtls_hash_info_get_size( *md_alg ) != hash_length )
Ronald Cron7bdbca32020-12-09 13:34:54 +0100370 return( PSA_ERROR_INVALID_ARGUMENT );
371 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100372
373 return( PSA_SUCCESS );
374}
375
Ronald Cron0266cfe2021-03-13 18:50:11 +0100376psa_status_t mbedtls_psa_rsa_sign_hash(
Ronald Cron7bdbca32020-12-09 13:34:54 +0100377 const psa_key_attributes_t *attributes,
378 const uint8_t *key_buffer, size_t key_buffer_size,
379 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
380 uint8_t *signature, size_t signature_size, size_t *signature_length )
381{
382 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
383 mbedtls_rsa_context *rsa = NULL;
384 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
385 mbedtls_md_type_t md_alg;
386
387 status = mbedtls_psa_rsa_load_representation( attributes->core.type,
388 key_buffer,
389 key_buffer_size,
390 &rsa );
391 if( status != PSA_SUCCESS )
392 return( status );
393
394 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
395 if( status != PSA_SUCCESS )
396 goto exit;
397
398 if( signature_size < mbedtls_rsa_get_len( rsa ) )
399 {
400 status = PSA_ERROR_BUFFER_TOO_SMALL;
401 goto exit;
402 }
403
Ronald Cron0266cfe2021-03-13 18:50:11 +0100404#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100405 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
406 {
Ronald Cronea7631b2021-06-03 18:51:59 +0200407 ret = mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
408 MBEDTLS_MD_NONE );
409 if( ret == 0 )
410 {
411 ret = mbedtls_rsa_pkcs1_sign( rsa,
412 mbedtls_psa_get_random,
413 MBEDTLS_PSA_RANDOM_STATE,
414 md_alg,
415 (unsigned int) hash_length,
416 hash,
417 signature );
418 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100419 }
420 else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100421#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
422#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100423 if( PSA_ALG_IS_RSA_PSS( alg ) )
424 {
Ronald Cronea7631b2021-06-03 18:51:59 +0200425 ret = mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
426
427 if( ret == 0 )
428 {
429 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
430 mbedtls_psa_get_random,
431 MBEDTLS_PSA_RANDOM_STATE,
432 MBEDTLS_MD_NONE,
433 (unsigned int) hash_length,
434 hash,
435 signature );
436 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100437 }
438 else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100439#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Ronald Cron7bdbca32020-12-09 13:34:54 +0100440 {
441 status = PSA_ERROR_INVALID_ARGUMENT;
442 goto exit;
443 }
444
445 if( ret == 0 )
446 *signature_length = mbedtls_rsa_get_len( rsa );
447 status = mbedtls_to_psa_error( ret );
448
449exit:
450 mbedtls_rsa_free( rsa );
451 mbedtls_free( rsa );
452
453 return( status );
454}
455
Ronald Cron0266cfe2021-03-13 18:50:11 +0100456#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200457static int rsa_pss_expected_salt_len( psa_algorithm_t alg,
458 const mbedtls_rsa_context *rsa,
459 size_t hash_length )
460{
461 if( PSA_ALG_IS_RSA_PSS_ANY_SALT( alg ) )
462 return( MBEDTLS_RSA_SALT_LEN_ANY );
463 /* Otherwise: standard salt length, i.e. largest possible salt length
464 * up to the hash length. */
Gilles Peskinef6892de2021-10-08 16:28:32 +0200465 int klen = (int) mbedtls_rsa_get_len( rsa ); // known to fit
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200466 int hlen = (int) hash_length; // known to fit
467 int room = klen - 2 - hlen;
468 if( room < 0 )
469 return( 0 ); // there is no valid signature in this case anyway
470 else if( room > hlen )
471 return( hlen );
472 else
473 return( room );
474}
Ronald Cron0266cfe2021-03-13 18:50:11 +0100475#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200476
Ronald Cron0266cfe2021-03-13 18:50:11 +0100477psa_status_t mbedtls_psa_rsa_verify_hash(
Ronald Cron7bdbca32020-12-09 13:34:54 +0100478 const psa_key_attributes_t *attributes,
479 const uint8_t *key_buffer, size_t key_buffer_size,
480 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
481 const uint8_t *signature, size_t signature_length )
482{
483 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
484 mbedtls_rsa_context *rsa = NULL;
485 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
486 mbedtls_md_type_t md_alg;
487
488 status = mbedtls_psa_rsa_load_representation( attributes->core.type,
489 key_buffer,
490 key_buffer_size,
491 &rsa );
492 if( status != PSA_SUCCESS )
493 goto exit;
494
495 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
496 if( status != PSA_SUCCESS )
497 goto exit;
498
499 if( signature_length != mbedtls_rsa_get_len( rsa ) )
500 {
501 status = PSA_ERROR_INVALID_SIGNATURE;
502 goto exit;
503 }
504
Ronald Cron0266cfe2021-03-13 18:50:11 +0100505#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100506 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
507 {
Ronald Cronea7631b2021-06-03 18:51:59 +0200508 ret = mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
509 MBEDTLS_MD_NONE );
510 if( ret == 0 )
511 {
512 ret = mbedtls_rsa_pkcs1_verify( rsa,
513 md_alg,
514 (unsigned int) hash_length,
515 hash,
516 signature );
517 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100518 }
519 else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100520#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
521#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100522 if( PSA_ALG_IS_RSA_PSS( alg ) )
523 {
Ronald Cronea7631b2021-06-03 18:51:59 +0200524 ret = mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
525 if( ret == 0 )
526 {
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200527 int slen = rsa_pss_expected_salt_len( alg, rsa, hash_length );
528 ret = mbedtls_rsa_rsassa_pss_verify_ext( rsa,
529 md_alg,
530 (unsigned) hash_length,
531 hash,
532 md_alg,
533 slen,
534 signature );
Ronald Cronea7631b2021-06-03 18:51:59 +0200535 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100536 }
537 else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100538#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Ronald Cron7bdbca32020-12-09 13:34:54 +0100539 {
540 status = PSA_ERROR_INVALID_ARGUMENT;
541 goto exit;
542 }
543
544 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
545 * the rest of the signature is invalid". This has little use in
546 * practice and PSA doesn't report this distinction. */
547 status = ( ret == MBEDTLS_ERR_RSA_INVALID_PADDING ) ?
548 PSA_ERROR_INVALID_SIGNATURE :
549 mbedtls_to_psa_error( ret );
550
551exit:
552 mbedtls_rsa_free( rsa );
553 mbedtls_free( rsa );
554
555 return( status );
556}
557
Ronald Crond2fb8542020-12-09 15:18:01 +0100558#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
559 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
560
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100561/****************************************************************/
562/* Asymmetric cryptography */
563/****************************************************************/
564
565#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
566static int psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
567 mbedtls_rsa_context *rsa )
568{
569 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
570 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
571 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
572
573 return( mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ) );
574}
575#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
576
577psa_status_t mbedtls_psa_asymmetric_encrypt( const psa_key_attributes_t *attributes,
578 const uint8_t *key_buffer,
579 size_t key_buffer_size,
580 psa_algorithm_t alg,
581 const uint8_t *input,
582 size_t input_length,
583 const uint8_t *salt,
584 size_t salt_length,
585 uint8_t *output,
586 size_t output_size,
587 size_t *output_length )
588{
589 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
590 (void) key_buffer;
591 (void) key_buffer_size;
592 (void) input;
593 (void) input_length;
594 (void) salt;
595 (void) salt_length;
596 (void) output;
597 (void) output_size;
598 (void) output_length;
599
600 if( PSA_KEY_TYPE_IS_RSA( attributes->core.type ) )
601 {
602#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
603 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
604 mbedtls_rsa_context *rsa = NULL;
605 status = mbedtls_psa_rsa_load_representation( attributes->core.type,
606 key_buffer,
607 key_buffer_size,
608 &rsa );
609 if( status != PSA_SUCCESS )
610 goto rsa_exit;
611
612 if( output_size < mbedtls_rsa_get_len( rsa ) )
613 {
614 status = PSA_ERROR_BUFFER_TOO_SMALL;
615 goto rsa_exit;
616 }
617#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
618 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
619 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
620 {
621#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
622 status = mbedtls_to_psa_error(
623 mbedtls_rsa_pkcs1_encrypt( rsa,
624 mbedtls_psa_get_random,
625 MBEDTLS_PSA_RANDOM_STATE,
626 input_length,
627 input,
628 output ) );
629#else
630 status = PSA_ERROR_NOT_SUPPORTED;
631#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
632 }
633 else
634 if( PSA_ALG_IS_RSA_OAEP( alg ) )
635 {
636#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
637 status = mbedtls_to_psa_error(
638 psa_rsa_oaep_set_padding_mode( alg, rsa ) );
639 if( status != PSA_SUCCESS )
640 goto rsa_exit;
641
642 status = mbedtls_to_psa_error(
643 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
644 mbedtls_psa_get_random,
645 MBEDTLS_PSA_RANDOM_STATE,
646 salt, salt_length,
647 input_length,
648 input,
649 output ) );
650#else
651 status = PSA_ERROR_NOT_SUPPORTED;
652#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
653 }
654 else
655 {
656 status = PSA_ERROR_INVALID_ARGUMENT;
657 }
658#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
659 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
660rsa_exit:
661 if( status == PSA_SUCCESS )
662 *output_length = mbedtls_rsa_get_len( rsa );
663
664 mbedtls_rsa_free( rsa );
665 mbedtls_free( rsa );
666#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
667 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
668 }
669 else
670 {
671 status = PSA_ERROR_NOT_SUPPORTED;
672 }
673
674 return status;
675}
676
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100677psa_status_t mbedtls_psa_asymmetric_decrypt( const psa_key_attributes_t *attributes,
678 const uint8_t *key_buffer,
679 size_t key_buffer_size,
680 psa_algorithm_t alg,
681 const uint8_t *input,
682 size_t input_length,
683 const uint8_t *salt,
684 size_t salt_length,
685 uint8_t *output,
686 size_t output_size,
687 size_t *output_length )
688{
689 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
690 (void) key_buffer;
691 (void) key_buffer_size;
692 (void) input;
693 (void) input_length;
694 (void) salt;
695 (void) salt_length;
696 (void) output;
697 (void) output_size;
698 (void) output_length;
699
700 *output_length = 0;
701
702 if( attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
703 {
704#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
705 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
706 mbedtls_rsa_context *rsa = NULL;
707 status = mbedtls_psa_rsa_load_representation( attributes->core.type,
708 key_buffer,
709 key_buffer_size,
710 &rsa );
711 if( status != PSA_SUCCESS )
712 goto rsa_exit;
713
714 if( input_length != mbedtls_rsa_get_len( rsa ) )
715 {
716 status = PSA_ERROR_INVALID_ARGUMENT;
717 goto rsa_exit;
718 }
719#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
720 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
721
722 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
723 {
724#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
725 status = mbedtls_to_psa_error(
726 mbedtls_rsa_pkcs1_decrypt( rsa,
727 mbedtls_psa_get_random,
728 MBEDTLS_PSA_RANDOM_STATE,
729 output_length,
730 input,
731 output,
732 output_size ) );
733#else
734 status = PSA_ERROR_NOT_SUPPORTED;
735#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
736 }
737 else
738 if( PSA_ALG_IS_RSA_OAEP( alg ) )
739 {
740#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
741 status = mbedtls_to_psa_error(
742 psa_rsa_oaep_set_padding_mode( alg, rsa ) );
743 if( status != PSA_SUCCESS )
744 goto rsa_exit;
745
746 status = mbedtls_to_psa_error(
747 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
748 mbedtls_psa_get_random,
749 MBEDTLS_PSA_RANDOM_STATE,
750 salt, salt_length,
751 output_length,
752 input,
753 output,
754 output_size ) );
755#else
756 status = PSA_ERROR_NOT_SUPPORTED;
757#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
758 }
759 else
760 {
761 status = PSA_ERROR_INVALID_ARGUMENT;
762 }
763
764#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
765 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
766rsa_exit:
767 mbedtls_rsa_free( rsa );
768 mbedtls_free( rsa );
769#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
770 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
771 }
772 else
773 {
774 status = PSA_ERROR_NOT_SUPPORTED;
775 }
776
777 return status;
778}
779
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100780#endif /* MBEDTLS_PSA_CRYPTO_C */