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