mbed TLS Build

Dependents:   Slave-prot-prod

Committer:
markrad
Date:
Thu Jan 05 00:18:44 2017 +0000
Revision:
0:cdf462088d13
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
markrad 0:cdf462088d13 1 /**
markrad 0:cdf462088d13 2 * \file ecdh.h
markrad 0:cdf462088d13 3 *
markrad 0:cdf462088d13 4 * \brief Elliptic curve Diffie-Hellman
markrad 0:cdf462088d13 5 *
markrad 0:cdf462088d13 6 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
markrad 0:cdf462088d13 7 * SPDX-License-Identifier: Apache-2.0
markrad 0:cdf462088d13 8 *
markrad 0:cdf462088d13 9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
markrad 0:cdf462088d13 10 * not use this file except in compliance with the License.
markrad 0:cdf462088d13 11 * You may obtain a copy of the License at
markrad 0:cdf462088d13 12 *
markrad 0:cdf462088d13 13 * http://www.apache.org/licenses/LICENSE-2.0
markrad 0:cdf462088d13 14 *
markrad 0:cdf462088d13 15 * Unless required by applicable law or agreed to in writing, software
markrad 0:cdf462088d13 16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
markrad 0:cdf462088d13 17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
markrad 0:cdf462088d13 18 * See the License for the specific language governing permissions and
markrad 0:cdf462088d13 19 * limitations under the License.
markrad 0:cdf462088d13 20 *
markrad 0:cdf462088d13 21 * This file is part of mbed TLS (https://tls.mbed.org)
markrad 0:cdf462088d13 22 */
markrad 0:cdf462088d13 23 #ifndef MBEDTLS_ECDH_H
markrad 0:cdf462088d13 24 #define MBEDTLS_ECDH_H
markrad 0:cdf462088d13 25
markrad 0:cdf462088d13 26 #include "ecp.h"
markrad 0:cdf462088d13 27
markrad 0:cdf462088d13 28 #ifdef __cplusplus
markrad 0:cdf462088d13 29 extern "C" {
markrad 0:cdf462088d13 30 #endif
markrad 0:cdf462088d13 31
markrad 0:cdf462088d13 32 /**
markrad 0:cdf462088d13 33 * When importing from an EC key, select if it is our key or the peer's key
markrad 0:cdf462088d13 34 */
markrad 0:cdf462088d13 35 typedef enum
markrad 0:cdf462088d13 36 {
markrad 0:cdf462088d13 37 MBEDTLS_ECDH_OURS,
markrad 0:cdf462088d13 38 MBEDTLS_ECDH_THEIRS,
markrad 0:cdf462088d13 39 } mbedtls_ecdh_side;
markrad 0:cdf462088d13 40
markrad 0:cdf462088d13 41 /**
markrad 0:cdf462088d13 42 * \brief ECDH context structure
markrad 0:cdf462088d13 43 */
markrad 0:cdf462088d13 44 typedef struct
markrad 0:cdf462088d13 45 {
markrad 0:cdf462088d13 46 mbedtls_ecp_group grp; /*!< elliptic curve used */
markrad 0:cdf462088d13 47 mbedtls_mpi d; /*!< our secret value (private key) */
markrad 0:cdf462088d13 48 mbedtls_ecp_point Q; /*!< our public value (public key) */
markrad 0:cdf462088d13 49 mbedtls_ecp_point Qp; /*!< peer's public value (public key) */
markrad 0:cdf462088d13 50 mbedtls_mpi z; /*!< shared secret */
markrad 0:cdf462088d13 51 int point_format; /*!< format for point export in TLS messages */
markrad 0:cdf462088d13 52 mbedtls_ecp_point Vi; /*!< blinding value (for later) */
markrad 0:cdf462088d13 53 mbedtls_ecp_point Vf; /*!< un-blinding value (for later) */
markrad 0:cdf462088d13 54 mbedtls_mpi _d; /*!< previous d (for later) */
markrad 0:cdf462088d13 55 }
markrad 0:cdf462088d13 56 mbedtls_ecdh_context;
markrad 0:cdf462088d13 57
markrad 0:cdf462088d13 58 /**
markrad 0:cdf462088d13 59 * \brief Generate a public key.
markrad 0:cdf462088d13 60 * Raw function that only does the core computation.
markrad 0:cdf462088d13 61 *
markrad 0:cdf462088d13 62 * \param grp ECP group
markrad 0:cdf462088d13 63 * \param d Destination MPI (secret exponent, aka private key)
markrad 0:cdf462088d13 64 * \param Q Destination point (public key)
markrad 0:cdf462088d13 65 * \param f_rng RNG function
markrad 0:cdf462088d13 66 * \param p_rng RNG parameter
markrad 0:cdf462088d13 67 *
markrad 0:cdf462088d13 68 * \return 0 if successful,
markrad 0:cdf462088d13 69 * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
markrad 0:cdf462088d13 70 */
markrad 0:cdf462088d13 71 int mbedtls_ecdh_gen_public( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
markrad 0:cdf462088d13 72 int (*f_rng)(void *, unsigned char *, size_t),
markrad 0:cdf462088d13 73 void *p_rng );
markrad 0:cdf462088d13 74
markrad 0:cdf462088d13 75 /**
markrad 0:cdf462088d13 76 * \brief Compute shared secret
markrad 0:cdf462088d13 77 * Raw function that only does the core computation.
markrad 0:cdf462088d13 78 *
markrad 0:cdf462088d13 79 * \param grp ECP group
markrad 0:cdf462088d13 80 * \param z Destination MPI (shared secret)
markrad 0:cdf462088d13 81 * \param Q Public key from other party
markrad 0:cdf462088d13 82 * \param d Our secret exponent (private key)
markrad 0:cdf462088d13 83 * \param f_rng RNG function (see notes)
markrad 0:cdf462088d13 84 * \param p_rng RNG parameter
markrad 0:cdf462088d13 85 *
markrad 0:cdf462088d13 86 * \return 0 if successful,
markrad 0:cdf462088d13 87 * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
markrad 0:cdf462088d13 88 *
markrad 0:cdf462088d13 89 * \note If f_rng is not NULL, it is used to implement
markrad 0:cdf462088d13 90 * countermeasures against potential elaborate timing
markrad 0:cdf462088d13 91 * attacks, see \c mbedtls_ecp_mul() for details.
markrad 0:cdf462088d13 92 */
markrad 0:cdf462088d13 93 int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z,
markrad 0:cdf462088d13 94 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
markrad 0:cdf462088d13 95 int (*f_rng)(void *, unsigned char *, size_t),
markrad 0:cdf462088d13 96 void *p_rng );
markrad 0:cdf462088d13 97
markrad 0:cdf462088d13 98 /**
markrad 0:cdf462088d13 99 * \brief Initialize context
markrad 0:cdf462088d13 100 *
markrad 0:cdf462088d13 101 * \param ctx Context to initialize
markrad 0:cdf462088d13 102 */
markrad 0:cdf462088d13 103 void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx );
markrad 0:cdf462088d13 104
markrad 0:cdf462088d13 105 /**
markrad 0:cdf462088d13 106 * \brief Free context
markrad 0:cdf462088d13 107 *
markrad 0:cdf462088d13 108 * \param ctx Context to free
markrad 0:cdf462088d13 109 */
markrad 0:cdf462088d13 110 void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx );
markrad 0:cdf462088d13 111
markrad 0:cdf462088d13 112 /**
markrad 0:cdf462088d13 113 * \brief Generate a public key and a TLS ServerKeyExchange payload.
markrad 0:cdf462088d13 114 * (First function used by a TLS server for ECDHE.)
markrad 0:cdf462088d13 115 *
markrad 0:cdf462088d13 116 * \param ctx ECDH context
markrad 0:cdf462088d13 117 * \param olen number of chars written
markrad 0:cdf462088d13 118 * \param buf destination buffer
markrad 0:cdf462088d13 119 * \param blen length of buffer
markrad 0:cdf462088d13 120 * \param f_rng RNG function
markrad 0:cdf462088d13 121 * \param p_rng RNG parameter
markrad 0:cdf462088d13 122 *
markrad 0:cdf462088d13 123 * \note This function assumes that ctx->grp has already been
markrad 0:cdf462088d13 124 * properly set (for example using mbedtls_ecp_group_load).
markrad 0:cdf462088d13 125 *
markrad 0:cdf462088d13 126 * \return 0 if successful, or an MBEDTLS_ERR_ECP_XXX error code
markrad 0:cdf462088d13 127 */
markrad 0:cdf462088d13 128 int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen,
markrad 0:cdf462088d13 129 unsigned char *buf, size_t blen,
markrad 0:cdf462088d13 130 int (*f_rng)(void *, unsigned char *, size_t),
markrad 0:cdf462088d13 131 void *p_rng );
markrad 0:cdf462088d13 132
markrad 0:cdf462088d13 133 /**
markrad 0:cdf462088d13 134 * \brief Parse and procress a TLS ServerKeyExhange payload.
markrad 0:cdf462088d13 135 * (First function used by a TLS client for ECDHE.)
markrad 0:cdf462088d13 136 *
markrad 0:cdf462088d13 137 * \param ctx ECDH context
markrad 0:cdf462088d13 138 * \param buf pointer to start of input buffer
markrad 0:cdf462088d13 139 * \param end one past end of buffer
markrad 0:cdf462088d13 140 *
markrad 0:cdf462088d13 141 * \return 0 if successful, or an MBEDTLS_ERR_ECP_XXX error code
markrad 0:cdf462088d13 142 */
markrad 0:cdf462088d13 143 int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx,
markrad 0:cdf462088d13 144 const unsigned char **buf, const unsigned char *end );
markrad 0:cdf462088d13 145
markrad 0:cdf462088d13 146 /**
markrad 0:cdf462088d13 147 * \brief Setup an ECDH context from an EC key.
markrad 0:cdf462088d13 148 * (Used by clients and servers in place of the
markrad 0:cdf462088d13 149 * ServerKeyEchange for static ECDH: import ECDH parameters
markrad 0:cdf462088d13 150 * from a certificate's EC key information.)
markrad 0:cdf462088d13 151 *
markrad 0:cdf462088d13 152 * \param ctx ECDH constext to set
markrad 0:cdf462088d13 153 * \param key EC key to use
markrad 0:cdf462088d13 154 * \param side Is it our key (1) or the peer's key (0) ?
markrad 0:cdf462088d13 155 *
markrad 0:cdf462088d13 156 * \return 0 if successful, or an MBEDTLS_ERR_ECP_XXX error code
markrad 0:cdf462088d13 157 */
markrad 0:cdf462088d13 158 int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx, const mbedtls_ecp_keypair *key,
markrad 0:cdf462088d13 159 mbedtls_ecdh_side side );
markrad 0:cdf462088d13 160
markrad 0:cdf462088d13 161 /**
markrad 0:cdf462088d13 162 * \brief Generate a public key and a TLS ClientKeyExchange payload.
markrad 0:cdf462088d13 163 * (Second function used by a TLS client for ECDH(E).)
markrad 0:cdf462088d13 164 *
markrad 0:cdf462088d13 165 * \param ctx ECDH context
markrad 0:cdf462088d13 166 * \param olen number of bytes actually written
markrad 0:cdf462088d13 167 * \param buf destination buffer
markrad 0:cdf462088d13 168 * \param blen size of destination buffer
markrad 0:cdf462088d13 169 * \param f_rng RNG function
markrad 0:cdf462088d13 170 * \param p_rng RNG parameter
markrad 0:cdf462088d13 171 *
markrad 0:cdf462088d13 172 * \return 0 if successful, or an MBEDTLS_ERR_ECP_XXX error code
markrad 0:cdf462088d13 173 */
markrad 0:cdf462088d13 174 int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen,
markrad 0:cdf462088d13 175 unsigned char *buf, size_t blen,
markrad 0:cdf462088d13 176 int (*f_rng)(void *, unsigned char *, size_t),
markrad 0:cdf462088d13 177 void *p_rng );
markrad 0:cdf462088d13 178
markrad 0:cdf462088d13 179 /**
markrad 0:cdf462088d13 180 * \brief Parse and process a TLS ClientKeyExchange payload.
markrad 0:cdf462088d13 181 * (Second function used by a TLS server for ECDH(E).)
markrad 0:cdf462088d13 182 *
markrad 0:cdf462088d13 183 * \param ctx ECDH context
markrad 0:cdf462088d13 184 * \param buf start of input buffer
markrad 0:cdf462088d13 185 * \param blen length of input buffer
markrad 0:cdf462088d13 186 *
markrad 0:cdf462088d13 187 * \return 0 if successful, or an MBEDTLS_ERR_ECP_XXX error code
markrad 0:cdf462088d13 188 */
markrad 0:cdf462088d13 189 int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx,
markrad 0:cdf462088d13 190 const unsigned char *buf, size_t blen );
markrad 0:cdf462088d13 191
markrad 0:cdf462088d13 192 /**
markrad 0:cdf462088d13 193 * \brief Derive and export the shared secret.
markrad 0:cdf462088d13 194 * (Last function used by both TLS client en servers.)
markrad 0:cdf462088d13 195 *
markrad 0:cdf462088d13 196 * \param ctx ECDH context
markrad 0:cdf462088d13 197 * \param olen number of bytes written
markrad 0:cdf462088d13 198 * \param buf destination buffer
markrad 0:cdf462088d13 199 * \param blen buffer length
markrad 0:cdf462088d13 200 * \param f_rng RNG function, see notes for \c mbedtls_ecdh_compute_shared()
markrad 0:cdf462088d13 201 * \param p_rng RNG parameter
markrad 0:cdf462088d13 202 *
markrad 0:cdf462088d13 203 * \return 0 if successful, or an MBEDTLS_ERR_ECP_XXX error code
markrad 0:cdf462088d13 204 */
markrad 0:cdf462088d13 205 int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
markrad 0:cdf462088d13 206 unsigned char *buf, size_t blen,
markrad 0:cdf462088d13 207 int (*f_rng)(void *, unsigned char *, size_t),
markrad 0:cdf462088d13 208 void *p_rng );
markrad 0:cdf462088d13 209
markrad 0:cdf462088d13 210 #ifdef __cplusplus
markrad 0:cdf462088d13 211 }
markrad 0:cdf462088d13 212 #endif
markrad 0:cdf462088d13 213
markrad 0:cdf462088d13 214 #endif /* ecdh.h */