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 |
| 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é-Gonnard | 6b798b9 | 2015-08-14 11:18:30 +0200 | [diff] [blame] | 26 | /* |
Manuel Pégourié-Gonnard | d8204a7 | 2015-08-14 13:36:55 +0200 | [diff] [blame] | 27 | * 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é-Gonnard | f7368c9 | 2015-08-14 14:33:05 +0200 | [diff] [blame] | 32 | * 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é-Gonnard | d8204a7 | 2015-08-14 13:36:55 +0200 | [diff] [blame] | 35 | * |
| 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é-Gonnard | f7368c9 | 2015-08-14 14:33:05 +0200 | [diff] [blame] | 39 | * The payloads are serialized in a way suitable for use in TLS, but could |
| 40 | * also be use outside TLS. |
Manuel Pégourié-Gonnard | 6b798b9 | 2015-08-14 11:18:30 +0200 | [diff] [blame] | 41 | */ |
| 42 | |
Manuel Pégourié-Gonnard | 4d8685b | 2015-08-05 15:44:42 +0200 | [diff] [blame] | 43 | #include "ecp.h" |
| 44 | #include "md.h" |
| 45 | |
| 46 | #ifdef __cplusplus |
| 47 | extern "C" { |
| 48 | #endif |
| 49 | |
Manuel Pégourié-Gonnard | 6b798b9 | 2015-08-14 11:18:30 +0200 | [diff] [blame] | 50 | /** |
| 51 | * Roles in the EC J-PAKE exchange |
| 52 | */ |
Manuel Pégourié-Gonnard | 6449391 | 2015-08-13 20:19:51 +0200 | [diff] [blame] | 53 | typedef enum { |
Manuel Pégourié-Gonnard | 6b798b9 | 2015-08-14 11:18:30 +0200 | [diff] [blame] | 54 | MBEDTLS_ECJPAKE_CLIENT = 0, /**< Client */ |
| 55 | MBEDTLS_ECJPAKE_SERVER, /**< Server */ |
Manuel Pégourié-Gonnard | 6449391 | 2015-08-13 20:19:51 +0200 | [diff] [blame] | 56 | } mbedtls_ecjpake_role; |
| 57 | |
Manuel Pégourié-Gonnard | 6b798b9 | 2015-08-14 11:18:30 +0200 | [diff] [blame] | 58 | /** |
Manuel Pégourié-Gonnard | ce45676 | 2015-08-14 11:54:35 +0200 | [diff] [blame] | 59 | * 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é-Gonnard | 6b798b9 | 2015-08-14 11:18:30 +0200 | [diff] [blame] | 68 | */ |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 69 | typedef struct |
| 70 | { |
| 71 | const mbedtls_md_info_t *md_info; /**< Hash to use */ |
| 72 | mbedtls_ecp_group grp; /**< Elliptic curve */ |
Manuel Pégourié-Gonnard | 6449391 | 2015-08-13 20:19:51 +0200 | [diff] [blame] | 73 | mbedtls_ecjpake_role role; /**< Are we client or server? */ |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 74 | |
Manuel Pégourié-Gonnard | ce45676 | 2015-08-14 11:54:35 +0200 | [diff] [blame] | 75 | 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é-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 80 | |
Manuel Pégourié-Gonnard | ce45676 | 2015-08-14 11:54:35 +0200 | [diff] [blame] | 81 | 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é-Gonnard | 23dcbe3 | 2015-08-13 09:37:00 +0200 | [diff] [blame] | 83 | |
Manuel Pégourié-Gonnard | 6b798b9 | 2015-08-14 11:18:30 +0200 | [diff] [blame] | 84 | mbedtls_mpi s; /**< Pre-shared secret (passphrase) */ |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 85 | } mbedtls_ecjpake_context; |
| 86 | |
Manuel Pégourié-Gonnard | f7368c9 | 2015-08-14 14:33:05 +0200 | [diff] [blame] | 87 | /** |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 88 | * \brief Initialize a context |
| 89 | * (just makes it ready for setup() or free()). |
| 90 | * |
| 91 | * \param ctx context to initialize |
| 92 | */ |
| 93 | void mbedtls_ecjpake_init( mbedtls_ecjpake_context *ctx ); |
| 94 | |
Manuel Pégourié-Gonnard | f7368c9 | 2015-08-14 14:33:05 +0200 | [diff] [blame] | 95 | /** |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 96 | * \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é-Gonnard | 6449391 | 2015-08-13 20:19:51 +0200 | [diff] [blame] | 102 | * \param role Our role: client or server |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 103 | * \param hash hash function to use (MBEDTLS_MD_XXX) |
| 104 | * \param curve elliptic curve identifier (MBEDTLS_ECP_DP_XXX) |
Manuel Pégourié-Gonnard | 6b798b9 | 2015-08-14 11:18:30 +0200 | [diff] [blame] | 105 | * \param secret pre-shared secret (passphrase) |
Manuel Pégourié-Gonnard | 23dcbe3 | 2015-08-13 09:37:00 +0200 | [diff] [blame] | 106 | * \param len length of the shared secret |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 107 | * |
| 108 | * \return 0 if successfull, |
| 109 | * a negative error code otherwise |
| 110 | */ |
| 111 | int mbedtls_ecjpake_setup( mbedtls_ecjpake_context *ctx, |
Manuel Pégourié-Gonnard | 6449391 | 2015-08-13 20:19:51 +0200 | [diff] [blame] | 112 | mbedtls_ecjpake_role role, |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 113 | mbedtls_md_type_t hash, |
Manuel Pégourié-Gonnard | 23dcbe3 | 2015-08-13 09:37:00 +0200 | [diff] [blame] | 114 | mbedtls_ecp_group_id curve, |
| 115 | const unsigned char *secret, |
| 116 | size_t len ); |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 117 | |
Manuel Pégourié-Gonnard | b813acc | 2015-09-15 15:34:09 +0200 | [diff] [blame] | 118 | /* |
| 119 | * \brief Check if a context is ready for use |
| 120 | * |
| 121 | * \param ctx Context to check |
| 122 | * |
| 123 | * \return 0 if the context is ready for use, |
| 124 | * MBEDTLS_ERR_ECP_BAD_INPUT_DATA otherwise |
| 125 | */ |
| 126 | int mbedtls_ecjpake_check( const mbedtls_ecjpake_context *ctx ); |
| 127 | |
Manuel Pégourié-Gonnard | f7368c9 | 2015-08-14 14:33:05 +0200 | [diff] [blame] | 128 | /** |
Manuel Pégourié-Gonnard | d8204a7 | 2015-08-14 13:36:55 +0200 | [diff] [blame] | 129 | * \brief Generate and write the first round message |
| 130 | * (TLS: contents of the Client/ServerHello extension, |
| 131 | * excluding extension type and length bytes) |
Manuel Pégourié-Gonnard | 4e8bc78 | 2015-08-12 20:50:31 +0200 | [diff] [blame] | 132 | * |
| 133 | * \param ctx Context to use |
| 134 | * \param buf Buffer to write the contents to |
| 135 | * \param len Buffer size |
| 136 | * \param olen Will be updated with the number of bytes written |
| 137 | * \param f_rng RNG function |
| 138 | * \param p_rng RNG parameter |
| 139 | * |
| 140 | * \return 0 if successfull, |
| 141 | * a negative error code otherwise |
| 142 | */ |
Manuel Pégourié-Gonnard | d8204a7 | 2015-08-14 13:36:55 +0200 | [diff] [blame] | 143 | int mbedtls_ecjpake_write_round_one( mbedtls_ecjpake_context *ctx, |
Manuel Pégourié-Gonnard | 4e8bc78 | 2015-08-12 20:50:31 +0200 | [diff] [blame] | 144 | unsigned char *buf, size_t len, size_t *olen, |
| 145 | int (*f_rng)(void *, unsigned char *, size_t), |
| 146 | void *p_rng ); |
Manuel Pégourié-Gonnard | f7368c9 | 2015-08-14 14:33:05 +0200 | [diff] [blame] | 147 | |
| 148 | /** |
| 149 | * \brief Read and process the first round message |
Manuel Pégourié-Gonnard | d8204a7 | 2015-08-14 13:36:55 +0200 | [diff] [blame] | 150 | * (TLS: contents of the Client/ServerHello extension, |
| 151 | * excluding extension type and length bytes) |
Manuel Pégourié-Gonnard | 4e8bc78 | 2015-08-12 20:50:31 +0200 | [diff] [blame] | 152 | * |
| 153 | * \param ctx Context to use |
| 154 | * \param buf Pointer to extension contents |
| 155 | * \param len Extension length |
| 156 | * |
| 157 | * \return 0 if successfull, |
| 158 | * a negative error code otherwise |
| 159 | */ |
Manuel Pégourié-Gonnard | d8204a7 | 2015-08-14 13:36:55 +0200 | [diff] [blame] | 160 | int mbedtls_ecjpake_read_round_one( mbedtls_ecjpake_context *ctx, |
| 161 | const unsigned char *buf, |
| 162 | size_t len ); |
Manuel Pégourié-Gonnard | 4e8bc78 | 2015-08-12 20:50:31 +0200 | [diff] [blame] | 163 | |
Manuel Pégourié-Gonnard | f7368c9 | 2015-08-14 14:33:05 +0200 | [diff] [blame] | 164 | /** |
| 165 | * \brief Generate and write the second round message |
| 166 | * (TLS: contents of the Client/ServerKeyExchange) |
Manuel Pégourié-Gonnard | 614bd5e | 2015-08-13 20:19:16 +0200 | [diff] [blame] | 167 | * |
| 168 | * \param ctx Context to use |
| 169 | * \param buf Buffer to write the contents to |
| 170 | * \param len Buffer size |
| 171 | * \param olen Will be updated with the number of bytes written |
| 172 | * \param f_rng RNG function |
| 173 | * \param p_rng RNG parameter |
| 174 | * |
| 175 | * \return 0 if successfull, |
| 176 | * a negative error code otherwise |
| 177 | */ |
Manuel Pégourié-Gonnard | e192710 | 2015-08-14 14:20:48 +0200 | [diff] [blame] | 178 | int mbedtls_ecjpake_write_round_two( mbedtls_ecjpake_context *ctx, |
Manuel Pégourié-Gonnard | 614bd5e | 2015-08-13 20:19:16 +0200 | [diff] [blame] | 179 | unsigned char *buf, size_t len, size_t *olen, |
| 180 | int (*f_rng)(void *, unsigned char *, size_t), |
| 181 | void *p_rng ); |
| 182 | |
Manuel Pégourié-Gonnard | f7368c9 | 2015-08-14 14:33:05 +0200 | [diff] [blame] | 183 | /** |
| 184 | * \brief Read and process the second round message |
| 185 | * (TLS: contents of the Client/ServerKeyExchange) |
Manuel Pégourié-Gonnard | ec0eece | 2015-08-13 19:13:20 +0200 | [diff] [blame] | 186 | * |
| 187 | * \param ctx Context to use |
| 188 | * \param buf Pointer to the message |
| 189 | * \param len Message length |
| 190 | * |
| 191 | * \return 0 if successfull, |
| 192 | * a negative error code otherwise |
| 193 | */ |
Manuel Pégourié-Gonnard | e192710 | 2015-08-14 14:20:48 +0200 | [diff] [blame] | 194 | int mbedtls_ecjpake_read_round_two( mbedtls_ecjpake_context *ctx, |
Manuel Pégourié-Gonnard | f7368c9 | 2015-08-14 14:33:05 +0200 | [diff] [blame] | 195 | const unsigned char *buf, |
| 196 | size_t len ); |
Manuel Pégourié-Gonnard | ec0eece | 2015-08-13 19:13:20 +0200 | [diff] [blame] | 197 | |
Manuel Pégourié-Gonnard | f7368c9 | 2015-08-14 14:33:05 +0200 | [diff] [blame] | 198 | /** |
| 199 | * \brief Derive the shared secret |
| 200 | * (TLS: Pre-Master Secret) |
Manuel Pégourié-Gonnard | 5f18829 | 2015-08-14 10:52:39 +0200 | [diff] [blame] | 201 | * |
Manuel Pégourié-Gonnard | 55f3d84 | 2015-08-14 15:08:43 +0200 | [diff] [blame] | 202 | * \param ctx Context to use |
Manuel Pégourié-Gonnard | 5f18829 | 2015-08-14 10:52:39 +0200 | [diff] [blame] | 203 | * \param buf Buffer to write the contents to |
| 204 | * \param len Buffer size |
| 205 | * \param olen Will be updated with the number of bytes written |
| 206 | * \param f_rng RNG function |
| 207 | * \param p_rng RNG parameter |
| 208 | * |
| 209 | * \return 0 if successfull, |
| 210 | * a negative error code otherwise |
| 211 | */ |
Manuel Pégourié-Gonnard | f7368c9 | 2015-08-14 14:33:05 +0200 | [diff] [blame] | 212 | int mbedtls_ecjpake_derive_secret( mbedtls_ecjpake_context *ctx, |
Manuel Pégourié-Gonnard | 5f18829 | 2015-08-14 10:52:39 +0200 | [diff] [blame] | 213 | unsigned char *buf, size_t len, size_t *olen, |
| 214 | int (*f_rng)(void *, unsigned char *, size_t), |
| 215 | void *p_rng ); |
| 216 | |
Manuel Pégourié-Gonnard | f7368c9 | 2015-08-14 14:33:05 +0200 | [diff] [blame] | 217 | /** |
Manuel Pégourié-Gonnard | 4e8bc78 | 2015-08-12 20:50:31 +0200 | [diff] [blame] | 218 | * \brief Free a context's content |
| 219 | * |
| 220 | * \param ctx context to free |
| 221 | */ |
| 222 | void mbedtls_ecjpake_free( mbedtls_ecjpake_context *ctx ); |
| 223 | |
Manuel Pégourié-Gonnard | 4d8685b | 2015-08-05 15:44:42 +0200 | [diff] [blame] | 224 | #if defined(MBEDTLS_SELF_TEST) |
| 225 | /** |
| 226 | * \brief Checkup routine |
| 227 | * |
| 228 | * \return 0 if successful, or 1 if a test failed |
| 229 | */ |
| 230 | int mbedtls_ecjpake_self_test( int verbose ); |
| 231 | #endif |
| 232 | |
| 233 | #ifdef __cplusplus |
| 234 | } |
| 235 | #endif |
| 236 | |
| 237 | #endif /* ecjpake.h */ |