mbedtls/include/mbedtls/ecp.h

745 lines
29 KiB
C
Raw Normal View History

2012-10-31 08:26:55 +00:00
/**
* \file ecp.h
*
2018-04-17 09:56:55 +00:00
* \brief This file contains ECP definitions and functions.
*
* The Elliptic Curve over P (ECP) is defined in <em>Standards for Efficient
* Cryptography Group (SECG): SEC1 Elliptic Curve Cryptography</em> and
* <em>RFC-4492: Elliptic Curve Cryptography (ECC) Cipher Suites
* for Transport Layer Security (TLS)</em>.
*
* <em>RFC-2409: The Internet Key Exchange (IKE)</em> defines ECP
* group types.
*
*/
2018-04-17 09:56:55 +00:00
/*
* Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
2015-09-04 12:21:07 +00:00
* SPDX-License-Identifier: Apache-2.0
2012-10-31 08:26:55 +00:00
*
2015-09-04 12:21:07 +00:00
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
2012-10-31 08:26:55 +00:00
*
2015-09-04 12:21:07 +00:00
* http://www.apache.org/licenses/LICENSE-2.0
2012-10-31 08:26:55 +00:00
*
2015-09-04 12:21:07 +00:00
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
2012-10-31 08:26:55 +00:00
*
2018-04-17 09:56:55 +00:00
* This file is part of Mbed TLS (https://tls.mbed.org)
2012-10-31 08:26:55 +00:00
*/
2018-04-17 09:56:55 +00:00
#ifndef MBEDTLS_ECP_H
#define MBEDTLS_ECP_H
2012-10-31 08:26:55 +00:00
2013-10-03 09:50:39 +00:00
#include "bignum.h"
2012-10-31 08:26:55 +00:00
/*
2012-11-05 09:06:12 +00:00
* ECP error codes
2012-10-31 08:26:55 +00:00
*/
#define MBEDTLS_ERR_ECP_BAD_INPUT_DATA -0x4F80 /**< Bad input parameters to function. */
#define MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL -0x4F00 /**< The buffer is too small to write to. */
2018-04-17 09:56:55 +00:00
#define MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE -0x4E80 /**< The requested curve not available. */
#define MBEDTLS_ERR_ECP_VERIFY_FAILED -0x4E00 /**< The signature is not valid. */
#define MBEDTLS_ERR_ECP_ALLOC_FAILED -0x4D80 /**< Memory allocation failed. */
2018-04-17 09:56:55 +00:00
#define MBEDTLS_ERR_ECP_RANDOM_FAILED -0x4D00 /**< Generation of random value, such as ephemeral key, failed. */
#define MBEDTLS_ERR_ECP_INVALID_KEY -0x4C80 /**< Invalid private or public key. */
#define MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH -0x4C00 /**< The buffer contains a valid signature followed by more data. */
2018-04-17 09:56:55 +00:00
#define MBEDTLS_ERR_ECP_HW_ACCEL_FAILED -0x4B80 /**< The ECP hardware accelerator failed. */
2012-11-02 08:40:25 +00:00
#if !defined(MBEDTLS_ECP_ALT)
/*
* default mbed TLS elliptic curve arithmetic implementation
*
* (in case MBEDTLS_ECP_ALT is defined then the developer has to provide an
* alternative implementation for the whole module and it will replace this
* one.)
*/
#ifdef __cplusplus
extern "C" {
#endif
/**
2018-04-17 09:56:55 +00:00
* Definition of domain parameter identifiers: curve, subgroup and generator.
*
2018-04-17 09:56:55 +00:00
* \note Only curves over prime fields are supported.
*
* \warning This library does not support validation of arbitrary domain
* parameters. Therefore, only well-known domain parameters from trusted
* sources should be used. See mbedtls_ecp_group_load().
*/
typedef enum
{
2018-04-17 09:56:55 +00:00
MBEDTLS_ECP_DP_NONE = 0, /*!< Curve not defined. */
MBEDTLS_ECP_DP_SECP192R1, /*!< Domain parameters for 192-bit NIST curve. */
MBEDTLS_ECP_DP_SECP224R1, /*!< Domain parameters for 224-bit NIST curve. */
MBEDTLS_ECP_DP_SECP256R1, /*!< Domain parameters for 256-bit NIST curve. */
MBEDTLS_ECP_DP_SECP384R1, /*!< Domain parameters for 384-bit NIST curve. */
MBEDTLS_ECP_DP_SECP521R1, /*!< Domain parameters for 521-bit NIST curve. */
MBEDTLS_ECP_DP_BP256R1, /*!< Domain parameters for 256-bit Brainpool curve. */
MBEDTLS_ECP_DP_BP384R1, /*!< Domain parameters for 384-bit Brainpool curve. */
MBEDTLS_ECP_DP_BP512R1, /*!< Domain parameters for 512-bit Brainpool curve. */
MBEDTLS_ECP_DP_CURVE25519, /*!< Domain parameters for a Curve25519 curve. */
MBEDTLS_ECP_DP_CURVE448, /*!< Domain parameters for a Curve448 curve. */
MBEDTLS_ECP_DP_SECP192K1, /*!< Domain parameters for 192-bit "Koblitz" curve. */
MBEDTLS_ECP_DP_SECP224K1, /*!< Domain parameters for 224-bit "Koblitz" curve. */
MBEDTLS_ECP_DP_SECP256K1, /*!< Domain parameters for 256-bit "Koblitz" curve. */
} mbedtls_ecp_group_id;
/**
2018-04-17 09:56:55 +00:00
* The number of supported curves, plus one for none.
2013-12-03 13:12:26 +00:00
*
2018-04-17 09:56:55 +00:00
* \note Montgomery curves are currently excluded.
*/
#define MBEDTLS_ECP_DP_MAX 12
/**
2018-04-17 09:56:55 +00:00
* Curve information, for use by other modules.
*/
typedef struct
{
2018-04-17 09:56:55 +00:00
mbedtls_ecp_group_id grp_id; /*!< An internal identifier. */
uint16_t tls_id; /*!< The TLS NamedCurve identifier. */
uint16_t bit_size; /*!< The size of the curve in bits. */
const char *name; /*!< A human-friendly name. */
} mbedtls_ecp_curve_info;
2012-10-31 08:26:55 +00:00
/**
2018-04-17 09:56:55 +00:00
* \brief The ECP point structure, in jacobian coordinates.
*
* \note All functions expect and return points satisfying
2018-04-17 09:56:55 +00:00
* the following condition: \p Z == 0 or \p Z == 1. Other
* values of \p Z are used only by internal functions.
* The point is zero, or "at infinity", if Z == 0.
* Otherwise, X and Y are its standard (affine) coordinates.
2012-10-31 08:26:55 +00:00
*/
typedef struct
{
2018-04-17 09:56:55 +00:00
mbedtls_mpi X; /*!< The X coordinate of the ECP point. */
mbedtls_mpi Y; /*!< The Y coordinate of the ECP point. */
mbedtls_mpi Z; /*!< The Z coordinate of the ECP point. */
2012-10-31 08:26:55 +00:00
}
mbedtls_ecp_point;
2012-10-31 08:26:55 +00:00
/**
2018-04-17 09:56:55 +00:00
* \brief The ECP group structure.
*
* We consider two types of curve equations:
* <ul><li>Short Weierstrass: y^2 = x^3 + \p A x + \p B mod P
* (SEC1 + RFC-4492)</li>
* <li>Montgomery: y^2 = x^3 + A x^2 + x mod P (Curve25519 + draft)</li></ul>
* In both cases, the generator (G) for a prime-order subgroup is fixed.
2012-10-31 08:26:55 +00:00
*
2018-04-17 09:56:55 +00:00
* For Short Weierstrass, this subgroup is the whole curve, and its
* cardinal is denoted by \p N. Our code requires that \p N is an odd prime.
2012-11-18 12:19:07 +00:00
*
2018-04-17 09:56:55 +00:00
* \note For blinding, use odd in mbedtls_ecp_mul() and prime in
* mbedtls_ecdsa_sign().
*
2018-04-17 09:56:55 +00:00
* For Montgomery curves, we do not store \p A, but (A + 2) / 4, which is
* the quantity used in the formulas. Additionally, \p nbits is not the
* size of \p N but the required size for private keys.
2012-11-18 12:19:07 +00:00
*
2018-04-17 09:56:55 +00:00
* If \p modp is NULL, reduction modulo \p P is done using a generic algorithm.
* Otherwise, it must point to a function that takes an \p mbedtls_mpi in the
* range of 0..2^(2*pbits)-1, and transforms it in-place in an integer of
* little more than \p pbits, so that the integer may be efficiently brought
* in the 0..P-1 range by a few additions or substractions.
*
* \return \c 0 on success
* \return Non-zero on failure.
2012-10-31 08:26:55 +00:00
*/
typedef struct
{
2018-04-17 09:56:55 +00:00
mbedtls_ecp_group_id id; /*!< An internal group identifier. */
mbedtls_mpi P; /*!< A prime modulus of the base field. */
mbedtls_mpi A; /*!< \p A in the equation or <code>(A + 2) / 4</code>. */
mbedtls_mpi B; /*!< \p B in the equation or unused. */
mbedtls_ecp_point G; /*!< The generator of the (sub)group used. */
mbedtls_mpi N; /*!< The order of \p G. */
size_t pbits; /*!< The number of bits in \p P.*/
size_t nbits; /*!< The number of bits in \p P, or the private
keys. */
unsigned int h; /*!< \internal 1 if the constants are static. */
int (*modp)(mbedtls_mpi *); /*!< The function for fast reduction mod P.*/
int (*t_pre)(mbedtls_ecp_point *, void *); /*!< Unused. */
int (*t_post)(mbedtls_ecp_point *, void *); /*!< Unused. */
void *t_data; /*!< Unused. */
mbedtls_ecp_point *T; /*!< Pre-computed points for ecp_mul_comb(). */
size_t T_size; /*!< The number for pre-computed points. */
2012-10-31 08:26:55 +00:00
}
mbedtls_ecp_group;
2012-10-31 08:26:55 +00:00
/**
2018-04-17 09:56:55 +00:00
* \brief The ECP key-pair structure.
*
2018-04-17 09:56:55 +00:00
* A generic key-pair that may be used for ECDSA and fixed ECDH, for example.
*
2018-04-17 09:56:55 +00:00
* \note Members are deliberately in the same order as in the
* #mbedtls_ecdsa_context structure.
*/
typedef struct
{
2018-04-17 09:56:55 +00:00
mbedtls_ecp_group grp; /*!< The elliptic curve and base point. */
mbedtls_mpi d; /*!< Our secret value. */
mbedtls_ecp_point Q; /*!< Our public value. */
}
mbedtls_ecp_keypair;
/**
* \name SECTION: Module settings
*
* The configuration options you can set for this module are in this section.
2018-04-17 09:56:55 +00:00
* Either change them in config.h, or define them using the compiler command line.
* \{
*/
#if !defined(MBEDTLS_ECP_MAX_BITS)
/**
2018-04-17 09:56:55 +00:00
* The maximum size of the groups, that is, of N and P.
*/
2018-04-17 09:56:55 +00:00
#define MBEDTLS_ECP_MAX_BITS 521 /**< The maximum size of groups, in bits. */
#endif
#define MBEDTLS_ECP_MAX_BYTES ( ( MBEDTLS_ECP_MAX_BITS + 7 ) / 8 )
#define MBEDTLS_ECP_MAX_PT_LEN ( 2 * MBEDTLS_ECP_MAX_BYTES + 1 )
#if !defined(MBEDTLS_ECP_WINDOW_SIZE)
/*
2013-11-20 17:39:55 +00:00
* Maximum "window" size used for point multiplication.
* Default: 6.
* Minimum value: 2. Maximum value: 7.
*
* Result is an array of at most ( 1 << ( MBEDTLS_ECP_WINDOW_SIZE - 1 ) )
* points used for point multiplication. This value is directly tied to EC
* peak memory usage, so decreasing it by one should roughly cut memory usage
* by two (if large curves are in use).
*
* Reduction in size may reduce speed, but larger curves are impacted first.
* Sample performances (in ECDHE handshakes/s, with FIXED_POINT_OPTIM = 1):
* w-size: 6 5 4 3 2
* 521 145 141 135 120 97
* 384 214 209 198 177 146
* 256 320 320 303 262 226
* 224 475 475 453 398 342
* 192 640 640 633 587 476
*/
2018-04-17 09:56:55 +00:00
#define MBEDTLS_ECP_WINDOW_SIZE 6 /**< The maximum window size used. */
#endif /* MBEDTLS_ECP_WINDOW_SIZE */
#if !defined(MBEDTLS_ECP_FIXED_POINT_OPTIM)
/*
* Trade memory for speed on fixed-point multiplication.
*
* This speeds up repeated multiplication of the generator (that is, the
* multiplication in ECDSA signatures, and half of the multiplications in
* ECDSA verification and ECDHE) by a factor roughly 3 to 4.
*
* The cost is increasing EC peak memory usage by a factor roughly 2.
*
* Change this value to 0 to reduce peak memory usage.
*/
2018-04-17 09:56:55 +00:00
#define MBEDTLS_ECP_FIXED_POINT_OPTIM 1 /**< Enable fixed-point speed-up. */
#endif /* MBEDTLS_ECP_FIXED_POINT_OPTIM */
/* \} name SECTION: Module settings */
/*
* Point formats, from RFC 4492's enum ECPointFormat
*/
2018-04-17 09:56:55 +00:00
#define MBEDTLS_ECP_PF_UNCOMPRESSED 0 /**< Uncompressed point format. */
#define MBEDTLS_ECP_PF_COMPRESSED 1 /**< Compressed point format. */
/*
* Some other constants from RFC 4492
*/
2018-04-17 09:56:55 +00:00
#define MBEDTLS_ECP_TLS_NAMED_CURVE 3 /**< The named_curve of ECCurveType. */
/**
2018-04-17 09:56:55 +00:00
* \brief This function retrieves the information defined in
* mbedtls_ecp_curve_info()for all supported curves in order
* of preference.
*
2018-04-17 09:56:55 +00:00
* \return A statically allocated array. The last entry is 0.
*/
const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list( void );
2014-02-04 13:48:50 +00:00
/**
2018-04-17 09:56:55 +00:00
* \brief This function retrieves the grp_id of all supported curves
* in order of preference.
2014-02-04 13:48:50 +00:00
*
* \return A statically allocated array,
* terminated with MBEDTLS_ECP_DP_NONE.
2014-02-04 13:48:50 +00:00
*/
const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list( void );
2014-02-04 13:48:50 +00:00
2013-10-23 18:19:57 +00:00
/**
2018-04-17 09:56:55 +00:00
* \brief This function retrieves curve information from an internal
* group identifier.
2013-10-23 18:19:57 +00:00
*
2018-04-17 09:56:55 +00:00
* \param grp_id An \c MBEDTLS_ECP_DP_XXX value.
2013-10-23 18:19:57 +00:00
*
2018-04-17 09:56:55 +00:00
* \return The associated curve information, or NULL.
2013-10-23 18:19:57 +00:00
*/
const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_grp_id( mbedtls_ecp_group_id grp_id );
2013-10-23 18:19:57 +00:00
/**
2018-04-17 09:56:55 +00:00
* \brief This function retrieves curve information from a TLS
* NamedCurve value.
2013-10-23 18:19:57 +00:00
*
2018-04-17 09:56:55 +00:00
* \param tls_id An \c MBEDTLS_ECP_DP_XXX value.
2013-10-23 18:19:57 +00:00
*
2018-04-17 09:56:55 +00:00
* \return The associated curve information, or NULL.
2013-10-23 18:19:57 +00:00
*/
const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id( uint16_t tls_id );
2013-10-23 18:19:57 +00:00
2013-11-30 14:10:14 +00:00
/**
2018-04-17 09:56:55 +00:00
* \brief This function retrieves curve information from a
* human-readable name.
2013-11-30 14:10:14 +00:00
*
2018-04-17 09:56:55 +00:00
* \param name The human-readable name.
2013-11-30 14:10:14 +00:00
*
2018-04-17 09:56:55 +00:00
* \return The associated curve information, or NULL.
2013-11-30 14:10:14 +00:00
*/
const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name( const char *name );
2013-11-30 14:10:14 +00:00
/**
2018-04-17 09:56:55 +00:00
* \brief This function initializes a point as zero.
*
* \param pt The point to initialize.
*/
void mbedtls_ecp_point_init( mbedtls_ecp_point *pt );
/**
2018-04-17 09:56:55 +00:00
* \brief This function initializes a group to something meaningless.
*/
void mbedtls_ecp_group_init( mbedtls_ecp_group *grp );
/**
2018-04-17 09:56:55 +00:00
* \brief This function initializes a key pair as an invalid one.
*
* \param key The key pair to initialize.
*/
void mbedtls_ecp_keypair_init( mbedtls_ecp_keypair *key );
2012-11-02 08:40:25 +00:00
/**
2018-04-17 09:56:55 +00:00
* \brief This function frees the components of a point.
*
* \param pt The point to free.
2012-11-02 08:40:25 +00:00
*/
void mbedtls_ecp_point_free( mbedtls_ecp_point *pt );
2012-11-02 08:40:25 +00:00
/**
2018-04-17 09:56:55 +00:00
* \brief This function frees the components of an ECP group.
* \param grp The group to free.
2012-11-02 08:40:25 +00:00
*/
void mbedtls_ecp_group_free( mbedtls_ecp_group *grp );
2012-11-02 08:40:25 +00:00
/**
2018-04-17 09:56:55 +00:00
* \brief This function frees the components of a key pair.
* \param key The key pair to free.
*/
void mbedtls_ecp_keypair_free( mbedtls_ecp_keypair *key );
/**
2018-04-17 09:56:55 +00:00
* \brief This function copies the contents of point \p Q into
* point \p P.
*
2018-04-17 09:56:55 +00:00
* \param P The destination point.
* \param Q The source point.
2013-01-26 15:05:22 +00:00
*
2018-04-17 09:56:55 +00:00
* \return \c 0 on success.
* \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation fails.
*/
int mbedtls_ecp_copy( mbedtls_ecp_point *P, const mbedtls_ecp_point *Q );
2013-01-26 15:05:22 +00:00
/**
2018-04-17 09:56:55 +00:00
* \brief This function copies the contents of group \p src into
* group \p dst.
2013-01-26 15:05:22 +00:00
*
2018-04-17 09:56:55 +00:00
* \param dst The destination group.
* \param src The source group.
2013-01-26 15:05:22 +00:00
*
2018-04-17 09:56:55 +00:00
* \return \c 0 on success.
* \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation fails.
2013-01-26 15:05:22 +00:00
*/
int mbedtls_ecp_group_copy( mbedtls_ecp_group *dst, const mbedtls_ecp_group *src );
2013-01-26 15:05:22 +00:00
2012-11-02 08:40:25 +00:00
/**
2018-04-17 09:56:55 +00:00
* \brief This function sets a point to zero.
2012-11-02 08:40:25 +00:00
*
2018-04-17 09:56:55 +00:00
* \param pt The point to set.
2012-11-02 08:40:25 +00:00
*
2018-04-17 09:56:55 +00:00
* \return \c 0 on success.
* \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation fails.
2012-11-02 08:40:25 +00:00
*/
int mbedtls_ecp_set_zero( mbedtls_ecp_point *pt );
2012-11-02 08:40:25 +00:00
2013-08-12 13:44:31 +00:00
/**
2018-04-17 09:56:55 +00:00
* \brief This function checks if a point is zero.
2013-08-12 13:44:31 +00:00
*
2018-04-17 09:56:55 +00:00
* \param pt The point to test.
2013-08-12 13:44:31 +00:00
*
2018-04-17 09:56:55 +00:00
* \return \c 1 if point is zero.
* \return \c 0 if point is non-zero.
2013-08-12 13:44:31 +00:00
*/
int mbedtls_ecp_is_zero( mbedtls_ecp_point *pt );
2013-08-12 13:44:31 +00:00
/**
2018-04-17 09:56:55 +00:00
* \brief This function compares two points.
*
2018-04-17 09:56:55 +00:00
* \note This assumes that the points are normalized. Otherwise,
* they may compare as "not equal" even if they are.
*
2018-04-17 09:56:55 +00:00
* \param P The first point to compare.
* \param Q The second point to compare.
*
2018-04-17 09:56:55 +00:00
* \return \c 0 if the points are equal.
* \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the points are not equal.
*/
int mbedtls_ecp_point_cmp( const mbedtls_ecp_point *P,
const mbedtls_ecp_point *Q );
2012-11-05 12:13:44 +00:00
/**
2018-04-17 09:56:55 +00:00
* \brief This function imports a non-zero point from two ASCII
* strings.
2012-11-05 12:13:44 +00:00
*
2018-04-17 09:56:55 +00:00
* \param P The destination point.
* \param radix The numeric base of the input.
* \param x The first affine coordinate, as a null-terminated string.
* \param y The second affine coordinate, as a null-terminated string.
2012-11-05 12:13:44 +00:00
*
2018-04-17 09:56:55 +00:00
* \return \c 0 on success.
* \return An \c MBEDTLS_ERR_MPI_XXX error code on failure.
2012-11-05 12:13:44 +00:00
*/
int mbedtls_ecp_point_read_string( mbedtls_ecp_point *P, int radix,
2012-11-05 12:13:44 +00:00
const char *x, const char *y );
2012-11-24 13:10:14 +00:00
/**
2018-04-17 09:56:55 +00:00
* \brief This function exports a point into unsigned binary data.
2012-11-24 13:10:14 +00:00
*
2018-04-17 09:56:55 +00:00
* \param grp The group to which the point should belong.
* \param P The point to export.
* \param format The point format. Should be an \c MBEDTLS_ECP_PF_XXX macro.
* \param olen The length of the output.
* \param buf The output buffer.
* \param buflen The length of the output buffer.
2012-11-24 13:10:14 +00:00
*
2018-04-17 09:56:55 +00:00
* \return \c 0 on success.
* \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA
* or #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL on failure.
2012-11-24 13:10:14 +00:00
*/
int mbedtls_ecp_point_write_binary( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *P,
2013-02-10 11:22:46 +00:00
int format, size_t *olen,
unsigned char *buf, size_t buflen );
2012-11-24 13:10:14 +00:00
2012-11-24 15:19:42 +00:00
/**
2018-04-17 09:56:55 +00:00
* \brief This function imports a point from unsigned binary data.
2012-11-24 15:19:42 +00:00
*
2018-04-17 09:56:55 +00:00
* \note This function does not check that the point actually
* belongs to the given group, see mbedtls_ecp_check_pubkey()
* for that.
2012-11-24 15:19:42 +00:00
*
2018-04-17 09:56:55 +00:00
* \param grp The group to which the point should belong.
* \param P The point to import.
* \param buf The input buffer.
* \param ilen The length of the input.
*
* \return \c 0 on success.
* \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid.
* \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
* \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the point format
* is not implemented.
2012-11-24 15:19:42 +00:00
*
*/
int mbedtls_ecp_point_read_binary( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P,
const unsigned char *buf, size_t ilen );
2012-11-07 19:24:05 +00:00
/**
2018-04-17 09:56:55 +00:00
* \brief This function imports a point from a TLS ECPoint record.
*
2018-04-17 09:56:55 +00:00
* \note On function return, \p buf is updated to point to immediately
* after the ECPoint.
*
2018-04-17 09:56:55 +00:00
* \param grp The ECP group used.
* \param pt The destination point.
* \param buf The address of the pointer to the start of input buffer.
* \param len The length of the buffer.
2014-11-21 08:14:52 +00:00
*
2018-04-17 09:56:55 +00:00
* \return \c 0 on success.
* \return An \c MBEDTLS_ERR_MPI_XXX error code if initialization failed.
* \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid.
2012-11-07 19:24:05 +00:00
*/
int mbedtls_ecp_tls_read_point( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
2013-10-23 18:19:57 +00:00
const unsigned char **buf, size_t len );
2012-11-07 19:24:05 +00:00
2013-02-10 11:06:19 +00:00
/**
2018-04-17 09:56:55 +00:00
* \brief This function exports a point as a TLS ECPoint record.
2013-02-10 11:06:19 +00:00
*
2018-04-17 09:56:55 +00:00
* \param grp The ECP group used.
* \param pt The point to export.
* \param format The export format.
* \param olen The length of data written.
* \param buf The Buffer to write to.
* \param blen The length of the Buffer.
2013-02-10 11:06:19 +00:00
*
2018-04-17 09:56:55 +00:00
* \return \c 0 on success.
* \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA or
* #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL on failure.
2013-02-10 11:06:19 +00:00
*/
int mbedtls_ecp_tls_write_point( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt,
2013-10-23 18:19:57 +00:00
int format, size_t *olen,
2013-02-10 11:06:19 +00:00
unsigned char *buf, size_t blen );
/**
2018-04-17 09:56:55 +00:00
* \brief This function sets a group using well-known domain parameters.
*
2018-04-17 09:56:55 +00:00
* \note The index should be a value of the NamedCurve enum,
* as defined in <em>RFC-4492: Elliptic Curve Cryptography
* (ECC) Cipher Suites for Transport Layer Security (TLS)</em>,
* usually in the form of an \c MBEDTLS_ECP_DP_XXX macro.
*
2018-04-17 09:56:55 +00:00
* \param grp The destination group.
* \param id The index in the list of well-known domain parameters.
2013-10-23 18:19:57 +00:00
*
2018-04-17 09:56:55 +00:00
* \return \c 0 on success,
* \return An \c MBEDTLS_ERR_MPI_XXX error code if initialization fails.
* \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE for unkownn groups.
*/
int mbedtls_ecp_group_load( mbedtls_ecp_group *grp, mbedtls_ecp_group_id id );
/**
2018-04-17 09:56:55 +00:00
* \brief This function sets a group from a TLS ECParameters record.
*
2018-04-17 09:56:55 +00:00
* \note \p buf is updated to point right after ECParameters on exit.
*
2018-04-17 09:56:55 +00:00
* \param grp The destination group.
* \param buf The address of the pointer to the start of input buffer.
* \param len The length of the buffer.
2014-11-21 08:14:52 +00:00
*
2018-04-17 09:56:55 +00:00
* \return \c 0 on success.
* \return An \c MBEDTLS_ERR_MPI_XXX error code if initialization fails.
* \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid.
*/
int mbedtls_ecp_tls_read_group( mbedtls_ecp_group *grp, const unsigned char **buf, size_t len );
/**
2018-04-17 09:56:55 +00:00
* \brief This function writes the TLS ECParameters record for a group.
*
2018-04-17 09:56:55 +00:00
* \param grp The ECP group used.
* \param olen The number of Bytes written.
* \param buf The buffer to write to.
* \param blen The length of the buffer.
*
2018-04-17 09:56:55 +00:00
* \return \c 0 on success.
* \return #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL on failure.
*/
int mbedtls_ecp_tls_write_group( const mbedtls_ecp_group *grp, size_t *olen,
2013-02-10 11:22:46 +00:00
unsigned char *buf, size_t blen );
2012-10-31 08:26:55 +00:00
/**
2018-04-17 09:56:55 +00:00
* \brief This function performs multiplication of a point by
* an integer: \p R = \p m * \p P.
*
* It is not thread-safe to use same group in multiple threads.
2012-10-31 08:26:55 +00:00
*
2018-04-17 09:56:55 +00:00
* \note To prevent timing attacks, this function
* executes the exact same sequence of base-field
* operations for any valid \p m. It avoids any if-branch or
* array index depending on the value of \p m.
2015-05-11 16:40:45 +00:00
*
2018-04-17 09:56:55 +00:00
* \note If \p f_rng is not NULL, it is used to randomize
* intermediate results to prevent potential timing attacks
* targeting these results. We recommend always providing
* a non-NULL \p f_rng. The overhead is negligible.
2015-05-11 16:40:45 +00:00
*
2018-04-17 09:56:55 +00:00
* \param grp The ECP group.
* \param R The destination point.
* \param m The integer by which to multiply.
* \param P The point to multiply.
* \param f_rng The RNG function.
* \param p_rng The RNG context.
2012-10-31 08:26:55 +00:00
*
2018-04-17 09:56:55 +00:00
* \return \c 0 on success.
* \return #MBEDTLS_ERR_ECP_INVALID_KEY if \p m is not a valid privkey,
* or \p P is not a valid pubkey.
* \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure.
2012-10-31 08:26:55 +00:00
*/
int mbedtls_ecp_mul( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
const mbedtls_mpi *m, const mbedtls_ecp_point *P,
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
2012-10-31 08:26:55 +00:00
2015-05-11 16:40:45 +00:00
/**
2018-04-17 09:56:55 +00:00
* \brief This function performs multiplication and addition of two
* points by integers: \p R = \p m * \p P + \p n * \p Q
* It is not thread-safe to use same group in multiple threads.
2015-05-11 16:40:45 +00:00
*
2018-04-17 09:56:55 +00:00
* \note In contrast to mbedtls_ecp_mul(), this function does not
* guarantee a constant execution flow and timing.
2015-05-11 16:40:45 +00:00
*
2018-04-17 09:56:55 +00:00
* \param grp The ECP group.
* \param R The destination point.
* \param m The integer by which to multiply \p P.
* \param P The point to multiply by \p m.
* \param n The integer by which to multiply \p Q.
* \param Q The point to be multiplied by \p n.
2015-05-11 16:40:45 +00:00
*
2018-04-17 09:56:55 +00:00
* \return \c 0 on success.
* \return #MBEDTLS_ERR_ECP_INVALID_KEY if \p m or \p n are not
* valid private keys, or \p P or \p Q are not valid public
* keys.
* \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure.
2015-05-11 16:40:45 +00:00
*/
int mbedtls_ecp_muladd( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
const mbedtls_mpi *m, const mbedtls_ecp_point *P,
const mbedtls_mpi *n, const mbedtls_ecp_point *Q );
/**
2018-04-17 09:56:55 +00:00
* \brief This function checks that a point is a valid public key
* on this curve.
*
2018-04-17 09:56:55 +00:00
* It only checks that the point is non-zero, has
* valid coordinates and lies on the curve. It does not verify
* that it is indeed a multiple of \p G. This additional
* check is computationally more expensive, is not required
* by standards, and should not be necessary if the group
* used has a small cofactor. In particular, it is useless for
* the NIST groups which all have a cofactor of 1.
*
2018-04-17 09:56:55 +00:00
* \note This function uses bare components rather than an
* mbedtls_ecp_keypair() structure, to ease use with other
* structures. For example, mbedtls_ecdh_context() or
* mbedtls_ecdsa_context().
*
2018-04-17 09:56:55 +00:00
* \param grp The curve or group the point should belong to.
* \param pt The point to check.
*
2018-04-17 09:56:55 +00:00
* \return \c 0 if the point is a valid public key.
* \return #MBEDTLS_ERR_ECP_INVALID_KEY otherwise.
*/
int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt );
/**
2018-04-17 09:56:55 +00:00
* \brief This function checks that an \p mbedtls_mpi is a valid private
* key for this curve.
*
2018-04-17 09:56:55 +00:00
* \note This function uses bare components rather than an
* mbedtls_ecp_keypair() structure to ease use with other
* structures such as mbedtls_ecdh_context() or
* mbedtls_ecdsa_context().
*
2018-04-17 09:56:55 +00:00
* \param grp The group used.
* \param d The integer to check.
*
2018-04-17 09:56:55 +00:00
* \return \c 0 if the point is a valid private key.
* \return #MBEDTLS_ERR_ECP_INVALID_KEY otherwise.
*/
int mbedtls_ecp_check_privkey( const mbedtls_ecp_group *grp, const mbedtls_mpi *d );
2015-08-11 12:31:03 +00:00
/**
2018-04-17 09:56:55 +00:00
* \brief This function generates a keypair with a configurable base
* point.
*
* \note This function uses bare components rather than an
* mbedtls_ecp_keypair() structure to ease use with other
* structures such as mbedtls_ecdh_context() or
* mbedtls_ecdsa_context().
*
* \param grp The ECP group.
* \param G The chosen base point.
* \param d The destination MPI (secret part).
* \param Q The destination point (public part).
* \param f_rng The RNG function.
* \param p_rng The RNG context.
*
* \return \c 0 on success.
* \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code
* on failure.
*/
2015-08-11 12:31:03 +00:00
int mbedtls_ecp_gen_keypair_base( mbedtls_ecp_group *grp,
const mbedtls_ecp_point *G,
mbedtls_mpi *d, mbedtls_ecp_point *Q,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng );
2013-01-26 13:42:45 +00:00
/**
2018-04-17 09:56:55 +00:00
* \brief This function generates a keypair.
2013-01-26 13:42:45 +00:00
*
2018-04-17 09:56:55 +00:00
* \note This function uses bare components rather than an
* mbedtls_ecp_keypair() structure to ease use with other
* structures such as mbedtls_ecdh_context() or
* mbedtls_ecdsa_context().
2013-01-26 13:42:45 +00:00
*
2018-04-17 09:56:55 +00:00
* \param grp The ECP group.
* \param d The destination MPI (secret part).
* \param Q The destination point (public part).
* \param f_rng The RNG function.
* \param p_rng The RNG context.
*
2018-04-17 09:56:55 +00:00
* \return \c 0 on success.
* \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code
* on failure.
2013-01-26 13:42:45 +00:00
*/
int mbedtls_ecp_gen_keypair( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
2013-01-26 13:42:45 +00:00
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng );
2013-11-30 13:13:16 +00:00
/**
2018-04-17 09:56:55 +00:00
* \brief This function generates a key.
2013-11-30 13:13:16 +00:00
*
2018-04-17 09:56:55 +00:00
* \param grp_id The ECP group identifier.
* \param key The destination key.
* \param f_rng The RNG function.
* \param p_rng The RNG context.
2013-11-30 13:13:16 +00:00
*
2018-04-17 09:56:55 +00:00
* \return \c 0 on success.
* \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code
* on failure.
2013-11-30 13:13:16 +00:00
*/
int mbedtls_ecp_gen_key( mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
2013-11-30 13:13:16 +00:00
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
2014-11-06 14:25:32 +00:00
/**
2018-04-17 09:56:55 +00:00
* \brief This function checks a public-private key pair.
*
* \param pub The keypair structure holding the public key.
* \param prv The keypair structure holding the private key.
2014-11-06 14:25:32 +00:00
*
2018-04-17 09:56:55 +00:00
* \note The both are keypairs, and may optionally hold the corresponding other key, but the public key passed in thee pub is checked against the private key passed in prv.
2014-11-06 14:25:32 +00:00
*
2018-04-17 09:56:55 +00:00
* \return \c 0 on success - the keys are valid and match.
* \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA, or an \c
* MBEDTLS_ERR_ECP_XXX or an \c MBEDTLS_ERR_MPI_XXX
* error code on failure.
2014-11-06 14:25:32 +00:00
*/
int mbedtls_ecp_check_pub_priv( const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv );
2014-11-06 14:25:32 +00:00
#if defined(MBEDTLS_SELF_TEST)
2012-10-31 08:26:55 +00:00
/**
2018-04-17 09:56:55 +00:00
* \brief The ECP checkup routine.
2012-10-31 08:26:55 +00:00
*
2018-04-17 09:56:55 +00:00
* \return \c 0 on success.
* \return \c 1 on failure.
2012-10-31 08:26:55 +00:00
*/
int mbedtls_ecp_self_test( int verbose );
#endif /* MBEDTLS_SELF_TEST */
2012-10-31 08:26:55 +00:00
#ifdef __cplusplus
}
#endif
#else /* MBEDTLS_ECP_ALT */
#include "ecp_alt.h"
#endif /* MBEDTLS_ECP_ALT */
#endif /* ecp.h */