blob: 0c8e8c927423fed8c833b1207de33145f1b030f1 [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
Mateusz Starzyk846f0212021-05-19 19:44:07 +020024#include "mbedtls/private_access.h"
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +020025
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020026/*
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +020027 * J-PAKE is a password-authenticated key exchange that allows deriving a
28 * strong shared secret from a (potentially low entropy) pre-shared
29 * passphrase, with forward secrecy and mutual authentication.
30 * https://en.wikipedia.org/wiki/Password_Authenticated_Key_Exchange_by_Juggling
31 *
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +020032 * This file implements the Elliptic Curve variant of J-PAKE,
33 * as defined in Chapter 7.4 of the Thread v1.0 Specification,
34 * available to members of the Thread Group http://threadgroup.org/
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +020035 *
36 * As the J-PAKE algorithm is inherently symmetric, so is our API.
37 * Each party needs to send its first round message, in any order, to the
38 * other party, then each sends its second round message, in any order.
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +020039 * The payloads are serialized in a way suitable for use in TLS, but could
40 * also be use outside TLS.
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020041 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050042#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010043#include "mbedtls/config.h"
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050044#else
45#include MBEDTLS_CONFIG_FILE
46#endif
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020047
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010048#include "mbedtls/ecp.h"
49#include "mbedtls/md.h"
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +020050
51#ifdef __cplusplus
52extern "C" {
53#endif
54
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020055/**
56 * Roles in the EC J-PAKE exchange
57 */
Manuel Pégourié-Gonnard64493912015-08-13 20:19:51 +020058typedef enum {
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020059 MBEDTLS_ECJPAKE_CLIENT = 0, /**< Client */
60 MBEDTLS_ECJPAKE_SERVER, /**< Server */
Manuel Pégourié-Gonnard64493912015-08-13 20:19:51 +020061} mbedtls_ecjpake_role;
62
Ron Eldor4e6d55d2018-02-07 16:36:15 +020063#if !defined(MBEDTLS_ECJPAKE_ALT)
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020064/**
Manuel Pégourié-Gonnardce456762015-08-14 11:54:35 +020065 * EC J-PAKE context structure.
66 *
67 * J-PAKE is a symmetric protocol, except for the identifiers used in
68 * Zero-Knowledge Proofs, and the serialization of the second message
69 * (KeyExchange) as defined by the Thread spec.
70 *
71 * In order to benefit from this symmetry, we choose a different naming
72 * convetion from the Thread v1.0 spec. Correspondance is indicated in the
Simon Butcher5b331b92016-01-03 16:14:14 +000073 * description as a pair C: client name, S: server name
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020074 */
Dawid Drozd428cc522018-07-24 10:02:47 +020075typedef struct mbedtls_ecjpake_context
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020076{
Mateusz Starzyk846f0212021-05-19 19:44:07 +020077 const mbedtls_md_info_t *MBEDTLS_PRIVATE(md_info); /**< Hash to use */
78 mbedtls_ecp_group MBEDTLS_PRIVATE(grp); /**< Elliptic curve */
79 mbedtls_ecjpake_role MBEDTLS_PRIVATE(role); /**< Are we client or server? */
80 int MBEDTLS_PRIVATE(point_format); /**< Format for point export */
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020081
Mateusz Starzyk846f0212021-05-19 19:44:07 +020082 mbedtls_ecp_point MBEDTLS_PRIVATE(Xm1); /**< My public key 1 C: X1, S: X3 */
83 mbedtls_ecp_point MBEDTLS_PRIVATE(Xm2); /**< My public key 2 C: X2, S: X4 */
84 mbedtls_ecp_point MBEDTLS_PRIVATE(Xp1); /**< Peer public key 1 C: X3, S: X1 */
85 mbedtls_ecp_point MBEDTLS_PRIVATE(Xp2); /**< Peer public key 2 C: X4, S: X2 */
86 mbedtls_ecp_point MBEDTLS_PRIVATE(Xp); /**< Peer public key C: Xs, S: Xc */
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020087
Mateusz Starzyk846f0212021-05-19 19:44:07 +020088 mbedtls_mpi MBEDTLS_PRIVATE(xm1); /**< My private key 1 C: x1, S: x3 */
89 mbedtls_mpi MBEDTLS_PRIVATE(xm2); /**< My private key 2 C: x2, S: x4 */
Manuel Pégourié-Gonnard23dcbe32015-08-13 09:37:00 +020090
Mateusz Starzyk846f0212021-05-19 19:44:07 +020091 mbedtls_mpi MBEDTLS_PRIVATE(s); /**< Pre-shared secret (passphrase) */
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020092} mbedtls_ecjpake_context;
93
Ron Eldor4e6d55d2018-02-07 16:36:15 +020094#else /* MBEDTLS_ECJPAKE_ALT */
95#include "ecjpake_alt.h"
96#endif /* MBEDTLS_ECJPAKE_ALT */
97
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +020098/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050099 * \brief Initialize an ECJPAKE context.
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200100 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500101 * \param ctx The ECJPAKE context to initialize.
102 * This must not be \c NULL.
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200103 */
104void mbedtls_ecjpake_init( mbedtls_ecjpake_context *ctx );
105
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200106/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500107 * \brief Set up an ECJPAKE context for use.
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200108 *
109 * \note Currently the only values for hash/curve allowed by the
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500110 * standard are #MBEDTLS_MD_SHA256/#MBEDTLS_ECP_DP_SECP256R1.
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200111 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500112 * \param ctx The ECJPAKE context to set up. This must be initialized.
113 * \param role The role of the caller. This must be either
114 * #MBEDTLS_ECJPAKE_CLIENT or #MBEDTLS_ECJPAKE_SERVER.
115 * \param hash The identifier of the hash function to use,
116 * for example #MBEDTLS_MD_SHA256.
117 * \param curve The identifier of the elliptic curve to use,
118 * for example #MBEDTLS_ECP_DP_SECP256R1.
119 * \param secret The pre-shared secret (passphrase). This must be
120 * a readable buffer of length \p len Bytes. It need
121 * only be valid for the duration of this call.
122 * \param len The length of the pre-shared secret \p secret.
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200123 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500124 * \return \c 0 if successful.
125 * \return A negative error code on failure.
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200126 */
127int mbedtls_ecjpake_setup( mbedtls_ecjpake_context *ctx,
Manuel Pégourié-Gonnard64493912015-08-13 20:19:51 +0200128 mbedtls_ecjpake_role role,
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200129 mbedtls_md_type_t hash,
Manuel Pégourié-Gonnard23dcbe32015-08-13 09:37:00 +0200130 mbedtls_ecp_group_id curve,
131 const unsigned char *secret,
132 size_t len );
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200133
Andres Amaya Garciaaf610a02016-12-14 10:13:43 +0000134/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500135 * \brief Check if an ECJPAKE context is ready for use.
Manuel Pégourié-Gonnardb813acc2015-09-15 15:34:09 +0200136 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500137 * \param ctx The ECJPAKE context to check. This must be
138 * initialized.
Manuel Pégourié-Gonnardb813acc2015-09-15 15:34:09 +0200139 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500140 * \return \c 0 if the context is ready for use.
141 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA otherwise.
Manuel Pégourié-Gonnardb813acc2015-09-15 15:34:09 +0200142 */
143int mbedtls_ecjpake_check( const mbedtls_ecjpake_context *ctx );
144
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200145/**
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +0200146 * \brief Generate and write the first round message
147 * (TLS: contents of the Client/ServerHello extension,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500148 * excluding extension type and length bytes).
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200149 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500150 * \param ctx The ECJPAKE context to use. This must be
151 * initialized and set up.
152 * \param buf The buffer to write the contents to. This must be a
153 * writable buffer of length \p len Bytes.
154 * \param len The length of \p buf in Bytes.
155 * \param olen The address at which to store the total number
156 * of Bytes written to \p buf. This must not be \c NULL.
157 * \param f_rng The RNG function to use. This must not be \c NULL.
158 * \param p_rng The RNG parameter to be passed to \p f_rng. This
159 * may be \c NULL if \p f_rng doesn't use a context.
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200160 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500161 * \return \c 0 if successful.
162 * \return A negative error code on failure.
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200163 */
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +0200164int mbedtls_ecjpake_write_round_one( mbedtls_ecjpake_context *ctx,
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200165 unsigned char *buf, size_t len, size_t *olen,
166 int (*f_rng)(void *, unsigned char *, size_t),
167 void *p_rng );
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200168
169/**
170 * \brief Read and process the first round message
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +0200171 * (TLS: contents of the Client/ServerHello extension,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500172 * excluding extension type and length bytes).
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200173 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500174 * \param ctx The ECJPAKE context to use. This must be initialized
175 * and set up.
176 * \param buf The buffer holding the first round message. This must
177 * be a readable buffer of length \p len Bytes.
178 * \param len The length in Bytes of \p buf.
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200179 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500180 * \return \c 0 if successful.
181 * \return A negative error code on failure.
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200182 */
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +0200183int mbedtls_ecjpake_read_round_one( mbedtls_ecjpake_context *ctx,
184 const unsigned char *buf,
185 size_t len );
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200186
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200187/**
188 * \brief Generate and write the second round message
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500189 * (TLS: contents of the Client/ServerKeyExchange).
Manuel Pégourié-Gonnard614bd5e2015-08-13 20:19:16 +0200190 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500191 * \param ctx The ECJPAKE context to use. This must be initialized,
192 * set up, and already have performed round one.
193 * \param buf The buffer to write the round two contents to.
194 * This must be a writable buffer of length \p len Bytes.
195 * \param len The size of \p buf in Bytes.
196 * \param olen The address at which to store the total number of Bytes
197 * written to \p buf. This must not be \c NULL.
198 * \param f_rng The RNG function to use. This must not be \c NULL.
199 * \param p_rng The RNG parameter to be passed to \p f_rng. This
200 * may be \c NULL if \p f_rng doesn't use a context.
Manuel Pégourié-Gonnard614bd5e2015-08-13 20:19:16 +0200201 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500202 * \return \c 0 if successful.
203 * \return A negative error code on failure.
Manuel Pégourié-Gonnard614bd5e2015-08-13 20:19:16 +0200204 */
Manuel Pégourié-Gonnarde1927102015-08-14 14:20:48 +0200205int mbedtls_ecjpake_write_round_two( mbedtls_ecjpake_context *ctx,
Manuel Pégourié-Gonnard614bd5e2015-08-13 20:19:16 +0200206 unsigned char *buf, size_t len, size_t *olen,
207 int (*f_rng)(void *, unsigned char *, size_t),
208 void *p_rng );
209
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200210/**
211 * \brief Read and process the second round message
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500212 * (TLS: contents of the Client/ServerKeyExchange).
Manuel Pégourié-Gonnardec0eece2015-08-13 19:13:20 +0200213 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500214 * \param ctx The ECJPAKE context to use. This must be initialized
215 * and set up and already have performed round one.
216 * \param buf The buffer holding the second round message. This must
217 * be a readable buffer of length \p len Bytes.
218 * \param len The length in Bytes of \p buf.
Manuel Pégourié-Gonnardec0eece2015-08-13 19:13:20 +0200219 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500220 * \return \c 0 if successful.
221 * \return A negative error code on failure.
Manuel Pégourié-Gonnardec0eece2015-08-13 19:13:20 +0200222 */
Manuel Pégourié-Gonnarde1927102015-08-14 14:20:48 +0200223int mbedtls_ecjpake_read_round_two( mbedtls_ecjpake_context *ctx,
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200224 const unsigned char *buf,
225 size_t len );
Manuel Pégourié-Gonnardec0eece2015-08-13 19:13:20 +0200226
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200227/**
228 * \brief Derive the shared secret
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500229 * (TLS: Pre-Master Secret).
Manuel Pégourié-Gonnard5f188292015-08-14 10:52:39 +0200230 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500231 * \param ctx The ECJPAKE context to use. This must be initialized,
232 * set up and have performed both round one and two.
233 * \param buf The buffer to write the derived secret to. This must
234 * be a writable buffer of length \p len Bytes.
235 * \param len The length of \p buf in Bytes.
236 * \param olen The address at which to store the total number of Bytes
237 * written to \p buf. This must not be \c NULL.
238 * \param f_rng The RNG function to use. This must not be \c NULL.
239 * \param p_rng The RNG parameter to be passed to \p f_rng. This
240 * may be \c NULL if \p f_rng doesn't use a context.
Manuel Pégourié-Gonnard5f188292015-08-14 10:52:39 +0200241 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500242 * \return \c 0 if successful.
243 * \return A negative error code on failure.
Manuel Pégourié-Gonnard5f188292015-08-14 10:52:39 +0200244 */
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200245int mbedtls_ecjpake_derive_secret( mbedtls_ecjpake_context *ctx,
Manuel Pégourié-Gonnard5f188292015-08-14 10:52:39 +0200246 unsigned char *buf, size_t len, size_t *olen,
247 int (*f_rng)(void *, unsigned char *, size_t),
248 void *p_rng );
249
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200250/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500251 * \brief This clears an ECJPAKE context and frees any
252 * embedded data structure.
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200253 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500254 * \param ctx The ECJPAKE context to free. This may be \c NULL,
255 * in which case this function does nothing. If it is not
256 * \c NULL, it must point to an initialized ECJPAKE context.
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200257 */
258void mbedtls_ecjpake_free( mbedtls_ecjpake_context *ctx );
259
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +0200260#if defined(MBEDTLS_SELF_TEST)
Hanno Becker616d1ca2018-01-24 10:25:05 +0000261
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +0200262/**
263 * \brief Checkup routine
264 *
265 * \return 0 if successful, or 1 if a test failed
266 */
267int mbedtls_ecjpake_self_test( int verbose );
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +0200268
Ron Eldor4e6d55d2018-02-07 16:36:15 +0200269#endif /* MBEDTLS_SELF_TEST */
270
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +0200271#ifdef __cplusplus
272}
273#endif
274
Hanno Becker616d1ca2018-01-24 10:25:05 +0000275
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +0200276#endif /* ecjpake.h */