blob: da1e426f97fd0c1729557286d5ea4a18374ad86a [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
42int mbedtls_everest_setup( mbedtls_ecdh_context *ctx, int grp )
43{
44 if( grp != MBEDTLS_ECP_DP_CURVE25519 )
45 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
46
47 ctx->var = MBEDTLS_ECDH_VARIANT_EVEREST;
48 ctx->grp_id = grp;
49
50 ctx->ctx.everest_ecdh.ctx = mbedtls_calloc( 1, sizeof( mbedtls_x25519_context ) );
51 mbedtls_x25519_init( ctx->ctx.everest_ecdh.ctx );
52
53 return 0;
54}
55
56void mbedtls_everest_free( mbedtls_ecdh_context *ctx )
57{
58 mbedtls_ecdh_context_everest *everest_ctx = &ctx->ctx.everest_ecdh;
59 mbedtls_x25519_context *x25519_ctx = ( mbedtls_x25519_context* )everest_ctx->ctx;
60
61 mbedtls_x25519_free( x25519_ctx );
62 mbedtls_free( x25519_ctx );
63
64 ctx->var = MBEDTLS_ECDH_VARIANT_NONE;
65 ctx->grp_id = MBEDTLS_ECP_DP_NONE;
66}
67
68int mbedtls_everest_make_params( mbedtls_ecdh_context *ctx, size_t *olen,
69 unsigned char *buf, size_t blen,
70 int( *f_rng )( void *, unsigned char *, size_t ),
71 void *p_rng )
72{
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +010073 mbedtls_ecdh_context_everest *everest_ctx = &ctx->ctx.everest_ecdh;
74 mbedtls_x25519_context *x25519_ctx = ( mbedtls_x25519_context* )everest_ctx->ctx;
Christoph M. Wintersteigerfb723672018-12-06 17:23:07 +000075 if( ctx->var != MBEDTLS_ECDH_VARIANT_EVEREST ) return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
76 return mbedtls_x25519_make_params( x25519_ctx, olen, buf, blen, f_rng, p_rng );
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +010077}
78
79int mbedtls_everest_read_params( mbedtls_ecdh_context *ctx,
80 const unsigned char **buf, const unsigned char *end )
81{
82 mbedtls_ecdh_context_everest *everest_ctx = &ctx->ctx.everest_ecdh;
83 mbedtls_x25519_context *x25519_ctx = ( mbedtls_x25519_context* )everest_ctx->ctx;
84 if( ctx->var != MBEDTLS_ECDH_VARIANT_EVEREST ) return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
85 return mbedtls_x25519_read_params( x25519_ctx, buf, end );
86}
87
88int mbedtls_everest_get_params( mbedtls_ecdh_context *ctx, const mbedtls_ecp_keypair *key,
89 int side )
90{
91 mbedtls_ecdh_context_everest *everest_ctx = &ctx->ctx.everest_ecdh;
92 mbedtls_x25519_context *x25519_ctx = ( mbedtls_x25519_context* )everest_ctx->ctx;
93 if( ctx->var != MBEDTLS_ECDH_VARIANT_EVEREST ) return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
94 return mbedtls_x25519_get_params( x25519_ctx, key, side );
95}
96
97int mbedtls_everest_make_public( mbedtls_ecdh_context *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_ecdh_context_everest *everest_ctx = &ctx->ctx.everest_ecdh;
103 mbedtls_x25519_context *x25519_ctx = ( mbedtls_x25519_context* )everest_ctx->ctx;
104 if( ctx->var != MBEDTLS_ECDH_VARIANT_EVEREST ) return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
105 return mbedtls_x25519_make_public( x25519_ctx, olen, buf, blen, f_rng, p_rng );
106}
107
108int mbedtls_everest_read_public( mbedtls_ecdh_context *ctx,
109 const unsigned char *buf, size_t blen )
110{
111 mbedtls_ecdh_context_everest *everest_ctx = &ctx->ctx.everest_ecdh;
112 mbedtls_x25519_context *x25519_ctx = ( mbedtls_x25519_context* )everest_ctx->ctx;
113 if( ctx->var != MBEDTLS_ECDH_VARIANT_EVEREST ) return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
114 return mbedtls_x25519_read_public ( x25519_ctx, buf, blen );
115}
116
117int mbedtls_everest_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
118 unsigned char *buf, size_t blen,
119 int( *f_rng )( void *, unsigned char *, size_t ),
120 void *p_rng )
121{
122 mbedtls_ecdh_context_everest *everest_ctx = &ctx->ctx.everest_ecdh;
123 mbedtls_x25519_context *x25519_ctx = ( mbedtls_x25519_context* )everest_ctx->ctx;
124 if( ctx->var != MBEDTLS_ECDH_VARIANT_EVEREST ) return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
125 return mbedtls_x25519_calc_secret( x25519_ctx, olen, buf, blen, f_rng, p_rng );
126}