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