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 |
| 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é-Gonnard | 6b798b9 | 2015-08-14 11:18:30 +0200 | [diff] [blame] | 27 | /* |
Manuel Pégourié-Gonnard | d8204a7 | 2015-08-14 13:36:55 +0200 | [diff] [blame] | 28 | * 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é-Gonnard | f7368c9 | 2015-08-14 14:33:05 +0200 | [diff] [blame] | 33 | * 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é-Gonnard | d8204a7 | 2015-08-14 13:36:55 +0200 | [diff] [blame] | 36 | * |
| 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é-Gonnard | f7368c9 | 2015-08-14 14:33:05 +0200 | [diff] [blame] | 40 | * The payloads are serialized in a way suitable for use in TLS, but could |
| 41 | * also be use outside TLS. |
Manuel Pégourié-Gonnard | 6b798b9 | 2015-08-14 11:18:30 +0200 | [diff] [blame] | 42 | */ |
| 43 | |
Manuel Pégourié-Gonnard | 4d8685b | 2015-08-05 15:44:42 +0200 | [diff] [blame] | 44 | #include "ecp.h" |
| 45 | #include "md.h" |
| 46 | |
Hanno Becker | 616d1ca | 2018-01-24 10:25:05 +0000 | [diff] [blame^] | 47 | #if !defined(MBEDTLS_ECJPAKE_ALT) |
| 48 | |
Manuel Pégourié-Gonnard | 4d8685b | 2015-08-05 15:44:42 +0200 | [diff] [blame] | 49 | #ifdef __cplusplus |
| 50 | extern "C" { |
| 51 | #endif |
| 52 | |
Manuel Pégourié-Gonnard | 6b798b9 | 2015-08-14 11:18:30 +0200 | [diff] [blame] | 53 | /** |
| 54 | * Roles in the EC J-PAKE exchange |
| 55 | */ |
Manuel Pégourié-Gonnard | 6449391 | 2015-08-13 20:19:51 +0200 | [diff] [blame] | 56 | typedef enum { |
Manuel Pégourié-Gonnard | 6b798b9 | 2015-08-14 11:18:30 +0200 | [diff] [blame] | 57 | MBEDTLS_ECJPAKE_CLIENT = 0, /**< Client */ |
| 58 | MBEDTLS_ECJPAKE_SERVER, /**< Server */ |
Manuel Pégourié-Gonnard | 6449391 | 2015-08-13 20:19:51 +0200 | [diff] [blame] | 59 | } mbedtls_ecjpake_role; |
| 60 | |
Manuel Pégourié-Gonnard | 6b798b9 | 2015-08-14 11:18:30 +0200 | [diff] [blame] | 61 | /** |
Manuel Pégourié-Gonnard | ce45676 | 2015-08-14 11:54:35 +0200 | [diff] [blame] | 62 | * EC J-PAKE context structure. |
| 63 | * |
| 64 | * J-PAKE is a symmetric protocol, except for the identifiers used in |
| 65 | * Zero-Knowledge Proofs, and the serialization of the second message |
| 66 | * (KeyExchange) as defined by the Thread spec. |
| 67 | * |
| 68 | * In order to benefit from this symmetry, we choose a different naming |
| 69 | * convetion from the Thread v1.0 spec. Correspondance is indicated in the |
Simon Butcher | 5b331b9 | 2016-01-03 16:14:14 +0000 | [diff] [blame] | 70 | * description as a pair C: client name, S: server name |
Manuel Pégourié-Gonnard | 6b798b9 | 2015-08-14 11:18:30 +0200 | [diff] [blame] | 71 | */ |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 72 | typedef struct |
| 73 | { |
| 74 | const mbedtls_md_info_t *md_info; /**< Hash to use */ |
| 75 | mbedtls_ecp_group grp; /**< Elliptic curve */ |
Manuel Pégourié-Gonnard | 6449391 | 2015-08-13 20:19:51 +0200 | [diff] [blame] | 76 | mbedtls_ecjpake_role role; /**< Are we client or server? */ |
Robert Cragie | 7cdad77 | 2015-10-02 13:31:41 +0100 | [diff] [blame] | 77 | int point_format; /**< Format for point export */ |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 78 | |
Manuel Pégourié-Gonnard | ce45676 | 2015-08-14 11:54:35 +0200 | [diff] [blame] | 79 | mbedtls_ecp_point Xm1; /**< My public key 1 C: X1, S: X3 */ |
| 80 | mbedtls_ecp_point Xm2; /**< My public key 2 C: X2, S: X4 */ |
| 81 | mbedtls_ecp_point Xp1; /**< Peer public key 1 C: X3, S: X1 */ |
| 82 | mbedtls_ecp_point Xp2; /**< Peer public key 2 C: X4, S: X2 */ |
| 83 | 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] | 84 | |
Manuel Pégourié-Gonnard | ce45676 | 2015-08-14 11:54:35 +0200 | [diff] [blame] | 85 | mbedtls_mpi xm1; /**< My private key 1 C: x1, S: x3 */ |
| 86 | 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] | 87 | |
Manuel Pégourié-Gonnard | 6b798b9 | 2015-08-14 11:18:30 +0200 | [diff] [blame] | 88 | mbedtls_mpi s; /**< Pre-shared secret (passphrase) */ |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 89 | } mbedtls_ecjpake_context; |
| 90 | |
Manuel Pégourié-Gonnard | f7368c9 | 2015-08-14 14:33:05 +0200 | [diff] [blame] | 91 | /** |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 92 | * \brief Initialize a context |
| 93 | * (just makes it ready for setup() or free()). |
| 94 | * |
| 95 | * \param ctx context to initialize |
| 96 | */ |
| 97 | void mbedtls_ecjpake_init( mbedtls_ecjpake_context *ctx ); |
| 98 | |
Manuel Pégourié-Gonnard | f7368c9 | 2015-08-14 14:33:05 +0200 | [diff] [blame] | 99 | /** |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 100 | * \brief Set up a context for use |
| 101 | * |
| 102 | * \note Currently the only values for hash/curve allowed by the |
| 103 | * standard are MBEDTLS_MD_SHA256/MBEDTLS_ECP_DP_SECP256R1. |
| 104 | * |
| 105 | * \param ctx context to set up |
Manuel Pégourié-Gonnard | 6449391 | 2015-08-13 20:19:51 +0200 | [diff] [blame] | 106 | * \param role Our role: client or server |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 107 | * \param hash hash function to use (MBEDTLS_MD_XXX) |
| 108 | * \param curve elliptic curve identifier (MBEDTLS_ECP_DP_XXX) |
Manuel Pégourié-Gonnard | 6b798b9 | 2015-08-14 11:18:30 +0200 | [diff] [blame] | 109 | * \param secret pre-shared secret (passphrase) |
Manuel Pégourié-Gonnard | 23dcbe3 | 2015-08-13 09:37:00 +0200 | [diff] [blame] | 110 | * \param len length of the shared secret |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 111 | * |
| 112 | * \return 0 if successfull, |
| 113 | * a negative error code otherwise |
| 114 | */ |
| 115 | int mbedtls_ecjpake_setup( mbedtls_ecjpake_context *ctx, |
Manuel Pégourié-Gonnard | 6449391 | 2015-08-13 20:19:51 +0200 | [diff] [blame] | 116 | mbedtls_ecjpake_role role, |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 117 | mbedtls_md_type_t hash, |
Manuel Pégourié-Gonnard | 23dcbe3 | 2015-08-13 09:37:00 +0200 | [diff] [blame] | 118 | mbedtls_ecp_group_id curve, |
| 119 | const unsigned char *secret, |
| 120 | size_t len ); |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 121 | |
Andres Amaya Garcia | af610a0 | 2016-12-14 10:13:43 +0000 | [diff] [blame] | 122 | /** |
Manuel Pégourié-Gonnard | b813acc | 2015-09-15 15:34:09 +0200 | [diff] [blame] | 123 | * \brief Check if a context is ready for use |
| 124 | * |
| 125 | * \param ctx Context to check |
| 126 | * |
| 127 | * \return 0 if the context is ready for use, |
| 128 | * MBEDTLS_ERR_ECP_BAD_INPUT_DATA otherwise |
| 129 | */ |
| 130 | int mbedtls_ecjpake_check( const mbedtls_ecjpake_context *ctx ); |
| 131 | |
Manuel Pégourié-Gonnard | f7368c9 | 2015-08-14 14:33:05 +0200 | [diff] [blame] | 132 | /** |
Manuel Pégourié-Gonnard | d8204a7 | 2015-08-14 13:36:55 +0200 | [diff] [blame] | 133 | * \brief Generate and write the first round message |
| 134 | * (TLS: contents of the Client/ServerHello extension, |
| 135 | * excluding extension type and length bytes) |
Manuel Pégourié-Gonnard | 4e8bc78 | 2015-08-12 20:50:31 +0200 | [diff] [blame] | 136 | * |
| 137 | * \param ctx Context to use |
| 138 | * \param buf Buffer to write the contents to |
| 139 | * \param len Buffer size |
| 140 | * \param olen Will be updated with the number of bytes written |
| 141 | * \param f_rng RNG function |
| 142 | * \param p_rng RNG parameter |
| 143 | * |
| 144 | * \return 0 if successfull, |
| 145 | * a negative error code otherwise |
| 146 | */ |
Manuel Pégourié-Gonnard | d8204a7 | 2015-08-14 13:36:55 +0200 | [diff] [blame] | 147 | int mbedtls_ecjpake_write_round_one( mbedtls_ecjpake_context *ctx, |
Manuel Pégourié-Gonnard | 4e8bc78 | 2015-08-12 20:50:31 +0200 | [diff] [blame] | 148 | unsigned char *buf, size_t len, size_t *olen, |
| 149 | int (*f_rng)(void *, unsigned char *, size_t), |
| 150 | void *p_rng ); |
Manuel Pégourié-Gonnard | f7368c9 | 2015-08-14 14:33:05 +0200 | [diff] [blame] | 151 | |
| 152 | /** |
| 153 | * \brief Read and process the first round message |
Manuel Pégourié-Gonnard | d8204a7 | 2015-08-14 13:36:55 +0200 | [diff] [blame] | 154 | * (TLS: contents of the Client/ServerHello extension, |
| 155 | * excluding extension type and length bytes) |
Manuel Pégourié-Gonnard | 4e8bc78 | 2015-08-12 20:50:31 +0200 | [diff] [blame] | 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 | */ |
Manuel Pégourié-Gonnard | d8204a7 | 2015-08-14 13:36:55 +0200 | [diff] [blame] | 164 | int mbedtls_ecjpake_read_round_one( mbedtls_ecjpake_context *ctx, |
| 165 | const unsigned char *buf, |
| 166 | size_t len ); |
Manuel Pégourié-Gonnard | 4e8bc78 | 2015-08-12 20:50:31 +0200 | [diff] [blame] | 167 | |
Manuel Pégourié-Gonnard | f7368c9 | 2015-08-14 14:33:05 +0200 | [diff] [blame] | 168 | /** |
| 169 | * \brief Generate and write the second round message |
| 170 | * (TLS: contents of the Client/ServerKeyExchange) |
Manuel Pégourié-Gonnard | 614bd5e | 2015-08-13 20:19:16 +0200 | [diff] [blame] | 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 | */ |
Manuel Pégourié-Gonnard | e192710 | 2015-08-14 14:20:48 +0200 | [diff] [blame] | 182 | int mbedtls_ecjpake_write_round_two( mbedtls_ecjpake_context *ctx, |
Manuel Pégourié-Gonnard | 614bd5e | 2015-08-13 20:19:16 +0200 | [diff] [blame] | 183 | unsigned char *buf, size_t len, size_t *olen, |
| 184 | int (*f_rng)(void *, unsigned char *, size_t), |
| 185 | void *p_rng ); |
| 186 | |
Manuel Pégourié-Gonnard | f7368c9 | 2015-08-14 14:33:05 +0200 | [diff] [blame] | 187 | /** |
| 188 | * \brief Read and process the second round message |
| 189 | * (TLS: contents of the Client/ServerKeyExchange) |
Manuel Pégourié-Gonnard | ec0eece | 2015-08-13 19:13:20 +0200 | [diff] [blame] | 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 | */ |
Manuel Pégourié-Gonnard | e192710 | 2015-08-14 14:20:48 +0200 | [diff] [blame] | 198 | int mbedtls_ecjpake_read_round_two( mbedtls_ecjpake_context *ctx, |
Manuel Pégourié-Gonnard | f7368c9 | 2015-08-14 14:33:05 +0200 | [diff] [blame] | 199 | const unsigned char *buf, |
| 200 | size_t len ); |
Manuel Pégourié-Gonnard | ec0eece | 2015-08-13 19:13:20 +0200 | [diff] [blame] | 201 | |
Manuel Pégourié-Gonnard | f7368c9 | 2015-08-14 14:33:05 +0200 | [diff] [blame] | 202 | /** |
| 203 | * \brief Derive the shared secret |
| 204 | * (TLS: Pre-Master Secret) |
Manuel Pégourié-Gonnard | 5f18829 | 2015-08-14 10:52:39 +0200 | [diff] [blame] | 205 | * |
Manuel Pégourié-Gonnard | 55f3d84 | 2015-08-14 15:08:43 +0200 | [diff] [blame] | 206 | * \param ctx Context to use |
Manuel Pégourié-Gonnard | 5f18829 | 2015-08-14 10:52:39 +0200 | [diff] [blame] | 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 | */ |
Manuel Pégourié-Gonnard | f7368c9 | 2015-08-14 14:33:05 +0200 | [diff] [blame] | 216 | int mbedtls_ecjpake_derive_secret( mbedtls_ecjpake_context *ctx, |
Manuel Pégourié-Gonnard | 5f18829 | 2015-08-14 10:52:39 +0200 | [diff] [blame] | 217 | unsigned char *buf, size_t len, size_t *olen, |
| 218 | int (*f_rng)(void *, unsigned char *, size_t), |
| 219 | void *p_rng ); |
| 220 | |
Manuel Pégourié-Gonnard | f7368c9 | 2015-08-14 14:33:05 +0200 | [diff] [blame] | 221 | /** |
Manuel Pégourié-Gonnard | 4e8bc78 | 2015-08-12 20:50:31 +0200 | [diff] [blame] | 222 | * \brief Free a context's content |
| 223 | * |
| 224 | * \param ctx context to free |
| 225 | */ |
| 226 | void mbedtls_ecjpake_free( mbedtls_ecjpake_context *ctx ); |
| 227 | |
Hanno Becker | 616d1ca | 2018-01-24 10:25:05 +0000 | [diff] [blame^] | 228 | #ifdef __cplusplus |
| 229 | } |
| 230 | #endif |
| 231 | |
| 232 | #else /* MBEDTLS_ECJPAKE_ALT */ |
| 233 | #include "ecjpake_alt.h" |
| 234 | #endif /* MBEDTLS_ECJPAKE_ALT */ |
| 235 | |
Manuel Pégourié-Gonnard | 4d8685b | 2015-08-05 15:44:42 +0200 | [diff] [blame] | 236 | #if defined(MBEDTLS_SELF_TEST) |
Hanno Becker | 616d1ca | 2018-01-24 10:25:05 +0000 | [diff] [blame^] | 237 | |
| 238 | #ifdef __cplusplus |
| 239 | extern "C" { |
| 240 | #endif |
| 241 | |
Manuel Pégourié-Gonnard | 4d8685b | 2015-08-05 15:44:42 +0200 | [diff] [blame] | 242 | /** |
| 243 | * \brief Checkup routine |
| 244 | * |
| 245 | * \return 0 if successful, or 1 if a test failed |
| 246 | */ |
| 247 | int mbedtls_ecjpake_self_test( int verbose ); |
Manuel Pégourié-Gonnard | 4d8685b | 2015-08-05 15:44:42 +0200 | [diff] [blame] | 248 | |
| 249 | #ifdef __cplusplus |
| 250 | } |
| 251 | #endif |
| 252 | |
Hanno Becker | 616d1ca | 2018-01-24 10:25:05 +0000 | [diff] [blame^] | 253 | #endif /* MBEDTLS_SELF_TEST */ |
| 254 | |
Manuel Pégourié-Gonnard | 4d8685b | 2015-08-05 15:44:42 +0200 | [diff] [blame] | 255 | #endif /* ecjpake.h */ |