blob: a8ff5b941100b1623454320c571c7c974e7595b9 [file] [log] [blame]
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +01001/*
2 * Elliptic curve Diffie-Hellman
3 *
Bence Szépkúti44bfbe32020-08-19 16:54:51 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkúti4e9f7122020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010024 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010045 */
46
47/*
48 * References:
49 *
50 * SEC1 http://www.secg.org/index.php?action=secg,docs_secg
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010051 * RFC 4492
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010052 */
53
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020056#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020058#endif
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#if defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010061
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000062#include "mbedtls/ecdh.h"
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010063
Rich Evans00ab4702015-02-06 13:43:58 +000064#include <string.h>
65
Ron Eldora84c1cb2017-10-10 19:04:27 +030066#if !defined(MBEDTLS_ECDH_GEN_PUBLIC_ALT)
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010067/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068 * Generate public key: simple wrapper around mbedtls_ecp_gen_keypair
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010069 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070int mbedtls_ecdh_gen_public( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010071 int (*f_rng)(void *, unsigned char *, size_t),
72 void *p_rng )
73{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074 return mbedtls_ecp_gen_keypair( grp, d, Q, f_rng, p_rng );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010075}
Ron Eldora84c1cb2017-10-10 19:04:27 +030076#endif /* MBEDTLS_ECDH_GEN_PUBLIC_ALT */
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010077
Ron Eldora84c1cb2017-10-10 19:04:27 +030078#if !defined(MBEDTLS_ECDH_COMPUTE_SHARED_ALT)
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010079/*
80 * Compute shared secret (SEC1 3.3.1)
81 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z,
83 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +020084 int (*f_rng)(void *, unsigned char *, size_t),
85 void *p_rng )
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010086{
87 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 mbedtls_ecp_point P;
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090 mbedtls_ecp_point_init( &P );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010091
92 /*
93 * Make sure Q is a valid pubkey before using it
94 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095 MBEDTLS_MPI_CHK( mbedtls_ecp_check_pubkey( grp, Q ) );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( grp, &P, d, Q, f_rng, p_rng ) );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 if( mbedtls_ecp_is_zero( &P ) )
Paul Bakkerb548d772013-07-26 14:21:34 +0200100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101 ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Paul Bakkerb548d772013-07-26 14:21:34 +0200102 goto cleanup;
103 }
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( z, &P.X ) );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100106
107cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108 mbedtls_ecp_point_free( &P );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100109
110 return( ret );
111}
Ron Eldora84c1cb2017-10-10 19:04:27 +0300112#endif /* MBEDTLS_ECDH_COMPUTE_SHARED_ALT */
113
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100114/*
115 * Initialize context
116 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx )
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100118{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 memset( ctx, 0, sizeof( mbedtls_ecdh_context ) );
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100120}
121
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100122/*
123 * Free context
124 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx )
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100126{
127 if( ctx == NULL )
128 return;
129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 mbedtls_ecp_group_free( &ctx->grp );
131 mbedtls_ecp_point_free( &ctx->Q );
132 mbedtls_ecp_point_free( &ctx->Qp );
133 mbedtls_ecp_point_free( &ctx->Vi );
134 mbedtls_ecp_point_free( &ctx->Vf );
135 mbedtls_mpi_free( &ctx->d );
136 mbedtls_mpi_free( &ctx->z );
137 mbedtls_mpi_free( &ctx->_d );
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100138}
139
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100140/*
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100141 * Setup and write the ServerKeyExhange parameters (RFC 4492)
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100142 * struct {
143 * ECParameters curve_params;
144 * ECPoint public;
145 * } ServerECDHParams;
146 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100148 unsigned char *buf, size_t blen,
149 int (*f_rng)(void *, unsigned char *, size_t),
150 void *p_rng )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100151{
152 int ret;
153 size_t grp_len, pt_len;
154
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100155 if( ctx == NULL || ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 if( ( ret = mbedtls_ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100159 != 0 )
160 return( ret );
161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 if( ( ret = mbedtls_ecp_tls_write_group( &ctx->grp, &grp_len, buf, blen ) )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100163 != 0 )
164 return( ret );
165
166 buf += grp_len;
167 blen -= grp_len;
168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169 if( ( ret = mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, ctx->point_format,
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100170 &pt_len, buf, blen ) ) != 0 )
171 return( ret );
172
173 *olen = grp_len + pt_len;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200174 return( 0 );
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100175}
176
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100177/*
178 * Read the ServerKeyExhange parameters (RFC 4492)
179 * struct {
180 * ECParameters curve_params;
181 * ECPoint public;
182 * } ServerECDHParams;
183 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx,
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100185 const unsigned char **buf, const unsigned char *end )
186{
187 int ret;
188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 if( ( ret = mbedtls_ecp_tls_read_group( &ctx->grp, buf, end - *buf ) ) != 0 )
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100190 return( ret );
191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192 if( ( ret = mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, buf, end - *buf ) )
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100193 != 0 )
194 return( ret );
195
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200196 return( 0 );
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100197}
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100198
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100199/*
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100200 * Get parameters from a keypair
201 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx, const mbedtls_ecp_keypair *key,
203 mbedtls_ecdh_side side )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100204{
205 int ret;
206
Gilles Peskinef58078c2018-11-07 22:10:59 +0100207 if( ctx->grp.id == MBEDTLS_ECP_DP_NONE )
208 {
209 /* This is the first call to get_params(). Copy the group information
210 * into the context. */
211 if( ( ret = mbedtls_ecp_group_copy( &ctx->grp, &key->grp ) ) != 0 )
212 return( ret );
213 }
214 else
215 {
216 /* This is not the first call to get_params(). Check that the group
217 * is the same as the first time. */
218 if( ctx->grp.id != key->grp.id )
219 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
220 }
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100221
222 /* If it's not our key, just import the public part as Qp */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 if( side == MBEDTLS_ECDH_THEIRS )
224 return( mbedtls_ecp_copy( &ctx->Qp, &key->Q ) );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100225
226 /* Our key: import public (as Q) and private parts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227 if( side != MBEDTLS_ECDH_OURS )
228 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 if( ( ret = mbedtls_ecp_copy( &ctx->Q, &key->Q ) ) != 0 ||
231 ( ret = mbedtls_mpi_copy( &ctx->d, &key->d ) ) != 0 )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100232 return( ret );
233
234 return( 0 );
235}
236
237/*
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100238 * Setup and export the client public value
239 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100241 unsigned char *buf, size_t blen,
242 int (*f_rng)(void *, unsigned char *, size_t),
243 void *p_rng )
244{
245 int ret;
246
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100247 if( ctx == NULL || ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 if( ( ret = mbedtls_ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) )
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100251 != 0 )
252 return( ret );
253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 return mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, ctx->point_format,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100255 olen, buf, blen );
256}
257
258/*
259 * Parse and import the client's public value
260 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100262 const unsigned char *buf, size_t blen )
263{
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100264 int ret;
265 const unsigned char *p = buf;
266
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100267 if( ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 if( ( ret = mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, &p, blen ) ) != 0 )
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100271 return( ret );
272
273 if( (size_t)( p - buf ) != blen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100275
276 return( 0 );
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100277}
278
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100279/*
280 * Derive and export the shared secret
281 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200283 unsigned char *buf, size_t blen,
284 int (*f_rng)(void *, unsigned char *, size_t),
285 void *p_rng )
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100286{
287 int ret;
288
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100289 if( ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 if( ( ret = mbedtls_ecdh_compute_shared( &ctx->grp, &ctx->z, &ctx->Qp, &ctx->d,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200293 f_rng, p_rng ) ) != 0 )
294 {
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100295 return( ret );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200296 }
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 if( mbedtls_mpi_size( &ctx->z ) > blen )
299 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Paul Bakker41c83d32013-03-20 14:39:14 +0100300
Manuel Pégourié-Gonnard0a56c2c2014-01-17 21:24:04 +0100301 *olen = ctx->grp.pbits / 8 + ( ( ctx->grp.pbits % 8 ) != 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 return mbedtls_mpi_write_binary( &ctx->z, buf, *olen );
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100303}
304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305#endif /* MBEDTLS_ECDH_C */