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