1 /*
2  *  Interface to code from Project Everest
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 #include "common.h"
23 
24 #include <string.h>
25 
26 #include "mbedtls/ecdh.h"
27 
28 #include "everest/x25519.h"
29 #include "everest/everest.h"
30 
31 #include "mbedtls/platform.h"
32 
33 #if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
34 
mbedtls_everest_setup(mbedtls_ecdh_context_everest * ctx,int grp_id)35 int mbedtls_everest_setup( mbedtls_ecdh_context_everest *ctx, int grp_id )
36 {
37     if( grp_id != MBEDTLS_ECP_DP_CURVE25519 )
38         return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
39     mbedtls_x25519_init( &ctx->ctx );
40     return 0;
41 }
42 
mbedtls_everest_free(mbedtls_ecdh_context_everest * ctx)43 void mbedtls_everest_free( mbedtls_ecdh_context_everest *ctx )
44 {
45     mbedtls_x25519_free( &ctx->ctx );
46 }
47 
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)48 int mbedtls_everest_make_params( mbedtls_ecdh_context_everest *ctx, size_t *olen,
49                                  unsigned char *buf, size_t blen,
50                                  int( *f_rng )( void *, unsigned char *, size_t ),
51                                  void *p_rng )
52 {
53     mbedtls_x25519_context *x25519_ctx = &ctx->ctx;
54     return mbedtls_x25519_make_params( x25519_ctx, olen, buf, blen, f_rng, p_rng );
55 }
56 
mbedtls_everest_read_params(mbedtls_ecdh_context_everest * ctx,const unsigned char ** buf,const unsigned char * end)57 int mbedtls_everest_read_params( mbedtls_ecdh_context_everest *ctx,
58                                  const unsigned char **buf,
59                                  const unsigned char *end )
60 {
61     mbedtls_x25519_context *x25519_ctx = &ctx->ctx;
62     return mbedtls_x25519_read_params( x25519_ctx, buf, end );
63 }
64 
mbedtls_everest_get_params(mbedtls_ecdh_context_everest * ctx,const mbedtls_ecp_keypair * key,mbedtls_everest_ecdh_side side)65 int mbedtls_everest_get_params( mbedtls_ecdh_context_everest *ctx,
66                                 const mbedtls_ecp_keypair *key,
67                                 mbedtls_everest_ecdh_side side )
68 {
69     mbedtls_x25519_context *x25519_ctx = &ctx->ctx;
70     mbedtls_x25519_ecdh_side s = side == MBEDTLS_EVEREST_ECDH_OURS ?
71                                             MBEDTLS_X25519_ECDH_OURS :
72                                             MBEDTLS_X25519_ECDH_THEIRS;
73     return mbedtls_x25519_get_params( x25519_ctx, key, s );
74 }
75 
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)76 int mbedtls_everest_make_public( mbedtls_ecdh_context_everest *ctx, size_t *olen,
77                                  unsigned char *buf, size_t blen,
78                                  int( *f_rng )( void *, unsigned char *, size_t ),
79                                  void *p_rng )
80 {
81     mbedtls_x25519_context *x25519_ctx = &ctx->ctx;
82     return mbedtls_x25519_make_public( x25519_ctx, olen, buf, blen, f_rng, p_rng );
83 }
84 
mbedtls_everest_read_public(mbedtls_ecdh_context_everest * ctx,const unsigned char * buf,size_t blen)85 int mbedtls_everest_read_public( mbedtls_ecdh_context_everest *ctx,
86                                  const unsigned char *buf, size_t blen )
87 {
88     mbedtls_x25519_context *x25519_ctx = &ctx->ctx;
89     return mbedtls_x25519_read_public ( x25519_ctx, buf, blen );
90 }
91 
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)92 int mbedtls_everest_calc_secret( mbedtls_ecdh_context_everest *ctx, size_t *olen,
93                                  unsigned char *buf, size_t blen,
94                                  int( *f_rng )( void *, unsigned char *, size_t ),
95                                  void *p_rng )
96 {
97     mbedtls_x25519_context *x25519_ctx = &ctx->ctx;
98     return mbedtls_x25519_calc_secret( x25519_ctx, olen, buf, blen, f_rng, p_rng );
99 }
100 
101 #endif /* MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED */
102 
103