2022-12-23 17:00:06 +01:00
|
|
|
/*
|
|
|
|
* PSA hashing layer on top of Mbed TLS software 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>
|
|
|
|
|
|
|
|
#include "psa_crypto_core.h"
|
2023-06-07 13:07:21 +02:00
|
|
|
#include <psa_util_internal.h>
|
2023-06-20 09:59:13 +02:00
|
|
|
|
|
|
|
/* The following includes are needed for MBEDTLS_ERR_XXX macros */
|
2022-12-23 17:00:06 +01:00
|
|
|
#include <mbedtls/error.h>
|
2023-06-20 09:59:13 +02:00
|
|
|
#if defined(MBEDTLS_MD_LIGHT)
|
|
|
|
#include <mbedtls/md.h>
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_LMS_C)
|
2022-12-23 17:00:06 +01:00
|
|
|
#include <mbedtls/lms.h>
|
2023-06-20 09:59:13 +02:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SSL_TLS_C) && \
|
|
|
|
(defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
|
2022-12-23 17:00:06 +01:00
|
|
|
#include <mbedtls/ssl.h>
|
2023-06-20 09:59:13 +02:00
|
|
|
#endif
|
|
|
|
#if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY) || \
|
2023-07-11 16:59:21 +02:00
|
|
|
defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC)
|
2022-12-23 17:00:06 +01:00
|
|
|
#include <mbedtls/rsa.h>
|
2023-06-20 09:59:13 +02:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
|
|
|
|
defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
|
|
|
|
#include <mbedtls/ecp.h>
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_PK_C)
|
|
|
|
#include <mbedtls/pk.h>
|
|
|
|
#endif
|
2022-12-23 17:00:06 +01:00
|
|
|
|
|
|
|
/* PSA_SUCCESS is kept at the top of each error table since
|
|
|
|
* it's the most common status when everything functions properly. */
|
2023-03-29 12:38:37 +02:00
|
|
|
#if defined(MBEDTLS_MD_LIGHT)
|
2023-03-03 11:54:13 +01:00
|
|
|
const mbedtls_error_pair_t psa_to_md_errors[] =
|
2022-12-23 17:00:06 +01:00
|
|
|
{
|
2023-02-28 16:32:47 +01:00
|
|
|
{ PSA_SUCCESS, 0 },
|
|
|
|
{ PSA_ERROR_NOT_SUPPORTED, MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE },
|
|
|
|
{ PSA_ERROR_INVALID_ARGUMENT, MBEDTLS_ERR_MD_BAD_INPUT_DATA },
|
|
|
|
{ PSA_ERROR_INSUFFICIENT_MEMORY, MBEDTLS_ERR_MD_ALLOC_FAILED }
|
2022-12-23 17:00:06 +01:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_LMS_C)
|
2023-03-03 11:54:13 +01:00
|
|
|
const mbedtls_error_pair_t psa_to_lms_errors[] =
|
2022-12-23 17:00:06 +01:00
|
|
|
{
|
2023-02-28 16:32:47 +01:00
|
|
|
{ PSA_SUCCESS, 0 },
|
|
|
|
{ PSA_ERROR_BUFFER_TOO_SMALL, MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL },
|
|
|
|
{ PSA_ERROR_INVALID_ARGUMENT, MBEDTLS_ERR_LMS_BAD_INPUT_DATA }
|
2022-12-23 17:00:06 +01:00
|
|
|
};
|
|
|
|
#endif
|
2023-06-20 09:59:13 +02:00
|
|
|
#if defined(MBEDTLS_SSL_TLS_C) && \
|
|
|
|
(defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
|
2023-03-03 11:54:13 +01:00
|
|
|
const mbedtls_error_pair_t psa_to_ssl_errors[] =
|
2022-12-23 17:00:06 +01:00
|
|
|
{
|
2023-02-28 16:32:47 +01:00
|
|
|
{ PSA_SUCCESS, 0 },
|
|
|
|
{ PSA_ERROR_INSUFFICIENT_MEMORY, MBEDTLS_ERR_SSL_ALLOC_FAILED },
|
|
|
|
{ PSA_ERROR_NOT_SUPPORTED, MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE },
|
|
|
|
{ PSA_ERROR_INVALID_SIGNATURE, MBEDTLS_ERR_SSL_INVALID_MAC },
|
|
|
|
{ PSA_ERROR_INVALID_ARGUMENT, MBEDTLS_ERR_SSL_BAD_INPUT_DATA },
|
|
|
|
{ PSA_ERROR_BAD_STATE, MBEDTLS_ERR_SSL_INTERNAL_ERROR },
|
|
|
|
{ PSA_ERROR_BUFFER_TOO_SMALL, MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL }
|
2022-12-23 17:00:06 +01:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY) || \
|
2023-07-10 10:55:12 +02:00
|
|
|
defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC)
|
2023-03-03 11:54:13 +01:00
|
|
|
const mbedtls_error_pair_t psa_to_pk_rsa_errors[] =
|
2022-12-23 17:00:06 +01:00
|
|
|
{
|
2023-02-28 16:32:47 +01:00
|
|
|
{ PSA_SUCCESS, 0 },
|
|
|
|
{ PSA_ERROR_NOT_PERMITTED, MBEDTLS_ERR_RSA_BAD_INPUT_DATA },
|
|
|
|
{ PSA_ERROR_INVALID_ARGUMENT, MBEDTLS_ERR_RSA_BAD_INPUT_DATA },
|
|
|
|
{ PSA_ERROR_INVALID_HANDLE, MBEDTLS_ERR_RSA_BAD_INPUT_DATA },
|
|
|
|
{ PSA_ERROR_BUFFER_TOO_SMALL, MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE },
|
|
|
|
{ PSA_ERROR_INSUFFICIENT_ENTROPY, MBEDTLS_ERR_RSA_RNG_FAILED },
|
|
|
|
{ PSA_ERROR_INVALID_SIGNATURE, MBEDTLS_ERR_RSA_VERIFY_FAILED },
|
|
|
|
{ PSA_ERROR_INVALID_PADDING, MBEDTLS_ERR_RSA_INVALID_PADDING }
|
2022-12-23 17:00:06 +01:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
|
|
|
|
defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
|
2023-03-03 11:54:13 +01:00
|
|
|
const mbedtls_error_pair_t psa_to_pk_ecdsa_errors[] =
|
2022-12-23 17:00:06 +01:00
|
|
|
{
|
2023-02-28 16:32:47 +01:00
|
|
|
{ PSA_SUCCESS, 0 },
|
|
|
|
{ PSA_ERROR_NOT_PERMITTED, MBEDTLS_ERR_ECP_BAD_INPUT_DATA },
|
|
|
|
{ PSA_ERROR_INVALID_ARGUMENT, MBEDTLS_ERR_ECP_BAD_INPUT_DATA },
|
|
|
|
{ PSA_ERROR_INVALID_HANDLE, MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE },
|
|
|
|
{ PSA_ERROR_BUFFER_TOO_SMALL, MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL },
|
|
|
|
{ PSA_ERROR_INSUFFICIENT_ENTROPY, MBEDTLS_ERR_ECP_RANDOM_FAILED },
|
|
|
|
{ PSA_ERROR_INVALID_SIGNATURE, MBEDTLS_ERR_ECP_VERIFY_FAILED }
|
2022-12-23 17:00:06 +01:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int psa_generic_status_to_mbedtls(psa_status_t status)
|
|
|
|
{
|
|
|
|
switch (status) {
|
|
|
|
case PSA_SUCCESS:
|
|
|
|
return 0;
|
|
|
|
case PSA_ERROR_NOT_SUPPORTED:
|
|
|
|
return MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED;
|
|
|
|
case PSA_ERROR_CORRUPTION_DETECTED:
|
|
|
|
return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
case PSA_ERROR_COMMUNICATION_FAILURE:
|
|
|
|
case PSA_ERROR_HARDWARE_FAILURE:
|
|
|
|
return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
|
|
|
|
case PSA_ERROR_NOT_PERMITTED:
|
|
|
|
default:
|
|
|
|
return MBEDTLS_ERR_ERROR_GENERIC_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int psa_status_to_mbedtls(psa_status_t status,
|
2023-03-03 11:54:13 +01:00
|
|
|
const mbedtls_error_pair_t *local_translations,
|
2023-03-27 14:02:08 +02:00
|
|
|
size_t local_errors_num,
|
2022-12-23 17:00:06 +01:00
|
|
|
int (*fallback_f)(psa_status_t))
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < local_errors_num; i++) {
|
2023-02-28 16:32:47 +01:00
|
|
|
if (status == local_translations[i].psa_status) {
|
|
|
|
return local_translations[i].mbedtls_error;
|
2022-12-23 17:00:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return fallback_f(status);
|
|
|
|
}
|
|
|
|
|
2023-06-20 09:59:13 +02:00
|
|
|
#if defined(MBEDTLS_PK_C)
|
2022-12-23 17:00:06 +01:00
|
|
|
int psa_pk_status_to_mbedtls(psa_status_t status)
|
|
|
|
{
|
|
|
|
switch (status) {
|
|
|
|
case PSA_ERROR_INVALID_HANDLE:
|
|
|
|
return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
|
|
|
|
case PSA_ERROR_BUFFER_TOO_SMALL:
|
|
|
|
return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
|
|
|
|
case PSA_ERROR_NOT_SUPPORTED:
|
|
|
|
return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
|
|
|
|
case PSA_ERROR_INVALID_ARGUMENT:
|
|
|
|
return MBEDTLS_ERR_PK_INVALID_ALG;
|
|
|
|
case PSA_ERROR_INSUFFICIENT_MEMORY:
|
|
|
|
return MBEDTLS_ERR_PK_ALLOC_FAILED;
|
|
|
|
case PSA_ERROR_BAD_STATE:
|
|
|
|
return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
|
|
|
|
case PSA_ERROR_DATA_CORRUPT:
|
|
|
|
case PSA_ERROR_DATA_INVALID:
|
|
|
|
case PSA_ERROR_STORAGE_FAILURE:
|
|
|
|
return MBEDTLS_ERR_PK_FILE_IO_ERROR;
|
|
|
|
default:
|
|
|
|
return psa_generic_status_to_mbedtls(status);
|
|
|
|
}
|
|
|
|
}
|
2023-06-20 09:59:13 +02:00
|
|
|
#endif /* MBEDTLS_PK_C */
|
2022-12-23 17:00:06 +01:00
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_C */
|