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