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" |
| 34 | #if !defined(MBEDTLS_PLATFORM_C) |
| 35 | #define mbedtls_calloc calloc |
| 36 | #define mbedtls_free free |
| 37 | #endif |
| 38 | |
| 39 | #include <mbedtls/rsa.h> |
| 40 | #include <mbedtls/error.h> |
| 41 | #include <mbedtls/pk.h> |
Dave Rodgman | 3b5e6f0 | 2021-04-06 17:58:16 +0100 | [diff] [blame] | 42 | #include "pk_wrap.h" |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 43 | |
Ronald Cron | f1057d3 | 2020-11-26 19:19:10 +0100 | [diff] [blame] | 44 | #if ( defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 45 | ( defined(PSA_CRYPTO_DRIVER_TEST) && \ |
| 46 | defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_RSA_KEY_PAIR) ) ) |
| 47 | #define BUILTIN_KEY_TYPE_RSA_KEY_PAIR 1 |
| 48 | #endif |
| 49 | |
| 50 | #if ( defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) || \ |
| 51 | ( defined(PSA_CRYPTO_DRIVER_TEST) && \ |
| 52 | defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_RSA_PUBLIC_KEY) ) ) |
| 53 | #define BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY 1 |
| 54 | #endif |
| 55 | |
Ronald Cron | d2fb854 | 2020-12-09 15:18:01 +0100 | [diff] [blame] | 56 | #if ( defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 57 | ( defined(PSA_CRYPTO_DRIVER_TEST) && \ |
| 58 | defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PKCS1V15_SIGN) && \ |
| 59 | defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V15) ) ) |
| 60 | #define BUILTIN_ALG_RSA_PKCS1V15_SIGN 1 |
| 61 | #endif |
| 62 | |
| 63 | #if ( defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \ |
| 64 | ( defined(PSA_CRYPTO_DRIVER_TEST) && \ |
| 65 | defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PSS) && \ |
| 66 | defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) ) ) |
| 67 | #define BUILTIN_ALG_RSA_PSS 1 |
| 68 | #endif |
| 69 | |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 70 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \ |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 71 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \ |
Ronald Cron | d2fb854 | 2020-12-09 15:18:01 +0100 | [diff] [blame] | 72 | defined(BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 73 | defined(BUILTIN_ALG_RSA_PSS) || \ |
Ronald Cron | f1057d3 | 2020-11-26 19:19:10 +0100 | [diff] [blame] | 74 | defined(BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 75 | defined(BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 76 | |
| 77 | /* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes |
| 78 | * that are not a multiple of 8) well. For example, there is only |
| 79 | * mbedtls_rsa_get_len(), which returns a number of bytes, and no |
| 80 | * way to return the exact bit size of a key. |
| 81 | * To keep things simple, reject non-byte-aligned key sizes. */ |
| 82 | static psa_status_t psa_check_rsa_key_byte_aligned( |
| 83 | const mbedtls_rsa_context *rsa ) |
| 84 | { |
| 85 | mbedtls_mpi n; |
| 86 | psa_status_t status; |
| 87 | mbedtls_mpi_init( &n ); |
| 88 | status = mbedtls_to_psa_error( |
| 89 | mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) ); |
| 90 | if( status == PSA_SUCCESS ) |
| 91 | { |
| 92 | if( mbedtls_mpi_bitlen( &n ) % 8 != 0 ) |
| 93 | status = PSA_ERROR_NOT_SUPPORTED; |
| 94 | } |
| 95 | mbedtls_mpi_free( &n ); |
| 96 | return( status ); |
| 97 | } |
| 98 | |
| 99 | psa_status_t mbedtls_psa_rsa_load_representation( |
| 100 | psa_key_type_t type, const uint8_t *data, size_t data_length, |
| 101 | mbedtls_rsa_context **p_rsa ) |
| 102 | { |
| 103 | psa_status_t status; |
| 104 | mbedtls_pk_context ctx; |
| 105 | size_t bits; |
| 106 | mbedtls_pk_init( &ctx ); |
| 107 | |
| 108 | /* Parse the data. */ |
| 109 | if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
| 110 | status = mbedtls_to_psa_error( |
Manuel Pégourié-Gonnard | 84dea01 | 2021-06-15 11:29:26 +0200 | [diff] [blame^] | 111 | mbedtls_pk_parse_key( &ctx, data, data_length, NULL, 0, |
| 112 | mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) ); |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 113 | else |
| 114 | status = mbedtls_to_psa_error( |
| 115 | mbedtls_pk_parse_public_key( &ctx, data, data_length ) ); |
| 116 | if( status != PSA_SUCCESS ) |
| 117 | goto exit; |
| 118 | |
| 119 | /* We have something that the pkparse module recognizes. If it is a |
| 120 | * valid RSA key, store it. */ |
| 121 | if( mbedtls_pk_get_type( &ctx ) != MBEDTLS_PK_RSA ) |
| 122 | { |
| 123 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 124 | goto exit; |
| 125 | } |
| 126 | |
| 127 | /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS |
| 128 | * supports non-byte-aligned key sizes, but not well. For example, |
| 129 | * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */ |
| 130 | bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( mbedtls_pk_rsa( ctx ) ) ); |
| 131 | if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS ) |
| 132 | { |
| 133 | status = PSA_ERROR_NOT_SUPPORTED; |
| 134 | goto exit; |
| 135 | } |
| 136 | status = psa_check_rsa_key_byte_aligned( mbedtls_pk_rsa( ctx ) ); |
| 137 | if( status != PSA_SUCCESS ) |
| 138 | goto exit; |
| 139 | |
| 140 | /* Copy out the pointer to the RSA context, and reset the PK context |
| 141 | * such that pk_free doesn't free the RSA context we just grabbed. */ |
| 142 | *p_rsa = mbedtls_pk_rsa( ctx ); |
| 143 | ctx.pk_info = NULL; |
| 144 | |
| 145 | exit: |
| 146 | mbedtls_pk_free( &ctx ); |
| 147 | return( status ); |
| 148 | } |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 149 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 150 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || |
Ronald Cron | d2fb854 | 2020-12-09 15:18:01 +0100 | [diff] [blame] | 151 | * defined(BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 152 | * defined(BUILTIN_ALG_RSA_PSS) || |
Ronald Cron | f1057d3 | 2020-11-26 19:19:10 +0100 | [diff] [blame] | 153 | * defined(BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 154 | * defined(BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 155 | |
Ronald Cron | f1057d3 | 2020-11-26 19:19:10 +0100 | [diff] [blame] | 156 | #if defined(BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 157 | defined(BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
Ronald Cron | abf2aef | 2020-11-27 18:13:44 +0100 | [diff] [blame] | 158 | |
Ronald Cron | 784fb32 | 2020-11-30 13:55:05 +0100 | [diff] [blame] | 159 | static psa_status_t rsa_import_key( |
Ronald Cron | abf2aef | 2020-11-27 18:13:44 +0100 | [diff] [blame] | 160 | const psa_key_attributes_t *attributes, |
| 161 | const uint8_t *data, size_t data_length, |
| 162 | uint8_t *key_buffer, size_t key_buffer_size, |
| 163 | size_t *key_buffer_length, size_t *bits ) |
| 164 | { |
| 165 | psa_status_t status; |
| 166 | mbedtls_rsa_context *rsa = NULL; |
| 167 | |
| 168 | /* Parse input */ |
| 169 | status = mbedtls_psa_rsa_load_representation( attributes->core.type, |
| 170 | data, |
| 171 | data_length, |
| 172 | &rsa ); |
| 173 | if( status != PSA_SUCCESS ) |
| 174 | goto exit; |
| 175 | |
| 176 | *bits = (psa_key_bits_t) PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) ); |
| 177 | |
| 178 | /* Re-export the data to PSA export format, such that we can store export |
| 179 | * representation in the key slot. Export representation in case of RSA is |
| 180 | * the smallest representation that's allowed as input, so a straight-up |
| 181 | * allocation of the same size as the input buffer will be large enough. */ |
| 182 | status = mbedtls_psa_rsa_export_key( attributes->core.type, |
| 183 | rsa, |
| 184 | key_buffer, |
| 185 | key_buffer_size, |
| 186 | key_buffer_length ); |
| 187 | exit: |
| 188 | /* Always free the RSA object */ |
| 189 | mbedtls_rsa_free( rsa ); |
| 190 | mbedtls_free( rsa ); |
| 191 | |
| 192 | return( status ); |
| 193 | } |
| 194 | |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 195 | psa_status_t mbedtls_psa_rsa_export_key( psa_key_type_t type, |
| 196 | mbedtls_rsa_context *rsa, |
| 197 | uint8_t *data, |
| 198 | size_t data_size, |
| 199 | size_t *data_length ) |
| 200 | { |
| 201 | #if defined(MBEDTLS_PK_WRITE_C) |
| 202 | int ret; |
| 203 | mbedtls_pk_context pk; |
| 204 | uint8_t *pos = data + data_size; |
| 205 | |
| 206 | mbedtls_pk_init( &pk ); |
| 207 | pk.pk_info = &mbedtls_rsa_info; |
| 208 | pk.pk_ctx = rsa; |
| 209 | |
| 210 | /* PSA Crypto API defines the format of an RSA key as a DER-encoded |
| 211 | * representation of the non-encrypted PKCS#1 RSAPrivateKey for a |
| 212 | * private key and of the RFC3279 RSAPublicKey for a public key. */ |
| 213 | if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) ) |
| 214 | ret = mbedtls_pk_write_key_der( &pk, data, data_size ); |
| 215 | else |
| 216 | ret = mbedtls_pk_write_pubkey( &pos, data, &pk ); |
| 217 | |
| 218 | if( ret < 0 ) |
| 219 | { |
| 220 | /* Clean up in case pk_write failed halfway through. */ |
| 221 | memset( data, 0, data_size ); |
| 222 | return( mbedtls_to_psa_error( ret ) ); |
| 223 | } |
| 224 | |
| 225 | /* The mbedtls_pk_xxx functions write to the end of the buffer. |
| 226 | * Move the data to the beginning and erase remaining data |
| 227 | * at the original location. */ |
| 228 | if( 2 * (size_t) ret <= data_size ) |
| 229 | { |
| 230 | memcpy( data, data + data_size - ret, ret ); |
| 231 | memset( data + data_size - ret, 0, ret ); |
| 232 | } |
| 233 | else if( (size_t) ret < data_size ) |
| 234 | { |
| 235 | memmove( data, data + data_size - ret, ret ); |
| 236 | memset( data + ret, 0, data_size - ret ); |
| 237 | } |
| 238 | |
| 239 | *data_length = ret; |
| 240 | return( PSA_SUCCESS ); |
| 241 | #else |
| 242 | (void) type; |
| 243 | (void) rsa; |
| 244 | (void) data; |
| 245 | (void) data_size; |
| 246 | (void) data_length; |
| 247 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 248 | #endif /* MBEDTLS_PK_WRITE_C */ |
| 249 | } |
| 250 | |
Ronald Cron | f1057d3 | 2020-11-26 19:19:10 +0100 | [diff] [blame] | 251 | static psa_status_t rsa_export_public_key( |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 252 | const psa_key_attributes_t *attributes, |
| 253 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 254 | uint8_t *data, size_t data_size, size_t *data_length ) |
| 255 | { |
| 256 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 257 | mbedtls_rsa_context *rsa = NULL; |
| 258 | |
| 259 | status = mbedtls_psa_rsa_load_representation( |
| 260 | attributes->core.type, key_buffer, key_buffer_size, &rsa ); |
| 261 | if( status != PSA_SUCCESS ) |
| 262 | return( status ); |
| 263 | |
| 264 | status = mbedtls_psa_rsa_export_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY, |
| 265 | rsa, |
| 266 | data, |
| 267 | data_size, |
| 268 | data_length ); |
| 269 | |
| 270 | mbedtls_rsa_free( rsa ); |
| 271 | mbedtls_free( rsa ); |
| 272 | |
| 273 | return( status ); |
| 274 | } |
Ronald Cron | f1057d3 | 2020-11-26 19:19:10 +0100 | [diff] [blame] | 275 | #endif /* defined(BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 276 | * defined(BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
| 277 | |
Jaeden Amero | 424fa93 | 2021-05-14 08:34:32 +0100 | [diff] [blame] | 278 | #if defined(BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \ |
| 279 | defined(MBEDTLS_GENPRIME) |
Ronald Cron | 2365fde | 2021-02-08 09:52:24 +0100 | [diff] [blame] | 280 | 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] | 281 | size_t domain_parameters_size, |
| 282 | int *exponent ) |
| 283 | { |
| 284 | size_t i; |
| 285 | uint32_t acc = 0; |
| 286 | |
| 287 | if( domain_parameters_size == 0 ) |
| 288 | { |
| 289 | *exponent = 65537; |
| 290 | return( PSA_SUCCESS ); |
| 291 | } |
| 292 | |
| 293 | /* Mbed TLS encodes the public exponent as an int. For simplicity, only |
| 294 | * support values that fit in a 32-bit integer, which is larger than |
| 295 | * int on just about every platform anyway. */ |
| 296 | if( domain_parameters_size > sizeof( acc ) ) |
| 297 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 298 | for( i = 0; i < domain_parameters_size; i++ ) |
| 299 | acc = ( acc << 8 ) | domain_parameters[i]; |
| 300 | if( acc > INT_MAX ) |
| 301 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 302 | *exponent = acc; |
| 303 | return( PSA_SUCCESS ); |
| 304 | } |
| 305 | |
Ronald Cron | 3a9c46b | 2020-11-06 09:38:35 +0100 | [diff] [blame] | 306 | static psa_status_t rsa_generate_key( |
Ronald Cron | 9e18fc1 | 2020-11-05 17:36:40 +0100 | [diff] [blame] | 307 | const psa_key_attributes_t *attributes, |
| 308 | uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length ) |
| 309 | { |
| 310 | psa_status_t status; |
| 311 | mbedtls_rsa_context rsa; |
| 312 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 313 | int exponent; |
| 314 | |
Ronald Cron | 2365fde | 2021-02-08 09:52:24 +0100 | [diff] [blame] | 315 | status = psa_rsa_read_exponent( attributes->domain_parameters, |
Ronald Cron | 9e18fc1 | 2020-11-05 17:36:40 +0100 | [diff] [blame] | 316 | attributes->domain_parameters_size, |
| 317 | &exponent ); |
| 318 | if( status != PSA_SUCCESS ) |
| 319 | return( status ); |
| 320 | |
Ronald Cron | c1905a1 | 2021-06-05 11:11:14 +0200 | [diff] [blame] | 321 | mbedtls_rsa_init( &rsa ); |
Ronald Cron | 9e18fc1 | 2020-11-05 17:36:40 +0100 | [diff] [blame] | 322 | ret = mbedtls_rsa_gen_key( &rsa, |
| 323 | mbedtls_psa_get_random, |
| 324 | MBEDTLS_PSA_RANDOM_STATE, |
| 325 | (unsigned int)attributes->core.bits, |
| 326 | exponent ); |
| 327 | if( ret != 0 ) |
| 328 | return( mbedtls_to_psa_error( ret ) ); |
| 329 | |
| 330 | status = mbedtls_psa_rsa_export_key( attributes->core.type, |
| 331 | &rsa, key_buffer, key_buffer_size, |
| 332 | key_buffer_length ); |
| 333 | mbedtls_rsa_free( &rsa ); |
| 334 | |
| 335 | return( status ); |
| 336 | } |
Jaeden Amero | 424fa93 | 2021-05-14 08:34:32 +0100 | [diff] [blame] | 337 | #endif /* defined(BUILTIN_KEY_TYPE_RSA_KEY_PAIR) |
| 338 | * defined(MBEDTLS_GENPRIME) */ |
Ronald Cron | 9e18fc1 | 2020-11-05 17:36:40 +0100 | [diff] [blame] | 339 | |
Ronald Cron | 7bdbca3 | 2020-12-09 13:34:54 +0100 | [diff] [blame] | 340 | /****************************************************************/ |
| 341 | /* Sign/verify hashes */ |
| 342 | /****************************************************************/ |
| 343 | |
Ronald Cron | d2fb854 | 2020-12-09 15:18:01 +0100 | [diff] [blame] | 344 | #if defined(BUILTIN_ALG_RSA_PKCS1V15_SIGN) || defined(BUILTIN_ALG_RSA_PSS) |
Ronald Cron | 7bdbca3 | 2020-12-09 13:34:54 +0100 | [diff] [blame] | 345 | |
| 346 | /* Decode the hash algorithm from alg and store the mbedtls encoding in |
| 347 | * md_alg. Verify that the hash length is acceptable. */ |
| 348 | static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg, |
| 349 | size_t hash_length, |
| 350 | mbedtls_md_type_t *md_alg ) |
| 351 | { |
| 352 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
| 353 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg ); |
| 354 | *md_alg = mbedtls_md_get_type( md_info ); |
| 355 | |
| 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 | |
Ronald Cron | d2fb854 | 2020-12-09 15:18:01 +0100 | [diff] [blame] | 364 | #if defined(BUILTIN_ALG_RSA_PKCS1V15_SIGN) |
Ronald Cron | 7bdbca3 | 2020-12-09 13:34:54 +0100 | [diff] [blame] | 365 | /* For PKCS#1 v1.5 signature, if using a hash, the hash length |
| 366 | * must be correct. */ |
| 367 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) && |
| 368 | alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW ) |
| 369 | { |
| 370 | if( md_info == NULL ) |
| 371 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 372 | if( mbedtls_md_get_size( md_info ) != hash_length ) |
| 373 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 374 | } |
Ronald Cron | d2fb854 | 2020-12-09 15:18:01 +0100 | [diff] [blame] | 375 | #endif /* BUILTIN_ALG_RSA_PKCS1V15_SIGN */ |
Ronald Cron | 7bdbca3 | 2020-12-09 13:34:54 +0100 | [diff] [blame] | 376 | |
Ronald Cron | d2fb854 | 2020-12-09 15:18:01 +0100 | [diff] [blame] | 377 | #if defined(BUILTIN_ALG_RSA_PSS) |
Ronald Cron | 7bdbca3 | 2020-12-09 13:34:54 +0100 | [diff] [blame] | 378 | /* PSS requires a hash internally. */ |
| 379 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 380 | { |
| 381 | if( md_info == NULL ) |
| 382 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 383 | } |
Ronald Cron | d2fb854 | 2020-12-09 15:18:01 +0100 | [diff] [blame] | 384 | #endif /* BUILTIN_ALG_RSA_PSS */ |
Ronald Cron | 7bdbca3 | 2020-12-09 13:34:54 +0100 | [diff] [blame] | 385 | |
| 386 | return( PSA_SUCCESS ); |
| 387 | } |
| 388 | |
Ronald Cron | d2fb854 | 2020-12-09 15:18:01 +0100 | [diff] [blame] | 389 | static psa_status_t rsa_sign_hash( |
Ronald Cron | 7bdbca3 | 2020-12-09 13:34:54 +0100 | [diff] [blame] | 390 | const psa_key_attributes_t *attributes, |
| 391 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 392 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
| 393 | uint8_t *signature, size_t signature_size, size_t *signature_length ) |
| 394 | { |
| 395 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 396 | mbedtls_rsa_context *rsa = NULL; |
| 397 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 398 | mbedtls_md_type_t md_alg; |
| 399 | |
| 400 | status = mbedtls_psa_rsa_load_representation( attributes->core.type, |
| 401 | key_buffer, |
| 402 | key_buffer_size, |
| 403 | &rsa ); |
| 404 | if( status != PSA_SUCCESS ) |
| 405 | return( status ); |
| 406 | |
| 407 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 408 | if( status != PSA_SUCCESS ) |
| 409 | goto exit; |
| 410 | |
| 411 | if( signature_size < mbedtls_rsa_get_len( rsa ) ) |
| 412 | { |
| 413 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 414 | goto exit; |
| 415 | } |
| 416 | |
Ronald Cron | d2fb854 | 2020-12-09 15:18:01 +0100 | [diff] [blame] | 417 | #if defined(BUILTIN_ALG_RSA_PKCS1V15_SIGN) |
Ronald Cron | 7bdbca3 | 2020-12-09 13:34:54 +0100 | [diff] [blame] | 418 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 419 | { |
Ronald Cron | ea7631b | 2021-06-03 18:51:59 +0200 | [diff] [blame] | 420 | ret = mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 421 | MBEDTLS_MD_NONE ); |
| 422 | if( ret == 0 ) |
| 423 | { |
| 424 | ret = mbedtls_rsa_pkcs1_sign( rsa, |
| 425 | mbedtls_psa_get_random, |
| 426 | MBEDTLS_PSA_RANDOM_STATE, |
| 427 | md_alg, |
| 428 | (unsigned int) hash_length, |
| 429 | hash, |
| 430 | signature ); |
| 431 | } |
Ronald Cron | 7bdbca3 | 2020-12-09 13:34:54 +0100 | [diff] [blame] | 432 | } |
| 433 | else |
Ronald Cron | d2fb854 | 2020-12-09 15:18:01 +0100 | [diff] [blame] | 434 | #endif /* BUILTIN_ALG_RSA_PKCS1V15_SIGN */ |
| 435 | #if defined(BUILTIN_ALG_RSA_PSS) |
Ronald Cron | 7bdbca3 | 2020-12-09 13:34:54 +0100 | [diff] [blame] | 436 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 437 | { |
Ronald Cron | ea7631b | 2021-06-03 18:51:59 +0200 | [diff] [blame] | 438 | ret = mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 439 | |
| 440 | if( ret == 0 ) |
| 441 | { |
| 442 | ret = mbedtls_rsa_rsassa_pss_sign( rsa, |
| 443 | mbedtls_psa_get_random, |
| 444 | MBEDTLS_PSA_RANDOM_STATE, |
| 445 | MBEDTLS_MD_NONE, |
| 446 | (unsigned int) hash_length, |
| 447 | hash, |
| 448 | signature ); |
| 449 | } |
Ronald Cron | 7bdbca3 | 2020-12-09 13:34:54 +0100 | [diff] [blame] | 450 | } |
| 451 | else |
Ronald Cron | d2fb854 | 2020-12-09 15:18:01 +0100 | [diff] [blame] | 452 | #endif /* BUILTIN_ALG_RSA_PSS */ |
Ronald Cron | 7bdbca3 | 2020-12-09 13:34:54 +0100 | [diff] [blame] | 453 | { |
| 454 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 455 | goto exit; |
| 456 | } |
| 457 | |
| 458 | if( ret == 0 ) |
| 459 | *signature_length = mbedtls_rsa_get_len( rsa ); |
| 460 | status = mbedtls_to_psa_error( ret ); |
| 461 | |
| 462 | exit: |
| 463 | mbedtls_rsa_free( rsa ); |
| 464 | mbedtls_free( rsa ); |
| 465 | |
| 466 | return( status ); |
| 467 | } |
| 468 | |
Ronald Cron | d2fb854 | 2020-12-09 15:18:01 +0100 | [diff] [blame] | 469 | static psa_status_t rsa_verify_hash( |
Ronald Cron | 7bdbca3 | 2020-12-09 13:34:54 +0100 | [diff] [blame] | 470 | const psa_key_attributes_t *attributes, |
| 471 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 472 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
| 473 | const uint8_t *signature, size_t signature_length ) |
| 474 | { |
| 475 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 476 | mbedtls_rsa_context *rsa = NULL; |
| 477 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 478 | mbedtls_md_type_t md_alg; |
| 479 | |
| 480 | status = mbedtls_psa_rsa_load_representation( attributes->core.type, |
| 481 | key_buffer, |
| 482 | key_buffer_size, |
| 483 | &rsa ); |
| 484 | if( status != PSA_SUCCESS ) |
| 485 | goto exit; |
| 486 | |
| 487 | status = psa_rsa_decode_md_type( alg, hash_length, &md_alg ); |
| 488 | if( status != PSA_SUCCESS ) |
| 489 | goto exit; |
| 490 | |
| 491 | if( signature_length != mbedtls_rsa_get_len( rsa ) ) |
| 492 | { |
| 493 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 494 | goto exit; |
| 495 | } |
| 496 | |
Ronald Cron | d2fb854 | 2020-12-09 15:18:01 +0100 | [diff] [blame] | 497 | #if defined(BUILTIN_ALG_RSA_PKCS1V15_SIGN) |
Ronald Cron | 7bdbca3 | 2020-12-09 13:34:54 +0100 | [diff] [blame] | 498 | if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ) |
| 499 | { |
Ronald Cron | ea7631b | 2021-06-03 18:51:59 +0200 | [diff] [blame] | 500 | ret = mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15, |
| 501 | MBEDTLS_MD_NONE ); |
| 502 | if( ret == 0 ) |
| 503 | { |
| 504 | ret = mbedtls_rsa_pkcs1_verify( rsa, |
| 505 | md_alg, |
| 506 | (unsigned int) hash_length, |
| 507 | hash, |
| 508 | signature ); |
| 509 | } |
Ronald Cron | 7bdbca3 | 2020-12-09 13:34:54 +0100 | [diff] [blame] | 510 | } |
| 511 | else |
Ronald Cron | d2fb854 | 2020-12-09 15:18:01 +0100 | [diff] [blame] | 512 | #endif /* BUILTIN_ALG_RSA_PKCS1V15_SIGN */ |
| 513 | #if defined(BUILTIN_ALG_RSA_PSS) |
Ronald Cron | 7bdbca3 | 2020-12-09 13:34:54 +0100 | [diff] [blame] | 514 | if( PSA_ALG_IS_RSA_PSS( alg ) ) |
| 515 | { |
Ronald Cron | ea7631b | 2021-06-03 18:51:59 +0200 | [diff] [blame] | 516 | ret = mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ); |
| 517 | if( ret == 0 ) |
| 518 | { |
| 519 | ret = mbedtls_rsa_rsassa_pss_verify( rsa, |
| 520 | MBEDTLS_MD_NONE, |
| 521 | (unsigned int) hash_length, |
| 522 | hash, |
| 523 | signature ); |
| 524 | } |
Ronald Cron | 7bdbca3 | 2020-12-09 13:34:54 +0100 | [diff] [blame] | 525 | } |
| 526 | else |
Ronald Cron | d2fb854 | 2020-12-09 15:18:01 +0100 | [diff] [blame] | 527 | #endif /* BUILTIN_ALG_RSA_PSS */ |
Ronald Cron | 7bdbca3 | 2020-12-09 13:34:54 +0100 | [diff] [blame] | 528 | { |
| 529 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 530 | goto exit; |
| 531 | } |
| 532 | |
| 533 | /* Mbed TLS distinguishes "invalid padding" from "valid padding but |
| 534 | * the rest of the signature is invalid". This has little use in |
| 535 | * practice and PSA doesn't report this distinction. */ |
| 536 | status = ( ret == MBEDTLS_ERR_RSA_INVALID_PADDING ) ? |
| 537 | PSA_ERROR_INVALID_SIGNATURE : |
| 538 | mbedtls_to_psa_error( ret ); |
| 539 | |
| 540 | exit: |
| 541 | mbedtls_rsa_free( rsa ); |
| 542 | mbedtls_free( rsa ); |
| 543 | |
| 544 | return( status ); |
| 545 | } |
| 546 | |
Ronald Cron | d2fb854 | 2020-12-09 15:18:01 +0100 | [diff] [blame] | 547 | #endif /* defined(BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 548 | * defined(BUILTIN_ALG_RSA_PSS) */ |
Ronald Cron | 7bdbca3 | 2020-12-09 13:34:54 +0100 | [diff] [blame] | 549 | |
Ronald Cron | f1057d3 | 2020-11-26 19:19:10 +0100 | [diff] [blame] | 550 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 551 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
| 552 | |
Ronald Cron | 784fb32 | 2020-11-30 13:55:05 +0100 | [diff] [blame] | 553 | psa_status_t mbedtls_psa_rsa_import_key( |
| 554 | const psa_key_attributes_t *attributes, |
| 555 | const uint8_t *data, size_t data_length, |
| 556 | uint8_t *key_buffer, size_t key_buffer_size, |
| 557 | size_t *key_buffer_length, size_t *bits ) |
| 558 | { |
| 559 | return( rsa_import_key( attributes, data, data_length, |
| 560 | key_buffer, key_buffer_size, |
| 561 | key_buffer_length, bits ) ); |
| 562 | } |
| 563 | |
Ronald Cron | f1057d3 | 2020-11-26 19:19:10 +0100 | [diff] [blame] | 564 | psa_status_t mbedtls_psa_rsa_export_public_key( |
| 565 | const psa_key_attributes_t *attributes, |
| 566 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 567 | uint8_t *data, size_t data_size, size_t *data_length ) |
| 568 | { |
| 569 | return( rsa_export_public_key( attributes, key_buffer, key_buffer_size, |
| 570 | data, data_size, data_length ) ); |
| 571 | } |
| 572 | |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 573 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || |
| 574 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
| 575 | |
Jaeden Amero | 424fa93 | 2021-05-14 08:34:32 +0100 | [diff] [blame] | 576 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \ |
| 577 | defined(MBEDTLS_GENPRIME) |
Ronald Cron | 3a9c46b | 2020-11-06 09:38:35 +0100 | [diff] [blame] | 578 | psa_status_t mbedtls_psa_rsa_generate_key( |
| 579 | const psa_key_attributes_t *attributes, |
| 580 | uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length ) |
| 581 | { |
| 582 | return( rsa_generate_key( attributes, key_buffer, key_buffer_size, |
| 583 | key_buffer_length ) ); |
| 584 | } |
Jaeden Amero | 424fa93 | 2021-05-14 08:34:32 +0100 | [diff] [blame] | 585 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) |
| 586 | * defined(MBEDTLS_GENPRIME) */ |
Ronald Cron | 3a9c46b | 2020-11-06 09:38:35 +0100 | [diff] [blame] | 587 | |
Ronald Cron | d2fb854 | 2020-12-09 15:18:01 +0100 | [diff] [blame] | 588 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 589 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
| 590 | psa_status_t mbedtls_psa_rsa_sign_hash( |
| 591 | const psa_key_attributes_t *attributes, |
| 592 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 593 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
| 594 | uint8_t *signature, size_t signature_size, size_t *signature_length ) |
| 595 | { |
| 596 | return( rsa_sign_hash( |
| 597 | attributes, |
| 598 | key_buffer, key_buffer_size, |
| 599 | alg, hash, hash_length, |
| 600 | signature, signature_size, signature_length ) ); |
| 601 | } |
| 602 | |
| 603 | psa_status_t mbedtls_psa_rsa_verify_hash( |
| 604 | const psa_key_attributes_t *attributes, |
| 605 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 606 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
| 607 | const uint8_t *signature, size_t signature_length ) |
| 608 | { |
| 609 | return( rsa_verify_hash( |
| 610 | attributes, |
| 611 | key_buffer, key_buffer_size, |
| 612 | alg, hash, hash_length, |
| 613 | signature, signature_length ) ); |
| 614 | } |
| 615 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 616 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
| 617 | |
Ronald Cron | f1057d3 | 2020-11-26 19:19:10 +0100 | [diff] [blame] | 618 | /* |
| 619 | * BEYOND THIS POINT, TEST DRIVER ENTRY POINTS ONLY. |
| 620 | */ |
| 621 | |
| 622 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 623 | |
| 624 | #if defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_RSA_KEY_PAIR) || \ |
| 625 | defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_RSA_PUBLIC_KEY) |
Ronald Cron | 784fb32 | 2020-11-30 13:55:05 +0100 | [diff] [blame] | 626 | |
| 627 | psa_status_t mbedtls_transparent_test_driver_rsa_import_key( |
| 628 | const psa_key_attributes_t *attributes, |
| 629 | const uint8_t *data, size_t data_length, |
| 630 | uint8_t *key_buffer, size_t key_buffer_size, |
| 631 | size_t *key_buffer_length, size_t *bits ) |
| 632 | { |
| 633 | return( rsa_import_key( attributes, data, data_length, |
| 634 | key_buffer, key_buffer_size, |
| 635 | key_buffer_length, bits ) ); |
| 636 | } |
| 637 | |
Ronald Cron | f1057d3 | 2020-11-26 19:19:10 +0100 | [diff] [blame] | 638 | psa_status_t mbedtls_transparent_test_driver_rsa_export_public_key( |
| 639 | const psa_key_attributes_t *attributes, |
| 640 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 641 | uint8_t *data, size_t data_size, size_t *data_length ) |
| 642 | { |
| 643 | return( rsa_export_public_key( attributes, key_buffer, key_buffer_size, |
| 644 | data, data_size, data_length ) ); |
| 645 | } |
Ronald Cron | 784fb32 | 2020-11-30 13:55:05 +0100 | [diff] [blame] | 646 | |
Ronald Cron | f1057d3 | 2020-11-26 19:19:10 +0100 | [diff] [blame] | 647 | #endif /* defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_RSA_KEY_PAIR) || |
| 648 | defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_RSA_PUBLIC_KEY) */ |
| 649 | |
Ronald Cron | 3a9c46b | 2020-11-06 09:38:35 +0100 | [diff] [blame] | 650 | #if defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_RSA_KEY_PAIR) |
| 651 | psa_status_t mbedtls_transparent_test_driver_rsa_generate_key( |
| 652 | const psa_key_attributes_t *attributes, |
| 653 | uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length ) |
| 654 | { |
| 655 | return( rsa_generate_key( attributes, key_buffer, key_buffer_size, |
| 656 | key_buffer_length ) ); |
| 657 | } |
| 658 | #endif /* defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_RSA_KEY_PAIR) */ |
| 659 | |
Ronald Cron | d2fb854 | 2020-12-09 15:18:01 +0100 | [diff] [blame] | 660 | #if defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PKCS1V15_SIGN) || \ |
| 661 | defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PSS) |
| 662 | psa_status_t mbedtls_transparent_test_driver_rsa_sign_hash( |
| 663 | const psa_key_attributes_t *attributes, |
| 664 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 665 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
| 666 | uint8_t *signature, size_t signature_size, size_t *signature_length ) |
| 667 | { |
| 668 | #if defined(MBEDTLS_RSA_C) && \ |
| 669 | (defined(MBEDTLS_PKCS1_V15) || defined(MBEDTLS_PKCS1_V21)) |
| 670 | return( rsa_sign_hash( |
| 671 | attributes, |
| 672 | key_buffer, key_buffer_size, |
| 673 | alg, hash, hash_length, |
| 674 | signature, signature_size, signature_length ) ); |
| 675 | #else |
| 676 | (void)attributes; |
| 677 | (void)key_buffer; |
| 678 | (void)key_buffer_size; |
| 679 | (void)alg; |
| 680 | (void)hash; |
| 681 | (void)hash_length; |
| 682 | (void)signature; |
| 683 | (void)signature_size; |
| 684 | (void)signature_length; |
| 685 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 686 | #endif |
| 687 | } |
| 688 | |
| 689 | psa_status_t mbedtls_transparent_test_driver_rsa_verify_hash( |
| 690 | const psa_key_attributes_t *attributes, |
| 691 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 692 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
| 693 | const uint8_t *signature, size_t signature_length ) |
| 694 | { |
| 695 | #if defined(MBEDTLS_RSA_C) && \ |
| 696 | (defined(MBEDTLS_PKCS1_V15) || defined(MBEDTLS_PKCS1_V21)) |
| 697 | return( rsa_verify_hash( |
| 698 | attributes, |
| 699 | key_buffer, key_buffer_size, |
| 700 | alg, hash, hash_length, |
| 701 | signature, signature_length ) ); |
| 702 | #else |
| 703 | (void)attributes; |
| 704 | (void)key_buffer; |
| 705 | (void)key_buffer_size; |
| 706 | (void)alg; |
| 707 | (void)hash; |
| 708 | (void)hash_length; |
| 709 | (void)signature; |
| 710 | (void)signature_length; |
| 711 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 712 | #endif |
| 713 | } |
| 714 | #endif /* defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PKCS1V15_SIGN) || |
| 715 | * defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PSS) */ |
| 716 | |
Ronald Cron | f1057d3 | 2020-11-26 19:19:10 +0100 | [diff] [blame] | 717 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 718 | |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 719 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |