2020-11-25 15:25:26 +01:00
|
|
|
/*
|
|
|
|
* PSA RSA layer on top of Mbed TLS crypto
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* Copyright The Mbed TLS Contributors
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_C)
|
|
|
|
|
|
|
|
#include <psa/crypto.h>
|
2021-12-09 11:13:58 +01:00
|
|
|
#include "psa/crypto_values.h"
|
2020-11-25 15:25:26 +01:00
|
|
|
#include "psa_crypto_core.h"
|
2020-11-05 17:36:40 +01:00
|
|
|
#include "psa_crypto_random_impl.h"
|
2020-11-25 15:25:26 +01:00
|
|
|
#include "psa_crypto_rsa.h"
|
2021-03-15 11:07:12 +01:00
|
|
|
#include "psa_crypto_hash.h"
|
2023-03-28 11:38:08 +02:00
|
|
|
#include "md_psa.h"
|
2020-11-25 15:25:26 +01:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "mbedtls/platform.h"
|
|
|
|
|
|
|
|
#include <mbedtls/rsa.h>
|
|
|
|
#include <mbedtls/error.h>
|
|
|
|
#include <mbedtls/pk.h>
|
2021-04-06 18:58:16 +02:00
|
|
|
#include "pk_wrap.h"
|
2020-11-25 15:25:26 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
|
2021-03-13 18:50:11 +01:00
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
|
2023-07-10 11:24:00 +02:00
|
|
|
defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) || \
|
2023-07-25 12:27:19 +02:00
|
|
|
defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
|
2021-03-13 18:50:11 +01:00
|
|
|
defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
|
2020-11-25 15:25:26 +01:00
|
|
|
|
|
|
|
/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
|
|
|
|
* that are not a multiple of 8) well. For example, there is only
|
|
|
|
* mbedtls_rsa_get_len(), which returns a number of bytes, and no
|
|
|
|
* way to return the exact bit size of a key.
|
|
|
|
* To keep things simple, reject non-byte-aligned key sizes. */
|
|
|
|
static psa_status_t psa_check_rsa_key_byte_aligned(
|
2023-01-11 14:50:10 +01:00
|
|
|
const mbedtls_rsa_context *rsa)
|
2020-11-25 15:25:26 +01:00
|
|
|
{
|
|
|
|
mbedtls_mpi n;
|
|
|
|
psa_status_t status;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&n);
|
2020-11-25 15:25:26 +01:00
|
|
|
status = mbedtls_to_psa_error(
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_rsa_export(rsa, &n, NULL, NULL, NULL, NULL));
|
|
|
|
if (status == PSA_SUCCESS) {
|
|
|
|
if (mbedtls_mpi_bitlen(&n) % 8 != 0) {
|
2020-11-25 15:25:26 +01:00
|
|
|
status = PSA_ERROR_NOT_SUPPORTED;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-11-25 15:25:26 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&n);
|
|
|
|
return status;
|
2020-11-25 15:25:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
psa_status_t mbedtls_psa_rsa_load_representation(
|
|
|
|
psa_key_type_t type, const uint8_t *data, size_t data_length,
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_rsa_context **p_rsa)
|
2020-11-25 15:25:26 +01:00
|
|
|
{
|
|
|
|
psa_status_t status;
|
|
|
|
mbedtls_pk_context ctx;
|
|
|
|
size_t bits;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pk_init(&ctx);
|
2020-11-25 15:25:26 +01:00
|
|
|
|
|
|
|
/* Parse the data. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
|
2020-11-25 15:25:26 +01:00
|
|
|
status = mbedtls_to_psa_error(
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pk_parse_key(&ctx, data, data_length, NULL, 0,
|
|
|
|
mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE));
|
|
|
|
} else {
|
2020-11-25 15:25:26 +01:00
|
|
|
status = mbedtls_to_psa_error(
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pk_parse_public_key(&ctx, data, data_length));
|
|
|
|
}
|
|
|
|
if (status != PSA_SUCCESS) {
|
2020-11-25 15:25:26 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-11-25 15:25:26 +01:00
|
|
|
|
|
|
|
/* We have something that the pkparse module recognizes. If it is a
|
|
|
|
* valid RSA key, store it. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_pk_get_type(&ctx) != MBEDTLS_PK_RSA) {
|
2020-11-25 15:25:26 +01:00
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
|
|
|
|
* supports non-byte-aligned key sizes, but not well. For example,
|
|
|
|
* mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
|
2023-01-11 14:50:10 +01:00
|
|
|
bits = PSA_BYTES_TO_BITS(mbedtls_rsa_get_len(mbedtls_pk_rsa(ctx)));
|
|
|
|
if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
|
2020-11-25 15:25:26 +01:00
|
|
|
status = PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
goto exit;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_check_rsa_key_byte_aligned(mbedtls_pk_rsa(ctx));
|
|
|
|
if (status != PSA_SUCCESS) {
|
2020-11-25 15:25:26 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-11-25 15:25:26 +01:00
|
|
|
|
|
|
|
/* Copy out the pointer to the RSA context, and reset the PK context
|
|
|
|
* such that pk_free doesn't free the RSA context we just grabbed. */
|
2023-01-11 14:50:10 +01:00
|
|
|
*p_rsa = mbedtls_pk_rsa(ctx);
|
2020-11-25 15:25:26 +01:00
|
|
|
ctx.pk_info = NULL;
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pk_free(&ctx);
|
|
|
|
return status;
|
2020-11-25 15:25:26 +01:00
|
|
|
}
|
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
|
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
|
2021-03-13 18:50:11 +01:00
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
|
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
|
2023-07-10 11:24:00 +02:00
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) ||
|
2023-07-25 12:27:19 +02:00
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
|
2021-03-13 18:50:11 +01:00
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
|
2020-11-25 15:25:26 +01:00
|
|
|
|
2023-07-25 12:27:19 +02:00
|
|
|
#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
|
2021-03-13 18:50:11 +01:00
|
|
|
defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
|
|
|
|
psa_status_t mbedtls_psa_rsa_import_key(
|
2020-11-27 18:13:44 +01:00
|
|
|
const psa_key_attributes_t *attributes,
|
|
|
|
const uint8_t *data, size_t data_length,
|
|
|
|
uint8_t *key_buffer, size_t key_buffer_size,
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t *key_buffer_length, size_t *bits)
|
2020-11-27 18:13:44 +01:00
|
|
|
{
|
|
|
|
psa_status_t status;
|
|
|
|
mbedtls_rsa_context *rsa = NULL;
|
|
|
|
|
|
|
|
/* Parse input */
|
2023-01-11 14:50:10 +01:00
|
|
|
status = mbedtls_psa_rsa_load_representation(attributes->core.type,
|
|
|
|
data,
|
|
|
|
data_length,
|
|
|
|
&rsa);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2020-11-27 18:13:44 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-11-27 18:13:44 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
*bits = (psa_key_bits_t) PSA_BYTES_TO_BITS(mbedtls_rsa_get_len(rsa));
|
2020-11-27 18:13:44 +01:00
|
|
|
|
|
|
|
/* Re-export the data to PSA export format, such that we can store export
|
|
|
|
* representation in the key slot. Export representation in case of RSA is
|
|
|
|
* the smallest representation that's allowed as input, so a straight-up
|
|
|
|
* allocation of the same size as the input buffer will be large enough. */
|
2023-01-11 14:50:10 +01:00
|
|
|
status = mbedtls_psa_rsa_export_key(attributes->core.type,
|
|
|
|
rsa,
|
|
|
|
key_buffer,
|
|
|
|
key_buffer_size,
|
|
|
|
key_buffer_length);
|
2020-11-27 18:13:44 +01:00
|
|
|
exit:
|
|
|
|
/* Always free the RSA object */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_rsa_free(rsa);
|
|
|
|
mbedtls_free(rsa);
|
2020-11-27 18:13:44 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return status;
|
2020-11-27 18:13:44 +01:00
|
|
|
}
|
2023-07-25 12:27:19 +02:00
|
|
|
#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
|
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
|
2023-07-10 11:24:00 +02:00
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
|
2020-11-27 18:13:44 +01:00
|
|
|
|
2023-07-10 11:24:00 +02:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_status_t mbedtls_psa_rsa_export_key(psa_key_type_t type,
|
|
|
|
mbedtls_rsa_context *rsa,
|
|
|
|
uint8_t *data,
|
|
|
|
size_t data_size,
|
|
|
|
size_t *data_length)
|
2020-11-26 16:36:16 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
mbedtls_pk_context pk;
|
|
|
|
uint8_t *pos = data + data_size;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pk_init(&pk);
|
2020-11-26 16:36:16 +01:00
|
|
|
pk.pk_info = &mbedtls_rsa_info;
|
|
|
|
pk.pk_ctx = rsa;
|
|
|
|
|
|
|
|
/* PSA Crypto API defines the format of an RSA key as a DER-encoded
|
|
|
|
* representation of the non-encrypted PKCS#1 RSAPrivateKey for a
|
|
|
|
* private key and of the RFC3279 RSAPublicKey for a public key. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
|
|
|
|
ret = mbedtls_pk_write_key_der(&pk, data, data_size);
|
|
|
|
} else {
|
|
|
|
ret = mbedtls_pk_write_pubkey(&pos, data, &pk);
|
|
|
|
}
|
2020-11-26 16:36:16 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret < 0) {
|
2020-11-26 16:36:16 +01:00
|
|
|
/* Clean up in case pk_write failed halfway through. */
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(data, 0, data_size);
|
|
|
|
return mbedtls_to_psa_error(ret);
|
2020-11-26 16:36:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* The mbedtls_pk_xxx functions write to the end of the buffer.
|
|
|
|
* Move the data to the beginning and erase remaining data
|
|
|
|
* at the original location. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (2 * (size_t) ret <= data_size) {
|
|
|
|
memcpy(data, data + data_size - ret, ret);
|
|
|
|
memset(data + data_size - ret, 0, ret);
|
|
|
|
} else if ((size_t) ret < data_size) {
|
|
|
|
memmove(data, data + data_size - ret, ret);
|
|
|
|
memset(data + ret, 0, data_size - ret);
|
2020-11-26 16:36:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
*data_length = ret;
|
2023-01-11 14:50:10 +01:00
|
|
|
return PSA_SUCCESS;
|
2020-11-26 16:36:16 +01:00
|
|
|
}
|
|
|
|
|
2021-03-13 18:50:11 +01:00
|
|
|
psa_status_t mbedtls_psa_rsa_export_public_key(
|
2020-11-26 16:36:16 +01:00
|
|
|
const psa_key_attributes_t *attributes,
|
|
|
|
const uint8_t *key_buffer, size_t key_buffer_size,
|
2023-01-11 14:50:10 +01:00
|
|
|
uint8_t *data, size_t data_size, size_t *data_length)
|
2020-11-26 16:36:16 +01:00
|
|
|
{
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
mbedtls_rsa_context *rsa = NULL;
|
|
|
|
|
|
|
|
status = mbedtls_psa_rsa_load_representation(
|
2023-01-11 14:50:10 +01:00
|
|
|
attributes->core.type, key_buffer, key_buffer_size, &rsa);
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
return status;
|
|
|
|
}
|
2020-11-26 16:36:16 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = mbedtls_psa_rsa_export_key(PSA_KEY_TYPE_RSA_PUBLIC_KEY,
|
|
|
|
rsa,
|
|
|
|
data,
|
|
|
|
data_size,
|
|
|
|
data_length);
|
2020-11-26 16:36:16 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_rsa_free(rsa);
|
|
|
|
mbedtls_free(rsa);
|
2020-11-26 16:36:16 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return status;
|
2020-11-26 16:36:16 +01:00
|
|
|
}
|
2023-07-10 11:24:00 +02:00
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
|
2021-03-13 18:50:11 +01:00
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
|
2020-11-26 19:19:10 +01:00
|
|
|
|
2023-07-11 14:11:28 +02:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
|
2023-01-11 14:50:10 +01:00
|
|
|
static psa_status_t psa_rsa_read_exponent(const uint8_t *domain_parameters,
|
|
|
|
size_t domain_parameters_size,
|
|
|
|
int *exponent)
|
2020-11-05 17:36:40 +01:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
uint32_t acc = 0;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (domain_parameters_size == 0) {
|
2020-11-05 17:36:40 +01:00
|
|
|
*exponent = 65537;
|
2023-01-11 14:50:10 +01:00
|
|
|
return PSA_SUCCESS;
|
2020-11-05 17:36:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Mbed TLS encodes the public exponent as an int. For simplicity, only
|
|
|
|
* support values that fit in a 32-bit integer, which is larger than
|
|
|
|
* int on just about every platform anyway. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (domain_parameters_size > sizeof(acc)) {
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
}
|
|
|
|
for (i = 0; i < domain_parameters_size; i++) {
|
|
|
|
acc = (acc << 8) | domain_parameters[i];
|
|
|
|
}
|
|
|
|
if (acc > INT_MAX) {
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
}
|
2020-11-05 17:36:40 +01:00
|
|
|
*exponent = acc;
|
2023-01-11 14:50:10 +01:00
|
|
|
return PSA_SUCCESS;
|
2020-11-05 17:36:40 +01:00
|
|
|
}
|
|
|
|
|
2021-03-13 18:50:11 +01:00
|
|
|
psa_status_t mbedtls_psa_rsa_generate_key(
|
2020-11-05 17:36:40 +01:00
|
|
|
const psa_key_attributes_t *attributes,
|
2023-01-11 14:50:10 +01:00
|
|
|
uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
|
2020-11-05 17:36:40 +01:00
|
|
|
{
|
|
|
|
psa_status_t status;
|
|
|
|
mbedtls_rsa_context rsa;
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
int exponent;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_rsa_read_exponent(attributes->domain_parameters,
|
|
|
|
attributes->domain_parameters_size,
|
|
|
|
&exponent);
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
mbedtls_rsa_init(&rsa);
|
|
|
|
ret = mbedtls_rsa_gen_key(&rsa,
|
|
|
|
mbedtls_psa_get_random,
|
|
|
|
MBEDTLS_PSA_RANDOM_STATE,
|
|
|
|
(unsigned int) attributes->core.bits,
|
|
|
|
exponent);
|
|
|
|
if (ret != 0) {
|
|
|
|
return mbedtls_to_psa_error(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
status = mbedtls_psa_rsa_export_key(attributes->core.type,
|
|
|
|
&rsa, key_buffer, key_buffer_size,
|
|
|
|
key_buffer_length);
|
|
|
|
mbedtls_rsa_free(&rsa);
|
|
|
|
|
|
|
|
return status;
|
2020-11-05 17:36:40 +01:00
|
|
|
}
|
2023-07-11 14:11:28 +02:00
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
|
2020-11-05 17:36:40 +01:00
|
|
|
|
2020-12-09 13:34:54 +01:00
|
|
|
/****************************************************************/
|
|
|
|
/* Sign/verify hashes */
|
|
|
|
/****************************************************************/
|
|
|
|
|
2021-03-13 18:50:11 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
|
2020-12-09 13:34:54 +01:00
|
|
|
|
|
|
|
/* Decode the hash algorithm from alg and store the mbedtls encoding in
|
|
|
|
* md_alg. Verify that the hash length is acceptable. */
|
2023-01-11 14:50:10 +01:00
|
|
|
static psa_status_t psa_rsa_decode_md_type(psa_algorithm_t alg,
|
|
|
|
size_t hash_length,
|
|
|
|
mbedtls_md_type_t *md_alg)
|
2020-12-09 13:34:54 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
|
2023-03-28 11:38:08 +02:00
|
|
|
*md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
|
2020-12-09 13:34:54 +01:00
|
|
|
|
|
|
|
/* The Mbed TLS RSA module uses an unsigned int for hash length
|
|
|
|
* parameters. Validate that it fits so that we don't risk an
|
|
|
|
* overflow later. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (hash_length > UINT_MAX) {
|
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
}
|
2020-12-09 13:34:54 +01:00
|
|
|
|
2021-06-07 15:34:10 +02:00
|
|
|
/* For signatures using a hash, the hash length must be correct. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW) {
|
|
|
|
if (*md_alg == MBEDTLS_MD_NONE) {
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
}
|
2023-03-28 11:14:24 +02:00
|
|
|
if (mbedtls_md_get_size_from_type(*md_alg) != hash_length) {
|
2023-01-11 14:50:10 +01:00
|
|
|
return PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
}
|
2020-12-09 13:34:54 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return PSA_SUCCESS;
|
2020-12-09 13:34:54 +01:00
|
|
|
}
|
|
|
|
|
2021-03-13 18:50:11 +01:00
|
|
|
psa_status_t mbedtls_psa_rsa_sign_hash(
|
2020-12-09 13:34:54 +01:00
|
|
|
const psa_key_attributes_t *attributes,
|
|
|
|
const uint8_t *key_buffer, size_t key_buffer_size,
|
|
|
|
psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
|
2023-01-11 14:50:10 +01:00
|
|
|
uint8_t *signature, size_t signature_size, size_t *signature_length)
|
2020-12-09 13:34:54 +01:00
|
|
|
{
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
mbedtls_rsa_context *rsa = NULL;
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
mbedtls_md_type_t md_alg;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = mbedtls_psa_rsa_load_representation(attributes->core.type,
|
|
|
|
key_buffer,
|
|
|
|
key_buffer_size,
|
|
|
|
&rsa);
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
return status;
|
|
|
|
}
|
2020-12-09 13:34:54 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_rsa_decode_md_type(alg, hash_length, &md_alg);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2020-12-09 13:34:54 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-12-09 13:34:54 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (signature_size < mbedtls_rsa_get_len(rsa)) {
|
2020-12-09 13:34:54 +01:00
|
|
|
status = PSA_ERROR_BUFFER_TOO_SMALL;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2021-03-13 18:50:11 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg)) {
|
|
|
|
ret = mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V15,
|
|
|
|
MBEDTLS_MD_NONE);
|
|
|
|
if (ret == 0) {
|
|
|
|
ret = mbedtls_rsa_pkcs1_sign(rsa,
|
|
|
|
mbedtls_psa_get_random,
|
|
|
|
MBEDTLS_PSA_RANDOM_STATE,
|
|
|
|
md_alg,
|
|
|
|
(unsigned int) hash_length,
|
|
|
|
hash,
|
|
|
|
signature);
|
2021-06-03 18:51:59 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
} else
|
2021-03-13 18:50:11 +01:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (PSA_ALG_IS_RSA_PSS(alg)) {
|
|
|
|
ret = mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
|
|
|
|
|
|
|
|
if (ret == 0) {
|
|
|
|
ret = mbedtls_rsa_rsassa_pss_sign(rsa,
|
|
|
|
mbedtls_psa_get_random,
|
|
|
|
MBEDTLS_PSA_RANDOM_STATE,
|
|
|
|
MBEDTLS_MD_NONE,
|
|
|
|
(unsigned int) hash_length,
|
|
|
|
hash,
|
|
|
|
signature);
|
2021-06-03 18:51:59 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
} else
|
2021-03-13 18:50:11 +01:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
|
2020-12-09 13:34:54 +01:00
|
|
|
{
|
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret == 0) {
|
|
|
|
*signature_length = mbedtls_rsa_get_len(rsa);
|
|
|
|
}
|
|
|
|
status = mbedtls_to_psa_error(ret);
|
2020-12-09 13:34:54 +01:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_rsa_free(rsa);
|
|
|
|
mbedtls_free(rsa);
|
2020-12-09 13:34:54 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return status;
|
2020-12-09 13:34:54 +01:00
|
|
|
}
|
|
|
|
|
2021-03-13 18:50:11 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
|
2023-01-11 14:50:10 +01:00
|
|
|
static int rsa_pss_expected_salt_len(psa_algorithm_t alg,
|
|
|
|
const mbedtls_rsa_context *rsa,
|
|
|
|
size_t hash_length)
|
2021-10-04 22:15:05 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (PSA_ALG_IS_RSA_PSS_ANY_SALT(alg)) {
|
|
|
|
return MBEDTLS_RSA_SALT_LEN_ANY;
|
|
|
|
}
|
2021-10-04 22:15:05 +02:00
|
|
|
/* Otherwise: standard salt length, i.e. largest possible salt length
|
|
|
|
* up to the hash length. */
|
2023-01-11 14:50:10 +01:00
|
|
|
int klen = (int) mbedtls_rsa_get_len(rsa); // known to fit
|
2021-10-04 22:15:05 +02:00
|
|
|
int hlen = (int) hash_length; // known to fit
|
|
|
|
int room = klen - 2 - hlen;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (room < 0) {
|
|
|
|
return 0; // there is no valid signature in this case anyway
|
|
|
|
} else if (room > hlen) {
|
|
|
|
return hlen;
|
|
|
|
} else {
|
|
|
|
return room;
|
|
|
|
}
|
2021-10-04 22:15:05 +02:00
|
|
|
}
|
2021-03-13 18:50:11 +01:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
|
2021-10-04 22:15:05 +02:00
|
|
|
|
2021-03-13 18:50:11 +01:00
|
|
|
psa_status_t mbedtls_psa_rsa_verify_hash(
|
2020-12-09 13:34:54 +01:00
|
|
|
const psa_key_attributes_t *attributes,
|
|
|
|
const uint8_t *key_buffer, size_t key_buffer_size,
|
|
|
|
psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
|
2023-01-11 14:50:10 +01:00
|
|
|
const uint8_t *signature, size_t signature_length)
|
2020-12-09 13:34:54 +01:00
|
|
|
{
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
mbedtls_rsa_context *rsa = NULL;
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
mbedtls_md_type_t md_alg;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = mbedtls_psa_rsa_load_representation(attributes->core.type,
|
|
|
|
key_buffer,
|
|
|
|
key_buffer_size,
|
|
|
|
&rsa);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2020-12-09 13:34:54 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-12-09 13:34:54 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_rsa_decode_md_type(alg, hash_length, &md_alg);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2020-12-09 13:34:54 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-12-09 13:34:54 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (signature_length != mbedtls_rsa_get_len(rsa)) {
|
2020-12-09 13:34:54 +01:00
|
|
|
status = PSA_ERROR_INVALID_SIGNATURE;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2021-03-13 18:50:11 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg)) {
|
|
|
|
ret = mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V15,
|
|
|
|
MBEDTLS_MD_NONE);
|
|
|
|
if (ret == 0) {
|
|
|
|
ret = mbedtls_rsa_pkcs1_verify(rsa,
|
|
|
|
md_alg,
|
|
|
|
(unsigned int) hash_length,
|
|
|
|
hash,
|
|
|
|
signature);
|
2021-06-03 18:51:59 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
} else
|
2021-03-13 18:50:11 +01:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (PSA_ALG_IS_RSA_PSS(alg)) {
|
|
|
|
ret = mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
|
|
|
|
if (ret == 0) {
|
|
|
|
int slen = rsa_pss_expected_salt_len(alg, rsa, hash_length);
|
|
|
|
ret = mbedtls_rsa_rsassa_pss_verify_ext(rsa,
|
|
|
|
md_alg,
|
|
|
|
(unsigned) hash_length,
|
|
|
|
hash,
|
|
|
|
md_alg,
|
|
|
|
slen,
|
|
|
|
signature);
|
2021-06-03 18:51:59 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
} else
|
2021-03-13 18:50:11 +01:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
|
2020-12-09 13:34:54 +01:00
|
|
|
{
|
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Mbed TLS distinguishes "invalid padding" from "valid padding but
|
|
|
|
* the rest of the signature is invalid". This has little use in
|
|
|
|
* practice and PSA doesn't report this distinction. */
|
2023-01-11 14:50:10 +01:00
|
|
|
status = (ret == MBEDTLS_ERR_RSA_INVALID_PADDING) ?
|
2020-12-09 13:34:54 +01:00
|
|
|
PSA_ERROR_INVALID_SIGNATURE :
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_to_psa_error(ret);
|
2020-12-09 13:34:54 +01:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_rsa_free(rsa);
|
|
|
|
mbedtls_free(rsa);
|
2020-12-09 13:34:54 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return status;
|
2020-12-09 13:34:54 +01:00
|
|
|
}
|
|
|
|
|
2020-12-09 15:18:01 +01:00
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
|
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
|
|
|
|
|
2021-12-09 11:13:58 +01:00
|
|
|
/****************************************************************/
|
|
|
|
/* Asymmetric cryptography */
|
|
|
|
/****************************************************************/
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
|
2023-01-11 14:50:10 +01:00
|
|
|
static int psa_rsa_oaep_set_padding_mode(psa_algorithm_t alg,
|
|
|
|
mbedtls_rsa_context *rsa)
|
2021-12-09 11:13:58 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH(alg);
|
2023-03-28 11:38:08 +02:00
|
|
|
mbedtls_md_type_t md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
|
2021-12-09 11:13:58 +01:00
|
|
|
|
2023-05-31 10:48:38 +02:00
|
|
|
/* Just to get the error status right, as rsa_set_padding() doesn't
|
|
|
|
* distinguish between "bad RSA algorithm" and "unknown hash". */
|
|
|
|
if (mbedtls_md_info_from_type(md_alg) == NULL) {
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
|
2021-12-09 11:13:58 +01:00
|
|
|
}
|
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_status_t mbedtls_psa_asymmetric_encrypt(const psa_key_attributes_t *attributes,
|
|
|
|
const uint8_t *key_buffer,
|
|
|
|
size_t key_buffer_size,
|
|
|
|
psa_algorithm_t alg,
|
|
|
|
const uint8_t *input,
|
|
|
|
size_t input_length,
|
|
|
|
const uint8_t *salt,
|
|
|
|
size_t salt_length,
|
|
|
|
uint8_t *output,
|
|
|
|
size_t output_size,
|
|
|
|
size_t *output_length)
|
2021-12-09 11:13:58 +01:00
|
|
|
{
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
(void) key_buffer;
|
|
|
|
(void) key_buffer_size;
|
|
|
|
(void) input;
|
|
|
|
(void) input_length;
|
|
|
|
(void) salt;
|
|
|
|
(void) salt_length;
|
|
|
|
(void) output;
|
|
|
|
(void) output_size;
|
|
|
|
(void) output_length;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
|
2021-12-09 11:13:58 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
|
2023-01-11 14:50:10 +01:00
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
|
2021-12-09 11:13:58 +01:00
|
|
|
mbedtls_rsa_context *rsa = NULL;
|
2023-01-11 14:50:10 +01:00
|
|
|
status = mbedtls_psa_rsa_load_representation(attributes->core.type,
|
|
|
|
key_buffer,
|
|
|
|
key_buffer_size,
|
|
|
|
&rsa);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-12-09 11:13:58 +01:00
|
|
|
goto rsa_exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-12-09 11:13:58 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (output_size < mbedtls_rsa_get_len(rsa)) {
|
2021-12-09 11:13:58 +01:00
|
|
|
status = PSA_ERROR_BUFFER_TOO_SMALL;
|
|
|
|
goto rsa_exit;
|
|
|
|
}
|
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
|
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
|
2021-12-09 11:13:58 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
|
|
|
|
status = mbedtls_to_psa_error(
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_rsa_pkcs1_encrypt(rsa,
|
|
|
|
mbedtls_psa_get_random,
|
|
|
|
MBEDTLS_PSA_RANDOM_STATE,
|
|
|
|
input_length,
|
|
|
|
input,
|
|
|
|
output));
|
2021-12-09 11:13:58 +01:00
|
|
|
#else
|
|
|
|
status = PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
|
2023-01-11 14:50:10 +01:00
|
|
|
} else
|
|
|
|
if (PSA_ALG_IS_RSA_OAEP(alg)) {
|
2021-12-09 11:13:58 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
|
|
|
|
status = mbedtls_to_psa_error(
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_rsa_oaep_set_padding_mode(alg, rsa));
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-12-09 11:13:58 +01:00
|
|
|
goto rsa_exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-12-09 11:13:58 +01:00
|
|
|
|
|
|
|
status = mbedtls_to_psa_error(
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_rsa_rsaes_oaep_encrypt(rsa,
|
|
|
|
mbedtls_psa_get_random,
|
|
|
|
MBEDTLS_PSA_RANDOM_STATE,
|
|
|
|
salt, salt_length,
|
|
|
|
input_length,
|
|
|
|
input,
|
|
|
|
output));
|
2021-12-09 11:13:58 +01:00
|
|
|
#else
|
|
|
|
status = PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2021-12-09 11:13:58 +01:00
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
}
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
|
2023-01-11 14:50:10 +01:00
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
|
2021-12-09 11:13:58 +01:00
|
|
|
rsa_exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
if (status == PSA_SUCCESS) {
|
|
|
|
*output_length = mbedtls_rsa_get_len(rsa);
|
|
|
|
}
|
2021-12-09 11:13:58 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_rsa_free(rsa);
|
|
|
|
mbedtls_free(rsa);
|
2021-12-09 11:13:58 +01:00
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
|
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2021-12-09 11:13:58 +01:00
|
|
|
status = PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_status_t mbedtls_psa_asymmetric_decrypt(const psa_key_attributes_t *attributes,
|
|
|
|
const uint8_t *key_buffer,
|
|
|
|
size_t key_buffer_size,
|
|
|
|
psa_algorithm_t alg,
|
|
|
|
const uint8_t *input,
|
|
|
|
size_t input_length,
|
|
|
|
const uint8_t *salt,
|
|
|
|
size_t salt_length,
|
|
|
|
uint8_t *output,
|
|
|
|
size_t output_size,
|
|
|
|
size_t *output_length)
|
2021-12-13 09:08:05 +01:00
|
|
|
{
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
(void) key_buffer;
|
|
|
|
(void) key_buffer_size;
|
|
|
|
(void) input;
|
|
|
|
(void) input_length;
|
|
|
|
(void) salt;
|
|
|
|
(void) salt_length;
|
|
|
|
(void) output;
|
|
|
|
(void) output_size;
|
|
|
|
(void) output_length;
|
|
|
|
|
|
|
|
*output_length = 0;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
|
2021-12-13 09:08:05 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
|
2023-01-11 14:50:10 +01:00
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
|
2021-12-13 09:08:05 +01:00
|
|
|
mbedtls_rsa_context *rsa = NULL;
|
2023-01-11 14:50:10 +01:00
|
|
|
status = mbedtls_psa_rsa_load_representation(attributes->core.type,
|
|
|
|
key_buffer,
|
|
|
|
key_buffer_size,
|
|
|
|
&rsa);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-12-13 09:08:05 +01:00
|
|
|
goto rsa_exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-12-13 09:08:05 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (input_length != mbedtls_rsa_get_len(rsa)) {
|
2021-12-13 09:08:05 +01:00
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
goto rsa_exit;
|
|
|
|
}
|
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
|
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
|
2021-12-13 09:08:05 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
|
|
|
|
status = mbedtls_to_psa_error(
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_rsa_pkcs1_decrypt(rsa,
|
|
|
|
mbedtls_psa_get_random,
|
|
|
|
MBEDTLS_PSA_RANDOM_STATE,
|
|
|
|
output_length,
|
|
|
|
input,
|
|
|
|
output,
|
|
|
|
output_size));
|
2021-12-13 09:08:05 +01:00
|
|
|
#else
|
|
|
|
status = PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
|
2023-01-11 14:50:10 +01:00
|
|
|
} else
|
|
|
|
if (PSA_ALG_IS_RSA_OAEP(alg)) {
|
2021-12-13 09:08:05 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
|
|
|
|
status = mbedtls_to_psa_error(
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_rsa_oaep_set_padding_mode(alg, rsa));
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-12-13 09:08:05 +01:00
|
|
|
goto rsa_exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-12-13 09:08:05 +01:00
|
|
|
|
|
|
|
status = mbedtls_to_psa_error(
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_rsa_rsaes_oaep_decrypt(rsa,
|
|
|
|
mbedtls_psa_get_random,
|
|
|
|
MBEDTLS_PSA_RANDOM_STATE,
|
|
|
|
salt, salt_length,
|
|
|
|
output_length,
|
|
|
|
input,
|
|
|
|
output,
|
|
|
|
output_size));
|
2021-12-13 09:08:05 +01:00
|
|
|
#else
|
|
|
|
status = PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2021-12-13 09:08:05 +01:00
|
|
|
status = PSA_ERROR_INVALID_ARGUMENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
|
2023-01-11 14:50:10 +01:00
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
|
2021-12-13 09:08:05 +01:00
|
|
|
rsa_exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_rsa_free(rsa);
|
|
|
|
mbedtls_free(rsa);
|
2021-12-13 09:08:05 +01:00
|
|
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
|
|
|
|
* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2021-12-13 09:08:05 +01:00
|
|
|
status = PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2020-11-25 15:25:26 +01:00
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_C */
|