Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 1 | /* |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 2 | * Elliptic curves over GF(p): generic functions |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 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 | * |
Manuel Pégourié-Gonnard | fe0af40 | 2013-12-04 18:14:55 +0100 | [diff] [blame] | 34 | * [M255] http://cr.yp.to/ecdh/curve25519-20060209.pdf |
| 35 | * |
Manuel Pégourié-Gonnard | 07de4b1 | 2013-09-02 16:26:04 +0200 | [diff] [blame] | 36 | * [2] CORON, Jean-Sébastien. Resistance against differential power analysis |
| 37 | * for elliptic curve cryptosystems. In : Cryptographic Hardware and |
| 38 | * Embedded Systems. Springer Berlin Heidelberg, 1999. p. 292-302. |
| 39 | * <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] | 40 | * |
| 41 | * [3] HEDABOU, Mustapha, PINEL, Pierre, et BÉNÉTEAU, Lucien. A comb method to |
| 42 | * render ECC resistant against Side Channel Attacks. IACR Cryptology |
| 43 | * ePrint Archive, 2004, vol. 2004, p. 342. |
| 44 | * <http://eprint.iacr.org/2004/342.pdf> |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 45 | */ |
| 46 | |
| 47 | #include "polarssl/config.h" |
| 48 | |
| 49 | #if defined(POLARSSL_ECP_C) |
| 50 | |
| 51 | #include "polarssl/ecp.h" |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 52 | |
| 53 | #if defined(POLARSSL_MEMORY_C) |
| 54 | #include "polarssl/memory.h" |
| 55 | #else |
| 56 | #define polarssl_malloc malloc |
| 57 | #define polarssl_free free |
| 58 | #endif |
| 59 | |
Manuel Pégourié-Gonnard | cdd4432 | 2012-11-21 16:00:55 +0100 | [diff] [blame] | 60 | #include <stdlib.h> |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 61 | |
Manuel Pégourié-Gonnard | 0267e3d | 2013-11-30 15:10:14 +0100 | [diff] [blame] | 62 | #if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \ |
| 63 | !defined(EFI32) |
| 64 | #define strcasecmp _stricmp |
| 65 | #endif |
| 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 | /* |
Manuel Pégourié-Gonnard | 9181481 | 2013-11-21 20:23:55 +0100 | [diff] [blame] | 77 | * Counts of point addition and doubling, and field multiplications. |
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 | */ |
Manuel Pégourié-Gonnard | 43863ee | 2013-12-01 16:51:27 +0100 | [diff] [blame] | 80 | static unsigned long add_count, dbl_count, mul_count; |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 81 | #endif |
| 82 | |
Manuel Pégourié-Gonnard | 7c94d8b | 2013-12-04 23:15:46 +0100 | [diff] [blame] | 83 | #if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED) || \ |
| 84 | defined(POLARSSL_ECP_DP_SECP224R1_ENABLED) || \ |
| 85 | defined(POLARSSL_ECP_DP_SECP256R1_ENABLED) || \ |
| 86 | defined(POLARSSL_ECP_DP_SECP384R1_ENABLED) || \ |
| 87 | defined(POLARSSL_ECP_DP_SECP521R1_ENABLED) || \ |
| 88 | defined(POLARSSL_ECP_DP_BP256R1_ENABLED) || \ |
| 89 | defined(POLARSSL_ECP_DP_BP384R1_ENABLED) || \ |
| 90 | defined(POLARSSL_ECP_DP_BP512R1_ENABLED) |
| 91 | #define POLARSSL_ECP_SHORT_WEIERSTRASS |
| 92 | #endif |
| 93 | |
| 94 | #if defined(POLARSSL_ECP_DP_M221_ENABLED) || \ |
| 95 | defined(POLARSSL_ECP_DP_M255_ENABLED) || \ |
| 96 | defined(POLARSSL_ECP_DP_M383_ENABLED) || \ |
| 97 | defined(POLARSSL_ECP_DP_M511_ENABLED) |
| 98 | #define POLARSSL_ECP_MONTGOMERY |
| 99 | #endif |
| 100 | |
| 101 | /* |
| 102 | * Curve types: internal for now, might be exposed later |
| 103 | */ |
| 104 | typedef enum |
| 105 | { |
| 106 | POLARSSL_ECP_TYPE_NONE = 0, |
| 107 | POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS, /* y^2 = x^3 + a x + b */ |
| 108 | POLARSSL_ECP_TYPE_MONTGOMERY, /* y^2 = x^3 + a x^2 + x */ |
| 109 | } ecp_curve_type; |
| 110 | |
Manuel Pégourié-Gonnard | 1e8c8ec | 2012-10-31 19:24:21 +0100 | [diff] [blame] | 111 | /* |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 112 | * List of supported curves: |
| 113 | * - internal ID |
Manuel Pégourié-Gonnard | 8195c1a | 2013-10-07 19:40:41 +0200 | [diff] [blame] | 114 | * - 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] | 115 | * - size in bits |
Manuel Pégourié-Gonnard | 8195c1a | 2013-10-07 19:40:41 +0200 | [diff] [blame] | 116 | * - readable name |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 117 | */ |
Manuel Pégourié-Gonnard | 43863ee | 2013-12-01 16:51:27 +0100 | [diff] [blame] | 118 | static const ecp_curve_info ecp_supported_curves[] = |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 119 | { |
Manuel Pégourié-Gonnard | 8195c1a | 2013-10-07 19:40:41 +0200 | [diff] [blame] | 120 | #if defined(POLARSSL_ECP_DP_BP512R1_ENABLED) |
Manuel Pégourié-Gonnard | 0267e3d | 2013-11-30 15:10:14 +0100 | [diff] [blame] | 121 | { POLARSSL_ECP_DP_BP512R1, 28, 512, "brainpoolP512r1" }, |
Manuel Pégourié-Gonnard | 8195c1a | 2013-10-07 19:40:41 +0200 | [diff] [blame] | 122 | #endif |
| 123 | #if defined(POLARSSL_ECP_DP_BP384R1_ENABLED) |
Manuel Pégourié-Gonnard | 0267e3d | 2013-11-30 15:10:14 +0100 | [diff] [blame] | 124 | { POLARSSL_ECP_DP_BP384R1, 27, 384, "brainpoolP384r1" }, |
Manuel Pégourié-Gonnard | 8195c1a | 2013-10-07 19:40:41 +0200 | [diff] [blame] | 125 | #endif |
| 126 | #if defined(POLARSSL_ECP_DP_BP256R1_ENABLED) |
Manuel Pégourié-Gonnard | 0267e3d | 2013-11-30 15:10:14 +0100 | [diff] [blame] | 127 | { POLARSSL_ECP_DP_BP256R1, 26, 256, "brainpoolP256r1" }, |
Manuel Pégourié-Gonnard | 8195c1a | 2013-10-07 19:40:41 +0200 | [diff] [blame] | 128 | #endif |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 129 | #if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED) |
Manuel Pégourié-Gonnard | 8195c1a | 2013-10-07 19:40:41 +0200 | [diff] [blame] | 130 | { POLARSSL_ECP_DP_SECP521R1, 25, 521, "secp521r1" }, |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 131 | #endif |
| 132 | #if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED) |
Manuel Pégourié-Gonnard | 8195c1a | 2013-10-07 19:40:41 +0200 | [diff] [blame] | 133 | { POLARSSL_ECP_DP_SECP384R1, 24, 384, "secp384r1" }, |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 134 | #endif |
| 135 | #if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED) |
Manuel Pégourié-Gonnard | 8195c1a | 2013-10-07 19:40:41 +0200 | [diff] [blame] | 136 | { POLARSSL_ECP_DP_SECP256R1, 23, 256, "secp256r1" }, |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 137 | #endif |
| 138 | #if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED) |
Manuel Pégourié-Gonnard | 8195c1a | 2013-10-07 19:40:41 +0200 | [diff] [blame] | 139 | { POLARSSL_ECP_DP_SECP224R1, 21, 224, "secp224r1" }, |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 140 | #endif |
| 141 | #if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED) |
Manuel Pégourié-Gonnard | 8195c1a | 2013-10-07 19:40:41 +0200 | [diff] [blame] | 142 | { POLARSSL_ECP_DP_SECP192R1, 19, 192, "secp192r1" }, |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 143 | #endif |
Manuel Pégourié-Gonnard | 8195c1a | 2013-10-07 19:40:41 +0200 | [diff] [blame] | 144 | { POLARSSL_ECP_DP_NONE, 0, 0, NULL }, |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | /* |
Manuel Pégourié-Gonnard | da179e4 | 2013-09-18 15:31:24 +0200 | [diff] [blame] | 148 | * List of supported curves and associated info |
| 149 | */ |
| 150 | const ecp_curve_info *ecp_curve_list( void ) |
| 151 | { |
| 152 | return ecp_supported_curves; |
| 153 | } |
| 154 | |
| 155 | /* |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 156 | * Get the curve info for the internal identifer |
| 157 | */ |
| 158 | const ecp_curve_info *ecp_curve_info_from_grp_id( ecp_group_id grp_id ) |
| 159 | { |
| 160 | const ecp_curve_info *curve_info; |
| 161 | |
| 162 | for( curve_info = ecp_curve_list(); |
| 163 | curve_info->grp_id != POLARSSL_ECP_DP_NONE; |
| 164 | curve_info++ ) |
| 165 | { |
| 166 | if( curve_info->grp_id == grp_id ) |
| 167 | return( curve_info ); |
| 168 | } |
| 169 | |
| 170 | return( NULL ); |
| 171 | } |
| 172 | |
| 173 | /* |
| 174 | * Get the curve info from the TLS identifier |
| 175 | */ |
| 176 | const ecp_curve_info *ecp_curve_info_from_tls_id( uint16_t tls_id ) |
| 177 | { |
| 178 | const ecp_curve_info *curve_info; |
| 179 | |
| 180 | for( curve_info = ecp_curve_list(); |
| 181 | curve_info->grp_id != POLARSSL_ECP_DP_NONE; |
| 182 | curve_info++ ) |
| 183 | { |
| 184 | if( curve_info->tls_id == tls_id ) |
| 185 | return( curve_info ); |
| 186 | } |
| 187 | |
| 188 | return( NULL ); |
| 189 | } |
| 190 | |
| 191 | /* |
Manuel Pégourié-Gonnard | 0267e3d | 2013-11-30 15:10:14 +0100 | [diff] [blame] | 192 | * Get the curve info from the name |
| 193 | */ |
| 194 | const ecp_curve_info *ecp_curve_info_from_name( const char *name ) |
| 195 | { |
| 196 | const ecp_curve_info *curve_info; |
| 197 | |
| 198 | for( curve_info = ecp_curve_list(); |
| 199 | curve_info->grp_id != POLARSSL_ECP_DP_NONE; |
| 200 | curve_info++ ) |
| 201 | { |
| 202 | if( strcasecmp( curve_info->name, name ) == 0 ) |
| 203 | return( curve_info ); |
| 204 | } |
| 205 | |
| 206 | return( NULL ); |
| 207 | } |
| 208 | |
| 209 | /* |
Manuel Pégourié-Gonnard | 7c94d8b | 2013-12-04 23:15:46 +0100 | [diff] [blame] | 210 | * Get the type of a curve |
Manuel Pégourié-Gonnard | 312d2e8 | 2013-12-04 11:08:01 +0100 | [diff] [blame] | 211 | */ |
Manuel Pégourié-Gonnard | 7c94d8b | 2013-12-04 23:15:46 +0100 | [diff] [blame] | 212 | static inline ecp_curve_type ecp_get_type( const ecp_group *grp ) |
Manuel Pégourié-Gonnard | 312d2e8 | 2013-12-04 11:08:01 +0100 | [diff] [blame] | 213 | { |
Manuel Pégourié-Gonnard | 7c94d8b | 2013-12-04 23:15:46 +0100 | [diff] [blame] | 214 | if( grp->G.X.p == NULL ) |
| 215 | return( POLARSSL_ECP_TYPE_NONE ); |
| 216 | |
| 217 | if( grp->G.Y.p == NULL ) |
| 218 | return( POLARSSL_ECP_TYPE_MONTGOMERY ); |
| 219 | else |
| 220 | return( POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS ); |
Manuel Pégourié-Gonnard | 312d2e8 | 2013-12-04 11:08:01 +0100 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | /* |
Manuel Pégourié-Gonnard | b505c27 | 2012-11-05 17:27:54 +0100 | [diff] [blame] | 224 | * Initialize (the components of) a point |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 225 | */ |
| 226 | void ecp_point_init( ecp_point *pt ) |
| 227 | { |
| 228 | if( pt == NULL ) |
| 229 | return; |
| 230 | |
Manuel Pégourié-Gonnard | b505c27 | 2012-11-05 17:27:54 +0100 | [diff] [blame] | 231 | mpi_init( &pt->X ); |
| 232 | mpi_init( &pt->Y ); |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 233 | mpi_init( &pt->Z ); |
Manuel Pégourié-Gonnard | b505c27 | 2012-11-05 17:27:54 +0100 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | /* |
| 237 | * Initialize (the components of) a group |
| 238 | */ |
| 239 | void ecp_group_init( ecp_group *grp ) |
| 240 | { |
| 241 | if( grp == NULL ) |
| 242 | return; |
| 243 | |
Manuel Pégourié-Gonnard | c972770 | 2013-09-16 18:56:28 +0200 | [diff] [blame] | 244 | memset( grp, 0, sizeof( ecp_group ) ); |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | /* |
Manuel Pégourié-Gonnard | b8c6e0e | 2013-07-01 13:40:52 +0200 | [diff] [blame] | 248 | * Initialize (the components of) a key pair |
| 249 | */ |
| 250 | void ecp_keypair_init( ecp_keypair *key ) |
| 251 | { |
| 252 | if ( key == NULL ) |
| 253 | return; |
| 254 | |
| 255 | ecp_group_init( &key->grp ); |
| 256 | mpi_init( &key->d ); |
| 257 | ecp_point_init( &key->Q ); |
Manuel Pégourié-Gonnard | b8c6e0e | 2013-07-01 13:40:52 +0200 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | /* |
Manuel Pégourié-Gonnard | 1e8c8ec | 2012-10-31 19:24:21 +0100 | [diff] [blame] | 261 | * Unallocate (the components of) a point |
| 262 | */ |
| 263 | void ecp_point_free( ecp_point *pt ) |
| 264 | { |
| 265 | if( pt == NULL ) |
| 266 | return; |
| 267 | |
| 268 | mpi_free( &( pt->X ) ); |
| 269 | mpi_free( &( pt->Y ) ); |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 270 | mpi_free( &( pt->Z ) ); |
Manuel Pégourié-Gonnard | 1e8c8ec | 2012-10-31 19:24:21 +0100 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | /* |
| 274 | * Unallocate (the components of) a group |
| 275 | */ |
| 276 | void ecp_group_free( ecp_group *grp ) |
| 277 | { |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 278 | size_t i; |
| 279 | |
Manuel Pégourié-Gonnard | 1e8c8ec | 2012-10-31 19:24:21 +0100 | [diff] [blame] | 280 | if( grp == NULL ) |
| 281 | return; |
| 282 | |
Manuel Pégourié-Gonnard | 1f82b04 | 2013-12-06 12:51:50 +0100 | [diff] [blame] | 283 | if( grp->h != 1 ) |
| 284 | { |
| 285 | mpi_free( &grp->P ); |
| 286 | mpi_free( &grp->A ); |
| 287 | mpi_free( &grp->B ); |
| 288 | ecp_point_free( &grp->G ); |
| 289 | mpi_free( &grp->N ); |
| 290 | } |
Manuel Pégourié-Gonnard | c972770 | 2013-09-16 18:56:28 +0200 | [diff] [blame] | 291 | |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 292 | if( grp->T != NULL ) |
| 293 | { |
| 294 | for( i = 0; i < grp->T_size; i++ ) |
| 295 | ecp_point_free( &grp->T[i] ); |
| 296 | polarssl_free( grp->T ); |
| 297 | } |
| 298 | |
Manuel Pégourié-Gonnard | c972770 | 2013-09-16 18:56:28 +0200 | [diff] [blame] | 299 | memset( grp, 0, sizeof( ecp_group ) ); |
Manuel Pégourié-Gonnard | 1e8c8ec | 2012-10-31 19:24:21 +0100 | [diff] [blame] | 300 | } |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 301 | |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 302 | /* |
Manuel Pégourié-Gonnard | b8c6e0e | 2013-07-01 13:40:52 +0200 | [diff] [blame] | 303 | * Unallocate (the components of) a key pair |
| 304 | */ |
| 305 | void ecp_keypair_free( ecp_keypair *key ) |
| 306 | { |
| 307 | if ( key == NULL ) |
| 308 | return; |
| 309 | |
| 310 | ecp_group_free( &key->grp ); |
| 311 | mpi_free( &key->d ); |
| 312 | ecp_point_free( &key->Q ); |
Manuel Pégourié-Gonnard | b8c6e0e | 2013-07-01 13:40:52 +0200 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | /* |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 316 | * Copy the contents of a point |
| 317 | */ |
| 318 | int ecp_copy( ecp_point *P, const ecp_point *Q ) |
| 319 | { |
| 320 | int ret; |
| 321 | |
| 322 | MPI_CHK( mpi_copy( &P->X, &Q->X ) ); |
| 323 | MPI_CHK( mpi_copy( &P->Y, &Q->Y ) ); |
| 324 | MPI_CHK( mpi_copy( &P->Z, &Q->Z ) ); |
| 325 | |
| 326 | cleanup: |
| 327 | return( ret ); |
| 328 | } |
| 329 | |
| 330 | /* |
| 331 | * Copy the contents of a group object |
| 332 | */ |
| 333 | int ecp_group_copy( ecp_group *dst, const ecp_group *src ) |
| 334 | { |
| 335 | return ecp_use_known_dp( dst, src->id ); |
| 336 | } |
| 337 | |
| 338 | /* |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 339 | * Set point to zero |
| 340 | */ |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 341 | int ecp_set_zero( ecp_point *pt ) |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 342 | { |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 343 | int ret; |
| 344 | |
| 345 | MPI_CHK( mpi_lset( &pt->X , 1 ) ); |
| 346 | MPI_CHK( mpi_lset( &pt->Y , 1 ) ); |
| 347 | MPI_CHK( mpi_lset( &pt->Z , 0 ) ); |
| 348 | |
| 349 | cleanup: |
| 350 | return( ret ); |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | /* |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 354 | * Tell if a point is zero |
| 355 | */ |
| 356 | int ecp_is_zero( ecp_point *pt ) |
| 357 | { |
| 358 | return( mpi_cmp_int( &pt->Z, 0 ) == 0 ); |
| 359 | } |
| 360 | |
| 361 | /* |
Manuel Pégourié-Gonnard | 847395a | 2012-11-05 13:13:44 +0100 | [diff] [blame] | 362 | * Import a non-zero point from ASCII strings |
| 363 | */ |
| 364 | int ecp_point_read_string( ecp_point *P, int radix, |
| 365 | const char *x, const char *y ) |
| 366 | { |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 367 | int ret; |
Manuel Pégourié-Gonnard | 847395a | 2012-11-05 13:13:44 +0100 | [diff] [blame] | 368 | |
Manuel Pégourié-Gonnard | 847395a | 2012-11-05 13:13:44 +0100 | [diff] [blame] | 369 | MPI_CHK( mpi_read_string( &P->X, radix, x ) ); |
| 370 | MPI_CHK( mpi_read_string( &P->Y, radix, y ) ); |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 371 | MPI_CHK( mpi_lset( &P->Z, 1 ) ); |
Manuel Pégourié-Gonnard | 847395a | 2012-11-05 13:13:44 +0100 | [diff] [blame] | 372 | |
| 373 | cleanup: |
| 374 | return( ret ); |
| 375 | } |
| 376 | |
| 377 | /* |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 378 | * 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] | 379 | */ |
Manuel Pégourié-Gonnard | 7e86025 | 2013-02-10 10:58:48 +0100 | [diff] [blame] | 380 | 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] | 381 | int format, size_t *olen, |
Manuel Pégourié-Gonnard | 7e86025 | 2013-02-10 10:58:48 +0100 | [diff] [blame] | 382 | unsigned char *buf, size_t buflen ) |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 383 | { |
Paul Bakker | a280d0f | 2013-04-08 13:40:17 +0200 | [diff] [blame] | 384 | int ret = 0; |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 385 | size_t plen; |
| 386 | |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 387 | if( format != POLARSSL_ECP_PF_UNCOMPRESSED && |
| 388 | format != POLARSSL_ECP_PF_COMPRESSED ) |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 389 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 390 | |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 391 | /* |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 392 | * Common case: P == 0 |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 393 | */ |
| 394 | if( mpi_cmp_int( &P->Z, 0 ) == 0 ) |
| 395 | { |
| 396 | if( buflen < 1 ) |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 397 | return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 398 | |
| 399 | buf[0] = 0x00; |
| 400 | *olen = 1; |
| 401 | |
| 402 | return( 0 ); |
| 403 | } |
| 404 | |
| 405 | plen = mpi_size( &grp->P ); |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 406 | |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 407 | if( format == POLARSSL_ECP_PF_UNCOMPRESSED ) |
| 408 | { |
| 409 | *olen = 2 * plen + 1; |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 410 | |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 411 | if( buflen < *olen ) |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 412 | return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 413 | |
| 414 | buf[0] = 0x04; |
| 415 | MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) ); |
| 416 | MPI_CHK( mpi_write_binary( &P->Y, buf + 1 + plen, plen ) ); |
| 417 | } |
| 418 | else if( format == POLARSSL_ECP_PF_COMPRESSED ) |
| 419 | { |
| 420 | *olen = plen + 1; |
| 421 | |
| 422 | if( buflen < *olen ) |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 423 | return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 424 | |
| 425 | buf[0] = 0x02 + mpi_get_bit( &P->Y, 0 ); |
| 426 | MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) ); |
| 427 | } |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 428 | |
| 429 | cleanup: |
| 430 | return( ret ); |
| 431 | } |
| 432 | |
| 433 | /* |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 434 | * Import a point from unsigned binary data (SEC1 2.3.4) |
| 435 | */ |
Manuel Pégourié-Gonnard | 7e86025 | 2013-02-10 10:58:48 +0100 | [diff] [blame] | 436 | int ecp_point_read_binary( const ecp_group *grp, ecp_point *pt, |
| 437 | const unsigned char *buf, size_t ilen ) { |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 438 | int ret; |
| 439 | size_t plen; |
| 440 | |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 441 | if( ilen == 1 && buf[0] == 0x00 ) |
Manuel Pégourié-Gonnard | d84895d | 2013-02-10 10:53:04 +0100 | [diff] [blame] | 442 | return( ecp_set_zero( pt ) ); |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 443 | |
Manuel Pégourié-Gonnard | d84895d | 2013-02-10 10:53:04 +0100 | [diff] [blame] | 444 | plen = mpi_size( &grp->P ); |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 445 | |
| 446 | if( ilen != 2 * plen + 1 || buf[0] != 0x04 ) |
Manuel Pégourié-Gonnard | d84895d | 2013-02-10 10:53:04 +0100 | [diff] [blame] | 447 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 448 | |
Manuel Pégourié-Gonnard | d84895d | 2013-02-10 10:53:04 +0100 | [diff] [blame] | 449 | MPI_CHK( mpi_read_binary( &pt->X, buf + 1, plen ) ); |
| 450 | MPI_CHK( mpi_read_binary( &pt->Y, buf + 1 + plen, plen ) ); |
| 451 | MPI_CHK( mpi_lset( &pt->Z, 1 ) ); |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 452 | |
| 453 | cleanup: |
| 454 | return( ret ); |
| 455 | } |
| 456 | |
| 457 | /* |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 458 | * Import a point from a TLS ECPoint record (RFC 4492) |
| 459 | * struct { |
| 460 | * opaque point <1..2^8-1>; |
| 461 | * } ECPoint; |
| 462 | */ |
| 463 | 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] | 464 | const unsigned char **buf, size_t buf_len ) |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 465 | { |
| 466 | unsigned char data_len; |
Manuel Pégourié-Gonnard | 98f5181 | 2013-02-10 13:38:29 +0100 | [diff] [blame] | 467 | const unsigned char *buf_start; |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 468 | |
| 469 | /* |
| 470 | * We must have at least two bytes (1 for length, at least of for data) |
| 471 | */ |
| 472 | if( buf_len < 2 ) |
| 473 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
| 474 | |
Manuel Pégourié-Gonnard | 98f5181 | 2013-02-10 13:38:29 +0100 | [diff] [blame] | 475 | data_len = *(*buf)++; |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 476 | if( data_len < 1 || data_len > buf_len - 1 ) |
| 477 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
| 478 | |
Manuel Pégourié-Gonnard | 98f5181 | 2013-02-10 13:38:29 +0100 | [diff] [blame] | 479 | /* |
| 480 | * Save buffer start for read_binary and update buf |
| 481 | */ |
| 482 | buf_start = *buf; |
| 483 | *buf += data_len; |
| 484 | |
| 485 | 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] | 486 | } |
| 487 | |
| 488 | /* |
| 489 | * Export a point as a TLS ECPoint record (RFC 4492) |
| 490 | * struct { |
| 491 | * opaque point <1..2^8-1>; |
| 492 | * } ECPoint; |
| 493 | */ |
| 494 | 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] | 495 | int format, size_t *olen, |
| 496 | unsigned char *buf, size_t blen ) |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 497 | { |
Manuel Pégourié-Gonnard | 420f1eb | 2013-02-10 12:22:46 +0100 | [diff] [blame] | 498 | int ret; |
| 499 | |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 500 | /* |
Manuel Pégourié-Gonnard | 420f1eb | 2013-02-10 12:22:46 +0100 | [diff] [blame] | 501 | * 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] | 502 | */ |
Manuel Pégourié-Gonnard | 420f1eb | 2013-02-10 12:22:46 +0100 | [diff] [blame] | 503 | if( blen < 1 ) |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 504 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
| 505 | |
Manuel Pégourié-Gonnard | 420f1eb | 2013-02-10 12:22:46 +0100 | [diff] [blame] | 506 | if( ( ret = ecp_point_write_binary( grp, pt, format, |
| 507 | olen, buf + 1, blen - 1) ) != 0 ) |
| 508 | return( ret ); |
| 509 | |
| 510 | /* |
| 511 | * write length to the first byte and update total length |
| 512 | */ |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 513 | buf[0] = (unsigned char) *olen; |
Manuel Pégourié-Gonnard | 420f1eb | 2013-02-10 12:22:46 +0100 | [diff] [blame] | 514 | ++*olen; |
| 515 | |
| 516 | return 0; |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | /* |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 520 | * Import an ECP group from ASCII strings, case A == -3 |
Manuel Pégourié-Gonnard | 210b458 | 2013-10-23 14:03:00 +0200 | [diff] [blame] | 521 | */ |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 522 | int ecp_group_read_string( ecp_group *grp, int radix, |
| 523 | const char *p, const char *b, |
| 524 | const char *gx, const char *gy, const char *n) |
Manuel Pégourié-Gonnard | 8433824 | 2012-11-11 20:45:18 +0100 | [diff] [blame] | 525 | { |
| 526 | int ret; |
Manuel Pégourié-Gonnard | 8433824 | 2012-11-11 20:45:18 +0100 | [diff] [blame] | 527 | |
Manuel Pégourié-Gonnard | d5e0fbe | 2013-12-02 17:20:39 +0100 | [diff] [blame] | 528 | MPI_CHK( mpi_read_string( &grp->P, radix, p ) ); |
Manuel Pégourié-Gonnard | d5e0fbe | 2013-12-02 17:20:39 +0100 | [diff] [blame] | 529 | MPI_CHK( mpi_read_string( &grp->B, radix, b ) ); |
| 530 | MPI_CHK( ecp_point_read_string( &grp->G, radix, gx, gy ) ); |
| 531 | MPI_CHK( mpi_read_string( &grp->N, radix, n ) ); |
| 532 | |
| 533 | grp->pbits = mpi_msb( &grp->P ); |
| 534 | grp->nbits = mpi_msb( &grp->N ); |
Manuel Pégourié-Gonnard | 8433824 | 2012-11-11 20:45:18 +0100 | [diff] [blame] | 535 | |
| 536 | cleanup: |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 537 | if( ret != 0 ) |
| 538 | ecp_group_free( grp ); |
Manuel Pégourié-Gonnard | e783f06 | 2013-10-21 14:52:21 +0200 | [diff] [blame] | 539 | |
| 540 | return( ret ); |
| 541 | } |
Manuel Pégourié-Gonnard | c04c530 | 2013-10-23 16:11:52 +0200 | [diff] [blame] | 542 | |
Manuel Pégourié-Gonnard | 62aad14 | 2012-11-10 00:27:12 +0100 | [diff] [blame] | 543 | /* |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 544 | * Set a group from an ECParameters record (RFC 4492) |
| 545 | */ |
Manuel Pégourié-Gonnard | 7c145c6 | 2013-02-10 13:20:52 +0100 | [diff] [blame] | 546 | 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] | 547 | { |
Manuel Pégourié-Gonnard | f24b4a7 | 2013-09-23 18:14:50 +0200 | [diff] [blame] | 548 | uint16_t tls_id; |
| 549 | const ecp_curve_info *curve_info; |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 550 | |
| 551 | /* |
| 552 | * We expect at least three bytes (see below) |
| 553 | */ |
| 554 | if( len < 3 ) |
| 555 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
| 556 | |
| 557 | /* |
| 558 | * First byte is curve_type; only named_curve is handled |
| 559 | */ |
Manuel Pégourié-Gonnard | 7c145c6 | 2013-02-10 13:20:52 +0100 | [diff] [blame] | 560 | if( *(*buf)++ != POLARSSL_ECP_TLS_NAMED_CURVE ) |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 561 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
| 562 | |
| 563 | /* |
Manuel Pégourié-Gonnard | b325887 | 2013-02-10 12:06:19 +0100 | [diff] [blame] | 564 | * Next two bytes are the namedcurve value |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 565 | */ |
Manuel Pégourié-Gonnard | f24b4a7 | 2013-09-23 18:14:50 +0200 | [diff] [blame] | 566 | tls_id = *(*buf)++; |
| 567 | tls_id <<= 8; |
| 568 | tls_id |= *(*buf)++; |
| 569 | |
| 570 | if( ( curve_info = ecp_curve_info_from_tls_id( tls_id ) ) == NULL ) |
| 571 | return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE ); |
| 572 | |
| 573 | return ecp_use_known_dp( grp, curve_info->grp_id ); |
Manuel Pégourié-Gonnard | b325887 | 2013-02-10 12:06:19 +0100 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | /* |
| 577 | * Write the ECParameters record corresponding to a group (RFC 4492) |
| 578 | */ |
| 579 | int ecp_tls_write_group( const ecp_group *grp, size_t *olen, |
| 580 | unsigned char *buf, size_t blen ) |
| 581 | { |
Manuel Pégourié-Gonnard | f24b4a7 | 2013-09-23 18:14:50 +0200 | [diff] [blame] | 582 | const ecp_curve_info *curve_info; |
| 583 | |
| 584 | if( ( curve_info = ecp_curve_info_from_grp_id( grp->id ) ) == NULL ) |
| 585 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 7038039 | 2013-09-16 16:19:53 +0200 | [diff] [blame] | 586 | |
Manuel Pégourié-Gonnard | b325887 | 2013-02-10 12:06:19 +0100 | [diff] [blame] | 587 | /* |
| 588 | * We are going to write 3 bytes (see below) |
| 589 | */ |
| 590 | *olen = 3; |
| 591 | if( blen < *olen ) |
| 592 | return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL ); |
| 593 | |
| 594 | /* |
| 595 | * First byte is curve_type, always named_curve |
| 596 | */ |
| 597 | *buf++ = POLARSSL_ECP_TLS_NAMED_CURVE; |
| 598 | |
| 599 | /* |
| 600 | * Next two bytes are the namedcurve value |
| 601 | */ |
Manuel Pégourié-Gonnard | f24b4a7 | 2013-09-23 18:14:50 +0200 | [diff] [blame] | 602 | buf[0] = curve_info->tls_id >> 8; |
| 603 | buf[1] = curve_info->tls_id & 0xFF; |
Manuel Pégourié-Gonnard | b325887 | 2013-02-10 12:06:19 +0100 | [diff] [blame] | 604 | |
| 605 | return 0; |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 606 | } |
Manuel Pégourié-Gonnard | ab38b70 | 2012-11-05 17:34:55 +0100 | [diff] [blame] | 607 | |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 608 | /* |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 609 | * Wrapper around fast quasi-modp functions, with fall-back to mpi_mod_mpi. |
| 610 | * See the documentation of struct ecp_group. |
| 611 | * |
| 612 | * 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] | 613 | */ |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 614 | static int ecp_modp( mpi *N, const ecp_group *grp ) |
Manuel Pégourié-Gonnard | 7038039 | 2013-09-16 16:19:53 +0200 | [diff] [blame] | 615 | { |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 616 | int ret; |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 617 | |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 618 | if( grp->modp == NULL ) |
| 619 | return( mpi_mod_mpi( N, N, &grp->P ) ); |
| 620 | |
| 621 | /* N->s < 0 is a much faster test, which fails only if N is 0 */ |
| 622 | if( ( N->s < 0 && mpi_cmp_int( N, 0 ) != 0 ) || |
| 623 | mpi_msb( N ) > 2 * grp->pbits ) |
Manuel Pégourié-Gonnard | 7038039 | 2013-09-16 16:19:53 +0200 | [diff] [blame] | 624 | { |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 625 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 7038039 | 2013-09-16 16:19:53 +0200 | [diff] [blame] | 626 | } |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 627 | |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 628 | MPI_CHK( grp->modp( N ) ); |
Manuel Pégourié-Gonnard | 7038039 | 2013-09-16 16:19:53 +0200 | [diff] [blame] | 629 | |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 630 | /* N->s < 0 is a much faster test, which fails only if N is 0 */ |
| 631 | while( N->s < 0 && mpi_cmp_int( N, 0 ) != 0 ) |
| 632 | MPI_CHK( mpi_add_mpi( N, N, &grp->P ) ); |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 633 | |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 634 | while( mpi_cmp_mpi( N, &grp->P ) >= 0 ) |
| 635 | /* we known P, N and the result are positive */ |
| 636 | MPI_CHK( mpi_sub_abs( N, N, &grp->P ) ); |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 637 | |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 638 | cleanup: |
| 639 | return( ret ); |
Manuel Pégourié-Gonnard | 7038039 | 2013-09-16 16:19:53 +0200 | [diff] [blame] | 640 | } |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 641 | |
Manuel Pégourié-Gonnard | 847395a | 2012-11-05 13:13:44 +0100 | [diff] [blame] | 642 | /* |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 643 | * 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] | 644 | * |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 645 | * In order to guarantee that, we need to ensure that operands of |
| 646 | * 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] | 647 | * bring the result back to this range. |
| 648 | * |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 649 | * The following macros are shortcuts for doing that. |
Manuel Pégourié-Gonnard | dada4da | 2012-11-10 14:23:17 +0100 | [diff] [blame] | 650 | */ |
| 651 | |
| 652 | /* |
Manuel Pégourié-Gonnard | 84d1aea | 2012-11-09 02:09:38 +0100 | [diff] [blame] | 653 | * Reduce a mpi mod p in-place, general case, to use after mpi_mul_mpi |
| 654 | */ |
Manuel Pégourié-Gonnard | 9181481 | 2013-11-21 20:23:55 +0100 | [diff] [blame] | 655 | #if defined(POLARSSL_SELF_TEST) |
| 656 | #define INC_MUL_COUNT mul_count++; |
| 657 | #else |
| 658 | #define INC_MUL_COUNT |
| 659 | #endif |
| 660 | |
| 661 | #define MOD_MUL( N ) do { MPI_CHK( ecp_modp( &N, grp ) ); INC_MUL_COUNT } \ |
| 662 | while( 0 ) |
Manuel Pégourié-Gonnard | 84d1aea | 2012-11-09 02:09:38 +0100 | [diff] [blame] | 663 | |
| 664 | /* |
| 665 | * 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] | 666 | * 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] | 667 | */ |
| 668 | #define MOD_SUB( N ) \ |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 669 | while( N.s < 0 && mpi_cmp_int( &N, 0 ) != 0 ) \ |
Manuel Pégourié-Gonnard | 84d1aea | 2012-11-09 02:09:38 +0100 | [diff] [blame] | 670 | MPI_CHK( mpi_add_mpi( &N, &N, &grp->P ) ) |
| 671 | |
| 672 | /* |
Manuel Pégourié-Gonnard | c9e387c | 2013-10-17 17:15:35 +0200 | [diff] [blame] | 673 | * Reduce a mpi mod p in-place, to use after mpi_add_mpi and mpi_mul_int. |
| 674 | * We known P, N and the result are positive, so sub_abs is correct, and |
| 675 | * a bit faster. |
Manuel Pégourié-Gonnard | 84d1aea | 2012-11-09 02:09:38 +0100 | [diff] [blame] | 676 | */ |
| 677 | #define MOD_ADD( N ) \ |
| 678 | while( mpi_cmp_mpi( &N, &grp->P ) >= 0 ) \ |
Manuel Pégourié-Gonnard | c9e387c | 2013-10-17 17:15:35 +0200 | [diff] [blame] | 679 | MPI_CHK( mpi_sub_abs( &N, &N, &grp->P ) ) |
Manuel Pégourié-Gonnard | 84d1aea | 2012-11-09 02:09:38 +0100 | [diff] [blame] | 680 | |
Manuel Pégourié-Gonnard | 7c94d8b | 2013-12-04 23:15:46 +0100 | [diff] [blame] | 681 | #if defined(POLARSSL_ECP_SHORT_WEIERSTRASS) |
| 682 | /* |
| 683 | * For curves in short Weierstrass form, we do all the internal operations in |
| 684 | * Jacobian coordinates. |
| 685 | * |
| 686 | * For multiplication, we'll use a comb method with coutermeasueres against |
| 687 | * SPA, hence timing attacks. |
| 688 | */ |
| 689 | |
Manuel Pégourié-Gonnard | 84d1aea | 2012-11-09 02:09:38 +0100 | [diff] [blame] | 690 | /* |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 691 | * 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] | 692 | * Cost: 1N := 1I + 3M + 1S |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 693 | */ |
Manuel Pégourié-Gonnard | 3c0b4ea | 2013-12-02 19:44:41 +0100 | [diff] [blame] | 694 | static int ecp_normalize_jac( const ecp_group *grp, ecp_point *pt ) |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 695 | { |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 696 | int ret; |
Manuel Pégourié-Gonnard | cdd4432 | 2012-11-21 16:00:55 +0100 | [diff] [blame] | 697 | mpi Zi, ZZi; |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 698 | |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 699 | if( mpi_cmp_int( &pt->Z, 0 ) == 0 ) |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 700 | return( 0 ); |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 701 | |
Manuel Pégourié-Gonnard | cdd4432 | 2012-11-21 16:00:55 +0100 | [diff] [blame] | 702 | mpi_init( &Zi ); mpi_init( &ZZi ); |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 703 | |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 704 | /* |
| 705 | * X = X / Z^2 mod p |
| 706 | */ |
| 707 | MPI_CHK( mpi_inv_mod( &Zi, &pt->Z, &grp->P ) ); |
| 708 | MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi ); |
| 709 | 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] | 710 | |
| 711 | /* |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 712 | * Y = Y / Z^3 mod p |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 713 | */ |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 714 | MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ZZi ) ); MOD_MUL( pt->Y ); |
| 715 | 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] | 716 | |
| 717 | /* |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 718 | * Z = 1 |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 719 | */ |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 720 | MPI_CHK( mpi_lset( &pt->Z, 1 ) ); |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 721 | |
| 722 | cleanup: |
| 723 | |
Manuel Pégourié-Gonnard | cdd4432 | 2012-11-21 16:00:55 +0100 | [diff] [blame] | 724 | mpi_free( &Zi ); mpi_free( &ZZi ); |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 725 | |
| 726 | return( ret ); |
| 727 | } |
| 728 | |
| 729 | /* |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 730 | * Normalize jacobian coordinates of an array of (pointers to) points, |
Manuel Pégourié-Gonnard | 3680c82 | 2012-11-21 18:49:45 +0100 | [diff] [blame] | 731 | * 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] | 732 | * (See for example Cohen's "A Course in Computational Algebraic Number |
| 733 | * Theory", Algorithm 10.3.4.) |
| 734 | * |
Manuel Pégourié-Gonnard | 07de4b1 | 2013-09-02 16:26:04 +0200 | [diff] [blame] | 735 | * Warning: fails (returning an error) if one of the points is zero! |
Manuel Pégourié-Gonnard | 7a949d3 | 2013-12-05 10:26:01 +0100 | [diff] [blame] | 736 | * This should never happen, see choice of w in ecp_mul_comb(). |
Manuel Pégourié-Gonnard | 04a0225 | 2013-11-20 22:57:38 +0100 | [diff] [blame] | 737 | * |
| 738 | * Cost: 1N(t) := 1I + (6t - 3)M + 1S |
Manuel Pégourié-Gonnard | cdd4432 | 2012-11-21 16:00:55 +0100 | [diff] [blame] | 739 | */ |
Manuel Pégourié-Gonnard | 3c0b4ea | 2013-12-02 19:44:41 +0100 | [diff] [blame] | 740 | static int ecp_normalize_jac_many( const ecp_group *grp, |
Manuel Pégourié-Gonnard | 7c94d8b | 2013-12-04 23:15:46 +0100 | [diff] [blame] | 741 | ecp_point *T[], size_t t_len ) |
Manuel Pégourié-Gonnard | cdd4432 | 2012-11-21 16:00:55 +0100 | [diff] [blame] | 742 | { |
| 743 | int ret; |
| 744 | size_t i; |
| 745 | mpi *c, u, Zi, ZZi; |
| 746 | |
| 747 | if( t_len < 2 ) |
Manuel Pégourié-Gonnard | 3c0b4ea | 2013-12-02 19:44:41 +0100 | [diff] [blame] | 748 | return( ecp_normalize_jac( grp, *T ) ); |
Manuel Pégourié-Gonnard | cdd4432 | 2012-11-21 16:00:55 +0100 | [diff] [blame] | 749 | |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 750 | if( ( c = (mpi *) polarssl_malloc( t_len * sizeof( mpi ) ) ) == NULL ) |
Manuel Pégourié-Gonnard | 456d3b9 | 2013-09-16 18:04:38 +0200 | [diff] [blame] | 751 | return( POLARSSL_ERR_ECP_MALLOC_FAILED ); |
Manuel Pégourié-Gonnard | cdd4432 | 2012-11-21 16:00:55 +0100 | [diff] [blame] | 752 | |
| 753 | mpi_init( &u ); mpi_init( &Zi ); mpi_init( &ZZi ); |
| 754 | for( i = 0; i < t_len; i++ ) |
| 755 | mpi_init( &c[i] ); |
| 756 | |
| 757 | /* |
| 758 | * c[i] = Z_0 * ... * Z_i |
| 759 | */ |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 760 | MPI_CHK( mpi_copy( &c[0], &T[0]->Z ) ); |
Manuel Pégourié-Gonnard | cdd4432 | 2012-11-21 16:00:55 +0100 | [diff] [blame] | 761 | for( i = 1; i < t_len; i++ ) |
| 762 | { |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 763 | 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] | 764 | MOD_MUL( c[i] ); |
| 765 | } |
| 766 | |
| 767 | /* |
| 768 | * u = 1 / (Z_0 * ... * Z_n) mod P |
| 769 | */ |
| 770 | MPI_CHK( mpi_inv_mod( &u, &c[t_len-1], &grp->P ) ); |
| 771 | |
| 772 | for( i = t_len - 1; ; i-- ) |
| 773 | { |
| 774 | /* |
| 775 | * Zi = 1 / Z_i mod p |
| 776 | * u = 1 / (Z_0 * ... * Z_i) mod P |
| 777 | */ |
| 778 | if( i == 0 ) { |
| 779 | MPI_CHK( mpi_copy( &Zi, &u ) ); |
| 780 | } |
| 781 | else |
| 782 | { |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 783 | MPI_CHK( mpi_mul_mpi( &Zi, &u, &c[i-1] ) ); MOD_MUL( Zi ); |
| 784 | 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] | 785 | } |
| 786 | |
| 787 | /* |
| 788 | * proceed as in normalize() |
| 789 | */ |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 790 | MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi ); |
| 791 | MPI_CHK( mpi_mul_mpi( &T[i]->X, &T[i]->X, &ZZi ) ); MOD_MUL( T[i]->X ); |
| 792 | MPI_CHK( mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &ZZi ) ); MOD_MUL( T[i]->Y ); |
| 793 | MPI_CHK( mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &Zi ) ); MOD_MUL( T[i]->Y ); |
| 794 | MPI_CHK( mpi_lset( &T[i]->Z, 1 ) ); |
Manuel Pégourié-Gonnard | cdd4432 | 2012-11-21 16:00:55 +0100 | [diff] [blame] | 795 | |
| 796 | if( i == 0 ) |
| 797 | break; |
| 798 | } |
| 799 | |
| 800 | cleanup: |
| 801 | |
| 802 | mpi_free( &u ); mpi_free( &Zi ); mpi_free( &ZZi ); |
| 803 | for( i = 0; i < t_len; i++ ) |
| 804 | mpi_free( &c[i] ); |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 805 | polarssl_free( c ); |
Manuel Pégourié-Gonnard | cdd4432 | 2012-11-21 16:00:55 +0100 | [diff] [blame] | 806 | |
| 807 | return( ret ); |
| 808 | } |
| 809 | |
Manuel Pégourié-Gonnard | cdd4432 | 2012-11-21 16:00:55 +0100 | [diff] [blame] | 810 | /* |
Manuel Pégourié-Gonnard | 01fca5e | 2013-11-21 17:47:12 +0100 | [diff] [blame] | 811 | * Conditional point inversion: Q -> -Q = (Q.X, -Q.Y, Q.Z) without leak. |
| 812 | * "inv" must be 0 (don't invert) or 1 (invert) or the result will be invalid |
| 813 | */ |
Manuel Pégourié-Gonnard | 3c0b4ea | 2013-12-02 19:44:41 +0100 | [diff] [blame] | 814 | static int ecp_safe_invert_jac( const ecp_group *grp, |
Manuel Pégourié-Gonnard | 01fca5e | 2013-11-21 17:47:12 +0100 | [diff] [blame] | 815 | ecp_point *Q, |
| 816 | unsigned char inv ) |
| 817 | { |
| 818 | int ret; |
| 819 | unsigned char nonzero; |
| 820 | mpi mQY; |
| 821 | |
| 822 | mpi_init( &mQY ); |
| 823 | |
| 824 | /* Use the fact that -Q.Y mod P = P - Q.Y unless Q.Y == 0 */ |
| 825 | MPI_CHK( mpi_sub_mpi( &mQY, &grp->P, &Q->Y ) ); |
| 826 | nonzero = mpi_cmp_int( &Q->Y, 0 ) != 0; |
| 827 | MPI_CHK( mpi_safe_cond_assign( &Q->Y, &mQY, inv & nonzero ) ); |
| 828 | |
| 829 | cleanup: |
| 830 | mpi_free( &mQY ); |
| 831 | |
| 832 | return( ret ); |
| 833 | } |
| 834 | |
| 835 | /* |
Manuel Pégourié-Gonnard | 0cd6f98 | 2013-10-10 15:55:39 +0200 | [diff] [blame] | 836 | * Point doubling R = 2 P, Jacobian coordinates |
Manuel Pégourié-Gonnard | 0ace4b3 | 2013-10-10 12:44:27 +0200 | [diff] [blame] | 837 | * |
Manuel Pégourié-Gonnard | 1c4aa24 | 2013-10-09 16:09:46 +0200 | [diff] [blame] | 838 | * 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] | 839 | * with heavy variable renaming, some reordering and one minor modification |
| 840 | * (a = 2 * b, c = d - 2a replaced with c = d, c = c - b, c = c - b) |
| 841 | * 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] | 842 | * |
| 843 | * Cost: 1D := 2M + 8S |
Manuel Pégourié-Gonnard | 1c4aa24 | 2013-10-09 16:09:46 +0200 | [diff] [blame] | 844 | */ |
Manuel Pégourié-Gonnard | 0cd6f98 | 2013-10-10 15:55:39 +0200 | [diff] [blame] | 845 | static int ecp_double_jac( const ecp_group *grp, ecp_point *R, |
| 846 | const ecp_point *P ) |
Manuel Pégourié-Gonnard | 1c4aa24 | 2013-10-09 16:09:46 +0200 | [diff] [blame] | 847 | { |
| 848 | int ret; |
Manuel Pégourié-Gonnard | 0ace4b3 | 2013-10-10 12:44:27 +0200 | [diff] [blame] | 849 | mpi T1, T2, T3, X3, Y3, Z3; |
Manuel Pégourié-Gonnard | 1c4aa24 | 2013-10-09 16:09:46 +0200 | [diff] [blame] | 850 | |
Manuel Pégourié-Gonnard | 0cd6f98 | 2013-10-10 15:55:39 +0200 | [diff] [blame] | 851 | #if defined(POLARSSL_SELF_TEST) |
| 852 | dbl_count++; |
| 853 | #endif |
| 854 | |
Manuel Pégourié-Gonnard | 0ace4b3 | 2013-10-10 12:44:27 +0200 | [diff] [blame] | 855 | mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 ); |
| 856 | mpi_init( &X3 ); mpi_init( &Y3 ); mpi_init( &Z3 ); |
Manuel Pégourié-Gonnard | 1c4aa24 | 2013-10-09 16:09:46 +0200 | [diff] [blame] | 857 | |
Manuel Pégourié-Gonnard | 0ace4b3 | 2013-10-10 12:44:27 +0200 | [diff] [blame] | 858 | MPI_CHK( mpi_mul_mpi( &T3, &P->X, &P->X ) ); MOD_MUL( T3 ); |
| 859 | MPI_CHK( mpi_mul_mpi( &T2, &P->Y, &P->Y ) ); MOD_MUL( T2 ); |
| 860 | MPI_CHK( mpi_mul_mpi( &Y3, &T2, &T2 ) ); MOD_MUL( Y3 ); |
| 861 | MPI_CHK( mpi_add_mpi( &X3, &P->X, &T2 ) ); MOD_ADD( X3 ); |
| 862 | MPI_CHK( mpi_mul_mpi( &X3, &X3, &X3 ) ); MOD_MUL( X3 ); |
| 863 | MPI_CHK( mpi_sub_mpi( &X3, &X3, &Y3 ) ); MOD_SUB( X3 ); |
| 864 | MPI_CHK( mpi_sub_mpi( &X3, &X3, &T3 ) ); MOD_SUB( X3 ); |
| 865 | MPI_CHK( mpi_mul_int( &T1, &X3, 2 ) ); MOD_ADD( T1 ); |
| 866 | MPI_CHK( mpi_mul_mpi( &Z3, &P->Z, &P->Z ) ); MOD_MUL( Z3 ); |
| 867 | MPI_CHK( mpi_mul_mpi( &X3, &Z3, &Z3 ) ); MOD_MUL( X3 ); |
| 868 | MPI_CHK( mpi_mul_int( &T3, &T3, 3 ) ); MOD_ADD( T3 ); |
Manuel Pégourié-Gonnard | 73cc01d | 2013-12-06 12:41:30 +0100 | [diff] [blame] | 869 | |
| 870 | /* Special case for A = -3 */ |
| 871 | if( grp->A.p == NULL ) |
| 872 | { |
| 873 | MPI_CHK( mpi_mul_int( &X3, &X3, 3 ) ); |
| 874 | X3.s = -1; /* mpi_mul_int doesn't handle negative numbers */ |
| 875 | MOD_SUB( X3 ); |
| 876 | } |
| 877 | else |
| 878 | MPI_CHK( mpi_mul_mpi( &X3, &X3, &grp->A ) ); MOD_MUL( X3 ); |
| 879 | |
Manuel Pégourié-Gonnard | 0ace4b3 | 2013-10-10 12:44:27 +0200 | [diff] [blame] | 880 | MPI_CHK( mpi_add_mpi( &T3, &T3, &X3 ) ); MOD_ADD( T3 ); |
| 881 | MPI_CHK( mpi_mul_mpi( &X3, &T3, &T3 ) ); MOD_MUL( X3 ); |
| 882 | MPI_CHK( mpi_sub_mpi( &X3, &X3, &T1 ) ); MOD_SUB( X3 ); |
| 883 | MPI_CHK( mpi_sub_mpi( &X3, &X3, &T1 ) ); MOD_SUB( X3 ); |
| 884 | MPI_CHK( mpi_sub_mpi( &T1, &T1, &X3 ) ); MOD_SUB( T1 ); |
| 885 | MPI_CHK( mpi_mul_mpi( &T1, &T3, &T1 ) ); MOD_MUL( T1 ); |
| 886 | MPI_CHK( mpi_mul_int( &T3, &Y3, 8 ) ); MOD_ADD( T3 ); |
| 887 | MPI_CHK( mpi_sub_mpi( &Y3, &T1, &T3 ) ); MOD_SUB( Y3 ); |
| 888 | MPI_CHK( mpi_add_mpi( &T1, &P->Y, &P->Z ) ); MOD_ADD( T1 ); |
| 889 | MPI_CHK( mpi_mul_mpi( &T1, &T1, &T1 ) ); MOD_MUL( T1 ); |
| 890 | MPI_CHK( mpi_sub_mpi( &T1, &T1, &T2 ) ); MOD_SUB( T1 ); |
| 891 | 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] | 892 | |
| 893 | MPI_CHK( mpi_copy( &R->X, &X3 ) ); |
| 894 | MPI_CHK( mpi_copy( &R->Y, &Y3 ) ); |
| 895 | MPI_CHK( mpi_copy( &R->Z, &Z3 ) ); |
| 896 | |
| 897 | cleanup: |
Manuel Pégourié-Gonnard | 0ace4b3 | 2013-10-10 12:44:27 +0200 | [diff] [blame] | 898 | mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 ); |
| 899 | mpi_free( &X3 ); mpi_free( &Y3 ); mpi_free( &Z3 ); |
Manuel Pégourié-Gonnard | 1c4aa24 | 2013-10-09 16:09:46 +0200 | [diff] [blame] | 900 | |
| 901 | return( ret ); |
| 902 | } |
| 903 | |
| 904 | /* |
Manuel Pégourié-Gonnard | 469a209 | 2013-11-21 18:20:43 +0100 | [diff] [blame] | 905 | * Addition: R = P + Q, mixed affine-Jacobian coordinates (GECC 3.22) |
Manuel Pégourié-Gonnard | 9674fd0 | 2012-11-19 21:23:27 +0100 | [diff] [blame] | 906 | * |
| 907 | * The coordinates of Q must be normalized (= affine), |
| 908 | * but those of P don't need to. R is not normalized. |
| 909 | * |
Manuel Pégourié-Gonnard | aade42f | 2013-11-21 19:19:54 +0100 | [diff] [blame] | 910 | * Special cases: (1) P or Q is zero, (2) R is zero, (3) P == Q. |
Manuel Pégourié-Gonnard | 7a949d3 | 2013-12-05 10:26:01 +0100 | [diff] [blame] | 911 | * None of these cases can happen as intermediate step in ecp_mul_comb(): |
Manuel Pégourié-Gonnard | aade42f | 2013-11-21 19:19:54 +0100 | [diff] [blame] | 912 | * - at each step, P, Q and R are multiples of the base point, the factor |
| 913 | * being less than its order, so none of them is zero; |
| 914 | * - Q is an odd multiple of the base point, P an even multiple, |
| 915 | * due to the choice of precomputed points in the modified comb method. |
| 916 | * So branches for these cases do not leak secret information. |
| 917 | * |
Manuel Pégourié-Gonnard | 72c172a | 2013-12-30 16:04:55 +0100 | [diff] [blame^] | 918 | * We accept Q->Z being unset (saving memory in tables) as meaning 1. |
| 919 | * |
Manuel Pégourié-Gonnard | 04a0225 | 2013-11-20 22:57:38 +0100 | [diff] [blame] | 920 | * Cost: 1A := 8M + 3S |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 921 | */ |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 922 | static int ecp_add_mixed( const ecp_group *grp, ecp_point *R, |
Manuel Pégourié-Gonnard | 469a209 | 2013-11-21 18:20:43 +0100 | [diff] [blame] | 923 | const ecp_point *P, const ecp_point *Q ) |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 924 | { |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 925 | int ret; |
Manuel Pégourié-Gonnard | 7e0adfb | 2012-11-08 23:21:46 +0100 | [diff] [blame] | 926 | mpi T1, T2, T3, T4, X, Y, Z; |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 927 | |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 928 | #if defined(POLARSSL_SELF_TEST) |
| 929 | add_count++; |
| 930 | #endif |
| 931 | |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 932 | /* |
Manuel Pégourié-Gonnard | aade42f | 2013-11-21 19:19:54 +0100 | [diff] [blame] | 933 | * Trivial cases: P == 0 or Q == 0 (case 1) |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 934 | */ |
Manuel Pégourié-Gonnard | 469a209 | 2013-11-21 18:20:43 +0100 | [diff] [blame] | 935 | if( mpi_cmp_int( &P->Z, 0 ) == 0 ) |
| 936 | return( ecp_copy( R, Q ) ); |
| 937 | |
Manuel Pégourié-Gonnard | 72c172a | 2013-12-30 16:04:55 +0100 | [diff] [blame^] | 938 | if( Q->Z.p != NULL && mpi_cmp_int( &Q->Z, 0 ) == 0 ) |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 939 | return( ecp_copy( R, P ) ); |
| 940 | |
| 941 | /* |
| 942 | * Make sure Q coordinates are normalized |
| 943 | */ |
Manuel Pégourié-Gonnard | 72c172a | 2013-12-30 16:04:55 +0100 | [diff] [blame^] | 944 | if( Q->Z.p != NULL && mpi_cmp_int( &Q->Z, 1 ) != 0 ) |
Manuel Pégourié-Gonnard | 456d3b9 | 2013-09-16 18:04:38 +0200 | [diff] [blame] | 945 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 946 | |
Manuel Pégourié-Gonnard | 7e0adfb | 2012-11-08 23:21:46 +0100 | [diff] [blame] | 947 | mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 ); mpi_init( &T4 ); |
| 948 | mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z ); |
Manuel Pégourié-Gonnard | ab38b70 | 2012-11-05 17:34:55 +0100 | [diff] [blame] | 949 | |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 950 | MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 ); |
| 951 | MPI_CHK( mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 ); |
| 952 | MPI_CHK( mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 ); |
| 953 | MPI_CHK( mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 ); |
| 954 | MPI_CHK( mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 ); |
| 955 | 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] | 956 | |
Manuel Pégourié-Gonnard | aade42f | 2013-11-21 19:19:54 +0100 | [diff] [blame] | 957 | /* Special cases (2) and (3) */ |
Manuel Pégourié-Gonnard | 7e0adfb | 2012-11-08 23:21:46 +0100 | [diff] [blame] | 958 | if( mpi_cmp_int( &T1, 0 ) == 0 ) |
| 959 | { |
| 960 | if( mpi_cmp_int( &T2, 0 ) == 0 ) |
| 961 | { |
| 962 | ret = ecp_double_jac( grp, R, P ); |
| 963 | goto cleanup; |
| 964 | } |
| 965 | else |
| 966 | { |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 967 | ret = ecp_set_zero( R ); |
Manuel Pégourié-Gonnard | 7e0adfb | 2012-11-08 23:21:46 +0100 | [diff] [blame] | 968 | goto cleanup; |
| 969 | } |
| 970 | } |
| 971 | |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 972 | MPI_CHK( mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z ); |
| 973 | MPI_CHK( mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 ); |
| 974 | MPI_CHK( mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 ); |
| 975 | MPI_CHK( mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 ); |
| 976 | MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 ); |
| 977 | MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X ); |
| 978 | MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X ); |
| 979 | MPI_CHK( mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X ); |
| 980 | MPI_CHK( mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 ); |
| 981 | MPI_CHK( mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 ); |
| 982 | MPI_CHK( mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 ); |
| 983 | 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] | 984 | |
Manuel Pégourié-Gonnard | 84d1aea | 2012-11-09 02:09:38 +0100 | [diff] [blame] | 985 | MPI_CHK( mpi_copy( &R->X, &X ) ); |
| 986 | MPI_CHK( mpi_copy( &R->Y, &Y ) ); |
| 987 | MPI_CHK( mpi_copy( &R->Z, &Z ) ); |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 988 | |
| 989 | cleanup: |
| 990 | |
Manuel Pégourié-Gonnard | 7e0adfb | 2012-11-08 23:21:46 +0100 | [diff] [blame] | 991 | mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 ); mpi_free( &T4 ); |
| 992 | mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z ); |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 993 | |
| 994 | return( ret ); |
| 995 | } |
| 996 | |
| 997 | /* |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 998 | * Addition: R = P + Q, result's coordinates normalized |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 999 | */ |
| 1000 | int ecp_add( const ecp_group *grp, ecp_point *R, |
| 1001 | const ecp_point *P, const ecp_point *Q ) |
| 1002 | { |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 1003 | int ret; |
Manuel Pégourié-Gonnard | 989c32b | 2012-11-08 22:02:42 +0100 | [diff] [blame] | 1004 | |
Manuel Pégourié-Gonnard | 7c94d8b | 2013-12-04 23:15:46 +0100 | [diff] [blame] | 1005 | if( ecp_get_type( grp ) != POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS ) |
Manuel Pégourié-Gonnard | 97871ef | 2013-12-04 20:52:04 +0100 | [diff] [blame] | 1006 | return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE ); |
| 1007 | |
Manuel Pégourié-Gonnard | 469a209 | 2013-11-21 18:20:43 +0100 | [diff] [blame] | 1008 | MPI_CHK( ecp_add_mixed( grp, R, P, Q ) ); |
Manuel Pégourié-Gonnard | 3c0b4ea | 2013-12-02 19:44:41 +0100 | [diff] [blame] | 1009 | MPI_CHK( ecp_normalize_jac( grp, R ) ); |
Manuel Pégourié-Gonnard | 9674fd0 | 2012-11-19 21:23:27 +0100 | [diff] [blame] | 1010 | |
| 1011 | cleanup: |
| 1012 | return( ret ); |
| 1013 | } |
| 1014 | |
| 1015 | /* |
| 1016 | * Subtraction: R = P - Q, result's coordinates normalized |
| 1017 | */ |
| 1018 | int ecp_sub( const ecp_group *grp, ecp_point *R, |
| 1019 | const ecp_point *P, const ecp_point *Q ) |
| 1020 | { |
| 1021 | int ret; |
Manuel Pégourié-Gonnard | 469a209 | 2013-11-21 18:20:43 +0100 | [diff] [blame] | 1022 | ecp_point mQ; |
Manuel Pégourié-Gonnard | 9674fd0 | 2012-11-19 21:23:27 +0100 | [diff] [blame] | 1023 | |
Manuel Pégourié-Gonnard | 469a209 | 2013-11-21 18:20:43 +0100 | [diff] [blame] | 1024 | ecp_point_init( &mQ ); |
| 1025 | |
Manuel Pégourié-Gonnard | 7c94d8b | 2013-12-04 23:15:46 +0100 | [diff] [blame] | 1026 | if( ecp_get_type( grp ) != POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS ) |
Manuel Pégourié-Gonnard | 97871ef | 2013-12-04 20:52:04 +0100 | [diff] [blame] | 1027 | return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE ); |
| 1028 | |
Manuel Pégourié-Gonnard | 469a209 | 2013-11-21 18:20:43 +0100 | [diff] [blame] | 1029 | /* mQ = - Q */ |
| 1030 | ecp_copy( &mQ, Q ); |
| 1031 | if( mpi_cmp_int( &mQ.Y, 0 ) != 0 ) |
| 1032 | MPI_CHK( mpi_sub_mpi( &mQ.Y, &grp->P, &mQ.Y ) ); |
| 1033 | |
| 1034 | MPI_CHK( ecp_add_mixed( grp, R, P, &mQ ) ); |
Manuel Pégourié-Gonnard | 3c0b4ea | 2013-12-02 19:44:41 +0100 | [diff] [blame] | 1035 | MPI_CHK( ecp_normalize_jac( grp, R ) ); |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 1036 | |
Manuel Pégourié-Gonnard | 989c32b | 2012-11-08 22:02:42 +0100 | [diff] [blame] | 1037 | cleanup: |
Manuel Pégourié-Gonnard | 469a209 | 2013-11-21 18:20:43 +0100 | [diff] [blame] | 1038 | ecp_point_free( &mQ ); |
| 1039 | |
Manuel Pégourié-Gonnard | 7e0adfb | 2012-11-08 23:21:46 +0100 | [diff] [blame] | 1040 | return( ret ); |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 1041 | } |
| 1042 | |
Manuel Pégourié-Gonnard | efaa31e | 2012-11-06 21:34:35 +0100 | [diff] [blame] | 1043 | /* |
Manuel Pégourié-Gonnard | 07de4b1 | 2013-09-02 16:26:04 +0200 | [diff] [blame] | 1044 | * Randomize jacobian coordinates: |
| 1045 | * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l |
Manuel Pégourié-Gonnard | 3c0b4ea | 2013-12-02 19:44:41 +0100 | [diff] [blame] | 1046 | * This is sort of the reverse operation of ecp_normalize_jac(). |
Manuel Pégourié-Gonnard | 44aab79 | 2013-11-21 10:53:59 +0100 | [diff] [blame] | 1047 | * |
| 1048 | * This countermeasure was first suggested in [2]. |
Manuel Pégourié-Gonnard | 07de4b1 | 2013-09-02 16:26:04 +0200 | [diff] [blame] | 1049 | */ |
Manuel Pégourié-Gonnard | 3afa07f | 2013-12-03 13:28:21 +0100 | [diff] [blame] | 1050 | static int ecp_randomize_jac( const ecp_group *grp, ecp_point *pt, |
Manuel Pégourié-Gonnard | 07de4b1 | 2013-09-02 16:26:04 +0200 | [diff] [blame] | 1051 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 1052 | { |
| 1053 | int ret; |
| 1054 | mpi l, ll; |
| 1055 | size_t p_size = (grp->pbits + 7) / 8; |
| 1056 | int count = 0; |
| 1057 | |
| 1058 | mpi_init( &l ); mpi_init( &ll ); |
| 1059 | |
| 1060 | /* Generate l such that 1 < l < p */ |
| 1061 | do |
| 1062 | { |
| 1063 | mpi_fill_random( &l, p_size, f_rng, p_rng ); |
| 1064 | |
| 1065 | while( mpi_cmp_mpi( &l, &grp->P ) >= 0 ) |
| 1066 | mpi_shift_r( &l, 1 ); |
| 1067 | |
| 1068 | if( count++ > 10 ) |
Manuel Pégourié-Gonnard | 456d3b9 | 2013-09-16 18:04:38 +0200 | [diff] [blame] | 1069 | return( POLARSSL_ERR_ECP_RANDOM_FAILED ); |
Manuel Pégourié-Gonnard | 07de4b1 | 2013-09-02 16:26:04 +0200 | [diff] [blame] | 1070 | } |
| 1071 | while( mpi_cmp_int( &l, 1 ) <= 0 ); |
| 1072 | |
| 1073 | /* Z = l * Z */ |
| 1074 | MPI_CHK( mpi_mul_mpi( &pt->Z, &pt->Z, &l ) ); MOD_MUL( pt->Z ); |
| 1075 | |
| 1076 | /* X = l^2 * X */ |
| 1077 | MPI_CHK( mpi_mul_mpi( &ll, &l, &l ) ); MOD_MUL( ll ); |
| 1078 | MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ll ) ); MOD_MUL( pt->X ); |
| 1079 | |
| 1080 | /* Y = l^3 * Y */ |
| 1081 | MPI_CHK( mpi_mul_mpi( &ll, &ll, &l ) ); MOD_MUL( ll ); |
| 1082 | MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ll ) ); MOD_MUL( pt->Y ); |
| 1083 | |
| 1084 | cleanup: |
| 1085 | mpi_free( &l ); mpi_free( &ll ); |
| 1086 | |
| 1087 | return( ret ); |
| 1088 | } |
| 1089 | |
| 1090 | /* |
Manuel Pégourié-Gonnard | c30200e | 2013-11-20 18:39:55 +0100 | [diff] [blame] | 1091 | * Check and define parameters used by the comb method (see below for details) |
| 1092 | */ |
| 1093 | #if POLARSSL_ECP_WINDOW_SIZE < 2 || POLARSSL_ECP_WINDOW_SIZE > 7 |
| 1094 | #error "POLARSSL_ECP_WINDOW_SIZE out of bounds" |
| 1095 | #endif |
| 1096 | |
| 1097 | /* d = ceil( n / w ) */ |
| 1098 | #define COMB_MAX_D ( POLARSSL_ECP_MAX_BITS + 1 ) / 2 |
| 1099 | |
| 1100 | /* number of precomputed points */ |
| 1101 | #define COMB_MAX_PRE ( 1 << ( POLARSSL_ECP_WINDOW_SIZE - 1 ) ) |
| 1102 | |
| 1103 | /* |
| 1104 | * 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] | 1105 | * |
| 1106 | * 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] | 1107 | * modified version that provides resistance to SPA by avoiding zero |
| 1108 | * digits in the representation as in [3]. We modify the method further by |
| 1109 | * 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] | 1110 | * representation uses one more K_i, due to carries. |
Manuel Pégourié-Gonnard | 101a39f | 2013-11-20 14:47:19 +0100 | [diff] [blame] | 1111 | * |
| 1112 | * Also, for the sake of compactness, only the seven low-order bits of x[i] |
| 1113 | * are used to represent K_i, and the msb of x[i] encodes the the sign (s_i in |
| 1114 | * 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] | 1115 | * |
| 1116 | * Calling conventions: |
Manuel Pégourié-Gonnard | 101a39f | 2013-11-20 14:47:19 +0100 | [diff] [blame] | 1117 | * - x is an array of size d + 1 |
Manuel Pégourié-Gonnard | c30200e | 2013-11-20 18:39:55 +0100 | [diff] [blame] | 1118 | * - w is the size, ie number of teeth, of the comb, and must be between |
| 1119 | * 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] | 1120 | * - m is the MPI, expected to be odd and such that bitlength(m) <= w * d |
| 1121 | * (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] | 1122 | */ |
Manuel Pégourié-Gonnard | 101a39f | 2013-11-20 14:47:19 +0100 | [diff] [blame] | 1123 | 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] | 1124 | unsigned char w, const mpi *m ) |
| 1125 | { |
| 1126 | size_t i, j; |
Manuel Pégourié-Gonnard | 101a39f | 2013-11-20 14:47:19 +0100 | [diff] [blame] | 1127 | unsigned char c, cc, adjust; |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1128 | |
Manuel Pégourié-Gonnard | 101a39f | 2013-11-20 14:47:19 +0100 | [diff] [blame] | 1129 | memset( x, 0, d+1 ); |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1130 | |
Manuel Pégourié-Gonnard | edc1a1f | 2013-11-21 09:50:00 +0100 | [diff] [blame] | 1131 | /* First get the classical comb values (except for x_d = 0) */ |
| 1132 | for( i = 0; i < d; i++ ) |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1133 | for( j = 0; j < w; j++ ) |
| 1134 | x[i] |= mpi_get_bit( m, i + d * j ) << j; |
| 1135 | |
Manuel Pégourié-Gonnard | edc1a1f | 2013-11-21 09:50:00 +0100 | [diff] [blame] | 1136 | /* Now make sure x_1 .. x_d are odd */ |
| 1137 | c = 0; |
| 1138 | for( i = 1; i <= d; i++ ) |
| 1139 | { |
Manuel Pégourié-Gonnard | 101a39f | 2013-11-20 14:47:19 +0100 | [diff] [blame] | 1140 | /* Add carry and update it */ |
| 1141 | cc = x[i] & c; |
| 1142 | x[i] = x[i] ^ c; |
| 1143 | c = cc; |
| 1144 | |
Manuel Pégourié-Gonnard | edc1a1f | 2013-11-21 09:50:00 +0100 | [diff] [blame] | 1145 | /* Adjust if needed, avoiding branches */ |
Manuel Pégourié-Gonnard | 101a39f | 2013-11-20 14:47:19 +0100 | [diff] [blame] | 1146 | adjust = 1 - ( x[i] & 0x01 ); |
| 1147 | c |= x[i] & ( x[i-1] * adjust ); |
| 1148 | x[i] = x[i] ^ ( x[i-1] * adjust ); |
| 1149 | x[i-1] |= adjust << 7; |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1150 | } |
| 1151 | } |
| 1152 | |
| 1153 | /* |
| 1154 | * Precompute points for the comb method |
| 1155 | * |
Manuel Pégourié-Gonnard | 101a39f | 2013-11-20 14:47:19 +0100 | [diff] [blame] | 1156 | * If i = i_{w-1} ... i_1 is the binary representation of i, then |
| 1157 | * 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] | 1158 | * |
Manuel Pégourié-Gonnard | 04a0225 | 2013-11-20 22:57:38 +0100 | [diff] [blame] | 1159 | * T must be able to hold 2^{w - 1} elements |
| 1160 | * |
| 1161 | * 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] | 1162 | */ |
| 1163 | static int ecp_precompute_comb( const ecp_group *grp, |
| 1164 | ecp_point T[], const ecp_point *P, |
| 1165 | unsigned char w, size_t d ) |
| 1166 | { |
| 1167 | int ret; |
Manuel Pégourié-Gonnard | c30200e | 2013-11-20 18:39:55 +0100 | [diff] [blame] | 1168 | unsigned char i, k; |
| 1169 | size_t j; |
| 1170 | ecp_point *cur, *TT[COMB_MAX_PRE - 1]; |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1171 | |
| 1172 | /* |
Manuel Pégourié-Gonnard | 101a39f | 2013-11-20 14:47:19 +0100 | [diff] [blame] | 1173 | * Set T[0] = P and |
| 1174 | * 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] | 1175 | */ |
| 1176 | MPI_CHK( ecp_copy( &T[0], P ) ); |
| 1177 | |
Manuel Pégourié-Gonnard | 101a39f | 2013-11-20 14:47:19 +0100 | [diff] [blame] | 1178 | k = 0; |
| 1179 | for( i = 1; i < ( 1U << (w-1) ); i <<= 1 ) |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1180 | { |
Manuel Pégourié-Gonnard | 101a39f | 2013-11-20 14:47:19 +0100 | [diff] [blame] | 1181 | cur = T + i; |
| 1182 | MPI_CHK( ecp_copy( cur, T + ( i >> 1 ) ) ); |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1183 | for( j = 0; j < d; j++ ) |
| 1184 | MPI_CHK( ecp_double_jac( grp, cur, cur ) ); |
Manuel Pégourié-Gonnard | 101a39f | 2013-11-20 14:47:19 +0100 | [diff] [blame] | 1185 | |
| 1186 | TT[k++] = cur; |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1187 | } |
| 1188 | |
Manuel Pégourié-Gonnard | 3c0b4ea | 2013-12-02 19:44:41 +0100 | [diff] [blame] | 1189 | ecp_normalize_jac_many( grp, TT, k ); |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1190 | |
| 1191 | /* |
| 1192 | * Compute the remaining ones using the minimal number of additions |
Manuel Pégourié-Gonnard | 101a39f | 2013-11-20 14:47:19 +0100 | [diff] [blame] | 1193 | * 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] | 1194 | */ |
Manuel Pégourié-Gonnard | 101a39f | 2013-11-20 14:47:19 +0100 | [diff] [blame] | 1195 | k = 0; |
| 1196 | for( i = 1; i < ( 1U << (w-1) ); i <<= 1 ) |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1197 | { |
Manuel Pégourié-Gonnard | 101a39f | 2013-11-20 14:47:19 +0100 | [diff] [blame] | 1198 | j = i; |
| 1199 | while( j-- ) |
| 1200 | { |
Manuel Pégourié-Gonnard | 469a209 | 2013-11-21 18:20:43 +0100 | [diff] [blame] | 1201 | ecp_add_mixed( grp, &T[i + j], &T[j], &T[i] ); |
Manuel Pégourié-Gonnard | 101a39f | 2013-11-20 14:47:19 +0100 | [diff] [blame] | 1202 | TT[k++] = &T[i + j]; |
| 1203 | } |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1204 | } |
| 1205 | |
Manuel Pégourié-Gonnard | 3c0b4ea | 2013-12-02 19:44:41 +0100 | [diff] [blame] | 1206 | ecp_normalize_jac_many( grp, TT, k ); |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1207 | |
Manuel Pégourié-Gonnard | e282012 | 2013-11-21 10:08:50 +0100 | [diff] [blame] | 1208 | /* |
Manuel Pégourié-Gonnard | 7f76231 | 2013-11-21 10:47:41 +0100 | [diff] [blame] | 1209 | * Post-precessing: reclaim some memory by |
| 1210 | * - not storing Z (always 1) |
| 1211 | * - shrinking other coordinates |
Manuel Pégourié-Gonnard | 01fca5e | 2013-11-21 17:47:12 +0100 | [diff] [blame] | 1212 | * Keep the same number of limbs as P to avoid re-growing on next use. |
Manuel Pégourié-Gonnard | e282012 | 2013-11-21 10:08:50 +0100 | [diff] [blame] | 1213 | */ |
| 1214 | for( i = 0; i < ( 1U << (w-1) ); i++ ) |
| 1215 | { |
| 1216 | mpi_free( &T[i].Z ); |
Manuel Pégourié-Gonnard | 7f76231 | 2013-11-21 10:47:41 +0100 | [diff] [blame] | 1217 | mpi_shrink( &T[i].X, grp->P.n ); |
| 1218 | mpi_shrink( &T[i].Y, grp->P.n ); |
Manuel Pégourié-Gonnard | e282012 | 2013-11-21 10:08:50 +0100 | [diff] [blame] | 1219 | } |
| 1220 | |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1221 | cleanup: |
| 1222 | return( ret ); |
| 1223 | } |
| 1224 | |
| 1225 | /* |
Manuel Pégourié-Gonnard | 101a39f | 2013-11-20 14:47:19 +0100 | [diff] [blame] | 1226 | * Select precomputed point: R = sign(i) * T[ abs(i) / 2 ] |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1227 | */ |
| 1228 | static int ecp_select_comb( const ecp_group *grp, ecp_point *R, |
Manuel Pégourié-Gonnard | 96c7a92 | 2013-11-25 18:28:53 +0100 | [diff] [blame] | 1229 | const ecp_point T[], unsigned char t_len, |
Manuel Pégourié-Gonnard | d728350 | 2013-11-21 20:00:38 +0100 | [diff] [blame] | 1230 | unsigned char i ) |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1231 | { |
| 1232 | int ret; |
Manuel Pégourié-Gonnard | d728350 | 2013-11-21 20:00:38 +0100 | [diff] [blame] | 1233 | unsigned char ii, j; |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1234 | |
Manuel Pégourié-Gonnard | d728350 | 2013-11-21 20:00:38 +0100 | [diff] [blame] | 1235 | /* Ignore the "sign" bit and scale down */ |
| 1236 | ii = ( i & 0x7Fu ) >> 1; |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1237 | |
Manuel Pégourié-Gonnard | d728350 | 2013-11-21 20:00:38 +0100 | [diff] [blame] | 1238 | /* Read the whole table to thwart cache-based timing attacks */ |
| 1239 | for( j = 0; j < t_len; j++ ) |
| 1240 | { |
| 1241 | MPI_CHK( mpi_safe_cond_assign( &R->X, &T[j].X, j == ii ) ); |
| 1242 | MPI_CHK( mpi_safe_cond_assign( &R->Y, &T[j].Y, j == ii ) ); |
| 1243 | } |
| 1244 | |
Manuel Pégourié-Gonnard | 01fca5e | 2013-11-21 17:47:12 +0100 | [diff] [blame] | 1245 | /* Safely invert result if i is "negative" */ |
Manuel Pégourié-Gonnard | 3c0b4ea | 2013-12-02 19:44:41 +0100 | [diff] [blame] | 1246 | MPI_CHK( ecp_safe_invert_jac( grp, R, i >> 7 ) ); |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1247 | |
| 1248 | cleanup: |
| 1249 | return( ret ); |
| 1250 | } |
| 1251 | |
| 1252 | /* |
| 1253 | * Core multiplication algorithm for the (modified) comb method. |
| 1254 | * 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] | 1255 | * |
| 1256 | * Cost: d A + d D + 1 R |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1257 | */ |
| 1258 | static int ecp_mul_comb_core( const ecp_group *grp, ecp_point *R, |
Manuel Pégourié-Gonnard | 96c7a92 | 2013-11-25 18:28:53 +0100 | [diff] [blame] | 1259 | const ecp_point T[], unsigned char t_len, |
Manuel Pégourié-Gonnard | 70c1437 | 2013-11-20 20:07:26 +0100 | [diff] [blame] | 1260 | const unsigned char x[], size_t d, |
| 1261 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1262 | void *p_rng ) |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1263 | { |
| 1264 | int ret; |
| 1265 | ecp_point Txi; |
| 1266 | size_t i; |
| 1267 | |
| 1268 | ecp_point_init( &Txi ); |
| 1269 | |
Manuel Pégourié-Gonnard | 70c1437 | 2013-11-20 20:07:26 +0100 | [diff] [blame] | 1270 | /* Start with a non-zero point and randomize its coordinates */ |
Manuel Pégourié-Gonnard | 101a39f | 2013-11-20 14:47:19 +0100 | [diff] [blame] | 1271 | i = d; |
Manuel Pégourié-Gonnard | d728350 | 2013-11-21 20:00:38 +0100 | [diff] [blame] | 1272 | MPI_CHK( ecp_select_comb( grp, R, T, t_len, x[i] ) ); |
Manuel Pégourié-Gonnard | 72c172a | 2013-12-30 16:04:55 +0100 | [diff] [blame^] | 1273 | MPI_CHK( mpi_lset( &R->Z, 1 ) ); |
Manuel Pégourié-Gonnard | 70c1437 | 2013-11-20 20:07:26 +0100 | [diff] [blame] | 1274 | if( f_rng != 0 ) |
Manuel Pégourié-Gonnard | 3afa07f | 2013-12-03 13:28:21 +0100 | [diff] [blame] | 1275 | MPI_CHK( ecp_randomize_jac( grp, R, f_rng, p_rng ) ); |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1276 | |
| 1277 | while( i-- != 0 ) |
| 1278 | { |
| 1279 | MPI_CHK( ecp_double_jac( grp, R, R ) ); |
Manuel Pégourié-Gonnard | d728350 | 2013-11-21 20:00:38 +0100 | [diff] [blame] | 1280 | MPI_CHK( ecp_select_comb( grp, &Txi, T, t_len, x[i] ) ); |
Manuel Pégourié-Gonnard | 469a209 | 2013-11-21 18:20:43 +0100 | [diff] [blame] | 1281 | MPI_CHK( ecp_add_mixed( grp, R, R, &Txi ) ); |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1282 | } |
| 1283 | |
| 1284 | cleanup: |
| 1285 | ecp_point_free( &Txi ); |
| 1286 | |
| 1287 | return( ret ); |
| 1288 | } |
| 1289 | |
| 1290 | /* |
Manuel Pégourié-Gonnard | a0179b8 | 2013-12-04 11:49:20 +0100 | [diff] [blame] | 1291 | * Multiplication using the comb method, |
| 1292 | * for curves in short Weierstrass form |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1293 | */ |
Manuel Pégourié-Gonnard | a0179b8 | 2013-12-04 11:49:20 +0100 | [diff] [blame] | 1294 | static int ecp_mul_comb( ecp_group *grp, ecp_point *R, |
| 1295 | const mpi *m, const ecp_point *P, |
| 1296 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1297 | void *p_rng ) |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1298 | { |
| 1299 | int ret; |
Manuel Pégourié-Gonnard | d728350 | 2013-11-21 20:00:38 +0100 | [diff] [blame] | 1300 | unsigned char w, m_is_odd, p_eq_g, pre_len, i; |
| 1301 | size_t d; |
Manuel Pégourié-Gonnard | c30200e | 2013-11-20 18:39:55 +0100 | [diff] [blame] | 1302 | unsigned char k[COMB_MAX_D + 1]; |
Manuel Pégourié-Gonnard | aade42f | 2013-11-21 19:19:54 +0100 | [diff] [blame] | 1303 | ecp_point *T; |
| 1304 | mpi M, mm; |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1305 | |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1306 | mpi_init( &M ); |
Manuel Pégourié-Gonnard | aade42f | 2013-11-21 19:19:54 +0100 | [diff] [blame] | 1307 | mpi_init( &mm ); |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1308 | |
Manuel Pégourié-Gonnard | a0179b8 | 2013-12-04 11:49:20 +0100 | [diff] [blame] | 1309 | /* we need N to be odd to trnaform m in an odd number, check now */ |
| 1310 | if( mpi_get_bit( &grp->N, 0 ) != 1 ) |
| 1311 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
| 1312 | |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1313 | /* |
Manuel Pégourié-Gonnard | 04a0225 | 2013-11-20 22:57:38 +0100 | [diff] [blame] | 1314 | * Minimize the number of multiplications, that is minimize |
Manuel Pégourié-Gonnard | 36daa13 | 2013-11-21 18:33:36 +0100 | [diff] [blame] | 1315 | * 10 * d * w + 18 * 2^(w-1) + 11 * d + 7 * w, with d = ceil( nbits / w ) |
Manuel Pégourié-Gonnard | 04a0225 | 2013-11-20 22:57:38 +0100 | [diff] [blame] | 1316 | * (see costs of the various parts, with 1S = 1M) |
| 1317 | */ |
| 1318 | w = grp->nbits >= 384 ? 5 : 4; |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1319 | |
Manuel Pégourié-Gonnard | 04a0225 | 2013-11-20 22:57:38 +0100 | [diff] [blame] | 1320 | /* |
| 1321 | * If P == G, pre-compute a bit more, since this may be re-used later. |
| 1322 | * Just adding one ups the cost of the first mul by at most 3%. |
| 1323 | */ |
Manuel Pégourié-Gonnard | a0179b8 | 2013-12-04 11:49:20 +0100 | [diff] [blame] | 1324 | p_eq_g = ( mpi_cmp_mpi( &P->Y, &grp->G.Y ) == 0 && |
| 1325 | mpi_cmp_mpi( &P->X, &grp->G.X ) == 0 ); |
Manuel Pégourié-Gonnard | 04a0225 | 2013-11-20 22:57:38 +0100 | [diff] [blame] | 1326 | if( p_eq_g ) |
| 1327 | w++; |
| 1328 | |
| 1329 | /* |
Manuel Pégourié-Gonnard | 36daa13 | 2013-11-21 18:33:36 +0100 | [diff] [blame] | 1330 | * Make sure w is within bounds. |
Manuel Pégourié-Gonnard | 04a0225 | 2013-11-20 22:57:38 +0100 | [diff] [blame] | 1331 | * (The last test is useful only for very small curves in the test suite.) |
| 1332 | */ |
| 1333 | if( w > POLARSSL_ECP_WINDOW_SIZE ) |
| 1334 | w = POLARSSL_ECP_WINDOW_SIZE; |
Manuel Pégourié-Gonnard | 36daa13 | 2013-11-21 18:33:36 +0100 | [diff] [blame] | 1335 | if( w >= grp->nbits ) |
Manuel Pégourié-Gonnard | 04a0225 | 2013-11-20 22:57:38 +0100 | [diff] [blame] | 1336 | w = 2; |
| 1337 | |
| 1338 | /* Other sizes that depend on w */ |
Manuel Pégourié-Gonnard | c30200e | 2013-11-20 18:39:55 +0100 | [diff] [blame] | 1339 | pre_len = 1U << ( w - 1 ); |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1340 | d = ( grp->nbits + w - 1 ) / w; |
| 1341 | |
| 1342 | /* |
| 1343 | * Prepare precomputed points: if P == G we want to |
Manuel Pégourié-Gonnard | edc1a1f | 2013-11-21 09:50:00 +0100 | [diff] [blame] | 1344 | * use grp->T if already initialized, or initialize it. |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1345 | */ |
Manuel Pégourié-Gonnard | aade42f | 2013-11-21 19:19:54 +0100 | [diff] [blame] | 1346 | T = p_eq_g ? grp->T : NULL; |
Manuel Pégourié-Gonnard | edc1a1f | 2013-11-21 09:50:00 +0100 | [diff] [blame] | 1347 | |
| 1348 | if( T == NULL ) |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1349 | { |
| 1350 | T = (ecp_point *) polarssl_malloc( pre_len * sizeof( ecp_point ) ); |
| 1351 | if( T == NULL ) |
| 1352 | { |
| 1353 | ret = POLARSSL_ERR_ECP_MALLOC_FAILED; |
| 1354 | goto cleanup; |
| 1355 | } |
| 1356 | |
| 1357 | for( i = 0; i < pre_len; i++ ) |
| 1358 | ecp_point_init( &T[i] ); |
| 1359 | |
| 1360 | MPI_CHK( ecp_precompute_comb( grp, T, P, w, d ) ); |
| 1361 | |
| 1362 | if( p_eq_g ) |
| 1363 | { |
| 1364 | grp->T = T; |
| 1365 | grp->T_size = pre_len; |
| 1366 | } |
| 1367 | } |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1368 | |
| 1369 | /* |
Manuel Pégourié-Gonnard | aade42f | 2013-11-21 19:19:54 +0100 | [diff] [blame] | 1370 | * Make sure M is odd (M = m or M = N - m, since N is odd) |
| 1371 | * using the fact that m * P = - (N - m) * P |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1372 | */ |
| 1373 | m_is_odd = ( mpi_get_bit( m, 0 ) == 1 ); |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1374 | MPI_CHK( mpi_copy( &M, m ) ); |
Manuel Pégourié-Gonnard | aade42f | 2013-11-21 19:19:54 +0100 | [diff] [blame] | 1375 | MPI_CHK( mpi_sub_mpi( &mm, &grp->N, m ) ); |
| 1376 | MPI_CHK( mpi_safe_cond_assign( &M, &mm, ! m_is_odd ) ); |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1377 | |
| 1378 | /* |
Manuel Pégourié-Gonnard | aade42f | 2013-11-21 19:19:54 +0100 | [diff] [blame] | 1379 | * Go for comb multiplication, R = M * P |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1380 | */ |
| 1381 | ecp_comb_fixed( k, d, w, &M ); |
Manuel Pégourié-Gonnard | d728350 | 2013-11-21 20:00:38 +0100 | [diff] [blame] | 1382 | MPI_CHK( ecp_mul_comb_core( grp, R, T, pre_len, k, d, f_rng, p_rng ) ); |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1383 | |
| 1384 | /* |
Manuel Pégourié-Gonnard | aade42f | 2013-11-21 19:19:54 +0100 | [diff] [blame] | 1385 | * Now get m * P from M * P and normalize it |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1386 | */ |
Manuel Pégourié-Gonnard | 3c0b4ea | 2013-12-02 19:44:41 +0100 | [diff] [blame] | 1387 | MPI_CHK( ecp_safe_invert_jac( grp, R, ! m_is_odd ) ); |
| 1388 | MPI_CHK( ecp_normalize_jac( grp, R ) ); |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1389 | |
| 1390 | cleanup: |
| 1391 | |
| 1392 | if( T != NULL && ! p_eq_g ) |
| 1393 | { |
| 1394 | for( i = 0; i < pre_len; i++ ) |
| 1395 | ecp_point_free( &T[i] ); |
| 1396 | polarssl_free( T ); |
| 1397 | } |
| 1398 | |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1399 | mpi_free( &M ); |
Manuel Pégourié-Gonnard | aade42f | 2013-11-21 19:19:54 +0100 | [diff] [blame] | 1400 | mpi_free( &mm ); |
| 1401 | |
| 1402 | if( ret != 0 ) |
| 1403 | ecp_point_free( R ); |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1404 | |
| 1405 | return( ret ); |
| 1406 | } |
| 1407 | |
Manuel Pégourié-Gonnard | 7c94d8b | 2013-12-04 23:15:46 +0100 | [diff] [blame] | 1408 | #endif /* POLARSSL_ECP_SHORT_WEIERSTRASS */ |
| 1409 | |
| 1410 | #if defined(POLARSSL_ECP_MONTGOMERY) |
| 1411 | /* |
| 1412 | * For Montgomery curves, we do all the internal arithmetic in projective |
| 1413 | * coordinates. Import/export of points uses only the x coordinates, which is |
| 1414 | * internaly represented as X / Z. |
| 1415 | * |
| 1416 | * For scalar multiplication, we'll use a Montgomery ladder. |
| 1417 | */ |
| 1418 | |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1419 | /* |
Manuel Pégourié-Gonnard | d9ea82e7 | 2013-12-03 12:02:28 +0100 | [diff] [blame] | 1420 | * Normalize Montgomery x/z coordinates: X = X/Z, Z = 1 |
| 1421 | * Cost: 1M + 1I |
| 1422 | */ |
| 1423 | static int ecp_normalize_mxz( const ecp_group *grp, ecp_point *P ) |
| 1424 | { |
| 1425 | int ret; |
| 1426 | |
| 1427 | MPI_CHK( mpi_inv_mod( &P->Z, &P->Z, &grp->P ) ); |
| 1428 | MPI_CHK( mpi_mul_mpi( &P->X, &P->X, &P->Z ) ); MOD_MUL( P->X ); |
| 1429 | MPI_CHK( mpi_lset( &P->Z, 1 ) ); |
| 1430 | |
| 1431 | cleanup: |
| 1432 | return( ret ); |
| 1433 | } |
| 1434 | |
| 1435 | /* |
Manuel Pégourié-Gonnard | 3afa07f | 2013-12-03 13:28:21 +0100 | [diff] [blame] | 1436 | * Randomize projective x/z coordinates: |
| 1437 | * (X, Z) -> (l X, l Z) for random l |
| 1438 | * This is sort of the reverse operation of ecp_normalize_mxz(). |
| 1439 | * |
| 1440 | * This countermeasure was first suggested in [2]. |
| 1441 | * Cost: 2M |
| 1442 | */ |
| 1443 | static int ecp_randomize_mxz( const ecp_group *grp, ecp_point *P, |
| 1444 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 1445 | { |
| 1446 | int ret; |
| 1447 | mpi l; |
| 1448 | size_t p_size = (grp->pbits + 7) / 8; |
| 1449 | int count = 0; |
| 1450 | |
| 1451 | mpi_init( &l ); |
| 1452 | |
| 1453 | /* Generate l such that 1 < l < p */ |
| 1454 | do |
| 1455 | { |
| 1456 | mpi_fill_random( &l, p_size, f_rng, p_rng ); |
| 1457 | |
| 1458 | while( mpi_cmp_mpi( &l, &grp->P ) >= 0 ) |
| 1459 | mpi_shift_r( &l, 1 ); |
| 1460 | |
| 1461 | if( count++ > 10 ) |
| 1462 | return( POLARSSL_ERR_ECP_RANDOM_FAILED ); |
| 1463 | } |
| 1464 | while( mpi_cmp_int( &l, 1 ) <= 0 ); |
| 1465 | |
| 1466 | MPI_CHK( mpi_mul_mpi( &P->X, &P->X, &l ) ); MOD_MUL( P->X ); |
| 1467 | MPI_CHK( mpi_mul_mpi( &P->Z, &P->Z, &l ) ); MOD_MUL( P->Z ); |
| 1468 | |
| 1469 | cleanup: |
| 1470 | mpi_free( &l ); |
| 1471 | |
| 1472 | return( ret ); |
| 1473 | } |
| 1474 | |
| 1475 | /* |
Manuel Pégourié-Gonnard | d9ea82e7 | 2013-12-03 12:02:28 +0100 | [diff] [blame] | 1476 | * Double-and-add: R = 2P, S = P + Q, with d = X(P - Q), |
| 1477 | * for Montgomery curves in x/z coordinates. |
| 1478 | * |
| 1479 | * http://www.hyperelliptic.org/EFD/g1p/auto-code/montgom/xz/ladder/mladd-1987-m.op3 |
| 1480 | * with |
| 1481 | * d = X1 |
| 1482 | * P = (X2, Z2) |
| 1483 | * Q = (X3, Z3) |
| 1484 | * R = (X4, Z4) |
| 1485 | * S = (X5, Z5) |
| 1486 | * and eliminating temporary variables tO, ..., t4. |
| 1487 | * |
| 1488 | * Cost: 5M + 4S |
| 1489 | */ |
| 1490 | static int ecp_double_add_mxz( const ecp_group *grp, |
| 1491 | ecp_point *R, ecp_point *S, |
| 1492 | const ecp_point *P, const ecp_point *Q, |
| 1493 | const mpi *d ) |
| 1494 | { |
| 1495 | int ret; |
| 1496 | mpi A, AA, B, BB, E, C, D, DA, CB; |
| 1497 | |
| 1498 | mpi_init( &A ); mpi_init( &AA ); mpi_init( &B ); |
| 1499 | mpi_init( &BB ); mpi_init( &E ); mpi_init( &C ); |
| 1500 | mpi_init( &D ); mpi_init( &DA ); mpi_init( &CB ); |
| 1501 | |
| 1502 | MPI_CHK( mpi_add_mpi( &A, &P->X, &P->Z ) ); MOD_ADD( A ); |
| 1503 | MPI_CHK( mpi_mul_mpi( &AA, &A, &A ) ); MOD_MUL( AA ); |
| 1504 | MPI_CHK( mpi_sub_mpi( &B, &P->X, &P->Z ) ); MOD_SUB( B ); |
| 1505 | MPI_CHK( mpi_mul_mpi( &BB, &B, &B ) ); MOD_MUL( BB ); |
| 1506 | MPI_CHK( mpi_sub_mpi( &E, &AA, &BB ) ); MOD_SUB( E ); |
| 1507 | MPI_CHK( mpi_add_mpi( &C, &Q->X, &Q->Z ) ); MOD_ADD( C ); |
| 1508 | MPI_CHK( mpi_sub_mpi( &D, &Q->X, &Q->Z ) ); MOD_SUB( D ); |
| 1509 | MPI_CHK( mpi_mul_mpi( &DA, &D, &A ) ); MOD_MUL( DA ); |
| 1510 | MPI_CHK( mpi_mul_mpi( &CB, &C, &B ) ); MOD_MUL( CB ); |
| 1511 | MPI_CHK( mpi_add_mpi( &S->X, &DA, &CB ) ); MOD_MUL( S->X ); |
| 1512 | MPI_CHK( mpi_mul_mpi( &S->X, &S->X, &S->X ) ); MOD_MUL( S->X ); |
| 1513 | MPI_CHK( mpi_sub_mpi( &S->Z, &DA, &CB ) ); MOD_SUB( S->Z ); |
| 1514 | MPI_CHK( mpi_mul_mpi( &S->Z, &S->Z, &S->Z ) ); MOD_MUL( S->Z ); |
| 1515 | MPI_CHK( mpi_mul_mpi( &S->Z, d, &S->Z ) ); MOD_MUL( S->Z ); |
| 1516 | MPI_CHK( mpi_mul_mpi( &R->X, &AA, &BB ) ); MOD_MUL( R->X ); |
| 1517 | MPI_CHK( mpi_mul_mpi( &R->Z, &grp->A, &E ) ); MOD_MUL( R->Z ); |
| 1518 | MPI_CHK( mpi_add_mpi( &R->Z, &BB, &R->Z ) ); MOD_ADD( R->Z ); |
| 1519 | MPI_CHK( mpi_mul_mpi( &R->Z, &E, &R->Z ) ); MOD_MUL( R->Z ); |
| 1520 | |
| 1521 | cleanup: |
| 1522 | mpi_free( &A ); mpi_free( &AA ); mpi_free( &B ); |
| 1523 | mpi_free( &BB ); mpi_free( &E ); mpi_free( &C ); |
| 1524 | mpi_free( &D ); mpi_free( &DA ); mpi_free( &CB ); |
| 1525 | |
| 1526 | return( ret ); |
| 1527 | } |
| 1528 | |
| 1529 | /* |
Manuel Pégourié-Gonnard | a0179b8 | 2013-12-04 11:49:20 +0100 | [diff] [blame] | 1530 | * Multiplication with Montgomery ladder in x/z coordinates, |
| 1531 | * for curves in Montgomery form |
Manuel Pégourié-Gonnard | d9ea82e7 | 2013-12-03 12:02:28 +0100 | [diff] [blame] | 1532 | */ |
Manuel Pégourié-Gonnard | a0179b8 | 2013-12-04 11:49:20 +0100 | [diff] [blame] | 1533 | static int ecp_mul_mxz( ecp_group *grp, ecp_point *R, |
| 1534 | const mpi *m, const ecp_point *P, |
| 1535 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1536 | void *p_rng ) |
Manuel Pégourié-Gonnard | d9ea82e7 | 2013-12-03 12:02:28 +0100 | [diff] [blame] | 1537 | { |
| 1538 | int ret; |
| 1539 | size_t i; |
Manuel Pégourié-Gonnard | b6f45a6 | 2013-12-04 21:54:36 +0100 | [diff] [blame] | 1540 | unsigned char b; |
Manuel Pégourié-Gonnard | d9ea82e7 | 2013-12-03 12:02:28 +0100 | [diff] [blame] | 1541 | ecp_point RP; |
| 1542 | mpi PX; |
| 1543 | |
| 1544 | ecp_point_init( &RP ); mpi_init( &PX ); |
| 1545 | |
Manuel Pégourié-Gonnard | 3afa07f | 2013-12-03 13:28:21 +0100 | [diff] [blame] | 1546 | /* Save PX and read from P before writing to R, in case P == R */ |
Manuel Pégourié-Gonnard | d9ea82e7 | 2013-12-03 12:02:28 +0100 | [diff] [blame] | 1547 | mpi_copy( &PX, &P->X ); |
| 1548 | MPI_CHK( ecp_copy( &RP, P ) ); |
Manuel Pégourié-Gonnard | 357ff65 | 2013-12-04 18:39:17 +0100 | [diff] [blame] | 1549 | |
| 1550 | /* Set R to zero in modified x/z coordinates */ |
| 1551 | MPI_CHK( mpi_lset( &R->X, 1 ) ); |
| 1552 | MPI_CHK( mpi_lset( &R->Z, 0 ) ); |
| 1553 | mpi_free( &R->Y ); |
Manuel Pégourié-Gonnard | d9ea82e7 | 2013-12-03 12:02:28 +0100 | [diff] [blame] | 1554 | |
Manuel Pégourié-Gonnard | 93f41db | 2013-12-05 10:48:42 +0100 | [diff] [blame] | 1555 | /* RP.X might be sligtly larger than P, so reduce it */ |
| 1556 | MOD_ADD( RP.X ); |
| 1557 | |
Manuel Pégourié-Gonnard | 3afa07f | 2013-12-03 13:28:21 +0100 | [diff] [blame] | 1558 | /* Randomize coordinates of the starting point */ |
Manuel Pégourié-Gonnard | 357ff65 | 2013-12-04 18:39:17 +0100 | [diff] [blame] | 1559 | if( f_rng != NULL ) |
| 1560 | MPI_CHK( ecp_randomize_mxz( grp, &RP, f_rng, p_rng ) ); |
Manuel Pégourié-Gonnard | d9ea82e7 | 2013-12-03 12:02:28 +0100 | [diff] [blame] | 1561 | |
Manuel Pégourié-Gonnard | b6f45a6 | 2013-12-04 21:54:36 +0100 | [diff] [blame] | 1562 | /* Loop invariant: R = result so far, RP = R + P */ |
Manuel Pégourié-Gonnard | 357ff65 | 2013-12-04 18:39:17 +0100 | [diff] [blame] | 1563 | i = mpi_msb( m ); /* one past the (zero-based) most significant bit */ |
Manuel Pégourié-Gonnard | d9ea82e7 | 2013-12-03 12:02:28 +0100 | [diff] [blame] | 1564 | while( i-- > 0 ) |
| 1565 | { |
Manuel Pégourié-Gonnard | b6f45a6 | 2013-12-04 21:54:36 +0100 | [diff] [blame] | 1566 | b = mpi_get_bit( m, i ); |
| 1567 | /* |
| 1568 | * if (b) R = 2R + P else R = 2R, |
| 1569 | * which is: |
| 1570 | * if (b) double_add( RP, R, RP, R ) |
| 1571 | * else double_add( R, RP, R, RP ) |
| 1572 | * but using safe conditional swaps to avoid leaks |
| 1573 | */ |
| 1574 | MPI_CHK( mpi_safe_cond_swap( &R->X, &RP.X, b ) ); |
| 1575 | MPI_CHK( mpi_safe_cond_swap( &R->Z, &RP.Z, b ) ); |
| 1576 | MPI_CHK( ecp_double_add_mxz( grp, R, &RP, R, &RP, &PX ) ); |
| 1577 | MPI_CHK( mpi_safe_cond_swap( &R->X, &RP.X, b ) ); |
| 1578 | MPI_CHK( mpi_safe_cond_swap( &R->Z, &RP.Z, b ) ); |
Manuel Pégourié-Gonnard | d9ea82e7 | 2013-12-03 12:02:28 +0100 | [diff] [blame] | 1579 | } |
| 1580 | |
| 1581 | MPI_CHK( ecp_normalize_mxz( grp, R ) ); |
| 1582 | |
| 1583 | cleanup: |
| 1584 | ecp_point_free( &RP ); mpi_free( &PX ); |
| 1585 | |
| 1586 | return( ret ); |
| 1587 | } |
| 1588 | |
Manuel Pégourié-Gonnard | 7c94d8b | 2013-12-04 23:15:46 +0100 | [diff] [blame] | 1589 | #endif /* POLARSSL_ECP_MONTGOMERY */ |
| 1590 | |
Manuel Pégourié-Gonnard | d9ea82e7 | 2013-12-03 12:02:28 +0100 | [diff] [blame] | 1591 | /* |
Manuel Pégourié-Gonnard | a0179b8 | 2013-12-04 11:49:20 +0100 | [diff] [blame] | 1592 | * Multiplication R = m * P |
| 1593 | */ |
| 1594 | int ecp_mul( ecp_group *grp, ecp_point *R, |
| 1595 | const mpi *m, const ecp_point *P, |
| 1596 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 1597 | { |
| 1598 | int ret; |
| 1599 | |
| 1600 | /* Common sanity checks */ |
| 1601 | if( mpi_cmp_int( &P->Z, 1 ) != 0 ) |
| 1602 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
| 1603 | |
| 1604 | if( ( ret = ecp_check_privkey( grp, m ) ) != 0 || |
| 1605 | ( ret = ecp_check_pubkey( grp, P ) ) != 0 ) |
| 1606 | return( ret ); |
| 1607 | |
Manuel Pégourié-Gonnard | 7c94d8b | 2013-12-04 23:15:46 +0100 | [diff] [blame] | 1608 | #if defined(POLARSSL_ECP_MONTGOMERY) |
| 1609 | if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_MONTGOMERY ) |
Manuel Pégourié-Gonnard | a0179b8 | 2013-12-04 11:49:20 +0100 | [diff] [blame] | 1610 | return( ecp_mul_mxz( grp, R, m, P, f_rng, p_rng ) ); |
Manuel Pégourié-Gonnard | 7c94d8b | 2013-12-04 23:15:46 +0100 | [diff] [blame] | 1611 | #endif |
| 1612 | #if defined(POLARSSL_ECP_SHORT_WEIERSTRASS) |
| 1613 | if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS ) |
Manuel Pégourié-Gonnard | a0179b8 | 2013-12-04 11:49:20 +0100 | [diff] [blame] | 1614 | return( ecp_mul_comb( grp, R, m, P, f_rng, p_rng ) ); |
Manuel Pégourié-Gonnard | 7c94d8b | 2013-12-04 23:15:46 +0100 | [diff] [blame] | 1615 | #endif |
| 1616 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | a0179b8 | 2013-12-04 11:49:20 +0100 | [diff] [blame] | 1617 | } |
| 1618 | |
Manuel Pégourié-Gonnard | d962273 | 2013-12-05 10:06:06 +0100 | [diff] [blame] | 1619 | #if defined(POLARSSL_ECP_SHORT_WEIERSTRASS) |
Manuel Pégourié-Gonnard | a0179b8 | 2013-12-04 11:49:20 +0100 | [diff] [blame] | 1620 | /* |
Manuel Pégourié-Gonnard | d962273 | 2013-12-05 10:06:06 +0100 | [diff] [blame] | 1621 | * Check that an affine point is valid as a public key, |
| 1622 | * short weierstrass curves (SEC1 3.2.3.1) |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 1623 | */ |
Manuel Pégourié-Gonnard | d962273 | 2013-12-05 10:06:06 +0100 | [diff] [blame] | 1624 | static int ecp_check_pubkey_sw( const ecp_group *grp, const ecp_point *pt ) |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 1625 | { |
| 1626 | int ret; |
| 1627 | mpi YY, RHS; |
| 1628 | |
Manuel Pégourié-Gonnard | 312d2e8 | 2013-12-04 11:08:01 +0100 | [diff] [blame] | 1629 | /* pt coordinates must be normalized for our checks */ |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 1630 | if( mpi_cmp_int( &pt->X, 0 ) < 0 || |
| 1631 | mpi_cmp_int( &pt->Y, 0 ) < 0 || |
| 1632 | mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 || |
| 1633 | mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 ) |
Manuel Pégourié-Gonnard | 456d3b9 | 2013-09-16 18:04:38 +0200 | [diff] [blame] | 1634 | return( POLARSSL_ERR_ECP_INVALID_KEY ); |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 1635 | |
| 1636 | mpi_init( &YY ); mpi_init( &RHS ); |
| 1637 | |
| 1638 | /* |
| 1639 | * YY = Y^2 |
Manuel Pégourié-Gonnard | cd7458a | 2013-10-08 13:11:30 +0200 | [diff] [blame] | 1640 | * 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] | 1641 | */ |
Manuel Pégourié-Gonnard | cd7458a | 2013-10-08 13:11:30 +0200 | [diff] [blame] | 1642 | MPI_CHK( mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY ); |
| 1643 | MPI_CHK( mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS ); |
Manuel Pégourié-Gonnard | 73cc01d | 2013-12-06 12:41:30 +0100 | [diff] [blame] | 1644 | |
| 1645 | /* Special case for A = -3 */ |
| 1646 | if( grp->A.p == NULL ) |
| 1647 | { |
| 1648 | MPI_CHK( mpi_sub_int( &RHS, &RHS, 3 ) ); MOD_SUB( RHS ); |
| 1649 | } |
| 1650 | else |
| 1651 | { |
| 1652 | MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->A ) ); MOD_ADD( RHS ); |
| 1653 | } |
| 1654 | |
Manuel Pégourié-Gonnard | cd7458a | 2013-10-08 13:11:30 +0200 | [diff] [blame] | 1655 | MPI_CHK( mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS ); |
| 1656 | 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] | 1657 | |
| 1658 | if( mpi_cmp_mpi( &YY, &RHS ) != 0 ) |
Manuel Pégourié-Gonnard | 456d3b9 | 2013-09-16 18:04:38 +0200 | [diff] [blame] | 1659 | ret = POLARSSL_ERR_ECP_INVALID_KEY; |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 1660 | |
| 1661 | cleanup: |
| 1662 | |
| 1663 | mpi_free( &YY ); mpi_free( &RHS ); |
| 1664 | |
| 1665 | return( ret ); |
| 1666 | } |
Manuel Pégourié-Gonnard | d962273 | 2013-12-05 10:06:06 +0100 | [diff] [blame] | 1667 | #endif /* POLARSSL_ECP_SHORT_WEIERSTRASS */ |
| 1668 | |
| 1669 | |
| 1670 | #if defined(POLARSSL_ECP_MONTGOMERY) |
| 1671 | /* |
| 1672 | * Check validity of a public key for Montgomery curves with x-only schemes |
| 1673 | */ |
| 1674 | static int ecp_check_pubkey_mx( const ecp_group *grp, const ecp_point *pt ) |
| 1675 | { |
| 1676 | /* [M255 p. 5] Just check X is the correct number of bytes */ |
| 1677 | if( mpi_size( &pt->X ) > ( grp->nbits + 7 ) / 8 ) |
| 1678 | return( POLARSSL_ERR_ECP_INVALID_KEY ); |
| 1679 | |
| 1680 | return( 0 ); |
| 1681 | } |
| 1682 | #endif /* POLARSSL_ECP_MONTGOMERY */ |
| 1683 | |
| 1684 | /* |
| 1685 | * Check that a point is valid as a public key |
| 1686 | */ |
| 1687 | int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt ) |
| 1688 | { |
| 1689 | /* Must use affine coordinates */ |
| 1690 | if( mpi_cmp_int( &pt->Z, 1 ) != 0 ) |
| 1691 | return( POLARSSL_ERR_ECP_INVALID_KEY ); |
| 1692 | |
| 1693 | #if defined(POLARSSL_ECP_MONTGOMERY) |
| 1694 | if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_MONTGOMERY ) |
| 1695 | return( ecp_check_pubkey_mx( grp, pt ) ); |
| 1696 | #endif |
| 1697 | #if defined(POLARSSL_ECP_SHORT_WEIERSTRASS) |
| 1698 | if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS ) |
| 1699 | return( ecp_check_pubkey_sw( grp, pt ) ); |
| 1700 | #endif |
| 1701 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
| 1702 | } |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 1703 | |
| 1704 | /* |
Manuel Pégourié-Gonnard | 312d2e8 | 2013-12-04 11:08:01 +0100 | [diff] [blame] | 1705 | * Check that an mpi is valid as a private key |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 1706 | */ |
Manuel Pégourié-Gonnard | de44a4a | 2013-07-09 16:05:52 +0200 | [diff] [blame] | 1707 | 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] | 1708 | { |
Manuel Pégourié-Gonnard | d962273 | 2013-12-05 10:06:06 +0100 | [diff] [blame] | 1709 | #if defined(POLARSSL_ECP_MONTGOMERY) |
Manuel Pégourié-Gonnard | 7c94d8b | 2013-12-04 23:15:46 +0100 | [diff] [blame] | 1710 | if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_MONTGOMERY ) |
Manuel Pégourié-Gonnard | 312d2e8 | 2013-12-04 11:08:01 +0100 | [diff] [blame] | 1711 | { |
Manuel Pégourié-Gonnard | fe0af40 | 2013-12-04 18:14:55 +0100 | [diff] [blame] | 1712 | /* see [M255] page 5 */ |
Manuel Pégourié-Gonnard | 312d2e8 | 2013-12-04 11:08:01 +0100 | [diff] [blame] | 1713 | if( mpi_get_bit( d, 0 ) != 0 || |
| 1714 | mpi_get_bit( d, 1 ) != 0 || |
| 1715 | mpi_get_bit( d, 2 ) != 0 || |
| 1716 | mpi_msb( d ) - 1 != grp->nbits ) /* mpi_msb is one-based! */ |
| 1717 | return( POLARSSL_ERR_ECP_INVALID_KEY ); |
Manuel Pégourié-Gonnard | d962273 | 2013-12-05 10:06:06 +0100 | [diff] [blame] | 1718 | else |
| 1719 | return( 0 ); |
Manuel Pégourié-Gonnard | 312d2e8 | 2013-12-04 11:08:01 +0100 | [diff] [blame] | 1720 | } |
Manuel Pégourié-Gonnard | d962273 | 2013-12-05 10:06:06 +0100 | [diff] [blame] | 1721 | #endif |
| 1722 | #if defined(POLARSSL_ECP_SHORT_WEIERSTRASS) |
| 1723 | if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS ) |
Manuel Pégourié-Gonnard | 312d2e8 | 2013-12-04 11:08:01 +0100 | [diff] [blame] | 1724 | { |
| 1725 | /* see SEC1 3.2 */ |
| 1726 | if( mpi_cmp_int( d, 1 ) < 0 || |
| 1727 | mpi_cmp_mpi( d, &grp->N ) >= 0 ) |
| 1728 | return( POLARSSL_ERR_ECP_INVALID_KEY ); |
Manuel Pégourié-Gonnard | d962273 | 2013-12-05 10:06:06 +0100 | [diff] [blame] | 1729 | else |
| 1730 | return( 0 ); |
Manuel Pégourié-Gonnard | 312d2e8 | 2013-12-04 11:08:01 +0100 | [diff] [blame] | 1731 | } |
Manuel Pégourié-Gonnard | d962273 | 2013-12-05 10:06:06 +0100 | [diff] [blame] | 1732 | #endif |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 1733 | |
Manuel Pégourié-Gonnard | d962273 | 2013-12-05 10:06:06 +0100 | [diff] [blame] | 1734 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 1735 | } |
| 1736 | |
| 1737 | /* |
Manuel Pégourié-Gonnard | fe0af40 | 2013-12-04 18:14:55 +0100 | [diff] [blame] | 1738 | * Generate a keypair |
Manuel Pégourié-Gonnard | 45a035a | 2013-01-26 14:42:45 +0100 | [diff] [blame] | 1739 | */ |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 1740 | 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] | 1741 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1742 | void *p_rng ) |
| 1743 | { |
Manuel Pégourié-Gonnard | 45a035a | 2013-01-26 14:42:45 +0100 | [diff] [blame] | 1744 | size_t n_size = (grp->nbits + 7) / 8; |
| 1745 | |
Manuel Pégourié-Gonnard | d962273 | 2013-12-05 10:06:06 +0100 | [diff] [blame] | 1746 | #if defined(POLARSSL_ECP_MONTGOMERY) |
Manuel Pégourié-Gonnard | 7c94d8b | 2013-12-04 23:15:46 +0100 | [diff] [blame] | 1747 | if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_MONTGOMERY ) |
Manuel Pégourié-Gonnard | 45a035a | 2013-01-26 14:42:45 +0100 | [diff] [blame] | 1748 | { |
Manuel Pégourié-Gonnard | fe0af40 | 2013-12-04 18:14:55 +0100 | [diff] [blame] | 1749 | /* [M225] page 5 */ |
| 1750 | size_t b; |
| 1751 | |
Manuel Pégourié-Gonnard | 45a035a | 2013-01-26 14:42:45 +0100 | [diff] [blame] | 1752 | mpi_fill_random( d, n_size, f_rng, p_rng ); |
| 1753 | |
Manuel Pégourié-Gonnard | fe0af40 | 2013-12-04 18:14:55 +0100 | [diff] [blame] | 1754 | /* Make sure the most significant bit is nbits */ |
| 1755 | b = mpi_msb( d ) - 1; /* mpi_msb is one-based */ |
| 1756 | if( b > grp->nbits ) |
| 1757 | mpi_shift_r( d, b - grp->nbits ); |
| 1758 | else |
| 1759 | mpi_set_bit( d, grp->nbits, 1 ); |
Manuel Pégourié-Gonnard | 45a035a | 2013-01-26 14:42:45 +0100 | [diff] [blame] | 1760 | |
Manuel Pégourié-Gonnard | fe0af40 | 2013-12-04 18:14:55 +0100 | [diff] [blame] | 1761 | /* Make sure the last three bits are unset */ |
| 1762 | mpi_set_bit( d, 0, 0 ); |
| 1763 | mpi_set_bit( d, 1, 0 ); |
| 1764 | mpi_set_bit( d, 2, 0 ); |
Manuel Pégourié-Gonnard | 45a035a | 2013-01-26 14:42:45 +0100 | [diff] [blame] | 1765 | } |
Manuel Pégourié-Gonnard | fe0af40 | 2013-12-04 18:14:55 +0100 | [diff] [blame] | 1766 | else |
Manuel Pégourié-Gonnard | d962273 | 2013-12-05 10:06:06 +0100 | [diff] [blame] | 1767 | #endif |
| 1768 | #if defined(POLARSSL_ECP_SHORT_WEIERSTRASS) |
| 1769 | if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS ) |
Manuel Pégourié-Gonnard | fe0af40 | 2013-12-04 18:14:55 +0100 | [diff] [blame] | 1770 | { |
| 1771 | /* SEC1 3.2.1: Generate d such that 1 <= n < N */ |
Manuel Pégourié-Gonnard | d962273 | 2013-12-05 10:06:06 +0100 | [diff] [blame] | 1772 | int count = 0; |
Manuel Pégourié-Gonnard | fe0af40 | 2013-12-04 18:14:55 +0100 | [diff] [blame] | 1773 | do |
| 1774 | { |
| 1775 | mpi_fill_random( d, n_size, f_rng, p_rng ); |
| 1776 | |
| 1777 | while( mpi_cmp_mpi( d, &grp->N ) >= 0 ) |
| 1778 | mpi_shift_r( d, 1 ); |
| 1779 | |
| 1780 | if( count++ > 10 ) |
| 1781 | return( POLARSSL_ERR_ECP_RANDOM_FAILED ); |
| 1782 | } |
| 1783 | while( mpi_cmp_int( d, 1 ) < 0 ); |
| 1784 | } |
Manuel Pégourié-Gonnard | d962273 | 2013-12-05 10:06:06 +0100 | [diff] [blame] | 1785 | else |
| 1786 | #endif |
| 1787 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 45a035a | 2013-01-26 14:42:45 +0100 | [diff] [blame] | 1788 | |
Manuel Pégourié-Gonnard | e09d2f8 | 2013-09-02 14:29:09 +0200 | [diff] [blame] | 1789 | 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] | 1790 | } |
Manuel Pégourié-Gonnard | efaa31e | 2012-11-06 21:34:35 +0100 | [diff] [blame] | 1791 | |
Manuel Pégourié-Gonnard | 104ee1d | 2013-11-30 14:13:16 +0100 | [diff] [blame] | 1792 | /* |
| 1793 | * Generate a keypair, prettier wrapper |
| 1794 | */ |
| 1795 | int ecp_gen_key( ecp_group_id grp_id, ecp_keypair *key, |
| 1796 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 1797 | { |
| 1798 | int ret; |
| 1799 | |
| 1800 | if( ( ret = ecp_use_known_dp( &key->grp, grp_id ) ) != 0 ) |
| 1801 | return( ret ); |
| 1802 | |
| 1803 | return( ecp_gen_keypair( &key->grp, &key->d, &key->Q, f_rng, p_rng ) ); |
| 1804 | } |
| 1805 | |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 1806 | #if defined(POLARSSL_SELF_TEST) |
| 1807 | |
Manuel Pégourié-Gonnard | b505c27 | 2012-11-05 17:27:54 +0100 | [diff] [blame] | 1808 | /* |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 1809 | * Checkup routine |
| 1810 | */ |
| 1811 | int ecp_self_test( int verbose ) |
| 1812 | { |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 1813 | int ret; |
| 1814 | size_t i; |
| 1815 | ecp_group grp; |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 1816 | ecp_point R, P; |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 1817 | mpi m; |
Manuel Pégourié-Gonnard | 9181481 | 2013-11-21 20:23:55 +0100 | [diff] [blame] | 1818 | unsigned long add_c_prev, dbl_c_prev, mul_c_prev; |
Manuel Pégourié-Gonnard | b8012fc | 2013-10-10 15:40:49 +0200 | [diff] [blame] | 1819 | /* exponents especially adapted for secp192r1 */ |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 1820 | const char *exponents[] = |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 1821 | { |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 1822 | "000000000000000000000000000000000000000000000001", /* one */ |
Manuel Pégourié-Gonnard | ff27b7c | 2013-11-21 09:28:03 +0100 | [diff] [blame] | 1823 | "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22830", /* N - 1 */ |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 1824 | "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */ |
Manuel Pégourié-Gonnard | ff27b7c | 2013-11-21 09:28:03 +0100 | [diff] [blame] | 1825 | "400000000000000000000000000000000000000000000000", /* one and zeros */ |
| 1826 | "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", /* all ones */ |
| 1827 | "555555555555555555555555555555555555555555555555", /* 101010... */ |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 1828 | }; |
| 1829 | |
| 1830 | ecp_group_init( &grp ); |
| 1831 | ecp_point_init( &R ); |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 1832 | ecp_point_init( &P ); |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 1833 | mpi_init( &m ); |
| 1834 | |
Manuel Pégourié-Gonnard | b8012fc | 2013-10-10 15:40:49 +0200 | [diff] [blame] | 1835 | /* Use secp192r1 if available, or any available curve */ |
Paul Bakker | 5dc6b5f | 2013-06-29 23:26:34 +0200 | [diff] [blame] | 1836 | #if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED) |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 1837 | MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP192R1 ) ); |
Paul Bakker | 5dc6b5f | 2013-06-29 23:26:34 +0200 | [diff] [blame] | 1838 | #else |
Manuel Pégourié-Gonnard | b8012fc | 2013-10-10 15:40:49 +0200 | [diff] [blame] | 1839 | MPI_CHK( ecp_use_known_dp( &grp, ecp_curve_list()->grp_id ) ); |
| 1840 | #endif |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 1841 | |
| 1842 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 1843 | printf( " ECP test #1 (constant op_count, base point G): " ); |
| 1844 | |
| 1845 | /* Do a dummy multiplication first to trigger precomputation */ |
| 1846 | MPI_CHK( mpi_lset( &m, 2 ) ); |
| 1847 | 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] | 1848 | |
| 1849 | add_count = 0; |
| 1850 | dbl_count = 0; |
Manuel Pégourié-Gonnard | 9181481 | 2013-11-21 20:23:55 +0100 | [diff] [blame] | 1851 | mul_count = 0; |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 1852 | MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) ); |
Manuel Pégourié-Gonnard | e09d2f8 | 2013-09-02 14:29:09 +0200 | [diff] [blame] | 1853 | 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] | 1854 | |
| 1855 | for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ ) |
| 1856 | { |
| 1857 | add_c_prev = add_count; |
| 1858 | dbl_c_prev = dbl_count; |
Manuel Pégourié-Gonnard | 9181481 | 2013-11-21 20:23:55 +0100 | [diff] [blame] | 1859 | mul_c_prev = mul_count; |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 1860 | add_count = 0; |
| 1861 | dbl_count = 0; |
Manuel Pégourié-Gonnard | 9181481 | 2013-11-21 20:23:55 +0100 | [diff] [blame] | 1862 | mul_count = 0; |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 1863 | |
| 1864 | MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) ); |
Manuel Pégourié-Gonnard | e09d2f8 | 2013-09-02 14:29:09 +0200 | [diff] [blame] | 1865 | 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] | 1866 | |
Manuel Pégourié-Gonnard | 9181481 | 2013-11-21 20:23:55 +0100 | [diff] [blame] | 1867 | if( add_count != add_c_prev || |
| 1868 | dbl_count != dbl_c_prev || |
| 1869 | mul_count != mul_c_prev ) |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 1870 | { |
| 1871 | if( verbose != 0 ) |
| 1872 | printf( "failed (%zu)\n", i ); |
| 1873 | |
| 1874 | ret = 1; |
| 1875 | goto cleanup; |
| 1876 | } |
| 1877 | } |
| 1878 | |
| 1879 | if( verbose != 0 ) |
| 1880 | printf( "passed\n" ); |
| 1881 | |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 1882 | if( verbose != 0 ) |
| 1883 | printf( " ECP test #2 (constant op_count, other point): " ); |
| 1884 | /* We computed P = 2G last time, use it */ |
| 1885 | |
| 1886 | add_count = 0; |
| 1887 | dbl_count = 0; |
Manuel Pégourié-Gonnard | 9181481 | 2013-11-21 20:23:55 +0100 | [diff] [blame] | 1888 | mul_count = 0; |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 1889 | MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) ); |
| 1890 | MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) ); |
| 1891 | |
| 1892 | for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ ) |
| 1893 | { |
| 1894 | add_c_prev = add_count; |
| 1895 | dbl_c_prev = dbl_count; |
Manuel Pégourié-Gonnard | 9181481 | 2013-11-21 20:23:55 +0100 | [diff] [blame] | 1896 | mul_c_prev = mul_count; |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 1897 | add_count = 0; |
| 1898 | dbl_count = 0; |
Manuel Pégourié-Gonnard | 9181481 | 2013-11-21 20:23:55 +0100 | [diff] [blame] | 1899 | mul_count = 0; |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 1900 | |
| 1901 | MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) ); |
| 1902 | MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) ); |
| 1903 | |
Manuel Pégourié-Gonnard | 9181481 | 2013-11-21 20:23:55 +0100 | [diff] [blame] | 1904 | if( add_count != add_c_prev || |
| 1905 | dbl_count != dbl_c_prev || |
| 1906 | mul_count != mul_c_prev ) |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 1907 | { |
| 1908 | if( verbose != 0 ) |
| 1909 | printf( "failed (%zu)\n", i ); |
| 1910 | |
| 1911 | ret = 1; |
| 1912 | goto cleanup; |
| 1913 | } |
| 1914 | } |
| 1915 | |
| 1916 | if( verbose != 0 ) |
| 1917 | printf( "passed\n" ); |
| 1918 | |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 1919 | cleanup: |
| 1920 | |
| 1921 | if( ret < 0 && verbose != 0 ) |
| 1922 | printf( "Unexpected error, return code = %08X\n", ret ); |
| 1923 | |
| 1924 | ecp_group_free( &grp ); |
| 1925 | ecp_point_free( &R ); |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 1926 | ecp_point_free( &P ); |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 1927 | mpi_free( &m ); |
| 1928 | |
| 1929 | if( verbose != 0 ) |
| 1930 | printf( "\n" ); |
| 1931 | |
| 1932 | return( ret ); |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 1933 | } |
| 1934 | |
| 1935 | #endif |
| 1936 | |
| 1937 | #endif |