Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 22 | #include "common.h" |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 23 | |
Christoph M. Wintersteiger | 6e0cac1 | 2019-02-22 17:02:12 +0000 | [diff] [blame] | 24 | #if defined(MBEDTLS_ECDH_C) && defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED) |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 25 | |
Christoph M. Wintersteiger | 4936beb | 2018-12-12 17:26:41 +0000 | [diff] [blame] | 26 | #include <mbedtls/ecdh.h> |
| 27 | |
Christoph M. Wintersteiger | 6e0cac1 | 2019-02-22 17:02:12 +0000 | [diff] [blame] | 28 | #if !(defined(__SIZEOF_INT128__) && (__SIZEOF_INT128__ == 16)) |
| 29 | #define KRML_VERIFIED_UINT128 |
| 30 | #endif |
| 31 | |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 32 | #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 | */ |
| 42 | void mbedtls_x25519_init( mbedtls_x25519_context *ctx ) |
| 43 | { |
Christoph M. Wintersteiger | 088ef49 | 2019-02-15 16:25:48 +0000 | [diff] [blame] | 44 | mbedtls_platform_zeroize( ctx, sizeof( mbedtls_x25519_context ) ); |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | /* |
| 48 | * Free context |
| 49 | */ |
| 50 | void mbedtls_x25519_free( mbedtls_x25519_context *ctx ) |
| 51 | { |
| 52 | if( ctx == NULL ) |
| 53 | return; |
| 54 | |
Christoph M. Wintersteiger | fb779f1 | 2019-02-15 16:20:54 +0000 | [diff] [blame] | 55 | 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. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | int 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. Wintersteiger | fb779f1 | 2019-02-15 16:20:54 +0000 | [diff] [blame] | 66 | uint8_t base[MBEDTLS_X25519_KEY_SIZE_BYTES] = {0}; |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 67 | |
Christoph M. Wintersteiger | fb779f1 | 2019-02-15 16:20:54 +0000 | [diff] [blame] | 68 | if( ( ret = f_rng( p_rng, ctx->our_secret, MBEDTLS_X25519_KEY_SIZE_BYTES ) ) != 0 ) |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 69 | return ret; |
| 70 | |
Christoph M. Wintersteiger | 8592958 | 2019-02-18 13:20:33 +0000 | [diff] [blame] | 71 | *olen = MBEDTLS_X25519_KEY_SIZE_BYTES + 4; |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 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. Wintersteiger | fb779f1 | 2019-02-15 16:20:54 +0000 | [diff] [blame] | 78 | *buf++ = MBEDTLS_X25519_KEY_SIZE_BYTES; |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 79 | |
| 80 | base[0] = 9; |
| 81 | Hacl_Curve25519_crypto_scalarmult( buf, ctx->our_secret, base ); |
| 82 | |
| 83 | base[0] = 0; |
Christoph M. Wintersteiger | fb779f1 | 2019-02-15 16:20:54 +0000 | [diff] [blame] | 84 | if( memcmp( buf, base, MBEDTLS_X25519_KEY_SIZE_BYTES) == 0 ) |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 85 | return MBEDTLS_ERR_ECP_RANDOM_FAILED; |
| 86 | |
| 87 | return( 0 ); |
| 88 | } |
| 89 | |
| 90 | int mbedtls_x25519_read_params( mbedtls_x25519_context *ctx, |
| 91 | const unsigned char **buf, const unsigned char *end ) |
| 92 | { |
Christoph M. Wintersteiger | 8592958 | 2019-02-18 13:20:33 +0000 | [diff] [blame] | 93 | if( end - *buf < MBEDTLS_X25519_KEY_SIZE_BYTES + 1 ) |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 94 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
| 95 | |
Christoph M. Wintersteiger | fb779f1 | 2019-02-15 16:20:54 +0000 | [diff] [blame] | 96 | if( ( *(*buf)++ != MBEDTLS_X25519_KEY_SIZE_BYTES ) ) |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 97 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
| 98 | |
Christoph M. Wintersteiger | fb779f1 | 2019-02-15 16:20:54 +0000 | [diff] [blame] | 99 | memcpy( ctx->peer_point, *buf, MBEDTLS_X25519_KEY_SIZE_BYTES ); |
| 100 | *buf += MBEDTLS_X25519_KEY_SIZE_BYTES; |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 101 | return( 0 ); |
| 102 | } |
| 103 | |
| 104 | int mbedtls_x25519_get_params( mbedtls_x25519_context *ctx, const mbedtls_ecp_keypair *key, |
Christoph M. Wintersteiger | 4936beb | 2018-12-12 17:26:41 +0000 | [diff] [blame] | 105 | mbedtls_x25519_ecdh_side side ) |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 106 | { |
| 107 | size_t olen = 0; |
| 108 | |
| 109 | switch( side ) { |
Christoph M. Wintersteiger | 4936beb | 2018-12-12 17:26:41 +0000 | [diff] [blame] | 110 | case MBEDTLS_X25519_ECDH_THEIRS: |
Christoph M. Wintersteiger | 0969eee | 2019-04-15 12:00:16 +0100 | [diff] [blame] | 111 | return mbedtls_ecp_point_write_binary( &key->grp, &key->Q, MBEDTLS_ECP_PF_COMPRESSED, &olen, ctx->peer_point, MBEDTLS_X25519_KEY_SIZE_BYTES ); |
Christoph M. Wintersteiger | 4936beb | 2018-12-12 17:26:41 +0000 | [diff] [blame] | 112 | case MBEDTLS_X25519_ECDH_OURS: |
Christoph M. Wintersteiger | 0969eee | 2019-04-15 12:00:16 +0100 | [diff] [blame] | 113 | return mbedtls_mpi_write_binary_le( &key->d, ctx->our_secret, MBEDTLS_X25519_KEY_SIZE_BYTES ); |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 114 | default: |
| 115 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | int mbedtls_x25519_calc_secret( mbedtls_x25519_context *ctx, size_t *olen, |
| 120 | unsigned char *buf, size_t blen, |
| 121 | int( *f_rng )(void *, unsigned char *, size_t), |
| 122 | void *p_rng ) |
| 123 | { |
Christoph M. Wintersteiger | 537f41e | 2019-02-15 16:50:54 +0000 | [diff] [blame] | 124 | /* f_rng and p_rng are not used here because this implementation does not |
| 125 | need blinding since it has constant trace. */ |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 126 | (( void )f_rng); |
| 127 | (( void )p_rng); |
| 128 | |
Christoph M. Wintersteiger | fb779f1 | 2019-02-15 16:20:54 +0000 | [diff] [blame] | 129 | *olen = MBEDTLS_X25519_KEY_SIZE_BYTES; |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 130 | |
| 131 | if( blen < *olen ) |
| 132 | return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL ); |
| 133 | |
| 134 | Hacl_Curve25519_crypto_scalarmult( buf, ctx->our_secret, ctx->peer_point); |
| 135 | |
| 136 | /* Wipe the DH secret and don't let the peer chose a small subgroup point */ |
Christoph M. Wintersteiger | 088ef49 | 2019-02-15 16:25:48 +0000 | [diff] [blame] | 137 | mbedtls_platform_zeroize( ctx->our_secret, MBEDTLS_X25519_KEY_SIZE_BYTES ); |
| 138 | |
Christoph M. Wintersteiger | fb779f1 | 2019-02-15 16:20:54 +0000 | [diff] [blame] | 139 | if( memcmp( buf, ctx->our_secret, MBEDTLS_X25519_KEY_SIZE_BYTES) == 0 ) |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 140 | return MBEDTLS_ERR_ECP_RANDOM_FAILED; |
| 141 | |
| 142 | return( 0 ); |
| 143 | } |
| 144 | |
| 145 | int mbedtls_x25519_make_public( mbedtls_x25519_context *ctx, size_t *olen, |
| 146 | unsigned char *buf, size_t blen, |
| 147 | int( *f_rng )(void *, unsigned char *, size_t), |
| 148 | void *p_rng ) |
| 149 | { |
Christoph M. Wintersteiger | efdf4d7 | 2019-02-15 17:21:04 +0000 | [diff] [blame] | 150 | int ret = 0; |
Christoph M. Wintersteiger | fb779f1 | 2019-02-15 16:20:54 +0000 | [diff] [blame] | 151 | unsigned char base[MBEDTLS_X25519_KEY_SIZE_BYTES] = { 0 }; |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 152 | |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 153 | if( ctx == NULL ) |
Christoph M. Wintersteiger | efdf4d7 | 2019-02-15 17:21:04 +0000 | [diff] [blame] | 154 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
| 155 | |
| 156 | if( ( ret = f_rng( p_rng, ctx->our_secret, MBEDTLS_X25519_KEY_SIZE_BYTES ) ) != 0 ) |
| 157 | return ret; |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 158 | |
Christoph M. Wintersteiger | 8592958 | 2019-02-18 13:20:33 +0000 | [diff] [blame] | 159 | *olen = MBEDTLS_X25519_KEY_SIZE_BYTES + 1; |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 160 | if( blen < *olen ) |
| 161 | return(MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL); |
Christoph M. Wintersteiger | fb779f1 | 2019-02-15 16:20:54 +0000 | [diff] [blame] | 162 | *buf++ = MBEDTLS_X25519_KEY_SIZE_BYTES; |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 163 | |
| 164 | base[0] = 9; |
| 165 | Hacl_Curve25519_crypto_scalarmult( buf, ctx->our_secret, base ); |
| 166 | |
| 167 | base[0] = 0; |
Christoph M. Wintersteiger | fb779f1 | 2019-02-15 16:20:54 +0000 | [diff] [blame] | 168 | if( memcmp( buf, base, MBEDTLS_X25519_KEY_SIZE_BYTES ) == 0 ) |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 169 | return MBEDTLS_ERR_ECP_RANDOM_FAILED; |
| 170 | |
Christoph M. Wintersteiger | efdf4d7 | 2019-02-15 17:21:04 +0000 | [diff] [blame] | 171 | return( ret ); |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | int mbedtls_x25519_read_public( mbedtls_x25519_context *ctx, |
| 175 | const unsigned char *buf, size_t blen ) |
| 176 | { |
Christoph M. Wintersteiger | 8592958 | 2019-02-18 13:20:33 +0000 | [diff] [blame] | 177 | if( blen < MBEDTLS_X25519_KEY_SIZE_BYTES + 1 ) |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 178 | return(MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL); |
Christoph M. Wintersteiger | fb779f1 | 2019-02-15 16:20:54 +0000 | [diff] [blame] | 179 | if( (*buf++ != MBEDTLS_X25519_KEY_SIZE_BYTES) ) |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 180 | return(MBEDTLS_ERR_ECP_BAD_INPUT_DATA); |
Christoph M. Wintersteiger | fb779f1 | 2019-02-15 16:20:54 +0000 | [diff] [blame] | 181 | memcpy( ctx->peer_point, buf, MBEDTLS_X25519_KEY_SIZE_BYTES ); |
Christoph M. Wintersteiger | efdf4d7 | 2019-02-15 17:21:04 +0000 | [diff] [blame] | 182 | return( 0 ); |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | |
Christoph M. Wintersteiger | 6e0cac1 | 2019-02-22 17:02:12 +0000 | [diff] [blame] | 186 | #endif /* MBEDTLS_ECDH_C && MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED */ |