Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Elliptic curves over GF(p) |
| 3 | * |
Paul Bakker | cf4365f | 2013-01-16 17:00:43 +0100 | [diff] [blame] | 4 | * Copyright (C) 2006-2013, Brainspark B.V. |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 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 | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 32 | * RFC 4492 for the related TLS structures and constants |
Manuel Pégourié-Gonnard | 07de4b1 | 2013-09-02 16:26:04 +0200 | [diff] [blame] | 33 | * |
| 34 | * [1] OKEYA, Katsuyuki and TAKAGI, Tsuyoshi. The width-w NAF method provides |
| 35 | * small memory and fast elliptic scalar multiplications secure against |
| 36 | * side channel attacks. In : Topics in Cryptology—CT-RSA 2003. Springer |
| 37 | * Berlin Heidelberg, 2003. p. 328-343. |
| 38 | * <http://rd.springer.com/chapter/10.1007/3-540-36563-X_23>. |
| 39 | * |
| 40 | * [2] CORON, Jean-Sébastien. Resistance against differential power analysis |
| 41 | * for elliptic curve cryptosystems. In : Cryptographic Hardware and |
| 42 | * Embedded Systems. Springer Berlin Heidelberg, 1999. p. 292-302. |
| 43 | * <http://link.springer.com/chapter/10.1007/3-540-48059-5_25> |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 44 | * |
| 45 | * [3] HEDABOU, Mustapha, PINEL, Pierre, et BÉNÉTEAU, Lucien. A comb method to |
| 46 | * render ECC resistant against Side Channel Attacks. IACR Cryptology |
| 47 | * ePrint Archive, 2004, vol. 2004, p. 342. |
| 48 | * <http://eprint.iacr.org/2004/342.pdf> |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 49 | */ |
| 50 | |
| 51 | #include "polarssl/config.h" |
| 52 | |
| 53 | #if defined(POLARSSL_ECP_C) |
| 54 | |
| 55 | #include "polarssl/ecp.h" |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 56 | |
| 57 | #if defined(POLARSSL_MEMORY_C) |
| 58 | #include "polarssl/memory.h" |
| 59 | #else |
| 60 | #define polarssl_malloc malloc |
| 61 | #define polarssl_free free |
| 62 | #endif |
| 63 | |
Manuel Pégourié-Gonnard | 8433824 | 2012-11-11 20:45:18 +0100 | [diff] [blame] | 64 | #include <limits.h> |
Manuel Pégourié-Gonnard | cdd4432 | 2012-11-21 16:00:55 +0100 | [diff] [blame] | 65 | #include <stdlib.h> |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 66 | |
Paul Bakker | 6a6087e | 2013-10-28 18:53:08 +0100 | [diff] [blame] | 67 | #if defined(_MSC_VER) && !defined(inline) |
| 68 | #define inline _inline |
| 69 | #else |
| 70 | #if defined(__ARMCC_VERSION) && !defined(inline) |
| 71 | #define inline __inline |
| 72 | #endif /* __ARMCC_VERSION */ |
| 73 | #endif /*_MSC_VER */ |
| 74 | |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 75 | #if defined(POLARSSL_SELF_TEST) |
| 76 | /* |
| 77 | * Counts of point addition and doubling operations. |
Manuel Pégourié-Gonnard | 07de4b1 | 2013-09-02 16:26:04 +0200 | [diff] [blame] | 78 | * Used to test resistance of point multiplication to simple timing attacks. |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 79 | */ |
| 80 | unsigned long add_count, dbl_count; |
| 81 | #endif |
| 82 | |
Manuel Pégourié-Gonnard | 1e8c8ec | 2012-10-31 19:24:21 +0100 | [diff] [blame] | 83 | /* |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 84 | * List of supported curves: |
| 85 | * - internal ID |
Manuel Pégourié-Gonnard | 8195c1a | 2013-10-07 19:40:41 +0200 | [diff] [blame] | 86 | * - TLS NamedCurve ID (RFC 4492 sec. 5.1.1, RFC 7071 sec. 2) |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 87 | * - size in bits |
Manuel Pégourié-Gonnard | 8195c1a | 2013-10-07 19:40:41 +0200 | [diff] [blame] | 88 | * - readable name |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 89 | */ |
Manuel Pégourié-Gonnard | a79d123 | 2013-09-17 15:42:35 +0200 | [diff] [blame] | 90 | const ecp_curve_info ecp_supported_curves[] = |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 91 | { |
Manuel Pégourié-Gonnard | 8195c1a | 2013-10-07 19:40:41 +0200 | [diff] [blame] | 92 | #if defined(POLARSSL_ECP_DP_BP512R1_ENABLED) |
| 93 | { POLARSSL_ECP_DP_BP512R1, 28, 512, "brainpool512r1" }, |
| 94 | #endif |
| 95 | #if defined(POLARSSL_ECP_DP_BP384R1_ENABLED) |
| 96 | { POLARSSL_ECP_DP_BP384R1, 27, 384, "brainpool384r1" }, |
| 97 | #endif |
| 98 | #if defined(POLARSSL_ECP_DP_BP256R1_ENABLED) |
| 99 | { POLARSSL_ECP_DP_BP256R1, 26, 256, "brainpool256r1" }, |
| 100 | #endif |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 101 | #if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED) |
Manuel Pégourié-Gonnard | 8195c1a | 2013-10-07 19:40:41 +0200 | [diff] [blame] | 102 | { POLARSSL_ECP_DP_SECP521R1, 25, 521, "secp521r1" }, |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 103 | #endif |
| 104 | #if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED) |
Manuel Pégourié-Gonnard | 8195c1a | 2013-10-07 19:40:41 +0200 | [diff] [blame] | 105 | { POLARSSL_ECP_DP_SECP384R1, 24, 384, "secp384r1" }, |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 106 | #endif |
| 107 | #if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED) |
Manuel Pégourié-Gonnard | 8195c1a | 2013-10-07 19:40:41 +0200 | [diff] [blame] | 108 | { POLARSSL_ECP_DP_SECP256R1, 23, 256, "secp256r1" }, |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 109 | #endif |
| 110 | #if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED) |
Manuel Pégourié-Gonnard | 8195c1a | 2013-10-07 19:40:41 +0200 | [diff] [blame] | 111 | { POLARSSL_ECP_DP_SECP224R1, 21, 224, "secp224r1" }, |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 112 | #endif |
| 113 | #if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED) |
Manuel Pégourié-Gonnard | 8195c1a | 2013-10-07 19:40:41 +0200 | [diff] [blame] | 114 | { POLARSSL_ECP_DP_SECP192R1, 19, 192, "secp192r1" }, |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 115 | #endif |
Manuel Pégourié-Gonnard | 8195c1a | 2013-10-07 19:40:41 +0200 | [diff] [blame] | 116 | { POLARSSL_ECP_DP_NONE, 0, 0, NULL }, |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | /* |
Manuel Pégourié-Gonnard | da179e4 | 2013-09-18 15:31:24 +0200 | [diff] [blame] | 120 | * List of supported curves and associated info |
| 121 | */ |
| 122 | const ecp_curve_info *ecp_curve_list( void ) |
| 123 | { |
| 124 | return ecp_supported_curves; |
| 125 | } |
| 126 | |
| 127 | /* |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 128 | * Get the curve info for the internal identifer |
| 129 | */ |
| 130 | const ecp_curve_info *ecp_curve_info_from_grp_id( ecp_group_id grp_id ) |
| 131 | { |
| 132 | const ecp_curve_info *curve_info; |
| 133 | |
| 134 | for( curve_info = ecp_curve_list(); |
| 135 | curve_info->grp_id != POLARSSL_ECP_DP_NONE; |
| 136 | curve_info++ ) |
| 137 | { |
| 138 | if( curve_info->grp_id == grp_id ) |
| 139 | return( curve_info ); |
| 140 | } |
| 141 | |
| 142 | return( NULL ); |
| 143 | } |
| 144 | |
| 145 | /* |
| 146 | * Get the curve info from the TLS identifier |
| 147 | */ |
| 148 | const ecp_curve_info *ecp_curve_info_from_tls_id( uint16_t tls_id ) |
| 149 | { |
| 150 | const ecp_curve_info *curve_info; |
| 151 | |
| 152 | for( curve_info = ecp_curve_list(); |
| 153 | curve_info->grp_id != POLARSSL_ECP_DP_NONE; |
| 154 | curve_info++ ) |
| 155 | { |
| 156 | if( curve_info->tls_id == tls_id ) |
| 157 | return( curve_info ); |
| 158 | } |
| 159 | |
| 160 | return( NULL ); |
| 161 | } |
| 162 | |
| 163 | /* |
Manuel Pégourié-Gonnard | b505c27 | 2012-11-05 17:27:54 +0100 | [diff] [blame] | 164 | * Initialize (the components of) a point |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 165 | */ |
| 166 | void ecp_point_init( ecp_point *pt ) |
| 167 | { |
| 168 | if( pt == NULL ) |
| 169 | return; |
| 170 | |
Manuel Pégourié-Gonnard | b505c27 | 2012-11-05 17:27:54 +0100 | [diff] [blame] | 171 | mpi_init( &pt->X ); |
| 172 | mpi_init( &pt->Y ); |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 173 | mpi_init( &pt->Z ); |
Manuel Pégourié-Gonnard | b505c27 | 2012-11-05 17:27:54 +0100 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | /* |
| 177 | * Initialize (the components of) a group |
| 178 | */ |
| 179 | void ecp_group_init( ecp_group *grp ) |
| 180 | { |
| 181 | if( grp == NULL ) |
| 182 | return; |
| 183 | |
Manuel Pégourié-Gonnard | c972770 | 2013-09-16 18:56:28 +0200 | [diff] [blame] | 184 | memset( grp, 0, sizeof( ecp_group ) ); |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | /* |
Manuel Pégourié-Gonnard | b8c6e0e | 2013-07-01 13:40:52 +0200 | [diff] [blame] | 188 | * Initialize (the components of) a key pair |
| 189 | */ |
| 190 | void ecp_keypair_init( ecp_keypair *key ) |
| 191 | { |
| 192 | if ( key == NULL ) |
| 193 | return; |
| 194 | |
| 195 | ecp_group_init( &key->grp ); |
| 196 | mpi_init( &key->d ); |
| 197 | ecp_point_init( &key->Q ); |
Manuel Pégourié-Gonnard | b8c6e0e | 2013-07-01 13:40:52 +0200 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | /* |
Manuel Pégourié-Gonnard | 1e8c8ec | 2012-10-31 19:24:21 +0100 | [diff] [blame] | 201 | * Unallocate (the components of) a point |
| 202 | */ |
| 203 | void ecp_point_free( ecp_point *pt ) |
| 204 | { |
| 205 | if( pt == NULL ) |
| 206 | return; |
| 207 | |
| 208 | mpi_free( &( pt->X ) ); |
| 209 | mpi_free( &( pt->Y ) ); |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 210 | mpi_free( &( pt->Z ) ); |
Manuel Pégourié-Gonnard | 1e8c8ec | 2012-10-31 19:24:21 +0100 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | /* |
| 214 | * Unallocate (the components of) a group |
| 215 | */ |
| 216 | void ecp_group_free( ecp_group *grp ) |
| 217 | { |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 218 | size_t i; |
| 219 | |
Manuel Pégourié-Gonnard | 1e8c8ec | 2012-10-31 19:24:21 +0100 | [diff] [blame] | 220 | if( grp == NULL ) |
| 221 | return; |
| 222 | |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 223 | mpi_free( &grp->P ); |
Manuel Pégourié-Gonnard | a070ada | 2013-10-08 12:04:56 +0200 | [diff] [blame] | 224 | mpi_free( &grp->A ); |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 225 | mpi_free( &grp->B ); |
| 226 | ecp_point_free( &grp->G ); |
| 227 | mpi_free( &grp->N ); |
Manuel Pégourié-Gonnard | c972770 | 2013-09-16 18:56:28 +0200 | [diff] [blame] | 228 | |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 229 | if( grp->T != NULL ) |
| 230 | { |
| 231 | for( i = 0; i < grp->T_size; i++ ) |
| 232 | ecp_point_free( &grp->T[i] ); |
| 233 | polarssl_free( grp->T ); |
| 234 | } |
| 235 | |
Manuel Pégourié-Gonnard | c972770 | 2013-09-16 18:56:28 +0200 | [diff] [blame] | 236 | memset( grp, 0, sizeof( ecp_group ) ); |
Manuel Pégourié-Gonnard | 1e8c8ec | 2012-10-31 19:24:21 +0100 | [diff] [blame] | 237 | } |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 238 | |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 239 | /* |
Manuel Pégourié-Gonnard | b8c6e0e | 2013-07-01 13:40:52 +0200 | [diff] [blame] | 240 | * Unallocate (the components of) a key pair |
| 241 | */ |
| 242 | void ecp_keypair_free( ecp_keypair *key ) |
| 243 | { |
| 244 | if ( key == NULL ) |
| 245 | return; |
| 246 | |
| 247 | ecp_group_free( &key->grp ); |
| 248 | mpi_free( &key->d ); |
| 249 | ecp_point_free( &key->Q ); |
Manuel Pégourié-Gonnard | b8c6e0e | 2013-07-01 13:40:52 +0200 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | /* |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 253 | * Copy the contents of a point |
| 254 | */ |
| 255 | int ecp_copy( ecp_point *P, const ecp_point *Q ) |
| 256 | { |
| 257 | int ret; |
| 258 | |
| 259 | MPI_CHK( mpi_copy( &P->X, &Q->X ) ); |
| 260 | MPI_CHK( mpi_copy( &P->Y, &Q->Y ) ); |
| 261 | MPI_CHK( mpi_copy( &P->Z, &Q->Z ) ); |
| 262 | |
| 263 | cleanup: |
| 264 | return( ret ); |
| 265 | } |
| 266 | |
| 267 | /* |
| 268 | * Copy the contents of a group object |
| 269 | */ |
| 270 | int ecp_group_copy( ecp_group *dst, const ecp_group *src ) |
| 271 | { |
| 272 | return ecp_use_known_dp( dst, src->id ); |
| 273 | } |
| 274 | |
| 275 | /* |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 276 | * Set point to zero |
| 277 | */ |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 278 | int ecp_set_zero( ecp_point *pt ) |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 279 | { |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 280 | int ret; |
| 281 | |
| 282 | MPI_CHK( mpi_lset( &pt->X , 1 ) ); |
| 283 | MPI_CHK( mpi_lset( &pt->Y , 1 ) ); |
| 284 | MPI_CHK( mpi_lset( &pt->Z , 0 ) ); |
| 285 | |
| 286 | cleanup: |
| 287 | return( ret ); |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | /* |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 291 | * Tell if a point is zero |
| 292 | */ |
| 293 | int ecp_is_zero( ecp_point *pt ) |
| 294 | { |
| 295 | return( mpi_cmp_int( &pt->Z, 0 ) == 0 ); |
| 296 | } |
| 297 | |
| 298 | /* |
Manuel Pégourié-Gonnard | 847395a | 2012-11-05 13:13:44 +0100 | [diff] [blame] | 299 | * Import a non-zero point from ASCII strings |
| 300 | */ |
| 301 | int ecp_point_read_string( ecp_point *P, int radix, |
| 302 | const char *x, const char *y ) |
| 303 | { |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 304 | int ret; |
Manuel Pégourié-Gonnard | 847395a | 2012-11-05 13:13:44 +0100 | [diff] [blame] | 305 | |
Manuel Pégourié-Gonnard | 847395a | 2012-11-05 13:13:44 +0100 | [diff] [blame] | 306 | MPI_CHK( mpi_read_string( &P->X, radix, x ) ); |
| 307 | MPI_CHK( mpi_read_string( &P->Y, radix, y ) ); |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 308 | MPI_CHK( mpi_lset( &P->Z, 1 ) ); |
Manuel Pégourié-Gonnard | 847395a | 2012-11-05 13:13:44 +0100 | [diff] [blame] | 309 | |
| 310 | cleanup: |
| 311 | return( ret ); |
| 312 | } |
| 313 | |
| 314 | /* |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 315 | * Export a point into unsigned binary data (SEC1 2.3.3) |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 316 | */ |
Manuel Pégourié-Gonnard | 7e86025 | 2013-02-10 10:58:48 +0100 | [diff] [blame] | 317 | int ecp_point_write_binary( const ecp_group *grp, const ecp_point *P, |
Manuel Pégourié-Gonnard | 420f1eb | 2013-02-10 12:22:46 +0100 | [diff] [blame] | 318 | int format, size_t *olen, |
Manuel Pégourié-Gonnard | 7e86025 | 2013-02-10 10:58:48 +0100 | [diff] [blame] | 319 | unsigned char *buf, size_t buflen ) |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 320 | { |
Paul Bakker | a280d0f | 2013-04-08 13:40:17 +0200 | [diff] [blame] | 321 | int ret = 0; |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 322 | size_t plen; |
| 323 | |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 324 | if( format != POLARSSL_ECP_PF_UNCOMPRESSED && |
| 325 | format != POLARSSL_ECP_PF_COMPRESSED ) |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 326 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 327 | |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 328 | /* |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 329 | * Common case: P == 0 |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 330 | */ |
| 331 | if( mpi_cmp_int( &P->Z, 0 ) == 0 ) |
| 332 | { |
| 333 | if( buflen < 1 ) |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 334 | return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 335 | |
| 336 | buf[0] = 0x00; |
| 337 | *olen = 1; |
| 338 | |
| 339 | return( 0 ); |
| 340 | } |
| 341 | |
| 342 | plen = mpi_size( &grp->P ); |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 343 | |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 344 | if( format == POLARSSL_ECP_PF_UNCOMPRESSED ) |
| 345 | { |
| 346 | *olen = 2 * plen + 1; |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 347 | |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 348 | if( buflen < *olen ) |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 349 | return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 350 | |
| 351 | buf[0] = 0x04; |
| 352 | MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) ); |
| 353 | MPI_CHK( mpi_write_binary( &P->Y, buf + 1 + plen, plen ) ); |
| 354 | } |
| 355 | else if( format == POLARSSL_ECP_PF_COMPRESSED ) |
| 356 | { |
| 357 | *olen = plen + 1; |
| 358 | |
| 359 | if( buflen < *olen ) |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 360 | return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 361 | |
| 362 | buf[0] = 0x02 + mpi_get_bit( &P->Y, 0 ); |
| 363 | MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) ); |
| 364 | } |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 365 | |
| 366 | cleanup: |
| 367 | return( ret ); |
| 368 | } |
| 369 | |
| 370 | /* |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 371 | * Import a point from unsigned binary data (SEC1 2.3.4) |
| 372 | */ |
Manuel Pégourié-Gonnard | 7e86025 | 2013-02-10 10:58:48 +0100 | [diff] [blame] | 373 | int ecp_point_read_binary( const ecp_group *grp, ecp_point *pt, |
| 374 | const unsigned char *buf, size_t ilen ) { |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 375 | int ret; |
| 376 | size_t plen; |
| 377 | |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 378 | if( ilen == 1 && buf[0] == 0x00 ) |
Manuel Pégourié-Gonnard | d84895d | 2013-02-10 10:53:04 +0100 | [diff] [blame] | 379 | return( ecp_set_zero( pt ) ); |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 380 | |
Manuel Pégourié-Gonnard | d84895d | 2013-02-10 10:53:04 +0100 | [diff] [blame] | 381 | plen = mpi_size( &grp->P ); |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 382 | |
| 383 | if( ilen != 2 * plen + 1 || buf[0] != 0x04 ) |
Manuel Pégourié-Gonnard | d84895d | 2013-02-10 10:53:04 +0100 | [diff] [blame] | 384 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 385 | |
Manuel Pégourié-Gonnard | d84895d | 2013-02-10 10:53:04 +0100 | [diff] [blame] | 386 | MPI_CHK( mpi_read_binary( &pt->X, buf + 1, plen ) ); |
| 387 | MPI_CHK( mpi_read_binary( &pt->Y, buf + 1 + plen, plen ) ); |
| 388 | MPI_CHK( mpi_lset( &pt->Z, 1 ) ); |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 389 | |
| 390 | cleanup: |
| 391 | return( ret ); |
| 392 | } |
| 393 | |
| 394 | /* |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 395 | * Import a point from a TLS ECPoint record (RFC 4492) |
| 396 | * struct { |
| 397 | * opaque point <1..2^8-1>; |
| 398 | * } ECPoint; |
| 399 | */ |
| 400 | int ecp_tls_read_point( const ecp_group *grp, ecp_point *pt, |
Manuel Pégourié-Gonnard | 98f5181 | 2013-02-10 13:38:29 +0100 | [diff] [blame] | 401 | const unsigned char **buf, size_t buf_len ) |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 402 | { |
| 403 | unsigned char data_len; |
Manuel Pégourié-Gonnard | 98f5181 | 2013-02-10 13:38:29 +0100 | [diff] [blame] | 404 | const unsigned char *buf_start; |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 405 | |
| 406 | /* |
| 407 | * We must have at least two bytes (1 for length, at least of for data) |
| 408 | */ |
| 409 | if( buf_len < 2 ) |
| 410 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
| 411 | |
Manuel Pégourié-Gonnard | 98f5181 | 2013-02-10 13:38:29 +0100 | [diff] [blame] | 412 | data_len = *(*buf)++; |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 413 | if( data_len < 1 || data_len > buf_len - 1 ) |
| 414 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
| 415 | |
Manuel Pégourié-Gonnard | 98f5181 | 2013-02-10 13:38:29 +0100 | [diff] [blame] | 416 | /* |
| 417 | * Save buffer start for read_binary and update buf |
| 418 | */ |
| 419 | buf_start = *buf; |
| 420 | *buf += data_len; |
| 421 | |
| 422 | return ecp_point_read_binary( grp, pt, buf_start, data_len ); |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | /* |
| 426 | * Export a point as a TLS ECPoint record (RFC 4492) |
| 427 | * struct { |
| 428 | * opaque point <1..2^8-1>; |
| 429 | * } ECPoint; |
| 430 | */ |
| 431 | int ecp_tls_write_point( const ecp_group *grp, const ecp_point *pt, |
Manuel Pégourié-Gonnard | 420f1eb | 2013-02-10 12:22:46 +0100 | [diff] [blame] | 432 | int format, size_t *olen, |
| 433 | unsigned char *buf, size_t blen ) |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 434 | { |
Manuel Pégourié-Gonnard | 420f1eb | 2013-02-10 12:22:46 +0100 | [diff] [blame] | 435 | int ret; |
| 436 | |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 437 | /* |
Manuel Pégourié-Gonnard | 420f1eb | 2013-02-10 12:22:46 +0100 | [diff] [blame] | 438 | * buffer length must be at least one, for our length byte |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 439 | */ |
Manuel Pégourié-Gonnard | 420f1eb | 2013-02-10 12:22:46 +0100 | [diff] [blame] | 440 | if( blen < 1 ) |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 441 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
| 442 | |
Manuel Pégourié-Gonnard | 420f1eb | 2013-02-10 12:22:46 +0100 | [diff] [blame] | 443 | if( ( ret = ecp_point_write_binary( grp, pt, format, |
| 444 | olen, buf + 1, blen - 1) ) != 0 ) |
| 445 | return( ret ); |
| 446 | |
| 447 | /* |
| 448 | * write length to the first byte and update total length |
| 449 | */ |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 450 | buf[0] = (unsigned char) *olen; |
Manuel Pégourié-Gonnard | 420f1eb | 2013-02-10 12:22:46 +0100 | [diff] [blame] | 451 | ++*olen; |
| 452 | |
| 453 | return 0; |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | /* |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 457 | * Import an ECP group from ASCII strings, general case (A used) |
Manuel Pégourié-Gonnard | 62aad14 | 2012-11-10 00:27:12 +0100 | [diff] [blame] | 458 | */ |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 459 | static int ecp_group_read_string_gen( ecp_group *grp, int radix, |
| 460 | const char *p, const char *a, const char *b, |
| 461 | const char *gx, const char *gy, const char *n) |
Manuel Pégourié-Gonnard | 62aad14 | 2012-11-10 00:27:12 +0100 | [diff] [blame] | 462 | { |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 463 | int ret; |
Manuel Pégourié-Gonnard | 62aad14 | 2012-11-10 00:27:12 +0100 | [diff] [blame] | 464 | |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 465 | MPI_CHK( mpi_read_string( &grp->P, radix, p ) ); |
| 466 | MPI_CHK( mpi_read_string( &grp->A, radix, a ) ); |
| 467 | MPI_CHK( mpi_read_string( &grp->B, radix, b ) ); |
| 468 | MPI_CHK( ecp_point_read_string( &grp->G, radix, gx, gy ) ); |
| 469 | MPI_CHK( mpi_read_string( &grp->N, radix, n ) ); |
Manuel Pégourié-Gonnard | 62aad14 | 2012-11-10 00:27:12 +0100 | [diff] [blame] | 470 | |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 471 | grp->pbits = mpi_msb( &grp->P ); |
| 472 | grp->nbits = mpi_msb( &grp->N ); |
Manuel Pégourié-Gonnard | 62aad14 | 2012-11-10 00:27:12 +0100 | [diff] [blame] | 473 | |
| 474 | cleanup: |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 475 | if( ret != 0 ) |
| 476 | ecp_group_free( grp ); |
| 477 | |
Manuel Pégourié-Gonnard | 62aad14 | 2012-11-10 00:27:12 +0100 | [diff] [blame] | 478 | return( ret ); |
| 479 | } |
| 480 | |
Manuel Pégourié-Gonnard | 210b458 | 2013-10-23 14:03:00 +0200 | [diff] [blame] | 481 | /* |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 482 | * Import an ECP group from ASCII strings, case A == -3 |
Manuel Pégourié-Gonnard | 210b458 | 2013-10-23 14:03:00 +0200 | [diff] [blame] | 483 | */ |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 484 | int ecp_group_read_string( ecp_group *grp, int radix, |
| 485 | const char *p, const char *b, |
| 486 | const char *gx, const char *gy, const char *n) |
Manuel Pégourié-Gonnard | 8433824 | 2012-11-11 20:45:18 +0100 | [diff] [blame] | 487 | { |
| 488 | int ret; |
Manuel Pégourié-Gonnard | 8433824 | 2012-11-11 20:45:18 +0100 | [diff] [blame] | 489 | |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 490 | MPI_CHK( ecp_group_read_string_gen( grp, radix, p, "00", b, gx, gy, n ) ); |
| 491 | MPI_CHK( mpi_add_int( &grp->A, &grp->P, -3 ) ); |
Manuel Pégourié-Gonnard | 8433824 | 2012-11-11 20:45:18 +0100 | [diff] [blame] | 492 | |
| 493 | cleanup: |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 494 | if( ret != 0 ) |
| 495 | ecp_group_free( grp ); |
Manuel Pégourié-Gonnard | e783f06 | 2013-10-21 14:52:21 +0200 | [diff] [blame] | 496 | |
| 497 | return( ret ); |
| 498 | } |
Manuel Pégourié-Gonnard | c04c530 | 2013-10-23 16:11:52 +0200 | [diff] [blame] | 499 | |
Manuel Pégourié-Gonnard | 62aad14 | 2012-11-10 00:27:12 +0100 | [diff] [blame] | 500 | /* |
Manuel Pégourié-Gonnard | dada4da | 2012-11-10 14:23:17 +0100 | [diff] [blame] | 501 | * Domain parameters for secp192r1 |
| 502 | */ |
| 503 | #define SECP192R1_P \ |
| 504 | "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF" |
| 505 | #define SECP192R1_B \ |
| 506 | "64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1" |
| 507 | #define SECP192R1_GX \ |
| 508 | "188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012" |
| 509 | #define SECP192R1_GY \ |
| 510 | "07192B95FFC8DA78631011ED6B24CDD573F977A11E794811" |
| 511 | #define SECP192R1_N \ |
| 512 | "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831" |
| 513 | |
| 514 | /* |
| 515 | * Domain parameters for secp224r1 |
| 516 | */ |
| 517 | #define SECP224R1_P \ |
| 518 | "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001" |
| 519 | #define SECP224R1_B \ |
| 520 | "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4" |
| 521 | #define SECP224R1_GX \ |
| 522 | "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21" |
| 523 | #define SECP224R1_GY \ |
| 524 | "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34" |
| 525 | #define SECP224R1_N \ |
| 526 | "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D" |
| 527 | |
| 528 | /* |
| 529 | * Domain parameters for secp256r1 |
| 530 | */ |
| 531 | #define SECP256R1_P \ |
| 532 | "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF" |
| 533 | #define SECP256R1_B \ |
| 534 | "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B" |
| 535 | #define SECP256R1_GX \ |
| 536 | "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296" |
| 537 | #define SECP256R1_GY \ |
| 538 | "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5" |
| 539 | #define SECP256R1_N \ |
| 540 | "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551" |
| 541 | |
| 542 | /* |
| 543 | * Domain parameters for secp384r1 |
| 544 | */ |
| 545 | #define SECP384R1_P \ |
| 546 | "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \ |
| 547 | "FFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF" |
| 548 | #define SECP384R1_B \ |
| 549 | "B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE814112" \ |
| 550 | "0314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF" |
| 551 | #define SECP384R1_GX \ |
| 552 | "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B98" \ |
| 553 | "59F741E082542A385502F25DBF55296C3A545E3872760AB7" |
| 554 | #define SECP384R1_GY \ |
| 555 | "3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A147C" \ |
| 556 | "E9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F" |
| 557 | #define SECP384R1_N \ |
| 558 | "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \ |
| 559 | "C7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973" |
| 560 | |
| 561 | /* |
| 562 | * Domain parameters for secp521r1 |
| 563 | */ |
| 564 | #define SECP521R1_P \ |
| 565 | "000001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \ |
| 566 | "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \ |
| 567 | "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" |
| 568 | #define SECP521R1_B \ |
| 569 | "00000051953EB9618E1C9A1F929A21A0B68540EEA2DA725B" \ |
| 570 | "99B315F3B8B489918EF109E156193951EC7E937B1652C0BD" \ |
| 571 | "3BB1BF073573DF883D2C34F1EF451FD46B503F00" |
| 572 | #define SECP521R1_GX \ |
| 573 | "000000C6858E06B70404E9CD9E3ECB662395B4429C648139" \ |
| 574 | "053FB521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127" \ |
| 575 | "A2FFA8DE3348B3C1856A429BF97E7E31C2E5BD66" |
| 576 | #define SECP521R1_GY \ |
| 577 | "0000011839296A789A3BC0045C8A5FB42C7D1BD998F54449" \ |
| 578 | "579B446817AFBD17273E662C97EE72995EF42640C550B901" \ |
| 579 | "3FAD0761353C7086A272C24088BE94769FD16650" |
| 580 | #define SECP521R1_N \ |
| 581 | "000001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \ |
| 582 | "FFFFFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148" \ |
| 583 | "F709A5D03BB5C9B8899C47AEBB6FB71E91386409" |
| 584 | |
| 585 | /* |
Manuel Pégourié-Gonnard | cec4a53 | 2013-10-07 19:52:27 +0200 | [diff] [blame] | 586 | * Domain parameters for brainpoolP256r1 (RFC 5639 3.4) |
| 587 | */ |
| 588 | #define BP256R1_P \ |
| 589 | "A9FB57DBA1EEA9BC3E660A909D838D726E3BF623D52620282013481D1F6E5377" |
| 590 | #define BP256R1_A \ |
| 591 | "7D5A0975FC2C3057EEF67530417AFFE7FB8055C126DC5C6CE94A4B44F330B5D9" |
| 592 | #define BP256R1_B \ |
| 593 | "26DC5C6CE94A4B44F330B5D9BBD77CBF958416295CF7E1CE6BCCDC18FF8C07B6" |
| 594 | #define BP256R1_GX \ |
| 595 | "8BD2AEB9CB7E57CB2C4B482FFC81B7AFB9DE27E1E3BD23C23A4453BD9ACE3262" |
| 596 | #define BP256R1_GY \ |
| 597 | "547EF835C3DAC4FD97F8461A14611DC9C27745132DED8E545C1D54C72F046997" |
| 598 | #define BP256R1_N \ |
| 599 | "A9FB57DBA1EEA9BC3E660A909D838D718C397AA3B561A6F7901E0E82974856A7" |
| 600 | |
| 601 | /* |
| 602 | * Domain parameters for brainpoolP384r1 (RFC 5639 3.6) |
| 603 | */ |
| 604 | #define BP384R1_P \ |
| 605 | "8CB91E82A3386D280F5D6F7E50E641DF152F7109ED5456B412B1DA197FB711" \ |
| 606 | "23ACD3A729901D1A71874700133107EC53" |
| 607 | #define BP384R1_A \ |
| 608 | "7BC382C63D8C150C3C72080ACE05AFA0C2BEA28E4FB22787139165EFBA91F9" \ |
| 609 | "0F8AA5814A503AD4EB04A8C7DD22CE2826" |
| 610 | #define BP384R1_B \ |
| 611 | "04A8C7DD22CE28268B39B55416F0447C2FB77DE107DCD2A62E880EA53EEB62" \ |
| 612 | "D57CB4390295DBC9943AB78696FA504C11" |
| 613 | #define BP384R1_GX \ |
| 614 | "1D1C64F068CF45FFA2A63A81B7C13F6B8847A3E77EF14FE3DB7FCAFE0CBD10" \ |
| 615 | "E8E826E03436D646AAEF87B2E247D4AF1E" |
| 616 | #define BP384R1_GY \ |
| 617 | "8ABE1D7520F9C2A45CB1EB8E95CFD55262B70B29FEEC5864E19C054FF99129" \ |
| 618 | "280E4646217791811142820341263C5315" |
| 619 | #define BP384R1_N \ |
| 620 | "8CB91E82A3386D280F5D6F7E50E641DF152F7109ED5456B31F166E6CAC0425" \ |
| 621 | "A7CF3AB6AF6B7FC3103B883202E9046565" |
| 622 | |
| 623 | /* |
| 624 | * Domain parameters for brainpoolP512r1 (RFC 5639 3.7) |
| 625 | */ |
| 626 | #define BP512R1_P \ |
| 627 | "AADD9DB8DBE9C48B3FD4E6AE33C9FC07CB308DB3B3C9D20ED6639CCA703308" \ |
| 628 | "717D4D9B009BC66842AECDA12AE6A380E62881FF2F2D82C68528AA6056583A48F3" |
| 629 | #define BP512R1_A \ |
| 630 | "7830A3318B603B89E2327145AC234CC594CBDD8D3DF91610A83441CAEA9863" \ |
| 631 | "BC2DED5D5AA8253AA10A2EF1C98B9AC8B57F1117A72BF2C7B9E7C1AC4D77FC94CA" |
| 632 | #define BP512R1_B \ |
| 633 | "3DF91610A83441CAEA9863BC2DED5D5AA8253AA10A2EF1C98B9AC8B57F1117" \ |
| 634 | "A72BF2C7B9E7C1AC4D77FC94CADC083E67984050B75EBAE5DD2809BD638016F723" |
| 635 | #define BP512R1_GX \ |
| 636 | "81AEE4BDD82ED9645A21322E9C4C6A9385ED9F70B5D916C1B43B62EEF4D009" \ |
| 637 | "8EFF3B1F78E2D0D48D50D1687B93B97D5F7C6D5047406A5E688B352209BCB9F822" |
| 638 | #define BP512R1_GY \ |
| 639 | "7DDE385D566332ECC0EABFA9CF7822FDF209F70024A57B1AA000C55B881F81" \ |
| 640 | "11B2DCDE494A5F485E5BCA4BD88A2763AED1CA2B2FA8F0540678CD1E0F3AD80892" |
| 641 | #define BP512R1_N \ |
| 642 | "AADD9DB8DBE9C48B3FD4E6AE33C9FC07CB308DB3B3C9D20ED6639CCA703308" \ |
| 643 | "70553E5C414CA92619418661197FAC10471DB1D381085DDADDB58796829CA90069" |
| 644 | |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 645 | #if defined(POLARSSL_ECP_NIST_OPTIM) |
| 646 | /* Forward declarations */ |
| 647 | static int ecp_mod_p192( mpi * ); |
| 648 | static int ecp_mod_p224( mpi * ); |
| 649 | static int ecp_mod_p256( mpi * ); |
| 650 | static int ecp_mod_p384( mpi * ); |
| 651 | static int ecp_mod_p521( mpi * ); |
| 652 | #endif |
| 653 | |
Manuel Pégourié-Gonnard | cec4a53 | 2013-10-07 19:52:27 +0200 | [diff] [blame] | 654 | /* |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 655 | * Set a group using well-known domain parameters |
| 656 | */ |
Manuel Pégourié-Gonnard | b325887 | 2013-02-10 12:06:19 +0100 | [diff] [blame] | 657 | int ecp_use_known_dp( ecp_group *grp, ecp_group_id id ) |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 658 | { |
Manuel Pégourié-Gonnard | b325887 | 2013-02-10 12:06:19 +0100 | [diff] [blame] | 659 | grp->id = id; |
| 660 | |
| 661 | switch( id ) |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 662 | { |
Paul Bakker | 5dc6b5f | 2013-06-29 23:26:34 +0200 | [diff] [blame] | 663 | #if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED) |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 664 | case POLARSSL_ECP_DP_SECP192R1: |
Manuel Pégourié-Gonnard | c04c530 | 2013-10-23 16:11:52 +0200 | [diff] [blame] | 665 | #if defined(POLARSSL_ECP_NIST_OPTIM) |
Manuel Pégourié-Gonnard | 8433824 | 2012-11-11 20:45:18 +0100 | [diff] [blame] | 666 | grp->modp = ecp_mod_p192; |
Manuel Pégourié-Gonnard | c04c530 | 2013-10-23 16:11:52 +0200 | [diff] [blame] | 667 | #endif |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 668 | return( ecp_group_read_string( grp, 16, |
Manuel Pégourié-Gonnard | dada4da | 2012-11-10 14:23:17 +0100 | [diff] [blame] | 669 | SECP192R1_P, SECP192R1_B, |
| 670 | SECP192R1_GX, SECP192R1_GY, SECP192R1_N ) ); |
Paul Bakker | 5dc6b5f | 2013-06-29 23:26:34 +0200 | [diff] [blame] | 671 | #endif /* POLARSSL_ECP_DP_SECP192R1_ENABLED */ |
Manuel Pégourié-Gonnard | dada4da | 2012-11-10 14:23:17 +0100 | [diff] [blame] | 672 | |
Paul Bakker | 5dc6b5f | 2013-06-29 23:26:34 +0200 | [diff] [blame] | 673 | #if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED) |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 674 | case POLARSSL_ECP_DP_SECP224R1: |
Manuel Pégourié-Gonnard | c04c530 | 2013-10-23 16:11:52 +0200 | [diff] [blame] | 675 | #if defined(POLARSSL_ECP_NIST_OPTIM) |
Manuel Pégourié-Gonnard | e783f06 | 2013-10-21 14:52:21 +0200 | [diff] [blame] | 676 | grp->modp = ecp_mod_p224; |
Manuel Pégourié-Gonnard | c04c530 | 2013-10-23 16:11:52 +0200 | [diff] [blame] | 677 | #endif |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 678 | return( ecp_group_read_string( grp, 16, |
Manuel Pégourié-Gonnard | dada4da | 2012-11-10 14:23:17 +0100 | [diff] [blame] | 679 | SECP224R1_P, SECP224R1_B, |
| 680 | SECP224R1_GX, SECP224R1_GY, SECP224R1_N ) ); |
Paul Bakker | 5dc6b5f | 2013-06-29 23:26:34 +0200 | [diff] [blame] | 681 | #endif /* POLARSSL_ECP_DP_SECP224R1_ENABLED */ |
Manuel Pégourié-Gonnard | dada4da | 2012-11-10 14:23:17 +0100 | [diff] [blame] | 682 | |
Paul Bakker | 5dc6b5f | 2013-06-29 23:26:34 +0200 | [diff] [blame] | 683 | #if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED) |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 684 | case POLARSSL_ECP_DP_SECP256R1: |
Manuel Pégourié-Gonnard | c04c530 | 2013-10-23 16:11:52 +0200 | [diff] [blame] | 685 | #if defined(POLARSSL_ECP_NIST_OPTIM) |
Manuel Pégourié-Gonnard | ec655c9 | 2013-10-23 14:50:39 +0200 | [diff] [blame] | 686 | grp->modp = ecp_mod_p256; |
Manuel Pégourié-Gonnard | c04c530 | 2013-10-23 16:11:52 +0200 | [diff] [blame] | 687 | #endif |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 688 | return( ecp_group_read_string( grp, 16, |
Manuel Pégourié-Gonnard | dada4da | 2012-11-10 14:23:17 +0100 | [diff] [blame] | 689 | SECP256R1_P, SECP256R1_B, |
| 690 | SECP256R1_GX, SECP256R1_GY, SECP256R1_N ) ); |
Paul Bakker | 5dc6b5f | 2013-06-29 23:26:34 +0200 | [diff] [blame] | 691 | #endif /* POLARSSL_ECP_DP_SECP256R1_ENABLED */ |
Manuel Pégourié-Gonnard | dada4da | 2012-11-10 14:23:17 +0100 | [diff] [blame] | 692 | |
Paul Bakker | 5dc6b5f | 2013-06-29 23:26:34 +0200 | [diff] [blame] | 693 | #if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED) |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 694 | case POLARSSL_ECP_DP_SECP384R1: |
Manuel Pégourié-Gonnard | c04c530 | 2013-10-23 16:11:52 +0200 | [diff] [blame] | 695 | #if defined(POLARSSL_ECP_NIST_OPTIM) |
Manuel Pégourié-Gonnard | 0f9149c | 2013-10-23 15:06:37 +0200 | [diff] [blame] | 696 | grp->modp = ecp_mod_p384; |
Manuel Pégourié-Gonnard | c04c530 | 2013-10-23 16:11:52 +0200 | [diff] [blame] | 697 | #endif |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 698 | return( ecp_group_read_string( grp, 16, |
Manuel Pégourié-Gonnard | dada4da | 2012-11-10 14:23:17 +0100 | [diff] [blame] | 699 | SECP384R1_P, SECP384R1_B, |
| 700 | SECP384R1_GX, SECP384R1_GY, SECP384R1_N ) ); |
Paul Bakker | 5dc6b5f | 2013-06-29 23:26:34 +0200 | [diff] [blame] | 701 | #endif /* POLARSSL_ECP_DP_SECP384R1_ENABLED */ |
Manuel Pégourié-Gonnard | dada4da | 2012-11-10 14:23:17 +0100 | [diff] [blame] | 702 | |
Paul Bakker | 5dc6b5f | 2013-06-29 23:26:34 +0200 | [diff] [blame] | 703 | #if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED) |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 704 | case POLARSSL_ECP_DP_SECP521R1: |
Manuel Pégourié-Gonnard | c04c530 | 2013-10-23 16:11:52 +0200 | [diff] [blame] | 705 | #if defined(POLARSSL_ECP_NIST_OPTIM) |
Manuel Pégourié-Gonnard | 62aad14 | 2012-11-10 00:27:12 +0100 | [diff] [blame] | 706 | grp->modp = ecp_mod_p521; |
Manuel Pégourié-Gonnard | c04c530 | 2013-10-23 16:11:52 +0200 | [diff] [blame] | 707 | #endif |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 708 | return( ecp_group_read_string( grp, 16, |
Manuel Pégourié-Gonnard | dada4da | 2012-11-10 14:23:17 +0100 | [diff] [blame] | 709 | SECP521R1_P, SECP521R1_B, |
| 710 | SECP521R1_GX, SECP521R1_GY, SECP521R1_N ) ); |
Paul Bakker | 5dc6b5f | 2013-06-29 23:26:34 +0200 | [diff] [blame] | 711 | #endif /* POLARSSL_ECP_DP_SECP521R1_ENABLED */ |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 712 | |
Manuel Pégourié-Gonnard | a070ada | 2013-10-08 12:04:56 +0200 | [diff] [blame] | 713 | #if defined(POLARSSL_ECP_DP_BP256R1_ENABLED) |
| 714 | case POLARSSL_ECP_DP_BP256R1: |
| 715 | return( ecp_group_read_string_gen( grp, 16, |
| 716 | BP256R1_P, BP256R1_A, BP256R1_B, |
| 717 | BP256R1_GX, BP256R1_GY, BP256R1_N ) ); |
| 718 | #endif /* POLARSSL_ECP_DP_BP256R1_ENABLED */ |
| 719 | |
| 720 | #if defined(POLARSSL_ECP_DP_BP384R1_ENABLED) |
| 721 | case POLARSSL_ECP_DP_BP384R1: |
| 722 | return( ecp_group_read_string_gen( grp, 16, |
| 723 | BP384R1_P, BP384R1_A, BP384R1_B, |
| 724 | BP384R1_GX, BP384R1_GY, BP384R1_N ) ); |
| 725 | #endif /* POLARSSL_ECP_DP_BP384R1_ENABLED */ |
| 726 | |
| 727 | #if defined(POLARSSL_ECP_DP_BP512R1_ENABLED) |
| 728 | case POLARSSL_ECP_DP_BP512R1: |
| 729 | return( ecp_group_read_string_gen( grp, 16, |
| 730 | BP512R1_P, BP512R1_A, BP512R1_B, |
| 731 | BP512R1_GX, BP512R1_GY, BP512R1_N ) ); |
| 732 | #endif /* POLARSSL_ECP_DP_BP512R1_ENABLED */ |
| 733 | |
Manuel Pégourié-Gonnard | 7038039 | 2013-09-16 16:19:53 +0200 | [diff] [blame] | 734 | default: |
Manuel Pégourié-Gonnard | a070ada | 2013-10-08 12:04:56 +0200 | [diff] [blame] | 735 | ecp_group_free( grp ); |
Manuel Pégourié-Gonnard | 7038039 | 2013-09-16 16:19:53 +0200 | [diff] [blame] | 736 | return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE ); |
| 737 | } |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | /* |
| 741 | * Set a group from an ECParameters record (RFC 4492) |
| 742 | */ |
Manuel Pégourié-Gonnard | 7c145c6 | 2013-02-10 13:20:52 +0100 | [diff] [blame] | 743 | int ecp_tls_read_group( ecp_group *grp, const unsigned char **buf, size_t len ) |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 744 | { |
Manuel Pégourié-Gonnard | f24b4a7 | 2013-09-23 18:14:50 +0200 | [diff] [blame] | 745 | uint16_t tls_id; |
| 746 | const ecp_curve_info *curve_info; |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 747 | |
| 748 | /* |
| 749 | * We expect at least three bytes (see below) |
| 750 | */ |
| 751 | if( len < 3 ) |
| 752 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
| 753 | |
| 754 | /* |
| 755 | * First byte is curve_type; only named_curve is handled |
| 756 | */ |
Manuel Pégourié-Gonnard | 7c145c6 | 2013-02-10 13:20:52 +0100 | [diff] [blame] | 757 | if( *(*buf)++ != POLARSSL_ECP_TLS_NAMED_CURVE ) |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 758 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
| 759 | |
| 760 | /* |
Manuel Pégourié-Gonnard | b325887 | 2013-02-10 12:06:19 +0100 | [diff] [blame] | 761 | * Next two bytes are the namedcurve value |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 762 | */ |
Manuel Pégourié-Gonnard | f24b4a7 | 2013-09-23 18:14:50 +0200 | [diff] [blame] | 763 | tls_id = *(*buf)++; |
| 764 | tls_id <<= 8; |
| 765 | tls_id |= *(*buf)++; |
| 766 | |
| 767 | if( ( curve_info = ecp_curve_info_from_tls_id( tls_id ) ) == NULL ) |
| 768 | return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE ); |
| 769 | |
| 770 | return ecp_use_known_dp( grp, curve_info->grp_id ); |
Manuel Pégourié-Gonnard | b325887 | 2013-02-10 12:06:19 +0100 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | /* |
| 774 | * Write the ECParameters record corresponding to a group (RFC 4492) |
| 775 | */ |
| 776 | int ecp_tls_write_group( const ecp_group *grp, size_t *olen, |
| 777 | unsigned char *buf, size_t blen ) |
| 778 | { |
Manuel Pégourié-Gonnard | f24b4a7 | 2013-09-23 18:14:50 +0200 | [diff] [blame] | 779 | const ecp_curve_info *curve_info; |
| 780 | |
| 781 | if( ( curve_info = ecp_curve_info_from_grp_id( grp->id ) ) == NULL ) |
| 782 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 7038039 | 2013-09-16 16:19:53 +0200 | [diff] [blame] | 783 | |
Manuel Pégourié-Gonnard | b325887 | 2013-02-10 12:06:19 +0100 | [diff] [blame] | 784 | /* |
| 785 | * We are going to write 3 bytes (see below) |
| 786 | */ |
| 787 | *olen = 3; |
| 788 | if( blen < *olen ) |
| 789 | return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL ); |
| 790 | |
| 791 | /* |
| 792 | * First byte is curve_type, always named_curve |
| 793 | */ |
| 794 | *buf++ = POLARSSL_ECP_TLS_NAMED_CURVE; |
| 795 | |
| 796 | /* |
| 797 | * Next two bytes are the namedcurve value |
| 798 | */ |
Manuel Pégourié-Gonnard | f24b4a7 | 2013-09-23 18:14:50 +0200 | [diff] [blame] | 799 | buf[0] = curve_info->tls_id >> 8; |
| 800 | buf[1] = curve_info->tls_id & 0xFF; |
Manuel Pégourié-Gonnard | b325887 | 2013-02-10 12:06:19 +0100 | [diff] [blame] | 801 | |
| 802 | return 0; |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 803 | } |
Manuel Pégourié-Gonnard | ab38b70 | 2012-11-05 17:34:55 +0100 | [diff] [blame] | 804 | |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 805 | /* |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 806 | * Wrapper around fast quasi-modp functions, with fall-back to mpi_mod_mpi. |
| 807 | * See the documentation of struct ecp_group. |
| 808 | * |
| 809 | * This function is in the critial loop for ecp_mul, so pay attention to perf. |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 810 | */ |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 811 | static int ecp_modp( mpi *N, const ecp_group *grp ) |
Manuel Pégourié-Gonnard | 7038039 | 2013-09-16 16:19:53 +0200 | [diff] [blame] | 812 | { |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 813 | int ret; |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 814 | |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 815 | if( grp->modp == NULL ) |
| 816 | return( mpi_mod_mpi( N, N, &grp->P ) ); |
| 817 | |
| 818 | /* N->s < 0 is a much faster test, which fails only if N is 0 */ |
| 819 | if( ( N->s < 0 && mpi_cmp_int( N, 0 ) != 0 ) || |
| 820 | mpi_msb( N ) > 2 * grp->pbits ) |
Manuel Pégourié-Gonnard | 7038039 | 2013-09-16 16:19:53 +0200 | [diff] [blame] | 821 | { |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 822 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 7038039 | 2013-09-16 16:19:53 +0200 | [diff] [blame] | 823 | } |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 824 | |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 825 | MPI_CHK( grp->modp( N ) ); |
Manuel Pégourié-Gonnard | 7038039 | 2013-09-16 16:19:53 +0200 | [diff] [blame] | 826 | |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 827 | /* N->s < 0 is a much faster test, which fails only if N is 0 */ |
| 828 | while( N->s < 0 && mpi_cmp_int( N, 0 ) != 0 ) |
| 829 | MPI_CHK( mpi_add_mpi( N, N, &grp->P ) ); |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 830 | |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 831 | while( mpi_cmp_mpi( N, &grp->P ) >= 0 ) |
| 832 | /* we known P, N and the result are positive */ |
| 833 | MPI_CHK( mpi_sub_abs( N, N, &grp->P ) ); |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 834 | |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 835 | cleanup: |
| 836 | return( ret ); |
Manuel Pégourié-Gonnard | 7038039 | 2013-09-16 16:19:53 +0200 | [diff] [blame] | 837 | } |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 838 | |
Manuel Pégourié-Gonnard | 847395a | 2012-11-05 13:13:44 +0100 | [diff] [blame] | 839 | /* |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 840 | * 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] | 841 | * |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 842 | * In order to guarantee that, we need to ensure that operands of |
| 843 | * 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] | 844 | * bring the result back to this range. |
| 845 | * |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 846 | * The following macros are shortcuts for doing that. |
Manuel Pégourié-Gonnard | dada4da | 2012-11-10 14:23:17 +0100 | [diff] [blame] | 847 | */ |
| 848 | |
| 849 | /* |
Manuel Pégourié-Gonnard | 84d1aea | 2012-11-09 02:09:38 +0100 | [diff] [blame] | 850 | * Reduce a mpi mod p in-place, general case, to use after mpi_mul_mpi |
| 851 | */ |
Manuel Pégourié-Gonnard | 62aad14 | 2012-11-10 00:27:12 +0100 | [diff] [blame] | 852 | #define MOD_MUL( N ) MPI_CHK( ecp_modp( &N, grp ) ) |
Manuel Pégourié-Gonnard | 84d1aea | 2012-11-09 02:09:38 +0100 | [diff] [blame] | 853 | |
| 854 | /* |
| 855 | * Reduce a mpi mod p in-place, to use after mpi_sub_mpi |
Manuel Pégourié-Gonnard | c9e387c | 2013-10-17 17:15:35 +0200 | [diff] [blame] | 856 | * N->s < 0 is a very fast test, which fails only if N is 0 |
Manuel Pégourié-Gonnard | 84d1aea | 2012-11-09 02:09:38 +0100 | [diff] [blame] | 857 | */ |
| 858 | #define MOD_SUB( N ) \ |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 859 | while( N.s < 0 && mpi_cmp_int( &N, 0 ) != 0 ) \ |
Manuel Pégourié-Gonnard | 84d1aea | 2012-11-09 02:09:38 +0100 | [diff] [blame] | 860 | MPI_CHK( mpi_add_mpi( &N, &N, &grp->P ) ) |
| 861 | |
| 862 | /* |
Manuel Pégourié-Gonnard | c9e387c | 2013-10-17 17:15:35 +0200 | [diff] [blame] | 863 | * Reduce a mpi mod p in-place, to use after mpi_add_mpi and mpi_mul_int. |
| 864 | * We known P, N and the result are positive, so sub_abs is correct, and |
| 865 | * a bit faster. |
Manuel Pégourié-Gonnard | 84d1aea | 2012-11-09 02:09:38 +0100 | [diff] [blame] | 866 | */ |
| 867 | #define MOD_ADD( N ) \ |
| 868 | while( mpi_cmp_mpi( &N, &grp->P ) >= 0 ) \ |
Manuel Pégourié-Gonnard | c9e387c | 2013-10-17 17:15:35 +0200 | [diff] [blame] | 869 | MPI_CHK( mpi_sub_abs( &N, &N, &grp->P ) ) |
Manuel Pégourié-Gonnard | 84d1aea | 2012-11-09 02:09:38 +0100 | [diff] [blame] | 870 | |
| 871 | /* |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 872 | * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1) |
Manuel Pégourié-Gonnard | 04a0225 | 2013-11-20 22:57:38 +0100 | [diff] [blame^] | 873 | * Cost: 1N := 1I + 3M + 1S |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 874 | */ |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 875 | 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] | 876 | { |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 877 | int ret; |
Manuel Pégourié-Gonnard | cdd4432 | 2012-11-21 16:00:55 +0100 | [diff] [blame] | 878 | mpi Zi, ZZi; |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 879 | |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 880 | if( mpi_cmp_int( &pt->Z, 0 ) == 0 ) |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 881 | return( 0 ); |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 882 | |
Manuel Pégourié-Gonnard | cdd4432 | 2012-11-21 16:00:55 +0100 | [diff] [blame] | 883 | mpi_init( &Zi ); mpi_init( &ZZi ); |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 884 | |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 885 | /* |
| 886 | * X = X / Z^2 mod p |
| 887 | */ |
| 888 | MPI_CHK( mpi_inv_mod( &Zi, &pt->Z, &grp->P ) ); |
| 889 | MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi ); |
| 890 | 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] | 891 | |
| 892 | /* |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 893 | * Y = Y / Z^3 mod p |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 894 | */ |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 895 | MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ZZi ) ); MOD_MUL( pt->Y ); |
| 896 | 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] | 897 | |
| 898 | /* |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 899 | * Z = 1 |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 900 | */ |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 901 | MPI_CHK( mpi_lset( &pt->Z, 1 ) ); |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 902 | |
| 903 | cleanup: |
| 904 | |
Manuel Pégourié-Gonnard | cdd4432 | 2012-11-21 16:00:55 +0100 | [diff] [blame] | 905 | mpi_free( &Zi ); mpi_free( &ZZi ); |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 906 | |
| 907 | return( ret ); |
| 908 | } |
| 909 | |
| 910 | /* |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 911 | * Normalize jacobian coordinates of an array of (pointers to) points, |
Manuel Pégourié-Gonnard | 3680c82 | 2012-11-21 18:49:45 +0100 | [diff] [blame] | 912 | * using Montgomery's trick to perform only one inversion mod P. |
Manuel Pégourié-Gonnard | cdd4432 | 2012-11-21 16:00:55 +0100 | [diff] [blame] | 913 | * (See for example Cohen's "A Course in Computational Algebraic Number |
| 914 | * Theory", Algorithm 10.3.4.) |
| 915 | * |
Manuel Pégourié-Gonnard | 07de4b1 | 2013-09-02 16:26:04 +0200 | [diff] [blame] | 916 | * Warning: fails (returning an error) if one of the points is zero! |
Manuel Pégourié-Gonnard | 3680c82 | 2012-11-21 18:49:45 +0100 | [diff] [blame] | 917 | * This should never happen, see choice of w in ecp_mul(). |
Manuel Pégourié-Gonnard | 04a0225 | 2013-11-20 22:57:38 +0100 | [diff] [blame^] | 918 | * |
| 919 | * Cost: 1N(t) := 1I + (6t - 3)M + 1S |
Manuel Pégourié-Gonnard | cdd4432 | 2012-11-21 16:00:55 +0100 | [diff] [blame] | 920 | */ |
| 921 | static int ecp_normalize_many( const ecp_group *grp, |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 922 | ecp_point *T[], size_t t_len ) |
Manuel Pégourié-Gonnard | cdd4432 | 2012-11-21 16:00:55 +0100 | [diff] [blame] | 923 | { |
| 924 | int ret; |
| 925 | size_t i; |
| 926 | mpi *c, u, Zi, ZZi; |
| 927 | |
| 928 | if( t_len < 2 ) |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 929 | return( ecp_normalize( grp, *T ) ); |
Manuel Pégourié-Gonnard | cdd4432 | 2012-11-21 16:00:55 +0100 | [diff] [blame] | 930 | |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 931 | if( ( c = (mpi *) polarssl_malloc( t_len * sizeof( mpi ) ) ) == NULL ) |
Manuel Pégourié-Gonnard | 456d3b9 | 2013-09-16 18:04:38 +0200 | [diff] [blame] | 932 | return( POLARSSL_ERR_ECP_MALLOC_FAILED ); |
Manuel Pégourié-Gonnard | cdd4432 | 2012-11-21 16:00:55 +0100 | [diff] [blame] | 933 | |
| 934 | mpi_init( &u ); mpi_init( &Zi ); mpi_init( &ZZi ); |
| 935 | for( i = 0; i < t_len; i++ ) |
| 936 | mpi_init( &c[i] ); |
| 937 | |
| 938 | /* |
| 939 | * c[i] = Z_0 * ... * Z_i |
| 940 | */ |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 941 | MPI_CHK( mpi_copy( &c[0], &T[0]->Z ) ); |
Manuel Pégourié-Gonnard | cdd4432 | 2012-11-21 16:00:55 +0100 | [diff] [blame] | 942 | for( i = 1; i < t_len; i++ ) |
| 943 | { |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 944 | MPI_CHK( mpi_mul_mpi( &c[i], &c[i-1], &T[i]->Z ) ); |
Manuel Pégourié-Gonnard | cdd4432 | 2012-11-21 16:00:55 +0100 | [diff] [blame] | 945 | MOD_MUL( c[i] ); |
| 946 | } |
| 947 | |
| 948 | /* |
| 949 | * u = 1 / (Z_0 * ... * Z_n) mod P |
| 950 | */ |
| 951 | MPI_CHK( mpi_inv_mod( &u, &c[t_len-1], &grp->P ) ); |
| 952 | |
| 953 | for( i = t_len - 1; ; i-- ) |
| 954 | { |
| 955 | /* |
| 956 | * Zi = 1 / Z_i mod p |
| 957 | * u = 1 / (Z_0 * ... * Z_i) mod P |
| 958 | */ |
| 959 | if( i == 0 ) { |
| 960 | MPI_CHK( mpi_copy( &Zi, &u ) ); |
| 961 | } |
| 962 | else |
| 963 | { |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 964 | MPI_CHK( mpi_mul_mpi( &Zi, &u, &c[i-1] ) ); MOD_MUL( Zi ); |
| 965 | MPI_CHK( mpi_mul_mpi( &u, &u, &T[i]->Z ) ); MOD_MUL( u ); |
Manuel Pégourié-Gonnard | cdd4432 | 2012-11-21 16:00:55 +0100 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | /* |
| 969 | * proceed as in normalize() |
| 970 | */ |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 971 | MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi ); |
| 972 | MPI_CHK( mpi_mul_mpi( &T[i]->X, &T[i]->X, &ZZi ) ); MOD_MUL( T[i]->X ); |
| 973 | MPI_CHK( mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &ZZi ) ); MOD_MUL( T[i]->Y ); |
| 974 | MPI_CHK( mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &Zi ) ); MOD_MUL( T[i]->Y ); |
| 975 | MPI_CHK( mpi_lset( &T[i]->Z, 1 ) ); |
Manuel Pégourié-Gonnard | cdd4432 | 2012-11-21 16:00:55 +0100 | [diff] [blame] | 976 | |
| 977 | if( i == 0 ) |
| 978 | break; |
| 979 | } |
| 980 | |
| 981 | cleanup: |
| 982 | |
| 983 | mpi_free( &u ); mpi_free( &Zi ); mpi_free( &ZZi ); |
| 984 | for( i = 0; i < t_len; i++ ) |
| 985 | mpi_free( &c[i] ); |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 986 | polarssl_free( c ); |
Manuel Pégourié-Gonnard | cdd4432 | 2012-11-21 16:00:55 +0100 | [diff] [blame] | 987 | |
| 988 | return( ret ); |
| 989 | } |
| 990 | |
Manuel Pégourié-Gonnard | cdd4432 | 2012-11-21 16:00:55 +0100 | [diff] [blame] | 991 | /* |
Manuel Pégourié-Gonnard | 0cd6f98 | 2013-10-10 15:55:39 +0200 | [diff] [blame] | 992 | * Point doubling R = 2 P, Jacobian coordinates |
Manuel Pégourié-Gonnard | 0ace4b3 | 2013-10-10 12:44:27 +0200 | [diff] [blame] | 993 | * |
Manuel Pégourié-Gonnard | 1c4aa24 | 2013-10-09 16:09:46 +0200 | [diff] [blame] | 994 | * http://www.hyperelliptic.org/EFD/g1p/auto-code/shortw/jacobian/doubling/dbl-2007-bl.op3 |
Manuel Pégourié-Gonnard | 0ace4b3 | 2013-10-10 12:44:27 +0200 | [diff] [blame] | 995 | * with heavy variable renaming, some reordering and one minor modification |
| 996 | * (a = 2 * b, c = d - 2a replaced with c = d, c = c - b, c = c - b) |
| 997 | * in order to use a lot less intermediate variables (6 vs 25). |
Manuel Pégourié-Gonnard | 04a0225 | 2013-11-20 22:57:38 +0100 | [diff] [blame^] | 998 | * |
| 999 | * Cost: 1D := 2M + 8S |
Manuel Pégourié-Gonnard | 1c4aa24 | 2013-10-09 16:09:46 +0200 | [diff] [blame] | 1000 | */ |
Manuel Pégourié-Gonnard | 0cd6f98 | 2013-10-10 15:55:39 +0200 | [diff] [blame] | 1001 | static int ecp_double_jac( const ecp_group *grp, ecp_point *R, |
| 1002 | const ecp_point *P ) |
Manuel Pégourié-Gonnard | 1c4aa24 | 2013-10-09 16:09:46 +0200 | [diff] [blame] | 1003 | { |
| 1004 | int ret; |
Manuel Pégourié-Gonnard | 0ace4b3 | 2013-10-10 12:44:27 +0200 | [diff] [blame] | 1005 | mpi T1, T2, T3, X3, Y3, Z3; |
Manuel Pégourié-Gonnard | 1c4aa24 | 2013-10-09 16:09:46 +0200 | [diff] [blame] | 1006 | |
Manuel Pégourié-Gonnard | 0cd6f98 | 2013-10-10 15:55:39 +0200 | [diff] [blame] | 1007 | #if defined(POLARSSL_SELF_TEST) |
| 1008 | dbl_count++; |
| 1009 | #endif |
| 1010 | |
Manuel Pégourié-Gonnard | 0ace4b3 | 2013-10-10 12:44:27 +0200 | [diff] [blame] | 1011 | mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 ); |
| 1012 | mpi_init( &X3 ); mpi_init( &Y3 ); mpi_init( &Z3 ); |
Manuel Pégourié-Gonnard | 1c4aa24 | 2013-10-09 16:09:46 +0200 | [diff] [blame] | 1013 | |
Manuel Pégourié-Gonnard | 0ace4b3 | 2013-10-10 12:44:27 +0200 | [diff] [blame] | 1014 | MPI_CHK( mpi_mul_mpi( &T3, &P->X, &P->X ) ); MOD_MUL( T3 ); |
| 1015 | MPI_CHK( mpi_mul_mpi( &T2, &P->Y, &P->Y ) ); MOD_MUL( T2 ); |
| 1016 | MPI_CHK( mpi_mul_mpi( &Y3, &T2, &T2 ) ); MOD_MUL( Y3 ); |
| 1017 | MPI_CHK( mpi_add_mpi( &X3, &P->X, &T2 ) ); MOD_ADD( X3 ); |
| 1018 | MPI_CHK( mpi_mul_mpi( &X3, &X3, &X3 ) ); MOD_MUL( X3 ); |
| 1019 | MPI_CHK( mpi_sub_mpi( &X3, &X3, &Y3 ) ); MOD_SUB( X3 ); |
| 1020 | MPI_CHK( mpi_sub_mpi( &X3, &X3, &T3 ) ); MOD_SUB( X3 ); |
| 1021 | MPI_CHK( mpi_mul_int( &T1, &X3, 2 ) ); MOD_ADD( T1 ); |
| 1022 | MPI_CHK( mpi_mul_mpi( &Z3, &P->Z, &P->Z ) ); MOD_MUL( Z3 ); |
| 1023 | MPI_CHK( mpi_mul_mpi( &X3, &Z3, &Z3 ) ); MOD_MUL( X3 ); |
| 1024 | MPI_CHK( mpi_mul_int( &T3, &T3, 3 ) ); MOD_ADD( T3 ); |
| 1025 | MPI_CHK( mpi_mul_mpi( &X3, &X3, &grp->A ) ); MOD_MUL( X3 ); |
| 1026 | MPI_CHK( mpi_add_mpi( &T3, &T3, &X3 ) ); MOD_ADD( T3 ); |
| 1027 | MPI_CHK( mpi_mul_mpi( &X3, &T3, &T3 ) ); MOD_MUL( X3 ); |
| 1028 | MPI_CHK( mpi_sub_mpi( &X3, &X3, &T1 ) ); MOD_SUB( X3 ); |
| 1029 | MPI_CHK( mpi_sub_mpi( &X3, &X3, &T1 ) ); MOD_SUB( X3 ); |
| 1030 | MPI_CHK( mpi_sub_mpi( &T1, &T1, &X3 ) ); MOD_SUB( T1 ); |
| 1031 | MPI_CHK( mpi_mul_mpi( &T1, &T3, &T1 ) ); MOD_MUL( T1 ); |
| 1032 | MPI_CHK( mpi_mul_int( &T3, &Y3, 8 ) ); MOD_ADD( T3 ); |
| 1033 | MPI_CHK( mpi_sub_mpi( &Y3, &T1, &T3 ) ); MOD_SUB( Y3 ); |
| 1034 | MPI_CHK( mpi_add_mpi( &T1, &P->Y, &P->Z ) ); MOD_ADD( T1 ); |
| 1035 | MPI_CHK( mpi_mul_mpi( &T1, &T1, &T1 ) ); MOD_MUL( T1 ); |
| 1036 | MPI_CHK( mpi_sub_mpi( &T1, &T1, &T2 ) ); MOD_SUB( T1 ); |
| 1037 | MPI_CHK( mpi_sub_mpi( &Z3, &T1, &Z3 ) ); MOD_SUB( Z3 ); |
Manuel Pégourié-Gonnard | 1c4aa24 | 2013-10-09 16:09:46 +0200 | [diff] [blame] | 1038 | |
| 1039 | MPI_CHK( mpi_copy( &R->X, &X3 ) ); |
| 1040 | MPI_CHK( mpi_copy( &R->Y, &Y3 ) ); |
| 1041 | MPI_CHK( mpi_copy( &R->Z, &Z3 ) ); |
| 1042 | |
| 1043 | cleanup: |
Manuel Pégourié-Gonnard | 0ace4b3 | 2013-10-10 12:44:27 +0200 | [diff] [blame] | 1044 | mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 ); |
| 1045 | mpi_free( &X3 ); mpi_free( &Y3 ); mpi_free( &Z3 ); |
Manuel Pégourié-Gonnard | 1c4aa24 | 2013-10-09 16:09:46 +0200 | [diff] [blame] | 1046 | |
| 1047 | return( ret ); |
| 1048 | } |
| 1049 | |
| 1050 | /* |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 1051 | * Addition or subtraction: R = P + Q or R = P - Q, |
Manuel Pégourié-Gonnard | 9674fd0 | 2012-11-19 21:23:27 +0100 | [diff] [blame] | 1052 | * mixed affine-Jacobian coordinates (GECC 3.22) |
| 1053 | * |
| 1054 | * The coordinates of Q must be normalized (= affine), |
| 1055 | * but those of P don't need to. R is not normalized. |
| 1056 | * |
| 1057 | * If sign >= 0, perform addition, otherwise perform subtraction, |
| 1058 | * taking advantage of the fact that, for Q != 0, we have |
| 1059 | * -Q = (Q.X, -Q.Y, Q.Z) |
Manuel Pégourié-Gonnard | 04a0225 | 2013-11-20 22:57:38 +0100 | [diff] [blame^] | 1060 | * |
| 1061 | * Cost: 1A := 8M + 3S |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 1062 | */ |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 1063 | 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] | 1064 | const ecp_point *P, const ecp_point *Q, |
| 1065 | signed char sign ) |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 1066 | { |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 1067 | int ret; |
Manuel Pégourié-Gonnard | 7e0adfb | 2012-11-08 23:21:46 +0100 | [diff] [blame] | 1068 | mpi T1, T2, T3, T4, X, Y, Z; |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 1069 | |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 1070 | #if defined(POLARSSL_SELF_TEST) |
| 1071 | add_count++; |
| 1072 | #endif |
| 1073 | |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 1074 | /* |
Manuel Pégourié-Gonnard | 7e0adfb | 2012-11-08 23:21:46 +0100 | [diff] [blame] | 1075 | * Trivial cases: P == 0 or Q == 0 |
Manuel Pégourié-Gonnard | 9674fd0 | 2012-11-19 21:23:27 +0100 | [diff] [blame] | 1076 | * (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] | 1077 | */ |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 1078 | if( mpi_cmp_int( &Q->Z, 0 ) == 0 ) |
| 1079 | return( ecp_copy( R, P ) ); |
| 1080 | |
Manuel Pégourié-Gonnard | 9674fd0 | 2012-11-19 21:23:27 +0100 | [diff] [blame] | 1081 | if( mpi_cmp_int( &P->Z, 0 ) == 0 ) |
| 1082 | { |
| 1083 | ret = ecp_copy( R, Q ); |
| 1084 | |
| 1085 | /* |
| 1086 | * -R.Y mod P = P - R.Y unless R.Y == 0 |
| 1087 | */ |
| 1088 | if( ret == 0 && sign < 0) |
| 1089 | if( mpi_cmp_int( &R->Y, 0 ) != 0 ) |
| 1090 | ret = mpi_sub_mpi( &R->Y, &grp->P, &R->Y ); |
| 1091 | |
| 1092 | return( ret ); |
| 1093 | } |
| 1094 | |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 1095 | /* |
| 1096 | * Make sure Q coordinates are normalized |
| 1097 | */ |
| 1098 | if( mpi_cmp_int( &Q->Z, 1 ) != 0 ) |
Manuel Pégourié-Gonnard | 456d3b9 | 2013-09-16 18:04:38 +0200 | [diff] [blame] | 1099 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 1100 | |
Manuel Pégourié-Gonnard | 7e0adfb | 2012-11-08 23:21:46 +0100 | [diff] [blame] | 1101 | mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 ); mpi_init( &T4 ); |
| 1102 | mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z ); |
Manuel Pégourié-Gonnard | ab38b70 | 2012-11-05 17:34:55 +0100 | [diff] [blame] | 1103 | |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 1104 | MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 ); |
| 1105 | MPI_CHK( mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 ); |
| 1106 | MPI_CHK( mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 ); |
| 1107 | 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] | 1108 | |
| 1109 | /* |
| 1110 | * For subtraction, -Q.Y should have been used instead of Q.Y, |
| 1111 | * so we replace T2 by -T2, which is P - T2 mod P |
| 1112 | */ |
| 1113 | if( sign < 0 ) |
| 1114 | { |
| 1115 | MPI_CHK( mpi_sub_mpi( &T2, &grp->P, &T2 ) ); |
| 1116 | MOD_SUB( T2 ); |
| 1117 | } |
| 1118 | |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 1119 | MPI_CHK( mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 ); |
| 1120 | 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] | 1121 | |
Manuel Pégourié-Gonnard | 7e0adfb | 2012-11-08 23:21:46 +0100 | [diff] [blame] | 1122 | if( mpi_cmp_int( &T1, 0 ) == 0 ) |
| 1123 | { |
| 1124 | if( mpi_cmp_int( &T2, 0 ) == 0 ) |
| 1125 | { |
| 1126 | ret = ecp_double_jac( grp, R, P ); |
| 1127 | goto cleanup; |
| 1128 | } |
| 1129 | else |
| 1130 | { |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 1131 | ret = ecp_set_zero( R ); |
Manuel Pégourié-Gonnard | 7e0adfb | 2012-11-08 23:21:46 +0100 | [diff] [blame] | 1132 | goto cleanup; |
| 1133 | } |
| 1134 | } |
| 1135 | |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 1136 | MPI_CHK( mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z ); |
| 1137 | MPI_CHK( mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 ); |
| 1138 | MPI_CHK( mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 ); |
| 1139 | MPI_CHK( mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 ); |
| 1140 | MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 ); |
| 1141 | MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X ); |
| 1142 | MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X ); |
| 1143 | MPI_CHK( mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X ); |
| 1144 | MPI_CHK( mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 ); |
| 1145 | MPI_CHK( mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 ); |
| 1146 | MPI_CHK( mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 ); |
| 1147 | 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] | 1148 | |
Manuel Pégourié-Gonnard | 84d1aea | 2012-11-09 02:09:38 +0100 | [diff] [blame] | 1149 | MPI_CHK( mpi_copy( &R->X, &X ) ); |
| 1150 | MPI_CHK( mpi_copy( &R->Y, &Y ) ); |
| 1151 | MPI_CHK( mpi_copy( &R->Z, &Z ) ); |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 1152 | |
| 1153 | cleanup: |
| 1154 | |
Manuel Pégourié-Gonnard | 7e0adfb | 2012-11-08 23:21:46 +0100 | [diff] [blame] | 1155 | mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 ); mpi_free( &T4 ); |
| 1156 | mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z ); |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 1157 | |
| 1158 | return( ret ); |
| 1159 | } |
| 1160 | |
| 1161 | /* |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 1162 | * Addition: R = P + Q, result's coordinates normalized |
Manuel Pégourié-Gonnard | 04a0225 | 2013-11-20 22:57:38 +0100 | [diff] [blame^] | 1163 | * Cost: 1A + 1N = 1I + 11M + 4S |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 1164 | */ |
| 1165 | int ecp_add( const ecp_group *grp, ecp_point *R, |
| 1166 | const ecp_point *P, const ecp_point *Q ) |
| 1167 | { |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 1168 | int ret; |
Manuel Pégourié-Gonnard | 989c32b | 2012-11-08 22:02:42 +0100 | [diff] [blame] | 1169 | |
Manuel Pégourié-Gonnard | 9674fd0 | 2012-11-19 21:23:27 +0100 | [diff] [blame] | 1170 | MPI_CHK( ecp_add_mixed( grp, R, P, Q , 1 ) ); |
| 1171 | MPI_CHK( ecp_normalize( grp, R ) ); |
| 1172 | |
| 1173 | cleanup: |
| 1174 | return( ret ); |
| 1175 | } |
| 1176 | |
| 1177 | /* |
| 1178 | * Subtraction: R = P - Q, result's coordinates normalized |
Manuel Pégourié-Gonnard | 04a0225 | 2013-11-20 22:57:38 +0100 | [diff] [blame^] | 1179 | * Cost: 1A + 1N = 1I + 11M + 4S |
Manuel Pégourié-Gonnard | 9674fd0 | 2012-11-19 21:23:27 +0100 | [diff] [blame] | 1180 | */ |
| 1181 | int ecp_sub( const ecp_group *grp, ecp_point *R, |
| 1182 | const ecp_point *P, const ecp_point *Q ) |
| 1183 | { |
| 1184 | int ret; |
| 1185 | |
| 1186 | MPI_CHK( ecp_add_mixed( grp, R, P, Q, -1 ) ); |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 1187 | MPI_CHK( ecp_normalize( grp, R ) ); |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 1188 | |
Manuel Pégourié-Gonnard | 989c32b | 2012-11-08 22:02:42 +0100 | [diff] [blame] | 1189 | cleanup: |
Manuel Pégourié-Gonnard | 7e0adfb | 2012-11-08 23:21:46 +0100 | [diff] [blame] | 1190 | return( ret ); |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 1191 | } |
| 1192 | |
Manuel Pégourié-Gonnard | efaa31e | 2012-11-06 21:34:35 +0100 | [diff] [blame] | 1193 | /* |
Manuel Pégourié-Gonnard | 8555607 | 2012-11-17 19:54:20 +0100 | [diff] [blame] | 1194 | * Compute a modified width-w non-adjacent form (NAF) of a number, |
Manuel Pégourié-Gonnard | 07de4b1 | 2013-09-02 16:26:04 +0200 | [diff] [blame] | 1195 | * with a fixed pattern for resistance to simple timing attacks (even SPA), |
| 1196 | * see [1]. (The resulting multiplication algorithm can also been seen as a |
| 1197 | * modification of 2^w-ary multiplication, with signed coefficients, all of |
| 1198 | * them odd.) |
Manuel Pégourié-Gonnard | 8555607 | 2012-11-17 19:54:20 +0100 | [diff] [blame] | 1199 | * |
| 1200 | * Input: |
| 1201 | * m must be an odd positive mpi less than w * k bits long |
| 1202 | * x must be an array of k elements |
| 1203 | * w must be less than a certain maximum (currently 8) |
| 1204 | * |
| 1205 | * The result is a sequence x[0], ..., x[k-1] with x[i] in the range |
| 1206 | * - 2^(width - 1) .. 2^(width - 1) - 1 such that |
| 1207 | * m = (2 * x[0] + 1) + 2^width * (2 * x[1] + 1) + ... |
| 1208 | * + 2^((k-1) * width) * (2 * x[k-1] + 1) |
| 1209 | * |
| 1210 | * Compared to "Algorithm SPA-resistant Width-w NAF with Odd Scalar" |
| 1211 | * p. 335 of the cited reference, here we return only u, not d_w since |
| 1212 | * it is known that the other d_w[j] will be 0. Moreover, the returned |
| 1213 | * string doesn't actually store u_i but x_i = u_i / 2 since it is known |
| 1214 | * that u_i is odd. Also, since we always select a positive value for d |
| 1215 | * mod 2^w, we don't need to check the sign of u[i-1] when the reference |
| 1216 | * does. Finally, there is an off-by-one error in the reference: the |
| 1217 | * last index should be k-1, not k. |
| 1218 | */ |
Manuel Pégourié-Gonnard | 7652a59 | 2012-11-21 10:00:45 +0100 | [diff] [blame] | 1219 | static int ecp_w_naf_fixed( signed char x[], size_t k, |
| 1220 | unsigned char w, const mpi *m ) |
Manuel Pégourié-Gonnard | 8555607 | 2012-11-17 19:54:20 +0100 | [diff] [blame] | 1221 | { |
| 1222 | int ret; |
| 1223 | unsigned int i, u, mask, carry; |
| 1224 | mpi M; |
| 1225 | |
| 1226 | mpi_init( &M ); |
| 1227 | |
| 1228 | MPI_CHK( mpi_copy( &M, m ) ); |
| 1229 | mask = ( 1 << w ) - 1; |
| 1230 | carry = 1 << ( w - 1 ); |
| 1231 | |
| 1232 | for( i = 0; i < k; i++ ) |
| 1233 | { |
| 1234 | u = M.p[0] & mask; |
| 1235 | |
| 1236 | if( ( u & 1 ) == 0 && i > 0 ) |
| 1237 | x[i - 1] -= carry; |
| 1238 | |
| 1239 | x[i] = u >> 1; |
| 1240 | mpi_shift_r( &M, w ); |
| 1241 | } |
| 1242 | |
| 1243 | /* |
Manuel Pégourié-Gonnard | 456d3b9 | 2013-09-16 18:04:38 +0200 | [diff] [blame] | 1244 | * We should have consumed all bits, unless the input value was too big |
Manuel Pégourié-Gonnard | 8555607 | 2012-11-17 19:54:20 +0100 | [diff] [blame] | 1245 | */ |
| 1246 | if( mpi_cmp_int( &M, 0 ) != 0 ) |
Manuel Pégourié-Gonnard | 456d3b9 | 2013-09-16 18:04:38 +0200 | [diff] [blame] | 1247 | ret = POLARSSL_ERR_ECP_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 8555607 | 2012-11-17 19:54:20 +0100 | [diff] [blame] | 1248 | |
| 1249 | cleanup: |
| 1250 | |
| 1251 | mpi_free( &M ); |
| 1252 | |
| 1253 | return( ret ); |
| 1254 | } |
| 1255 | |
| 1256 | /* |
Manuel Pégourié-Gonnard | 7652a59 | 2012-11-21 10:00:45 +0100 | [diff] [blame] | 1257 | * Precompute odd multiples of P up to (2 * t_len - 1) P. |
| 1258 | * The table is filled with T[i] = (2 * i + 1) P. |
| 1259 | */ |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 1260 | static int ecp_precompute( const ecp_group *grp, |
| 1261 | ecp_point T[], size_t t_len, |
| 1262 | const ecp_point *P ) |
Manuel Pégourié-Gonnard | 7652a59 | 2012-11-21 10:00:45 +0100 | [diff] [blame] | 1263 | { |
| 1264 | int ret; |
| 1265 | size_t i; |
| 1266 | ecp_point PP; |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1267 | ecp_point *TT[ 1 << ( POLARSSL_ECP_WINDOW_SIZE - 1 ) ]; |
Manuel Pégourié-Gonnard | 7652a59 | 2012-11-21 10:00:45 +0100 | [diff] [blame] | 1268 | |
| 1269 | ecp_point_init( &PP ); |
| 1270 | |
| 1271 | MPI_CHK( ecp_add( grp, &PP, P, P ) ); |
| 1272 | |
| 1273 | MPI_CHK( ecp_copy( &T[0], P ) ); |
| 1274 | |
Manuel Pégourié-Gonnard | 7652a59 | 2012-11-21 10:00:45 +0100 | [diff] [blame] | 1275 | for( i = 1; i < t_len; i++ ) |
Manuel Pégourié-Gonnard | cdd4432 | 2012-11-21 16:00:55 +0100 | [diff] [blame] | 1276 | MPI_CHK( ecp_add_mixed( grp, &T[i], &T[i-1], &PP, +1 ) ); |
| 1277 | |
| 1278 | /* |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1279 | * T[0] = P already has normalized coordinates, normalize others |
Manuel Pégourié-Gonnard | cdd4432 | 2012-11-21 16:00:55 +0100 | [diff] [blame] | 1280 | */ |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1281 | for( i = 1; i < t_len; i++ ) |
| 1282 | TT[i-1] = &T[i]; |
| 1283 | MPI_CHK( ecp_normalize_many( grp, TT, t_len - 1 ) ); |
Manuel Pégourié-Gonnard | 7652a59 | 2012-11-21 10:00:45 +0100 | [diff] [blame] | 1284 | |
| 1285 | cleanup: |
| 1286 | |
| 1287 | ecp_point_free( &PP ); |
| 1288 | |
| 1289 | return( ret ); |
| 1290 | } |
| 1291 | |
| 1292 | /* |
Manuel Pégourié-Gonnard | 07de4b1 | 2013-09-02 16:26:04 +0200 | [diff] [blame] | 1293 | * Randomize jacobian coordinates: |
| 1294 | * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l |
| 1295 | * This is sort of the reverse operation of ecp_normalize(). |
| 1296 | */ |
| 1297 | static int ecp_randomize_coordinates( const ecp_group *grp, ecp_point *pt, |
| 1298 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 1299 | { |
| 1300 | int ret; |
| 1301 | mpi l, ll; |
| 1302 | size_t p_size = (grp->pbits + 7) / 8; |
| 1303 | int count = 0; |
| 1304 | |
| 1305 | mpi_init( &l ); mpi_init( &ll ); |
| 1306 | |
| 1307 | /* Generate l such that 1 < l < p */ |
| 1308 | do |
| 1309 | { |
| 1310 | mpi_fill_random( &l, p_size, f_rng, p_rng ); |
| 1311 | |
| 1312 | while( mpi_cmp_mpi( &l, &grp->P ) >= 0 ) |
| 1313 | mpi_shift_r( &l, 1 ); |
| 1314 | |
| 1315 | if( count++ > 10 ) |
Manuel Pégourié-Gonnard | 456d3b9 | 2013-09-16 18:04:38 +0200 | [diff] [blame] | 1316 | return( POLARSSL_ERR_ECP_RANDOM_FAILED ); |
Manuel Pégourié-Gonnard | 07de4b1 | 2013-09-02 16:26:04 +0200 | [diff] [blame] | 1317 | } |
| 1318 | while( mpi_cmp_int( &l, 1 ) <= 0 ); |
| 1319 | |
| 1320 | /* Z = l * Z */ |
| 1321 | MPI_CHK( mpi_mul_mpi( &pt->Z, &pt->Z, &l ) ); MOD_MUL( pt->Z ); |
| 1322 | |
| 1323 | /* X = l^2 * X */ |
| 1324 | MPI_CHK( mpi_mul_mpi( &ll, &l, &l ) ); MOD_MUL( ll ); |
| 1325 | MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ll ) ); MOD_MUL( pt->X ); |
| 1326 | |
| 1327 | /* Y = l^3 * Y */ |
| 1328 | MPI_CHK( mpi_mul_mpi( &ll, &ll, &l ) ); MOD_MUL( ll ); |
| 1329 | MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ll ) ); MOD_MUL( pt->Y ); |
| 1330 | |
| 1331 | cleanup: |
| 1332 | mpi_free( &l ); mpi_free( &ll ); |
| 1333 | |
| 1334 | return( ret ); |
| 1335 | } |
| 1336 | |
| 1337 | /* |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 1338 | * Maximum length of the precomputed table |
| 1339 | */ |
| 1340 | #define MAX_PRE_LEN ( 1 << (POLARSSL_ECP_WINDOW_SIZE - 1) ) |
| 1341 | |
| 1342 | /* |
| 1343 | * Maximum length of the NAF: ceil( grp->nbits + 1 ) / w |
| 1344 | * (that is: grp->nbits / w + 1) |
| 1345 | * Allow p_bits + 1 bits in case M = grp->N + 1 is one bit longer than N. |
| 1346 | */ |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 1347 | #define MAX_NAF_LEN ( POLARSSL_ECP_MAX_BITS / 2 + 1 ) |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 1348 | |
| 1349 | /* |
| 1350 | * Integer multiplication: R = m * P |
| 1351 | * |
Manuel Pégourié-Gonnard | 07de4b1 | 2013-09-02 16:26:04 +0200 | [diff] [blame] | 1352 | * Based on fixed-pattern width-w NAF, see comments of ecp_w_naf_fixed(). |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 1353 | * |
| 1354 | * This function executes a fixed number of operations for |
| 1355 | * random m in the range 0 .. 2^nbits - 1. |
Manuel Pégourié-Gonnard | 07de4b1 | 2013-09-02 16:26:04 +0200 | [diff] [blame] | 1356 | * |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 1357 | * As an additional countermeasure against potential timing attacks, |
| 1358 | * we randomize coordinates before each addition. This was suggested as a |
Manuel Pégourié-Gonnard | 07de4b1 | 2013-09-02 16:26:04 +0200 | [diff] [blame] | 1359 | * countermeasure against DPA in 5.3 of [2] (with the obvious adaptation that |
| 1360 | * we use jacobian coordinates, not standard projective coordinates). |
Manuel Pégourié-Gonnard | efaa31e | 2012-11-06 21:34:35 +0100 | [diff] [blame] | 1361 | */ |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1362 | int ecp_mul_wnaf( ecp_group *grp, ecp_point *R, |
| 1363 | const mpi *m, const ecp_point *P, |
| 1364 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
Manuel Pégourié-Gonnard | efaa31e | 2012-11-06 21:34:35 +0100 | [diff] [blame] | 1365 | { |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 1366 | int ret; |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 1367 | unsigned char w, m_is_odd, p_eq_g; |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 1368 | size_t pre_len = 1, naf_len, i, j; |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 1369 | signed char naf[ MAX_NAF_LEN ]; |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 1370 | ecp_point Q, *T = NULL, S[2]; |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 1371 | mpi M; |
Manuel Pégourié-Gonnard | efaa31e | 2012-11-06 21:34:35 +0100 | [diff] [blame] | 1372 | |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 1373 | if( mpi_cmp_int( m, 0 ) < 0 || mpi_msb( m ) > grp->nbits ) |
Manuel Pégourié-Gonnard | e09d2f8 | 2013-09-02 14:29:09 +0200 | [diff] [blame] | 1374 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 4bdd47d | 2012-11-11 14:33:59 +0100 | [diff] [blame] | 1375 | |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 1376 | mpi_init( &M ); |
| 1377 | ecp_point_init( &Q ); |
| 1378 | ecp_point_init( &S[0] ); |
| 1379 | ecp_point_init( &S[1] ); |
| 1380 | |
| 1381 | /* |
| 1382 | * Check if P == G |
| 1383 | */ |
| 1384 | p_eq_g = ( mpi_cmp_int( &P->Z, 1 ) == 0 && |
| 1385 | mpi_cmp_mpi( &P->Y, &grp->G.Y ) == 0 && |
| 1386 | mpi_cmp_mpi( &P->X, &grp->G.X ) == 0 ); |
| 1387 | |
| 1388 | /* |
| 1389 | * If P == G, pre-compute a lot of points: this will be re-used later, |
| 1390 | * otherwise, choose window size depending on curve size |
| 1391 | */ |
| 1392 | if( p_eq_g ) |
| 1393 | w = POLARSSL_ECP_WINDOW_SIZE; |
| 1394 | else |
| 1395 | w = grp->nbits >= 512 ? 6 : |
| 1396 | grp->nbits >= 224 ? 5 : |
| 1397 | 4; |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 1398 | |
Manuel Pégourié-Gonnard | 3680c82 | 2012-11-21 18:49:45 +0100 | [diff] [blame] | 1399 | /* |
| 1400 | * Make sure w is within the limits. |
| 1401 | * The last test ensures that none of the precomputed points is zero, |
| 1402 | * which wouldn't be handled correctly by ecp_normalize_many(). |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 1403 | * It is only useful for very small curves as used in the test suite. |
Manuel Pégourié-Gonnard | 3680c82 | 2012-11-21 18:49:45 +0100 | [diff] [blame] | 1404 | */ |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 1405 | if( w > POLARSSL_ECP_WINDOW_SIZE ) |
| 1406 | w = POLARSSL_ECP_WINDOW_SIZE; |
Manuel Pégourié-Gonnard | 3680c82 | 2012-11-21 18:49:45 +0100 | [diff] [blame] | 1407 | if( w < 2 || w >= grp->nbits ) |
| 1408 | w = 2; |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 1409 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 1410 | pre_len <<= ( w - 1 ); |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 1411 | naf_len = grp->nbits / w + 1; |
| 1412 | |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 1413 | /* |
| 1414 | * Prepare precomputed points: if P == G we want to |
| 1415 | * use grp->T if already initialized, or initiliaze it. |
| 1416 | */ |
| 1417 | if( ! p_eq_g || grp->T == NULL ) |
| 1418 | { |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 1419 | T = (ecp_point *) polarssl_malloc( pre_len * sizeof( ecp_point ) ); |
| 1420 | if( T == NULL ) |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 1421 | { |
| 1422 | ret = POLARSSL_ERR_ECP_MALLOC_FAILED; |
| 1423 | goto cleanup; |
| 1424 | } |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 1425 | |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 1426 | for( i = 0; i < pre_len; i++ ) |
| 1427 | ecp_point_init( &T[i] ); |
| 1428 | |
| 1429 | MPI_CHK( ecp_precompute( grp, T, pre_len, P ) ); |
| 1430 | |
| 1431 | if( p_eq_g ) |
| 1432 | { |
| 1433 | grp->T = T; |
| 1434 | grp->T_size = pre_len; |
| 1435 | } |
| 1436 | } |
| 1437 | else |
| 1438 | { |
| 1439 | T = grp->T; |
| 1440 | |
| 1441 | /* Should never happen, but we want to be extra sure */ |
| 1442 | if( pre_len != grp->T_size ) |
| 1443 | { |
| 1444 | ret = POLARSSL_ERR_ECP_BAD_INPUT_DATA; |
| 1445 | goto cleanup; |
| 1446 | } |
| 1447 | } |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 1448 | |
Manuel Pégourié-Gonnard | efaa31e | 2012-11-06 21:34:35 +0100 | [diff] [blame] | 1449 | /* |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 1450 | * Make sure M is odd (M = m + 1 or M = m + 2) |
| 1451 | * later we'll get m * P by subtracting P or 2 * P to M * P. |
Manuel Pégourié-Gonnard | efaa31e | 2012-11-06 21:34:35 +0100 | [diff] [blame] | 1452 | */ |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 1453 | m_is_odd = ( mpi_get_bit( m, 0 ) == 1 ); |
| 1454 | |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 1455 | MPI_CHK( mpi_copy( &M, m ) ); |
| 1456 | MPI_CHK( mpi_add_int( &M, &M, 1 + m_is_odd ) ); |
Manuel Pégourié-Gonnard | efaa31e | 2012-11-06 21:34:35 +0100 | [diff] [blame] | 1457 | |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 1458 | /* |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 1459 | * Compute the fixed-pattern NAF of M |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 1460 | */ |
| 1461 | MPI_CHK( ecp_w_naf_fixed( naf, naf_len, w, &M ) ); |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 1462 | |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 1463 | /* |
| 1464 | * Compute M * P, using a variant of left-to-right 2^w-ary multiplication: |
| 1465 | * at each step we add (2 * naf[i] + 1) P, then multiply by 2^w. |
| 1466 | * |
| 1467 | * If naf[i] >= 0, we have (2 * naf[i] + 1) P == T[ naf[i] ] |
| 1468 | * Otherwise, (2 * naf[i] + 1) P == - ( 2 * ( - naf[i] - 1 ) + 1) P |
| 1469 | * == T[ - naf[i] - 1 ] |
| 1470 | */ |
| 1471 | MPI_CHK( ecp_set_zero( &Q ) ); |
| 1472 | i = naf_len - 1; |
| 1473 | while( 1 ) |
Manuel Pégourié-Gonnard | efaa31e | 2012-11-06 21:34:35 +0100 | [diff] [blame] | 1474 | { |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 1475 | /* Countermeasure (see comments above) */ |
| 1476 | if( f_rng != NULL ) |
| 1477 | ecp_randomize_coordinates( grp, &Q, f_rng, p_rng ); |
| 1478 | |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 1479 | if( naf[i] < 0 ) |
| 1480 | { |
| 1481 | MPI_CHK( ecp_add_mixed( grp, &Q, &Q, &T[ - naf[i] - 1 ], -1 ) ); |
| 1482 | } |
| 1483 | else |
| 1484 | { |
| 1485 | MPI_CHK( ecp_add_mixed( grp, &Q, &Q, &T[ naf[i] ], +1 ) ); |
| 1486 | } |
Manuel Pégourié-Gonnard | efaa31e | 2012-11-06 21:34:35 +0100 | [diff] [blame] | 1487 | |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 1488 | if( i == 0 ) |
Manuel Pégourié-Gonnard | efaa31e | 2012-11-06 21:34:35 +0100 | [diff] [blame] | 1489 | break; |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 1490 | i--; |
| 1491 | |
| 1492 | for( j = 0; j < w; j++ ) |
| 1493 | { |
| 1494 | MPI_CHK( ecp_double_jac( grp, &Q, &Q ) ); |
| 1495 | } |
Manuel Pégourié-Gonnard | efaa31e | 2012-11-06 21:34:35 +0100 | [diff] [blame] | 1496 | } |
| 1497 | |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 1498 | /* |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 1499 | * Now get m * P from M * P |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 1500 | */ |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 1501 | MPI_CHK( ecp_copy( &S[0], P ) ); |
| 1502 | MPI_CHK( ecp_add( grp, &S[1], P, P ) ); |
| 1503 | MPI_CHK( ecp_sub( grp, R, &Q, &S[m_is_odd] ) ); |
Manuel Pégourié-Gonnard | efaa31e | 2012-11-06 21:34:35 +0100 | [diff] [blame] | 1504 | |
Manuel Pégourié-Gonnard | 3680c82 | 2012-11-21 18:49:45 +0100 | [diff] [blame] | 1505 | |
Manuel Pégourié-Gonnard | efaa31e | 2012-11-06 21:34:35 +0100 | [diff] [blame] | 1506 | cleanup: |
| 1507 | |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 1508 | if( T != NULL && ! p_eq_g ) |
| 1509 | { |
| 1510 | for( i = 0; i < pre_len; i++ ) |
| 1511 | ecp_point_free( &T[i] ); |
| 1512 | polarssl_free( T ); |
| 1513 | } |
| 1514 | |
| 1515 | ecp_point_free( &S[1] ); |
| 1516 | ecp_point_free( &S[0] ); |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 1517 | ecp_point_free( &Q ); |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 1518 | mpi_free( &M ); |
Manuel Pégourié-Gonnard | efaa31e | 2012-11-06 21:34:35 +0100 | [diff] [blame] | 1519 | |
| 1520 | return( ret ); |
| 1521 | } |
| 1522 | |
Manuel Pégourié-Gonnard | 45a035a | 2013-01-26 14:42:45 +0100 | [diff] [blame] | 1523 | /* |
Manuel Pégourié-Gonnard | c30200e | 2013-11-20 18:39:55 +0100 | [diff] [blame] | 1524 | * Check and define parameters used by the comb method (see below for details) |
| 1525 | */ |
| 1526 | #if POLARSSL_ECP_WINDOW_SIZE < 2 || POLARSSL_ECP_WINDOW_SIZE > 7 |
| 1527 | #error "POLARSSL_ECP_WINDOW_SIZE out of bounds" |
| 1528 | #endif |
| 1529 | |
| 1530 | /* d = ceil( n / w ) */ |
| 1531 | #define COMB_MAX_D ( POLARSSL_ECP_MAX_BITS + 1 ) / 2 |
| 1532 | |
| 1533 | /* number of precomputed points */ |
| 1534 | #define COMB_MAX_PRE ( 1 << ( POLARSSL_ECP_WINDOW_SIZE - 1 ) ) |
| 1535 | |
| 1536 | /* |
| 1537 | * Compute the representation of m that will be used with our comb method. |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1538 | * |
| 1539 | * The basic comb method is described in GECC 3.44 for example. We use a |
Manuel Pégourié-Gonnard | 101a39f | 2013-11-20 14:47:19 +0100 | [diff] [blame] | 1540 | * modified version that provides resistance to SPA by avoiding zero |
| 1541 | * digits in the representation as in [3]. We modify the method further by |
| 1542 | * requiring that all K_i be odd, which has the small cost that our |
Manuel Pégourié-Gonnard | c30200e | 2013-11-20 18:39:55 +0100 | [diff] [blame] | 1543 | * representation uses one more K_i, due to carries. |
Manuel Pégourié-Gonnard | 101a39f | 2013-11-20 14:47:19 +0100 | [diff] [blame] | 1544 | * |
| 1545 | * Also, for the sake of compactness, only the seven low-order bits of x[i] |
| 1546 | * are used to represent K_i, and the msb of x[i] encodes the the sign (s_i in |
| 1547 | * the paper): it is set if and only if if s_i == -1; |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1548 | * |
| 1549 | * Calling conventions: |
Manuel Pégourié-Gonnard | 101a39f | 2013-11-20 14:47:19 +0100 | [diff] [blame] | 1550 | * - x is an array of size d + 1 |
Manuel Pégourié-Gonnard | c30200e | 2013-11-20 18:39:55 +0100 | [diff] [blame] | 1551 | * - w is the size, ie number of teeth, of the comb, and must be between |
| 1552 | * 2 and 7 (in practice, between 2 and POLARSSL_ECP_WINDOW_SIZE) |
Manuel Pégourié-Gonnard | 101a39f | 2013-11-20 14:47:19 +0100 | [diff] [blame] | 1553 | * - m is the MPI, expected to be odd and such that bitlength(m) <= w * d |
| 1554 | * (the result will be incorrect if these assumptions are not satisfied) |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1555 | */ |
Manuel Pégourié-Gonnard | 101a39f | 2013-11-20 14:47:19 +0100 | [diff] [blame] | 1556 | static void ecp_comb_fixed( unsigned char x[], size_t d, |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1557 | unsigned char w, const mpi *m ) |
| 1558 | { |
| 1559 | size_t i, j; |
Manuel Pégourié-Gonnard | 101a39f | 2013-11-20 14:47:19 +0100 | [diff] [blame] | 1560 | unsigned char c, cc, adjust; |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1561 | |
Manuel Pégourié-Gonnard | 101a39f | 2013-11-20 14:47:19 +0100 | [diff] [blame] | 1562 | memset( x, 0, d+1 ); |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1563 | |
| 1564 | /* For x[0] use the classical comb value without adjustement */ |
| 1565 | for( j = 0; j < w; j++ ) |
| 1566 | x[0] |= mpi_get_bit( m, d * j ) << j; |
Manuel Pégourié-Gonnard | 101a39f | 2013-11-20 14:47:19 +0100 | [diff] [blame] | 1567 | c = 0; |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1568 | |
| 1569 | for( i = 1; i < d; i++ ) |
| 1570 | { |
| 1571 | /* Get the classical comb value */ |
| 1572 | for( j = 0; j < w; j++ ) |
| 1573 | x[i] |= mpi_get_bit( m, i + d * j ) << j; |
| 1574 | |
Manuel Pégourié-Gonnard | 101a39f | 2013-11-20 14:47:19 +0100 | [diff] [blame] | 1575 | /* Add carry and update it */ |
| 1576 | cc = x[i] & c; |
| 1577 | x[i] = x[i] ^ c; |
| 1578 | c = cc; |
| 1579 | |
| 1580 | /* Make sure x[i] is odd, avoiding if-branches */ |
| 1581 | adjust = 1 - ( x[i] & 0x01 ); |
| 1582 | c |= x[i] & ( x[i-1] * adjust ); |
| 1583 | x[i] = x[i] ^ ( x[i-1] * adjust ); |
| 1584 | x[i-1] |= adjust << 7; |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1585 | } |
Manuel Pégourié-Gonnard | 101a39f | 2013-11-20 14:47:19 +0100 | [diff] [blame] | 1586 | |
| 1587 | /* Finish with the carry */ |
| 1588 | x[i] = c; |
| 1589 | adjust = 1 - ( x[i] & 0x01 ); |
| 1590 | c |= x[i] & ( x[i-1] * adjust ); |
| 1591 | x[i] = x[i] ^ ( x[i-1] * adjust ); |
| 1592 | x[i-1] |= adjust << 7; |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1593 | } |
| 1594 | |
| 1595 | /* |
| 1596 | * Precompute points for the comb method |
| 1597 | * |
Manuel Pégourié-Gonnard | 101a39f | 2013-11-20 14:47:19 +0100 | [diff] [blame] | 1598 | * If i = i_{w-1} ... i_1 is the binary representation of i, then |
| 1599 | * T[i] = i_{w-1} 2^{(w-1)d} P + ... + i_1 2^d P + P |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1600 | * |
Manuel Pégourié-Gonnard | 04a0225 | 2013-11-20 22:57:38 +0100 | [diff] [blame^] | 1601 | * T must be able to hold 2^{w - 1} elements |
| 1602 | * |
| 1603 | * Cost: d(w-1) D + (2^{w-1} - 1) A + 1 N(w-1) + 1 N(2^{w-1} - 1) |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1604 | */ |
| 1605 | static int ecp_precompute_comb( const ecp_group *grp, |
| 1606 | ecp_point T[], const ecp_point *P, |
| 1607 | unsigned char w, size_t d ) |
| 1608 | { |
| 1609 | int ret; |
Manuel Pégourié-Gonnard | c30200e | 2013-11-20 18:39:55 +0100 | [diff] [blame] | 1610 | unsigned char i, k; |
| 1611 | size_t j; |
| 1612 | ecp_point *cur, *TT[COMB_MAX_PRE - 1]; |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1613 | |
| 1614 | /* |
Manuel Pégourié-Gonnard | 101a39f | 2013-11-20 14:47:19 +0100 | [diff] [blame] | 1615 | * Set T[0] = P and |
| 1616 | * T[2^{l-1}] = 2^{dl} P for l = 1 .. w-1 (this is not the final value) |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1617 | */ |
| 1618 | MPI_CHK( ecp_copy( &T[0], P ) ); |
| 1619 | |
Manuel Pégourié-Gonnard | 101a39f | 2013-11-20 14:47:19 +0100 | [diff] [blame] | 1620 | k = 0; |
| 1621 | for( i = 1; i < ( 1U << (w-1) ); i <<= 1 ) |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1622 | { |
Manuel Pégourié-Gonnard | 101a39f | 2013-11-20 14:47:19 +0100 | [diff] [blame] | 1623 | cur = T + i; |
| 1624 | MPI_CHK( ecp_copy( cur, T + ( i >> 1 ) ) ); |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1625 | for( j = 0; j < d; j++ ) |
| 1626 | MPI_CHK( ecp_double_jac( grp, cur, cur ) ); |
Manuel Pégourié-Gonnard | 101a39f | 2013-11-20 14:47:19 +0100 | [diff] [blame] | 1627 | |
| 1628 | TT[k++] = cur; |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1629 | } |
| 1630 | |
Manuel Pégourié-Gonnard | 101a39f | 2013-11-20 14:47:19 +0100 | [diff] [blame] | 1631 | ecp_normalize_many( grp, TT, k ); |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1632 | |
| 1633 | /* |
| 1634 | * Compute the remaining ones using the minimal number of additions |
Manuel Pégourié-Gonnard | 101a39f | 2013-11-20 14:47:19 +0100 | [diff] [blame] | 1635 | * Be careful to update T[2^l] only after using it! |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1636 | */ |
Manuel Pégourié-Gonnard | 101a39f | 2013-11-20 14:47:19 +0100 | [diff] [blame] | 1637 | k = 0; |
| 1638 | for( i = 1; i < ( 1U << (w-1) ); i <<= 1 ) |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1639 | { |
Manuel Pégourié-Gonnard | 101a39f | 2013-11-20 14:47:19 +0100 | [diff] [blame] | 1640 | j = i; |
| 1641 | while( j-- ) |
| 1642 | { |
| 1643 | ecp_add_mixed( grp, &T[i + j], &T[j], &T[i], +1 ); |
| 1644 | TT[k++] = &T[i + j]; |
| 1645 | } |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1646 | } |
| 1647 | |
Manuel Pégourié-Gonnard | 101a39f | 2013-11-20 14:47:19 +0100 | [diff] [blame] | 1648 | ecp_normalize_many( grp, TT, k ); |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1649 | |
| 1650 | cleanup: |
| 1651 | return( ret ); |
| 1652 | } |
| 1653 | |
| 1654 | /* |
Manuel Pégourié-Gonnard | 101a39f | 2013-11-20 14:47:19 +0100 | [diff] [blame] | 1655 | * Select precomputed point: R = sign(i) * T[ abs(i) / 2 ] |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1656 | */ |
| 1657 | static int ecp_select_comb( const ecp_group *grp, ecp_point *R, |
Manuel Pégourié-Gonnard | 101a39f | 2013-11-20 14:47:19 +0100 | [diff] [blame] | 1658 | const ecp_point T[], unsigned char i ) |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1659 | { |
| 1660 | int ret; |
| 1661 | |
Manuel Pégourié-Gonnard | 101a39f | 2013-11-20 14:47:19 +0100 | [diff] [blame] | 1662 | /* Ignore the "sign" bit */ |
| 1663 | MPI_CHK( ecp_copy( R, &T[ ( i & 0x7Fu ) >> 1 ] ) ); |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1664 | |
| 1665 | /* |
| 1666 | * -R = (R.X, -R.Y, R.Z), and |
| 1667 | * -R.Y mod P = P - R.Y unless R.Y == 0 |
| 1668 | */ |
Manuel Pégourié-Gonnard | 101a39f | 2013-11-20 14:47:19 +0100 | [diff] [blame] | 1669 | if( ( i & 0x80 ) != 0 ) |
| 1670 | if( mpi_cmp_int( &R->Y, 0 ) != 0 ) |
| 1671 | MPI_CHK( mpi_sub_mpi( &R->Y, &grp->P, &R->Y ) ); |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1672 | |
| 1673 | cleanup: |
| 1674 | return( ret ); |
| 1675 | } |
| 1676 | |
| 1677 | /* |
| 1678 | * Core multiplication algorithm for the (modified) comb method. |
| 1679 | * This part is actually common with the basic comb method (GECC 3.44) |
Manuel Pégourié-Gonnard | 04a0225 | 2013-11-20 22:57:38 +0100 | [diff] [blame^] | 1680 | * |
| 1681 | * Cost: d A + d D + 1 R |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1682 | */ |
| 1683 | static int ecp_mul_comb_core( const ecp_group *grp, ecp_point *R, |
Manuel Pégourié-Gonnard | 70c1437 | 2013-11-20 20:07:26 +0100 | [diff] [blame] | 1684 | const ecp_point T[], |
| 1685 | const unsigned char x[], size_t d, |
| 1686 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1687 | void *p_rng ) |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1688 | { |
| 1689 | int ret; |
| 1690 | ecp_point Txi; |
| 1691 | size_t i; |
| 1692 | |
| 1693 | ecp_point_init( &Txi ); |
| 1694 | |
Manuel Pégourié-Gonnard | 70c1437 | 2013-11-20 20:07:26 +0100 | [diff] [blame] | 1695 | /* Start with a non-zero point and randomize its coordinates */ |
Manuel Pégourié-Gonnard | 101a39f | 2013-11-20 14:47:19 +0100 | [diff] [blame] | 1696 | i = d; |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1697 | MPI_CHK( ecp_select_comb( grp, R, T, x[i] ) ); |
Manuel Pégourié-Gonnard | 70c1437 | 2013-11-20 20:07:26 +0100 | [diff] [blame] | 1698 | if( f_rng != 0 ) |
| 1699 | MPI_CHK( ecp_randomize_coordinates( grp, R, f_rng, p_rng ) ); |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1700 | |
| 1701 | while( i-- != 0 ) |
| 1702 | { |
| 1703 | MPI_CHK( ecp_double_jac( grp, R, R ) ); |
| 1704 | MPI_CHK( ecp_select_comb( grp, &Txi, T, x[i] ) ); |
| 1705 | MPI_CHK( ecp_add_mixed( grp, R, R, &Txi, +1 ) ); |
| 1706 | } |
| 1707 | |
| 1708 | cleanup: |
| 1709 | ecp_point_free( &Txi ); |
| 1710 | |
| 1711 | return( ret ); |
| 1712 | } |
| 1713 | |
| 1714 | /* |
Manuel Pégourié-Gonnard | 04a0225 | 2013-11-20 22:57:38 +0100 | [diff] [blame^] | 1715 | * Multiplication using the comb method |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1716 | */ |
| 1717 | int ecp_mul_comb( ecp_group *grp, ecp_point *R, |
| 1718 | const mpi *m, const ecp_point *P, |
| 1719 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 1720 | { |
| 1721 | int ret; |
| 1722 | unsigned char w, m_is_odd, p_eq_g; |
| 1723 | size_t pre_len, d, i; |
Manuel Pégourié-Gonnard | c30200e | 2013-11-20 18:39:55 +0100 | [diff] [blame] | 1724 | unsigned char k[COMB_MAX_D + 1]; |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1725 | ecp_point Q, *T = NULL, S[2]; |
| 1726 | mpi M; |
| 1727 | |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1728 | if( mpi_cmp_int( m, 0 ) < 0 || mpi_msb( m ) > grp->nbits ) |
| 1729 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
| 1730 | |
| 1731 | mpi_init( &M ); |
| 1732 | ecp_point_init( &Q ); |
| 1733 | ecp_point_init( &S[0] ); |
| 1734 | ecp_point_init( &S[1] ); |
| 1735 | |
| 1736 | /* |
| 1737 | * Check if P == G |
| 1738 | */ |
| 1739 | p_eq_g = ( mpi_cmp_int( &P->Z, 1 ) == 0 && |
| 1740 | mpi_cmp_mpi( &P->Y, &grp->G.Y ) == 0 && |
| 1741 | mpi_cmp_mpi( &P->X, &grp->G.X ) == 0 ); |
| 1742 | |
Manuel Pégourié-Gonnard | 04a0225 | 2013-11-20 22:57:38 +0100 | [diff] [blame^] | 1743 | /* |
| 1744 | * Minimize the number of multiplications, that is minimize |
| 1745 | * 10 * d * w + 18 * 2^(w-1) + 11 * d + 7 * w |
| 1746 | * (see costs of the various parts, with 1S = 1M) |
| 1747 | */ |
| 1748 | w = grp->nbits >= 384 ? 5 : 4; |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1749 | |
Manuel Pégourié-Gonnard | 04a0225 | 2013-11-20 22:57:38 +0100 | [diff] [blame^] | 1750 | /* |
| 1751 | * If P == G, pre-compute a bit more, since this may be re-used later. |
| 1752 | * Just adding one ups the cost of the first mul by at most 3%. |
| 1753 | */ |
| 1754 | if( p_eq_g ) |
| 1755 | w++; |
| 1756 | |
| 1757 | /* |
| 1758 | * Make sure w is within limits. |
| 1759 | * (The last test is useful only for very small curves in the test suite.) |
| 1760 | */ |
| 1761 | if( w > POLARSSL_ECP_WINDOW_SIZE ) |
| 1762 | w = POLARSSL_ECP_WINDOW_SIZE; |
| 1763 | if( w < 2 || w >= grp->nbits ) |
| 1764 | w = 2; |
| 1765 | |
| 1766 | /* Other sizes that depend on w */ |
Manuel Pégourié-Gonnard | c30200e | 2013-11-20 18:39:55 +0100 | [diff] [blame] | 1767 | pre_len = 1U << ( w - 1 ); |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1768 | d = ( grp->nbits + w - 1 ) / w; |
| 1769 | |
| 1770 | /* |
| 1771 | * Prepare precomputed points: if P == G we want to |
| 1772 | * use grp->T if already initialized, or initiliaze it. |
| 1773 | */ |
| 1774 | if( ! p_eq_g || grp->T == NULL ) |
| 1775 | { |
| 1776 | T = (ecp_point *) polarssl_malloc( pre_len * sizeof( ecp_point ) ); |
| 1777 | if( T == NULL ) |
| 1778 | { |
| 1779 | ret = POLARSSL_ERR_ECP_MALLOC_FAILED; |
| 1780 | goto cleanup; |
| 1781 | } |
| 1782 | |
| 1783 | for( i = 0; i < pre_len; i++ ) |
| 1784 | ecp_point_init( &T[i] ); |
| 1785 | |
| 1786 | MPI_CHK( ecp_precompute_comb( grp, T, P, w, d ) ); |
| 1787 | |
| 1788 | if( p_eq_g ) |
| 1789 | { |
| 1790 | grp->T = T; |
| 1791 | grp->T_size = pre_len; |
| 1792 | } |
| 1793 | } |
| 1794 | else |
| 1795 | { |
| 1796 | T = grp->T; |
| 1797 | |
| 1798 | /* Should never happen, but we want to be extra sure */ |
| 1799 | if( pre_len != grp->T_size ) |
| 1800 | { |
| 1801 | ret = POLARSSL_ERR_ECP_BAD_INPUT_DATA; |
| 1802 | goto cleanup; |
| 1803 | } |
| 1804 | } |
| 1805 | |
| 1806 | /* |
| 1807 | * Make sure M is odd (M = m + 1 or M = m + 2) |
| 1808 | * later we'll get m * P by subtracting P or 2 * P to M * P. |
| 1809 | */ |
| 1810 | m_is_odd = ( mpi_get_bit( m, 0 ) == 1 ); |
| 1811 | |
| 1812 | MPI_CHK( mpi_copy( &M, m ) ); |
| 1813 | MPI_CHK( mpi_add_int( &M, &M, 1 + m_is_odd ) ); |
| 1814 | |
| 1815 | /* |
| 1816 | * Go for comb multiplication, Q = M * P |
| 1817 | */ |
| 1818 | ecp_comb_fixed( k, d, w, &M ); |
Manuel Pégourié-Gonnard | 70c1437 | 2013-11-20 20:07:26 +0100 | [diff] [blame] | 1819 | ecp_mul_comb_core( grp, &Q, T, k, d, f_rng, p_rng ); |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1820 | |
| 1821 | /* |
| 1822 | * Now get m * P from M * P |
| 1823 | */ |
| 1824 | MPI_CHK( ecp_copy( &S[0], P ) ); |
| 1825 | MPI_CHK( ecp_add( grp, &S[1], P, P ) ); |
| 1826 | MPI_CHK( ecp_sub( grp, R, &Q, &S[m_is_odd] ) ); |
| 1827 | |
| 1828 | cleanup: |
| 1829 | |
| 1830 | if( T != NULL && ! p_eq_g ) |
| 1831 | { |
| 1832 | for( i = 0; i < pre_len; i++ ) |
| 1833 | ecp_point_free( &T[i] ); |
| 1834 | polarssl_free( T ); |
| 1835 | } |
| 1836 | |
| 1837 | ecp_point_free( &S[1] ); |
| 1838 | ecp_point_free( &S[0] ); |
| 1839 | ecp_point_free( &Q ); |
| 1840 | mpi_free( &M ); |
| 1841 | |
| 1842 | return( ret ); |
| 1843 | } |
| 1844 | |
| 1845 | /* |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 1846 | * Check that a point is valid as a public key (SEC1 3.2.3.1) |
| 1847 | */ |
| 1848 | int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt ) |
| 1849 | { |
| 1850 | int ret; |
| 1851 | mpi YY, RHS; |
| 1852 | |
| 1853 | if( mpi_cmp_int( &pt->Z, 0 ) == 0 ) |
Manuel Pégourié-Gonnard | 456d3b9 | 2013-09-16 18:04:38 +0200 | [diff] [blame] | 1854 | return( POLARSSL_ERR_ECP_INVALID_KEY ); |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 1855 | |
| 1856 | /* |
| 1857 | * pt coordinates must be normalized for our checks |
| 1858 | */ |
| 1859 | if( mpi_cmp_int( &pt->Z, 1 ) != 0 ) |
Manuel Pégourié-Gonnard | 456d3b9 | 2013-09-16 18:04:38 +0200 | [diff] [blame] | 1860 | return( POLARSSL_ERR_ECP_INVALID_KEY ); |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 1861 | |
| 1862 | if( mpi_cmp_int( &pt->X, 0 ) < 0 || |
| 1863 | mpi_cmp_int( &pt->Y, 0 ) < 0 || |
| 1864 | mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 || |
| 1865 | mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 ) |
Manuel Pégourié-Gonnard | 456d3b9 | 2013-09-16 18:04:38 +0200 | [diff] [blame] | 1866 | return( POLARSSL_ERR_ECP_INVALID_KEY ); |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 1867 | |
| 1868 | mpi_init( &YY ); mpi_init( &RHS ); |
| 1869 | |
| 1870 | /* |
| 1871 | * YY = Y^2 |
Manuel Pégourié-Gonnard | cd7458a | 2013-10-08 13:11:30 +0200 | [diff] [blame] | 1872 | * RHS = X (X^2 + A) + B = X^3 + A X + B |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 1873 | */ |
Manuel Pégourié-Gonnard | cd7458a | 2013-10-08 13:11:30 +0200 | [diff] [blame] | 1874 | MPI_CHK( mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY ); |
| 1875 | MPI_CHK( mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS ); |
Manuel Pégourié-Gonnard | 0cd6f98 | 2013-10-10 15:55:39 +0200 | [diff] [blame] | 1876 | MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->A ) ); MOD_ADD( RHS ); |
Manuel Pégourié-Gonnard | cd7458a | 2013-10-08 13:11:30 +0200 | [diff] [blame] | 1877 | MPI_CHK( mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS ); |
| 1878 | MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS ); |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 1879 | |
| 1880 | if( mpi_cmp_mpi( &YY, &RHS ) != 0 ) |
Manuel Pégourié-Gonnard | 456d3b9 | 2013-09-16 18:04:38 +0200 | [diff] [blame] | 1881 | ret = POLARSSL_ERR_ECP_INVALID_KEY; |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 1882 | |
| 1883 | cleanup: |
| 1884 | |
| 1885 | mpi_free( &YY ); mpi_free( &RHS ); |
| 1886 | |
| 1887 | return( ret ); |
| 1888 | } |
| 1889 | |
| 1890 | /* |
| 1891 | * Check that an mpi is valid as a private key (SEC1 3.2) |
| 1892 | */ |
Manuel Pégourié-Gonnard | de44a4a | 2013-07-09 16:05:52 +0200 | [diff] [blame] | 1893 | int ecp_check_privkey( const ecp_group *grp, const mpi *d ) |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 1894 | { |
| 1895 | /* We want 1 <= d <= N-1 */ |
| 1896 | if ( mpi_cmp_int( d, 1 ) < 0 || mpi_cmp_mpi( d, &grp->N ) >= 0 ) |
Manuel Pégourié-Gonnard | 456d3b9 | 2013-09-16 18:04:38 +0200 | [diff] [blame] | 1897 | return( POLARSSL_ERR_ECP_INVALID_KEY ); |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 1898 | |
| 1899 | return( 0 ); |
| 1900 | } |
| 1901 | |
| 1902 | /* |
Manuel Pégourié-Gonnard | 45a035a | 2013-01-26 14:42:45 +0100 | [diff] [blame] | 1903 | * Generate a keypair (SEC1 3.2.1) |
| 1904 | */ |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 1905 | int ecp_gen_keypair( ecp_group *grp, mpi *d, ecp_point *Q, |
Manuel Pégourié-Gonnard | 45a035a | 2013-01-26 14:42:45 +0100 | [diff] [blame] | 1906 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1907 | void *p_rng ) |
| 1908 | { |
| 1909 | int count = 0; |
| 1910 | size_t n_size = (grp->nbits + 7) / 8; |
| 1911 | |
| 1912 | /* |
| 1913 | * Generate d such that 1 <= n < N |
| 1914 | */ |
| 1915 | do |
| 1916 | { |
| 1917 | mpi_fill_random( d, n_size, f_rng, p_rng ); |
| 1918 | |
| 1919 | while( mpi_cmp_mpi( d, &grp->N ) >= 0 ) |
| 1920 | mpi_shift_r( d, 1 ); |
| 1921 | |
| 1922 | if( count++ > 10 ) |
Manuel Pégourié-Gonnard | 456d3b9 | 2013-09-16 18:04:38 +0200 | [diff] [blame] | 1923 | return( POLARSSL_ERR_ECP_RANDOM_FAILED ); |
Manuel Pégourié-Gonnard | 45a035a | 2013-01-26 14:42:45 +0100 | [diff] [blame] | 1924 | } |
| 1925 | while( mpi_cmp_int( d, 1 ) < 0 ); |
| 1926 | |
Manuel Pégourié-Gonnard | e09d2f8 | 2013-09-02 14:29:09 +0200 | [diff] [blame] | 1927 | return( ecp_mul( grp, Q, d, &grp->G, f_rng, p_rng ) ); |
Manuel Pégourié-Gonnard | 45a035a | 2013-01-26 14:42:45 +0100 | [diff] [blame] | 1928 | } |
Manuel Pégourié-Gonnard | efaa31e | 2012-11-06 21:34:35 +0100 | [diff] [blame] | 1929 | |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 1930 | #if defined(POLARSSL_ECP_NIST_OPTIM) |
Manuel Pégourié-Gonnard | 9fcceac | 2013-10-23 20:56:12 +0200 | [diff] [blame] | 1931 | /* |
| 1932 | * Fast reduction modulo the primes used by the NIST curves. |
| 1933 | * |
| 1934 | * These functions are: critical for speed, but not need for correct |
| 1935 | * operations. So, we make the choice to heavily rely on the internals of our |
| 1936 | * bignum library, which creates a tight coupling between these functions and |
| 1937 | * our MPI implementation. However, the coupling between the ECP module and |
| 1938 | * MPI remains loose, since these functions can be deactivated at will. |
| 1939 | */ |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 1940 | |
| 1941 | #if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED) |
| 1942 | /* |
| 1943 | * Compared to the way things are presented in FIPS 186-3 D.2, |
| 1944 | * we proceed in columns, from right (least significant chunk) to left, |
| 1945 | * adding chunks to N in place, and keeping a carry for the next chunk. |
| 1946 | * This avoids moving things around in memory, and uselessly adding zeros, |
| 1947 | * compared to the more straightforward, line-oriented approach. |
| 1948 | * |
| 1949 | * For this prime we need to handle data in chunks of 64 bits. |
| 1950 | * Since this is always a multiple of our basic t_uint, we can |
| 1951 | * use a t_uint * to designate such a chunk, and small loops to handle it. |
| 1952 | */ |
| 1953 | |
| 1954 | /* Add 64-bit chunks (dst += src) and update carry */ |
| 1955 | static inline void add64( t_uint *dst, t_uint *src, t_uint *carry ) |
| 1956 | { |
| 1957 | unsigned char i; |
| 1958 | t_uint c = 0; |
| 1959 | for( i = 0; i < 8 / sizeof( t_uint ); i++, dst++, src++ ) |
| 1960 | { |
| 1961 | *dst += c; c = ( *dst < c ); |
| 1962 | *dst += *src; c += ( *dst < *src ); |
| 1963 | } |
| 1964 | *carry += c; |
| 1965 | } |
| 1966 | |
| 1967 | /* Add carry to a 64-bit chunk and update carry */ |
| 1968 | static inline void carry64( t_uint *dst, t_uint *carry ) |
| 1969 | { |
| 1970 | unsigned char i; |
| 1971 | for( i = 0; i < 8 / sizeof( t_uint ); i++, dst++ ) |
| 1972 | { |
| 1973 | *dst += *carry; |
| 1974 | *carry = ( *dst < *carry ); |
| 1975 | } |
| 1976 | } |
| 1977 | |
| 1978 | #define WIDTH 8 / sizeof( t_uint ) |
| 1979 | #define A( i ) N->p + i * WIDTH |
| 1980 | #define ADD( i ) add64( p, A( i ), &c ) |
| 1981 | #define NEXT p += WIDTH; carry64( p, &c ) |
| 1982 | #define LAST p += WIDTH; *p = c; while( ++p < end ) *p = 0 |
| 1983 | |
| 1984 | /* |
| 1985 | * Fast quasi-reduction modulo p192 (FIPS 186-3 D.2.1) |
| 1986 | */ |
| 1987 | static int ecp_mod_p192( mpi *N ) |
| 1988 | { |
| 1989 | int ret; |
| 1990 | t_uint c = 0; |
| 1991 | t_uint *p, *end; |
| 1992 | |
| 1993 | /* Make sure we have enough blocks so that A(5) is legal */ |
| 1994 | MPI_CHK( mpi_grow( N, 6 * WIDTH ) ); |
| 1995 | |
| 1996 | p = N->p; |
| 1997 | end = p + N->n; |
| 1998 | |
| 1999 | ADD( 3 ); ADD( 5 ); NEXT; // A0 += A3 + A5 |
| 2000 | ADD( 3 ); ADD( 4 ); ADD( 5 ); NEXT; // A1 += A3 + A4 + A5 |
| 2001 | ADD( 4 ); ADD( 5 ); LAST; // A2 += A4 + A5 |
| 2002 | |
| 2003 | cleanup: |
| 2004 | return( ret ); |
| 2005 | } |
| 2006 | |
| 2007 | #undef WIDTH |
| 2008 | #undef A |
| 2009 | #undef ADD |
| 2010 | #undef NEXT |
| 2011 | #undef LAST |
| 2012 | #endif /* POLARSSL_ECP_DP_SECP192R1_ENABLED */ |
| 2013 | |
| 2014 | #if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED) || \ |
| 2015 | defined(POLARSSL_ECP_DP_SECP256R1_ENABLED) || \ |
| 2016 | defined(POLARSSL_ECP_DP_SECP384R1_ENABLED) |
| 2017 | /* |
| 2018 | * The reader is advised to first understand ecp_mod_p192() since the same |
| 2019 | * general structure is used here, but with additional complications: |
| 2020 | * (1) chunks of 32 bits, and (2) subtractions. |
| 2021 | */ |
| 2022 | |
| 2023 | /* |
| 2024 | * For these primes, we need to handle data in chunks of 32 bits. |
| 2025 | * This makes it more complicated if we use 64 bits limbs in MPI, |
| 2026 | * which prevents us from using a uniform access method as for p192. |
| 2027 | * |
| 2028 | * So, we define a mini abstraction layer to access 32 bit chunks, |
| 2029 | * load them in 'cur' for work, and store them back from 'cur' when done. |
| 2030 | * |
| 2031 | * While at it, also define the size of N in terms of 32-bit chunks. |
| 2032 | */ |
| 2033 | #define LOAD32 cur = A( i ); |
| 2034 | |
| 2035 | #if defined(POLARSSL_HAVE_INT8) /* 8 bit */ |
| 2036 | |
| 2037 | #define MAX32 N->n / 4 |
| 2038 | #define A( j ) (uint32_t)( N->p[4*j+0] ) | \ |
| 2039 | ( N->p[4*j+1] << 8 ) | \ |
| 2040 | ( N->p[4*j+2] << 16 ) | \ |
| 2041 | ( N->p[4*j+3] << 24 ) |
| 2042 | #define STORE32 N->p[4*i+0] = (uint8_t)( cur ); \ |
| 2043 | N->p[4*i+1] = (uint8_t)( cur >> 8 ); \ |
| 2044 | N->p[4*i+2] = (uint8_t)( cur >> 16 ); \ |
| 2045 | N->p[4*i+3] = (uint8_t)( cur >> 24 ); |
| 2046 | |
| 2047 | #elif defined(POLARSSL_HAVE_INT16) /* 16 bit */ |
| 2048 | |
| 2049 | #define MAX32 N->n / 2 |
| 2050 | #define A( j ) (uint32_t)( N->p[2*j] ) | ( N->p[2*j+1] << 16 ) |
| 2051 | #define STORE32 N->p[2*i+0] = (uint16_t)( cur ); \ |
| 2052 | N->p[2*i+1] = (uint16_t)( cur >> 16 ); |
| 2053 | |
| 2054 | #elif defined(POLARSSL_HAVE_INT32) /* 32 bit */ |
| 2055 | |
| 2056 | #define MAX32 N->n |
| 2057 | #define A( j ) N->p[j] |
| 2058 | #define STORE32 N->p[i] = cur; |
| 2059 | |
| 2060 | #else /* 64-bit */ |
| 2061 | |
| 2062 | #define MAX32 N->n * 2 |
| 2063 | #define A( j ) j % 2 ? (uint32_t)( N->p[j/2] >> 32 ) : (uint32_t)( N->p[j/2] ) |
| 2064 | #define STORE32 \ |
| 2065 | if( i % 2 ) { \ |
| 2066 | N->p[i/2] &= 0x00000000FFFFFFFF; \ |
| 2067 | N->p[i/2] |= ((uint64_t) cur) << 32; \ |
| 2068 | } else { \ |
| 2069 | N->p[i/2] &= 0xFFFFFFFF00000000; \ |
| 2070 | N->p[i/2] |= (uint64_t) cur; \ |
| 2071 | } |
| 2072 | |
| 2073 | #endif /* sizeof( t_uint ) */ |
| 2074 | |
| 2075 | /* |
| 2076 | * Helpers for addition and subtraction of chunks, with signed carry. |
| 2077 | */ |
| 2078 | static inline void add32( uint32_t *dst, uint32_t src, signed char *carry ) |
| 2079 | { |
| 2080 | *dst += src; |
| 2081 | *carry += ( *dst < src ); |
| 2082 | } |
| 2083 | |
| 2084 | static inline void sub32( uint32_t *dst, uint32_t src, signed char *carry ) |
| 2085 | { |
| 2086 | *carry -= ( *dst < src ); |
| 2087 | *dst -= src; |
| 2088 | } |
| 2089 | |
| 2090 | #define ADD( j ) add32( &cur, A( j ), &c ); |
| 2091 | #define SUB( j ) sub32( &cur, A( j ), &c ); |
| 2092 | |
| 2093 | /* |
| 2094 | * Helpers for the main 'loop' |
Manuel Pégourié-Gonnard | b21c81f | 2013-10-23 20:45:04 +0200 | [diff] [blame] | 2095 | * (see fix_negative for the motivation of C) |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 2096 | */ |
| 2097 | #define INIT( b ) \ |
| 2098 | int ret; \ |
| 2099 | signed char c = 0, cc; \ |
| 2100 | uint32_t cur; \ |
| 2101 | size_t i = 0, bits = b; \ |
Manuel Pégourié-Gonnard | b21c81f | 2013-10-23 20:45:04 +0200 | [diff] [blame] | 2102 | mpi C; \ |
| 2103 | t_uint Cp[ b / 8 / sizeof( t_uint) + 1 ]; \ |
| 2104 | \ |
| 2105 | C.s = 1; \ |
| 2106 | C.n = b / 8 / sizeof( t_uint) + 1; \ |
| 2107 | C.p = Cp; \ |
| 2108 | memset( Cp, 0, C.n * sizeof( t_uint ) ); \ |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 2109 | \ |
| 2110 | MPI_CHK( mpi_grow( N, b * 2 / 8 / sizeof( t_uint ) ) ); \ |
| 2111 | LOAD32; |
| 2112 | |
| 2113 | #define NEXT \ |
| 2114 | STORE32; i++; LOAD32; \ |
| 2115 | cc = c; c = 0; \ |
| 2116 | if( cc < 0 ) \ |
| 2117 | sub32( &cur, -cc, &c ); \ |
| 2118 | else \ |
| 2119 | add32( &cur, cc, &c ); \ |
| 2120 | |
| 2121 | #define LAST \ |
| 2122 | STORE32; i++; \ |
| 2123 | cur = c > 0 ? c : 0; STORE32; \ |
| 2124 | cur = 0; while( ++i < MAX32 ) { STORE32; } \ |
Manuel Pégourié-Gonnard | b21c81f | 2013-10-23 20:45:04 +0200 | [diff] [blame] | 2125 | if( c < 0 ) fix_negative( N, c, &C, bits ); |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 2126 | |
| 2127 | /* |
| 2128 | * If the result is negative, we get it in the form |
| 2129 | * c * 2^(bits + 32) + N, with c negative and N positive shorter than 'bits' |
| 2130 | */ |
Manuel Pégourié-Gonnard | b21c81f | 2013-10-23 20:45:04 +0200 | [diff] [blame] | 2131 | static inline int fix_negative( mpi *N, signed char c, mpi *C, size_t bits ) |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 2132 | { |
| 2133 | int ret; |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 2134 | |
| 2135 | /* C = - c * 2^(bits + 32) */ |
Manuel Pégourié-Gonnard | b21c81f | 2013-10-23 20:45:04 +0200 | [diff] [blame] | 2136 | #if !defined(POLARSSL_HAVE_INT64) |
| 2137 | ((void) bits); |
| 2138 | #else |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 2139 | if( bits == 224 ) |
Manuel Pégourié-Gonnard | b21c81f | 2013-10-23 20:45:04 +0200 | [diff] [blame] | 2140 | C->p[ C->n - 1 ] = ((t_uint) -c) << 32; |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 2141 | else |
| 2142 | #endif |
Manuel Pégourié-Gonnard | b21c81f | 2013-10-23 20:45:04 +0200 | [diff] [blame] | 2143 | C->p[ C->n - 1 ] = (t_uint) -c; |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 2144 | |
| 2145 | /* N = - ( C - N ) */ |
Manuel Pégourié-Gonnard | b21c81f | 2013-10-23 20:45:04 +0200 | [diff] [blame] | 2146 | MPI_CHK( mpi_sub_abs( N, C, N ) ); |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 2147 | N->s = -1; |
| 2148 | |
| 2149 | cleanup: |
| 2150 | |
| 2151 | return( ret ); |
| 2152 | } |
| 2153 | |
| 2154 | #if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED) |
| 2155 | /* |
| 2156 | * Fast quasi-reduction modulo p224 (FIPS 186-3 D.2.2) |
| 2157 | */ |
| 2158 | static int ecp_mod_p224( mpi *N ) |
| 2159 | { |
| 2160 | INIT( 224 ); |
| 2161 | |
| 2162 | SUB( 7 ); SUB( 11 ); NEXT; // A0 += -A7 - A11 |
| 2163 | SUB( 8 ); SUB( 12 ); NEXT; // A1 += -A8 - A12 |
| 2164 | SUB( 9 ); SUB( 13 ); NEXT; // A2 += -A9 - A13 |
| 2165 | SUB( 10 ); ADD( 7 ); ADD( 11 ); NEXT; // A3 += -A10 + A7 + A11 |
| 2166 | SUB( 11 ); ADD( 8 ); ADD( 12 ); NEXT; // A4 += -A11 + A8 + A12 |
| 2167 | SUB( 12 ); ADD( 9 ); ADD( 13 ); NEXT; // A5 += -A12 + A9 + A13 |
| 2168 | SUB( 13 ); ADD( 10 ); LAST; // A6 += -A13 + A10 |
| 2169 | |
| 2170 | cleanup: |
| 2171 | return( ret ); |
| 2172 | } |
| 2173 | #endif /* POLARSSL_ECP_DP_SECP224R1_ENABLED */ |
| 2174 | |
| 2175 | #if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED) |
| 2176 | /* |
| 2177 | * Fast quasi-reduction modulo p256 (FIPS 186-3 D.2.3) |
| 2178 | */ |
| 2179 | static int ecp_mod_p256( mpi *N ) |
| 2180 | { |
| 2181 | INIT( 256 ); |
| 2182 | |
| 2183 | ADD( 8 ); ADD( 9 ); |
| 2184 | SUB( 11 ); SUB( 12 ); SUB( 13 ); SUB( 14 ); NEXT; // A0 |
| 2185 | |
| 2186 | ADD( 9 ); ADD( 10 ); |
| 2187 | SUB( 12 ); SUB( 13 ); SUB( 14 ); SUB( 15 ); NEXT; // A1 |
| 2188 | |
| 2189 | ADD( 10 ); ADD( 11 ); |
| 2190 | SUB( 13 ); SUB( 14 ); SUB( 15 ); NEXT; // A2 |
| 2191 | |
| 2192 | ADD( 11 ); ADD( 11 ); ADD( 12 ); ADD( 12 ); ADD( 13 ); |
| 2193 | SUB( 15 ); SUB( 8 ); SUB( 9 ); NEXT; // A3 |
| 2194 | |
| 2195 | ADD( 12 ); ADD( 12 ); ADD( 13 ); ADD( 13 ); ADD( 14 ); |
| 2196 | SUB( 9 ); SUB( 10 ); NEXT; // A4 |
| 2197 | |
| 2198 | ADD( 13 ); ADD( 13 ); ADD( 14 ); ADD( 14 ); ADD( 15 ); |
| 2199 | SUB( 10 ); SUB( 11 ); NEXT; // A5 |
| 2200 | |
| 2201 | ADD( 14 ); ADD( 14 ); ADD( 15 ); ADD( 15 ); ADD( 14 ); ADD( 13 ); |
| 2202 | SUB( 8 ); SUB( 9 ); NEXT; // A6 |
| 2203 | |
| 2204 | ADD( 15 ); ADD( 15 ); ADD( 15 ); ADD( 8 ); |
| 2205 | SUB( 10 ); SUB( 11 ); SUB( 12 ); SUB( 13 ); LAST; // A7 |
| 2206 | |
| 2207 | cleanup: |
| 2208 | return( ret ); |
| 2209 | } |
| 2210 | #endif /* POLARSSL_ECP_DP_SECP256R1_ENABLED */ |
| 2211 | |
| 2212 | #if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED) |
| 2213 | /* |
| 2214 | * Fast quasi-reduction modulo p384 (FIPS 186-3 D.2.4) |
| 2215 | */ |
| 2216 | static int ecp_mod_p384( mpi *N ) |
| 2217 | { |
| 2218 | INIT( 384 ); |
| 2219 | |
| 2220 | ADD( 12 ); ADD( 21 ); ADD( 20 ); |
| 2221 | SUB( 23 ); NEXT; // A0 |
| 2222 | |
| 2223 | ADD( 13 ); ADD( 22 ); ADD( 23 ); |
| 2224 | SUB( 12 ); SUB( 20 ); NEXT; // A2 |
| 2225 | |
| 2226 | ADD( 14 ); ADD( 23 ); |
| 2227 | SUB( 13 ); SUB( 21 ); NEXT; // A2 |
| 2228 | |
| 2229 | ADD( 15 ); ADD( 12 ); ADD( 20 ); ADD( 21 ); |
| 2230 | SUB( 14 ); SUB( 22 ); SUB( 23 ); NEXT; // A3 |
| 2231 | |
| 2232 | ADD( 21 ); ADD( 21 ); ADD( 16 ); ADD( 13 ); ADD( 12 ); ADD( 20 ); ADD( 22 ); |
| 2233 | SUB( 15 ); SUB( 23 ); SUB( 23 ); NEXT; // A4 |
| 2234 | |
| 2235 | ADD( 22 ); ADD( 22 ); ADD( 17 ); ADD( 14 ); ADD( 13 ); ADD( 21 ); ADD( 23 ); |
| 2236 | SUB( 16 ); NEXT; // A5 |
| 2237 | |
| 2238 | ADD( 23 ); ADD( 23 ); ADD( 18 ); ADD( 15 ); ADD( 14 ); ADD( 22 ); |
| 2239 | SUB( 17 ); NEXT; // A6 |
| 2240 | |
| 2241 | ADD( 19 ); ADD( 16 ); ADD( 15 ); ADD( 23 ); |
| 2242 | SUB( 18 ); NEXT; // A7 |
| 2243 | |
| 2244 | ADD( 20 ); ADD( 17 ); ADD( 16 ); |
| 2245 | SUB( 19 ); NEXT; // A8 |
| 2246 | |
| 2247 | ADD( 21 ); ADD( 18 ); ADD( 17 ); |
| 2248 | SUB( 20 ); NEXT; // A9 |
| 2249 | |
| 2250 | ADD( 22 ); ADD( 19 ); ADD( 18 ); |
| 2251 | SUB( 21 ); NEXT; // A10 |
| 2252 | |
| 2253 | ADD( 23 ); ADD( 20 ); ADD( 19 ); |
| 2254 | SUB( 22 ); LAST; // A11 |
| 2255 | |
| 2256 | cleanup: |
| 2257 | return( ret ); |
| 2258 | } |
| 2259 | #endif /* POLARSSL_ECP_DP_SECP384R1_ENABLED */ |
| 2260 | |
| 2261 | #undef A |
| 2262 | #undef LOAD32 |
| 2263 | #undef STORE32 |
| 2264 | #undef MAX32 |
| 2265 | #undef INIT |
| 2266 | #undef NEXT |
| 2267 | #undef LAST |
| 2268 | |
| 2269 | #endif /* POLARSSL_ECP_DP_SECP224R1_ENABLED || |
| 2270 | POLARSSL_ECP_DP_SECP256R1_ENABLED || |
| 2271 | POLARSSL_ECP_DP_SECP384R1_ENABLED */ |
| 2272 | |
| 2273 | #if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED) |
| 2274 | /* |
| 2275 | * Here we have an actual Mersenne prime, so things are more straightforward. |
| 2276 | * However, chunks are aligned on a 'weird' boundary (521 bits). |
| 2277 | */ |
| 2278 | |
| 2279 | /* Size of p521 in terms of t_uint */ |
| 2280 | #define P521_WIDTH ( 521 / 8 / sizeof( t_uint ) + 1 ) |
| 2281 | |
| 2282 | /* Bits to keep in the most significant t_uint */ |
| 2283 | #if defined(POLARSSL_HAVE_INT8) |
| 2284 | #define P521_MASK 0x01 |
| 2285 | #else |
| 2286 | #define P521_MASK 0x01FF |
| 2287 | #endif |
| 2288 | |
| 2289 | /* |
| 2290 | * Fast quasi-reduction modulo p521 (FIPS 186-3 D.2.5) |
| 2291 | * Write N as A1 + 2^521 A0, return A0 + A1 |
| 2292 | */ |
| 2293 | static int ecp_mod_p521( mpi *N ) |
| 2294 | { |
| 2295 | int ret; |
| 2296 | size_t i; |
| 2297 | mpi M; |
| 2298 | t_uint Mp[P521_WIDTH + 1]; |
| 2299 | /* Worst case for the size of M is when t_uint is 16 bits: |
| 2300 | * we need to hold bits 513 to 1056, which is 34 limbs, that is |
| 2301 | * P521_WIDTH + 1. Otherwise P521_WIDTH is enough. */ |
| 2302 | |
| 2303 | if( N->n < P521_WIDTH ) |
| 2304 | return( 0 ); |
| 2305 | |
| 2306 | /* M = A1 */ |
| 2307 | M.s = 1; |
| 2308 | M.n = N->n - ( P521_WIDTH - 1 ); |
| 2309 | if( M.n > P521_WIDTH + 1 ) |
| 2310 | M.n = P521_WIDTH + 1; |
| 2311 | M.p = Mp; |
| 2312 | memcpy( Mp, N->p + P521_WIDTH - 1, M.n * sizeof( t_uint ) ); |
| 2313 | MPI_CHK( mpi_shift_r( &M, 521 % ( 8 * sizeof( t_uint ) ) ) ); |
| 2314 | |
| 2315 | /* N = A0 */ |
| 2316 | N->p[P521_WIDTH - 1] &= P521_MASK; |
| 2317 | for( i = P521_WIDTH; i < N->n; i++ ) |
| 2318 | N->p[i] = 0; |
| 2319 | |
| 2320 | /* N = A0 + A1 */ |
| 2321 | MPI_CHK( mpi_add_abs( N, N, &M ) ); |
| 2322 | |
| 2323 | cleanup: |
| 2324 | return( ret ); |
| 2325 | } |
| 2326 | |
| 2327 | #undef P521_WIDTH |
| 2328 | #undef P521_MASK |
| 2329 | #endif /* POLARSSL_ECP_DP_SECP521R1_ENABLED */ |
| 2330 | |
| 2331 | #endif /* POLARSSL_ECP_NIST_OPTIM */ |
| 2332 | |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 2333 | #if defined(POLARSSL_SELF_TEST) |
| 2334 | |
Manuel Pégourié-Gonnard | b505c27 | 2012-11-05 17:27:54 +0100 | [diff] [blame] | 2335 | /* |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 2336 | * Checkup routine |
| 2337 | */ |
| 2338 | int ecp_self_test( int verbose ) |
| 2339 | { |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 2340 | int ret; |
| 2341 | size_t i; |
| 2342 | ecp_group grp; |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 2343 | ecp_point R, P; |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 2344 | mpi m; |
| 2345 | unsigned long add_c_prev, dbl_c_prev; |
Manuel Pégourié-Gonnard | b8012fc | 2013-10-10 15:40:49 +0200 | [diff] [blame] | 2346 | /* exponents especially adapted for secp192r1 */ |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 2347 | const char *exponents[] = |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 2348 | { |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 2349 | "000000000000000000000000000000000000000000000000", /* zero */ |
| 2350 | "000000000000000000000000000000000000000000000001", /* one */ |
| 2351 | "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831", /* N */ |
| 2352 | "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */ |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 2353 | "400000000000000000000000000000000000000000000000", |
| 2354 | "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", |
| 2355 | "555555555555555555555555555555555555555555555555", |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 2356 | }; |
| 2357 | |
| 2358 | ecp_group_init( &grp ); |
| 2359 | ecp_point_init( &R ); |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 2360 | ecp_point_init( &P ); |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 2361 | mpi_init( &m ); |
| 2362 | |
Manuel Pégourié-Gonnard | b8012fc | 2013-10-10 15:40:49 +0200 | [diff] [blame] | 2363 | /* Use secp192r1 if available, or any available curve */ |
Paul Bakker | 5dc6b5f | 2013-06-29 23:26:34 +0200 | [diff] [blame] | 2364 | #if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED) |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 2365 | MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP192R1 ) ); |
Paul Bakker | 5dc6b5f | 2013-06-29 23:26:34 +0200 | [diff] [blame] | 2366 | #else |
Manuel Pégourié-Gonnard | b8012fc | 2013-10-10 15:40:49 +0200 | [diff] [blame] | 2367 | MPI_CHK( ecp_use_known_dp( &grp, ecp_curve_list()->grp_id ) ); |
| 2368 | #endif |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 2369 | |
| 2370 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 2371 | printf( " ECP test #1 (constant op_count, base point G): " ); |
| 2372 | |
| 2373 | /* Do a dummy multiplication first to trigger precomputation */ |
| 2374 | MPI_CHK( mpi_lset( &m, 2 ) ); |
| 2375 | MPI_CHK( ecp_mul( &grp, &P, &m, &grp.G, NULL, NULL ) ); |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 2376 | |
| 2377 | add_count = 0; |
| 2378 | dbl_count = 0; |
| 2379 | MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) ); |
Manuel Pégourié-Gonnard | e09d2f8 | 2013-09-02 14:29:09 +0200 | [diff] [blame] | 2380 | MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) ); |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 2381 | |
| 2382 | for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ ) |
| 2383 | { |
| 2384 | add_c_prev = add_count; |
| 2385 | dbl_c_prev = dbl_count; |
| 2386 | add_count = 0; |
| 2387 | dbl_count = 0; |
| 2388 | |
| 2389 | MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) ); |
Manuel Pégourié-Gonnard | e09d2f8 | 2013-09-02 14:29:09 +0200 | [diff] [blame] | 2390 | MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) ); |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 2391 | |
| 2392 | if( add_count != add_c_prev || dbl_count != dbl_c_prev ) |
| 2393 | { |
| 2394 | if( verbose != 0 ) |
| 2395 | printf( "failed (%zu)\n", i ); |
| 2396 | |
| 2397 | ret = 1; |
| 2398 | goto cleanup; |
| 2399 | } |
| 2400 | } |
| 2401 | |
| 2402 | if( verbose != 0 ) |
| 2403 | printf( "passed\n" ); |
| 2404 | |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 2405 | if( verbose != 0 ) |
| 2406 | printf( " ECP test #2 (constant op_count, other point): " ); |
| 2407 | /* We computed P = 2G last time, use it */ |
| 2408 | |
| 2409 | add_count = 0; |
| 2410 | dbl_count = 0; |
| 2411 | MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) ); |
| 2412 | MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) ); |
| 2413 | |
| 2414 | for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ ) |
| 2415 | { |
| 2416 | add_c_prev = add_count; |
| 2417 | dbl_c_prev = dbl_count; |
| 2418 | add_count = 0; |
| 2419 | dbl_count = 0; |
| 2420 | |
| 2421 | MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) ); |
| 2422 | MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) ); |
| 2423 | |
| 2424 | if( add_count != add_c_prev || dbl_count != dbl_c_prev ) |
| 2425 | { |
| 2426 | if( verbose != 0 ) |
| 2427 | printf( "failed (%zu)\n", i ); |
| 2428 | |
| 2429 | ret = 1; |
| 2430 | goto cleanup; |
| 2431 | } |
| 2432 | } |
| 2433 | |
| 2434 | if( verbose != 0 ) |
| 2435 | printf( "passed\n" ); |
| 2436 | |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 2437 | cleanup: |
| 2438 | |
| 2439 | if( ret < 0 && verbose != 0 ) |
| 2440 | printf( "Unexpected error, return code = %08X\n", ret ); |
| 2441 | |
| 2442 | ecp_group_free( &grp ); |
| 2443 | ecp_point_free( &R ); |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 2444 | ecp_point_free( &P ); |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 2445 | mpi_free( &m ); |
| 2446 | |
| 2447 | if( verbose != 0 ) |
| 2448 | printf( "\n" ); |
| 2449 | |
| 2450 | return( ret ); |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 2451 | } |
| 2452 | |
| 2453 | #endif |
| 2454 | |
| 2455 | #endif |