2012-10-31 09:26:55 +01:00
|
|
|
/**
|
|
|
|
* \file ecp.h
|
|
|
|
*
|
|
|
|
* \brief Elliptic curves over GF(p)
|
|
|
|
*
|
2013-01-16 17:00:43 +01:00
|
|
|
* Copyright (C) 2006-2013, Brainspark B.V.
|
2012-10-31 09:26:55 +01:00
|
|
|
*
|
|
|
|
* This file is part of PolarSSL (http://www.polarssl.org)
|
|
|
|
* Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
|
|
|
|
*
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
#ifndef POLARSSL_ECP_H
|
|
|
|
#define POLARSSL_ECP_H
|
|
|
|
|
2013-01-26 15:30:46 +01:00
|
|
|
#include "polarssl/bignum.h"
|
2012-10-31 09:26:55 +01:00
|
|
|
|
|
|
|
/*
|
2012-11-05 10:06:12 +01:00
|
|
|
* ECP error codes
|
2012-10-31 09:26:55 +01:00
|
|
|
*/
|
2013-01-16 17:00:43 +01:00
|
|
|
#define POLARSSL_ERR_ECP_BAD_INPUT_DATA -0x4F80 /**< Bad input parameters to function. */
|
2013-06-29 23:31:33 +02:00
|
|
|
#define POLARSSL_ERR_ECP_BUFFER_TOO_SMALL -0x4F00 /**< The buffer is too small to write to. */
|
2013-08-27 15:11:23 +02:00
|
|
|
#define POLARSSL_ERR_ECP_GENERIC -0x4E80 /**< Generic ECP error. */
|
2013-06-29 23:31:33 +02:00
|
|
|
#define POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE -0x4E00 /**< Requested curve not available. */
|
2013-08-27 15:11:23 +02:00
|
|
|
#define POLARSSL_ERR_ECP_VERIFY_FAILED -0x4E00 /**< The signature is not valid. */
|
2012-11-02 09:40:25 +01:00
|
|
|
|
2013-06-27 14:29:21 +02:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2013-09-16 16:19:53 +02:00
|
|
|
/**
|
|
|
|
* Domain parameters (curve, subgroup and generator) identifiers.
|
|
|
|
*
|
|
|
|
* 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 ecp_use_known_dp().
|
|
|
|
*/
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
POLARSSL_ECP_DP_NONE = 0,
|
2013-09-16 17:30:04 +02:00
|
|
|
POLARSSL_ECP_DP_SECP192R1, /*!< 192-bits NIST curve */
|
|
|
|
POLARSSL_ECP_DP_SECP224R1, /*!< 224-bits NIST curve */
|
|
|
|
POLARSSL_ECP_DP_SECP256R1, /*!< 256-bits NIST curve */
|
|
|
|
POLARSSL_ECP_DP_SECP384R1, /*!< 384-bits NIST curve */
|
|
|
|
POLARSSL_ECP_DP_SECP521R1, /*!< 521-bits NIST curve */
|
2013-09-16 16:19:53 +02:00
|
|
|
} ecp_group_id;
|
|
|
|
|
2013-09-16 17:30:04 +02:00
|
|
|
/**
|
|
|
|
* Curve information for use by the SSL module
|
|
|
|
*/
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
ecp_group_id grp_id; /*!< Internal identifier */
|
|
|
|
uint16_t name; /*!< TLS NamedCurve value */
|
|
|
|
uint16_t size; /*!< Curve size in bits */
|
|
|
|
} ecp_curve_info;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List of supported curves
|
|
|
|
*/
|
|
|
|
extern ecp_curve_info ecp_supported_curves[];
|
|
|
|
|
2012-10-31 09:26:55 +01:00
|
|
|
/**
|
2012-11-19 20:16:28 +01:00
|
|
|
* \brief ECP point structure (jacobian coordinates)
|
2012-11-02 18:14:40 +01:00
|
|
|
*
|
2012-11-19 20:16:28 +01:00
|
|
|
* \note All functions expect and return points satisfying
|
|
|
|
* the following condition: Z == 0 or Z == 1. (Other
|
|
|
|
* values of Z are used by internal functions only.)
|
|
|
|
* The point is zero, or "at infinity", if Z == 0.
|
|
|
|
* Otherwise, X and Y are its standard (affine) coordinates.
|
2012-10-31 09:26:55 +01:00
|
|
|
*/
|
|
|
|
typedef struct
|
|
|
|
{
|
2012-10-31 19:37:54 +01:00
|
|
|
mpi X; /*!< the point's X coordinate */
|
|
|
|
mpi Y; /*!< the point's Y coordinate */
|
2012-11-19 20:16:28 +01:00
|
|
|
mpi Z; /*!< the point's Z coordinate */
|
2012-10-31 09:26:55 +01:00
|
|
|
}
|
|
|
|
ecp_point;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief ECP group structure
|
|
|
|
*
|
2012-11-18 13:19:07 +01:00
|
|
|
* The curves we consider are defined by y^2 = x^3 - 3x + B mod P,
|
|
|
|
* and a generator for a large subgroup of order N is fixed.
|
|
|
|
*
|
|
|
|
* pbits and nbits must be the size of P and N in bits.
|
|
|
|
*
|
|
|
|
* If modp is NULL, reduction modulo P is done using a generic
|
|
|
|
* algorithm. Otherwise, it must point to a function that takes an mpi
|
|
|
|
* in the range 0..2^(2*pbits) and transforms it in-place in an integer
|
|
|
|
* of little more than pbits, so that the integer may be efficiently
|
|
|
|
* brought in the 0..P range by a few additions or substractions. It
|
|
|
|
* must return 0 on success and a POLARSSL_ERR_ECP_XXX error on failure.
|
2012-10-31 09:26:55 +01:00
|
|
|
*/
|
|
|
|
typedef struct
|
|
|
|
{
|
2013-02-10 12:06:19 +01:00
|
|
|
ecp_group_id id; /*!< RFC 4492 group ID */
|
2012-10-31 09:26:55 +01:00
|
|
|
mpi P; /*!< prime modulus of the base field */
|
|
|
|
mpi B; /*!< constant term in the equation */
|
|
|
|
ecp_point G; /*!< generator of the subgroup used */
|
|
|
|
mpi N; /*!< the order of G */
|
2012-11-18 13:19:07 +01:00
|
|
|
size_t pbits; /*!< number of bits in P */
|
|
|
|
size_t nbits; /*!< number of bits in N */
|
2012-11-10 00:27:12 +01:00
|
|
|
int (*modp)(mpi *); /*!< function for fast reduction mod P */
|
2012-10-31 09:26:55 +01:00
|
|
|
}
|
|
|
|
ecp_group;
|
|
|
|
|
2013-07-01 13:40:52 +02:00
|
|
|
/**
|
|
|
|
* \brief ECP key pair structure
|
|
|
|
*
|
|
|
|
* A generic key pair that could be used for ECDSA, fixed ECDH, etc.
|
2013-08-14 18:16:50 +02:00
|
|
|
*
|
|
|
|
* \note Members purposefully in the same order as struc ecdsa_context.
|
2013-07-01 13:40:52 +02:00
|
|
|
*/
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
ecp_group grp; /*!< Elliptic curve and base point */
|
|
|
|
mpi d; /*!< our secret value */
|
|
|
|
ecp_point Q; /*!< our public value */
|
|
|
|
}
|
|
|
|
ecp_keypair;
|
|
|
|
|
2012-11-21 13:00:58 +01:00
|
|
|
/**
|
2013-08-08 13:30:57 +02:00
|
|
|
* Maximum size of the groups (that is, of N and P)
|
2012-11-21 13:00:58 +01:00
|
|
|
*/
|
2013-08-08 13:30:57 +02:00
|
|
|
#define POLARSSL_ECP_MAX_BITS 521
|
|
|
|
#define POLARSSL_ECP_MAX_BYTES ( ( POLARSSL_ECP_MAX_BITS + 7 ) / 8 )
|
2013-09-12 01:39:07 +02:00
|
|
|
#define POLARSSL_ECP_MAX_PT_LEN ( 2 * POLARSSL_ECP_MAX_BYTES + 1 )
|
2012-11-21 13:00:58 +01:00
|
|
|
|
2012-11-17 19:54:20 +01:00
|
|
|
/*
|
2012-11-21 13:00:58 +01:00
|
|
|
* Maximum window size (actually, NAF width) used for point multipliation.
|
|
|
|
* Default: 7.
|
2012-11-17 19:54:20 +01:00
|
|
|
* Minimum value: 2. Maximum value: 8.
|
|
|
|
*
|
2012-11-21 13:00:58 +01:00
|
|
|
* Result is an array of at most ( 1 << ( POLARSSL_ECP_WINDOW_SIZE - 1 ) )
|
2012-11-17 19:54:20 +01:00
|
|
|
* points used for point multiplication, so at most 64 by default.
|
|
|
|
* In practice, most curves will use less precomputed points.
|
|
|
|
*
|
|
|
|
* Reduction in size may reduce speed for big curves.
|
|
|
|
*/
|
2012-11-21 13:00:58 +01:00
|
|
|
#define POLARSSL_ECP_WINDOW_SIZE 7 /**< Maximum NAF width used. */
|
2012-11-17 19:54:20 +01:00
|
|
|
|
2012-11-24 15:19:55 +01:00
|
|
|
/*
|
2013-02-09 19:00:07 +01:00
|
|
|
* Point formats, from RFC 4492's enum ECPointFormat
|
2012-11-24 15:19:55 +01:00
|
|
|
*/
|
|
|
|
#define POLARSSL_ECP_PF_UNCOMPRESSED 0 /**< Uncompressed point format */
|
|
|
|
#define POLARSSL_ECP_PF_COMPRESSED 1 /**< Compressed point format */
|
|
|
|
|
2013-02-09 17:03:58 +01:00
|
|
|
/*
|
2013-02-09 19:00:07 +01:00
|
|
|
* Some other constants from RFC 4492
|
2013-02-09 17:03:58 +01:00
|
|
|
*/
|
2013-02-09 19:00:07 +01:00
|
|
|
#define POLARSSL_ECP_TLS_NAMED_CURVE 3 /**< ECCurveType's named_curve */
|
2013-02-09 17:03:58 +01:00
|
|
|
|
2013-07-01 13:40:52 +02:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2012-11-02 18:14:40 +01:00
|
|
|
/**
|
2012-11-05 13:13:44 +01:00
|
|
|
* \brief Initialize a point (as zero)
|
2012-11-02 18:14:40 +01:00
|
|
|
*/
|
|
|
|
void ecp_point_init( ecp_point *pt );
|
|
|
|
|
2012-11-05 17:27:54 +01:00
|
|
|
/**
|
|
|
|
* \brief Initialize a group (to something meaningless)
|
|
|
|
*/
|
|
|
|
void ecp_group_init( ecp_group *grp );
|
|
|
|
|
2013-07-01 13:40:52 +02:00
|
|
|
/**
|
|
|
|
* \brief Initialize a key pair (as an invalid one)
|
|
|
|
*/
|
|
|
|
void ecp_keypair_init( ecp_keypair *key );
|
|
|
|
|
2012-11-02 09:40:25 +01:00
|
|
|
/**
|
|
|
|
* \brief Free the components of a point
|
|
|
|
*/
|
|
|
|
void ecp_point_free( ecp_point *pt );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Free the components of an ECP group
|
|
|
|
*/
|
|
|
|
void ecp_group_free( ecp_group *grp );
|
|
|
|
|
2013-07-01 13:40:52 +02:00
|
|
|
/**
|
|
|
|
* \brief Free the components of a key pair
|
|
|
|
*/
|
|
|
|
void ecp_keypair_free( ecp_keypair *key );
|
|
|
|
|
2012-11-02 18:14:40 +01:00
|
|
|
/**
|
|
|
|
* \brief Set a point to zero
|
2012-11-19 20:16:28 +01:00
|
|
|
*
|
2013-01-26 16:05:22 +01:00
|
|
|
* \param pt Destination point
|
|
|
|
*
|
2012-11-19 20:16:28 +01:00
|
|
|
* \return 0 if successful,
|
|
|
|
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
2012-11-02 18:14:40 +01:00
|
|
|
*/
|
2012-11-19 20:16:28 +01:00
|
|
|
int ecp_set_zero( ecp_point *pt );
|
2012-11-02 18:14:40 +01:00
|
|
|
|
2013-01-26 16:05:22 +01:00
|
|
|
/**
|
|
|
|
* \brief Tell if a point is zero
|
|
|
|
*
|
|
|
|
* \param pt Point to test
|
|
|
|
*
|
|
|
|
* \return 1 if point is zero, 0 otherwise
|
|
|
|
*/
|
|
|
|
int ecp_is_zero( ecp_point *pt );
|
|
|
|
|
2012-11-02 09:40:25 +01:00
|
|
|
/**
|
|
|
|
* \brief Copy the contents of point Q into P
|
|
|
|
*
|
|
|
|
* \param P Destination point
|
|
|
|
* \param Q Source point
|
|
|
|
*
|
2012-11-05 10:06:12 +01:00
|
|
|
* \return 0 if successful,
|
|
|
|
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
2012-11-02 09:40:25 +01:00
|
|
|
*/
|
|
|
|
int ecp_copy( ecp_point *P, const ecp_point *Q );
|
|
|
|
|
2013-08-12 15:44:31 +02:00
|
|
|
/**
|
|
|
|
* \brief Copy the contents of a group object
|
|
|
|
*
|
|
|
|
* \param dst Destination group
|
|
|
|
* \param src Source group
|
|
|
|
*
|
|
|
|
* \return 0 if successful,
|
|
|
|
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
|
|
|
*/
|
|
|
|
int ecp_group_copy( ecp_group *dst, const ecp_group *src );
|
|
|
|
|
2012-11-05 13:13:44 +01:00
|
|
|
/**
|
|
|
|
* \brief Import a non-zero point from two ASCII strings
|
|
|
|
*
|
|
|
|
* \param P Destination point
|
|
|
|
* \param radix Input numeric base
|
|
|
|
* \param x First affine coordinate as a null-terminated string
|
|
|
|
* \param y Second affine coordinate as a null-terminated string
|
|
|
|
*
|
|
|
|
* \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
|
|
|
|
*/
|
|
|
|
int ecp_point_read_string( ecp_point *P, int radix,
|
|
|
|
const char *x, const char *y );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Import an ECP group from null-terminated ASCII strings
|
|
|
|
*
|
|
|
|
* \param grp Destination group
|
2012-11-05 17:27:54 +01:00
|
|
|
* \param radix Input numeric base
|
2012-11-05 13:13:44 +01:00
|
|
|
* \param p Prime modulus of the base field
|
|
|
|
* \param b Constant term in the equation
|
|
|
|
* \param gx The generator's X coordinate
|
|
|
|
* \param gy The generator's Y coordinate
|
|
|
|
* \param n The generator's order
|
|
|
|
*
|
|
|
|
* \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
|
2012-11-18 13:19:07 +01:00
|
|
|
*
|
|
|
|
* \note Sets all fields except modp.
|
2012-11-05 13:13:44 +01:00
|
|
|
*/
|
|
|
|
int ecp_group_read_string( ecp_group *grp, int radix,
|
|
|
|
const char *p, const char *b,
|
|
|
|
const char *gx, const char *gy, const char *n);
|
|
|
|
|
2012-11-24 14:10:14 +01:00
|
|
|
/**
|
2012-11-24 15:19:55 +01:00
|
|
|
* \brief Export a point into unsigned binary data
|
2012-11-24 14:10:14 +01:00
|
|
|
*
|
2012-11-24 16:19:42 +01:00
|
|
|
* \param grp Group to which the point should belong
|
2012-11-24 14:10:14 +01:00
|
|
|
* \param P Point to export
|
2012-11-24 15:19:55 +01:00
|
|
|
* \param format Point format, should be a POLARSSL_ECP_PF_XXX macro
|
2013-02-09 19:00:07 +01:00
|
|
|
* \param olen Length of the actual output
|
2012-11-24 14:10:14 +01:00
|
|
|
* \param buf Output buffer
|
|
|
|
* \param buflen Length of the output buffer
|
|
|
|
*
|
2013-02-09 19:00:07 +01:00
|
|
|
* \return 0 if successful,
|
|
|
|
* or POLARSSL_ERR_ECP_BAD_INPUT_DATA
|
|
|
|
* or POLARSSL_ERR_ECP_BUFFER_TOO_SMALL
|
2012-11-24 14:10:14 +01:00
|
|
|
*/
|
2013-02-10 10:58:48 +01:00
|
|
|
int ecp_point_write_binary( const ecp_group *grp, const ecp_point *P,
|
2013-02-10 12:22:46 +01:00
|
|
|
int format, size_t *olen,
|
2013-02-10 10:58:48 +01:00
|
|
|
unsigned char *buf, size_t buflen );
|
2012-11-24 14:10:14 +01:00
|
|
|
|
2012-11-24 16:19:42 +01:00
|
|
|
/**
|
|
|
|
* \brief Import a point from unsigned binary data
|
|
|
|
*
|
|
|
|
* \param grp Group to which the point should belong
|
|
|
|
* \param P Point to import
|
|
|
|
* \param buf Input buffer
|
|
|
|
* \param ilen Actual length of input
|
|
|
|
*
|
|
|
|
* \return 0 if successful,
|
|
|
|
* POLARSSL_ERR_ECP_GENERIC if input is invalid
|
|
|
|
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
|
|
|
*
|
|
|
|
* \note This function does NOT check that the point actually
|
|
|
|
* belongs to the given group, see ecp_check_pubkey() for
|
|
|
|
* that.
|
|
|
|
*/
|
2013-02-10 10:58:48 +01:00
|
|
|
int ecp_point_read_binary( const ecp_group *grp, ecp_point *P,
|
|
|
|
const unsigned char *buf, size_t ilen );
|
2013-02-09 17:03:58 +01:00
|
|
|
|
2012-11-07 20:24:05 +01:00
|
|
|
/**
|
|
|
|
* \brief Set a group using well-known domain parameters
|
|
|
|
*
|
|
|
|
* \param grp Destination group
|
|
|
|
* \param index Index in the list of well-known domain parameters
|
|
|
|
*
|
2012-11-10 14:44:24 +01:00
|
|
|
* \return O if successful,
|
2012-11-07 20:24:05 +01:00
|
|
|
* POLARSSL_ERR_MPI_XXX if initialization failed
|
2013-09-16 16:19:53 +02:00
|
|
|
* POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE for unkownn groups
|
2012-11-07 20:24:05 +01:00
|
|
|
*
|
2013-02-09 17:03:58 +01:00
|
|
|
* \note Index should be a value of RFC 4492's enum NamdeCurve,
|
|
|
|
* possibly in the form of a POLARSSL_ECP_DP_XXX macro.
|
|
|
|
*/
|
2013-09-10 16:16:50 +02:00
|
|
|
int ecp_use_known_dp( ecp_group *grp, ecp_group_id index );
|
2013-02-09 17:03:58 +01:00
|
|
|
|
|
|
|
/**
|
2013-02-09 19:00:07 +01:00
|
|
|
* \brief Set a group from a TLS ECParameters record
|
2013-02-09 17:03:58 +01:00
|
|
|
*
|
|
|
|
* \param grp Destination group
|
2013-02-10 13:20:52 +01:00
|
|
|
* \param buf &(Start of input buffer)
|
2013-02-09 17:03:58 +01:00
|
|
|
* \param len Buffer length
|
|
|
|
*
|
|
|
|
* \return O if successful,
|
|
|
|
* POLARSSL_ERR_MPI_XXX if initialization failed
|
|
|
|
* POLARSSL_ERR_ECP_BAD_INPUT_DATA if input is invalid
|
2012-11-07 20:24:05 +01:00
|
|
|
*/
|
2013-02-10 13:20:52 +01:00
|
|
|
int ecp_tls_read_group( ecp_group *grp, const unsigned char **buf, size_t len );
|
2012-11-07 20:24:05 +01:00
|
|
|
|
2013-02-10 12:06:19 +01:00
|
|
|
/**
|
|
|
|
* \brief Write the TLS ECParameters record for a group
|
|
|
|
*
|
|
|
|
* \param grp ECP group used
|
|
|
|
* \param olen Number of bytes actually written
|
|
|
|
* \param buf Buffer to write to
|
|
|
|
* \param blen Buffer length
|
|
|
|
*
|
|
|
|
* \return 0 if successful,
|
|
|
|
* or POLARSSL_ERR_ECP_BUFFER_TOO_SMALL
|
|
|
|
*/
|
|
|
|
int ecp_tls_write_group( const ecp_group *grp, size_t *olen,
|
|
|
|
unsigned char *buf, size_t blen );
|
|
|
|
|
2013-09-16 16:19:53 +02:00
|
|
|
/**
|
|
|
|
* \brief Get a TLS NamedCurve value from an internal group identifier
|
|
|
|
*
|
|
|
|
* \param grp_id A POLARSSL_ECP_DP_XXX value
|
|
|
|
*
|
|
|
|
* \return The associated TLS NamedCurve value on success,
|
|
|
|
* 0 on failure.
|
|
|
|
*/
|
2013-09-16 17:30:04 +02:00
|
|
|
uint16_t ecp_named_curve_from_grp_id( ecp_group_id id );
|
2013-09-16 16:19:53 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Get an internal group identifier from a TLS NamedCurve value
|
|
|
|
*
|
|
|
|
* \param curve A value from TLS's enum NamedCurve
|
|
|
|
*
|
|
|
|
* \return The associated POLARSSL_ECP_DP_XXX identifer on success,
|
|
|
|
* POLARSSL_ECP_DP_NONE on failure.
|
|
|
|
*/
|
2013-09-16 17:30:04 +02:00
|
|
|
ecp_group_id ecp_grp_id_from_named_curve( uint16_t curve );
|
2013-09-16 16:19:53 +02:00
|
|
|
|
2013-02-09 19:00:07 +01:00
|
|
|
/**
|
|
|
|
* \brief Import a point from a TLS ECPoint record
|
|
|
|
*
|
|
|
|
* \param grp ECP group used
|
|
|
|
* \param pt Destination point
|
2013-02-10 13:38:29 +01:00
|
|
|
* \param buf $(Start of input buffer)
|
2013-02-09 19:00:07 +01:00
|
|
|
* \param len Buffer length
|
|
|
|
*
|
|
|
|
* \return O if successful,
|
|
|
|
* POLARSSL_ERR_MPI_XXX if initialization failed
|
|
|
|
* POLARSSL_ERR_ECP_BAD_INPUT_DATA if input is invalid
|
|
|
|
*/
|
|
|
|
int ecp_tls_read_point( const ecp_group *grp, ecp_point *pt,
|
2013-02-10 13:38:29 +01:00
|
|
|
const unsigned char **buf, size_t len );
|
2013-02-09 19:00:07 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Export a point as a TLS ECPoint record
|
|
|
|
*
|
|
|
|
* \param grp ECP group used
|
|
|
|
* \param pt Point to export
|
|
|
|
* \param format Export format
|
2013-09-10 16:16:50 +02:00
|
|
|
* \param olen length of data written
|
2013-02-09 19:00:07 +01:00
|
|
|
* \param buf Buffer to write to
|
2013-09-10 16:16:50 +02:00
|
|
|
* \param blen Buffer length
|
2013-02-09 19:00:07 +01:00
|
|
|
*
|
|
|
|
* \return 0 if successful,
|
|
|
|
* or POLARSSL_ERR_ECP_BAD_INPUT_DATA
|
|
|
|
* or POLARSSL_ERR_ECP_BUFFER_TOO_SMALL
|
|
|
|
*/
|
|
|
|
int ecp_tls_write_point( const ecp_group *grp, const ecp_point *pt,
|
2013-02-10 12:22:46 +01:00
|
|
|
int format, size_t *olen,
|
|
|
|
unsigned char *buf, size_t blen );
|
2013-02-09 19:00:07 +01:00
|
|
|
|
2012-10-31 09:26:55 +01:00
|
|
|
/**
|
|
|
|
* \brief Addition: R = P + Q
|
|
|
|
*
|
|
|
|
* \param grp ECP group
|
|
|
|
* \param R Destination point
|
|
|
|
* \param P Left-hand point
|
|
|
|
* \param Q Right-hand point
|
|
|
|
*
|
2012-11-05 10:06:12 +01:00
|
|
|
* \return 0 if successful,
|
2012-11-10 00:27:12 +01:00
|
|
|
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
2012-10-31 09:26:55 +01:00
|
|
|
*/
|
|
|
|
int ecp_add( const ecp_group *grp, ecp_point *R,
|
|
|
|
const ecp_point *P, const ecp_point *Q );
|
|
|
|
|
2012-11-19 21:23:27 +01:00
|
|
|
/**
|
|
|
|
* \brief Subtraction: R = P - Q
|
|
|
|
*
|
|
|
|
* \param grp ECP group
|
|
|
|
* \param R Destination point
|
|
|
|
* \param P Left-hand point
|
|
|
|
* \param Q Right-hand point
|
|
|
|
*
|
|
|
|
* \return 0 if successful,
|
|
|
|
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
|
|
|
*/
|
|
|
|
int ecp_sub( const ecp_group *grp, ecp_point *R,
|
|
|
|
const ecp_point *P, const ecp_point *Q );
|
|
|
|
|
2012-10-31 09:26:55 +01:00
|
|
|
/**
|
|
|
|
* \brief Multiplication by an integer: R = m * P
|
|
|
|
*
|
|
|
|
* \param grp ECP group
|
|
|
|
* \param R Destination point
|
|
|
|
* \param m Integer by which to multiply
|
|
|
|
* \param P Point to multiply
|
2013-09-02 14:29:09 +02:00
|
|
|
* \param f_rng RNG function (see notes)
|
|
|
|
* \param p_rng RNG parameter
|
2012-10-31 09:26:55 +01:00
|
|
|
*
|
2012-11-05 10:06:12 +01:00
|
|
|
* \return 0 if successful,
|
2012-11-10 00:27:12 +01:00
|
|
|
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
2013-09-02 14:29:09 +02:00
|
|
|
* POLARSSL_ERR_ECP_BAD_INPUT_DATA if m < 0 of m has greater
|
|
|
|
* bit length than N, the number of points in the group.
|
2012-11-21 13:00:58 +01:00
|
|
|
*
|
2013-09-02 14:29:09 +02:00
|
|
|
* \note In order to prevent simple timing attacks, this function
|
|
|
|
* executes a constant number of operations (that is, point
|
|
|
|
* doubling and addition of distinct points) for random m in
|
|
|
|
* the allowed range.
|
|
|
|
*
|
|
|
|
* \note If f_rng is not NULL, it is used to randomize projective
|
|
|
|
* coordinates of indermediate results, in order to prevent
|
|
|
|
* more elaborate timing attacks relying on intermediate
|
2013-09-07 11:52:27 +02:00
|
|
|
* operations. (This is a prophylactic measure since no such
|
|
|
|
* attack has been published yet.) Since this contermeasure
|
|
|
|
* has very low overhead, it is recommended to always provide
|
|
|
|
* a non-NULL f_rng parameter when using secret inputs.
|
2012-10-31 09:26:55 +01:00
|
|
|
*/
|
2012-10-31 19:37:54 +01:00
|
|
|
int ecp_mul( const ecp_group *grp, ecp_point *R,
|
2013-09-02 14:29:09 +02:00
|
|
|
const mpi *m, const ecp_point *P,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
|
|
|
|
|
2012-10-31 09:26:55 +01:00
|
|
|
|
2013-07-01 14:06:13 +02:00
|
|
|
/**
|
|
|
|
* \brief Check that a point is a valid public key on this curve
|
|
|
|
*
|
|
|
|
* \param grp Curve/group the point should belong to
|
|
|
|
* \param pt Point to check
|
|
|
|
*
|
|
|
|
* \return 0 if point is a valid public key,
|
|
|
|
* POLARSSL_ERR_ECP_GENERIC otherwise.
|
|
|
|
*
|
|
|
|
* \note This function only checks the point is non-zero, has valid
|
|
|
|
* coordinates and lies on the curve, but not that it is
|
|
|
|
* indeed a multiple of G. This is additional check is more
|
|
|
|
* expensive, isn't required by standards, and shouldn't 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.
|
|
|
|
*
|
|
|
|
* \note Uses bare components rather than an ecp_keypair structure
|
|
|
|
* in order to ease use with other structures such as
|
|
|
|
* ecdh_context of ecdsa_context.
|
|
|
|
*/
|
|
|
|
int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Check that an mpi is a valid private key for this curve
|
|
|
|
*
|
|
|
|
* \param grp Group used
|
|
|
|
* \param d Integer to check
|
|
|
|
*
|
|
|
|
* \return 0 if point is a valid private key,
|
|
|
|
* POLARSSL_ERR_ECP_GENERIC otherwise.
|
|
|
|
*
|
|
|
|
* \note Uses bare components rather than an ecp_keypair structure
|
|
|
|
* in order to ease use with other structures such as
|
|
|
|
* ecdh_context of ecdsa_context.
|
|
|
|
*/
|
2013-07-09 16:05:52 +02:00
|
|
|
int ecp_check_privkey( const ecp_group *grp, const mpi *d );
|
2013-07-01 14:06:13 +02:00
|
|
|
|
2013-01-26 14:42:45 +01:00
|
|
|
/**
|
|
|
|
* \brief Generate a keypair
|
|
|
|
*
|
|
|
|
* \param grp ECP group
|
|
|
|
* \param d Destination MPI (secret part)
|
|
|
|
* \param Q Destination point (public part)
|
|
|
|
* \param f_rng RNG function
|
|
|
|
* \param p_rng RNG parameter
|
|
|
|
*
|
|
|
|
* \return 0 if successful,
|
|
|
|
* or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code
|
2013-07-01 14:06:13 +02:00
|
|
|
*
|
|
|
|
* \note Uses bare components rather than an ecp_keypair structure
|
|
|
|
* in order to ease use with other structures such as
|
|
|
|
* ecdh_context of ecdsa_context.
|
2013-01-26 14:42:45 +01:00
|
|
|
*/
|
|
|
|
int ecp_gen_keypair( const ecp_group *grp, mpi *d, ecp_point *Q,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t),
|
|
|
|
void *p_rng );
|
|
|
|
|
2012-10-31 09:26:55 +01:00
|
|
|
/**
|
|
|
|
* \brief Checkup routine
|
|
|
|
*
|
|
|
|
* \return 0 if successful, or 1 if the test failed
|
|
|
|
*/
|
|
|
|
int ecp_self_test( int verbose );
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|