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 | |
Valerio Setti | 483738e | 2023-05-17 15:37:29 +0200 | [diff] [blame] | 26 | #include "mbedtls/pk.h" |
| 27 | |
Valerio Setti | 229bf10 | 2023-05-15 11:13:55 +0200 | [diff] [blame] | 28 | #if defined(MBEDTLS_ECP_LIGHT) |
| 29 | #include "mbedtls/ecp.h" |
| 30 | #endif |
| 31 | |
Valerio Setti | 483738e | 2023-05-17 15:37:29 +0200 | [diff] [blame] | 32 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 33 | #include "psa/crypto.h" |
| 34 | #endif |
| 35 | |
Valerio Setti | 229bf10 | 2023-05-15 11:13:55 +0200 | [diff] [blame] | 36 | #if defined(MBEDTLS_ECP_LIGHT) |
| 37 | /** |
| 38 | * 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] | 39 | * 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 Setti | 229bf10 | 2023-05-15 11:13:55 +0200 | [diff] [blame] | 41 | * structure (pk_context) fields. |
| 42 | * 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] | 43 | * 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] | 44 | * 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 | */ |
| 49 | static 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 Setti | f70b3e0 | 2023-05-15 12:57:40 +0200 | [diff] [blame] | 55 | return (const mbedtls_ecp_keypair *) (pk).MBEDTLS_PRIVATE(pk_ctx); |
Valerio Setti | 229bf10 | 2023-05-15 11:13:55 +0200 | [diff] [blame] | 56 | default: |
| 57 | return NULL; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | static 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 Setti | 4064dbb | 2023-05-17 15:33:07 +0200 | [diff] [blame] | 72 | |
| 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 | |
| 77 | static 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 Setti | 229bf10 | 2023-05-15 11:13:55 +0200 | [diff] [blame] | 92 | #endif /* MBEDTLS_ECP_LIGHT */ |
| 93 | |
Valerio Setti | 483738e | 2023-05-17 15:37:29 +0200 | [diff] [blame] | 94 | #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 | */ |
| 105 | int 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 Setti | 229bf10 | 2023-05-15 11:13:55 +0200 | [diff] [blame] | 109 | #endif /* MBEDTLS_PK_INTERNAL_H */ |