blob: f9291644a1cc9facfeb92a5a7feaf584d41131fa [file] [log] [blame]
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +02001/**
2 * \file ecjpake.h
3 *
4 * \brief Elliptic curve J-PAKE
Darryl Greena40a1012018-01-05 15:33:17 +00005 */
6/*
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +02007 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
8 * SPDX-License-Identifier: Apache-2.0
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 *
22 * This file is part of mbed TLS (https://tls.mbed.org)
23 */
24#ifndef MBEDTLS_ECJPAKE_H
25#define MBEDTLS_ECJPAKE_H
26
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020027/*
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +020028 * J-PAKE is a password-authenticated key exchange that allows deriving a
29 * strong shared secret from a (potentially low entropy) pre-shared
30 * passphrase, with forward secrecy and mutual authentication.
31 * https://en.wikipedia.org/wiki/Password_Authenticated_Key_Exchange_by_Juggling
32 *
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +020033 * This file implements the Elliptic Curve variant of J-PAKE,
34 * as defined in Chapter 7.4 of the Thread v1.0 Specification,
35 * available to members of the Thread Group http://threadgroup.org/
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +020036 *
37 * As the J-PAKE algorithm is inherently symmetric, so is our API.
38 * Each party needs to send its first round message, in any order, to the
39 * other party, then each sends its second round message, in any order.
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +020040 * The payloads are serialized in a way suitable for use in TLS, but could
41 * also be use outside TLS.
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020042 */
43
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +020044#include "ecp.h"
45#include "md.h"
46
47#ifdef __cplusplus
48extern "C" {
49#endif
50
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020051/**
52 * Roles in the EC J-PAKE exchange
53 */
Manuel Pégourié-Gonnard64493912015-08-13 20:19:51 +020054typedef enum {
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020055 MBEDTLS_ECJPAKE_CLIENT = 0, /**< Client */
56 MBEDTLS_ECJPAKE_SERVER, /**< Server */
Manuel Pégourié-Gonnard64493912015-08-13 20:19:51 +020057} mbedtls_ecjpake_role;
58
Ron Eldor4e6d55d2018-02-07 16:36:15 +020059#if !defined(MBEDTLS_ECJPAKE_ALT)
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020060/**
Manuel Pégourié-Gonnardce456762015-08-14 11:54:35 +020061 * EC J-PAKE context structure.
62 *
63 * J-PAKE is a symmetric protocol, except for the identifiers used in
64 * Zero-Knowledge Proofs, and the serialization of the second message
65 * (KeyExchange) as defined by the Thread spec.
66 *
67 * In order to benefit from this symmetry, we choose a different naming
68 * convetion from the Thread v1.0 spec. Correspondance is indicated in the
Simon Butcher5b331b92016-01-03 16:14:14 +000069 * description as a pair C: client name, S: server name
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020070 */
Dawid Drozd428cc522018-07-24 10:02:47 +020071typedef struct mbedtls_ecjpake_context
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020072{
73 const mbedtls_md_info_t *md_info; /**< Hash to use */
74 mbedtls_ecp_group grp; /**< Elliptic curve */
Manuel Pégourié-Gonnard64493912015-08-13 20:19:51 +020075 mbedtls_ecjpake_role role; /**< Are we client or server? */
Robert Cragie7cdad772015-10-02 13:31:41 +010076 int point_format; /**< Format for point export */
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020077
Manuel Pégourié-Gonnardce456762015-08-14 11:54:35 +020078 mbedtls_ecp_point Xm1; /**< My public key 1 C: X1, S: X3 */
79 mbedtls_ecp_point Xm2; /**< My public key 2 C: X2, S: X4 */
80 mbedtls_ecp_point Xp1; /**< Peer public key 1 C: X3, S: X1 */
81 mbedtls_ecp_point Xp2; /**< Peer public key 2 C: X4, S: X2 */
82 mbedtls_ecp_point Xp; /**< Peer public key C: Xs, S: Xc */
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020083
Manuel Pégourié-Gonnardce456762015-08-14 11:54:35 +020084 mbedtls_mpi xm1; /**< My private key 1 C: x1, S: x3 */
85 mbedtls_mpi xm2; /**< My private key 2 C: x2, S: x4 */
Manuel Pégourié-Gonnard23dcbe32015-08-13 09:37:00 +020086
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020087 mbedtls_mpi s; /**< Pre-shared secret (passphrase) */
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020088} mbedtls_ecjpake_context;
89
Ron Eldor4e6d55d2018-02-07 16:36:15 +020090#else /* MBEDTLS_ECJPAKE_ALT */
91#include "ecjpake_alt.h"
92#endif /* MBEDTLS_ECJPAKE_ALT */
93
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +020094/**
Hanno Beckerc4e5aa52018-12-14 17:09:27 +000095 * \brief Initialize an ECJPAKE context.
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020096 *
Hanno Beckerc4e5aa52018-12-14 17:09:27 +000097 * \param ctx The ECJPAKE context to initialize.
98 * This must not be \c NULL.
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020099 */
100void mbedtls_ecjpake_init( mbedtls_ecjpake_context *ctx );
101
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200102/**
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000103 * \brief Set up an ECJPAKE context for use.
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200104 *
105 * \note Currently the only values for hash/curve allowed by the
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000106 * standard are #MBEDTLS_MD_SHA256/#MBEDTLS_ECP_DP_SECP256R1.
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200107 *
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000108 * \param ctx The ECJPAKE context to set up. This must be initialized.
109 * \param role The role of the caller. This must be either
110 * #MBEDTLS_ECJPAKE_CLIENT or #MBEDTLS_ECJPAKE_SERVER.
111 * \param hash The identifier of the hash function to use,
112 * for example #MBEDTLS_MD_SHA256.
Hanno Becker185e5162018-12-19 09:48:50 +0000113 * \param curve The identifier of the elliptic curve to use,
114 * for example #MBEDTLS_ECP_DP_SECP256R1.
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000115 * \param secret The pre-shared secret (passphrase). This must be
116 * a readable buffer of length \p len Bytes, but need
117 * only be valid for the duration of this call. It may
118 * be \c NULL if \p len is zero.
119 * \param len The length of the pre-shared secret \p secret.
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200120 *
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000121 * \return \c 0 if successful.
122 * \return A negative error code on failure.
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200123 */
124int mbedtls_ecjpake_setup( mbedtls_ecjpake_context *ctx,
Manuel Pégourié-Gonnard64493912015-08-13 20:19:51 +0200125 mbedtls_ecjpake_role role,
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200126 mbedtls_md_type_t hash,
Manuel Pégourié-Gonnard23dcbe32015-08-13 09:37:00 +0200127 mbedtls_ecp_group_id curve,
128 const unsigned char *secret,
129 size_t len );
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200130
Andres Amaya Garciaaf610a02016-12-14 10:13:43 +0000131/**
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000132 * \brief Check if an ECJPAKE context is ready for use.
Manuel Pégourié-Gonnardb813acc2015-09-15 15:34:09 +0200133 *
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000134 * \param ctx The ECJPAKE context to check. This must be
135 * initialized.
Manuel Pégourié-Gonnardb813acc2015-09-15 15:34:09 +0200136 *
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000137 * \return \c 0 if the context is ready for use.
138 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA otherwise.
Manuel Pégourié-Gonnardb813acc2015-09-15 15:34:09 +0200139 */
140int mbedtls_ecjpake_check( const mbedtls_ecjpake_context *ctx );
141
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200142/**
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +0200143 * \brief Generate and write the first round message
144 * (TLS: contents of the Client/ServerHello extension,
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000145 * excluding extension type and length bytes).
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200146 *
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000147 * \param ctx The ECJPAKE context to use. This must be
148 * initialized and set up.
149 * \param buf The buffer to write the contents to. This must be a
150 * writable buffer of length \p len Bytes.
151 * \param len The length of \p buf in Bytes.
152 * \param olen The address at which to store the total number
153 * of Bytes written to \p buf. This must not be \c NULL.
154 * \param f_rng The RNG function to use. This must not be \c NULL.
155 * \param p_rng The RNG parameter to be passed to \p f_rng. This
156 * may be \c NULL if \p f_rng doesn't use a context.
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200157 *
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000158 * \return \c 0 if successful.
159 * \return A negative error code on failure.
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200160 */
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +0200161int mbedtls_ecjpake_write_round_one( mbedtls_ecjpake_context *ctx,
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200162 unsigned char *buf, size_t len, size_t *olen,
163 int (*f_rng)(void *, unsigned char *, size_t),
164 void *p_rng );
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200165
166/**
167 * \brief Read and process the first round message
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +0200168 * (TLS: contents of the Client/ServerHello extension,
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000169 * excluding extension type and length bytes).
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200170 *
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000171 * \param ctx The ECJPAKE context to use. This must be initialized
172 * and set up.
173 * \param buf The buffer holding the first round message. This must
174 * be a readable buffer of length \p len Bytes.
175 * \param len The length in Bytes of \p buf.
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200176 *
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000177 * \return \c 0 if successful.
178 * \return A negative error code on failure.
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200179 */
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +0200180int mbedtls_ecjpake_read_round_one( mbedtls_ecjpake_context *ctx,
181 const unsigned char *buf,
182 size_t len );
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200183
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200184/**
185 * \brief Generate and write the second round message
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000186 * (TLS: contents of the Client/ServerKeyExchange).
Manuel Pégourié-Gonnard614bd5e2015-08-13 20:19:16 +0200187 *
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000188 * \param ctx The ECJPAKE context to use. This must be initialized,
189 * set up, and already have performed round one.
190 * \param buf The buffer to write the round two contents to.
191 * This must be a writable buffer of length \p len Bytes.
192 * \param len The size of \p buf in Bytes.
193 * \param olen The address at which to store the total number of Bytes
194 * written to \p buf. This must not be \c NULL.
195 * \param f_rng The RNG function to use. This must not be \c NULL.
196 * \param p_rng The RNG parameter to be passed to \p f_rng. This
197 * may be \c NULL if \p f_rng doesn't use a context.
Manuel Pégourié-Gonnard614bd5e2015-08-13 20:19:16 +0200198 *
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000199 * \return \c 0 if successful.
200 * \return A negative error code on failure.
Manuel Pégourié-Gonnard614bd5e2015-08-13 20:19:16 +0200201 */
Manuel Pégourié-Gonnarde1927102015-08-14 14:20:48 +0200202int mbedtls_ecjpake_write_round_two( mbedtls_ecjpake_context *ctx,
Manuel Pégourié-Gonnard614bd5e2015-08-13 20:19:16 +0200203 unsigned char *buf, size_t len, size_t *olen,
204 int (*f_rng)(void *, unsigned char *, size_t),
205 void *p_rng );
206
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200207/**
208 * \brief Read and process the second round message
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000209 * (TLS: contents of the Client/ServerKeyExchange).
Manuel Pégourié-Gonnardec0eece2015-08-13 19:13:20 +0200210 *
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000211 * \param ctx The ECJPAKE context to use. This must be initialized
Hanno Becker185e5162018-12-19 09:48:50 +0000212 * and set up and have performed round one.
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000213 * \param buf The buffer holding the second round message. This must
214 * be a readable buffer of length \p len Bytes.
215 * \param len The length in Bytes of \p buf.
Manuel Pégourié-Gonnardec0eece2015-08-13 19:13:20 +0200216 *
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000217 * \return \c 0 if successful.
218 * \return A negative error code on failure.
Manuel Pégourié-Gonnardec0eece2015-08-13 19:13:20 +0200219 */
Manuel Pégourié-Gonnarde1927102015-08-14 14:20:48 +0200220int mbedtls_ecjpake_read_round_two( mbedtls_ecjpake_context *ctx,
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200221 const unsigned char *buf,
222 size_t len );
Manuel Pégourié-Gonnardec0eece2015-08-13 19:13:20 +0200223
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200224/**
225 * \brief Derive the shared secret
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000226 * (TLS: Pre-Master Secret).
Manuel Pégourié-Gonnard5f188292015-08-14 10:52:39 +0200227 *
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000228 * \param ctx The ECJPAKE context to use. This must be initialized,
229 * set up and have performed both round one and two.
230 * \param buf The buffer to write the derived secret to. This must
231 * be a writable buffer of length \p len Bytes.
232 * \param len The length of \p buf in Bytes.
233 * \param olen The address at which to store the total number of Bytes
234 * written to \p buf. This must not be \c NULL.
235 * \param f_rng The RNG function to use. This must not be \c NULL.
236 * \param p_rng The RNG parameter to be passed to \p f_rng. This
237 * may be \c NULL if \p f_rng doesn't use a context.
Manuel Pégourié-Gonnard5f188292015-08-14 10:52:39 +0200238 *
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000239 * \return \c 0 if successful.
240 * \return A negative error code on failure.
Manuel Pégourié-Gonnard5f188292015-08-14 10:52:39 +0200241 */
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200242int mbedtls_ecjpake_derive_secret( mbedtls_ecjpake_context *ctx,
Manuel Pégourié-Gonnard5f188292015-08-14 10:52:39 +0200243 unsigned char *buf, size_t len, size_t *olen,
244 int (*f_rng)(void *, unsigned char *, size_t),
245 void *p_rng );
246
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200247/**
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000248 * \brief Free an ECJPAKE context.
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200249 *
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000250 * \param ctx The ECJPAKE context to free. This may be \c NULL,
251 * in which case this function does nothing. If it is not
252 * \c NULL, it must point to an initialized ECJPAKE context.
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200253 */
254void mbedtls_ecjpake_free( mbedtls_ecjpake_context *ctx );
255
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +0200256#if defined(MBEDTLS_SELF_TEST)
Hanno Becker616d1ca2018-01-24 10:25:05 +0000257
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +0200258/**
259 * \brief Checkup routine
260 *
261 * \return 0 if successful, or 1 if a test failed
262 */
263int mbedtls_ecjpake_self_test( int verbose );
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +0200264
Ron Eldor4e6d55d2018-02-07 16:36:15 +0200265#endif /* MBEDTLS_SELF_TEST */
266
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +0200267#ifdef __cplusplus
268}
269#endif
270
Hanno Becker616d1ca2018-01-24 10:25:05 +0000271
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +0200272#endif /* ecjpake.h */