blob: 74276d545169f0c6f9f6b8be54aa7bdfedd3a5c4 [file] [log] [blame]
Manuel Pégourié-Gonnard2be8c632023-06-07 13:07:21 +02001/**
2 * \file psa_util_internal.h
3 *
4 * \brief Internal utility functions for use of PSA Crypto.
5 */
6/*
7 * Copyright The Mbed TLS Contributors
8 * SPDX-License-Identifier: Apache-2.0
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 */
22
23#ifndef MBEDTLS_PSA_UTIL_INTERNAL_H
24#define MBEDTLS_PSA_UTIL_INTERNAL_H
25
26/* Include the public header so that users only need one include. */
27#include "mbedtls/psa_util.h"
28
29#if defined(MBEDTLS_PSA_CRYPTO_C)
30
Manuel Pégourié-Gonnarda5a8f292023-06-07 13:25:51 +020031/*************************************************************************
32 * FFDH
33 ************************************************************************/
34
35#define MBEDTLS_PSA_MAX_FFDH_PUBKEY_LENGTH \
36 PSA_KEY_EXPORT_FFDH_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_FFDH_MAX_KEY_BITS)
37
38/*************************************************************************
39 * ECC
40 ************************************************************************/
41
42#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH \
43 PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)
44
45#define MBEDTLS_PSA_MAX_EC_KEY_PAIR_LENGTH \
46 PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)
47
Manuel Pégourié-Gonnardb7e89392023-06-08 10:06:10 +020048/*************************************************************************
49 * Error translation
50 ************************************************************************/
51
52typedef struct {
53 /* Error codes used by PSA crypto are in -255..-128, fitting in 16 bits. */
54 int16_t psa_status;
55 /* Error codes used by Mbed TLS are in one of the ranges
56 * -127..-1 (low-level) or -32767..-4096 (high-level with a low-level
57 * code optionally added), fitting in 16 bits. */
58 int16_t mbedtls_error;
59} mbedtls_error_pair_t;
60
61#if defined(MBEDTLS_MD_LIGHT)
62extern const mbedtls_error_pair_t psa_to_md_errors[4];
63#endif
64
65#if defined(MBEDTLS_LMS_C)
66extern const mbedtls_error_pair_t psa_to_lms_errors[3];
67#endif
68
69#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
70extern const mbedtls_error_pair_t psa_to_ssl_errors[7];
71#endif
72
73#if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY) || \
74 defined(MBEDTLS_PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_LEGACY)
75extern const mbedtls_error_pair_t psa_to_pk_rsa_errors[8];
76#endif
77
78#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
79 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
80extern const mbedtls_error_pair_t psa_to_pk_ecdsa_errors[7];
81#endif
82
83/* Generic fallback function for error translation,
84 * when the received state was not module-specific. */
85int psa_generic_status_to_mbedtls(psa_status_t status);
86
87/* This function iterates over provided local error translations,
88 * and if no match was found - calls the fallback error translation function. */
89int psa_status_to_mbedtls(psa_status_t status,
90 const mbedtls_error_pair_t *local_translations,
91 size_t local_errors_num,
92 int (*fallback_f)(psa_status_t));
93
94/* The second out of three-stage error handling functions of the pk module,
95 * acts as a fallback after RSA / ECDSA error translation, and if no match
96 * is found, it itself calls psa_generic_status_to_mbedtls. */
97int psa_pk_status_to_mbedtls(psa_status_t status);
98
99/* Utility macro to shorten the defines of error translator in modules. */
100#define PSA_TO_MBEDTLS_ERR_LIST(status, error_list, fallback_f) \
101 psa_status_to_mbedtls(status, error_list, \
102 sizeof(error_list)/sizeof(error_list[0]), \
103 fallback_f)
104
Manuel Pégourié-Gonnard2be8c632023-06-07 13:07:21 +0200105#endif /* MBEDTLS_PSA_CRYPTO_C */
106#endif /* MBEDTLS_PSA_UTIL_INTERNAL_H */