blob: b2859c4b6ec77d98563b68d98d2cb6e6011e6a81 [file] [log] [blame]
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +01001/*
2 * Elliptic curve Diffie-Hellman
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010020 */
21
22/*
23 * References:
24 *
25 * SEC1 http://www.secg.org/index.php?action=secg,docs_secg
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010026 * RFC 4492
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010027 */
28
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020033#endif
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010036
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000037#include "mbedtls/ecdh.h"
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010038
Rich Evans00ab4702015-02-06 13:43:58 +000039#include <string.h>
40
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010041/*
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020042 * Generate public key (restartable version)
43 */
44static int ecdh_gen_public_restartable( mbedtls_ecp_group *grp,
45 mbedtls_mpi *d, mbedtls_ecp_point *Q,
46 int (*f_rng)(void *, unsigned char *, size_t),
47 void *p_rng,
48 mbedtls_ecp_restart_ctx *rs_ctx )
49{
50 int ret;
51
52 /* If multiplication is in progress, we already generated a privkey */
53#if defined(MBEDTLS_ECP_RESTARTABLE)
54 if( rs_ctx == NULL || rs_ctx->rsm == NULL )
55#endif
56 MBEDTLS_MPI_CHK( mbedtls_ecp_gen_privkey( grp, d, f_rng, p_rng ) );
57
58 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_restartable( grp, Q, d, &grp->G,
59 f_rng, p_rng, rs_ctx ) );
60
61cleanup:
62 return( ret );
63}
64
65/*
66 * Generate public key
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010067 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068int mbedtls_ecdh_gen_public( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010069 int (*f_rng)(void *, unsigned char *, size_t),
70 void *p_rng )
71{
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020072 return( ecdh_gen_public_restartable( grp, d, Q, f_rng, p_rng, NULL ) );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010073}
74
75/*
76 * Compute shared secret (SEC1 3.3.1)
77 */
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020078static int ecdh_compute_shared_restartable( mbedtls_ecp_group *grp,
79 mbedtls_mpi *z,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +020081 int (*f_rng)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020082 void *p_rng,
83 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010084{
85 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086 mbedtls_ecp_point P;
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 mbedtls_ecp_point_init( &P );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010089
90 /*
91 * Make sure Q is a valid pubkey before using it
92 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093 MBEDTLS_MPI_CHK( mbedtls_ecp_check_pubkey( grp, Q ) );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010094
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020095 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_restartable( grp, &P, d, Q,
96 f_rng, p_rng, rs_ctx ) );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 if( mbedtls_ecp_is_zero( &P ) )
Paul Bakkerb548d772013-07-26 14:21:34 +020099 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Paul Bakkerb548d772013-07-26 14:21:34 +0200101 goto cleanup;
102 }
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( z, &P.X ) );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100105
106cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 mbedtls_ecp_point_free( &P );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100108
109 return( ret );
110}
111
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100112/*
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200113 * Compute shared secret (SEC1 3.3.1)
114 */
115int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z,
116 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
117 int (*f_rng)(void *, unsigned char *, size_t),
118 void *p_rng )
119{
120 return( ecdh_compute_shared_restartable( grp, z, Q, d,
121 f_rng, p_rng, NULL ) );
122}
123
124/*
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100125 * Initialize context
126 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx )
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100128{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 memset( ctx, 0, sizeof( mbedtls_ecdh_context ) );
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200130
131#if defined(MBEDTLS_ECP_RESTARTABLE)
132 mbedtls_ecp_restart_init( &ctx->rs );
133#endif
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100134}
135
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100136/*
137 * Free context
138 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx )
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100140{
141 if( ctx == NULL )
142 return;
143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 mbedtls_ecp_group_free( &ctx->grp );
145 mbedtls_ecp_point_free( &ctx->Q );
146 mbedtls_ecp_point_free( &ctx->Qp );
147 mbedtls_ecp_point_free( &ctx->Vi );
148 mbedtls_ecp_point_free( &ctx->Vf );
149 mbedtls_mpi_free( &ctx->d );
150 mbedtls_mpi_free( &ctx->z );
151 mbedtls_mpi_free( &ctx->_d );
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200152
153#if defined(MBEDTLS_ECP_RESTARTABLE)
154 mbedtls_ecp_restart_free( &ctx->rs );
155#endif
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100156}
157
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100158/*
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100159 * Setup and write the ServerKeyExhange parameters (RFC 4492)
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100160 * struct {
161 * ECParameters curve_params;
162 * ECPoint public;
163 * } ServerECDHParams;
164 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100166 unsigned char *buf, size_t blen,
167 int (*f_rng)(void *, unsigned char *, size_t),
168 void *p_rng )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100169{
170 int ret;
171 size_t grp_len, pt_len;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200172 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100173
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100174 if( ctx == NULL || ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100176
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200177#if defined(MBEDTLS_ECP_RESTARTABLE)
178 rs_ctx = &ctx->rs;
179#endif
180
181 if( ( ret = ecdh_gen_public_restartable( &ctx->grp, &ctx->d, &ctx->Q,
182 f_rng, p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100183 return( ret );
184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 if( ( ret = mbedtls_ecp_tls_write_group( &ctx->grp, &grp_len, buf, blen ) )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100186 != 0 )
187 return( ret );
188
189 buf += grp_len;
190 blen -= grp_len;
191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192 if( ( ret = mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, ctx->point_format,
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100193 &pt_len, buf, blen ) ) != 0 )
194 return( ret );
195
196 *olen = grp_len + pt_len;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200197 return( 0 );
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100198}
199
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100200/*
201 * Read the ServerKeyExhange parameters (RFC 4492)
202 * struct {
203 * ECParameters curve_params;
204 * ECPoint public;
205 * } ServerECDHParams;
206 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx,
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100208 const unsigned char **buf, const unsigned char *end )
209{
210 int ret;
211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212 if( ( ret = mbedtls_ecp_tls_read_group( &ctx->grp, buf, end - *buf ) ) != 0 )
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100213 return( ret );
214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 if( ( ret = mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, buf, end - *buf ) )
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100216 != 0 )
217 return( ret );
218
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200219 return( 0 );
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100220}
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100221
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100222/*
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100223 * Get parameters from a keypair
224 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx, const mbedtls_ecp_keypair *key,
226 mbedtls_ecdh_side side )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100227{
228 int ret;
229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 if( ( ret = mbedtls_ecp_group_copy( &ctx->grp, &key->grp ) ) != 0 )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100231 return( ret );
232
233 /* If it's not our key, just import the public part as Qp */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 if( side == MBEDTLS_ECDH_THEIRS )
235 return( mbedtls_ecp_copy( &ctx->Qp, &key->Q ) );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100236
237 /* Our key: import public (as Q) and private parts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238 if( side != MBEDTLS_ECDH_OURS )
239 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 if( ( ret = mbedtls_ecp_copy( &ctx->Q, &key->Q ) ) != 0 ||
242 ( ret = mbedtls_mpi_copy( &ctx->d, &key->d ) ) != 0 )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100243 return( ret );
244
245 return( 0 );
246}
247
248/*
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100249 * Setup and export the client public value
250 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100252 unsigned char *buf, size_t blen,
253 int (*f_rng)(void *, unsigned char *, size_t),
254 void *p_rng )
255{
256 int ret;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200257 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100258
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100259 if( ctx == NULL || ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100261
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200262#if defined(MBEDTLS_ECP_RESTARTABLE)
263 rs_ctx = &ctx->rs;
264#endif
265
266 if( ( ret = ecdh_gen_public_restartable( &ctx->grp, &ctx->d, &ctx->Q,
267 f_rng, p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100268 return( ret );
269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 return mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, ctx->point_format,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100271 olen, buf, blen );
272}
273
274/*
275 * Parse and import the client's public value
276 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100278 const unsigned char *buf, size_t blen )
279{
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100280 int ret;
281 const unsigned char *p = buf;
282
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100283 if( ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 if( ( ret = mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, &p, blen ) ) != 0 )
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100287 return( ret );
288
289 if( (size_t)( p - buf ) != blen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100291
292 return( 0 );
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100293}
294
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100295/*
296 * Derive and export the shared secret
297 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200299 unsigned char *buf, size_t blen,
300 int (*f_rng)(void *, unsigned char *, size_t),
301 void *p_rng )
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100302{
303 int ret;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200304 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100305
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200306 if( ctx == NULL || ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100308
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200309#if defined(MBEDTLS_ECP_RESTARTABLE)
310 rs_ctx = &ctx->rs;
311#endif
312
313 if( ( ret = ecdh_compute_shared_restartable( &ctx->grp,
314 &ctx->z, &ctx->Qp, &ctx->d, f_rng, p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200315 {
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100316 return( ret );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200317 }
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 if( mbedtls_mpi_size( &ctx->z ) > blen )
320 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Paul Bakker41c83d32013-03-20 14:39:14 +0100321
Manuel Pégourié-Gonnard0a56c2c2014-01-17 21:24:04 +0100322 *olen = ctx->grp.pbits / 8 + ( ( ctx->grp.pbits % 8 ) != 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 return mbedtls_mpi_write_binary( &ctx->z, buf, *olen );
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100324}
325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326#endif /* MBEDTLS_ECDH_C */