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