blob: 7c4f285719f2df8a31a9fc06e03a055ac66da5dc [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
26#if defined(MBEDTLS_ECP_LIGHT)
27#include "mbedtls/ecp.h"
28#endif
29
30#if defined(MBEDTLS_ECP_LIGHT)
31/**
32 * Public function mbedtls_pk_ec() can be used to get direct access to the
33 * wrapped ecp_keypair strucure pointed to the pk_ctx. However this is not
34 * ideal because it bypasses the PK module on the control of its internal's
35 * structure (pk_context) fields.
36 * For backward compatibility we keep mbedtls_pk_ec() when ECP_C is defined, but
37 * we provide 2 very similar function when only ECP_LIGHT is enabled and not
38 * ECP_C.
39 * These variants embed the "ro" or "rw" keywords in their name to make the
40 * usage of the returned pointer explicit. Of course the returned value is
41 * const or non-const accordingly.
42 */
43static inline const mbedtls_ecp_keypair *mbedtls_pk_ec_ro(const mbedtls_pk_context pk)
44{
45 switch (mbedtls_pk_get_type(&pk)) {
46 case MBEDTLS_PK_ECKEY:
47 case MBEDTLS_PK_ECKEY_DH:
48 case MBEDTLS_PK_ECDSA:
Valerio Settif70b3e02023-05-15 12:57:40 +020049 return (const mbedtls_ecp_keypair *) (pk).MBEDTLS_PRIVATE(pk_ctx);
Valerio Setti229bf102023-05-15 11:13:55 +020050 default:
51 return NULL;
52 }
53}
54
55static inline mbedtls_ecp_keypair *mbedtls_pk_ec_rw(const mbedtls_pk_context pk)
56{
57 switch (mbedtls_pk_get_type(&pk)) {
58 case MBEDTLS_PK_ECKEY:
59 case MBEDTLS_PK_ECKEY_DH:
60 case MBEDTLS_PK_ECDSA:
61 return (mbedtls_ecp_keypair *) (pk).MBEDTLS_PRIVATE(pk_ctx);
62 default:
63 return NULL;
64 }
65}
66#endif /* MBEDTLS_ECP_LIGHT */
67
68#endif /* MBEDTLS_PK_INTERNAL_H */