blob: dbb7bc1659c989ce74505760a6de055c9057794a [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 Setti229bf102023-05-15 11:13:55 +020036#if defined(MBEDTLS_ECP_LIGHT)
37/**
38 * Public function mbedtls_pk_ec() can be used to get direct access to the
Valerio Setti4ac9d442023-05-17 12:32:13 +020039 * wrapped ecp_keypair structure pointed to the pk_ctx. However this is not
40 * ideal because it bypasses the PK module on the control of its internal
Valerio Setti229bf102023-05-15 11:13:55 +020041 * structure (pk_context) fields.
42 * For backward compatibility we keep mbedtls_pk_ec() when ECP_C is defined, but
Valerio Setti4ac9d442023-05-17 12:32:13 +020043 * we provide 2 very similar functions when only ECP_LIGHT is enabled and not
Valerio Setti229bf102023-05-15 11:13:55 +020044 * ECP_C.
45 * These variants embed the "ro" or "rw" keywords in their name to make the
46 * usage of the returned pointer explicit. Of course the returned value is
47 * const or non-const accordingly.
48 */
49static inline const mbedtls_ecp_keypair *mbedtls_pk_ec_ro(const mbedtls_pk_context pk)
50{
51 switch (mbedtls_pk_get_type(&pk)) {
52 case MBEDTLS_PK_ECKEY:
53 case MBEDTLS_PK_ECKEY_DH:
54 case MBEDTLS_PK_ECDSA:
Valerio Settif70b3e02023-05-15 12:57:40 +020055 return (const mbedtls_ecp_keypair *) (pk).MBEDTLS_PRIVATE(pk_ctx);
Valerio Setti229bf102023-05-15 11:13:55 +020056 default:
57 return NULL;
58 }
59}
60
61static inline mbedtls_ecp_keypair *mbedtls_pk_ec_rw(const mbedtls_pk_context pk)
62{
63 switch (mbedtls_pk_get_type(&pk)) {
64 case MBEDTLS_PK_ECKEY:
65 case MBEDTLS_PK_ECKEY_DH:
66 case MBEDTLS_PK_ECDSA:
67 return (mbedtls_ecp_keypair *) (pk).MBEDTLS_PRIVATE(pk_ctx);
68 default:
69 return NULL;
70 }
71}
Valerio Setti4064dbb2023-05-17 15:33:07 +020072
73/* Helpers for Montgomery curves */
74#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
75#define MBEDTLS_PK_HAVE_RFC8410_CURVES
76
77static inline int mbedtls_pk_is_rfc8410_curve(mbedtls_ecp_group_id id)
78{
79#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
80 if (id == MBEDTLS_ECP_DP_CURVE25519) {
81 return 1;
82 }
83#endif
84#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
85 if (id == MBEDTLS_ECP_DP_CURVE448) {
86 return 1;
87 }
88#endif
89 return 0;
90}
91#endif /* MBEDTLS_ECP_DP_CURVE25519_ENABLED || MBEDTLS_ECP_DP_CURVE448_ENABLED */
Valerio Setti229bf102023-05-15 11:13:55 +020092#endif /* MBEDTLS_ECP_LIGHT */
93
Valerio Setti483738e2023-05-17 15:37:29 +020094#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
95/**
96 * \brief Copy the public key content in raw format from "ctx->pk_ctx"
97 * (which is an ecp_keypair) into the internal "ctx->pub_raw" buffer.
98 *
99 * \note This is a temporary function that can be removed as soon as the pk
100 * module is free from ECP_C
101 *
102 * \param pk It is the pk_context which is going to be updated. It acts both
103 * as input and output.
104 */
105int mbedtls_pk_update_public_key_from_keypair(mbedtls_pk_context *pk,
106 mbedtls_ecp_keypair *ecp_keypair);
107#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
108
Valerio Setti229bf102023-05-15 11:13:55 +0200109#endif /* MBEDTLS_PK_INTERNAL_H */