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 | /* |
| 27 | * Implementation based on Chapter 7.4 of the Thread v1.0 Specification, |
| 28 | * available from the Thread Group http://threadgroup.org/ |
| 29 | * |
| 30 | * This file implements the EC J-PAKE algorithm, with payload serializations |
| 31 | * suitable for use in TLS, but the result could be used outside TLS. |
| 32 | */ |
| 33 | |
Manuel Pégourié-Gonnard | 4d8685b | 2015-08-05 15:44:42 +0200 | [diff] [blame] | 34 | #include "ecp.h" |
| 35 | #include "md.h" |
| 36 | |
| 37 | #ifdef __cplusplus |
| 38 | extern "C" { |
| 39 | #endif |
| 40 | |
Manuel Pégourié-Gonnard | 6b798b9 | 2015-08-14 11:18:30 +0200 | [diff] [blame] | 41 | /** |
| 42 | * Roles in the EC J-PAKE exchange |
| 43 | */ |
Manuel Pégourié-Gonnard | 6449391 | 2015-08-13 20:19:51 +0200 | [diff] [blame] | 44 | typedef enum { |
Manuel Pégourié-Gonnard | 6b798b9 | 2015-08-14 11:18:30 +0200 | [diff] [blame] | 45 | MBEDTLS_ECJPAKE_CLIENT = 0, /**< Client */ |
| 46 | MBEDTLS_ECJPAKE_SERVER, /**< Server */ |
Manuel Pégourié-Gonnard | 6449391 | 2015-08-13 20:19:51 +0200 | [diff] [blame] | 47 | } mbedtls_ecjpake_role; |
| 48 | |
Manuel Pégourié-Gonnard | 6b798b9 | 2015-08-14 11:18:30 +0200 | [diff] [blame] | 49 | /** |
Manuel Pégourié-Gonnard | ce45676 | 2015-08-14 11:54:35 +0200 | [diff] [blame] | 50 | * EC J-PAKE context structure. |
| 51 | * |
| 52 | * J-PAKE is a symmetric protocol, except for the identifiers used in |
| 53 | * Zero-Knowledge Proofs, and the serialization of the second message |
| 54 | * (KeyExchange) as defined by the Thread spec. |
| 55 | * |
| 56 | * In order to benefit from this symmetry, we choose a different naming |
| 57 | * convetion from the Thread v1.0 spec. Correspondance is indicated in the |
| 58 | * description as a pair C: <client name>, S: <server name> |
Manuel Pégourié-Gonnard | 6b798b9 | 2015-08-14 11:18:30 +0200 | [diff] [blame] | 59 | */ |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 60 | typedef struct |
| 61 | { |
| 62 | const mbedtls_md_info_t *md_info; /**< Hash to use */ |
| 63 | mbedtls_ecp_group grp; /**< Elliptic curve */ |
Manuel Pégourié-Gonnard | 6449391 | 2015-08-13 20:19:51 +0200 | [diff] [blame] | 64 | mbedtls_ecjpake_role role; /**< Are we client or server? */ |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 65 | |
Manuel Pégourié-Gonnard | ce45676 | 2015-08-14 11:54:35 +0200 | [diff] [blame] | 66 | mbedtls_ecp_point Xm1; /**< My public key 1 C: X1, S: X3 */ |
| 67 | mbedtls_ecp_point Xm2; /**< My public key 2 C: X2, S: X4 */ |
| 68 | mbedtls_ecp_point Xp1; /**< Peer public key 1 C: X3, S: X1 */ |
| 69 | mbedtls_ecp_point Xp2; /**< Peer public key 2 C: X4, S: X2 */ |
| 70 | 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] | 71 | |
Manuel Pégourié-Gonnard | ce45676 | 2015-08-14 11:54:35 +0200 | [diff] [blame] | 72 | mbedtls_mpi xm1; /**< My private key 1 C: x1, S: x3 */ |
| 73 | 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] | 74 | |
Manuel Pégourié-Gonnard | 6b798b9 | 2015-08-14 11:18:30 +0200 | [diff] [blame] | 75 | mbedtls_mpi s; /**< Pre-shared secret (passphrase) */ |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 76 | } mbedtls_ecjpake_context; |
| 77 | |
| 78 | /* |
| 79 | * \brief Initialize a context |
| 80 | * (just makes it ready for setup() or free()). |
| 81 | * |
| 82 | * \param ctx context to initialize |
| 83 | */ |
| 84 | void mbedtls_ecjpake_init( mbedtls_ecjpake_context *ctx ); |
| 85 | |
| 86 | /* |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 87 | * \brief Set up a context for use |
| 88 | * |
| 89 | * \note Currently the only values for hash/curve allowed by the |
| 90 | * standard are MBEDTLS_MD_SHA256/MBEDTLS_ECP_DP_SECP256R1. |
| 91 | * |
| 92 | * \param ctx context to set up |
Manuel Pégourié-Gonnard | 6449391 | 2015-08-13 20:19:51 +0200 | [diff] [blame] | 93 | * \param role Our role: client or server |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 94 | * \param hash hash function to use (MBEDTLS_MD_XXX) |
| 95 | * \param curve elliptic curve identifier (MBEDTLS_ECP_DP_XXX) |
Manuel Pégourié-Gonnard | 6b798b9 | 2015-08-14 11:18:30 +0200 | [diff] [blame] | 96 | * \param secret pre-shared secret (passphrase) |
Manuel Pégourié-Gonnard | 23dcbe3 | 2015-08-13 09:37:00 +0200 | [diff] [blame] | 97 | * \param len length of the shared secret |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 98 | * |
| 99 | * \return 0 if successfull, |
| 100 | * a negative error code otherwise |
| 101 | */ |
| 102 | int mbedtls_ecjpake_setup( mbedtls_ecjpake_context *ctx, |
Manuel Pégourié-Gonnard | 6449391 | 2015-08-13 20:19:51 +0200 | [diff] [blame] | 103 | mbedtls_ecjpake_role role, |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 104 | mbedtls_md_type_t hash, |
Manuel Pégourié-Gonnard | 23dcbe3 | 2015-08-13 09:37:00 +0200 | [diff] [blame] | 105 | mbedtls_ecp_group_id curve, |
| 106 | const unsigned char *secret, |
| 107 | size_t len ); |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 108 | |
Manuel Pégourié-Gonnard | 4e8bc78 | 2015-08-12 20:50:31 +0200 | [diff] [blame] | 109 | /* |
| 110 | * \brief Generate and write contents of ClientHello extension |
| 111 | * (excluding extension type and length bytes) |
| 112 | * |
| 113 | * \param ctx Context to use |
| 114 | * \param buf Buffer to write the contents to |
| 115 | * \param len Buffer size |
| 116 | * \param olen Will be updated with the number of bytes written |
| 117 | * \param f_rng RNG function |
| 118 | * \param p_rng RNG parameter |
| 119 | * |
| 120 | * \return 0 if successfull, |
| 121 | * a negative error code otherwise |
| 122 | */ |
| 123 | int mbedtls_ecjpake_tls_write_client_ext( mbedtls_ecjpake_context *ctx, |
| 124 | unsigned char *buf, size_t len, size_t *olen, |
| 125 | int (*f_rng)(void *, unsigned char *, size_t), |
| 126 | void *p_rng ); |
| 127 | /* |
| 128 | * \brief Read and process contents of the ClientHello extension |
| 129 | * (excluding extension type and length bytes) |
| 130 | * |
| 131 | * \param ctx Context to use |
| 132 | * \param buf Pointer to extension contents |
| 133 | * \param len Extension length |
| 134 | * |
| 135 | * \return 0 if successfull, |
| 136 | * a negative error code otherwise |
| 137 | */ |
| 138 | int mbedtls_ecjpake_tls_read_client_ext( mbedtls_ecjpake_context *ctx, |
| 139 | const unsigned char *buf, |
| 140 | size_t len ); |
| 141 | |
| 142 | /* |
| 143 | * \brief Generate and write contents of ServerHello extension |
| 144 | * (excluding extension type and length bytes) |
| 145 | * |
| 146 | * \param ctx Context to use |
| 147 | * \param buf Buffer to write the contents to |
| 148 | * \param len Buffer size |
| 149 | * \param olen Will be updated with the number of bytes written |
| 150 | * \param f_rng RNG function |
| 151 | * \param p_rng RNG parameter |
| 152 | * |
| 153 | * \return 0 if successfull, |
| 154 | * a negative error code otherwise |
| 155 | */ |
| 156 | int mbedtls_ecjpake_tls_write_server_ext( mbedtls_ecjpake_context *ctx, |
| 157 | unsigned char *buf, size_t len, size_t *olen, |
| 158 | int (*f_rng)(void *, unsigned char *, size_t), |
| 159 | void *p_rng ); |
Manuel Pégourié-Gonnard | cb7cd03 | 2015-08-13 10:09:10 +0200 | [diff] [blame] | 160 | |
Manuel Pégourié-Gonnard | 4e8bc78 | 2015-08-12 20:50:31 +0200 | [diff] [blame] | 161 | /* |
| 162 | * \brief Read and process contents of the ServerHello extension |
| 163 | * (excluding extension type and length bytes) |
| 164 | * |
| 165 | * \param ctx Context to use |
| 166 | * \param buf Pointer to extension contents |
| 167 | * \param len Extension length |
| 168 | * |
| 169 | * \return 0 if successfull, |
| 170 | * a negative error code otherwise |
| 171 | */ |
| 172 | int mbedtls_ecjpake_tls_read_server_ext( mbedtls_ecjpake_context *ctx, |
| 173 | const unsigned char *buf, |
| 174 | size_t len ); |
| 175 | |
| 176 | /* |
Manuel Pégourié-Gonnard | cb7cd03 | 2015-08-13 10:09:10 +0200 | [diff] [blame] | 177 | * \brief Generate and write ServerECJPAKEParams |
| 178 | * (the contents for the ServerKeyExchange) |
| 179 | * |
| 180 | * \param ctx Context to use |
| 181 | * \param buf Buffer to write the contents to |
| 182 | * \param len Buffer size |
| 183 | * \param olen Will be updated with the number of bytes written |
| 184 | * \param f_rng RNG function |
| 185 | * \param p_rng RNG parameter |
| 186 | * |
| 187 | * \return 0 if successfull, |
| 188 | * a negative error code otherwise |
| 189 | */ |
| 190 | int mbedtls_ecjpake_tls_write_server_params( mbedtls_ecjpake_context *ctx, |
| 191 | unsigned char *buf, size_t len, size_t *olen, |
| 192 | int (*f_rng)(void *, unsigned char *, size_t), |
| 193 | void *p_rng ); |
| 194 | |
| 195 | /* |
| 196 | * \brief Read and process ServerECJPAKEParams |
| 197 | * (the contents for the ServerKeyExchange) |
| 198 | * |
| 199 | * \param ctx Context to use |
| 200 | * \param buf Pointer to the message |
| 201 | * \param len Message length |
| 202 | * |
| 203 | * \return 0 if successfull, |
| 204 | * a negative error code otherwise |
| 205 | */ |
| 206 | int mbedtls_ecjpake_tls_read_server_params( mbedtls_ecjpake_context *ctx, |
| 207 | const unsigned char *buf, |
| 208 | size_t len ); |
| 209 | |
| 210 | /* |
Manuel Pégourié-Gonnard | 614bd5e | 2015-08-13 20:19:16 +0200 | [diff] [blame] | 211 | * \brief Generate and write ClientECJPAKEParams |
| 212 | * (the contents for the ClientKeyExchange) |
| 213 | * |
| 214 | * \param ctx Context to use |
| 215 | * \param buf Buffer to write the contents to |
| 216 | * \param len Buffer size |
| 217 | * \param olen Will be updated with the number of bytes written |
| 218 | * \param f_rng RNG function |
| 219 | * \param p_rng RNG parameter |
| 220 | * |
| 221 | * \return 0 if successfull, |
| 222 | * a negative error code otherwise |
| 223 | */ |
| 224 | int mbedtls_ecjpake_tls_write_client_params( mbedtls_ecjpake_context *ctx, |
| 225 | unsigned char *buf, size_t len, size_t *olen, |
| 226 | int (*f_rng)(void *, unsigned char *, size_t), |
| 227 | void *p_rng ); |
| 228 | |
| 229 | /* |
Manuel Pégourié-Gonnard | ec0eece | 2015-08-13 19:13:20 +0200 | [diff] [blame] | 230 | * \brief Read and process ClientECJPAKEParams |
| 231 | * (the contents for the ClientKeyExchange) |
| 232 | * |
| 233 | * \param ctx Context to use |
| 234 | * \param buf Pointer to the message |
| 235 | * \param len Message length |
| 236 | * |
| 237 | * \return 0 if successfull, |
| 238 | * a negative error code otherwise |
| 239 | */ |
| 240 | int mbedtls_ecjpake_tls_read_client_params( mbedtls_ecjpake_context *ctx, |
| 241 | const unsigned char *buf, |
| 242 | size_t len ); |
| 243 | |
| 244 | /* |
Manuel Pégourié-Gonnard | 5f18829 | 2015-08-14 10:52:39 +0200 | [diff] [blame] | 245 | * \brief Derive the Pre-Master Secret used by TLS |
| 246 | * |
| 247 | * \param ctx |
| 248 | * \param buf Buffer to write the contents to |
| 249 | * \param len Buffer size |
| 250 | * \param olen Will be updated with the number of bytes written |
| 251 | * \param f_rng RNG function |
| 252 | * \param p_rng RNG parameter |
| 253 | * |
| 254 | * \return 0 if successfull, |
| 255 | * a negative error code otherwise |
| 256 | */ |
| 257 | int mbedtls_ecjpake_tls_derive_pms( mbedtls_ecjpake_context *ctx, |
| 258 | unsigned char *buf, size_t len, size_t *olen, |
| 259 | int (*f_rng)(void *, unsigned char *, size_t), |
| 260 | void *p_rng ); |
| 261 | |
| 262 | /* |
Manuel Pégourié-Gonnard | 4e8bc78 | 2015-08-12 20:50:31 +0200 | [diff] [blame] | 263 | * \brief Free a context's content |
| 264 | * |
| 265 | * \param ctx context to free |
| 266 | */ |
| 267 | void mbedtls_ecjpake_free( mbedtls_ecjpake_context *ctx ); |
| 268 | |
Manuel Pégourié-Gonnard | 4d8685b | 2015-08-05 15:44:42 +0200 | [diff] [blame] | 269 | #if defined(MBEDTLS_SELF_TEST) |
| 270 | /** |
| 271 | * \brief Checkup routine |
| 272 | * |
| 273 | * \return 0 if successful, or 1 if a test failed |
| 274 | */ |
| 275 | int mbedtls_ecjpake_self_test( int verbose ); |
| 276 | #endif |
| 277 | |
| 278 | #ifdef __cplusplus |
| 279 | } |
| 280 | #endif |
| 281 | |
| 282 | #endif /* ecjpake.h */ |