blob: 1b6c6ac244e73fa87b990dffa6b717ef22ef15f9 [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
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 * **********
48 *
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +020049 * This file is part of mbed TLS (https://tls.mbed.org)
50 */
51#ifndef MBEDTLS_ECJPAKE_H
52#define MBEDTLS_ECJPAKE_H
53
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020054/*
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +020055 * J-PAKE is a password-authenticated key exchange that allows deriving a
56 * strong shared secret from a (potentially low entropy) pre-shared
57 * passphrase, with forward secrecy and mutual authentication.
58 * https://en.wikipedia.org/wiki/Password_Authenticated_Key_Exchange_by_Juggling
59 *
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +020060 * This file implements the Elliptic Curve variant of J-PAKE,
61 * as defined in Chapter 7.4 of the Thread v1.0 Specification,
62 * available to members of the Thread Group http://threadgroup.org/
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +020063 *
64 * As the J-PAKE algorithm is inherently symmetric, so is our API.
65 * Each party needs to send its first round message, in any order, to the
66 * other party, then each sends its second round message, in any order.
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +020067 * The payloads are serialized in a way suitable for use in TLS, but could
68 * also be use outside TLS.
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020069 */
Ron Eldor8b0cf2e2018-02-14 16:02:41 +020070#if !defined(MBEDTLS_CONFIG_FILE)
71#include "config.h"
72#else
73#include MBEDTLS_CONFIG_FILE
74#endif
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020075
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +020076#include "ecp.h"
77#include "md.h"
78
79#ifdef __cplusplus
80extern "C" {
81#endif
82
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020083/**
84 * Roles in the EC J-PAKE exchange
85 */
Manuel Pégourié-Gonnard64493912015-08-13 20:19:51 +020086typedef enum {
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020087 MBEDTLS_ECJPAKE_CLIENT = 0, /**< Client */
88 MBEDTLS_ECJPAKE_SERVER, /**< Server */
Manuel Pégourié-Gonnard64493912015-08-13 20:19:51 +020089} mbedtls_ecjpake_role;
90
Ron Eldor4e6d55d2018-02-07 16:36:15 +020091#if !defined(MBEDTLS_ECJPAKE_ALT)
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020092/**
Manuel Pégourié-Gonnardce456762015-08-14 11:54:35 +020093 * EC J-PAKE context structure.
94 *
95 * J-PAKE is a symmetric protocol, except for the identifiers used in
96 * Zero-Knowledge Proofs, and the serialization of the second message
97 * (KeyExchange) as defined by the Thread spec.
98 *
99 * In order to benefit from this symmetry, we choose a different naming
100 * convetion from the Thread v1.0 spec. Correspondance is indicated in the
Simon Butcher5b331b92016-01-03 16:14:14 +0000101 * description as a pair C: client name, S: server name
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +0200102 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200103typedef struct mbedtls_ecjpake_context
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200104{
105 const mbedtls_md_info_t *md_info; /**< Hash to use */
106 mbedtls_ecp_group grp; /**< Elliptic curve */
Manuel Pégourié-Gonnard64493912015-08-13 20:19:51 +0200107 mbedtls_ecjpake_role role; /**< Are we client or server? */
Robert Cragie7cdad772015-10-02 13:31:41 +0100108 int point_format; /**< Format for point export */
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200109
Manuel Pégourié-Gonnardce456762015-08-14 11:54:35 +0200110 mbedtls_ecp_point Xm1; /**< My public key 1 C: X1, S: X3 */
111 mbedtls_ecp_point Xm2; /**< My public key 2 C: X2, S: X4 */
112 mbedtls_ecp_point Xp1; /**< Peer public key 1 C: X3, S: X1 */
113 mbedtls_ecp_point Xp2; /**< Peer public key 2 C: X4, S: X2 */
114 mbedtls_ecp_point Xp; /**< Peer public key C: Xs, S: Xc */
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200115
Manuel Pégourié-Gonnardce456762015-08-14 11:54:35 +0200116 mbedtls_mpi xm1; /**< My private key 1 C: x1, S: x3 */
117 mbedtls_mpi xm2; /**< My private key 2 C: x2, S: x4 */
Manuel Pégourié-Gonnard23dcbe32015-08-13 09:37:00 +0200118
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +0200119 mbedtls_mpi s; /**< Pre-shared secret (passphrase) */
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200120} mbedtls_ecjpake_context;
121
Ron Eldor4e6d55d2018-02-07 16:36:15 +0200122#else /* MBEDTLS_ECJPAKE_ALT */
123#include "ecjpake_alt.h"
124#endif /* MBEDTLS_ECJPAKE_ALT */
125
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200126/**
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000127 * \brief Initialize an ECJPAKE context.
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200128 *
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000129 * \param ctx The ECJPAKE context to initialize.
130 * This must not be \c NULL.
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200131 */
132void mbedtls_ecjpake_init( mbedtls_ecjpake_context *ctx );
133
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200134/**
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000135 * \brief Set up an ECJPAKE context for use.
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200136 *
137 * \note Currently the only values for hash/curve allowed by the
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000138 * standard are #MBEDTLS_MD_SHA256/#MBEDTLS_ECP_DP_SECP256R1.
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200139 *
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000140 * \param ctx The ECJPAKE context to set up. This must be initialized.
141 * \param role The role of the caller. This must be either
142 * #MBEDTLS_ECJPAKE_CLIENT or #MBEDTLS_ECJPAKE_SERVER.
143 * \param hash The identifier of the hash function to use,
144 * for example #MBEDTLS_MD_SHA256.
Hanno Becker185e5162018-12-19 09:48:50 +0000145 * \param curve The identifier of the elliptic curve to use,
146 * for example #MBEDTLS_ECP_DP_SECP256R1.
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000147 * \param secret The pre-shared secret (passphrase). This must be
Hanno Beckerfbf67772018-12-19 10:14:43 +0000148 * a readable buffer of length \p len Bytes. It need
149 * only be valid for the duration of this call.
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000150 * \param len The length of the pre-shared secret \p secret.
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200151 *
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000152 * \return \c 0 if successful.
153 * \return A negative error code on failure.
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200154 */
155int mbedtls_ecjpake_setup( mbedtls_ecjpake_context *ctx,
Manuel Pégourié-Gonnard64493912015-08-13 20:19:51 +0200156 mbedtls_ecjpake_role role,
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200157 mbedtls_md_type_t hash,
Manuel Pégourié-Gonnard23dcbe32015-08-13 09:37:00 +0200158 mbedtls_ecp_group_id curve,
159 const unsigned char *secret,
160 size_t len );
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200161
Andres Amaya Garciaaf610a02016-12-14 10:13:43 +0000162/**
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000163 * \brief Check if an ECJPAKE context is ready for use.
Manuel Pégourié-Gonnardb813acc2015-09-15 15:34:09 +0200164 *
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000165 * \param ctx The ECJPAKE context to check. This must be
166 * initialized.
Manuel Pégourié-Gonnardb813acc2015-09-15 15:34:09 +0200167 *
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000168 * \return \c 0 if the context is ready for use.
169 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA otherwise.
Manuel Pégourié-Gonnardb813acc2015-09-15 15:34:09 +0200170 */
171int mbedtls_ecjpake_check( const mbedtls_ecjpake_context *ctx );
172
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200173/**
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +0200174 * \brief Generate and write the first round message
175 * (TLS: contents of the Client/ServerHello extension,
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000176 * excluding extension type and length bytes).
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200177 *
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000178 * \param ctx The ECJPAKE context to use. This must be
179 * initialized and set up.
180 * \param buf The buffer to write the contents to. This must be a
181 * writable buffer of length \p len Bytes.
182 * \param len The length of \p buf in Bytes.
183 * \param olen The address at which to store the total number
184 * of Bytes written to \p buf. This must not be \c NULL.
185 * \param f_rng The RNG function to use. This must not be \c NULL.
186 * \param p_rng The RNG parameter to be passed to \p f_rng. This
187 * may be \c NULL if \p f_rng doesn't use a context.
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200188 *
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000189 * \return \c 0 if successful.
190 * \return A negative error code on failure.
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200191 */
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +0200192int mbedtls_ecjpake_write_round_one( mbedtls_ecjpake_context *ctx,
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200193 unsigned char *buf, size_t len, size_t *olen,
194 int (*f_rng)(void *, unsigned char *, size_t),
195 void *p_rng );
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200196
197/**
198 * \brief Read and process the first round message
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +0200199 * (TLS: contents of the Client/ServerHello extension,
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000200 * excluding extension type and length bytes).
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200201 *
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000202 * \param ctx The ECJPAKE context to use. This must be initialized
203 * and set up.
204 * \param buf The buffer holding the first round message. This must
205 * be a readable buffer of length \p len Bytes.
206 * \param len The length in Bytes of \p buf.
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200207 *
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000208 * \return \c 0 if successful.
209 * \return A negative error code on failure.
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200210 */
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +0200211int mbedtls_ecjpake_read_round_one( mbedtls_ecjpake_context *ctx,
212 const unsigned char *buf,
213 size_t len );
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200214
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200215/**
216 * \brief Generate and write the second round message
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000217 * (TLS: contents of the Client/ServerKeyExchange).
Manuel Pégourié-Gonnard614bd5e2015-08-13 20:19:16 +0200218 *
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000219 * \param ctx The ECJPAKE context to use. This must be initialized,
220 * set up, and already have performed round one.
221 * \param buf The buffer to write the round two contents to.
222 * This must be a writable buffer of length \p len Bytes.
223 * \param len The size of \p buf in Bytes.
224 * \param olen The address at which to store the total number of Bytes
225 * written to \p buf. This must not be \c NULL.
226 * \param f_rng The RNG function to use. This must not be \c NULL.
227 * \param p_rng The RNG parameter to be passed to \p f_rng. This
228 * may be \c NULL if \p f_rng doesn't use a context.
Manuel Pégourié-Gonnard614bd5e2015-08-13 20:19:16 +0200229 *
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000230 * \return \c 0 if successful.
231 * \return A negative error code on failure.
Manuel Pégourié-Gonnard614bd5e2015-08-13 20:19:16 +0200232 */
Manuel Pégourié-Gonnarde1927102015-08-14 14:20:48 +0200233int mbedtls_ecjpake_write_round_two( mbedtls_ecjpake_context *ctx,
Manuel Pégourié-Gonnard614bd5e2015-08-13 20:19:16 +0200234 unsigned char *buf, size_t len, size_t *olen,
235 int (*f_rng)(void *, unsigned char *, size_t),
236 void *p_rng );
237
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200238/**
239 * \brief Read and process the second round message
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000240 * (TLS: contents of the Client/ServerKeyExchange).
Manuel Pégourié-Gonnardec0eece2015-08-13 19:13:20 +0200241 *
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000242 * \param ctx The ECJPAKE context to use. This must be initialized
Hanno Beckerfbf67772018-12-19 10:14:43 +0000243 * and set up and already have performed round one.
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000244 * \param buf The buffer holding the second round message. This must
245 * be a readable buffer of length \p len Bytes.
246 * \param len The length in Bytes of \p buf.
Manuel Pégourié-Gonnardec0eece2015-08-13 19:13:20 +0200247 *
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000248 * \return \c 0 if successful.
249 * \return A negative error code on failure.
Manuel Pégourié-Gonnardec0eece2015-08-13 19:13:20 +0200250 */
Manuel Pégourié-Gonnarde1927102015-08-14 14:20:48 +0200251int mbedtls_ecjpake_read_round_two( mbedtls_ecjpake_context *ctx,
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200252 const unsigned char *buf,
253 size_t len );
Manuel Pégourié-Gonnardec0eece2015-08-13 19:13:20 +0200254
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200255/**
256 * \brief Derive the shared secret
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000257 * (TLS: Pre-Master Secret).
Manuel Pégourié-Gonnard5f188292015-08-14 10:52:39 +0200258 *
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000259 * \param ctx The ECJPAKE context to use. This must be initialized,
260 * set up and have performed both round one and two.
261 * \param buf The buffer to write the derived secret to. This must
262 * be a writable buffer of length \p len Bytes.
263 * \param len The length of \p buf in Bytes.
264 * \param olen The address at which to store the total number of Bytes
265 * written to \p buf. This must not be \c NULL.
266 * \param f_rng The RNG function to use. This must not be \c NULL.
267 * \param p_rng The RNG parameter to be passed to \p f_rng. This
268 * may be \c NULL if \p f_rng doesn't use a context.
Manuel Pégourié-Gonnard5f188292015-08-14 10:52:39 +0200269 *
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000270 * \return \c 0 if successful.
271 * \return A negative error code on failure.
Manuel Pégourié-Gonnard5f188292015-08-14 10:52:39 +0200272 */
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200273int mbedtls_ecjpake_derive_secret( mbedtls_ecjpake_context *ctx,
Manuel Pégourié-Gonnard5f188292015-08-14 10:52:39 +0200274 unsigned char *buf, size_t len, size_t *olen,
275 int (*f_rng)(void *, unsigned char *, size_t),
276 void *p_rng );
277
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200278/**
Hanno Beckerfbf67772018-12-19 10:14:43 +0000279 * \brief This clears an ECJPAKE context and frees any
280 * embedded data structure.
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200281 *
Hanno Beckerc4e5aa52018-12-14 17:09:27 +0000282 * \param ctx The ECJPAKE context to free. This may be \c NULL,
283 * in which case this function does nothing. If it is not
284 * \c NULL, it must point to an initialized ECJPAKE context.
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200285 */
286void mbedtls_ecjpake_free( mbedtls_ecjpake_context *ctx );
287
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +0200288#if defined(MBEDTLS_SELF_TEST)
Hanno Becker616d1ca2018-01-24 10:25:05 +0000289
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +0200290/**
291 * \brief Checkup routine
292 *
293 * \return 0 if successful, or 1 if a test failed
294 */
295int mbedtls_ecjpake_self_test( int verbose );
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +0200296
Ron Eldor4e6d55d2018-02-07 16:36:15 +0200297#endif /* MBEDTLS_SELF_TEST */
298
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +0200299#ifdef __cplusplus
300}
301#endif
302
Hanno Becker616d1ca2018-01-24 10:25:05 +0000303
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +0200304#endif /* ecjpake.h */