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