blob: 1e941702f60aa3656b5ab09c99824a989642dd2f [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úti44bfbe32020-08-19 16:54:51 +02007 * Copyright The Mbed TLS Contributors
Bence Szépkúti4e9f7122020-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úti4e9f7122020-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 * **********
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +020048 */
49#ifndef MBEDTLS_ECJPAKE_H
50#define MBEDTLS_ECJPAKE_H
51
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020052/*
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +020053 * J-PAKE is a password-authenticated key exchange that allows deriving a
54 * strong shared secret from a (potentially low entropy) pre-shared
55 * passphrase, with forward secrecy and mutual authentication.
56 * https://en.wikipedia.org/wiki/Password_Authenticated_Key_Exchange_by_Juggling
57 *
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +020058 * This file implements the Elliptic Curve variant of J-PAKE,
59 * as defined in Chapter 7.4 of the Thread v1.0 Specification,
60 * available to members of the Thread Group http://threadgroup.org/
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +020061 *
62 * As the J-PAKE algorithm is inherently symmetric, so is our API.
63 * Each party needs to send its first round message, in any order, to the
64 * other party, then each sends its second round message, in any order.
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +020065 * The payloads are serialized in a way suitable for use in TLS, but could
66 * also be use outside TLS.
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020067 */
Ron Eldor0559c662018-02-14 16:02:41 +020068#if !defined(MBEDTLS_CONFIG_FILE)
69#include "config.h"
70#else
71#include MBEDTLS_CONFIG_FILE
72#endif
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020073
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +020074#include "ecp.h"
75#include "md.h"
76
Hanno Becker616d1ca2018-01-24 10:25:05 +000077#if !defined(MBEDTLS_ECJPAKE_ALT)
78
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +020079#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
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020091/**
Manuel Pégourié-Gonnardce456762015-08-14 11:54:35 +020092 * EC J-PAKE context structure.
93 *
94 * J-PAKE is a symmetric protocol, except for the identifiers used in
95 * Zero-Knowledge Proofs, and the serialization of the second message
96 * (KeyExchange) as defined by the Thread spec.
97 *
98 * In order to benefit from this symmetry, we choose a different naming
99 * convetion from the Thread v1.0 spec. Correspondance is indicated in the
Simon Butcher5b331b92016-01-03 16:14:14 +0000100 * description as a pair C: client name, S: server name
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +0200101 */
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200102typedef struct
103{
104 const mbedtls_md_info_t *md_info; /**< Hash to use */
105 mbedtls_ecp_group grp; /**< Elliptic curve */
Manuel Pégourié-Gonnard64493912015-08-13 20:19:51 +0200106 mbedtls_ecjpake_role role; /**< Are we client or server? */
Robert Cragie7cdad772015-10-02 13:31:41 +0100107 int point_format; /**< Format for point export */
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200108
Manuel Pégourié-Gonnardce456762015-08-14 11:54:35 +0200109 mbedtls_ecp_point Xm1; /**< My public key 1 C: X1, S: X3 */
110 mbedtls_ecp_point Xm2; /**< My public key 2 C: X2, S: X4 */
111 mbedtls_ecp_point Xp1; /**< Peer public key 1 C: X3, S: X1 */
112 mbedtls_ecp_point Xp2; /**< Peer public key 2 C: X4, S: X2 */
113 mbedtls_ecp_point Xp; /**< Peer public key C: Xs, S: Xc */
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200114
Manuel Pégourié-Gonnardce456762015-08-14 11:54:35 +0200115 mbedtls_mpi xm1; /**< My private key 1 C: x1, S: x3 */
116 mbedtls_mpi xm2; /**< My private key 2 C: x2, S: x4 */
Manuel Pégourié-Gonnard23dcbe32015-08-13 09:37:00 +0200117
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +0200118 mbedtls_mpi s; /**< Pre-shared secret (passphrase) */
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200119} mbedtls_ecjpake_context;
120
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200121/**
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200122 * \brief Initialize a context
123 * (just makes it ready for setup() or free()).
124 *
125 * \param ctx context to initialize
126 */
127void mbedtls_ecjpake_init( mbedtls_ecjpake_context *ctx );
128
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200129/**
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200130 * \brief Set up a context for use
131 *
132 * \note Currently the only values for hash/curve allowed by the
133 * standard are MBEDTLS_MD_SHA256/MBEDTLS_ECP_DP_SECP256R1.
134 *
135 * \param ctx context to set up
Manuel Pégourié-Gonnard64493912015-08-13 20:19:51 +0200136 * \param role Our role: client or server
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200137 * \param hash hash function to use (MBEDTLS_MD_XXX)
138 * \param curve elliptic curve identifier (MBEDTLS_ECP_DP_XXX)
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +0200139 * \param secret pre-shared secret (passphrase)
Manuel Pégourié-Gonnard23dcbe32015-08-13 09:37:00 +0200140 * \param len length of the shared secret
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200141 *
142 * \return 0 if successfull,
143 * a negative error code otherwise
144 */
145int mbedtls_ecjpake_setup( mbedtls_ecjpake_context *ctx,
Manuel Pégourié-Gonnard64493912015-08-13 20:19:51 +0200146 mbedtls_ecjpake_role role,
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200147 mbedtls_md_type_t hash,
Manuel Pégourié-Gonnard23dcbe32015-08-13 09:37:00 +0200148 mbedtls_ecp_group_id curve,
149 const unsigned char *secret,
150 size_t len );
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200151
Andres Amaya Garciaaf610a02016-12-14 10:13:43 +0000152/**
Manuel Pégourié-Gonnardb813acc2015-09-15 15:34:09 +0200153 * \brief Check if a context is ready for use
154 *
155 * \param ctx Context to check
156 *
157 * \return 0 if the context is ready for use,
158 * MBEDTLS_ERR_ECP_BAD_INPUT_DATA otherwise
159 */
160int mbedtls_ecjpake_check( const mbedtls_ecjpake_context *ctx );
161
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200162/**
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +0200163 * \brief Generate and write the first round message
164 * (TLS: contents of the Client/ServerHello extension,
165 * excluding extension type and length bytes)
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200166 *
167 * \param ctx Context to use
168 * \param buf Buffer to write the contents to
169 * \param len Buffer size
170 * \param olen Will be updated with the number of bytes written
171 * \param f_rng RNG function
172 * \param p_rng RNG parameter
173 *
174 * \return 0 if successfull,
175 * a negative error code otherwise
176 */
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +0200177int mbedtls_ecjpake_write_round_one( mbedtls_ecjpake_context *ctx,
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200178 unsigned char *buf, size_t len, size_t *olen,
179 int (*f_rng)(void *, unsigned char *, size_t),
180 void *p_rng );
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200181
182/**
183 * \brief Read and process the first round message
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +0200184 * (TLS: contents of the Client/ServerHello extension,
185 * excluding extension type and length bytes)
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200186 *
187 * \param ctx Context to use
188 * \param buf Pointer to extension contents
189 * \param len Extension length
190 *
191 * \return 0 if successfull,
192 * a negative error code otherwise
193 */
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +0200194int mbedtls_ecjpake_read_round_one( mbedtls_ecjpake_context *ctx,
195 const unsigned char *buf,
196 size_t len );
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200197
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200198/**
199 * \brief Generate and write the second round message
200 * (TLS: contents of the Client/ServerKeyExchange)
Manuel Pégourié-Gonnard614bd5e2015-08-13 20:19:16 +0200201 *
202 * \param ctx Context to use
203 * \param buf Buffer to write the contents to
204 * \param len Buffer size
205 * \param olen Will be updated with the number of bytes written
206 * \param f_rng RNG function
207 * \param p_rng RNG parameter
208 *
209 * \return 0 if successfull,
210 * a negative error code otherwise
211 */
Manuel Pégourié-Gonnarde1927102015-08-14 14:20:48 +0200212int mbedtls_ecjpake_write_round_two( mbedtls_ecjpake_context *ctx,
Manuel Pégourié-Gonnard614bd5e2015-08-13 20:19:16 +0200213 unsigned char *buf, size_t len, size_t *olen,
214 int (*f_rng)(void *, unsigned char *, size_t),
215 void *p_rng );
216
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200217/**
218 * \brief Read and process the second round message
219 * (TLS: contents of the Client/ServerKeyExchange)
Manuel Pégourié-Gonnardec0eece2015-08-13 19:13:20 +0200220 *
221 * \param ctx Context to use
222 * \param buf Pointer to the message
223 * \param len Message length
224 *
225 * \return 0 if successfull,
226 * a negative error code otherwise
227 */
Manuel Pégourié-Gonnarde1927102015-08-14 14:20:48 +0200228int mbedtls_ecjpake_read_round_two( mbedtls_ecjpake_context *ctx,
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200229 const unsigned char *buf,
230 size_t len );
Manuel Pégourié-Gonnardec0eece2015-08-13 19:13:20 +0200231
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200232/**
233 * \brief Derive the shared secret
234 * (TLS: Pre-Master Secret)
Manuel Pégourié-Gonnard5f188292015-08-14 10:52:39 +0200235 *
Manuel Pégourié-Gonnard55f3d842015-08-14 15:08:43 +0200236 * \param ctx Context to use
Manuel Pégourié-Gonnard5f188292015-08-14 10:52:39 +0200237 * \param buf Buffer to write the contents to
238 * \param len Buffer size
239 * \param olen Will be updated with the number of bytes written
240 * \param f_rng RNG function
241 * \param p_rng RNG parameter
242 *
243 * \return 0 if successfull,
244 * a negative error code otherwise
245 */
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/**
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200252 * \brief Free a context's content
253 *
254 * \param ctx context to free
255 */
256void mbedtls_ecjpake_free( mbedtls_ecjpake_context *ctx );
257
Hanno Becker616d1ca2018-01-24 10:25:05 +0000258#ifdef __cplusplus
259}
260#endif
261
262#else /* MBEDTLS_ECJPAKE_ALT */
263#include "ecjpake_alt.h"
264#endif /* MBEDTLS_ECJPAKE_ALT */
265
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +0200266#if defined(MBEDTLS_SELF_TEST)
Hanno Becker616d1ca2018-01-24 10:25:05 +0000267
268#ifdef __cplusplus
269extern "C" {
270#endif
271
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +0200272/**
273 * \brief Checkup routine
274 *
275 * \return 0 if successful, or 1 if a test failed
276 */
277int mbedtls_ecjpake_self_test( int verbose );
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +0200278
279#ifdef __cplusplus
280}
281#endif
282
Hanno Becker616d1ca2018-01-24 10:25:05 +0000283#endif /* MBEDTLS_SELF_TEST */
284
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +0200285#endif /* ecjpake.h */