Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame^] | 1 | /** |
| 2 | * \file ecp.h |
| 3 | * |
| 4 | * \brief Elliptic curves over GF(p) |
| 5 | * |
| 6 | * Copyright (C) 2012, Brainspark B.V. |
| 7 | * |
| 8 | * This file is part of PolarSSL (http://www.polarssl.org) |
| 9 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
| 10 | * |
| 11 | * All rights reserved. |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or |
| 16 | * (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License along |
| 24 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 25 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 26 | */ |
| 27 | #ifndef POLARSSL_ECP_H |
| 28 | #define POLARSSL_ECP_H |
| 29 | |
| 30 | #include "bignum.h" |
| 31 | |
| 32 | /* |
| 33 | * ECP Error codes |
| 34 | */ |
| 35 | |
| 36 | /** |
| 37 | * \brief ECP point structure (affine coordinates) |
| 38 | */ |
| 39 | typedef struct |
| 40 | { |
| 41 | mpi X; /*!< the point's X coordinate */ |
| 42 | mpi Y; /*!< the point's Y coordinate */ |
| 43 | } |
| 44 | ecp_point; |
| 45 | |
| 46 | /** |
| 47 | * \brief ECP group structure |
| 48 | * |
| 49 | * The curves we consider are defined by y^2 = x^3 - 3x + b mod p, |
| 50 | * and a generator for a large subgroup is fixed. |
| 51 | */ |
| 52 | typedef struct |
| 53 | { |
| 54 | mpi P; /*!< prime modulus of the base field */ |
| 55 | mpi B; /*!< constant term in the equation */ |
| 56 | ecp_point G; /*!< generator of the subgroup used */ |
| 57 | mpi N; /*!< the order of G */ |
| 58 | unsigned char h; /*!< the cofactor of the subgroup */ |
| 59 | } |
| 60 | ecp_group; |
| 61 | |
| 62 | /** |
| 63 | * RFC 5114 defines a number of standardized ECP groups for use with TLS. |
| 64 | * |
| 65 | * These also are the NIST-recommended ECP groups, are the random ECP groups |
| 66 | * recommended by SECG, and include the two groups used by NSA Suite B. |
| 67 | * |
| 68 | * \warning This library does not support validation of arbitrary domain |
| 69 | * parameters. Therefore, only well-known domain parameters from trusted |
| 70 | * sources (such as the ones below) should be used. |
| 71 | */ |
| 72 | |
| 73 | #ifdef __cplusplus |
| 74 | extern "C" { |
| 75 | #endif |
| 76 | |
| 77 | /** |
| 78 | * \brief Addition: R = P + Q |
| 79 | * |
| 80 | * \param grp ECP group |
| 81 | * \param R Destination point |
| 82 | * \param P Left-hand point |
| 83 | * \param Q Right-hand point |
| 84 | * |
| 85 | * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code |
| 86 | */ |
| 87 | int ecp_add( const ecp_group *grp, ecp_point *R, |
| 88 | const ecp_point *P, const ecp_point *Q ); |
| 89 | |
| 90 | /** |
| 91 | * \brief Duplication: R = 2 P |
| 92 | * |
| 93 | * \param grp ECP group |
| 94 | * \param R Destination point |
| 95 | * \param P Point to double |
| 96 | * |
| 97 | * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code |
| 98 | */ |
| 99 | int ecp_double( const ecp_group *grp, ecp_point *R, |
| 100 | const ecp_point *P ); |
| 101 | |
| 102 | /** |
| 103 | * \brief Multiplication by an integer: R = m * P |
| 104 | * |
| 105 | * \param grp ECP group |
| 106 | * \param R Destination point |
| 107 | * \param m Integer by which to multiply |
| 108 | * \param P Point to multiply |
| 109 | * |
| 110 | * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code |
| 111 | */ |
| 112 | int ecp_multiply( const ecp_group *grp, ecp_point *R, |
| 113 | const mpi *m, const ecp_point *P ); |
| 114 | |
| 115 | /** |
| 116 | * \brief Free the components of a point |
| 117 | */ |
| 118 | void ecp_point_free( ecp_point *pt ); |
| 119 | |
| 120 | /** |
| 121 | * \brief Free the components of an ECP group |
| 122 | */ |
| 123 | void ecp_group_free( ecp_group *grp ); |
| 124 | |
| 125 | /** |
| 126 | * \brief Checkup routine |
| 127 | * |
| 128 | * \return 0 if successful, or 1 if the test failed |
| 129 | */ |
| 130 | int ecp_self_test( int verbose ); |
| 131 | |
| 132 | #ifdef __cplusplus |
| 133 | } |
| 134 | #endif |
| 135 | |
| 136 | #endif |