blob: 8d624d4391eb62b58d030b23abb75505a5d2aa33 [file] [log] [blame]
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +02001/**
2 * \file ecjpake.h
3 *
4 * \brief Elliptic curve J-PAKE
5 *
6 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
7 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 *
21 * This file is part of mbed TLS (https://tls.mbed.org)
22 */
23#ifndef MBEDTLS_ECJPAKE_H
24#define MBEDTLS_ECJPAKE_H
25
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020026/*
27 * Implementation based on Chapter 7.4 of the Thread v1.0 Specification,
28 * available from the Thread Group http://threadgroup.org/
29 *
30 * This file implements the EC J-PAKE algorithm, with payload serializations
31 * suitable for use in TLS, but the result could be used outside TLS.
32 */
33
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +020034#include "ecp.h"
35#include "md.h"
36
37#ifdef __cplusplus
38extern "C" {
39#endif
40
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020041/**
42 * Roles in the EC J-PAKE exchange
43 */
Manuel Pégourié-Gonnard64493912015-08-13 20:19:51 +020044typedef enum {
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020045 MBEDTLS_ECJPAKE_CLIENT = 0, /**< Client */
46 MBEDTLS_ECJPAKE_SERVER, /**< Server */
Manuel Pégourié-Gonnard64493912015-08-13 20:19:51 +020047} mbedtls_ecjpake_role;
48
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020049/**
Manuel Pégourié-Gonnardce456762015-08-14 11:54:35 +020050 * EC J-PAKE context structure.
51 *
52 * J-PAKE is a symmetric protocol, except for the identifiers used in
53 * Zero-Knowledge Proofs, and the serialization of the second message
54 * (KeyExchange) as defined by the Thread spec.
55 *
56 * In order to benefit from this symmetry, we choose a different naming
57 * convetion from the Thread v1.0 spec. Correspondance is indicated in the
58 * description as a pair C: <client name>, S: <server name>
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020059 */
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020060typedef struct
61{
62 const mbedtls_md_info_t *md_info; /**< Hash to use */
63 mbedtls_ecp_group grp; /**< Elliptic curve */
Manuel Pégourié-Gonnard64493912015-08-13 20:19:51 +020064 mbedtls_ecjpake_role role; /**< Are we client or server? */
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020065
Manuel Pégourié-Gonnardce456762015-08-14 11:54:35 +020066 mbedtls_ecp_point Xm1; /**< My public key 1 C: X1, S: X3 */
67 mbedtls_ecp_point Xm2; /**< My public key 2 C: X2, S: X4 */
68 mbedtls_ecp_point Xp1; /**< Peer public key 1 C: X3, S: X1 */
69 mbedtls_ecp_point Xp2; /**< Peer public key 2 C: X4, S: X2 */
70 mbedtls_ecp_point Xp; /**< Peer public key C: Xs, S: Xc */
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020071
Manuel Pégourié-Gonnardce456762015-08-14 11:54:35 +020072 mbedtls_mpi xm1; /**< My private key 1 C: x1, S: x3 */
73 mbedtls_mpi xm2; /**< My private key 2 C: x2, S: x4 */
Manuel Pégourié-Gonnard23dcbe32015-08-13 09:37:00 +020074
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020075 mbedtls_mpi s; /**< Pre-shared secret (passphrase) */
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020076} mbedtls_ecjpake_context;
77
78/*
79 * \brief Initialize a context
80 * (just makes it ready for setup() or free()).
81 *
82 * \param ctx context to initialize
83 */
84void mbedtls_ecjpake_init( mbedtls_ecjpake_context *ctx );
85
86/*
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020087 * \brief Set up a context for use
88 *
89 * \note Currently the only values for hash/curve allowed by the
90 * standard are MBEDTLS_MD_SHA256/MBEDTLS_ECP_DP_SECP256R1.
91 *
92 * \param ctx context to set up
Manuel Pégourié-Gonnard64493912015-08-13 20:19:51 +020093 * \param role Our role: client or server
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020094 * \param hash hash function to use (MBEDTLS_MD_XXX)
95 * \param curve elliptic curve identifier (MBEDTLS_ECP_DP_XXX)
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020096 * \param secret pre-shared secret (passphrase)
Manuel Pégourié-Gonnard23dcbe32015-08-13 09:37:00 +020097 * \param len length of the shared secret
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020098 *
99 * \return 0 if successfull,
100 * a negative error code otherwise
101 */
102int mbedtls_ecjpake_setup( mbedtls_ecjpake_context *ctx,
Manuel Pégourié-Gonnard64493912015-08-13 20:19:51 +0200103 mbedtls_ecjpake_role role,
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200104 mbedtls_md_type_t hash,
Manuel Pégourié-Gonnard23dcbe32015-08-13 09:37:00 +0200105 mbedtls_ecp_group_id curve,
106 const unsigned char *secret,
107 size_t len );
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200108
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200109/*
110 * \brief Generate and write contents of ClientHello extension
111 * (excluding extension type and length bytes)
112 *
113 * \param ctx Context to use
114 * \param buf Buffer to write the contents to
115 * \param len Buffer size
116 * \param olen Will be updated with the number of bytes written
117 * \param f_rng RNG function
118 * \param p_rng RNG parameter
119 *
120 * \return 0 if successfull,
121 * a negative error code otherwise
122 */
123int mbedtls_ecjpake_tls_write_client_ext( mbedtls_ecjpake_context *ctx,
124 unsigned char *buf, size_t len, size_t *olen,
125 int (*f_rng)(void *, unsigned char *, size_t),
126 void *p_rng );
127/*
128 * \brief Read and process contents of the ClientHello extension
129 * (excluding extension type and length bytes)
130 *
131 * \param ctx Context to use
132 * \param buf Pointer to extension contents
133 * \param len Extension length
134 *
135 * \return 0 if successfull,
136 * a negative error code otherwise
137 */
138int mbedtls_ecjpake_tls_read_client_ext( mbedtls_ecjpake_context *ctx,
139 const unsigned char *buf,
140 size_t len );
141
142/*
143 * \brief Generate and write contents of ServerHello extension
144 * (excluding extension type and length bytes)
145 *
146 * \param ctx Context to use
147 * \param buf Buffer to write the contents to
148 * \param len Buffer size
149 * \param olen Will be updated with the number of bytes written
150 * \param f_rng RNG function
151 * \param p_rng RNG parameter
152 *
153 * \return 0 if successfull,
154 * a negative error code otherwise
155 */
156int mbedtls_ecjpake_tls_write_server_ext( mbedtls_ecjpake_context *ctx,
157 unsigned char *buf, size_t len, size_t *olen,
158 int (*f_rng)(void *, unsigned char *, size_t),
159 void *p_rng );
Manuel Pégourié-Gonnardcb7cd032015-08-13 10:09:10 +0200160
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200161/*
162 * \brief Read and process contents of the ServerHello extension
163 * (excluding extension type and length bytes)
164 *
165 * \param ctx Context to use
166 * \param buf Pointer to extension contents
167 * \param len Extension length
168 *
169 * \return 0 if successfull,
170 * a negative error code otherwise
171 */
172int mbedtls_ecjpake_tls_read_server_ext( mbedtls_ecjpake_context *ctx,
173 const unsigned char *buf,
174 size_t len );
175
176/*
Manuel Pégourié-Gonnardcb7cd032015-08-13 10:09:10 +0200177 * \brief Generate and write ServerECJPAKEParams
178 * (the contents for the ServerKeyExchange)
179 *
180 * \param ctx Context to use
181 * \param buf Buffer to write the contents to
182 * \param len Buffer size
183 * \param olen Will be updated with the number of bytes written
184 * \param f_rng RNG function
185 * \param p_rng RNG parameter
186 *
187 * \return 0 if successfull,
188 * a negative error code otherwise
189 */
190int mbedtls_ecjpake_tls_write_server_params( mbedtls_ecjpake_context *ctx,
191 unsigned char *buf, size_t len, size_t *olen,
192 int (*f_rng)(void *, unsigned char *, size_t),
193 void *p_rng );
194
195/*
196 * \brief Read and process ServerECJPAKEParams
197 * (the contents for the ServerKeyExchange)
198 *
199 * \param ctx Context to use
200 * \param buf Pointer to the message
201 * \param len Message length
202 *
203 * \return 0 if successfull,
204 * a negative error code otherwise
205 */
206int mbedtls_ecjpake_tls_read_server_params( mbedtls_ecjpake_context *ctx,
207 const unsigned char *buf,
208 size_t len );
209
210/*
Manuel Pégourié-Gonnard614bd5e2015-08-13 20:19:16 +0200211 * \brief Generate and write ClientECJPAKEParams
212 * (the contents for the ClientKeyExchange)
213 *
214 * \param ctx Context to use
215 * \param buf Buffer to write the contents to
216 * \param len Buffer size
217 * \param olen Will be updated with the number of bytes written
218 * \param f_rng RNG function
219 * \param p_rng RNG parameter
220 *
221 * \return 0 if successfull,
222 * a negative error code otherwise
223 */
224int mbedtls_ecjpake_tls_write_client_params( mbedtls_ecjpake_context *ctx,
225 unsigned char *buf, size_t len, size_t *olen,
226 int (*f_rng)(void *, unsigned char *, size_t),
227 void *p_rng );
228
229/*
Manuel Pégourié-Gonnardec0eece2015-08-13 19:13:20 +0200230 * \brief Read and process ClientECJPAKEParams
231 * (the contents for the ClientKeyExchange)
232 *
233 * \param ctx Context to use
234 * \param buf Pointer to the message
235 * \param len Message length
236 *
237 * \return 0 if successfull,
238 * a negative error code otherwise
239 */
240int mbedtls_ecjpake_tls_read_client_params( mbedtls_ecjpake_context *ctx,
241 const unsigned char *buf,
242 size_t len );
243
244/*
Manuel Pégourié-Gonnard5f188292015-08-14 10:52:39 +0200245 * \brief Derive the Pre-Master Secret used by TLS
246 *
247 * \param ctx
248 * \param buf Buffer to write the contents to
249 * \param len Buffer size
250 * \param olen Will be updated with the number of bytes written
251 * \param f_rng RNG function
252 * \param p_rng RNG parameter
253 *
254 * \return 0 if successfull,
255 * a negative error code otherwise
256 */
257int mbedtls_ecjpake_tls_derive_pms( mbedtls_ecjpake_context *ctx,
258 unsigned char *buf, size_t len, size_t *olen,
259 int (*f_rng)(void *, unsigned char *, size_t),
260 void *p_rng );
261
262/*
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200263 * \brief Free a context's content
264 *
265 * \param ctx context to free
266 */
267void mbedtls_ecjpake_free( mbedtls_ecjpake_context *ctx );
268
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +0200269#if defined(MBEDTLS_SELF_TEST)
270/**
271 * \brief Checkup routine
272 *
273 * \return 0 if successful, or 1 if a test failed
274 */
275int mbedtls_ecjpake_self_test( int verbose );
276#endif
277
278#ifdef __cplusplus
279}
280#endif
281
282#endif /* ecjpake.h */