blob: b66cb58676902b2cad10dd38c3de2ef8c5c1f698 [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
Ron Eldor433f39c2017-06-18 17:57:51 +030041#if !defined(MBEDTLS_ECDH_ALT)
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010042/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043 * Generate public key: simple wrapper around mbedtls_ecp_gen_keypair
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010044 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045int mbedtls_ecdh_gen_public( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010046 int (*f_rng)(void *, unsigned char *, size_t),
47 void *p_rng )
48{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049 return mbedtls_ecp_gen_keypair( grp, d, Q, f_rng, p_rng );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010050}
51
52/*
53 * Compute shared secret (SEC1 3.3.1)
54 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z,
56 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +020057 int (*f_rng)(void *, unsigned char *, size_t),
58 void *p_rng )
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010059{
60 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061 mbedtls_ecp_point P;
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063 mbedtls_ecp_point_init( &P );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010064
65 /*
66 * Make sure Q is a valid pubkey before using it
67 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068 MBEDTLS_MPI_CHK( mbedtls_ecp_check_pubkey( grp, Q ) );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( grp, &P, d, Q, f_rng, p_rng ) );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072 if( mbedtls_ecp_is_zero( &P ) )
Paul Bakkerb548d772013-07-26 14:21:34 +020073 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074 ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Paul Bakkerb548d772013-07-26 14:21:34 +020075 goto cleanup;
76 }
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( z, &P.X ) );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010079
80cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081 mbedtls_ecp_point_free( &P );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010082
83 return( ret );
84}
Ron Eldor433f39c2017-06-18 17:57:51 +030085#endif /* MBEDTLS_ECDH_ALT */
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010086/*
87 * Initialize context
88 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx )
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010090{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091 memset( ctx, 0, sizeof( mbedtls_ecdh_context ) );
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010092}
93
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010094/*
95 * Free context
96 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx )
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010098{
99 if( ctx == NULL )
100 return;
101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 mbedtls_ecp_group_free( &ctx->grp );
103 mbedtls_ecp_point_free( &ctx->Q );
104 mbedtls_ecp_point_free( &ctx->Qp );
105 mbedtls_ecp_point_free( &ctx->Vi );
106 mbedtls_ecp_point_free( &ctx->Vf );
107 mbedtls_mpi_free( &ctx->d );
108 mbedtls_mpi_free( &ctx->z );
109 mbedtls_mpi_free( &ctx->_d );
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100110}
111
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100112/*
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100113 * Setup and write the ServerKeyExhange parameters (RFC 4492)
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100114 * struct {
115 * ECParameters curve_params;
116 * ECPoint public;
117 * } ServerECDHParams;
118 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100120 unsigned char *buf, size_t blen,
121 int (*f_rng)(void *, unsigned char *, size_t),
122 void *p_rng )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100123{
124 int ret;
125 size_t grp_len, pt_len;
126
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100127 if( ctx == NULL || ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 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 +0100131 != 0 )
132 return( ret );
133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 if( ( ret = mbedtls_ecp_tls_write_group( &ctx->grp, &grp_len, buf, blen ) )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100135 != 0 )
136 return( ret );
137
138 buf += grp_len;
139 blen -= grp_len;
140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 if( ( ret = mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, ctx->point_format,
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100142 &pt_len, buf, blen ) ) != 0 )
143 return( ret );
144
145 *olen = grp_len + pt_len;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200146 return( 0 );
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100147}
148
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100149/*
150 * Read the ServerKeyExhange parameters (RFC 4492)
151 * struct {
152 * ECParameters curve_params;
153 * ECPoint public;
154 * } ServerECDHParams;
155 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx,
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100157 const unsigned char **buf, const unsigned char *end )
158{
159 int ret;
160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 if( ( ret = mbedtls_ecp_tls_read_group( &ctx->grp, buf, end - *buf ) ) != 0 )
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100162 return( ret );
163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 if( ( ret = mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, buf, end - *buf ) )
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100165 != 0 )
166 return( ret );
167
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200168 return( 0 );
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100169}
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100170
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100171/*
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100172 * Get parameters from a keypair
173 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx, const mbedtls_ecp_keypair *key,
175 mbedtls_ecdh_side side )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100176{
177 int ret;
178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 if( ( ret = mbedtls_ecp_group_copy( &ctx->grp, &key->grp ) ) != 0 )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100180 return( ret );
181
182 /* If it's not our key, just import the public part as Qp */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 if( side == MBEDTLS_ECDH_THEIRS )
184 return( mbedtls_ecp_copy( &ctx->Qp, &key->Q ) );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100185
186 /* Our key: import public (as Q) and private parts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 if( side != MBEDTLS_ECDH_OURS )
188 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 if( ( ret = mbedtls_ecp_copy( &ctx->Q, &key->Q ) ) != 0 ||
191 ( ret = mbedtls_mpi_copy( &ctx->d, &key->d ) ) != 0 )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100192 return( ret );
193
194 return( 0 );
195}
196
197/*
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100198 * Setup and export the client public value
199 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100201 unsigned char *buf, size_t blen,
202 int (*f_rng)(void *, unsigned char *, size_t),
203 void *p_rng )
204{
205 int ret;
206
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100207 if( ctx == NULL || ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 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 +0100211 != 0 )
212 return( ret );
213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 return mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, ctx->point_format,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100215 olen, buf, blen );
216}
217
218/*
219 * Parse and import the client's public value
220 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100222 const unsigned char *buf, size_t blen )
223{
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100224 int ret;
225 const unsigned char *p = buf;
226
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100227 if( ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 if( ( ret = mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, &p, blen ) ) != 0 )
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100231 return( ret );
232
233 if( (size_t)( p - buf ) != blen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100235
236 return( 0 );
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100237}
238
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100239/*
240 * Derive and export the shared secret
241 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200243 unsigned char *buf, size_t blen,
244 int (*f_rng)(void *, unsigned char *, size_t),
245 void *p_rng )
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100246{
247 int ret;
248
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100249 if( ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 if( ( ret = mbedtls_ecdh_compute_shared( &ctx->grp, &ctx->z, &ctx->Qp, &ctx->d,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200253 f_rng, p_rng ) ) != 0 )
254 {
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100255 return( ret );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200256 }
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 if( mbedtls_mpi_size( &ctx->z ) > blen )
259 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Paul Bakker41c83d32013-03-20 14:39:14 +0100260
Manuel Pégourié-Gonnard0a56c2c2014-01-17 21:24:04 +0100261 *olen = ctx->grp.pbits / 8 + ( ( ctx->grp.pbits % 8 ) != 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 return mbedtls_mpi_write_binary( &ctx->z, buf, *olen );
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100263}
264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265#endif /* MBEDTLS_ECDH_C */