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