Valerio Setti | 229bf10 | 2023-05-15 11:13:55 +0200 | [diff] [blame] | 1 | /** |
| 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 |
Valerio Setti | 4ac9d44 | 2023-05-17 12:32:13 +0200 | [diff] [blame] | 33 | * wrapped ecp_keypair structure pointed to the pk_ctx. However this is not |
| 34 | * ideal because it bypasses the PK module on the control of its internal |
Valerio Setti | 229bf10 | 2023-05-15 11:13:55 +0200 | [diff] [blame] | 35 | * structure (pk_context) fields. |
| 36 | * For backward compatibility we keep mbedtls_pk_ec() when ECP_C is defined, but |
Valerio Setti | 4ac9d44 | 2023-05-17 12:32:13 +0200 | [diff] [blame] | 37 | * we provide 2 very similar functions when only ECP_LIGHT is enabled and not |
Valerio Setti | 229bf10 | 2023-05-15 11:13:55 +0200 | [diff] [blame] | 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 | */ |
| 43 | static 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 Setti | f70b3e0 | 2023-05-15 12:57:40 +0200 | [diff] [blame] | 49 | return (const mbedtls_ecp_keypair *) (pk).MBEDTLS_PRIVATE(pk_ctx); |
Valerio Setti | 229bf10 | 2023-05-15 11:13:55 +0200 | [diff] [blame] | 50 | default: |
| 51 | return NULL; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | static 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 | } |
Valerio Setti | 4064dbb | 2023-05-17 15:33:07 +0200 | [diff] [blame^] | 66 | |
| 67 | /* Helpers for Montgomery curves */ |
| 68 | #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) |
| 69 | #define MBEDTLS_PK_HAVE_RFC8410_CURVES |
| 70 | |
| 71 | static inline int mbedtls_pk_is_rfc8410_curve(mbedtls_ecp_group_id id) |
| 72 | { |
| 73 | #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) |
| 74 | if (id == MBEDTLS_ECP_DP_CURVE25519) { |
| 75 | return 1; |
| 76 | } |
| 77 | #endif |
| 78 | #if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) |
| 79 | if (id == MBEDTLS_ECP_DP_CURVE448) { |
| 80 | return 1; |
| 81 | } |
| 82 | #endif |
| 83 | return 0; |
| 84 | } |
| 85 | #endif /* MBEDTLS_ECP_DP_CURVE25519_ENABLED || MBEDTLS_ECP_DP_CURVE448_ENABLED */ |
Valerio Setti | 229bf10 | 2023-05-15 11:13:55 +0200 | [diff] [blame] | 86 | #endif /* MBEDTLS_ECP_LIGHT */ |
| 87 | |
| 88 | #endif /* MBEDTLS_PK_INTERNAL_H */ |