blob: 59e80e50ea1546c9f1769ed42418598a3d8f19d2 [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/*
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +020027 * J-PAKE is a password-authenticated key exchange that allows deriving a
28 * strong shared secret from a (potentially low entropy) pre-shared
29 * passphrase, with forward secrecy and mutual authentication.
30 * https://en.wikipedia.org/wiki/Password_Authenticated_Key_Exchange_by_Juggling
31 *
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +020032 * This file implements the Elliptic Curve variant of J-PAKE,
33 * as defined in Chapter 7.4 of the Thread v1.0 Specification,
34 * available to members of the Thread Group http://threadgroup.org/
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +020035 *
36 * As the J-PAKE algorithm is inherently symmetric, so is our API.
37 * Each party needs to send its first round message, in any order, to the
38 * other party, then each sends its second round message, in any order.
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +020039 * The payloads are serialized in a way suitable for use in TLS, but could
40 * also be use outside TLS.
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020041 */
42
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +020043#include "ecp.h"
44#include "md.h"
45
46#ifdef __cplusplus
47extern "C" {
48#endif
49
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020050/**
51 * Roles in the EC J-PAKE exchange
52 */
Manuel Pégourié-Gonnard64493912015-08-13 20:19:51 +020053typedef enum {
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020054 MBEDTLS_ECJPAKE_CLIENT = 0, /**< Client */
55 MBEDTLS_ECJPAKE_SERVER, /**< Server */
Manuel Pégourié-Gonnard64493912015-08-13 20:19:51 +020056} mbedtls_ecjpake_role;
57
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020058/**
Manuel Pégourié-Gonnardce456762015-08-14 11:54:35 +020059 * EC J-PAKE context structure.
60 *
61 * J-PAKE is a symmetric protocol, except for the identifiers used in
62 * Zero-Knowledge Proofs, and the serialization of the second message
63 * (KeyExchange) as defined by the Thread spec.
64 *
65 * In order to benefit from this symmetry, we choose a different naming
66 * convetion from the Thread v1.0 spec. Correspondance is indicated in the
67 * description as a pair C: <client name>, S: <server name>
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020068 */
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020069typedef struct
70{
71 const mbedtls_md_info_t *md_info; /**< Hash to use */
72 mbedtls_ecp_group grp; /**< Elliptic curve */
Manuel Pégourié-Gonnard64493912015-08-13 20:19:51 +020073 mbedtls_ecjpake_role role; /**< Are we client or server? */
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020074
Manuel Pégourié-Gonnardce456762015-08-14 11:54:35 +020075 mbedtls_ecp_point Xm1; /**< My public key 1 C: X1, S: X3 */
76 mbedtls_ecp_point Xm2; /**< My public key 2 C: X2, S: X4 */
77 mbedtls_ecp_point Xp1; /**< Peer public key 1 C: X3, S: X1 */
78 mbedtls_ecp_point Xp2; /**< Peer public key 2 C: X4, S: X2 */
79 mbedtls_ecp_point Xp; /**< Peer public key C: Xs, S: Xc */
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020080
Manuel Pégourié-Gonnardce456762015-08-14 11:54:35 +020081 mbedtls_mpi xm1; /**< My private key 1 C: x1, S: x3 */
82 mbedtls_mpi xm2; /**< My private key 2 C: x2, S: x4 */
Manuel Pégourié-Gonnard23dcbe32015-08-13 09:37:00 +020083
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +020084 mbedtls_mpi s; /**< Pre-shared secret (passphrase) */
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020085} mbedtls_ecjpake_context;
86
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +020087/**
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020088 * \brief Initialize a context
89 * (just makes it ready for setup() or free()).
90 *
91 * \param ctx context to initialize
92 */
93void mbedtls_ecjpake_init( mbedtls_ecjpake_context *ctx );
94
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +020095/**
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +020096 * \brief Set up a context for use
97 *
98 * \note Currently the only values for hash/curve allowed by the
99 * standard are MBEDTLS_MD_SHA256/MBEDTLS_ECP_DP_SECP256R1.
100 *
101 * \param ctx context to set up
Manuel Pégourié-Gonnard64493912015-08-13 20:19:51 +0200102 * \param role Our role: client or server
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200103 * \param hash hash function to use (MBEDTLS_MD_XXX)
104 * \param curve elliptic curve identifier (MBEDTLS_ECP_DP_XXX)
Manuel Pégourié-Gonnard6b798b92015-08-14 11:18:30 +0200105 * \param secret pre-shared secret (passphrase)
Manuel Pégourié-Gonnard23dcbe32015-08-13 09:37:00 +0200106 * \param len length of the shared secret
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200107 *
108 * \return 0 if successfull,
109 * a negative error code otherwise
110 */
111int mbedtls_ecjpake_setup( mbedtls_ecjpake_context *ctx,
Manuel Pégourié-Gonnard64493912015-08-13 20:19:51 +0200112 mbedtls_ecjpake_role role,
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200113 mbedtls_md_type_t hash,
Manuel Pégourié-Gonnard23dcbe32015-08-13 09:37:00 +0200114 mbedtls_ecp_group_id curve,
115 const unsigned char *secret,
116 size_t len );
Manuel Pégourié-Gonnard7af8bc12015-08-12 16:58:50 +0200117
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200118/**
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +0200119 * \brief Generate and write the first round message
120 * (TLS: contents of the Client/ServerHello extension,
121 * excluding extension type and length bytes)
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200122 *
123 * \param ctx Context to use
124 * \param buf Buffer to write the contents to
125 * \param len Buffer size
126 * \param olen Will be updated with the number of bytes written
127 * \param f_rng RNG function
128 * \param p_rng RNG parameter
129 *
130 * \return 0 if successfull,
131 * a negative error code otherwise
132 */
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +0200133int mbedtls_ecjpake_write_round_one( mbedtls_ecjpake_context *ctx,
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200134 unsigned char *buf, size_t len, size_t *olen,
135 int (*f_rng)(void *, unsigned char *, size_t),
136 void *p_rng );
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200137
138/**
139 * \brief Read and process the first round message
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +0200140 * (TLS: contents of the Client/ServerHello extension,
141 * excluding extension type and length bytes)
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200142 *
143 * \param ctx Context to use
144 * \param buf Pointer to extension contents
145 * \param len Extension length
146 *
147 * \return 0 if successfull,
148 * a negative error code otherwise
149 */
Manuel Pégourié-Gonnardd8204a72015-08-14 13:36:55 +0200150int mbedtls_ecjpake_read_round_one( mbedtls_ecjpake_context *ctx,
151 const unsigned char *buf,
152 size_t len );
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200153
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200154/**
155 * \brief Generate and write the second round message
156 * (TLS: contents of the Client/ServerKeyExchange)
Manuel Pégourié-Gonnard614bd5e2015-08-13 20:19:16 +0200157 *
158 * \param ctx Context to use
159 * \param buf Buffer to write the contents to
160 * \param len Buffer size
161 * \param olen Will be updated with the number of bytes written
162 * \param f_rng RNG function
163 * \param p_rng RNG parameter
164 *
165 * \return 0 if successfull,
166 * a negative error code otherwise
167 */
Manuel Pégourié-Gonnarde1927102015-08-14 14:20:48 +0200168int mbedtls_ecjpake_write_round_two( mbedtls_ecjpake_context *ctx,
Manuel Pégourié-Gonnard614bd5e2015-08-13 20:19:16 +0200169 unsigned char *buf, size_t len, size_t *olen,
170 int (*f_rng)(void *, unsigned char *, size_t),
171 void *p_rng );
172
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200173/**
174 * \brief Read and process the second round message
175 * (TLS: contents of the Client/ServerKeyExchange)
Manuel Pégourié-Gonnardec0eece2015-08-13 19:13:20 +0200176 *
177 * \param ctx Context to use
178 * \param buf Pointer to the message
179 * \param len Message length
180 *
181 * \return 0 if successfull,
182 * a negative error code otherwise
183 */
Manuel Pégourié-Gonnarde1927102015-08-14 14:20:48 +0200184int mbedtls_ecjpake_read_round_two( mbedtls_ecjpake_context *ctx,
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200185 const unsigned char *buf,
186 size_t len );
Manuel Pégourié-Gonnardec0eece2015-08-13 19:13:20 +0200187
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200188/**
189 * \brief Derive the shared secret
190 * (TLS: Pre-Master Secret)
Manuel Pégourié-Gonnard5f188292015-08-14 10:52:39 +0200191 *
192 * \param ctx
193 * \param buf Buffer to write the contents to
194 * \param len Buffer size
195 * \param olen Will be updated with the number of bytes written
196 * \param f_rng RNG function
197 * \param p_rng RNG parameter
198 *
199 * \return 0 if successfull,
200 * a negative error code otherwise
201 */
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200202int mbedtls_ecjpake_derive_secret( mbedtls_ecjpake_context *ctx,
Manuel Pégourié-Gonnard5f188292015-08-14 10:52:39 +0200203 unsigned char *buf, size_t len, size_t *olen,
204 int (*f_rng)(void *, unsigned char *, size_t),
205 void *p_rng );
206
Manuel Pégourié-Gonnardf7368c92015-08-14 14:33:05 +0200207/**
Manuel Pégourié-Gonnard4e8bc782015-08-12 20:50:31 +0200208 * \brief Free a context's content
209 *
210 * \param ctx context to free
211 */
212void mbedtls_ecjpake_free( mbedtls_ecjpake_context *ctx );
213
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +0200214#if defined(MBEDTLS_SELF_TEST)
215/**
216 * \brief Checkup routine
217 *
218 * \return 0 if successful, or 1 if a test failed
219 */
220int mbedtls_ecjpake_self_test( int verbose );
221#endif
222
223#ifdef __cplusplus
224}
225#endif
226
227#endif /* ecjpake.h */