blob: 2b111af00dfc07f4dba2e20173d2cea17df0a3f3 [file] [log] [blame]
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +01001/*
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#if !defined(MBEDTLS_CONFIG_FILE)
23#include "mbedtls/config.h"
24#else
25#include MBEDTLS_CONFIG_FILE
26#endif
27
28#include <string.h>
29
30#include "mbedtls/ecdh.h"
31
32#include "everest/x25519.h"
33#include "everest/everest.h"
34
35#if defined(MBEDTLS_PLATFORM_C)
36#include "mbedtls/platform.h"
37#else
38#define mbedtls_calloc calloc
39#define mbedtls_free free
40#endif
41
Christoph M. Wintersteiger6acfbb52018-12-07 13:19:53 +000042#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
43
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +010044int mbedtls_everest_setup( mbedtls_ecdh_context *ctx, int grp )
45{
46 if( grp != MBEDTLS_ECP_DP_CURVE25519 )
47 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
48
49 ctx->var = MBEDTLS_ECDH_VARIANT_EVEREST;
50 ctx->grp_id = grp;
51
52 ctx->ctx.everest_ecdh.ctx = mbedtls_calloc( 1, sizeof( mbedtls_x25519_context ) );
53 mbedtls_x25519_init( ctx->ctx.everest_ecdh.ctx );
54
55 return 0;
56}
57
58void mbedtls_everest_free( mbedtls_ecdh_context *ctx )
59{
60 mbedtls_ecdh_context_everest *everest_ctx = &ctx->ctx.everest_ecdh;
61 mbedtls_x25519_context *x25519_ctx = ( mbedtls_x25519_context* )everest_ctx->ctx;
62
63 mbedtls_x25519_free( x25519_ctx );
64 mbedtls_free( x25519_ctx );
65
66 ctx->var = MBEDTLS_ECDH_VARIANT_NONE;
67 ctx->grp_id = MBEDTLS_ECP_DP_NONE;
68}
69
70int mbedtls_everest_make_params( mbedtls_ecdh_context *ctx, size_t *olen,
71 unsigned char *buf, size_t blen,
72 int( *f_rng )( void *, unsigned char *, size_t ),
73 void *p_rng )
74{
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +010075 mbedtls_ecdh_context_everest *everest_ctx = &ctx->ctx.everest_ecdh;
76 mbedtls_x25519_context *x25519_ctx = ( mbedtls_x25519_context* )everest_ctx->ctx;
Christoph M. Wintersteigerfb723672018-12-06 17:23:07 +000077 if( ctx->var != MBEDTLS_ECDH_VARIANT_EVEREST ) return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
78 return mbedtls_x25519_make_params( x25519_ctx, olen, buf, blen, f_rng, p_rng );
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +010079}
80
81int mbedtls_everest_read_params( mbedtls_ecdh_context *ctx,
82 const unsigned char **buf, const unsigned char *end )
83{
84 mbedtls_ecdh_context_everest *everest_ctx = &ctx->ctx.everest_ecdh;
85 mbedtls_x25519_context *x25519_ctx = ( mbedtls_x25519_context* )everest_ctx->ctx;
86 if( ctx->var != MBEDTLS_ECDH_VARIANT_EVEREST ) return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
87 return mbedtls_x25519_read_params( x25519_ctx, buf, end );
88}
89
90int mbedtls_everest_get_params( mbedtls_ecdh_context *ctx, const mbedtls_ecp_keypair *key,
91 int side )
92{
93 mbedtls_ecdh_context_everest *everest_ctx = &ctx->ctx.everest_ecdh;
94 mbedtls_x25519_context *x25519_ctx = ( mbedtls_x25519_context* )everest_ctx->ctx;
95 if( ctx->var != MBEDTLS_ECDH_VARIANT_EVEREST ) return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
96 return mbedtls_x25519_get_params( x25519_ctx, key, side );
97}
98
99int mbedtls_everest_make_public( mbedtls_ecdh_context *ctx, size_t *olen,
100 unsigned char *buf, size_t blen,
101 int( *f_rng )( void *, unsigned char *, size_t ),
102 void *p_rng )
103{
104 mbedtls_ecdh_context_everest *everest_ctx = &ctx->ctx.everest_ecdh;
105 mbedtls_x25519_context *x25519_ctx = ( mbedtls_x25519_context* )everest_ctx->ctx;
106 if( ctx->var != MBEDTLS_ECDH_VARIANT_EVEREST ) return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
107 return mbedtls_x25519_make_public( x25519_ctx, olen, buf, blen, f_rng, p_rng );
108}
109
110int mbedtls_everest_read_public( mbedtls_ecdh_context *ctx,
111 const unsigned char *buf, size_t blen )
112{
113 mbedtls_ecdh_context_everest *everest_ctx = &ctx->ctx.everest_ecdh;
114 mbedtls_x25519_context *x25519_ctx = ( mbedtls_x25519_context* )everest_ctx->ctx;
115 if( ctx->var != MBEDTLS_ECDH_VARIANT_EVEREST ) return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
116 return mbedtls_x25519_read_public ( x25519_ctx, buf, blen );
117}
118
119int mbedtls_everest_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
120 unsigned char *buf, size_t blen,
121 int( *f_rng )( void *, unsigned char *, size_t ),
122 void *p_rng )
123{
124 mbedtls_ecdh_context_everest *everest_ctx = &ctx->ctx.everest_ecdh;
125 mbedtls_x25519_context *x25519_ctx = ( mbedtls_x25519_context* )everest_ctx->ctx;
126 if( ctx->var != MBEDTLS_ECDH_VARIANT_EVEREST ) return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
127 return mbedtls_x25519_calc_secret( x25519_ctx, olen, buf, blen, f_rng, p_rng );
128}
Christoph M. Wintersteiger6acfbb52018-12-07 13:19:53 +0000129
130#endif /* MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED */