ecjpake.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /**
  2. * \file ecjpake.h
  3. *
  4. * \brief Elliptic curve J-PAKE
  5. */
  6. /*
  7. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  8. * SPDX-License-Identifier: Apache-2.0
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  11. * not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  18. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. *
  22. * This file is part of mbed TLS (https://tls.mbed.org)
  23. */
  24. #ifndef MBEDTLS_ECJPAKE_H
  25. #define MBEDTLS_ECJPAKE_H
  26. /*
  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. *
  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/
  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.
  39. * The payloads are serialized in a way suitable for use in TLS, but could
  40. * also be use outside TLS.
  41. */
  42. #include "ecp.h"
  43. #include "md.h"
  44. #ifdef __cplusplus
  45. extern "C" {
  46. #endif
  47. /**
  48. * Roles in the EC J-PAKE exchange
  49. */
  50. typedef enum {
  51. MBEDTLS_ECJPAKE_CLIENT = 0, /**< Client */
  52. MBEDTLS_ECJPAKE_SERVER, /**< Server */
  53. } mbedtls_ecjpake_role;
  54. #if !defined(MBEDTLS_ECJPAKE_ALT)
  55. /**
  56. * EC J-PAKE context structure.
  57. *
  58. * J-PAKE is a symmetric protocol, except for the identifiers used in
  59. * Zero-Knowledge Proofs, and the serialization of the second message
  60. * (KeyExchange) as defined by the Thread spec.
  61. *
  62. * In order to benefit from this symmetry, we choose a different naming
  63. * convetion from the Thread v1.0 spec. Correspondance is indicated in the
  64. * description as a pair C: client name, S: server name
  65. */
  66. typedef struct
  67. {
  68. const mbedtls_md_info_t *md_info; /**< Hash to use */
  69. mbedtls_ecp_group grp; /**< Elliptic curve */
  70. mbedtls_ecjpake_role role; /**< Are we client or server? */
  71. int point_format; /**< Format for point export */
  72. mbedtls_ecp_point Xm1; /**< My public key 1 C: X1, S: X3 */
  73. mbedtls_ecp_point Xm2; /**< My public key 2 C: X2, S: X4 */
  74. mbedtls_ecp_point Xp1; /**< Peer public key 1 C: X3, S: X1 */
  75. mbedtls_ecp_point Xp2; /**< Peer public key 2 C: X4, S: X2 */
  76. mbedtls_ecp_point Xp; /**< Peer public key C: Xs, S: Xc */
  77. mbedtls_mpi xm1; /**< My private key 1 C: x1, S: x3 */
  78. mbedtls_mpi xm2; /**< My private key 2 C: x2, S: x4 */
  79. mbedtls_mpi s; /**< Pre-shared secret (passphrase) */
  80. } mbedtls_ecjpake_context;
  81. #else /* MBEDTLS_ECJPAKE_ALT */
  82. #include "ecjpake_alt.h"
  83. #endif /* MBEDTLS_ECJPAKE_ALT */
  84. /**
  85. * \brief Initialize a context
  86. * (just makes it ready for setup() or free()).
  87. *
  88. * \param ctx context to initialize
  89. */
  90. void mbedtls_ecjpake_init( mbedtls_ecjpake_context *ctx );
  91. /**
  92. * \brief Set up a context for use
  93. *
  94. * \note Currently the only values for hash/curve allowed by the
  95. * standard are MBEDTLS_MD_SHA256/MBEDTLS_ECP_DP_SECP256R1.
  96. *
  97. * \param ctx context to set up
  98. * \param role Our role: client or server
  99. * \param hash hash function to use (MBEDTLS_MD_XXX)
  100. * \param curve elliptic curve identifier (MBEDTLS_ECP_DP_XXX)
  101. * \param secret pre-shared secret (passphrase)
  102. * \param len length of the shared secret
  103. *
  104. * \return 0 if successfull,
  105. * a negative error code otherwise
  106. */
  107. int mbedtls_ecjpake_setup( mbedtls_ecjpake_context *ctx,
  108. mbedtls_ecjpake_role role,
  109. mbedtls_md_type_t hash,
  110. mbedtls_ecp_group_id curve,
  111. const unsigned char *secret,
  112. size_t len );
  113. /**
  114. * \brief Check if a context is ready for use
  115. *
  116. * \param ctx Context to check
  117. *
  118. * \return 0 if the context is ready for use,
  119. * MBEDTLS_ERR_ECP_BAD_INPUT_DATA otherwise
  120. */
  121. int mbedtls_ecjpake_check( const mbedtls_ecjpake_context *ctx );
  122. /**
  123. * \brief Generate and write the first round message
  124. * (TLS: contents of the Client/ServerHello extension,
  125. * excluding extension type and length bytes)
  126. *
  127. * \param ctx Context to use
  128. * \param buf Buffer to write the contents to
  129. * \param len Buffer size
  130. * \param olen Will be updated with the number of bytes written
  131. * \param f_rng RNG function
  132. * \param p_rng RNG parameter
  133. *
  134. * \return 0 if successfull,
  135. * a negative error code otherwise
  136. */
  137. int mbedtls_ecjpake_write_round_one( mbedtls_ecjpake_context *ctx,
  138. unsigned char *buf, size_t len, size_t *olen,
  139. int (*f_rng)(void *, unsigned char *, size_t),
  140. void *p_rng );
  141. /**
  142. * \brief Read and process the first round message
  143. * (TLS: contents of the Client/ServerHello extension,
  144. * excluding extension type and length bytes)
  145. *
  146. * \param ctx Context to use
  147. * \param buf Pointer to extension contents
  148. * \param len Extension length
  149. *
  150. * \return 0 if successfull,
  151. * a negative error code otherwise
  152. */
  153. int mbedtls_ecjpake_read_round_one( mbedtls_ecjpake_context *ctx,
  154. const unsigned char *buf,
  155. size_t len );
  156. /**
  157. * \brief Generate and write the second round message
  158. * (TLS: contents of the Client/ServerKeyExchange)
  159. *
  160. * \param ctx Context to use
  161. * \param buf Buffer to write the contents to
  162. * \param len Buffer size
  163. * \param olen Will be updated with the number of bytes written
  164. * \param f_rng RNG function
  165. * \param p_rng RNG parameter
  166. *
  167. * \return 0 if successfull,
  168. * a negative error code otherwise
  169. */
  170. int mbedtls_ecjpake_write_round_two( mbedtls_ecjpake_context *ctx,
  171. unsigned char *buf, size_t len, size_t *olen,
  172. int (*f_rng)(void *, unsigned char *, size_t),
  173. void *p_rng );
  174. /**
  175. * \brief Read and process the second round message
  176. * (TLS: contents of the Client/ServerKeyExchange)
  177. *
  178. * \param ctx Context to use
  179. * \param buf Pointer to the message
  180. * \param len Message length
  181. *
  182. * \return 0 if successfull,
  183. * a negative error code otherwise
  184. */
  185. int mbedtls_ecjpake_read_round_two( mbedtls_ecjpake_context *ctx,
  186. const unsigned char *buf,
  187. size_t len );
  188. /**
  189. * \brief Derive the shared secret
  190. * (TLS: Pre-Master Secret)
  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_derive_secret( 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. * \brief Free a context's content
  208. *
  209. * \param ctx context to free
  210. */
  211. void mbedtls_ecjpake_free( mbedtls_ecjpake_context *ctx );
  212. #if defined(MBEDTLS_SELF_TEST)
  213. /**
  214. * \brief Checkup routine
  215. *
  216. * \return 0 if successful, or 1 if a test failed
  217. */
  218. int mbedtls_ecjpake_self_test( int verbose );
  219. #endif /* MBEDTLS_SELF_TEST */
  220. #ifdef __cplusplus
  221. }
  222. #endif
  223. #endif /* ecjpake.h */