Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ECDH: Add the Everest X25519 implementation #2073

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
78 commits
Select commit Hold shift + click to select a range
c59266d
ECDH: Add Everest Curve25519 to 3rdparty/everest
Oct 25, 2018
d904855
ECDH: Add VS2010 support files for Everest Curve25519
Oct 25, 2018
f4ffcc1
ECDH: Add new ECDH context for Everest Curve25519
Oct 25, 2018
f3f7bde
ECDH: Include Everest Curve25519 in build scripts
Oct 25, 2018
285a666
ECDH: Add new (non-legacy) ECDH benchmark
Oct 25, 2018
8e4da4a
ECDH: Enable Everest Curve25519 in ECDH/ECDSA/ECP
Oct 25, 2018
0547dde
ECDH: Add #ifdef filter to tests/scripts/list-enum-consts.pl
Oct 25, 2018
26cf22d
ECDH: Rename full handshake benchmark
Dec 6, 2018
a3c344c
ECDH: Fix typo in ecdh.c
Dec 6, 2018
0d5c92a
ECDH: Fix error code in mbedtls_ecdsa_sign
Dec 6, 2018
e0969f6
ECDH: Remove old code from mbedtls_everest_make_params
Dec 6, 2018
b0b8e80
ECDH: Replace hex literal with decimal in ecp.c
Dec 6, 2018
39e26c0
ECDH: Add #ifdef to cleanly disable the Everest code
Dec 7, 2018
43eddd5
ECDH: Improve ECDH full handshake benchmark
Dec 7, 2018
fb64d2d
ECDH: Add Everest Curve25519 to VS project files
Dec 6, 2018
817e9b5
ECDH: Clean up the interface to Everest code
Dec 12, 2018
a01ef4a
ECDH: Remove YOTTA config #define
Dec 14, 2018
25718fb
ECDH: Fix whitespace, permissions
Dec 14, 2018
19931cb
ECDH: Use LOCAL_CFLAGS instead of CFLAGS
Dec 14, 2018
b920b54
ECDH: Fix error checks in benchmark.c
Dec 14, 2018
ad587c0
ECDH: Everest: Remove unnecessary file
Dec 14, 2018
feda5cb
ECDH: Fix Everest ECDH side type
Jan 7, 2019
eeee437
Add new 3rdparty build scripts
Dec 14, 2018
a1f6d27
Fix preprocessor directive recognition in list-enum-consts.pl
Dec 14, 2018
2a97fbd
ECDH: Exclude FStar and Hacl* from exported symbol checks
Dec 14, 2018
04135e5
Make check-names.sh find the right names in 3rdparty
Dec 14, 2018
52ea25e
ECDSA: Add mbedtls_ecdsa_can_do
Jan 7, 2019
adc5d4e
ECDH: Fix whitespace and permission problems
Jan 7, 2019
f4aee34
ECDH: Disables MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED by default
Jan 17, 2019
4c4898e
Silences missing documentation warning for MBEDTLS_ECDH_VARIANT_EVERE…
Jan 17, 2019
e17bc42
3rdparty: Add additional build facilities for 3rd-party code
Jan 21, 2019
98512a6
ECDH: Make benchmarks check MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED
Feb 6, 2019
a68f194
Fix build with gcc -Wshadow
gilles-peskine-arm Feb 11, 2019
3376d65
Add mbedtls_ecdh_can_do
gilles-peskine-arm Feb 11, 2019
1433f34
Document that curve lists can include partially-supported curves
gilles-peskine-arm Feb 11, 2019
e15e11f
Add a changelog entry for Everest ECDH (X25519)
gilles-peskine-arm Feb 11, 2019
0a54c26
ECDSA: Refactor return value checks for mbedtls_ecdsa_can_do
Feb 15, 2019
d32a947
3rdparty: Fix Makefile coding conventions
Feb 15, 2019
e0e5707
3rdparty: Adjust .gitignore
Feb 15, 2019
dac6a14
3rdparty: Remove unnecessary copy of license file
Feb 15, 2019
738cecb
3rdparty: Update description of MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED
Feb 15, 2019
a37fd05
ECDSA: Fix formatting
Feb 15, 2019
90c1c4c
ECDH/ECDSA: Fix indentation
Feb 15, 2019
44ebf5b
3rdparty: Rename THIRDPARTY_OBJECTS
Feb 15, 2019
f838e8a
3rdparty: Use LOCAL_FLAGS instead of CFLAGS
Feb 15, 2019
99bb966
Fix file permissions
Feb 15, 2019
5eedba8
ECDH: Remove unnecessary #include
Feb 15, 2019
2597730
ECP: add Curve448 to ecp_supported_curves
Feb 15, 2019
b7f3bb3
3rdparty: Fix Everest platform detection for CMake
Feb 15, 2019
2a90f60
3rdparty: Pull Everest x25519 key size into macro
Feb 15, 2019
8965395
3rdparty: Make proper use of mbedtls_platform_zeroize in Everest x25519
Feb 15, 2019
61417bd
3rdparty: Updated comments for Everest x25519
Feb 15, 2019
85fabe9
ECDH: Fix Everest x25519 make_public
Feb 15, 2019
174f460
ECDH: Removed unnecessary calls to mbedtls_ecp_group_load in ECDH ben…
Feb 15, 2019
fc4368d
ECDH: Remove duplicate lines of code
Feb 18, 2019
7c27862
ECDH: Fix memory leaks due to context re-initialization
Feb 18, 2019
fca086b
ECDH: Use macro-based sizes in Everest x25519
Feb 18, 2019
d2178bb
3rdparty: Fix .gitignore
Feb 18, 2019
6566138
3rdparty: Improve Everest README.md
Feb 18, 2019
17a1e61
ECDH: Fix context initialization
Feb 18, 2019
0db2d4d
Revert "ECDH: Fix context initialization"
Feb 20, 2019
f76049c
Revert "ECDH: Fix memory leaks due to context re-initialization"
Feb 20, 2019
5baf4e9
ECDH: Fix use of ECDH API in full handshake benchmark
Feb 20, 2019
fd8bc74
ECDH: Fix inclusion of platform.h for proper use of MBEDTLS_ERR_PLATF…
Feb 20, 2019
17c12ef
3rdparty: don't claim armcc support in Everest Readme.md
Feb 20, 2019
72ab13a
3rdparty: Adjust use of Everest in ecp_supported_curves
Feb 22, 2019
542c9d9
3rdparty: Fix Everest build to not depend on build-time macros
Feb 22, 2019
3c04cc6
3rdparty: Fix newlines and trailing whitespace
Feb 26, 2019
afec434
3rdparty: fix paths in Everest build scripts
Apr 5, 2019
a653f76
3rdparty: Fix Everest header installation
Apr 8, 2019
e379b3f
3rdparty: fix inclusion order of CMakeLists.txt
Apr 8, 2019
20c5638
Update generated files
Apr 12, 2019
a4fd9f2
Fix macros in benchmark.c
Apr 15, 2019
9e1fe28
3rdparty: Added config checks for Everest
Apr 15, 2019
92bd50e
3rdparty: Fix Everest's mbedtls_x25519_get_params
Apr 15, 2019
8390fc9
Add Everest components to all.sh
gilles-peskine-arm Apr 12, 2019
3487b9c
Fix code style
Apr 16, 2019
16bdf8c
3rdparty: Fix include directories
Apr 16, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions 3rdparty/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/Makefile
11 changes: 11 additions & 0 deletions 3rdparty/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
list (APPEND thirdparty_src)
list (APPEND thirdparty_lib)
list (APPEND thirdparty_inc)
list (APPEND thirdparty_def)

