1a8e1175bSopenharmony_ci/* 2a8e1175bSopenharmony_ci * Interface to code from Project Everest 3a8e1175bSopenharmony_ci * 4a8e1175bSopenharmony_ci * Copyright 2016-2018 INRIA and Microsoft Corporation 5a8e1175bSopenharmony_ci * SPDX-License-Identifier: Apache-2.0 6a8e1175bSopenharmony_ci * 7a8e1175bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); you may 8a8e1175bSopenharmony_ci * not use this file except in compliance with the License. 9a8e1175bSopenharmony_ci * You may obtain a copy of the License at 10a8e1175bSopenharmony_ci * 11a8e1175bSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 12a8e1175bSopenharmony_ci * 13a8e1175bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 14a8e1175bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 15a8e1175bSopenharmony_ci * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16a8e1175bSopenharmony_ci * See the License for the specific language governing permissions and 17a8e1175bSopenharmony_ci * limitations under the License. 18a8e1175bSopenharmony_ci * 19a8e1175bSopenharmony_ci * This file is part of Mbed TLS (https://tls.mbed.org). 20a8e1175bSopenharmony_ci */ 21a8e1175bSopenharmony_ci 22a8e1175bSopenharmony_ci#include "common.h" 23a8e1175bSopenharmony_ci 24a8e1175bSopenharmony_ci#include <string.h> 25a8e1175bSopenharmony_ci 26a8e1175bSopenharmony_ci#include "mbedtls/ecdh.h" 27a8e1175bSopenharmony_ci 28a8e1175bSopenharmony_ci#include "everest/x25519.h" 29a8e1175bSopenharmony_ci#include "everest/everest.h" 30a8e1175bSopenharmony_ci 31a8e1175bSopenharmony_ci#include "mbedtls/platform.h" 32a8e1175bSopenharmony_ci 33a8e1175bSopenharmony_ci#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED) 34a8e1175bSopenharmony_ci 35a8e1175bSopenharmony_ciint mbedtls_everest_setup( mbedtls_ecdh_context_everest *ctx, int grp_id ) 36a8e1175bSopenharmony_ci{ 37a8e1175bSopenharmony_ci if( grp_id != MBEDTLS_ECP_DP_CURVE25519 ) 38a8e1175bSopenharmony_ci return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; 39a8e1175bSopenharmony_ci mbedtls_x25519_init( &ctx->ctx ); 40a8e1175bSopenharmony_ci return 0; 41a8e1175bSopenharmony_ci} 42a8e1175bSopenharmony_ci 43a8e1175bSopenharmony_civoid mbedtls_everest_free( mbedtls_ecdh_context_everest *ctx ) 44a8e1175bSopenharmony_ci{ 45a8e1175bSopenharmony_ci mbedtls_x25519_free( &ctx->ctx ); 46a8e1175bSopenharmony_ci} 47a8e1175bSopenharmony_ci 48a8e1175bSopenharmony_ciint mbedtls_everest_make_params( mbedtls_ecdh_context_everest *ctx, size_t *olen, 49a8e1175bSopenharmony_ci unsigned char *buf, size_t blen, 50a8e1175bSopenharmony_ci int( *f_rng )( void *, unsigned char *, size_t ), 51a8e1175bSopenharmony_ci void *p_rng ) 52a8e1175bSopenharmony_ci{ 53a8e1175bSopenharmony_ci mbedtls_x25519_context *x25519_ctx = &ctx->ctx; 54a8e1175bSopenharmony_ci return mbedtls_x25519_make_params( x25519_ctx, olen, buf, blen, f_rng, p_rng ); 55a8e1175bSopenharmony_ci} 56a8e1175bSopenharmony_ci 57a8e1175bSopenharmony_ciint mbedtls_everest_read_params( mbedtls_ecdh_context_everest *ctx, 58a8e1175bSopenharmony_ci const unsigned char **buf, 59a8e1175bSopenharmony_ci const unsigned char *end ) 60a8e1175bSopenharmony_ci{ 61a8e1175bSopenharmony_ci mbedtls_x25519_context *x25519_ctx = &ctx->ctx; 62a8e1175bSopenharmony_ci return mbedtls_x25519_read_params( x25519_ctx, buf, end ); 63a8e1175bSopenharmony_ci} 64a8e1175bSopenharmony_ci 65a8e1175bSopenharmony_ciint mbedtls_everest_get_params( mbedtls_ecdh_context_everest *ctx, 66a8e1175bSopenharmony_ci const mbedtls_ecp_keypair *key, 67a8e1175bSopenharmony_ci mbedtls_everest_ecdh_side side ) 68a8e1175bSopenharmony_ci{ 69a8e1175bSopenharmony_ci mbedtls_x25519_context *x25519_ctx = &ctx->ctx; 70a8e1175bSopenharmony_ci mbedtls_x25519_ecdh_side s = side == MBEDTLS_EVEREST_ECDH_OURS ? 71a8e1175bSopenharmony_ci MBEDTLS_X25519_ECDH_OURS : 72a8e1175bSopenharmony_ci MBEDTLS_X25519_ECDH_THEIRS; 73a8e1175bSopenharmony_ci return mbedtls_x25519_get_params( x25519_ctx, key, s ); 74a8e1175bSopenharmony_ci} 75a8e1175bSopenharmony_ci 76a8e1175bSopenharmony_ciint mbedtls_everest_make_public( mbedtls_ecdh_context_everest *ctx, size_t *olen, 77a8e1175bSopenharmony_ci unsigned char *buf, size_t blen, 78a8e1175bSopenharmony_ci int( *f_rng )( void *, unsigned char *, size_t ), 79a8e1175bSopenharmony_ci void *p_rng ) 80a8e1175bSopenharmony_ci{ 81a8e1175bSopenharmony_ci mbedtls_x25519_context *x25519_ctx = &ctx->ctx; 82a8e1175bSopenharmony_ci return mbedtls_x25519_make_public( x25519_ctx, olen, buf, blen, f_rng, p_rng ); 83a8e1175bSopenharmony_ci} 84a8e1175bSopenharmony_ci 85a8e1175bSopenharmony_ciint mbedtls_everest_read_public( mbedtls_ecdh_context_everest *ctx, 86a8e1175bSopenharmony_ci const unsigned char *buf, size_t blen ) 87a8e1175bSopenharmony_ci{ 88a8e1175bSopenharmony_ci mbedtls_x25519_context *x25519_ctx = &ctx->ctx; 89a8e1175bSopenharmony_ci return mbedtls_x25519_read_public ( x25519_ctx, buf, blen ); 90a8e1175bSopenharmony_ci} 91a8e1175bSopenharmony_ci 92a8e1175bSopenharmony_ciint mbedtls_everest_calc_secret( mbedtls_ecdh_context_everest *ctx, size_t *olen, 93a8e1175bSopenharmony_ci unsigned char *buf, size_t blen, 94a8e1175bSopenharmony_ci int( *f_rng )( void *, unsigned char *, size_t ), 95a8e1175bSopenharmony_ci void *p_rng ) 96a8e1175bSopenharmony_ci{ 97a8e1175bSopenharmony_ci mbedtls_x25519_context *x25519_ctx = &ctx->ctx; 98a8e1175bSopenharmony_ci return mbedtls_x25519_calc_secret( x25519_ctx, olen, buf, blen, f_rng, p_rng ); 99a8e1175bSopenharmony_ci} 100a8e1175bSopenharmony_ci 101a8e1175bSopenharmony_ci#endif /* MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED */ 102a8e1175bSopenharmony_ci 103