ecp.h
Go to the documentation of this file.
/*
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*
* This file is part of mbed TLS (https://tls.mbed.org)
*/
#ifndef MBEDTLS_ECP_H
#define MBEDTLS_ECP_H
#include "bignum.h"
/*
* ECP error codes
*/
#define MBEDTLS_ERR_ECP_BAD_INPUT_DATA -0x4F80
#define MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL -0x4F00
#define MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE -0x4E80
#define MBEDTLS_ERR_ECP_VERIFY_FAILED -0x4E00
#define MBEDTLS_ERR_ECP_ALLOC_FAILED -0x4D80
#define MBEDTLS_ERR_ECP_RANDOM_FAILED -0x4D00
#define MBEDTLS_ERR_ECP_INVALID_KEY -0x4C80
#define MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH -0x4C00
#define MBEDTLS_ERR_ECP_HW_ACCEL_FAILED -0x4B80
#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
typedef enum
{
MBEDTLS_ECP_DP_NONE = 0,
MBEDTLS_ECP_DP_SECP192R1,
MBEDTLS_ECP_DP_SECP224R1,
MBEDTLS_ECP_DP_SECP256R1,
MBEDTLS_ECP_DP_SECP384R1,
MBEDTLS_ECP_DP_SECP521R1,
MBEDTLS_ECP_DP_BP256R1,
MBEDTLS_ECP_DP_BP384R1,
MBEDTLS_ECP_DP_BP512R1,
MBEDTLS_ECP_DP_CURVE25519,
MBEDTLS_ECP_DP_SECP192K1,
MBEDTLS_ECP_DP_SECP224K1,
MBEDTLS_ECP_DP_SECP256K1,
} mbedtls_ecp_group_id;
#define MBEDTLS_ECP_DP_MAX 12
typedef struct
{
mbedtls_ecp_group_id grp_id;
uint16_t tls_id;
uint16_t bit_size;
const char *name;
} mbedtls_ecp_curve_info;
typedef struct
{
mbedtls_mpi X;
mbedtls_mpi Y;
mbedtls_mpi Z;
}
mbedtls_ecp_point;
typedef struct
{
mbedtls_ecp_group_id id;
mbedtls_mpi P;
mbedtls_mpi A;
mbedtls_mpi B;
mbedtls_ecp_point G;
mbedtls_mpi N;
size_t pbits;
size_t nbits;
unsigned int h;
int (*modp)(mbedtls_mpi *);
int (*t_pre)(mbedtls_ecp_point *, void *);
int (*t_post)(mbedtls_ecp_point *, void *);
void *t_data;
mbedtls_ecp_point *T;
size_t T_size;
}
mbedtls_ecp_group;
typedef struct
{
mbedtls_ecp_group grp;
mbedtls_mpi d;
mbedtls_ecp_point Q;
}
mbedtls_ecp_keypair;
#if !defined(MBEDTLS_ECP_MAX_BITS)
#define MBEDTLS_ECP_MAX_BITS 521
#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)
/*
* 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
*/
#define MBEDTLS_ECP_WINDOW_SIZE 6
#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.
*/
#define MBEDTLS_ECP_FIXED_POINT_OPTIM 1
#endif /* MBEDTLS_ECP_FIXED_POINT_OPTIM */
/* \} name SECTION: Module settings */
/*
* Point formats, from RFC 4492's enum ECPointFormat
*/
#define MBEDTLS_ECP_PF_UNCOMPRESSED 0
#define MBEDTLS_ECP_PF_COMPRESSED 1
/*
* Some other constants from RFC 4492
*/
#define MBEDTLS_ECP_TLS_NAMED_CURVE 3
const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list( void );
const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list( void );
const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_grp_id( mbedtls_ecp_group_id grp_id );
const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id( uint16_t tls_id );
const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name( const char *name );
void mbedtls_ecp_point_init( mbedtls_ecp_point *pt );
void mbedtls_ecp_group_init( mbedtls_ecp_group *grp );
void mbedtls_ecp_keypair_init( mbedtls_ecp_keypair *key );
void mbedtls_ecp_point_free( mbedtls_ecp_point *pt );
void mbedtls_ecp_group_free( mbedtls_ecp_group *grp );
void mbedtls_ecp_keypair_free( mbedtls_ecp_keypair *key );
int mbedtls_ecp_copy( mbedtls_ecp_point *P, const mbedtls_ecp_point *Q );
int mbedtls_ecp_group_copy( mbedtls_ecp_group *dst, const mbedtls_ecp_group *src );
int mbedtls_ecp_set_zero( mbedtls_ecp_point *pt );
int mbedtls_ecp_is_zero( mbedtls_ecp_point *pt );
int mbedtls_ecp_point_cmp( const mbedtls_ecp_point *P,
const mbedtls_ecp_point *Q );
int mbedtls_ecp_point_read_string( mbedtls_ecp_point *P, int radix,
const char *x, const char *y );
int mbedtls_ecp_point_write_binary( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *P,
int format, size_t *olen,
unsigned char *buf, size_t buflen );
int mbedtls_ecp_point_read_binary( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P,
const unsigned char *buf, size_t ilen );
int mbedtls_ecp_tls_read_point( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
const unsigned char **buf, size_t len );
int mbedtls_ecp_tls_write_point( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt,
int format, size_t *olen,
unsigned char *buf, size_t blen );
int mbedtls_ecp_group_load( mbedtls_ecp_group *grp, mbedtls_ecp_group_id id );
int mbedtls_ecp_tls_read_group( mbedtls_ecp_group *grp, const unsigned char **buf, size_t len );
int mbedtls_ecp_tls_write_group( const mbedtls_ecp_group *grp, size_t *olen,
unsigned char *buf, size_t blen );
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 );
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 );
int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt );
int mbedtls_ecp_check_privkey( const mbedtls_ecp_group *grp, const mbedtls_mpi *d );
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 );
int mbedtls_ecp_gen_keypair( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng );
int mbedtls_ecp_gen_key( mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
int mbedtls_ecp_check_pub_priv( const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv );
#if defined(MBEDTLS_SELF_TEST)
int mbedtls_ecp_self_test( int verbose );
#endif /* MBEDTLS_SELF_TEST */
#ifdef __cplusplus
}
#endif
#else /* MBEDTLS_ECP_ALT */
#include "ecp_alt.h"
#endif /* MBEDTLS_ECP_ALT */
#endif /* ecp.h */
mbedtls_ecp_curve_info::tls_id
uint16_t tls_id
Definition: ecp.h:94
mbedtls_ecp_is_zero
int mbedtls_ecp_is_zero(mbedtls_ecp_point *pt)
Tell if a point is zero.
mbedtls_ecp_group::N
mbedtls_mpi N
Definition: ecp.h:147
MBEDTLS_ECP_DP_SECP192R1
Definition: ecp.h:67
mbedtls_ecp_point::Z
mbedtls_mpi Z
Definition: ecp.h:112
mbedtls_ecp_muladd
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)
Multiplication and addition of two points by integers: R = m * P + n * Q (Not thread-safe to use same...
mbedtls_ecp_check_pub_priv
int mbedtls_ecp_check_pub_priv(const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv)
Check a public-private key pair.
mbedtls_ecp_point_read_binary
int mbedtls_ecp_point_read_binary(const mbedtls_ecp_group *grp, mbedtls_ecp_point *P, const unsigned char *buf, size_t ilen)
Import a point from unsigned binary data.
mbedtls_ecp_point::Y
mbedtls_mpi Y
Definition: ecp.h:111
mbedtls_ecp_keypair::grp
mbedtls_ecp_group grp
Definition: ecp.h:169
MBEDTLS_ECP_DP_BP512R1
Definition: ecp.h:74
mbedtls_ecp_keypair
ECP key pair structure.
Definition: ecp.h:167
mbedtls_ecp_set_zero
int mbedtls_ecp_set_zero(mbedtls_ecp_point *pt)
Set a point to zero.
mbedtls_ecp_copy
int mbedtls_ecp_copy(mbedtls_ecp_point *P, const mbedtls_ecp_point *Q)
Copy the contents of point Q into P.
mbedtls_ecp_grp_id_list
const mbedtls_ecp_group_id * mbedtls_ecp_grp_id_list(void)
Get the list of supported curves in order of preferrence (grp_id only)
mbedtls_ecp_group_copy
int mbedtls_ecp_group_copy(mbedtls_ecp_group *dst, const mbedtls_ecp_group *src)
Copy the contents of a group object.
MBEDTLS_ECP_DP_SECP224K1
Definition: ecp.h:77
MBEDTLS_ECP_DP_NONE
Definition: ecp.h:66
mbedtls_ecp_group::nbits
size_t nbits
Definition: ecp.h:149
mbedtls_ecp_gen_keypair
int mbedtls_ecp_gen_keypair(mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Generate a keypair.
mbedtls_ecp_point_free
void mbedtls_ecp_point_free(mbedtls_ecp_point *pt)
Free the components of a point.
mbedtls_ecp_keypair_init
void mbedtls_ecp_keypair_init(mbedtls_ecp_keypair *key)
Initialize a key pair (as an invalid one)
mbedtls_ecp_group::pbits
size_t pbits
Definition: ecp.h:148
mbedtls_ecp_point::X
mbedtls_mpi X
Definition: ecp.h:110
mbedtls_ecp_curve_info
Definition: ecp.h:91
bignum.h
Multi-precision integer library.
mbedtls_ecp_gen_key
int mbedtls_ecp_gen_key(mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Generate a keypair.
mbedtls_ecp_group
ECP group structure.
Definition: ecp.h:140
mbedtls_ecp_check_privkey
int mbedtls_ecp_check_privkey(const mbedtls_ecp_group *grp, const mbedtls_mpi *d)
Check that an mbedtls_mpi is a valid private key for this curve.
mbedtls_ecp_group::id
mbedtls_ecp_group_id id
Definition: ecp.h:142
mbedtls_ecp_curve_info_from_grp_id
const mbedtls_ecp_curve_info * mbedtls_ecp_curve_info_from_grp_id(mbedtls_ecp_group_id grp_id)
Get curve information from an internal group identifier.
mbedtls_ecp_tls_write_group
int mbedtls_ecp_tls_write_group(const mbedtls_ecp_group *grp, size_t *olen, unsigned char *buf, size_t blen)
Write the TLS ECParameters record for a group.
mbedtls_ecp_group_free
void mbedtls_ecp_group_free(mbedtls_ecp_group *grp)
Free the components of an ECP group.
mbedtls_ecp_mul
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)
Multiplication by an integer: R = m * P (Not thread-safe to use same group in multiple threads) ...
mbedtls_ecp_check_pubkey
int mbedtls_ecp_check_pubkey(const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt)
Check that a point is a valid public key on this curve.
mbedtls_ecp_group::A
mbedtls_mpi A
Definition: ecp.h:144
MBEDTLS_ECP_DP_SECP192K1
Definition: ecp.h:76
mbedtls_ecp_group::P
mbedtls_mpi P
Definition: ecp.h:143
mbedtls_ecp_keypair_free
void mbedtls_ecp_keypair_free(mbedtls_ecp_keypair *key)
Free the components of a key pair.
mbedtls_ecp_tls_read_group
int mbedtls_ecp_tls_read_group(mbedtls_ecp_group *grp, const unsigned char **buf, size_t len)
Set a group from a TLS ECParameters record.
mbedtls_ecp_keypair::Q
mbedtls_ecp_point Q
Definition: ecp.h:171
mbedtls_ecp_group::t_data
void * t_data
Definition: ecp.h:154
mbedtls_ecp_point_init
void mbedtls_ecp_point_init(mbedtls_ecp_point *pt)
Initialize a point (as zero)
mbedtls_ecp_group_id
mbedtls_ecp_group_id
Definition: ecp.h:64
mbedtls_ecp_point_read_string
int mbedtls_ecp_point_read_string(mbedtls_ecp_point *P, int radix, const char *x, const char *y)
Import a non-zero point from two ASCII strings.
mbedtls_ecp_self_test
int mbedtls_ecp_self_test(int verbose)
Checkup routine.
mbedtls_ecp_point_write_binary
int mbedtls_ecp_point_write_binary(const mbedtls_ecp_group *grp, const mbedtls_ecp_point *P, int format, size_t *olen, unsigned char *buf, size_t buflen)
Export a point into unsigned binary data.
mbedtls_ecp_curve_info_from_tls_id
const mbedtls_ecp_curve_info * mbedtls_ecp_curve_info_from_tls_id(uint16_t tls_id)
Get curve information from a TLS NamedCurve value.
mbedtls_ecp_tls_read_point
int mbedtls_ecp_tls_read_point(const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt, const unsigned char **buf, size_t len)
Import a point from a TLS ECPoint record.
mbedtls_ecp_curve_info::grp_id
mbedtls_ecp_group_id grp_id
Definition: ecp.h:93
mbedtls_ecp_gen_keypair_base
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)
Generate a keypair with configurable base point.
mbedtls_ecp_keypair::d
mbedtls_mpi d
Definition: ecp.h:170
MBEDTLS_ECP_DP_SECP224R1
Definition: ecp.h:68
mbedtls_ecp_group::h
unsigned int h
Definition: ecp.h:150
mbedtls_ecp_point_cmp
int mbedtls_ecp_point_cmp(const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q)
Compare two points.
mbedtls_ecp_tls_write_point
int mbedtls_ecp_tls_write_point(const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt, int format, size_t *olen, unsigned char *buf, size_t blen)
Export a point as a TLS ECPoint record.
mbedtls_ecp_group::T_size
size_t T_size
Definition: ecp.h:156
MBEDTLS_ECP_DP_CURVE25519
Definition: ecp.h:75
mbedtls_ecp_group::T
mbedtls_ecp_point * T
Definition: ecp.h:155
mbedtls_ecp_group::G
mbedtls_ecp_point G
Definition: ecp.h:146
mbedtls_mpi
MPI structure.
Definition: bignum.h:180
mbedtls_ecp_point
ECP point structure (jacobian coordinates)
Definition: ecp.h:108
MBEDTLS_ECP_DP_SECP256R1
Definition: ecp.h:69
mbedtls_ecp_curve_info_from_name
const mbedtls_ecp_curve_info * mbedtls_ecp_curve_info_from_name(const char *name)
Get curve information from a human-readable name.
MBEDTLS_ECP_DP_SECP384R1
Definition: ecp.h:70
mbedtls_ecp_curve_info::name
const char * name
Definition: ecp.h:96
MBEDTLS_ECP_DP_BP256R1
Definition: ecp.h:72
MBEDTLS_ECP_DP_SECP521R1
Definition: ecp.h:71
mbedtls_ecp_group_load
int mbedtls_ecp_group_load(mbedtls_ecp_group *grp, mbedtls_ecp_group_id id)
Set a group using well-known domain parameters.
MBEDTLS_ECP_DP_BP384R1
Definition: ecp.h:73
mbedtls_ecp_curve_info::bit_size
uint16_t bit_size
Definition: ecp.h:95
MBEDTLS_ECP_DP_SECP256K1
Definition: ecp.h:78
mbedtls_ecp_group::B
mbedtls_mpi B
Definition: ecp.h:145
mbedtls_ecp_group_init
void mbedtls_ecp_group_init(mbedtls_ecp_group *grp)
Initialize a group (to something meaningless)