add_subdirectory(everest)

set(thirdparty_src ${thirdparty_src} PARENT_SCOPE)
set(thirdparty_lib ${thirdparty_lib} PARENT_SCOPE)
set(thirdparty_inc ${thirdparty_inc} PARENT_SCOPE)
set(thirdparty_def ${thirdparty_def} PARENT_SCOPE)
2 changes: 2 additions & 0 deletions 3rdparty/Makefile.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
include ../3rdparty/everest/Makefile.inc

2 changes: 2 additions & 0 deletions 3rdparty/everest/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.o
gilles-peskine-arm marked this conversation as resolved.
Show resolved Hide resolved
Makefile
31 changes: 31 additions & 0 deletions 3rdparty/everest/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
list (APPEND everest_src)
list (APPEND everest_inc)
list (APPEND everest_def)

set(everest_src
${CMAKE_CURRENT_SOURCE_DIR}/library/everest.c
${CMAKE_CURRENT_SOURCE_DIR}/library/x25519.c
${CMAKE_CURRENT_SOURCE_DIR}/library/Hacl_Curve25519_joined.c
)

list(APPEND everest_inc ${CMAKE_CURRENT_SOURCE_DIR}/../../include ${CMAKE_CURRENT_SOURCE_DIR}/include ${CMAKE_CURRENT_SOURCE_DIR}/include/everest ${CMAKE_CURRENT_SOURCE_DIR}/include/everest/kremlib)

