ecdh.h
Go to the documentation of this file.
/*
* Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This file is part of Mbed TLS (https://tls.mbed.org)
*/
#ifndef MBEDTLS_ECDH_H
#define MBEDTLS_ECDH_H
#if !defined(MBEDTLS_CONFIG_FILE)
#include "config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif
#include "ecp.h"
/*
* Use a backward compatible ECDH context.
*
* This flag is always enabled for now and future versions might add a
* configuration option that conditionally undefines this flag.
* The configuration option in question may have a different name.
*
* Features undefining this flag, must have a warning in their description in
* config.h stating that the feature breaks backward compatibility.
*/
#define MBEDTLS_ECDH_LEGACY_CONTEXT
#ifdef __cplusplus
extern "C" {
#endif
typedef enum
{
MBEDTLS_ECDH_OURS,
MBEDTLS_ECDH_THEIRS,
} mbedtls_ecdh_side;
#if !defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
typedef enum
{
MBEDTLS_ECDH_VARIANT_NONE = 0,
MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0,
} mbedtls_ecdh_variant;
typedef struct mbedtls_ecdh_context_mbed
{
mbedtls_ecp_group grp;
mbedtls_mpi d;
mbedtls_ecp_point Q;
mbedtls_ecp_point Qp;
mbedtls_mpi z;
#if defined(MBEDTLS_ECP_RESTARTABLE)
mbedtls_ecp_restart_ctx rs;
#endif
} mbedtls_ecdh_context_mbed;
#endif
typedef struct mbedtls_ecdh_context
{
#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
mbedtls_ecp_group grp;
mbedtls_mpi d;
mbedtls_ecp_point Q;
mbedtls_ecp_point Qp;
mbedtls_mpi z;
int point_format;
mbedtls_ecp_point Vi;
mbedtls_ecp_point Vf;
mbedtls_mpi _d;
#if defined(MBEDTLS_ECP_RESTARTABLE)
int restart_enabled;
mbedtls_ecp_restart_ctx rs;
#endif /* MBEDTLS_ECP_RESTARTABLE */
#else
uint8_t point_format;
mbedtls_ecp_group_id grp_id;
mbedtls_ecdh_variant var;
union
{
mbedtls_ecdh_context_mbed mbed_ecdh;
} ctx;
#if defined(MBEDTLS_ECP_RESTARTABLE)
uint8_t restart_enabled;
#endif /* MBEDTLS_ECP_RESTARTABLE */
#endif /* MBEDTLS_ECDH_LEGACY_CONTEXT */
}
mbedtls_ecdh_context;
int mbedtls_ecdh_gen_public( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng );
int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z,
const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng );
void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx );
int mbedtls_ecdh_setup( mbedtls_ecdh_context *ctx,
mbedtls_ecp_group_id grp_id );
void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx );
int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen,
unsigned char *buf, size_t blen,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng );
int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx,
const unsigned char **buf,
const unsigned char *end );
int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx,
const mbedtls_ecp_keypair *key,
mbedtls_ecdh_side side );
int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen,
unsigned char *buf, size_t blen,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng );
int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx,
const unsigned char *buf, size_t blen );
int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
unsigned char *buf, size_t blen,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng );
#if defined(MBEDTLS_ECP_RESTARTABLE)
void mbedtls_ecdh_enable_restart( mbedtls_ecdh_context *ctx );
#endif /* MBEDTLS_ECP_RESTARTABLE */
#ifdef __cplusplus
}
#endif
#endif /* ecdh.h */
mbedtls_ecdh_context::z
mbedtls_mpi z
Definition: ecdh.h:117
mbedtls_ecp_group
The ECP group structure.
Definition: ecp.h:173
ecp.h
This file provides an API for Elliptic Curves over GF(P) (ECP).
mbedtls_ecdh_calc_secret
int mbedtls_ecdh_calc_secret(mbedtls_ecdh_context *ctx, size_t *olen, unsigned char *buf, size_t blen, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
This function derives and exports the shared secret.
mbedtls_ecdh_context::d
mbedtls_mpi d
Definition: ecdh.h:114
mbedtls_mpi
MPI structure.
Definition: bignum.h:185
mbedtls_ecdh_init
void mbedtls_ecdh_init(mbedtls_ecdh_context *ctx)
This function initializes an ECDH context.
mbedtls_ecdh_make_params
int mbedtls_ecdh_make_params(mbedtls_ecdh_context *ctx, size_t *olen, unsigned char *buf, size_t blen, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
This function generates an EC key pair and exports its in the format used in a TLS ServerKeyExchange ...
mbedtls_ecdh_context
The ECDH context structure.
Definition: ecdh.h:110
mbedtls_ecdh_context
struct mbedtls_ecdh_context mbedtls_ecdh_context
The ECDH context structure.
mbedtls_ecdh_context::_d
mbedtls_mpi _d
Definition: ecdh.h:121
mbedtls_ecp_restart_ctx
void mbedtls_ecp_restart_ctx
Definition: ecp.h:320
mbedtls_ecdh_context::Vi
mbedtls_ecp_point Vi
Definition: ecdh.h:119
mbedtls_ecdh_context::grp
mbedtls_ecp_group grp
Definition: ecdh.h:113
mbedtls_ecp_keypair
The ECP key-pair structure.
Definition: ecp.h:332
mbedtls_ecp_group_id
mbedtls_ecp_group_id
Definition: ecp.h:77
mbedtls_ecdh_context::point_format
int point_format
Definition: ecdh.h:118
mbedtls_ecdh_context::Qp
mbedtls_ecp_point Qp
Definition: ecdh.h:116
mbedtls_ecdh_context::Q
mbedtls_ecp_point Q
Definition: ecdh.h:115
mbedtls_ecdh_gen_public
int mbedtls_ecdh_gen_public(mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
This function generates an ECDH keypair on an elliptic curve.
mbedtls_ecdh_free
void mbedtls_ecdh_free(mbedtls_ecdh_context *ctx)
This function frees a context.
mbedtls_ecdh_side
mbedtls_ecdh_side
Definition: ecdh.h:64
MBEDTLS_ECDH_THEIRS
Definition: ecdh.h:67
mbedtls_ecdh_context::Vf
mbedtls_ecp_point Vf
Definition: ecdh.h:120
mbedtls_ecdh_read_public
int mbedtls_ecdh_read_public(mbedtls_ecdh_context *ctx, const unsigned char *buf, size_t blen)
This function parses and processes the ECDHE payload of a TLS ClientKeyExchange message.
mbedtls_ecdh_make_public
int mbedtls_ecdh_make_public(mbedtls_ecdh_context *ctx, size_t *olen, unsigned char *buf, size_t blen, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
This function generates a public key and exports it as a TLS ClientKeyExchange payload.
config.h
Configuration options (set of defines)
mbedtls_ecdh_compute_shared
int mbedtls_ecdh_compute_shared(mbedtls_ecp_group *grp, mbedtls_mpi *z, const mbedtls_ecp_point *Q, const mbedtls_mpi *d, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
This function computes the shared secret.
mbedtls_ecp_point
The ECP point structure, in Jacobian coordinates.
Definition: ecp.h:124
MBEDTLS_ECDH_OURS
Definition: ecdh.h:66
mbedtls_ecdh_read_params
int mbedtls_ecdh_read_params(mbedtls_ecdh_context *ctx, const unsigned char **buf, const unsigned char *end)
This function parses the ECDHE parameters in a TLS ServerKeyExchange handshake message.
mbedtls_ecdh_get_params
int mbedtls_ecdh_get_params(mbedtls_ecdh_context *ctx, const mbedtls_ecp_keypair *key, mbedtls_ecdh_side side)
This function sets up an ECDH context from an EC key.
mbedtls_ecdh_setup
int mbedtls_ecdh_setup(mbedtls_ecdh_context *ctx, mbedtls_ecp_group_id grp_id)
This function sets up the ECDH context with the information given.