2012-10-31 09:26:55 +01:00
|
|
|
/*
|
2013-12-02 15:49:09 +01:00
|
|
|
* Elliptic curves over GF(p): generic functions
|
2012-10-31 09:26:55 +01:00
|
|
|
*
|
2015-07-27 11:11:48 +02:00
|
|
|
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
Update license headers to Apache-2.0 OR GPL-2.0-or-later
This will allow us to ship the LTS branches in a single archive
This commit was generated using the following script:
# ========================
#!/bin/sh
header1='\ * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later\
*\
* This file is provided under the Apache License 2.0, or the\
* GNU General Public License v2.0 or later.\
*\
* **********\
* Apache License 2.0:\
*\
* Licensed under the Apache License, Version 2.0 (the "License"); you may\
* not use this file except in compliance with the License.\
* You may obtain a copy of the License at\
*\
* http://www.apache.org/licenses/LICENSE-2.0\
*\
* Unless required by applicable law or agreed to in writing, software\
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT\
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\
* See the License for the specific language governing permissions and\
* limitations under the License.\
*\
* **********\
*\
* **********\
* GNU General Public License v2.0 or later:\
*\
* This program is free software; you can redistribute it and/or modify\
* it under the terms of the GNU General Public License as published by\
* the Free Software Foundation; either version 2 of the License, or\
* (at your option) any later version.\
*\
* This program is distributed in the hope that it will be useful,\
* but WITHOUT ANY WARRANTY; without even the implied warranty of\
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\
* GNU General Public License for more details.\
*\
* You should have received a copy of the GNU General Public License along\
* with this program; if not, write to the Free Software Foundation, Inc.,\
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\
*\
* **********'
find -path './.git' -prune -o '(' -name '*.c' -o -name '*.cpp' -o -name '*.fmt' -o -name '*.h' ')' -print | xargs sed -i "
# Normalize the first line of the copyright headers (no text on the first line of a block comment)
/^\/\*.*Copyright.*Arm/I s/\/\*/&\n */
# Insert new copyright header
/SPDX-License-Identifier/ i\
$header1
# Delete old copyright header
/SPDX-License-Identifier/,$ {
# Delete lines until the one preceding the mbedtls declaration
N
1,/This file is part of/ {
/This file is part of/! D
}
}
"
# Format copyright header for inclusion into scripts
header2=$(echo "$header1" | sed 's/^\\\? \* \?/#/')
find -path './.git' -prune -o '(' -name '*.gdb' -o -name '*.pl' -o -name '*.py' -o -name '*.sh' ')' -print | xargs sed -i "
# Insert new copyright header
/SPDX-License-Identifier/ i\
$header2
# Delete old copyright header
/SPDX-License-Identifier/,$ {
# Delete lines until the one preceding the mbedtls declaration
N
1,/This file is part of/ {
/This file is part of/! D
}
}
"
# ========================
Signed-off-by: Bence Szépkúti <bence.szepkuti@arm.com>
2020-06-05 13:02:18 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
|
|
|
*
|
|
|
|
* This file is provided under the Apache License 2.0, or the
|
|
|
|
* GNU General Public License v2.0 or later.
|
|
|
|
*
|
|
|
|
* **********
|
|
|
|
* Apache License 2.0:
|
2012-10-31 09:26:55 +01:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2012-10-31 09:26:55 +01:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-10-31 09:26:55 +01:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2012-10-31 09:26:55 +01:00
|
|
|
*
|
Update license headers to Apache-2.0 OR GPL-2.0-or-later
This will allow us to ship the LTS branches in a single archive
This commit was generated using the following script:
# ========================
#!/bin/sh
header1='\ * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later\
*\
* This file is provided under the Apache License 2.0, or the\
* GNU General Public License v2.0 or later.\
*\
* **********\
* Apache License 2.0:\
*\
* Licensed under the Apache License, Version 2.0 (the "License"); you may\
* not use this file except in compliance with the License.\
* You may obtain a copy of the License at\
*\
* http://www.apache.org/licenses/LICENSE-2.0\
*\
* Unless required by applicable law or agreed to in writing, software\
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT\
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\
* See the License for the specific language governing permissions and\
* limitations under the License.\
*\
* **********\
*\
* **********\
* GNU General Public License v2.0 or later:\
*\
* This program is free software; you can redistribute it and/or modify\
* it under the terms of the GNU General Public License as published by\
* the Free Software Foundation; either version 2 of the License, or\
* (at your option) any later version.\
*\
* This program is distributed in the hope that it will be useful,\
* but WITHOUT ANY WARRANTY; without even the implied warranty of\
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\
* GNU General Public License for more details.\
*\
* You should have received a copy of the GNU General Public License along\
* with this program; if not, write to the Free Software Foundation, Inc.,\
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\
*\
* **********'
find -path './.git' -prune -o '(' -name '*.c' -o -name '*.cpp' -o -name '*.fmt' -o -name '*.h' ')' -print | xargs sed -i "
# Normalize the first line of the copyright headers (no text on the first line of a block comment)
/^\/\*.*Copyright.*Arm/I s/\/\*/&\n */
# Insert new copyright header
/SPDX-License-Identifier/ i\
$header1
# Delete old copyright header
/SPDX-License-Identifier/,$ {
# Delete lines until the one preceding the mbedtls declaration
N
1,/This file is part of/ {
/This file is part of/! D
}
}
"
# Format copyright header for inclusion into scripts
header2=$(echo "$header1" | sed 's/^\\\? \* \?/#/')
find -path './.git' -prune -o '(' -name '*.gdb' -o -name '*.pl' -o -name '*.py' -o -name '*.sh' ')' -print | xargs sed -i "
# Insert new copyright header
/SPDX-License-Identifier/ i\
$header2
# Delete old copyright header
/SPDX-License-Identifier/,$ {
# Delete lines until the one preceding the mbedtls declaration
N
1,/This file is part of/ {
/This file is part of/! D
}
}
"
# ========================
Signed-off-by: Bence Szépkúti <bence.szepkuti@arm.com>
2020-06-05 13:02:18 +02:00
|
|
|
* **********
|
|
|
|
*
|
|
|
|
* **********
|
|
|
|
* GNU General Public License v2.0 or later:
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
* **********
|
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* This file is part of mbed TLS (https://tls.mbed.org)
|
2012-10-31 09:26:55 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* References:
|
|
|
|
*
|
2012-11-02 09:40:25 +01:00
|
|
|
* SEC1 http://www.secg.org/index.php?action=secg,docs_secg
|
2012-11-08 17:40:51 +01:00
|
|
|
* GECC = Guide to Elliptic Curve Cryptography - Hankerson, Menezes, Vanstone
|
2012-11-10 00:27:12 +01:00
|
|
|
* FIPS 186-3 http://csrc.nist.gov/publications/fips/fips186-3/fips_186-3.pdf
|
2013-02-09 17:03:58 +01:00
|
|
|
* RFC 4492 for the related TLS structures and constants
|
2013-09-02 16:26:04 +02:00
|
|
|
*
|
2015-06-23 00:18:41 +02:00
|
|
|
* [Curve25519] http://cr.yp.to/ecdh/curve25519-20060209.pdf
|
2013-12-04 18:14:55 +01:00
|
|
|
*
|
2015-04-03 13:48:06 +02:00
|
|
|
* [2] CORON, Jean-S'ebastien. Resistance against differential power analysis
|
2013-09-02 16:26:04 +02:00
|
|
|
* for elliptic curve cryptosystems. In : Cryptographic Hardware and
|
|
|
|
* Embedded Systems. Springer Berlin Heidelberg, 1999. p. 292-302.
|
|
|
|
* <http://link.springer.com/chapter/10.1007/3-540-48059-5_25>
|
2013-11-16 15:50:12 +01:00
|
|
|
*
|
2015-04-03 13:48:06 +02:00
|
|
|
* [3] HEDABOU, Mustapha, PINEL, Pierre, et B'EN'ETEAU, Lucien. A comb method to
|
2013-11-16 15:50:12 +01:00
|
|
|
* render ECC resistant against Side Channel Attacks. IACR Cryptology
|
|
|
|
* ePrint Archive, 2004, vol. 2004, p. 342.
|
|
|
|
* <http://eprint.iacr.org/2004/342.pdf>
|
2012-10-31 09:26:55 +01:00
|
|
|
*/
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if !defined(MBEDTLS_CONFIG_FILE)
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/config.h"
|
2014-04-29 12:39:06 +02:00
|
|
|
#else
|
2015-04-08 12:49:31 +02:00
|
|
|
#include MBEDTLS_CONFIG_FILE
|
2014-04-29 12:39:06 +02:00
|
|
|
#endif
|
2012-10-31 09:26:55 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_ECP_C)
|
2012-10-31 09:26:55 +01:00
|
|
|
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/ecp.h"
|
2016-11-03 15:25:37 +01:00
|
|
|
#include "mbedtls/threading.h"
|
2013-07-03 13:37:05 +02:00
|
|
|
|
2015-02-06 14:43:58 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
2016-08-18 13:38:46 +02:00
|
|
|
#if !defined(MBEDTLS_ECP_ALT)
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_PLATFORM_C)
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/platform.h"
|
2013-07-03 13:37:05 +02:00
|
|
|
#else
|
2015-02-06 14:43:58 +01:00
|
|
|
#include <stdlib.h>
|
2015-02-17 16:46:45 +01:00
|
|
|
#include <stdio.h>
|
2015-04-08 12:49:31 +02:00
|
|
|
#define mbedtls_printf printf
|
2015-05-26 16:04:06 +02:00
|
|
|
#define mbedtls_calloc calloc
|
2015-04-08 12:49:31 +02:00
|
|
|
#define mbedtls_free free
|
2013-07-03 13:37:05 +02:00
|
|
|
#endif
|
|
|
|
|
2016-11-01 14:22:05 +01:00
|
|
|
#include "mbedtls/ecp_internal.h"
|
2016-08-18 13:38:46 +02:00
|
|
|
|
2020-05-22 12:12:36 +02:00
|
|
|
#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
|
2020-06-04 12:32:14 +02:00
|
|
|
#if defined(MBEDTLS_HMAC_DRBG_C)
|
2020-05-22 12:12:36 +02:00
|
|
|
#include "mbedtls/hmac_drbg.h"
|
2020-06-04 12:32:14 +02:00
|
|
|
#elif defined(MBEDTLS_CTR_DRBG_C)
|
|
|
|
#include "mbedtls/ctr_drbg.h"
|
2020-06-16 12:51:42 +02:00
|
|
|
#elif defined(MBEDTLS_SHA512_C)
|
|
|
|
#include "mbedtls/sha512.h"
|
|
|
|
#elif defined(MBEDTLS_SHA256_C)
|
|
|
|
#include "mbedtls/sha256.h"
|
2020-05-22 12:12:36 +02:00
|
|
|
#else
|
|
|
|
#error "Invalid configuration detected. Include check_config.h to ensure that the configuration is valid."
|
|
|
|
#endif
|
|
|
|
#endif /* MBEDTLS_ECP_NO_INTERNAL_RNG */
|
|
|
|
|
2015-10-05 12:40:01 +02:00
|
|
|
#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
|
|
|
|
!defined(inline) && !defined(__cplusplus)
|
2013-10-28 18:53:08 +01:00
|
|
|
#define inline __inline
|
2015-07-07 18:33:39 +02:00
|
|
|
#endif
|
2013-10-28 18:53:08 +01:00
|
|
|
|
2014-06-13 17:20:13 +02:00
|
|
|
/* Implementation that should never be optimized out by the compiler */
|
2015-04-08 12:49:31 +02:00
|
|
|
static void mbedtls_zeroize( void *v, size_t n ) {
|
2014-06-13 17:20:13 +02:00
|
|
|
volatile unsigned char *p = v; while( n-- ) *p++ = 0;
|
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SELF_TEST)
|
2012-11-13 20:57:00 +01:00
|
|
|
/*
|
2013-11-21 20:23:55 +01:00
|
|
|
* Counts of point addition and doubling, and field multiplications.
|
2013-09-02 16:26:04 +02:00
|
|
|
* Used to test resistance of point multiplication to simple timing attacks.
|
2012-11-13 20:57:00 +01:00
|
|
|
*/
|
2013-12-01 16:51:27 +01:00
|
|
|
static unsigned long add_count, dbl_count, mul_count;
|
2012-11-13 20:57:00 +01:00
|
|
|
#endif
|
|
|
|
|
2020-05-22 12:12:36 +02:00
|
|
|
#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
|
|
|
|
/*
|
|
|
|
* Currently ecp_mul() takes a RNG function as an argument, used for
|
2020-06-10 09:18:25 +02:00
|
|
|
* side-channel protection, but it can be NULL. The initial reasoning was
|
2020-05-22 12:12:36 +02:00
|
|
|
* that people will pass non-NULL RNG when they care about side-channels, but
|
|
|
|
* unfortunately we have some APIs that call ecp_mul() with a NULL RNG, with
|
|
|
|
* no opportunity for the user to do anything about it.
|
|
|
|
*
|
|
|
|
* The obvious strategies for addressing that include:
|
|
|
|
* - change those APIs so that they take RNG arguments;
|
|
|
|
* - require a global RNG to be available to all crypto modules.
|
|
|
|
*
|
|
|
|
* Unfortunately those would break compatibility. So what we do instead is
|
|
|
|
* have our own internal DRBG instance, seeded from the secret scalar.
|
|
|
|
*
|
|
|
|
* The following is a light-weight abstraction layer for doing that with
|
2020-06-04 12:32:14 +02:00
|
|
|
* HMAC_DRBG (first choice) or CTR_DRBG.
|
2020-05-22 12:12:36 +02:00
|
|
|
*/
|
|
|
|
|
2020-06-04 12:32:14 +02:00
|
|
|
#if defined(MBEDTLS_HMAC_DRBG_C)
|
|
|
|
|
2020-05-22 12:12:36 +02:00
|
|
|
/* DRBG context type */
|
2020-06-04 12:32:14 +02:00
|
|
|
typedef mbedtls_hmac_drbg_context ecp_drbg_context;
|
2020-05-22 12:12:36 +02:00
|
|
|
|
|
|
|
/* DRBG context init */
|
|
|
|
static inline void ecp_drbg_init( ecp_drbg_context *ctx )
|
|
|
|
{
|
2020-06-04 12:32:14 +02:00
|
|
|
mbedtls_hmac_drbg_init( ctx );
|
2020-05-22 12:12:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* DRBG context free */
|
|
|
|
static inline void ecp_drbg_free( ecp_drbg_context *ctx )
|
|
|
|
{
|
2020-06-04 12:32:14 +02:00
|
|
|
mbedtls_hmac_drbg_free( ctx );
|
2020-05-22 12:12:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* DRBG function */
|
|
|
|
static inline int ecp_drbg_random( void *p_rng,
|
|
|
|
unsigned char *output, size_t output_len )
|
|
|
|
{
|
2020-06-04 12:32:14 +02:00
|
|
|
return( mbedtls_hmac_drbg_random( p_rng, output, output_len ) );
|
2020-05-22 12:12:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* DRBG context seeding */
|
2020-06-16 12:51:42 +02:00
|
|
|
static int ecp_drbg_seed( ecp_drbg_context *ctx,
|
|
|
|
const mbedtls_mpi *secret, size_t secret_len )
|
2020-05-22 12:12:36 +02:00
|
|
|
{
|
2020-06-17 10:12:43 +02:00
|
|
|
int ret;
|
|
|
|
unsigned char secret_bytes[MBEDTLS_ECP_MAX_BYTES];
|
2020-06-04 12:32:14 +02:00
|
|
|
/* The list starts with strong hashes */
|
|
|
|
const mbedtls_md_type_t md_type = mbedtls_md_list()[0];
|
|
|
|
const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_type );
|
|
|
|
|
2020-06-17 10:12:43 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( secret,
|
|
|
|
secret_bytes, secret_len ) );
|
|
|
|
|
|
|
|
ret = mbedtls_hmac_drbg_seed_buf( ctx, md_info, secret_bytes, secret_len );
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
mbedtls_zeroize( secret_bytes, secret_len );
|
|
|
|
|
|
|
|
return( ret );
|
2020-05-22 12:12:36 +02:00
|
|
|
}
|
|
|
|
|
2020-06-04 12:32:14 +02:00
|
|
|
#elif defined(MBEDTLS_CTR_DRBG_C)
|
|
|
|
|
2020-05-22 12:12:36 +02:00
|
|
|
/* DRBG context type */
|
2020-06-04 12:32:14 +02:00
|
|
|
typedef mbedtls_ctr_drbg_context ecp_drbg_context;
|
2020-05-22 12:12:36 +02:00
|
|
|
|
|
|
|
/* DRBG context init */
|
|
|
|
static inline void ecp_drbg_init( ecp_drbg_context *ctx )
|
|
|
|
{
|
2020-06-04 12:32:14 +02:00
|
|
|
mbedtls_ctr_drbg_init( ctx );
|
2020-05-22 12:12:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* DRBG context free */
|
|
|
|
static inline void ecp_drbg_free( ecp_drbg_context *ctx )
|
|
|
|
{
|
2020-06-04 12:32:14 +02:00
|
|
|
mbedtls_ctr_drbg_free( ctx );
|
2020-05-22 12:12:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* DRBG function */
|
|
|
|
static inline int ecp_drbg_random( void *p_rng,
|
|
|
|
unsigned char *output, size_t output_len )
|
|
|
|
{
|
2020-06-04 12:32:14 +02:00
|
|
|
return( mbedtls_ctr_drbg_random( p_rng, output, output_len ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Since CTR_DRBG doesn't have a seed_buf() function the way HMAC_DRBG does,
|
|
|
|
* we need to pass an entropy function when seeding. So we use a dummy
|
|
|
|
* function for that, and pass the actual entropy as customisation string.
|
|
|
|
* (During seeding of CTR_DRBG the entropy input and customisation string are
|
|
|
|
* concatenated before being used to update the secret state.)
|
|
|
|
*/
|
|
|
|
static int ecp_ctr_drbg_null_entropy(void *ctx, unsigned char *out, size_t len)
|
|
|
|
{
|
|
|
|
(void) ctx;
|
|
|
|
memset( out, 0, len );
|
|
|
|
return( 0 );
|
2020-05-22 12:12:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* DRBG context seeding */
|
2020-06-16 12:51:42 +02:00
|
|
|
static int ecp_drbg_seed( ecp_drbg_context *ctx,
|
|
|
|
const mbedtls_mpi *secret, size_t secret_len )
|
2020-05-22 12:12:36 +02:00
|
|
|
{
|
2020-06-17 10:12:43 +02:00
|
|
|
int ret;
|
|
|
|
unsigned char secret_bytes[MBEDTLS_ECP_MAX_BYTES];
|
|
|
|
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( secret,
|
|
|
|
secret_bytes, secret_len ) );
|
|
|
|
|
|
|
|
ret = mbedtls_ctr_drbg_seed( ctx, ecp_ctr_drbg_null_entropy, NULL,
|
|
|
|
secret_bytes, secret_len );
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
mbedtls_zeroize( secret_bytes, secret_len );
|
2020-05-22 12:12:36 +02:00
|
|
|
|
2020-06-17 10:12:43 +02:00
|
|
|
return( ret );
|
2020-05-22 12:12:36 +02:00
|
|
|
}
|
|
|
|
|
2020-06-18 12:14:34 +02:00
|
|
|
#elif defined(MBEDTLS_SHA512_C) || defined(MBEDTLS_SHA256_C)
|
2020-06-16 12:51:42 +02:00
|
|
|
|
|
|
|
/* This will be used in the self-test function */
|
|
|
|
#define ECP_ONE_STEP_KDF
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We need to expand secret data (the scalar) into a longer stream of bytes.
|
|
|
|
*
|
|
|
|
* We'll use the One-Step KDF from NIST SP 800-56C, with option 1 (H is a hash
|
|
|
|
* function) and empty FixedInfo. (Though we'll make it fit the DRBG API for
|
|
|
|
* convenience, this is not a full-fledged DRBG, but we don't need one here.)
|
|
|
|
*
|
2020-06-18 12:14:34 +02:00
|
|
|
* We need a basic hash abstraction layer to use whatever SHA-2 is available.
|
2020-06-16 12:51:42 +02:00
|
|
|
*/
|
|
|
|
#if defined(MBEDTLS_SHA512_C)
|
|
|
|
|
|
|
|
#define HASH_FUNC( in, ilen, out ) mbedtls_sha512_ret( in, ilen, out, 0 );
|
|
|
|
#define HASH_BLOCK_BYTES ( 512 / 8 )
|
|
|
|
|
|
|
|
#elif defined(MBEDTLS_SHA256_C)
|
|
|
|
|
|
|
|
#define HASH_FUNC( in, ilen, out ) mbedtls_sha256_ret( in, ilen, out, 0 );
|
|
|
|
#define HASH_BLOCK_BYTES ( 256 / 8 )
|
|
|
|
|
2020-06-18 12:14:34 +02:00
|
|
|
#endif /* SHA512/SHA256 abstraction */
|
2020-06-16 12:51:42 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* State consists of a 32-bit counter plus the secret value.
|
|
|
|
*
|
|
|
|
* We stored them concatenated in a single buffer as that's what will get
|
|
|
|
* passed to the hash function.
|
|
|
|
*/
|
|
|
|
typedef struct {
|
|
|
|
size_t total_len;
|
|
|
|
uint8_t buf[4 + MBEDTLS_ECP_MAX_BYTES];
|
|
|
|
} ecp_drbg_context;
|
|
|
|
|
|
|
|
static void ecp_drbg_init( ecp_drbg_context *ctx )
|
|
|
|
{
|
|
|
|
memset( ctx, 0, sizeof( ecp_drbg_context ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ecp_drbg_free( ecp_drbg_context *ctx )
|
|
|
|
{
|
|
|
|
mbedtls_zeroize( ctx, sizeof( ecp_drbg_context ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ecp_drbg_seed( ecp_drbg_context *ctx,
|
|
|
|
const mbedtls_mpi *secret, size_t secret_len )
|
|
|
|
{
|
|
|
|
ctx->total_len = 4 + secret_len;
|
|
|
|
memset( ctx->buf, 0, 4);
|
|
|
|
return( mbedtls_mpi_write_binary( secret, ctx->buf + 4, secret_len ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ecp_drbg_random( void *p_rng, unsigned char *output, size_t output_len )
|
|
|
|
{
|
|
|
|
ecp_drbg_context *ctx = p_rng;
|
|
|
|
int ret;
|
|
|
|
size_t len_done = 0;
|
2020-06-17 12:40:57 +02:00
|
|
|
uint8_t tmp[HASH_BLOCK_BYTES];
|
2020-06-16 12:51:42 +02:00
|
|
|
|
|
|
|
while( len_done < output_len )
|
|
|
|
{
|
|
|
|
uint8_t use_len;
|
|
|
|
|
2020-06-17 12:57:33 +02:00
|
|
|
/* This function is only called for coordinate randomisation, which
|
|
|
|
* happens only twice in a scalar multiplication. Each time needs a
|
|
|
|
* random value in the range [2, p-1], and gets it by drawing len(p)
|
|
|
|
* bytes from this function, and retrying up to 10 times if unlucky.
|
|
|
|
*
|
|
|
|
* So for the largest curve, each scalar multiplication draws at most
|
2020-06-18 12:14:34 +02:00
|
|
|
* 20 * 66 bytes. The minimum block size is 32 (SHA-256), so with
|
|
|
|
* rounding that means a most 20 * 3 blocks.
|
2020-06-17 12:57:33 +02:00
|
|
|
*
|
|
|
|
* Since we don't need to draw more that 255 blocks, don't bother
|
|
|
|
* with carry propagation and just return an error instead. We can
|
|
|
|
* change that it we even need to draw more blinding values.
|
|
|
|
*/
|
2020-06-16 12:51:42 +02:00
|
|
|
ctx->buf[3] += 1;
|
|
|
|
if( ctx->buf[3] == 0 )
|
|
|
|
return( MBEDTLS_ERR_ECP_RANDOM_FAILED );
|
|
|
|
|
|
|
|
ret = HASH_FUNC( ctx->buf, ctx->total_len, tmp );
|
|
|
|
if( ret != 0 )
|
|
|
|
return( ret );
|
|
|
|
|
|
|
|
if( output_len - len_done > HASH_BLOCK_BYTES )
|
|
|
|
use_len = HASH_BLOCK_BYTES;
|
|
|
|
else
|
|
|
|
use_len = output_len - len_done;
|
|
|
|
|
|
|
|
memcpy( output + len_done, tmp, use_len );
|
|
|
|
len_done += use_len;
|
|
|
|
}
|
|
|
|
|
2020-06-17 12:40:57 +02:00
|
|
|
mbedtls_zeroize( tmp, sizeof( tmp ) );
|
|
|
|
|
2020-06-16 12:51:42 +02:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* DRBG/SHA modules */
|
2020-05-22 12:12:36 +02:00
|
|
|
#error "Invalid configuration detected. Include check_config.h to ensure that the configuration is valid."
|
2020-06-16 12:51:42 +02:00
|
|
|
#endif /* DRBG/SHA modules */
|
2020-05-22 12:12:36 +02:00
|
|
|
#endif /* MBEDTLS_ECP_NO_INTERNAL_RNG */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) || \
|
|
|
|
defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || \
|
|
|
|
defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || \
|
|
|
|
defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) || \
|
|
|
|
defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) || \
|
|
|
|
defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) || \
|
|
|
|
defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) || \
|
|
|
|
defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) || \
|
|
|
|
defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || \
|
|
|
|
defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || \
|
|
|
|
defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
|
2015-04-09 12:14:31 +02:00
|
|
|
#define ECP_SHORTWEIERSTRASS
|
2013-12-04 23:15:46 +01:00
|
|
|
#endif
|
|
|
|
|
2015-06-23 00:18:41 +02:00
|
|
|
#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
|
2015-04-09 12:14:31 +02:00
|
|
|
#define ECP_MONTGOMERY
|
2013-12-04 23:15:46 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Curve types: internal for now, might be exposed later
|
|
|
|
*/
|
|
|
|
typedef enum
|
|
|
|
{
|
2015-04-09 12:14:31 +02:00
|
|
|
ECP_TYPE_NONE = 0,
|
|
|
|
ECP_TYPE_SHORT_WEIERSTRASS, /* y^2 = x^3 + a x + b */
|
|
|
|
ECP_TYPE_MONTGOMERY, /* y^2 = x^3 + a x^2 + x */
|
2013-12-04 23:15:46 +01:00
|
|
|
} ecp_curve_type;
|
|
|
|
|
2013-09-16 17:30:04 +02:00
|
|
|
/*
|
|
|
|
* List of supported curves:
|
|
|
|
* - internal ID
|
2013-10-07 19:40:41 +02:00
|
|
|
* - TLS NamedCurve ID (RFC 4492 sec. 5.1.1, RFC 7071 sec. 2)
|
2013-09-16 17:30:04 +02:00
|
|
|
* - size in bits
|
2013-10-07 19:40:41 +02:00
|
|
|
* - readable name
|
2014-01-22 11:22:20 +01:00
|
|
|
*
|
2014-02-04 14:48:50 +01:00
|
|
|
* Curves are listed in order: largest curves first, and for a given size,
|
|
|
|
* fastest curves first. This provides the default order for the SSL module.
|
2015-06-15 14:34:59 +02:00
|
|
|
*
|
|
|
|
* Reminder: update profiles in x509_crt.c when adding a new curves!
|
2013-09-16 17:30:04 +02:00
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
static const mbedtls_ecp_curve_info ecp_supported_curves[] =
|
2013-09-16 17:30:04 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
|
|
|
|
{ MBEDTLS_ECP_DP_SECP521R1, 25, 521, "secp521r1" },
|
2013-09-16 17:30:04 +02:00
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
|
|
|
|
{ MBEDTLS_ECP_DP_BP512R1, 28, 512, "brainpoolP512r1" },
|
2014-01-22 11:22:20 +01:00
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
|
|
|
|
{ MBEDTLS_ECP_DP_SECP384R1, 24, 384, "secp384r1" },
|
2013-09-16 17:30:04 +02:00
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
|
|
|
|
{ MBEDTLS_ECP_DP_BP384R1, 27, 384, "brainpoolP384r1" },
|
2014-01-22 11:22:20 +01:00
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
|
|
|
|
{ MBEDTLS_ECP_DP_SECP256R1, 23, 256, "secp256r1" },
|
2013-09-16 17:30:04 +02:00
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
|
|
|
|
{ MBEDTLS_ECP_DP_SECP256K1, 22, 256, "secp256k1" },
|
2014-02-04 14:48:50 +01:00
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
|
|
|
|
{ MBEDTLS_ECP_DP_BP256R1, 26, 256, "brainpoolP256r1" },
|
2014-01-22 11:22:20 +01:00
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
|
|
|
|
{ MBEDTLS_ECP_DP_SECP224R1, 21, 224, "secp224r1" },
|
2013-09-16 17:30:04 +02:00
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
|
|
|
|
{ MBEDTLS_ECP_DP_SECP224K1, 20, 224, "secp224k1" },
|
2014-01-10 18:26:48 +01:00
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
|
|
|
|
{ MBEDTLS_ECP_DP_SECP192R1, 19, 192, "secp192r1" },
|
2014-02-04 14:48:50 +01:00
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
|
|
|
|
{ MBEDTLS_ECP_DP_SECP192K1, 18, 192, "secp192k1" },
|
2013-09-16 17:30:04 +02:00
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
{ MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
|
2013-09-16 17:30:04 +02:00
|
|
|
};
|
2014-02-04 14:48:50 +01:00
|
|
|
|
2014-07-08 13:31:34 +02:00
|
|
|
#define ECP_NB_CURVES sizeof( ecp_supported_curves ) / \
|
|
|
|
sizeof( ecp_supported_curves[0] )
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
static mbedtls_ecp_group_id ecp_supported_grp_id[ECP_NB_CURVES];
|
2013-09-16 17:30:04 +02:00
|
|
|
|
2013-09-18 15:31:24 +02:00
|
|
|
/*
|
|
|
|
* List of supported curves and associated info
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list( void )
|
2013-09-18 15:31:24 +02:00
|
|
|
{
|
2014-06-17 14:06:49 +02:00
|
|
|
return( ecp_supported_curves );
|
2013-09-18 15:31:24 +02:00
|
|
|
}
|
|
|
|
|
2013-10-23 20:19:57 +02:00
|
|
|
/*
|
2014-02-04 14:48:50 +01:00
|
|
|
* List of supported curves, group ID only
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list( void )
|
2014-02-04 14:48:50 +01:00
|
|
|
{
|
|
|
|
static int init_done = 0;
|
|
|
|
|
|
|
|
if( ! init_done )
|
|
|
|
{
|
|
|
|
size_t i = 0;
|
2015-04-08 12:49:31 +02:00
|
|
|
const mbedtls_ecp_curve_info *curve_info;
|
2014-02-04 14:48:50 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
for( curve_info = mbedtls_ecp_curve_list();
|
|
|
|
curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
|
2014-02-04 14:48:50 +01:00
|
|
|
curve_info++ )
|
|
|
|
{
|
|
|
|
ecp_supported_grp_id[i++] = curve_info->grp_id;
|
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
ecp_supported_grp_id[i] = MBEDTLS_ECP_DP_NONE;
|
2014-02-04 14:48:50 +01:00
|
|
|
|
|
|
|
init_done = 1;
|
|
|
|
}
|
|
|
|
|
2014-06-17 14:06:49 +02:00
|
|
|
return( ecp_supported_grp_id );
|
2014-02-04 14:48:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the curve info for the internal identifier
|
2013-10-23 20:19:57 +02:00
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_grp_id( mbedtls_ecp_group_id grp_id )
|
2013-10-23 20:19:57 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
const mbedtls_ecp_curve_info *curve_info;
|
2013-10-23 20:19:57 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
for( curve_info = mbedtls_ecp_curve_list();
|
|
|
|
curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
|
2013-10-23 20:19:57 +02:00
|
|
|
curve_info++ )
|
|
|
|
{
|
|
|
|
if( curve_info->grp_id == grp_id )
|
|
|
|
return( curve_info );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the curve info from the TLS identifier
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id( uint16_t tls_id )
|
2013-10-23 20:19:57 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
const mbedtls_ecp_curve_info *curve_info;
|
2013-10-23 20:19:57 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
for( curve_info = mbedtls_ecp_curve_list();
|
|
|
|
curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
|
2013-10-23 20:19:57 +02:00
|
|
|
curve_info++ )
|
|
|
|
{
|
|
|
|
if( curve_info->tls_id == tls_id )
|
|
|
|
return( curve_info );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
|
2013-11-30 15:10:14 +01:00
|
|
|
/*
|
|
|
|
* Get the curve info from the name
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name( const char *name )
|
2013-11-30 15:10:14 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
const mbedtls_ecp_curve_info *curve_info;
|
2013-11-30 15:10:14 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
for( curve_info = mbedtls_ecp_curve_list();
|
|
|
|
curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
|
2013-11-30 15:10:14 +01:00
|
|
|
curve_info++ )
|
|
|
|
{
|
2015-05-28 17:06:07 +02:00
|
|
|
if( strcmp( curve_info->name, name ) == 0 )
|
2013-11-30 15:10:14 +01:00
|
|
|
return( curve_info );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
|
2013-12-04 11:08:01 +01:00
|
|
|
/*
|
2013-12-04 23:15:46 +01:00
|
|
|
* Get the type of a curve
|
2013-12-04 11:08:01 +01:00
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
static inline ecp_curve_type ecp_get_type( const mbedtls_ecp_group *grp )
|
2013-12-04 11:08:01 +01:00
|
|
|
{
|
2013-12-04 23:15:46 +01:00
|
|
|
if( grp->G.X.p == NULL )
|
2015-04-09 12:14:31 +02:00
|
|
|
return( ECP_TYPE_NONE );
|
2013-12-04 23:15:46 +01:00
|
|
|
|
|
|
|
if( grp->G.Y.p == NULL )
|
2015-04-09 12:14:31 +02:00
|
|
|
return( ECP_TYPE_MONTGOMERY );
|
2013-12-04 23:15:46 +01:00
|
|
|
else
|
2015-04-09 12:14:31 +02:00
|
|
|
return( ECP_TYPE_SHORT_WEIERSTRASS );
|
2013-12-04 11:08:01 +01:00
|
|
|
}
|
|
|
|
|
2012-11-02 18:14:40 +01:00
|
|
|
/*
|
2012-11-05 17:27:54 +01:00
|
|
|
* Initialize (the components of) a point
|
2012-11-02 18:14:40 +01:00
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_ecp_point_init( mbedtls_ecp_point *pt )
|
2012-11-02 18:14:40 +01:00
|
|
|
{
|
|
|
|
if( pt == NULL )
|
|
|
|
return;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_init( &pt->X );
|
|
|
|
mbedtls_mpi_init( &pt->Y );
|
|
|
|
mbedtls_mpi_init( &pt->Z );
|
2012-11-05 17:27:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize (the components of) a group
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_ecp_group_init( mbedtls_ecp_group *grp )
|
2012-11-05 17:27:54 +01:00
|
|
|
{
|
|
|
|
if( grp == NULL )
|
|
|
|
return;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
memset( grp, 0, sizeof( mbedtls_ecp_group ) );
|
2012-11-02 18:14:40 +01:00
|
|
|
}
|
|
|
|
|
2013-07-01 13:40:52 +02:00
|
|
|
/*
|
|
|
|
* Initialize (the components of) a key pair
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_ecp_keypair_init( mbedtls_ecp_keypair *key )
|
2013-07-01 13:40:52 +02:00
|
|
|
{
|
2014-06-17 16:39:18 +02:00
|
|
|
if( key == NULL )
|
2013-07-01 13:40:52 +02:00
|
|
|
return;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group_init( &key->grp );
|
|
|
|
mbedtls_mpi_init( &key->d );
|
|
|
|
mbedtls_ecp_point_init( &key->Q );
|
2013-07-01 13:40:52 +02:00
|
|
|
}
|
|
|
|
|
2012-10-31 19:24:21 +01:00
|
|
|
/*
|
|
|
|
* Unallocate (the components of) a point
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_ecp_point_free( mbedtls_ecp_point *pt )
|
2012-10-31 19:24:21 +01:00
|
|
|
{
|
|
|
|
if( pt == NULL )
|
|
|
|
return;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_free( &( pt->X ) );
|
|
|
|
mbedtls_mpi_free( &( pt->Y ) );
|
|
|
|
mbedtls_mpi_free( &( pt->Z ) );
|
2012-10-31 19:24:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unallocate (the components of) a group
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_ecp_group_free( mbedtls_ecp_group *grp )
|
2012-10-31 19:24:21 +01:00
|
|
|
{
|
2013-09-17 19:13:10 +02:00
|
|
|
size_t i;
|
|
|
|
|
2012-10-31 19:24:21 +01:00
|
|
|
if( grp == NULL )
|
|
|
|
return;
|
|
|
|
|
2013-12-06 12:51:50 +01:00
|
|
|
if( grp->h != 1 )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_free( &grp->P );
|
|
|
|
mbedtls_mpi_free( &grp->A );
|
|
|
|
mbedtls_mpi_free( &grp->B );
|
|
|
|
mbedtls_ecp_point_free( &grp->G );
|
|
|
|
mbedtls_mpi_free( &grp->N );
|
2013-12-06 12:51:50 +01:00
|
|
|
}
|
2013-09-16 18:56:28 +02:00
|
|
|
|
2013-09-17 19:13:10 +02:00
|
|
|
if( grp->T != NULL )
|
|
|
|
{
|
|
|
|
for( i = 0; i < grp->T_size; i++ )
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_point_free( &grp->T[i] );
|
|
|
|
mbedtls_free( grp->T );
|
2013-09-17 19:13:10 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_zeroize( grp, sizeof( mbedtls_ecp_group ) );
|
2012-10-31 19:24:21 +01:00
|
|
|
}
|
2012-10-31 09:26:55 +01:00
|
|
|
|
2013-07-01 13:40:52 +02:00
|
|
|
/*
|
|
|
|
* Unallocate (the components of) a key pair
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_ecp_keypair_free( mbedtls_ecp_keypair *key )
|
2013-07-01 13:40:52 +02:00
|
|
|
{
|
2014-06-17 16:39:18 +02:00
|
|
|
if( key == NULL )
|
2013-07-01 13:40:52 +02:00
|
|
|
return;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group_free( &key->grp );
|
|
|
|
mbedtls_mpi_free( &key->d );
|
|
|
|
mbedtls_ecp_point_free( &key->Q );
|
2013-07-01 13:40:52 +02:00
|
|
|
}
|
|
|
|
|
2012-11-02 18:14:40 +01:00
|
|
|
/*
|
2013-10-23 20:19:57 +02:00
|
|
|
* Copy the contents of a point
|
2012-11-02 09:40:25 +01:00
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_ecp_copy( mbedtls_ecp_point *P, const mbedtls_ecp_point *Q )
|
2012-11-02 09:40:25 +01:00
|
|
|
{
|
2012-11-10 14:44:24 +01:00
|
|
|
int ret;
|
2012-11-02 09:40:25 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &P->X, &Q->X ) );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &P->Y, &Q->Y ) );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &P->Z, &Q->Z ) );
|
2012-11-02 09:40:25 +01:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
return( ret );
|
|
|
|
}
|
2012-10-31 19:37:54 +01:00
|
|
|
|
2013-08-12 15:44:31 +02:00
|
|
|
/*
|
|
|
|
* Copy the contents of a group object
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_ecp_group_copy( mbedtls_ecp_group *dst, const mbedtls_ecp_group *src )
|
2013-08-12 15:44:31 +02:00
|
|
|
{
|
2015-05-11 18:46:47 +02:00
|
|
|
return mbedtls_ecp_group_load( dst, src->id );
|
2013-08-12 15:44:31 +02:00
|
|
|
}
|
|
|
|
|
2012-11-05 13:13:44 +01:00
|
|
|
/*
|
2013-10-23 20:19:57 +02:00
|
|
|
* Set point to zero
|
2012-11-05 13:13:44 +01:00
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_ecp_set_zero( mbedtls_ecp_point *pt )
|
2012-11-05 13:13:44 +01:00
|
|
|
{
|
2012-11-10 14:44:24 +01:00
|
|
|
int ret;
|
2012-11-05 13:13:44 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->X , 1 ) );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Y , 1 ) );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Z , 0 ) );
|
2012-11-05 13:13:44 +01:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-10-23 20:19:57 +02:00
|
|
|
* Tell if a point is zero
|
2012-11-05 13:13:44 +01:00
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_ecp_is_zero( mbedtls_ecp_point *pt )
|
2012-11-05 13:13:44 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
return( mbedtls_mpi_cmp_int( &pt->Z, 0 ) == 0 );
|
2013-10-08 12:04:56 +02:00
|
|
|
}
|
|
|
|
|
2015-08-11 15:44:41 +02:00
|
|
|
/*
|
2018-10-23 01:40:49 +02:00
|
|
|
* Compare two points lazily
|
2015-08-11 15:44:41 +02:00
|
|
|
*/
|
|
|
|
int mbedtls_ecp_point_cmp( const mbedtls_ecp_point *P,
|
|
|
|
const mbedtls_ecp_point *Q )
|
|
|
|
{
|
|
|
|
if( mbedtls_mpi_cmp_mpi( &P->X, &Q->X ) == 0 &&
|
|
|
|
mbedtls_mpi_cmp_mpi( &P->Y, &Q->Y ) == 0 &&
|
|
|
|
mbedtls_mpi_cmp_mpi( &P->Z, &Q->Z ) == 0 )
|
|
|
|
{
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
|
|
|
|
}
|
|
|
|
|
2013-10-08 12:04:56 +02:00
|
|
|
/*
|
2013-10-23 20:19:57 +02:00
|
|
|
* Import a non-zero point from ASCII strings
|
2013-10-08 12:04:56 +02:00
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_ecp_point_read_string( mbedtls_ecp_point *P, int radix,
|
2013-10-23 20:19:57 +02:00
|
|
|
const char *x, const char *y )
|
2013-10-08 12:04:56 +02:00
|
|
|
{
|
2013-10-10 15:55:39 +02:00
|
|
|
int ret;
|
2013-10-08 12:04:56 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &P->X, radix, x ) );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &P->Y, radix, y ) );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &P->Z, 1 ) );
|
2013-10-10 15:55:39 +02:00
|
|
|
|
|
|
|
cleanup:
|
2012-11-05 13:13:44 +01:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2012-11-24 14:10:14 +01:00
|
|
|
/*
|
2012-11-24 15:19:55 +01:00
|
|
|
* Export a point into unsigned binary data (SEC1 2.3.3)
|
2012-11-24 14:10:14 +01:00
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_ecp_point_write_binary( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *P,
|
2013-02-10 12:22:46 +01:00
|
|
|
int format, size_t *olen,
|
2013-02-10 10:58:48 +01:00
|
|
|
unsigned char *buf, size_t buflen )
|
2012-11-24 14:10:14 +01:00
|
|
|
{
|
2013-04-08 13:40:17 +02:00
|
|
|
int ret = 0;
|
2012-11-24 14:10:14 +01:00
|
|
|
size_t plen;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( format != MBEDTLS_ECP_PF_UNCOMPRESSED &&
|
|
|
|
format != MBEDTLS_ECP_PF_COMPRESSED )
|
|
|
|
return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
|
2012-11-24 15:19:55 +01:00
|
|
|
|
2012-11-24 14:10:14 +01:00
|
|
|
/*
|
2012-11-24 15:19:55 +01:00
|
|
|
* Common case: P == 0
|
2012-11-24 14:10:14 +01:00
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
if( mbedtls_mpi_cmp_int( &P->Z, 0 ) == 0 )
|
2012-11-24 14:10:14 +01:00
|
|
|
{
|
|
|
|
if( buflen < 1 )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
|
2012-11-24 14:10:14 +01:00
|
|
|
|
|
|
|
buf[0] = 0x00;
|
|
|
|
*olen = 1;
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
plen = mbedtls_mpi_size( &grp->P );
|
2012-11-24 14:10:14 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( format == MBEDTLS_ECP_PF_UNCOMPRESSED )
|
2012-11-24 15:19:55 +01:00
|
|
|
{
|
|
|
|
*olen = 2 * plen + 1;
|
|
|
|
|
|
|
|
if( buflen < *olen )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
|
2012-11-24 14:10:14 +01:00
|
|
|
|
2012-11-24 15:19:55 +01:00
|
|
|
buf[0] = 0x04;
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &P->X, buf + 1, plen ) );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &P->Y, buf + 1 + plen, plen ) );
|
2012-11-24 15:19:55 +01:00
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
else if( format == MBEDTLS_ECP_PF_COMPRESSED )
|
2012-11-24 15:19:55 +01:00
|
|
|
{
|
|
|
|
*olen = plen + 1;
|
|
|
|
|
|
|
|
if( buflen < *olen )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
|
2012-11-24 15:19:55 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
buf[0] = 0x02 + mbedtls_mpi_get_bit( &P->Y, 0 );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &P->X, buf + 1, plen ) );
|
2012-11-24 15:19:55 +01:00
|
|
|
}
|
2012-11-24 14:10:14 +01:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2012-11-24 16:19:42 +01:00
|
|
|
/*
|
|
|
|
* Import a point from unsigned binary data (SEC1 2.3.4)
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_ecp_point_read_binary( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
|
2014-03-19 16:18:38 +01:00
|
|
|
const unsigned char *buf, size_t ilen )
|
|
|
|
{
|
2012-11-24 16:19:42 +01:00
|
|
|
int ret;
|
|
|
|
size_t plen;
|
|
|
|
|
2014-10-20 13:59:19 +02:00
|
|
|
if( ilen < 1 )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
|
2014-07-08 13:09:24 +02:00
|
|
|
|
2014-03-26 14:12:20 +01:00
|
|
|
if( buf[0] == 0x00 )
|
|
|
|
{
|
|
|
|
if( ilen == 1 )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( mbedtls_ecp_set_zero( pt ) );
|
2014-03-26 14:12:20 +01:00
|
|
|
else
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
|
2014-03-26 14:12:20 +01:00
|
|
|
}
|
2012-11-24 16:19:42 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
plen = mbedtls_mpi_size( &grp->P );
|
2012-11-24 16:19:42 +01:00
|
|
|
|
2014-03-19 16:18:38 +01:00
|
|
|
if( buf[0] != 0x04 )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
|
2014-03-19 16:18:38 +01:00
|
|
|
|
|
|
|
if( ilen != 2 * plen + 1 )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
|
2012-11-24 16:19:42 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &pt->X, buf + 1, plen ) );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &pt->Y, buf + 1 + plen, plen ) );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Z, 1 ) );
|
2012-11-24 16:19:42 +01:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2013-02-09 19:00:07 +01:00
|
|
|
/*
|
|
|
|
* Import a point from a TLS ECPoint record (RFC 4492)
|
|
|
|
* struct {
|
|
|
|
* opaque point <1..2^8-1>;
|
|
|
|
* } ECPoint;
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_ecp_tls_read_point( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
|
2013-02-10 13:38:29 +01:00
|
|
|
const unsigned char **buf, size_t buf_len )
|
2013-02-09 19:00:07 +01:00
|
|
|
{
|
|
|
|
unsigned char data_len;
|
2013-02-10 13:38:29 +01:00
|
|
|
const unsigned char *buf_start;
|
2013-02-09 19:00:07 +01:00
|
|
|
|
|
|
|
/*
|
2014-07-08 13:09:24 +02:00
|
|
|
* We must have at least two bytes (1 for length, at least one for data)
|
2013-02-09 19:00:07 +01:00
|
|
|
*/
|
|
|
|
if( buf_len < 2 )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
|
2013-02-09 19:00:07 +01:00
|
|
|
|
2013-02-10 13:38:29 +01:00
|
|
|
data_len = *(*buf)++;
|
2013-02-09 19:00:07 +01:00
|
|
|
if( data_len < 1 || data_len > buf_len - 1 )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
|
2013-02-09 19:00:07 +01:00
|
|
|
|
2013-02-10 13:38:29 +01:00
|
|
|
/*
|
|
|
|
* Save buffer start for read_binary and update buf
|
|
|
|
*/
|
|
|
|
buf_start = *buf;
|
|
|
|
*buf += data_len;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
return mbedtls_ecp_point_read_binary( grp, pt, buf_start, data_len );
|
2013-02-09 19:00:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Export a point as a TLS ECPoint record (RFC 4492)
|
|
|
|
* struct {
|
|
|
|
* opaque point <1..2^8-1>;
|
|
|
|
* } ECPoint;
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_ecp_tls_write_point( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt,
|
2013-02-10 12:22:46 +01:00
|
|
|
int format, size_t *olen,
|
|
|
|
unsigned char *buf, size_t blen )
|
2013-02-09 19:00:07 +01:00
|
|
|
{
|
2013-02-10 12:22:46 +01:00
|
|
|
int ret;
|
|
|
|
|
2013-02-09 19:00:07 +01:00
|
|
|
/*
|
2013-02-10 12:22:46 +01:00
|
|
|
* buffer length must be at least one, for our length byte
|
2013-02-09 19:00:07 +01:00
|
|
|
*/
|
2013-02-10 12:22:46 +01:00
|
|
|
if( blen < 1 )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
|
2013-02-09 19:00:07 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_ecp_point_write_binary( grp, pt, format,
|
2013-02-10 12:22:46 +01:00
|
|
|
olen, buf + 1, blen - 1) ) != 0 )
|
|
|
|
return( ret );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* write length to the first byte and update total length
|
|
|
|
*/
|
2013-10-11 18:58:55 +02:00
|
|
|
buf[0] = (unsigned char) *olen;
|
2013-02-10 12:22:46 +01:00
|
|
|
++*olen;
|
|
|
|
|
2014-06-17 14:06:49 +02:00
|
|
|
return( 0 );
|
2013-02-09 19:00:07 +01:00
|
|
|
}
|
|
|
|
|
2013-10-23 20:19:57 +02:00
|
|
|
/*
|
|
|
|
* Set a group from an ECParameters record (RFC 4492)
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_ecp_tls_read_group( mbedtls_ecp_group *grp, const unsigned char **buf, size_t len )
|
2013-10-23 20:19:57 +02:00
|
|
|
{
|
|
|
|
uint16_t tls_id;
|
2015-04-08 12:49:31 +02:00
|
|
|
const mbedtls_ecp_curve_info *curve_info;
|
2013-10-23 20:19:57 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We expect at least three bytes (see below)
|
|
|
|
*/
|
|
|
|
if( len < 3 )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
|
2013-10-23 20:19:57 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* First byte is curve_type; only named_curve is handled
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
if( *(*buf)++ != MBEDTLS_ECP_TLS_NAMED_CURVE )
|
|
|
|
return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
|
2013-10-23 20:19:57 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Next two bytes are the namedcurve value
|
|
|
|
*/
|
|
|
|
tls_id = *(*buf)++;
|
|
|
|
tls_id <<= 8;
|
|
|
|
tls_id |= *(*buf)++;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( curve_info = mbedtls_ecp_curve_info_from_tls_id( tls_id ) ) == NULL )
|
|
|
|
return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
|
2013-10-23 20:19:57 +02:00
|
|
|
|
2015-05-11 18:46:47 +02:00
|
|
|
return mbedtls_ecp_group_load( grp, curve_info->grp_id );
|
2013-10-23 20:19:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write the ECParameters record corresponding to a group (RFC 4492)
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_ecp_tls_write_group( const mbedtls_ecp_group *grp, size_t *olen,
|
2013-10-23 20:19:57 +02:00
|
|
|
unsigned char *buf, size_t blen )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
const mbedtls_ecp_curve_info *curve_info;
|
2013-10-23 20:19:57 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( curve_info = mbedtls_ecp_curve_info_from_grp_id( grp->id ) ) == NULL )
|
|
|
|
return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
|
2013-10-23 20:19:57 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We are going to write 3 bytes (see below)
|
|
|
|
*/
|
|
|
|
*olen = 3;
|
|
|
|
if( blen < *olen )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
|
2013-10-23 20:19:57 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* First byte is curve_type, always named_curve
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
*buf++ = MBEDTLS_ECP_TLS_NAMED_CURVE;
|
2013-10-23 20:19:57 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Next two bytes are the namedcurve value
|
|
|
|
*/
|
|
|
|
buf[0] = curve_info->tls_id >> 8;
|
|
|
|
buf[1] = curve_info->tls_id & 0xFF;
|
|
|
|
|
2014-06-17 14:06:49 +02:00
|
|
|
return( 0 );
|
2013-10-23 20:19:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2015-04-08 12:49:31 +02:00
|
|
|
* Wrapper around fast quasi-modp functions, with fall-back to mbedtls_mpi_mod_mpi.
|
|
|
|
* See the documentation of struct mbedtls_ecp_group.
|
2013-10-23 20:19:57 +02:00
|
|
|
*
|
2015-04-08 12:49:31 +02:00
|
|
|
* This function is in the critial loop for mbedtls_ecp_mul, so pay attention to perf.
|
2013-10-23 20:19:57 +02:00
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
static int ecp_modp( mbedtls_mpi *N, const mbedtls_ecp_group *grp )
|
2013-10-23 20:19:57 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if( grp->modp == NULL )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( mbedtls_mpi_mod_mpi( N, N, &grp->P ) );
|
2013-10-23 20:19:57 +02:00
|
|
|
|
|
|
|
/* N->s < 0 is a much faster test, which fails only if N is 0 */
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( N->s < 0 && mbedtls_mpi_cmp_int( N, 0 ) != 0 ) ||
|
2015-06-18 16:47:17 +02:00
|
|
|
mbedtls_mpi_bitlen( N ) > 2 * grp->pbits )
|
2013-10-23 20:19:57 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
|
2013-10-23 20:19:57 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( grp->modp( N ) );
|
2013-10-23 20:19:57 +02:00
|
|
|
|
|
|
|
/* N->s < 0 is a much faster test, which fails only if N is 0 */
|
2015-04-08 12:49:31 +02:00
|
|
|
while( N->s < 0 && mbedtls_mpi_cmp_int( N, 0 ) != 0 )
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( N, N, &grp->P ) );
|
2013-10-23 20:19:57 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
while( mbedtls_mpi_cmp_mpi( N, &grp->P ) >= 0 )
|
2013-10-23 20:19:57 +02:00
|
|
|
/* we known P, N and the result are positive */
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( N, N, &grp->P ) );
|
2013-10-23 20:19:57 +02:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fast mod-p functions expect their argument to be in the 0..p^2 range.
|
|
|
|
*
|
|
|
|
* In order to guarantee that, we need to ensure that operands of
|
2015-04-08 12:49:31 +02:00
|
|
|
* mbedtls_mpi_mul_mpi are in the 0..p range. So, after each operation we will
|
2013-10-23 20:19:57 +02:00
|
|
|
* bring the result back to this range.
|
|
|
|
*
|
|
|
|
* The following macros are shortcuts for doing that.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2015-04-08 12:49:31 +02:00
|
|
|
* Reduce a mbedtls_mpi mod p in-place, general case, to use after mbedtls_mpi_mul_mpi
|
2013-10-23 20:19:57 +02:00
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SELF_TEST)
|
2013-11-21 20:23:55 +01:00
|
|
|
#define INC_MUL_COUNT mul_count++;
|
|
|
|
#else
|
|
|
|
#define INC_MUL_COUNT
|
|
|
|
#endif
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#define MOD_MUL( N ) do { MBEDTLS_MPI_CHK( ecp_modp( &N, grp ) ); INC_MUL_COUNT } \
|
2013-11-21 20:23:55 +01:00
|
|
|
while( 0 )
|
2013-10-23 20:19:57 +02:00
|
|
|
|
|
|
|
/*
|
2015-04-08 12:49:31 +02:00
|
|
|
* Reduce a mbedtls_mpi mod p in-place, to use after mbedtls_mpi_sub_mpi
|
2013-10-23 20:19:57 +02:00
|
|
|
* N->s < 0 is a very fast test, which fails only if N is 0
|
|
|
|
*/
|
|
|
|
#define MOD_SUB( N ) \
|
2015-04-08 12:49:31 +02:00
|
|
|
while( N.s < 0 && mbedtls_mpi_cmp_int( &N, 0 ) != 0 ) \
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &N, &N, &grp->P ) )
|
2013-10-23 20:19:57 +02:00
|
|
|
|
|
|
|
/*
|
2015-04-08 12:49:31 +02:00
|
|
|
* Reduce a mbedtls_mpi mod p in-place, to use after mbedtls_mpi_add_mpi and mbedtls_mpi_mul_int.
|
2013-10-23 20:19:57 +02:00
|
|
|
* We known P, N and the result are positive, so sub_abs is correct, and
|
|
|
|
* a bit faster.
|
|
|
|
*/
|
|
|
|
#define MOD_ADD( N ) \
|
2015-04-08 12:49:31 +02:00
|
|
|
while( mbedtls_mpi_cmp_mpi( &N, &grp->P ) >= 0 ) \
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &N, &N, &grp->P ) )
|
2013-10-23 20:19:57 +02:00
|
|
|
|
2015-04-09 12:14:31 +02:00
|
|
|
#if defined(ECP_SHORTWEIERSTRASS)
|
2013-12-04 23:15:46 +01:00
|
|
|
/*
|
|
|
|
* For curves in short Weierstrass form, we do all the internal operations in
|
|
|
|
* Jacobian coordinates.
|
|
|
|
*
|
|
|
|
* For multiplication, we'll use a comb method with coutermeasueres against
|
|
|
|
* SPA, hence timing attacks.
|
|
|
|
*/
|
|
|
|
|
2013-10-23 20:19:57 +02:00
|
|
|
/*
|
|
|
|
* Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
|
2013-11-20 22:57:38 +01:00
|
|
|
* Cost: 1N := 1I + 3M + 1S
|
2013-10-23 20:19:57 +02:00
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
static int ecp_normalize_jac( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt )
|
2013-10-23 20:19:57 +02:00
|
|
|
{
|
|
|
|
int ret;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi Zi, ZZi;
|
2013-10-23 20:19:57 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( mbedtls_mpi_cmp_int( &pt->Z, 0 ) == 0 )
|
2013-10-23 20:19:57 +02:00
|
|
|
return( 0 );
|
|
|
|
|
2016-08-18 13:38:46 +02:00
|
|
|
#if defined(MBEDTLS_ECP_NORMALIZE_JAC_ALT)
|
2016-11-18 17:38:23 +01:00
|
|
|
if ( mbedtls_internal_ecp_grp_capable( grp ) )
|
2016-08-18 13:38:46 +02:00
|
|
|
{
|
2016-11-18 17:38:23 +01:00
|
|
|
return mbedtls_internal_ecp_normalize_jac( grp, pt );
|
2016-08-18 13:38:46 +02:00
|
|
|
}
|
2016-10-28 17:53:11 +02:00
|
|
|
#endif /* MBEDTLS_ECP_NORMALIZE_JAC_ALT */
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_init( &Zi ); mbedtls_mpi_init( &ZZi );
|
2013-10-23 20:19:57 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* X = X / Z^2 mod p
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &Zi, &pt->Z, &grp->P ) );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->X, &pt->X, &ZZi ) ); MOD_MUL( pt->X );
|
2013-10-23 20:19:57 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Y = Y / Z^3 mod p
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->Y, &pt->Y, &ZZi ) ); MOD_MUL( pt->Y );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->Y, &pt->Y, &Zi ) ); MOD_MUL( pt->Y );
|
2013-10-23 20:19:57 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Z = 1
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Z, 1 ) );
|
2013-10-23 20:19:57 +02:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_free( &Zi ); mbedtls_mpi_free( &ZZi );
|
2013-10-23 20:19:57 +02:00
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2012-11-07 20:24:05 +01:00
|
|
|
/*
|
2013-11-16 15:50:12 +01:00
|
|
|
* Normalize jacobian coordinates of an array of (pointers to) points,
|
2013-10-23 20:19:57 +02:00
|
|
|
* using Montgomery's trick to perform only one inversion mod P.
|
|
|
|
* (See for example Cohen's "A Course in Computational Algebraic Number
|
|
|
|
* Theory", Algorithm 10.3.4.)
|
|
|
|
*
|
|
|
|
* Warning: fails (returning an error) if one of the points is zero!
|
2013-12-05 10:26:01 +01:00
|
|
|
* This should never happen, see choice of w in ecp_mul_comb().
|
2013-11-20 22:57:38 +01:00
|
|
|
*
|
|
|
|
* Cost: 1N(t) := 1I + (6t - 3)M + 1S
|
2012-11-07 20:24:05 +01:00
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
static int ecp_normalize_jac_many( const mbedtls_ecp_group *grp,
|
|
|
|
mbedtls_ecp_point *T[], size_t t_len )
|
2012-11-07 20:24:05 +01:00
|
|
|
{
|
2013-10-23 20:19:57 +02:00
|
|
|
int ret;
|
|
|
|
size_t i;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi *c, u, Zi, ZZi;
|
2013-02-10 12:06:19 +01:00
|
|
|
|
2013-10-23 20:19:57 +02:00
|
|
|
if( t_len < 2 )
|
2013-12-02 19:44:41 +01:00
|
|
|
return( ecp_normalize_jac( grp, *T ) );
|
2013-10-23 20:19:57 +02:00
|
|
|
|
2016-08-18 13:38:46 +02:00
|
|
|
#if defined(MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT)
|
2016-11-18 17:38:23 +01:00
|
|
|
if ( mbedtls_internal_ecp_grp_capable( grp ) )
|
2016-08-18 13:38:46 +02:00
|
|
|
{
|
2016-11-18 17:38:23 +01:00
|
|
|
return mbedtls_internal_ecp_normalize_jac_many(grp, T, t_len);
|
2016-08-18 13:38:46 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-05-26 16:04:06 +02:00
|
|
|
if( ( c = mbedtls_calloc( t_len, sizeof( mbedtls_mpi ) ) ) == NULL )
|
2015-05-28 09:33:39 +02:00
|
|
|
return( MBEDTLS_ERR_ECP_ALLOC_FAILED );
|
2013-10-23 20:19:57 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_init( &u ); mbedtls_mpi_init( &Zi ); mbedtls_mpi_init( &ZZi );
|
2013-10-23 20:19:57 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* c[i] = Z_0 * ... * Z_i
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &c[0], &T[0]->Z ) );
|
2013-10-23 20:19:57 +02:00
|
|
|
for( i = 1; i < t_len; i++ )
|
2012-11-07 20:24:05 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &c[i], &c[i-1], &T[i]->Z ) );
|
2013-10-23 20:19:57 +02:00
|
|
|
MOD_MUL( c[i] );
|
|
|
|
}
|
2012-11-10 14:23:17 +01:00
|
|
|
|
2013-10-23 20:19:57 +02:00
|
|
|
/*
|
|
|
|
* u = 1 / (Z_0 * ... * Z_n) mod P
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &u, &c[t_len-1], &grp->P ) );
|
2012-11-10 14:23:17 +01:00
|
|
|
|
2013-10-23 20:19:57 +02:00
|
|
|
for( i = t_len - 1; ; i-- )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Zi = 1 / Z_i mod p
|
|
|
|
* u = 1 / (Z_0 * ... * Z_i) mod P
|
|
|
|
*/
|
|
|
|
if( i == 0 ) {
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Zi, &u ) );
|
2013-10-23 20:19:57 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &Zi, &u, &c[i-1] ) ); MOD_MUL( Zi );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &u, &u, &T[i]->Z ) ); MOD_MUL( u );
|
2013-10-23 20:19:57 +02:00
|
|
|
}
|
2012-11-10 14:23:17 +01:00
|
|
|
|
2013-10-23 20:19:57 +02:00
|
|
|
/*
|
|
|
|
* proceed as in normalize()
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T[i]->X, &T[i]->X, &ZZi ) ); MOD_MUL( T[i]->X );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &ZZi ) ); MOD_MUL( T[i]->Y );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &Zi ) ); MOD_MUL( T[i]->Y );
|
2013-12-30 17:31:56 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Post-precessing: reclaim some memory by shrinking coordinates
|
|
|
|
* - not storing Z (always 1)
|
|
|
|
* - shrinking other coordinates, but still keeping the same number of
|
|
|
|
* limbs as P, as otherwise it will too likely be regrown too fast.
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_shrink( &T[i]->X, grp->P.n ) );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_shrink( &T[i]->Y, grp->P.n ) );
|
|
|
|
mbedtls_mpi_free( &T[i]->Z );
|
2012-11-10 14:23:17 +01:00
|
|
|
|
2013-10-23 20:19:57 +02:00
|
|
|
if( i == 0 )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_free( &u ); mbedtls_mpi_free( &Zi ); mbedtls_mpi_free( &ZZi );
|
2013-10-23 20:19:57 +02:00
|
|
|
for( i = 0; i < t_len; i++ )
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_free( &c[i] );
|
|
|
|
mbedtls_free( c );
|
2013-10-23 20:19:57 +02:00
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2013-11-21 17:47:12 +01:00
|
|
|
/*
|
|
|
|
* Conditional point inversion: Q -> -Q = (Q.X, -Q.Y, Q.Z) without leak.
|
|
|
|
* "inv" must be 0 (don't invert) or 1 (invert) or the result will be invalid
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
static int ecp_safe_invert_jac( const mbedtls_ecp_group *grp,
|
|
|
|
mbedtls_ecp_point *Q,
|
2013-11-21 17:47:12 +01:00
|
|
|
unsigned char inv )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
unsigned char nonzero;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi mQY;
|
2013-11-21 17:47:12 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_init( &mQY );
|
2013-11-21 17:47:12 +01:00
|
|
|
|
|
|
|
/* Use the fact that -Q.Y mod P = P - Q.Y unless Q.Y == 0 */
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &mQY, &grp->P, &Q->Y ) );
|
|
|
|
nonzero = mbedtls_mpi_cmp_int( &Q->Y, 0 ) != 0;
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &Q->Y, &mQY, inv & nonzero ) );
|
2013-11-21 17:47:12 +01:00
|
|
|
|
|
|
|
cleanup:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_free( &mQY );
|
2013-11-21 17:47:12 +01:00
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2013-10-23 20:19:57 +02:00
|
|
|
/*
|
|
|
|
* Point doubling R = 2 P, Jacobian coordinates
|
|
|
|
*
|
2015-02-07 08:43:51 +01:00
|
|
|
* Based on http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian.html#doubling-dbl-1998-cmo-2 .
|
2013-11-20 22:57:38 +01:00
|
|
|
*
|
2015-02-07 08:43:51 +01:00
|
|
|
* We follow the variable naming fairly closely. The formula variations that trade a MUL for a SQR
|
|
|
|
* (plus a few ADDs) aren't useful as our bignum implementation doesn't distinguish squaring.
|
|
|
|
*
|
|
|
|
* Standard optimizations are applied when curve parameter A is one of { 0, -3 }.
|
|
|
|
*
|
|
|
|
* Cost: 1D := 3M + 4S (A == 0)
|
|
|
|
* 4M + 4S (A == -3)
|
|
|
|
* 3M + 6S + 1a otherwise
|
2013-10-23 20:19:57 +02:00
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
static int ecp_double_jac( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
|
|
|
|
const mbedtls_ecp_point *P )
|
2013-10-23 20:19:57 +02:00
|
|
|
{
|
|
|
|
int ret;
|
2015-05-12 10:36:26 +02:00
|
|
|
mbedtls_mpi M, S, T, U;
|
2013-10-23 20:19:57 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SELF_TEST)
|
2013-10-23 20:19:57 +02:00
|
|
|
dbl_count++;
|
2013-10-23 16:11:52 +02:00
|
|
|
#endif
|
2012-11-07 20:24:05 +01:00
|
|
|
|
2016-08-18 13:38:46 +02:00
|
|
|
#if defined(MBEDTLS_ECP_DOUBLE_JAC_ALT)
|
2016-11-18 17:38:23 +01:00
|
|
|
if ( mbedtls_internal_ecp_grp_capable( grp ) )
|
2016-08-18 13:38:46 +02:00
|
|
|
{
|
2016-11-18 17:38:23 +01:00
|
|
|
return mbedtls_internal_ecp_double_jac( grp, R, P );
|
2016-08-18 13:38:46 +02:00
|
|
|
}
|
2016-10-28 17:53:11 +02:00
|
|
|
#endif /* MBEDTLS_ECP_DOUBLE_JAC_ALT */
|
2016-08-18 13:38:46 +02:00
|
|
|
|
2015-05-12 10:36:26 +02:00
|
|
|
mbedtls_mpi_init( &M ); mbedtls_mpi_init( &S ); mbedtls_mpi_init( &T ); mbedtls_mpi_init( &U );
|
2013-12-06 12:41:30 +01:00
|
|
|
|
|
|
|
/* Special case for A = -3 */
|
|
|
|
if( grp->A.p == NULL )
|
|
|
|
{
|
2015-02-07 08:43:51 +01:00
|
|
|
/* M = 3(X + Z^2)(X - Z^2) */
|
2015-05-12 10:36:26 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &P->Z, &P->Z ) ); MOD_MUL( S );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &P->X, &S ) ); MOD_ADD( T );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U, &P->X, &S ) ); MOD_SUB( U );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &T, &U ) ); MOD_MUL( S );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &M, &S, 3 ) ); MOD_ADD( M );
|
2013-12-06 12:41:30 +01:00
|
|
|
}
|
|
|
|
else
|
2014-08-06 00:48:39 +02:00
|
|
|
{
|
2015-02-07 08:43:51 +01:00
|
|
|
/* M = 3.X^2 */
|
2015-05-12 10:36:26 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &P->X, &P->X ) ); MOD_MUL( S );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &M, &S, 3 ) ); MOD_ADD( M );
|
2015-02-07 08:43:51 +01:00
|
|
|
|
|
|
|
/* Optimize away for "koblitz" curves with A = 0 */
|
2015-05-12 10:36:26 +02:00
|
|
|
if( mbedtls_mpi_cmp_int( &grp->A, 0 ) != 0 )
|
2015-02-07 08:43:51 +01:00
|
|
|
{
|
|
|
|
/* M += A.Z^4 */
|
2015-05-12 10:36:26 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &P->Z, &P->Z ) ); MOD_MUL( S );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &S, &S ) ); MOD_MUL( T );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &T, &grp->A ) ); MOD_MUL( S );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &M, &M, &S ) ); MOD_ADD( M );
|
2015-02-07 08:43:51 +01:00
|
|
|
}
|
2014-08-06 00:48:39 +02:00
|
|
|
}
|
2013-12-06 12:41:30 +01:00
|
|
|
|
2015-02-07 08:43:51 +01:00
|
|
|
/* S = 4.X.Y^2 */
|
2015-05-12 10:36:26 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &P->Y, &P->Y ) ); MOD_MUL( T );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &T, 1 ) ); MOD_ADD( T );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &P->X, &T ) ); MOD_MUL( S );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &S, 1 ) ); MOD_ADD( S );
|
2015-02-07 08:43:51 +01:00
|
|
|
|
|
|
|
/* U = 8.Y^4 */
|
2015-05-12 10:36:26 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &U, &T, &T ) ); MOD_MUL( U );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &U, 1 ) ); MOD_ADD( U );
|
2015-02-07 08:43:51 +01:00
|
|
|
|
|
|
|
/* T = M^2 - 2.S */
|
2015-05-12 10:36:26 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &M, &M ) ); MOD_MUL( T );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T, &S ) ); MOD_SUB( T );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T, &S ) ); MOD_SUB( T );
|
2015-02-07 08:43:51 +01:00
|
|
|
|
|
|
|
/* S = M(S - T) - U */
|
2015-05-12 10:36:26 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &S, &S, &T ) ); MOD_SUB( S );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &S, &M ) ); MOD_MUL( S );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &S, &S, &U ) ); MOD_SUB( S );
|
2015-02-07 08:43:51 +01:00
|
|
|
|
|
|
|
/* U = 2.Y.Z */
|
2015-05-12 10:36:26 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &U, &P->Y, &P->Z ) ); MOD_MUL( U );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &U, 1 ) ); MOD_ADD( U );
|
2015-02-07 08:43:51 +01:00
|
|
|
|
2015-05-12 10:36:26 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->X, &T ) );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Y, &S ) );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Z, &U ) );
|
2013-10-08 12:04:56 +02:00
|
|
|
|
2013-10-23 20:19:57 +02:00
|
|
|
cleanup:
|
2015-05-12 10:36:26 +02:00
|
|
|
mbedtls_mpi_free( &M ); mbedtls_mpi_free( &S ); mbedtls_mpi_free( &T ); mbedtls_mpi_free( &U );
|
2013-10-23 20:19:57 +02:00
|
|
|
|
|
|
|
return( ret );
|
2013-02-09 17:03:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-11-21 18:20:43 +01:00
|
|
|
* Addition: R = P + Q, mixed affine-Jacobian coordinates (GECC 3.22)
|
2013-10-23 20:19:57 +02:00
|
|
|
*
|
|
|
|
* The coordinates of Q must be normalized (= affine),
|
|
|
|
* but those of P don't need to. R is not normalized.
|
|
|
|
*
|
2013-11-21 19:19:54 +01:00
|
|
|
* Special cases: (1) P or Q is zero, (2) R is zero, (3) P == Q.
|
2013-12-05 10:26:01 +01:00
|
|
|
* None of these cases can happen as intermediate step in ecp_mul_comb():
|
2013-11-21 19:19:54 +01:00
|
|
|
* - at each step, P, Q and R are multiples of the base point, the factor
|
|
|
|
* being less than its order, so none of them is zero;
|
|
|
|
* - Q is an odd multiple of the base point, P an even multiple,
|
|
|
|
* due to the choice of precomputed points in the modified comb method.
|
|
|
|
* So branches for these cases do not leak secret information.
|
|
|
|
*
|
2013-12-30 16:04:55 +01:00
|
|
|
* We accept Q->Z being unset (saving memory in tables) as meaning 1.
|
|
|
|
*
|
2013-11-20 22:57:38 +01:00
|
|
|
* Cost: 1A := 8M + 3S
|
2013-02-09 17:03:58 +01:00
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
static int ecp_add_mixed( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
|
|
|
|
const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q )
|
2013-02-09 17:03:58 +01:00
|
|
|
{
|
2013-10-23 20:19:57 +02:00
|
|
|
int ret;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi T1, T2, T3, T4, X, Y, Z;
|
2013-10-23 20:19:57 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SELF_TEST)
|
2013-10-23 20:19:57 +02:00
|
|
|
add_count++;
|
|
|
|
#endif
|
2013-02-09 17:03:58 +01:00
|
|
|
|
2016-08-18 13:38:46 +02:00
|
|
|
#if defined(MBEDTLS_ECP_ADD_MIXED_ALT)
|
2016-11-18 17:38:23 +01:00
|
|
|
if ( mbedtls_internal_ecp_grp_capable( grp ) )
|
2016-08-18 13:38:46 +02:00
|
|
|
{
|
2016-11-18 17:38:23 +01:00
|
|
|
return mbedtls_internal_ecp_add_mixed( grp, R, P, Q );
|
2016-08-18 13:38:46 +02:00
|
|
|
}
|
2016-10-28 17:53:11 +02:00
|
|
|
#endif /* MBEDTLS_ECP_ADD_MIXED_ALT */
|
2016-08-18 13:38:46 +02:00
|
|
|
|
2013-02-09 17:03:58 +01:00
|
|
|
/*
|
2013-11-21 19:19:54 +01:00
|
|
|
* Trivial cases: P == 0 or Q == 0 (case 1)
|
2013-02-09 17:03:58 +01:00
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
if( mbedtls_mpi_cmp_int( &P->Z, 0 ) == 0 )
|
|
|
|
return( mbedtls_ecp_copy( R, Q ) );
|
2013-10-23 20:19:57 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( Q->Z.p != NULL && mbedtls_mpi_cmp_int( &Q->Z, 0 ) == 0 )
|
|
|
|
return( mbedtls_ecp_copy( R, P ) );
|
2013-02-09 17:03:58 +01:00
|
|
|
|
|
|
|
/*
|
2013-10-23 20:19:57 +02:00
|
|
|
* Make sure Q coordinates are normalized
|
2013-02-09 17:03:58 +01:00
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
if( Q->Z.p != NULL && mbedtls_mpi_cmp_int( &Q->Z, 1 ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
|
2013-02-09 17:03:58 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 ); mbedtls_mpi_init( &T3 ); mbedtls_mpi_init( &T4 );
|
|
|
|
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
|
2013-10-23 20:19:57 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T2, &T2, &P->Y ) ); MOD_SUB( T2 );
|
2013-02-10 12:06:19 +01:00
|
|
|
|
2013-11-21 19:19:54 +01:00
|
|
|
/* Special cases (2) and (3) */
|
2015-04-08 12:49:31 +02:00
|
|
|
if( mbedtls_mpi_cmp_int( &T1, 0 ) == 0 )
|
2013-10-23 20:19:57 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
if( mbedtls_mpi_cmp_int( &T2, 0 ) == 0 )
|
2013-10-23 20:19:57 +02:00
|
|
|
{
|
|
|
|
ret = ecp_double_jac( grp, R, P );
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = mbedtls_ecp_set_zero( R );
|
2013-10-23 20:19:57 +02:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
2013-09-23 18:14:50 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &Y, &T3, &T4 ) ); MOD_SUB( Y );
|
|
|
|
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->X, &X ) );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Y, &Y ) );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Z, &Z ) );
|
2013-02-10 12:06:19 +01:00
|
|
|
|
2013-10-23 20:19:57 +02:00
|
|
|
cleanup:
|
2013-02-10 12:06:19 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 ); mbedtls_mpi_free( &T3 ); mbedtls_mpi_free( &T4 );
|
|
|
|
mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
|
2013-02-10 12:06:19 +01:00
|
|
|
|
2013-10-23 20:19:57 +02:00
|
|
|
return( ret );
|
2012-11-07 20:24:05 +01:00
|
|
|
}
|
2012-11-05 17:34:55 +01:00
|
|
|
|
2012-11-02 18:14:40 +01:00
|
|
|
/*
|
2013-10-23 20:19:57 +02:00
|
|
|
* Randomize jacobian coordinates:
|
|
|
|
* (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l
|
2013-12-02 19:44:41 +01:00
|
|
|
* This is sort of the reverse operation of ecp_normalize_jac().
|
2013-11-21 10:53:59 +01:00
|
|
|
*
|
|
|
|
* This countermeasure was first suggested in [2].
|
2013-10-09 16:09:46 +02:00
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
static int ecp_randomize_jac( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
|
2013-10-23 20:19:57 +02:00
|
|
|
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
|
2013-10-09 16:09:46 +02:00
|
|
|
{
|
|
|
|
int ret;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi l, ll;
|
2016-08-18 13:38:46 +02:00
|
|
|
size_t p_size;
|
2013-10-23 20:19:57 +02:00
|
|
|
int count = 0;
|
2013-10-10 12:44:27 +02:00
|
|
|
|
2016-08-18 13:38:46 +02:00
|
|
|
#if defined(MBEDTLS_ECP_RANDOMIZE_JAC_ALT)
|
2016-11-18 17:38:23 +01:00
|
|
|
if ( mbedtls_internal_ecp_grp_capable( grp ) )
|
2016-08-18 13:38:46 +02:00
|
|
|
{
|
2016-11-18 17:38:23 +01:00
|
|
|
return mbedtls_internal_ecp_randomize_jac( grp, pt, f_rng, p_rng );
|
2016-08-18 13:38:46 +02:00
|
|
|
}
|
2016-10-28 17:53:11 +02:00
|
|
|
#endif /* MBEDTLS_ECP_RANDOMIZE_JAC_ALT */
|
2016-08-18 13:38:46 +02:00
|
|
|
|
|
|
|
p_size = ( grp->pbits + 7 ) / 8;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_init( &l ); mbedtls_mpi_init( &ll );
|
2013-10-10 15:55:39 +02:00
|
|
|
|
2013-10-23 20:19:57 +02:00
|
|
|
/* Generate l such that 1 < l < p */
|
|
|
|
do
|
|
|
|
{
|
2017-01-12 13:50:50 +01:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &l, p_size, f_rng, p_rng ) );
|
2013-10-10 12:44:27 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
while( mbedtls_mpi_cmp_mpi( &l, &grp->P ) >= 0 )
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &l, 1 ) );
|
2013-10-09 16:09:46 +02:00
|
|
|
|
2013-10-23 20:19:57 +02:00
|
|
|
if( count++ > 10 )
|
2020-05-08 09:57:18 +02:00
|
|
|
{
|
|
|
|
ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2013-10-23 20:19:57 +02:00
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
while( mbedtls_mpi_cmp_int( &l, 1 ) <= 0 );
|
2013-10-23 20:19:57 +02:00
|
|
|
|
|
|
|
/* Z = l * Z */
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->Z, &pt->Z, &l ) ); MOD_MUL( pt->Z );
|
2013-10-23 20:19:57 +02:00
|
|
|
|
|
|
|
/* X = l^2 * X */
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ll, &l, &l ) ); MOD_MUL( ll );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->X, &pt->X, &ll ) ); MOD_MUL( pt->X );
|
2013-10-23 20:19:57 +02:00
|
|
|
|
|
|
|
/* Y = l^3 * Y */
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ll, &ll, &l ) ); MOD_MUL( ll );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->Y, &pt->Y, &ll ) ); MOD_MUL( pt->Y );
|
2013-10-09 16:09:46 +02:00
|
|
|
|
|
|
|
cleanup:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_free( &l ); mbedtls_mpi_free( &ll );
|
2013-10-09 16:09:46 +02:00
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2013-11-16 15:50:12 +01:00
|
|
|
/*
|
2013-11-20 18:39:55 +01:00
|
|
|
* Check and define parameters used by the comb method (see below for details)
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
#if MBEDTLS_ECP_WINDOW_SIZE < 2 || MBEDTLS_ECP_WINDOW_SIZE > 7
|
|
|
|
#error "MBEDTLS_ECP_WINDOW_SIZE out of bounds"
|
2013-11-20 18:39:55 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* d = ceil( n / w ) */
|
2015-04-08 12:49:31 +02:00
|
|
|
#define COMB_MAX_D ( MBEDTLS_ECP_MAX_BITS + 1 ) / 2
|
2013-11-20 18:39:55 +01:00
|
|
|
|
|
|
|
/* number of precomputed points */
|
2015-04-08 12:49:31 +02:00
|
|
|
#define COMB_MAX_PRE ( 1 << ( MBEDTLS_ECP_WINDOW_SIZE - 1 ) )
|
2013-11-20 18:39:55 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Compute the representation of m that will be used with our comb method.
|
2013-11-16 15:50:12 +01:00
|
|
|
*
|
|
|
|
* The basic comb method is described in GECC 3.44 for example. We use a
|
2013-11-20 14:47:19 +01:00
|
|
|
* modified version that provides resistance to SPA by avoiding zero
|
|
|
|
* digits in the representation as in [3]. We modify the method further by
|
|
|
|
* requiring that all K_i be odd, which has the small cost that our
|
2013-11-20 18:39:55 +01:00
|
|
|
* representation uses one more K_i, due to carries.
|
2013-11-20 14:47:19 +01:00
|
|
|
*
|
|
|
|
* Also, for the sake of compactness, only the seven low-order bits of x[i]
|
|
|
|
* are used to represent K_i, and the msb of x[i] encodes the the sign (s_i in
|
|
|
|
* the paper): it is set if and only if if s_i == -1;
|
2013-11-16 15:50:12 +01:00
|
|
|
*
|
|
|
|
* Calling conventions:
|
2013-11-20 14:47:19 +01:00
|
|
|
* - x is an array of size d + 1
|
2013-11-20 18:39:55 +01:00
|
|
|
* - w is the size, ie number of teeth, of the comb, and must be between
|
2015-04-08 12:49:31 +02:00
|
|
|
* 2 and 7 (in practice, between 2 and MBEDTLS_ECP_WINDOW_SIZE)
|
2013-11-20 14:47:19 +01:00
|
|
|
* - m is the MPI, expected to be odd and such that bitlength(m) <= w * d
|
|
|
|
* (the result will be incorrect if these assumptions are not satisfied)
|
2013-11-16 15:50:12 +01:00
|
|
|
*/
|
2013-11-20 14:47:19 +01:00
|
|
|
static void ecp_comb_fixed( unsigned char x[], size_t d,
|
2015-04-08 12:49:31 +02:00
|
|
|
unsigned char w, const mbedtls_mpi *m )
|
2013-11-16 15:50:12 +01:00
|
|
|
{
|
|
|
|
size_t i, j;
|
2013-11-20 14:47:19 +01:00
|
|
|
unsigned char c, cc, adjust;
|
2013-11-16 15:50:12 +01:00
|
|
|
|
2013-11-20 14:47:19 +01:00
|
|
|
memset( x, 0, d+1 );
|
2013-11-16 15:50:12 +01:00
|
|
|
|
2013-11-21 09:50:00 +01:00
|
|
|
/* First get the classical comb values (except for x_d = 0) */
|
|
|
|
for( i = 0; i < d; i++ )
|
2013-11-16 15:50:12 +01:00
|
|
|
for( j = 0; j < w; j++ )
|
2015-04-08 12:49:31 +02:00
|
|
|
x[i] |= mbedtls_mpi_get_bit( m, i + d * j ) << j;
|
2013-11-16 15:50:12 +01:00
|
|
|
|
2013-11-21 09:50:00 +01:00
|
|
|
/* Now make sure x_1 .. x_d are odd */
|
|
|
|
c = 0;
|
|
|
|
for( i = 1; i <= d; i++ )
|
|
|
|
{
|
2013-11-20 14:47:19 +01:00
|
|
|
/* Add carry and update it */
|
|
|
|
cc = x[i] & c;
|
|
|
|
x[i] = x[i] ^ c;
|
|
|
|
c = cc;
|
|
|
|
|
2013-11-21 09:50:00 +01:00
|
|
|
/* Adjust if needed, avoiding branches */
|
2013-11-20 14:47:19 +01:00
|
|
|
adjust = 1 - ( x[i] & 0x01 );
|
|
|
|
c |= x[i] & ( x[i-1] * adjust );
|
|
|
|
x[i] = x[i] ^ ( x[i-1] * adjust );
|
|
|
|
x[i-1] |= adjust << 7;
|
2013-11-16 15:50:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Precompute points for the comb method
|
|
|
|
*
|
2013-11-20 14:47:19 +01:00
|
|
|
* If i = i_{w-1} ... i_1 is the binary representation of i, then
|
|
|
|
* T[i] = i_{w-1} 2^{(w-1)d} P + ... + i_1 2^d P + P
|
2013-11-16 15:50:12 +01:00
|
|
|
*
|
2013-11-20 22:57:38 +01:00
|
|
|
* T must be able to hold 2^{w - 1} elements
|
|
|
|
*
|
|
|
|
* Cost: d(w-1) D + (2^{w-1} - 1) A + 1 N(w-1) + 1 N(2^{w-1} - 1)
|
2013-11-16 15:50:12 +01:00
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
static int ecp_precompute_comb( const mbedtls_ecp_group *grp,
|
|
|
|
mbedtls_ecp_point T[], const mbedtls_ecp_point *P,
|
2013-11-16 15:50:12 +01:00
|
|
|
unsigned char w, size_t d )
|
|
|
|
{
|
|
|
|
int ret;
|
2013-11-20 18:39:55 +01:00
|
|
|
unsigned char i, k;
|
|
|
|
size_t j;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_point *cur, *TT[COMB_MAX_PRE - 1];
|
2013-11-16 15:50:12 +01:00
|
|
|
|
|
|
|
/*
|
2013-11-20 14:47:19 +01:00
|
|
|
* Set T[0] = P and
|
|
|
|
* T[2^{l-1}] = 2^{dl} P for l = 1 .. w-1 (this is not the final value)
|
2013-11-16 15:50:12 +01:00
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_ecp_copy( &T[0], P ) );
|
2013-11-16 15:50:12 +01:00
|
|
|
|
2013-11-20 14:47:19 +01:00
|
|
|
k = 0;
|
2014-06-17 16:39:18 +02:00
|
|
|
for( i = 1; i < ( 1U << ( w - 1 ) ); i <<= 1 )
|
2013-11-16 15:50:12 +01:00
|
|
|
{
|
2013-11-20 14:47:19 +01:00
|
|
|
cur = T + i;
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_ecp_copy( cur, T + ( i >> 1 ) ) );
|
2013-11-16 15:50:12 +01:00
|
|
|
for( j = 0; j < d; j++ )
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( ecp_double_jac( grp, cur, cur ) );
|
2013-11-20 14:47:19 +01:00
|
|
|
|
|
|
|
TT[k++] = cur;
|
2013-11-16 15:50:12 +01:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( ecp_normalize_jac_many( grp, TT, k ) );
|
2013-11-16 15:50:12 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Compute the remaining ones using the minimal number of additions
|
2013-11-20 14:47:19 +01:00
|
|
|
* Be careful to update T[2^l] only after using it!
|
2013-11-16 15:50:12 +01:00
|
|
|
*/
|
2013-11-20 14:47:19 +01:00
|
|
|
k = 0;
|
2014-06-17 16:39:18 +02:00
|
|
|
for( i = 1; i < ( 1U << ( w - 1 ) ); i <<= 1 )
|
2013-11-16 15:50:12 +01:00
|
|
|
{
|
2013-11-20 14:47:19 +01:00
|
|
|
j = i;
|
|
|
|
while( j-- )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( ecp_add_mixed( grp, &T[i + j], &T[j], &T[i] ) );
|
2013-11-20 14:47:19 +01:00
|
|
|
TT[k++] = &T[i + j];
|
|
|
|
}
|
2013-11-16 15:50:12 +01:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( ecp_normalize_jac_many( grp, TT, k ) );
|
2013-11-21 10:08:50 +01:00
|
|
|
|
2013-11-16 15:50:12 +01:00
|
|
|
cleanup:
|
2016-08-18 13:38:46 +02:00
|
|
|
|
2013-11-16 15:50:12 +01:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-11-20 14:47:19 +01:00
|
|
|
* Select precomputed point: R = sign(i) * T[ abs(i) / 2 ]
|
2013-11-16 15:50:12 +01:00
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
static int ecp_select_comb( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
|
|
|
|
const mbedtls_ecp_point T[], unsigned char t_len,
|
2013-11-21 20:00:38 +01:00
|
|
|
unsigned char i )
|
2013-11-16 15:50:12 +01:00
|
|
|
{
|
|
|
|
int ret;
|
2013-11-21 20:00:38 +01:00
|
|
|
unsigned char ii, j;
|
2013-11-16 15:50:12 +01:00
|
|
|
|
2013-11-21 20:00:38 +01:00
|
|
|
/* Ignore the "sign" bit and scale down */
|
|
|
|
ii = ( i & 0x7Fu ) >> 1;
|
2013-11-16 15:50:12 +01:00
|
|
|
|
2013-11-21 20:00:38 +01:00
|
|
|
/* Read the whole table to thwart cache-based timing attacks */
|
|
|
|
for( j = 0; j < t_len; j++ )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &R->X, &T[j].X, j == ii ) );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &R->Y, &T[j].Y, j == ii ) );
|
2013-11-21 20:00:38 +01:00
|
|
|
}
|
|
|
|
|
2013-11-21 17:47:12 +01:00
|
|
|
/* Safely invert result if i is "negative" */
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( ecp_safe_invert_jac( grp, R, i >> 7 ) );
|
2013-11-16 15:50:12 +01:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Core multiplication algorithm for the (modified) comb method.
|
|
|
|
* This part is actually common with the basic comb method (GECC 3.44)
|
2013-11-20 22:57:38 +01:00
|
|
|
*
|
|
|
|
* Cost: d A + d D + 1 R
|
2013-11-16 15:50:12 +01:00
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
static int ecp_mul_comb_core( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
|
|
|
|
const mbedtls_ecp_point T[], unsigned char t_len,
|
2013-11-20 20:07:26 +01:00
|
|
|
const unsigned char x[], size_t d,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t),
|
|
|
|
void *p_rng )
|
2013-11-16 15:50:12 +01:00
|
|
|
{
|
|
|
|
int ret;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_point Txi;
|
2013-11-16 15:50:12 +01:00
|
|
|
size_t i;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_point_init( &Txi );
|
2013-11-16 15:50:12 +01:00
|
|
|
|
2013-11-20 20:07:26 +01:00
|
|
|
/* Start with a non-zero point and randomize its coordinates */
|
2013-11-20 14:47:19 +01:00
|
|
|
i = d;
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( ecp_select_comb( grp, R, T, t_len, x[i] ) );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R->Z, 1 ) );
|
2020-06-19 10:36:16 +02:00
|
|
|
#if defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
|
2013-11-20 20:07:26 +01:00
|
|
|
if( f_rng != 0 )
|
2020-06-19 10:36:16 +02:00
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( ecp_randomize_jac( grp, R, f_rng, p_rng ) );
|
2013-11-16 15:50:12 +01:00
|
|
|
|
|
|
|
while( i-- != 0 )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( ecp_double_jac( grp, R, R ) );
|
|
|
|
MBEDTLS_MPI_CHK( ecp_select_comb( grp, &Txi, T, t_len, x[i] ) );
|
|
|
|
MBEDTLS_MPI_CHK( ecp_add_mixed( grp, R, R, &Txi ) );
|
2013-11-16 15:50:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
2016-08-18 13:38:46 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_point_free( &Txi );
|
2013-11-16 15:50:12 +01:00
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-12-04 11:49:20 +01:00
|
|
|
* Multiplication using the comb method,
|
|
|
|
* for curves in short Weierstrass form
|
2013-11-16 15:50:12 +01:00
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
static int ecp_mul_comb( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
|
|
|
|
const mbedtls_mpi *m, const mbedtls_ecp_point *P,
|
2013-12-04 11:49:20 +01:00
|
|
|
int (*f_rng)(void *, unsigned char *, size_t),
|
|
|
|
void *p_rng )
|
2013-11-16 15:50:12 +01:00
|
|
|
{
|
|
|
|
int ret;
|
2013-11-21 20:00:38 +01:00
|
|
|
unsigned char w, m_is_odd, p_eq_g, pre_len, i;
|
|
|
|
size_t d;
|
2013-11-20 18:39:55 +01:00
|
|
|
unsigned char k[COMB_MAX_D + 1];
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_point *T;
|
|
|
|
mbedtls_mpi M, mm;
|
2013-11-16 15:50:12 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_init( &M );
|
|
|
|
mbedtls_mpi_init( &mm );
|
2013-11-16 15:50:12 +01:00
|
|
|
|
2013-12-04 11:49:20 +01:00
|
|
|
/* we need N to be odd to trnaform m in an odd number, check now */
|
2015-04-08 12:49:31 +02:00
|
|
|
if( mbedtls_mpi_get_bit( &grp->N, 0 ) != 1 )
|
|
|
|
return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
|
2013-12-04 11:49:20 +01:00
|
|
|
|
2013-11-20 22:57:38 +01:00
|
|
|
/*
|
|
|
|
* Minimize the number of multiplications, that is minimize
|
2013-11-21 18:33:36 +01:00
|
|
|
* 10 * d * w + 18 * 2^(w-1) + 11 * d + 7 * w, with d = ceil( nbits / w )
|
2013-11-20 22:57:38 +01:00
|
|
|
* (see costs of the various parts, with 1S = 1M)
|
|
|
|
*/
|
|
|
|
w = grp->nbits >= 384 ? 5 : 4;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If P == G, pre-compute a bit more, since this may be re-used later.
|
2013-12-30 18:41:16 +01:00
|
|
|
* Just adding one avoids upping the cost of the first mul too much,
|
|
|
|
* and the memory cost too.
|
2013-11-20 22:57:38 +01:00
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
#if MBEDTLS_ECP_FIXED_POINT_OPTIM == 1
|
|
|
|
p_eq_g = ( mbedtls_mpi_cmp_mpi( &P->Y, &grp->G.Y ) == 0 &&
|
|
|
|
mbedtls_mpi_cmp_mpi( &P->X, &grp->G.X ) == 0 );
|
2013-11-20 22:57:38 +01:00
|
|
|
if( p_eq_g )
|
|
|
|
w++;
|
2013-12-30 18:41:16 +01:00
|
|
|
#else
|
|
|
|
p_eq_g = 0;
|
|
|
|
#endif
|
2013-11-20 22:57:38 +01:00
|
|
|
|
|
|
|
/*
|
2013-11-21 18:33:36 +01:00
|
|
|
* Make sure w is within bounds.
|
2013-11-20 22:57:38 +01:00
|
|
|
* (The last test is useful only for very small curves in the test suite.)
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
if( w > MBEDTLS_ECP_WINDOW_SIZE )
|
|
|
|
w = MBEDTLS_ECP_WINDOW_SIZE;
|
2013-11-21 18:33:36 +01:00
|
|
|
if( w >= grp->nbits )
|
2013-11-20 22:57:38 +01:00
|
|
|
w = 2;
|
2013-11-16 15:50:12 +01:00
|
|
|
|
2013-11-20 22:57:38 +01:00
|
|
|
/* Other sizes that depend on w */
|
2013-11-20 18:39:55 +01:00
|
|
|
pre_len = 1U << ( w - 1 );
|
2013-11-16 15:50:12 +01:00
|
|
|
d = ( grp->nbits + w - 1 ) / w;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Prepare precomputed points: if P == G we want to
|
2013-11-21 09:50:00 +01:00
|
|
|
* use grp->T if already initialized, or initialize it.
|
2013-11-16 15:50:12 +01:00
|
|
|
*/
|
2013-11-21 19:19:54 +01:00
|
|
|
T = p_eq_g ? grp->T : NULL;
|
2013-11-21 09:50:00 +01:00
|
|
|
|
|
|
|
if( T == NULL )
|
2013-11-16 15:50:12 +01:00
|
|
|
{
|
2015-05-26 16:04:06 +02:00
|
|
|
T = mbedtls_calloc( pre_len, sizeof( mbedtls_ecp_point ) );
|
2013-11-16 15:50:12 +01:00
|
|
|
if( T == NULL )
|
|
|
|
{
|
2015-05-28 09:33:39 +02:00
|
|
|
ret = MBEDTLS_ERR_ECP_ALLOC_FAILED;
|
2013-11-16 15:50:12 +01:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( ecp_precompute_comb( grp, T, P, w, d ) );
|
2013-11-16 15:50:12 +01:00
|
|
|
|
|
|
|
if( p_eq_g )
|
|
|
|
{
|
|
|
|
grp->T = T;
|
|
|
|
grp->T_size = pre_len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-11-21 19:19:54 +01:00
|
|
|
* Make sure M is odd (M = m or M = N - m, since N is odd)
|
|
|
|
* using the fact that m * P = - (N - m) * P
|
2013-11-16 15:50:12 +01:00
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
m_is_odd = ( mbedtls_mpi_get_bit( m, 0 ) == 1 );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &M, m ) );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &mm, &grp->N, m ) );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &M, &mm, ! m_is_odd ) );
|
2013-11-16 15:50:12 +01:00
|
|
|
|
|
|
|
/*
|
2013-11-21 19:19:54 +01:00
|
|
|
* Go for comb multiplication, R = M * P
|
2013-11-16 15:50:12 +01:00
|
|
|
*/
|
|
|
|
ecp_comb_fixed( k, d, w, &M );
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( ecp_mul_comb_core( grp, R, T, pre_len, k, d, f_rng, p_rng ) );
|
2013-11-16 15:50:12 +01:00
|
|
|
|
|
|
|
/*
|
2013-11-21 19:19:54 +01:00
|
|
|
* Now get m * P from M * P and normalize it
|
2013-11-16 15:50:12 +01:00
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( ecp_safe_invert_jac( grp, R, ! m_is_odd ) );
|
2020-04-01 11:25:51 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Knowledge of the jacobian coordinates may leak the last few bits of the
|
|
|
|
* scalar [1], and since our MPI implementation isn't constant-flow,
|
|
|
|
* inversion (used for coordinate normalization) may leak the full value
|
|
|
|
* of its input via side-channels [2].
|
|
|
|
*
|
|
|
|
* [1] https://eprint.iacr.org/2003/191
|
|
|
|
* [2] https://eprint.iacr.org/2020/055
|
|
|
|
*
|
|
|
|
* Avoid the leak by randomizing coordinates before we normalize them.
|
|
|
|
*/
|
2020-06-19 10:36:16 +02:00
|
|
|
#if defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
|
2020-04-01 11:25:51 +02:00
|
|
|
if( f_rng != 0 )
|
2020-06-19 10:36:16 +02:00
|
|
|
#endif
|
2020-04-01 11:25:51 +02:00
|
|
|
MBEDTLS_MPI_CHK( ecp_randomize_jac( grp, R, f_rng, p_rng ) );
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( ecp_normalize_jac( grp, R ) );
|
2013-11-16 15:50:12 +01:00
|
|
|
|
2012-11-19 21:23:27 +01:00
|
|
|
cleanup:
|
|
|
|
|
2017-09-06 07:07:17 +02:00
|
|
|
/* There are two cases where T is not stored in grp:
|
|
|
|
* - P != G
|
|
|
|
* - An intermediate operation failed before setting grp->T
|
|
|
|
* In either case, T must be freed.
|
|
|
|
*/
|
|
|
|
if( T != NULL && T != grp->T )
|
2013-10-23 20:19:57 +02:00
|
|
|
{
|
|
|
|
for( i = 0; i < pre_len; i++ )
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_point_free( &T[i] );
|
|
|
|
mbedtls_free( T );
|
2013-10-23 20:19:57 +02:00
|
|
|
}
|
2012-11-19 21:23:27 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_free( &M );
|
|
|
|
mbedtls_mpi_free( &mm );
|
2013-11-21 19:19:54 +01:00
|
|
|
|
|
|
|
if( ret != 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_point_free( R );
|
2012-11-02 18:14:40 +01:00
|
|
|
|
2013-12-03 12:02:28 +01:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2015-04-09 12:14:31 +02:00
|
|
|
#endif /* ECP_SHORTWEIERSTRASS */
|
2013-12-04 23:15:46 +01:00
|
|
|
|
2015-04-09 12:14:31 +02:00
|
|
|
#if defined(ECP_MONTGOMERY)
|
2013-12-04 23:15:46 +01:00
|
|
|
/*
|
|
|
|
* For Montgomery curves, we do all the internal arithmetic in projective
|
|
|
|
* coordinates. Import/export of points uses only the x coordinates, which is
|
|
|
|
* internaly represented as X / Z.
|
|
|
|
*
|
|
|
|
* For scalar multiplication, we'll use a Montgomery ladder.
|
|
|
|
*/
|
|
|
|
|
2013-12-03 12:02:28 +01:00
|
|
|
/*
|
|
|
|
* Normalize Montgomery x/z coordinates: X = X/Z, Z = 1
|
|
|
|
* Cost: 1M + 1I
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
static int ecp_normalize_mxz( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P )
|
2013-12-03 12:02:28 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2016-08-18 13:38:46 +02:00
|
|
|
#if defined(MBEDTLS_ECP_NORMALIZE_MXZ_ALT)
|
2016-11-18 17:38:23 +01:00
|
|
|
if ( mbedtls_internal_ecp_grp_capable( grp ) )
|
2016-08-18 13:38:46 +02:00
|
|
|
{
|
2016-11-18 17:38:23 +01:00
|
|
|
return mbedtls_internal_ecp_normalize_mxz( grp, P );
|
2016-08-18 13:38:46 +02:00
|
|
|
}
|
2016-10-28 17:53:11 +02:00
|
|
|
#endif /* MBEDTLS_ECP_NORMALIZE_MXZ_ALT */
|
2016-08-18 13:38:46 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &P->Z, &P->Z, &grp->P ) );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &P->X, &P->X, &P->Z ) ); MOD_MUL( P->X );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &P->Z, 1 ) );
|
2013-12-03 12:02:28 +01:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2013-12-03 13:28:21 +01:00
|
|
|
/*
|
|
|
|
* Randomize projective x/z coordinates:
|
|
|
|
* (X, Z) -> (l X, l Z) for random l
|
|
|
|
* This is sort of the reverse operation of ecp_normalize_mxz().
|
|
|
|
*
|
|
|
|
* This countermeasure was first suggested in [2].
|
|
|
|
* Cost: 2M
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
static int ecp_randomize_mxz( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P,
|
2013-12-03 13:28:21 +01:00
|
|
|
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
|
|
|
|
{
|
|
|
|
int ret;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi l;
|
2016-08-18 13:38:46 +02:00
|
|
|
size_t p_size;
|
2013-12-03 13:28:21 +01:00
|
|
|
int count = 0;
|
|
|
|
|
2016-08-18 13:38:46 +02:00
|
|
|
#if defined(MBEDTLS_ECP_RANDOMIZE_MXZ_ALT)
|
2016-11-18 17:38:23 +01:00
|
|
|
if ( mbedtls_internal_ecp_grp_capable( grp ) )
|
2016-08-18 13:38:46 +02:00
|
|
|
{
|
2016-11-18 17:38:23 +01:00
|
|
|
return mbedtls_internal_ecp_randomize_mxz( grp, P, f_rng, p_rng );
|
2016-08-18 13:38:46 +02:00
|
|
|
}
|
2016-10-28 17:53:11 +02:00
|
|
|
#endif /* MBEDTLS_ECP_RANDOMIZE_MXZ_ALT */
|
2016-08-18 13:38:46 +02:00
|
|
|
|
|
|
|
p_size = ( grp->pbits + 7 ) / 8;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_init( &l );
|
2013-12-03 13:28:21 +01:00
|
|
|
|
|
|
|
/* Generate l such that 1 < l < p */
|
|
|
|
do
|
|
|
|
{
|
2017-01-12 13:50:50 +01:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &l, p_size, f_rng, p_rng ) );
|
2013-12-03 13:28:21 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
while( mbedtls_mpi_cmp_mpi( &l, &grp->P ) >= 0 )
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &l, 1 ) );
|
2013-12-03 13:28:21 +01:00
|
|
|
|
|
|
|
if( count++ > 10 )
|
2020-05-08 09:57:18 +02:00
|
|
|
{
|
|
|
|
ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2013-12-03 13:28:21 +01:00
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
while( mbedtls_mpi_cmp_int( &l, 1 ) <= 0 );
|
2013-12-03 13:28:21 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &P->X, &P->X, &l ) ); MOD_MUL( P->X );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &P->Z, &P->Z, &l ) ); MOD_MUL( P->Z );
|
2013-12-03 13:28:21 +01:00
|
|
|
|
|
|
|
cleanup:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_free( &l );
|
2013-12-03 13:28:21 +01:00
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2013-12-03 12:02:28 +01:00
|
|
|
/*
|
|
|
|
* Double-and-add: R = 2P, S = P + Q, with d = X(P - Q),
|
|
|
|
* for Montgomery curves in x/z coordinates.
|
|
|
|
*
|
|
|
|
* http://www.hyperelliptic.org/EFD/g1p/auto-code/montgom/xz/ladder/mladd-1987-m.op3
|
|
|
|
* with
|
|
|
|
* d = X1
|
|
|
|
* P = (X2, Z2)
|
|
|
|
* Q = (X3, Z3)
|
|
|
|
* R = (X4, Z4)
|
|
|
|
* S = (X5, Z5)
|
|
|
|
* and eliminating temporary variables tO, ..., t4.
|
|
|
|
*
|
|
|
|
* Cost: 5M + 4S
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
static int ecp_double_add_mxz( const mbedtls_ecp_group *grp,
|
|
|
|
mbedtls_ecp_point *R, mbedtls_ecp_point *S,
|
|
|
|
const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q,
|
|
|
|
const mbedtls_mpi *d )
|
2013-12-03 12:02:28 +01:00
|
|
|
{
|
|
|
|
int ret;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi A, AA, B, BB, E, C, D, DA, CB;
|
|
|
|
|
2016-08-18 13:38:46 +02:00
|
|
|
#if defined(MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT)
|
2016-11-18 17:38:23 +01:00
|
|
|
if ( mbedtls_internal_ecp_grp_capable( grp ) )
|
2016-08-18 13:38:46 +02:00
|
|
|
{
|
2016-11-18 17:38:23 +01:00
|
|
|
return mbedtls_internal_ecp_double_add_mxz( grp, R, S, P, Q, d );
|
2016-08-18 13:38:46 +02:00
|
|
|
}
|
2016-10-28 17:53:11 +02:00
|
|
|
#endif /* MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT */
|
2016-08-18 13:38:46 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_init( &A ); mbedtls_mpi_init( &AA ); mbedtls_mpi_init( &B );
|
|
|
|
mbedtls_mpi_init( &BB ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &C );
|
|
|
|
mbedtls_mpi_init( &D ); mbedtls_mpi_init( &DA ); mbedtls_mpi_init( &CB );
|
|
|
|
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &A, &P->X, &P->Z ) ); MOD_ADD( A );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &AA, &A, &A ) ); MOD_MUL( AA );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &B, &P->X, &P->Z ) ); MOD_SUB( B );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &BB, &B, &B ) ); MOD_MUL( BB );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &E, &AA, &BB ) ); MOD_SUB( E );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &C, &Q->X, &Q->Z ) ); MOD_ADD( C );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &D, &Q->X, &Q->Z ) ); MOD_SUB( D );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DA, &D, &A ) ); MOD_MUL( DA );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &CB, &C, &B ) ); MOD_MUL( CB );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &S->X, &DA, &CB ) ); MOD_MUL( S->X );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S->X, &S->X, &S->X ) ); MOD_MUL( S->X );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &S->Z, &DA, &CB ) ); MOD_SUB( S->Z );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S->Z, &S->Z, &S->Z ) ); MOD_MUL( S->Z );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S->Z, d, &S->Z ) ); MOD_MUL( S->Z );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &R->X, &AA, &BB ) ); MOD_MUL( R->X );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &R->Z, &grp->A, &E ) ); MOD_MUL( R->Z );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &R->Z, &BB, &R->Z ) ); MOD_ADD( R->Z );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &R->Z, &E, &R->Z ) ); MOD_MUL( R->Z );
|
2013-12-03 12:02:28 +01:00
|
|
|
|
|
|
|
cleanup:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_free( &A ); mbedtls_mpi_free( &AA ); mbedtls_mpi_free( &B );
|
|
|
|
mbedtls_mpi_free( &BB ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &C );
|
|
|
|
mbedtls_mpi_free( &D ); mbedtls_mpi_free( &DA ); mbedtls_mpi_free( &CB );
|
2013-12-03 12:02:28 +01:00
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-12-04 11:49:20 +01:00
|
|
|
* Multiplication with Montgomery ladder in x/z coordinates,
|
|
|
|
* for curves in Montgomery form
|
2013-12-03 12:02:28 +01:00
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
static int ecp_mul_mxz( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
|
|
|
|
const mbedtls_mpi *m, const mbedtls_ecp_point *P,
|
2013-12-04 11:49:20 +01:00
|
|
|
int (*f_rng)(void *, unsigned char *, size_t),
|
|
|
|
void *p_rng )
|
2013-12-03 12:02:28 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t i;
|
2013-12-04 21:54:36 +01:00
|
|
|
unsigned char b;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_point RP;
|
|
|
|
mbedtls_mpi PX;
|
2013-12-03 12:02:28 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_point_init( &RP ); mbedtls_mpi_init( &PX );
|
2013-12-03 12:02:28 +01:00
|
|
|
|
2013-12-03 13:28:21 +01:00
|
|
|
/* Save PX and read from P before writing to R, in case P == R */
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &PX, &P->X ) );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_ecp_copy( &RP, P ) );
|
2013-12-04 18:39:17 +01:00
|
|
|
|
|
|
|
/* Set R to zero in modified x/z coordinates */
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R->X, 1 ) );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R->Z, 0 ) );
|
|
|
|
mbedtls_mpi_free( &R->Y );
|
2013-12-03 12:02:28 +01:00
|
|
|
|
2013-12-05 10:48:42 +01:00
|
|
|
/* RP.X might be sligtly larger than P, so reduce it */
|
|
|
|
MOD_ADD( RP.X );
|
|
|
|
|
2013-12-03 13:28:21 +01:00
|
|
|
/* Randomize coordinates of the starting point */
|
2020-06-19 10:36:16 +02:00
|
|
|
#if defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
|
2013-12-04 18:39:17 +01:00
|
|
|
if( f_rng != NULL )
|
2020-06-19 10:36:16 +02:00
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( ecp_randomize_mxz( grp, &RP, f_rng, p_rng ) );
|
2013-12-03 12:02:28 +01:00
|
|
|
|
2013-12-04 21:54:36 +01:00
|
|
|
/* Loop invariant: R = result so far, RP = R + P */
|
2015-06-18 16:47:17 +02:00
|
|
|
i = mbedtls_mpi_bitlen( m ); /* one past the (zero-based) most significant bit */
|
2013-12-03 12:02:28 +01:00
|
|
|
while( i-- > 0 )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
b = mbedtls_mpi_get_bit( m, i );
|
2013-12-04 21:54:36 +01:00
|
|
|
/*
|
|
|
|
* if (b) R = 2R + P else R = 2R,
|
|
|
|
* which is:
|
|
|
|
* if (b) double_add( RP, R, RP, R )
|
|
|
|
* else double_add( R, RP, R, RP )
|
|
|
|
* but using safe conditional swaps to avoid leaks
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->X, &RP.X, b ) );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->Z, &RP.Z, b ) );
|
|
|
|
MBEDTLS_MPI_CHK( ecp_double_add_mxz( grp, R, &RP, R, &RP, &PX ) );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->X, &RP.X, b ) );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->Z, &RP.Z, b ) );
|
2013-12-03 12:02:28 +01:00
|
|
|
}
|
|
|
|
|
2020-04-01 11:25:51 +02:00
|
|
|
/*
|
|
|
|
* Knowledge of the projective coordinates may leak the last few bits of the
|
|
|
|
* scalar [1], and since our MPI implementation isn't constant-flow,
|
|
|
|
* inversion (used for coordinate normalization) may leak the full value
|
|
|
|
* of its input via side-channels [2].
|
|
|
|
*
|
|
|
|
* [1] https://eprint.iacr.org/2003/191
|
|
|
|
* [2] https://eprint.iacr.org/2020/055
|
|
|
|
*
|
|
|
|
* Avoid the leak by randomizing coordinates before we normalize them.
|
|
|
|
*/
|
2020-06-19 10:36:16 +02:00
|
|
|
#if defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
|
2020-04-01 11:25:51 +02:00
|
|
|
if( f_rng != NULL )
|
2020-06-19 10:36:16 +02:00
|
|
|
#endif
|
2020-04-01 11:25:51 +02:00
|
|
|
MBEDTLS_MPI_CHK( ecp_randomize_mxz( grp, R, f_rng, p_rng ) );
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( ecp_normalize_mxz( grp, R ) );
|
2013-12-03 12:02:28 +01:00
|
|
|
|
|
|
|
cleanup:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_point_free( &RP ); mbedtls_mpi_free( &PX );
|
2013-12-03 12:02:28 +01:00
|
|
|
|
2012-11-08 23:21:46 +01:00
|
|
|
return( ret );
|
2012-11-02 18:14:40 +01:00
|
|
|
}
|
|
|
|
|
2015-04-09 12:14:31 +02:00
|
|
|
#endif /* ECP_MONTGOMERY */
|
2013-12-04 23:15:46 +01:00
|
|
|
|
2013-12-04 11:49:20 +01:00
|
|
|
/*
|
|
|
|
* Multiplication R = m * P
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_ecp_mul( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
|
|
|
|
const mbedtls_mpi *m, const mbedtls_ecp_point *P,
|
2013-12-04 11:49:20 +01:00
|
|
|
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
|
|
|
|
{
|
2016-08-18 13:38:46 +02:00
|
|
|
int ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
2016-11-18 17:38:23 +01:00
|
|
|
#if defined(MBEDTLS_ECP_INTERNAL_ALT)
|
|
|
|
char is_grp_capable = 0;
|
|
|
|
#endif
|
2020-05-22 12:12:36 +02:00
|
|
|
#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
|
|
|
|
ecp_drbg_context drbg_ctx;
|
|
|
|
|
|
|
|
ecp_drbg_init( &drbg_ctx );
|
|
|
|
#endif /* !MBEDTLS_ECP_NO_INTERNAL_RNG */
|
2013-12-04 11:49:20 +01:00
|
|
|
|
|
|
|
/* Common sanity checks */
|
2015-04-08 12:49:31 +02:00
|
|
|
if( mbedtls_mpi_cmp_int( &P->Z, 1 ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
|
2013-12-04 11:49:20 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_ecp_check_privkey( grp, m ) ) != 0 ||
|
|
|
|
( ret = mbedtls_ecp_check_pubkey( grp, P ) ) != 0 )
|
2013-12-04 11:49:20 +01:00
|
|
|
return( ret );
|
|
|
|
|
2020-05-22 12:12:36 +02:00
|
|
|
#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
|
|
|
|
if( f_rng == NULL )
|
|
|
|
{
|
2020-06-16 12:51:42 +02:00
|
|
|
const size_t m_len = ( grp->nbits + 7 ) / 8;
|
|
|
|
MBEDTLS_MPI_CHK( ecp_drbg_seed( &drbg_ctx, m, m_len ) );
|
2020-05-22 12:12:36 +02:00
|
|
|
f_rng = &ecp_drbg_random;
|
|
|
|
p_rng = &drbg_ctx;
|
|
|
|
}
|
|
|
|
#endif /* !MBEDTLS_ECP_NO_INTERNAL_RNG */
|
|
|
|
|
2016-11-18 17:38:23 +01:00
|
|
|
#if defined(MBEDTLS_ECP_INTERNAL_ALT)
|
|
|
|
if ( is_grp_capable = mbedtls_internal_ecp_grp_capable( grp ) )
|
2016-08-18 13:38:46 +02:00
|
|
|
{
|
2016-11-18 17:38:23 +01:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_internal_ecp_init( grp ) );
|
2016-08-18 13:38:46 +02:00
|
|
|
}
|
2016-11-18 17:38:23 +01:00
|
|
|
#endif /* MBEDTLS_ECP_INTERNAL_ALT */
|
2020-05-22 12:12:36 +02:00
|
|
|
|
2015-04-09 12:14:31 +02:00
|
|
|
#if defined(ECP_MONTGOMERY)
|
|
|
|
if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY )
|
2016-08-18 13:38:46 +02:00
|
|
|
ret = ecp_mul_mxz( grp, R, m, P, f_rng, p_rng );
|
2016-11-03 15:25:37 +01:00
|
|
|
#endif
|
2015-04-09 12:14:31 +02:00
|
|
|
#if defined(ECP_SHORTWEIERSTRASS)
|
|
|
|
if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS )
|
2016-08-18 13:38:46 +02:00
|
|
|
ret = ecp_mul_comb( grp, R, m, P, f_rng, p_rng );
|
2020-05-22 12:12:36 +02:00
|
|
|
#endif
|
2016-08-18 13:38:46 +02:00
|
|
|
|
2020-05-22 12:12:36 +02:00
|
|
|
#if defined(MBEDTLS_ECP_INTERNAL_ALT) || !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
|
|
|
|
cleanup:
|
2016-11-03 15:25:37 +01:00
|
|
|
#endif
|
2016-11-18 17:38:23 +01:00
|
|
|
#if defined(MBEDTLS_ECP_INTERNAL_ALT)
|
|
|
|
if ( is_grp_capable )
|
2016-08-18 13:38:46 +02:00
|
|
|
{
|
2016-11-18 17:38:23 +01:00
|
|
|
mbedtls_internal_ecp_free( grp );
|
2016-08-18 13:38:46 +02:00
|
|
|
}
|
2016-11-18 17:38:23 +01:00
|
|
|
#endif /* MBEDTLS_ECP_INTERNAL_ALT */
|
2020-05-22 12:12:36 +02:00
|
|
|
|
|
|
|
#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
|
|
|
|
ecp_drbg_free( &drbg_ctx );
|
|
|
|
#endif
|
|
|
|
|
2016-08-18 13:38:46 +02:00
|
|
|
return( ret );
|
2013-12-04 11:49:20 +01:00
|
|
|
}
|
|
|
|
|
2015-04-09 12:14:31 +02:00
|
|
|
#if defined(ECP_SHORTWEIERSTRASS)
|
2012-11-17 19:54:20 +01:00
|
|
|
/*
|
2013-12-05 10:06:06 +01:00
|
|
|
* Check that an affine point is valid as a public key,
|
|
|
|
* short weierstrass curves (SEC1 3.2.3.1)
|
2012-11-17 19:54:20 +01:00
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
static int ecp_check_pubkey_sw( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt )
|
2012-11-17 19:54:20 +01:00
|
|
|
{
|
|
|
|
int ret;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi YY, RHS;
|
2012-11-17 19:54:20 +01:00
|
|
|
|
2013-12-04 11:08:01 +01:00
|
|
|
/* pt coordinates must be normalized for our checks */
|
2015-04-08 12:49:31 +02:00
|
|
|
if( mbedtls_mpi_cmp_int( &pt->X, 0 ) < 0 ||
|
|
|
|
mbedtls_mpi_cmp_int( &pt->Y, 0 ) < 0 ||
|
|
|
|
mbedtls_mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
|
|
|
|
mbedtls_mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
|
|
|
|
return( MBEDTLS_ERR_ECP_INVALID_KEY );
|
2012-11-17 19:54:20 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_init( &YY ); mbedtls_mpi_init( &RHS );
|
2012-11-17 19:54:20 +01:00
|
|
|
|
|
|
|
/*
|
2013-10-23 20:19:57 +02:00
|
|
|
* YY = Y^2
|
|
|
|
* RHS = X (X^2 + A) + B = X^3 + A X + B
|
2012-11-17 19:54:20 +01:00
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS );
|
2013-12-06 12:41:30 +01:00
|
|
|
|
|
|
|
/* Special case for A = -3 */
|
|
|
|
if( grp->A.p == NULL )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &RHS, &RHS, 3 ) ); MOD_SUB( RHS );
|
2013-12-06 12:41:30 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &RHS, &RHS, &grp->A ) ); MOD_ADD( RHS );
|
2013-12-06 12:41:30 +01:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS );
|
2013-10-23 20:19:57 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( mbedtls_mpi_cmp_mpi( &YY, &RHS ) != 0 )
|
|
|
|
ret = MBEDTLS_ERR_ECP_INVALID_KEY;
|
2012-11-17 19:54:20 +01:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_free( &YY ); mbedtls_mpi_free( &RHS );
|
2012-11-17 19:54:20 +01:00
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
2015-04-09 12:14:31 +02:00
|
|
|
#endif /* ECP_SHORTWEIERSTRASS */
|
2013-12-05 10:06:06 +01:00
|
|
|
|
2015-10-23 15:50:37 +02:00
|
|
|
/*
|
|
|
|
* R = m * P with shortcuts for m == 1 and m == -1
|
|
|
|
* NOT constant-time - ONLY for short Weierstrass!
|
|
|
|
*/
|
|
|
|
static int mbedtls_ecp_mul_shortcuts( mbedtls_ecp_group *grp,
|
|
|
|
mbedtls_ecp_point *R,
|
|
|
|
const mbedtls_mpi *m,
|
|
|
|
const mbedtls_ecp_point *P )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if( mbedtls_mpi_cmp_int( m, 1 ) == 0 )
|
|
|
|
{
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, P ) );
|
|
|
|
}
|
|
|
|
else if( mbedtls_mpi_cmp_int( m, -1 ) == 0 )
|
|
|
|
{
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, P ) );
|
|
|
|
if( mbedtls_mpi_cmp_int( &R->Y, 0 ) != 0 )
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &R->Y, &grp->P, &R->Y ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_ecp_mul( grp, R, m, P, NULL, NULL ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2015-05-11 18:40:45 +02:00
|
|
|
/*
|
|
|
|
* Linear combination
|
2015-10-23 15:50:37 +02:00
|
|
|
* NOT constant-time
|
2015-05-11 18:40:45 +02:00
|
|
|
*/
|
|
|
|
int mbedtls_ecp_muladd( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
|
|
|
|
const mbedtls_mpi *m, const mbedtls_ecp_point *P,
|
|
|
|
const mbedtls_mpi *n, const mbedtls_ecp_point *Q )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
mbedtls_ecp_point mP;
|
2016-11-18 17:38:23 +01:00
|
|
|
#if defined(MBEDTLS_ECP_INTERNAL_ALT)
|
|
|
|
char is_grp_capable = 0;
|
|
|
|
#endif
|
2015-05-11 18:40:45 +02:00
|
|
|
|
|
|
|
if( ecp_get_type( grp ) != ECP_TYPE_SHORT_WEIERSTRASS )
|
|
|
|
return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
|
|
|
|
|
|
|
|
mbedtls_ecp_point_init( &mP );
|
|
|
|
|
2015-10-23 15:50:37 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_ecp_mul_shortcuts( grp, &mP, m, P ) );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_ecp_mul_shortcuts( grp, R, n, Q ) );
|
2015-08-13 10:19:09 +02:00
|
|
|
|
2016-11-18 17:38:23 +01:00
|
|
|
#if defined(MBEDTLS_ECP_INTERNAL_ALT)
|
|
|
|
if ( is_grp_capable = mbedtls_internal_ecp_grp_capable( grp ) )
|
2016-08-18 13:38:46 +02:00
|
|
|
{
|
2016-11-18 17:38:23 +01:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_internal_ecp_init( grp ) );
|
2016-08-18 13:38:46 +02:00
|
|
|
}
|
2016-11-03 15:25:37 +01:00
|
|
|
|
2016-11-18 17:38:23 +01:00
|
|
|
#endif /* MBEDTLS_ECP_INTERNAL_ALT */
|
2015-05-11 18:40:45 +02:00
|
|
|
MBEDTLS_MPI_CHK( ecp_add_mixed( grp, R, &mP, R ) );
|
|
|
|
MBEDTLS_MPI_CHK( ecp_normalize_jac( grp, R ) );
|
|
|
|
|
|
|
|
cleanup:
|
2016-08-18 13:38:46 +02:00
|
|
|
|
2016-11-18 17:38:23 +01:00
|
|
|
#if defined(MBEDTLS_ECP_INTERNAL_ALT)
|
|
|
|
if ( is_grp_capable )
|
2016-08-18 13:38:46 +02:00
|
|
|
{
|
2016-11-18 17:38:23 +01:00
|
|
|
mbedtls_internal_ecp_free( grp );
|
2016-08-18 13:38:46 +02:00
|
|
|
}
|
2016-11-03 15:25:37 +01:00
|
|
|
|
2016-11-18 17:38:23 +01:00
|
|
|
#endif /* MBEDTLS_ECP_INTERNAL_ALT */
|
2015-05-11 18:40:45 +02:00
|
|
|
mbedtls_ecp_point_free( &mP );
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2013-12-05 10:06:06 +01:00
|
|
|
|
2015-04-09 12:14:31 +02:00
|
|
|
#if defined(ECP_MONTGOMERY)
|
2013-12-05 10:06:06 +01:00
|
|
|
/*
|
|
|
|
* Check validity of a public key for Montgomery curves with x-only schemes
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
static int ecp_check_pubkey_mx( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt )
|
2013-12-05 10:06:06 +01:00
|
|
|
{
|
2015-06-23 00:18:41 +02:00
|
|
|
/* [Curve25519 p. 5] Just check X is the correct number of bytes */
|
2015-04-08 12:49:31 +02:00
|
|
|
if( mbedtls_mpi_size( &pt->X ) > ( grp->nbits + 7 ) / 8 )
|
|
|
|
return( MBEDTLS_ERR_ECP_INVALID_KEY );
|
2013-12-05 10:06:06 +01:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
2015-04-09 12:14:31 +02:00
|
|
|
#endif /* ECP_MONTGOMERY */
|
2013-12-05 10:06:06 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check that a point is valid as a public key
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt )
|
2013-12-05 10:06:06 +01:00
|
|
|
{
|
|
|
|
/* Must use affine coordinates */
|
2015-04-08 12:49:31 +02:00
|
|
|
if( mbedtls_mpi_cmp_int( &pt->Z, 1 ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_ECP_INVALID_KEY );
|
2013-12-05 10:06:06 +01:00
|
|
|
|
2015-04-09 12:14:31 +02:00
|
|
|
#if defined(ECP_MONTGOMERY)
|
|
|
|
if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY )
|
2013-12-05 10:06:06 +01:00
|
|
|
return( ecp_check_pubkey_mx( grp, pt ) );
|
|
|
|
#endif
|
2015-04-09 12:14:31 +02:00
|
|
|
#if defined(ECP_SHORTWEIERSTRASS)
|
|
|
|
if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS )
|
2013-12-05 10:06:06 +01:00
|
|
|
return( ecp_check_pubkey_sw( grp, pt ) );
|
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
|
2013-12-05 10:06:06 +01:00
|
|
|
}
|
2012-11-17 19:54:20 +01:00
|
|
|
|
2012-11-21 10:00:45 +01:00
|
|
|
/*
|
2015-04-08 12:49:31 +02:00
|
|
|
* Check that an mbedtls_mpi is valid as a private key
|
2012-11-21 10:00:45 +01:00
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_ecp_check_privkey( const mbedtls_ecp_group *grp, const mbedtls_mpi *d )
|
2012-11-21 10:00:45 +01:00
|
|
|
{
|
2015-04-09 12:14:31 +02:00
|
|
|
#if defined(ECP_MONTGOMERY)
|
|
|
|
if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY )
|
2013-12-04 11:08:01 +01:00
|
|
|
{
|
2015-06-23 00:18:41 +02:00
|
|
|
/* see [Curve25519] page 5 */
|
2015-04-08 12:49:31 +02:00
|
|
|
if( mbedtls_mpi_get_bit( d, 0 ) != 0 ||
|
|
|
|
mbedtls_mpi_get_bit( d, 1 ) != 0 ||
|
|
|
|
mbedtls_mpi_get_bit( d, 2 ) != 0 ||
|
2015-06-18 16:47:17 +02:00
|
|
|
mbedtls_mpi_bitlen( d ) - 1 != grp->nbits ) /* mbedtls_mpi_bitlen is one-based! */
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_ECP_INVALID_KEY );
|
2013-12-05 10:06:06 +01:00
|
|
|
else
|
|
|
|
return( 0 );
|
2013-12-04 11:08:01 +01:00
|
|
|
}
|
2015-04-09 12:14:31 +02:00
|
|
|
#endif /* ECP_MONTGOMERY */
|
|
|
|
#if defined(ECP_SHORTWEIERSTRASS)
|
|
|
|
if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS )
|
2013-12-04 11:08:01 +01:00
|
|
|
{
|
|
|
|
/* see SEC1 3.2 */
|
2015-04-08 12:49:31 +02:00
|
|
|
if( mbedtls_mpi_cmp_int( d, 1 ) < 0 ||
|
|
|
|
mbedtls_mpi_cmp_mpi( d, &grp->N ) >= 0 )
|
|
|
|
return( MBEDTLS_ERR_ECP_INVALID_KEY );
|
2013-12-05 10:06:06 +01:00
|
|
|
else
|
|
|
|
return( 0 );
|
2013-12-04 11:08:01 +01:00
|
|
|
}
|
2015-04-09 12:14:31 +02:00
|
|
|
#endif /* ECP_SHORTWEIERSTRASS */
|
2012-11-21 10:00:45 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
|
2013-10-23 20:19:57 +02:00
|
|
|
}
|
2012-11-21 10:00:45 +01:00
|
|
|
|
2013-10-23 20:19:57 +02:00
|
|
|
/*
|
2017-04-20 15:37:46 +02:00
|
|
|
* Generate a private key
|
2013-10-23 20:19:57 +02:00
|
|
|
*/
|
2017-04-20 15:37:46 +02:00
|
|
|
int mbedtls_ecp_gen_privkey( const mbedtls_ecp_group *grp,
|
|
|
|
mbedtls_mpi *d,
|
2013-10-23 20:19:57 +02:00
|
|
|
int (*f_rng)(void *, unsigned char *, size_t),
|
|
|
|
void *p_rng )
|
|
|
|
{
|
2017-04-20 15:37:46 +02:00
|
|
|
int ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
2014-06-17 16:39:18 +02:00
|
|
|
size_t n_size = ( grp->nbits + 7 ) / 8;
|
2012-11-21 16:00:55 +01:00
|
|
|
|
2015-04-09 12:14:31 +02:00
|
|
|
#if defined(ECP_MONTGOMERY)
|
|
|
|
if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY )
|
2013-10-23 20:19:57 +02:00
|
|
|
{
|
2013-12-04 18:14:55 +01:00
|
|
|
/* [M225] page 5 */
|
|
|
|
size_t b;
|
|
|
|
|
2016-05-31 15:03:54 +02:00
|
|
|
do {
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( d, n_size, f_rng, p_rng ) );
|
|
|
|
} while( mbedtls_mpi_bitlen( d ) == 0);
|
2012-11-21 10:00:45 +01:00
|
|
|
|
2013-12-04 18:14:55 +01:00
|
|
|
/* Make sure the most significant bit is nbits */
|
2015-06-18 16:47:17 +02:00
|
|
|
b = mbedtls_mpi_bitlen( d ) - 1; /* mbedtls_mpi_bitlen is one-based */
|
2013-12-04 18:14:55 +01:00
|
|
|
if( b > grp->nbits )
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( d, b - grp->nbits ) );
|
2013-12-04 18:14:55 +01:00
|
|
|
else
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, grp->nbits, 1 ) );
|
2012-11-21 10:00:45 +01:00
|
|
|
|
2013-12-04 18:14:55 +01:00
|
|
|
/* Make sure the last three bits are unset */
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, 0, 0 ) );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, 1, 0 ) );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, 2, 0 ) );
|
2013-12-04 18:14:55 +01:00
|
|
|
}
|
2015-04-09 12:14:31 +02:00
|
|
|
#endif /* ECP_MONTGOMERY */
|
2017-04-20 15:37:46 +02:00
|
|
|
|
2015-04-09 12:14:31 +02:00
|
|
|
#if defined(ECP_SHORTWEIERSTRASS)
|
|
|
|
if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS )
|
2013-12-04 18:14:55 +01:00
|
|
|
{
|
|
|
|
/* SEC1 3.2.1: Generate d such that 1 <= n < N */
|
2013-12-05 10:06:06 +01:00
|
|
|
int count = 0;
|
2019-10-11 15:21:53 +02:00
|
|
|
unsigned cmp = 0;
|
2014-01-03 12:35:05 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Match the procedure given in RFC 6979 (deterministic ECDSA):
|
|
|
|
* - use the same byte ordering;
|
|
|
|
* - keep the leftmost nbits bits of the generated octet string;
|
|
|
|
* - try until result is in the desired range.
|
|
|
|
* This also avoids any biais, which is especially important for ECDSA.
|
|
|
|
*/
|
2013-12-04 18:14:55 +01:00
|
|
|
do
|
|
|
|
{
|
2017-10-17 16:19:38 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( d, n_size, f_rng, p_rng ) );
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( d, 8 * n_size - grp->nbits ) );
|
2013-12-04 18:14:55 +01:00
|
|
|
|
2014-01-28 19:30:56 +01:00
|
|
|
/*
|
|
|
|
* Each try has at worst a probability 1/2 of failing (the msb has
|
|
|
|
* a probability 1/2 of being 0, and then the result will be < N),
|
|
|
|
* so after 30 tries failure probability is a most 2**(-30).
|
|
|
|
*
|
|
|
|
* For most curves, 1 try is enough with overwhelming probability,
|
|
|
|
* since N starts with a lot of 1s in binary, but some curves
|
|
|
|
* such as secp224k1 are actually very close to the worst case.
|
|
|
|
*/
|
|
|
|
if( ++count > 30 )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_ECP_RANDOM_FAILED );
|
2019-09-16 15:27:39 +02:00
|
|
|
|
2019-10-11 15:21:53 +02:00
|
|
|
ret = mbedtls_mpi_lt_mpi_ct( d, &grp->N, &cmp );
|
2019-09-16 15:27:39 +02:00
|
|
|
if( ret != 0 )
|
|
|
|
{
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2013-12-04 18:14:55 +01:00
|
|
|
}
|
2019-10-11 15:21:53 +02:00
|
|
|
while( mbedtls_mpi_cmp_int( d, 1 ) < 0 || cmp != 1 );
|
2013-10-23 20:19:57 +02:00
|
|
|
}
|
2015-04-09 12:14:31 +02:00
|
|
|
#endif /* ECP_SHORTWEIERSTRASS */
|
2012-11-21 10:00:45 +01:00
|
|
|
|
2014-01-03 12:54:00 +01:00
|
|
|
cleanup:
|
2017-04-20 15:37:46 +02:00
|
|
|
return( ret );
|
|
|
|
}
|
2014-01-03 12:54:00 +01:00
|
|
|
|
2017-04-20 15:37:46 +02:00
|
|
|
/*
|
|
|
|
* Generate a keypair with configurable base point
|
|
|
|
*/
|
|
|
|
int mbedtls_ecp_gen_keypair_base( mbedtls_ecp_group *grp,
|
|
|
|
const mbedtls_ecp_point *G,
|
|
|
|
mbedtls_mpi *d, mbedtls_ecp_point *Q,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t),
|
|
|
|
void *p_rng )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_ecp_gen_privkey( grp, d, f_rng, p_rng ) );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_ecp_mul( grp, Q, d, G, f_rng, p_rng ) );
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
return( ret );
|
2015-08-11 14:31:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Generate key pair, wrapper for conventional base point
|
|
|
|
*/
|
|
|
|
int mbedtls_ecp_gen_keypair( mbedtls_ecp_group *grp,
|
|
|
|
mbedtls_mpi *d, mbedtls_ecp_point *Q,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t),
|
|
|
|
void *p_rng )
|
|
|
|
{
|
|
|
|
return( mbedtls_ecp_gen_keypair_base( grp, &grp->G, d, Q, f_rng, p_rng ) );
|
2013-10-23 20:19:57 +02:00
|
|
|
}
|
|
|
|
|
2013-11-30 14:13:16 +01:00
|
|
|
/*
|
|
|
|
* Generate a keypair, prettier wrapper
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_ecp_gen_key( mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
|
2013-11-30 14:13:16 +01:00
|
|
|
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2015-05-11 18:46:47 +02:00
|
|
|
if( ( ret = mbedtls_ecp_group_load( &key->grp, grp_id ) ) != 0 )
|
2013-11-30 14:13:16 +01:00
|
|
|
return( ret );
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
return( mbedtls_ecp_gen_keypair( &key->grp, &key->d, &key->Q, f_rng, p_rng ) );
|
2013-11-30 14:13:16 +01:00
|
|
|
}
|
|
|
|
|
2014-11-06 15:25:32 +01:00
|
|
|
/*
|
|
|
|
* Check a public-private key pair
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_ecp_check_pub_priv( const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv )
|
2014-11-06 15:25:32 +01:00
|
|
|
{
|
|
|
|
int ret;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_point Q;
|
|
|
|
mbedtls_ecp_group grp;
|
2014-11-06 15:25:32 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( pub->grp.id == MBEDTLS_ECP_DP_NONE ||
|
2014-11-06 15:25:32 +01:00
|
|
|
pub->grp.id != prv->grp.id ||
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_cmp_mpi( &pub->Q.X, &prv->Q.X ) ||
|
|
|
|
mbedtls_mpi_cmp_mpi( &pub->Q.Y, &prv->Q.Y ) ||
|
|
|
|
mbedtls_mpi_cmp_mpi( &pub->Q.Z, &prv->Q.Z ) )
|
2014-11-06 15:25:32 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
|
2014-11-06 15:25:32 +01:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_point_init( &Q );
|
|
|
|
mbedtls_ecp_group_init( &grp );
|
2014-11-06 15:25:32 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* mbedtls_ecp_mul() needs a non-const group... */
|
|
|
|
mbedtls_ecp_group_copy( &grp, &prv->grp );
|
2014-11-06 15:25:32 +01:00
|
|
|
|
|
|
|
/* Also checks d is valid */
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &Q, &prv->d, &prv->grp.G, NULL, NULL ) );
|
2014-11-06 15:25:32 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( mbedtls_mpi_cmp_mpi( &Q.X, &prv->Q.X ) ||
|
|
|
|
mbedtls_mpi_cmp_mpi( &Q.Y, &prv->Q.Y ) ||
|
|
|
|
mbedtls_mpi_cmp_mpi( &Q.Z, &prv->Q.Z ) )
|
2014-11-06 15:25:32 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
2014-11-06 15:25:32 +01:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_point_free( &Q );
|
|
|
|
mbedtls_ecp_group_free( &grp );
|
2014-11-06 15:25:32 +01:00
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SELF_TEST)
|
2012-10-31 09:26:55 +01:00
|
|
|
|
2020-06-16 12:51:42 +02:00
|
|
|
#if defined(ECP_ONE_STEP_KDF)
|
|
|
|
/*
|
|
|
|
* There are no test vectors from NIST for the One-Step KDF in SP 800-56C,
|
|
|
|
* but unofficial ones can be found at:
|
|
|
|
* https://github.com/patrickfav/singlestep-kdf/wiki/NIST-SP-800-56C-Rev1:-Non-Official-Test-Vectors
|
|
|
|
*
|
|
|
|
* We only use the ones with empty fixedInfo, and for brevity's sake, only
|
2020-06-22 10:18:58 +02:00
|
|
|
* 40-bytes output (with SHA-256 that's more than one block, and with SHA-512
|
2020-06-18 12:14:34 +02:00
|
|
|
* less than one block).
|
2020-06-16 12:51:42 +02:00
|
|
|
*/
|
|
|
|
#if defined(MBEDTLS_SHA512_C)
|
|
|
|
|
|
|
|
static const uint8_t test_kdf_z[16] = {
|
2020-06-22 10:18:58 +02:00
|
|
|
0x3b, 0xa9, 0x79, 0xe9, 0xbc, 0x5e, 0x3e, 0xc7,
|
|
|
|
0x61, 0x30, 0x36, 0xb6, 0xf5, 0x1c, 0xd5, 0xaa,
|
2020-06-16 12:51:42 +02:00
|
|
|
};
|
2020-06-22 10:18:58 +02:00
|
|
|
static const uint8_t test_kdf_out[40] = {
|
|
|
|
0x3e, 0xf6, 0xda, 0xf9, 0x51, 0x60, 0x70, 0x5f,
|
|
|
|
0xdf, 0x21, 0xcd, 0xab, 0xac, 0x25, 0x7b, 0x05,
|
|
|
|
0xfe, 0xc1, 0xab, 0x7c, 0xc9, 0x68, 0x43, 0x25,
|
|
|
|
0x8a, 0xfc, 0x40, 0x6e, 0x5b, 0xf7, 0x98, 0x27,
|
|
|
|
0x10, 0xfa, 0x7b, 0x93, 0x52, 0xd4, 0x16, 0xaa,
|
2020-06-16 12:51:42 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#elif defined(MBEDTLS_SHA256_C)
|
|
|
|
|
|
|
|
static const uint8_t test_kdf_z[16] = {
|
2020-06-22 10:18:58 +02:00
|
|
|
0xc8, 0x3e, 0x35, 0x8e, 0x99, 0xa6, 0x89, 0xc6,
|
|
|
|
0x7d, 0xb4, 0xfe, 0x39, 0xcf, 0x8f, 0x26, 0xe1,
|
2020-06-16 12:51:42 +02:00
|
|
|
};
|
2020-06-22 10:18:58 +02:00
|
|
|
static const uint8_t test_kdf_out[40] = {
|
|
|
|
0x7d, 0xf6, 0x41, 0xf8, 0x3c, 0x47, 0xdc, 0x28,
|
|
|
|
0x5f, 0x7f, 0xaa, 0xde, 0x05, 0x64, 0xd6, 0x25,
|
|
|
|
0x00, 0x6a, 0x47, 0xd9, 0x1e, 0xa4, 0xa0, 0x8c,
|
|
|
|
0xd7, 0xf7, 0x0c, 0x99, 0xaa, 0xa0, 0x72, 0x66,
|
|
|
|
0x69, 0x0e, 0x25, 0xaa, 0xa1, 0x63, 0x14, 0x79,
|
2020-06-16 12:51:42 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int ecp_kdf_self_test( void )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
ecp_drbg_context kdf_ctx;
|
|
|
|
mbedtls_mpi scalar;
|
|
|
|
uint8_t out[sizeof( test_kdf_out )];
|
|
|
|
|
|
|
|
ecp_drbg_init( &kdf_ctx );
|
|
|
|
mbedtls_mpi_init( &scalar );
|
|
|
|
memset( out, 0, sizeof( out ) );
|
|
|
|
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &scalar,
|
|
|
|
test_kdf_z, sizeof( test_kdf_z ) ) );
|
|
|
|
|
|
|
|
MBEDTLS_MPI_CHK( ecp_drbg_seed( &kdf_ctx,
|
|
|
|
&scalar, sizeof( test_kdf_z ) ) );
|
|
|
|
|
|
|
|
MBEDTLS_MPI_CHK( ecp_drbg_random( &kdf_ctx, out, sizeof( out ) ) );
|
|
|
|
|
|
|
|
if( memcmp( out, test_kdf_out, sizeof( out ) ) != 0 )
|
|
|
|
ret = -1;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
ecp_drbg_free( &kdf_ctx );
|
|
|
|
mbedtls_mpi_free( &scalar );
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
#endif /* ECP_ONE_STEP_KDF */
|
|
|
|
|
2012-10-31 09:26:55 +01:00
|
|
|
/*
|
|
|
|
* Checkup routine
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_ecp_self_test( int verbose )
|
2012-10-31 09:26:55 +01:00
|
|
|
{
|
2012-11-13 20:57:00 +01:00
|
|
|
int ret;
|
|
|
|
size_t i;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group grp;
|
|
|
|
mbedtls_ecp_point R, P;
|
|
|
|
mbedtls_mpi m;
|
2013-11-21 20:23:55 +01:00
|
|
|
unsigned long add_c_prev, dbl_c_prev, mul_c_prev;
|
2013-10-10 15:40:49 +02:00
|
|
|
/* exponents especially adapted for secp192r1 */
|
2013-06-25 16:25:17 +02:00
|
|
|
const char *exponents[] =
|
2012-11-13 20:57:00 +01:00
|
|
|
{
|
2012-11-21 13:00:58 +01:00
|
|
|
"000000000000000000000000000000000000000000000001", /* one */
|
2013-11-21 09:28:03 +01:00
|
|
|
"FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22830", /* N - 1 */
|
2012-11-21 13:00:58 +01:00
|
|
|
"5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
|
2013-11-21 09:28:03 +01:00
|
|
|
"400000000000000000000000000000000000000000000000", /* one and zeros */
|
|
|
|
"7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", /* all ones */
|
|
|
|
"555555555555555555555555555555555555555555555555", /* 101010... */
|
2012-11-13 20:57:00 +01:00
|
|
|
};
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group_init( &grp );
|
|
|
|
mbedtls_ecp_point_init( &R );
|
|
|
|
mbedtls_ecp_point_init( &P );
|
|
|
|
mbedtls_mpi_init( &m );
|
2012-11-13 20:57:00 +01:00
|
|
|
|
2013-10-10 15:40:49 +02:00
|
|
|
/* Use secp192r1 if available, or any available curve */
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
|
2015-05-11 18:46:47 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &grp, MBEDTLS_ECP_DP_SECP192R1 ) );
|
2013-06-29 23:26:34 +02:00
|
|
|
#else
|
2015-05-11 18:46:47 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &grp, mbedtls_ecp_curve_list()->grp_id ) );
|
2013-10-10 15:40:49 +02:00
|
|
|
#endif
|
2012-11-13 20:57:00 +01:00
|
|
|
|
|
|
|
if( verbose != 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_printf( " ECP test #1 (constant op_count, base point G): " );
|
2013-09-17 19:13:10 +02:00
|
|
|
|
|
|
|
/* Do a dummy multiplication first to trigger precomputation */
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &m, 2 ) );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &P, &m, &grp.G, NULL, NULL ) );
|
2012-11-13 20:57:00 +01:00
|
|
|
|
|
|
|
add_count = 0;
|
|
|
|
dbl_count = 0;
|
2013-11-21 20:23:55 +01:00
|
|
|
mul_count = 0;
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[0] ) );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
|
2012-11-13 20:57:00 +01:00
|
|
|
|
|
|
|
for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
|
|
|
|
{
|
|
|
|
add_c_prev = add_count;
|
|
|
|
dbl_c_prev = dbl_count;
|
2013-11-21 20:23:55 +01:00
|
|
|
mul_c_prev = mul_count;
|
2012-11-13 20:57:00 +01:00
|
|
|
add_count = 0;
|
|
|
|
dbl_count = 0;
|
2013-11-21 20:23:55 +01:00
|
|
|
mul_count = 0;
|
2012-11-13 20:57:00 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[i] ) );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
|
2012-11-13 20:57:00 +01:00
|
|
|
|
2013-11-21 20:23:55 +01:00
|
|
|
if( add_count != add_c_prev ||
|
|
|
|
dbl_count != dbl_c_prev ||
|
|
|
|
mul_count != mul_c_prev )
|
2012-11-13 20:57:00 +01:00
|
|
|
{
|
|
|
|
if( verbose != 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_printf( "failed (%u)\n", (unsigned int) i );
|
2012-11-13 20:57:00 +01:00
|
|
|
|
|
|
|
ret = 1;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( verbose != 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_printf( "passed\n" );
|
2012-11-13 20:57:00 +01:00
|
|
|
|
2013-09-17 19:13:10 +02:00
|
|
|
if( verbose != 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_printf( " ECP test #2 (constant op_count, other point): " );
|
2013-09-17 19:13:10 +02:00
|
|
|
/* We computed P = 2G last time, use it */
|
|
|
|
|
|
|
|
add_count = 0;
|
|
|
|
dbl_count = 0;
|
2013-11-21 20:23:55 +01:00
|
|
|
mul_count = 0;
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[0] ) );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
|
2013-09-17 19:13:10 +02:00
|
|
|
|
|
|
|
for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
|
|
|
|
{
|
|
|
|
add_c_prev = add_count;
|
|
|
|
dbl_c_prev = dbl_count;
|
2013-11-21 20:23:55 +01:00
|
|
|
mul_c_prev = mul_count;
|
2013-09-17 19:13:10 +02:00
|
|
|
add_count = 0;
|
|
|
|
dbl_count = 0;
|
2013-11-21 20:23:55 +01:00
|
|
|
mul_count = 0;
|
2013-09-17 19:13:10 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[i] ) );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
|
2013-09-17 19:13:10 +02:00
|
|
|
|
2013-11-21 20:23:55 +01:00
|
|
|
if( add_count != add_c_prev ||
|
|
|
|
dbl_count != dbl_c_prev ||
|
|
|
|
mul_count != mul_c_prev )
|
2013-09-17 19:13:10 +02:00
|
|
|
{
|
|
|
|
if( verbose != 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_printf( "failed (%u)\n", (unsigned int) i );
|
2013-09-17 19:13:10 +02:00
|
|
|
|
|
|
|
ret = 1;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( verbose != 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_printf( "passed\n" );
|
2013-09-17 19:13:10 +02:00
|
|
|
|
2020-06-16 12:51:42 +02:00
|
|
|
#if defined(ECP_ONE_STEP_KDF)
|
|
|
|
if( verbose != 0 )
|
|
|
|
mbedtls_printf( " ECP test #3 (internal KDF): " );
|
|
|
|
|
|
|
|
ret = ecp_kdf_self_test();
|
|
|
|
if( ret != 0 )
|
|
|
|
{
|
|
|
|
if( verbose != 0 )
|
|
|
|
mbedtls_printf( "failed\n" );
|
|
|
|
|
|
|
|
ret = 1;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( verbose != 0 )
|
|
|
|
mbedtls_printf( "passed\n" );
|
|
|
|
#endif /* ECP_ONE_STEP_KDF */
|
|
|
|
|
2012-11-13 20:57:00 +01:00
|
|
|
cleanup:
|
|
|
|
|
|
|
|
if( ret < 0 && verbose != 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_printf( "Unexpected error, return code = %08X\n", ret );
|
2012-11-13 20:57:00 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group_free( &grp );
|
|
|
|
mbedtls_ecp_point_free( &R );
|
|
|
|
mbedtls_ecp_point_free( &P );
|
|
|
|
mbedtls_mpi_free( &m );
|
2012-11-13 20:57:00 +01:00
|
|
|
|
|
|
|
if( verbose != 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_printf( "\n" );
|
2012-11-13 20:57:00 +01:00
|
|
|
|
|
|
|
return( ret );
|
2012-10-31 09:26:55 +01:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_SELF_TEST */
|
2012-10-31 09:26:55 +01:00
|
|
|
|
2016-08-18 13:38:46 +02:00
|
|
|
#endif /* !MBEDTLS_ECP_ALT */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_ECP_C */
|