blob: b9928386dcd31187ee445b84ee41369b01712237 [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
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +02008 * 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.
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +020021 */
22#ifndef MBEDTLS_ECJPAKE_H
23#define MBEDTLS_ECJPAKE_H
24
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020025/*
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +020026 * J-PAKE is a password-authenticated key exchange that allows deriving a
27 * strong shared secret from a (potentially low entropy) pre-shared
28 * passphrase, with forward secrecy and mutual authentication.
29 * https://en.wikipedia.org/wiki/Password_Authenticated_Key_Exchange_by_Juggling
30 *
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +020031 * This file implements the Elliptic Curve variant of J-PAKE,
32 * as defined in Chapter 7.4 of the Thread v1.0 Specification,
33 * available to members of the Thread Group http://threadgroup.org/
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +020034 *
35 * As the J-PAKE algorithm is inherently symmetric, so is our API.
36 * Each party needs to send its first round message, in any order, to the
37 * other party, then each sends its second round message, in any order.
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +020038 * The payloads are serialized in a way suitable for use in TLS, but could
39 * also be use outside TLS.
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020040 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050041#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010042#include "mbedtls/config.h"
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050043#else
44#include MBEDTLS_CONFIG_FILE
45#endif
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020046
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010047#include "mbedtls/ecp.h"
48#include "mbedtls/md.h"
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +020049
50#ifdef __cplusplus
51extern "C" {
52#endif
53
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020054/**
55 * Roles in the EC J-PAKE exchange
56 */
Manuel Pégourié-Gonnard64493912015-08-13 20:19:51 +020057typedef enum {
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020058 MBEDTLS_ECJPAKE_CLIENT = 0, /**< Client */
59 MBEDTLS_ECJPAKE_SERVER, /**< Server */
Manuel Pégourié-Gonnard64493912015-08-13 20:19:51 +020060} mbedtls_ecjpake_role;
61
Ron Eldor4e6d55d2018-02-07 16:36:15 +020062#if !defined(MBEDTLS_ECJPAKE_ALT)
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020063/**
Manuel Pégourié-Gonnardce456762015-08-14 11:54:35 +020064 * EC J-PAKE context structure.
65 *
66 * J-PAKE is a symmetric protocol, except for the identifiers used in
67 * Zero-Knowledge Proofs, and the serialization of the second message
68 * (KeyExchange) as defined by the Thread spec.
69 *
70 * In order to benefit from this symmetry, we choose a different naming
Shaun Case0e7791f2021-12-20 21:14:10 -080071 * convention from the Thread v1.0 spec. Correspondence is indicated in the
Simon Butcher5b331b92016-01-03 16:14:14 +000072 * description as a pair C: client name, S: server name
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020073 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010074typedef struct mbedtls_ecjpake_context {
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020075 const mbedtls_md_info_t *md_info; /**< Hash to use */
76 mbedtls_ecp_group grp; /**< Elliptic curve */
Manuel Pégourié-Gonnard64493912015-08-13 20:19:51 +020077 mbedtls_ecjpake_role role; /**< Are we client or server? */
Robert Cragie7cdad772015-10-02 13:31:41 +010078 int point_format; /**< Format for point export */
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020079
Manuel Pégourié-Gonnardce456762015-08-14 11:54:35 +020080 mbedtls_ecp_point Xm1; /**< My public key 1 C: X1, S: X3 */
81 mbedtls_ecp_point Xm2; /**< My public key 2 C: X2, S: X4 */
82 mbedtls_ecp_point Xp1; /**< Peer public key 1 C: X3, S: X1 */
83 mbedtls_ecp_point Xp2; /**< Peer public key 2 C: X4, S: X2 */
84 mbedtls_ecp_point Xp; /**< Peer public key C: Xs, S: Xc */
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020085
Manuel Pégourié-Gonnardce456762015-08-14 11:54:35 +020086 mbedtls_mpi xm1; /**< My private key 1 C: x1, S: x3 */
87 mbedtls_mpi xm2; /**< My private key 2 C: x2, S: x4 */
Manuel Pégourié-Gonnard23dcbe32015-08-13 09:37:00 +020088
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020089 mbedtls_mpi s; /**< Pre-shared secret (passphrase) */
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020090} mbedtls_ecjpake_context;
91
Ron Eldor4e6d55d2018-02-07 16:36:15 +020092#else /* MBEDTLS_ECJPAKE_ALT */
93#include "ecjpake_alt.h"
94#endif /* MBEDTLS_ECJPAKE_ALT */
95
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +020096/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050097 * \brief Initialize an ECJPAKE context.
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020098 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050099 * \param ctx The ECJPAKE context to initialize.
100 * This must not be \c NULL.
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200101 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100102void mbedtls_ecjpake_init(mbedtls_ecjpake_context *ctx);
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200103
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200104/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500105 * \brief Set up an ECJPAKE context for use.
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200106 *
107 * \note Currently the only values for hash/curve allowed by the
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500108 * standard are #MBEDTLS_MD_SHA256/#MBEDTLS_ECP_DP_SECP256R1.
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200109 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500110 * \param ctx The ECJPAKE context to set up. This must be initialized.
111 * \param role The role of the caller. This must be either
112 * #MBEDTLS_ECJPAKE_CLIENT or #MBEDTLS_ECJPAKE_SERVER.
113 * \param hash The identifier of the hash function to use,
114 * for example #MBEDTLS_MD_SHA256.
115 * \param curve The identifier of the elliptic curve to use,
116 * for example #MBEDTLS_ECP_DP_SECP256R1.
117 * \param secret The pre-shared secret (passphrase). This must be
118 * a readable buffer of length \p len Bytes. It need
119 * only be valid for the duration of this call.
120 * \param len The length of the pre-shared secret \p secret.
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200121 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500122 * \return \c 0 if successful.
123 * \return A negative error code on failure.
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200124 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100125int mbedtls_ecjpake_setup(mbedtls_ecjpake_context *ctx,
126 mbedtls_ecjpake_role role,
127 mbedtls_md_type_t hash,
128 mbedtls_ecp_group_id curve,
129 const unsigned char *secret,
130 size_t len);
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200131
Andres Amaya Garciaaf610a02016-12-14 10:13:43 +0000132/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500133 * \brief Check if an ECJPAKE context is ready for use.
Manuel Pégourié-Gonnardb813acc2015-09-15 15:34:09 +0200134 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500135 * \param ctx The ECJPAKE context to check. This must be
136 * initialized.
Manuel Pégourié-Gonnardb813acc2015-09-15 15:34:09 +0200137 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500138 * \return \c 0 if the context is ready for use.
139 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA otherwise.
Manuel Pégourié-Gonnardb813acc2015-09-15 15:34:09 +0200140 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100141int mbedtls_ecjpake_check(const mbedtls_ecjpake_context *ctx);
Manuel Pégourié-Gonnardb813acc2015-09-15 15:34:09 +0200142
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200143/**
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +0200144 * \brief Generate and write the first round message
145 * (TLS: contents of the Client/ServerHello extension,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500146 * excluding extension type and length bytes).
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200147 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500148 * \param ctx The ECJPAKE context to use. This must be
149 * initialized and set up.
150 * \param buf The buffer to write the contents to. This must be a
151 * writable buffer of length \p len Bytes.
152 * \param len The length of \p buf in Bytes.
153 * \param olen The address at which to store the total number
154 * of Bytes written to \p buf. This must not be \c NULL.
155 * \param f_rng The RNG function to use. This must not be \c NULL.
156 * \param p_rng The RNG parameter to be passed to \p f_rng. This
157 * may be \c NULL if \p f_rng doesn't use a context.
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200158 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500159 * \return \c 0 if successful.
160 * \return A negative error code on failure.
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200161 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100162int mbedtls_ecjpake_write_round_one(mbedtls_ecjpake_context *ctx,
163 unsigned char *buf, size_t len, size_t *olen,
164 int (*f_rng)(void *, unsigned char *, size_t),
165 void *p_rng);
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200166
167/**
168 * \brief Read and process the first round message
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +0200169 * (TLS: contents of the Client/ServerHello extension,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500170 * excluding extension type and length bytes).
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200171 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500172 * \param ctx The ECJPAKE context to use. This must be initialized
173 * and set up.
174 * \param buf The buffer holding the first round message. This must
175 * be a readable buffer of length \p len Bytes.
176 * \param len The length in Bytes of \p buf.
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200177 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500178 * \return \c 0 if successful.
179 * \return A negative error code on failure.
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200180 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100181int mbedtls_ecjpake_read_round_one(mbedtls_ecjpake_context *ctx,
182 const unsigned char *buf,
183 size_t len);
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200184
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200185/**
186 * \brief Generate and write the second round message
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500187 * (TLS: contents of the Client/ServerKeyExchange).
Manuel Pégourié-Gonnard614bd5e2015-08-13 20:19:16 +0200188 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500189 * \param ctx The ECJPAKE context to use. This must be initialized,
190 * set up, and already have performed round one.
191 * \param buf The buffer to write the round two contents to.
192 * This must be a writable buffer of length \p len Bytes.
193 * \param len The size of \p buf in Bytes.
194 * \param olen The address at which to store the total number of Bytes
195 * written to \p buf. This must not be \c NULL.
196 * \param f_rng The RNG function to use. This must not be \c NULL.
197 * \param p_rng The RNG parameter to be passed to \p f_rng. This
198 * may be \c NULL if \p f_rng doesn't use a context.
Manuel Pégourié-Gonnard614bd5e2015-08-13 20:19:16 +0200199 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500200 * \return \c 0 if successful.
201 * \return A negative error code on failure.
Manuel Pégourié-Gonnard614bd5e2015-08-13 20:19:16 +0200202 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100203int mbedtls_ecjpake_write_round_two(mbedtls_ecjpake_context *ctx,
204 unsigned char *buf, size_t len, size_t *olen,
205 int (*f_rng)(void *, unsigned char *, size_t),
206 void *p_rng);
Manuel Pégourié-Gonnard614bd5e2015-08-13 20:19:16 +0200207
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200208/**
209 * \brief Read and process the second round message
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500210 * (TLS: contents of the Client/ServerKeyExchange).
Manuel Pégourié-Gonnardec0eece2015-08-13 19:13:20 +0200211 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500212 * \param ctx The ECJPAKE context to use. This must be initialized
213 * and set up and already have performed round one.
214 * \param buf The buffer holding the second round message. This must
215 * be a readable buffer of length \p len Bytes.
216 * \param len The length in Bytes of \p buf.
Manuel Pégourié-Gonnardec0eece2015-08-13 19:13:20 +0200217 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500218 * \return \c 0 if successful.
219 * \return A negative error code on failure.
Manuel Pégourié-Gonnardec0eece2015-08-13 19:13:20 +0200220 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100221int mbedtls_ecjpake_read_round_two(mbedtls_ecjpake_context *ctx,
222 const unsigned char *buf,
223 size_t len);
Manuel Pégourié-Gonnardec0eece2015-08-13 19:13:20 +0200224
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200225/**
226 * \brief Derive the shared secret
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500227 * (TLS: Pre-Master Secret).
Manuel Pégourié-Gonnard5f188292015-08-14 10:52:39 +0200228 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500229 * \param ctx The ECJPAKE context to use. This must be initialized,
230 * set up and have performed both round one and two.
231 * \param buf The buffer to write the derived secret to. This must
232 * be a writable buffer of length \p len Bytes.
233 * \param len The length of \p buf in Bytes.
234 * \param olen The address at which to store the total number of Bytes
235 * written to \p buf. This must not be \c NULL.
236 * \param f_rng The RNG function to use. This must not be \c NULL.
237 * \param p_rng The RNG parameter to be passed to \p f_rng. This
238 * may be \c NULL if \p f_rng doesn't use a context.
Manuel Pégourié-Gonnard5f188292015-08-14 10:52:39 +0200239 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500240 * \return \c 0 if successful.
241 * \return A negative error code on failure.
Manuel Pégourié-Gonnard5f188292015-08-14 10:52:39 +0200242 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100243int mbedtls_ecjpake_derive_secret(mbedtls_ecjpake_context *ctx,
244 unsigned char *buf, size_t len, size_t *olen,
245 int (*f_rng)(void *, unsigned char *, size_t),
246 void *p_rng);
Manuel Pégourié-Gonnard5f188292015-08-14 10:52:39 +0200247
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200248/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500249 * \brief This clears an ECJPAKE context and frees any
250 * embedded data structure.
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200251 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500252 * \param ctx The ECJPAKE context to free. This may be \c NULL,
253 * in which case this function does nothing. If it is not
254 * \c NULL, it must point to an initialized ECJPAKE context.
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200255 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100256void mbedtls_ecjpake_free(mbedtls_ecjpake_context *ctx);
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200257
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +0200258#if defined(MBEDTLS_SELF_TEST)
Hanno Becker616d1ca2018-01-24 10:25:05 +0000259
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +0200260/**
261 * \brief Checkup routine
262 *
263 * \return 0 if successful, or 1 if a test failed
264 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100265int mbedtls_ecjpake_self_test(int verbose);
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +0200266
Ron Eldor4e6d55d2018-02-07 16:36:15 +0200267#endif /* MBEDTLS_SELF_TEST */
268
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +0200269#ifdef __cplusplus
270}
271#endif
272
Hanno Becker616d1ca2018-01-24 10:25:05 +0000273
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +0200274#endif /* ecjpake.h */