blob: 2207ad9f659a6d35e11625a8b07eeb36e3653037 [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
26#include "ecp.h"
27#include "md.h"
28
29#ifdef __cplusplus
30extern "C" {
31#endif
32
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020033typedef struct
34{
35 const mbedtls_md_info_t *md_info; /**< Hash to use */
36 mbedtls_ecp_group grp; /**< Elliptic curve */
37
38 mbedtls_ecp_point X1; /**< Public key one */
39 mbedtls_ecp_point X2; /**< Public key two */
40 mbedtls_ecp_point X3; /**< Public key three */
41 mbedtls_ecp_point X4; /**< Public key four */
Manuel Pégourié-Gonnardcb7cd032015-08-13 10:09:10 +020042 mbedtls_ecp_point Xp; /**< Peer's public key (Xs or Xc) */
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020043
44 mbedtls_mpi xa; /**< Our first secret (x1 or x3) */
45 mbedtls_mpi xb; /**< Our second secret (x2 or x4) */
Manuel Pégourié-Gonnard23dcbe32015-08-13 09:37:00 +020046
47 mbedtls_mpi s; /**< Pre-shared secret */
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020048} mbedtls_ecjpake_context;
49
50/*
51 * \brief Initialize a context
52 * (just makes it ready for setup() or free()).
53 *
54 * \param ctx context to initialize
55 */
56void mbedtls_ecjpake_init( mbedtls_ecjpake_context *ctx );
57
58/*
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020059 * \brief Set up a context for use
60 *
61 * \note Currently the only values for hash/curve allowed by the
62 * standard are MBEDTLS_MD_SHA256/MBEDTLS_ECP_DP_SECP256R1.
63 *
64 * \param ctx context to set up
65 * \param hash hash function to use (MBEDTLS_MD_XXX)
66 * \param curve elliptic curve identifier (MBEDTLS_ECP_DP_XXX)
Manuel Pégourié-Gonnard23dcbe32015-08-13 09:37:00 +020067 * \param secret shared secret
68 * \param len length of the shared secret
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020069 *
70 * \return 0 if successfull,
71 * a negative error code otherwise
72 */
73int mbedtls_ecjpake_setup( mbedtls_ecjpake_context *ctx,
74 mbedtls_md_type_t hash,
Manuel Pégourié-Gonnard23dcbe32015-08-13 09:37:00 +020075 mbedtls_ecp_group_id curve,
76 const unsigned char *secret,
77 size_t len );
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020078
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +020079/*
80 * \brief Generate and write contents of ClientHello extension
81 * (excluding extension type and length bytes)
82 *
83 * \param ctx Context to use
84 * \param buf Buffer to write the contents to
85 * \param len Buffer size
86 * \param olen Will be updated with the number of bytes written
87 * \param f_rng RNG function
88 * \param p_rng RNG parameter
89 *
90 * \return 0 if successfull,
91 * a negative error code otherwise
92 */
93int mbedtls_ecjpake_tls_write_client_ext( mbedtls_ecjpake_context *ctx,
94 unsigned char *buf, size_t len, size_t *olen,
95 int (*f_rng)(void *, unsigned char *, size_t),
96 void *p_rng );
97/*
98 * \brief Read and process contents of the ClientHello extension
99 * (excluding extension type and length bytes)
100 *
101 * \param ctx Context to use
102 * \param buf Pointer to extension contents
103 * \param len Extension length
104 *
105 * \return 0 if successfull,
106 * a negative error code otherwise
107 */
108int mbedtls_ecjpake_tls_read_client_ext( mbedtls_ecjpake_context *ctx,
109 const unsigned char *buf,
110 size_t len );
111
112/*
113 * \brief Generate and write contents of ServerHello extension
114 * (excluding extension type and length bytes)
115 *
116 * \param ctx Context to use
117 * \param buf Buffer to write the contents to
118 * \param len Buffer size
119 * \param olen Will be updated with the number of bytes written
120 * \param f_rng RNG function
121 * \param p_rng RNG parameter
122 *
123 * \return 0 if successfull,
124 * a negative error code otherwise
125 */
126int mbedtls_ecjpake_tls_write_server_ext( mbedtls_ecjpake_context *ctx,
127 unsigned char *buf, size_t len, size_t *olen,
128 int (*f_rng)(void *, unsigned char *, size_t),
129 void *p_rng );
Manuel Pégourié-Gonnardcb7cd032015-08-13 10:09:10 +0200130
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200131/*
132 * \brief Read and process contents of the ServerHello extension
133 * (excluding extension type and length bytes)
134 *
135 * \param ctx Context to use
136 * \param buf Pointer to extension contents
137 * \param len Extension length
138 *
139 * \return 0 if successfull,
140 * a negative error code otherwise
141 */
142int mbedtls_ecjpake_tls_read_server_ext( mbedtls_ecjpake_context *ctx,
143 const unsigned char *buf,
144 size_t len );
145
146/*
Manuel Pégourié-Gonnardcb7cd032015-08-13 10:09:10 +0200147 * \brief Generate and write ServerECJPAKEParams
148 * (the contents for the ServerKeyExchange)
149 *
150 * \param ctx Context to use
151 * \param buf Buffer to write the contents to
152 * \param len Buffer size
153 * \param olen Will be updated with the number of bytes written
154 * \param f_rng RNG function
155 * \param p_rng RNG parameter
156 *
157 * \return 0 if successfull,
158 * a negative error code otherwise
159 */
160int mbedtls_ecjpake_tls_write_server_params( 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 ServerECJPAKEParams
167 * (the contents for the ServerKeyExchange)
168 *
169 * \param ctx Context to use
170 * \param buf Pointer to the message
171 * \param len Message length
172 *
173 * \return 0 if successfull,
174 * a negative error code otherwise
175 */
176int mbedtls_ecjpake_tls_read_server_params( mbedtls_ecjpake_context *ctx,
177 const unsigned char *buf,
178 size_t len );
179
180/*
Manuel Pégourié-Gonnard614bd5e2015-08-13 20:19:16 +0200181 * \brief Generate and write ClientECJPAKEParams
182 * (the contents for the ClientKeyExchange)
183 *
184 * \param ctx Context to use
185 * \param buf Buffer to write the contents to
186 * \param len Buffer size
187 * \param olen Will be updated with the number of bytes written
188 * \param f_rng RNG function
189 * \param p_rng RNG parameter
190 *
191 * \return 0 if successfull,
192 * a negative error code otherwise
193 */
194int mbedtls_ecjpake_tls_write_client_params( mbedtls_ecjpake_context *ctx,
195 unsigned char *buf, size_t len, size_t *olen,
196 int (*f_rng)(void *, unsigned char *, size_t),
197 void *p_rng );
198
199/*
Manuel Pégourié-Gonnardec0eece2015-08-13 19:13:20 +0200200 * \brief Read and process ClientECJPAKEParams
201 * (the contents for the ClientKeyExchange)
202 *
203 * \param ctx Context to use
204 * \param buf Pointer to the message
205 * \param len Message length
206 *
207 * \return 0 if successfull,
208 * a negative error code otherwise
209 */
210int mbedtls_ecjpake_tls_read_client_params( mbedtls_ecjpake_context *ctx,
211 const unsigned char *buf,
212 size_t len );
213
214/*
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200215 * \brief Free a context's content
216 *
217 * \param ctx context to free
218 */
219void mbedtls_ecjpake_free( mbedtls_ecjpake_context *ctx );
220
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +0200221#if defined(MBEDTLS_SELF_TEST)
222/**
223 * \brief Checkup routine
224 *
225 * \return 0 if successful, or 1 if a test failed
226 */
227int mbedtls_ecjpake_self_test( int verbose );
228#endif
229
230#ifdef __cplusplus
231}
232#endif
233
234#endif /* ecjpake.h */