blob: 97387c3b4880ab56611ac47a7446e1d2097326d1 [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
8 * 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.
21 *
22 * This file is part of mbed TLS (https://tls.mbed.org)
23 */
24#ifndef MBEDTLS_ECJPAKE_H
25#define MBEDTLS_ECJPAKE_H
26
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020027/*
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +020028 * J-PAKE is a password-authenticated key exchange that allows deriving a
29 * strong shared secret from a (potentially low entropy) pre-shared
30 * passphrase, with forward secrecy and mutual authentication.
31 * https://en.wikipedia.org/wiki/Password_Authenticated_Key_Exchange_by_Juggling
32 *
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +020033 * This file implements the Elliptic Curve variant of J-PAKE,
34 * as defined in Chapter 7.4 of the Thread v1.0 Specification,
35 * available to members of the Thread Group http://threadgroup.org/
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +020036 *
37 * As the J-PAKE algorithm is inherently symmetric, so is our API.
38 * Each party needs to send its first round message, in any order, to the
39 * other party, then each sends its second round message, in any order.
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +020040 * The payloads are serialized in a way suitable for use in TLS, but could
41 * also be use outside TLS.
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020042 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050043#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010044#include "mbedtls/config.h"
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050045#else
46#include MBEDTLS_CONFIG_FILE
47#endif
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020048
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010049#include "mbedtls/ecp.h"
50#include "mbedtls/md.h"
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +020051
52#ifdef __cplusplus
53extern "C" {
54#endif
55
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020056/**
57 * Roles in the EC J-PAKE exchange
58 */
Manuel Pégourié-Gonnard64493912015-08-13 20:19:51 +020059typedef enum {
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020060 MBEDTLS_ECJPAKE_CLIENT = 0, /**< Client */
61 MBEDTLS_ECJPAKE_SERVER, /**< Server */
Manuel Pégourié-Gonnard64493912015-08-13 20:19:51 +020062} mbedtls_ecjpake_role;
63
Ron Eldor4e6d55d2018-02-07 16:36:15 +020064#if !defined(MBEDTLS_ECJPAKE_ALT)
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020065/**
Manuel Pégourié-Gonnardce456762015-08-14 11:54:35 +020066 * EC J-PAKE context structure.
67 *
68 * J-PAKE is a symmetric protocol, except for the identifiers used in
69 * Zero-Knowledge Proofs, and the serialization of the second message
70 * (KeyExchange) as defined by the Thread spec.
71 *
72 * In order to benefit from this symmetry, we choose a different naming
73 * convetion from the Thread v1.0 spec. Correspondance is indicated in the
Simon Butcher5b331b92016-01-03 16:14:14 +000074 * description as a pair C: client name, S: server name
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020075 */
Dawid Drozd428cc522018-07-24 10:02:47 +020076typedef struct mbedtls_ecjpake_context
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020077{
78 const mbedtls_md_info_t *md_info; /**< Hash to use */
79 mbedtls_ecp_group grp; /**< Elliptic curve */
Manuel Pégourié-Gonnard64493912015-08-13 20:19:51 +020080 mbedtls_ecjpake_role role; /**< Are we client or server? */
Robert Cragie7cdad772015-10-02 13:31:41 +010081 int point_format; /**< Format for point export */
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020082
Manuel Pégourié-Gonnardce456762015-08-14 11:54:35 +020083 mbedtls_ecp_point Xm1; /**< My public key 1 C: X1, S: X3 */
84 mbedtls_ecp_point Xm2; /**< My public key 2 C: X2, S: X4 */
85 mbedtls_ecp_point Xp1; /**< Peer public key 1 C: X3, S: X1 */
86 mbedtls_ecp_point Xp2; /**< Peer public key 2 C: X4, S: X2 */
87 mbedtls_ecp_point Xp; /**< Peer public key C: Xs, S: Xc */
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020088
Manuel Pégourié-Gonnardce456762015-08-14 11:54:35 +020089 mbedtls_mpi xm1; /**< My private key 1 C: x1, S: x3 */
90 mbedtls_mpi xm2; /**< My private key 2 C: x2, S: x4 */
Manuel Pégourié-Gonnard23dcbe32015-08-13 09:37:00 +020091
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020092 mbedtls_mpi s; /**< Pre-shared secret (passphrase) */
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020093} mbedtls_ecjpake_context;
94
Ron Eldor4e6d55d2018-02-07 16:36:15 +020095#else /* MBEDTLS_ECJPAKE_ALT */
96#include "ecjpake_alt.h"
97#endif /* MBEDTLS_ECJPAKE_ALT */
98
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +020099/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500100 * \brief Initialize an ECJPAKE context.
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200101 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500102 * \param ctx The ECJPAKE context to initialize.
103 * This must not be \c NULL.
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200104 */
105void mbedtls_ecjpake_init( mbedtls_ecjpake_context *ctx );
106
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200107/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500108 * \brief Set up an ECJPAKE context for use.
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200109 *
110 * \note Currently the only values for hash/curve allowed by the
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500111 * standard are #MBEDTLS_MD_SHA256/#MBEDTLS_ECP_DP_SECP256R1.
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200112 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500113 * \param ctx The ECJPAKE context to set up. This must be initialized.
114 * \param role The role of the caller. This must be either
115 * #MBEDTLS_ECJPAKE_CLIENT or #MBEDTLS_ECJPAKE_SERVER.
116 * \param hash The identifier of the hash function to use,
117 * for example #MBEDTLS_MD_SHA256.
118 * \param curve The identifier of the elliptic curve to use,
119 * for example #MBEDTLS_ECP_DP_SECP256R1.
120 * \param secret The pre-shared secret (passphrase). This must be
121 * a readable buffer of length \p len Bytes. It need
122 * only be valid for the duration of this call.
123 * \param len The length of the pre-shared secret \p secret.
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200124 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500125 * \return \c 0 if successful.
126 * \return A negative error code on failure.
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200127 */
128int mbedtls_ecjpake_setup( mbedtls_ecjpake_context *ctx,
Manuel Pégourié-Gonnard64493912015-08-13 20:19:51 +0200129 mbedtls_ecjpake_role role,
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200130 mbedtls_md_type_t hash,
Manuel Pégourié-Gonnard23dcbe32015-08-13 09:37:00 +0200131 mbedtls_ecp_group_id curve,
132 const unsigned char *secret,
133 size_t len );
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200134
Andres Amaya Garciaaf610a02016-12-14 10:13:43 +0000135/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500136 * \brief Check if an ECJPAKE context is ready for use.
Manuel Pégourié-Gonnardb813acc2015-09-15 15:34:09 +0200137 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500138 * \param ctx The ECJPAKE context to check. This must be
139 * initialized.
Manuel Pégourié-Gonnardb813acc2015-09-15 15:34:09 +0200140 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500141 * \return \c 0 if the context is ready for use.
142 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA otherwise.
Manuel Pégourié-Gonnardb813acc2015-09-15 15:34:09 +0200143 */
144int mbedtls_ecjpake_check( const mbedtls_ecjpake_context *ctx );
145
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200146/**
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +0200147 * \brief Generate and write the first round message
148 * (TLS: contents of the Client/ServerHello extension,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500149 * excluding extension type and length bytes).
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200150 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500151 * \param ctx The ECJPAKE context to use. This must be
152 * initialized and set up.
153 * \param buf The buffer to write the contents to. This must be a
154 * writable buffer of length \p len Bytes.
155 * \param len The length of \p buf in Bytes.
156 * \param olen The address at which to store the total number
157 * of Bytes written to \p buf. This must not be \c NULL.
158 * \param f_rng The RNG function to use. This must not be \c NULL.
159 * \param p_rng The RNG parameter to be passed to \p f_rng. This
160 * may be \c NULL if \p f_rng doesn't use a context.
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200161 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500162 * \return \c 0 if successful.
163 * \return A negative error code on failure.
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200164 */
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +0200165int mbedtls_ecjpake_write_round_one( mbedtls_ecjpake_context *ctx,
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200166 unsigned char *buf, size_t len, size_t *olen,
167 int (*f_rng)(void *, unsigned char *, size_t),
168 void *p_rng );
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200169
170/**
171 * \brief Read and process the first round message
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +0200172 * (TLS: contents of the Client/ServerHello extension,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500173 * excluding extension type and length bytes).
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200174 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500175 * \param ctx The ECJPAKE context to use. This must be initialized
176 * and set up.
177 * \param buf The buffer holding the first round message. This must
178 * be a readable buffer of length \p len Bytes.
179 * \param len The length in Bytes of \p buf.
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200180 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500181 * \return \c 0 if successful.
182 * \return A negative error code on failure.
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200183 */
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +0200184int mbedtls_ecjpake_read_round_one( mbedtls_ecjpake_context *ctx,
185 const unsigned char *buf,
186 size_t len );
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200187
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200188/**
189 * \brief Generate and write the second round message
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500190 * (TLS: contents of the Client/ServerKeyExchange).
Manuel Pégourié-Gonnard614bd5e2015-08-13 20:19:16 +0200191 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500192 * \param ctx The ECJPAKE context to use. This must be initialized,
193 * set up, and already have performed round one.
194 * \param buf The buffer to write the round two contents to.
195 * This must be a writable buffer of length \p len Bytes.
196 * \param len The size of \p buf in Bytes.
197 * \param olen The address at which to store the total number of Bytes
198 * written to \p buf. This must not be \c NULL.
199 * \param f_rng The RNG function to use. This must not be \c NULL.
200 * \param p_rng The RNG parameter to be passed to \p f_rng. This
201 * may be \c NULL if \p f_rng doesn't use a context.
Manuel Pégourié-Gonnard614bd5e2015-08-13 20:19:16 +0200202 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500203 * \return \c 0 if successful.
204 * \return A negative error code on failure.
Manuel Pégourié-Gonnard614bd5e2015-08-13 20:19:16 +0200205 */
Manuel Pégourié-Gonnarde1927102015-08-14 14:20:48 +0200206int mbedtls_ecjpake_write_round_two( mbedtls_ecjpake_context *ctx,
Manuel Pégourié-Gonnard614bd5e2015-08-13 20:19:16 +0200207 unsigned char *buf, size_t len, size_t *olen,
208 int (*f_rng)(void *, unsigned char *, size_t),
209 void *p_rng );
210
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200211/**
212 * \brief Read and process the second round message
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500213 * (TLS: contents of the Client/ServerKeyExchange).
Manuel Pégourié-Gonnardec0eece2015-08-13 19:13:20 +0200214 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500215 * \param ctx The ECJPAKE context to use. This must be initialized
216 * and set up and already have performed round one.
217 * \param buf The buffer holding the second round message. This must
218 * be a readable buffer of length \p len Bytes.
219 * \param len The length in Bytes of \p buf.
Manuel Pégourié-Gonnardec0eece2015-08-13 19:13:20 +0200220 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500221 * \return \c 0 if successful.
222 * \return A negative error code on failure.
Manuel Pégourié-Gonnardec0eece2015-08-13 19:13:20 +0200223 */
Manuel Pégourié-Gonnarde1927102015-08-14 14:20:48 +0200224int mbedtls_ecjpake_read_round_two( mbedtls_ecjpake_context *ctx,
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200225 const unsigned char *buf,
226 size_t len );
Manuel Pégourié-Gonnardec0eece2015-08-13 19:13:20 +0200227
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200228/**
229 * \brief Derive the shared secret
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500230 * (TLS: Pre-Master Secret).
Manuel Pégourié-Gonnard5f188292015-08-14 10:52:39 +0200231 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500232 * \param ctx The ECJPAKE context to use. This must be initialized,
233 * set up and have performed both round one and two.
234 * \param buf The buffer to write the derived secret to. This must
235 * be a writable buffer of length \p len Bytes.
236 * \param len The length of \p buf in Bytes.
237 * \param olen The address at which to store the total number of Bytes
238 * written to \p buf. This must not be \c NULL.
239 * \param f_rng The RNG function to use. This must not be \c NULL.
240 * \param p_rng The RNG parameter to be passed to \p f_rng. This
241 * may be \c NULL if \p f_rng doesn't use a context.
Manuel Pégourié-Gonnard5f188292015-08-14 10:52:39 +0200242 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500243 * \return \c 0 if successful.
244 * \return A negative error code on failure.
Manuel Pégourié-Gonnard5f188292015-08-14 10:52:39 +0200245 */
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200246int mbedtls_ecjpake_derive_secret( mbedtls_ecjpake_context *ctx,
Manuel Pégourié-Gonnard5f188292015-08-14 10:52:39 +0200247 unsigned char *buf, size_t len, size_t *olen,
248 int (*f_rng)(void *, unsigned char *, size_t),
249 void *p_rng );
250
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200251/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500252 * \brief This clears an ECJPAKE context and frees any
253 * embedded data structure.
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200254 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500255 * \param ctx The ECJPAKE context to free. This may be \c NULL,
256 * in which case this function does nothing. If it is not
257 * \c NULL, it must point to an initialized ECJPAKE context.
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200258 */
259void mbedtls_ecjpake_free( mbedtls_ecjpake_context *ctx );
260
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +0200261#if defined(MBEDTLS_SELF_TEST)
Hanno Becker616d1ca2018-01-24 10:25:05 +0000262
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +0200263/**
264 * \brief Checkup routine
265 *
266 * \return 0 if successful, or 1 if a test failed
267 */
268int mbedtls_ecjpake_self_test( int verbose );
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +0200269
Ron Eldor4e6d55d2018-02-07 16:36:15 +0200270#endif /* MBEDTLS_SELF_TEST */
271
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +0200272#ifdef __cplusplus
273}
274#endif
275
Hanno Becker616d1ca2018-01-24 10:25:05 +0000276
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +0200277#endif /* ecjpake.h */