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