blob: 7660b6433caf9a1a48beed65dd8ace1093c70862 [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{
Christoph M. Wintersteiger088ef492019-02-15 16:25:48 +000044 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_x25519_context ) );
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +010045}
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. Wintersteiger088ef492019-02-15 16:25:48 +0000141 mbedtls_platform_zeroize( ctx->our_secret, MBEDTLS_X25519_KEY_SIZE_BYTES );
142
Christoph M. Wintersteigerfb779f12019-02-15 16:20:54 +0000143 if( memcmp( buf, ctx->our_secret, MBEDTLS_X25519_KEY_SIZE_BYTES) == 0 )
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +0100144 return MBEDTLS_ERR_ECP_RANDOM_FAILED;
145
146 return( 0 );
147}
148
149int mbedtls_x25519_make_public( mbedtls_x25519_context *ctx, size_t *olen,
150 unsigned char *buf, size_t blen,
151 int( *f_rng )(void *, unsigned char *, size_t),
152 void *p_rng )
153{
Christoph M. Wintersteigerfb779f12019-02-15 16:20:54 +0000154 unsigned char base[MBEDTLS_X25519_KEY_SIZE_BYTES] = { 0 };
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +0100155
156 /* CMW: Is it okay that f_rng, p_rng are not used? */
157 (( void )f_rng);
158 (( void )p_rng);
159
160 if( ctx == NULL )
161 return(MBEDTLS_ERR_ECP_BAD_INPUT_DATA);
162
163 *olen = 33;
164 if( blen < *olen )
165 return(MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL);
Christoph M. Wintersteigerfb779f12019-02-15 16:20:54 +0000166 *buf++ = MBEDTLS_X25519_KEY_SIZE_BYTES;
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +0100167
168 base[0] = 9;
169 Hacl_Curve25519_crypto_scalarmult( buf, ctx->our_secret, base );
170
171 base[0] = 0;
Christoph M. Wintersteigerfb779f12019-02-15 16:20:54 +0000172 if( memcmp( buf, base, MBEDTLS_X25519_KEY_SIZE_BYTES ) == 0 )
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +0100173 return MBEDTLS_ERR_ECP_RANDOM_FAILED;
174
175 return(0);
176}
177
178int mbedtls_x25519_read_public( mbedtls_x25519_context *ctx,
179 const unsigned char *buf, size_t blen )
180{
181 if( blen < 33 )
182 return(MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL);
Christoph M. Wintersteigerfb779f12019-02-15 16:20:54 +0000183 if( (*buf++ != MBEDTLS_X25519_KEY_SIZE_BYTES) )
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +0100184 return(MBEDTLS_ERR_ECP_BAD_INPUT_DATA);
Christoph M. Wintersteigerfb779f12019-02-15 16:20:54 +0000185 memcpy( ctx->peer_point, buf, MBEDTLS_X25519_KEY_SIZE_BYTES );
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +0100186 return(0);
187}
188
189
190#endif /* MBEDTLS_ECDH_C */