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> |
| 26 | #include "psa_crypto_core.h" |
Ronald Cron | 9e18fc1 | 2020-11-05 17:36:40 +0100 | [diff] [blame] | 27 | #include "psa_crypto_random_impl.h" |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 28 | #include "psa_crypto_rsa.h" |
Steven Cooreman | 5f88e77 | 2021-03-15 11:07:12 +0100 | [diff] [blame] | 29 | #include "psa_crypto_hash.h" |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 30 | |
| 31 | #include <stdlib.h> |
| 32 | #include <string.h> |
| 33 | #include "mbedtls/platform.h" |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 34 | |
| 35 | #include <mbedtls/rsa.h> |
| 36 | #include <mbedtls/error.h> |
| 37 | #include <mbedtls/pk.h> |
| 38 | #include <mbedtls/pk_internal.h> |
| 39 | |
| 40 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 41 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \ |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 42 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 43 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \ |
| 44 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 45 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 46 | |
| 47 | /* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes |
| 48 | * that are not a multiple of 8) well. For example, there is only |
| 49 | * mbedtls_rsa_get_len(), which returns a number of bytes, and no |
| 50 | * way to return the exact bit size of a key. |
| 51 | * To keep things simple, reject non-byte-aligned key sizes. */ |
| 52 | static psa_status_t psa_check_rsa_key_byte_aligned( |
| 53 | const mbedtls_rsa_context *rsa ) |
| 54 | { |
| 55 | mbedtls_mpi n; |
| 56 | psa_status_t status; |
| 57 | mbedtls_mpi_init( &n ); |
| 58 | status = mbedtls_to_psa_error( |
| 59 | mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) ); |
| 60 | if( status == PSA_SUCCESS ) |
| 61 | { |
| 62 | if( mbedtls_mpi_bitlen( &n ) % 8 != 0 ) |
| 63 | status = PSA_ERROR_NOT_SUPPORTED; |
| 64 | } |
| 65 | mbedtls_mpi_free( &n ); |
| 66 | return( status ); |
| 67 | } |
| 68 | |
| 69 | psa_status_t mbedtls_psa_rsa_load_representation( |
| 70 | psa_key_type_t type, const uint8_t *data, size_t data_length, |
| 71 | mbedtls_rsa_context **p_rsa ) |
| 72 | { |
| 73 | psa_status_t status; |
| 74 | mbedtls_pk_context ctx; |
| 75 | size_t bits; |
| 76 | mbedtls_pk_init( &ctx ); |
| 77 | |
| 78 | /* Parse the data. */ |
| 79 | if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
| 80 | status = mbedtls_to_psa_error( |
| 81 | mbedtls_pk_parse_key( &ctx, data, data_length, NULL, 0 ) ); |
| 82 | else |
| 83 | status = mbedtls_to_psa_error( |
| 84 | mbedtls_pk_parse_public_key( &ctx, data, data_length ) ); |
| 85 | if( status != PSA_SUCCESS ) |
| 86 | goto exit; |
| 87 | |
| 88 | /* We have something that the pkparse module recognizes. If it is a |
| 89 | * valid RSA key, store it. */ |
| 90 | if( mbedtls_pk_get_type( &ctx ) != MBEDTLS_PK_RSA ) |
| 91 | { |
| 92 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 93 | goto exit; |
| 94 | } |
| 95 | |
| 96 | /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS |
| 97 | * supports non-byte-aligned key sizes, but not well. For example, |
| 98 | * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */ |
| 99 | bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( mbedtls_pk_rsa( ctx ) ) ); |
| 100 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 101 | { |
| 102 | status = PSA_ERROR_NOT_SUPPORTED; |
| 103 | goto exit; |
| 104 | } |
| 105 | status = psa_check_rsa_key_byte_aligned( mbedtls_pk_rsa( ctx ) ); |
| 106 | if( status != PSA_SUCCESS ) |
| 107 | goto exit; |
| 108 | |
| 109 | /* Copy out the pointer to the RSA context, and reset the PK context |
| 110 | * such that pk_free doesn't free the RSA context we just grabbed. */ |
| 111 | *p_rsa = mbedtls_pk_rsa( ctx ); |
| 112 | ctx.pk_info = NULL; |
| 113 | |
| 114 | exit: |
| 115 | mbedtls_pk_free( &ctx ); |
| 116 | return( status ); |
| 117 | } |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 118 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 119 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 120 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 121 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || |
| 122 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 123 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 124 | |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 125 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 126 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Ronald Cron | abf2aef | 2020-11-27 18:13:44 +0100 | [diff] [blame] | 127 | |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 128 | psa_status_t mbedtls_psa_rsa_import_key( |
Ronald Cron | abf2aef | 2020-11-27 18:13:44 +0100 | [diff] [blame] | 129 | const psa_key_attributes_t *attributes, |
| 130 | const uint8_t *data, size_t data_length, |
| 131 | uint8_t *key_buffer, size_t key_buffer_size, |
| 132 | size_t *key_buffer_length, size_t *bits ) |
| 133 | { |
| 134 | psa_status_t status; |
| 135 | mbedtls_rsa_context *rsa = NULL; |
| 136 | |
| 137 | /* Parse input */ |
| 138 | status = mbedtls_psa_rsa_load_representation( attributes->core.type, |
| 139 | data, |
| 140 | data_length, |
| 141 | &rsa ); |
| 142 | if( status != PSA_SUCCESS ) |
| 143 | goto exit; |
| 144 | |
| 145 | *bits = (psa_key_bits_t) PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) ); |
| 146 | |
| 147 | /* Re-export the data to PSA export format, such that we can store export |
| 148 | * representation in the key slot. Export representation in case of RSA is |
| 149 | * the smallest representation that's allowed as input, so a straight-up |
| 150 | * allocation of the same size as the input buffer will be large enough. */ |
| 151 | status = mbedtls_psa_rsa_export_key( attributes->core.type, |
| 152 | rsa, |
| 153 | key_buffer, |
| 154 | key_buffer_size, |
| 155 | key_buffer_length ); |
| 156 | exit: |
| 157 | /* Always free the RSA object */ |
| 158 | mbedtls_rsa_free( rsa ); |
| 159 | mbedtls_free( rsa ); |
| 160 | |
| 161 | return( status ); |
| 162 | } |
| 163 | |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 164 | psa_status_t mbedtls_psa_rsa_export_key( psa_key_type_t type, |
| 165 | mbedtls_rsa_context *rsa, |
| 166 | uint8_t *data, |
| 167 | size_t data_size, |
| 168 | size_t *data_length ) |
| 169 | { |
| 170 | #if defined(MBEDTLS_PK_WRITE_C) |
| 171 | int ret; |
| 172 | mbedtls_pk_context pk; |
| 173 | uint8_t *pos = data + data_size; |
| 174 | |
| 175 | mbedtls_pk_init( &pk ); |
| 176 | pk.pk_info = &mbedtls_rsa_info; |
| 177 | pk.pk_ctx = rsa; |
| 178 | |
| 179 | /* PSA Crypto API defines the format of an RSA key as a DER-encoded |
| 180 | * representation of the non-encrypted PKCS#1 RSAPrivateKey for a |
| 181 | * private key and of the RFC3279 RSAPublicKey for a public key. */ |
| 182 | if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
| 183 | ret = mbedtls_pk_write_key_der( &pk, data, data_size ); |
| 184 | else |
| 185 | ret = mbedtls_pk_write_pubkey( &pos, data, &pk ); |
| 186 | |
| 187 | if( ret < 0 ) |
| 188 | { |
| 189 | /* Clean up in case pk_write failed halfway through. */ |
| 190 | memset( data, 0, data_size ); |
| 191 | return( mbedtls_to_psa_error( ret ) ); |
| 192 | } |
| 193 | |
| 194 | /* The mbedtls_pk_xxx functions write to the end of the buffer. |
| 195 | * Move the data to the beginning and erase remaining data |
| 196 | * at the original location. */ |
| 197 | if( 2 * (size_t) ret <= data_size ) |
| 198 | { |
| 199 | memcpy( data, data + data_size - ret, ret ); |
| 200 | memset( data + data_size - ret, 0, ret ); |
| 201 | } |
| 202 | else if( (size_t) ret < data_size ) |
| 203 | { |
| 204 | memmove( data, data + data_size - ret, ret ); |
| 205 | memset( data + ret, 0, data_size - ret ); |
| 206 | } |
| 207 | |
| 208 | *data_length = ret; |
| 209 | return( PSA_SUCCESS ); |
| 210 | #else |
| 211 | (void) type; |
| 212 | (void) rsa; |
| 213 | (void) data; |
| 214 | (void) data_size; |
| 215 | (void) data_length; |
| 216 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 217 | #endif /* MBEDTLS_PK_WRITE_C */ |
| 218 | } |
| 219 | |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 220 | psa_status_t mbedtls_psa_rsa_export_public_key( |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 221 | const psa_key_attributes_t *attributes, |
| 222 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 223 | uint8_t *data, size_t data_size, size_t *data_length ) |
| 224 | { |
| 225 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 226 | mbedtls_rsa_context *rsa = NULL; |
| 227 | |
| 228 | status = mbedtls_psa_rsa_load_representation( |
| 229 | attributes->core.type, key_buffer, key_buffer_size, &rsa ); |
| 230 | if( status != PSA_SUCCESS ) |
| 231 | return( status ); |
| 232 | |
| 233 | status = mbedtls_psa_rsa_export_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY, |
| 234 | rsa, |
| 235 | data, |
| 236 | data_size, |
| 237 | data_length ); |
| 238 | |
| 239 | mbedtls_rsa_free( rsa ); |
| 240 | mbedtls_free( rsa ); |
| 241 | |
| 242 | return( status ); |
| 243 | } |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 244 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 245 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Ronald Cron | f1057d3 | 2020-11-26 19:19:10 +0100 | [diff] [blame] | 246 | |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 247 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \ |
Jaeden Amero | c17f293 | 2021-05-14 08:34:32 +0100 | [diff] [blame] | 248 | defined(MBEDTLS_GENPRIME) |
Ronald Cron | 2365fde | 2021-02-08 09:52:24 +0100 | [diff] [blame] | 249 | 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] | 250 | size_t domain_parameters_size, |
| 251 | int *exponent ) |
| 252 | { |
| 253 | size_t i; |
| 254 | uint32_t acc = 0; |
| 255 | |
| 256 | if( domain_parameters_size == 0 ) |
| 257 | { |
| 258 | *exponent = 65537; |
| 259 | return( PSA_SUCCESS ); |
| 260 | } |
| 261 | |
| 262 | /* Mbed TLS encodes the public exponent as an int. For simplicity, only |
| 263 | * support values that fit in a 32-bit integer, which is larger than |
| 264 | * int on just about every platform anyway. */ |
| 265 | if( domain_parameters_size > sizeof( acc ) ) |
| 266 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 267 | for( i = 0; i < domain_parameters_size; i++ ) |
| 268 | acc = ( acc << 8 ) | domain_parameters[i]; |
| 269 | if( acc > INT_MAX ) |
| 270 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 271 | *exponent = acc; |
| 272 | return( PSA_SUCCESS ); |
| 273 | } |
| 274 | |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 275 | psa_status_t mbedtls_psa_rsa_generate_key( |
Ronald Cron | 9e18fc1 | 2020-11-05 17:36:40 +0100 | [diff] [blame] | 276 | const psa_key_attributes_t *attributes, |
| 277 | uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length ) |
| 278 | { |
| 279 | psa_status_t status; |
| 280 | mbedtls_rsa_context rsa; |
| 281 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 282 | int exponent; |
| 283 | |
Ronald Cron | 2365fde | 2021-02-08 09:52:24 +0100 | [diff] [blame] | 284 | status = psa_rsa_read_exponent( attributes->domain_parameters, |
Ronald Cron | 9e18fc1 | 2020-11-05 17:36:40 +0100 | [diff] [blame] | 285 | attributes->domain_parameters_size, |
| 286 | &exponent ); |
| 287 | if( status != PSA_SUCCESS ) |
| 288 | return( status ); |
| 289 | |
| 290 | mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE ); |
| 291 | ret = mbedtls_rsa_gen_key( &rsa, |
| 292 | mbedtls_psa_get_random, |
| 293 | MBEDTLS_PSA_RANDOM_STATE, |
| 294 | (unsigned int)attributes->core.bits, |
| 295 | exponent ); |
| 296 | if( ret != 0 ) |
| 297 | return( mbedtls_to_psa_error( ret ) ); |
| 298 | |
| 299 | status = mbedtls_psa_rsa_export_key( attributes->core.type, |
| 300 | &rsa, key_buffer, key_buffer_size, |
| 301 | key_buffer_length ); |
| 302 | mbedtls_rsa_free( &rsa ); |
| 303 | |
| 304 | return( status ); |
| 305 | } |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 306 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) |
Jaeden Amero | c17f293 | 2021-05-14 08:34:32 +0100 | [diff] [blame] | 307 | * defined(MBEDTLS_GENPRIME) */ |
Ronald Cron | 9e18fc1 | 2020-11-05 17:36:40 +0100 | [diff] [blame] | 308 | |
Ronald Cron | 7bdbca3 | 2020-12-09 13:34:54 +0100 | [diff] [blame] | 309 | /****************************************************************/ |
| 310 | /* Sign/verify hashes */ |
| 311 | /****************************************************************/ |
| 312 | |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 313 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 314 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Ronald Cron | 7bdbca3 | 2020-12-09 13:34:54 +0100 | [diff] [blame] | 315 | |
| 316 | /* Decode the hash algorithm from alg and store the mbedtls encoding in |
| 317 | * md_alg. Verify that the hash length is acceptable. */ |
| 318 | static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg, |
| 319 | size_t hash_length, |
| 320 | mbedtls_md_type_t *md_alg ) |
| 321 | { |
| 322 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
| 323 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 324 | *md_alg = mbedtls_md_get_type( md_info ); |
| 325 | |
| 326 | /* The Mbed TLS RSA module uses an unsigned int for hash length |
| 327 | * parameters. Validate that it fits so that we don't risk an |
| 328 | * overflow later. */ |
| 329 | #if SIZE_MAX > UINT_MAX |
| 330 | if( hash_length > UINT_MAX ) |
| 331 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 332 | #endif |
| 333 | |
Janos Follath | b23b574 | 2021-06-07 14:34:10 +0100 | [diff] [blame] | 334 | /* For signatures using a hash, the hash length must be correct. */ |
| 335 | if( alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW ) |
Ronald Cron | 7bdbca3 | 2020-12-09 13:34:54 +0100 | [diff] [blame] | 336 | { |
| 337 | if( md_info == NULL ) |
| 338 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 339 | if( mbedtls_md_get_size( md_info ) != hash_length ) |
| 340 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 341 | } |
Ronald Cron | 7bdbca3 | 2020-12-09 13:34:54 +0100 | [diff] [blame] | 342 | |
| 343 | return( PSA_SUCCESS ); |
| 344 | } |
| 345 | |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 346 | psa_status_t mbedtls_psa_rsa_sign_hash( |
Ronald Cron | 7bdbca3 | 2020-12-09 13:34:54 +0100 | [diff] [blame] | 347 | const psa_key_attributes_t *attributes, |
| 348 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 349 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
| 350 | uint8_t *signature, size_t signature_size, size_t *signature_length ) |
| 351 | { |
| 352 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 353 | mbedtls_rsa_context *rsa = NULL; |
| 354 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 355 | mbedtls_md_type_t md_alg; |
| 356 | |
| 357 | status = mbedtls_psa_rsa_load_representation( attributes->core.type, |
| 358 | key_buffer, |
| 359 | key_buffer_size, |
| 360 | &rsa ); |
| 361 | if( status != PSA_SUCCESS ) |
| 362 | return( status ); |
| 363 | |
| 364 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 365 | if( status != PSA_SUCCESS ) |
| 366 | goto exit; |
| 367 | |
| 368 | if( signature_size < mbedtls_rsa_get_len( rsa ) ) |
| 369 | { |
| 370 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 371 | goto exit; |
| 372 | } |
| 373 | |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 374 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) |
Ronald Cron | 7bdbca3 | 2020-12-09 13:34:54 +0100 | [diff] [blame] | 375 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 376 | { |
| 377 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 378 | MBEDTLS_MD_NONE ); |
| 379 | ret = mbedtls_rsa_pkcs1_sign( rsa, |
| 380 | mbedtls_psa_get_random, |
| 381 | MBEDTLS_PSA_RANDOM_STATE, |
| 382 | MBEDTLS_RSA_PRIVATE, |
| 383 | md_alg, |
| 384 | (unsigned int) hash_length, |
| 385 | hash, |
| 386 | signature ); |
| 387 | } |
| 388 | else |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 389 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */ |
| 390 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Ronald Cron | 7bdbca3 | 2020-12-09 13:34:54 +0100 | [diff] [blame] | 391 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 392 | { |
| 393 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 394 | ret = mbedtls_rsa_rsassa_pss_sign( rsa, |
| 395 | mbedtls_psa_get_random, |
| 396 | MBEDTLS_PSA_RANDOM_STATE, |
| 397 | MBEDTLS_RSA_PRIVATE, |
| 398 | MBEDTLS_MD_NONE, |
| 399 | (unsigned int) hash_length, |
| 400 | hash, |
| 401 | signature ); |
| 402 | } |
| 403 | else |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 404 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */ |
Ronald Cron | 7bdbca3 | 2020-12-09 13:34:54 +0100 | [diff] [blame] | 405 | { |
| 406 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 407 | goto exit; |
| 408 | } |
| 409 | |
| 410 | if( ret == 0 ) |
| 411 | *signature_length = mbedtls_rsa_get_len( rsa ); |
| 412 | status = mbedtls_to_psa_error( ret ); |
| 413 | |
| 414 | exit: |
| 415 | mbedtls_rsa_free( rsa ); |
| 416 | mbedtls_free( rsa ); |
| 417 | |
| 418 | return( status ); |
| 419 | } |
| 420 | |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 421 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Gilles Peskine | 44fa40cd | 2021-10-04 22:15:05 +0200 | [diff] [blame] | 422 | static int rsa_pss_expected_salt_len( psa_algorithm_t alg, |
| 423 | const mbedtls_rsa_context *rsa, |
| 424 | size_t hash_length ) |
| 425 | { |
| 426 | if( PSA_ALG_IS_RSA_PSS_ANY_SALT( alg ) ) |
| 427 | return( MBEDTLS_RSA_SALT_LEN_ANY ); |
| 428 | /* Otherwise: standard salt length, i.e. largest possible salt length |
| 429 | * up to the hash length. */ |
Gilles Peskine | f8362ca | 2021-10-08 16:28:32 +0200 | [diff] [blame] | 430 | int klen = (int) mbedtls_rsa_get_len( rsa ); // known to fit |
Gilles Peskine | 44fa40cd | 2021-10-04 22:15:05 +0200 | [diff] [blame] | 431 | int hlen = (int) hash_length; // known to fit |
| 432 | int room = klen - 2 - hlen; |
| 433 | if( room < 0 ) |
| 434 | return( 0 ); // there is no valid signature in this case anyway |
| 435 | else if( room > hlen ) |
| 436 | return( hlen ); |
| 437 | else |
| 438 | return( room ); |
| 439 | } |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 440 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */ |
Gilles Peskine | 44fa40cd | 2021-10-04 22:15:05 +0200 | [diff] [blame] | 441 | |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 442 | psa_status_t mbedtls_psa_rsa_verify_hash( |
Ronald Cron | 7bdbca3 | 2020-12-09 13:34:54 +0100 | [diff] [blame] | 443 | const psa_key_attributes_t *attributes, |
| 444 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 445 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
| 446 | const uint8_t *signature, size_t signature_length ) |
| 447 | { |
| 448 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 449 | mbedtls_rsa_context *rsa = NULL; |
| 450 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 451 | mbedtls_md_type_t md_alg; |
| 452 | |
| 453 | status = mbedtls_psa_rsa_load_representation( attributes->core.type, |
| 454 | key_buffer, |
| 455 | key_buffer_size, |
| 456 | &rsa ); |
| 457 | if( status != PSA_SUCCESS ) |
| 458 | goto exit; |
| 459 | |
| 460 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 461 | if( status != PSA_SUCCESS ) |
| 462 | goto exit; |
| 463 | |
| 464 | if( signature_length != mbedtls_rsa_get_len( rsa ) ) |
| 465 | { |
| 466 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 467 | goto exit; |
| 468 | } |
| 469 | |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 470 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) |
Ronald Cron | 7bdbca3 | 2020-12-09 13:34:54 +0100 | [diff] [blame] | 471 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 472 | { |
| 473 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 474 | MBEDTLS_MD_NONE ); |
| 475 | ret = mbedtls_rsa_pkcs1_verify( rsa, |
| 476 | mbedtls_psa_get_random, |
| 477 | MBEDTLS_PSA_RANDOM_STATE, |
| 478 | MBEDTLS_RSA_PUBLIC, |
| 479 | md_alg, |
| 480 | (unsigned int) hash_length, |
| 481 | hash, |
| 482 | signature ); |
| 483 | } |
| 484 | else |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 485 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */ |
| 486 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
Ronald Cron | 7bdbca3 | 2020-12-09 13:34:54 +0100 | [diff] [blame] | 487 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 488 | { |
Gilles Peskine | 44fa40cd | 2021-10-04 22:15:05 +0200 | [diff] [blame] | 489 | int slen = rsa_pss_expected_salt_len( alg, rsa, hash_length ); |
Ronald Cron | 7bdbca3 | 2020-12-09 13:34:54 +0100 | [diff] [blame] | 490 | mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
Gilles Peskine | 44fa40cd | 2021-10-04 22:15:05 +0200 | [diff] [blame] | 491 | ret = mbedtls_rsa_rsassa_pss_verify_ext( rsa, |
| 492 | mbedtls_psa_get_random, |
| 493 | MBEDTLS_PSA_RANDOM_STATE, |
| 494 | MBEDTLS_RSA_PUBLIC, |
| 495 | md_alg, |
| 496 | (unsigned int) hash_length, |
| 497 | hash, |
| 498 | md_alg, |
| 499 | slen, |
| 500 | signature ); |
Ronald Cron | 7bdbca3 | 2020-12-09 13:34:54 +0100 | [diff] [blame] | 501 | } |
| 502 | else |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 503 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */ |
Ronald Cron | 7bdbca3 | 2020-12-09 13:34:54 +0100 | [diff] [blame] | 504 | { |
| 505 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 506 | goto exit; |
| 507 | } |
| 508 | |
| 509 | /* Mbed TLS distinguishes "invalid padding" from "valid padding but |
| 510 | * the rest of the signature is invalid". This has little use in |
| 511 | * practice and PSA doesn't report this distinction. */ |
| 512 | status = ( ret == MBEDTLS_ERR_RSA_INVALID_PADDING ) ? |
| 513 | PSA_ERROR_INVALID_SIGNATURE : |
| 514 | mbedtls_to_psa_error( ret ); |
| 515 | |
| 516 | exit: |
| 517 | mbedtls_rsa_free( rsa ); |
| 518 | mbedtls_free( rsa ); |
| 519 | |
| 520 | return( status ); |
| 521 | } |
| 522 | |
Ronald Cron | d2fb854 | 2020-12-09 15:18:01 +0100 | [diff] [blame] | 523 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 524 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
| 525 | |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 526 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |