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