blob: bc5ed9c760cd8d4339421b1b5725db658a4dbfeb [file] [log] [blame]
Ronald Cron00b7bfc2020-11-25 15:25:26 +01001/*
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
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020025# include <psa/crypto.h>
26# include "psa_crypto_core.h"
27# include "psa_crypto_random_impl.h"
28# include "psa_crypto_rsa.h"
29# include "psa_crypto_hash.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010030
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020031# include <stdlib.h>
32# include <string.h>
33# include "mbedtls/platform.h"
34# if !defined(MBEDTLS_PLATFORM_C)
35# define mbedtls_calloc calloc
36# define mbedtls_free free
37# endif
Ronald Cron00b7bfc2020-11-25 15:25:26 +010038
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020039# include <mbedtls/rsa.h>
40# include <mbedtls/error.h>
41# include <mbedtls/pk.h>
42# include "pk_wrap.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010043
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020044# if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
45 (defined(PSA_CRYPTO_DRIVER_TEST) && \
46 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_RSA_KEY_PAIR)))
47# define BUILTIN_KEY_TYPE_RSA_KEY_PAIR 1
48# endif
Ronald Cronf1057d32020-11-26 19:19:10 +010049
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020050# if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) || \
51 (defined(PSA_CRYPTO_DRIVER_TEST) && \
52 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_RSA_PUBLIC_KEY)))
53# define BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY 1
54# endif
Ronald Cronf1057d32020-11-26 19:19:10 +010055
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020056# if (defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
57 (defined(PSA_CRYPTO_DRIVER_TEST) && \
58 defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PKCS1V15_SIGN) && \
59 defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V15)))
60# define BUILTIN_ALG_RSA_PKCS1V15_SIGN 1
61# endif
Ronald Crond2fb8542020-12-09 15:18:01 +010062
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020063# if (defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
64 (defined(PSA_CRYPTO_DRIVER_TEST) && \
65 defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PSS) && defined(MBEDTLS_RSA_C) && \
66 defined(MBEDTLS_PKCS1_V21)))
67# define BUILTIN_ALG_RSA_PSS 1
68# endif
Ronald Crond2fb8542020-12-09 15:18:01 +010069
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020070# if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
71 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
72 defined(BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
73 defined(BUILTIN_ALG_RSA_PSS) || \
74 defined(BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
75 defined(BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Cron00b7bfc2020-11-25 15:25:26 +010076
77/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
78 * that are not a multiple of 8) well. For example, there is only
79 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
80 * way to return the exact bit size of a key.
81 * To keep things simple, reject non-byte-aligned key sizes. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020082static psa_status_t
83psa_check_rsa_key_byte_aligned(const mbedtls_rsa_context *rsa)
Ronald Cron00b7bfc2020-11-25 15:25:26 +010084{
85 mbedtls_mpi n;
86 psa_status_t status;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020087 mbedtls_mpi_init(&n);
Ronald Cron00b7bfc2020-11-25 15:25:26 +010088 status = mbedtls_to_psa_error(
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020089 mbedtls_rsa_export(rsa, &n, NULL, NULL, NULL, NULL));
90 if (status == PSA_SUCCESS) {
91 if (mbedtls_mpi_bitlen(&n) % 8 != 0)
Ronald Cron00b7bfc2020-11-25 15:25:26 +010092 status = PSA_ERROR_NOT_SUPPORTED;
93 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020094 mbedtls_mpi_free(&n);
95 return status;
Ronald Cron00b7bfc2020-11-25 15:25:26 +010096}
97
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020098psa_status_t mbedtls_psa_rsa_load_representation(psa_key_type_t type,
99 const uint8_t *data,
100 size_t data_length,
101 mbedtls_rsa_context **p_rsa)
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100102{
103 psa_status_t status;
104 mbedtls_pk_context ctx;
105 size_t bits;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200106 mbedtls_pk_init(&ctx);
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100107
108 /* Parse the data. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200109 if (PSA_KEY_TYPE_IS_KEY_PAIR(type))
110 status = mbedtls_to_psa_error(mbedtls_pk_parse_key(
111 &ctx, data, data_length, NULL, 0, mbedtls_psa_get_random,
112 MBEDTLS_PSA_RANDOM_STATE));
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100113 else
114 status = mbedtls_to_psa_error(
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200115 mbedtls_pk_parse_public_key(&ctx, data, data_length));
116 if (status != PSA_SUCCESS)
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100117 goto exit;
118
119 /* We have something that the pkparse module recognizes. If it is a
120 * valid RSA key, store it. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200121 if (mbedtls_pk_get_type(&ctx) != MBEDTLS_PK_RSA) {
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100122 status = PSA_ERROR_INVALID_ARGUMENT;
123 goto exit;
124 }
125
126 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
127 * supports non-byte-aligned key sizes, but not well. For example,
128 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200129 bits = PSA_BYTES_TO_BITS(mbedtls_rsa_get_len(mbedtls_pk_rsa(ctx)));
130 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100131 status = PSA_ERROR_NOT_SUPPORTED;
132 goto exit;
133 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200134 status = psa_check_rsa_key_byte_aligned(mbedtls_pk_rsa(ctx));
135 if (status != PSA_SUCCESS)
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100136 goto exit;
137
138 /* Copy out the pointer to the RSA context, and reset the PK context
139 * such that pk_free doesn't free the RSA context we just grabbed. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200140 *p_rsa = mbedtls_pk_rsa(ctx);
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100141 ctx.pk_info = NULL;
142
143exit:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200144 mbedtls_pk_free(&ctx);
145 return status;
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100146}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200147# endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
148 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
149 * defined(BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
150 * defined(BUILTIN_ALG_RSA_PSS) || \
151 * defined(BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
152 * defined(BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100153
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200154# if defined(BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
155 defined(BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Cronabf2aef2020-11-27 18:13:44 +0100156
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200157static psa_status_t rsa_import_key(const psa_key_attributes_t *attributes,
158 const uint8_t *data,
159 size_t data_length,
160 uint8_t *key_buffer,
161 size_t key_buffer_size,
162 size_t *key_buffer_length,
163 size_t *bits)
Ronald Cronabf2aef2020-11-27 18:13:44 +0100164{
165 psa_status_t status;
166 mbedtls_rsa_context *rsa = NULL;
167
168 /* Parse input */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200169 status = mbedtls_psa_rsa_load_representation(attributes->core.type, data,
170 data_length, &rsa);
171 if (status != PSA_SUCCESS)
Ronald Cronabf2aef2020-11-27 18:13:44 +0100172 goto exit;
173
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200174 *bits = (psa_key_bits_t)PSA_BYTES_TO_BITS(mbedtls_rsa_get_len(rsa));
Ronald Cronabf2aef2020-11-27 18:13:44 +0100175
176 /* Re-export the data to PSA export format, such that we can store export
177 * representation in the key slot. Export representation in case of RSA is
178 * the smallest representation that's allowed as input, so a straight-up
179 * allocation of the same size as the input buffer will be large enough. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200180 status = mbedtls_psa_rsa_export_key(attributes->core.type, rsa, key_buffer,
181 key_buffer_size, key_buffer_length);
Ronald Cronabf2aef2020-11-27 18:13:44 +0100182exit:
183 /* Always free the RSA object */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200184 mbedtls_rsa_free(rsa);
185 mbedtls_free(rsa);
Ronald Cronabf2aef2020-11-27 18:13:44 +0100186
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200187 return status;
Ronald Cronabf2aef2020-11-27 18:13:44 +0100188}
189
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200190psa_status_t mbedtls_psa_rsa_export_key(psa_key_type_t type,
191 mbedtls_rsa_context *rsa,
192 uint8_t *data,
193 size_t data_size,
194 size_t *data_length)
Ronald Crone5ca3d82020-11-26 16:36:16 +0100195{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200196# if defined(MBEDTLS_PK_WRITE_C)
Ronald Crone5ca3d82020-11-26 16:36:16 +0100197 int ret;
198 mbedtls_pk_context pk;
199 uint8_t *pos = data + data_size;
200
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200201 mbedtls_pk_init(&pk);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100202 pk.pk_info = &mbedtls_rsa_info;
203 pk.pk_ctx = rsa;
204
205 /* PSA Crypto API defines the format of an RSA key as a DER-encoded
206 * representation of the non-encrypted PKCS#1 RSAPrivateKey for a
207 * private key and of the RFC3279 RSAPublicKey for a public key. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200208 if (PSA_KEY_TYPE_IS_KEY_PAIR(type))
209 ret = mbedtls_pk_write_key_der(&pk, data, data_size);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100210 else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200211 ret = mbedtls_pk_write_pubkey(&pos, data, &pk);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100212
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200213 if (ret < 0) {
Ronald Crone5ca3d82020-11-26 16:36:16 +0100214 /* Clean up in case pk_write failed halfway through. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200215 memset(data, 0, data_size);
216 return mbedtls_to_psa_error(ret);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100217 }
218
219 /* The mbedtls_pk_xxx functions write to the end of the buffer.
220 * Move the data to the beginning and erase remaining data
221 * at the original location. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200222 if (2 * (size_t)ret <= data_size) {
223 memcpy(data, data + data_size - ret, ret);
224 memset(data + data_size - ret, 0, ret);
225 } else if ((size_t)ret < data_size) {
226 memmove(data, data + data_size - ret, ret);
227 memset(data + ret, 0, data_size - ret);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100228 }
229
230 *data_length = ret;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200231 return PSA_SUCCESS;
232# else
233 (void)type;
234 (void)rsa;
235 (void)data;
236 (void)data_size;
237 (void)data_length;
238 return PSA_ERROR_NOT_SUPPORTED;
239# endif /* MBEDTLS_PK_WRITE_C */
Ronald Crone5ca3d82020-11-26 16:36:16 +0100240}
241
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200242static psa_status_t
243rsa_export_public_key(const psa_key_attributes_t *attributes,
244 const uint8_t *key_buffer,
245 size_t key_buffer_size,
246 uint8_t *data,
247 size_t data_size,
248 size_t *data_length)
Ronald Crone5ca3d82020-11-26 16:36:16 +0100249{
250 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
251 mbedtls_rsa_context *rsa = NULL;
252
253 status = mbedtls_psa_rsa_load_representation(
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200254 attributes->core.type, key_buffer, key_buffer_size, &rsa);
255 if (status != PSA_SUCCESS)
256 return status;
Ronald Crone5ca3d82020-11-26 16:36:16 +0100257
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200258 status = mbedtls_psa_rsa_export_key(PSA_KEY_TYPE_RSA_PUBLIC_KEY, rsa, data,
259 data_size, data_length);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100260
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200261 mbedtls_rsa_free(rsa);
262 mbedtls_free(rsa);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100263
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200264 return status;
Ronald Crone5ca3d82020-11-26 16:36:16 +0100265}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200266# endif /* defined(BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
267 * defined(BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cronf1057d32020-11-26 19:19:10 +0100268
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200269# if defined(BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && defined(MBEDTLS_GENPRIME)
270static psa_status_t psa_rsa_read_exponent(const uint8_t *domain_parameters,
271 size_t domain_parameters_size,
272 int *exponent)
Ronald Cron9e18fc12020-11-05 17:36:40 +0100273{
274 size_t i;
275 uint32_t acc = 0;
276
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200277 if (domain_parameters_size == 0) {
Ronald Cron9e18fc12020-11-05 17:36:40 +0100278 *exponent = 65537;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200279 return PSA_SUCCESS;
Ronald Cron9e18fc12020-11-05 17:36:40 +0100280 }
281
282 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
283 * support values that fit in a 32-bit integer, which is larger than
284 * int on just about every platform anyway. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200285 if (domain_parameters_size > sizeof(acc))
286 return PSA_ERROR_NOT_SUPPORTED;
287 for (i = 0; i < domain_parameters_size; i++)
288 acc = (acc << 8) | domain_parameters[i];
289 if (acc > INT_MAX)
290 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9e18fc12020-11-05 17:36:40 +0100291 *exponent = acc;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200292 return PSA_SUCCESS;
Ronald Cron9e18fc12020-11-05 17:36:40 +0100293}
294
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200295static psa_status_t rsa_generate_key(const psa_key_attributes_t *attributes,
296 uint8_t *key_buffer,
297 size_t key_buffer_size,
298 size_t *key_buffer_length)
Ronald Cron9e18fc12020-11-05 17:36:40 +0100299{
300 psa_status_t status;
301 mbedtls_rsa_context rsa;
302 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
303 int exponent;
304
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200305 status = psa_rsa_read_exponent(attributes->domain_parameters,
306 attributes->domain_parameters_size,
307 &exponent);
308 if (status != PSA_SUCCESS)
309 return status;
Ronald Cron9e18fc12020-11-05 17:36:40 +0100310
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200311 mbedtls_rsa_init(&rsa);
312 ret = mbedtls_rsa_gen_key(&rsa, mbedtls_psa_get_random,
313 MBEDTLS_PSA_RANDOM_STATE,
314 (unsigned int)attributes->core.bits, exponent);
315 if (ret != 0)
316 return mbedtls_to_psa_error(ret);
Ronald Cron9e18fc12020-11-05 17:36:40 +0100317
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200318 status = mbedtls_psa_rsa_export_key(attributes->core.type, &rsa, key_buffer,
319 key_buffer_size, key_buffer_length);
320 mbedtls_rsa_free(&rsa);
Ronald Cron9e18fc12020-11-05 17:36:40 +0100321
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200322 return status;
Ronald Cron9e18fc12020-11-05 17:36:40 +0100323}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200324# endif /* defined(BUILTIN_KEY_TYPE_RSA_KEY_PAIR) \
325 * defined(MBEDTLS_GENPRIME) */
Ronald Cron9e18fc12020-11-05 17:36:40 +0100326
Ronald Cron7bdbca32020-12-09 13:34:54 +0100327/****************************************************************/
328/* Sign/verify hashes */
329/****************************************************************/
330
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200331# if defined(BUILTIN_ALG_RSA_PKCS1V15_SIGN) || defined(BUILTIN_ALG_RSA_PSS)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100332
333/* Decode the hash algorithm from alg and store the mbedtls encoding in
334 * md_alg. Verify that the hash length is acceptable. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200335static psa_status_t psa_rsa_decode_md_type(psa_algorithm_t alg,
336 size_t hash_length,
337 mbedtls_md_type_t *md_alg)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100338{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200339 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
340 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa(hash_alg);
341 *md_alg = mbedtls_md_get_type(md_info);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100342
343 /* The Mbed TLS RSA module uses an unsigned int for hash length
344 * parameters. Validate that it fits so that we don't risk an
345 * overflow later. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200346# if SIZE_MAX > UINT_MAX
347 if (hash_length > UINT_MAX)
348 return PSA_ERROR_INVALID_ARGUMENT;
349# endif
Ronald Cron7bdbca32020-12-09 13:34:54 +0100350
Janos Follath0af093b2021-06-07 14:34:10 +0100351 /* For signatures using a hash, the hash length must be correct. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200352 if (alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW) {
353 if (md_info == NULL)
354 return PSA_ERROR_NOT_SUPPORTED;
355 if (mbedtls_md_get_size(md_info) != hash_length)
356 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron7bdbca32020-12-09 13:34:54 +0100357 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100358
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200359 return PSA_SUCCESS;
Ronald Cron7bdbca32020-12-09 13:34:54 +0100360}
361
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200362static psa_status_t rsa_sign_hash(const psa_key_attributes_t *attributes,
363 const uint8_t *key_buffer,
364 size_t key_buffer_size,
365 psa_algorithm_t alg,
366 const uint8_t *hash,
367 size_t hash_length,
368 uint8_t *signature,
369 size_t signature_size,
370 size_t *signature_length)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100371{
372 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
373 mbedtls_rsa_context *rsa = NULL;
374 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
375 mbedtls_md_type_t md_alg;
376
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200377 status = mbedtls_psa_rsa_load_representation(
378 attributes->core.type, key_buffer, key_buffer_size, &rsa);
379 if (status != PSA_SUCCESS)
380 return status;
Ronald Cron7bdbca32020-12-09 13:34:54 +0100381
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200382 status = psa_rsa_decode_md_type(alg, hash_length, &md_alg);
383 if (status != PSA_SUCCESS)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100384 goto exit;
385
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200386 if (signature_size < mbedtls_rsa_get_len(rsa)) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100387 status = PSA_ERROR_BUFFER_TOO_SMALL;
388 goto exit;
389 }
390
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200391# if defined(BUILTIN_ALG_RSA_PKCS1V15_SIGN)
392 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg)) {
393 ret =
394 mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE);
395 if (ret == 0) {
396 ret = mbedtls_rsa_pkcs1_sign(rsa, mbedtls_psa_get_random,
397 MBEDTLS_PSA_RANDOM_STATE, md_alg,
398 (unsigned int)hash_length, hash,
399 signature);
Ronald Cronea7631b2021-06-03 18:51:59 +0200400 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200401 } else
402# endif /* BUILTIN_ALG_RSA_PKCS1V15_SIGN */
403# if defined(BUILTIN_ALG_RSA_PSS)
404 if (PSA_ALG_IS_RSA_PSS(alg)) {
405 ret = mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
Ronald Cronea7631b2021-06-03 18:51:59 +0200406
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200407 if (ret == 0) {
408 ret = mbedtls_rsa_rsassa_pss_sign(
409 rsa, mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE,
410 MBEDTLS_MD_NONE, (unsigned int)hash_length, hash, signature);
Ronald Cronea7631b2021-06-03 18:51:59 +0200411 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200412 } else
413# endif /* BUILTIN_ALG_RSA_PSS */
Ronald Cron7bdbca32020-12-09 13:34:54 +0100414 {
415 status = PSA_ERROR_INVALID_ARGUMENT;
416 goto exit;
417 }
418
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200419 if (ret == 0)
420 *signature_length = mbedtls_rsa_get_len(rsa);
421 status = mbedtls_to_psa_error(ret);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100422
423exit:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200424 mbedtls_rsa_free(rsa);
425 mbedtls_free(rsa);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100426
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200427 return status;
Ronald Cron7bdbca32020-12-09 13:34:54 +0100428}
429
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200430static psa_status_t rsa_verify_hash(const psa_key_attributes_t *attributes,
431 const uint8_t *key_buffer,
432 size_t key_buffer_size,
433 psa_algorithm_t alg,
434 const uint8_t *hash,
435 size_t hash_length,
436 const uint8_t *signature,
437 size_t signature_length)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100438{
439 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
440 mbedtls_rsa_context *rsa = NULL;
441 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
442 mbedtls_md_type_t md_alg;
443
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200444 status = mbedtls_psa_rsa_load_representation(
445 attributes->core.type, key_buffer, key_buffer_size, &rsa);
446 if (status != PSA_SUCCESS)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100447 goto exit;
448
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200449 status = psa_rsa_decode_md_type(alg, hash_length, &md_alg);
450 if (status != PSA_SUCCESS)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100451 goto exit;
452
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200453 if (signature_length != mbedtls_rsa_get_len(rsa)) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100454 status = PSA_ERROR_INVALID_SIGNATURE;
455 goto exit;
456 }
457
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200458# if defined(BUILTIN_ALG_RSA_PKCS1V15_SIGN)
459 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg)) {
460 ret =
461 mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE);
462 if (ret == 0) {
463 ret = mbedtls_rsa_pkcs1_verify(
464 rsa, md_alg, (unsigned int)hash_length, hash, signature);
Ronald Cronea7631b2021-06-03 18:51:59 +0200465 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200466 } else
467# endif /* BUILTIN_ALG_RSA_PKCS1V15_SIGN */
468# if defined(BUILTIN_ALG_RSA_PSS)
469 if (PSA_ALG_IS_RSA_PSS(alg)) {
470 ret = mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
471 if (ret == 0) {
472 ret = mbedtls_rsa_rsassa_pss_verify(
473 rsa, md_alg, (unsigned int)hash_length, hash, signature);
Ronald Cronea7631b2021-06-03 18:51:59 +0200474 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200475 } else
476# endif /* BUILTIN_ALG_RSA_PSS */
Ronald Cron7bdbca32020-12-09 13:34:54 +0100477 {
478 status = PSA_ERROR_INVALID_ARGUMENT;
479 goto exit;
480 }
481
482 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
483 * the rest of the signature is invalid". This has little use in
484 * practice and PSA doesn't report this distinction. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200485 status = (ret == MBEDTLS_ERR_RSA_INVALID_PADDING) ?
486 PSA_ERROR_INVALID_SIGNATURE :
487 mbedtls_to_psa_error(ret);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100488
489exit:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200490 mbedtls_rsa_free(rsa);
491 mbedtls_free(rsa);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100492
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200493 return status;
Ronald Cron7bdbca32020-12-09 13:34:54 +0100494}
495
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200496# endif /* defined(BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
497 * defined(BUILTIN_ALG_RSA_PSS) */
Ronald Cron7bdbca32020-12-09 13:34:54 +0100498
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200499# if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
500 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Cronf1057d32020-11-26 19:19:10 +0100501
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200502psa_status_t mbedtls_psa_rsa_import_key(const psa_key_attributes_t *attributes,
503 const uint8_t *data,
504 size_t data_length,
505 uint8_t *key_buffer,
506 size_t key_buffer_size,
507 size_t *key_buffer_length,
508 size_t *bits)
Ronald Cron784fb322020-11-30 13:55:05 +0100509{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200510 return (rsa_import_key(attributes, data, data_length, key_buffer,
511 key_buffer_size, key_buffer_length, bits));
Ronald Cron784fb322020-11-30 13:55:05 +0100512}
513
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200514psa_status_t
515mbedtls_psa_rsa_export_public_key(const psa_key_attributes_t *attributes,
516 const uint8_t *key_buffer,
517 size_t key_buffer_size,
518 uint8_t *data,
519 size_t data_size,
520 size_t *data_length)
Ronald Cronf1057d32020-11-26 19:19:10 +0100521{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200522 return (rsa_export_public_key(attributes, key_buffer, key_buffer_size, data,
523 data_size, data_length));
Ronald Cronf1057d32020-11-26 19:19:10 +0100524}
525
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200526# endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
527 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Crone5ca3d82020-11-26 16:36:16 +0100528
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200529# if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
530 defined(MBEDTLS_GENPRIME)
531psa_status_t
532mbedtls_psa_rsa_generate_key(const psa_key_attributes_t *attributes,
533 uint8_t *key_buffer,
534 size_t key_buffer_size,
535 size_t *key_buffer_length)
Ronald Cron3a9c46b2020-11-06 09:38:35 +0100536{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200537 return (rsa_generate_key(attributes, key_buffer, key_buffer_size,
538 key_buffer_length));
Ronald Cron3a9c46b2020-11-06 09:38:35 +0100539}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200540# endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) \
541 * defined(MBEDTLS_GENPRIME) */
Ronald Cron3a9c46b2020-11-06 09:38:35 +0100542
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200543# if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
544 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
545psa_status_t mbedtls_psa_rsa_sign_hash(const psa_key_attributes_t *attributes,
546 const uint8_t *key_buffer,
547 size_t key_buffer_size,
548 psa_algorithm_t alg,
549 const uint8_t *hash,
550 size_t hash_length,
551 uint8_t *signature,
552 size_t signature_size,
553 size_t *signature_length)
Ronald Crond2fb8542020-12-09 15:18:01 +0100554{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200555 return (rsa_sign_hash(attributes, key_buffer, key_buffer_size, alg, hash,
556 hash_length, signature, signature_size,
557 signature_length));
Ronald Crond2fb8542020-12-09 15:18:01 +0100558}
559
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200560psa_status_t mbedtls_psa_rsa_verify_hash(const psa_key_attributes_t *attributes,
561 const uint8_t *key_buffer,
562 size_t key_buffer_size,
563 psa_algorithm_t alg,
564 const uint8_t *hash,
565 size_t hash_length,
566 const uint8_t *signature,
567 size_t signature_length)
Ronald Crond2fb8542020-12-09 15:18:01 +0100568{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200569 return (rsa_verify_hash(attributes, key_buffer, key_buffer_size, alg, hash,
570 hash_length, signature, signature_length));
Ronald Crond2fb8542020-12-09 15:18:01 +0100571}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200572# endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
573 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Ronald Crond2fb8542020-12-09 15:18:01 +0100574
Ronald Cronf1057d32020-11-26 19:19:10 +0100575/*
576 * BEYOND THIS POINT, TEST DRIVER ENTRY POINTS ONLY.
577 */
578
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200579# if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronf1057d32020-11-26 19:19:10 +0100580
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200581# if defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_RSA_KEY_PAIR) || \
582 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Cron784fb322020-11-30 13:55:05 +0100583
584psa_status_t mbedtls_transparent_test_driver_rsa_import_key(
585 const psa_key_attributes_t *attributes,
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200586 const uint8_t *data,
587 size_t data_length,
588 uint8_t *key_buffer,
589 size_t key_buffer_size,
590 size_t *key_buffer_length,
591 size_t *bits)
Ronald Cron784fb322020-11-30 13:55:05 +0100592{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200593 return (rsa_import_key(attributes, data, data_length, key_buffer,
594 key_buffer_size, key_buffer_length, bits));
Ronald Cron784fb322020-11-30 13:55:05 +0100595}
596
Ronald Cronf1057d32020-11-26 19:19:10 +0100597psa_status_t mbedtls_transparent_test_driver_rsa_export_public_key(
598 const psa_key_attributes_t *attributes,
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200599 const uint8_t *key_buffer,
600 size_t key_buffer_size,
601 uint8_t *data,
602 size_t data_size,
603 size_t *data_length)
Ronald Cronf1057d32020-11-26 19:19:10 +0100604{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200605 return (rsa_export_public_key(attributes, key_buffer, key_buffer_size, data,
606 data_size, data_length));
Ronald Cronf1057d32020-11-26 19:19:10 +0100607}
Ronald Cron784fb322020-11-30 13:55:05 +0100608
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200609# endif /* defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_RSA_KEY_PAIR) || \
610 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cronf1057d32020-11-26 19:19:10 +0100611
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200612# if defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_RSA_KEY_PAIR)
Ronald Cron3a9c46b2020-11-06 09:38:35 +0100613psa_status_t mbedtls_transparent_test_driver_rsa_generate_key(
614 const psa_key_attributes_t *attributes,
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200615 uint8_t *key_buffer,
616 size_t key_buffer_size,
617 size_t *key_buffer_length)
Ronald Cron3a9c46b2020-11-06 09:38:35 +0100618{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200619 return (rsa_generate_key(attributes, key_buffer, key_buffer_size,
620 key_buffer_length));
Ronald Cron3a9c46b2020-11-06 09:38:35 +0100621}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200622# endif /* defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron3a9c46b2020-11-06 09:38:35 +0100623
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200624# if defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PKCS1V15_SIGN) || \
625 defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PSS)
Ronald Crond2fb8542020-12-09 15:18:01 +0100626psa_status_t mbedtls_transparent_test_driver_rsa_sign_hash(
627 const psa_key_attributes_t *attributes,
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200628 const uint8_t *key_buffer,
629 size_t key_buffer_size,
630 psa_algorithm_t alg,
631 const uint8_t *hash,
632 size_t hash_length,
633 uint8_t *signature,
634 size_t signature_size,
635 size_t *signature_length)
Ronald Crond2fb8542020-12-09 15:18:01 +0100636{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200637# if defined(MBEDTLS_RSA_C) && \
638 (defined(MBEDTLS_PKCS1_V15) || defined(MBEDTLS_PKCS1_V21))
639 return (rsa_sign_hash(attributes, key_buffer, key_buffer_size, alg, hash,
640 hash_length, signature, signature_size,
641 signature_length));
642# else
Ronald Crond2fb8542020-12-09 15:18:01 +0100643 (void)attributes;
644 (void)key_buffer;
645 (void)key_buffer_size;
646 (void)alg;
647 (void)hash;
648 (void)hash_length;
649 (void)signature;
650 (void)signature_size;
651 (void)signature_length;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200652 return PSA_ERROR_NOT_SUPPORTED;
653# endif
Ronald Crond2fb8542020-12-09 15:18:01 +0100654}
655
656psa_status_t mbedtls_transparent_test_driver_rsa_verify_hash(
657 const psa_key_attributes_t *attributes,
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200658 const uint8_t *key_buffer,
659 size_t key_buffer_size,
660 psa_algorithm_t alg,
661 const uint8_t *hash,
662 size_t hash_length,
663 const uint8_t *signature,
664 size_t signature_length)
Ronald Crond2fb8542020-12-09 15:18:01 +0100665{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200666# if defined(MBEDTLS_RSA_C) && \
667 (defined(MBEDTLS_PKCS1_V15) || defined(MBEDTLS_PKCS1_V21))
668 return (rsa_verify_hash(attributes, key_buffer, key_buffer_size, alg, hash,
669 hash_length, signature, signature_length));
670# else
Ronald Crond2fb8542020-12-09 15:18:01 +0100671 (void)attributes;
672 (void)key_buffer;
673 (void)key_buffer_size;
674 (void)alg;
675 (void)hash;
676 (void)hash_length;
677 (void)signature;
678 (void)signature_length;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200679 return PSA_ERROR_NOT_SUPPORTED;
680# endif
Ronald Crond2fb8542020-12-09 15:18:01 +0100681}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200682# endif /* defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PKCS1V15_SIGN) || \
683 * defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PSS) */
Ronald Crond2fb8542020-12-09 15:18:01 +0100684
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200685# endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronf1057d32020-11-26 19:19:10 +0100686
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100687#endif /* MBEDTLS_PSA_CRYPTO_C */