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 | /** |
| 50 | * EC J-PAKE context structure |
| 51 | */ |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 52 | typedef struct |
| 53 | { |
| 54 | const mbedtls_md_info_t *md_info; /**< Hash to use */ |
| 55 | mbedtls_ecp_group grp; /**< Elliptic curve */ |
Manuel Pégourié-Gonnard | 6449391 | 2015-08-13 20:19:51 +0200 | [diff] [blame] | 56 | mbedtls_ecjpake_role role; /**< Are we client or server? */ |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 57 | |
| 58 | mbedtls_ecp_point X1; /**< Public key one */ |
| 59 | mbedtls_ecp_point X2; /**< Public key two */ |
| 60 | mbedtls_ecp_point X3; /**< Public key three */ |
| 61 | mbedtls_ecp_point X4; /**< Public key four */ |
Manuel Pégourié-Gonnard | cb7cd03 | 2015-08-13 10:09:10 +0200 | [diff] [blame] | 62 | mbedtls_ecp_point Xp; /**< Peer's public key (Xs or Xc) */ |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 63 | |
| 64 | mbedtls_mpi xa; /**< Our first secret (x1 or x3) */ |
| 65 | mbedtls_mpi xb; /**< Our second secret (x2 or x4) */ |
Manuel Pégourié-Gonnard | 23dcbe3 | 2015-08-13 09:37:00 +0200 | [diff] [blame] | 66 | |
Manuel Pégourié-Gonnard | 6b798b9 | 2015-08-14 11:18:30 +0200 | [diff] [blame^] | 67 | mbedtls_mpi s; /**< Pre-shared secret (passphrase) */ |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 68 | } mbedtls_ecjpake_context; |
| 69 | |
| 70 | /* |
| 71 | * \brief Initialize a context |
| 72 | * (just makes it ready for setup() or free()). |
| 73 | * |
| 74 | * \param ctx context to initialize |
| 75 | */ |
| 76 | void mbedtls_ecjpake_init( mbedtls_ecjpake_context *ctx ); |
| 77 | |
| 78 | /* |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 79 | * \brief Set up a context for use |
| 80 | * |
| 81 | * \note Currently the only values for hash/curve allowed by the |
| 82 | * standard are MBEDTLS_MD_SHA256/MBEDTLS_ECP_DP_SECP256R1. |
| 83 | * |
| 84 | * \param ctx context to set up |
Manuel Pégourié-Gonnard | 6449391 | 2015-08-13 20:19:51 +0200 | [diff] [blame] | 85 | * \param role Our role: client or server |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 86 | * \param hash hash function to use (MBEDTLS_MD_XXX) |
| 87 | * \param curve elliptic curve identifier (MBEDTLS_ECP_DP_XXX) |
Manuel Pégourié-Gonnard | 6b798b9 | 2015-08-14 11:18:30 +0200 | [diff] [blame^] | 88 | * \param secret pre-shared secret (passphrase) |
Manuel Pégourié-Gonnard | 23dcbe3 | 2015-08-13 09:37:00 +0200 | [diff] [blame] | 89 | * \param len length of the shared secret |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 90 | * |
| 91 | * \return 0 if successfull, |
| 92 | * a negative error code otherwise |
| 93 | */ |
| 94 | int mbedtls_ecjpake_setup( mbedtls_ecjpake_context *ctx, |
Manuel Pégourié-Gonnard | 6449391 | 2015-08-13 20:19:51 +0200 | [diff] [blame] | 95 | mbedtls_ecjpake_role role, |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 96 | mbedtls_md_type_t hash, |
Manuel Pégourié-Gonnard | 23dcbe3 | 2015-08-13 09:37:00 +0200 | [diff] [blame] | 97 | mbedtls_ecp_group_id curve, |
| 98 | const unsigned char *secret, |
| 99 | size_t len ); |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 100 | |
Manuel Pégourié-Gonnard | 4e8bc78 | 2015-08-12 20:50:31 +0200 | [diff] [blame] | 101 | /* |
| 102 | * \brief Generate and write contents of ClientHello extension |
| 103 | * (excluding extension type and length bytes) |
| 104 | * |
| 105 | * \param ctx Context to use |
| 106 | * \param buf Buffer to write the contents to |
| 107 | * \param len Buffer size |
| 108 | * \param olen Will be updated with the number of bytes written |
| 109 | * \param f_rng RNG function |
| 110 | * \param p_rng RNG parameter |
| 111 | * |
| 112 | * \return 0 if successfull, |
| 113 | * a negative error code otherwise |
| 114 | */ |
| 115 | int mbedtls_ecjpake_tls_write_client_ext( mbedtls_ecjpake_context *ctx, |
| 116 | unsigned char *buf, size_t len, size_t *olen, |
| 117 | int (*f_rng)(void *, unsigned char *, size_t), |
| 118 | void *p_rng ); |
| 119 | /* |
| 120 | * \brief Read and process contents of the ClientHello extension |
| 121 | * (excluding extension type and length bytes) |
| 122 | * |
| 123 | * \param ctx Context to use |
| 124 | * \param buf Pointer to extension contents |
| 125 | * \param len Extension length |
| 126 | * |
| 127 | * \return 0 if successfull, |
| 128 | * a negative error code otherwise |
| 129 | */ |
| 130 | int mbedtls_ecjpake_tls_read_client_ext( mbedtls_ecjpake_context *ctx, |
| 131 | const unsigned char *buf, |
| 132 | size_t len ); |
| 133 | |
| 134 | /* |
| 135 | * \brief Generate and write contents of ServerHello extension |
| 136 | * (excluding extension type and length bytes) |
| 137 | * |
| 138 | * \param ctx Context to use |
| 139 | * \param buf Buffer to write the contents to |
| 140 | * \param len Buffer size |
| 141 | * \param olen Will be updated with the number of bytes written |
| 142 | * \param f_rng RNG function |
| 143 | * \param p_rng RNG parameter |
| 144 | * |
| 145 | * \return 0 if successfull, |
| 146 | * a negative error code otherwise |
| 147 | */ |
| 148 | int mbedtls_ecjpake_tls_write_server_ext( mbedtls_ecjpake_context *ctx, |
| 149 | unsigned char *buf, size_t len, size_t *olen, |
| 150 | int (*f_rng)(void *, unsigned char *, size_t), |
| 151 | void *p_rng ); |
Manuel Pégourié-Gonnard | cb7cd03 | 2015-08-13 10:09:10 +0200 | [diff] [blame] | 152 | |
Manuel Pégourié-Gonnard | 4e8bc78 | 2015-08-12 20:50:31 +0200 | [diff] [blame] | 153 | /* |
| 154 | * \brief Read and process contents of the ServerHello extension |
| 155 | * (excluding extension type and length bytes) |
| 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 | */ |
| 164 | int mbedtls_ecjpake_tls_read_server_ext( mbedtls_ecjpake_context *ctx, |
| 165 | const unsigned char *buf, |
| 166 | size_t len ); |
| 167 | |
| 168 | /* |
Manuel Pégourié-Gonnard | cb7cd03 | 2015-08-13 10:09:10 +0200 | [diff] [blame] | 169 | * \brief Generate and write ServerECJPAKEParams |
| 170 | * (the contents for the ServerKeyExchange) |
| 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 | */ |
| 182 | int mbedtls_ecjpake_tls_write_server_params( mbedtls_ecjpake_context *ctx, |
| 183 | unsigned char *buf, size_t len, size_t *olen, |
| 184 | int (*f_rng)(void *, unsigned char *, size_t), |
| 185 | void *p_rng ); |
| 186 | |
| 187 | /* |
| 188 | * \brief Read and process ServerECJPAKEParams |
| 189 | * (the contents for the ServerKeyExchange) |
| 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 | */ |
| 198 | int mbedtls_ecjpake_tls_read_server_params( mbedtls_ecjpake_context *ctx, |
| 199 | const unsigned char *buf, |
| 200 | size_t len ); |
| 201 | |
| 202 | /* |
Manuel Pégourié-Gonnard | 614bd5e | 2015-08-13 20:19:16 +0200 | [diff] [blame] | 203 | * \brief Generate and write ClientECJPAKEParams |
| 204 | * (the contents for the ClientKeyExchange) |
| 205 | * |
| 206 | * \param ctx Context to use |
| 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 | */ |
| 216 | int mbedtls_ecjpake_tls_write_client_params( mbedtls_ecjpake_context *ctx, |
| 217 | unsigned char *buf, size_t len, size_t *olen, |
| 218 | int (*f_rng)(void *, unsigned char *, size_t), |
| 219 | void *p_rng ); |
| 220 | |
| 221 | /* |
Manuel Pégourié-Gonnard | ec0eece | 2015-08-13 19:13:20 +0200 | [diff] [blame] | 222 | * \brief Read and process ClientECJPAKEParams |
| 223 | * (the contents for the ClientKeyExchange) |
| 224 | * |
| 225 | * \param ctx Context to use |
| 226 | * \param buf Pointer to the message |
| 227 | * \param len Message length |
| 228 | * |
| 229 | * \return 0 if successfull, |
| 230 | * a negative error code otherwise |
| 231 | */ |
| 232 | int mbedtls_ecjpake_tls_read_client_params( mbedtls_ecjpake_context *ctx, |
| 233 | const unsigned char *buf, |
| 234 | size_t len ); |
| 235 | |
| 236 | /* |
Manuel Pégourié-Gonnard | 5f18829 | 2015-08-14 10:52:39 +0200 | [diff] [blame] | 237 | * \brief Derive the Pre-Master Secret used by TLS |
| 238 | * |
| 239 | * \param ctx |
| 240 | * \param buf Buffer to write the contents to |
| 241 | * \param len Buffer size |
| 242 | * \param olen Will be updated with the number of bytes written |
| 243 | * \param f_rng RNG function |
| 244 | * \param p_rng RNG parameter |
| 245 | * |
| 246 | * \return 0 if successfull, |
| 247 | * a negative error code otherwise |
| 248 | */ |
| 249 | int mbedtls_ecjpake_tls_derive_pms( mbedtls_ecjpake_context *ctx, |
| 250 | unsigned char *buf, size_t len, size_t *olen, |
| 251 | int (*f_rng)(void *, unsigned char *, size_t), |
| 252 | void *p_rng ); |
| 253 | |
| 254 | /* |
Manuel Pégourié-Gonnard | 4e8bc78 | 2015-08-12 20:50:31 +0200 | [diff] [blame] | 255 | * \brief Free a context's content |
| 256 | * |
| 257 | * \param ctx context to free |
| 258 | */ |
| 259 | void mbedtls_ecjpake_free( mbedtls_ecjpake_context *ctx ); |
| 260 | |
Manuel Pégourié-Gonnard | 4d8685b | 2015-08-05 15:44:42 +0200 | [diff] [blame] | 261 | #if defined(MBEDTLS_SELF_TEST) |
| 262 | /** |
| 263 | * \brief Checkup routine |
| 264 | * |
| 265 | * \return 0 if successful, or 1 if a test failed |
| 266 | */ |
| 267 | int mbedtls_ecjpake_self_test( int verbose ); |
| 268 | #endif |
| 269 | |
| 270 | #ifdef __cplusplus |
| 271 | } |
| 272 | #endif |
| 273 | |
| 274 | #endif /* ecjpake.h */ |