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