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