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 | |
| 22 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 23 | #include "mbedtls/config.h" |
| 24 | #else |
| 25 | #include MBEDTLS_CONFIG_FILE |
| 26 | #endif |
| 27 | |
Christoph M. Wintersteiger | 6e0cac1 | 2019-02-22 17:02:12 +0000 | [diff] [blame] | 28 | #if defined(MBEDTLS_ECDH_C) && defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED) |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 29 | |
Christoph M. Wintersteiger | 4936beb | 2018-12-12 17:26:41 +0000 | [diff] [blame] | 30 | #include <mbedtls/ecdh.h> |
| 31 | |
Christoph M. Wintersteiger | 6e0cac1 | 2019-02-22 17:02:12 +0000 | [diff] [blame] | 32 | #if !(defined(__SIZEOF_INT128__) && (__SIZEOF_INT128__ == 16)) |
| 33 | #define KRML_VERIFIED_UINT128 |
| 34 | #endif |
| 35 | |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 36 | #include <Hacl_Curve25519.h> |
| 37 | #include <mbedtls/platform_util.h> |
| 38 | |
| 39 | #include "x25519.h" |
| 40 | |
| 41 | #include <string.h> |
| 42 | |
| 43 | /* |
| 44 | * Initialize context |
| 45 | */ |
| 46 | void mbedtls_x25519_init( mbedtls_x25519_context *ctx ) |
| 47 | { |
Christoph M. Wintersteiger | 088ef49 | 2019-02-15 16:25:48 +0000 | [diff] [blame] | 48 | mbedtls_platform_zeroize( ctx, sizeof( mbedtls_x25519_context ) ); |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | /* |
| 52 | * Free context |
| 53 | */ |
| 54 | void mbedtls_x25519_free( mbedtls_x25519_context *ctx ) |
| 55 | { |
| 56 | if( ctx == NULL ) |
| 57 | return; |
| 58 | |
Christoph M. Wintersteiger | fb779f1 | 2019-02-15 16:20:54 +0000 | [diff] [blame] | 59 | mbedtls_platform_zeroize( ctx->our_secret, MBEDTLS_X25519_KEY_SIZE_BYTES ); |
| 60 | mbedtls_platform_zeroize( ctx->peer_point, MBEDTLS_X25519_KEY_SIZE_BYTES ); |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | int mbedtls_x25519_make_params( mbedtls_x25519_context *ctx, size_t *olen, |
| 64 | unsigned char *buf, size_t blen, |
| 65 | int( *f_rng )(void *, unsigned char *, size_t), |
| 66 | void *p_rng ) |
| 67 | { |
| 68 | int ret = 0; |
| 69 | |
Christoph M. Wintersteiger | fb779f1 | 2019-02-15 16:20:54 +0000 | [diff] [blame] | 70 | uint8_t base[MBEDTLS_X25519_KEY_SIZE_BYTES] = {0}; |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 71 | |
Christoph M. Wintersteiger | fb779f1 | 2019-02-15 16:20:54 +0000 | [diff] [blame] | 72 | 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] | 73 | return ret; |
| 74 | |
Christoph M. Wintersteiger | 8592958 | 2019-02-18 13:20:33 +0000 | [diff] [blame] | 75 | *olen = MBEDTLS_X25519_KEY_SIZE_BYTES + 4; |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 76 | if( blen < *olen ) |
| 77 | return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL ); |
| 78 | |
| 79 | *buf++ = MBEDTLS_ECP_TLS_NAMED_CURVE; |
| 80 | *buf++ = MBEDTLS_ECP_TLS_CURVE25519 >> 8; |
| 81 | *buf++ = MBEDTLS_ECP_TLS_CURVE25519 & 0xFF; |
Christoph M. Wintersteiger | fb779f1 | 2019-02-15 16:20:54 +0000 | [diff] [blame] | 82 | *buf++ = MBEDTLS_X25519_KEY_SIZE_BYTES; |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 83 | |
| 84 | base[0] = 9; |
| 85 | Hacl_Curve25519_crypto_scalarmult( buf, ctx->our_secret, base ); |
| 86 | |
| 87 | base[0] = 0; |
Christoph M. Wintersteiger | fb779f1 | 2019-02-15 16:20:54 +0000 | [diff] [blame] | 88 | if( memcmp( buf, base, MBEDTLS_X25519_KEY_SIZE_BYTES) == 0 ) |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 89 | return MBEDTLS_ERR_ECP_RANDOM_FAILED; |
| 90 | |
| 91 | return( 0 ); |
| 92 | } |
| 93 | |
| 94 | int mbedtls_x25519_read_params( mbedtls_x25519_context *ctx, |
| 95 | const unsigned char **buf, const unsigned char *end ) |
| 96 | { |
Christoph M. Wintersteiger | 8592958 | 2019-02-18 13:20:33 +0000 | [diff] [blame] | 97 | if( end - *buf < MBEDTLS_X25519_KEY_SIZE_BYTES + 1 ) |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 98 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
| 99 | |
Christoph M. Wintersteiger | fb779f1 | 2019-02-15 16:20:54 +0000 | [diff] [blame] | 100 | if( ( *(*buf)++ != MBEDTLS_X25519_KEY_SIZE_BYTES ) ) |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 101 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
| 102 | |
Christoph M. Wintersteiger | fb779f1 | 2019-02-15 16:20:54 +0000 | [diff] [blame] | 103 | memcpy( ctx->peer_point, *buf, MBEDTLS_X25519_KEY_SIZE_BYTES ); |
| 104 | *buf += MBEDTLS_X25519_KEY_SIZE_BYTES; |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 105 | return( 0 ); |
| 106 | } |
| 107 | |
| 108 | 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] | 109 | mbedtls_x25519_ecdh_side side ) |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 110 | { |
| 111 | size_t olen = 0; |
| 112 | |
| 113 | switch( side ) { |
Christoph M. Wintersteiger | 4936beb | 2018-12-12 17:26:41 +0000 | [diff] [blame] | 114 | case MBEDTLS_X25519_ECDH_THEIRS: |
Christoph M. Wintersteiger | 0969eee | 2019-04-15 12:00:16 +0100 | [diff] [blame^] | 115 | 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] | 116 | case MBEDTLS_X25519_ECDH_OURS: |
Christoph M. Wintersteiger | 0969eee | 2019-04-15 12:00:16 +0100 | [diff] [blame^] | 117 | 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] | 118 | default: |
| 119 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | int mbedtls_x25519_calc_secret( mbedtls_x25519_context *ctx, size_t *olen, |
| 124 | unsigned char *buf, size_t blen, |
| 125 | int( *f_rng )(void *, unsigned char *, size_t), |
| 126 | void *p_rng ) |
| 127 | { |
Christoph M. Wintersteiger | 537f41e | 2019-02-15 16:50:54 +0000 | [diff] [blame] | 128 | /* f_rng and p_rng are not used here because this implementation does not |
| 129 | need blinding since it has constant trace. */ |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 130 | (( void )f_rng); |
| 131 | (( void )p_rng); |
| 132 | |
Christoph M. Wintersteiger | fb779f1 | 2019-02-15 16:20:54 +0000 | [diff] [blame] | 133 | *olen = MBEDTLS_X25519_KEY_SIZE_BYTES; |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 134 | |
| 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. Wintersteiger | 088ef49 | 2019-02-15 16:25:48 +0000 | [diff] [blame] | 141 | mbedtls_platform_zeroize( ctx->our_secret, MBEDTLS_X25519_KEY_SIZE_BYTES ); |
| 142 | |
Christoph M. Wintersteiger | fb779f1 | 2019-02-15 16:20:54 +0000 | [diff] [blame] | 143 | 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] | 144 | return MBEDTLS_ERR_ECP_RANDOM_FAILED; |
| 145 | |
| 146 | return( 0 ); |
| 147 | } |
| 148 | |
| 149 | int 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. Wintersteiger | efdf4d7 | 2019-02-15 17:21:04 +0000 | [diff] [blame] | 154 | int ret = 0; |
Christoph M. Wintersteiger | fb779f1 | 2019-02-15 16:20:54 +0000 | [diff] [blame] | 155 | unsigned char base[MBEDTLS_X25519_KEY_SIZE_BYTES] = { 0 }; |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 156 | |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 157 | if( ctx == NULL ) |
Christoph M. Wintersteiger | efdf4d7 | 2019-02-15 17:21:04 +0000 | [diff] [blame] | 158 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
| 159 | |
| 160 | if( ( ret = f_rng( p_rng, ctx->our_secret, MBEDTLS_X25519_KEY_SIZE_BYTES ) ) != 0 ) |
| 161 | return ret; |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 162 | |
Christoph M. Wintersteiger | 8592958 | 2019-02-18 13:20:33 +0000 | [diff] [blame] | 163 | *olen = MBEDTLS_X25519_KEY_SIZE_BYTES + 1; |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 164 | if( blen < *olen ) |
| 165 | return(MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL); |
Christoph M. Wintersteiger | fb779f1 | 2019-02-15 16:20:54 +0000 | [diff] [blame] | 166 | *buf++ = MBEDTLS_X25519_KEY_SIZE_BYTES; |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 167 | |
| 168 | base[0] = 9; |
| 169 | Hacl_Curve25519_crypto_scalarmult( buf, ctx->our_secret, base ); |
| 170 | |
| 171 | base[0] = 0; |
Christoph M. Wintersteiger | fb779f1 | 2019-02-15 16:20:54 +0000 | [diff] [blame] | 172 | if( memcmp( buf, base, MBEDTLS_X25519_KEY_SIZE_BYTES ) == 0 ) |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 173 | return MBEDTLS_ERR_ECP_RANDOM_FAILED; |
| 174 | |
Christoph M. Wintersteiger | efdf4d7 | 2019-02-15 17:21:04 +0000 | [diff] [blame] | 175 | return( ret ); |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | int mbedtls_x25519_read_public( mbedtls_x25519_context *ctx, |
| 179 | const unsigned char *buf, size_t blen ) |
| 180 | { |
Christoph M. Wintersteiger | 8592958 | 2019-02-18 13:20:33 +0000 | [diff] [blame] | 181 | if( blen < MBEDTLS_X25519_KEY_SIZE_BYTES + 1 ) |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 182 | return(MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL); |
Christoph M. Wintersteiger | fb779f1 | 2019-02-15 16:20:54 +0000 | [diff] [blame] | 183 | if( (*buf++ != MBEDTLS_X25519_KEY_SIZE_BYTES) ) |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 184 | return(MBEDTLS_ERR_ECP_BAD_INPUT_DATA); |
Christoph M. Wintersteiger | fb779f1 | 2019-02-15 16:20:54 +0000 | [diff] [blame] | 185 | memcpy( ctx->peer_point, buf, MBEDTLS_X25519_KEY_SIZE_BYTES ); |
Christoph M. Wintersteiger | efdf4d7 | 2019-02-15 17:21:04 +0000 | [diff] [blame] | 186 | return( 0 ); |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | |
Christoph M. Wintersteiger | 6e0cac1 | 2019-02-22 17:02:12 +0000 | [diff] [blame] | 190 | #endif /* MBEDTLS_ECDH_C && MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED */ |