blob: 75630bd35686d7cf41c9c6a91956ae710354e03c [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 Eldora84c1cb2017-10-10 19:04:27 +030041#if !defined(MBEDTLS_ECDH_GEN_PUBLIC_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}
Ron Eldora84c1cb2017-10-10 19:04:27 +030051#endif /* MBEDTLS_ECDH_GEN_PUBLIC_ALT */
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010052
Ron Eldora84c1cb2017-10-10 19:04:27 +030053#if !defined(MBEDTLS_ECDH_COMPUTE_SHARED_ALT)
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010054/*
55 * Compute shared secret (SEC1 3.3.1)
56 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z,
58 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +020059 int (*f_rng)(void *, unsigned char *, size_t),
60 void *p_rng )
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010061{
62 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063 mbedtls_ecp_point P;
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065 mbedtls_ecp_point_init( &P );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010066
67 /*
68 * Make sure Q is a valid pubkey before using it
69 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070 MBEDTLS_MPI_CHK( mbedtls_ecp_check_pubkey( grp, Q ) );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( grp, &P, d, Q, f_rng, p_rng ) );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074 if( mbedtls_ecp_is_zero( &P ) )
Paul Bakkerb548d772013-07-26 14:21:34 +020075 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076 ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Paul Bakkerb548d772013-07-26 14:21:34 +020077 goto cleanup;
78 }
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( z, &P.X ) );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010081
82cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083 mbedtls_ecp_point_free( &P );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010084
85 return( ret );
86}
Ron Eldora84c1cb2017-10-10 19:04:27 +030087#endif /* MBEDTLS_ECDH_COMPUTE_SHARED_ALT */
88
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010089/*
90 * Initialize context
91 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx )
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010093{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 memset( ctx, 0, sizeof( mbedtls_ecdh_context ) );
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010095}
96
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010097/*
98 * Free context
99 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx )
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100101{
102 if( ctx == NULL )
103 return;
104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105 mbedtls_ecp_group_free( &ctx->grp );
106 mbedtls_ecp_point_free( &ctx->Q );
107 mbedtls_ecp_point_free( &ctx->Qp );
108 mbedtls_ecp_point_free( &ctx->Vi );
109 mbedtls_ecp_point_free( &ctx->Vf );
110 mbedtls_mpi_free( &ctx->d );
111 mbedtls_mpi_free( &ctx->z );
112 mbedtls_mpi_free( &ctx->_d );
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100113}
114
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100115/*
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100116 * Setup and write the ServerKeyExhange parameters (RFC 4492)
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100117 * struct {
118 * ECParameters curve_params;
119 * ECPoint public;
120 * } ServerECDHParams;
121 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100123 unsigned char *buf, size_t blen,
124 int (*f_rng)(void *, unsigned char *, size_t),
125 void *p_rng )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100126{
127 int ret;
128 size_t grp_len, pt_len;
129
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100130 if( ctx == NULL || ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 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 +0100134 != 0 )
135 return( ret );
136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 if( ( ret = mbedtls_ecp_tls_write_group( &ctx->grp, &grp_len, buf, blen ) )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100138 != 0 )
139 return( ret );
140
141 buf += grp_len;
142 blen -= grp_len;
143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 if( ( ret = mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, ctx->point_format,
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100145 &pt_len, buf, blen ) ) != 0 )
146 return( ret );
147
148 *olen = grp_len + pt_len;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200149 return( 0 );
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100150}
151
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100152/*
153 * Read the ServerKeyExhange parameters (RFC 4492)
154 * struct {
155 * ECParameters curve_params;
156 * ECPoint public;
157 * } ServerECDHParams;
158 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx,
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100160 const unsigned char **buf, const unsigned char *end )
161{
162 int ret;
163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 if( ( ret = mbedtls_ecp_tls_read_group( &ctx->grp, buf, end - *buf ) ) != 0 )
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100165 return( ret );
166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 if( ( ret = mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, buf, end - *buf ) )
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100168 != 0 )
169 return( ret );
170
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200171 return( 0 );
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100172}
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100173
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100174/*
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100175 * Get parameters from a keypair
176 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx, const mbedtls_ecp_keypair *key,
178 mbedtls_ecdh_side side )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100179{
180 int ret;
181
Gilles Peskinef58078c2018-11-07 22:10:59 +0100182 if( ctx->grp.id == MBEDTLS_ECP_DP_NONE )
183 {
184 /* This is the first call to get_params(). Copy the group information
185 * into the context. */
186 if( ( ret = mbedtls_ecp_group_copy( &ctx->grp, &key->grp ) ) != 0 )
187 return( ret );
188 }
189 else
190 {
191 /* This is not the first call to get_params(). Check that the group
192 * is the same as the first time. */
193 if( ctx->grp.id != key->grp.id )
194 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
195 }
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100196
197 /* If it's not our key, just import the public part as Qp */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 if( side == MBEDTLS_ECDH_THEIRS )
199 return( mbedtls_ecp_copy( &ctx->Qp, &key->Q ) );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100200
201 /* Our key: import public (as Q) and private parts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 if( side != MBEDTLS_ECDH_OURS )
203 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 if( ( ret = mbedtls_ecp_copy( &ctx->Q, &key->Q ) ) != 0 ||
206 ( ret = mbedtls_mpi_copy( &ctx->d, &key->d ) ) != 0 )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100207 return( ret );
208
209 return( 0 );
210}
211
212/*
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100213 * Setup and export the client public value
214 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100216 unsigned char *buf, size_t blen,
217 int (*f_rng)(void *, unsigned char *, size_t),
218 void *p_rng )
219{
220 int ret;
221
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100222 if( ctx == NULL || ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 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 +0100226 != 0 )
227 return( ret );
228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 return mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, ctx->point_format,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100230 olen, buf, blen );
231}
232
233/*
234 * Parse and import the client's public value
235 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100237 const unsigned char *buf, size_t blen )
238{
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100239 int ret;
240 const unsigned char *p = buf;
241
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100242 if( ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 if( ( ret = mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, &p, blen ) ) != 0 )
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100246 return( ret );
247
248 if( (size_t)( p - buf ) != blen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100250
251 return( 0 );
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100252}
253
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100254/*
255 * Derive and export the shared secret
256 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200258 unsigned char *buf, size_t blen,
259 int (*f_rng)(void *, unsigned char *, size_t),
260 void *p_rng )
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100261{
262 int ret;
263
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100264 if( ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 if( ( ret = mbedtls_ecdh_compute_shared( &ctx->grp, &ctx->z, &ctx->Qp, &ctx->d,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200268 f_rng, p_rng ) ) != 0 )
269 {
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100270 return( ret );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200271 }
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 if( mbedtls_mpi_size( &ctx->z ) > blen )
274 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Paul Bakker41c83d32013-03-20 14:39:14 +0100275
Manuel Pégourié-Gonnard0a56c2c2014-01-17 21:24:04 +0100276 *olen = ctx->grp.pbits / 8 + ( ( ctx->grp.pbits % 8 ) != 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 return mbedtls_mpi_write_binary( &ctx->z, buf, *olen );
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100278}
279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280#endif /* MBEDTLS_ECDH_C */