mbed client on ethernet with LWIP
Dependencies: mbed Socket lwip-eth lwip-sys lwip
Fork of mbed-client-classic-example-lwip by
mbedtls/mbedtls/ecjpake.h@11:cada08fc8a70, 2016-06-09 (annotated)
- Committer:
- mbedAustin
- Date:
- Thu Jun 09 17:08:36 2016 +0000
- Revision:
- 11:cada08fc8a70
Commit for public Consumption
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbedAustin | 11:cada08fc8a70 | 1 | /** |
mbedAustin | 11:cada08fc8a70 | 2 | * \file ecjpake.h |
mbedAustin | 11:cada08fc8a70 | 3 | * |
mbedAustin | 11:cada08fc8a70 | 4 | * \brief Elliptic curve J-PAKE |
mbedAustin | 11:cada08fc8a70 | 5 | * |
mbedAustin | 11:cada08fc8a70 | 6 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
mbedAustin | 11:cada08fc8a70 | 7 | * SPDX-License-Identifier: Apache-2.0 |
mbedAustin | 11:cada08fc8a70 | 8 | * |
mbedAustin | 11:cada08fc8a70 | 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
mbedAustin | 11:cada08fc8a70 | 10 | * not use this file except in compliance with the License. |
mbedAustin | 11:cada08fc8a70 | 11 | * You may obtain a copy of the License at |
mbedAustin | 11:cada08fc8a70 | 12 | * |
mbedAustin | 11:cada08fc8a70 | 13 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbedAustin | 11:cada08fc8a70 | 14 | * |
mbedAustin | 11:cada08fc8a70 | 15 | * Unless required by applicable law or agreed to in writing, software |
mbedAustin | 11:cada08fc8a70 | 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
mbedAustin | 11:cada08fc8a70 | 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mbedAustin | 11:cada08fc8a70 | 18 | * See the License for the specific language governing permissions and |
mbedAustin | 11:cada08fc8a70 | 19 | * limitations under the License. |
mbedAustin | 11:cada08fc8a70 | 20 | * |
mbedAustin | 11:cada08fc8a70 | 21 | * This file is part of mbed TLS (https://tls.mbed.org) |
mbedAustin | 11:cada08fc8a70 | 22 | */ |
mbedAustin | 11:cada08fc8a70 | 23 | #ifndef MBEDTLS_ECJPAKE_H |
mbedAustin | 11:cada08fc8a70 | 24 | #define MBEDTLS_ECJPAKE_H |
mbedAustin | 11:cada08fc8a70 | 25 | |
mbedAustin | 11:cada08fc8a70 | 26 | /* |
mbedAustin | 11:cada08fc8a70 | 27 | * J-PAKE is a password-authenticated key exchange that allows deriving a |
mbedAustin | 11:cada08fc8a70 | 28 | * strong shared secret from a (potentially low entropy) pre-shared |
mbedAustin | 11:cada08fc8a70 | 29 | * passphrase, with forward secrecy and mutual authentication. |
mbedAustin | 11:cada08fc8a70 | 30 | * https://en.wikipedia.org/wiki/Password_Authenticated_Key_Exchange_by_Juggling |
mbedAustin | 11:cada08fc8a70 | 31 | * |
mbedAustin | 11:cada08fc8a70 | 32 | * This file implements the Elliptic Curve variant of J-PAKE, |
mbedAustin | 11:cada08fc8a70 | 33 | * as defined in Chapter 7.4 of the Thread v1.0 Specification, |
mbedAustin | 11:cada08fc8a70 | 34 | * available to members of the Thread Group http://threadgroup.org/ |
mbedAustin | 11:cada08fc8a70 | 35 | * |
mbedAustin | 11:cada08fc8a70 | 36 | * As the J-PAKE algorithm is inherently symmetric, so is our API. |
mbedAustin | 11:cada08fc8a70 | 37 | * Each party needs to send its first round message, in any order, to the |
mbedAustin | 11:cada08fc8a70 | 38 | * other party, then each sends its second round message, in any order. |
mbedAustin | 11:cada08fc8a70 | 39 | * The payloads are serialized in a way suitable for use in TLS, but could |
mbedAustin | 11:cada08fc8a70 | 40 | * also be use outside TLS. |
mbedAustin | 11:cada08fc8a70 | 41 | */ |
mbedAustin | 11:cada08fc8a70 | 42 | |
mbedAustin | 11:cada08fc8a70 | 43 | #include "ecp.h" |
mbedAustin | 11:cada08fc8a70 | 44 | #include "md.h" |
mbedAustin | 11:cada08fc8a70 | 45 | |
mbedAustin | 11:cada08fc8a70 | 46 | #ifdef __cplusplus |
mbedAustin | 11:cada08fc8a70 | 47 | extern "C" { |
mbedAustin | 11:cada08fc8a70 | 48 | #endif |
mbedAustin | 11:cada08fc8a70 | 49 | |
mbedAustin | 11:cada08fc8a70 | 50 | /** |
mbedAustin | 11:cada08fc8a70 | 51 | * Roles in the EC J-PAKE exchange |
mbedAustin | 11:cada08fc8a70 | 52 | */ |
mbedAustin | 11:cada08fc8a70 | 53 | typedef enum { |
mbedAustin | 11:cada08fc8a70 | 54 | MBEDTLS_ECJPAKE_CLIENT = 0, /**< Client */ |
mbedAustin | 11:cada08fc8a70 | 55 | MBEDTLS_ECJPAKE_SERVER, /**< Server */ |
mbedAustin | 11:cada08fc8a70 | 56 | } mbedtls_ecjpake_role; |
mbedAustin | 11:cada08fc8a70 | 57 | |
mbedAustin | 11:cada08fc8a70 | 58 | /** |
mbedAustin | 11:cada08fc8a70 | 59 | * EC J-PAKE context structure. |
mbedAustin | 11:cada08fc8a70 | 60 | * |
mbedAustin | 11:cada08fc8a70 | 61 | * J-PAKE is a symmetric protocol, except for the identifiers used in |
mbedAustin | 11:cada08fc8a70 | 62 | * Zero-Knowledge Proofs, and the serialization of the second message |
mbedAustin | 11:cada08fc8a70 | 63 | * (KeyExchange) as defined by the Thread spec. |
mbedAustin | 11:cada08fc8a70 | 64 | * |
mbedAustin | 11:cada08fc8a70 | 65 | * In order to benefit from this symmetry, we choose a different naming |
mbedAustin | 11:cada08fc8a70 | 66 | * convetion from the Thread v1.0 spec. Correspondance is indicated in the |
mbedAustin | 11:cada08fc8a70 | 67 | * description as a pair C: <client name>, S: <server name> |
mbedAustin | 11:cada08fc8a70 | 68 | */ |
mbedAustin | 11:cada08fc8a70 | 69 | typedef struct |
mbedAustin | 11:cada08fc8a70 | 70 | { |
mbedAustin | 11:cada08fc8a70 | 71 | const mbedtls_md_info_t *md_info; /**< Hash to use */ |
mbedAustin | 11:cada08fc8a70 | 72 | mbedtls_ecp_group grp; /**< Elliptic curve */ |
mbedAustin | 11:cada08fc8a70 | 73 | mbedtls_ecjpake_role role; /**< Are we client or server? */ |
mbedAustin | 11:cada08fc8a70 | 74 | int point_format; /**< Format for point export */ |
mbedAustin | 11:cada08fc8a70 | 75 | |
mbedAustin | 11:cada08fc8a70 | 76 | mbedtls_ecp_point Xm1; /**< My public key 1 C: X1, S: X3 */ |
mbedAustin | 11:cada08fc8a70 | 77 | mbedtls_ecp_point Xm2; /**< My public key 2 C: X2, S: X4 */ |
mbedAustin | 11:cada08fc8a70 | 78 | mbedtls_ecp_point Xp1; /**< Peer public key 1 C: X3, S: X1 */ |
mbedAustin | 11:cada08fc8a70 | 79 | mbedtls_ecp_point Xp2; /**< Peer public key 2 C: X4, S: X2 */ |
mbedAustin | 11:cada08fc8a70 | 80 | mbedtls_ecp_point Xp; /**< Peer public key C: Xs, S: Xc */ |
mbedAustin | 11:cada08fc8a70 | 81 | |
mbedAustin | 11:cada08fc8a70 | 82 | mbedtls_mpi xm1; /**< My private key 1 C: x1, S: x3 */ |
mbedAustin | 11:cada08fc8a70 | 83 | mbedtls_mpi xm2; /**< My private key 2 C: x2, S: x4 */ |
mbedAustin | 11:cada08fc8a70 | 84 | |
mbedAustin | 11:cada08fc8a70 | 85 | mbedtls_mpi s; /**< Pre-shared secret (passphrase) */ |
mbedAustin | 11:cada08fc8a70 | 86 | } mbedtls_ecjpake_context; |
mbedAustin | 11:cada08fc8a70 | 87 | |
mbedAustin | 11:cada08fc8a70 | 88 | /** |
mbedAustin | 11:cada08fc8a70 | 89 | * \brief Initialize a context |
mbedAustin | 11:cada08fc8a70 | 90 | * (just makes it ready for setup() or free()). |
mbedAustin | 11:cada08fc8a70 | 91 | * |
mbedAustin | 11:cada08fc8a70 | 92 | * \param ctx context to initialize |
mbedAustin | 11:cada08fc8a70 | 93 | */ |
mbedAustin | 11:cada08fc8a70 | 94 | void mbedtls_ecjpake_init( mbedtls_ecjpake_context *ctx ); |
mbedAustin | 11:cada08fc8a70 | 95 | |
mbedAustin | 11:cada08fc8a70 | 96 | /** |
mbedAustin | 11:cada08fc8a70 | 97 | * \brief Set up a context for use |
mbedAustin | 11:cada08fc8a70 | 98 | * |
mbedAustin | 11:cada08fc8a70 | 99 | * \note Currently the only values for hash/curve allowed by the |
mbedAustin | 11:cada08fc8a70 | 100 | * standard are MBEDTLS_MD_SHA256/MBEDTLS_ECP_DP_SECP256R1. |
mbedAustin | 11:cada08fc8a70 | 101 | * |
mbedAustin | 11:cada08fc8a70 | 102 | * \param ctx context to set up |
mbedAustin | 11:cada08fc8a70 | 103 | * \param role Our role: client or server |
mbedAustin | 11:cada08fc8a70 | 104 | * \param hash hash function to use (MBEDTLS_MD_XXX) |
mbedAustin | 11:cada08fc8a70 | 105 | * \param curve elliptic curve identifier (MBEDTLS_ECP_DP_XXX) |
mbedAustin | 11:cada08fc8a70 | 106 | * \param secret pre-shared secret (passphrase) |
mbedAustin | 11:cada08fc8a70 | 107 | * \param len length of the shared secret |
mbedAustin | 11:cada08fc8a70 | 108 | * |
mbedAustin | 11:cada08fc8a70 | 109 | * \return 0 if successfull, |
mbedAustin | 11:cada08fc8a70 | 110 | * a negative error code otherwise |
mbedAustin | 11:cada08fc8a70 | 111 | */ |
mbedAustin | 11:cada08fc8a70 | 112 | int mbedtls_ecjpake_setup( mbedtls_ecjpake_context *ctx, |
mbedAustin | 11:cada08fc8a70 | 113 | mbedtls_ecjpake_role role, |
mbedAustin | 11:cada08fc8a70 | 114 | mbedtls_md_type_t hash, |
mbedAustin | 11:cada08fc8a70 | 115 | mbedtls_ecp_group_id curve, |
mbedAustin | 11:cada08fc8a70 | 116 | const unsigned char *secret, |
mbedAustin | 11:cada08fc8a70 | 117 | size_t len ); |
mbedAustin | 11:cada08fc8a70 | 118 | |
mbedAustin | 11:cada08fc8a70 | 119 | /* |
mbedAustin | 11:cada08fc8a70 | 120 | * \brief Check if a context is ready for use |
mbedAustin | 11:cada08fc8a70 | 121 | * |
mbedAustin | 11:cada08fc8a70 | 122 | * \param ctx Context to check |
mbedAustin | 11:cada08fc8a70 | 123 | * |
mbedAustin | 11:cada08fc8a70 | 124 | * \return 0 if the context is ready for use, |
mbedAustin | 11:cada08fc8a70 | 125 | * MBEDTLS_ERR_ECP_BAD_INPUT_DATA otherwise |
mbedAustin | 11:cada08fc8a70 | 126 | */ |
mbedAustin | 11:cada08fc8a70 | 127 | int mbedtls_ecjpake_check( const mbedtls_ecjpake_context *ctx ); |
mbedAustin | 11:cada08fc8a70 | 128 | |
mbedAustin | 11:cada08fc8a70 | 129 | /** |
mbedAustin | 11:cada08fc8a70 | 130 | * \brief Generate and write the first round message |
mbedAustin | 11:cada08fc8a70 | 131 | * (TLS: contents of the Client/ServerHello extension, |
mbedAustin | 11:cada08fc8a70 | 132 | * excluding extension type and length bytes) |
mbedAustin | 11:cada08fc8a70 | 133 | * |
mbedAustin | 11:cada08fc8a70 | 134 | * \param ctx Context to use |
mbedAustin | 11:cada08fc8a70 | 135 | * \param buf Buffer to write the contents to |
mbedAustin | 11:cada08fc8a70 | 136 | * \param len Buffer size |
mbedAustin | 11:cada08fc8a70 | 137 | * \param olen Will be updated with the number of bytes written |
mbedAustin | 11:cada08fc8a70 | 138 | * \param f_rng RNG function |
mbedAustin | 11:cada08fc8a70 | 139 | * \param p_rng RNG parameter |
mbedAustin | 11:cada08fc8a70 | 140 | * |
mbedAustin | 11:cada08fc8a70 | 141 | * \return 0 if successfull, |
mbedAustin | 11:cada08fc8a70 | 142 | * a negative error code otherwise |
mbedAustin | 11:cada08fc8a70 | 143 | */ |
mbedAustin | 11:cada08fc8a70 | 144 | int mbedtls_ecjpake_write_round_one( mbedtls_ecjpake_context *ctx, |
mbedAustin | 11:cada08fc8a70 | 145 | unsigned char *buf, size_t len, size_t *olen, |
mbedAustin | 11:cada08fc8a70 | 146 | int (*f_rng)(void *, unsigned char *, size_t), |
mbedAustin | 11:cada08fc8a70 | 147 | void *p_rng ); |
mbedAustin | 11:cada08fc8a70 | 148 | |
mbedAustin | 11:cada08fc8a70 | 149 | /** |
mbedAustin | 11:cada08fc8a70 | 150 | * \brief Read and process the first round message |
mbedAustin | 11:cada08fc8a70 | 151 | * (TLS: contents of the Client/ServerHello extension, |
mbedAustin | 11:cada08fc8a70 | 152 | * excluding extension type and length bytes) |
mbedAustin | 11:cada08fc8a70 | 153 | * |
mbedAustin | 11:cada08fc8a70 | 154 | * \param ctx Context to use |
mbedAustin | 11:cada08fc8a70 | 155 | * \param buf Pointer to extension contents |
mbedAustin | 11:cada08fc8a70 | 156 | * \param len Extension length |
mbedAustin | 11:cada08fc8a70 | 157 | * |
mbedAustin | 11:cada08fc8a70 | 158 | * \return 0 if successfull, |
mbedAustin | 11:cada08fc8a70 | 159 | * a negative error code otherwise |
mbedAustin | 11:cada08fc8a70 | 160 | */ |
mbedAustin | 11:cada08fc8a70 | 161 | int mbedtls_ecjpake_read_round_one( mbedtls_ecjpake_context *ctx, |
mbedAustin | 11:cada08fc8a70 | 162 | const unsigned char *buf, |
mbedAustin | 11:cada08fc8a70 | 163 | size_t len ); |
mbedAustin | 11:cada08fc8a70 | 164 | |
mbedAustin | 11:cada08fc8a70 | 165 | /** |
mbedAustin | 11:cada08fc8a70 | 166 | * \brief Generate and write the second round message |
mbedAustin | 11:cada08fc8a70 | 167 | * (TLS: contents of the Client/ServerKeyExchange) |
mbedAustin | 11:cada08fc8a70 | 168 | * |
mbedAustin | 11:cada08fc8a70 | 169 | * \param ctx Context to use |
mbedAustin | 11:cada08fc8a70 | 170 | * \param buf Buffer to write the contents to |
mbedAustin | 11:cada08fc8a70 | 171 | * \param len Buffer size |
mbedAustin | 11:cada08fc8a70 | 172 | * \param olen Will be updated with the number of bytes written |
mbedAustin | 11:cada08fc8a70 | 173 | * \param f_rng RNG function |
mbedAustin | 11:cada08fc8a70 | 174 | * \param p_rng RNG parameter |
mbedAustin | 11:cada08fc8a70 | 175 | * |
mbedAustin | 11:cada08fc8a70 | 176 | * \return 0 if successfull, |
mbedAustin | 11:cada08fc8a70 | 177 | * a negative error code otherwise |
mbedAustin | 11:cada08fc8a70 | 178 | */ |
mbedAustin | 11:cada08fc8a70 | 179 | int mbedtls_ecjpake_write_round_two( mbedtls_ecjpake_context *ctx, |
mbedAustin | 11:cada08fc8a70 | 180 | unsigned char *buf, size_t len, size_t *olen, |
mbedAustin | 11:cada08fc8a70 | 181 | int (*f_rng)(void *, unsigned char *, size_t), |
mbedAustin | 11:cada08fc8a70 | 182 | void *p_rng ); |
mbedAustin | 11:cada08fc8a70 | 183 | |
mbedAustin | 11:cada08fc8a70 | 184 | /** |
mbedAustin | 11:cada08fc8a70 | 185 | * \brief Read and process the second round message |
mbedAustin | 11:cada08fc8a70 | 186 | * (TLS: contents of the Client/ServerKeyExchange) |
mbedAustin | 11:cada08fc8a70 | 187 | * |
mbedAustin | 11:cada08fc8a70 | 188 | * \param ctx Context to use |
mbedAustin | 11:cada08fc8a70 | 189 | * \param buf Pointer to the message |
mbedAustin | 11:cada08fc8a70 | 190 | * \param len Message length |
mbedAustin | 11:cada08fc8a70 | 191 | * |
mbedAustin | 11:cada08fc8a70 | 192 | * \return 0 if successfull, |
mbedAustin | 11:cada08fc8a70 | 193 | * a negative error code otherwise |
mbedAustin | 11:cada08fc8a70 | 194 | */ |
mbedAustin | 11:cada08fc8a70 | 195 | int mbedtls_ecjpake_read_round_two( mbedtls_ecjpake_context *ctx, |
mbedAustin | 11:cada08fc8a70 | 196 | const unsigned char *buf, |
mbedAustin | 11:cada08fc8a70 | 197 | size_t len ); |
mbedAustin | 11:cada08fc8a70 | 198 | |
mbedAustin | 11:cada08fc8a70 | 199 | /** |
mbedAustin | 11:cada08fc8a70 | 200 | * \brief Derive the shared secret |
mbedAustin | 11:cada08fc8a70 | 201 | * (TLS: Pre-Master Secret) |
mbedAustin | 11:cada08fc8a70 | 202 | * |
mbedAustin | 11:cada08fc8a70 | 203 | * \param ctx Context to use |
mbedAustin | 11:cada08fc8a70 | 204 | * \param buf Buffer to write the contents to |
mbedAustin | 11:cada08fc8a70 | 205 | * \param len Buffer size |
mbedAustin | 11:cada08fc8a70 | 206 | * \param olen Will be updated with the number of bytes written |
mbedAustin | 11:cada08fc8a70 | 207 | * \param f_rng RNG function |
mbedAustin | 11:cada08fc8a70 | 208 | * \param p_rng RNG parameter |
mbedAustin | 11:cada08fc8a70 | 209 | * |
mbedAustin | 11:cada08fc8a70 | 210 | * \return 0 if successfull, |
mbedAustin | 11:cada08fc8a70 | 211 | * a negative error code otherwise |
mbedAustin | 11:cada08fc8a70 | 212 | */ |
mbedAustin | 11:cada08fc8a70 | 213 | int mbedtls_ecjpake_derive_secret( mbedtls_ecjpake_context *ctx, |
mbedAustin | 11:cada08fc8a70 | 214 | unsigned char *buf, size_t len, size_t *olen, |
mbedAustin | 11:cada08fc8a70 | 215 | int (*f_rng)(void *, unsigned char *, size_t), |
mbedAustin | 11:cada08fc8a70 | 216 | void *p_rng ); |
mbedAustin | 11:cada08fc8a70 | 217 | |
mbedAustin | 11:cada08fc8a70 | 218 | /** |
mbedAustin | 11:cada08fc8a70 | 219 | * \brief Free a context's content |
mbedAustin | 11:cada08fc8a70 | 220 | * |
mbedAustin | 11:cada08fc8a70 | 221 | * \param ctx context to free |
mbedAustin | 11:cada08fc8a70 | 222 | */ |
mbedAustin | 11:cada08fc8a70 | 223 | void mbedtls_ecjpake_free( mbedtls_ecjpake_context *ctx ); |
mbedAustin | 11:cada08fc8a70 | 224 | |
mbedAustin | 11:cada08fc8a70 | 225 | #if defined(MBEDTLS_SELF_TEST) |
mbedAustin | 11:cada08fc8a70 | 226 | /** |
mbedAustin | 11:cada08fc8a70 | 227 | * \brief Checkup routine |
mbedAustin | 11:cada08fc8a70 | 228 | * |
mbedAustin | 11:cada08fc8a70 | 229 | * \return 0 if successful, or 1 if a test failed |
mbedAustin | 11:cada08fc8a70 | 230 | */ |
mbedAustin | 11:cada08fc8a70 | 231 | int mbedtls_ecjpake_self_test( int verbose ); |
mbedAustin | 11:cada08fc8a70 | 232 | #endif |
mbedAustin | 11:cada08fc8a70 | 233 | |
mbedAustin | 11:cada08fc8a70 | 234 | #ifdef __cplusplus |
mbedAustin | 11:cada08fc8a70 | 235 | } |
mbedAustin | 11:cada08fc8a70 | 236 | #endif |
mbedAustin | 11:cada08fc8a70 | 237 | |
mbedAustin | 11:cada08fc8a70 | 238 | #endif /* ecjpake.h */ |