Jaeden Amero | e54e693 | 2018-08-06 16:19:58 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Elliptic curves over GF(p): generic functions |
| 3 | * |
| 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
| 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | * |
| 19 | * This file is part of Mbed Crypto (https://tls.mbed.org) |
| 20 | */ |
| 21 | |
| 22 | /* |
| 23 | * References: |
| 24 | * |
| 25 | * SEC1 http://www.secg.org/index.php?action=secg,docs_secg |
| 26 | * GECC = Guide to Elliptic Curve Cryptography - Hankerson, Menezes, Vanstone |
| 27 | * FIPS 186-3 http://csrc.nist.gov/publications/fips/fips186-3/fips_186-3.pdf |
| 28 | * RFC 4492 for the related TLS structures and constants |
| 29 | * RFC 7748 for the Curve448 and Curve25519 curve definitions |
| 30 | * |
| 31 | * [Curve25519] http://cr.yp.to/ecdh/curve25519-20060209.pdf |
| 32 | * |
| 33 | * [2] CORON, Jean-S'ebastien. Resistance against differential power analysis |
| 34 | * for elliptic curve cryptosystems. In : Cryptographic Hardware and |
| 35 | * Embedded Systems. Springer Berlin Heidelberg, 1999. p. 292-302. |
| 36 | * <http://link.springer.com/chapter/10.1007/3-540-48059-5_25> |
| 37 | * |
| 38 | * [3] HEDABOU, Mustapha, PINEL, Pierre, et B'EN'ETEAU, Lucien. A comb method to |
| 39 | * render ECC resistant against Side Channel Attacks. IACR Cryptology |
| 40 | * ePrint Archive, 2004, vol. 2004, p. 342. |
| 41 | * <http://eprint.iacr.org/2004/342.pdf> |
| 42 | */ |
| 43 | |
| 44 | #if !defined(MBEDCRYPTO_CONFIG_FILE) |
| 45 | #include "mbedcrypto/config.h" |
| 46 | #else |
| 47 | #include MBEDCRYPTO_CONFIG_FILE |
| 48 | #endif |
| 49 | |
| 50 | #if defined(MBEDCRYPTO_ECP_C) |
| 51 | |
| 52 | #include "mbedcrypto/ecp.h" |
| 53 | #include "mbedcrypto/threading.h" |
| 54 | #include "mbedcrypto/platform_util.h" |
| 55 | |
| 56 | #include <string.h> |
| 57 | |
| 58 | #if !defined(MBEDCRYPTO_ECP_ALT) |
| 59 | |
| 60 | #if defined(MBEDCRYPTO_PLATFORM_C) |
| 61 | #include "mbedcrypto/platform.h" |
| 62 | #else |
| 63 | #include <stdlib.h> |
| 64 | #include <stdio.h> |
| 65 | #define mbedcrypto_printf printf |
| 66 | #define mbedcrypto_calloc calloc |
| 67 | #define mbedcrypto_free free |
| 68 | #endif |
| 69 | |
| 70 | #include "mbedcrypto/ecp_internal.h" |
| 71 | |
| 72 | #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \ |
| 73 | !defined(inline) && !defined(__cplusplus) |
| 74 | #define inline __inline |
| 75 | #endif |
| 76 | |
| 77 | #if defined(MBEDCRYPTO_SELF_TEST) |
| 78 | /* |
| 79 | * Counts of point addition and doubling, and field multiplications. |
| 80 | * Used to test resistance of point multiplication to simple timing attacks. |
| 81 | */ |
| 82 | static unsigned long add_count, dbl_count, mul_count; |
| 83 | #endif |
| 84 | |
| 85 | #if defined(MBEDCRYPTO_ECP_DP_SECP192R1_ENABLED) || \ |
| 86 | defined(MBEDCRYPTO_ECP_DP_SECP224R1_ENABLED) || \ |
| 87 | defined(MBEDCRYPTO_ECP_DP_SECP256R1_ENABLED) || \ |
| 88 | defined(MBEDCRYPTO_ECP_DP_SECP384R1_ENABLED) || \ |
| 89 | defined(MBEDCRYPTO_ECP_DP_SECP521R1_ENABLED) || \ |
| 90 | defined(MBEDCRYPTO_ECP_DP_BP256R1_ENABLED) || \ |
| 91 | defined(MBEDCRYPTO_ECP_DP_BP384R1_ENABLED) || \ |
| 92 | defined(MBEDCRYPTO_ECP_DP_BP512R1_ENABLED) || \ |
| 93 | defined(MBEDCRYPTO_ECP_DP_SECP192K1_ENABLED) || \ |
| 94 | defined(MBEDCRYPTO_ECP_DP_SECP224K1_ENABLED) || \ |
| 95 | defined(MBEDCRYPTO_ECP_DP_SECP256K1_ENABLED) |
| 96 | #define ECP_SHORTWEIERSTRASS |
| 97 | #endif |
| 98 | |
| 99 | #if defined(MBEDCRYPTO_ECP_DP_CURVE25519_ENABLED) || \ |
| 100 | defined(MBEDCRYPTO_ECP_DP_CURVE448_ENABLED) |
| 101 | #define ECP_MONTGOMERY |
| 102 | #endif |
| 103 | |
| 104 | /* |
| 105 | * Curve types: internal for now, might be exposed later |
| 106 | */ |
| 107 | typedef enum |
| 108 | { |
| 109 | ECP_TYPE_NONE = 0, |
| 110 | ECP_TYPE_SHORT_WEIERSTRASS, /* y^2 = x^3 + a x + b */ |
| 111 | ECP_TYPE_MONTGOMERY, /* y^2 = x^3 + a x^2 + x */ |
| 112 | } ecp_curve_type; |
| 113 | |
| 114 | /* |
| 115 | * List of supported curves: |
| 116 | * - internal ID |
| 117 | * - TLS NamedCurve ID (RFC 4492 sec. 5.1.1, RFC 7071 sec. 2) |
| 118 | * - size in bits |
| 119 | * - readable name |
| 120 | * |
| 121 | * Curves are listed in order: largest curves first, and for a given size, |
| 122 | * fastest curves first. This provides the default order for the SSL module. |
| 123 | * |
| 124 | * Reminder: update profiles in x509_crt.c when adding a new curves! |
| 125 | */ |
| 126 | static const mbedcrypto_ecp_curve_info ecp_supported_curves[] = |
| 127 | { |
| 128 | #if defined(MBEDCRYPTO_ECP_DP_SECP521R1_ENABLED) |
| 129 | { MBEDCRYPTO_ECP_DP_SECP521R1, 25, 521, "secp521r1" }, |
| 130 | #endif |
| 131 | #if defined(MBEDCRYPTO_ECP_DP_BP512R1_ENABLED) |
| 132 | { MBEDCRYPTO_ECP_DP_BP512R1, 28, 512, "brainpoolP512r1" }, |
| 133 | #endif |
| 134 | #if defined(MBEDCRYPTO_ECP_DP_SECP384R1_ENABLED) |
| 135 | { MBEDCRYPTO_ECP_DP_SECP384R1, 24, 384, "secp384r1" }, |
| 136 | #endif |
| 137 | #if defined(MBEDCRYPTO_ECP_DP_BP384R1_ENABLED) |
| 138 | { MBEDCRYPTO_ECP_DP_BP384R1, 27, 384, "brainpoolP384r1" }, |
| 139 | #endif |
| 140 | #if defined(MBEDCRYPTO_ECP_DP_SECP256R1_ENABLED) |
| 141 | { MBEDCRYPTO_ECP_DP_SECP256R1, 23, 256, "secp256r1" }, |
| 142 | #endif |
| 143 | #if defined(MBEDCRYPTO_ECP_DP_SECP256K1_ENABLED) |
| 144 | { MBEDCRYPTO_ECP_DP_SECP256K1, 22, 256, "secp256k1" }, |
| 145 | #endif |
| 146 | #if defined(MBEDCRYPTO_ECP_DP_BP256R1_ENABLED) |
| 147 | { MBEDCRYPTO_ECP_DP_BP256R1, 26, 256, "brainpoolP256r1" }, |
| 148 | #endif |
| 149 | #if defined(MBEDCRYPTO_ECP_DP_SECP224R1_ENABLED) |
| 150 | { MBEDCRYPTO_ECP_DP_SECP224R1, 21, 224, "secp224r1" }, |
| 151 | #endif |
| 152 | #if defined(MBEDCRYPTO_ECP_DP_SECP224K1_ENABLED) |
| 153 | { MBEDCRYPTO_ECP_DP_SECP224K1, 20, 224, "secp224k1" }, |
| 154 | #endif |
| 155 | #if defined(MBEDCRYPTO_ECP_DP_SECP192R1_ENABLED) |
| 156 | { MBEDCRYPTO_ECP_DP_SECP192R1, 19, 192, "secp192r1" }, |
| 157 | #endif |
| 158 | #if defined(MBEDCRYPTO_ECP_DP_SECP192K1_ENABLED) |
| 159 | { MBEDCRYPTO_ECP_DP_SECP192K1, 18, 192, "secp192k1" }, |
| 160 | #endif |
| 161 | { MBEDCRYPTO_ECP_DP_NONE, 0, 0, NULL }, |
| 162 | }; |
| 163 | |
| 164 | #define ECP_NB_CURVES sizeof( ecp_supported_curves ) / \ |
| 165 | sizeof( ecp_supported_curves[0] ) |
| 166 | |
| 167 | static mbedcrypto_ecp_group_id ecp_supported_grp_id[ECP_NB_CURVES]; |
| 168 | |
| 169 | /* |
| 170 | * List of supported curves and associated info |
| 171 | */ |
| 172 | const mbedcrypto_ecp_curve_info *mbedcrypto_ecp_curve_list( void ) |
| 173 | { |
| 174 | return( ecp_supported_curves ); |
| 175 | } |
| 176 | |
| 177 | /* |
| 178 | * List of supported curves, group ID only |
| 179 | */ |
| 180 | const mbedcrypto_ecp_group_id *mbedcrypto_ecp_grp_id_list( void ) |
| 181 | { |
| 182 | static int init_done = 0; |
| 183 | |
| 184 | if( ! init_done ) |
| 185 | { |
| 186 | size_t i = 0; |
| 187 | const mbedcrypto_ecp_curve_info *curve_info; |
| 188 | |
| 189 | for( curve_info = mbedcrypto_ecp_curve_list(); |
| 190 | curve_info->grp_id != MBEDCRYPTO_ECP_DP_NONE; |
| 191 | curve_info++ ) |
| 192 | { |
| 193 | ecp_supported_grp_id[i++] = curve_info->grp_id; |
| 194 | } |
| 195 | ecp_supported_grp_id[i] = MBEDCRYPTO_ECP_DP_NONE; |
| 196 | |
| 197 | init_done = 1; |
| 198 | } |
| 199 | |
| 200 | return( ecp_supported_grp_id ); |
| 201 | } |
| 202 | |
| 203 | /* |
| 204 | * Get the curve info for the internal identifier |
| 205 | */ |
| 206 | const mbedcrypto_ecp_curve_info *mbedcrypto_ecp_curve_info_from_grp_id( mbedcrypto_ecp_group_id grp_id ) |
| 207 | { |
| 208 | const mbedcrypto_ecp_curve_info *curve_info; |
| 209 | |
| 210 | for( curve_info = mbedcrypto_ecp_curve_list(); |
| 211 | curve_info->grp_id != MBEDCRYPTO_ECP_DP_NONE; |
| 212 | curve_info++ ) |
| 213 | { |
| 214 | if( curve_info->grp_id == grp_id ) |
| 215 | return( curve_info ); |
| 216 | } |
| 217 | |
| 218 | return( NULL ); |
| 219 | } |
| 220 | |
| 221 | /* |
| 222 | * Get the curve info from the TLS identifier |
| 223 | */ |
| 224 | const mbedcrypto_ecp_curve_info *mbedcrypto_ecp_curve_info_from_tls_id( uint16_t tls_id ) |
| 225 | { |
| 226 | const mbedcrypto_ecp_curve_info *curve_info; |
| 227 | |
| 228 | for( curve_info = mbedcrypto_ecp_curve_list(); |
| 229 | curve_info->grp_id != MBEDCRYPTO_ECP_DP_NONE; |
| 230 | curve_info++ ) |
| 231 | { |
| 232 | if( curve_info->tls_id == tls_id ) |
| 233 | return( curve_info ); |
| 234 | } |
| 235 | |
| 236 | return( NULL ); |
| 237 | } |
| 238 | |
| 239 | /* |
| 240 | * Get the curve info from the name |
| 241 | */ |
| 242 | const mbedcrypto_ecp_curve_info *mbedcrypto_ecp_curve_info_from_name( const char *name ) |
| 243 | { |
| 244 | const mbedcrypto_ecp_curve_info *curve_info; |
| 245 | |
| 246 | for( curve_info = mbedcrypto_ecp_curve_list(); |
| 247 | curve_info->grp_id != MBEDCRYPTO_ECP_DP_NONE; |
| 248 | curve_info++ ) |
| 249 | { |
| 250 | if( strcmp( curve_info->name, name ) == 0 ) |
| 251 | return( curve_info ); |
| 252 | } |
| 253 | |
| 254 | return( NULL ); |
| 255 | } |
| 256 | |
| 257 | /* |
| 258 | * Get the type of a curve |
| 259 | */ |
| 260 | static inline ecp_curve_type ecp_get_type( const mbedcrypto_ecp_group *grp ) |
| 261 | { |
| 262 | if( grp->G.X.p == NULL ) |
| 263 | return( ECP_TYPE_NONE ); |
| 264 | |
| 265 | if( grp->G.Y.p == NULL ) |
| 266 | return( ECP_TYPE_MONTGOMERY ); |
| 267 | else |
| 268 | return( ECP_TYPE_SHORT_WEIERSTRASS ); |
| 269 | } |
| 270 | |
| 271 | /* |
| 272 | * Initialize (the components of) a point |
| 273 | */ |
| 274 | void mbedcrypto_ecp_point_init( mbedcrypto_ecp_point *pt ) |
| 275 | { |
| 276 | if( pt == NULL ) |
| 277 | return; |
| 278 | |
| 279 | mbedcrypto_mpi_init( &pt->X ); |
| 280 | mbedcrypto_mpi_init( &pt->Y ); |
| 281 | mbedcrypto_mpi_init( &pt->Z ); |
| 282 | } |
| 283 | |
| 284 | /* |
| 285 | * Initialize (the components of) a group |
| 286 | */ |
| 287 | void mbedcrypto_ecp_group_init( mbedcrypto_ecp_group *grp ) |
| 288 | { |
| 289 | if( grp == NULL ) |
| 290 | return; |
| 291 | |
| 292 | memset( grp, 0, sizeof( mbedcrypto_ecp_group ) ); |
| 293 | } |
| 294 | |
| 295 | /* |
| 296 | * Initialize (the components of) a key pair |
| 297 | */ |
| 298 | void mbedcrypto_ecp_keypair_init( mbedcrypto_ecp_keypair *key ) |
| 299 | { |
| 300 | if( key == NULL ) |
| 301 | return; |
| 302 | |
| 303 | mbedcrypto_ecp_group_init( &key->grp ); |
| 304 | mbedcrypto_mpi_init( &key->d ); |
| 305 | mbedcrypto_ecp_point_init( &key->Q ); |
| 306 | } |
| 307 | |
| 308 | /* |
| 309 | * Unallocate (the components of) a point |
| 310 | */ |
| 311 | void mbedcrypto_ecp_point_free( mbedcrypto_ecp_point *pt ) |
| 312 | { |
| 313 | if( pt == NULL ) |
| 314 | return; |
| 315 | |
| 316 | mbedcrypto_mpi_free( &( pt->X ) ); |
| 317 | mbedcrypto_mpi_free( &( pt->Y ) ); |
| 318 | mbedcrypto_mpi_free( &( pt->Z ) ); |
| 319 | } |
| 320 | |
| 321 | /* |
| 322 | * Unallocate (the components of) a group |
| 323 | */ |
| 324 | void mbedcrypto_ecp_group_free( mbedcrypto_ecp_group *grp ) |
| 325 | { |
| 326 | size_t i; |
| 327 | |
| 328 | if( grp == NULL ) |
| 329 | return; |
| 330 | |
| 331 | if( grp->h != 1 ) |
| 332 | { |
| 333 | mbedcrypto_mpi_free( &grp->P ); |
| 334 | mbedcrypto_mpi_free( &grp->A ); |
| 335 | mbedcrypto_mpi_free( &grp->B ); |
| 336 | mbedcrypto_ecp_point_free( &grp->G ); |
| 337 | mbedcrypto_mpi_free( &grp->N ); |
| 338 | } |
| 339 | |
| 340 | if( grp->T != NULL ) |
| 341 | { |
| 342 | for( i = 0; i < grp->T_size; i++ ) |
| 343 | mbedcrypto_ecp_point_free( &grp->T[i] ); |
| 344 | mbedcrypto_free( grp->T ); |
| 345 | } |
| 346 | |
| 347 | mbedcrypto_platform_zeroize( grp, sizeof( mbedcrypto_ecp_group ) ); |
| 348 | } |
| 349 | |
| 350 | /* |
| 351 | * Unallocate (the components of) a key pair |
| 352 | */ |
| 353 | void mbedcrypto_ecp_keypair_free( mbedcrypto_ecp_keypair *key ) |
| 354 | { |
| 355 | if( key == NULL ) |
| 356 | return; |
| 357 | |
| 358 | mbedcrypto_ecp_group_free( &key->grp ); |
| 359 | mbedcrypto_mpi_free( &key->d ); |
| 360 | mbedcrypto_ecp_point_free( &key->Q ); |
| 361 | } |
| 362 | |
| 363 | /* |
| 364 | * Copy the contents of a point |
| 365 | */ |
| 366 | int mbedcrypto_ecp_copy( mbedcrypto_ecp_point *P, const mbedcrypto_ecp_point *Q ) |
| 367 | { |
| 368 | int ret; |
| 369 | |
| 370 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_copy( &P->X, &Q->X ) ); |
| 371 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_copy( &P->Y, &Q->Y ) ); |
| 372 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_copy( &P->Z, &Q->Z ) ); |
| 373 | |
| 374 | cleanup: |
| 375 | return( ret ); |
| 376 | } |
| 377 | |
| 378 | /* |
| 379 | * Copy the contents of a group object |
| 380 | */ |
| 381 | int mbedcrypto_ecp_group_copy( mbedcrypto_ecp_group *dst, const mbedcrypto_ecp_group *src ) |
| 382 | { |
| 383 | return mbedcrypto_ecp_group_load( dst, src->id ); |
| 384 | } |
| 385 | |
| 386 | /* |
| 387 | * Set point to zero |
| 388 | */ |
| 389 | int mbedcrypto_ecp_set_zero( mbedcrypto_ecp_point *pt ) |
| 390 | { |
| 391 | int ret; |
| 392 | |
| 393 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_lset( &pt->X , 1 ) ); |
| 394 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_lset( &pt->Y , 1 ) ); |
| 395 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_lset( &pt->Z , 0 ) ); |
| 396 | |
| 397 | cleanup: |
| 398 | return( ret ); |
| 399 | } |
| 400 | |
| 401 | /* |
| 402 | * Tell if a point is zero |
| 403 | */ |
| 404 | int mbedcrypto_ecp_is_zero( mbedcrypto_ecp_point *pt ) |
| 405 | { |
| 406 | return( mbedcrypto_mpi_cmp_int( &pt->Z, 0 ) == 0 ); |
| 407 | } |
| 408 | |
| 409 | /* |
| 410 | * Compare two points lazyly |
| 411 | */ |
| 412 | int mbedcrypto_ecp_point_cmp( const mbedcrypto_ecp_point *P, |
| 413 | const mbedcrypto_ecp_point *Q ) |
| 414 | { |
| 415 | if( mbedcrypto_mpi_cmp_mpi( &P->X, &Q->X ) == 0 && |
| 416 | mbedcrypto_mpi_cmp_mpi( &P->Y, &Q->Y ) == 0 && |
| 417 | mbedcrypto_mpi_cmp_mpi( &P->Z, &Q->Z ) == 0 ) |
| 418 | { |
| 419 | return( 0 ); |
| 420 | } |
| 421 | |
| 422 | return( MBEDCRYPTO_ERR_ECP_BAD_INPUT_DATA ); |
| 423 | } |
| 424 | |
| 425 | /* |
| 426 | * Import a non-zero point from ASCII strings |
| 427 | */ |
| 428 | int mbedcrypto_ecp_point_read_string( mbedcrypto_ecp_point *P, int radix, |
| 429 | const char *x, const char *y ) |
| 430 | { |
| 431 | int ret; |
| 432 | |
| 433 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_read_string( &P->X, radix, x ) ); |
| 434 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_read_string( &P->Y, radix, y ) ); |
| 435 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_lset( &P->Z, 1 ) ); |
| 436 | |
| 437 | cleanup: |
| 438 | return( ret ); |
| 439 | } |
| 440 | |
| 441 | /* |
| 442 | * Export a point into unsigned binary data (SEC1 2.3.3) |
| 443 | */ |
| 444 | int mbedcrypto_ecp_point_write_binary( const mbedcrypto_ecp_group *grp, const mbedcrypto_ecp_point *P, |
| 445 | int format, size_t *olen, |
| 446 | unsigned char *buf, size_t buflen ) |
| 447 | { |
| 448 | int ret = 0; |
| 449 | size_t plen; |
| 450 | |
| 451 | if( format != MBEDCRYPTO_ECP_PF_UNCOMPRESSED && |
| 452 | format != MBEDCRYPTO_ECP_PF_COMPRESSED ) |
| 453 | return( MBEDCRYPTO_ERR_ECP_BAD_INPUT_DATA ); |
| 454 | |
| 455 | /* |
| 456 | * Common case: P == 0 |
| 457 | */ |
| 458 | if( mbedcrypto_mpi_cmp_int( &P->Z, 0 ) == 0 ) |
| 459 | { |
| 460 | if( buflen < 1 ) |
| 461 | return( MBEDCRYPTO_ERR_ECP_BUFFER_TOO_SMALL ); |
| 462 | |
| 463 | buf[0] = 0x00; |
| 464 | *olen = 1; |
| 465 | |
| 466 | return( 0 ); |
| 467 | } |
| 468 | |
| 469 | plen = mbedcrypto_mpi_size( &grp->P ); |
| 470 | |
| 471 | if( format == MBEDCRYPTO_ECP_PF_UNCOMPRESSED ) |
| 472 | { |
| 473 | *olen = 2 * plen + 1; |
| 474 | |
| 475 | if( buflen < *olen ) |
| 476 | return( MBEDCRYPTO_ERR_ECP_BUFFER_TOO_SMALL ); |
| 477 | |
| 478 | buf[0] = 0x04; |
| 479 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_write_binary( &P->X, buf + 1, plen ) ); |
| 480 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_write_binary( &P->Y, buf + 1 + plen, plen ) ); |
| 481 | } |
| 482 | else if( format == MBEDCRYPTO_ECP_PF_COMPRESSED ) |
| 483 | { |
| 484 | *olen = plen + 1; |
| 485 | |
| 486 | if( buflen < *olen ) |
| 487 | return( MBEDCRYPTO_ERR_ECP_BUFFER_TOO_SMALL ); |
| 488 | |
| 489 | buf[0] = 0x02 + mbedcrypto_mpi_get_bit( &P->Y, 0 ); |
| 490 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_write_binary( &P->X, buf + 1, plen ) ); |
| 491 | } |
| 492 | |
| 493 | cleanup: |
| 494 | return( ret ); |
| 495 | } |
| 496 | |
| 497 | /* |
| 498 | * Import a point from unsigned binary data (SEC1 2.3.4) |
| 499 | */ |
| 500 | int mbedcrypto_ecp_point_read_binary( const mbedcrypto_ecp_group *grp, mbedcrypto_ecp_point *pt, |
| 501 | const unsigned char *buf, size_t ilen ) |
| 502 | { |
| 503 | int ret; |
| 504 | size_t plen; |
| 505 | |
| 506 | if( ilen < 1 ) |
| 507 | return( MBEDCRYPTO_ERR_ECP_BAD_INPUT_DATA ); |
| 508 | |
| 509 | if( buf[0] == 0x00 ) |
| 510 | { |
| 511 | if( ilen == 1 ) |
| 512 | return( mbedcrypto_ecp_set_zero( pt ) ); |
| 513 | else |
| 514 | return( MBEDCRYPTO_ERR_ECP_BAD_INPUT_DATA ); |
| 515 | } |
| 516 | |
| 517 | plen = mbedcrypto_mpi_size( &grp->P ); |
| 518 | |
| 519 | if( buf[0] != 0x04 ) |
| 520 | return( MBEDCRYPTO_ERR_ECP_FEATURE_UNAVAILABLE ); |
| 521 | |
| 522 | if( ilen != 2 * plen + 1 ) |
| 523 | return( MBEDCRYPTO_ERR_ECP_BAD_INPUT_DATA ); |
| 524 | |
| 525 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_read_binary( &pt->X, buf + 1, plen ) ); |
| 526 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_read_binary( &pt->Y, buf + 1 + plen, plen ) ); |
| 527 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_lset( &pt->Z, 1 ) ); |
| 528 | |
| 529 | cleanup: |
| 530 | return( ret ); |
| 531 | } |
| 532 | |
| 533 | /* |
| 534 | * Import a point from a TLS ECPoint record (RFC 4492) |
| 535 | * struct { |
| 536 | * opaque point <1..2^8-1>; |
| 537 | * } ECPoint; |
| 538 | */ |
| 539 | int mbedcrypto_ecp_tls_read_point( const mbedcrypto_ecp_group *grp, mbedcrypto_ecp_point *pt, |
| 540 | const unsigned char **buf, size_t buf_len ) |
| 541 | { |
| 542 | unsigned char data_len; |
| 543 | const unsigned char *buf_start; |
| 544 | |
| 545 | /* |
| 546 | * We must have at least two bytes (1 for length, at least one for data) |
| 547 | */ |
| 548 | if( buf_len < 2 ) |
| 549 | return( MBEDCRYPTO_ERR_ECP_BAD_INPUT_DATA ); |
| 550 | |
| 551 | data_len = *(*buf)++; |
| 552 | if( data_len < 1 || data_len > buf_len - 1 ) |
| 553 | return( MBEDCRYPTO_ERR_ECP_BAD_INPUT_DATA ); |
| 554 | |
| 555 | /* |
| 556 | * Save buffer start for read_binary and update buf |
| 557 | */ |
| 558 | buf_start = *buf; |
| 559 | *buf += data_len; |
| 560 | |
| 561 | return mbedcrypto_ecp_point_read_binary( grp, pt, buf_start, data_len ); |
| 562 | } |
| 563 | |
| 564 | /* |
| 565 | * Export a point as a TLS ECPoint record (RFC 4492) |
| 566 | * struct { |
| 567 | * opaque point <1..2^8-1>; |
| 568 | * } ECPoint; |
| 569 | */ |
| 570 | int mbedcrypto_ecp_tls_write_point( const mbedcrypto_ecp_group *grp, const mbedcrypto_ecp_point *pt, |
| 571 | int format, size_t *olen, |
| 572 | unsigned char *buf, size_t blen ) |
| 573 | { |
| 574 | int ret; |
| 575 | |
| 576 | /* |
| 577 | * buffer length must be at least one, for our length byte |
| 578 | */ |
| 579 | if( blen < 1 ) |
| 580 | return( MBEDCRYPTO_ERR_ECP_BAD_INPUT_DATA ); |
| 581 | |
| 582 | if( ( ret = mbedcrypto_ecp_point_write_binary( grp, pt, format, |
| 583 | olen, buf + 1, blen - 1) ) != 0 ) |
| 584 | return( ret ); |
| 585 | |
| 586 | /* |
| 587 | * write length to the first byte and update total length |
| 588 | */ |
| 589 | buf[0] = (unsigned char) *olen; |
| 590 | ++*olen; |
| 591 | |
| 592 | return( 0 ); |
| 593 | } |
| 594 | |
| 595 | /* |
| 596 | * Set a group from an ECParameters record (RFC 4492) |
| 597 | */ |
| 598 | int mbedcrypto_ecp_tls_read_group( mbedcrypto_ecp_group *grp, const unsigned char **buf, size_t len ) |
| 599 | { |
| 600 | uint16_t tls_id; |
| 601 | const mbedcrypto_ecp_curve_info *curve_info; |
| 602 | |
| 603 | /* |
| 604 | * We expect at least three bytes (see below) |
| 605 | */ |
| 606 | if( len < 3 ) |
| 607 | return( MBEDCRYPTO_ERR_ECP_BAD_INPUT_DATA ); |
| 608 | |
| 609 | /* |
| 610 | * First byte is curve_type; only named_curve is handled |
| 611 | */ |
| 612 | if( *(*buf)++ != MBEDCRYPTO_ECP_TLS_NAMED_CURVE ) |
| 613 | return( MBEDCRYPTO_ERR_ECP_BAD_INPUT_DATA ); |
| 614 | |
| 615 | /* |
| 616 | * Next two bytes are the namedcurve value |
| 617 | */ |
| 618 | tls_id = *(*buf)++; |
| 619 | tls_id <<= 8; |
| 620 | tls_id |= *(*buf)++; |
| 621 | |
| 622 | if( ( curve_info = mbedcrypto_ecp_curve_info_from_tls_id( tls_id ) ) == NULL ) |
| 623 | return( MBEDCRYPTO_ERR_ECP_FEATURE_UNAVAILABLE ); |
| 624 | |
| 625 | return mbedcrypto_ecp_group_load( grp, curve_info->grp_id ); |
| 626 | } |
| 627 | |
| 628 | /* |
| 629 | * Write the ECParameters record corresponding to a group (RFC 4492) |
| 630 | */ |
| 631 | int mbedcrypto_ecp_tls_write_group( const mbedcrypto_ecp_group *grp, size_t *olen, |
| 632 | unsigned char *buf, size_t blen ) |
| 633 | { |
| 634 | const mbedcrypto_ecp_curve_info *curve_info; |
| 635 | |
| 636 | if( ( curve_info = mbedcrypto_ecp_curve_info_from_grp_id( grp->id ) ) == NULL ) |
| 637 | return( MBEDCRYPTO_ERR_ECP_BAD_INPUT_DATA ); |
| 638 | |
| 639 | /* |
| 640 | * We are going to write 3 bytes (see below) |
| 641 | */ |
| 642 | *olen = 3; |
| 643 | if( blen < *olen ) |
| 644 | return( MBEDCRYPTO_ERR_ECP_BUFFER_TOO_SMALL ); |
| 645 | |
| 646 | /* |
| 647 | * First byte is curve_type, always named_curve |
| 648 | */ |
| 649 | *buf++ = MBEDCRYPTO_ECP_TLS_NAMED_CURVE; |
| 650 | |
| 651 | /* |
| 652 | * Next two bytes are the namedcurve value |
| 653 | */ |
| 654 | buf[0] = curve_info->tls_id >> 8; |
| 655 | buf[1] = curve_info->tls_id & 0xFF; |
| 656 | |
| 657 | return( 0 ); |
| 658 | } |
| 659 | |
| 660 | /* |
| 661 | * Wrapper around fast quasi-modp functions, with fall-back to mbedcrypto_mpi_mod_mpi. |
| 662 | * See the documentation of struct mbedcrypto_ecp_group. |
| 663 | * |
| 664 | * This function is in the critial loop for mbedcrypto_ecp_mul, so pay attention to perf. |
| 665 | */ |
| 666 | static int ecp_modp( mbedcrypto_mpi *N, const mbedcrypto_ecp_group *grp ) |
| 667 | { |
| 668 | int ret; |
| 669 | |
| 670 | if( grp->modp == NULL ) |
| 671 | return( mbedcrypto_mpi_mod_mpi( N, N, &grp->P ) ); |
| 672 | |
| 673 | /* N->s < 0 is a much faster test, which fails only if N is 0 */ |
| 674 | if( ( N->s < 0 && mbedcrypto_mpi_cmp_int( N, 0 ) != 0 ) || |
| 675 | mbedcrypto_mpi_bitlen( N ) > 2 * grp->pbits ) |
| 676 | { |
| 677 | return( MBEDCRYPTO_ERR_ECP_BAD_INPUT_DATA ); |
| 678 | } |
| 679 | |
| 680 | MBEDCRYPTO_MPI_CHK( grp->modp( N ) ); |
| 681 | |
| 682 | /* N->s < 0 is a much faster test, which fails only if N is 0 */ |
| 683 | while( N->s < 0 && mbedcrypto_mpi_cmp_int( N, 0 ) != 0 ) |
| 684 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_add_mpi( N, N, &grp->P ) ); |
| 685 | |
| 686 | while( mbedcrypto_mpi_cmp_mpi( N, &grp->P ) >= 0 ) |
| 687 | /* we known P, N and the result are positive */ |
| 688 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_sub_abs( N, N, &grp->P ) ); |
| 689 | |
| 690 | cleanup: |
| 691 | return( ret ); |
| 692 | } |
| 693 | |
| 694 | /* |
| 695 | * Fast mod-p functions expect their argument to be in the 0..p^2 range. |
| 696 | * |
| 697 | * In order to guarantee that, we need to ensure that operands of |
| 698 | * mbedcrypto_mpi_mul_mpi are in the 0..p range. So, after each operation we will |
| 699 | * bring the result back to this range. |
| 700 | * |
| 701 | * The following macros are shortcuts for doing that. |
| 702 | */ |
| 703 | |
| 704 | /* |
| 705 | * Reduce a mbedcrypto_mpi mod p in-place, general case, to use after mbedcrypto_mpi_mul_mpi |
| 706 | */ |
| 707 | #if defined(MBEDCRYPTO_SELF_TEST) |
| 708 | #define INC_MUL_COUNT mul_count++; |
| 709 | #else |
| 710 | #define INC_MUL_COUNT |
| 711 | #endif |
| 712 | |
| 713 | #define MOD_MUL( N ) do { MBEDCRYPTO_MPI_CHK( ecp_modp( &N, grp ) ); INC_MUL_COUNT } \ |
| 714 | while( 0 ) |
| 715 | |
| 716 | /* |
| 717 | * Reduce a mbedcrypto_mpi mod p in-place, to use after mbedcrypto_mpi_sub_mpi |
| 718 | * N->s < 0 is a very fast test, which fails only if N is 0 |
| 719 | */ |
| 720 | #define MOD_SUB( N ) \ |
| 721 | while( N.s < 0 && mbedcrypto_mpi_cmp_int( &N, 0 ) != 0 ) \ |
| 722 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_add_mpi( &N, &N, &grp->P ) ) |
| 723 | |
| 724 | /* |
| 725 | * Reduce a mbedcrypto_mpi mod p in-place, to use after mbedcrypto_mpi_add_mpi and mbedcrypto_mpi_mul_int. |
| 726 | * We known P, N and the result are positive, so sub_abs is correct, and |
| 727 | * a bit faster. |
| 728 | */ |
| 729 | #define MOD_ADD( N ) \ |
| 730 | while( mbedcrypto_mpi_cmp_mpi( &N, &grp->P ) >= 0 ) \ |
| 731 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_sub_abs( &N, &N, &grp->P ) ) |
| 732 | |
| 733 | #if defined(ECP_SHORTWEIERSTRASS) |
| 734 | /* |
| 735 | * For curves in short Weierstrass form, we do all the internal operations in |
| 736 | * Jacobian coordinates. |
| 737 | * |
| 738 | * For multiplication, we'll use a comb method with coutermeasueres against |
| 739 | * SPA, hence timing attacks. |
| 740 | */ |
| 741 | |
| 742 | /* |
| 743 | * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1) |
| 744 | * Cost: 1N := 1I + 3M + 1S |
| 745 | */ |
| 746 | static int ecp_normalize_jac( const mbedcrypto_ecp_group *grp, mbedcrypto_ecp_point *pt ) |
| 747 | { |
| 748 | int ret; |
| 749 | mbedcrypto_mpi Zi, ZZi; |
| 750 | |
| 751 | if( mbedcrypto_mpi_cmp_int( &pt->Z, 0 ) == 0 ) |
| 752 | return( 0 ); |
| 753 | |
| 754 | #if defined(MBEDCRYPTO_ECP_NORMALIZE_JAC_ALT) |
| 755 | if ( mbedcrypto_internal_ecp_grp_capable( grp ) ) |
| 756 | { |
| 757 | return mbedcrypto_internal_ecp_normalize_jac( grp, pt ); |
| 758 | } |
| 759 | #endif /* MBEDCRYPTO_ECP_NORMALIZE_JAC_ALT */ |
| 760 | mbedcrypto_mpi_init( &Zi ); mbedcrypto_mpi_init( &ZZi ); |
| 761 | |
| 762 | /* |
| 763 | * X = X / Z^2 mod p |
| 764 | */ |
| 765 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_inv_mod( &Zi, &pt->Z, &grp->P ) ); |
| 766 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi ); |
| 767 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_mpi( &pt->X, &pt->X, &ZZi ) ); MOD_MUL( pt->X ); |
| 768 | |
| 769 | /* |
| 770 | * Y = Y / Z^3 mod p |
| 771 | */ |
| 772 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_mpi( &pt->Y, &pt->Y, &ZZi ) ); MOD_MUL( pt->Y ); |
| 773 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_mpi( &pt->Y, &pt->Y, &Zi ) ); MOD_MUL( pt->Y ); |
| 774 | |
| 775 | /* |
| 776 | * Z = 1 |
| 777 | */ |
| 778 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_lset( &pt->Z, 1 ) ); |
| 779 | |
| 780 | cleanup: |
| 781 | |
| 782 | mbedcrypto_mpi_free( &Zi ); mbedcrypto_mpi_free( &ZZi ); |
| 783 | |
| 784 | return( ret ); |
| 785 | } |
| 786 | |
| 787 | /* |
| 788 | * Normalize jacobian coordinates of an array of (pointers to) points, |
| 789 | * using Montgomery's trick to perform only one inversion mod P. |
| 790 | * (See for example Cohen's "A Course in Computational Algebraic Number |
| 791 | * Theory", Algorithm 10.3.4.) |
| 792 | * |
| 793 | * Warning: fails (returning an error) if one of the points is zero! |
| 794 | * This should never happen, see choice of w in ecp_mul_comb(). |
| 795 | * |
| 796 | * Cost: 1N(t) := 1I + (6t - 3)M + 1S |
| 797 | */ |
| 798 | static int ecp_normalize_jac_many( const mbedcrypto_ecp_group *grp, |
| 799 | mbedcrypto_ecp_point *T[], size_t t_len ) |
| 800 | { |
| 801 | int ret; |
| 802 | size_t i; |
| 803 | mbedcrypto_mpi *c, u, Zi, ZZi; |
| 804 | |
| 805 | if( t_len < 2 ) |
| 806 | return( ecp_normalize_jac( grp, *T ) ); |
| 807 | |
| 808 | #if defined(MBEDCRYPTO_ECP_NORMALIZE_JAC_MANY_ALT) |
| 809 | if ( mbedcrypto_internal_ecp_grp_capable( grp ) ) |
| 810 | { |
| 811 | return mbedcrypto_internal_ecp_normalize_jac_many(grp, T, t_len); |
| 812 | } |
| 813 | #endif |
| 814 | |
| 815 | if( ( c = mbedcrypto_calloc( t_len, sizeof( mbedcrypto_mpi ) ) ) == NULL ) |
| 816 | return( MBEDCRYPTO_ERR_ECP_ALLOC_FAILED ); |
| 817 | |
| 818 | mbedcrypto_mpi_init( &u ); mbedcrypto_mpi_init( &Zi ); mbedcrypto_mpi_init( &ZZi ); |
| 819 | |
| 820 | /* |
| 821 | * c[i] = Z_0 * ... * Z_i |
| 822 | */ |
| 823 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_copy( &c[0], &T[0]->Z ) ); |
| 824 | for( i = 1; i < t_len; i++ ) |
| 825 | { |
| 826 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_mpi( &c[i], &c[i-1], &T[i]->Z ) ); |
| 827 | MOD_MUL( c[i] ); |
| 828 | } |
| 829 | |
| 830 | /* |
| 831 | * u = 1 / (Z_0 * ... * Z_n) mod P |
| 832 | */ |
| 833 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_inv_mod( &u, &c[t_len-1], &grp->P ) ); |
| 834 | |
| 835 | for( i = t_len - 1; ; i-- ) |
| 836 | { |
| 837 | /* |
| 838 | * Zi = 1 / Z_i mod p |
| 839 | * u = 1 / (Z_0 * ... * Z_i) mod P |
| 840 | */ |
| 841 | if( i == 0 ) { |
| 842 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_copy( &Zi, &u ) ); |
| 843 | } |
| 844 | else |
| 845 | { |
| 846 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_mpi( &Zi, &u, &c[i-1] ) ); MOD_MUL( Zi ); |
| 847 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_mpi( &u, &u, &T[i]->Z ) ); MOD_MUL( u ); |
| 848 | } |
| 849 | |
| 850 | /* |
| 851 | * proceed as in normalize() |
| 852 | */ |
| 853 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi ); |
| 854 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_mpi( &T[i]->X, &T[i]->X, &ZZi ) ); MOD_MUL( T[i]->X ); |
| 855 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &ZZi ) ); MOD_MUL( T[i]->Y ); |
| 856 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &Zi ) ); MOD_MUL( T[i]->Y ); |
| 857 | |
| 858 | /* |
| 859 | * Post-precessing: reclaim some memory by shrinking coordinates |
| 860 | * - not storing Z (always 1) |
| 861 | * - shrinking other coordinates, but still keeping the same number of |
| 862 | * limbs as P, as otherwise it will too likely be regrown too fast. |
| 863 | */ |
| 864 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_shrink( &T[i]->X, grp->P.n ) ); |
| 865 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_shrink( &T[i]->Y, grp->P.n ) ); |
| 866 | mbedcrypto_mpi_free( &T[i]->Z ); |
| 867 | |
| 868 | if( i == 0 ) |
| 869 | break; |
| 870 | } |
| 871 | |
| 872 | cleanup: |
| 873 | |
| 874 | mbedcrypto_mpi_free( &u ); mbedcrypto_mpi_free( &Zi ); mbedcrypto_mpi_free( &ZZi ); |
| 875 | for( i = 0; i < t_len; i++ ) |
| 876 | mbedcrypto_mpi_free( &c[i] ); |
| 877 | mbedcrypto_free( c ); |
| 878 | |
| 879 | return( ret ); |
| 880 | } |
| 881 | |
| 882 | /* |
| 883 | * Conditional point inversion: Q -> -Q = (Q.X, -Q.Y, Q.Z) without leak. |
| 884 | * "inv" must be 0 (don't invert) or 1 (invert) or the result will be invalid |
| 885 | */ |
| 886 | static int ecp_safe_invert_jac( const mbedcrypto_ecp_group *grp, |
| 887 | mbedcrypto_ecp_point *Q, |
| 888 | unsigned char inv ) |
| 889 | { |
| 890 | int ret; |
| 891 | unsigned char nonzero; |
| 892 | mbedcrypto_mpi mQY; |
| 893 | |
| 894 | mbedcrypto_mpi_init( &mQY ); |
| 895 | |
| 896 | /* Use the fact that -Q.Y mod P = P - Q.Y unless Q.Y == 0 */ |
| 897 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_sub_mpi( &mQY, &grp->P, &Q->Y ) ); |
| 898 | nonzero = mbedcrypto_mpi_cmp_int( &Q->Y, 0 ) != 0; |
| 899 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_safe_cond_assign( &Q->Y, &mQY, inv & nonzero ) ); |
| 900 | |
| 901 | cleanup: |
| 902 | mbedcrypto_mpi_free( &mQY ); |
| 903 | |
| 904 | return( ret ); |
| 905 | } |
| 906 | |
| 907 | /* |
| 908 | * Point doubling R = 2 P, Jacobian coordinates |
| 909 | * |
| 910 | * Based on http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian.html#doubling-dbl-1998-cmo-2 . |
| 911 | * |
| 912 | * We follow the variable naming fairly closely. The formula variations that trade a MUL for a SQR |
| 913 | * (plus a few ADDs) aren't useful as our bignum implementation doesn't distinguish squaring. |
| 914 | * |
| 915 | * Standard optimizations are applied when curve parameter A is one of { 0, -3 }. |
| 916 | * |
| 917 | * Cost: 1D := 3M + 4S (A == 0) |
| 918 | * 4M + 4S (A == -3) |
| 919 | * 3M + 6S + 1a otherwise |
| 920 | */ |
| 921 | static int ecp_double_jac( const mbedcrypto_ecp_group *grp, mbedcrypto_ecp_point *R, |
| 922 | const mbedcrypto_ecp_point *P ) |
| 923 | { |
| 924 | int ret; |
| 925 | mbedcrypto_mpi M, S, T, U; |
| 926 | |
| 927 | #if defined(MBEDCRYPTO_SELF_TEST) |
| 928 | dbl_count++; |
| 929 | #endif |
| 930 | |
| 931 | #if defined(MBEDCRYPTO_ECP_DOUBLE_JAC_ALT) |
| 932 | if ( mbedcrypto_internal_ecp_grp_capable( grp ) ) |
| 933 | { |
| 934 | return mbedcrypto_internal_ecp_double_jac( grp, R, P ); |
| 935 | } |
| 936 | #endif /* MBEDCRYPTO_ECP_DOUBLE_JAC_ALT */ |
| 937 | |
| 938 | mbedcrypto_mpi_init( &M ); mbedcrypto_mpi_init( &S ); mbedcrypto_mpi_init( &T ); mbedcrypto_mpi_init( &U ); |
| 939 | |
| 940 | /* Special case for A = -3 */ |
| 941 | if( grp->A.p == NULL ) |
| 942 | { |
| 943 | /* M = 3(X + Z^2)(X - Z^2) */ |
| 944 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_mpi( &S, &P->Z, &P->Z ) ); MOD_MUL( S ); |
| 945 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_add_mpi( &T, &P->X, &S ) ); MOD_ADD( T ); |
| 946 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_sub_mpi( &U, &P->X, &S ) ); MOD_SUB( U ); |
| 947 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_mpi( &S, &T, &U ) ); MOD_MUL( S ); |
| 948 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_int( &M, &S, 3 ) ); MOD_ADD( M ); |
| 949 | } |
| 950 | else |
| 951 | { |
| 952 | /* M = 3.X^2 */ |
| 953 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_mpi( &S, &P->X, &P->X ) ); MOD_MUL( S ); |
| 954 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_int( &M, &S, 3 ) ); MOD_ADD( M ); |
| 955 | |
| 956 | /* Optimize away for "koblitz" curves with A = 0 */ |
| 957 | if( mbedcrypto_mpi_cmp_int( &grp->A, 0 ) != 0 ) |
| 958 | { |
| 959 | /* M += A.Z^4 */ |
| 960 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_mpi( &S, &P->Z, &P->Z ) ); MOD_MUL( S ); |
| 961 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_mpi( &T, &S, &S ) ); MOD_MUL( T ); |
| 962 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_mpi( &S, &T, &grp->A ) ); MOD_MUL( S ); |
| 963 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_add_mpi( &M, &M, &S ) ); MOD_ADD( M ); |
| 964 | } |
| 965 | } |
| 966 | |
| 967 | /* S = 4.X.Y^2 */ |
| 968 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_mpi( &T, &P->Y, &P->Y ) ); MOD_MUL( T ); |
| 969 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_shift_l( &T, 1 ) ); MOD_ADD( T ); |
| 970 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_mpi( &S, &P->X, &T ) ); MOD_MUL( S ); |
| 971 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_shift_l( &S, 1 ) ); MOD_ADD( S ); |
| 972 | |
| 973 | /* U = 8.Y^4 */ |
| 974 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_mpi( &U, &T, &T ) ); MOD_MUL( U ); |
| 975 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_shift_l( &U, 1 ) ); MOD_ADD( U ); |
| 976 | |
| 977 | /* T = M^2 - 2.S */ |
| 978 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_mpi( &T, &M, &M ) ); MOD_MUL( T ); |
| 979 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_sub_mpi( &T, &T, &S ) ); MOD_SUB( T ); |
| 980 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_sub_mpi( &T, &T, &S ) ); MOD_SUB( T ); |
| 981 | |
| 982 | /* S = M(S - T) - U */ |
| 983 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_sub_mpi( &S, &S, &T ) ); MOD_SUB( S ); |
| 984 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_mpi( &S, &S, &M ) ); MOD_MUL( S ); |
| 985 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_sub_mpi( &S, &S, &U ) ); MOD_SUB( S ); |
| 986 | |
| 987 | /* U = 2.Y.Z */ |
| 988 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_mpi( &U, &P->Y, &P->Z ) ); MOD_MUL( U ); |
| 989 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_shift_l( &U, 1 ) ); MOD_ADD( U ); |
| 990 | |
| 991 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_copy( &R->X, &T ) ); |
| 992 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_copy( &R->Y, &S ) ); |
| 993 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_copy( &R->Z, &U ) ); |
| 994 | |
| 995 | cleanup: |
| 996 | mbedcrypto_mpi_free( &M ); mbedcrypto_mpi_free( &S ); mbedcrypto_mpi_free( &T ); mbedcrypto_mpi_free( &U ); |
| 997 | |
| 998 | return( ret ); |
| 999 | } |
| 1000 | |
| 1001 | /* |
| 1002 | * Addition: R = P + Q, mixed affine-Jacobian coordinates (GECC 3.22) |
| 1003 | * |
| 1004 | * The coordinates of Q must be normalized (= affine), |
| 1005 | * but those of P don't need to. R is not normalized. |
| 1006 | * |
| 1007 | * Special cases: (1) P or Q is zero, (2) R is zero, (3) P == Q. |
| 1008 | * None of these cases can happen as intermediate step in ecp_mul_comb(): |
| 1009 | * - at each step, P, Q and R are multiples of the base point, the factor |
| 1010 | * being less than its order, so none of them is zero; |
| 1011 | * - Q is an odd multiple of the base point, P an even multiple, |
| 1012 | * due to the choice of precomputed points in the modified comb method. |
| 1013 | * So branches for these cases do not leak secret information. |
| 1014 | * |
| 1015 | * We accept Q->Z being unset (saving memory in tables) as meaning 1. |
| 1016 | * |
| 1017 | * Cost: 1A := 8M + 3S |
| 1018 | */ |
| 1019 | static int ecp_add_mixed( const mbedcrypto_ecp_group *grp, mbedcrypto_ecp_point *R, |
| 1020 | const mbedcrypto_ecp_point *P, const mbedcrypto_ecp_point *Q ) |
| 1021 | { |
| 1022 | int ret; |
| 1023 | mbedcrypto_mpi T1, T2, T3, T4, X, Y, Z; |
| 1024 | |
| 1025 | #if defined(MBEDCRYPTO_SELF_TEST) |
| 1026 | add_count++; |
| 1027 | #endif |
| 1028 | |
| 1029 | #if defined(MBEDCRYPTO_ECP_ADD_MIXED_ALT) |
| 1030 | if ( mbedcrypto_internal_ecp_grp_capable( grp ) ) |
| 1031 | { |
| 1032 | return mbedcrypto_internal_ecp_add_mixed( grp, R, P, Q ); |
| 1033 | } |
| 1034 | #endif /* MBEDCRYPTO_ECP_ADD_MIXED_ALT */ |
| 1035 | |
| 1036 | /* |
| 1037 | * Trivial cases: P == 0 or Q == 0 (case 1) |
| 1038 | */ |
| 1039 | if( mbedcrypto_mpi_cmp_int( &P->Z, 0 ) == 0 ) |
| 1040 | return( mbedcrypto_ecp_copy( R, Q ) ); |
| 1041 | |
| 1042 | if( Q->Z.p != NULL && mbedcrypto_mpi_cmp_int( &Q->Z, 0 ) == 0 ) |
| 1043 | return( mbedcrypto_ecp_copy( R, P ) ); |
| 1044 | |
| 1045 | /* |
| 1046 | * Make sure Q coordinates are normalized |
| 1047 | */ |
| 1048 | if( Q->Z.p != NULL && mbedcrypto_mpi_cmp_int( &Q->Z, 1 ) != 0 ) |
| 1049 | return( MBEDCRYPTO_ERR_ECP_BAD_INPUT_DATA ); |
| 1050 | |
| 1051 | mbedcrypto_mpi_init( &T1 ); mbedcrypto_mpi_init( &T2 ); mbedcrypto_mpi_init( &T3 ); mbedcrypto_mpi_init( &T4 ); |
| 1052 | mbedcrypto_mpi_init( &X ); mbedcrypto_mpi_init( &Y ); mbedcrypto_mpi_init( &Z ); |
| 1053 | |
| 1054 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 ); |
| 1055 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 ); |
| 1056 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 ); |
| 1057 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 ); |
| 1058 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 ); |
| 1059 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_sub_mpi( &T2, &T2, &P->Y ) ); MOD_SUB( T2 ); |
| 1060 | |
| 1061 | /* Special cases (2) and (3) */ |
| 1062 | if( mbedcrypto_mpi_cmp_int( &T1, 0 ) == 0 ) |
| 1063 | { |
| 1064 | if( mbedcrypto_mpi_cmp_int( &T2, 0 ) == 0 ) |
| 1065 | { |
| 1066 | ret = ecp_double_jac( grp, R, P ); |
| 1067 | goto cleanup; |
| 1068 | } |
| 1069 | else |
| 1070 | { |
| 1071 | ret = mbedcrypto_ecp_set_zero( R ); |
| 1072 | goto cleanup; |
| 1073 | } |
| 1074 | } |
| 1075 | |
| 1076 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z ); |
| 1077 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 ); |
| 1078 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 ); |
| 1079 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 ); |
| 1080 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 ); |
| 1081 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X ); |
| 1082 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X ); |
| 1083 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X ); |
| 1084 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 ); |
| 1085 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 ); |
| 1086 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 ); |
| 1087 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_sub_mpi( &Y, &T3, &T4 ) ); MOD_SUB( Y ); |
| 1088 | |
| 1089 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_copy( &R->X, &X ) ); |
| 1090 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_copy( &R->Y, &Y ) ); |
| 1091 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_copy( &R->Z, &Z ) ); |
| 1092 | |
| 1093 | cleanup: |
| 1094 | |
| 1095 | mbedcrypto_mpi_free( &T1 ); mbedcrypto_mpi_free( &T2 ); mbedcrypto_mpi_free( &T3 ); mbedcrypto_mpi_free( &T4 ); |
| 1096 | mbedcrypto_mpi_free( &X ); mbedcrypto_mpi_free( &Y ); mbedcrypto_mpi_free( &Z ); |
| 1097 | |
| 1098 | return( ret ); |
| 1099 | } |
| 1100 | |
| 1101 | /* |
| 1102 | * Randomize jacobian coordinates: |
| 1103 | * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l |
| 1104 | * This is sort of the reverse operation of ecp_normalize_jac(). |
| 1105 | * |
| 1106 | * This countermeasure was first suggested in [2]. |
| 1107 | */ |
| 1108 | static int ecp_randomize_jac( const mbedcrypto_ecp_group *grp, mbedcrypto_ecp_point *pt, |
| 1109 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 1110 | { |
| 1111 | int ret; |
| 1112 | mbedcrypto_mpi l, ll; |
| 1113 | size_t p_size; |
| 1114 | int count = 0; |
| 1115 | |
| 1116 | #if defined(MBEDCRYPTO_ECP_RANDOMIZE_JAC_ALT) |
| 1117 | if ( mbedcrypto_internal_ecp_grp_capable( grp ) ) |
| 1118 | { |
| 1119 | return mbedcrypto_internal_ecp_randomize_jac( grp, pt, f_rng, p_rng ); |
| 1120 | } |
| 1121 | #endif /* MBEDCRYPTO_ECP_RANDOMIZE_JAC_ALT */ |
| 1122 | |
| 1123 | p_size = ( grp->pbits + 7 ) / 8; |
| 1124 | mbedcrypto_mpi_init( &l ); mbedcrypto_mpi_init( &ll ); |
| 1125 | |
| 1126 | /* Generate l such that 1 < l < p */ |
| 1127 | do |
| 1128 | { |
| 1129 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_fill_random( &l, p_size, f_rng, p_rng ) ); |
| 1130 | |
| 1131 | while( mbedcrypto_mpi_cmp_mpi( &l, &grp->P ) >= 0 ) |
| 1132 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_shift_r( &l, 1 ) ); |
| 1133 | |
| 1134 | if( count++ > 10 ) |
| 1135 | return( MBEDCRYPTO_ERR_ECP_RANDOM_FAILED ); |
| 1136 | } |
| 1137 | while( mbedcrypto_mpi_cmp_int( &l, 1 ) <= 0 ); |
| 1138 | |
| 1139 | /* Z = l * Z */ |
| 1140 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_mpi( &pt->Z, &pt->Z, &l ) ); MOD_MUL( pt->Z ); |
| 1141 | |
| 1142 | /* X = l^2 * X */ |
| 1143 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_mpi( &ll, &l, &l ) ); MOD_MUL( ll ); |
| 1144 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_mpi( &pt->X, &pt->X, &ll ) ); MOD_MUL( pt->X ); |
| 1145 | |
| 1146 | /* Y = l^3 * Y */ |
| 1147 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_mpi( &ll, &ll, &l ) ); MOD_MUL( ll ); |
| 1148 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_mpi( &pt->Y, &pt->Y, &ll ) ); MOD_MUL( pt->Y ); |
| 1149 | |
| 1150 | cleanup: |
| 1151 | mbedcrypto_mpi_free( &l ); mbedcrypto_mpi_free( &ll ); |
| 1152 | |
| 1153 | return( ret ); |
| 1154 | } |
| 1155 | |
| 1156 | /* |
| 1157 | * Check and define parameters used by the comb method (see below for details) |
| 1158 | */ |
| 1159 | #if MBEDCRYPTO_ECP_WINDOW_SIZE < 2 || MBEDCRYPTO_ECP_WINDOW_SIZE > 7 |
| 1160 | #error "MBEDCRYPTO_ECP_WINDOW_SIZE out of bounds" |
| 1161 | #endif |
| 1162 | |
| 1163 | /* d = ceil( n / w ) */ |
| 1164 | #define COMB_MAX_D ( MBEDCRYPTO_ECP_MAX_BITS + 1 ) / 2 |
| 1165 | |
| 1166 | /* number of precomputed points */ |
| 1167 | #define COMB_MAX_PRE ( 1 << ( MBEDCRYPTO_ECP_WINDOW_SIZE - 1 ) ) |
| 1168 | |
| 1169 | /* |
| 1170 | * Compute the representation of m that will be used with our comb method. |
| 1171 | * |
| 1172 | * The basic comb method is described in GECC 3.44 for example. We use a |
| 1173 | * modified version that provides resistance to SPA by avoiding zero |
| 1174 | * digits in the representation as in [3]. We modify the method further by |
| 1175 | * requiring that all K_i be odd, which has the small cost that our |
| 1176 | * representation uses one more K_i, due to carries. |
| 1177 | * |
| 1178 | * Also, for the sake of compactness, only the seven low-order bits of x[i] |
| 1179 | * are used to represent K_i, and the msb of x[i] encodes the the sign (s_i in |
| 1180 | * the paper): it is set if and only if if s_i == -1; |
| 1181 | * |
| 1182 | * Calling conventions: |
| 1183 | * - x is an array of size d + 1 |
| 1184 | * - w is the size, ie number of teeth, of the comb, and must be between |
| 1185 | * 2 and 7 (in practice, between 2 and MBEDCRYPTO_ECP_WINDOW_SIZE) |
| 1186 | * - m is the MPI, expected to be odd and such that bitlength(m) <= w * d |
| 1187 | * (the result will be incorrect if these assumptions are not satisfied) |
| 1188 | */ |
| 1189 | static void ecp_comb_fixed( unsigned char x[], size_t d, |
| 1190 | unsigned char w, const mbedcrypto_mpi *m ) |
| 1191 | { |
| 1192 | size_t i, j; |
| 1193 | unsigned char c, cc, adjust; |
| 1194 | |
| 1195 | memset( x, 0, d+1 ); |
| 1196 | |
| 1197 | /* First get the classical comb values (except for x_d = 0) */ |
| 1198 | for( i = 0; i < d; i++ ) |
| 1199 | for( j = 0; j < w; j++ ) |
| 1200 | x[i] |= mbedcrypto_mpi_get_bit( m, i + d * j ) << j; |
| 1201 | |
| 1202 | /* Now make sure x_1 .. x_d are odd */ |
| 1203 | c = 0; |
| 1204 | for( i = 1; i <= d; i++ ) |
| 1205 | { |
| 1206 | /* Add carry and update it */ |
| 1207 | cc = x[i] & c; |
| 1208 | x[i] = x[i] ^ c; |
| 1209 | c = cc; |
| 1210 | |
| 1211 | /* Adjust if needed, avoiding branches */ |
| 1212 | adjust = 1 - ( x[i] & 0x01 ); |
| 1213 | c |= x[i] & ( x[i-1] * adjust ); |
| 1214 | x[i] = x[i] ^ ( x[i-1] * adjust ); |
| 1215 | x[i-1] |= adjust << 7; |
| 1216 | } |
| 1217 | } |
| 1218 | |
| 1219 | /* |
| 1220 | * Precompute points for the comb method |
| 1221 | * |
| 1222 | * If i = i_{w-1} ... i_1 is the binary representation of i, then |
| 1223 | * T[i] = i_{w-1} 2^{(w-1)d} P + ... + i_1 2^d P + P |
| 1224 | * |
| 1225 | * T must be able to hold 2^{w - 1} elements |
| 1226 | * |
| 1227 | * Cost: d(w-1) D + (2^{w-1} - 1) A + 1 N(w-1) + 1 N(2^{w-1} - 1) |
| 1228 | */ |
| 1229 | static int ecp_precompute_comb( const mbedcrypto_ecp_group *grp, |
| 1230 | mbedcrypto_ecp_point T[], const mbedcrypto_ecp_point *P, |
| 1231 | unsigned char w, size_t d ) |
| 1232 | { |
| 1233 | int ret; |
| 1234 | unsigned char i, k; |
| 1235 | size_t j; |
| 1236 | mbedcrypto_ecp_point *cur, *TT[COMB_MAX_PRE - 1]; |
| 1237 | |
| 1238 | /* |
| 1239 | * Set T[0] = P and |
| 1240 | * T[2^{l-1}] = 2^{dl} P for l = 1 .. w-1 (this is not the final value) |
| 1241 | */ |
| 1242 | MBEDCRYPTO_MPI_CHK( mbedcrypto_ecp_copy( &T[0], P ) ); |
| 1243 | |
| 1244 | k = 0; |
| 1245 | for( i = 1; i < ( 1U << ( w - 1 ) ); i <<= 1 ) |
| 1246 | { |
| 1247 | cur = T + i; |
| 1248 | MBEDCRYPTO_MPI_CHK( mbedcrypto_ecp_copy( cur, T + ( i >> 1 ) ) ); |
| 1249 | for( j = 0; j < d; j++ ) |
| 1250 | MBEDCRYPTO_MPI_CHK( ecp_double_jac( grp, cur, cur ) ); |
| 1251 | |
| 1252 | TT[k++] = cur; |
| 1253 | } |
| 1254 | |
| 1255 | MBEDCRYPTO_MPI_CHK( ecp_normalize_jac_many( grp, TT, k ) ); |
| 1256 | |
| 1257 | /* |
| 1258 | * Compute the remaining ones using the minimal number of additions |
| 1259 | * Be careful to update T[2^l] only after using it! |
| 1260 | */ |
| 1261 | k = 0; |
| 1262 | for( i = 1; i < ( 1U << ( w - 1 ) ); i <<= 1 ) |
| 1263 | { |
| 1264 | j = i; |
| 1265 | while( j-- ) |
| 1266 | { |
| 1267 | MBEDCRYPTO_MPI_CHK( ecp_add_mixed( grp, &T[i + j], &T[j], &T[i] ) ); |
| 1268 | TT[k++] = &T[i + j]; |
| 1269 | } |
| 1270 | } |
| 1271 | |
| 1272 | MBEDCRYPTO_MPI_CHK( ecp_normalize_jac_many( grp, TT, k ) ); |
| 1273 | |
| 1274 | cleanup: |
| 1275 | |
| 1276 | return( ret ); |
| 1277 | } |
| 1278 | |
| 1279 | /* |
| 1280 | * Select precomputed point: R = sign(i) * T[ abs(i) / 2 ] |
| 1281 | */ |
| 1282 | static int ecp_select_comb( const mbedcrypto_ecp_group *grp, mbedcrypto_ecp_point *R, |
| 1283 | const mbedcrypto_ecp_point T[], unsigned char t_len, |
| 1284 | unsigned char i ) |
| 1285 | { |
| 1286 | int ret; |
| 1287 | unsigned char ii, j; |
| 1288 | |
| 1289 | /* Ignore the "sign" bit and scale down */ |
| 1290 | ii = ( i & 0x7Fu ) >> 1; |
| 1291 | |
| 1292 | /* Read the whole table to thwart cache-based timing attacks */ |
| 1293 | for( j = 0; j < t_len; j++ ) |
| 1294 | { |
| 1295 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_safe_cond_assign( &R->X, &T[j].X, j == ii ) ); |
| 1296 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_safe_cond_assign( &R->Y, &T[j].Y, j == ii ) ); |
| 1297 | } |
| 1298 | |
| 1299 | /* Safely invert result if i is "negative" */ |
| 1300 | MBEDCRYPTO_MPI_CHK( ecp_safe_invert_jac( grp, R, i >> 7 ) ); |
| 1301 | |
| 1302 | cleanup: |
| 1303 | return( ret ); |
| 1304 | } |
| 1305 | |
| 1306 | /* |
| 1307 | * Core multiplication algorithm for the (modified) comb method. |
| 1308 | * This part is actually common with the basic comb method (GECC 3.44) |
| 1309 | * |
| 1310 | * Cost: d A + d D + 1 R |
| 1311 | */ |
| 1312 | static int ecp_mul_comb_core( const mbedcrypto_ecp_group *grp, mbedcrypto_ecp_point *R, |
| 1313 | const mbedcrypto_ecp_point T[], unsigned char t_len, |
| 1314 | const unsigned char x[], size_t d, |
| 1315 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1316 | void *p_rng ) |
| 1317 | { |
| 1318 | int ret; |
| 1319 | mbedcrypto_ecp_point Txi; |
| 1320 | size_t i; |
| 1321 | |
| 1322 | mbedcrypto_ecp_point_init( &Txi ); |
| 1323 | |
| 1324 | /* Start with a non-zero point and randomize its coordinates */ |
| 1325 | i = d; |
| 1326 | MBEDCRYPTO_MPI_CHK( ecp_select_comb( grp, R, T, t_len, x[i] ) ); |
| 1327 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_lset( &R->Z, 1 ) ); |
| 1328 | if( f_rng != 0 ) |
| 1329 | MBEDCRYPTO_MPI_CHK( ecp_randomize_jac( grp, R, f_rng, p_rng ) ); |
| 1330 | |
| 1331 | while( i-- != 0 ) |
| 1332 | { |
| 1333 | MBEDCRYPTO_MPI_CHK( ecp_double_jac( grp, R, R ) ); |
| 1334 | MBEDCRYPTO_MPI_CHK( ecp_select_comb( grp, &Txi, T, t_len, x[i] ) ); |
| 1335 | MBEDCRYPTO_MPI_CHK( ecp_add_mixed( grp, R, R, &Txi ) ); |
| 1336 | } |
| 1337 | |
| 1338 | cleanup: |
| 1339 | |
| 1340 | mbedcrypto_ecp_point_free( &Txi ); |
| 1341 | |
| 1342 | return( ret ); |
| 1343 | } |
| 1344 | |
| 1345 | /* |
| 1346 | * Multiplication using the comb method, |
| 1347 | * for curves in short Weierstrass form |
| 1348 | */ |
| 1349 | static int ecp_mul_comb( mbedcrypto_ecp_group *grp, mbedcrypto_ecp_point *R, |
| 1350 | const mbedcrypto_mpi *m, const mbedcrypto_ecp_point *P, |
| 1351 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1352 | void *p_rng ) |
| 1353 | { |
| 1354 | int ret; |
| 1355 | unsigned char w, m_is_odd, p_eq_g, pre_len, i; |
| 1356 | size_t d; |
| 1357 | unsigned char k[COMB_MAX_D + 1]; |
| 1358 | mbedcrypto_ecp_point *T; |
| 1359 | mbedcrypto_mpi M, mm; |
| 1360 | |
| 1361 | mbedcrypto_mpi_init( &M ); |
| 1362 | mbedcrypto_mpi_init( &mm ); |
| 1363 | |
| 1364 | /* we need N to be odd to trnaform m in an odd number, check now */ |
| 1365 | if( mbedcrypto_mpi_get_bit( &grp->N, 0 ) != 1 ) |
| 1366 | return( MBEDCRYPTO_ERR_ECP_BAD_INPUT_DATA ); |
| 1367 | |
| 1368 | /* |
| 1369 | * Minimize the number of multiplications, that is minimize |
| 1370 | * 10 * d * w + 18 * 2^(w-1) + 11 * d + 7 * w, with d = ceil( nbits / w ) |
| 1371 | * (see costs of the various parts, with 1S = 1M) |
| 1372 | */ |
| 1373 | w = grp->nbits >= 384 ? 5 : 4; |
| 1374 | |
| 1375 | /* |
| 1376 | * If P == G, pre-compute a bit more, since this may be re-used later. |
| 1377 | * Just adding one avoids upping the cost of the first mul too much, |
| 1378 | * and the memory cost too. |
| 1379 | */ |
| 1380 | #if MBEDCRYPTO_ECP_FIXED_POINT_OPTIM == 1 |
| 1381 | p_eq_g = ( mbedcrypto_mpi_cmp_mpi( &P->Y, &grp->G.Y ) == 0 && |
| 1382 | mbedcrypto_mpi_cmp_mpi( &P->X, &grp->G.X ) == 0 ); |
| 1383 | if( p_eq_g ) |
| 1384 | w++; |
| 1385 | #else |
| 1386 | p_eq_g = 0; |
| 1387 | #endif |
| 1388 | |
| 1389 | /* |
| 1390 | * Make sure w is within bounds. |
| 1391 | * (The last test is useful only for very small curves in the test suite.) |
| 1392 | */ |
| 1393 | if( w > MBEDCRYPTO_ECP_WINDOW_SIZE ) |
| 1394 | w = MBEDCRYPTO_ECP_WINDOW_SIZE; |
| 1395 | if( w >= grp->nbits ) |
| 1396 | w = 2; |
| 1397 | |
| 1398 | /* Other sizes that depend on w */ |
| 1399 | pre_len = 1U << ( w - 1 ); |
| 1400 | d = ( grp->nbits + w - 1 ) / w; |
| 1401 | |
| 1402 | /* |
| 1403 | * Prepare precomputed points: if P == G we want to |
| 1404 | * use grp->T if already initialized, or initialize it. |
| 1405 | */ |
| 1406 | T = p_eq_g ? grp->T : NULL; |
| 1407 | |
| 1408 | if( T == NULL ) |
| 1409 | { |
| 1410 | T = mbedcrypto_calloc( pre_len, sizeof( mbedcrypto_ecp_point ) ); |
| 1411 | if( T == NULL ) |
| 1412 | { |
| 1413 | ret = MBEDCRYPTO_ERR_ECP_ALLOC_FAILED; |
| 1414 | goto cleanup; |
| 1415 | } |
| 1416 | |
| 1417 | MBEDCRYPTO_MPI_CHK( ecp_precompute_comb( grp, T, P, w, d ) ); |
| 1418 | |
| 1419 | if( p_eq_g ) |
| 1420 | { |
| 1421 | grp->T = T; |
| 1422 | grp->T_size = pre_len; |
| 1423 | } |
| 1424 | } |
| 1425 | |
| 1426 | /* |
| 1427 | * Make sure M is odd (M = m or M = N - m, since N is odd) |
| 1428 | * using the fact that m * P = - (N - m) * P |
| 1429 | */ |
| 1430 | m_is_odd = ( mbedcrypto_mpi_get_bit( m, 0 ) == 1 ); |
| 1431 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_copy( &M, m ) ); |
| 1432 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_sub_mpi( &mm, &grp->N, m ) ); |
| 1433 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_safe_cond_assign( &M, &mm, ! m_is_odd ) ); |
| 1434 | |
| 1435 | /* |
| 1436 | * Go for comb multiplication, R = M * P |
| 1437 | */ |
| 1438 | ecp_comb_fixed( k, d, w, &M ); |
| 1439 | MBEDCRYPTO_MPI_CHK( ecp_mul_comb_core( grp, R, T, pre_len, k, d, f_rng, p_rng ) ); |
| 1440 | |
| 1441 | /* |
| 1442 | * Now get m * P from M * P and normalize it |
| 1443 | */ |
| 1444 | MBEDCRYPTO_MPI_CHK( ecp_safe_invert_jac( grp, R, ! m_is_odd ) ); |
| 1445 | MBEDCRYPTO_MPI_CHK( ecp_normalize_jac( grp, R ) ); |
| 1446 | |
| 1447 | cleanup: |
| 1448 | |
| 1449 | if( T != NULL && ! p_eq_g ) |
| 1450 | { |
| 1451 | for( i = 0; i < pre_len; i++ ) |
| 1452 | mbedcrypto_ecp_point_free( &T[i] ); |
| 1453 | mbedcrypto_free( T ); |
| 1454 | } |
| 1455 | |
| 1456 | mbedcrypto_mpi_free( &M ); |
| 1457 | mbedcrypto_mpi_free( &mm ); |
| 1458 | |
| 1459 | if( ret != 0 ) |
| 1460 | mbedcrypto_ecp_point_free( R ); |
| 1461 | |
| 1462 | return( ret ); |
| 1463 | } |
| 1464 | |
| 1465 | #endif /* ECP_SHORTWEIERSTRASS */ |
| 1466 | |
| 1467 | #if defined(ECP_MONTGOMERY) |
| 1468 | /* |
| 1469 | * For Montgomery curves, we do all the internal arithmetic in projective |
| 1470 | * coordinates. Import/export of points uses only the x coordinates, which is |
| 1471 | * internaly represented as X / Z. |
| 1472 | * |
| 1473 | * For scalar multiplication, we'll use a Montgomery ladder. |
| 1474 | */ |
| 1475 | |
| 1476 | /* |
| 1477 | * Normalize Montgomery x/z coordinates: X = X/Z, Z = 1 |
| 1478 | * Cost: 1M + 1I |
| 1479 | */ |
| 1480 | static int ecp_normalize_mxz( const mbedcrypto_ecp_group *grp, mbedcrypto_ecp_point *P ) |
| 1481 | { |
| 1482 | int ret; |
| 1483 | |
| 1484 | #if defined(MBEDCRYPTO_ECP_NORMALIZE_MXZ_ALT) |
| 1485 | if ( mbedcrypto_internal_ecp_grp_capable( grp ) ) |
| 1486 | { |
| 1487 | return mbedcrypto_internal_ecp_normalize_mxz( grp, P ); |
| 1488 | } |
| 1489 | #endif /* MBEDCRYPTO_ECP_NORMALIZE_MXZ_ALT */ |
| 1490 | |
| 1491 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_inv_mod( &P->Z, &P->Z, &grp->P ) ); |
| 1492 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_mpi( &P->X, &P->X, &P->Z ) ); MOD_MUL( P->X ); |
| 1493 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_lset( &P->Z, 1 ) ); |
| 1494 | |
| 1495 | cleanup: |
| 1496 | return( ret ); |
| 1497 | } |
| 1498 | |
| 1499 | /* |
| 1500 | * Randomize projective x/z coordinates: |
| 1501 | * (X, Z) -> (l X, l Z) for random l |
| 1502 | * This is sort of the reverse operation of ecp_normalize_mxz(). |
| 1503 | * |
| 1504 | * This countermeasure was first suggested in [2]. |
| 1505 | * Cost: 2M |
| 1506 | */ |
| 1507 | static int ecp_randomize_mxz( const mbedcrypto_ecp_group *grp, mbedcrypto_ecp_point *P, |
| 1508 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 1509 | { |
| 1510 | int ret; |
| 1511 | mbedcrypto_mpi l; |
| 1512 | size_t p_size; |
| 1513 | int count = 0; |
| 1514 | |
| 1515 | #if defined(MBEDCRYPTO_ECP_RANDOMIZE_MXZ_ALT) |
| 1516 | if ( mbedcrypto_internal_ecp_grp_capable( grp ) ) |
| 1517 | { |
| 1518 | return mbedcrypto_internal_ecp_randomize_mxz( grp, P, f_rng, p_rng ); |
| 1519 | } |
| 1520 | #endif /* MBEDCRYPTO_ECP_RANDOMIZE_MXZ_ALT */ |
| 1521 | |
| 1522 | p_size = ( grp->pbits + 7 ) / 8; |
| 1523 | mbedcrypto_mpi_init( &l ); |
| 1524 | |
| 1525 | /* Generate l such that 1 < l < p */ |
| 1526 | do |
| 1527 | { |
| 1528 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_fill_random( &l, p_size, f_rng, p_rng ) ); |
| 1529 | |
| 1530 | while( mbedcrypto_mpi_cmp_mpi( &l, &grp->P ) >= 0 ) |
| 1531 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_shift_r( &l, 1 ) ); |
| 1532 | |
| 1533 | if( count++ > 10 ) |
| 1534 | return( MBEDCRYPTO_ERR_ECP_RANDOM_FAILED ); |
| 1535 | } |
| 1536 | while( mbedcrypto_mpi_cmp_int( &l, 1 ) <= 0 ); |
| 1537 | |
| 1538 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_mpi( &P->X, &P->X, &l ) ); MOD_MUL( P->X ); |
| 1539 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_mpi( &P->Z, &P->Z, &l ) ); MOD_MUL( P->Z ); |
| 1540 | |
| 1541 | cleanup: |
| 1542 | mbedcrypto_mpi_free( &l ); |
| 1543 | |
| 1544 | return( ret ); |
| 1545 | } |
| 1546 | |
| 1547 | /* |
| 1548 | * Double-and-add: R = 2P, S = P + Q, with d = X(P - Q), |
| 1549 | * for Montgomery curves in x/z coordinates. |
| 1550 | * |
| 1551 | * http://www.hyperelliptic.org/EFD/g1p/auto-code/montgom/xz/ladder/mladd-1987-m.op3 |
| 1552 | * with |
| 1553 | * d = X1 |
| 1554 | * P = (X2, Z2) |
| 1555 | * Q = (X3, Z3) |
| 1556 | * R = (X4, Z4) |
| 1557 | * S = (X5, Z5) |
| 1558 | * and eliminating temporary variables tO, ..., t4. |
| 1559 | * |
| 1560 | * Cost: 5M + 4S |
| 1561 | */ |
| 1562 | static int ecp_double_add_mxz( const mbedcrypto_ecp_group *grp, |
| 1563 | mbedcrypto_ecp_point *R, mbedcrypto_ecp_point *S, |
| 1564 | const mbedcrypto_ecp_point *P, const mbedcrypto_ecp_point *Q, |
| 1565 | const mbedcrypto_mpi *d ) |
| 1566 | { |
| 1567 | int ret; |
| 1568 | mbedcrypto_mpi A, AA, B, BB, E, C, D, DA, CB; |
| 1569 | |
| 1570 | #if defined(MBEDCRYPTO_ECP_DOUBLE_ADD_MXZ_ALT) |
| 1571 | if ( mbedcrypto_internal_ecp_grp_capable( grp ) ) |
| 1572 | { |
| 1573 | return mbedcrypto_internal_ecp_double_add_mxz( grp, R, S, P, Q, d ); |
| 1574 | } |
| 1575 | #endif /* MBEDCRYPTO_ECP_DOUBLE_ADD_MXZ_ALT */ |
| 1576 | |
| 1577 | mbedcrypto_mpi_init( &A ); mbedcrypto_mpi_init( &AA ); mbedcrypto_mpi_init( &B ); |
| 1578 | mbedcrypto_mpi_init( &BB ); mbedcrypto_mpi_init( &E ); mbedcrypto_mpi_init( &C ); |
| 1579 | mbedcrypto_mpi_init( &D ); mbedcrypto_mpi_init( &DA ); mbedcrypto_mpi_init( &CB ); |
| 1580 | |
| 1581 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_add_mpi( &A, &P->X, &P->Z ) ); MOD_ADD( A ); |
| 1582 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_mpi( &AA, &A, &A ) ); MOD_MUL( AA ); |
| 1583 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_sub_mpi( &B, &P->X, &P->Z ) ); MOD_SUB( B ); |
| 1584 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_mpi( &BB, &B, &B ) ); MOD_MUL( BB ); |
| 1585 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_sub_mpi( &E, &AA, &BB ) ); MOD_SUB( E ); |
| 1586 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_add_mpi( &C, &Q->X, &Q->Z ) ); MOD_ADD( C ); |
| 1587 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_sub_mpi( &D, &Q->X, &Q->Z ) ); MOD_SUB( D ); |
| 1588 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_mpi( &DA, &D, &A ) ); MOD_MUL( DA ); |
| 1589 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_mpi( &CB, &C, &B ) ); MOD_MUL( CB ); |
| 1590 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_add_mpi( &S->X, &DA, &CB ) ); MOD_MUL( S->X ); |
| 1591 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_mpi( &S->X, &S->X, &S->X ) ); MOD_MUL( S->X ); |
| 1592 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_sub_mpi( &S->Z, &DA, &CB ) ); MOD_SUB( S->Z ); |
| 1593 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_mpi( &S->Z, &S->Z, &S->Z ) ); MOD_MUL( S->Z ); |
| 1594 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_mpi( &S->Z, d, &S->Z ) ); MOD_MUL( S->Z ); |
| 1595 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_mpi( &R->X, &AA, &BB ) ); MOD_MUL( R->X ); |
| 1596 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_mpi( &R->Z, &grp->A, &E ) ); MOD_MUL( R->Z ); |
| 1597 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_add_mpi( &R->Z, &BB, &R->Z ) ); MOD_ADD( R->Z ); |
| 1598 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_mpi( &R->Z, &E, &R->Z ) ); MOD_MUL( R->Z ); |
| 1599 | |
| 1600 | cleanup: |
| 1601 | mbedcrypto_mpi_free( &A ); mbedcrypto_mpi_free( &AA ); mbedcrypto_mpi_free( &B ); |
| 1602 | mbedcrypto_mpi_free( &BB ); mbedcrypto_mpi_free( &E ); mbedcrypto_mpi_free( &C ); |
| 1603 | mbedcrypto_mpi_free( &D ); mbedcrypto_mpi_free( &DA ); mbedcrypto_mpi_free( &CB ); |
| 1604 | |
| 1605 | return( ret ); |
| 1606 | } |
| 1607 | |
| 1608 | /* |
| 1609 | * Multiplication with Montgomery ladder in x/z coordinates, |
| 1610 | * for curves in Montgomery form |
| 1611 | */ |
| 1612 | static int ecp_mul_mxz( mbedcrypto_ecp_group *grp, mbedcrypto_ecp_point *R, |
| 1613 | const mbedcrypto_mpi *m, const mbedcrypto_ecp_point *P, |
| 1614 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1615 | void *p_rng ) |
| 1616 | { |
| 1617 | int ret; |
| 1618 | size_t i; |
| 1619 | unsigned char b; |
| 1620 | mbedcrypto_ecp_point RP; |
| 1621 | mbedcrypto_mpi PX; |
| 1622 | |
| 1623 | mbedcrypto_ecp_point_init( &RP ); mbedcrypto_mpi_init( &PX ); |
| 1624 | |
| 1625 | /* Save PX and read from P before writing to R, in case P == R */ |
| 1626 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_copy( &PX, &P->X ) ); |
| 1627 | MBEDCRYPTO_MPI_CHK( mbedcrypto_ecp_copy( &RP, P ) ); |
| 1628 | |
| 1629 | /* Set R to zero in modified x/z coordinates */ |
| 1630 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_lset( &R->X, 1 ) ); |
| 1631 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_lset( &R->Z, 0 ) ); |
| 1632 | mbedcrypto_mpi_free( &R->Y ); |
| 1633 | |
| 1634 | /* RP.X might be sligtly larger than P, so reduce it */ |
| 1635 | MOD_ADD( RP.X ); |
| 1636 | |
| 1637 | /* Randomize coordinates of the starting point */ |
| 1638 | if( f_rng != NULL ) |
| 1639 | MBEDCRYPTO_MPI_CHK( ecp_randomize_mxz( grp, &RP, f_rng, p_rng ) ); |
| 1640 | |
| 1641 | /* Loop invariant: R = result so far, RP = R + P */ |
| 1642 | i = mbedcrypto_mpi_bitlen( m ); /* one past the (zero-based) most significant bit */ |
| 1643 | while( i-- > 0 ) |
| 1644 | { |
| 1645 | b = mbedcrypto_mpi_get_bit( m, i ); |
| 1646 | /* |
| 1647 | * if (b) R = 2R + P else R = 2R, |
| 1648 | * which is: |
| 1649 | * if (b) double_add( RP, R, RP, R ) |
| 1650 | * else double_add( R, RP, R, RP ) |
| 1651 | * but using safe conditional swaps to avoid leaks |
| 1652 | */ |
| 1653 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_safe_cond_swap( &R->X, &RP.X, b ) ); |
| 1654 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_safe_cond_swap( &R->Z, &RP.Z, b ) ); |
| 1655 | MBEDCRYPTO_MPI_CHK( ecp_double_add_mxz( grp, R, &RP, R, &RP, &PX ) ); |
| 1656 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_safe_cond_swap( &R->X, &RP.X, b ) ); |
| 1657 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_safe_cond_swap( &R->Z, &RP.Z, b ) ); |
| 1658 | } |
| 1659 | |
| 1660 | MBEDCRYPTO_MPI_CHK( ecp_normalize_mxz( grp, R ) ); |
| 1661 | |
| 1662 | cleanup: |
| 1663 | mbedcrypto_ecp_point_free( &RP ); mbedcrypto_mpi_free( &PX ); |
| 1664 | |
| 1665 | return( ret ); |
| 1666 | } |
| 1667 | |
| 1668 | #endif /* ECP_MONTGOMERY */ |
| 1669 | |
| 1670 | /* |
| 1671 | * Multiplication R = m * P |
| 1672 | */ |
| 1673 | int mbedcrypto_ecp_mul( mbedcrypto_ecp_group *grp, mbedcrypto_ecp_point *R, |
| 1674 | const mbedcrypto_mpi *m, const mbedcrypto_ecp_point *P, |
| 1675 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 1676 | { |
| 1677 | int ret = MBEDCRYPTO_ERR_ECP_BAD_INPUT_DATA; |
| 1678 | #if defined(MBEDCRYPTO_ECP_INTERNAL_ALT) |
| 1679 | char is_grp_capable = 0; |
| 1680 | #endif |
| 1681 | |
| 1682 | /* Common sanity checks */ |
| 1683 | if( mbedcrypto_mpi_cmp_int( &P->Z, 1 ) != 0 ) |
| 1684 | return( MBEDCRYPTO_ERR_ECP_BAD_INPUT_DATA ); |
| 1685 | |
| 1686 | if( ( ret = mbedcrypto_ecp_check_privkey( grp, m ) ) != 0 || |
| 1687 | ( ret = mbedcrypto_ecp_check_pubkey( grp, P ) ) != 0 ) |
| 1688 | return( ret ); |
| 1689 | |
| 1690 | #if defined(MBEDCRYPTO_ECP_INTERNAL_ALT) |
| 1691 | if ( is_grp_capable = mbedcrypto_internal_ecp_grp_capable( grp ) ) |
| 1692 | { |
| 1693 | MBEDCRYPTO_MPI_CHK( mbedcrypto_internal_ecp_init( grp ) ); |
| 1694 | } |
| 1695 | |
| 1696 | #endif /* MBEDCRYPTO_ECP_INTERNAL_ALT */ |
| 1697 | #if defined(ECP_MONTGOMERY) |
| 1698 | if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY ) |
| 1699 | ret = ecp_mul_mxz( grp, R, m, P, f_rng, p_rng ); |
| 1700 | |
| 1701 | #endif |
| 1702 | #if defined(ECP_SHORTWEIERSTRASS) |
| 1703 | if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS ) |
| 1704 | ret = ecp_mul_comb( grp, R, m, P, f_rng, p_rng ); |
| 1705 | |
| 1706 | #endif |
| 1707 | #if defined(MBEDCRYPTO_ECP_INTERNAL_ALT) |
| 1708 | cleanup: |
| 1709 | |
| 1710 | if ( is_grp_capable ) |
| 1711 | { |
| 1712 | mbedcrypto_internal_ecp_free( grp ); |
| 1713 | } |
| 1714 | |
| 1715 | #endif /* MBEDCRYPTO_ECP_INTERNAL_ALT */ |
| 1716 | return( ret ); |
| 1717 | } |
| 1718 | |
| 1719 | #if defined(ECP_SHORTWEIERSTRASS) |
| 1720 | /* |
| 1721 | * Check that an affine point is valid as a public key, |
| 1722 | * short weierstrass curves (SEC1 3.2.3.1) |
| 1723 | */ |
| 1724 | static int ecp_check_pubkey_sw( const mbedcrypto_ecp_group *grp, const mbedcrypto_ecp_point *pt ) |
| 1725 | { |
| 1726 | int ret; |
| 1727 | mbedcrypto_mpi YY, RHS; |
| 1728 | |
| 1729 | /* pt coordinates must be normalized for our checks */ |
| 1730 | if( mbedcrypto_mpi_cmp_int( &pt->X, 0 ) < 0 || |
| 1731 | mbedcrypto_mpi_cmp_int( &pt->Y, 0 ) < 0 || |
| 1732 | mbedcrypto_mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 || |
| 1733 | mbedcrypto_mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 ) |
| 1734 | return( MBEDCRYPTO_ERR_ECP_INVALID_KEY ); |
| 1735 | |
| 1736 | mbedcrypto_mpi_init( &YY ); mbedcrypto_mpi_init( &RHS ); |
| 1737 | |
| 1738 | /* |
| 1739 | * YY = Y^2 |
| 1740 | * RHS = X (X^2 + A) + B = X^3 + A X + B |
| 1741 | */ |
| 1742 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY ); |
| 1743 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS ); |
| 1744 | |
| 1745 | /* Special case for A = -3 */ |
| 1746 | if( grp->A.p == NULL ) |
| 1747 | { |
| 1748 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_sub_int( &RHS, &RHS, 3 ) ); MOD_SUB( RHS ); |
| 1749 | } |
| 1750 | else |
| 1751 | { |
| 1752 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_add_mpi( &RHS, &RHS, &grp->A ) ); MOD_ADD( RHS ); |
| 1753 | } |
| 1754 | |
| 1755 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS ); |
| 1756 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS ); |
| 1757 | |
| 1758 | if( mbedcrypto_mpi_cmp_mpi( &YY, &RHS ) != 0 ) |
| 1759 | ret = MBEDCRYPTO_ERR_ECP_INVALID_KEY; |
| 1760 | |
| 1761 | cleanup: |
| 1762 | |
| 1763 | mbedcrypto_mpi_free( &YY ); mbedcrypto_mpi_free( &RHS ); |
| 1764 | |
| 1765 | return( ret ); |
| 1766 | } |
| 1767 | #endif /* ECP_SHORTWEIERSTRASS */ |
| 1768 | |
| 1769 | /* |
| 1770 | * R = m * P with shortcuts for m == 1 and m == -1 |
| 1771 | * NOT constant-time - ONLY for short Weierstrass! |
| 1772 | */ |
| 1773 | static int mbedcrypto_ecp_mul_shortcuts( mbedcrypto_ecp_group *grp, |
| 1774 | mbedcrypto_ecp_point *R, |
| 1775 | const mbedcrypto_mpi *m, |
| 1776 | const mbedcrypto_ecp_point *P ) |
| 1777 | { |
| 1778 | int ret; |
| 1779 | |
| 1780 | if( mbedcrypto_mpi_cmp_int( m, 1 ) == 0 ) |
| 1781 | { |
| 1782 | MBEDCRYPTO_MPI_CHK( mbedcrypto_ecp_copy( R, P ) ); |
| 1783 | } |
| 1784 | else if( mbedcrypto_mpi_cmp_int( m, -1 ) == 0 ) |
| 1785 | { |
| 1786 | MBEDCRYPTO_MPI_CHK( mbedcrypto_ecp_copy( R, P ) ); |
| 1787 | if( mbedcrypto_mpi_cmp_int( &R->Y, 0 ) != 0 ) |
| 1788 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_sub_mpi( &R->Y, &grp->P, &R->Y ) ); |
| 1789 | } |
| 1790 | else |
| 1791 | { |
| 1792 | MBEDCRYPTO_MPI_CHK( mbedcrypto_ecp_mul( grp, R, m, P, NULL, NULL ) ); |
| 1793 | } |
| 1794 | |
| 1795 | cleanup: |
| 1796 | return( ret ); |
| 1797 | } |
| 1798 | |
| 1799 | /* |
| 1800 | * Linear combination |
| 1801 | * NOT constant-time |
| 1802 | */ |
| 1803 | int mbedcrypto_ecp_muladd( mbedcrypto_ecp_group *grp, mbedcrypto_ecp_point *R, |
| 1804 | const mbedcrypto_mpi *m, const mbedcrypto_ecp_point *P, |
| 1805 | const mbedcrypto_mpi *n, const mbedcrypto_ecp_point *Q ) |
| 1806 | { |
| 1807 | int ret; |
| 1808 | mbedcrypto_ecp_point mP; |
| 1809 | #if defined(MBEDCRYPTO_ECP_INTERNAL_ALT) |
| 1810 | char is_grp_capable = 0; |
| 1811 | #endif |
| 1812 | |
| 1813 | if( ecp_get_type( grp ) != ECP_TYPE_SHORT_WEIERSTRASS ) |
| 1814 | return( MBEDCRYPTO_ERR_ECP_FEATURE_UNAVAILABLE ); |
| 1815 | |
| 1816 | mbedcrypto_ecp_point_init( &mP ); |
| 1817 | |
| 1818 | MBEDCRYPTO_MPI_CHK( mbedcrypto_ecp_mul_shortcuts( grp, &mP, m, P ) ); |
| 1819 | MBEDCRYPTO_MPI_CHK( mbedcrypto_ecp_mul_shortcuts( grp, R, n, Q ) ); |
| 1820 | |
| 1821 | #if defined(MBEDCRYPTO_ECP_INTERNAL_ALT) |
| 1822 | if ( is_grp_capable = mbedcrypto_internal_ecp_grp_capable( grp ) ) |
| 1823 | { |
| 1824 | MBEDCRYPTO_MPI_CHK( mbedcrypto_internal_ecp_init( grp ) ); |
| 1825 | } |
| 1826 | |
| 1827 | #endif /* MBEDCRYPTO_ECP_INTERNAL_ALT */ |
| 1828 | MBEDCRYPTO_MPI_CHK( ecp_add_mixed( grp, R, &mP, R ) ); |
| 1829 | MBEDCRYPTO_MPI_CHK( ecp_normalize_jac( grp, R ) ); |
| 1830 | |
| 1831 | cleanup: |
| 1832 | |
| 1833 | #if defined(MBEDCRYPTO_ECP_INTERNAL_ALT) |
| 1834 | if ( is_grp_capable ) |
| 1835 | { |
| 1836 | mbedcrypto_internal_ecp_free( grp ); |
| 1837 | } |
| 1838 | |
| 1839 | #endif /* MBEDCRYPTO_ECP_INTERNAL_ALT */ |
| 1840 | mbedcrypto_ecp_point_free( &mP ); |
| 1841 | |
| 1842 | return( ret ); |
| 1843 | } |
| 1844 | |
| 1845 | |
| 1846 | #if defined(ECP_MONTGOMERY) |
| 1847 | /* |
| 1848 | * Check validity of a public key for Montgomery curves with x-only schemes |
| 1849 | */ |
| 1850 | static int ecp_check_pubkey_mx( const mbedcrypto_ecp_group *grp, const mbedcrypto_ecp_point *pt ) |
| 1851 | { |
| 1852 | /* [Curve25519 p. 5] Just check X is the correct number of bytes */ |
| 1853 | /* Allow any public value, if it's too big then we'll just reduce it mod p |
| 1854 | * (RFC 7748 sec. 5 para. 3). */ |
| 1855 | if( mbedcrypto_mpi_size( &pt->X ) > ( grp->nbits + 7 ) / 8 ) |
| 1856 | return( MBEDCRYPTO_ERR_ECP_INVALID_KEY ); |
| 1857 | |
| 1858 | return( 0 ); |
| 1859 | } |
| 1860 | #endif /* ECP_MONTGOMERY */ |
| 1861 | |
| 1862 | /* |
| 1863 | * Check that a point is valid as a public key |
| 1864 | */ |
| 1865 | int mbedcrypto_ecp_check_pubkey( const mbedcrypto_ecp_group *grp, const mbedcrypto_ecp_point *pt ) |
| 1866 | { |
| 1867 | /* Must use affine coordinates */ |
| 1868 | if( mbedcrypto_mpi_cmp_int( &pt->Z, 1 ) != 0 ) |
| 1869 | return( MBEDCRYPTO_ERR_ECP_INVALID_KEY ); |
| 1870 | |
| 1871 | #if defined(ECP_MONTGOMERY) |
| 1872 | if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY ) |
| 1873 | return( ecp_check_pubkey_mx( grp, pt ) ); |
| 1874 | #endif |
| 1875 | #if defined(ECP_SHORTWEIERSTRASS) |
| 1876 | if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS ) |
| 1877 | return( ecp_check_pubkey_sw( grp, pt ) ); |
| 1878 | #endif |
| 1879 | return( MBEDCRYPTO_ERR_ECP_BAD_INPUT_DATA ); |
| 1880 | } |
| 1881 | |
| 1882 | /* |
| 1883 | * Check that an mbedcrypto_mpi is valid as a private key |
| 1884 | */ |
| 1885 | int mbedcrypto_ecp_check_privkey( const mbedcrypto_ecp_group *grp, const mbedcrypto_mpi *d ) |
| 1886 | { |
| 1887 | #if defined(ECP_MONTGOMERY) |
| 1888 | if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY ) |
| 1889 | { |
| 1890 | /* see RFC 7748 sec. 5 para. 5 */ |
| 1891 | if( mbedcrypto_mpi_get_bit( d, 0 ) != 0 || |
| 1892 | mbedcrypto_mpi_get_bit( d, 1 ) != 0 || |
| 1893 | mbedcrypto_mpi_bitlen( d ) - 1 != grp->nbits ) /* mbedcrypto_mpi_bitlen is one-based! */ |
| 1894 | return( MBEDCRYPTO_ERR_ECP_INVALID_KEY ); |
| 1895 | else |
| 1896 | |
| 1897 | /* see [Curve25519] page 5 */ |
| 1898 | if( grp->nbits == 254 && mbedcrypto_mpi_get_bit( d, 2 ) != 0 ) |
| 1899 | return( MBEDCRYPTO_ERR_ECP_INVALID_KEY ); |
| 1900 | |
| 1901 | return( 0 ); |
| 1902 | } |
| 1903 | #endif /* ECP_MONTGOMERY */ |
| 1904 | #if defined(ECP_SHORTWEIERSTRASS) |
| 1905 | if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS ) |
| 1906 | { |
| 1907 | /* see SEC1 3.2 */ |
| 1908 | if( mbedcrypto_mpi_cmp_int( d, 1 ) < 0 || |
| 1909 | mbedcrypto_mpi_cmp_mpi( d, &grp->N ) >= 0 ) |
| 1910 | return( MBEDCRYPTO_ERR_ECP_INVALID_KEY ); |
| 1911 | else |
| 1912 | return( 0 ); |
| 1913 | } |
| 1914 | #endif /* ECP_SHORTWEIERSTRASS */ |
| 1915 | |
| 1916 | return( MBEDCRYPTO_ERR_ECP_BAD_INPUT_DATA ); |
| 1917 | } |
| 1918 | |
| 1919 | /* |
| 1920 | * Generate a keypair with configurable base point |
| 1921 | */ |
| 1922 | int mbedcrypto_ecp_gen_keypair_base( mbedcrypto_ecp_group *grp, |
| 1923 | const mbedcrypto_ecp_point *G, |
| 1924 | mbedcrypto_mpi *d, mbedcrypto_ecp_point *Q, |
| 1925 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1926 | void *p_rng ) |
| 1927 | { |
| 1928 | int ret; |
| 1929 | size_t n_size = ( grp->nbits + 7 ) / 8; |
| 1930 | |
| 1931 | #if defined(ECP_MONTGOMERY) |
| 1932 | if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY ) |
| 1933 | { |
| 1934 | /* [M225] page 5 */ |
| 1935 | size_t b; |
| 1936 | |
| 1937 | do { |
| 1938 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_fill_random( d, n_size, f_rng, p_rng ) ); |
| 1939 | } while( mbedcrypto_mpi_bitlen( d ) == 0); |
| 1940 | |
| 1941 | /* Make sure the most significant bit is nbits */ |
| 1942 | b = mbedcrypto_mpi_bitlen( d ) - 1; /* mbedcrypto_mpi_bitlen is one-based */ |
| 1943 | if( b > grp->nbits ) |
| 1944 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_shift_r( d, b - grp->nbits ) ); |
| 1945 | else |
| 1946 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_set_bit( d, grp->nbits, 1 ) ); |
| 1947 | |
| 1948 | /* Make sure the last two bits are unset for Curve448, three bits for |
| 1949 | Curve25519 */ |
| 1950 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_set_bit( d, 0, 0 ) ); |
| 1951 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_set_bit( d, 1, 0 ) ); |
| 1952 | if( grp->nbits == 254 ) |
| 1953 | { |
| 1954 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_set_bit( d, 2, 0 ) ); |
| 1955 | } |
| 1956 | } |
| 1957 | else |
| 1958 | #endif /* ECP_MONTGOMERY */ |
| 1959 | #if defined(ECP_SHORTWEIERSTRASS) |
| 1960 | if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS ) |
| 1961 | { |
| 1962 | /* SEC1 3.2.1: Generate d such that 1 <= n < N */ |
| 1963 | int count = 0; |
| 1964 | |
| 1965 | /* |
| 1966 | * Match the procedure given in RFC 6979 (deterministic ECDSA): |
| 1967 | * - use the same byte ordering; |
| 1968 | * - keep the leftmost nbits bits of the generated octet string; |
| 1969 | * - try until result is in the desired range. |
| 1970 | * This also avoids any biais, which is especially important for ECDSA. |
| 1971 | */ |
| 1972 | do |
| 1973 | { |
| 1974 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_fill_random( d, n_size, f_rng, p_rng ) ); |
| 1975 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_shift_r( d, 8 * n_size - grp->nbits ) ); |
| 1976 | |
| 1977 | /* |
| 1978 | * Each try has at worst a probability 1/2 of failing (the msb has |
| 1979 | * a probability 1/2 of being 0, and then the result will be < N), |
| 1980 | * so after 30 tries failure probability is a most 2**(-30). |
| 1981 | * |
| 1982 | * For most curves, 1 try is enough with overwhelming probability, |
| 1983 | * since N starts with a lot of 1s in binary, but some curves |
| 1984 | * such as secp224k1 are actually very close to the worst case. |
| 1985 | */ |
| 1986 | if( ++count > 30 ) |
| 1987 | return( MBEDCRYPTO_ERR_ECP_RANDOM_FAILED ); |
| 1988 | } |
| 1989 | while( mbedcrypto_mpi_cmp_int( d, 1 ) < 0 || |
| 1990 | mbedcrypto_mpi_cmp_mpi( d, &grp->N ) >= 0 ); |
| 1991 | } |
| 1992 | else |
| 1993 | #endif /* ECP_SHORTWEIERSTRASS */ |
| 1994 | return( MBEDCRYPTO_ERR_ECP_BAD_INPUT_DATA ); |
| 1995 | |
| 1996 | cleanup: |
| 1997 | if( ret != 0 ) |
| 1998 | return( ret ); |
| 1999 | |
| 2000 | return( mbedcrypto_ecp_mul( grp, Q, d, G, f_rng, p_rng ) ); |
| 2001 | } |
| 2002 | |
| 2003 | /* |
| 2004 | * Generate key pair, wrapper for conventional base point |
| 2005 | */ |
| 2006 | int mbedcrypto_ecp_gen_keypair( mbedcrypto_ecp_group *grp, |
| 2007 | mbedcrypto_mpi *d, mbedcrypto_ecp_point *Q, |
| 2008 | int (*f_rng)(void *, unsigned char *, size_t), |
| 2009 | void *p_rng ) |
| 2010 | { |
| 2011 | return( mbedcrypto_ecp_gen_keypair_base( grp, &grp->G, d, Q, f_rng, p_rng ) ); |
| 2012 | } |
| 2013 | |
| 2014 | /* |
| 2015 | * Generate a keypair, prettier wrapper |
| 2016 | */ |
| 2017 | int mbedcrypto_ecp_gen_key( mbedcrypto_ecp_group_id grp_id, mbedcrypto_ecp_keypair *key, |
| 2018 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 2019 | { |
| 2020 | int ret; |
| 2021 | |
| 2022 | if( ( ret = mbedcrypto_ecp_group_load( &key->grp, grp_id ) ) != 0 ) |
| 2023 | return( ret ); |
| 2024 | |
| 2025 | return( mbedcrypto_ecp_gen_keypair( &key->grp, &key->d, &key->Q, f_rng, p_rng ) ); |
| 2026 | } |
| 2027 | |
| 2028 | /* |
| 2029 | * Check a public-private key pair |
| 2030 | */ |
| 2031 | int mbedcrypto_ecp_check_pub_priv( const mbedcrypto_ecp_keypair *pub, const mbedcrypto_ecp_keypair *prv ) |
| 2032 | { |
| 2033 | int ret; |
| 2034 | mbedcrypto_ecp_point Q; |
| 2035 | mbedcrypto_ecp_group grp; |
| 2036 | |
| 2037 | if( pub->grp.id == MBEDCRYPTO_ECP_DP_NONE || |
| 2038 | pub->grp.id != prv->grp.id || |
| 2039 | mbedcrypto_mpi_cmp_mpi( &pub->Q.X, &prv->Q.X ) || |
| 2040 | mbedcrypto_mpi_cmp_mpi( &pub->Q.Y, &prv->Q.Y ) || |
| 2041 | mbedcrypto_mpi_cmp_mpi( &pub->Q.Z, &prv->Q.Z ) ) |
| 2042 | { |
| 2043 | return( MBEDCRYPTO_ERR_ECP_BAD_INPUT_DATA ); |
| 2044 | } |
| 2045 | |
| 2046 | mbedcrypto_ecp_point_init( &Q ); |
| 2047 | mbedcrypto_ecp_group_init( &grp ); |
| 2048 | |
| 2049 | /* mbedcrypto_ecp_mul() needs a non-const group... */ |
| 2050 | mbedcrypto_ecp_group_copy( &grp, &prv->grp ); |
| 2051 | |
| 2052 | /* Also checks d is valid */ |
| 2053 | MBEDCRYPTO_MPI_CHK( mbedcrypto_ecp_mul( &grp, &Q, &prv->d, &prv->grp.G, NULL, NULL ) ); |
| 2054 | |
| 2055 | if( mbedcrypto_mpi_cmp_mpi( &Q.X, &prv->Q.X ) || |
| 2056 | mbedcrypto_mpi_cmp_mpi( &Q.Y, &prv->Q.Y ) || |
| 2057 | mbedcrypto_mpi_cmp_mpi( &Q.Z, &prv->Q.Z ) ) |
| 2058 | { |
| 2059 | ret = MBEDCRYPTO_ERR_ECP_BAD_INPUT_DATA; |
| 2060 | goto cleanup; |
| 2061 | } |
| 2062 | |
| 2063 | cleanup: |
| 2064 | mbedcrypto_ecp_point_free( &Q ); |
| 2065 | mbedcrypto_ecp_group_free( &grp ); |
| 2066 | |
| 2067 | return( ret ); |
| 2068 | } |
| 2069 | |
| 2070 | #if defined(MBEDCRYPTO_SELF_TEST) |
| 2071 | |
| 2072 | /* |
| 2073 | * Checkup routine |
| 2074 | */ |
| 2075 | int mbedcrypto_ecp_self_test( int verbose ) |
| 2076 | { |
| 2077 | int ret; |
| 2078 | size_t i; |
| 2079 | mbedcrypto_ecp_group grp; |
| 2080 | mbedcrypto_ecp_point R, P; |
| 2081 | mbedcrypto_mpi m; |
| 2082 | unsigned long add_c_prev, dbl_c_prev, mul_c_prev; |
| 2083 | /* exponents especially adapted for secp192r1 */ |
| 2084 | const char *exponents[] = |
| 2085 | { |
| 2086 | "000000000000000000000000000000000000000000000001", /* one */ |
| 2087 | "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22830", /* N - 1 */ |
| 2088 | "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */ |
| 2089 | "400000000000000000000000000000000000000000000000", /* one and zeros */ |
| 2090 | "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", /* all ones */ |
| 2091 | "555555555555555555555555555555555555555555555555", /* 101010... */ |
| 2092 | }; |
| 2093 | |
| 2094 | mbedcrypto_ecp_group_init( &grp ); |
| 2095 | mbedcrypto_ecp_point_init( &R ); |
| 2096 | mbedcrypto_ecp_point_init( &P ); |
| 2097 | mbedcrypto_mpi_init( &m ); |
| 2098 | |
| 2099 | /* Use secp192r1 if available, or any available curve */ |
| 2100 | #if defined(MBEDCRYPTO_ECP_DP_SECP192R1_ENABLED) |
| 2101 | MBEDCRYPTO_MPI_CHK( mbedcrypto_ecp_group_load( &grp, MBEDCRYPTO_ECP_DP_SECP192R1 ) ); |
| 2102 | #else |
| 2103 | MBEDCRYPTO_MPI_CHK( mbedcrypto_ecp_group_load( &grp, mbedcrypto_ecp_curve_list()->grp_id ) ); |
| 2104 | #endif |
| 2105 | |
| 2106 | if( verbose != 0 ) |
| 2107 | mbedcrypto_printf( " ECP test #1 (constant op_count, base point G): " ); |
| 2108 | |
| 2109 | /* Do a dummy multiplication first to trigger precomputation */ |
| 2110 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_lset( &m, 2 ) ); |
| 2111 | MBEDCRYPTO_MPI_CHK( mbedcrypto_ecp_mul( &grp, &P, &m, &grp.G, NULL, NULL ) ); |
| 2112 | |
| 2113 | add_count = 0; |
| 2114 | dbl_count = 0; |
| 2115 | mul_count = 0; |
| 2116 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_read_string( &m, 16, exponents[0] ) ); |
| 2117 | MBEDCRYPTO_MPI_CHK( mbedcrypto_ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) ); |
| 2118 | |
| 2119 | for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ ) |
| 2120 | { |
| 2121 | add_c_prev = add_count; |
| 2122 | dbl_c_prev = dbl_count; |
| 2123 | mul_c_prev = mul_count; |
| 2124 | add_count = 0; |
| 2125 | dbl_count = 0; |
| 2126 | mul_count = 0; |
| 2127 | |
| 2128 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_read_string( &m, 16, exponents[i] ) ); |
| 2129 | MBEDCRYPTO_MPI_CHK( mbedcrypto_ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) ); |
| 2130 | |
| 2131 | if( add_count != add_c_prev || |
| 2132 | dbl_count != dbl_c_prev || |
| 2133 | mul_count != mul_c_prev ) |
| 2134 | { |
| 2135 | if( verbose != 0 ) |
| 2136 | mbedcrypto_printf( "failed (%u)\n", (unsigned int) i ); |
| 2137 | |
| 2138 | ret = 1; |
| 2139 | goto cleanup; |
| 2140 | } |
| 2141 | } |
| 2142 | |
| 2143 | if( verbose != 0 ) |
| 2144 | mbedcrypto_printf( "passed\n" ); |
| 2145 | |
| 2146 | if( verbose != 0 ) |
| 2147 | mbedcrypto_printf( " ECP test #2 (constant op_count, other point): " ); |
| 2148 | /* We computed P = 2G last time, use it */ |
| 2149 | |
| 2150 | add_count = 0; |
| 2151 | dbl_count = 0; |
| 2152 | mul_count = 0; |
| 2153 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_read_string( &m, 16, exponents[0] ) ); |
| 2154 | MBEDCRYPTO_MPI_CHK( mbedcrypto_ecp_mul( &grp, &R, &m, &P, NULL, NULL ) ); |
| 2155 | |
| 2156 | for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ ) |
| 2157 | { |
| 2158 | add_c_prev = add_count; |
| 2159 | dbl_c_prev = dbl_count; |
| 2160 | mul_c_prev = mul_count; |
| 2161 | add_count = 0; |
| 2162 | dbl_count = 0; |
| 2163 | mul_count = 0; |
| 2164 | |
| 2165 | MBEDCRYPTO_MPI_CHK( mbedcrypto_mpi_read_string( &m, 16, exponents[i] ) ); |
| 2166 | MBEDCRYPTO_MPI_CHK( mbedcrypto_ecp_mul( &grp, &R, &m, &P, NULL, NULL ) ); |
| 2167 | |
| 2168 | if( add_count != add_c_prev || |
| 2169 | dbl_count != dbl_c_prev || |
| 2170 | mul_count != mul_c_prev ) |
| 2171 | { |
| 2172 | if( verbose != 0 ) |
| 2173 | mbedcrypto_printf( "failed (%u)\n", (unsigned int) i ); |
| 2174 | |
| 2175 | ret = 1; |
| 2176 | goto cleanup; |
| 2177 | } |
| 2178 | } |
| 2179 | |
| 2180 | if( verbose != 0 ) |
| 2181 | mbedcrypto_printf( "passed\n" ); |
| 2182 | |
| 2183 | cleanup: |
| 2184 | |
| 2185 | if( ret < 0 && verbose != 0 ) |
| 2186 | mbedcrypto_printf( "Unexpected error, return code = %08X\n", ret ); |
| 2187 | |
| 2188 | mbedcrypto_ecp_group_free( &grp ); |
| 2189 | mbedcrypto_ecp_point_free( &R ); |
| 2190 | mbedcrypto_ecp_point_free( &P ); |
| 2191 | mbedcrypto_mpi_free( &m ); |
| 2192 | |
| 2193 | if( verbose != 0 ) |
| 2194 | mbedcrypto_printf( "\n" ); |
| 2195 | |
| 2196 | return( ret ); |
| 2197 | } |
| 2198 | |
| 2199 | #endif /* MBEDCRYPTO_SELF_TEST */ |
| 2200 | |
| 2201 | #endif /* !MBEDCRYPTO_ECP_ALT */ |
| 2202 | |
| 2203 | #endif /* MBEDCRYPTO_ECP_C */ |