blob: 6fcffc777a1c22abb6bbbdfd51998dac1c78aa3e [file] [log] [blame]
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +02001/**
2 * \file ecjpake.h
3 *
4 * \brief Elliptic curve J-PAKE
Darryl Greena40a1012018-01-05 15:33:17 +00005 */
6/*
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +02007 * 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
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020027/*
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +020028 * 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 *
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +020033 * 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/
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +020036 *
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.
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +020040 * The payloads are serialized in a way suitable for use in TLS, but could
41 * also be use outside TLS.
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020042 */
43
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +020044#include "ecp.h"
45#include "md.h"
46
47#ifdef __cplusplus
48extern "C" {
49#endif
50
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020051/**
52 * Roles in the EC J-PAKE exchange
53 */
Manuel Pégourié-Gonnard64493912015-08-13 20:19:51 +020054typedef enum {
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020055 MBEDTLS_ECJPAKE_CLIENT = 0, /**< Client */
56 MBEDTLS_ECJPAKE_SERVER, /**< Server */
Manuel Pégourié-Gonnard64493912015-08-13 20:19:51 +020057} mbedtls_ecjpake_role;
58
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020059/**
Manuel Pégourié-Gonnardce456762015-08-14 11:54:35 +020060 * EC J-PAKE context structure.
61 *
62 * J-PAKE is a symmetric protocol, except for the identifiers used in
63 * Zero-Knowledge Proofs, and the serialization of the second message
64 * (KeyExchange) as defined by the Thread spec.
65 *
66 * In order to benefit from this symmetry, we choose a different naming
67 * convetion from the Thread v1.0 spec. Correspondance is indicated in the
Simon Butcher5b331b92016-01-03 16:14:14 +000068 * description as a pair C: client name, S: server name
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020069 */
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020070typedef struct
71{
72 const mbedtls_md_info_t *md_info; /**< Hash to use */
73 mbedtls_ecp_group grp; /**< Elliptic curve */
Manuel Pégourié-Gonnard64493912015-08-13 20:19:51 +020074 mbedtls_ecjpake_role role; /**< Are we client or server? */
Robert Cragie7cdad772015-10-02 13:31:41 +010075 int point_format; /**< Format for point export */
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020076
Manuel Pégourié-Gonnardce456762015-08-14 11:54:35 +020077 mbedtls_ecp_point Xm1; /**< My public key 1 C: X1, S: X3 */
78 mbedtls_ecp_point Xm2; /**< My public key 2 C: X2, S: X4 */
79 mbedtls_ecp_point Xp1; /**< Peer public key 1 C: X3, S: X1 */
80 mbedtls_ecp_point Xp2; /**< Peer public key 2 C: X4, S: X2 */
81 mbedtls_ecp_point Xp; /**< Peer public key C: Xs, S: Xc */
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020082
Manuel Pégourié-Gonnardce456762015-08-14 11:54:35 +020083 mbedtls_mpi xm1; /**< My private key 1 C: x1, S: x3 */
84 mbedtls_mpi xm2; /**< My private key 2 C: x2, S: x4 */
Manuel Pégourié-Gonnard23dcbe32015-08-13 09:37:00 +020085
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020086 mbedtls_mpi s; /**< Pre-shared secret (passphrase) */
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020087} mbedtls_ecjpake_context;
88
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +020089/**
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020090 * \brief Initialize a context
91 * (just makes it ready for setup() or free()).
92 *
93 * \param ctx context to initialize
94 */
95void mbedtls_ecjpake_init( mbedtls_ecjpake_context *ctx );
96
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +020097/**
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020098 * \brief Set up a context for use
99 *
100 * \note Currently the only values for hash/curve allowed by the
101 * standard are MBEDTLS_MD_SHA256/MBEDTLS_ECP_DP_SECP256R1.
102 *
103 * \param ctx context to set up
Manuel Pégourié-Gonnard64493912015-08-13 20:19:51 +0200104 * \param role Our role: client or server
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200105 * \param hash hash function to use (MBEDTLS_MD_XXX)
106 * \param curve elliptic curve identifier (MBEDTLS_ECP_DP_XXX)
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +0200107 * \param secret pre-shared secret (passphrase)
Manuel Pégourié-Gonnard23dcbe32015-08-13 09:37:00 +0200108 * \param len length of the shared secret
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200109 *
110 * \return 0 if successfull,
111 * a negative error code otherwise
112 */
113int mbedtls_ecjpake_setup( mbedtls_ecjpake_context *ctx,
Manuel Pégourié-Gonnard64493912015-08-13 20:19:51 +0200114 mbedtls_ecjpake_role role,
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200115 mbedtls_md_type_t hash,
Manuel Pégourié-Gonnard23dcbe32015-08-13 09:37:00 +0200116 mbedtls_ecp_group_id curve,
117 const unsigned char *secret,
118 size_t len );
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200119
Andres Amaya Garciaaf610a02016-12-14 10:13:43 +0000120/**
Manuel Pégourié-Gonnardb813acc2015-09-15 15:34:09 +0200121 * \brief Check if a context is ready for use
122 *
123 * \param ctx Context to check
124 *
125 * \return 0 if the context is ready for use,
126 * MBEDTLS_ERR_ECP_BAD_INPUT_DATA otherwise
127 */
128int mbedtls_ecjpake_check( const mbedtls_ecjpake_context *ctx );
129
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200130/**
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +0200131 * \brief Generate and write the first round message
132 * (TLS: contents of the Client/ServerHello extension,
133 * excluding extension type and length bytes)
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200134 *
135 * \param ctx Context to use
136 * \param buf Buffer to write the contents to
137 * \param len Buffer size
138 * \param olen Will be updated with the number of bytes written
139 * \param f_rng RNG function
140 * \param p_rng RNG parameter
141 *
142 * \return 0 if successfull,
143 * a negative error code otherwise
144 */
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +0200145int mbedtls_ecjpake_write_round_one( mbedtls_ecjpake_context *ctx,
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200146 unsigned char *buf, size_t len, size_t *olen,
147 int (*f_rng)(void *, unsigned char *, size_t),
148 void *p_rng );
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200149
150/**
151 * \brief Read and process the first round message
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +0200152 * (TLS: contents of the Client/ServerHello extension,
153 * excluding extension type and length bytes)
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200154 *
155 * \param ctx Context to use
156 * \param buf Pointer to extension contents
157 * \param len Extension length
158 *
159 * \return 0 if successfull,
160 * a negative error code otherwise
161 */
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +0200162int mbedtls_ecjpake_read_round_one( mbedtls_ecjpake_context *ctx,
163 const unsigned char *buf,
164 size_t len );
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200165
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200166/**
167 * \brief Generate and write the second round message
168 * (TLS: contents of the Client/ServerKeyExchange)
Manuel Pégourié-Gonnard614bd5e2015-08-13 20:19:16 +0200169 *
170 * \param ctx Context to use
171 * \param buf Buffer to write the contents to
172 * \param len Buffer size
173 * \param olen Will be updated with the number of bytes written
174 * \param f_rng RNG function
175 * \param p_rng RNG parameter
176 *
177 * \return 0 if successfull,
178 * a negative error code otherwise
179 */
Manuel Pégourié-Gonnarde1927102015-08-14 14:20:48 +0200180int mbedtls_ecjpake_write_round_two( mbedtls_ecjpake_context *ctx,
Manuel Pégourié-Gonnard614bd5e2015-08-13 20:19:16 +0200181 unsigned char *buf, size_t len, size_t *olen,
182 int (*f_rng)(void *, unsigned char *, size_t),
183 void *p_rng );
184
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200185/**
186 * \brief Read and process the second round message
187 * (TLS: contents of the Client/ServerKeyExchange)
Manuel Pégourié-Gonnardec0eece2015-08-13 19:13:20 +0200188 *
189 * \param ctx Context to use
190 * \param buf Pointer to the message
191 * \param len Message length
192 *
193 * \return 0 if successfull,
194 * a negative error code otherwise
195 */
Manuel Pégourié-Gonnarde1927102015-08-14 14:20:48 +0200196int mbedtls_ecjpake_read_round_two( mbedtls_ecjpake_context *ctx,
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200197 const unsigned char *buf,
198 size_t len );
Manuel Pégourié-Gonnardec0eece2015-08-13 19:13:20 +0200199
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200200/**
201 * \brief Derive the shared secret
202 * (TLS: Pre-Master Secret)
Manuel Pégourié-Gonnard5f188292015-08-14 10:52:39 +0200203 *
Manuel Pégourié-Gonnard55f3d842015-08-14 15:08:43 +0200204 * \param ctx Context to use
Manuel Pégourié-Gonnard5f188292015-08-14 10:52:39 +0200205 * \param buf Buffer to write the contents to
206 * \param len Buffer size
207 * \param olen Will be updated with the number of bytes written
208 * \param f_rng RNG function
209 * \param p_rng RNG parameter
210 *
211 * \return 0 if successfull,
212 * a negative error code otherwise
213 */
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200214int mbedtls_ecjpake_derive_secret( mbedtls_ecjpake_context *ctx,
Manuel Pégourié-Gonnard5f188292015-08-14 10:52:39 +0200215 unsigned char *buf, size_t len, size_t *olen,
216 int (*f_rng)(void *, unsigned char *, size_t),
217 void *p_rng );
218
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200219/**
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200220 * \brief Free a context's content
221 *
222 * \param ctx context to free
223 */
224void mbedtls_ecjpake_free( mbedtls_ecjpake_context *ctx );
225
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +0200226#if defined(MBEDTLS_SELF_TEST)
227/**
228 * \brief Checkup routine
229 *
230 * \return 0 if successful, or 1 if a test failed
231 */
232int mbedtls_ecjpake_self_test( int verbose );
233#endif
234
235#ifdef __cplusplus
236}
237#endif
238
239#endif /* ecjpake.h */