blob: 21fb34a8f4b3cc9a52964c73ca7f4291d89041a2 [file] [log] [blame]
Valerio Setti229bf102023-05-15 11:13:55 +02001/**
2 * \file pk_internal.h
3 *
4 * \brief Public Key abstraction layer: internal (i.e. library only) functions
5 * and definitions.
6 */
7/*
8 * Copyright The Mbed TLS Contributors
9 * SPDX-License-Identifier: Apache-2.0
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License"); you may
12 * not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
22 */
23#ifndef MBEDTLS_PK_INTERNAL_H
24#define MBEDTLS_PK_INTERNAL_H
25
Valerio Setti483738e2023-05-17 15:37:29 +020026#include "mbedtls/pk.h"
27
Valerio Setti229bf102023-05-15 11:13:55 +020028#if defined(MBEDTLS_ECP_LIGHT)
29#include "mbedtls/ecp.h"
30#endif
31
Valerio Setti483738e2023-05-17 15:37:29 +020032#if defined(MBEDTLS_USE_PSA_CRYPTO)
33#include "psa/crypto.h"
34#endif
35
Valerio Setti8a622502023-05-18 18:46:38 +020036#if defined(MBEDTLS_PSA_CRYPTO_C)
37#include "mbedtls/psa_util.h"
38#define PSA_PK_TO_MBEDTLS_ERR(status) psa_pk_status_to_mbedtls(status)
39#define PSA_PK_RSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
40 psa_to_pk_rsa_errors, \
41 psa_pk_status_to_mbedtls)
42#define PSA_PK_ECDSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
43 psa_to_pk_ecdsa_errors, \
44 psa_pk_status_to_mbedtls)
45#endif
46
Valerio Setti229bf102023-05-15 11:13:55 +020047#if defined(MBEDTLS_ECP_LIGHT)
48/**
49 * Public function mbedtls_pk_ec() can be used to get direct access to the
Valerio Setti4ac9d442023-05-17 12:32:13 +020050 * wrapped ecp_keypair structure pointed to the pk_ctx. However this is not
51 * ideal because it bypasses the PK module on the control of its internal
Valerio Setti229bf102023-05-15 11:13:55 +020052 * structure (pk_context) fields.
53 * For backward compatibility we keep mbedtls_pk_ec() when ECP_C is defined, but
Valerio Setti4ac9d442023-05-17 12:32:13 +020054 * we provide 2 very similar functions when only ECP_LIGHT is enabled and not
Valerio Setti229bf102023-05-15 11:13:55 +020055 * ECP_C.
56 * These variants embed the "ro" or "rw" keywords in their name to make the
57 * usage of the returned pointer explicit. Of course the returned value is
58 * const or non-const accordingly.
59 */
60static inline const mbedtls_ecp_keypair *mbedtls_pk_ec_ro(const mbedtls_pk_context pk)
61{
62 switch (mbedtls_pk_get_type(&pk)) {
63 case MBEDTLS_PK_ECKEY:
64 case MBEDTLS_PK_ECKEY_DH:
65 case MBEDTLS_PK_ECDSA:
Valerio Settif70b3e02023-05-15 12:57:40 +020066 return (const mbedtls_ecp_keypair *) (pk).MBEDTLS_PRIVATE(pk_ctx);
Valerio Setti229bf102023-05-15 11:13:55 +020067 default:
68 return NULL;
69 }
70}
71
72static inline mbedtls_ecp_keypair *mbedtls_pk_ec_rw(const mbedtls_pk_context pk)
73{
74 switch (mbedtls_pk_get_type(&pk)) {
75 case MBEDTLS_PK_ECKEY:
76 case MBEDTLS_PK_ECKEY_DH:
77 case MBEDTLS_PK_ECDSA:
78 return (mbedtls_ecp_keypair *) (pk).MBEDTLS_PRIVATE(pk_ctx);
79 default:
80 return NULL;
81 }
82}
Valerio Setti4064dbb2023-05-17 15:33:07 +020083
Valerio Settie0e63112023-05-18 18:48:07 +020084static inline mbedtls_ecp_group_id mbedtls_pk_get_group_id(const mbedtls_pk_context *pk)
85{
86 mbedtls_ecp_group_id id;
valerioa87601d2023-05-31 12:01:55 +020087
88#if defined(MBEDTLS_USE_PSA_CRYPTO)
89 psa_key_attributes_t opaque_attrs = PSA_KEY_ATTRIBUTES_INIT;
90 psa_key_type_t opaque_key_type;
91 psa_ecc_family_t curve;
92
93 if (mbedtls_pk_get_type(pk) == MBEDTLS_PK_OPAQUE) {
94 if (psa_get_key_attributes(pk->priv_id, &opaque_attrs) != PSA_SUCCESS) {
95 return MBEDTLS_ECP_DP_NONE;
96 }
97 opaque_key_type = psa_get_key_type(&opaque_attrs);
98 curve = PSA_KEY_TYPE_ECC_GET_FAMILY(opaque_key_type);
99 id = mbedtls_ecc_group_of_psa(curve, psa_get_key_bits(&opaque_attrs), 0);
100 psa_reset_key_attributes(&opaque_attrs);
101 } else
102#endif /* MBEDTLS_USE_PSA_CRYPTO */
103 {
Valerio Settie0e63112023-05-18 18:48:07 +0200104#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
valerioa87601d2023-05-31 12:01:55 +0200105 id = mbedtls_ecc_group_of_psa(pk->ec_family, pk->ec_bits, 0);
Valerio Settie0e63112023-05-18 18:48:07 +0200106#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
valerioa87601d2023-05-31 12:01:55 +0200107 id = mbedtls_pk_ec_ro(*pk)->grp.id;
Valerio Settie0e63112023-05-18 18:48:07 +0200108#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
valerioa87601d2023-05-31 12:01:55 +0200109 }
110
Valerio Settie0e63112023-05-18 18:48:07 +0200111 return id;
112}
113
114/* Helper for Montgomery curves */
Valerio Setti4064dbb2023-05-17 15:33:07 +0200115#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
116#define MBEDTLS_PK_HAVE_RFC8410_CURVES
Valerio Setti4064dbb2023-05-17 15:33:07 +0200117#endif /* MBEDTLS_ECP_DP_CURVE25519_ENABLED || MBEDTLS_ECP_DP_CURVE448_ENABLED */
Valerio Setti229bf102023-05-15 11:13:55 +0200118#endif /* MBEDTLS_ECP_LIGHT */
119
Valerio Setti483738e2023-05-17 15:37:29 +0200120#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
121/**
122 * \brief Copy the public key content in raw format from "ctx->pk_ctx"
123 * (which is an ecp_keypair) into the internal "ctx->pub_raw" buffer.
124 *
125 * \note This is a temporary function that can be removed as soon as the pk
126 * module is free from ECP_C
127 *
128 * \param pk It is the pk_context which is going to be updated. It acts both
129 * as input and output.
130 */
131int mbedtls_pk_update_public_key_from_keypair(mbedtls_pk_context *pk,
132 mbedtls_ecp_keypair *ecp_keypair);
133#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
134
Valerio Setti229bf102023-05-15 11:13:55 +0200135#endif /* MBEDTLS_PK_INTERNAL_H */