execute_process(COMMAND ${PERL_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/../../scripts/config.pl -f ${CMAKE_CURRENT_SOURCE_DIR}/../../include/mbedtls/config.h get MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED RESULT_VARIABLE result)

if(${result} EQUAL 0)

if(INSTALL_MBEDTLS_HEADERS)

install(DIRECTORY include/everest
DESTINATION include
FILE_PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ
DIRECTORY_PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
FILES_MATCHING PATTERN "*.h")

endif(INSTALL_MBEDTLS_HEADERS)

endif()

set(thirdparty_src ${thirdparty_src} ${everest_src} PARENT_SCOPE)
set(thirdparty_inc ${thirdparty_inc} ${everest_inc} PARENT_SCOPE)
set(thirdparty_def ${thirdparty_def} ${everest_def} PARENT_SCOPE)
6 changes: 6 additions & 0 deletions 3rdparty/everest/Makefile.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
THIRDPARTY_INCLUDES+=-I../3rdparty/everest/include -I../3rdparty/everest/include/everest -I../3rdparty/everest/include/everest/kremlib

THIRDPARTY_CRYPTO_OBJECTS+= \
../3rdparty/everest/library/everest.o \
../3rdparty/everest/library/x25519.o \
../3rdparty/everest/library/Hacl_Curve25519_joined.o
5 changes: 5 additions & 0 deletions 3rdparty/everest/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
The files in this directory stem from [Project Everest](https://project-everest.github.io/) and are distributed under the Apache 2.0 license.
gilles-peskine-arm marked this conversation as resolved.
Show resolved Hide resolved

This is a formally verified implementation of Curve25519-based handshakes. The C code is automatically derived from the (verified) [original implementation](https://github.com/project-everest/hacl-star/tree/master/code/curve25519) in the [F* language](https://github.com/fstarlang/fstar) by [KreMLin](https://github.com/fstarlang/kremlin). In addition to the improved safety and security of the implementation, it is also significantly faster than the default implementation of Curve25519 in mbedTLS.

The caveat is that not all platforms are supported, although the version in `everest/library/legacy` should work on most systems. The main issue is that some platforms do not provide a 128-bit integer type and KreMLin therefore has to use additional (also verified) code to simulate them, resulting in less of a performance gain overall. Explictly supported platforms are currently `x86` and `x86_64` using gcc or clang, and Visual C (2010 and later).
21 changes: 21 additions & 0 deletions 3rdparty/everest/include/everest/Hacl_Curve25519.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* Copyright (c) INRIA and Microsoft Corporation. All rights reserved.
Licensed under the Apache 2.0 License. */

/* This file was generated by KreMLin <https://github.com/FStarLang/kremlin>
* KreMLin invocation: /mnt/e/everest/verify/kremlin/krml -fc89 -fparentheses -fno-shadow -header /mnt/e/everest/verify/hdrcLh -minimal -fbuiltin-uint128 -fc89 -fparentheses -fno-shadow -header /mnt/e/everest/verify/hdrcLh -minimal -I /mnt/e/everest/verify/hacl-star/code/lib/kremlin -I /mnt/e/everest/verify/kremlin/kremlib/compat -I /mnt/e/everest/verify/hacl-star/specs -I /mnt/e/everest/verify/hacl-star/specs/old -I . -ccopt -march=native -verbose -ldopt -flto -tmpdir x25519-c -I ../bignum -bundle Hacl.Curve25519=* -minimal -add-include "kremlib.h" -skip-compilation x25519-c/out.krml -o x25519-c/Hacl_Curve25519.c
* F* version: 059db0c8
* KreMLin version: 916c37ac
*/



#ifndef __Hacl_Curve25519_H
#define __Hacl_Curve25519_H


#include "kremlib.h"

void Hacl_Curve25519_crypto_scalarmult(uint8_t *mypublic, uint8_t *secret, uint8_t *basepoint);
wintersteiger marked this conversation as resolved.
Show resolved Hide resolved

#define __Hacl_Curve25519_H_DEFINED
#endif
234 changes: 234 additions & 0 deletions 3rdparty/everest/include/everest/everest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
/*
* Interface to code from Project Everest
*
* Copyright 2016-2018 INRIA and Microsoft Corporation
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This file is part of Mbed TLS (https://tls.mbed.org).
*/

#ifndef MBEDTLS_EVEREST_H
#define MBEDTLS_EVEREST_H

#include "everest/x25519.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
* Defines the source of the imported EC key.
*/
typedef enum
{
MBEDTLS_EVEREST_ECDH_OURS, /**< Our key. */
MBEDTLS_EVEREST_ECDH_THEIRS, /**< The key of the peer. */
} mbedtls_everest_ecdh_side;

typedef struct {
mbedtls_x25519_context ctx;
} mbedtls_ecdh_context_everest;


/**
* \brief This function sets up the ECDH context with the information
* given.
*
* This function should be called after mbedtls_ecdh_init() but
* before mbedtls_ecdh_make_params(). There is no need to call
* this function before mbedtls_ecdh_read_params().
*
* This is the first function used by a TLS server for ECDHE
* ciphersuites.
*
* \param ctx The ECDH context to set up.
* \param grp_id The group id of the group to set up the context for.
*
* \return \c 0 on success.
*/
int mbedtls_everest_setup( mbedtls_ecdh_context_everest *ctx, int grp_id );

/**
* \brief This function frees a context.
*
* \param ctx The context to free.
*/
void mbedtls_everest_free( mbedtls_ecdh_context_everest *ctx );

/**
* \brief This function generates a public key and a TLS
* ServerKeyExchange payload.
*
* This is the second function used by a TLS server for ECDHE
* ciphersuites. (It is called after mbedtls_ecdh_setup().)
*
* \note This function assumes that the ECP group (grp) of the
* \p ctx context has already been properly set,
* for example, using mbedtls_ecp_group_load().
*
* \see ecp.h
*
* \param ctx The ECDH context.
* \param olen The number of characters written.
* \param buf The destination buffer.
* \param blen The length of the destination buffer.
* \param f_rng The RNG function.
* \param p_rng The RNG context.
*
* \return \c 0 on success.
* \return An \c MBEDTLS_ERR_ECP_XXX error code on failure.
*/
int mbedtls_everest_make_params( mbedtls_ecdh_context_everest *ctx, size_t *olen,
unsigned char *buf, size_t blen,
int( *f_rng )( void *, unsigned char *, size_t ),
void *p_rng );

/**
* \brief This function parses and processes a TLS ServerKeyExhange
* payload.
*
* This is the first function used by a TLS client for ECDHE
* ciphersuites.
*
* \see ecp.h
*
* \param ctx The ECDH context.
* \param buf The pointer to the start of the input buffer.
* \param end The address for one Byte past the end of the buffer.
*
* \return \c 0 on success.
* \return An \c MBEDTLS_ERR_ECP_XXX error code on failure.
*
*/
int mbedtls_everest_read_params( mbedtls_ecdh_context_everest *ctx,
const unsigned char **buf, const unsigned char *end );

/**
* \brief This function parses and processes a TLS ServerKeyExhange
* payload.
*
* This is the first function used by a TLS client for ECDHE
* ciphersuites.
*
* \see ecp.h
*
* \param ctx The ECDH context.
* \param buf The pointer to the start of the input buffer.
* \param end The address for one Byte past the end of the buffer.
*
* \return \c 0 on success.
* \return An \c MBEDTLS_ERR_ECP_XXX error code on failure.
*
*/
int mbedtls_everest_read_params( mbedtls_ecdh_context_everest *ctx,
const unsigned char **buf, const unsigned char *end );

/**
* \brief This function sets up an ECDH context from an EC key.
*
* It is used by clients and servers in place of the
* ServerKeyEchange for static ECDH, and imports ECDH
* parameters from the EC key information of a certificate.
*
* \see ecp.h
*
* \param ctx The ECDH context to set up.
* \param key The EC key to use.
* \param side Defines the source of the key: 1: Our key, or
* 0: The key of the peer.
*
* \return \c 0 on success.
* \return An \c MBEDTLS_ERR_ECP_XXX error code on failure.
*
*/
int mbedtls_everest_get_params( mbedtls_ecdh_context_everest *ctx, const mbedtls_ecp_keypair *key,
mbedtls_everest_ecdh_side side );

/**
* \brief This function generates a public key and a TLS
* ClientKeyExchange payload.
*
* This is the second function used by a TLS client for ECDH(E)
* ciphersuites.
*
* \see ecp.h
*
* \param ctx The ECDH context.
* \param olen The number of Bytes written.
* \param buf The destination buffer.
* \param blen The size of the destination buffer.
* \param f_rng The RNG function.
* \param p_rng The RNG context.
*
* \return \c 0 on success.
* \return An \c MBEDTLS_ERR_ECP_XXX error code on failure.
*/
int mbedtls_everest_make_public( mbedtls_ecdh_context_everest *ctx, size_t *olen,
unsigned char *buf, size_t blen,
int( *f_rng )( void *, unsigned char *, size_t ),
void *p_rng );

/**
* \brief This function parses and processes a TLS ClientKeyExchange
* payload.
*
* This is the third function used by a TLS server for ECDH(E)
* ciphersuites. (It is called after mbedtls_ecdh_setup() and
* mbedtls_ecdh_make_params().)
*
* \see ecp.h
*
* \param ctx The ECDH context.
* \param buf The start of the input buffer.
* \param blen The length of the input buffer.
*
* \return \c 0 on success.
* \return An \c MBEDTLS_ERR_ECP_XXX error code on failure.
*/
int mbedtls_everest_read_public( mbedtls_ecdh_context_everest *ctx,
const unsigned char *buf, size_t blen );

/**
* \brief This function derives and exports the shared secret.
*
* This is the last function used by both TLS client
* and servers.
*
* \note If \p f_rng is not NULL, it is used to implement
* countermeasures against side-channel attacks.
* For more information, see mbedtls_ecp_mul().
*
* \see ecp.h
*
* \param ctx The ECDH context.
* \param olen The number of Bytes written.
* \param buf The destination buffer.
* \param blen The length of the destination buffer.
* \param f_rng The RNG function.
* \param p_rng The RNG context.
*
* \return \c 0 on success.
* \return An \c MBEDTLS_ERR_ECP_XXX error code on failure.
*/
int mbedtls_everest_calc_secret( mbedtls_ecdh_context_everest *ctx, size_t *olen,
unsigned char *buf, size_t blen,
int( *f_rng )( void *, unsigned char *, size_t ),
void *p_rng );

#ifdef __cplusplus
}
#endif

#endif /* MBEDTLS_EVEREST_H */
29 changes: 29 additions & 0 deletions 3rdparty/everest/include/everest/kremlib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2016-2018 INRIA and Microsoft Corporation
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This file is part of Mbed TLS (https://tls.mbed.org) and
* originated from Project Everest (https://project-everest.github.io/)
*/

#ifndef __KREMLIB_H
#define __KREMLIB_H

#include "kremlin/internal/target.h"
#include "kremlin/internal/types.h"
#include "kremlin/c_endianness.h"

#endif /* __KREMLIB_H */
Loading