mbed TLS v2.16.6
ecdh.h
Go to the documentation of this file.
1 
15 /*
16  * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
17  * SPDX-License-Identifier: GPL-2.0
18  *
19  * This program is free software; you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation; either version 2 of the License, or
22  * (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License along
30  * with this program; if not, write to the Free Software Foundation, Inc.,
31  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
32  *
33  * This file is part of Mbed TLS (https://tls.mbed.org)
34  */
35 
36 #ifndef MBEDTLS_ECDH_H
37 #define MBEDTLS_ECDH_H
38 
39 #if !defined(MBEDTLS_CONFIG_FILE)
40 #include "config.h"
41 #else
42 #include MBEDTLS_CONFIG_FILE
43 #endif
44 
45 #include "ecp.h"
46 
47 /*
48  * Use a backward compatible ECDH context.
49  *
50  * This flag is always enabled for now and future versions might add a
51  * configuration option that conditionally undefines this flag.
52  * The configuration option in question may have a different name.
53  *
54  * Features undefining this flag, must have a warning in their description in
55  * config.h stating that the feature breaks backward compatibility.
56  */
57 #define MBEDTLS_ECDH_LEGACY_CONTEXT
58 
59 #ifdef __cplusplus
60 extern "C" {
61 #endif
62 
66 typedef enum
67 {
71 
72 #if !defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
73 
79 typedef enum
80 {
81  MBEDTLS_ECDH_VARIANT_NONE = 0,
82  MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0,
83 } mbedtls_ecdh_variant;
84 
92 typedef struct mbedtls_ecdh_context_mbed
93 {
94  mbedtls_ecp_group grp;
95  mbedtls_mpi d;
98  mbedtls_mpi z;
99 #if defined(MBEDTLS_ECP_RESTARTABLE)
101 #endif
102 } mbedtls_ecdh_context_mbed;
103 #endif
104 
112 typedef struct mbedtls_ecdh_context
113 {
114 #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
124 #if defined(MBEDTLS_ECP_RESTARTABLE)
125  int restart_enabled;
127 #endif /* MBEDTLS_ECP_RESTARTABLE */
128 #else
129  uint8_t point_format;
131  mbedtls_ecp_group_id grp_id;
132  mbedtls_ecdh_variant var;
133  union
134  {
135  mbedtls_ecdh_context_mbed mbed_ecdh;
136  } ctx;
139 #if defined(MBEDTLS_ECP_RESTARTABLE)
140  uint8_t restart_enabled;
145 #endif /* MBEDTLS_ECP_RESTARTABLE */
146 #endif /* MBEDTLS_ECDH_LEGACY_CONTEXT */
147 }
149 
176  int (*f_rng)(void *, unsigned char *, size_t),
177  void *p_rng );
178 
214  const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
215  int (*f_rng)(void *, unsigned char *, size_t),
216  void *p_rng );
217 
224 
242  mbedtls_ecp_group_id grp_id );
243 
252 
278 int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen,
279  unsigned char *buf, size_t blen,
280  int (*f_rng)(void *, unsigned char *, size_t),
281  void *p_rng );
282 
308  const unsigned char **buf,
309  const unsigned char *end );
310 
331  const mbedtls_ecp_keypair *key,
332  mbedtls_ecdh_side side );
333 
360 int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen,
361  unsigned char *buf, size_t blen,
362  int (*f_rng)(void *, unsigned char *, size_t),
363  void *p_rng );
364 
385  const unsigned char *buf, size_t blen );
386 
417 int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
418  unsigned char *buf, size_t blen,
419  int (*f_rng)(void *, unsigned char *, size_t),
420  void *p_rng );
421 
422 #if defined(MBEDTLS_ECP_RESTARTABLE)
423 
435 void mbedtls_ecdh_enable_restart( mbedtls_ecdh_context *ctx );
436 #endif /* MBEDTLS_ECP_RESTARTABLE */
437 
438 #ifdef __cplusplus
439 }
440 #endif
441 
442 #endif /* ecdh.h */
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 ...
mbedtls_mpi z
Definition: ecdh.h:119
This file provides an API for Elliptic Curves over GF(P) (ECP).
mbedtls_mpi d
Definition: ecdh.h:116
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.
The ECP key-pair structure.
Definition: ecp.h:334
void mbedtls_ecdh_free(mbedtls_ecdh_context *ctx)
This function frees a context.
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.
Configuration options (set of defines)
mbedtls_mpi _d
Definition: ecdh.h:123
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.
The ECP group structure.
Definition: ecp.h:175
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.
mbedtls_ecp_point Vi
Definition: ecdh.h:121
mbedtls_ecp_group grp
Definition: ecdh.h:115
void mbedtls_ecdh_init(mbedtls_ecdh_context *ctx)
This function initializes an ECDH context.
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.
mbedtls_ecdh_side
Definition: ecdh.h:66
void mbedtls_ecp_restart_ctx
Definition: ecp.h:322
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.
mbedtls_ecp_group_id
Definition: ecp.h:79
struct mbedtls_ecdh_context mbedtls_ecdh_context
The ECDH context structure.
The ECDH context structure.
Definition: ecdh.h:112
mbedtls_ecp_point Qp
Definition: ecdh.h:118
mbedtls_ecp_point Q
Definition: ecdh.h:117
mbedtls_ecp_point Vf
Definition: ecdh.h:122
MPI structure.
Definition: bignum.h:187
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.
The ECP point structure, in Jacobian coordinates.
Definition: ecp.h:126
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.