Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  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: GPL-2.0
  9.  *
  10.  *  This program is free software; you can redistribute it and/or modify
  11.  *  it under the terms of the GNU General Public License as published by
  12.  *  the Free Software Foundation; either version 2 of the License, or
  13.  *  (at your option) any later version.
  14.  *
  15.  *  This program is distributed in the hope that it will be useful,
  16.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  *  GNU General Public License for more details.
  19.  *
  20.  *  You should have received a copy of the GNU General Public License along
  21.  *  with this program; if not, write to the Free Software Foundation, Inc.,
  22.  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  23.  *
  24.  *  This file is part of mbed TLS (https://tls.mbed.org)
  25.  */
  26. #ifndef MBEDTLS_ECJPAKE_H
  27. #define MBEDTLS_ECJPAKE_H
  28.  
  29. /*
  30.  * J-PAKE is a password-authenticated key exchange that allows deriving a
  31.  * strong shared secret from a (potentially low entropy) pre-shared
  32.  * passphrase, with forward secrecy and mutual authentication.
  33.  * https://en.wikipedia.org/wiki/Password_Authenticated_Key_Exchange_by_Juggling
  34.  *
  35.  * This file implements the Elliptic Curve variant of J-PAKE,
  36.  * as defined in Chapter 7.4 of the Thread v1.0 Specification,
  37.  * available to members of the Thread Group http://threadgroup.org/
  38.  *
  39.  * As the J-PAKE algorithm is inherently symmetric, so is our API.
  40.  * Each party needs to send its first round message, in any order, to the
  41.  * other party, then each sends its second round message, in any order.
  42.  * The payloads are serialized in a way suitable for use in TLS, but could
  43.  * also be use outside TLS.
  44.  */
  45. #if !defined(MBEDTLS_CONFIG_FILE)
  46. #include "config.h"
  47. #else
  48. #include MBEDTLS_CONFIG_FILE
  49. #endif
  50.  
  51. #include "ecp.h"
  52. #include "md.h"
  53.  
  54. #ifdef __cplusplus
  55. extern "C" {
  56. #endif
  57.  
  58. /**
  59.  * Roles in the EC J-PAKE exchange
  60.  */
  61. typedef enum {
  62.     MBEDTLS_ECJPAKE_CLIENT = 0,         /**< Client                         */
  63.     MBEDTLS_ECJPAKE_SERVER,             /**< Server                         */
  64. } mbedtls_ecjpake_role;
  65.  
  66. #if !defined(MBEDTLS_ECJPAKE_ALT)
  67. /**
  68.  * EC J-PAKE context structure.
  69.  *
  70.  * J-PAKE is a symmetric protocol, except for the identifiers used in
  71.  * Zero-Knowledge Proofs, and the serialization of the second message
  72.  * (KeyExchange) as defined by the Thread spec.
  73.  *
  74.  * In order to benefit from this symmetry, we choose a different naming
  75.  * convetion from the Thread v1.0 spec. Correspondance is indicated in the
  76.  * description as a pair C: client name, S: server name
  77.  */
  78. typedef struct mbedtls_ecjpake_context
  79. {
  80.     const mbedtls_md_info_t *md_info;   /**< Hash to use                    */
  81.     mbedtls_ecp_group grp;              /**< Elliptic curve                 */
  82.     mbedtls_ecjpake_role role;          /**< Are we client or server?       */
  83.     int point_format;                   /**< Format for point export        */
  84.  
  85.     mbedtls_ecp_point Xm1;              /**< My public key 1   C: X1, S: X3 */
  86.     mbedtls_ecp_point Xm2;              /**< My public key 2   C: X2, S: X4 */
  87.     mbedtls_ecp_point Xp1;              /**< Peer public key 1 C: X3, S: X1 */
  88.     mbedtls_ecp_point Xp2;              /**< Peer public key 2 C: X4, S: X2 */
  89.     mbedtls_ecp_point Xp;               /**< Peer public key   C: Xs, S: Xc */
  90.  
  91.     mbedtls_mpi xm1;                    /**< My private key 1  C: x1, S: x3 */
  92.     mbedtls_mpi xm2;                    /**< My private key 2  C: x2, S: x4 */
  93.  
  94.     mbedtls_mpi s;                      /**< Pre-shared secret (passphrase) */
  95. } mbedtls_ecjpake_context;
  96.  
  97. #else  /* MBEDTLS_ECJPAKE_ALT */
  98. #include "ecjpake_alt.h"
  99. #endif /* MBEDTLS_ECJPAKE_ALT */
  100.  
  101. /**
  102.  * \brief           Initialize an ECJPAKE context.
  103.  *
  104.  * \param ctx       The ECJPAKE context to initialize.
  105.  *                  This must not be \c NULL.
  106.  */
  107. void mbedtls_ecjpake_init( mbedtls_ecjpake_context *ctx );
  108.  
  109. /**
  110.  * \brief           Set up an ECJPAKE context for use.
  111.  *
  112.  * \note            Currently the only values for hash/curve allowed by the
  113.  *                  standard are #MBEDTLS_MD_SHA256/#MBEDTLS_ECP_DP_SECP256R1.
  114.  *
  115.  * \param ctx       The ECJPAKE context to set up. This must be initialized.
  116.  * \param role      The role of the caller. This must be either
  117.  *                  #MBEDTLS_ECJPAKE_CLIENT or #MBEDTLS_ECJPAKE_SERVER.
  118.  * \param hash      The identifier of the hash function to use,
  119.  *                  for example #MBEDTLS_MD_SHA256.
  120.  * \param curve     The identifier of the elliptic curve to use,
  121.  *                  for example #MBEDTLS_ECP_DP_SECP256R1.
  122.  * \param secret    The pre-shared secret (passphrase). This must be
  123.  *                  a readable buffer of length \p len Bytes. It need
  124.  *                  only be valid for the duration of this call.
  125.  * \param len       The length of the pre-shared secret \p secret.
  126.  *
  127.  * \return          \c 0 if successful.
  128.  * \return          A negative error code on failure.
  129.  */
  130. int mbedtls_ecjpake_setup( mbedtls_ecjpake_context *ctx,
  131.                            mbedtls_ecjpake_role role,
  132.                            mbedtls_md_type_t hash,
  133.                            mbedtls_ecp_group_id curve,
  134.                            const unsigned char *secret,
  135.                            size_t len );
  136.  
  137. /**
  138.  * \brief           Check if an ECJPAKE context is ready for use.
  139.  *
  140.  * \param ctx       The ECJPAKE context to check. This must be
  141.  *                  initialized.
  142.  *
  143.  * \return          \c 0 if the context is ready for use.
  144.  * \return          #MBEDTLS_ERR_ECP_BAD_INPUT_DATA otherwise.
  145.  */
  146. int mbedtls_ecjpake_check( const mbedtls_ecjpake_context *ctx );
  147.  
  148. /**
  149.  * \brief           Generate and write the first round message
  150.  *                  (TLS: contents of the Client/ServerHello extension,
  151.  *                  excluding extension type and length bytes).
  152.  *
  153.  * \param ctx       The ECJPAKE context to use. This must be
  154.  *                  initialized and set up.
  155.  * \param buf       The buffer to write the contents to. This must be a
  156.  *                  writable buffer of length \p len Bytes.
  157.  * \param len       The length of \p buf in Bytes.
  158.  * \param olen      The address at which to store the total number
  159.  *                  of Bytes written to \p buf. This must not be \c NULL.
  160.  * \param f_rng     The RNG function to use. This must not be \c NULL.
  161.  * \param p_rng     The RNG parameter to be passed to \p f_rng. This
  162.  *                  may be \c NULL if \p f_rng doesn't use a context.
  163.  *
  164.  * \return          \c 0 if successful.
  165.  * \return          A negative error code on failure.
  166.  */
  167. int mbedtls_ecjpake_write_round_one( mbedtls_ecjpake_context *ctx,
  168.                             unsigned char *buf, size_t len, size_t *olen,
  169.                             int (*f_rng)(void *, unsigned char *, size_t),
  170.                             void *p_rng );
  171.  
  172. /**
  173.  * \brief           Read and process the first round message
  174.  *                  (TLS: contents of the Client/ServerHello extension,
  175.  *                  excluding extension type and length bytes).
  176.  *
  177.  * \param ctx       The ECJPAKE context to use. This must be initialized
  178.  *                  and set up.
  179.  * \param buf       The buffer holding the first round message. This must
  180.  *                  be a readable buffer of length \p len Bytes.
  181.  * \param len       The length in Bytes of \p buf.
  182.  *
  183.  * \return          \c 0 if successful.
  184.  * \return          A negative error code on failure.
  185.  */
  186. int mbedtls_ecjpake_read_round_one( mbedtls_ecjpake_context *ctx,
  187.                                     const unsigned char *buf,
  188.                                     size_t len );
  189.  
  190. /**
  191.  * \brief           Generate and write the second round message
  192.  *                  (TLS: contents of the Client/ServerKeyExchange).
  193.  *
  194.  * \param ctx       The ECJPAKE context to use. This must be initialized,
  195.  *                  set up, and already have performed round one.
  196.  * \param buf       The buffer to write the round two contents to.
  197.  *                  This must be a writable buffer of length \p len Bytes.
  198.  * \param len       The size of \p buf in Bytes.
  199.  * \param olen      The address at which to store the total number of Bytes
  200.  *                  written to \p buf. This must not be \c NULL.
  201.  * \param f_rng     The RNG function to use. This must not be \c NULL.
  202.  * \param p_rng     The RNG parameter to be passed to \p f_rng. This
  203.  *                  may be \c NULL if \p f_rng doesn't use a context.
  204.  *
  205.  * \return          \c 0 if successful.
  206.  * \return          A negative error code on failure.
  207.  */
  208. int mbedtls_ecjpake_write_round_two( mbedtls_ecjpake_context *ctx,
  209.                             unsigned char *buf, size_t len, size_t *olen,
  210.                             int (*f_rng)(void *, unsigned char *, size_t),
  211.                             void *p_rng );
  212.  
  213. /**
  214.  * \brief           Read and process the second round message
  215.  *                  (TLS: contents of the Client/ServerKeyExchange).
  216.  *
  217.  * \param ctx       The ECJPAKE context to use. This must be initialized
  218.  *                  and set up and already have performed round one.
  219.  * \param buf       The buffer holding the second round message. This must
  220.  *                  be a readable buffer of length \p len Bytes.
  221.  * \param len       The length in Bytes of \p buf.
  222.  *
  223.  * \return          \c 0 if successful.
  224.  * \return          A negative error code on failure.
  225.  */
  226. int mbedtls_ecjpake_read_round_two( mbedtls_ecjpake_context *ctx,
  227.                                     const unsigned char *buf,
  228.                                     size_t len );
  229.  
  230. /**
  231.  * \brief           Derive the shared secret
  232.  *                  (TLS: Pre-Master Secret).
  233.  *
  234.  * \param ctx       The ECJPAKE context to use. This must be initialized,
  235.  *                  set up and have performed both round one and two.
  236.  * \param buf       The buffer to write the derived secret to. This must
  237.  *                  be a writable buffer of length \p len Bytes.
  238.  * \param len       The length of \p buf in Bytes.
  239.  * \param olen      The address at which to store the total number of Bytes
  240.  *                  written to \p buf. This must not be \c NULL.
  241.  * \param f_rng     The RNG function to use. This must not be \c NULL.
  242.  * \param p_rng     The RNG parameter to be passed to \p f_rng. This
  243.  *                  may be \c NULL if \p f_rng doesn't use a context.
  244.  *
  245.  * \return          \c 0 if successful.
  246.  * \return          A negative error code on failure.
  247.  */
  248. int mbedtls_ecjpake_derive_secret( mbedtls_ecjpake_context *ctx,
  249.                             unsigned char *buf, size_t len, size_t *olen,
  250.                             int (*f_rng)(void *, unsigned char *, size_t),
  251.                             void *p_rng );
  252.  
  253. /**
  254.  * \brief           This clears an ECJPAKE context and frees any
  255.  *                  embedded data structure.
  256.  *
  257.  * \param ctx       The ECJPAKE context to free. This may be \c NULL,
  258.  *                  in which case this function does nothing. If it is not
  259.  *                  \c NULL, it must point to an initialized ECJPAKE context.
  260.  */
  261. void mbedtls_ecjpake_free( mbedtls_ecjpake_context *ctx );
  262.  
  263. #if defined(MBEDTLS_SELF_TEST)
  264.  
  265. /**
  266.  * \brief          Checkup routine
  267.  *
  268.  * \return         0 if successful, or 1 if a test failed
  269.  */
  270. int mbedtls_ecjpake_self_test( int verbose );
  271.  
  272. #endif /* MBEDTLS_SELF_TEST */
  273.  
  274. #ifdef __cplusplus
  275. }
  276. #endif
  277.  
  278.  
  279. #endif /* ecjpake.h */
  280.