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