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