blob: a9b68d00c61b37e04ce9ebe867001a53ecb1d8f9 [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/*
Bence Szépkútia2947ac2020-08-19 16:37:36 +02007 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +02008 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
9 *
10 * This file is provided under the Apache License 2.0, or the
11 * GNU General Public License v2.0 or later.
12 *
13 * **********
14 * Apache License 2.0:
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +020015 *
16 * Licensed under the Apache License, Version 2.0 (the "License"); you may
17 * not use this file except in compliance with the License.
18 * You may obtain a copy of the License at
19 *
20 * http://www.apache.org/licenses/LICENSE-2.0
21 *
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
24 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
27 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020028 * **********
29 *
30 * **********
31 * GNU General Public License v2.0 or later:
32 *
33 * This program is free software; you can redistribute it and/or modify
34 * it under the terms of the GNU General Public License as published by
35 * the Free Software Foundation; either version 2 of the License, or
36 * (at your option) any later version.
37 *
38 * This program is distributed in the hope that it will be useful,
39 * but WITHOUT ANY WARRANTY; without even the implied warranty of
40 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
41 * GNU General Public License for more details.
42 *
43 * You should have received a copy of the GNU General Public License along
44 * with this program; if not, write to the Free Software Foundation, Inc.,
45 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
46 *
47 * **********
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +020048 */
49#ifndef MBEDTLS_ECJPAKE_H
50#define MBEDTLS_ECJPAKE_H
51
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020052/*
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +020053 * J-PAKE is a password-authenticated key exchange that allows deriving a
54 * strong shared secret from a (potentially low entropy) pre-shared
55 * passphrase, with forward secrecy and mutual authentication.
56 * https://en.wikipedia.org/wiki/Password_Authenticated_Key_Exchange_by_Juggling
57 *
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +020058 * This file implements the Elliptic Curve variant of J-PAKE,
59 * as defined in Chapter 7.4 of the Thread v1.0 Specification,
60 * available to members of the Thread Group http://threadgroup.org/
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +020061 *
62 * As the J-PAKE algorithm is inherently symmetric, so is our API.
63 * Each party needs to send its first round message, in any order, to the
64 * other party, then each sends its second round message, in any order.
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +020065 * The payloads are serialized in a way suitable for use in TLS, but could
66 * also be use outside TLS.
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020067 */
Ron Eldor8b0cf2e2018-02-14 16:02:41 +020068#if !defined(MBEDTLS_CONFIG_FILE)
69#include "config.h"
70#else
71#include MBEDTLS_CONFIG_FILE
72#endif
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020073
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +020074#include "ecp.h"
75#include "md.h"
76
77#ifdef __cplusplus
78extern "C" {
79#endif
80
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020081/**
82 * Roles in the EC J-PAKE exchange
83 */
Manuel Pégourié-Gonnard64493912015-08-13 20:19:51 +020084typedef enum {
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020085 MBEDTLS_ECJPAKE_CLIENT = 0, /**< Client */
86 MBEDTLS_ECJPAKE_SERVER, /**< Server */
Manuel Pégourié-Gonnard64493912015-08-13 20:19:51 +020087} mbedtls_ecjpake_role;
88
Ron Eldor4e6d55d2018-02-07 16:36:15 +020089#if !defined(MBEDTLS_ECJPAKE_ALT)
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020090/**
Manuel Pégourié-Gonnardce456762015-08-14 11:54:35 +020091 * EC J-PAKE context structure.
92 *
93 * J-PAKE is a symmetric protocol, except for the identifiers used in
94 * Zero-Knowledge Proofs, and the serialization of the second message
95 * (KeyExchange) as defined by the Thread spec.
96 *
97 * In order to benefit from this symmetry, we choose a different naming
98 * convetion from the Thread v1.0 spec. Correspondance is indicated in the
Simon Butcher5b331b92016-01-03 16:14:14 +000099 * description as a pair C: client name, S: server name
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +0200100 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200101typedef struct mbedtls_ecjpake_context
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200102{
103 const mbedtls_md_info_t *md_info; /**< Hash to use */
104 mbedtls_ecp_group grp; /**< Elliptic curve */
Manuel Pégourié-Gonnard64493912015-08-13 20:19:51 +0200105 mbedtls_ecjpake_role role; /**< Are we client or server? */
Robert Cragie7cdad772015-10-02 13:31:41 +0100106 int point_format; /**< Format for point export */
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200107
Manuel Pégourié-Gonnardce456762015-08-14 11:54:35 +0200108 mbedtls_ecp_point Xm1; /**< My public key 1 C: X1, S: X3 */
109 mbedtls_ecp_point Xm2; /**< My public key 2 C: X2, S: X4 */
110 mbedtls_ecp_point Xp1; /**< Peer public key 1 C: X3, S: X1 */
111 mbedtls_ecp_point Xp2; /**< Peer public key 2 C: X4, S: X2 */
112 mbedtls_ecp_point Xp; /**< Peer public key C: Xs, S: Xc */
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200113
Manuel Pégourié-Gonnardce456762015-08-14 11:54:35 +0200114 mbedtls_mpi xm1; /**< My private key 1 C: x1, S: x3 */
115 mbedtls_mpi xm2; /**< My private key 2 C: x2, S: x4 */
Manuel Pégourié-Gonnard23dcbe32015-08-13 09:37:00 +0200116
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +0200117 mbedtls_mpi s; /**< Pre-shared secret (passphrase) */
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200118} mbedtls_ecjpake_context;
119
Ron Eldor4e6d55d2018-02-07 16:36:15 +0200120#else /* MBEDTLS_ECJPAKE_ALT */
121#include "ecjpake_alt.h"
122#endif /* MBEDTLS_ECJPAKE_ALT */
123
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200124/**
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000125 * \brief Initialize an ECJPAKE context.
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200126 *
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000127 * \param ctx The ECJPAKE context to initialize.
128 * This must not be \c NULL.
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200129 */
130void mbedtls_ecjpake_init( mbedtls_ecjpake_context *ctx );
131
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200132/**
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000133 * \brief Set up an ECJPAKE context for use.
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200134 *
135 * \note Currently the only values for hash/curve allowed by the
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000136 * standard are #MBEDTLS_MD_SHA256/#MBEDTLS_ECP_DP_SECP256R1.
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200137 *
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000138 * \param ctx The ECJPAKE context to set up. This must be initialized.
139 * \param role The role of the caller. This must be either
140 * #MBEDTLS_ECJPAKE_CLIENT or #MBEDTLS_ECJPAKE_SERVER.
141 * \param hash The identifier of the hash function to use,
142 * for example #MBEDTLS_MD_SHA256.
Hanno Becker185e5162018-12-19 09:48:50 +0000143 * \param curve The identifier of the elliptic curve to use,
144 * for example #MBEDTLS_ECP_DP_SECP256R1.
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000145 * \param secret The pre-shared secret (passphrase). This must be
Hanno Beckerfbf67772018-12-19 10:14:43 +0000146 * a readable buffer of length \p len Bytes. It need
147 * only be valid for the duration of this call.
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000148 * \param len The length of the pre-shared secret \p secret.
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200149 *
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000150 * \return \c 0 if successful.
151 * \return A negative error code on failure.
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200152 */
153int mbedtls_ecjpake_setup( mbedtls_ecjpake_context *ctx,
Manuel Pégourié-Gonnard64493912015-08-13 20:19:51 +0200154 mbedtls_ecjpake_role role,
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200155 mbedtls_md_type_t hash,
Manuel Pégourié-Gonnard23dcbe32015-08-13 09:37:00 +0200156 mbedtls_ecp_group_id curve,
157 const unsigned char *secret,
158 size_t len );
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200159
Andres Amaya Garciaaf610a02016-12-14 10:13:43 +0000160/**
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000161 * \brief Check if an ECJPAKE context is ready for use.
Manuel Pégourié-Gonnardb813acc2015-09-15 15:34:09 +0200162 *
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000163 * \param ctx The ECJPAKE context to check. This must be
164 * initialized.
Manuel Pégourié-Gonnardb813acc2015-09-15 15:34:09 +0200165 *
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000166 * \return \c 0 if the context is ready for use.
167 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA otherwise.
Manuel Pégourié-Gonnardb813acc2015-09-15 15:34:09 +0200168 */
169int mbedtls_ecjpake_check( const mbedtls_ecjpake_context *ctx );
170
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200171/**
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +0200172 * \brief Generate and write the first round message
173 * (TLS: contents of the Client/ServerHello extension,
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000174 * excluding extension type and length bytes).
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200175 *
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000176 * \param ctx The ECJPAKE context to use. This must be
177 * initialized and set up.
178 * \param buf The buffer to write the contents to. This must be a
179 * writable buffer of length \p len Bytes.
180 * \param len The length of \p buf in Bytes.
181 * \param olen The address at which to store the total number
182 * of Bytes written to \p buf. This must not be \c NULL.
183 * \param f_rng The RNG function to use. This must not be \c NULL.
184 * \param p_rng The RNG parameter to be passed to \p f_rng. This
185 * may be \c NULL if \p f_rng doesn't use a context.
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200186 *
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000187 * \return \c 0 if successful.
188 * \return A negative error code on failure.
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200189 */
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +0200190int mbedtls_ecjpake_write_round_one( mbedtls_ecjpake_context *ctx,
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200191 unsigned char *buf, size_t len, size_t *olen,
192 int (*f_rng)(void *, unsigned char *, size_t),
193 void *p_rng );
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200194
195/**
196 * \brief Read and process the first round message
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +0200197 * (TLS: contents of the Client/ServerHello extension,
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000198 * excluding extension type and length bytes).
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200199 *
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000200 * \param ctx The ECJPAKE context to use. This must be initialized
201 * and set up.
202 * \param buf The buffer holding the first round message. This must
203 * be a readable buffer of length \p len Bytes.
204 * \param len The length in Bytes of \p buf.
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200205 *
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000206 * \return \c 0 if successful.
207 * \return A negative error code on failure.
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200208 */
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +0200209int mbedtls_ecjpake_read_round_one( mbedtls_ecjpake_context *ctx,
210 const unsigned char *buf,
211 size_t len );
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200212
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200213/**
214 * \brief Generate and write the second round message
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000215 * (TLS: contents of the Client/ServerKeyExchange).
Manuel Pégourié-Gonnard614bd5e2015-08-13 20:19:16 +0200216 *
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000217 * \param ctx The ECJPAKE context to use. This must be initialized,
218 * set up, and already have performed round one.
219 * \param buf The buffer to write the round two contents to.
220 * This must be a writable buffer of length \p len Bytes.
221 * \param len The size of \p buf in Bytes.
222 * \param olen The address at which to store the total number of Bytes
223 * written to \p buf. This must not be \c NULL.
224 * \param f_rng The RNG function to use. This must not be \c NULL.
225 * \param p_rng The RNG parameter to be passed to \p f_rng. This
226 * may be \c NULL if \p f_rng doesn't use a context.
Manuel Pégourié-Gonnard614bd5e2015-08-13 20:19:16 +0200227 *
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000228 * \return \c 0 if successful.
229 * \return A negative error code on failure.
Manuel Pégourié-Gonnard614bd5e2015-08-13 20:19:16 +0200230 */
Manuel Pégourié-Gonnarde1927102015-08-14 14:20:48 +0200231int mbedtls_ecjpake_write_round_two( mbedtls_ecjpake_context *ctx,
Manuel Pégourié-Gonnard614bd5e2015-08-13 20:19:16 +0200232 unsigned char *buf, size_t len, size_t *olen,
233 int (*f_rng)(void *, unsigned char *, size_t),
234 void *p_rng );
235
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200236/**
237 * \brief Read and process the second round message
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000238 * (TLS: contents of the Client/ServerKeyExchange).
Manuel Pégourié-Gonnardec0eece2015-08-13 19:13:20 +0200239 *
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000240 * \param ctx The ECJPAKE context to use. This must be initialized
Hanno Beckerfbf67772018-12-19 10:14:43 +0000241 * and set up and already have performed round one.
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000242 * \param buf The buffer holding the second round message. This must
243 * be a readable buffer of length \p len Bytes.
244 * \param len The length in Bytes of \p buf.
Manuel Pégourié-Gonnardec0eece2015-08-13 19:13:20 +0200245 *
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000246 * \return \c 0 if successful.
247 * \return A negative error code on failure.
Manuel Pégourié-Gonnardec0eece2015-08-13 19:13:20 +0200248 */
Manuel Pégourié-Gonnarde1927102015-08-14 14:20:48 +0200249int mbedtls_ecjpake_read_round_two( mbedtls_ecjpake_context *ctx,
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200250 const unsigned char *buf,
251 size_t len );
Manuel Pégourié-Gonnardec0eece2015-08-13 19:13:20 +0200252
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200253/**
254 * \brief Derive the shared secret
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000255 * (TLS: Pre-Master Secret).
Manuel Pégourié-Gonnard5f188292015-08-14 10:52:39 +0200256 *
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000257 * \param ctx The ECJPAKE context to use. This must be initialized,
258 * set up and have performed both round one and two.
259 * \param buf The buffer to write the derived secret to. This must
260 * be a writable buffer of length \p len Bytes.
261 * \param len The length of \p buf in Bytes.
262 * \param olen The address at which to store the total number of Bytes
263 * written to \p buf. This must not be \c NULL.
264 * \param f_rng The RNG function to use. This must not be \c NULL.
265 * \param p_rng The RNG parameter to be passed to \p f_rng. This
266 * may be \c NULL if \p f_rng doesn't use a context.
Manuel Pégourié-Gonnard5f188292015-08-14 10:52:39 +0200267 *
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000268 * \return \c 0 if successful.
269 * \return A negative error code on failure.
Manuel Pégourié-Gonnard5f188292015-08-14 10:52:39 +0200270 */
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200271int mbedtls_ecjpake_derive_secret( mbedtls_ecjpake_context *ctx,
Manuel Pégourié-Gonnard5f188292015-08-14 10:52:39 +0200272 unsigned char *buf, size_t len, size_t *olen,
273 int (*f_rng)(void *, unsigned char *, size_t),
274 void *p_rng );
275
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200276/**
Hanno Beckerfbf67772018-12-19 10:14:43 +0000277 * \brief This clears an ECJPAKE context and frees any
278 * embedded data structure.
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200279 *
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000280 * \param ctx The ECJPAKE context to free. This may be \c NULL,
281 * in which case this function does nothing. If it is not
282 * \c NULL, it must point to an initialized ECJPAKE context.
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200283 */
284void mbedtls_ecjpake_free( mbedtls_ecjpake_context *ctx );
285
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +0200286#if defined(MBEDTLS_SELF_TEST)
Hanno Becker616d1ca2018-01-24 10:25:05 +0000287
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +0200288/**
289 * \brief Checkup routine
290 *
291 * \return 0 if successful, or 1 if a test failed
292 */
293int mbedtls_ecjpake_self_test( int verbose );
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +0200294
Ron Eldor4e6d55d2018-02-07 16:36:15 +0200295#endif /* MBEDTLS_SELF_TEST */
296
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +0200297#ifdef __cplusplus
298}
299#endif
300
Hanno Becker616d1ca2018-01-24 10:25:05 +0000301
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +0200302#endif /* ecjpake.h */