| Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 1 | /* | 
|  | 2 | *  PSA RSA layer on top of Mbed TLS crypto | 
|  | 3 | */ | 
|  | 4 | /* | 
|  | 5 | *  Copyright The Mbed TLS Contributors | 
|  | 6 | *  SPDX-License-Identifier: Apache-2.0 | 
|  | 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 Stekiel | 234f318 | 2021-12-09 11:13:58 +0100 | [diff] [blame] | 26 | #include "psa/crypto_values.h" | 
| Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 27 | #include "psa_crypto_core.h" | 
| Ronald Cron | 9e18fc1 | 2020-11-05 17:36:40 +0100 | [diff] [blame] | 28 | #include "psa_crypto_random_impl.h" | 
| Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 29 | #include "psa_crypto_rsa.h" | 
| Steven Cooreman | 5f88e77 | 2021-03-15 11:07:12 +0100 | [diff] [blame] | 30 | #include "psa_crypto_hash.h" | 
| Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 31 |  | 
|  | 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 Rodgman | 3b5e6f0 | 2021-04-06 17:58:16 +0100 | [diff] [blame] | 43 | #include "pk_wrap.h" | 
| Manuel Pégourié-Gonnard | 4772884 | 2022-07-18 13:00:40 +0200 | [diff] [blame] | 44 | #include "hash_info.h" | 
| Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 45 |  | 
|  | 46 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ | 
| Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 47 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \ | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 48 | 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 Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 52 |  | 
|  | 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. */ | 
|  | 58 | static 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 |  | 
|  | 75 | psa_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é-Gonnard | 84dea01 | 2021-06-15 11:29:26 +0200 | [diff] [blame] | 87 | mbedtls_pk_parse_key( &ctx, data, data_length, NULL, 0, | 
|  | 88 | mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) ); | 
| Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 89 | 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 |  | 
|  | 121 | exit: | 
|  | 122 | mbedtls_pk_free( &ctx ); | 
|  | 123 | return( status ); | 
|  | 124 | } | 
| Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 125 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || | 
| Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 126 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 127 | * 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 Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 131 |  | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 132 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ | 
|  | 133 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) | 
| Ronald Cron | abf2aef | 2020-11-27 18:13:44 +0100 | [diff] [blame] | 134 |  | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 135 | psa_status_t mbedtls_psa_rsa_import_key( | 
| Ronald Cron | abf2aef | 2020-11-27 18:13:44 +0100 | [diff] [blame] | 136 | 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 ); | 
|  | 163 | exit: | 
|  | 164 | /* Always free the RSA object */ | 
|  | 165 | mbedtls_rsa_free( rsa ); | 
|  | 166 | mbedtls_free( rsa ); | 
|  | 167 |  | 
|  | 168 | return( status ); | 
|  | 169 | } | 
|  | 170 |  | 
| Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 171 | psa_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 Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 227 | psa_status_t mbedtls_psa_rsa_export_public_key( | 
| Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 228 | 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 Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 251 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || | 
|  | 252 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ | 
| Ronald Cron | f1057d3 | 2020-11-26 19:19:10 +0100 | [diff] [blame] | 253 |  | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 254 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \ | 
| Jaeden Amero | 424fa93 | 2021-05-14 08:34:32 +0100 | [diff] [blame] | 255 | defined(MBEDTLS_GENPRIME) | 
| Ronald Cron | 2365fde | 2021-02-08 09:52:24 +0100 | [diff] [blame] | 256 | static psa_status_t psa_rsa_read_exponent( const uint8_t *domain_parameters, | 
| Ronald Cron | 9e18fc1 | 2020-11-05 17:36:40 +0100 | [diff] [blame] | 257 | 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 Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 282 | psa_status_t mbedtls_psa_rsa_generate_key( | 
| Ronald Cron | 9e18fc1 | 2020-11-05 17:36:40 +0100 | [diff] [blame] | 283 | 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 Cron | 2365fde | 2021-02-08 09:52:24 +0100 | [diff] [blame] | 291 | status = psa_rsa_read_exponent( attributes->domain_parameters, | 
| Ronald Cron | 9e18fc1 | 2020-11-05 17:36:40 +0100 | [diff] [blame] | 292 | attributes->domain_parameters_size, | 
|  | 293 | &exponent ); | 
|  | 294 | if( status != PSA_SUCCESS ) | 
|  | 295 | return( status ); | 
|  | 296 |  | 
| Ronald Cron | c1905a1 | 2021-06-05 11:11:14 +0200 | [diff] [blame] | 297 | mbedtls_rsa_init( &rsa ); | 
| Ronald Cron | 9e18fc1 | 2020-11-05 17:36:40 +0100 | [diff] [blame] | 298 | 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 Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 313 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) | 
| Jaeden Amero | 424fa93 | 2021-05-14 08:34:32 +0100 | [diff] [blame] | 314 | * defined(MBEDTLS_GENPRIME) */ | 
| Ronald Cron | 9e18fc1 | 2020-11-05 17:36:40 +0100 | [diff] [blame] | 315 |  | 
| Ronald Cron | 7bdbca3 | 2020-12-09 13:34:54 +0100 | [diff] [blame] | 316 | /****************************************************************/ | 
|  | 317 | /* Sign/verify hashes */ | 
|  | 318 | /****************************************************************/ | 
|  | 319 |  | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 320 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ | 
|  | 321 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) | 
| Ronald Cron | 7bdbca3 | 2020-12-09 13:34:54 +0100 | [diff] [blame] | 322 |  | 
|  | 323 | /* Decode the hash algorithm from alg and store the mbedtls encoding in | 
|  | 324 | * md_alg. Verify that the hash length is acceptable. */ | 
|  | 325 | static 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é-Gonnard | 130fa4d | 2022-07-18 15:12:48 +0200 | [diff] [blame] | 330 | *md_alg = mbedtls_hash_info_md_from_psa( hash_alg ); | 
| Ronald Cron | 7bdbca3 | 2020-12-09 13:34:54 +0100 | [diff] [blame] | 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 Follath | 0af093b | 2021-06-07 14:34:10 +0100 | [diff] [blame] | 340 | /* For signatures using a hash, the hash length must be correct. */ | 
|  | 341 | if( alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW ) | 
| Ronald Cron | 7bdbca3 | 2020-12-09 13:34:54 +0100 | [diff] [blame] | 342 | { | 
| Manuel Pégourié-Gonnard | 3f47789 | 2022-07-05 11:30:31 +0200 | [diff] [blame] | 343 | if( *md_alg == MBEDTLS_MD_NONE ) | 
| Ronald Cron | 7bdbca3 | 2020-12-09 13:34:54 +0100 | [diff] [blame] | 344 | return( PSA_ERROR_NOT_SUPPORTED ); | 
| Manuel Pégourié-Gonnard | 4772884 | 2022-07-18 13:00:40 +0200 | [diff] [blame] | 345 | if( mbedtls_hash_info_get_size( *md_alg ) != hash_length ) | 
| Ronald Cron | 7bdbca3 | 2020-12-09 13:34:54 +0100 | [diff] [blame] | 346 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 347 | } | 
| Ronald Cron | 7bdbca3 | 2020-12-09 13:34:54 +0100 | [diff] [blame] | 348 |  | 
|  | 349 | return( PSA_SUCCESS ); | 
|  | 350 | } | 
|  | 351 |  | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 352 | psa_status_t mbedtls_psa_rsa_sign_hash( | 
| Ronald Cron | 7bdbca3 | 2020-12-09 13:34:54 +0100 | [diff] [blame] | 353 | 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 Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 380 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) | 
| Ronald Cron | 7bdbca3 | 2020-12-09 13:34:54 +0100 | [diff] [blame] | 381 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) | 
|  | 382 | { | 
| Ronald Cron | ea7631b | 2021-06-03 18:51:59 +0200 | [diff] [blame] | 383 | 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 Cron | 7bdbca3 | 2020-12-09 13:34:54 +0100 | [diff] [blame] | 395 | } | 
|  | 396 | else | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 397 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */ | 
|  | 398 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) | 
| Ronald Cron | 7bdbca3 | 2020-12-09 13:34:54 +0100 | [diff] [blame] | 399 | if( PSA_ALG_IS_RSA_PSS( alg ) ) | 
|  | 400 | { | 
| Ronald Cron | ea7631b | 2021-06-03 18:51:59 +0200 | [diff] [blame] | 401 | 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 Cron | 7bdbca3 | 2020-12-09 13:34:54 +0100 | [diff] [blame] | 413 | } | 
|  | 414 | else | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 415 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */ | 
| Ronald Cron | 7bdbca3 | 2020-12-09 13:34:54 +0100 | [diff] [blame] | 416 | { | 
|  | 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 |  | 
|  | 425 | exit: | 
|  | 426 | mbedtls_rsa_free( rsa ); | 
|  | 427 | mbedtls_free( rsa ); | 
|  | 428 |  | 
|  | 429 | return( status ); | 
|  | 430 | } | 
|  | 431 |  | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 432 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) | 
| Gilles Peskine | b9b817e | 2021-10-04 22:15:05 +0200 | [diff] [blame] | 433 | static 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 Peskine | f6892de | 2021-10-08 16:28:32 +0200 | [diff] [blame] | 441 | int klen = (int) mbedtls_rsa_get_len( rsa ); // known to fit | 
| Gilles Peskine | b9b817e | 2021-10-04 22:15:05 +0200 | [diff] [blame] | 442 | 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 Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 451 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */ | 
| Gilles Peskine | b9b817e | 2021-10-04 22:15:05 +0200 | [diff] [blame] | 452 |  | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 453 | psa_status_t mbedtls_psa_rsa_verify_hash( | 
| Ronald Cron | 7bdbca3 | 2020-12-09 13:34:54 +0100 | [diff] [blame] | 454 | 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 Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 481 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) | 
| Ronald Cron | 7bdbca3 | 2020-12-09 13:34:54 +0100 | [diff] [blame] | 482 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) | 
|  | 483 | { | 
| Ronald Cron | ea7631b | 2021-06-03 18:51:59 +0200 | [diff] [blame] | 484 | 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 Cron | 7bdbca3 | 2020-12-09 13:34:54 +0100 | [diff] [blame] | 494 | } | 
|  | 495 | else | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 496 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */ | 
|  | 497 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) | 
| Ronald Cron | 7bdbca3 | 2020-12-09 13:34:54 +0100 | [diff] [blame] | 498 | if( PSA_ALG_IS_RSA_PSS( alg ) ) | 
|  | 499 | { | 
| Ronald Cron | ea7631b | 2021-06-03 18:51:59 +0200 | [diff] [blame] | 500 | ret = mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); | 
|  | 501 | if( ret == 0 ) | 
|  | 502 | { | 
| Gilles Peskine | b9b817e | 2021-10-04 22:15:05 +0200 | [diff] [blame] | 503 | 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 Cron | ea7631b | 2021-06-03 18:51:59 +0200 | [diff] [blame] | 511 | } | 
| Ronald Cron | 7bdbca3 | 2020-12-09 13:34:54 +0100 | [diff] [blame] | 512 | } | 
|  | 513 | else | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 514 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */ | 
| Ronald Cron | 7bdbca3 | 2020-12-09 13:34:54 +0100 | [diff] [blame] | 515 | { | 
|  | 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 |  | 
|  | 527 | exit: | 
|  | 528 | mbedtls_rsa_free( rsa ); | 
|  | 529 | mbedtls_free( rsa ); | 
|  | 530 |  | 
|  | 531 | return( status ); | 
|  | 532 | } | 
|  | 533 |  | 
| Ronald Cron | d2fb854 | 2020-12-09 15:18:01 +0100 | [diff] [blame] | 534 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || | 
|  | 535 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ | 
|  | 536 |  | 
| Przemyslaw Stekiel | 234f318 | 2021-12-09 11:13:58 +0100 | [diff] [blame] | 537 | /****************************************************************/ | 
|  | 538 | /* Asymmetric cryptography */ | 
|  | 539 | /****************************************************************/ | 
|  | 540 |  | 
|  | 541 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) | 
|  | 542 | static 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 ); | 
| Manuel Pégourié-Gonnard | d82a9ed | 2022-07-18 15:21:37 +0200 | [diff] [blame] | 546 | mbedtls_md_type_t md_alg = mbedtls_hash_info_md_from_psa( hash_alg ); | 
| Przemyslaw Stekiel | 234f318 | 2021-12-09 11:13:58 +0100 | [diff] [blame] | 547 |  | 
|  | 548 | return( mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ) ); | 
|  | 549 | } | 
|  | 550 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ | 
|  | 551 |  | 
|  | 552 | psa_status_t mbedtls_psa_asymmetric_encrypt( const psa_key_attributes_t *attributes, | 
|  | 553 | const uint8_t *key_buffer, | 
|  | 554 | size_t key_buffer_size, | 
|  | 555 | psa_algorithm_t alg, | 
|  | 556 | const uint8_t *input, | 
|  | 557 | size_t input_length, | 
|  | 558 | const uint8_t *salt, | 
|  | 559 | size_t salt_length, | 
|  | 560 | uint8_t *output, | 
|  | 561 | size_t output_size, | 
|  | 562 | size_t *output_length ) | 
|  | 563 | { | 
|  | 564 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 565 | (void) key_buffer; | 
|  | 566 | (void) key_buffer_size; | 
|  | 567 | (void) input; | 
|  | 568 | (void) input_length; | 
|  | 569 | (void) salt; | 
|  | 570 | (void) salt_length; | 
|  | 571 | (void) output; | 
|  | 572 | (void) output_size; | 
|  | 573 | (void) output_length; | 
|  | 574 |  | 
|  | 575 | if( PSA_KEY_TYPE_IS_RSA( attributes->core.type ) ) | 
|  | 576 | { | 
|  | 577 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ | 
|  | 578 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) | 
|  | 579 | mbedtls_rsa_context *rsa = NULL; | 
|  | 580 | status = mbedtls_psa_rsa_load_representation( attributes->core.type, | 
|  | 581 | key_buffer, | 
|  | 582 | key_buffer_size, | 
|  | 583 | &rsa ); | 
|  | 584 | if( status != PSA_SUCCESS ) | 
|  | 585 | goto rsa_exit; | 
|  | 586 |  | 
|  | 587 | if( output_size < mbedtls_rsa_get_len( rsa ) ) | 
|  | 588 | { | 
|  | 589 | status = PSA_ERROR_BUFFER_TOO_SMALL; | 
|  | 590 | goto rsa_exit; | 
|  | 591 | } | 
|  | 592 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || | 
|  | 593 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ | 
|  | 594 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) | 
|  | 595 | { | 
|  | 596 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) | 
|  | 597 | status = mbedtls_to_psa_error( | 
|  | 598 | mbedtls_rsa_pkcs1_encrypt( rsa, | 
|  | 599 | mbedtls_psa_get_random, | 
|  | 600 | MBEDTLS_PSA_RANDOM_STATE, | 
|  | 601 | input_length, | 
|  | 602 | input, | 
|  | 603 | output ) ); | 
|  | 604 | #else | 
|  | 605 | status = PSA_ERROR_NOT_SUPPORTED; | 
|  | 606 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */ | 
|  | 607 | } | 
|  | 608 | else | 
|  | 609 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) | 
|  | 610 | { | 
|  | 611 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) | 
|  | 612 | status = mbedtls_to_psa_error( | 
|  | 613 | psa_rsa_oaep_set_padding_mode( alg, rsa ) ); | 
|  | 614 | if( status != PSA_SUCCESS ) | 
|  | 615 | goto rsa_exit; | 
|  | 616 |  | 
|  | 617 | status = mbedtls_to_psa_error( | 
|  | 618 | mbedtls_rsa_rsaes_oaep_encrypt( rsa, | 
|  | 619 | mbedtls_psa_get_random, | 
|  | 620 | MBEDTLS_PSA_RANDOM_STATE, | 
|  | 621 | salt, salt_length, | 
|  | 622 | input_length, | 
|  | 623 | input, | 
|  | 624 | output ) ); | 
|  | 625 | #else | 
|  | 626 | status = PSA_ERROR_NOT_SUPPORTED; | 
|  | 627 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */ | 
|  | 628 | } | 
|  | 629 | else | 
|  | 630 | { | 
|  | 631 | status = PSA_ERROR_INVALID_ARGUMENT; | 
|  | 632 | } | 
|  | 633 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ | 
|  | 634 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) | 
|  | 635 | rsa_exit: | 
|  | 636 | if( status == PSA_SUCCESS ) | 
|  | 637 | *output_length = mbedtls_rsa_get_len( rsa ); | 
|  | 638 |  | 
|  | 639 | mbedtls_rsa_free( rsa ); | 
|  | 640 | mbedtls_free( rsa ); | 
|  | 641 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || | 
|  | 642 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ | 
|  | 643 | } | 
|  | 644 | else | 
|  | 645 | { | 
|  | 646 | status = PSA_ERROR_NOT_SUPPORTED; | 
|  | 647 | } | 
|  | 648 |  | 
|  | 649 | return status; | 
|  | 650 | } | 
|  | 651 |  | 
| Przemyslaw Stekiel | 2ecfd57 | 2021-12-13 09:08:05 +0100 | [diff] [blame] | 652 | psa_status_t mbedtls_psa_asymmetric_decrypt( const psa_key_attributes_t *attributes, | 
|  | 653 | const uint8_t *key_buffer, | 
|  | 654 | size_t key_buffer_size, | 
|  | 655 | psa_algorithm_t alg, | 
|  | 656 | const uint8_t *input, | 
|  | 657 | size_t input_length, | 
|  | 658 | const uint8_t *salt, | 
|  | 659 | size_t salt_length, | 
|  | 660 | uint8_t *output, | 
|  | 661 | size_t output_size, | 
|  | 662 | size_t *output_length ) | 
|  | 663 | { | 
|  | 664 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 665 | (void) key_buffer; | 
|  | 666 | (void) key_buffer_size; | 
|  | 667 | (void) input; | 
|  | 668 | (void) input_length; | 
|  | 669 | (void) salt; | 
|  | 670 | (void) salt_length; | 
|  | 671 | (void) output; | 
|  | 672 | (void) output_size; | 
|  | 673 | (void) output_length; | 
|  | 674 |  | 
|  | 675 | *output_length = 0; | 
|  | 676 |  | 
|  | 677 | if( attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR ) | 
|  | 678 | { | 
|  | 679 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ | 
|  | 680 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) | 
|  | 681 | mbedtls_rsa_context *rsa = NULL; | 
|  | 682 | status = mbedtls_psa_rsa_load_representation( attributes->core.type, | 
|  | 683 | key_buffer, | 
|  | 684 | key_buffer_size, | 
|  | 685 | &rsa ); | 
|  | 686 | if( status != PSA_SUCCESS ) | 
|  | 687 | goto rsa_exit; | 
|  | 688 |  | 
|  | 689 | if( input_length != mbedtls_rsa_get_len( rsa ) ) | 
|  | 690 | { | 
|  | 691 | status = PSA_ERROR_INVALID_ARGUMENT; | 
|  | 692 | goto rsa_exit; | 
|  | 693 | } | 
|  | 694 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || | 
|  | 695 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ | 
|  | 696 |  | 
|  | 697 | if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT ) | 
|  | 698 | { | 
|  | 699 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) | 
|  | 700 | status = mbedtls_to_psa_error( | 
|  | 701 | mbedtls_rsa_pkcs1_decrypt( rsa, | 
|  | 702 | mbedtls_psa_get_random, | 
|  | 703 | MBEDTLS_PSA_RANDOM_STATE, | 
|  | 704 | output_length, | 
|  | 705 | input, | 
|  | 706 | output, | 
|  | 707 | output_size ) ); | 
|  | 708 | #else | 
|  | 709 | status = PSA_ERROR_NOT_SUPPORTED; | 
|  | 710 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */ | 
|  | 711 | } | 
|  | 712 | else | 
|  | 713 | if( PSA_ALG_IS_RSA_OAEP( alg ) ) | 
|  | 714 | { | 
|  | 715 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) | 
|  | 716 | status = mbedtls_to_psa_error( | 
|  | 717 | psa_rsa_oaep_set_padding_mode( alg, rsa ) ); | 
|  | 718 | if( status != PSA_SUCCESS ) | 
|  | 719 | goto rsa_exit; | 
|  | 720 |  | 
|  | 721 | status = mbedtls_to_psa_error( | 
|  | 722 | mbedtls_rsa_rsaes_oaep_decrypt( rsa, | 
|  | 723 | mbedtls_psa_get_random, | 
|  | 724 | MBEDTLS_PSA_RANDOM_STATE, | 
|  | 725 | salt, salt_length, | 
|  | 726 | output_length, | 
|  | 727 | input, | 
|  | 728 | output, | 
|  | 729 | output_size ) ); | 
|  | 730 | #else | 
|  | 731 | status = PSA_ERROR_NOT_SUPPORTED; | 
|  | 732 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */ | 
|  | 733 | } | 
|  | 734 | else | 
|  | 735 | { | 
|  | 736 | status = PSA_ERROR_INVALID_ARGUMENT; | 
|  | 737 | } | 
|  | 738 |  | 
|  | 739 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ | 
|  | 740 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) | 
|  | 741 | rsa_exit: | 
|  | 742 | mbedtls_rsa_free( rsa ); | 
|  | 743 | mbedtls_free( rsa ); | 
|  | 744 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || | 
|  | 745 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */ | 
|  | 746 | } | 
|  | 747 | else | 
|  | 748 | { | 
|  | 749 | status = PSA_ERROR_NOT_SUPPORTED; | 
|  | 750 | } | 
|  | 751 |  | 
|  | 752 | return status; | 
|  | 753 | } | 
|  | 754 |  | 
| Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 755 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |