2 * Elliptic curves over GF(p): generic functions
4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * This file is part of mbed TLS (https://tls.mbed.org)
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
30 * [Curve25519] http://cr.yp.to/ecdh/curve25519-20060209.pdf
32 * [2] CORON, Jean-S'ebastien. Resistance against differential power analysis
33 * for elliptic curve cryptosystems. In : Cryptographic Hardware and
34 * Embedded Systems. Springer Berlin Heidelberg, 1999. p. 292-302.
35 * <http://link.springer.com/chapter/10.1007/3-540-48059-5_25>
37 * [3] HEDABOU, Mustapha, PINEL, Pierre, et B'EN'ETEAU, Lucien. A comb method to
38 * render ECC resistant against Side Channel Attacks. IACR Cryptology
39 * ePrint Archive, 2004, vol. 2004, p. 342.
40 * <http://eprint.iacr.org/2004/342.pdf>
43 #if !defined(MBEDTLS_CONFIG_FILE)
44 #include "mbedtls/config.h"
46 #include MBEDTLS_CONFIG_FILE
49 #if defined(MBEDTLS_ECP_C)
51 #include "mbedtls/ecp.h"
55 #if defined(MBEDTLS_PLATFORM_C)
56 #include "mbedtls/platform.h"
60 #define mbedtls_printf printf
61 #define mbedtls_calloc calloc
62 #define mbedtls_free free
65 #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
66 !defined(inline) && !defined(__cplusplus)
67 #define inline __inline
70 /* Implementation that should never be optimized out by the compiler */
71 static void mbedtls_zeroize( void *v
, size_t n
) {
72 volatile unsigned char *p
= v
; while( n
-- ) *p
++ = 0;
75 #if defined(MBEDTLS_SELF_TEST)
77 * Counts of point addition and doubling, and field multiplications.
78 * Used to test resistance of point multiplication to simple timing attacks.
80 static unsigned long add_count
, dbl_count
, mul_count
;
83 #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) || \
84 defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || \
85 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || \
86 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) || \
87 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) || \
88 defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) || \
89 defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) || \
90 defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) || \
91 defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || \
92 defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || \
93 defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
94 #define ECP_SHORTWEIERSTRASS
97 #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
98 #define ECP_MONTGOMERY
102 * Curve types: internal for now, might be exposed later
107 ECP_TYPE_SHORT_WEIERSTRASS
, /* y^2 = x^3 + a x + b */
108 ECP_TYPE_MONTGOMERY
, /* y^2 = x^3 + a x^2 + x */
112 * List of supported curves:
114 * - TLS NamedCurve ID (RFC 4492 sec. 5.1.1, RFC 7071 sec. 2)
118 * Curves are listed in order: largest curves first, and for a given size,
119 * fastest curves first. This provides the default order for the SSL module.
121 * Reminder: update profiles in x509_crt.c when adding a new curves!
123 static const mbedtls_ecp_curve_info ecp_supported_curves
[] =
125 #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
126 { MBEDTLS_ECP_DP_SECP521R1
, 25, 521, "secp521r1" },
128 #if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
129 { MBEDTLS_ECP_DP_BP512R1
, 28, 512, "brainpoolP512r1" },
131 #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
132 { MBEDTLS_ECP_DP_SECP384R1
, 24, 384, "secp384r1" },
134 #if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
135 { MBEDTLS_ECP_DP_BP384R1
, 27, 384, "brainpoolP384r1" },
137 #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
138 { MBEDTLS_ECP_DP_SECP256R1
, 23, 256, "secp256r1" },
140 #if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
141 { MBEDTLS_ECP_DP_SECP256K1
, 22, 256, "secp256k1" },
143 #if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
144 { MBEDTLS_ECP_DP_BP256R1
, 26, 256, "brainpoolP256r1" },
146 #if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
147 { MBEDTLS_ECP_DP_SECP224R1
, 21, 224, "secp224r1" },
149 #if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
150 { MBEDTLS_ECP_DP_SECP224K1
, 20, 224, "secp224k1" },
152 #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
153 { MBEDTLS_ECP_DP_SECP192R1
, 19, 192, "secp192r1" },
155 #if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
156 { MBEDTLS_ECP_DP_SECP192K1
, 18, 192, "secp192k1" },
158 { MBEDTLS_ECP_DP_NONE
, 0, 0, NULL
},
161 #define ECP_NB_CURVES sizeof( ecp_supported_curves ) / \
162 sizeof( ecp_supported_curves[0] )
164 static mbedtls_ecp_group_id ecp_supported_grp_id
[ECP_NB_CURVES
];
167 * List of supported curves and associated info
169 const mbedtls_ecp_curve_info
*mbedtls_ecp_curve_list( void )
171 return( ecp_supported_curves
);
175 * List of supported curves, group ID only
177 const mbedtls_ecp_group_id
*mbedtls_ecp_grp_id_list( void )
179 static int init_done
= 0;
184 const mbedtls_ecp_curve_info
*curve_info
;
186 for( curve_info
= mbedtls_ecp_curve_list();
187 curve_info
->grp_id
!= MBEDTLS_ECP_DP_NONE
;
190 ecp_supported_grp_id
[i
++] = curve_info
->grp_id
;
192 ecp_supported_grp_id
[i
] = MBEDTLS_ECP_DP_NONE
;
197 return( ecp_supported_grp_id
);
201 * Get the curve info for the internal identifier
203 const mbedtls_ecp_curve_info
*mbedtls_ecp_curve_info_from_grp_id( mbedtls_ecp_group_id grp_id
)
205 const mbedtls_ecp_curve_info
*curve_info
;
207 for( curve_info
= mbedtls_ecp_curve_list();
208 curve_info
->grp_id
!= MBEDTLS_ECP_DP_NONE
;
211 if( curve_info
->grp_id
== grp_id
)
212 return( curve_info
);
219 * Get the curve info from the TLS identifier
221 const mbedtls_ecp_curve_info
*mbedtls_ecp_curve_info_from_tls_id( uint16_t tls_id
)
223 const mbedtls_ecp_curve_info
*curve_info
;
225 for( curve_info
= mbedtls_ecp_curve_list();
226 curve_info
->grp_id
!= MBEDTLS_ECP_DP_NONE
;
229 if( curve_info
->tls_id
== tls_id
)
230 return( curve_info
);
237 * Get the curve info from the name
239 const mbedtls_ecp_curve_info
*mbedtls_ecp_curve_info_from_name( const char *name
)
241 const mbedtls_ecp_curve_info
*curve_info
;
243 for( curve_info
= mbedtls_ecp_curve_list();
244 curve_info
->grp_id
!= MBEDTLS_ECP_DP_NONE
;
247 if( strcmp( curve_info
->name
, name
) == 0 )
248 return( curve_info
);
255 * Get the type of a curve
257 static inline ecp_curve_type
ecp_get_type( const mbedtls_ecp_group
*grp
)
259 if( grp
->G
.X
.p
== NULL
)
260 return( ECP_TYPE_NONE
);
262 if( grp
->G
.Y
.p
== NULL
)
263 return( ECP_TYPE_MONTGOMERY
);
265 return( ECP_TYPE_SHORT_WEIERSTRASS
);
269 * Initialize (the components of) a point
271 void mbedtls_ecp_point_init( mbedtls_ecp_point
*pt
)
276 mbedtls_mpi_init( &pt
->X
);
277 mbedtls_mpi_init( &pt
->Y
);
278 mbedtls_mpi_init( &pt
->Z
);
282 * Initialize (the components of) a group
284 void mbedtls_ecp_group_init( mbedtls_ecp_group
*grp
)
289 memset( grp
, 0, sizeof( mbedtls_ecp_group
) );
293 * Initialize (the components of) a key pair
295 void mbedtls_ecp_keypair_init( mbedtls_ecp_keypair
*key
)
300 mbedtls_ecp_group_init( &key
->grp
);
301 mbedtls_mpi_init( &key
->d
);
302 mbedtls_ecp_point_init( &key
->Q
);
306 * Unallocate (the components of) a point
308 void mbedtls_ecp_point_free( mbedtls_ecp_point
*pt
)
313 mbedtls_mpi_free( &( pt
->X
) );
314 mbedtls_mpi_free( &( pt
->Y
) );
315 mbedtls_mpi_free( &( pt
->Z
) );
319 * Unallocate (the components of) a group
321 void mbedtls_ecp_group_free( mbedtls_ecp_group
*grp
)
330 mbedtls_mpi_free( &grp
->P
);
331 mbedtls_mpi_free( &grp
->A
);
332 mbedtls_mpi_free( &grp
->B
);
333 mbedtls_ecp_point_free( &grp
->G
);
334 mbedtls_mpi_free( &grp
->N
);
339 for( i
= 0; i
< grp
->T_size
; i
++ )
340 mbedtls_ecp_point_free( &grp
->T
[i
] );
341 mbedtls_free( grp
->T
);
344 mbedtls_zeroize( grp
, sizeof( mbedtls_ecp_group
) );
348 * Unallocate (the components of) a key pair
350 void mbedtls_ecp_keypair_free( mbedtls_ecp_keypair
*key
)
355 mbedtls_ecp_group_free( &key
->grp
);
356 mbedtls_mpi_free( &key
->d
);
357 mbedtls_ecp_point_free( &key
->Q
);
361 * Copy the contents of a point
363 int mbedtls_ecp_copy( mbedtls_ecp_point
*P
, const mbedtls_ecp_point
*Q
)
367 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &P
->X
, &Q
->X
) );
368 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &P
->Y
, &Q
->Y
) );
369 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &P
->Z
, &Q
->Z
) );
376 * Copy the contents of a group object
378 int mbedtls_ecp_group_copy( mbedtls_ecp_group
*dst
, const mbedtls_ecp_group
*src
)
380 return mbedtls_ecp_group_load( dst
, src
->id
);
386 int mbedtls_ecp_set_zero( mbedtls_ecp_point
*pt
)
390 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt
->X
, 1 ) );
391 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt
->Y
, 1 ) );
392 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt
->Z
, 0 ) );
399 * Tell if a point is zero
401 int mbedtls_ecp_is_zero( mbedtls_ecp_point
*pt
)
403 return( mbedtls_mpi_cmp_int( &pt
->Z
, 0 ) == 0 );
407 * Import a non-zero point from ASCII strings
409 int mbedtls_ecp_point_read_string( mbedtls_ecp_point
*P
, int radix
,
410 const char *x
, const char *y
)
414 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &P
->X
, radix
, x
) );
415 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &P
->Y
, radix
, y
) );
416 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &P
->Z
, 1 ) );
423 * Export a point into unsigned binary data (SEC1 2.3.3)
425 int mbedtls_ecp_point_write_binary( const mbedtls_ecp_group
*grp
, const mbedtls_ecp_point
*P
,
426 int format
, size_t *olen
,
427 unsigned char *buf
, size_t buflen
)
432 if( format
!= MBEDTLS_ECP_PF_UNCOMPRESSED
&&
433 format
!= MBEDTLS_ECP_PF_COMPRESSED
)
434 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA
);
437 * Common case: P == 0
439 if( mbedtls_mpi_cmp_int( &P
->Z
, 0 ) == 0 )
442 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL
);
450 plen
= mbedtls_mpi_size( &grp
->P
);
452 if( format
== MBEDTLS_ECP_PF_UNCOMPRESSED
)
454 *olen
= 2 * plen
+ 1;
457 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL
);
460 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &P
->X
, buf
+ 1, plen
) );
461 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &P
->Y
, buf
+ 1 + plen
, plen
) );
463 else if( format
== MBEDTLS_ECP_PF_COMPRESSED
)
468 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL
);
470 buf
[0] = 0x02 + mbedtls_mpi_get_bit( &P
->Y
, 0 );
471 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &P
->X
, buf
+ 1, plen
) );
479 * Import a point from unsigned binary data (SEC1 2.3.4)
481 int mbedtls_ecp_point_read_binary( const mbedtls_ecp_group
*grp
, mbedtls_ecp_point
*pt
,
482 const unsigned char *buf
, size_t ilen
)
488 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA
);
493 return( mbedtls_ecp_set_zero( pt
) );
495 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA
);
498 plen
= mbedtls_mpi_size( &grp
->P
);
501 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE
);
503 if( ilen
!= 2 * plen
+ 1 )
504 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA
);
506 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &pt
->X
, buf
+ 1, plen
) );
507 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &pt
->Y
, buf
+ 1 + plen
, plen
) );
508 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt
->Z
, 1 ) );
515 * Import a point from a TLS ECPoint record (RFC 4492)
517 * opaque point <1..2^8-1>;
520 int mbedtls_ecp_tls_read_point( const mbedtls_ecp_group
*grp
, mbedtls_ecp_point
*pt
,
521 const unsigned char **buf
, size_t buf_len
)
523 unsigned char data_len
;
524 const unsigned char *buf_start
;
527 * We must have at least two bytes (1 for length, at least one for data)
530 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA
);
532 data_len
= *(*buf
)++;
533 if( data_len
< 1 || data_len
> buf_len
- 1 )
534 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA
);
537 * Save buffer start for read_binary and update buf
542 return mbedtls_ecp_point_read_binary( grp
, pt
, buf_start
, data_len
);
546 * Export a point as a TLS ECPoint record (RFC 4492)
548 * opaque point <1..2^8-1>;
551 int mbedtls_ecp_tls_write_point( const mbedtls_ecp_group
*grp
, const mbedtls_ecp_point
*pt
,
552 int format
, size_t *olen
,
553 unsigned char *buf
, size_t blen
)
558 * buffer length must be at least one, for our length byte
561 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA
);
563 if( ( ret
= mbedtls_ecp_point_write_binary( grp
, pt
, format
,
564 olen
, buf
+ 1, blen
- 1) ) != 0 )
568 * write length to the first byte and update total length
570 buf
[0] = (unsigned char) *olen
;
577 * Set a group from an ECParameters record (RFC 4492)
579 int mbedtls_ecp_tls_read_group( mbedtls_ecp_group
*grp
, const unsigned char **buf
, size_t len
)
582 const mbedtls_ecp_curve_info
*curve_info
;
585 * We expect at least three bytes (see below)
588 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA
);
591 * First byte is curve_type; only named_curve is handled
593 if( *(*buf
)++ != MBEDTLS_ECP_TLS_NAMED_CURVE
)
594 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA
);
597 * Next two bytes are the namedcurve value
603 if( ( curve_info
= mbedtls_ecp_curve_info_from_tls_id( tls_id
) ) == NULL
)
604 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE
);
606 return mbedtls_ecp_group_load( grp
, curve_info
->grp_id
);
610 * Write the ECParameters record corresponding to a group (RFC 4492)
612 int mbedtls_ecp_tls_write_group( const mbedtls_ecp_group
*grp
, size_t *olen
,
613 unsigned char *buf
, size_t blen
)
615 const mbedtls_ecp_curve_info
*curve_info
;
617 if( ( curve_info
= mbedtls_ecp_curve_info_from_grp_id( grp
->id
) ) == NULL
)
618 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA
);
621 * We are going to write 3 bytes (see below)
625 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL
);
628 * First byte is curve_type, always named_curve
630 *buf
++ = MBEDTLS_ECP_TLS_NAMED_CURVE
;
633 * Next two bytes are the namedcurve value
635 buf
[0] = curve_info
->tls_id
>> 8;
636 buf
[1] = curve_info
->tls_id
& 0xFF;
642 * Wrapper around fast quasi-modp functions, with fall-back to mbedtls_mpi_mod_mpi.
643 * See the documentation of struct mbedtls_ecp_group.
645 * This function is in the critial loop for mbedtls_ecp_mul, so pay attention to perf.
647 static int ecp_modp( mbedtls_mpi
*N
, const mbedtls_ecp_group
*grp
)
651 if( grp
->modp
== NULL
)
652 return( mbedtls_mpi_mod_mpi( N
, N
, &grp
->P
) );
654 /* N->s < 0 is a much faster test, which fails only if N is 0 */
655 if( ( N
->s
< 0 && mbedtls_mpi_cmp_int( N
, 0 ) != 0 ) ||
656 mbedtls_mpi_bitlen( N
) > 2 * grp
->pbits
)
658 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA
);
661 MBEDTLS_MPI_CHK( grp
->modp( N
) );
663 /* N->s < 0 is a much faster test, which fails only if N is 0 */
664 while( N
->s
< 0 && mbedtls_mpi_cmp_int( N
, 0 ) != 0 )
665 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( N
, N
, &grp
->P
) );
667 while( mbedtls_mpi_cmp_mpi( N
, &grp
->P
) >= 0 )
668 /* we known P, N and the result are positive */
669 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( N
, N
, &grp
->P
) );
676 * Fast mod-p functions expect their argument to be in the 0..p^2 range.
678 * In order to guarantee that, we need to ensure that operands of
679 * mbedtls_mpi_mul_mpi are in the 0..p range. So, after each operation we will
680 * bring the result back to this range.
682 * The following macros are shortcuts for doing that.
686 * Reduce a mbedtls_mpi mod p in-place, general case, to use after mbedtls_mpi_mul_mpi
688 #if defined(MBEDTLS_SELF_TEST)
689 #define INC_MUL_COUNT mul_count++;
691 #define INC_MUL_COUNT
694 #define MOD_MUL( N ) do { MBEDTLS_MPI_CHK( ecp_modp( &N, grp ) ); INC_MUL_COUNT } \
698 * Reduce a mbedtls_mpi mod p in-place, to use after mbedtls_mpi_sub_mpi
699 * N->s < 0 is a very fast test, which fails only if N is 0
701 #define MOD_SUB( N ) \
702 while( N.s < 0 && mbedtls_mpi_cmp_int( &N, 0 ) != 0 ) \
703 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &N, &N, &grp->P ) )
706 * Reduce a mbedtls_mpi mod p in-place, to use after mbedtls_mpi_add_mpi and mbedtls_mpi_mul_int.
707 * We known P, N and the result are positive, so sub_abs is correct, and
710 #define MOD_ADD( N ) \
711 while( mbedtls_mpi_cmp_mpi( &N, &grp->P ) >= 0 ) \
712 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &N, &N, &grp->P ) )
714 #if defined(ECP_SHORTWEIERSTRASS)
716 * For curves in short Weierstrass form, we do all the internal operations in
717 * Jacobian coordinates.
719 * For multiplication, we'll use a comb method with coutermeasueres against
720 * SPA, hence timing attacks.
724 * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
725 * Cost: 1N := 1I + 3M + 1S
727 static int ecp_normalize_jac( const mbedtls_ecp_group
*grp
, mbedtls_ecp_point
*pt
)
732 if( mbedtls_mpi_cmp_int( &pt
->Z
, 0 ) == 0 )
735 mbedtls_mpi_init( &Zi
); mbedtls_mpi_init( &ZZi
);
740 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &Zi
, &pt
->Z
, &grp
->P
) );
741 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ZZi
, &Zi
, &Zi
) ); MOD_MUL( ZZi
);
742 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt
->X
, &pt
->X
, &ZZi
) ); MOD_MUL( pt
->X
);
747 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt
->Y
, &pt
->Y
, &ZZi
) ); MOD_MUL( pt
->Y
);
748 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt
->Y
, &pt
->Y
, &Zi
) ); MOD_MUL( pt
->Y
);
753 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt
->Z
, 1 ) );
757 mbedtls_mpi_free( &Zi
); mbedtls_mpi_free( &ZZi
);
763 * Normalize jacobian coordinates of an array of (pointers to) points,
764 * using Montgomery's trick to perform only one inversion mod P.
765 * (See for example Cohen's "A Course in Computational Algebraic Number
766 * Theory", Algorithm 10.3.4.)
768 * Warning: fails (returning an error) if one of the points is zero!
769 * This should never happen, see choice of w in ecp_mul_comb().
771 * Cost: 1N(t) := 1I + (6t - 3)M + 1S
773 static int ecp_normalize_jac_many( const mbedtls_ecp_group
*grp
,
774 mbedtls_ecp_point
*T
[], size_t t_len
)
778 mbedtls_mpi
*c
, u
, Zi
, ZZi
;
781 return( ecp_normalize_jac( grp
, *T
) );
783 if( ( c
= mbedtls_calloc( t_len
, sizeof( mbedtls_mpi
) ) ) == NULL
)
784 return( MBEDTLS_ERR_ECP_ALLOC_FAILED
);
786 mbedtls_mpi_init( &u
); mbedtls_mpi_init( &Zi
); mbedtls_mpi_init( &ZZi
);
789 * c[i] = Z_0 * ... * Z_i
791 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &c
[0], &T
[0]->Z
) );
792 for( i
= 1; i
< t_len
; i
++ )
794 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &c
[i
], &c
[i
-1], &T
[i
]->Z
) );
799 * u = 1 / (Z_0 * ... * Z_n) mod P
801 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &u
, &c
[t_len
-1], &grp
->P
) );
803 for( i
= t_len
- 1; ; i
-- )
807 * u = 1 / (Z_0 * ... * Z_i) mod P
810 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Zi
, &u
) );
814 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &Zi
, &u
, &c
[i
-1] ) ); MOD_MUL( Zi
);
815 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &u
, &u
, &T
[i
]->Z
) ); MOD_MUL( u
);
819 * proceed as in normalize()
821 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ZZi
, &Zi
, &Zi
) ); MOD_MUL( ZZi
);
822 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T
[i
]->X
, &T
[i
]->X
, &ZZi
) ); MOD_MUL( T
[i
]->X
);
823 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T
[i
]->Y
, &T
[i
]->Y
, &ZZi
) ); MOD_MUL( T
[i
]->Y
);
824 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T
[i
]->Y
, &T
[i
]->Y
, &Zi
) ); MOD_MUL( T
[i
]->Y
);
827 * Post-precessing: reclaim some memory by shrinking coordinates
828 * - not storing Z (always 1)
829 * - shrinking other coordinates, but still keeping the same number of
830 * limbs as P, as otherwise it will too likely be regrown too fast.
832 MBEDTLS_MPI_CHK( mbedtls_mpi_shrink( &T
[i
]->X
, grp
->P
.n
) );
833 MBEDTLS_MPI_CHK( mbedtls_mpi_shrink( &T
[i
]->Y
, grp
->P
.n
) );
834 mbedtls_mpi_free( &T
[i
]->Z
);
842 mbedtls_mpi_free( &u
); mbedtls_mpi_free( &Zi
); mbedtls_mpi_free( &ZZi
);
843 for( i
= 0; i
< t_len
; i
++ )
844 mbedtls_mpi_free( &c
[i
] );
851 * Conditional point inversion: Q -> -Q = (Q.X, -Q.Y, Q.Z) without leak.
852 * "inv" must be 0 (don't invert) or 1 (invert) or the result will be invalid
854 static int ecp_safe_invert_jac( const mbedtls_ecp_group
*grp
,
855 mbedtls_ecp_point
*Q
,
859 unsigned char nonzero
;
862 mbedtls_mpi_init( &mQY
);
864 /* Use the fact that -Q.Y mod P = P - Q.Y unless Q.Y == 0 */
865 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &mQY
, &grp
->P
, &Q
->Y
) );
866 nonzero
= mbedtls_mpi_cmp_int( &Q
->Y
, 0 ) != 0;
867 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &Q
->Y
, &mQY
, inv
& nonzero
) );
870 mbedtls_mpi_free( &mQY
);
876 * Point doubling R = 2 P, Jacobian coordinates
878 * Based on http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian.html#doubling-dbl-1998-cmo-2 .
880 * We follow the variable naming fairly closely. The formula variations that trade a MUL for a SQR
881 * (plus a few ADDs) aren't useful as our bignum implementation doesn't distinguish squaring.
883 * Standard optimizations are applied when curve parameter A is one of { 0, -3 }.
885 * Cost: 1D := 3M + 4S (A == 0)
887 * 3M + 6S + 1a otherwise
889 static int ecp_double_jac( const mbedtls_ecp_group
*grp
, mbedtls_ecp_point
*R
,
890 const mbedtls_ecp_point
*P
)
893 mbedtls_mpi M
, S
, T
, U
;
895 #if defined(MBEDTLS_SELF_TEST)
899 mbedtls_mpi_init( &M
); mbedtls_mpi_init( &S
); mbedtls_mpi_init( &T
); mbedtls_mpi_init( &U
);
901 /* Special case for A = -3 */
902 if( grp
->A
.p
== NULL
)
904 /* M = 3(X + Z^2)(X - Z^2) */
905 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S
, &P
->Z
, &P
->Z
) ); MOD_MUL( S
);
906 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T
, &P
->X
, &S
) ); MOD_ADD( T
);
907 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U
, &P
->X
, &S
) ); MOD_SUB( U
);
908 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S
, &T
, &U
) ); MOD_MUL( S
);
909 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &M
, &S
, 3 ) ); MOD_ADD( M
);
914 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S
, &P
->X
, &P
->X
) ); MOD_MUL( S
);
915 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &M
, &S
, 3 ) ); MOD_ADD( M
);
917 /* Optimize away for "koblitz" curves with A = 0 */
918 if( mbedtls_mpi_cmp_int( &grp
->A
, 0 ) != 0 )
921 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S
, &P
->Z
, &P
->Z
) ); MOD_MUL( S
);
922 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T
, &S
, &S
) ); MOD_MUL( T
);
923 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S
, &T
, &grp
->A
) ); MOD_MUL( S
);
924 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &M
, &M
, &S
) ); MOD_ADD( M
);
929 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T
, &P
->Y
, &P
->Y
) ); MOD_MUL( T
);
930 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &T
, 1 ) ); MOD_ADD( T
);
931 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S
, &P
->X
, &T
) ); MOD_MUL( S
);
932 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &S
, 1 ) ); MOD_ADD( S
);
935 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &U
, &T
, &T
) ); MOD_MUL( U
);
936 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &U
, 1 ) ); MOD_ADD( U
);
939 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T
, &M
, &M
) ); MOD_MUL( T
);
940 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T
, &T
, &S
) ); MOD_SUB( T
);
941 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T
, &T
, &S
) ); MOD_SUB( T
);
943 /* S = M(S - T) - U */
944 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &S
, &S
, &T
) ); MOD_SUB( S
);
945 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S
, &S
, &M
) ); MOD_MUL( S
);
946 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &S
, &S
, &U
) ); MOD_SUB( S
);
949 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &U
, &P
->Y
, &P
->Z
) ); MOD_MUL( U
);
950 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &U
, 1 ) ); MOD_ADD( U
);
952 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R
->X
, &T
) );
953 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R
->Y
, &S
) );
954 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R
->Z
, &U
) );
957 mbedtls_mpi_free( &M
); mbedtls_mpi_free( &S
); mbedtls_mpi_free( &T
); mbedtls_mpi_free( &U
);
963 * Addition: R = P + Q, mixed affine-Jacobian coordinates (GECC 3.22)
965 * The coordinates of Q must be normalized (= affine),
966 * but those of P don't need to. R is not normalized.
968 * Special cases: (1) P or Q is zero, (2) R is zero, (3) P == Q.
969 * None of these cases can happen as intermediate step in ecp_mul_comb():
970 * - at each step, P, Q and R are multiples of the base point, the factor
971 * being less than its order, so none of them is zero;
972 * - Q is an odd multiple of the base point, P an even multiple,
973 * due to the choice of precomputed points in the modified comb method.
974 * So branches for these cases do not leak secret information.
976 * We accept Q->Z being unset (saving memory in tables) as meaning 1.
978 * Cost: 1A := 8M + 3S
980 static int ecp_add_mixed( const mbedtls_ecp_group
*grp
, mbedtls_ecp_point
*R
,
981 const mbedtls_ecp_point
*P
, const mbedtls_ecp_point
*Q
)
984 mbedtls_mpi T1
, T2
, T3
, T4
, X
, Y
, Z
;
986 #if defined(MBEDTLS_SELF_TEST)
991 * Trivial cases: P == 0 or Q == 0 (case 1)
993 if( mbedtls_mpi_cmp_int( &P
->Z
, 0 ) == 0 )
994 return( mbedtls_ecp_copy( R
, Q
) );
996 if( Q
->Z
.p
!= NULL
&& mbedtls_mpi_cmp_int( &Q
->Z
, 0 ) == 0 )
997 return( mbedtls_ecp_copy( R
, P
) );
1000 * Make sure Q coordinates are normalized
1002 if( Q
->Z
.p
!= NULL
&& mbedtls_mpi_cmp_int( &Q
->Z
, 1 ) != 0 )
1003 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA
);
1005 mbedtls_mpi_init( &T1
); mbedtls_mpi_init( &T2
); mbedtls_mpi_init( &T3
); mbedtls_mpi_init( &T4
);
1006 mbedtls_mpi_init( &X
); mbedtls_mpi_init( &Y
); mbedtls_mpi_init( &Z
);
1008 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1
, &P
->Z
, &P
->Z
) ); MOD_MUL( T1
);
1009 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T2
, &T1
, &P
->Z
) ); MOD_MUL( T2
);
1010 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1
, &T1
, &Q
->X
) ); MOD_MUL( T1
);
1011 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T2
, &T2
, &Q
->Y
) ); MOD_MUL( T2
);
1012 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T1
, &T1
, &P
->X
) ); MOD_SUB( T1
);
1013 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T2
, &T2
, &P
->Y
) ); MOD_SUB( T2
);
1015 /* Special cases (2) and (3) */
1016 if( mbedtls_mpi_cmp_int( &T1
, 0 ) == 0 )
1018 if( mbedtls_mpi_cmp_int( &T2
, 0 ) == 0 )
1020 ret
= ecp_double_jac( grp
, R
, P
);
1025 ret
= mbedtls_ecp_set_zero( R
);
1030 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &Z
, &P
->Z
, &T1
) ); MOD_MUL( Z
);
1031 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T3
, &T1
, &T1
) ); MOD_MUL( T3
);
1032 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T4
, &T3
, &T1
) ); MOD_MUL( T4
);
1033 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T3
, &T3
, &P
->X
) ); MOD_MUL( T3
);
1034 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T1
, &T3
, 2 ) ); MOD_ADD( T1
);
1035 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &X
, &T2
, &T2
) ); MOD_MUL( X
);
1036 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X
, &X
, &T1
) ); MOD_SUB( X
);
1037 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X
, &X
, &T4
) ); MOD_SUB( X
);
1038 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T3
, &T3
, &X
) ); MOD_SUB( T3
);
1039 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T3
, &T3
, &T2
) ); MOD_MUL( T3
);
1040 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T4
, &T4
, &P
->Y
) ); MOD_MUL( T4
);
1041 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &Y
, &T3
, &T4
) ); MOD_SUB( Y
);
1043 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R
->X
, &X
) );
1044 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R
->Y
, &Y
) );
1045 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R
->Z
, &Z
) );
1049 mbedtls_mpi_free( &T1
); mbedtls_mpi_free( &T2
); mbedtls_mpi_free( &T3
); mbedtls_mpi_free( &T4
);
1050 mbedtls_mpi_free( &X
); mbedtls_mpi_free( &Y
); mbedtls_mpi_free( &Z
);
1056 * Randomize jacobian coordinates:
1057 * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l
1058 * This is sort of the reverse operation of ecp_normalize_jac().
1060 * This countermeasure was first suggested in [2].
1062 static int ecp_randomize_jac( const mbedtls_ecp_group
*grp
, mbedtls_ecp_point
*pt
,
1063 int (*f_rng
)(void *, unsigned char *, size_t), void *p_rng
)
1067 size_t p_size
= ( grp
->pbits
+ 7 ) / 8;
1070 mbedtls_mpi_init( &l
); mbedtls_mpi_init( &ll
);
1072 /* Generate l such that 1 < l < p */
1075 mbedtls_mpi_fill_random( &l
, p_size
, f_rng
, p_rng
);
1077 while( mbedtls_mpi_cmp_mpi( &l
, &grp
->P
) >= 0 )
1078 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &l
, 1 ) );
1081 return( MBEDTLS_ERR_ECP_RANDOM_FAILED
);
1083 while( mbedtls_mpi_cmp_int( &l
, 1 ) <= 0 );
1086 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt
->Z
, &pt
->Z
, &l
) ); MOD_MUL( pt
->Z
);
1089 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ll
, &l
, &l
) ); MOD_MUL( ll
);
1090 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt
->X
, &pt
->X
, &ll
) ); MOD_MUL( pt
->X
);
1093 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ll
, &ll
, &l
) ); MOD_MUL( ll
);
1094 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt
->Y
, &pt
->Y
, &ll
) ); MOD_MUL( pt
->Y
);
1097 mbedtls_mpi_free( &l
); mbedtls_mpi_free( &ll
);
1103 * Check and define parameters used by the comb method (see below for details)
1105 #if MBEDTLS_ECP_WINDOW_SIZE < 2 || MBEDTLS_ECP_WINDOW_SIZE > 7
1106 #error "MBEDTLS_ECP_WINDOW_SIZE out of bounds"
1109 /* d = ceil( n / w ) */
1110 #define COMB_MAX_D ( MBEDTLS_ECP_MAX_BITS + 1 ) / 2
1112 /* number of precomputed points */
1113 #define COMB_MAX_PRE ( 1 << ( MBEDTLS_ECP_WINDOW_SIZE - 1 ) )
1116 * Compute the representation of m that will be used with our comb method.
1118 * The basic comb method is described in GECC 3.44 for example. We use a
1119 * modified version that provides resistance to SPA by avoiding zero
1120 * digits in the representation as in [3]. We modify the method further by
1121 * requiring that all K_i be odd, which has the small cost that our
1122 * representation uses one more K_i, due to carries.
1124 * Also, for the sake of compactness, only the seven low-order bits of x[i]
1125 * are used to represent K_i, and the msb of x[i] encodes the the sign (s_i in
1126 * the paper): it is set if and only if if s_i == -1;
1128 * Calling conventions:
1129 * - x is an array of size d + 1
1130 * - w is the size, ie number of teeth, of the comb, and must be between
1131 * 2 and 7 (in practice, between 2 and MBEDTLS_ECP_WINDOW_SIZE)
1132 * - m is the MPI, expected to be odd and such that bitlength(m) <= w * d
1133 * (the result will be incorrect if these assumptions are not satisfied)
1135 static void ecp_comb_fixed( unsigned char x
[], size_t d
,
1136 unsigned char w
, const mbedtls_mpi
*m
)
1139 unsigned char c
, cc
, adjust
;
1141 memset( x
, 0, d
+1 );
1143 /* First get the classical comb values (except for x_d = 0) */
1144 for( i
= 0; i
< d
; i
++ )
1145 for( j
= 0; j
< w
; j
++ )
1146 x
[i
] |= mbedtls_mpi_get_bit( m
, i
+ d
* j
) << j
;
1148 /* Now make sure x_1 .. x_d are odd */
1150 for( i
= 1; i
<= d
; i
++ )
1152 /* Add carry and update it */
1157 /* Adjust if needed, avoiding branches */
1158 adjust
= 1 - ( x
[i
] & 0x01 );
1159 c
|= x
[i
] & ( x
[i
-1] * adjust
);
1160 x
[i
] = x
[i
] ^ ( x
[i
-1] * adjust
);
1161 x
[i
-1] |= adjust
<< 7;
1166 * Precompute points for the comb method
1168 * If i = i_{w-1} ... i_1 is the binary representation of i, then
1169 * T[i] = i_{w-1} 2^{(w-1)d} P + ... + i_1 2^d P + P
1171 * T must be able to hold 2^{w - 1} elements
1173 * Cost: d(w-1) D + (2^{w-1} - 1) A + 1 N(w-1) + 1 N(2^{w-1} - 1)
1175 static int ecp_precompute_comb( const mbedtls_ecp_group
*grp
,
1176 mbedtls_ecp_point T
[], const mbedtls_ecp_point
*P
,
1177 unsigned char w
, size_t d
)
1182 mbedtls_ecp_point
*cur
, *TT
[COMB_MAX_PRE
- 1];
1186 * T[2^{l-1}] = 2^{dl} P for l = 1 .. w-1 (this is not the final value)
1188 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( &T
[0], P
) );
1191 for( i
= 1; i
< ( 1U << ( w
- 1 ) ); i
<<= 1 )
1194 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( cur
, T
+ ( i
>> 1 ) ) );
1195 for( j
= 0; j
< d
; j
++ )
1196 MBEDTLS_MPI_CHK( ecp_double_jac( grp
, cur
, cur
) );
1201 MBEDTLS_MPI_CHK( ecp_normalize_jac_many( grp
, TT
, k
) );
1204 * Compute the remaining ones using the minimal number of additions
1205 * Be careful to update T[2^l] only after using it!
1208 for( i
= 1; i
< ( 1U << ( w
- 1 ) ); i
<<= 1 )
1213 MBEDTLS_MPI_CHK( ecp_add_mixed( grp
, &T
[i
+ j
], &T
[j
], &T
[i
] ) );
1214 TT
[k
++] = &T
[i
+ j
];
1218 MBEDTLS_MPI_CHK( ecp_normalize_jac_many( grp
, TT
, k
) );
1225 * Select precomputed point: R = sign(i) * T[ abs(i) / 2 ]
1227 static int ecp_select_comb( const mbedtls_ecp_group
*grp
, mbedtls_ecp_point
*R
,
1228 const mbedtls_ecp_point T
[], unsigned char t_len
,
1232 unsigned char ii
, j
;
1234 /* Ignore the "sign" bit and scale down */
1235 ii
= ( i
& 0x7Fu
) >> 1;
1237 /* Read the whole table to thwart cache-based timing attacks */
1238 for( j
= 0; j
< t_len
; j
++ )
1240 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &R
->X
, &T
[j
].X
, j
== ii
) );
1241 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &R
->Y
, &T
[j
].Y
, j
== ii
) );
1244 /* Safely invert result if i is "negative" */
1245 MBEDTLS_MPI_CHK( ecp_safe_invert_jac( grp
, R
, i
>> 7 ) );
1252 * Core multiplication algorithm for the (modified) comb method.
1253 * This part is actually common with the basic comb method (GECC 3.44)
1255 * Cost: d A + d D + 1 R
1257 static int ecp_mul_comb_core( const mbedtls_ecp_group
*grp
, mbedtls_ecp_point
*R
,
1258 const mbedtls_ecp_point T
[], unsigned char t_len
,
1259 const unsigned char x
[], size_t d
,
1260 int (*f_rng
)(void *, unsigned char *, size_t),
1264 mbedtls_ecp_point Txi
;
1267 mbedtls_ecp_point_init( &Txi
);
1269 /* Start with a non-zero point and randomize its coordinates */
1271 MBEDTLS_MPI_CHK( ecp_select_comb( grp
, R
, T
, t_len
, x
[i
] ) );
1272 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R
->Z
, 1 ) );
1274 MBEDTLS_MPI_CHK( ecp_randomize_jac( grp
, R
, f_rng
, p_rng
) );
1278 MBEDTLS_MPI_CHK( ecp_double_jac( grp
, R
, R
) );
1279 MBEDTLS_MPI_CHK( ecp_select_comb( grp
, &Txi
, T
, t_len
, x
[i
] ) );
1280 MBEDTLS_MPI_CHK( ecp_add_mixed( grp
, R
, R
, &Txi
) );
1284 mbedtls_ecp_point_free( &Txi
);
1290 * Multiplication using the comb method,
1291 * for curves in short Weierstrass form
1293 static int ecp_mul_comb( mbedtls_ecp_group
*grp
, mbedtls_ecp_point
*R
,
1294 const mbedtls_mpi
*m
, const mbedtls_ecp_point
*P
,
1295 int (*f_rng
)(void *, unsigned char *, size_t),
1299 unsigned char w
, m_is_odd
, p_eq_g
, pre_len
, i
;
1301 unsigned char k
[COMB_MAX_D
+ 1];
1302 mbedtls_ecp_point
*T
;
1305 mbedtls_mpi_init( &M
);
1306 mbedtls_mpi_init( &mm
);
1308 /* we need N to be odd to trnaform m in an odd number, check now */
1309 if( mbedtls_mpi_get_bit( &grp
->N
, 0 ) != 1 )
1310 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA
);
1313 * Minimize the number of multiplications, that is minimize
1314 * 10 * d * w + 18 * 2^(w-1) + 11 * d + 7 * w, with d = ceil( nbits / w )
1315 * (see costs of the various parts, with 1S = 1M)
1317 w
= grp
->nbits
>= 384 ? 5 : 4;
1320 * If P == G, pre-compute a bit more, since this may be re-used later.
1321 * Just adding one avoids upping the cost of the first mul too much,
1322 * and the memory cost too.
1324 #if MBEDTLS_ECP_FIXED_POINT_OPTIM == 1
1325 p_eq_g
= ( mbedtls_mpi_cmp_mpi( &P
->Y
, &grp
->G
.Y
) == 0 &&
1326 mbedtls_mpi_cmp_mpi( &P
->X
, &grp
->G
.X
) == 0 );
1334 * Make sure w is within bounds.
1335 * (The last test is useful only for very small curves in the test suite.)
1337 if( w
> MBEDTLS_ECP_WINDOW_SIZE
)
1338 w
= MBEDTLS_ECP_WINDOW_SIZE
;
1339 if( w
>= grp
->nbits
)
1342 /* Other sizes that depend on w */
1343 pre_len
= 1U << ( w
- 1 );
1344 d
= ( grp
->nbits
+ w
- 1 ) / w
;
1347 * Prepare precomputed points: if P == G we want to
1348 * use grp->T if already initialized, or initialize it.
1350 T
= p_eq_g
? grp
->T
: NULL
;
1354 T
= mbedtls_calloc( pre_len
, sizeof( mbedtls_ecp_point
) );
1357 ret
= MBEDTLS_ERR_ECP_ALLOC_FAILED
;
1361 MBEDTLS_MPI_CHK( ecp_precompute_comb( grp
, T
, P
, w
, d
) );
1366 grp
->T_size
= pre_len
;
1371 * Make sure M is odd (M = m or M = N - m, since N is odd)
1372 * using the fact that m * P = - (N - m) * P
1374 m_is_odd
= ( mbedtls_mpi_get_bit( m
, 0 ) == 1 );
1375 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &M
, m
) );
1376 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &mm
, &grp
->N
, m
) );
1377 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &M
, &mm
, ! m_is_odd
) );
1380 * Go for comb multiplication, R = M * P
1382 ecp_comb_fixed( k
, d
, w
, &M
);
1383 MBEDTLS_MPI_CHK( ecp_mul_comb_core( grp
, R
, T
, pre_len
, k
, d
, f_rng
, p_rng
) );
1386 * Now get m * P from M * P and normalize it
1388 MBEDTLS_MPI_CHK( ecp_safe_invert_jac( grp
, R
, ! m_is_odd
) );
1389 MBEDTLS_MPI_CHK( ecp_normalize_jac( grp
, R
) );
1393 if( T
!= NULL
&& ! p_eq_g
)
1395 for( i
= 0; i
< pre_len
; i
++ )
1396 mbedtls_ecp_point_free( &T
[i
] );
1400 mbedtls_mpi_free( &M
);
1401 mbedtls_mpi_free( &mm
);
1404 mbedtls_ecp_point_free( R
);
1409 #endif /* ECP_SHORTWEIERSTRASS */
1411 #if defined(ECP_MONTGOMERY)
1413 * For Montgomery curves, we do all the internal arithmetic in projective
1414 * coordinates. Import/export of points uses only the x coordinates, which is
1415 * internaly represented as X / Z.
1417 * For scalar multiplication, we'll use a Montgomery ladder.
1421 * Normalize Montgomery x/z coordinates: X = X/Z, Z = 1
1424 static int ecp_normalize_mxz( const mbedtls_ecp_group
*grp
, mbedtls_ecp_point
*P
)
1428 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &P
->Z
, &P
->Z
, &grp
->P
) );
1429 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &P
->X
, &P
->X
, &P
->Z
) ); MOD_MUL( P
->X
);
1430 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &P
->Z
, 1 ) );
1437 * Randomize projective x/z coordinates:
1438 * (X, Z) -> (l X, l Z) for random l
1439 * This is sort of the reverse operation of ecp_normalize_mxz().
1441 * This countermeasure was first suggested in [2].
1444 static int ecp_randomize_mxz( const mbedtls_ecp_group
*grp
, mbedtls_ecp_point
*P
,
1445 int (*f_rng
)(void *, unsigned char *, size_t), void *p_rng
)
1449 size_t p_size
= ( grp
->pbits
+ 7 ) / 8;
1452 mbedtls_mpi_init( &l
);
1454 /* Generate l such that 1 < l < p */
1457 mbedtls_mpi_fill_random( &l
, p_size
, f_rng
, p_rng
);
1459 while( mbedtls_mpi_cmp_mpi( &l
, &grp
->P
) >= 0 )
1460 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &l
, 1 ) );
1463 return( MBEDTLS_ERR_ECP_RANDOM_FAILED
);
1465 while( mbedtls_mpi_cmp_int( &l
, 1 ) <= 0 );
1467 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &P
->X
, &P
->X
, &l
) ); MOD_MUL( P
->X
);
1468 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &P
->Z
, &P
->Z
, &l
) ); MOD_MUL( P
->Z
);
1471 mbedtls_mpi_free( &l
);
1477 * Double-and-add: R = 2P, S = P + Q, with d = X(P - Q),
1478 * for Montgomery curves in x/z coordinates.
1480 * http://www.hyperelliptic.org/EFD/g1p/auto-code/montgom/xz/ladder/mladd-1987-m.op3
1487 * and eliminating temporary variables tO, ..., t4.
1491 static int ecp_double_add_mxz( const mbedtls_ecp_group
*grp
,
1492 mbedtls_ecp_point
*R
, mbedtls_ecp_point
*S
,
1493 const mbedtls_ecp_point
*P
, const mbedtls_ecp_point
*Q
,
1494 const mbedtls_mpi
*d
)
1497 mbedtls_mpi A
, AA
, B
, BB
, E
, C
, D
, DA
, CB
;
1499 mbedtls_mpi_init( &A
); mbedtls_mpi_init( &AA
); mbedtls_mpi_init( &B
);
1500 mbedtls_mpi_init( &BB
); mbedtls_mpi_init( &E
); mbedtls_mpi_init( &C
);
1501 mbedtls_mpi_init( &D
); mbedtls_mpi_init( &DA
); mbedtls_mpi_init( &CB
);
1503 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &A
, &P
->X
, &P
->Z
) ); MOD_ADD( A
);
1504 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &AA
, &A
, &A
) ); MOD_MUL( AA
);
1505 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &B
, &P
->X
, &P
->Z
) ); MOD_SUB( B
);
1506 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &BB
, &B
, &B
) ); MOD_MUL( BB
);
1507 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &E
, &AA
, &BB
) ); MOD_SUB( E
);
1508 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &C
, &Q
->X
, &Q
->Z
) ); MOD_ADD( C
);
1509 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &D
, &Q
->X
, &Q
->Z
) ); MOD_SUB( D
);
1510 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DA
, &D
, &A
) ); MOD_MUL( DA
);
1511 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &CB
, &C
, &B
) ); MOD_MUL( CB
);
1512 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &S
->X
, &DA
, &CB
) ); MOD_MUL( S
->X
);
1513 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S
->X
, &S
->X
, &S
->X
) ); MOD_MUL( S
->X
);
1514 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &S
->Z
, &DA
, &CB
) ); MOD_SUB( S
->Z
);
1515 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S
->Z
, &S
->Z
, &S
->Z
) ); MOD_MUL( S
->Z
);
1516 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S
->Z
, d
, &S
->Z
) ); MOD_MUL( S
->Z
);
1517 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &R
->X
, &AA
, &BB
) ); MOD_MUL( R
->X
);
1518 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &R
->Z
, &grp
->A
, &E
) ); MOD_MUL( R
->Z
);
1519 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &R
->Z
, &BB
, &R
->Z
) ); MOD_ADD( R
->Z
);
1520 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &R
->Z
, &E
, &R
->Z
) ); MOD_MUL( R
->Z
);
1523 mbedtls_mpi_free( &A
); mbedtls_mpi_free( &AA
); mbedtls_mpi_free( &B
);
1524 mbedtls_mpi_free( &BB
); mbedtls_mpi_free( &E
); mbedtls_mpi_free( &C
);
1525 mbedtls_mpi_free( &D
); mbedtls_mpi_free( &DA
); mbedtls_mpi_free( &CB
);
1531 * Multiplication with Montgomery ladder in x/z coordinates,
1532 * for curves in Montgomery form
1534 static int ecp_mul_mxz( mbedtls_ecp_group
*grp
, mbedtls_ecp_point
*R
,
1535 const mbedtls_mpi
*m
, const mbedtls_ecp_point
*P
,
1536 int (*f_rng
)(void *, unsigned char *, size_t),
1542 mbedtls_ecp_point RP
;
1545 mbedtls_ecp_point_init( &RP
); mbedtls_mpi_init( &PX
);
1547 /* Save PX and read from P before writing to R, in case P == R */
1548 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &PX
, &P
->X
) );
1549 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( &RP
, P
) );
1551 /* Set R to zero in modified x/z coordinates */
1552 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R
->X
, 1 ) );
1553 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R
->Z
, 0 ) );
1554 mbedtls_mpi_free( &R
->Y
);
1556 /* RP.X might be sligtly larger than P, so reduce it */
1559 /* Randomize coordinates of the starting point */
1561 MBEDTLS_MPI_CHK( ecp_randomize_mxz( grp
, &RP
, f_rng
, p_rng
) );
1563 /* Loop invariant: R = result so far, RP = R + P */
1564 i
= mbedtls_mpi_bitlen( m
); /* one past the (zero-based) most significant bit */
1567 b
= mbedtls_mpi_get_bit( m
, i
);
1569 * if (b) R = 2R + P else R = 2R,
1571 * if (b) double_add( RP, R, RP, R )
1572 * else double_add( R, RP, R, RP )
1573 * but using safe conditional swaps to avoid leaks
1575 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R
->X
, &RP
.X
, b
) );
1576 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R
->Z
, &RP
.Z
, b
) );
1577 MBEDTLS_MPI_CHK( ecp_double_add_mxz( grp
, R
, &RP
, R
, &RP
, &PX
) );
1578 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R
->X
, &RP
.X
, b
) );
1579 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R
->Z
, &RP
.Z
, b
) );
1582 MBEDTLS_MPI_CHK( ecp_normalize_mxz( grp
, R
) );
1585 mbedtls_ecp_point_free( &RP
); mbedtls_mpi_free( &PX
);
1590 #endif /* ECP_MONTGOMERY */
1593 * Multiplication R = m * P
1595 int mbedtls_ecp_mul( mbedtls_ecp_group
*grp
, mbedtls_ecp_point
*R
,
1596 const mbedtls_mpi
*m
, const mbedtls_ecp_point
*P
,
1597 int (*f_rng
)(void *, unsigned char *, size_t), void *p_rng
)
1601 /* Common sanity checks */
1602 if( mbedtls_mpi_cmp_int( &P
->Z
, 1 ) != 0 )
1603 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA
);
1605 if( ( ret
= mbedtls_ecp_check_privkey( grp
, m
) ) != 0 ||
1606 ( ret
= mbedtls_ecp_check_pubkey( grp
, P
) ) != 0 )
1609 #if defined(ECP_MONTGOMERY)
1610 if( ecp_get_type( grp
) == ECP_TYPE_MONTGOMERY
)
1611 return( ecp_mul_mxz( grp
, R
, m
, P
, f_rng
, p_rng
) );
1613 #if defined(ECP_SHORTWEIERSTRASS)
1614 if( ecp_get_type( grp
) == ECP_TYPE_SHORT_WEIERSTRASS
)
1615 return( ecp_mul_comb( grp
, R
, m
, P
, f_rng
, p_rng
) );
1617 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA
);
1620 #if defined(ECP_SHORTWEIERSTRASS)
1622 * Check that an affine point is valid as a public key,
1623 * short weierstrass curves (SEC1 3.2.3.1)
1625 static int ecp_check_pubkey_sw( const mbedtls_ecp_group
*grp
, const mbedtls_ecp_point
*pt
)
1628 mbedtls_mpi YY
, RHS
;
1630 /* pt coordinates must be normalized for our checks */
1631 if( mbedtls_mpi_cmp_int( &pt
->X
, 0 ) < 0 ||
1632 mbedtls_mpi_cmp_int( &pt
->Y
, 0 ) < 0 ||
1633 mbedtls_mpi_cmp_mpi( &pt
->X
, &grp
->P
) >= 0 ||
1634 mbedtls_mpi_cmp_mpi( &pt
->Y
, &grp
->P
) >= 0 )
1635 return( MBEDTLS_ERR_ECP_INVALID_KEY
);
1637 mbedtls_mpi_init( &YY
); mbedtls_mpi_init( &RHS
);
1641 * RHS = X (X^2 + A) + B = X^3 + A X + B
1643 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &YY
, &pt
->Y
, &pt
->Y
) ); MOD_MUL( YY
);
1644 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &RHS
, &pt
->X
, &pt
->X
) ); MOD_MUL( RHS
);
1646 /* Special case for A = -3 */
1647 if( grp
->A
.p
== NULL
)
1649 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &RHS
, &RHS
, 3 ) ); MOD_SUB( RHS
);
1653 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &RHS
, &RHS
, &grp
->A
) ); MOD_ADD( RHS
);
1656 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &RHS
, &RHS
, &pt
->X
) ); MOD_MUL( RHS
);
1657 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &RHS
, &RHS
, &grp
->B
) ); MOD_ADD( RHS
);
1659 if( mbedtls_mpi_cmp_mpi( &YY
, &RHS
) != 0 )
1660 ret
= MBEDTLS_ERR_ECP_INVALID_KEY
;
1664 mbedtls_mpi_free( &YY
); mbedtls_mpi_free( &RHS
);
1668 #endif /* ECP_SHORTWEIERSTRASS */
1671 * Linear combination
1673 int mbedtls_ecp_muladd( mbedtls_ecp_group
*grp
, mbedtls_ecp_point
*R
,
1674 const mbedtls_mpi
*m
, const mbedtls_ecp_point
*P
,
1675 const mbedtls_mpi
*n
, const mbedtls_ecp_point
*Q
)
1678 mbedtls_ecp_point mP
;
1680 if( ecp_get_type( grp
) != ECP_TYPE_SHORT_WEIERSTRASS
)
1681 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE
);
1683 mbedtls_ecp_point_init( &mP
);
1685 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( grp
, &mP
, m
, P
, NULL
, NULL
) );
1686 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( grp
, R
, n
, Q
, NULL
, NULL
) );
1687 MBEDTLS_MPI_CHK( ecp_add_mixed( grp
, R
, &mP
, R
) );
1688 MBEDTLS_MPI_CHK( ecp_normalize_jac( grp
, R
) );
1691 mbedtls_ecp_point_free( &mP
);
1697 #if defined(ECP_MONTGOMERY)
1699 * Check validity of a public key for Montgomery curves with x-only schemes
1701 static int ecp_check_pubkey_mx( const mbedtls_ecp_group
*grp
, const mbedtls_ecp_point
*pt
)
1703 /* [Curve25519 p. 5] Just check X is the correct number of bytes */
1704 if( mbedtls_mpi_size( &pt
->X
) > ( grp
->nbits
+ 7 ) / 8 )
1705 return( MBEDTLS_ERR_ECP_INVALID_KEY
);
1709 #endif /* ECP_MONTGOMERY */
1712 * Check that a point is valid as a public key
1714 int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group
*grp
, const mbedtls_ecp_point
*pt
)
1716 /* Must use affine coordinates */
1717 if( mbedtls_mpi_cmp_int( &pt
->Z
, 1 ) != 0 )
1718 return( MBEDTLS_ERR_ECP_INVALID_KEY
);
1720 #if defined(ECP_MONTGOMERY)
1721 if( ecp_get_type( grp
) == ECP_TYPE_MONTGOMERY
)
1722 return( ecp_check_pubkey_mx( grp
, pt
) );
1724 #if defined(ECP_SHORTWEIERSTRASS)
1725 if( ecp_get_type( grp
) == ECP_TYPE_SHORT_WEIERSTRASS
)
1726 return( ecp_check_pubkey_sw( grp
, pt
) );
1728 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA
);
1732 * Check that an mbedtls_mpi is valid as a private key
1734 int mbedtls_ecp_check_privkey( const mbedtls_ecp_group
*grp
, const mbedtls_mpi
*d
)
1736 #if defined(ECP_MONTGOMERY)
1737 if( ecp_get_type( grp
) == ECP_TYPE_MONTGOMERY
)
1739 /* see [Curve25519] page 5 */
1740 if( mbedtls_mpi_get_bit( d
, 0 ) != 0 ||
1741 mbedtls_mpi_get_bit( d
, 1 ) != 0 ||
1742 mbedtls_mpi_get_bit( d
, 2 ) != 0 ||
1743 mbedtls_mpi_bitlen( d
) - 1 != grp
->nbits
) /* mbedtls_mpi_bitlen is one-based! */
1744 return( MBEDTLS_ERR_ECP_INVALID_KEY
);
1748 #endif /* ECP_MONTGOMERY */
1749 #if defined(ECP_SHORTWEIERSTRASS)
1750 if( ecp_get_type( grp
) == ECP_TYPE_SHORT_WEIERSTRASS
)
1753 if( mbedtls_mpi_cmp_int( d
, 1 ) < 0 ||
1754 mbedtls_mpi_cmp_mpi( d
, &grp
->N
) >= 0 )
1755 return( MBEDTLS_ERR_ECP_INVALID_KEY
);
1759 #endif /* ECP_SHORTWEIERSTRASS */
1761 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA
);
1765 * Generate a keypair
1767 int mbedtls_ecp_gen_keypair( mbedtls_ecp_group
*grp
, mbedtls_mpi
*d
, mbedtls_ecp_point
*Q
,
1768 int (*f_rng
)(void *, unsigned char *, size_t),
1772 size_t n_size
= ( grp
->nbits
+ 7 ) / 8;
1774 #if defined(ECP_MONTGOMERY)
1775 if( ecp_get_type( grp
) == ECP_TYPE_MONTGOMERY
)
1780 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( d
, n_size
, f_rng
, p_rng
) );
1782 /* Make sure the most significant bit is nbits */
1783 b
= mbedtls_mpi_bitlen( d
) - 1; /* mbedtls_mpi_bitlen is one-based */
1784 if( b
> grp
->nbits
)
1785 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( d
, b
- grp
->nbits
) );
1787 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d
, grp
->nbits
, 1 ) );
1789 /* Make sure the last three bits are unset */
1790 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d
, 0, 0 ) );
1791 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d
, 1, 0 ) );
1792 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d
, 2, 0 ) );
1795 #endif /* ECP_MONTGOMERY */
1796 #if defined(ECP_SHORTWEIERSTRASS)
1797 if( ecp_get_type( grp
) == ECP_TYPE_SHORT_WEIERSTRASS
)
1799 /* SEC1 3.2.1: Generate d such that 1 <= n < N */
1801 unsigned char rnd
[MBEDTLS_ECP_MAX_BYTES
];
1804 * Match the procedure given in RFC 6979 (deterministic ECDSA):
1805 * - use the same byte ordering;
1806 * - keep the leftmost nbits bits of the generated octet string;
1807 * - try until result is in the desired range.
1808 * This also avoids any biais, which is especially important for ECDSA.
1812 MBEDTLS_MPI_CHK( f_rng( p_rng
, rnd
, n_size
) );
1813 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( d
, rnd
, n_size
) );
1814 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( d
, 8 * n_size
- grp
->nbits
) );
1817 * Each try has at worst a probability 1/2 of failing (the msb has
1818 * a probability 1/2 of being 0, and then the result will be < N),
1819 * so after 30 tries failure probability is a most 2**(-30).
1821 * For most curves, 1 try is enough with overwhelming probability,
1822 * since N starts with a lot of 1s in binary, but some curves
1823 * such as secp224k1 are actually very close to the worst case.
1826 return( MBEDTLS_ERR_ECP_RANDOM_FAILED
);
1828 while( mbedtls_mpi_cmp_int( d
, 1 ) < 0 ||
1829 mbedtls_mpi_cmp_mpi( d
, &grp
->N
) >= 0 );
1832 #endif /* ECP_SHORTWEIERSTRASS */
1833 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA
);
1839 return( mbedtls_ecp_mul( grp
, Q
, d
, &grp
->G
, f_rng
, p_rng
) );
1843 * Generate a keypair, prettier wrapper
1845 int mbedtls_ecp_gen_key( mbedtls_ecp_group_id grp_id
, mbedtls_ecp_keypair
*key
,
1846 int (*f_rng
)(void *, unsigned char *, size_t), void *p_rng
)
1850 if( ( ret
= mbedtls_ecp_group_load( &key
->grp
, grp_id
) ) != 0 )
1853 return( mbedtls_ecp_gen_keypair( &key
->grp
, &key
->d
, &key
->Q
, f_rng
, p_rng
) );
1857 * Check a public-private key pair
1859 int mbedtls_ecp_check_pub_priv( const mbedtls_ecp_keypair
*pub
, const mbedtls_ecp_keypair
*prv
)
1862 mbedtls_ecp_point Q
;
1863 mbedtls_ecp_group grp
;
1865 if( pub
->grp
.id
== MBEDTLS_ECP_DP_NONE
||
1866 pub
->grp
.id
!= prv
->grp
.id
||
1867 mbedtls_mpi_cmp_mpi( &pub
->Q
.X
, &prv
->Q
.X
) ||
1868 mbedtls_mpi_cmp_mpi( &pub
->Q
.Y
, &prv
->Q
.Y
) ||
1869 mbedtls_mpi_cmp_mpi( &pub
->Q
.Z
, &prv
->Q
.Z
) )
1871 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA
);
1874 mbedtls_ecp_point_init( &Q
);
1875 mbedtls_ecp_group_init( &grp
);
1877 /* mbedtls_ecp_mul() needs a non-const group... */
1878 mbedtls_ecp_group_copy( &grp
, &prv
->grp
);
1880 /* Also checks d is valid */
1881 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp
, &Q
, &prv
->d
, &prv
->grp
.G
, NULL
, NULL
) );
1883 if( mbedtls_mpi_cmp_mpi( &Q
.X
, &prv
->Q
.X
) ||
1884 mbedtls_mpi_cmp_mpi( &Q
.Y
, &prv
->Q
.Y
) ||
1885 mbedtls_mpi_cmp_mpi( &Q
.Z
, &prv
->Q
.Z
) )
1887 ret
= MBEDTLS_ERR_ECP_BAD_INPUT_DATA
;
1892 mbedtls_ecp_point_free( &Q
);
1893 mbedtls_ecp_group_free( &grp
);
1898 #if defined(MBEDTLS_SELF_TEST)
1903 int mbedtls_ecp_self_test( int verbose
)
1907 mbedtls_ecp_group grp
;
1908 mbedtls_ecp_point R
, P
;
1910 unsigned long add_c_prev
, dbl_c_prev
, mul_c_prev
;
1911 /* exponents especially adapted for secp192r1 */
1912 const char *exponents
[] =
1914 "000000000000000000000000000000000000000000000001", /* one */
1915 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22830", /* N - 1 */
1916 "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
1917 "400000000000000000000000000000000000000000000000", /* one and zeros */
1918 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", /* all ones */
1919 "555555555555555555555555555555555555555555555555", /* 101010... */
1922 mbedtls_ecp_group_init( &grp
);
1923 mbedtls_ecp_point_init( &R
);
1924 mbedtls_ecp_point_init( &P
);
1925 mbedtls_mpi_init( &m
);
1927 /* Use secp192r1 if available, or any available curve */
1928 #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
1929 MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &grp
, MBEDTLS_ECP_DP_SECP192R1
) );
1931 MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &grp
, mbedtls_ecp_curve_list()->grp_id
) );
1935 mbedtls_printf( " ECP test #1 (constant op_count, base point G): " );
1937 /* Do a dummy multiplication first to trigger precomputation */
1938 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &m
, 2 ) );
1939 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp
, &P
, &m
, &grp
.G
, NULL
, NULL
) );
1944 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m
, 16, exponents
[0] ) );
1945 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp
, &R
, &m
, &grp
.G
, NULL
, NULL
) );
1947 for( i
= 1; i
< sizeof( exponents
) / sizeof( exponents
[0] ); i
++ )
1949 add_c_prev
= add_count
;
1950 dbl_c_prev
= dbl_count
;
1951 mul_c_prev
= mul_count
;
1956 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m
, 16, exponents
[i
] ) );
1957 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp
, &R
, &m
, &grp
.G
, NULL
, NULL
) );
1959 if( add_count
!= add_c_prev
||
1960 dbl_count
!= dbl_c_prev
||
1961 mul_count
!= mul_c_prev
)
1964 mbedtls_printf( "failed (%u)\n", (unsigned int) i
);
1972 mbedtls_printf( "passed\n" );
1975 mbedtls_printf( " ECP test #2 (constant op_count, other point): " );
1976 /* We computed P = 2G last time, use it */
1981 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m
, 16, exponents
[0] ) );
1982 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp
, &R
, &m
, &P
, NULL
, NULL
) );
1984 for( i
= 1; i
< sizeof( exponents
) / sizeof( exponents
[0] ); i
++ )
1986 add_c_prev
= add_count
;
1987 dbl_c_prev
= dbl_count
;
1988 mul_c_prev
= mul_count
;
1993 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m
, 16, exponents
[i
] ) );
1994 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp
, &R
, &m
, &P
, NULL
, NULL
) );
1996 if( add_count
!= add_c_prev
||
1997 dbl_count
!= dbl_c_prev
||
1998 mul_count
!= mul_c_prev
)
2001 mbedtls_printf( "failed (%u)\n", (unsigned int) i
);
2009 mbedtls_printf( "passed\n" );
2013 if( ret
< 0 && verbose
!= 0 )
2014 mbedtls_printf( "Unexpected error, return code = %08X\n", ret
);
2016 mbedtls_ecp_group_free( &grp
);
2017 mbedtls_ecp_point_free( &R
);
2018 mbedtls_ecp_point_free( &P
);
2019 mbedtls_mpi_free( &m
);
2022 mbedtls_printf( "\n" );
2027 #endif /* MBEDTLS_SELF_TEST */
2029 #endif /* MBEDTLS_ECP_C */