blob: 70455e8c749680f24a2ce32d3644b26601ae6240 [file] [log] [blame]
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +01001/**
2 * \file ecdh.h
3 *
Rose Zadik68993282018-03-27 11:12:25 +01004 * \brief This file contains ECDH definitions and functions.
5 *
6 * The Elliptic Curve Diffie-Hellman (ECDH) protocol is an anonymous
7 * key agreement protocol allowing two parties to establish a shared
8 * secret over an insecure channel. Each party must have an
Rose Zadikde2d6222018-01-25 21:57:43 +00009 * elliptic-curve public–private key pair.
10 *
11 * For more information, see <em>NIST SP 800-56A Rev. 2: Recommendation for
12 * Pair-Wise Key Establishment Schemes Using Discrete Logarithm
13 * Cryptography</em>.
Darryl Greena40a1012018-01-05 15:33:17 +000014 */
15/*
Rose Zadikde2d6222018-01-25 21:57:43 +000016 * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020017 * 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.
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010030 *
Rose Zadikde2d6222018-01-25 21:57:43 +000031 * This file is part of Mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010032 */
Rose Zadikde2d6222018-01-25 21:57:43 +000033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#ifndef MBEDTLS_ECDH_H
35#define MBEDTLS_ECDH_H
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010036
Manuel Pégourié-Gonnardbdc96762013-10-03 11:50:39 +020037#include "ecp.h"
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010038
Paul Bakker407a0da2013-06-27 14:29:21 +020039#ifdef __cplusplus
40extern "C" {
41#endif
42
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010043/**
Rose Zadik68993282018-03-27 11:12:25 +010044 * Defines the source of the imported EC key.
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +010045 */
46typedef enum
47{
Rose Zadik68993282018-03-27 11:12:25 +010048 MBEDTLS_ECDH_OURS, /**< Our key. */
49 MBEDTLS_ECDH_THEIRS, /**< The key of the peer. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050} mbedtls_ecdh_side;
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +010051
52/**
Rose Zadikde2d6222018-01-25 21:57:43 +000053 * \brief The ECDH context structure.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010054 */
55typedef struct
56{
Rose Zadikde2d6222018-01-25 21:57:43 +000057 mbedtls_ecp_group grp; /*!< The elliptic curve used. */
58 mbedtls_mpi d; /*!< The private key. */
59 mbedtls_ecp_point Q; /*!< The public key. */
60 mbedtls_ecp_point Qp; /*!< The value of the public key of the peer. */
61 mbedtls_mpi z; /*!< The shared secret. */
62 int point_format; /*!< The format of point export in TLS messages. */
63 mbedtls_ecp_point Vi; /*!< The blinding value. */
64 mbedtls_ecp_point Vf; /*!< The unblinding value. */
65 mbedtls_mpi _d; /*!< The previous \p d. */
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010066}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067mbedtls_ecdh_context;
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010068
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010069/**
Rose Zadikde2d6222018-01-25 21:57:43 +000070 * \brief This function generates an ECDH keypair on an elliptic
71 * curve.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010072 *
Rose Zadikde2d6222018-01-25 21:57:43 +000073 * This function performs the first of two core computations
74 * implemented during the ECDH key exchange. The second core
75 * computation is performed by mbedtls_ecdh_compute_shared().
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010076 *
Rose Zadik68993282018-03-27 11:12:25 +010077 * \see ecp.h
78 *
Rose Zadikde2d6222018-01-25 21:57:43 +000079 * \param grp The ECP group.
80 * \param d The destination MPI (private key).
81 * \param Q The destination point (public key).
82 * \param f_rng The RNG function.
Rose Zadik68993282018-03-27 11:12:25 +010083 * \param p_rng The RNG context.
Rose Zadikde2d6222018-01-25 21:57:43 +000084 *
Rose Zadik68993282018-03-27 11:12:25 +010085 * \return \c 0 on success.
86 * \return An \c MBEDTLS_ERR_ECP_XXX or
Rose Zadikde2d6222018-01-25 21:57:43 +000087 * \c MBEDTLS_MPI_XXX error code on failure.
88 *
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010089 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090int mbedtls_ecdh_gen_public( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010091 int (*f_rng)(void *, unsigned char *, size_t),
92 void *p_rng );
93
94/**
Rose Zadikde2d6222018-01-25 21:57:43 +000095 * \brief This function computes the shared secret.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010096 *
Rose Zadikde2d6222018-01-25 21:57:43 +000097 * This function performs the second of two core computations
98 * implemented during the ECDH key exchange. The first core
99 * computation is performed by mbedtls_ecdh_gen_public().
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100100 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000101 * \see ecp.h
102 *
103 * \note If \p f_rng is not NULL, it is used to implement
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200104 * countermeasures against potential elaborate timing
Rose Zadikde2d6222018-01-25 21:57:43 +0000105 * attacks. For more information, see mbedtls_ecp_mul().
Rose Zadik68993282018-03-27 11:12:25 +0100106 *
107 * \param grp The ECP group.
108 * \param z The destination MPI (shared secret).
109 * \param Q The public key from another party.
110 * \param d Our secret exponent (private key).
111 * \param f_rng The RNG function.
112 * \param p_rng The RNG context.
113 *
114 * \return \c 0 on success.
115 * \return An \c MBEDTLS_ERR_ECP_XXX or
116 * \c MBEDTLS_MPI_XXX error code on failure.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100117 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z,
119 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200120 int (*f_rng)(void *, unsigned char *, size_t),
121 void *p_rng );
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100122
123/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000124 * \brief This function initializes an ECDH context.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100125 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000126 * \param ctx The ECDH context to initialize.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100127 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx );
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100129
130/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000131 * \brief This function frees a context.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100132 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000133 * \param ctx The context to free.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100134 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx );
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100136
137/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000138 * \brief This function generates a public key and a TLS
139 * ServerKeyExchange payload.
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100140 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000141 * This is the first function used by a TLS server for ECDHE
142 * ciphersuites.
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100143 *
Rose Zadik68993282018-03-27 11:12:25 +0100144 * \note This function assumes that the ECP group (grp) of the
145 * \p ctx context has already been properly set,
146 * for example, using mbedtls_ecp_group_load().
147 *
148 * \see ecp.h
149 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000150 * \param ctx The ECDH context.
151 * \param olen The number of characters written.
152 * \param buf The destination buffer.
153 * \param blen The length of the destination buffer.
154 * \param f_rng The RNG function.
Rose Zadik68993282018-03-27 11:12:25 +0100155 * \param p_rng The RNG context.
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100156 *
Rose Zadik68993282018-03-27 11:12:25 +0100157 * \return \c 0 on success.
158 * \return An \c MBEDTLS_ERR_ECP_XXX error code on failure.
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100159 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100161 unsigned char *buf, size_t blen,
162 int (*f_rng)(void *, unsigned char *, size_t),
163 void *p_rng );
164
165/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000166 * \brief This function parses and processes a TLS ServerKeyExhange
167 * payload.
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100168 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000169 * This is the first function used by a TLS client for ECDHE
170 * ciphersuites.
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100171 *
Rose Zadik68993282018-03-27 11:12:25 +0100172 * \see ecp.h
173 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000174 * \param ctx The ECDH context.
175 * \param buf The pointer to the start of the input buffer.
176 * \param end The address for one Byte past the end of the buffer.
177 *
Rose Zadik68993282018-03-27 11:12:25 +0100178 * \return \c 0 on success.
179 * \return An \c MBEDTLS_ERR_ECP_XXX error code on failure.
Rose Zadikde2d6222018-01-25 21:57:43 +0000180 *
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100181 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx,
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100183 const unsigned char **buf, const unsigned char *end );
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100184
185/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000186 * \brief This function sets up an ECDH context from an EC key.
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100187 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000188 * It is used by clients and servers in place of the
189 * ServerKeyEchange for static ECDH, and imports ECDH
190 * parameters from the EC key information of a certificate.
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100191 *
Rose Zadik68993282018-03-27 11:12:25 +0100192 * \see ecp.h
193 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000194 * \param ctx The ECDH context to set up.
195 * \param key The EC key to use.
Rose Zadik68993282018-03-27 11:12:25 +0100196 * \param side Defines the source of the key: 1: Our key, or
197 * 0: The key of the peer.
Rose Zadikde2d6222018-01-25 21:57:43 +0000198 *
Rose Zadik68993282018-03-27 11:12:25 +0100199 * \return \c 0 on success.
200 * \return An \c MBEDTLS_ERR_ECP_XXX error code on failure.
Rose Zadikde2d6222018-01-25 21:57:43 +0000201 *
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100202 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx, const mbedtls_ecp_keypair *key,
204 mbedtls_ecdh_side side );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100205
206/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000207 * \brief This function generates a public key and a TLS
208 * ClientKeyExchange payload.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100209 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000210 * This is the second function used by a TLS client for ECDH(E)
211 * ciphersuites.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100212 *
Rose Zadik68993282018-03-27 11:12:25 +0100213 * \see ecp.h
214 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000215 * \param ctx The ECDH context.
216 * \param olen The number of Bytes written.
217 * \param buf The destination buffer.
218 * \param blen The size of the destination buffer.
219 * \param f_rng The RNG function.
Rose Zadik68993282018-03-27 11:12:25 +0100220 * \param p_rng The RNG context.
Rose Zadikde2d6222018-01-25 21:57:43 +0000221 *
Rose Zadik68993282018-03-27 11:12:25 +0100222 * \return \c 0 on success.
223 * \return An \c MBEDTLS_ERR_ECP_XXX error code on failure.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100224 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100226 unsigned char *buf, size_t blen,
227 int (*f_rng)(void *, unsigned char *, size_t),
228 void *p_rng );
229
230/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000231 * \brief This function parses and processes a TLS ClientKeyExchange
232 * payload.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100233 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000234 * This is the second function used by a TLS server for ECDH(E)
235 * ciphersuites.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100236 *
Rose Zadik68993282018-03-27 11:12:25 +0100237 * \see ecp.h
238 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000239 * \param ctx The ECDH context.
240 * \param buf The start of the input buffer.
241 * \param blen The length of the input buffer.
242 *
Rose Zadik68993282018-03-27 11:12:25 +0100243 * \return \c 0 on success.
244 * \return An \c MBEDTLS_ERR_ECP_XXX error code on failure.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100245 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100247 const unsigned char *buf, size_t blen );
248
249/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000250 * \brief This function derives and exports the shared secret.
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100251 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000252 * This is the last function used by both TLS client
253 * and servers.
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100254 *
Rose Zadik68993282018-03-27 11:12:25 +0100255 * \note If \p f_rng is not NULL, it is used to implement
256 * countermeasures against potential elaborate timing
257 * attacks. For more information, see mbedtls_ecp_mul().
258 *
259 * \see ecp.h
260 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000261 * \param ctx The ECDH context.
262 * \param olen The number of Bytes written.
263 * \param buf The destination buffer.
264 * \param blen The length of the destination buffer.
265 * \param f_rng The RNG function.
Rose Zadik68993282018-03-27 11:12:25 +0100266 * \param p_rng The RNG context.
Rose Zadikde2d6222018-01-25 21:57:43 +0000267 *
Rose Zadik68993282018-03-27 11:12:25 +0100268 * \return \c 0 on success.
269 * \return An \c MBEDTLS_ERR_ECP_XXX error code on failure.
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100270 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200272 unsigned char *buf, size_t blen,
273 int (*f_rng)(void *, unsigned char *, size_t),
274 void *p_rng );
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100275
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100276#ifdef __cplusplus
277}
278#endif
279
Paul Bakker9af723c2014-05-01 13:03:14 +0200280#endif /* ecdh.h */