blob: 99cfde00d001e55a869d72e0df66808a841cfdc5 [file] [log] [blame]
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +01001/**
2 * \file ecdh.h
3 *
Rose Zadikde2d6222018-01-25 21:57:43 +00004 * \brief The Elliptic Curve Diffie-Hellman (ECDH) protocol APIs.
5 *
6 * ECDH is an anonymous key agreement protocol allowing two parties to
7 * establish a shared secret over an insecure channel. Each party must have an
8 * elliptic-curve public–private key pair.
9 *
10 * For more information, see <em>NIST SP 800-56A Rev. 2: Recommendation for
11 * Pair-Wise Key Establishment Schemes Using Discrete Logarithm
12 * Cryptography</em>.
Darryl Greena40a1012018-01-05 15:33:17 +000013 */
14/*
Rose Zadikde2d6222018-01-25 21:57:43 +000015 * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020016 * SPDX-License-Identifier: Apache-2.0
17 *
18 * Licensed under the Apache License, Version 2.0 (the "License"); you may
19 * not use this file except in compliance with the License.
20 * You may obtain a copy of the License at
21 *
22 * http://www.apache.org/licenses/LICENSE-2.0
23 *
24 * Unless required by applicable law or agreed to in writing, software
25 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
26 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27 * See the License for the specific language governing permissions and
28 * limitations under the License.
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010029 *
Rose Zadikde2d6222018-01-25 21:57:43 +000030 * This file is part of Mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010031 */
Rose Zadikde2d6222018-01-25 21:57:43 +000032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#ifndef MBEDTLS_ECDH_H
34#define MBEDTLS_ECDH_H
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010035
Manuel Pégourié-Gonnardbdc96762013-10-03 11:50:39 +020036#include "ecp.h"
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010037
Paul Bakker407a0da2013-06-27 14:29:21 +020038#ifdef __cplusplus
39extern "C" {
40#endif
41
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010042/**
Rose Zadikde2d6222018-01-25 21:57:43 +000043 * Defines the source of the imported EC key:
44 * <ul><li>Our key.</li>
45 * <li>The key of the peer.</li></ul>
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +010046 */
47typedef enum
48{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049 MBEDTLS_ECDH_OURS,
50 MBEDTLS_ECDH_THEIRS,
51} mbedtls_ecdh_side;
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +010052
53/**
Rose Zadikde2d6222018-01-25 21:57:43 +000054 * \brief The ECDH context structure.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010055 */
56typedef struct
57{
Rose Zadikde2d6222018-01-25 21:57:43 +000058 mbedtls_ecp_group grp; /*!< The elliptic curve used. */
59 mbedtls_mpi d; /*!< The private key. */
60 mbedtls_ecp_point Q; /*!< The public key. */
61 mbedtls_ecp_point Qp; /*!< The value of the public key of the peer. */
62 mbedtls_mpi z; /*!< The shared secret. */
63 int point_format; /*!< The format of point export in TLS messages. */
64 mbedtls_ecp_point Vi; /*!< The blinding value. */
65 mbedtls_ecp_point Vf; /*!< The unblinding value. */
66 mbedtls_mpi _d; /*!< The previous \p d. */
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010067}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068mbedtls_ecdh_context;
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010069
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010070/**
Rose Zadikde2d6222018-01-25 21:57:43 +000071 * \brief This function generates an ECDH keypair on an elliptic
72 * curve.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010073 *
Rose Zadikde2d6222018-01-25 21:57:43 +000074 * This function performs the first of two core computations
75 * implemented during the ECDH key exchange. The second core
76 * computation is performed by mbedtls_ecdh_compute_shared().
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010077 *
Rose Zadikde2d6222018-01-25 21:57:43 +000078 * \param grp The ECP group.
79 * \param d The destination MPI (private key).
80 * \param Q The destination point (public key).
81 * \param f_rng The RNG function.
82 * \param p_rng The RNG parameter.
83 *
84 * \return \c 0 on success, or an \c MBEDTLS_ERR_ECP_XXX or
85 * \c MBEDTLS_MPI_XXX error code on failure.
86 *
87 * \see ecp.h
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010088 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089int mbedtls_ecdh_gen_public( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010090 int (*f_rng)(void *, unsigned char *, size_t),
91 void *p_rng );
92
93/**
Rose Zadikde2d6222018-01-25 21:57:43 +000094 * \brief This function computes the shared secret.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010095 *
Rose Zadikde2d6222018-01-25 21:57:43 +000096 * This function performs the second of two core computations
97 * implemented during the ECDH key exchange. The first core
98 * computation is performed by mbedtls_ecdh_gen_public().
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010099 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000100 * \param grp The ECP group.
101 * \param z The destination MPI (shared secret).
102 * \param Q The public key from another party.
103 * \param d Our secret exponent (private key).
104 * \param f_rng The RNG function.
105 * \param p_rng The RNG parameter.
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200106 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000107 * \return \c 0 on success, or an \c MBEDTLS_ERR_ECP_XXX or
108 * \c MBEDTLS_MPI_XXX error code on failure.
109 *
110 * \see ecp.h
111 *
112 * \note If \p f_rng is not NULL, it is used to implement
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200113 * countermeasures against potential elaborate timing
Rose Zadikde2d6222018-01-25 21:57:43 +0000114 * attacks. For more information, see mbedtls_ecp_mul().
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100115 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z,
117 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200118 int (*f_rng)(void *, unsigned char *, size_t),
119 void *p_rng );
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100120
121/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000122 * \brief This function initializes an ECDH context.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100123 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000124 * \param ctx The ECDH context to initialize.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100125 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx );
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100127
128/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000129 * \brief This function frees a context.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100130 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000131 * \param ctx The context to free.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100132 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx );
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100134
135/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000136 * \brief This function generates a public key and a TLS
137 * ServerKeyExchange payload.
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100138 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000139 * This is the first function used by a TLS server for ECDHE
140 * ciphersuites.
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100141 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000142 * \param ctx The ECDH context.
143 * \param olen The number of characters written.
144 * \param buf The destination buffer.
145 * \param blen The length of the destination buffer.
146 * \param f_rng The RNG function.
147 * \param p_rng The RNG parameter.
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100148 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000149 * \note This function assumes that the ECP group (grp) of the
150 * \p ctx context has already been properly set,
151 * for example, using mbedtls_ecp_group_load().
152 *
153 * \return \c 0 on success, or an \c MBEDTLS_ERR_ECP_XXX error code
154 * on failure.
155 *
156 * \see ecp.h
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100157 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100159 unsigned char *buf, size_t blen,
160 int (*f_rng)(void *, unsigned char *, size_t),
161 void *p_rng );
162
163/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000164 * \brief This function parses and processes a TLS ServerKeyExhange
165 * payload.
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100166 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000167 * This is the first function used by a TLS client for ECDHE
168 * ciphersuites.
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100169 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000170 * \param ctx The ECDH context.
171 * \param buf The pointer to the start of the input buffer.
172 * \param end The address for one Byte past the end of the buffer.
173 *
174 * \return \c 0 on success, or an \c MBEDTLS_ERR_ECP_XXX error code
175 * on failure.
176 *
177 * \see ecp.h
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100178 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx,
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100180 const unsigned char **buf, const unsigned char *end );
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100181
182/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000183 * \brief This function sets up an ECDH context from an EC key.
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100184 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000185 * It is used by clients and servers in place of the
186 * ServerKeyEchange for static ECDH, and imports ECDH
187 * parameters from the EC key information of a certificate.
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100188 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000189 * \param ctx The ECDH context to set up.
190 * \param key The EC key to use.
191 * \param side Defines the source of the key:
192 * <ul><li>1: Our key.</li>
193 <li>0: The key of the peer.</li></ul>
194 *
195 * \return \c 0 on success, or an \c MBEDTLS_ERR_ECP_XXX error code
196 * on failure.
197 *
198 * \see ecp.h
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100199 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx, const mbedtls_ecp_keypair *key,
201 mbedtls_ecdh_side side );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100202
203/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000204 * \brief This function generates a public key and a TLS
205 * ClientKeyExchange payload.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100206 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000207 * This is the second function used by a TLS client for ECDH(E)
208 * ciphersuites.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100209 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000210 * \param ctx The ECDH context.
211 * \param olen The number of Bytes written.
212 * \param buf The destination buffer.
213 * \param blen The size of the destination buffer.
214 * \param f_rng The RNG function.
215 * \param p_rng The RNG parameter.
216 *
217 * \return \c 0 on success, or an \c MBEDTLS_ERR_ECP_XXX error code
218 * on failure.
219 *
220 * \see ecp.h
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100221 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100223 unsigned char *buf, size_t blen,
224 int (*f_rng)(void *, unsigned char *, size_t),
225 void *p_rng );
226
227/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000228 * \brief This function parses and processes a TLS ClientKeyExchange
229 * payload.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100230 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000231 * This is the second function used by a TLS server for ECDH(E)
232 * ciphersuites.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100233 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000234 * \param ctx The ECDH context.
235 * \param buf The start of the input buffer.
236 * \param blen The length of the input buffer.
237 *
238 * \return \c 0 on success, or an \c MBEDTLS_ERR_ECP_XXX error code
239 * on failure.
240 *
241 * \see ecp.h
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100242 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100244 const unsigned char *buf, size_t blen );
245
246/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000247 * \brief This function derives and exports the shared secret.
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100248 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000249 * This is the last function used by both TLS client
250 * and servers.
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100251 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000252 * \param ctx The ECDH context.
253 * \param olen The number of Bytes written.
254 * \param buf The destination buffer.
255 * \param blen The length of the destination buffer.
256 * \param f_rng The RNG function.
257 * \param p_rng The RNG parameter.
258 *
259 * \return \c 0 on success, or an \c MBEDTLS_ERR_ECP_XXX error code
260 * on failure.
261 *
262 * \see ecp.h
263 *
264 * \note If \p f_rng is not NULL, it is used to implement
265 * countermeasures against potential elaborate timing
266 * attacks. For more information, see mbedtls_ecp_mul().
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100267 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200269 unsigned char *buf, size_t blen,
270 int (*f_rng)(void *, unsigned char *, size_t),
271 void *p_rng );
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100272
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100273#ifdef __cplusplus
274}
275#endif
276
Paul Bakker9af723c2014-05-01 13:03:14 +0200277#endif /* ecdh.h */