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 | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 30 | * GECC = Guide to Elliptic Curve Cryptography - Hankerson, Menezes, Vanstone |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 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 | b505c27 | 2012-11-05 17:27:54 +0100 | [diff] [blame] | 40 | * Initialize (the components of) a point |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 41 | */ |
| 42 | void ecp_point_init( ecp_point *pt ) |
| 43 | { |
| 44 | if( pt == NULL ) |
| 45 | return; |
| 46 | |
| 47 | pt->is_zero = 1; |
Manuel Pégourié-Gonnard | b505c27 | 2012-11-05 17:27:54 +0100 | [diff] [blame] | 48 | mpi_init( &pt->X ); |
| 49 | mpi_init( &pt->Y ); |
| 50 | } |
| 51 | |
| 52 | /* |
| 53 | * Initialize (the components of) a group |
| 54 | */ |
| 55 | void ecp_group_init( ecp_group *grp ) |
| 56 | { |
| 57 | if( grp == NULL ) |
| 58 | return; |
| 59 | |
| 60 | mpi_init( &grp->P ); |
| 61 | mpi_init( &grp->B ); |
| 62 | ecp_point_init( &grp->G ); |
| 63 | mpi_init( &grp->N ); |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /* |
Manuel Pégourié-Gonnard | 1e8c8ec | 2012-10-31 19:24:21 +0100 | [diff] [blame] | 67 | * Unallocate (the components of) a point |
| 68 | */ |
| 69 | void ecp_point_free( ecp_point *pt ) |
| 70 | { |
| 71 | if( pt == NULL ) |
| 72 | return; |
| 73 | |
Manuel Pégourié-Gonnard | 5179e46 | 2012-10-31 19:37:54 +0100 | [diff] [blame] | 74 | pt->is_zero = 1; |
Manuel Pégourié-Gonnard | 1e8c8ec | 2012-10-31 19:24:21 +0100 | [diff] [blame] | 75 | mpi_free( &( pt->X ) ); |
| 76 | mpi_free( &( pt->Y ) ); |
| 77 | } |
| 78 | |
| 79 | /* |
| 80 | * Unallocate (the components of) a group |
| 81 | */ |
| 82 | void ecp_group_free( ecp_group *grp ) |
| 83 | { |
| 84 | if( grp == NULL ) |
| 85 | return; |
| 86 | |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 87 | mpi_free( &grp->P ); |
| 88 | mpi_free( &grp->B ); |
| 89 | ecp_point_free( &grp->G ); |
| 90 | mpi_free( &grp->N ); |
Manuel Pégourié-Gonnard | 1e8c8ec | 2012-10-31 19:24:21 +0100 | [diff] [blame] | 91 | } |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 92 | |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 93 | /* |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 94 | * Set point to zero |
| 95 | */ |
| 96 | void ecp_set_zero( ecp_point *pt ) |
| 97 | { |
| 98 | pt->is_zero = 1; |
Manuel Pégourié-Gonnard | b505c27 | 2012-11-05 17:27:54 +0100 | [diff] [blame] | 99 | mpi_free( &pt->X ); |
| 100 | mpi_free( &pt->Y ); |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /* |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 104 | * Copy the contents of Q into P |
| 105 | */ |
| 106 | int ecp_copy( ecp_point *P, const ecp_point *Q ) |
| 107 | { |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 108 | int ret = 0; |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 109 | |
Manuel Pégourié-Gonnard | b505c27 | 2012-11-05 17:27:54 +0100 | [diff] [blame] | 110 | if( Q->is_zero ) { |
| 111 | ecp_set_zero( P ); |
| 112 | return( ret ); |
| 113 | } |
| 114 | |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 115 | P->is_zero = Q->is_zero; |
| 116 | MPI_CHK( mpi_copy( &P->X, &Q->X ) ); |
| 117 | MPI_CHK( mpi_copy( &P->Y, &Q->Y ) ); |
| 118 | |
| 119 | cleanup: |
| 120 | return( ret ); |
| 121 | } |
Manuel Pégourié-Gonnard | 5179e46 | 2012-10-31 19:37:54 +0100 | [diff] [blame] | 122 | |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 123 | /* |
Manuel Pégourié-Gonnard | 847395a | 2012-11-05 13:13:44 +0100 | [diff] [blame] | 124 | * Import a non-zero point from ASCII strings |
| 125 | */ |
| 126 | int ecp_point_read_string( ecp_point *P, int radix, |
| 127 | const char *x, const char *y ) |
| 128 | { |
| 129 | int ret = 0; |
| 130 | |
| 131 | P->is_zero = 0; |
| 132 | MPI_CHK( mpi_read_string( &P->X, radix, x ) ); |
| 133 | MPI_CHK( mpi_read_string( &P->Y, radix, y ) ); |
| 134 | |
| 135 | cleanup: |
| 136 | return( ret ); |
| 137 | } |
| 138 | |
| 139 | /* |
| 140 | * Import an ECP group from ASCII strings |
| 141 | */ |
| 142 | int ecp_group_read_string( ecp_group *grp, int radix, |
| 143 | const char *p, const char *b, |
| 144 | const char *gx, const char *gy, const char *n) |
| 145 | { |
| 146 | int ret = 0; |
| 147 | |
| 148 | MPI_CHK( mpi_read_string( &grp->P, radix, p ) ); |
| 149 | MPI_CHK( mpi_read_string( &grp->B, radix, b ) ); |
| 150 | MPI_CHK( ecp_point_read_string( &grp->G, radix, gx, gy ) ); |
| 151 | MPI_CHK( mpi_read_string( &grp->N, radix, n ) ); |
| 152 | |
| 153 | cleanup: |
| 154 | return( ret ); |
| 155 | } |
| 156 | |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 157 | /* |
| 158 | * Set a group using well-known domain parameters |
| 159 | */ |
| 160 | int ecp_use_known_dp( ecp_group *grp, size_t index ) |
| 161 | { |
| 162 | switch( index ) |
| 163 | { |
| 164 | case POLARSSL_ECP_DP_SECP192R1: |
| 165 | return( ecp_group_read_string( grp, 16, |
| 166 | POLARSSL_ECP_SECP192R1_P, |
| 167 | POLARSSL_ECP_SECP192R1_B, |
| 168 | POLARSSL_ECP_SECP192R1_GX, |
| 169 | POLARSSL_ECP_SECP192R1_GY, |
| 170 | POLARSSL_ECP_SECP192R1_N ) |
| 171 | ); |
| 172 | case POLARSSL_ECP_DP_SECP224R1: |
| 173 | return( ecp_group_read_string( grp, 16, |
| 174 | POLARSSL_ECP_SECP224R1_P, |
| 175 | POLARSSL_ECP_SECP224R1_B, |
| 176 | POLARSSL_ECP_SECP224R1_GX, |
| 177 | POLARSSL_ECP_SECP224R1_GY, |
| 178 | POLARSSL_ECP_SECP224R1_N ) |
| 179 | ); |
| 180 | case POLARSSL_ECP_DP_SECP256R1: |
| 181 | return( ecp_group_read_string( grp, 16, |
| 182 | POLARSSL_ECP_SECP256R1_P, |
| 183 | POLARSSL_ECP_SECP256R1_B, |
| 184 | POLARSSL_ECP_SECP256R1_GX, |
| 185 | POLARSSL_ECP_SECP256R1_GY, |
| 186 | POLARSSL_ECP_SECP256R1_N ) |
| 187 | ); |
| 188 | case POLARSSL_ECP_DP_SECP384R1: |
| 189 | return( ecp_group_read_string( grp, 16, |
| 190 | POLARSSL_ECP_SECP384R1_P, |
| 191 | POLARSSL_ECP_SECP384R1_B, |
| 192 | POLARSSL_ECP_SECP384R1_GX, |
| 193 | POLARSSL_ECP_SECP384R1_GY, |
| 194 | POLARSSL_ECP_SECP384R1_N ) |
| 195 | ); |
| 196 | case POLARSSL_ECP_DP_SECP521R1: |
| 197 | return( ecp_group_read_string( grp, 16, |
| 198 | POLARSSL_ECP_SECP521R1_P, |
| 199 | POLARSSL_ECP_SECP521R1_B, |
| 200 | POLARSSL_ECP_SECP521R1_GX, |
| 201 | POLARSSL_ECP_SECP521R1_GY, |
| 202 | POLARSSL_ECP_SECP521R1_N ) |
| 203 | ); |
| 204 | } |
| 205 | |
| 206 | return( POLARSSL_ERR_ECP_GENERIC ); |
| 207 | } |
Manuel Pégourié-Gonnard | ab38b70 | 2012-11-05 17:34:55 +0100 | [diff] [blame] | 208 | |
Manuel Pégourié-Gonnard | 847395a | 2012-11-05 13:13:44 +0100 | [diff] [blame] | 209 | /* |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 210 | * Internal point format used for fast addition/doubling/multiplication: |
| 211 | * Jacobian coordinates (GECC example 3.20) |
| 212 | */ |
| 213 | typedef struct |
| 214 | { |
| 215 | mpi X, Y, Z; |
| 216 | } |
| 217 | ecp_ptjac; |
| 218 | |
| 219 | /* |
Manuel Pégourié-Gonnard | 989c32b | 2012-11-08 22:02:42 +0100 | [diff] [blame^] | 220 | * Initialize a point in Jacobian coordinates |
| 221 | */ |
| 222 | static void ecp_ptjac_init( ecp_ptjac *P ) |
| 223 | { |
| 224 | mpi_init( &P->X ); mpi_init( &P->Y ); mpi_init( &P->Z ); |
| 225 | } |
| 226 | |
| 227 | /* |
| 228 | * Free a point in Jacobian cooridnates |
| 229 | */ |
| 230 | static void ecp_ptjac_free( ecp_ptjac *P ) |
| 231 | { |
| 232 | mpi_free( &P->X ); mpi_free( &P->Y ); mpi_free( &P->Z ); |
| 233 | } |
| 234 | |
| 235 | /* |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 236 | * Convert from affine to Jacobian coordinates |
| 237 | */ |
Manuel Pégourié-Gonnard | 989c32b | 2012-11-08 22:02:42 +0100 | [diff] [blame^] | 238 | static int ecp_aff_to_jac( ecp_ptjac *jac, const ecp_point *aff ) |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 239 | { |
| 240 | int ret = 0; |
| 241 | |
| 242 | if( aff->is_zero ) |
| 243 | { |
| 244 | MPI_CHK( mpi_lset( &jac->X, 1 ) ); |
| 245 | MPI_CHK( mpi_lset( &jac->Y, 1 ) ); |
| 246 | MPI_CHK( mpi_lset( &jac->Z, 0 ) ); |
| 247 | } |
| 248 | else |
| 249 | { |
| 250 | MPI_CHK( mpi_copy( &jac->X, &aff->X ) ); |
| 251 | MPI_CHK( mpi_copy( &jac->Y, &aff->Y ) ); |
| 252 | MPI_CHK( mpi_lset( &jac->Z, 1 ) ); |
| 253 | } |
| 254 | |
| 255 | cleanup: |
| 256 | return( ret ); |
| 257 | } |
| 258 | |
| 259 | /* |
| 260 | * Convert from Jacobian to affine coordinates |
| 261 | */ |
| 262 | static int ecp_jac_to_aff( const ecp_group *grp, |
Manuel Pégourié-Gonnard | 989c32b | 2012-11-08 22:02:42 +0100 | [diff] [blame^] | 263 | ecp_point *aff, const ecp_ptjac *jac ) |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 264 | { |
| 265 | int ret = 0; |
| 266 | mpi Zi, ZZi, T; |
| 267 | |
| 268 | if( mpi_cmp_int( &jac->Z, 0 ) == 0 ) { |
| 269 | ecp_set_zero( aff ); |
| 270 | return( 0 ); |
| 271 | } |
| 272 | |
| 273 | mpi_init( &Zi ); mpi_init( &ZZi ); mpi_init( &T ); |
| 274 | |
| 275 | aff->is_zero = 0; |
| 276 | |
| 277 | /* |
| 278 | * aff.X = jac.X / (jac.Z)^2 mod p |
| 279 | */ |
| 280 | MPI_CHK( mpi_inv_mod( &Zi, &jac->Z, &grp->P ) ); |
| 281 | MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); |
| 282 | MPI_CHK( mpi_mul_mpi( &T, &jac->X, &ZZi ) ); |
| 283 | MPI_CHK( mpi_mod_mpi( &aff->X, &T, &grp->P ) ); |
| 284 | |
| 285 | /* |
| 286 | * aff.Y = jac.Y / (jac.Z)^3 mod p |
| 287 | */ |
| 288 | MPI_CHK( mpi_mul_mpi( &T, &jac->Y, &ZZi ) ); |
| 289 | MPI_CHK( mpi_mul_mpi( &T, &T, &Zi ) ); |
| 290 | MPI_CHK( mpi_mod_mpi( &aff->Y, &T, &grp->P ) ); |
| 291 | |
| 292 | cleanup: |
| 293 | |
| 294 | mpi_free( &Zi ); mpi_free( &ZZi ); mpi_free( &T ); |
| 295 | |
| 296 | return( ret ); |
| 297 | } |
| 298 | |
| 299 | /* |
Manuel Pégourié-Gonnard | 989c32b | 2012-11-08 22:02:42 +0100 | [diff] [blame^] | 300 | * Point doubling R = 2 P, Jacobian coordinates (GECC 3.21) |
| 301 | */ |
| 302 | static int ecp_double_jac( const ecp_group *grp, ecp_ptjac *R, |
| 303 | const ecp_ptjac *P ) |
| 304 | { |
| 305 | int ret = 0; |
| 306 | mpi T1, T2, T3, X, Y, Z; |
| 307 | |
| 308 | mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 ); |
| 309 | mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z ); |
| 310 | |
| 311 | if( mpi_cmp_int( &P->Z, 0 ) == 0 ) |
| 312 | { |
| 313 | MPI_CHK( mpi_lset( &R->X, 1 ) ); |
| 314 | MPI_CHK( mpi_lset( &R->Y, 1 ) ); |
| 315 | MPI_CHK( mpi_lset( &R->Z, 0 ) ); |
| 316 | goto cleanup; |
| 317 | } |
| 318 | |
| 319 | MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); |
| 320 | MPI_CHK( mpi_sub_mpi( &T2, &P->X, &T1 ) ); |
| 321 | MPI_CHK( mpi_add_mpi( &T1, &P->X, &T1 ) ); |
| 322 | MPI_CHK( mpi_mul_mpi( &T2, &T2, &T1 ) ); |
| 323 | MPI_CHK( mpi_mul_int( &T2, &T2, 3 ) ); |
| 324 | MPI_CHK( mpi_copy ( &Y, &P->Y ) ); |
| 325 | MPI_CHK( mpi_shift_l( &Y, 1 ) ); |
| 326 | MPI_CHK( mpi_mul_mpi( &Z, &Y, &P->Z ) ); |
| 327 | MPI_CHK( mpi_mul_mpi( &Y, &Y, &Y ) ); |
| 328 | MPI_CHK( mpi_mul_mpi( &T3, &Y, &P->X ) ); |
| 329 | MPI_CHK( mpi_mul_mpi( &Y, &Y, &Y ) ); |
| 330 | MPI_CHK( mpi_shift_r( &Y, 1 ) ); |
| 331 | MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); |
| 332 | MPI_CHK( mpi_copy ( &T1, &T3 ) ); |
| 333 | MPI_CHK( mpi_shift_l( &T1, 1 ) ); |
| 334 | MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); |
| 335 | MPI_CHK( mpi_sub_mpi( &T1, &T3, &X ) ); |
| 336 | MPI_CHK( mpi_mul_mpi( &T1, &T1, &T2 ) ); |
| 337 | MPI_CHK( mpi_sub_mpi( &Y, &T1, &Y ) ); |
| 338 | |
| 339 | MPI_CHK( mpi_mod_mpi( &R->X, &X, &grp->P ) ); |
| 340 | MPI_CHK( mpi_mod_mpi( &R->Y, &Y, &grp->P ) ); |
| 341 | MPI_CHK( mpi_mod_mpi( &R->Z, &Z, &grp->P ) ); |
| 342 | |
| 343 | cleanup: |
| 344 | |
| 345 | mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 ); |
| 346 | mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z ); |
| 347 | |
| 348 | return( ret ); |
| 349 | } |
| 350 | |
| 351 | /* |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 352 | * Addition: R = P + Q, generic case (P != Q, P != 0, Q != 0, R != 0) |
| 353 | * Cf SEC1 v2 p. 7, item 4 |
| 354 | */ |
| 355 | static int ecp_add_generic( const ecp_group *grp, ecp_point *R, |
| 356 | const ecp_point *P, const ecp_point *Q ) |
| 357 | { |
| 358 | int ret = 0; |
Manuel Pégourié-Gonnard | ab38b70 | 2012-11-05 17:34:55 +0100 | [diff] [blame] | 359 | mpi DX, DY, K, L, LL, X, Y; |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 360 | |
| 361 | mpi_init( &DX ); mpi_init( &DY ); mpi_init( &K ); mpi_init( &L ); |
Manuel Pégourié-Gonnard | ab38b70 | 2012-11-05 17:34:55 +0100 | [diff] [blame] | 362 | mpi_init( &LL ); mpi_init( &X ); mpi_init( &Y ); |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 363 | |
| 364 | /* |
| 365 | * L = (Q.Y - P.Y) / (Q.X - P.X) mod p |
| 366 | */ |
| 367 | MPI_CHK( mpi_sub_mpi( &DY, &Q->Y, &P->Y ) ); |
| 368 | MPI_CHK( mpi_sub_mpi( &DX, &Q->X, &P->X ) ); |
| 369 | MPI_CHK( mpi_inv_mod( &K, &DX, &grp->P ) ); |
| 370 | MPI_CHK( mpi_mul_mpi( &K, &K, &DY ) ); |
| 371 | MPI_CHK( mpi_mod_mpi( &L, &K, &grp->P ) ); |
| 372 | |
| 373 | /* |
| 374 | * LL = L^2 mod p |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 375 | */ |
| 376 | MPI_CHK( mpi_mul_mpi( &LL, &L, &L ) ); |
| 377 | MPI_CHK( mpi_mod_mpi( &LL, &LL, &grp->P ) ); |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 378 | |
| 379 | /* |
Manuel Pégourié-Gonnard | ab38b70 | 2012-11-05 17:34:55 +0100 | [diff] [blame] | 380 | * X = L^2 - P.X - Q.X |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 381 | */ |
Manuel Pégourié-Gonnard | ab38b70 | 2012-11-05 17:34:55 +0100 | [diff] [blame] | 382 | MPI_CHK( mpi_sub_mpi( &X, &LL, &P->X ) ); |
| 383 | MPI_CHK( mpi_sub_mpi( &X, &X, &Q->X ) ); |
| 384 | |
| 385 | /* |
| 386 | * Y = L * (P.X - X) - P.Y |
| 387 | */ |
| 388 | MPI_CHK( mpi_sub_mpi( &Y, &P->X, &X) ); |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 389 | MPI_CHK( mpi_mul_mpi( &Y, &Y, &L ) ); |
| 390 | MPI_CHK( mpi_sub_mpi( &Y, &Y, &P->Y ) ); |
| 391 | |
| 392 | /* |
| 393 | * R = (X mod p, Y mod p) |
| 394 | */ |
| 395 | R->is_zero = 0; |
| 396 | MPI_CHK( mpi_mod_mpi( &R->X, &X, &grp->P ) ); |
| 397 | MPI_CHK( mpi_mod_mpi( &R->Y, &Y, &grp->P ) ); |
| 398 | |
| 399 | cleanup: |
| 400 | |
| 401 | mpi_free( &DX ); mpi_free( &DY ); mpi_free( &K ); mpi_free( &L ); |
Manuel Pégourié-Gonnard | ab38b70 | 2012-11-05 17:34:55 +0100 | [diff] [blame] | 402 | mpi_free( &LL ); mpi_free( &X ); mpi_free( &Y ); |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 403 | |
| 404 | return( ret ); |
| 405 | } |
| 406 | |
| 407 | /* |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 408 | * Addition: R = P + Q, cf p. 7 of SEC1 v2 |
| 409 | */ |
| 410 | int ecp_add( const ecp_group *grp, ecp_point *R, |
| 411 | const ecp_point *P, const ecp_point *Q ) |
| 412 | { |
| 413 | int ret = 0; |
Manuel Pégourié-Gonnard | 989c32b | 2012-11-08 22:02:42 +0100 | [diff] [blame^] | 414 | ecp_ptjac J; |
| 415 | |
| 416 | ecp_ptjac_init( &J ); |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 417 | |
| 418 | if( P->is_zero ) |
| 419 | { |
| 420 | ret = ecp_copy( R, Q ); |
| 421 | } |
| 422 | else if( Q->is_zero ) |
| 423 | { |
| 424 | ret = ecp_copy( R, P ); |
| 425 | } |
| 426 | else if( mpi_cmp_mpi( &P->X, &Q->X ) != 0 ) |
| 427 | { |
| 428 | ret = ecp_add_generic( grp, R, P, Q ); |
| 429 | } |
Manuel Pégourié-Gonnard | 989c32b | 2012-11-08 22:02:42 +0100 | [diff] [blame^] | 430 | else if( mpi_cmp_mpi( &P->Y, &Q->Y ) != 0 ) |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 431 | { |
| 432 | ecp_set_zero( R ); |
| 433 | } |
| 434 | else |
| 435 | { |
| 436 | /* |
| 437 | * P == Q |
| 438 | */ |
Manuel Pégourié-Gonnard | 989c32b | 2012-11-08 22:02:42 +0100 | [diff] [blame^] | 439 | ecp_aff_to_jac( &J, P ); |
| 440 | MPI_CHK( ecp_double_jac( grp, &J, &J ) ); |
| 441 | MPI_CHK( ecp_jac_to_aff( grp, R, &J ) ); |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 442 | } |
| 443 | |
Manuel Pégourié-Gonnard | 989c32b | 2012-11-08 22:02:42 +0100 | [diff] [blame^] | 444 | cleanup: |
| 445 | |
| 446 | ecp_ptjac_free( &J ); |
| 447 | |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 448 | return ret; |
| 449 | } |
| 450 | |
Manuel Pégourié-Gonnard | efaa31e | 2012-11-06 21:34:35 +0100 | [diff] [blame] | 451 | /* |
Manuel Pégourié-Gonnard | 989c32b | 2012-11-08 22:02:42 +0100 | [diff] [blame^] | 452 | * Integer multiplication: R = m * P (GECC 5.7, SPA-resistant variant) |
Manuel Pégourié-Gonnard | efaa31e | 2012-11-06 21:34:35 +0100 | [diff] [blame] | 453 | */ |
| 454 | int ecp_mul( const ecp_group *grp, ecp_point *R, |
| 455 | const mpi *m, const ecp_point *P ) |
| 456 | { |
| 457 | int ret = 0; |
| 458 | size_t pos; |
Manuel Pégourié-Gonnard | 27b1ba8 | 2012-11-08 18:24:10 +0100 | [diff] [blame] | 459 | ecp_point Q[2]; |
Manuel Pégourié-Gonnard | efaa31e | 2012-11-06 21:34:35 +0100 | [diff] [blame] | 460 | |
Manuel Pégourié-Gonnard | 27b1ba8 | 2012-11-08 18:24:10 +0100 | [diff] [blame] | 461 | ecp_point_init( &Q[0] ); ecp_point_init( &Q[1] ); |
Manuel Pégourié-Gonnard | efaa31e | 2012-11-06 21:34:35 +0100 | [diff] [blame] | 462 | |
| 463 | /* |
Manuel Pégourié-Gonnard | 27b1ba8 | 2012-11-08 18:24:10 +0100 | [diff] [blame] | 464 | * The general method works only for m >= 1 |
Manuel Pégourié-Gonnard | efaa31e | 2012-11-06 21:34:35 +0100 | [diff] [blame] | 465 | */ |
| 466 | if( mpi_cmp_int( m, 0 ) == 0 ) { |
| 467 | ecp_set_zero( R ); |
| 468 | goto cleanup; |
| 469 | } |
| 470 | |
Manuel Pégourié-Gonnard | 27b1ba8 | 2012-11-08 18:24:10 +0100 | [diff] [blame] | 471 | ecp_set_zero( &Q[0] ); |
Manuel Pégourié-Gonnard | efaa31e | 2012-11-06 21:34:35 +0100 | [diff] [blame] | 472 | |
Manuel Pégourié-Gonnard | 989c32b | 2012-11-08 22:02:42 +0100 | [diff] [blame^] | 473 | for( pos = mpi_msb( m ) - 1 ; ; pos-- ) |
Manuel Pégourié-Gonnard | efaa31e | 2012-11-06 21:34:35 +0100 | [diff] [blame] | 474 | { |
Manuel Pégourié-Gonnard | 27b1ba8 | 2012-11-08 18:24:10 +0100 | [diff] [blame] | 475 | MPI_CHK( ecp_add( grp, &Q[0], &Q[0], &Q[0] ) ); |
| 476 | MPI_CHK( ecp_add( grp, &Q[1], &Q[0], P ) ); |
| 477 | MPI_CHK( ecp_copy( &Q[0], &Q[ mpi_get_bit( m, pos ) ] ) ); |
Manuel Pégourié-Gonnard | efaa31e | 2012-11-06 21:34:35 +0100 | [diff] [blame] | 478 | |
| 479 | if( pos == 0 ) |
| 480 | break; |
| 481 | } |
| 482 | |
Manuel Pégourié-Gonnard | 27b1ba8 | 2012-11-08 18:24:10 +0100 | [diff] [blame] | 483 | MPI_CHK( ecp_copy( R, &Q[0] ) ); |
Manuel Pégourié-Gonnard | efaa31e | 2012-11-06 21:34:35 +0100 | [diff] [blame] | 484 | |
| 485 | cleanup: |
| 486 | |
Manuel Pégourié-Gonnard | 27b1ba8 | 2012-11-08 18:24:10 +0100 | [diff] [blame] | 487 | ecp_point_free( &Q[0] ); ecp_point_free( &Q[1] ); |
Manuel Pégourié-Gonnard | efaa31e | 2012-11-06 21:34:35 +0100 | [diff] [blame] | 488 | |
| 489 | return( ret ); |
| 490 | } |
| 491 | |
| 492 | |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 493 | #if defined(POLARSSL_SELF_TEST) |
| 494 | |
Manuel Pégourié-Gonnard | b505c27 | 2012-11-05 17:27:54 +0100 | [diff] [blame] | 495 | /* |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 496 | * Checkup routine |
| 497 | */ |
| 498 | int ecp_self_test( int verbose ) |
| 499 | { |
Manuel Pégourié-Gonnard | 4b8c3f2 | 2012-11-07 21:39:45 +0100 | [diff] [blame] | 500 | return( verbose++ ); |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | #endif |
| 504 | |
| 505 | #endif |