blob: a75a8ec8f87f4f988344de3e1ada193fe70c089a [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úti1e148272020-08-07 13:07:28 +02007 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00008 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +02009 */
10#ifndef MBEDTLS_ECJPAKE_H
11#define MBEDTLS_ECJPAKE_H
Mateusz Starzyk846f0212021-05-19 19:44:07 +020012#include "mbedtls/private_access.h"
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +020013
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020014/*
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +020015 * J-PAKE is a password-authenticated key exchange that allows deriving a
16 * strong shared secret from a (potentially low entropy) pre-shared
17 * passphrase, with forward secrecy and mutual authentication.
18 * https://en.wikipedia.org/wiki/Password_Authenticated_Key_Exchange_by_Juggling
19 *
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +020020 * This file implements the Elliptic Curve variant of J-PAKE,
21 * as defined in Chapter 7.4 of the Thread v1.0 Specification,
22 * available to members of the Thread Group http://threadgroup.org/
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +020023 *
24 * As the J-PAKE algorithm is inherently symmetric, so is our API.
25 * Each party needs to send its first round message, in any order, to the
26 * other party, then each sends its second round message, in any order.
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +020027 * The payloads are serialized in a way suitable for use in TLS, but could
28 * also be use outside TLS.
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020029 */
Bence Szépkútic662b362021-05-27 11:25:03 +020030#include "mbedtls/build_info.h"
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020031
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010032#include "mbedtls/ecp.h"
33#include "mbedtls/md.h"
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +020034
35#ifdef __cplusplus
36extern "C" {
37#endif
38
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020039/**
40 * Roles in the EC J-PAKE exchange
41 */
Manuel Pégourié-Gonnard64493912015-08-13 20:19:51 +020042typedef enum {
Przemek Stekielaede2ad2023-04-25 14:30:34 +020043 MBEDTLS_ECJPAKE_CLIENT = 0, /**< Client */
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020044 MBEDTLS_ECJPAKE_SERVER, /**< Server */
Przemek Stekielaede2ad2023-04-25 14:30:34 +020045 MBEDTLS_ECJPAKE_NONE, /**< Undefined */
Manuel Pégourié-Gonnard64493912015-08-13 20:19:51 +020046} mbedtls_ecjpake_role;
47
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020048/**
Manuel Pégourié-Gonnardce456762015-08-14 11:54:35 +020049 * EC J-PAKE context structure.
50 *
51 * J-PAKE is a symmetric protocol, except for the identifiers used in
52 * Zero-Knowledge Proofs, and the serialization of the second message
53 * (KeyExchange) as defined by the Thread spec.
54 *
55 * In order to benefit from this symmetry, we choose a different naming
Shaun Case8b0ecbc2021-12-20 21:14:10 -080056 * convention from the Thread v1.0 spec. Correspondence is indicated in the
Simon Butcher5b331b92016-01-03 16:14:14 +000057 * description as a pair C: client name, S: server name
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020058 */
Gilles Peskine449bd832023-01-11 14:50:10 +010059typedef struct mbedtls_ecjpake_context {
Neil Armstrong0d763412022-08-11 10:32:22 +020060 mbedtls_md_type_t MBEDTLS_PRIVATE(md_type); /**< Hash to use */
Mateusz Starzyk846f0212021-05-19 19:44:07 +020061 mbedtls_ecp_group MBEDTLS_PRIVATE(grp); /**< Elliptic curve */
62 mbedtls_ecjpake_role MBEDTLS_PRIVATE(role); /**< Are we client or server? */
63 int MBEDTLS_PRIVATE(point_format); /**< Format for point export */
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020064
Mateusz Starzyk846f0212021-05-19 19:44:07 +020065 mbedtls_ecp_point MBEDTLS_PRIVATE(Xm1); /**< My public key 1 C: X1, S: X3 */
66 mbedtls_ecp_point MBEDTLS_PRIVATE(Xm2); /**< My public key 2 C: X2, S: X4 */
67 mbedtls_ecp_point MBEDTLS_PRIVATE(Xp1); /**< Peer public key 1 C: X3, S: X1 */
68 mbedtls_ecp_point MBEDTLS_PRIVATE(Xp2); /**< Peer public key 2 C: X4, S: X2 */
69 mbedtls_ecp_point MBEDTLS_PRIVATE(Xp); /**< Peer public key C: Xs, S: Xc */
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020070
Mateusz Starzyk846f0212021-05-19 19:44:07 +020071 mbedtls_mpi MBEDTLS_PRIVATE(xm1); /**< My private key 1 C: x1, S: x3 */
72 mbedtls_mpi MBEDTLS_PRIVATE(xm2); /**< My private key 2 C: x2, S: x4 */
Manuel Pégourié-Gonnard23dcbe32015-08-13 09:37:00 +020073
Mateusz Starzyk846f0212021-05-19 19:44:07 +020074 mbedtls_mpi MBEDTLS_PRIVATE(s); /**< Pre-shared secret (passphrase) */
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020075} mbedtls_ecjpake_context;
76
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +020077/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050078 * \brief Initialize an ECJPAKE context.
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020079 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050080 * \param ctx The ECJPAKE context to initialize.
81 * This must not be \c NULL.
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020082 */
Gilles Peskine449bd832023-01-11 14:50:10 +010083void mbedtls_ecjpake_init(mbedtls_ecjpake_context *ctx);
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020084
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +020085/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050086 * \brief Set up an ECJPAKE context for use.
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020087 *
88 * \note Currently the only values for hash/curve allowed by the
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050089 * standard are #MBEDTLS_MD_SHA256/#MBEDTLS_ECP_DP_SECP256R1.
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020090 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050091 * \param ctx The ECJPAKE context to set up. This must be initialized.
92 * \param role The role of the caller. This must be either
93 * #MBEDTLS_ECJPAKE_CLIENT or #MBEDTLS_ECJPAKE_SERVER.
94 * \param hash The identifier of the hash function to use,
95 * for example #MBEDTLS_MD_SHA256.
96 * \param curve The identifier of the elliptic curve to use,
97 * for example #MBEDTLS_ECP_DP_SECP256R1.
98 * \param secret The pre-shared secret (passphrase). This must be
Valerio Settiaca21b72022-11-17 18:17:01 +010099 * a readable not empty buffer of length \p len Bytes. It need
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500100 * only be valid for the duration of this call.
101 * \param len The length of the pre-shared secret \p secret.
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200102 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500103 * \return \c 0 if successful.
104 * \return A negative error code on failure.
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200105 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100106int mbedtls_ecjpake_setup(mbedtls_ecjpake_context *ctx,
107 mbedtls_ecjpake_role role,
108 mbedtls_md_type_t hash,
109 mbedtls_ecp_group_id curve,
110 const unsigned char *secret,
111 size_t len);
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200112
Andres Amaya Garciaaf610a02016-12-14 10:13:43 +0000113/**
Gilles Peskinecd07e222021-05-27 23:17:34 +0200114 * \brief Set the point format for future reads and writes.
115 *
116 * \param ctx The ECJPAKE context to configure.
117 * \param point_format The point format to use:
118 * #MBEDTLS_ECP_PF_UNCOMPRESSED (default)
119 * or #MBEDTLS_ECP_PF_COMPRESSED.
120 *
121 * \return \c 0 if successful.
122 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p point_format
123 * is invalid.
124 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100125int mbedtls_ecjpake_set_point_format(mbedtls_ecjpake_context *ctx,
126 int point_format);
Gilles Peskinecd07e222021-05-27 23:17:34 +0200127
128/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500129 * \brief Check if an ECJPAKE context is ready for use.
Manuel Pégourié-Gonnardb813acc2015-09-15 15:34:09 +0200130 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500131 * \param ctx The ECJPAKE context to check. This must be
132 * initialized.
Manuel Pégourié-Gonnardb813acc2015-09-15 15:34:09 +0200133 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500134 * \return \c 0 if the context is ready for use.
135 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA otherwise.
Manuel Pégourié-Gonnardb813acc2015-09-15 15:34:09 +0200136 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100137int mbedtls_ecjpake_check(const mbedtls_ecjpake_context *ctx);
Manuel Pégourié-Gonnardb813acc2015-09-15 15:34:09 +0200138
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200139/**
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +0200140 * \brief Generate and write the first round message
141 * (TLS: contents of the Client/ServerHello extension,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500142 * excluding extension type and length bytes).
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200143 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500144 * \param ctx The ECJPAKE context to use. This must be
145 * initialized and set up.
146 * \param buf The buffer to write the contents to. This must be a
147 * writable buffer of length \p len Bytes.
148 * \param len The length of \p buf in Bytes.
149 * \param olen The address at which to store the total number
150 * of Bytes written to \p buf. This must not be \c NULL.
151 * \param f_rng The RNG function to use. This must not be \c NULL.
152 * \param p_rng The RNG parameter to be passed to \p f_rng. This
153 * may be \c NULL if \p f_rng doesn't use a context.
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200154 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500155 * \return \c 0 if successful.
156 * \return A negative error code on failure.
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200157 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100158int mbedtls_ecjpake_write_round_one(mbedtls_ecjpake_context *ctx,
159 unsigned char *buf, size_t len, size_t *olen,
160 int (*f_rng)(void *, unsigned char *, size_t),
161 void *p_rng);
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200162
163/**
164 * \brief Read and process the first round message
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +0200165 * (TLS: contents of the Client/ServerHello extension,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500166 * excluding extension type and length bytes).
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200167 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500168 * \param ctx The ECJPAKE context to use. This must be initialized
169 * and set up.
170 * \param buf The buffer holding the first round message. This must
171 * be a readable buffer of length \p len Bytes.
172 * \param len The length in Bytes of \p buf.
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200173 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500174 * \return \c 0 if successful.
175 * \return A negative error code on failure.
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200176 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100177int mbedtls_ecjpake_read_round_one(mbedtls_ecjpake_context *ctx,
178 const unsigned char *buf,
179 size_t len);
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200180
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200181/**
182 * \brief Generate and write the second round message
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500183 * (TLS: contents of the Client/ServerKeyExchange).
Manuel Pégourié-Gonnard614bd5e2015-08-13 20:19:16 +0200184 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500185 * \param ctx The ECJPAKE context to use. This must be initialized,
186 * set up, and already have performed round one.
187 * \param buf The buffer to write the round two contents to.
188 * This must be a writable buffer of length \p len Bytes.
189 * \param len The size of \p buf in Bytes.
190 * \param olen The address at which to store the total number of Bytes
191 * written to \p buf. This must not be \c NULL.
192 * \param f_rng The RNG function to use. This must not be \c NULL.
193 * \param p_rng The RNG parameter to be passed to \p f_rng. This
194 * may be \c NULL if \p f_rng doesn't use a context.
Manuel Pégourié-Gonnard614bd5e2015-08-13 20:19:16 +0200195 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500196 * \return \c 0 if successful.
197 * \return A negative error code on failure.
Manuel Pégourié-Gonnard614bd5e2015-08-13 20:19:16 +0200198 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100199int mbedtls_ecjpake_write_round_two(mbedtls_ecjpake_context *ctx,
200 unsigned char *buf, size_t len, size_t *olen,
201 int (*f_rng)(void *, unsigned char *, size_t),
202 void *p_rng);
Manuel Pégourié-Gonnard614bd5e2015-08-13 20:19:16 +0200203
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200204/**
205 * \brief Read and process the second round message
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500206 * (TLS: contents of the Client/ServerKeyExchange).
Manuel Pégourié-Gonnardec0eece2015-08-13 19:13:20 +0200207 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500208 * \param ctx The ECJPAKE context to use. This must be initialized
209 * and set up and already have performed round one.
210 * \param buf The buffer holding the second round message. This must
211 * be a readable buffer of length \p len Bytes.
212 * \param len The length in Bytes of \p buf.
Manuel Pégourié-Gonnardec0eece2015-08-13 19:13:20 +0200213 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500214 * \return \c 0 if successful.
215 * \return A negative error code on failure.
Manuel Pégourié-Gonnardec0eece2015-08-13 19:13:20 +0200216 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100217int mbedtls_ecjpake_read_round_two(mbedtls_ecjpake_context *ctx,
218 const unsigned char *buf,
219 size_t len);
Manuel Pégourié-Gonnardec0eece2015-08-13 19:13:20 +0200220
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200221/**
222 * \brief Derive the shared secret
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500223 * (TLS: Pre-Master Secret).
Manuel Pégourié-Gonnard5f188292015-08-14 10:52:39 +0200224 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500225 * \param ctx The ECJPAKE context to use. This must be initialized,
226 * set up and have performed both round one and two.
227 * \param buf The buffer to write the derived secret to. This must
228 * be a writable buffer of length \p len Bytes.
229 * \param len The length of \p buf in Bytes.
230 * \param olen The address at which to store the total number of Bytes
231 * written to \p buf. This must not be \c NULL.
232 * \param f_rng The RNG function to use. This must not be \c NULL.
233 * \param p_rng The RNG parameter to be passed to \p f_rng. This
234 * may be \c NULL if \p f_rng doesn't use a context.
Manuel Pégourié-Gonnard5f188292015-08-14 10:52:39 +0200235 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500236 * \return \c 0 if successful.
237 * \return A negative error code on failure.
Manuel Pégourié-Gonnard5f188292015-08-14 10:52:39 +0200238 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100239int mbedtls_ecjpake_derive_secret(mbedtls_ecjpake_context *ctx,
240 unsigned char *buf, size_t len, size_t *olen,
241 int (*f_rng)(void *, unsigned char *, size_t),
242 void *p_rng);
Manuel Pégourié-Gonnard5f188292015-08-14 10:52:39 +0200243
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200244/**
Neil Armstrong12663092022-06-15 16:00:00 +0200245 * \brief Write the shared key material to be passed to a Key
246 * Derivation Function as described in RFC8236.
247 *
248 * \param ctx The ECJPAKE context to use. This must be initialized,
249 * set up and have performed both round one and two.
250 * \param buf The buffer to write the derived secret to. This must
251 * be a writable buffer of length \p len Bytes.
252 * \param len The length of \p buf in Bytes.
Neil Armstrong7cd4eac2022-09-08 14:57:55 +0200253 * \param olen The address at which to store the total number of bytes
Neil Armstrong12663092022-06-15 16:00:00 +0200254 * written to \p buf. This must not be \c NULL.
255 * \param f_rng The RNG function to use. This must not be \c NULL.
256 * \param p_rng The RNG parameter to be passed to \p f_rng. This
257 * may be \c NULL if \p f_rng doesn't use a context.
258 *
259 * \return \c 0 if successful.
260 * \return A negative error code on failure.
261 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100262int mbedtls_ecjpake_write_shared_key(mbedtls_ecjpake_context *ctx,
263 unsigned char *buf, size_t len, size_t *olen,
264 int (*f_rng)(void *, unsigned char *, size_t),
265 void *p_rng);
Neil Armstrong12663092022-06-15 16:00:00 +0200266
267/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500268 * \brief This clears an ECJPAKE context and frees any
269 * embedded data structure.
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200270 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500271 * \param ctx The ECJPAKE context to free. This may be \c NULL,
272 * in which case this function does nothing. If it is not
273 * \c NULL, it must point to an initialized ECJPAKE context.
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200274 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100275void mbedtls_ecjpake_free(mbedtls_ecjpake_context *ctx);
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200276
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +0200277#if defined(MBEDTLS_SELF_TEST)
Hanno Becker616d1ca2018-01-24 10:25:05 +0000278
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +0200279/**
280 * \brief Checkup routine
281 *
282 * \return 0 if successful, or 1 if a test failed
283 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100284int mbedtls_ecjpake_self_test(int verbose);
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +0200285
Ron Eldor4e6d55d2018-02-07 16:36:15 +0200286#endif /* MBEDTLS_SELF_TEST */
287
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +0200288#ifdef __cplusplus
289}
290#endif
291
Hanno Becker616d1ca2018-01-24 10:25:05 +0000292
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +0200293#endif /* ecjpake.h */