mbed TLS v2.16.7
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: Apache-2.0 OR GPL-2.0-or-later
18  *
19  * This file is provided under the Apache License 2.0, or the
20  * GNU General Public License v2.0 or later.
21  *
22  * **********
23  * Apache License 2.0:
24  *
25  * Licensed under the Apache License, Version 2.0 (the "License"); you may
26  * not use this file except in compliance with the License.
27  * You may obtain a copy of the License at
28  *
29  * http://www.apache.org/licenses/LICENSE-2.0
30  *
31  * Unless required by applicable law or agreed to in writing, software
32  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
33  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34  * See the License for the specific language governing permissions and
35  * limitations under the License.
36  *
37  * **********
38  *
39  * **********
40  * GNU General Public License v2.0 or later:
41  *
42  * This program is free software; you can redistribute it and/or modify
43  * it under the terms of the GNU General Public License as published by
44  * the Free Software Foundation; either version 2 of the License, or
45  * (at your option) any later version.
46  *
47  * This program is distributed in the hope that it will be useful,
48  * but WITHOUT ANY WARRANTY; without even the implied warranty of
49  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
50  * GNU General Public License for more details.
51  *
52  * You should have received a copy of the GNU General Public License along
53  * with this program; if not, write to the Free Software Foundation, Inc.,
54  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
55  *
56  * **********
57  *
58  * This file is part of Mbed TLS (https://tls.mbed.org)
59  */
60 
61 #ifndef MBEDTLS_ECDH_H
62 #define MBEDTLS_ECDH_H
63 
64 #if !defined(MBEDTLS_CONFIG_FILE)
65 #include "config.h"
66 #else
67 #include MBEDTLS_CONFIG_FILE
68 #endif
69 
70 #include "ecp.h"
71 
72 /*
73  * Use a backward compatible ECDH context.
74  *
75  * This flag is always enabled for now and future versions might add a
76  * configuration option that conditionally undefines this flag.
77  * The configuration option in question may have a different name.
78  *
79  * Features undefining this flag, must have a warning in their description in
80  * config.h stating that the feature breaks backward compatibility.
81  */
82 #define MBEDTLS_ECDH_LEGACY_CONTEXT
83 
84 #ifdef __cplusplus
85 extern "C" {
86 #endif
87 
91 typedef enum
92 {
96 
97 #if !defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
98 
104 typedef enum
105 {
106  MBEDTLS_ECDH_VARIANT_NONE = 0,
107  MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0,
108 } mbedtls_ecdh_variant;
109 
117 typedef struct mbedtls_ecdh_context_mbed
118 {
119  mbedtls_ecp_group grp;
120  mbedtls_mpi d;
122  mbedtls_ecp_point Qp;
123  mbedtls_mpi z;
124 #if defined(MBEDTLS_ECP_RESTARTABLE)
126 #endif
127 } mbedtls_ecdh_context_mbed;
128 #endif
129 
137 typedef struct mbedtls_ecdh_context
138 {
139 #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
149 #if defined(MBEDTLS_ECP_RESTARTABLE)
150  int restart_enabled;
152 #endif /* MBEDTLS_ECP_RESTARTABLE */
153 #else
154  uint8_t point_format;
156  mbedtls_ecp_group_id grp_id;
157  mbedtls_ecdh_variant var;
158  union
159  {
160  mbedtls_ecdh_context_mbed mbed_ecdh;
161  } ctx;
164 #if defined(MBEDTLS_ECP_RESTARTABLE)
165  uint8_t restart_enabled;
170 #endif /* MBEDTLS_ECP_RESTARTABLE */
171 #endif /* MBEDTLS_ECDH_LEGACY_CONTEXT */
172 }
174 
201  int (*f_rng)(void *, unsigned char *, size_t),
202  void *p_rng );
203 
239  const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
240  int (*f_rng)(void *, unsigned char *, size_t),
241  void *p_rng );
242 
249 
267  mbedtls_ecp_group_id grp_id );
268 
277 
303 int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen,
304  unsigned char *buf, size_t blen,
305  int (*f_rng)(void *, unsigned char *, size_t),
306  void *p_rng );
307 
333  const unsigned char **buf,
334  const unsigned char *end );
335 
356  const mbedtls_ecp_keypair *key,
357  mbedtls_ecdh_side side );
358 
385 int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen,
386  unsigned char *buf, size_t blen,
387  int (*f_rng)(void *, unsigned char *, size_t),
388  void *p_rng );
389 
410  const unsigned char *buf, size_t blen );
411 
442 int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
443  unsigned char *buf, size_t blen,
444  int (*f_rng)(void *, unsigned char *, size_t),
445  void *p_rng );
446 
447 #if defined(MBEDTLS_ECP_RESTARTABLE)
448 
460 void mbedtls_ecdh_enable_restart( mbedtls_ecdh_context *ctx );
461 #endif /* MBEDTLS_ECP_RESTARTABLE */
462 
463 #ifdef __cplusplus
464 }
465 #endif
466 
467 #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:144
This file provides an API for Elliptic Curves over GF(P) (ECP).
mbedtls_mpi d
Definition: ecdh.h:141
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:359
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:148
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:200
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:146
mbedtls_ecp_group grp
Definition: ecdh.h:140
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:91
void mbedtls_ecp_restart_ctx
Definition: ecp.h:347
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:104
struct mbedtls_ecdh_context mbedtls_ecdh_context
The ECDH context structure.
The ECDH context structure.
Definition: ecdh.h:137
mbedtls_ecp_point Qp
Definition: ecdh.h:143
mbedtls_ecp_point Q
Definition: ecdh.h:142
mbedtls_ecp_point Vf
Definition: ecdh.h:147
MPI structure.
Definition: bignum.h:212
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:151
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.