Mbed TLS v3.5.0
ecdh.h
Go to the documentation of this file.
1
15/*
16 * Copyright The Mbed TLS Contributors
17 * SPDX-License-Identifier: Apache-2.0
18 *
19 * Licensed under the Apache License, Version 2.0 (the "License"); you may
20 * not use this file except in compliance with the License.
21 * You may obtain a copy of the License at
22 *
23 * http://www.apache.org/licenses/LICENSE-2.0
24 *
25 * Unless required by applicable law or agreed to in writing, software
26 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
27 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28 * See the License for the specific language governing permissions and
29 * limitations under the License.
30 */
31
32#ifndef MBEDTLS_ECDH_H
33#define MBEDTLS_ECDH_H
35
36#include "mbedtls/build_info.h"
37
38#include "mbedtls/ecp.h"
39
40/*
41 * Mbed TLS supports two formats for ECDH contexts (#mbedtls_ecdh_context
42 * defined in `ecdh.h`). For most applications, the choice of format makes
43 * no difference, since all library functions can work with either format,
44 * except that the new format is incompatible with MBEDTLS_ECP_RESTARTABLE.
45
46 * The new format used when this option is disabled is smaller
47 * (56 bytes on a 32-bit platform). In future versions of the library, it
48 * will support alternative implementations of ECDH operations.
49 * The new format is incompatible with applications that access
50 * context fields directly and with restartable ECP operations.
51 */
52
53#if defined(MBEDTLS_ECP_RESTARTABLE)
54#define MBEDTLS_ECDH_LEGACY_CONTEXT
55#else
56#undef MBEDTLS_ECDH_LEGACY_CONTEXT
57#endif
58
59#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
60#undef MBEDTLS_ECDH_LEGACY_CONTEXT
61#include "everest/everest.h"
62#endif
63
64#ifdef __cplusplus
65extern "C" {
66#endif
67
71typedef enum {
75
76#if !defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
83typedef enum {
86#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
87 MBEDTLS_ECDH_VARIANT_EVEREST
88#endif
90
104#if defined(MBEDTLS_ECP_RESTARTABLE)
106#endif
108#endif
109
117typedef struct mbedtls_ecdh_context {
118#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
124 int MBEDTLS_PRIVATE(point_format);
128#if defined(MBEDTLS_ECP_RESTARTABLE)
129 int MBEDTLS_PRIVATE(restart_enabled);
131#endif /* MBEDTLS_ECP_RESTARTABLE */
132#else
133 uint8_t MBEDTLS_PRIVATE(point_format);
137 union {
139#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
140 mbedtls_ecdh_context_everest MBEDTLS_PRIVATE(everest_ecdh);
141#endif
142 } MBEDTLS_PRIVATE(ctx);
145#if defined(MBEDTLS_ECP_RESTARTABLE)
146 uint8_t MBEDTLS_PRIVATE(restart_enabled);
151#endif /* MBEDTLS_ECP_RESTARTABLE */
152#endif /* MBEDTLS_ECDH_LEGACY_CONTEXT */
153}
155
164
191 int (*f_rng)(void *, unsigned char *, size_t),
192 void *p_rng);
193
226 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
227 int (*f_rng)(void *, unsigned char *, size_t),
228 void *p_rng);
229
236
254 mbedtls_ecp_group_id grp_id);
255
264
291 unsigned char *buf, size_t blen,
292 int (*f_rng)(void *, unsigned char *, size_t),
293 void *p_rng);
294
320 const unsigned char **buf,
321 const unsigned char *end);
322
343 const mbedtls_ecp_keypair *key,
344 mbedtls_ecdh_side side);
345
373 unsigned char *buf, size_t blen,
374 int (*f_rng)(void *, unsigned char *, size_t),
375 void *p_rng);
376
397 const unsigned char *buf, size_t blen);
398
429 unsigned char *buf, size_t blen,
430 int (*f_rng)(void *, unsigned char *, size_t),
431 void *p_rng);
432
433#if defined(MBEDTLS_ECP_RESTARTABLE)
446void mbedtls_ecdh_enable_restart(mbedtls_ecdh_context *ctx);
447#endif /* MBEDTLS_ECP_RESTARTABLE */
448
449#ifdef __cplusplus
450}
451#endif
452
453#endif /* ecdh.h */
int mbedtls_ecdh_setup(mbedtls_ecdh_context *ctx, mbedtls_ecp_group_id grp_id)
This function sets up the ECDH context with the information given.
int mbedtls_ecdh_get_params(mbedtls_ecdh_context *ctx, const mbedtls_ecp_keypair *key, mbedtls_ecdh_side side)
This function sets up an ECDH context from an EC key.
mbedtls_ecdh_variant
Definition: ecdh.h:83
@ MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0
Definition: ecdh.h:85
@ MBEDTLS_ECDH_VARIANT_NONE
Definition: ecdh.h:84
int mbedtls_ecdh_gen_public(mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
This function generates an ECDH keypair on an elliptic curve.
int mbedtls_ecdh_make_public(mbedtls_ecdh_context *ctx, size_t *olen, unsigned char *buf, size_t blen, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
This function generates a public key and exports it as a TLS ClientKeyExchange payload.
int mbedtls_ecdh_calc_secret(mbedtls_ecdh_context *ctx, size_t *olen, unsigned char *buf, size_t blen, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
This function derives and exports the shared secret.
int mbedtls_ecdh_can_do(mbedtls_ecp_group_id gid)
Check whether a given group can be used for ECDH.
int mbedtls_ecdh_make_params(mbedtls_ecdh_context *ctx, size_t *olen, unsigned char *buf, size_t blen, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
This function generates an EC key pair and exports its in the format used in a TLS ServerKeyExchange ...
void mbedtls_ecdh_free(mbedtls_ecdh_context *ctx)
This function frees a context.
void mbedtls_ecdh_init(mbedtls_ecdh_context *ctx)
This function initializes an ECDH context.
int mbedtls_ecdh_read_public(mbedtls_ecdh_context *ctx, const unsigned char *buf, size_t blen)
This function parses and processes the ECDHE payload of a TLS ClientKeyExchange message.
struct mbedtls_ecdh_context mbedtls_ecdh_context
The ECDH context structure.
int mbedtls_ecdh_compute_shared(mbedtls_ecp_group *grp, mbedtls_mpi *z, const mbedtls_ecp_point *Q, const mbedtls_mpi *d, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
This function computes the shared secret.
struct mbedtls_ecdh_context_mbed mbedtls_ecdh_context_mbed
mbedtls_ecdh_side
Definition: ecdh.h:71
@ MBEDTLS_ECDH_OURS
Definition: ecdh.h:72
@ MBEDTLS_ECDH_THEIRS
Definition: ecdh.h:73
int mbedtls_ecdh_read_params(mbedtls_ecdh_context *ctx, const unsigned char **buf, const unsigned char *end)
This function parses the ECDHE parameters in a TLS ServerKeyExchange handshake message.
This file provides an API for Elliptic Curves over GF(P) (ECP).
void mbedtls_ecp_restart_ctx
Definition: ecp.h:427
mbedtls_ecp_group_id
Definition: ecp.h:113
Build-time configuration info.
Macro wrapper for struct's members.
#define MBEDTLS_PRIVATE(member)
The ECDH context structure.
Definition: ecdh.h:117
The ECP group structure.
Definition: ecp.h:244
The ECP key-pair structure.
Definition: ecp.h:439
The ECP point structure, in Jacobian coordinates.
Definition: ecp.h:169
MPI structure.
Definition: bignum.h:219