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 | 62aad14 | 2012-11-10 00:27:12 +0100 | [diff] [blame] | 31 | * FIPS 186-3 http://csrc.nist.gov/publications/fips/fips186-3/fips_186-3.pdf |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 32 | */ |
| 33 | |
| 34 | #include "polarssl/config.h" |
| 35 | |
| 36 | #if defined(POLARSSL_ECP_C) |
| 37 | |
| 38 | #include "polarssl/ecp.h" |
Manuel Pégourié-Gonnard | 8433824 | 2012-11-11 20:45:18 +0100 | [diff] [blame] | 39 | #include <limits.h> |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 40 | |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame^] | 41 | #if defined(POLARSSL_SELF_TEST) |
| 42 | /* |
| 43 | * Counts of point addition and doubling operations. |
| 44 | * Used to test resistance of point multiplication to SPA/timing attacks. |
| 45 | */ |
| 46 | unsigned long add_count, dbl_count; |
| 47 | #endif |
| 48 | |
Manuel Pégourié-Gonnard | 1e8c8ec | 2012-10-31 19:24:21 +0100 | [diff] [blame] | 49 | /* |
Manuel Pégourié-Gonnard | b505c27 | 2012-11-05 17:27:54 +0100 | [diff] [blame] | 50 | * Initialize (the components of) a point |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 51 | */ |
| 52 | void ecp_point_init( ecp_point *pt ) |
| 53 | { |
| 54 | if( pt == NULL ) |
| 55 | return; |
| 56 | |
Manuel Pégourié-Gonnard | b505c27 | 2012-11-05 17:27:54 +0100 | [diff] [blame] | 57 | mpi_init( &pt->X ); |
| 58 | mpi_init( &pt->Y ); |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 59 | mpi_init( &pt->Z ); |
Manuel Pégourié-Gonnard | b505c27 | 2012-11-05 17:27:54 +0100 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | /* |
| 63 | * Initialize (the components of) a group |
| 64 | */ |
| 65 | void ecp_group_init( ecp_group *grp ) |
| 66 | { |
| 67 | if( grp == NULL ) |
| 68 | return; |
| 69 | |
| 70 | mpi_init( &grp->P ); |
| 71 | mpi_init( &grp->B ); |
| 72 | ecp_point_init( &grp->G ); |
| 73 | mpi_init( &grp->N ); |
Manuel Pégourié-Gonnard | 62aad14 | 2012-11-10 00:27:12 +0100 | [diff] [blame] | 74 | |
Manuel Pégourié-Gonnard | 62aad14 | 2012-11-10 00:27:12 +0100 | [diff] [blame] | 75 | grp->pbits = 0; |
Manuel Pégourié-Gonnard | 773ed54 | 2012-11-18 13:19:07 +0100 | [diff] [blame] | 76 | grp->nbits = 0; |
| 77 | |
| 78 | grp->modp = NULL; |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /* |
Manuel Pégourié-Gonnard | 1e8c8ec | 2012-10-31 19:24:21 +0100 | [diff] [blame] | 82 | * Unallocate (the components of) a point |
| 83 | */ |
| 84 | void ecp_point_free( ecp_point *pt ) |
| 85 | { |
| 86 | if( pt == NULL ) |
| 87 | return; |
| 88 | |
| 89 | mpi_free( &( pt->X ) ); |
| 90 | mpi_free( &( pt->Y ) ); |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 91 | mpi_free( &( pt->Z ) ); |
Manuel Pégourié-Gonnard | 1e8c8ec | 2012-10-31 19:24:21 +0100 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /* |
| 95 | * Unallocate (the components of) a group |
| 96 | */ |
| 97 | void ecp_group_free( ecp_group *grp ) |
| 98 | { |
| 99 | if( grp == NULL ) |
| 100 | return; |
| 101 | |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 102 | mpi_free( &grp->P ); |
| 103 | mpi_free( &grp->B ); |
| 104 | ecp_point_free( &grp->G ); |
| 105 | mpi_free( &grp->N ); |
Manuel Pégourié-Gonnard | 1e8c8ec | 2012-10-31 19:24:21 +0100 | [diff] [blame] | 106 | } |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 107 | |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 108 | /* |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 109 | * Set point to zero |
| 110 | */ |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 111 | int ecp_set_zero( ecp_point *pt ) |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 112 | { |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 113 | int ret; |
| 114 | |
| 115 | MPI_CHK( mpi_lset( &pt->X , 1 ) ); |
| 116 | MPI_CHK( mpi_lset( &pt->Y , 1 ) ); |
| 117 | MPI_CHK( mpi_lset( &pt->Z , 0 ) ); |
| 118 | |
| 119 | cleanup: |
| 120 | return( ret ); |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /* |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 124 | * Copy the contents of Q into P |
| 125 | */ |
| 126 | int ecp_copy( ecp_point *P, const ecp_point *Q ) |
| 127 | { |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 128 | int ret; |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 129 | |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 130 | MPI_CHK( mpi_copy( &P->X, &Q->X ) ); |
| 131 | MPI_CHK( mpi_copy( &P->Y, &Q->Y ) ); |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 132 | MPI_CHK( mpi_copy( &P->Z, &Q->Z ) ); |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 133 | |
| 134 | cleanup: |
| 135 | return( ret ); |
| 136 | } |
Manuel Pégourié-Gonnard | 5179e46 | 2012-10-31 19:37:54 +0100 | [diff] [blame] | 137 | |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 138 | /* |
Manuel Pégourié-Gonnard | 847395a | 2012-11-05 13:13:44 +0100 | [diff] [blame] | 139 | * Import a non-zero point from ASCII strings |
| 140 | */ |
| 141 | int ecp_point_read_string( ecp_point *P, int radix, |
| 142 | const char *x, const char *y ) |
| 143 | { |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 144 | int ret; |
Manuel Pégourié-Gonnard | 847395a | 2012-11-05 13:13:44 +0100 | [diff] [blame] | 145 | |
Manuel Pégourié-Gonnard | 847395a | 2012-11-05 13:13:44 +0100 | [diff] [blame] | 146 | MPI_CHK( mpi_read_string( &P->X, radix, x ) ); |
| 147 | MPI_CHK( mpi_read_string( &P->Y, radix, y ) ); |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 148 | MPI_CHK( mpi_lset( &P->Z, 1 ) ); |
Manuel Pégourié-Gonnard | 847395a | 2012-11-05 13:13:44 +0100 | [diff] [blame] | 149 | |
| 150 | cleanup: |
| 151 | return( ret ); |
| 152 | } |
| 153 | |
| 154 | /* |
| 155 | * Import an ECP group from ASCII strings |
| 156 | */ |
| 157 | int ecp_group_read_string( ecp_group *grp, int radix, |
| 158 | const char *p, const char *b, |
| 159 | const char *gx, const char *gy, const char *n) |
| 160 | { |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 161 | int ret; |
Manuel Pégourié-Gonnard | 847395a | 2012-11-05 13:13:44 +0100 | [diff] [blame] | 162 | |
| 163 | MPI_CHK( mpi_read_string( &grp->P, radix, p ) ); |
| 164 | MPI_CHK( mpi_read_string( &grp->B, radix, b ) ); |
| 165 | MPI_CHK( ecp_point_read_string( &grp->G, radix, gx, gy ) ); |
| 166 | MPI_CHK( mpi_read_string( &grp->N, radix, n ) ); |
| 167 | |
Manuel Pégourié-Gonnard | 773ed54 | 2012-11-18 13:19:07 +0100 | [diff] [blame] | 168 | grp->pbits = mpi_msb( &grp->P ); |
| 169 | grp->nbits = mpi_msb( &grp->N ); |
| 170 | |
Manuel Pégourié-Gonnard | 847395a | 2012-11-05 13:13:44 +0100 | [diff] [blame] | 171 | cleanup: |
| 172 | return( ret ); |
| 173 | } |
| 174 | |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 175 | /* |
Manuel Pégourié-Gonnard | 773ed54 | 2012-11-18 13:19:07 +0100 | [diff] [blame] | 176 | * Wrapper around fast quasi-modp functions, with fall-back to mpi_mod_mpi. |
| 177 | * See the documentation of struct ecp_group. |
Manuel Pégourié-Gonnard | 62aad14 | 2012-11-10 00:27:12 +0100 | [diff] [blame] | 178 | */ |
| 179 | static int ecp_modp( mpi *N, const ecp_group *grp ) |
| 180 | { |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 181 | int ret; |
Manuel Pégourié-Gonnard | 62aad14 | 2012-11-10 00:27:12 +0100 | [diff] [blame] | 182 | |
| 183 | if( grp->modp == NULL ) |
| 184 | return( mpi_mod_mpi( N, N, &grp->P ) ); |
| 185 | |
| 186 | if( mpi_cmp_int( N, 0 ) < 0 || mpi_msb( N ) > 2 * grp->pbits ) |
| 187 | return( POLARSSL_ERR_ECP_GENERIC ); |
| 188 | |
| 189 | MPI_CHK( grp->modp( N ) ); |
| 190 | |
| 191 | while( mpi_cmp_int( N, 0 ) < 0 ) |
| 192 | MPI_CHK( mpi_add_mpi( N, N, &grp->P ) ); |
| 193 | |
| 194 | while( mpi_cmp_mpi( N, &grp->P ) >= 0 ) |
| 195 | MPI_CHK( mpi_sub_mpi( N, N, &grp->P ) ); |
| 196 | |
| 197 | cleanup: |
| 198 | return( ret ); |
| 199 | } |
| 200 | |
| 201 | /* |
Manuel Pégourié-Gonnard | 8433824 | 2012-11-11 20:45:18 +0100 | [diff] [blame] | 202 | * 192 bits in terms of t_uint |
| 203 | */ |
| 204 | #define P192_SIZE_INT ( 192 / CHAR_BIT / sizeof( t_uint ) ) |
| 205 | |
| 206 | /* |
| 207 | * Table to get S1, S2, S3 of FIPS 186-3 D.2.1: |
| 208 | * -1 means let this chunk be 0 |
| 209 | * a positive value i means A_i. |
| 210 | */ |
| 211 | #define P192_CHUNKS 3 |
| 212 | #define P192_CHUNK_CHAR ( 64 / CHAR_BIT ) |
| 213 | #define P192_CHUNK_INT ( P192_CHUNK_CHAR / sizeof( t_uint ) ) |
| 214 | |
| 215 | const signed char p192_tbl[][P192_CHUNKS] = { |
| 216 | { -1, 3, 3 }, /* S1 */ |
| 217 | { 4, 4, -1 }, /* S2 */ |
| 218 | { 5, 5, 5 }, /* S3 */ |
| 219 | }; |
| 220 | |
| 221 | /* |
| 222 | * Fast quasi-reduction modulo p192 (FIPS 186-3 D.2.1) |
| 223 | */ |
| 224 | static int ecp_mod_p192( mpi *N ) |
| 225 | { |
| 226 | int ret; |
| 227 | unsigned char i, j, offset; |
| 228 | signed char chunk; |
| 229 | mpi tmp, acc; |
| 230 | t_uint tmp_p[P192_SIZE_INT], acc_p[P192_SIZE_INT + 1]; |
| 231 | |
| 232 | tmp.s = 1; |
| 233 | tmp.n = sizeof( tmp_p ) / sizeof( tmp_p[0] ); |
| 234 | tmp.p = tmp_p; |
| 235 | |
| 236 | acc.s = 1; |
| 237 | acc.n = sizeof( acc_p ) / sizeof( acc_p[0] ); |
| 238 | acc.p = acc_p; |
| 239 | |
| 240 | MPI_CHK( mpi_grow( N, P192_SIZE_INT * 2 ) ); |
| 241 | |
| 242 | /* |
| 243 | * acc = T |
| 244 | */ |
| 245 | memset( acc_p, 0, sizeof( acc_p ) ); |
| 246 | memcpy( acc_p, N->p, P192_CHUNK_CHAR * P192_CHUNKS ); |
| 247 | |
| 248 | for( i = 0; i < sizeof( p192_tbl ) / sizeof( p192_tbl[0] ); i++) |
| 249 | { |
| 250 | /* |
| 251 | * tmp = S_i |
| 252 | */ |
| 253 | memset( tmp_p, 0, sizeof( tmp_p ) ); |
| 254 | for( j = 0, offset = P192_CHUNKS - 1; j < P192_CHUNKS; j++, offset-- ) |
| 255 | { |
| 256 | chunk = p192_tbl[i][j]; |
| 257 | if( chunk >= 0 ) |
| 258 | memcpy( tmp_p + offset * P192_CHUNK_INT, |
| 259 | N->p + chunk * P192_CHUNK_INT, |
| 260 | P192_CHUNK_CHAR ); |
| 261 | } |
| 262 | |
| 263 | /* |
| 264 | * acc += tmp |
| 265 | */ |
| 266 | MPI_CHK( mpi_add_abs( &acc, &acc, &tmp ) ); |
| 267 | } |
| 268 | |
| 269 | MPI_CHK( mpi_copy( N, &acc ) ); |
| 270 | |
| 271 | cleanup: |
| 272 | return( ret ); |
| 273 | } |
| 274 | |
| 275 | /* |
Manuel Pégourié-Gonnard | 62aad14 | 2012-11-10 00:27:12 +0100 | [diff] [blame] | 276 | * Size of p521 in terms of t_uint |
| 277 | */ |
Manuel Pégourié-Gonnard | 8433824 | 2012-11-11 20:45:18 +0100 | [diff] [blame] | 278 | #define P521_SIZE_INT ( 521 / CHAR_BIT / sizeof( t_uint ) + 1 ) |
Manuel Pégourié-Gonnard | 62aad14 | 2012-11-10 00:27:12 +0100 | [diff] [blame] | 279 | |
| 280 | /* |
| 281 | * Bits to keep in the most significant t_uint |
| 282 | */ |
| 283 | #if defined(POLARSS_HAVE_INT8) |
| 284 | #define P521_MASK 0x01 |
| 285 | #else |
| 286 | #define P521_MASK 0x01FF |
| 287 | #endif |
| 288 | |
| 289 | /* |
| 290 | * Fast quasi-reduction modulo p521 (FIPS 186-3 D.2.5) |
Manuel Pégourié-Gonnard | 62aad14 | 2012-11-10 00:27:12 +0100 | [diff] [blame] | 291 | */ |
| 292 | static int ecp_mod_p521( mpi *N ) |
| 293 | { |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 294 | int ret; |
Manuel Pégourié-Gonnard | 62aad14 | 2012-11-10 00:27:12 +0100 | [diff] [blame] | 295 | t_uint Mp[P521_SIZE_INT]; |
| 296 | mpi M; |
| 297 | |
| 298 | if( N->n < P521_SIZE_INT ) |
| 299 | return( 0 ); |
| 300 | |
| 301 | memset( Mp, 0, P521_SIZE_INT * sizeof( t_uint ) ); |
| 302 | memcpy( Mp, N->p, P521_SIZE_INT * sizeof( t_uint ) ); |
| 303 | Mp[P521_SIZE_INT - 1] &= P521_MASK; |
| 304 | |
| 305 | M.s = 1; |
| 306 | M.n = P521_SIZE_INT; |
| 307 | M.p = Mp; |
| 308 | |
| 309 | MPI_CHK( mpi_shift_r( N, 521 ) ); |
| 310 | |
| 311 | MPI_CHK( mpi_add_abs( N, N, &M ) ); |
| 312 | |
| 313 | cleanup: |
| 314 | return( ret ); |
| 315 | } |
| 316 | |
| 317 | /* |
Manuel Pégourié-Gonnard | dada4da | 2012-11-10 14:23:17 +0100 | [diff] [blame] | 318 | * Domain parameters for secp192r1 |
| 319 | */ |
| 320 | #define SECP192R1_P \ |
| 321 | "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF" |
| 322 | #define SECP192R1_B \ |
| 323 | "64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1" |
| 324 | #define SECP192R1_GX \ |
| 325 | "188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012" |
| 326 | #define SECP192R1_GY \ |
| 327 | "07192B95FFC8DA78631011ED6B24CDD573F977A11E794811" |
| 328 | #define SECP192R1_N \ |
| 329 | "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831" |
| 330 | |
| 331 | /* |
| 332 | * Domain parameters for secp224r1 |
| 333 | */ |
| 334 | #define SECP224R1_P \ |
| 335 | "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001" |
| 336 | #define SECP224R1_B \ |
| 337 | "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4" |
| 338 | #define SECP224R1_GX \ |
| 339 | "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21" |
| 340 | #define SECP224R1_GY \ |
| 341 | "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34" |
| 342 | #define SECP224R1_N \ |
| 343 | "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D" |
| 344 | |
| 345 | /* |
| 346 | * Domain parameters for secp256r1 |
| 347 | */ |
| 348 | #define SECP256R1_P \ |
| 349 | "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF" |
| 350 | #define SECP256R1_B \ |
| 351 | "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B" |
| 352 | #define SECP256R1_GX \ |
| 353 | "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296" |
| 354 | #define SECP256R1_GY \ |
| 355 | "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5" |
| 356 | #define SECP256R1_N \ |
| 357 | "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551" |
| 358 | |
| 359 | /* |
| 360 | * Domain parameters for secp384r1 |
| 361 | */ |
| 362 | #define SECP384R1_P \ |
| 363 | "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \ |
| 364 | "FFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF" |
| 365 | #define SECP384R1_B \ |
| 366 | "B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE814112" \ |
| 367 | "0314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF" |
| 368 | #define SECP384R1_GX \ |
| 369 | "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B98" \ |
| 370 | "59F741E082542A385502F25DBF55296C3A545E3872760AB7" |
| 371 | #define SECP384R1_GY \ |
| 372 | "3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A147C" \ |
| 373 | "E9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F" |
| 374 | #define SECP384R1_N \ |
| 375 | "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \ |
| 376 | "C7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973" |
| 377 | |
| 378 | /* |
| 379 | * Domain parameters for secp521r1 |
| 380 | */ |
| 381 | #define SECP521R1_P \ |
| 382 | "000001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \ |
| 383 | "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \ |
| 384 | "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" |
| 385 | #define SECP521R1_B \ |
| 386 | "00000051953EB9618E1C9A1F929A21A0B68540EEA2DA725B" \ |
| 387 | "99B315F3B8B489918EF109E156193951EC7E937B1652C0BD" \ |
| 388 | "3BB1BF073573DF883D2C34F1EF451FD46B503F00" |
| 389 | #define SECP521R1_GX \ |
| 390 | "000000C6858E06B70404E9CD9E3ECB662395B4429C648139" \ |
| 391 | "053FB521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127" \ |
| 392 | "A2FFA8DE3348B3C1856A429BF97E7E31C2E5BD66" |
| 393 | #define SECP521R1_GY \ |
| 394 | "0000011839296A789A3BC0045C8A5FB42C7D1BD998F54449" \ |
| 395 | "579B446817AFBD17273E662C97EE72995EF42640C550B901" \ |
| 396 | "3FAD0761353C7086A272C24088BE94769FD16650" |
| 397 | #define SECP521R1_N \ |
| 398 | "000001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \ |
| 399 | "FFFFFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148" \ |
| 400 | "F709A5D03BB5C9B8899C47AEBB6FB71E91386409" |
| 401 | |
| 402 | /* |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 403 | * Set a group using well-known domain parameters |
| 404 | */ |
| 405 | int ecp_use_known_dp( ecp_group *grp, size_t index ) |
| 406 | { |
| 407 | switch( index ) |
| 408 | { |
| 409 | case POLARSSL_ECP_DP_SECP192R1: |
Manuel Pégourié-Gonnard | 8433824 | 2012-11-11 20:45:18 +0100 | [diff] [blame] | 410 | grp->modp = ecp_mod_p192; |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 411 | return( ecp_group_read_string( grp, 16, |
Manuel Pégourié-Gonnard | dada4da | 2012-11-10 14:23:17 +0100 | [diff] [blame] | 412 | SECP192R1_P, SECP192R1_B, |
| 413 | SECP192R1_GX, SECP192R1_GY, SECP192R1_N ) ); |
| 414 | |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 415 | case POLARSSL_ECP_DP_SECP224R1: |
| 416 | return( ecp_group_read_string( grp, 16, |
Manuel Pégourié-Gonnard | dada4da | 2012-11-10 14:23:17 +0100 | [diff] [blame] | 417 | SECP224R1_P, SECP224R1_B, |
| 418 | SECP224R1_GX, SECP224R1_GY, SECP224R1_N ) ); |
| 419 | |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 420 | case POLARSSL_ECP_DP_SECP256R1: |
| 421 | return( ecp_group_read_string( grp, 16, |
Manuel Pégourié-Gonnard | dada4da | 2012-11-10 14:23:17 +0100 | [diff] [blame] | 422 | SECP256R1_P, SECP256R1_B, |
| 423 | SECP256R1_GX, SECP256R1_GY, SECP256R1_N ) ); |
| 424 | |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 425 | case POLARSSL_ECP_DP_SECP384R1: |
| 426 | return( ecp_group_read_string( grp, 16, |
Manuel Pégourié-Gonnard | dada4da | 2012-11-10 14:23:17 +0100 | [diff] [blame] | 427 | SECP384R1_P, SECP384R1_B, |
| 428 | SECP384R1_GX, SECP384R1_GY, SECP384R1_N ) ); |
| 429 | |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 430 | case POLARSSL_ECP_DP_SECP521R1: |
Manuel Pégourié-Gonnard | 62aad14 | 2012-11-10 00:27:12 +0100 | [diff] [blame] | 431 | grp->modp = ecp_mod_p521; |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 432 | return( ecp_group_read_string( grp, 16, |
Manuel Pégourié-Gonnard | dada4da | 2012-11-10 14:23:17 +0100 | [diff] [blame] | 433 | SECP521R1_P, SECP521R1_B, |
| 434 | SECP521R1_GX, SECP521R1_GY, SECP521R1_N ) ); |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | return( POLARSSL_ERR_ECP_GENERIC ); |
| 438 | } |
Manuel Pégourié-Gonnard | ab38b70 | 2012-11-05 17:34:55 +0100 | [diff] [blame] | 439 | |
Manuel Pégourié-Gonnard | 847395a | 2012-11-05 13:13:44 +0100 | [diff] [blame] | 440 | /* |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 441 | * Fast mod-p functions expect their argument to be in the 0..p^2 range. |
Manuel Pégourié-Gonnard | dada4da | 2012-11-10 14:23:17 +0100 | [diff] [blame] | 442 | * |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 443 | * In order to guarantee that, we need to ensure that operands of |
| 444 | * mpi_mul_mpi are in the 0..p range. So, after each operation we will |
Manuel Pégourié-Gonnard | dada4da | 2012-11-10 14:23:17 +0100 | [diff] [blame] | 445 | * bring the result back to this range. |
| 446 | * |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 447 | * The following macros are shortcuts for doing that. |
Manuel Pégourié-Gonnard | dada4da | 2012-11-10 14:23:17 +0100 | [diff] [blame] | 448 | */ |
| 449 | |
| 450 | /* |
Manuel Pégourié-Gonnard | 84d1aea | 2012-11-09 02:09:38 +0100 | [diff] [blame] | 451 | * Reduce a mpi mod p in-place, general case, to use after mpi_mul_mpi |
| 452 | */ |
Manuel Pégourié-Gonnard | 62aad14 | 2012-11-10 00:27:12 +0100 | [diff] [blame] | 453 | #define MOD_MUL( N ) MPI_CHK( ecp_modp( &N, grp ) ) |
Manuel Pégourié-Gonnard | 84d1aea | 2012-11-09 02:09:38 +0100 | [diff] [blame] | 454 | |
| 455 | /* |
| 456 | * Reduce a mpi mod p in-place, to use after mpi_sub_mpi |
| 457 | */ |
| 458 | #define MOD_SUB( N ) \ |
| 459 | while( mpi_cmp_int( &N, 0 ) < 0 ) \ |
| 460 | MPI_CHK( mpi_add_mpi( &N, &N, &grp->P ) ) |
| 461 | |
| 462 | /* |
| 463 | * Reduce a mpi mod p in-place, to use after mpi_add_mpi and mpi_mul_int |
| 464 | */ |
| 465 | #define MOD_ADD( N ) \ |
| 466 | while( mpi_cmp_mpi( &N, &grp->P ) >= 0 ) \ |
| 467 | MPI_CHK( mpi_sub_mpi( &N, &N, &grp->P ) ) |
| 468 | |
| 469 | /* |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 470 | * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1) |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 471 | */ |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 472 | static int ecp_normalize( const ecp_group *grp, ecp_point *pt ) |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 473 | { |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 474 | int ret; |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 475 | mpi Zi, ZZi, T; |
| 476 | |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 477 | if( mpi_cmp_int( &pt->Z, 0 ) == 0 ) |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 478 | return( 0 ); |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 479 | |
| 480 | mpi_init( &Zi ); mpi_init( &ZZi ); mpi_init( &T ); |
| 481 | |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 482 | /* |
| 483 | * X = X / Z^2 mod p |
| 484 | */ |
| 485 | MPI_CHK( mpi_inv_mod( &Zi, &pt->Z, &grp->P ) ); |
| 486 | MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi ); |
| 487 | MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ZZi ) ); MOD_MUL( pt->X ); |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 488 | |
| 489 | /* |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 490 | * Y = Y / Z^3 mod p |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 491 | */ |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 492 | MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ZZi ) ); MOD_MUL( pt->Y ); |
| 493 | MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &Zi ) ); MOD_MUL( pt->Y ); |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 494 | |
| 495 | /* |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 496 | * Z = 1 |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 497 | */ |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 498 | MPI_CHK( mpi_lset( &pt->Z, 1 ) ); |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 499 | |
| 500 | cleanup: |
| 501 | |
| 502 | mpi_free( &Zi ); mpi_free( &ZZi ); mpi_free( &T ); |
| 503 | |
| 504 | return( ret ); |
| 505 | } |
| 506 | |
| 507 | /* |
Manuel Pégourié-Gonnard | 989c32b | 2012-11-08 22:02:42 +0100 | [diff] [blame] | 508 | * Point doubling R = 2 P, Jacobian coordinates (GECC 3.21) |
| 509 | */ |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 510 | static int ecp_double_jac( const ecp_group *grp, ecp_point *R, |
| 511 | const ecp_point *P ) |
Manuel Pégourié-Gonnard | 989c32b | 2012-11-08 22:02:42 +0100 | [diff] [blame] | 512 | { |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 513 | int ret; |
Manuel Pégourié-Gonnard | 989c32b | 2012-11-08 22:02:42 +0100 | [diff] [blame] | 514 | mpi T1, T2, T3, X, Y, Z; |
| 515 | |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame^] | 516 | #if defined(POLARSSL_SELF_TEST) |
| 517 | dbl_count++; |
| 518 | #endif |
| 519 | |
Manuel Pégourié-Gonnard | 7e0adfb | 2012-11-08 23:21:46 +0100 | [diff] [blame] | 520 | if( mpi_cmp_int( &P->Z, 0 ) == 0 ) |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 521 | return( ecp_set_zero( R ) ); |
Manuel Pégourié-Gonnard | 7e0adfb | 2012-11-08 23:21:46 +0100 | [diff] [blame] | 522 | |
Manuel Pégourié-Gonnard | 989c32b | 2012-11-08 22:02:42 +0100 | [diff] [blame] | 523 | mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 ); |
| 524 | mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z ); |
| 525 | |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 526 | MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 ); |
| 527 | MPI_CHK( mpi_sub_mpi( &T2, &P->X, &T1 ) ); MOD_SUB( T2 ); |
| 528 | MPI_CHK( mpi_add_mpi( &T1, &P->X, &T1 ) ); MOD_ADD( T1 ); |
| 529 | MPI_CHK( mpi_mul_mpi( &T2, &T2, &T1 ) ); MOD_MUL( T2 ); |
| 530 | MPI_CHK( mpi_mul_int( &T2, &T2, 3 ) ); MOD_ADD( T2 ); |
| 531 | MPI_CHK( mpi_mul_int( &Y, &P->Y, 2 ) ); MOD_ADD( Y ); |
| 532 | MPI_CHK( mpi_mul_mpi( &Z, &Y, &P->Z ) ); MOD_MUL( Z ); |
| 533 | MPI_CHK( mpi_mul_mpi( &Y, &Y, &Y ) ); MOD_MUL( Y ); |
| 534 | MPI_CHK( mpi_mul_mpi( &T3, &Y, &P->X ) ); MOD_MUL( T3 ); |
| 535 | MPI_CHK( mpi_mul_mpi( &Y, &Y, &Y ) ); MOD_MUL( Y ); |
Manuel Pégourié-Gonnard | 989c32b | 2012-11-08 22:02:42 +0100 | [diff] [blame] | 536 | |
Manuel Pégourié-Gonnard | 84d1aea | 2012-11-09 02:09:38 +0100 | [diff] [blame] | 537 | /* |
| 538 | * For Y = Y / 2 mod p, we must make sure that Y is even before |
| 539 | * using right-shift. No need to reduce mod p afterwards. |
| 540 | */ |
| 541 | if( mpi_get_bit( &Y, 0 ) == 1 ) |
| 542 | MPI_CHK( mpi_add_mpi( &Y, &Y, &grp->P ) ); |
| 543 | MPI_CHK( mpi_shift_r( &Y, 1 ) ); |
| 544 | |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 545 | MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X ); |
| 546 | MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 ); |
| 547 | MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X ); |
| 548 | MPI_CHK( mpi_sub_mpi( &T1, &T3, &X ) ); MOD_SUB( T1 ); |
| 549 | MPI_CHK( mpi_mul_mpi( &T1, &T1, &T2 ) ); MOD_MUL( T1 ); |
| 550 | MPI_CHK( mpi_sub_mpi( &Y, &T1, &Y ) ); MOD_SUB( Y ); |
Manuel Pégourié-Gonnard | 84d1aea | 2012-11-09 02:09:38 +0100 | [diff] [blame] | 551 | |
| 552 | MPI_CHK( mpi_copy( &R->X, &X ) ); |
| 553 | MPI_CHK( mpi_copy( &R->Y, &Y ) ); |
| 554 | MPI_CHK( mpi_copy( &R->Z, &Z ) ); |
Manuel Pégourié-Gonnard | 989c32b | 2012-11-08 22:02:42 +0100 | [diff] [blame] | 555 | |
| 556 | cleanup: |
| 557 | |
| 558 | mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 ); |
| 559 | mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z ); |
| 560 | |
| 561 | return( ret ); |
| 562 | } |
| 563 | |
| 564 | /* |
Manuel Pégourié-Gonnard | 9674fd0 | 2012-11-19 21:23:27 +0100 | [diff] [blame] | 565 | * Addition or subtraction: R = P + Q or R = P + Q, |
| 566 | * mixed affine-Jacobian coordinates (GECC 3.22) |
| 567 | * |
| 568 | * The coordinates of Q must be normalized (= affine), |
| 569 | * but those of P don't need to. R is not normalized. |
| 570 | * |
| 571 | * If sign >= 0, perform addition, otherwise perform subtraction, |
| 572 | * taking advantage of the fact that, for Q != 0, we have |
| 573 | * -Q = (Q.X, -Q.Y, Q.Z) |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 574 | */ |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 575 | static int ecp_add_mixed( const ecp_group *grp, ecp_point *R, |
Manuel Pégourié-Gonnard | 9674fd0 | 2012-11-19 21:23:27 +0100 | [diff] [blame] | 576 | const ecp_point *P, const ecp_point *Q, |
| 577 | signed char sign ) |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 578 | { |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 579 | int ret; |
Manuel Pégourié-Gonnard | 7e0adfb | 2012-11-08 23:21:46 +0100 | [diff] [blame] | 580 | mpi T1, T2, T3, T4, X, Y, Z; |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 581 | |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame^] | 582 | #if defined(POLARSSL_SELF_TEST) |
| 583 | add_count++; |
| 584 | #endif |
| 585 | |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 586 | /* |
Manuel Pégourié-Gonnard | 7e0adfb | 2012-11-08 23:21:46 +0100 | [diff] [blame] | 587 | * Trivial cases: P == 0 or Q == 0 |
Manuel Pégourié-Gonnard | 9674fd0 | 2012-11-19 21:23:27 +0100 | [diff] [blame] | 588 | * (Check Q first, so that we know Q != 0 when we compute -Q.) |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 589 | */ |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 590 | if( mpi_cmp_int( &Q->Z, 0 ) == 0 ) |
| 591 | return( ecp_copy( R, P ) ); |
| 592 | |
Manuel Pégourié-Gonnard | 9674fd0 | 2012-11-19 21:23:27 +0100 | [diff] [blame] | 593 | if( mpi_cmp_int( &P->Z, 0 ) == 0 ) |
| 594 | { |
| 595 | ret = ecp_copy( R, Q ); |
| 596 | |
| 597 | /* |
| 598 | * -R.Y mod P = P - R.Y unless R.Y == 0 |
| 599 | */ |
| 600 | if( ret == 0 && sign < 0) |
| 601 | if( mpi_cmp_int( &R->Y, 0 ) != 0 ) |
| 602 | ret = mpi_sub_mpi( &R->Y, &grp->P, &R->Y ); |
| 603 | |
| 604 | return( ret ); |
| 605 | } |
| 606 | |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 607 | /* |
| 608 | * Make sure Q coordinates are normalized |
| 609 | */ |
| 610 | if( mpi_cmp_int( &Q->Z, 1 ) != 0 ) |
| 611 | return( POLARSSL_ERR_ECP_GENERIC ); |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 612 | |
Manuel Pégourié-Gonnard | 7e0adfb | 2012-11-08 23:21:46 +0100 | [diff] [blame] | 613 | mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 ); mpi_init( &T4 ); |
| 614 | mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z ); |
Manuel Pégourié-Gonnard | ab38b70 | 2012-11-05 17:34:55 +0100 | [diff] [blame] | 615 | |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 616 | MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 ); |
| 617 | MPI_CHK( mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 ); |
| 618 | MPI_CHK( mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 ); |
| 619 | MPI_CHK( mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 ); |
Manuel Pégourié-Gonnard | 9674fd0 | 2012-11-19 21:23:27 +0100 | [diff] [blame] | 620 | |
| 621 | /* |
| 622 | * For subtraction, -Q.Y should have been used instead of Q.Y, |
| 623 | * so we replace T2 by -T2, which is P - T2 mod P |
| 624 | */ |
| 625 | if( sign < 0 ) |
| 626 | { |
| 627 | MPI_CHK( mpi_sub_mpi( &T2, &grp->P, &T2 ) ); |
| 628 | MOD_SUB( T2 ); |
| 629 | } |
| 630 | |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 631 | MPI_CHK( mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 ); |
| 632 | MPI_CHK( mpi_sub_mpi( &T2, &T2, &P->Y ) ); MOD_SUB( T2 ); |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 633 | |
Manuel Pégourié-Gonnard | 7e0adfb | 2012-11-08 23:21:46 +0100 | [diff] [blame] | 634 | if( mpi_cmp_int( &T1, 0 ) == 0 ) |
| 635 | { |
| 636 | if( mpi_cmp_int( &T2, 0 ) == 0 ) |
| 637 | { |
| 638 | ret = ecp_double_jac( grp, R, P ); |
| 639 | goto cleanup; |
| 640 | } |
| 641 | else |
| 642 | { |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 643 | ret = ecp_set_zero( R ); |
Manuel Pégourié-Gonnard | 7e0adfb | 2012-11-08 23:21:46 +0100 | [diff] [blame] | 644 | goto cleanup; |
| 645 | } |
| 646 | } |
| 647 | |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 648 | MPI_CHK( mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z ); |
| 649 | MPI_CHK( mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 ); |
| 650 | MPI_CHK( mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 ); |
| 651 | MPI_CHK( mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 ); |
| 652 | MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 ); |
| 653 | MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X ); |
| 654 | MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X ); |
| 655 | MPI_CHK( mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X ); |
| 656 | MPI_CHK( mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 ); |
| 657 | MPI_CHK( mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 ); |
| 658 | MPI_CHK( mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 ); |
| 659 | MPI_CHK( mpi_sub_mpi( &Y, &T3, &T4 ) ); MOD_SUB( Y ); |
Manuel Pégourié-Gonnard | 7e0adfb | 2012-11-08 23:21:46 +0100 | [diff] [blame] | 660 | |
Manuel Pégourié-Gonnard | 84d1aea | 2012-11-09 02:09:38 +0100 | [diff] [blame] | 661 | MPI_CHK( mpi_copy( &R->X, &X ) ); |
| 662 | MPI_CHK( mpi_copy( &R->Y, &Y ) ); |
| 663 | MPI_CHK( mpi_copy( &R->Z, &Z ) ); |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 664 | |
| 665 | cleanup: |
| 666 | |
Manuel Pégourié-Gonnard | 7e0adfb | 2012-11-08 23:21:46 +0100 | [diff] [blame] | 667 | mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 ); mpi_free( &T4 ); |
| 668 | mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z ); |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 669 | |
| 670 | return( ret ); |
| 671 | } |
| 672 | |
| 673 | /* |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 674 | * Addition: R = P + Q, result's coordinates normalized |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 675 | */ |
| 676 | int ecp_add( const ecp_group *grp, ecp_point *R, |
| 677 | const ecp_point *P, const ecp_point *Q ) |
| 678 | { |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 679 | int ret; |
Manuel Pégourié-Gonnard | 989c32b | 2012-11-08 22:02:42 +0100 | [diff] [blame] | 680 | |
Manuel Pégourié-Gonnard | 9674fd0 | 2012-11-19 21:23:27 +0100 | [diff] [blame] | 681 | MPI_CHK( ecp_add_mixed( grp, R, P, Q , 1 ) ); |
| 682 | MPI_CHK( ecp_normalize( grp, R ) ); |
| 683 | |
| 684 | cleanup: |
| 685 | return( ret ); |
| 686 | } |
| 687 | |
| 688 | /* |
| 689 | * Subtraction: R = P - Q, result's coordinates normalized |
| 690 | */ |
| 691 | int ecp_sub( const ecp_group *grp, ecp_point *R, |
| 692 | const ecp_point *P, const ecp_point *Q ) |
| 693 | { |
| 694 | int ret; |
| 695 | |
| 696 | MPI_CHK( ecp_add_mixed( grp, R, P, Q, -1 ) ); |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 697 | MPI_CHK( ecp_normalize( grp, R ) ); |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 698 | |
Manuel Pégourié-Gonnard | 989c32b | 2012-11-08 22:02:42 +0100 | [diff] [blame] | 699 | cleanup: |
Manuel Pégourié-Gonnard | 7e0adfb | 2012-11-08 23:21:46 +0100 | [diff] [blame] | 700 | return( ret ); |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 701 | } |
| 702 | |
Manuel Pégourié-Gonnard | efaa31e | 2012-11-06 21:34:35 +0100 | [diff] [blame] | 703 | /* |
Manuel Pégourié-Gonnard | 4bdd47d | 2012-11-11 14:33:59 +0100 | [diff] [blame] | 704 | * Integer multiplication: R = m * P (GECC 5.7, SPA-resistant) |
Manuel Pégourié-Gonnard | efaa31e | 2012-11-06 21:34:35 +0100 | [diff] [blame] | 705 | */ |
| 706 | int ecp_mul( const ecp_group *grp, ecp_point *R, |
| 707 | const mpi *m, const ecp_point *P ) |
| 708 | { |
Manuel Pégourié-Gonnard | 4bdd47d | 2012-11-11 14:33:59 +0100 | [diff] [blame] | 709 | int ret, cmp; |
Manuel Pégourié-Gonnard | efaa31e | 2012-11-06 21:34:35 +0100 | [diff] [blame] | 710 | size_t pos; |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 711 | ecp_point Q[2]; |
Manuel Pégourié-Gonnard | efaa31e | 2012-11-06 21:34:35 +0100 | [diff] [blame] | 712 | |
Manuel Pégourié-Gonnard | 4bdd47d | 2012-11-11 14:33:59 +0100 | [diff] [blame] | 713 | cmp = mpi_cmp_int( m, 0 ); |
| 714 | |
| 715 | if( cmp < 0 ) |
| 716 | return( POLARSSL_ERR_ECP_GENERIC ); |
| 717 | |
Manuel Pégourié-Gonnard | efaa31e | 2012-11-06 21:34:35 +0100 | [diff] [blame] | 718 | /* |
Manuel Pégourié-Gonnard | 4bdd47d | 2012-11-11 14:33:59 +0100 | [diff] [blame] | 719 | * The general method works only for m != 0 |
Manuel Pégourié-Gonnard | efaa31e | 2012-11-06 21:34:35 +0100 | [diff] [blame] | 720 | */ |
Manuel Pégourié-Gonnard | 4bdd47d | 2012-11-11 14:33:59 +0100 | [diff] [blame] | 721 | if( cmp == 0 ) { |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 722 | return( ecp_set_zero( R ) ); |
Manuel Pégourié-Gonnard | efaa31e | 2012-11-06 21:34:35 +0100 | [diff] [blame] | 723 | } |
| 724 | |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 725 | ecp_point_init( &Q[0] ); ecp_point_init( &Q[1] ); |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 726 | |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 727 | MPI_CHK( ecp_set_zero( &Q[0] ) ); |
Manuel Pégourié-Gonnard | efaa31e | 2012-11-06 21:34:35 +0100 | [diff] [blame] | 728 | |
Manuel Pégourié-Gonnard | 989c32b | 2012-11-08 22:02:42 +0100 | [diff] [blame] | 729 | for( pos = mpi_msb( m ) - 1 ; ; pos-- ) |
Manuel Pégourié-Gonnard | efaa31e | 2012-11-06 21:34:35 +0100 | [diff] [blame] | 730 | { |
Manuel Pégourié-Gonnard | e0c1692 | 2012-11-08 23:27:28 +0100 | [diff] [blame] | 731 | MPI_CHK( ecp_double_jac( grp, &Q[0], &Q[0] ) ); |
Manuel Pégourié-Gonnard | 9674fd0 | 2012-11-19 21:23:27 +0100 | [diff] [blame] | 732 | MPI_CHK( ecp_add_mixed( grp, &Q[1], &Q[0], P, 1 ) ); |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 733 | 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] | 734 | |
| 735 | if( pos == 0 ) |
| 736 | break; |
| 737 | } |
| 738 | |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 739 | MPI_CHK( ecp_copy( R, &Q[0] ) ); |
| 740 | MPI_CHK( ecp_normalize( grp, R ) ); |
Manuel Pégourié-Gonnard | efaa31e | 2012-11-06 21:34:35 +0100 | [diff] [blame] | 741 | |
| 742 | cleanup: |
| 743 | |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 744 | ecp_point_free( &Q[0] ); ecp_point_free( &Q[1] ); |
Manuel Pégourié-Gonnard | efaa31e | 2012-11-06 21:34:35 +0100 | [diff] [blame] | 745 | |
| 746 | return( ret ); |
| 747 | } |
| 748 | |
| 749 | |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 750 | #if defined(POLARSSL_SELF_TEST) |
| 751 | |
Manuel Pégourié-Gonnard | b505c27 | 2012-11-05 17:27:54 +0100 | [diff] [blame] | 752 | /* |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 753 | * Checkup routine |
| 754 | */ |
| 755 | int ecp_self_test( int verbose ) |
| 756 | { |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame^] | 757 | int ret; |
| 758 | size_t i; |
| 759 | ecp_group grp; |
| 760 | ecp_point R; |
| 761 | mpi m; |
| 762 | unsigned long add_c_prev, dbl_c_prev; |
| 763 | char *exponents[] = |
| 764 | { |
| 765 | "400000000000000000000000000000000000000000000000", |
| 766 | "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", |
| 767 | "555555555555555555555555555555555555555555555555", |
| 768 | "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", |
| 769 | "000000000000000000000000000000000000000000000010", |
| 770 | }; |
| 771 | |
| 772 | ecp_group_init( &grp ); |
| 773 | ecp_point_init( &R ); |
| 774 | mpi_init( &m ); |
| 775 | |
| 776 | MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP192R1 ) ); |
| 777 | |
| 778 | if( verbose != 0 ) |
| 779 | printf( " ECP test #1 (SPA resistance): " ); |
| 780 | |
| 781 | add_count = 0; |
| 782 | dbl_count = 0; |
| 783 | MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) ); |
| 784 | MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G ) ); |
| 785 | |
| 786 | for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ ) |
| 787 | { |
| 788 | add_c_prev = add_count; |
| 789 | dbl_c_prev = dbl_count; |
| 790 | add_count = 0; |
| 791 | dbl_count = 0; |
| 792 | |
| 793 | MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) ); |
| 794 | MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G ) ); |
| 795 | |
| 796 | if( add_count != add_c_prev || dbl_count != dbl_c_prev ) |
| 797 | { |
| 798 | if( verbose != 0 ) |
| 799 | printf( "failed (%zu)\n", i ); |
| 800 | |
| 801 | ret = 1; |
| 802 | goto cleanup; |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | if( verbose != 0 ) |
| 807 | printf( "passed\n" ); |
| 808 | |
| 809 | cleanup: |
| 810 | |
| 811 | if( ret < 0 && verbose != 0 ) |
| 812 | printf( "Unexpected error, return code = %08X\n", ret ); |
| 813 | |
| 814 | ecp_group_free( &grp ); |
| 815 | ecp_point_free( &R ); |
| 816 | mpi_free( &m ); |
| 817 | |
| 818 | if( verbose != 0 ) |
| 819 | printf( "\n" ); |
| 820 | |
| 821 | return( ret ); |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | #endif |
| 825 | |
| 826 | #endif |