From 39d2adbbd0bde687ff768753aeaea98691788bbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Wed, 31 Oct 2012 09:26:55 +0100 Subject: [PATCH] Added (skeleton) ecp.[ch] --- include/polarssl/config.h | 12 ++++ include/polarssl/ecp.h | 136 ++++++++++++++++++++++++++++++++++++++ library/CMakeLists.txt | 1 + library/ecp.c | 52 +++++++++++++++ 4 files changed, 201 insertions(+) create mode 100644 include/polarssl/ecp.h create mode 100644 library/ecp.c diff --git a/include/polarssl/config.h b/include/polarssl/config.h index dac70e715..2493233b9 100644 --- a/include/polarssl/config.h +++ b/include/polarssl/config.h @@ -829,6 +829,18 @@ * Caller: */ #define POLARSSL_XTEA_C + +/** + * \def POLARSSL_ECP_C + * + * Enable the elliptic curve over GF(p) library. + * + * Module: library/ecp.c + * Caller: + * + * Requires: POLARSSL_BIGNUM_C + */ +#define POLARSSL_ECP_C /* \} name */ #endif /* config.h */ diff --git a/include/polarssl/ecp.h b/include/polarssl/ecp.h new file mode 100644 index 000000000..a616e1663 --- /dev/null +++ b/include/polarssl/ecp.h @@ -0,0 +1,136 @@ +/** + * \file ecp.h + * + * \brief Elliptic curves over GF(p) + * + * Copyright (C) 2012, Brainspark B.V. + * + * This file is part of PolarSSL (http://www.polarssl.org) + * Lead Maintainer: Paul Bakker + * + * 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 + +#include "bignum.h" + +/* + * ECP Error codes + */ + +/** + * \brief ECP point structure (affine coordinates) + */ +typedef struct +{ + mpi X; /*!< the point's X coordinate */ + mpi Y; /*!< the point's Y coordinate */ +} +ecp_point; + +/** + * \brief ECP group structure + * + * The curves we consider are defined by y^2 = x^3 - 3x + b mod p, + * and a generator for a large subgroup is fixed. + */ +typedef struct +{ + 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 */ + unsigned char h; /*!< the cofactor of the subgroup */ +} +ecp_group; + +/** + * RFC 5114 defines a number of standardized ECP groups for use with TLS. + * + * These also are the NIST-recommended ECP groups, are the random ECP groups + * recommended by SECG, and include the two groups used by NSA Suite B. + * + * \warning This library does not support validation of arbitrary domain + * parameters. Therefore, only well-known domain parameters from trusted + * sources (such as the ones below) should be used. + */ + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * \brief Addition: 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, or an POLARSSL_ERR_ECP_XXX error code + */ +int ecp_add( const ecp_group *grp, ecp_point *R, + const ecp_point *P, const ecp_point *Q ); + +/** + * \brief Duplication: R = 2 P + * + * \param grp ECP group + * \param R Destination point + * \param P Point to double + * + * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code + */ +int ecp_double( const ecp_group *grp, ecp_point *R, + const ecp_point *P ); + +/** + * \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 + * + * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code + */ +int ecp_multiply( const ecp_group *grp, ecp_point *R, + const mpi *m, const ecp_point *P ); + +/** + * \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 ); + +/** + * \brief Checkup routine + * + * \return 0 if successful, or 1 if the test failed + */ +int ecp_self_test( int verbose ); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/library/CMakeLists.txt b/library/CMakeLists.txt index aae1f509a..704a4b08f 100644 --- a/library/CMakeLists.txt +++ b/library/CMakeLists.txt @@ -16,6 +16,7 @@ set(src debug.c des.c dhm.c + ecp.c entropy.c entropy_poll.c error.c diff --git a/library/ecp.c b/library/ecp.c new file mode 100644 index 000000000..1caf0c6dd --- /dev/null +++ b/library/ecp.c @@ -0,0 +1,52 @@ +/* + * Elliptic curves over GF(p) + * + * Copyright (C) 2012, Brainspark B.V. + * + * This file is part of PolarSSL (http://www.polarssl.org) + * Lead Maintainer: Paul Bakker + * + * 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. + */ + +/* + * References: + * + * SEC1-v2 (XXX: insert url) + * Guide to Elliptic Curve Cryptography - Hankerson, Menezes, Vanstone + */ + +#include "polarssl/config.h" + +#if defined(POLARSSL_ECP_C) + +#include "polarssl/ecp.h" + + +#if defined(POLARSSL_SELF_TEST) + +/* + * Checkup routine + */ +int ecp_self_test( int verbose ) +{ + return( verbose++ ); +} + +#endif + +#endif