Mbed OS Reference
Loading...
Searching...
No Matches
ecjpake.h
Go to the documentation of this file.
1/**
2 * \file ecjpake.h
3 *
4 * \brief Elliptic curve J-PAKE
5 */
6/*
7 * Copyright The Mbed TLS Contributors
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#ifndef MBEDTLS_ECJPAKE_H
23#define MBEDTLS_ECJPAKE_H
24
25/*
26 * 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 *
31 * 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/
34 *
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.
38 * The payloads are serialized in a way suitable for use in TLS, but could
39 * also be use outside TLS.
40 */
41#if !defined(MBEDTLS_CONFIG_FILE)
42#include "mbedtls/config.h"
43#else
44#include MBEDTLS_CONFIG_FILE
45#endif
46
47#include "mbedtls/ecp.h"
48#include "mbedtls/md.h"
49
50#ifdef __cplusplus
51extern "C" {
52#endif
53
54/**
55* \addtogroup mbedtls
56* \{
57* \defgroup mbedtls_ecjpake_module Elliptic Curve J-PAKE
58* \{
59*/
60
61/**
62 * Roles in the EC J-PAKE exchange
63 */
64typedef enum {
65 MBEDTLS_ECJPAKE_CLIENT = 0, /**< Client */
66 MBEDTLS_ECJPAKE_SERVER, /**< Server */
68
69#if !defined(MBEDTLS_ECJPAKE_ALT)
70/**
71 * EC J-PAKE context structure.
72 *
73 * J-PAKE is a symmetric protocol, except for the identifiers used in
74 * Zero-Knowledge Proofs, and the serialization of the second message
75 * (KeyExchange) as defined by the Thread spec.
76 *
77 * In order to benefit from this symmetry, we choose a different naming
78 * convetion from the Thread v1.0 spec. Correspondance is indicated in the
79 * description as a pair C: client name, S: server name
80 */
82{
83 const mbedtls_md_info_t *md_info; /**< Hash to use */
84 mbedtls_ecp_group grp; /**< Elliptic curve */
85 mbedtls_ecjpake_role role; /**< Are we client or server? */
86 int point_format; /**< Format for point export */
87
88 mbedtls_ecp_point Xm1; /**< My public key 1 C: X1, S: X3 */
89 mbedtls_ecp_point Xm2; /**< My public key 2 C: X2, S: X4 */
90 mbedtls_ecp_point Xp1; /**< Peer public key 1 C: X3, S: X1 */
91 mbedtls_ecp_point Xp2; /**< Peer public key 2 C: X4, S: X2 */
92 mbedtls_ecp_point Xp; /**< Peer public key C: Xs, S: Xc */
93
94 mbedtls_mpi xm1; /**< My private key 1 C: x1, S: x3 */
95 mbedtls_mpi xm2; /**< My private key 2 C: x2, S: x4 */
96
97 mbedtls_mpi s; /**< Pre-shared secret (passphrase) */
99
100#else /* MBEDTLS_ECJPAKE_ALT */
101#include "ecjpake_alt.h"
102#endif /* MBEDTLS_ECJPAKE_ALT */
103
104/**
105 * \brief Initialize an ECJPAKE context.
106 *
107 * \param ctx The ECJPAKE context to initialize.
108 * This must not be \c NULL.
109 */
111
112/**
113 * \brief Set up an ECJPAKE context for use.
114 *
115 * \note Currently the only values for hash/curve allowed by the
116 * standard are #MBEDTLS_MD_SHA256/#MBEDTLS_ECP_DP_SECP256R1.
117 *
118 * \param ctx The ECJPAKE context to set up. This must be initialized.
119 * \param role The role of the caller. This must be either
120 * #MBEDTLS_ECJPAKE_CLIENT or #MBEDTLS_ECJPAKE_SERVER.
121 * \param hash The identifier of the hash function to use,
122 * for example #MBEDTLS_MD_SHA256.
123 * \param curve The identifier of the elliptic curve to use,
124 * for example #MBEDTLS_ECP_DP_SECP256R1.
125 * \param secret The pre-shared secret (passphrase). This must be
126 * a readable buffer of length \p len Bytes. It need
127 * only be valid for the duration of this call.
128 * \param len The length of the pre-shared secret \p secret.
129 *
130 * \return \c 0 if successful.
131 * \return A negative error code on failure.
132 */
137 const unsigned char *secret,
138 size_t len );
139
140/**
141 * \brief Check if an ECJPAKE context is ready for use.
142 *
143 * \param ctx The ECJPAKE context to check. This must be
144 * initialized.
145 *
146 * \return \c 0 if the context is ready for use.
147 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA otherwise.
148 */
150
151/**
152 * \brief Generate and write the first round message
153 * (TLS: contents of the Client/ServerHello extension,
154 * excluding extension type and length bytes).
155 *
156 * \param ctx The ECJPAKE context to use. This must be
157 * initialized and set up.
158 * \param buf The buffer to write the contents to. This must be a
159 * writable buffer of length \p len Bytes.
160 * \param len The length of \p buf in Bytes.
161 * \param olen The address at which to store the total number
162 * of Bytes written to \p buf. This must not be \c NULL.
163 * \param f_rng The RNG function to use. This must not be \c NULL.
164 * \param p_rng The RNG parameter to be passed to \p f_rng. This
165 * may be \c NULL if \p f_rng doesn't use a context.
166 *
167 * \return \c 0 if successful.
168 * \return A negative error code on failure.
169 */
171 unsigned char *buf, size_t len, size_t *olen,
172 int (*f_rng)(void *, unsigned char *, size_t),
173 void *p_rng );
174
175/**
176 * \brief Read and process the first round message
177 * (TLS: contents of the Client/ServerHello extension,
178 * excluding extension type and length bytes).
179 *
180 * \param ctx The ECJPAKE context to use. This must be initialized
181 * and set up.
182 * \param buf The buffer holding the first round message. This must
183 * be a readable buffer of length \p len Bytes.
184 * \param len The length in Bytes of \p buf.
185 *
186 * \return \c 0 if successful.
187 * \return A negative error code on failure.
188 */
190 const unsigned char *buf,
191 size_t len );
192
193/**
194 * \brief Generate and write the second round message
195 * (TLS: contents of the Client/ServerKeyExchange).
196 *
197 * \param ctx The ECJPAKE context to use. This must be initialized,
198 * set up, and already have performed round one.
199 * \param buf The buffer to write the round two contents to.
200 * This must be a writable buffer of length \p len Bytes.
201 * \param len The size of \p buf in Bytes.
202 * \param olen The address at which to store the total number of Bytes
203 * written to \p buf. This must not be \c NULL.
204 * \param f_rng The RNG function to use. This must not be \c NULL.
205 * \param p_rng The RNG parameter to be passed to \p f_rng. This
206 * may be \c NULL if \p f_rng doesn't use a context.
207 *
208 * \return \c 0 if successful.
209 * \return A negative error code on failure.
210 */
212 unsigned char *buf, size_t len, size_t *olen,
213 int (*f_rng)(void *, unsigned char *, size_t),
214 void *p_rng );
215
216/**
217 * \brief Read and process the second round message
218 * (TLS: contents of the Client/ServerKeyExchange).
219 *
220 * \param ctx The ECJPAKE context to use. This must be initialized
221 * and set up and already have performed round one.
222 * \param buf The buffer holding the second round message. This must
223 * be a readable buffer of length \p len Bytes.
224 * \param len The length in Bytes of \p buf.
225 *
226 * \return \c 0 if successful.
227 * \return A negative error code on failure.
228 */
230 const unsigned char *buf,
231 size_t len );
232
233/**
234 * \brief Derive the shared secret
235 * (TLS: Pre-Master Secret).
236 *
237 * \param ctx The ECJPAKE context to use. This must be initialized,
238 * set up and have performed both round one and two.
239 * \param buf The buffer to write the derived secret to. This must
240 * be a writable buffer of length \p len Bytes.
241 * \param len The length of \p buf in Bytes.
242 * \param olen The address at which to store the total number of Bytes
243 * written to \p buf. This must not be \c NULL.
244 * \param f_rng The RNG function to use. This must not be \c NULL.
245 * \param p_rng The RNG parameter to be passed to \p f_rng. This
246 * may be \c NULL if \p f_rng doesn't use a context.
247 *
248 * \return \c 0 if successful.
249 * \return A negative error code on failure.
250 */
252 unsigned char *buf, size_t len, size_t *olen,
253 int (*f_rng)(void *, unsigned char *, size_t),
254 void *p_rng );
255
256/**
257 * \brief This clears an ECJPAKE context and frees any
258 * embedded data structure.
259 *
260 * \param ctx The ECJPAKE context to free. This may be \c NULL,
261 * in which case this function does nothing. If it is not
262 * \c NULL, it must point to an initialized ECJPAKE context.
263 */
265
266#if defined(MBEDTLS_SELF_TEST)
267
268/**
269 * \brief Checkup routine
270 *
271 * \return 0 if successful, or 1 if a test failed
272 */
273int mbedtls_ecjpake_self_test( int verbose );
274
275#endif /* MBEDTLS_SELF_TEST */
276
277/// \}
278/// \}
279
280#ifdef __cplusplus
281}
282#endif
283
284
285#endif /* ecjpake.h */
Configuration options (set of defines)
int mbedtls_ecjpake_read_round_two(mbedtls_ecjpake_context *ctx, const unsigned char *buf, size_t len)
Read and process the second round message (TLS: contents of the Client/ServerKeyExchange).
int mbedtls_ecjpake_write_round_one(mbedtls_ecjpake_context *ctx, unsigned char *buf, size_t len, size_t *olen, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Generate and write the first round message (TLS: contents of the Client/ServerHello extension,...
mbedtls_ecjpake_role
Roles in the EC J-PAKE exchange.
Definition: ecjpake.h:64
int mbedtls_ecjpake_setup(mbedtls_ecjpake_context *ctx, mbedtls_ecjpake_role role, mbedtls_md_type_t hash, mbedtls_ecp_group_id curve, const unsigned char *secret, size_t len)
Set up an ECJPAKE context for use.
void mbedtls_ecjpake_init(mbedtls_ecjpake_context *ctx)
Initialize an ECJPAKE context.
int mbedtls_ecjpake_write_round_two(mbedtls_ecjpake_context *ctx, unsigned char *buf, size_t len, size_t *olen, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Generate and write the second round message (TLS: contents of the Client/ServerKeyExchange).
int mbedtls_ecjpake_derive_secret(mbedtls_ecjpake_context *ctx, unsigned char *buf, size_t len, size_t *olen, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Derive the shared secret (TLS: Pre-Master Secret).
int mbedtls_ecjpake_check(const mbedtls_ecjpake_context *ctx)
Check if an ECJPAKE context is ready for use.
int mbedtls_ecjpake_read_round_one(mbedtls_ecjpake_context *ctx, const unsigned char *buf, size_t len)
Read and process the first round message (TLS: contents of the Client/ServerHello extension,...
void mbedtls_ecjpake_free(mbedtls_ecjpake_context *ctx)
This clears an ECJPAKE context and frees any embedded data structure.
@ MBEDTLS_ECJPAKE_CLIENT
Client
Definition: ecjpake.h:65
@ MBEDTLS_ECJPAKE_SERVER
Server
Definition: ecjpake.h:66
mbedtls_ecp_group_id
Domain-parameter identifiers: curve, subgroup, and generator.
mbedtls_md_type_t
Supported message digests.
Definition: md.h:64
This file provides an API for Elliptic Curves over GF(P) (ECP).
This file contains the generic message-digest wrapper.
EC J-PAKE context structure.
Definition: ecjpake.h:82
mbedtls_ecjpake_role role
Are we client or server?
Definition: ecjpake.h:85
mbedtls_ecp_point Xm2
My public key 2 C: X2, S: X4.
Definition: ecjpake.h:89
const mbedtls_md_info_t * md_info
Hash to use
Definition: ecjpake.h:83
mbedtls_ecp_point Xp2
Peer public key 2 C: X4, S: X2.
Definition: ecjpake.h:91
mbedtls_mpi xm2
My private key 2 C: x2, S: x4.
Definition: ecjpake.h:95
int point_format
Format for point export
Definition: ecjpake.h:86
mbedtls_ecp_point Xp1
Peer public key 1 C: X3, S: X1.
Definition: ecjpake.h:90
mbedtls_mpi xm1
My private key 1 C: x1, S: x3.
Definition: ecjpake.h:94
mbedtls_ecp_point Xp
Peer public key C: Xs, S: Xc.
Definition: ecjpake.h:92
mbedtls_ecp_point Xm1
My public key 1 C: X1, S: X3.
Definition: ecjpake.h:88
mbedtls_ecp_group grp
Elliptic curve
Definition: ecjpake.h:84
mbedtls_mpi s
Pre-shared secret (passphrase)
Definition: ecjpake.h:97
The ECP group structure.
The ECP point structure, in Jacobian coordinates.
Message digest information.
Definition: md_internal.h:46
MPI structure.
Definition: bignum.h:185