Manuel Pégourié-Gonnard | 4d8685b | 2015-08-05 15:44:42 +0200 | [diff] [blame] | 1 | /** |
| 2 | * \file ecjpake.h |
| 3 | * |
| 4 | * \brief Elliptic curve J-PAKE |
Darryl Green | a40a101 | 2018-01-05 15:33:17 +0000 | [diff] [blame] | 5 | */ |
| 6 | /* |
Manuel Pégourié-Gonnard | 4d8685b | 2015-08-05 15:44:42 +0200 | [diff] [blame] | 7 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Bence Szépkúti | 4e9f712 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 8 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 9 | * |
| 10 | * This file is provided under the Apache License 2.0, or the |
| 11 | * GNU General Public License v2.0 or later. |
| 12 | * |
| 13 | * ********** |
| 14 | * Apache License 2.0: |
Manuel Pégourié-Gonnard | 4d8685b | 2015-08-05 15:44:42 +0200 | [diff] [blame] | 15 | * |
| 16 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 17 | * not use this file except in compliance with the License. |
| 18 | * You may obtain a copy of the License at |
| 19 | * |
| 20 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 21 | * |
| 22 | * Unless required by applicable law or agreed to in writing, software |
| 23 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 24 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 25 | * See the License for the specific language governing permissions and |
| 26 | * limitations under the License. |
| 27 | * |
Bence Szépkúti | 4e9f712 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 28 | * ********** |
| 29 | * |
| 30 | * ********** |
| 31 | * GNU General Public License v2.0 or later: |
| 32 | * |
| 33 | * This program is free software; you can redistribute it and/or modify |
| 34 | * it under the terms of the GNU General Public License as published by |
| 35 | * the Free Software Foundation; either version 2 of the License, or |
| 36 | * (at your option) any later version. |
| 37 | * |
| 38 | * This program is distributed in the hope that it will be useful, |
| 39 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 40 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 41 | * GNU General Public License for more details. |
| 42 | * |
| 43 | * You should have received a copy of the GNU General Public License along |
| 44 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 45 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 46 | * |
| 47 | * ********** |
| 48 | * |
Manuel Pégourié-Gonnard | 4d8685b | 2015-08-05 15:44:42 +0200 | [diff] [blame] | 49 | * This file is part of mbed TLS (https://tls.mbed.org) |
| 50 | */ |
| 51 | #ifndef MBEDTLS_ECJPAKE_H |
| 52 | #define MBEDTLS_ECJPAKE_H |
| 53 | |
Manuel Pégourié-Gonnard | 6b798b9 | 2015-08-14 11:18:30 +0200 | [diff] [blame] | 54 | /* |
Manuel Pégourié-Gonnard | d8204a7 | 2015-08-14 13:36:55 +0200 | [diff] [blame] | 55 | * J-PAKE is a password-authenticated key exchange that allows deriving a |
| 56 | * strong shared secret from a (potentially low entropy) pre-shared |
| 57 | * passphrase, with forward secrecy and mutual authentication. |
| 58 | * https://en.wikipedia.org/wiki/Password_Authenticated_Key_Exchange_by_Juggling |
| 59 | * |
Manuel Pégourié-Gonnard | f7368c9 | 2015-08-14 14:33:05 +0200 | [diff] [blame] | 60 | * This file implements the Elliptic Curve variant of J-PAKE, |
| 61 | * as defined in Chapter 7.4 of the Thread v1.0 Specification, |
| 62 | * available to members of the Thread Group http://threadgroup.org/ |
Manuel Pégourié-Gonnard | d8204a7 | 2015-08-14 13:36:55 +0200 | [diff] [blame] | 63 | * |
| 64 | * As the J-PAKE algorithm is inherently symmetric, so is our API. |
| 65 | * Each party needs to send its first round message, in any order, to the |
| 66 | * other party, then each sends its second round message, in any order. |
Manuel Pégourié-Gonnard | f7368c9 | 2015-08-14 14:33:05 +0200 | [diff] [blame] | 67 | * The payloads are serialized in a way suitable for use in TLS, but could |
| 68 | * also be use outside TLS. |
Manuel Pégourié-Gonnard | 6b798b9 | 2015-08-14 11:18:30 +0200 | [diff] [blame] | 69 | */ |
Ron Eldor | 0559c66 | 2018-02-14 16:02:41 +0200 | [diff] [blame] | 70 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 71 | #include "config.h" |
| 72 | #else |
| 73 | #include MBEDTLS_CONFIG_FILE |
| 74 | #endif |
Manuel Pégourié-Gonnard | 6b798b9 | 2015-08-14 11:18:30 +0200 | [diff] [blame] | 75 | |
Manuel Pégourié-Gonnard | 4d8685b | 2015-08-05 15:44:42 +0200 | [diff] [blame] | 76 | #include "ecp.h" |
| 77 | #include "md.h" |
| 78 | |
Hanno Becker | 616d1ca | 2018-01-24 10:25:05 +0000 | [diff] [blame] | 79 | #if !defined(MBEDTLS_ECJPAKE_ALT) |
| 80 | |
Manuel Pégourié-Gonnard | 4d8685b | 2015-08-05 15:44:42 +0200 | [diff] [blame] | 81 | #ifdef __cplusplus |
| 82 | extern "C" { |
| 83 | #endif |
| 84 | |
Manuel Pégourié-Gonnard | 6b798b9 | 2015-08-14 11:18:30 +0200 | [diff] [blame] | 85 | /** |
| 86 | * Roles in the EC J-PAKE exchange |
| 87 | */ |
Manuel Pégourié-Gonnard | 6449391 | 2015-08-13 20:19:51 +0200 | [diff] [blame] | 88 | typedef enum { |
Manuel Pégourié-Gonnard | 6b798b9 | 2015-08-14 11:18:30 +0200 | [diff] [blame] | 89 | MBEDTLS_ECJPAKE_CLIENT = 0, /**< Client */ |
| 90 | MBEDTLS_ECJPAKE_SERVER, /**< Server */ |
Manuel Pégourié-Gonnard | 6449391 | 2015-08-13 20:19:51 +0200 | [diff] [blame] | 91 | } mbedtls_ecjpake_role; |
| 92 | |
Manuel Pégourié-Gonnard | 6b798b9 | 2015-08-14 11:18:30 +0200 | [diff] [blame] | 93 | /** |
Manuel Pégourié-Gonnard | ce45676 | 2015-08-14 11:54:35 +0200 | [diff] [blame] | 94 | * EC J-PAKE context structure. |
| 95 | * |
| 96 | * J-PAKE is a symmetric protocol, except for the identifiers used in |
| 97 | * Zero-Knowledge Proofs, and the serialization of the second message |
| 98 | * (KeyExchange) as defined by the Thread spec. |
| 99 | * |
| 100 | * In order to benefit from this symmetry, we choose a different naming |
| 101 | * convetion from the Thread v1.0 spec. Correspondance is indicated in the |
Simon Butcher | 5b331b9 | 2016-01-03 16:14:14 +0000 | [diff] [blame] | 102 | * description as a pair C: client name, S: server name |
Manuel Pégourié-Gonnard | 6b798b9 | 2015-08-14 11:18:30 +0200 | [diff] [blame] | 103 | */ |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 104 | typedef struct |
| 105 | { |
| 106 | const mbedtls_md_info_t *md_info; /**< Hash to use */ |
| 107 | mbedtls_ecp_group grp; /**< Elliptic curve */ |
Manuel Pégourié-Gonnard | 6449391 | 2015-08-13 20:19:51 +0200 | [diff] [blame] | 108 | mbedtls_ecjpake_role role; /**< Are we client or server? */ |
Robert Cragie | 7cdad77 | 2015-10-02 13:31:41 +0100 | [diff] [blame] | 109 | int point_format; /**< Format for point export */ |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 110 | |
Manuel Pégourié-Gonnard | ce45676 | 2015-08-14 11:54:35 +0200 | [diff] [blame] | 111 | mbedtls_ecp_point Xm1; /**< My public key 1 C: X1, S: X3 */ |
| 112 | mbedtls_ecp_point Xm2; /**< My public key 2 C: X2, S: X4 */ |
| 113 | mbedtls_ecp_point Xp1; /**< Peer public key 1 C: X3, S: X1 */ |
| 114 | mbedtls_ecp_point Xp2; /**< Peer public key 2 C: X4, S: X2 */ |
| 115 | mbedtls_ecp_point Xp; /**< Peer public key C: Xs, S: Xc */ |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 116 | |
Manuel Pégourié-Gonnard | ce45676 | 2015-08-14 11:54:35 +0200 | [diff] [blame] | 117 | mbedtls_mpi xm1; /**< My private key 1 C: x1, S: x3 */ |
| 118 | mbedtls_mpi xm2; /**< My private key 2 C: x2, S: x4 */ |
Manuel Pégourié-Gonnard | 23dcbe3 | 2015-08-13 09:37:00 +0200 | [diff] [blame] | 119 | |
Manuel Pégourié-Gonnard | 6b798b9 | 2015-08-14 11:18:30 +0200 | [diff] [blame] | 120 | mbedtls_mpi s; /**< Pre-shared secret (passphrase) */ |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 121 | } mbedtls_ecjpake_context; |
| 122 | |
Manuel Pégourié-Gonnard | f7368c9 | 2015-08-14 14:33:05 +0200 | [diff] [blame] | 123 | /** |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 124 | * \brief Initialize a context |
| 125 | * (just makes it ready for setup() or free()). |
| 126 | * |
| 127 | * \param ctx context to initialize |
| 128 | */ |
| 129 | void mbedtls_ecjpake_init( mbedtls_ecjpake_context *ctx ); |
| 130 | |
Manuel Pégourié-Gonnard | f7368c9 | 2015-08-14 14:33:05 +0200 | [diff] [blame] | 131 | /** |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 132 | * \brief Set up a context for use |
| 133 | * |
| 134 | * \note Currently the only values for hash/curve allowed by the |
| 135 | * standard are MBEDTLS_MD_SHA256/MBEDTLS_ECP_DP_SECP256R1. |
| 136 | * |
| 137 | * \param ctx context to set up |
Manuel Pégourié-Gonnard | 6449391 | 2015-08-13 20:19:51 +0200 | [diff] [blame] | 138 | * \param role Our role: client or server |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 139 | * \param hash hash function to use (MBEDTLS_MD_XXX) |
| 140 | * \param curve elliptic curve identifier (MBEDTLS_ECP_DP_XXX) |
Manuel Pégourié-Gonnard | 6b798b9 | 2015-08-14 11:18:30 +0200 | [diff] [blame] | 141 | * \param secret pre-shared secret (passphrase) |
Manuel Pégourié-Gonnard | 23dcbe3 | 2015-08-13 09:37:00 +0200 | [diff] [blame] | 142 | * \param len length of the shared secret |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 143 | * |
| 144 | * \return 0 if successfull, |
| 145 | * a negative error code otherwise |
| 146 | */ |
| 147 | int mbedtls_ecjpake_setup( mbedtls_ecjpake_context *ctx, |
Manuel Pégourié-Gonnard | 6449391 | 2015-08-13 20:19:51 +0200 | [diff] [blame] | 148 | mbedtls_ecjpake_role role, |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 149 | mbedtls_md_type_t hash, |
Manuel Pégourié-Gonnard | 23dcbe3 | 2015-08-13 09:37:00 +0200 | [diff] [blame] | 150 | mbedtls_ecp_group_id curve, |
| 151 | const unsigned char *secret, |
| 152 | size_t len ); |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 153 | |
Andres Amaya Garcia | af610a0 | 2016-12-14 10:13:43 +0000 | [diff] [blame] | 154 | /** |
Manuel Pégourié-Gonnard | b813acc | 2015-09-15 15:34:09 +0200 | [diff] [blame] | 155 | * \brief Check if a context is ready for use |
| 156 | * |
| 157 | * \param ctx Context to check |
| 158 | * |
| 159 | * \return 0 if the context is ready for use, |
| 160 | * MBEDTLS_ERR_ECP_BAD_INPUT_DATA otherwise |
| 161 | */ |
| 162 | int mbedtls_ecjpake_check( const mbedtls_ecjpake_context *ctx ); |
| 163 | |
Manuel Pégourié-Gonnard | f7368c9 | 2015-08-14 14:33:05 +0200 | [diff] [blame] | 164 | /** |
Manuel Pégourié-Gonnard | d8204a7 | 2015-08-14 13:36:55 +0200 | [diff] [blame] | 165 | * \brief Generate and write the first round message |
| 166 | * (TLS: contents of the Client/ServerHello extension, |
| 167 | * excluding extension type and length bytes) |
Manuel Pégourié-Gonnard | 4e8bc78 | 2015-08-12 20:50:31 +0200 | [diff] [blame] | 168 | * |
| 169 | * \param ctx Context to use |
| 170 | * \param buf Buffer to write the contents to |
| 171 | * \param len Buffer size |
| 172 | * \param olen Will be updated with the number of bytes written |
| 173 | * \param f_rng RNG function |
| 174 | * \param p_rng RNG parameter |
| 175 | * |
| 176 | * \return 0 if successfull, |
| 177 | * a negative error code otherwise |
| 178 | */ |
Manuel Pégourié-Gonnard | d8204a7 | 2015-08-14 13:36:55 +0200 | [diff] [blame] | 179 | int mbedtls_ecjpake_write_round_one( mbedtls_ecjpake_context *ctx, |
Manuel Pégourié-Gonnard | 4e8bc78 | 2015-08-12 20:50:31 +0200 | [diff] [blame] | 180 | unsigned char *buf, size_t len, size_t *olen, |
| 181 | int (*f_rng)(void *, unsigned char *, size_t), |
| 182 | void *p_rng ); |
Manuel Pégourié-Gonnard | f7368c9 | 2015-08-14 14:33:05 +0200 | [diff] [blame] | 183 | |
| 184 | /** |
| 185 | * \brief Read and process the first round message |
Manuel Pégourié-Gonnard | d8204a7 | 2015-08-14 13:36:55 +0200 | [diff] [blame] | 186 | * (TLS: contents of the Client/ServerHello extension, |
| 187 | * excluding extension type and length bytes) |
Manuel Pégourié-Gonnard | 4e8bc78 | 2015-08-12 20:50:31 +0200 | [diff] [blame] | 188 | * |
| 189 | * \param ctx Context to use |
| 190 | * \param buf Pointer to extension contents |
| 191 | * \param len Extension length |
| 192 | * |
| 193 | * \return 0 if successfull, |
| 194 | * a negative error code otherwise |
| 195 | */ |
Manuel Pégourié-Gonnard | d8204a7 | 2015-08-14 13:36:55 +0200 | [diff] [blame] | 196 | int mbedtls_ecjpake_read_round_one( mbedtls_ecjpake_context *ctx, |
| 197 | const unsigned char *buf, |
| 198 | size_t len ); |
Manuel Pégourié-Gonnard | 4e8bc78 | 2015-08-12 20:50:31 +0200 | [diff] [blame] | 199 | |
Manuel Pégourié-Gonnard | f7368c9 | 2015-08-14 14:33:05 +0200 | [diff] [blame] | 200 | /** |
| 201 | * \brief Generate and write the second round message |
| 202 | * (TLS: contents of the Client/ServerKeyExchange) |
Manuel Pégourié-Gonnard | 614bd5e | 2015-08-13 20:19:16 +0200 | [diff] [blame] | 203 | * |
| 204 | * \param ctx Context to use |
| 205 | * \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é-Gonnard | e192710 | 2015-08-14 14:20:48 +0200 | [diff] [blame] | 214 | int mbedtls_ecjpake_write_round_two( mbedtls_ecjpake_context *ctx, |
Manuel Pégourié-Gonnard | 614bd5e | 2015-08-13 20:19:16 +0200 | [diff] [blame] | 215 | 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é-Gonnard | f7368c9 | 2015-08-14 14:33:05 +0200 | [diff] [blame] | 219 | /** |
| 220 | * \brief Read and process the second round message |
| 221 | * (TLS: contents of the Client/ServerKeyExchange) |
Manuel Pégourié-Gonnard | ec0eece | 2015-08-13 19:13:20 +0200 | [diff] [blame] | 222 | * |
| 223 | * \param ctx Context to use |
| 224 | * \param buf Pointer to the message |
| 225 | * \param len Message length |
| 226 | * |
| 227 | * \return 0 if successfull, |
| 228 | * a negative error code otherwise |
| 229 | */ |
Manuel Pégourié-Gonnard | e192710 | 2015-08-14 14:20:48 +0200 | [diff] [blame] | 230 | int mbedtls_ecjpake_read_round_two( mbedtls_ecjpake_context *ctx, |
Manuel Pégourié-Gonnard | f7368c9 | 2015-08-14 14:33:05 +0200 | [diff] [blame] | 231 | const unsigned char *buf, |
| 232 | size_t len ); |
Manuel Pégourié-Gonnard | ec0eece | 2015-08-13 19:13:20 +0200 | [diff] [blame] | 233 | |
Manuel Pégourié-Gonnard | f7368c9 | 2015-08-14 14:33:05 +0200 | [diff] [blame] | 234 | /** |
| 235 | * \brief Derive the shared secret |
| 236 | * (TLS: Pre-Master Secret) |
Manuel Pégourié-Gonnard | 5f18829 | 2015-08-14 10:52:39 +0200 | [diff] [blame] | 237 | * |
Manuel Pégourié-Gonnard | 55f3d84 | 2015-08-14 15:08:43 +0200 | [diff] [blame] | 238 | * \param ctx Context to use |
Manuel Pégourié-Gonnard | 5f18829 | 2015-08-14 10:52:39 +0200 | [diff] [blame] | 239 | * \param buf Buffer to write the contents to |
| 240 | * \param len Buffer size |
| 241 | * \param olen Will be updated with the number of bytes written |
| 242 | * \param f_rng RNG function |
| 243 | * \param p_rng RNG parameter |
| 244 | * |
| 245 | * \return 0 if successfull, |
| 246 | * a negative error code otherwise |
| 247 | */ |
Manuel Pégourié-Gonnard | f7368c9 | 2015-08-14 14:33:05 +0200 | [diff] [blame] | 248 | int mbedtls_ecjpake_derive_secret( mbedtls_ecjpake_context *ctx, |
Manuel Pégourié-Gonnard | 5f18829 | 2015-08-14 10:52:39 +0200 | [diff] [blame] | 249 | unsigned char *buf, size_t len, size_t *olen, |
| 250 | int (*f_rng)(void *, unsigned char *, size_t), |
| 251 | void *p_rng ); |
| 252 | |
Manuel Pégourié-Gonnard | f7368c9 | 2015-08-14 14:33:05 +0200 | [diff] [blame] | 253 | /** |
Manuel Pégourié-Gonnard | 4e8bc78 | 2015-08-12 20:50:31 +0200 | [diff] [blame] | 254 | * \brief Free a context's content |
| 255 | * |
| 256 | * \param ctx context to free |
| 257 | */ |
| 258 | void mbedtls_ecjpake_free( mbedtls_ecjpake_context *ctx ); |
| 259 | |
Hanno Becker | 616d1ca | 2018-01-24 10:25:05 +0000 | [diff] [blame] | 260 | #ifdef __cplusplus |
| 261 | } |
| 262 | #endif |
| 263 | |
| 264 | #else /* MBEDTLS_ECJPAKE_ALT */ |
| 265 | #include "ecjpake_alt.h" |
| 266 | #endif /* MBEDTLS_ECJPAKE_ALT */ |
| 267 | |
Manuel Pégourié-Gonnard | 4d8685b | 2015-08-05 15:44:42 +0200 | [diff] [blame] | 268 | #if defined(MBEDTLS_SELF_TEST) |
Hanno Becker | 616d1ca | 2018-01-24 10:25:05 +0000 | [diff] [blame] | 269 | |
| 270 | #ifdef __cplusplus |
| 271 | extern "C" { |
| 272 | #endif |
| 273 | |
Manuel Pégourié-Gonnard | 4d8685b | 2015-08-05 15:44:42 +0200 | [diff] [blame] | 274 | /** |
| 275 | * \brief Checkup routine |
| 276 | * |
| 277 | * \return 0 if successful, or 1 if a test failed |
| 278 | */ |
| 279 | int mbedtls_ecjpake_self_test( int verbose ); |
Manuel Pégourié-Gonnard | 4d8685b | 2015-08-05 15:44:42 +0200 | [diff] [blame] | 280 | |
| 281 | #ifdef __cplusplus |
| 282 | } |
| 283 | #endif |
| 284 | |
Hanno Becker | 616d1ca | 2018-01-24 10:25:05 +0000 | [diff] [blame] | 285 | #endif /* MBEDTLS_SELF_TEST */ |
| 286 | |
Manuel Pégourié-Gonnard | 4d8685b | 2015-08-05 15:44:42 +0200 | [diff] [blame] | 287 | #endif /* ecjpake.h */ |