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 ecjpake.h
markrad 0:cdf462088d13 3 *
markrad 0:cdf462088d13 4 * \brief Elliptic curve J-PAKE
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_ECJPAKE_H
markrad 0:cdf462088d13 24 #define MBEDTLS_ECJPAKE_H
markrad 0:cdf462088d13 25
markrad 0:cdf462088d13 26 /*
markrad 0:cdf462088d13 27 * J-PAKE is a password-authenticated key exchange that allows deriving a
markrad 0:cdf462088d13 28 * strong shared secret from a (potentially low entropy) pre-shared
markrad 0:cdf462088d13 29 * passphrase, with forward secrecy and mutual authentication.
markrad 0:cdf462088d13 30 * https://en.wikipedia.org/wiki/Password_Authenticated_Key_Exchange_by_Juggling
markrad 0:cdf462088d13 31 *
markrad 0:cdf462088d13 32 * This file implements the Elliptic Curve variant of J-PAKE,
markrad 0:cdf462088d13 33 * as defined in Chapter 7.4 of the Thread v1.0 Specification,
markrad 0:cdf462088d13 34 * available to members of the Thread Group http://threadgroup.org/
markrad 0:cdf462088d13 35 *
markrad 0:cdf462088d13 36 * As the J-PAKE algorithm is inherently symmetric, so is our API.
markrad 0:cdf462088d13 37 * Each party needs to send its first round message, in any order, to the
markrad 0:cdf462088d13 38 * other party, then each sends its second round message, in any order.
markrad 0:cdf462088d13 39 * The payloads are serialized in a way suitable for use in TLS, but could
markrad 0:cdf462088d13 40 * also be use outside TLS.
markrad 0:cdf462088d13 41 */
markrad 0:cdf462088d13 42
markrad 0:cdf462088d13 43 #include "ecp.h"
markrad 0:cdf462088d13 44 #include "md.h"
markrad 0:cdf462088d13 45
markrad 0:cdf462088d13 46 #ifdef __cplusplus
markrad 0:cdf462088d13 47 extern "C" {
markrad 0:cdf462088d13 48 #endif
markrad 0:cdf462088d13 49
markrad 0:cdf462088d13 50 /**
markrad 0:cdf462088d13 51 * Roles in the EC J-PAKE exchange
markrad 0:cdf462088d13 52 */
markrad 0:cdf462088d13 53 typedef enum {
markrad 0:cdf462088d13 54 MBEDTLS_ECJPAKE_CLIENT = 0, /**< Client */
markrad 0:cdf462088d13 55 MBEDTLS_ECJPAKE_SERVER, /**< Server */
markrad 0:cdf462088d13 56 } mbedtls_ecjpake_role;
markrad 0:cdf462088d13 57
markrad 0:cdf462088d13 58 /**
markrad 0:cdf462088d13 59 * EC J-PAKE context structure.
markrad 0:cdf462088d13 60 *
markrad 0:cdf462088d13 61 * J-PAKE is a symmetric protocol, except for the identifiers used in
markrad 0:cdf462088d13 62 * Zero-Knowledge Proofs, and the serialization of the second message
markrad 0:cdf462088d13 63 * (KeyExchange) as defined by the Thread spec.
markrad 0:cdf462088d13 64 *
markrad 0:cdf462088d13 65 * In order to benefit from this symmetry, we choose a different naming
markrad 0:cdf462088d13 66 * convetion from the Thread v1.0 spec. Correspondance is indicated in the
markrad 0:cdf462088d13 67 * description as a pair C: client name, S: server name
markrad 0:cdf462088d13 68 */
markrad 0:cdf462088d13 69 typedef struct
markrad 0:cdf462088d13 70 {
markrad 0:cdf462088d13 71 const mbedtls_md_info_t *md_info; /**< Hash to use */
markrad 0:cdf462088d13 72 mbedtls_ecp_group grp; /**< Elliptic curve */
markrad 0:cdf462088d13 73 mbedtls_ecjpake_role role; /**< Are we client or server? */
markrad 0:cdf462088d13 74 int point_format; /**< Format for point export */
markrad 0:cdf462088d13 75
markrad 0:cdf462088d13 76 mbedtls_ecp_point Xm1; /**< My public key 1 C: X1, S: X3 */
markrad 0:cdf462088d13 77 mbedtls_ecp_point Xm2; /**< My public key 2 C: X2, S: X4 */
markrad 0:cdf462088d13 78 mbedtls_ecp_point Xp1; /**< Peer public key 1 C: X3, S: X1 */
markrad 0:cdf462088d13 79 mbedtls_ecp_point Xp2; /**< Peer public key 2 C: X4, S: X2 */
markrad 0:cdf462088d13 80 mbedtls_ecp_point Xp; /**< Peer public key C: Xs, S: Xc */
markrad 0:cdf462088d13 81
markrad 0:cdf462088d13 82 mbedtls_mpi xm1; /**< My private key 1 C: x1, S: x3 */
markrad 0:cdf462088d13 83 mbedtls_mpi xm2; /**< My private key 2 C: x2, S: x4 */
markrad 0:cdf462088d13 84
markrad 0:cdf462088d13 85 mbedtls_mpi s; /**< Pre-shared secret (passphrase) */
markrad 0:cdf462088d13 86 } mbedtls_ecjpake_context;
markrad 0:cdf462088d13 87
markrad 0:cdf462088d13 88 /**
markrad 0:cdf462088d13 89 * \brief Initialize a context
markrad 0:cdf462088d13 90 * (just makes it ready for setup() or free()).
markrad 0:cdf462088d13 91 *
markrad 0:cdf462088d13 92 * \param ctx context to initialize
markrad 0:cdf462088d13 93 */
markrad 0:cdf462088d13 94 void mbedtls_ecjpake_init( mbedtls_ecjpake_context *ctx );
markrad 0:cdf462088d13 95
markrad 0:cdf462088d13 96 /**
markrad 0:cdf462088d13 97 * \brief Set up a context for use
markrad 0:cdf462088d13 98 *
markrad 0:cdf462088d13 99 * \note Currently the only values for hash/curve allowed by the
markrad 0:cdf462088d13 100 * standard are MBEDTLS_MD_SHA256/MBEDTLS_ECP_DP_SECP256R1.
markrad 0:cdf462088d13 101 *
markrad 0:cdf462088d13 102 * \param ctx context to set up
markrad 0:cdf462088d13 103 * \param role Our role: client or server
markrad 0:cdf462088d13 104 * \param hash hash function to use (MBEDTLS_MD_XXX)
markrad 0:cdf462088d13 105 * \param curve elliptic curve identifier (MBEDTLS_ECP_DP_XXX)
markrad 0:cdf462088d13 106 * \param secret pre-shared secret (passphrase)
markrad 0:cdf462088d13 107 * \param len length of the shared secret
markrad 0:cdf462088d13 108 *
markrad 0:cdf462088d13 109 * \return 0 if successfull,
markrad 0:cdf462088d13 110 * a negative error code otherwise
markrad 0:cdf462088d13 111 */
markrad 0:cdf462088d13 112 int mbedtls_ecjpake_setup( mbedtls_ecjpake_context *ctx,
markrad 0:cdf462088d13 113 mbedtls_ecjpake_role role,
markrad 0:cdf462088d13 114 mbedtls_md_type_t hash,
markrad 0:cdf462088d13 115 mbedtls_ecp_group_id curve,
markrad 0:cdf462088d13 116 const unsigned char *secret,
markrad 0:cdf462088d13 117 size_t len );
markrad 0:cdf462088d13 118
markrad 0:cdf462088d13 119 /*
markrad 0:cdf462088d13 120 * \brief Check if a context is ready for use
markrad 0:cdf462088d13 121 *
markrad 0:cdf462088d13 122 * \param ctx Context to check
markrad 0:cdf462088d13 123 *
markrad 0:cdf462088d13 124 * \return 0 if the context is ready for use,
markrad 0:cdf462088d13 125 * MBEDTLS_ERR_ECP_BAD_INPUT_DATA otherwise
markrad 0:cdf462088d13 126 */
markrad 0:cdf462088d13 127 int mbedtls_ecjpake_check( const mbedtls_ecjpake_context *ctx );
markrad 0:cdf462088d13 128
markrad 0:cdf462088d13 129 /**
markrad 0:cdf462088d13 130 * \brief Generate and write the first round message
markrad 0:cdf462088d13 131 * (TLS: contents of the Client/ServerHello extension,
markrad 0:cdf462088d13 132 * excluding extension type and length bytes)
markrad 0:cdf462088d13 133 *
markrad 0:cdf462088d13 134 * \param ctx Context to use
markrad 0:cdf462088d13 135 * \param buf Buffer to write the contents to
markrad 0:cdf462088d13 136 * \param len Buffer size
markrad 0:cdf462088d13 137 * \param olen Will be updated with the number of bytes written
markrad 0:cdf462088d13 138 * \param f_rng RNG function
markrad 0:cdf462088d13 139 * \param p_rng RNG parameter
markrad 0:cdf462088d13 140 *
markrad 0:cdf462088d13 141 * \return 0 if successfull,
markrad 0:cdf462088d13 142 * a negative error code otherwise
markrad 0:cdf462088d13 143 */
markrad 0:cdf462088d13 144 int mbedtls_ecjpake_write_round_one( mbedtls_ecjpake_context *ctx,
markrad 0:cdf462088d13 145 unsigned char *buf, size_t len, size_t *olen,
markrad 0:cdf462088d13 146 int (*f_rng)(void *, unsigned char *, size_t),
markrad 0:cdf462088d13 147 void *p_rng );
markrad 0:cdf462088d13 148
markrad 0:cdf462088d13 149 /**
markrad 0:cdf462088d13 150 * \brief Read and process the first round message
markrad 0:cdf462088d13 151 * (TLS: contents of the Client/ServerHello extension,
markrad 0:cdf462088d13 152 * excluding extension type and length bytes)
markrad 0:cdf462088d13 153 *
markrad 0:cdf462088d13 154 * \param ctx Context to use
markrad 0:cdf462088d13 155 * \param buf Pointer to extension contents
markrad 0:cdf462088d13 156 * \param len Extension length
markrad 0:cdf462088d13 157 *
markrad 0:cdf462088d13 158 * \return 0 if successfull,
markrad 0:cdf462088d13 159 * a negative error code otherwise
markrad 0:cdf462088d13 160 */
markrad 0:cdf462088d13 161 int mbedtls_ecjpake_read_round_one( mbedtls_ecjpake_context *ctx,
markrad 0:cdf462088d13 162 const unsigned char *buf,
markrad 0:cdf462088d13 163 size_t len );
markrad 0:cdf462088d13 164
markrad 0:cdf462088d13 165 /**
markrad 0:cdf462088d13 166 * \brief Generate and write the second round message
markrad 0:cdf462088d13 167 * (TLS: contents of the Client/ServerKeyExchange)
markrad 0:cdf462088d13 168 *
markrad 0:cdf462088d13 169 * \param ctx Context to use
markrad 0:cdf462088d13 170 * \param buf Buffer to write the contents to
markrad 0:cdf462088d13 171 * \param len Buffer size
markrad 0:cdf462088d13 172 * \param olen Will be updated with the number of bytes written
markrad 0:cdf462088d13 173 * \param f_rng RNG function
markrad 0:cdf462088d13 174 * \param p_rng RNG parameter
markrad 0:cdf462088d13 175 *
markrad 0:cdf462088d13 176 * \return 0 if successfull,
markrad 0:cdf462088d13 177 * a negative error code otherwise
markrad 0:cdf462088d13 178 */
markrad 0:cdf462088d13 179 int mbedtls_ecjpake_write_round_two( mbedtls_ecjpake_context *ctx,
markrad 0:cdf462088d13 180 unsigned char *buf, size_t len, size_t *olen,
markrad 0:cdf462088d13 181 int (*f_rng)(void *, unsigned char *, size_t),
markrad 0:cdf462088d13 182 void *p_rng );
markrad 0:cdf462088d13 183
markrad 0:cdf462088d13 184 /**
markrad 0:cdf462088d13 185 * \brief Read and process the second round message
markrad 0:cdf462088d13 186 * (TLS: contents of the Client/ServerKeyExchange)
markrad 0:cdf462088d13 187 *
markrad 0:cdf462088d13 188 * \param ctx Context to use
markrad 0:cdf462088d13 189 * \param buf Pointer to the message
markrad 0:cdf462088d13 190 * \param len Message length
markrad 0:cdf462088d13 191 *
markrad 0:cdf462088d13 192 * \return 0 if successfull,
markrad 0:cdf462088d13 193 * a negative error code otherwise
markrad 0:cdf462088d13 194 */
markrad 0:cdf462088d13 195 int mbedtls_ecjpake_read_round_two( mbedtls_ecjpake_context *ctx,
markrad 0:cdf462088d13 196 const unsigned char *buf,
markrad 0:cdf462088d13 197 size_t len );
markrad 0:cdf462088d13 198
markrad 0:cdf462088d13 199 /**
markrad 0:cdf462088d13 200 * \brief Derive the shared secret
markrad 0:cdf462088d13 201 * (TLS: Pre-Master Secret)
markrad 0:cdf462088d13 202 *
markrad 0:cdf462088d13 203 * \param ctx Context to use
markrad 0:cdf462088d13 204 * \param buf Buffer to write the contents to
markrad 0:cdf462088d13 205 * \param len Buffer size
markrad 0:cdf462088d13 206 * \param olen Will be updated with the number of bytes written
markrad 0:cdf462088d13 207 * \param f_rng RNG function
markrad 0:cdf462088d13 208 * \param p_rng RNG parameter
markrad 0:cdf462088d13 209 *
markrad 0:cdf462088d13 210 * \return 0 if successfull,
markrad 0:cdf462088d13 211 * a negative error code otherwise
markrad 0:cdf462088d13 212 */
markrad 0:cdf462088d13 213 int mbedtls_ecjpake_derive_secret( mbedtls_ecjpake_context *ctx,
markrad 0:cdf462088d13 214 unsigned char *buf, size_t len, size_t *olen,
markrad 0:cdf462088d13 215 int (*f_rng)(void *, unsigned char *, size_t),
markrad 0:cdf462088d13 216 void *p_rng );
markrad 0:cdf462088d13 217
markrad 0:cdf462088d13 218 /**
markrad 0:cdf462088d13 219 * \brief Free a context's content
markrad 0:cdf462088d13 220 *
markrad 0:cdf462088d13 221 * \param ctx context to free
markrad 0:cdf462088d13 222 */
markrad 0:cdf462088d13 223 void mbedtls_ecjpake_free( mbedtls_ecjpake_context *ctx );
markrad 0:cdf462088d13 224
markrad 0:cdf462088d13 225 #if defined(MBEDTLS_SELF_TEST)
markrad 0:cdf462088d13 226 /**
markrad 0:cdf462088d13 227 * \brief Checkup routine
markrad 0:cdf462088d13 228 *
markrad 0:cdf462088d13 229 * \return 0 if successful, or 1 if a test failed
markrad 0:cdf462088d13 230 */
markrad 0:cdf462088d13 231 int mbedtls_ecjpake_self_test( int verbose );
markrad 0:cdf462088d13 232 #endif
markrad 0:cdf462088d13 233
markrad 0:cdf462088d13 234 #ifdef __cplusplus
markrad 0:cdf462088d13 235 }
markrad 0:cdf462088d13 236 #endif
markrad 0:cdf462088d13 237
markrad 0:cdf462088d13 238 #endif /* ecjpake.h */