Skip to content
0.2.0-dev Download ZIP
132 lines ·5.1 KB Raw
1/**
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 OR GPL-2.0-or-later
9 */
10
11#ifndef TF_PSA_CRYPTO_PSA_UTIL_INTERNAL_H
12#define TF_PSA_CRYPTO_PSA_UTIL_INTERNAL_H
13
14/* Include the public header so that users only need one include. */
15#include "mbedtls/psa_util.h"
16
17#include "psa/crypto.h"
18
19#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
20
21#include "mbedtls/private/ecp.h"
22
23#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
24
25#if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
26
27/*************************************************************************
28 * FFDH
29 ************************************************************************/
30
31#define MBEDTLS_PSA_MAX_FFDH_PUBKEY_LENGTH \
32 PSA_KEY_EXPORT_FFDH_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_FFDH_MAX_KEY_BITS)
33
34/*************************************************************************
35 * ECC
36 ************************************************************************/
37
38#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH \
39 PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)
40
41#define MBEDTLS_PSA_MAX_EC_KEY_PAIR_LENGTH \
42 PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)
43
44/*************************************************************************
45 * Error translation
46 ************************************************************************/
47
48typedef struct {
49 /* Error codes used by PSA crypto are in -255..-128, fitting in 16 bits. */
50 int16_t psa_status;
51 /* Error codes used by Mbed TLS are in one of the ranges
52 * -127..-1 (low-level) or -32767..-4096 (high-level with a low-level
53 * code optionally added), fitting in 16 bits. */
54 int16_t mbedtls_error;
55} mbedtls_error_pair_t;
56
57#if defined(MBEDTLS_MD_LIGHT)
58extern const mbedtls_error_pair_t psa_to_md_errors[4];
59#endif
60
61#if defined(MBEDTLS_BLOCK_CIPHER_SOME_PSA)
62extern const mbedtls_error_pair_t psa_to_cipher_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(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY) || \
70 defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC)
71extern const mbedtls_error_pair_t psa_to_pk_rsa_errors[8];
72#endif
73
74#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
75extern const mbedtls_error_pair_t psa_to_pk_ecdsa_errors[7];
76#endif
77
78/* Generic fallback function for error translation,
79 * when the received state was not module-specific. */
80int psa_generic_status_to_mbedtls(psa_status_t status);
81
82/* This function iterates over provided local error translations,
83 * and if no match was found - calls the fallback error translation function. */
84int psa_status_to_mbedtls(psa_status_t status,
85 const mbedtls_error_pair_t *local_translations,
86 size_t local_errors_num,
87 int (*fallback_f)(psa_status_t));
88
89/* The second out of three-stage error handling functions of the pk module,
90 * acts as a fallback after RSA / ECDSA error translation, and if no match
91 * is found, it itself calls psa_generic_status_to_mbedtls. */
92int psa_pk_status_to_mbedtls(psa_status_t status);
93
94/* Utility macro to shorten the defines of error translator in modules. */
95#define PSA_TO_MBEDTLS_ERR_LIST(status, error_list, fallback_f) \
96 psa_status_to_mbedtls(status, error_list, \
97 sizeof(error_list)/sizeof(error_list[0]), \
98 fallback_f)
99
100#endif /* MBEDTLS_PSA_CRYPTO_CLIENT */
101#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
102/** Convert an ECC curve identifier from the Mbed TLS encoding to PSA.
103 *
104 * \param grpid An Mbed TLS elliptic curve identifier
105 * (`MBEDTLS_ECP_DP_xxx`).
106 * \param[out] bits On success the bit size of the curve; 0 on failure.
107 *
108 * \return If the curve is supported in the PSA API, this function
109 * returns the proper PSA curve identifier
110 * (`PSA_ECC_FAMILY_xxx`). This holds even if the curve is
111 * not supported by the ECP module.
112 * \return \c 0 if the curve is not supported in the PSA API.
113 */
114psa_ecc_family_t mbedtls_ecc_group_to_psa(mbedtls_ecp_group_id grpid,
115 size_t *bits);
116
117/** Convert an ECC curve identifier from the PSA encoding to Mbed TLS.
118 *
119 * \param family A PSA elliptic curve family identifier
120 * (`PSA_ECC_FAMILY_xxx`).
121 * \param bits The bit-length of a private key on \p curve.
122 *
123 * \return If the curve is supported in the PSA API, this function
124 * returns the corresponding Mbed TLS elliptic curve
125 * identifier (`MBEDTLS_ECP_DP_xxx`).
126 * \return #MBEDTLS_ECP_DP_NONE if the combination of \c curve
127 * and \p bits is not supported.
128 */
129mbedtls_ecp_group_id mbedtls_ecc_group_from_psa(psa_ecc_family_t family,
130 size_t bits);
131#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
132#endif /* TF_PSA_CRYPTO_PSA_UTIL_INTERNAL_H */