Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Elliptic curves over GF(p) |
| 3 | * |
| 4 | * Copyright (C) 2012, Brainspark B.V. |
| 5 | * |
| 6 | * This file is part of PolarSSL (http://www.polarssl.org) |
| 7 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
| 8 | * |
| 9 | * All rights reserved. |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along |
| 22 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 24 | */ |
| 25 | |
| 26 | /* |
| 27 | * References: |
| 28 | * |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 29 | * SEC1 http://www.secg.org/index.php?action=secg,docs_secg |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 30 | * Guide to Elliptic Curve Cryptography - Hankerson, Menezes, Vanstone |
| 31 | */ |
| 32 | |
| 33 | #include "polarssl/config.h" |
| 34 | |
| 35 | #if defined(POLARSSL_ECP_C) |
| 36 | |
| 37 | #include "polarssl/ecp.h" |
| 38 | |
Manuel Pégourié-Gonnard | 1e8c8ec | 2012-10-31 19:24:21 +0100 | [diff] [blame] | 39 | /* |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame^] | 40 | * Initialize a point |
| 41 | */ |
| 42 | void ecp_point_init( ecp_point *pt ) |
| 43 | { |
| 44 | if( pt == NULL ) |
| 45 | return; |
| 46 | |
| 47 | pt->is_zero = 1; |
| 48 | mpi_init( &( pt->X ) ); |
| 49 | mpi_init( &( pt->Y ) ); |
| 50 | } |
| 51 | |
| 52 | /* |
Manuel Pégourié-Gonnard | 1e8c8ec | 2012-10-31 19:24:21 +0100 | [diff] [blame] | 53 | * Unallocate (the components of) a point |
| 54 | */ |
| 55 | void ecp_point_free( ecp_point *pt ) |
| 56 | { |
| 57 | if( pt == NULL ) |
| 58 | return; |
| 59 | |
Manuel Pégourié-Gonnard | 5179e46 | 2012-10-31 19:37:54 +0100 | [diff] [blame] | 60 | pt->is_zero = 1; |
Manuel Pégourié-Gonnard | 1e8c8ec | 2012-10-31 19:24:21 +0100 | [diff] [blame] | 61 | mpi_free( &( pt->X ) ); |
| 62 | mpi_free( &( pt->Y ) ); |
| 63 | } |
| 64 | |
| 65 | /* |
| 66 | * Unallocate (the components of) a group |
| 67 | */ |
| 68 | void ecp_group_free( ecp_group *grp ) |
| 69 | { |
| 70 | if( grp == NULL ) |
| 71 | return; |
| 72 | |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 73 | mpi_free( &grp->P ); |
| 74 | mpi_free( &grp->B ); |
| 75 | ecp_point_free( &grp->G ); |
| 76 | mpi_free( &grp->N ); |
Manuel Pégourié-Gonnard | 1e8c8ec | 2012-10-31 19:24:21 +0100 | [diff] [blame] | 77 | } |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 78 | |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 79 | /* |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame^] | 80 | * Set point to zero |
| 81 | */ |
| 82 | void ecp_set_zero( ecp_point *pt ) |
| 83 | { |
| 84 | pt->is_zero = 1; |
| 85 | mpi_free( &( pt->X ) ); |
| 86 | mpi_free( &( pt->Y ) ); |
| 87 | } |
| 88 | |
| 89 | /* |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 90 | * Copy the contents of Q into P |
| 91 | */ |
| 92 | int ecp_copy( ecp_point *P, const ecp_point *Q ) |
| 93 | { |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame^] | 94 | int ret = 0; |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 95 | |
| 96 | P->is_zero = Q->is_zero; |
| 97 | MPI_CHK( mpi_copy( &P->X, &Q->X ) ); |
| 98 | MPI_CHK( mpi_copy( &P->Y, &Q->Y ) ); |
| 99 | |
| 100 | cleanup: |
| 101 | return( ret ); |
| 102 | } |
Manuel Pégourié-Gonnard | 5179e46 | 2012-10-31 19:37:54 +0100 | [diff] [blame] | 103 | |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame^] | 104 | /* |
| 105 | * Addition: R = P + Q, generic case (P != Q, P != 0, Q != 0, R != 0) |
| 106 | * Cf SEC1 v2 p. 7, item 4 |
| 107 | */ |
| 108 | static int ecp_add_generic( const ecp_group *grp, ecp_point *R, |
| 109 | const ecp_point *P, const ecp_point *Q ) |
| 110 | { |
| 111 | int ret = 0; |
| 112 | mpi DX, DY, K, L, LL, M, X, Y; |
| 113 | |
| 114 | mpi_init( &DX ); mpi_init( &DY ); mpi_init( &K ); mpi_init( &L ); |
| 115 | mpi_init( &LL ); mpi_init( &M ); mpi_init( &X ); mpi_init( &Y ); |
| 116 | |
| 117 | /* |
| 118 | * L = (Q.Y - P.Y) / (Q.X - P.X) mod p |
| 119 | */ |
| 120 | MPI_CHK( mpi_sub_mpi( &DY, &Q->Y, &P->Y ) ); |
| 121 | MPI_CHK( mpi_sub_mpi( &DX, &Q->X, &P->X ) ); |
| 122 | MPI_CHK( mpi_inv_mod( &K, &DX, &grp->P ) ); |
| 123 | MPI_CHK( mpi_mul_mpi( &K, &K, &DY ) ); |
| 124 | MPI_CHK( mpi_mod_mpi( &L, &K, &grp->P ) ); |
| 125 | |
| 126 | /* |
| 127 | * LL = L^2 mod p |
| 128 | * M = L^2 - Q.X |
| 129 | * X = L^2 - P.X - Q.X |
| 130 | */ |
| 131 | MPI_CHK( mpi_mul_mpi( &LL, &L, &L ) ); |
| 132 | MPI_CHK( mpi_mod_mpi( &LL, &LL, &grp->P ) ); |
| 133 | MPI_CHK( mpi_sub_mpi( &M, &LL, &Q->X ) ); |
| 134 | MPI_CHK( mpi_sub_mpi( &X, &M, &P->X ) ); |
| 135 | |
| 136 | /* |
| 137 | * Y = L * (P.X - X) - P.Y = L * (-M) - P.Y |
| 138 | */ |
| 139 | MPI_CHK( mpi_copy( &Y, &M ) ); |
| 140 | Y.s = - Y.s; |
| 141 | MPI_CHK( mpi_mul_mpi( &Y, &Y, &L ) ); |
| 142 | MPI_CHK( mpi_sub_mpi( &Y, &Y, &P->Y ) ); |
| 143 | |
| 144 | /* |
| 145 | * R = (X mod p, Y mod p) |
| 146 | */ |
| 147 | R->is_zero = 0; |
| 148 | MPI_CHK( mpi_mod_mpi( &R->X, &X, &grp->P ) ); |
| 149 | MPI_CHK( mpi_mod_mpi( &R->Y, &Y, &grp->P ) ); |
| 150 | |
| 151 | cleanup: |
| 152 | |
| 153 | mpi_free( &DX ); mpi_free( &DY ); mpi_free( &K ); mpi_free( &L ); |
| 154 | mpi_free( &LL ); mpi_free( &M ); mpi_free( &X ); mpi_free( &Y ); |
| 155 | |
| 156 | return( ret ); |
| 157 | } |
| 158 | |
| 159 | /* |
| 160 | * Doubling: R = 2 * P, generic case (P != 0, R != 0) |
| 161 | * |
| 162 | * Returns POLARSSL_ERR_MPI_XXX on error. |
| 163 | */ |
| 164 | static int ecp_double_generic( const ecp_group *grp, ecp_point *R, |
| 165 | const ecp_point *P ) |
| 166 | { |
| 167 | int ret = 0; |
| 168 | |
| 169 | (void) grp; |
| 170 | (void) R; |
| 171 | (void) P; |
| 172 | |
| 173 | return( ret ); |
| 174 | } |
| 175 | |
| 176 | /* |
| 177 | * Addition: R = P + Q, cf p. 7 of SEC1 v2 |
| 178 | */ |
| 179 | int ecp_add( const ecp_group *grp, ecp_point *R, |
| 180 | const ecp_point *P, const ecp_point *Q ) |
| 181 | { |
| 182 | int ret = 0; |
| 183 | |
| 184 | if( P->is_zero ) |
| 185 | { |
| 186 | ret = ecp_copy( R, Q ); |
| 187 | } |
| 188 | else if( Q->is_zero ) |
| 189 | { |
| 190 | ret = ecp_copy( R, P ); |
| 191 | } |
| 192 | else if( mpi_cmp_mpi( &P->X, &Q->X ) != 0 ) |
| 193 | { |
| 194 | ret = ecp_add_generic( grp, R, P, Q ); |
| 195 | } |
| 196 | else if( mpi_cmp_int( &P->Y, 0 ) == 0 || |
| 197 | mpi_cmp_mpi( &P->Y, &Q->Y ) != 0 ) |
| 198 | { |
| 199 | ecp_set_zero( R ); |
| 200 | } |
| 201 | else |
| 202 | { |
| 203 | /* |
| 204 | * P == Q |
| 205 | */ |
| 206 | ret = ecp_double_generic( grp, R, P ); |
| 207 | } |
| 208 | |
| 209 | return ret; |
| 210 | } |
| 211 | |
Manuel Pégourié-Gonnard | 5179e46 | 2012-10-31 19:37:54 +0100 | [diff] [blame] | 212 | |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 213 | #if defined(POLARSSL_SELF_TEST) |
| 214 | |
| 215 | /* |
| 216 | * Checkup routine |
| 217 | */ |
| 218 | int ecp_self_test( int verbose ) |
| 219 | { |
| 220 | return( verbose++ ); |
| 221 | } |
| 222 | |
| 223 | #endif |
| 224 | |
| 225 | #endif |