blob: 52496c6e1cb06fdca221f51201a502b40faf80a3 [file] [log] [blame]
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +01001/*
2 * ECDH with curve-optimized implementation multiplexing
3 *
4 * Copyright 2016-2018 INRIA and Microsoft Corporation
5 * 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.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22#if !defined(MBEDTLS_CONFIG_FILE)
23#include "mbedtls/config.h"
24#else
25#include MBEDTLS_CONFIG_FILE
26#endif
27
28#if defined(MBEDTLS_ECDH_C)
29
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +000030#include <mbedtls/ecdh.h>
31
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +010032#include <Hacl_Curve25519.h>
33#include <mbedtls/platform_util.h>
34
35#include "x25519.h"
36
37#include <string.h>
38
39/*
40 * Initialize context
41 */
42void mbedtls_x25519_init( mbedtls_x25519_context *ctx )
43{
44 memset( ctx, 0, sizeof( mbedtls_x25519_context ) );
45}
46
47/*
48 * Free context
49 */
50void mbedtls_x25519_free( mbedtls_x25519_context *ctx )
51{
52 if( ctx == NULL )
53 return;
54
Christoph M. Wintersteigerfb779f12019-02-15 16:20:54 +000055 mbedtls_platform_zeroize( ctx->our_secret, MBEDTLS_X25519_KEY_SIZE_BYTES );
56 mbedtls_platform_zeroize( ctx->peer_point, MBEDTLS_X25519_KEY_SIZE_BYTES );
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +010057}
58
59int mbedtls_x25519_make_params( mbedtls_x25519_context *ctx, size_t *olen,
60 unsigned char *buf, size_t blen,
61 int( *f_rng )(void *, unsigned char *, size_t),
62 void *p_rng )
63{
64 int ret = 0;
65
Christoph M. Wintersteigerfb779f12019-02-15 16:20:54 +000066 uint8_t base[MBEDTLS_X25519_KEY_SIZE_BYTES] = {0};
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +010067
Christoph M. Wintersteigerfb779f12019-02-15 16:20:54 +000068 if( ( ret = f_rng( p_rng, ctx->our_secret, MBEDTLS_X25519_KEY_SIZE_BYTES ) ) != 0 )
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +010069 return ret;
70
71 *olen = 36;
72 if( blen < *olen )
73 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
74
75 *buf++ = MBEDTLS_ECP_TLS_NAMED_CURVE;
76 *buf++ = MBEDTLS_ECP_TLS_CURVE25519 >> 8;
77 *buf++ = MBEDTLS_ECP_TLS_CURVE25519 & 0xFF;
Christoph M. Wintersteigerfb779f12019-02-15 16:20:54 +000078 *buf++ = MBEDTLS_X25519_KEY_SIZE_BYTES;
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +010079
80 base[0] = 9;
81 Hacl_Curve25519_crypto_scalarmult( buf, ctx->our_secret, base );
82
83 base[0] = 0;
Christoph M. Wintersteigerfb779f12019-02-15 16:20:54 +000084 if( memcmp( buf, base, MBEDTLS_X25519_KEY_SIZE_BYTES) == 0 )
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +010085 return MBEDTLS_ERR_ECP_RANDOM_FAILED;
86
87 return( 0 );
88}
89
90int mbedtls_x25519_read_params( mbedtls_x25519_context *ctx,
91 const unsigned char **buf, const unsigned char *end )
92{
93 if( end - *buf < 33 )
94 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
95
Christoph M. Wintersteigerfb779f12019-02-15 16:20:54 +000096 if( ( *(*buf)++ != MBEDTLS_X25519_KEY_SIZE_BYTES ) )
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +010097 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
98
Christoph M. Wintersteigerfb779f12019-02-15 16:20:54 +000099 memcpy( ctx->peer_point, *buf, MBEDTLS_X25519_KEY_SIZE_BYTES );
100 *buf += MBEDTLS_X25519_KEY_SIZE_BYTES;
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +0100101 return( 0 );
102}
103
104int mbedtls_x25519_get_params( mbedtls_x25519_context *ctx, const mbedtls_ecp_keypair *key,
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +0000105 mbedtls_x25519_ecdh_side side )
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +0100106{
107 size_t olen = 0;
108
109 switch( side ) {
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +0000110 case MBEDTLS_X25519_ECDH_THEIRS:
Christoph M. Wintersteigerfb779f12019-02-15 16:20:54 +0000111 mbedtls_ecp_point_write_binary( &key->grp, &key->Q, MBEDTLS_ECP_PF_COMPRESSED, &olen, ctx->peer_point, MBEDTLS_X25519_KEY_SIZE_BYTES );
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +0100112 /* untested; defensively throw an error for now. */
113 return(MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE);
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +0000114 case MBEDTLS_X25519_ECDH_OURS:
Christoph M. Wintersteigerfb779f12019-02-15 16:20:54 +0000115 mbedtls_mpi_write_binary( &key->d, ctx->our_secret, MBEDTLS_X25519_KEY_SIZE_BYTES );
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +0100116 /* CMW: key->Q = key->d * base; do we need to set up ctx.peer_point here? */
117 /* untested; defensively throw an error for now. */
118 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
119 default:
120 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
121 }
122}
123
124int mbedtls_x25519_calc_secret( mbedtls_x25519_context *ctx, size_t *olen,
125 unsigned char *buf, size_t blen,
126 int( *f_rng )(void *, unsigned char *, size_t),
127 void *p_rng )
128{
129 /* CMW: Is it okay that f_rng, p_rng are not used? */
130 (( void )f_rng);
131 (( void )p_rng);
132
Christoph M. Wintersteigerfb779f12019-02-15 16:20:54 +0000133 *olen = MBEDTLS_X25519_KEY_SIZE_BYTES;
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +0100134
135 if( blen < *olen )
136 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
137
138 Hacl_Curve25519_crypto_scalarmult( buf, ctx->our_secret, ctx->peer_point);
139
140 /* Wipe the DH secret and don't let the peer chose a small subgroup point */
Christoph M. Wintersteigerfb779f12019-02-15 16:20:54 +0000141 memset( ctx->our_secret, 0, MBEDTLS_X25519_KEY_SIZE_BYTES );
142 if( memcmp( buf, ctx->our_secret, MBEDTLS_X25519_KEY_SIZE_BYTES) == 0 )
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +0100143 return MBEDTLS_ERR_ECP_RANDOM_FAILED;
144
145 return( 0 );
146}
147
148int mbedtls_x25519_make_public( mbedtls_x25519_context *ctx, size_t *olen,
149 unsigned char *buf, size_t blen,
150 int( *f_rng )(void *, unsigned char *, size_t),
151 void *p_rng )
152{
Christoph M. Wintersteigerfb779f12019-02-15 16:20:54 +0000153 unsigned char base[MBEDTLS_X25519_KEY_SIZE_BYTES] = { 0 };
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +0100154
155 /* CMW: Is it okay that f_rng, p_rng are not used? */
156 (( void )f_rng);
157 (( void )p_rng);
158
159 if( ctx == NULL )
160 return(MBEDTLS_ERR_ECP_BAD_INPUT_DATA);
161
162 *olen = 33;
163 if( blen < *olen )
164 return(MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL);
Christoph M. Wintersteigerfb779f12019-02-15 16:20:54 +0000165 *buf++ = MBEDTLS_X25519_KEY_SIZE_BYTES;
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +0100166
167 base[0] = 9;
168 Hacl_Curve25519_crypto_scalarmult( buf, ctx->our_secret, base );
169
170 base[0] = 0;
Christoph M. Wintersteigerfb779f12019-02-15 16:20:54 +0000171 if( memcmp( buf, base, MBEDTLS_X25519_KEY_SIZE_BYTES ) == 0 )
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +0100172 return MBEDTLS_ERR_ECP_RANDOM_FAILED;
173
174 return(0);
175}
176
177int mbedtls_x25519_read_public( mbedtls_x25519_context *ctx,
178 const unsigned char *buf, size_t blen )
179{
180 if( blen < 33 )
181 return(MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL);
Christoph M. Wintersteigerfb779f12019-02-15 16:20:54 +0000182 if( (*buf++ != MBEDTLS_X25519_KEY_SIZE_BYTES) )
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +0100183 return(MBEDTLS_ERR_ECP_BAD_INPUT_DATA);
Christoph M. Wintersteigerfb779f12019-02-15 16:20:54 +0000184 memcpy( ctx->peer_point, buf, MBEDTLS_X25519_KEY_SIZE_BYTES );
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +0100185 return(0);
186}
187
188
189#endif /* MBEDTLS_ECDH_C */