blob: e3efe0a2683add46057f364f0474c01391ec1d4f [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/**
50 * EC J-PAKE context structure
51 */
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020052typedef struct
53{
54 const mbedtls_md_info_t *md_info; /**< Hash to use */
55 mbedtls_ecp_group grp; /**< Elliptic curve */
Manuel Pégourié-Gonnard64493912015-08-13 20:19:51 +020056 mbedtls_ecjpake_role role; /**< Are we client or server? */
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020057
58 mbedtls_ecp_point X1; /**< Public key one */
59 mbedtls_ecp_point X2; /**< Public key two */
60 mbedtls_ecp_point X3; /**< Public key three */
61 mbedtls_ecp_point X4; /**< Public key four */
Manuel Pégourié-Gonnardcb7cd032015-08-13 10:09:10 +020062 mbedtls_ecp_point Xp; /**< Peer's public key (Xs or Xc) */
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020063
64 mbedtls_mpi xa; /**< Our first secret (x1 or x3) */
65 mbedtls_mpi xb; /**< Our second secret (x2 or x4) */
Manuel Pégourié-Gonnard23dcbe32015-08-13 09:37:00 +020066
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020067 mbedtls_mpi s; /**< Pre-shared secret (passphrase) */
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020068} mbedtls_ecjpake_context;
69
70/*
71 * \brief Initialize a context
72 * (just makes it ready for setup() or free()).
73 *
74 * \param ctx context to initialize
75 */
76void mbedtls_ecjpake_init( mbedtls_ecjpake_context *ctx );
77
78/*
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020079 * \brief Set up a context for use
80 *
81 * \note Currently the only values for hash/curve allowed by the
82 * standard are MBEDTLS_MD_SHA256/MBEDTLS_ECP_DP_SECP256R1.
83 *
84 * \param ctx context to set up
Manuel Pégourié-Gonnard64493912015-08-13 20:19:51 +020085 * \param role Our role: client or server
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020086 * \param hash hash function to use (MBEDTLS_MD_XXX)
87 * \param curve elliptic curve identifier (MBEDTLS_ECP_DP_XXX)
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020088 * \param secret pre-shared secret (passphrase)
Manuel Pégourié-Gonnard23dcbe32015-08-13 09:37:00 +020089 * \param len length of the shared secret
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020090 *
91 * \return 0 if successfull,
92 * a negative error code otherwise
93 */
94int mbedtls_ecjpake_setup( mbedtls_ecjpake_context *ctx,
Manuel Pégourié-Gonnard64493912015-08-13 20:19:51 +020095 mbedtls_ecjpake_role role,
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020096 mbedtls_md_type_t hash,
Manuel Pégourié-Gonnard23dcbe32015-08-13 09:37:00 +020097 mbedtls_ecp_group_id curve,
98 const unsigned char *secret,
99 size_t len );
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200100
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200101/*
102 * \brief Generate and write contents of ClientHello extension
103 * (excluding extension type and length bytes)
104 *
105 * \param ctx Context to use
106 * \param buf Buffer to write the contents to
107 * \param len Buffer size
108 * \param olen Will be updated with the number of bytes written
109 * \param f_rng RNG function
110 * \param p_rng RNG parameter
111 *
112 * \return 0 if successfull,
113 * a negative error code otherwise
114 */
115int mbedtls_ecjpake_tls_write_client_ext( mbedtls_ecjpake_context *ctx,
116 unsigned char *buf, size_t len, size_t *olen,
117 int (*f_rng)(void *, unsigned char *, size_t),
118 void *p_rng );
119/*
120 * \brief Read and process contents of the ClientHello extension
121 * (excluding extension type and length bytes)
122 *
123 * \param ctx Context to use
124 * \param buf Pointer to extension contents
125 * \param len Extension length
126 *
127 * \return 0 if successfull,
128 * a negative error code otherwise
129 */
130int mbedtls_ecjpake_tls_read_client_ext( mbedtls_ecjpake_context *ctx,
131 const unsigned char *buf,
132 size_t len );
133
134/*
135 * \brief Generate and write contents of ServerHello extension
136 * (excluding extension type and length bytes)
137 *
138 * \param ctx Context to use
139 * \param buf Buffer to write the contents to
140 * \param len Buffer size
141 * \param olen Will be updated with the number of bytes written
142 * \param f_rng RNG function
143 * \param p_rng RNG parameter
144 *
145 * \return 0 if successfull,
146 * a negative error code otherwise
147 */
148int mbedtls_ecjpake_tls_write_server_ext( mbedtls_ecjpake_context *ctx,
149 unsigned char *buf, size_t len, size_t *olen,
150 int (*f_rng)(void *, unsigned char *, size_t),
151 void *p_rng );
Manuel Pégourié-Gonnardcb7cd032015-08-13 10:09:10 +0200152
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200153/*
154 * \brief Read and process contents of the ServerHello extension
155 * (excluding extension type and length bytes)
156 *
157 * \param ctx Context to use
158 * \param buf Pointer to extension contents
159 * \param len Extension length
160 *
161 * \return 0 if successfull,
162 * a negative error code otherwise
163 */
164int mbedtls_ecjpake_tls_read_server_ext( mbedtls_ecjpake_context *ctx,
165 const unsigned char *buf,
166 size_t len );
167
168/*
Manuel Pégourié-Gonnardcb7cd032015-08-13 10:09:10 +0200169 * \brief Generate and write ServerECJPAKEParams
170 * (the contents for the ServerKeyExchange)
171 *
172 * \param ctx Context to use
173 * \param buf Buffer to write the contents to
174 * \param len Buffer size
175 * \param olen Will be updated with the number of bytes written
176 * \param f_rng RNG function
177 * \param p_rng RNG parameter
178 *
179 * \return 0 if successfull,
180 * a negative error code otherwise
181 */
182int mbedtls_ecjpake_tls_write_server_params( mbedtls_ecjpake_context *ctx,
183 unsigned char *buf, size_t len, size_t *olen,
184 int (*f_rng)(void *, unsigned char *, size_t),
185 void *p_rng );
186
187/*
188 * \brief Read and process ServerECJPAKEParams
189 * (the contents for the ServerKeyExchange)
190 *
191 * \param ctx Context to use
192 * \param buf Pointer to the message
193 * \param len Message length
194 *
195 * \return 0 if successfull,
196 * a negative error code otherwise
197 */
198int mbedtls_ecjpake_tls_read_server_params( mbedtls_ecjpake_context *ctx,
199 const unsigned char *buf,
200 size_t len );
201
202/*
Manuel Pégourié-Gonnard614bd5e2015-08-13 20:19:16 +0200203 * \brief Generate and write ClientECJPAKEParams
204 * (the contents for the ClientKeyExchange)
205 *
206 * \param ctx Context to use
207 * \param buf Buffer to write the contents to
208 * \param len Buffer size
209 * \param olen Will be updated with the number of bytes written
210 * \param f_rng RNG function
211 * \param p_rng RNG parameter
212 *
213 * \return 0 if successfull,
214 * a negative error code otherwise
215 */
216int mbedtls_ecjpake_tls_write_client_params( mbedtls_ecjpake_context *ctx,
217 unsigned char *buf, size_t len, size_t *olen,
218 int (*f_rng)(void *, unsigned char *, size_t),
219 void *p_rng );
220
221/*
Manuel Pégourié-Gonnardec0eece2015-08-13 19:13:20 +0200222 * \brief Read and process ClientECJPAKEParams
223 * (the contents for the ClientKeyExchange)
224 *
225 * \param ctx Context to use
226 * \param buf Pointer to the message
227 * \param len Message length
228 *
229 * \return 0 if successfull,
230 * a negative error code otherwise
231 */
232int mbedtls_ecjpake_tls_read_client_params( mbedtls_ecjpake_context *ctx,
233 const unsigned char *buf,
234 size_t len );
235
236/*
Manuel Pégourié-Gonnard5f188292015-08-14 10:52:39 +0200237 * \brief Derive the Pre-Master Secret used by TLS
238 *
239 * \param ctx
240 * \param buf Buffer to write the contents to
241 * \param len Buffer size
242 * \param olen Will be updated with the number of bytes written
243 * \param f_rng RNG function
244 * \param p_rng RNG parameter
245 *
246 * \return 0 if successfull,
247 * a negative error code otherwise
248 */
249int mbedtls_ecjpake_tls_derive_pms( mbedtls_ecjpake_context *ctx,
250 unsigned char *buf, size_t len, size_t *olen,
251 int (*f_rng)(void *, unsigned char *, size_t),
252 void *p_rng );
253
254/*
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200255 * \brief Free a context's content
256 *
257 * \param ctx context to free
258 */
259void mbedtls_ecjpake_free( mbedtls_ecjpake_context *ctx );
260
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +0200261#if defined(MBEDTLS_SELF_TEST)
262/**
263 * \brief Checkup routine
264 *
265 * \return 0 if successful, or 1 if a test failed
266 */
267int mbedtls_ecjpake_self_test( int verbose );
268#endif
269
270#ifdef __cplusplus
271}
272#endif
273
274#endif /* ecjpake.h */