blob: 95620c52d63497c93926ed985fc09d1cf4216ebe [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
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 * **********
48 *
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +020049 * This file is part of mbed TLS (https://tls.mbed.org)
50 */
51#ifndef MBEDTLS_ECJPAKE_H
52#define MBEDTLS_ECJPAKE_H
53
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020054/*
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +020055 * J-PAKE is a password-authenticated key exchange that allows deriving a
56 * strong shared secret from a (potentially low entropy) pre-shared
57 * passphrase, with forward secrecy and mutual authentication.
58 * https://en.wikipedia.org/wiki/Password_Authenticated_Key_Exchange_by_Juggling
59 *
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +020060 * This file implements the Elliptic Curve variant of J-PAKE,
61 * as defined in Chapter 7.4 of the Thread v1.0 Specification,
62 * available to members of the Thread Group http://threadgroup.org/
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +020063 *
64 * As the J-PAKE algorithm is inherently symmetric, so is our API.
65 * Each party needs to send its first round message, in any order, to the
66 * other party, then each sends its second round message, in any order.
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +020067 * The payloads are serialized in a way suitable for use in TLS, but could
68 * also be use outside TLS.
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020069 */
Ron Eldor0559c662018-02-14 16:02:41 +020070#if !defined(MBEDTLS_CONFIG_FILE)
71#include "config.h"
72#else
73#include MBEDTLS_CONFIG_FILE
74#endif
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020075
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +020076#include "ecp.h"
77#include "md.h"
78
Hanno Becker616d1ca2018-01-24 10:25:05 +000079#if !defined(MBEDTLS_ECJPAKE_ALT)
80
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +020081#ifdef __cplusplus
82extern "C" {
83#endif
84
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020085/**
86 * Roles in the EC J-PAKE exchange
87 */
Manuel Pégourié-Gonnard64493912015-08-13 20:19:51 +020088typedef enum {
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020089 MBEDTLS_ECJPAKE_CLIENT = 0, /**< Client */
90 MBEDTLS_ECJPAKE_SERVER, /**< Server */
Manuel Pégourié-Gonnard64493912015-08-13 20:19:51 +020091} mbedtls_ecjpake_role;
92
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020093/**
Manuel Pégourié-Gonnardce456762015-08-14 11:54:35 +020094 * EC J-PAKE context structure.
95 *
96 * J-PAKE is a symmetric protocol, except for the identifiers used in
97 * Zero-Knowledge Proofs, and the serialization of the second message
98 * (KeyExchange) as defined by the Thread spec.
99 *
100 * In order to benefit from this symmetry, we choose a different naming
101 * convetion from the Thread v1.0 spec. Correspondance is indicated in the
Simon Butcher5b331b92016-01-03 16:14:14 +0000102 * description as a pair C: client name, S: server name
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +0200103 */
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200104typedef struct
105{
106 const mbedtls_md_info_t *md_info; /**< Hash to use */
107 mbedtls_ecp_group grp; /**< Elliptic curve */
Manuel Pégourié-Gonnard64493912015-08-13 20:19:51 +0200108 mbedtls_ecjpake_role role; /**< Are we client or server? */
Robert Cragie7cdad772015-10-02 13:31:41 +0100109 int point_format; /**< Format for point export */
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200110
Manuel Pégourié-Gonnardce456762015-08-14 11:54:35 +0200111 mbedtls_ecp_point Xm1; /**< My public key 1 C: X1, S: X3 */
112 mbedtls_ecp_point Xm2; /**< My public key 2 C: X2, S: X4 */
113 mbedtls_ecp_point Xp1; /**< Peer public key 1 C: X3, S: X1 */
114 mbedtls_ecp_point Xp2; /**< Peer public key 2 C: X4, S: X2 */
115 mbedtls_ecp_point Xp; /**< Peer public key C: Xs, S: Xc */
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200116
Manuel Pégourié-Gonnardce456762015-08-14 11:54:35 +0200117 mbedtls_mpi xm1; /**< My private key 1 C: x1, S: x3 */
118 mbedtls_mpi xm2; /**< My private key 2 C: x2, S: x4 */
Manuel Pégourié-Gonnard23dcbe32015-08-13 09:37:00 +0200119
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +0200120 mbedtls_mpi s; /**< Pre-shared secret (passphrase) */
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200121} mbedtls_ecjpake_context;
122
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200123/**
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200124 * \brief Initialize a context
125 * (just makes it ready for setup() or free()).
126 *
127 * \param ctx context to initialize
128 */
129void mbedtls_ecjpake_init( mbedtls_ecjpake_context *ctx );
130
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200131/**
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200132 * \brief Set up a context for use
133 *
134 * \note Currently the only values for hash/curve allowed by the
135 * standard are MBEDTLS_MD_SHA256/MBEDTLS_ECP_DP_SECP256R1.
136 *
137 * \param ctx context to set up
Manuel Pégourié-Gonnard64493912015-08-13 20:19:51 +0200138 * \param role Our role: client or server
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200139 * \param hash hash function to use (MBEDTLS_MD_XXX)
140 * \param curve elliptic curve identifier (MBEDTLS_ECP_DP_XXX)
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +0200141 * \param secret pre-shared secret (passphrase)
Manuel Pégourié-Gonnard23dcbe32015-08-13 09:37:00 +0200142 * \param len length of the shared secret
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200143 *
144 * \return 0 if successfull,
145 * a negative error code otherwise
146 */
147int mbedtls_ecjpake_setup( mbedtls_ecjpake_context *ctx,
Manuel Pégourié-Gonnard64493912015-08-13 20:19:51 +0200148 mbedtls_ecjpake_role role,
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200149 mbedtls_md_type_t hash,
Manuel Pégourié-Gonnard23dcbe32015-08-13 09:37:00 +0200150 mbedtls_ecp_group_id curve,
151 const unsigned char *secret,
152 size_t len );
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200153
Andres Amaya Garciaaf610a02016-12-14 10:13:43 +0000154/**
Manuel Pégourié-Gonnardb813acc2015-09-15 15:34:09 +0200155 * \brief Check if a context is ready for use
156 *
157 * \param ctx Context to check
158 *
159 * \return 0 if the context is ready for use,
160 * MBEDTLS_ERR_ECP_BAD_INPUT_DATA otherwise
161 */
162int mbedtls_ecjpake_check( const mbedtls_ecjpake_context *ctx );
163
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200164/**
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +0200165 * \brief Generate and write the first round message
166 * (TLS: contents of the Client/ServerHello extension,
167 * excluding extension type and length bytes)
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200168 *
169 * \param ctx Context to use
170 * \param buf Buffer to write the contents to
171 * \param len Buffer size
172 * \param olen Will be updated with the number of bytes written
173 * \param f_rng RNG function
174 * \param p_rng RNG parameter
175 *
176 * \return 0 if successfull,
177 * a negative error code otherwise
178 */
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +0200179int mbedtls_ecjpake_write_round_one( mbedtls_ecjpake_context *ctx,
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200180 unsigned char *buf, size_t len, size_t *olen,
181 int (*f_rng)(void *, unsigned char *, size_t),
182 void *p_rng );
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200183
184/**
185 * \brief Read and process the first round message
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +0200186 * (TLS: contents of the Client/ServerHello extension,
187 * excluding extension type and length bytes)
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200188 *
189 * \param ctx Context to use
190 * \param buf Pointer to extension contents
191 * \param len Extension length
192 *
193 * \return 0 if successfull,
194 * a negative error code otherwise
195 */
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +0200196int mbedtls_ecjpake_read_round_one( mbedtls_ecjpake_context *ctx,
197 const unsigned char *buf,
198 size_t len );
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200199
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200200/**
201 * \brief Generate and write the second round message
202 * (TLS: contents of the Client/ServerKeyExchange)
Manuel Pégourié-Gonnard614bd5e2015-08-13 20:19:16 +0200203 *
204 * \param ctx Context to use
205 * \param buf Buffer to write the contents to
206 * \param len Buffer size
207 * \param olen Will be updated with the number of bytes written
208 * \param f_rng RNG function
209 * \param p_rng RNG parameter
210 *
211 * \return 0 if successfull,
212 * a negative error code otherwise
213 */
Manuel Pégourié-Gonnarde1927102015-08-14 14:20:48 +0200214int mbedtls_ecjpake_write_round_two( mbedtls_ecjpake_context *ctx,
Manuel Pégourié-Gonnard614bd5e2015-08-13 20:19:16 +0200215 unsigned char *buf, size_t len, size_t *olen,
216 int (*f_rng)(void *, unsigned char *, size_t),
217 void *p_rng );
218
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200219/**
220 * \brief Read and process the second round message
221 * (TLS: contents of the Client/ServerKeyExchange)
Manuel Pégourié-Gonnardec0eece2015-08-13 19:13:20 +0200222 *
223 * \param ctx Context to use
224 * \param buf Pointer to the message
225 * \param len Message length
226 *
227 * \return 0 if successfull,
228 * a negative error code otherwise
229 */
Manuel Pégourié-Gonnarde1927102015-08-14 14:20:48 +0200230int mbedtls_ecjpake_read_round_two( mbedtls_ecjpake_context *ctx,
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200231 const unsigned char *buf,
232 size_t len );
Manuel Pégourié-Gonnardec0eece2015-08-13 19:13:20 +0200233
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200234/**
235 * \brief Derive the shared secret
236 * (TLS: Pre-Master Secret)
Manuel Pégourié-Gonnard5f188292015-08-14 10:52:39 +0200237 *
Manuel Pégourié-Gonnard55f3d842015-08-14 15:08:43 +0200238 * \param ctx Context to use
Manuel Pégourié-Gonnard5f188292015-08-14 10:52:39 +0200239 * \param buf Buffer to write the contents to
240 * \param len Buffer size
241 * \param olen Will be updated with the number of bytes written
242 * \param f_rng RNG function
243 * \param p_rng RNG parameter
244 *
245 * \return 0 if successfull,
246 * a negative error code otherwise
247 */
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200248int mbedtls_ecjpake_derive_secret( mbedtls_ecjpake_context *ctx,
Manuel Pégourié-Gonnard5f188292015-08-14 10:52:39 +0200249 unsigned char *buf, size_t len, size_t *olen,
250 int (*f_rng)(void *, unsigned char *, size_t),
251 void *p_rng );
252
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200253/**
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200254 * \brief Free a context's content
255 *
256 * \param ctx context to free
257 */
258void mbedtls_ecjpake_free( mbedtls_ecjpake_context *ctx );
259
Hanno Becker616d1ca2018-01-24 10:25:05 +0000260#ifdef __cplusplus
261}
262#endif
263
264#else /* MBEDTLS_ECJPAKE_ALT */
265#include "ecjpake_alt.h"
266#endif /* MBEDTLS_ECJPAKE_ALT */
267
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +0200268#if defined(MBEDTLS_SELF_TEST)
Hanno Becker616d1ca2018-01-24 10:25:05 +0000269
270#ifdef __cplusplus
271extern "C" {
272#endif
273
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +0200274/**
275 * \brief Checkup routine
276 *
277 * \return 0 if successful, or 1 if a test failed
278 */
279int mbedtls_ecjpake_self_test( int verbose );
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +0200280
281#ifdef __cplusplus
282}
283#endif
284
Hanno Becker616d1ca2018-01-24 10:25:05 +0000285#endif /* MBEDTLS_SELF_TEST */
286
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +0200287#endif /* ecjpake.h */