Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame^] | 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 | #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 | |
| 42 | int 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 | |
| 56 | void 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 | |
| 68 | int 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 | { |
| 73 | int ret = 0; |
| 74 | size_t grp_len; |
| 75 | mbedtls_ecp_group grp; |
| 76 | mbedtls_ecdh_context_everest *everest_ctx = &ctx->ctx.everest_ecdh; |
| 77 | mbedtls_x25519_context *x25519_ctx = ( mbedtls_x25519_context* )everest_ctx->ctx; |
| 78 | |
| 79 | if( ctx->var != MBEDTLS_ECDH_VARIANT_EVEREST ) |
| 80 | return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
| 81 | |
| 82 | mbedtls_ecp_group_init( &grp ); |
| 83 | |
| 84 | if( ( ret = mbedtls_x25519_make_params( x25519_ctx, olen, buf, blen, f_rng, p_rng ) ) != 0 ) |
| 85 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
| 86 | |
| 87 | mbedtls_ecp_group_load( &grp, MBEDTLS_ECP_DP_CURVE25519 ); |
| 88 | ret = mbedtls_ecp_tls_write_group( &grp, &grp_len, buf, blen ); |
| 89 | mbedtls_ecp_group_free( &grp ); |
| 90 | if (ret != 0) |
| 91 | return( ret ); |
| 92 | |
| 93 | buf += grp_len; |
| 94 | blen -= grp_len; |
| 95 | |
| 96 | if( blen < 32 ) |
| 97 | return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL; |
| 98 | |
| 99 | memcpy( x25519_ctx->peer_point, buf, 32 ); |
| 100 | *olen = grp_len + 1 + 32; |
| 101 | return( ret ); |
| 102 | } |
| 103 | |
| 104 | int mbedtls_everest_read_params( mbedtls_ecdh_context *ctx, |
| 105 | const unsigned char **buf, const unsigned char *end ) |
| 106 | { |
| 107 | mbedtls_ecdh_context_everest *everest_ctx = &ctx->ctx.everest_ecdh; |
| 108 | mbedtls_x25519_context *x25519_ctx = ( mbedtls_x25519_context* )everest_ctx->ctx; |
| 109 | if( ctx->var != MBEDTLS_ECDH_VARIANT_EVEREST ) return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
| 110 | return mbedtls_x25519_read_params( x25519_ctx, buf, end ); |
| 111 | } |
| 112 | |
| 113 | int mbedtls_everest_get_params( mbedtls_ecdh_context *ctx, const mbedtls_ecp_keypair *key, |
| 114 | int side ) |
| 115 | { |
| 116 | mbedtls_ecdh_context_everest *everest_ctx = &ctx->ctx.everest_ecdh; |
| 117 | mbedtls_x25519_context *x25519_ctx = ( mbedtls_x25519_context* )everest_ctx->ctx; |
| 118 | if( ctx->var != MBEDTLS_ECDH_VARIANT_EVEREST ) return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
| 119 | return mbedtls_x25519_get_params( x25519_ctx, key, side ); |
| 120 | } |
| 121 | |
| 122 | int mbedtls_everest_make_public( mbedtls_ecdh_context *ctx, size_t *olen, |
| 123 | unsigned char *buf, size_t blen, |
| 124 | int( *f_rng )( void *, unsigned char *, size_t ), |
| 125 | void *p_rng ) |
| 126 | { |
| 127 | mbedtls_ecdh_context_everest *everest_ctx = &ctx->ctx.everest_ecdh; |
| 128 | mbedtls_x25519_context *x25519_ctx = ( mbedtls_x25519_context* )everest_ctx->ctx; |
| 129 | if( ctx->var != MBEDTLS_ECDH_VARIANT_EVEREST ) return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
| 130 | return mbedtls_x25519_make_public( x25519_ctx, olen, buf, blen, f_rng, p_rng ); |
| 131 | } |
| 132 | |
| 133 | int mbedtls_everest_read_public( mbedtls_ecdh_context *ctx, |
| 134 | const unsigned char *buf, size_t blen ) |
| 135 | { |
| 136 | mbedtls_ecdh_context_everest *everest_ctx = &ctx->ctx.everest_ecdh; |
| 137 | mbedtls_x25519_context *x25519_ctx = ( mbedtls_x25519_context* )everest_ctx->ctx; |
| 138 | if( ctx->var != MBEDTLS_ECDH_VARIANT_EVEREST ) return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
| 139 | return mbedtls_x25519_read_public ( x25519_ctx, buf, blen ); |
| 140 | } |
| 141 | |
| 142 | int mbedtls_everest_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen, |
| 143 | unsigned char *buf, size_t blen, |
| 144 | int( *f_rng )( void *, unsigned char *, size_t ), |
| 145 | void *p_rng ) |
| 146 | { |
| 147 | mbedtls_ecdh_context_everest *everest_ctx = &ctx->ctx.everest_ecdh; |
| 148 | mbedtls_x25519_context *x25519_ctx = ( mbedtls_x25519_context* )everest_ctx->ctx; |
| 149 | if( ctx->var != MBEDTLS_ECDH_VARIANT_EVEREST ) return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
| 150 | return mbedtls_x25519_calc_secret( x25519_ctx, olen, buf, blen, f_rng, p_rng ); |
| 151 | } |