blob: e707558a87987500a4fe78733cf150bafdbe8888 [file] [log] [blame]
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +01001/**
2 * \file ecdh.h
3 *
4 * \brief Elliptic curve Diffie-Hellman
5 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02006 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02007 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010020 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000021 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010022 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#ifndef MBEDTLS_ECDH_H
24#define MBEDTLS_ECDH_H
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010025
Manuel Pégourié-Gonnardbdc96762013-10-03 11:50:39 +020026#include "ecp.h"
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010027
Paul Bakker407a0da2013-06-27 14:29:21 +020028#ifdef __cplusplus
29extern "C" {
30#endif
31
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010032/**
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +010033 * When importing from an EC key, select if it is our key or the peer's key
34 */
35typedef enum
36{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037 MBEDTLS_ECDH_OURS,
38 MBEDTLS_ECDH_THEIRS,
39} mbedtls_ecdh_side;
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +010040
41/**
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010042 * \brief ECDH context structure
43 */
44typedef struct
45{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046 mbedtls_ecp_group grp; /*!< elliptic curve used */
47 mbedtls_mpi d; /*!< our secret value (private key) */
48 mbedtls_ecp_point Q; /*!< our public value (public key) */
49 mbedtls_ecp_point Qp; /*!< peer's public value (public key) */
50 mbedtls_mpi z; /*!< shared secret */
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +020051 int point_format; /*!< format for point export in TLS messages */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052 mbedtls_ecp_point Vi; /*!< blinding value (for later) */
53 mbedtls_ecp_point Vf; /*!< un-blinding value (for later) */
54 mbedtls_mpi _d; /*!< previous d (for later) */
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020055#if defined(MBEDTLS_ECP_RESTARTABLE)
56 mbedtls_ecp_restart_ctx rs; /*!< restart context for EC computations */
57#endif
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010058}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059mbedtls_ecdh_context;
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010060
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010061/**
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +020062 * \brief Generate a public key.
63 * Raw function that only does the core computation.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010064 *
65 * \param grp ECP group
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +020066 * \param d Destination MPI (secret exponent, aka private key)
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010067 * \param Q Destination point (public key)
68 * \param f_rng RNG function
69 * \param p_rng RNG parameter
70 *
71 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072 * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
Manuel Pégourié-Gonnardc90d3b02017-04-27 10:48:29 +020073 * MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
74 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010075 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076int mbedtls_ecdh_gen_public( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010077 int (*f_rng)(void *, unsigned char *, size_t),
78 void *p_rng );
79
80/**
81 * \brief Compute shared secret
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +020082 * Raw function that only does the core computation.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010083 *
84 * \param grp ECP group
85 * \param z Destination MPI (shared secret)
86 * \param Q Public key from other party
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +020087 * \param d Our secret exponent (private key)
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +020088 * \param f_rng RNG function (see notes)
89 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010090 *
91 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092 * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
Manuel Pégourié-Gonnardc90d3b02017-04-27 10:48:29 +020093 * MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
94 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +020095 *
96 * \note If f_rng is not NULL, it is used to implement
97 * countermeasures against potential elaborate timing
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 * attacks, see \c mbedtls_ecp_mul() for details.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010099 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z,
101 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200102 int (*f_rng)(void *, unsigned char *, size_t),
103 void *p_rng );
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100104
105/**
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100106 * \brief Initialize context
107 *
108 * \param ctx Context to initialize
109 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx );
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100111
112/**
113 * \brief Free context
114 *
115 * \param ctx Context to free
116 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx );
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100118
119/**
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +0200120 * \brief Generate a public key and a TLS ServerKeyExchange payload.
121 * (First function used by a TLS server for ECDHE.)
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100122 *
123 * \param ctx ECDH context
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100124 * \param olen number of chars written
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200125 * \param buf destination buffer
126 * \param blen length of buffer
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100127 * \param f_rng RNG function
128 * \param p_rng RNG parameter
129 *
130 * \note This function assumes that ctx->grp has already been
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200131 * properly set (for example using mbedtls_ecp_group_load).
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100132 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 * \return 0 if successful, or an MBEDTLS_ERR_ECP_XXX error code
Manuel Pégourié-Gonnardc90d3b02017-04-27 10:48:29 +0200134 * MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
135 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100136 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100138 unsigned char *buf, size_t blen,
139 int (*f_rng)(void *, unsigned char *, size_t),
140 void *p_rng );
141
142/**
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +0200143 * \brief Parse and procress a TLS ServerKeyExhange payload.
144 * (First function used by a TLS client for ECDHE.)
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100145 *
146 * \param ctx ECDH context
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200147 * \param buf pointer to start of input buffer
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100148 * \param end one past end of buffer
149 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 * \return 0 if successful, or an MBEDTLS_ERR_ECP_XXX error code
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100151 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx,
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100153 const unsigned char **buf, const unsigned char *end );
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100154
155/**
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +0200156 * \brief Setup an ECDH context from an EC key.
157 * (Used by clients and servers in place of the
158 * ServerKeyEchange for static ECDH: import ECDH parameters
159 * from a certificate's EC key information.)
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100160 *
161 * \param ctx ECDH constext to set
162 * \param key EC key to use
Paul Bakkera36d23e2013-12-30 17:57:27 +0100163 * \param side Is it our key (1) or the peer's key (0) ?
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100164 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 * \return 0 if successful, or an MBEDTLS_ERR_ECP_XXX error code
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100166 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx, const mbedtls_ecp_keypair *key,
168 mbedtls_ecdh_side side );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100169
170/**
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +0200171 * \brief Generate a public key and a TLS ClientKeyExchange payload.
172 * (Second function used by a TLS client for ECDH(E).)
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100173 *
174 * \param ctx ECDH context
175 * \param olen number of bytes actually written
176 * \param buf destination buffer
177 * \param blen size of destination buffer
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200178 * \param f_rng RNG function
179 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100180 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 * \return 0 if successful, or an MBEDTLS_ERR_ECP_XXX error code
Manuel Pégourié-Gonnardc90d3b02017-04-27 10:48:29 +0200182 * MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
183 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100184 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100186 unsigned char *buf, size_t blen,
187 int (*f_rng)(void *, unsigned char *, size_t),
188 void *p_rng );
189
190/**
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +0200191 * \brief Parse and process a TLS ClientKeyExchange payload.
192 * (Second function used by a TLS server for ECDH(E).)
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100193 *
194 * \param ctx ECDH context
195 * \param buf start of input buffer
196 * \param blen length of input buffer
197 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 * \return 0 if successful, or an MBEDTLS_ERR_ECP_XXX error code
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100199 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100201 const unsigned char *buf, size_t blen );
202
203/**
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +0200204 * \brief Derive and export the shared secret.
205 * (Last function used by both TLS client en servers.)
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100206 *
207 * \param ctx ECDH context
208 * \param olen number of bytes written
209 * \param buf destination buffer
210 * \param blen buffer length
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 * \param f_rng RNG function, see notes for \c mbedtls_ecdh_compute_shared()
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200212 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100213 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 * \return 0 if successful, or an MBEDTLS_ERR_ECP_XXX error code
Manuel Pégourié-Gonnardc90d3b02017-04-27 10:48:29 +0200215 * MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
216 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100217 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200219 unsigned char *buf, size_t blen,
220 int (*f_rng)(void *, unsigned char *, size_t),
221 void *p_rng );
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100222
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100223#ifdef __cplusplus
224}
225#endif
226
Paul Bakker9af723c2014-05-01 13:03:14 +0200227#endif /* ecdh.h */