ECP: Add module and function level replacement options.
diff --git a/include/mbedtls/alt_internal/ecp_function_alt.h b/include/mbedtls/alt_internal/ecp_function_alt.h
new file mode 100644
index 0000000..e1cca10
--- /dev/null
+++ b/include/mbedtls/alt_internal/ecp_function_alt.h
@@ -0,0 +1,82 @@
+/**
+ * \file alt_func_internal.h
+ *
+ * \brief Function declarations for alternate implementation.
+ *
+ * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
+ * SPDX-License-Identifier: Apache-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.
+ *
+ * This file is part of mbed TLS (https://tls.mbed.org)
+ */
+#ifndef MBEDTLS_ECP_FUNCTION_ALT_H
+#define MBEDTLS_ECP_FUNCTION_ALT_H
+
+
+#if defined(MBEDTLS_ECP_FUNCTION_ALT)
+
+unsigned char ecp_alt_grp_capable( const mbedtls_ecp_group *grp );
+
+#if defined(MBEDTLS_ECP_ALT_INIT)
+int ecp_alt_init( const mbedtls_ecp_group *grp );
+#endif
+
+#if defined(MBEDTLS_ECP_ALT_DEINIT)
+void ecp_alt_deinit( const mbedtls_ecp_group *grp );
+#endif
+
+#if defined(MBEDTLS_ECP_RANDOMIZE_JAC_ALT)
+int ecp_randomize_jac_alt( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
+ int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
+#endif
+
+#if defined(MBEDTLS_ECP_ADD_MIXED_ALT)
+int ecp_add_mixed_alt( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
+ const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q );
+#endif
+
+#if defined(MBEDTLS_ECP_DOUBLE_JAC_ALT)
+int ecp_double_jac_alt( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
+ const mbedtls_ecp_point *P );
+#endif
+
+#if defined(MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT)
+int ecp_normalize_jac_many_alt( const mbedtls_ecp_group *grp,
+ mbedtls_ecp_point *T[], size_t t_len );
+#endif
+
+#if defined(MBEDTLS_ECP_NORMALIZE_JAC_ALT)
+int ecp_normalize_jac_alt( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt );
+#endif
+
+#if defined(MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT)
+int ecp_double_add_mxz_alt( 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 );
+#endif
+
+#if defined(MBEDTLS_ECP_RANDOMIZE_MXZ_ALT)
+int ecp_randomize_mxz_alt( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P,
+ int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
+#endif
+
+#if defined(MBEDTLS_ECP_NORMALIZE_MXZ_ALT)
+int ecp_normalize_mxz_alt( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P );
+#endif
+
+#endif // MBEDTLS_ECP_FUNCTION_ALT
+
+#endif /* ecp_function_alt.h */
+
diff --git a/include/mbedtls/check_config.h b/include/mbedtls/check_config.h
index fe86c1e..8495ef2 100644
--- a/include/mbedtls/check_config.h
+++ b/include/mbedtls/check_config.h
@@ -150,6 +150,46 @@
#error "MBEDTLS_GCM_C defined, but not all prerequisites"
#endif
+#if defined(MBEDTLS_ECP_ALT_INIT) && !defined(MBEDTLS_ECP_FUNCTION_ALT)
+#error "MBEDTLS_ECP_ALT_INIT defined, but not all prerequisites"
+#endif
+
+#if defined(MBEDTLS_ECP_ALT_DEINIT) && !defined(MBEDTLS_ECP_FUNCTION_ALT)
+#error "MBEDTLS_ECP_ALT_DEINIT defined, but not all prerequisites"
+#endif
+
+#if defined(MBEDTLS_ECP_RANDOMIZE_JAC_ALT) && !defined(MBEDTLS_ECP_FUNCTION_ALT)
+#error "MBEDTLS_ECP_RANDOMIZE_JAC_ALT defined, but not all prerequisites"
+#endif
+
+#if defined(MBEDTLS_ECP_ADD_MIXED_ALT) && !defined(MBEDTLS_ECP_FUNCTION_ALT)
+#error "MBEDTLS_ECP_ADD_MIXED_ALT defined, but not all prerequisites"
+#endif
+
+#if defined(MBEDTLS_ECP_DOUBLE_JAC_ALT) && !defined(MBEDTLS_ECP_FUNCTION_ALT)
+#error "MBEDTLS_ECP_DOUBLE_JAC_ALT defined, but not all prerequisites"
+#endif
+
+#if defined(MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT) && !defined(MBEDTLS_ECP_FUNCTION_ALT)
+#error "MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT defined, but not all prerequisites"
+#endif
+
+#if defined(MBEDTLS_ECP_NORMALIZE_JAC_ALT) && !defined(MBEDTLS_ECP_FUNCTION_ALT)
+#error "MBEDTLS_ECP_NORMALIZE_JAC_ALT defined, but not all prerequisites"
+#endif
+
+#if defined(MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT) && !defined(MBEDTLS_ECP_FUNCTION_ALT)
+#error "MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT defined, but not all prerequisites"
+#endif
+
+#if defined(MBEDTLS_ECP_RANDOMIZE_MXZ_ALT) && !defined(MBEDTLS_ECP_FUNCTION_ALT)
+#error "MBEDTLS_ECP_RANDOMIZE_MXZ_ALT defined, but not all prerequisites"
+#endif
+
+#if defined(MBEDTLS_ECP_NORMALIZE_MXZ_ALT) && !defined(MBEDTLS_ECP_FUNCTION_ALT)
+#error "MBEDTLS_ECP_NORMALIZE_MXZ_ALT defined, but not all prerequisites"
+#endif
+
#if defined(MBEDTLS_HAVEGE_C) && !defined(MBEDTLS_TIMING_C)
#error "MBEDTLS_HAVEGE_C defined, but not all prerequisites"
#endif
diff --git a/include/mbedtls/config.h b/include/mbedtls/config.h
index 0f7e29b..a6976dc 100644
--- a/include/mbedtls/config.h
+++ b/include/mbedtls/config.h
@@ -218,9 +218,9 @@
* \def MBEDTLS_AES_ALT
*
* MBEDTLS__MODULE_NAME__ALT: Uncomment a macro to let mbed TLS use your
- * alternate core implementation of a symmetric crypto or hash module (e.g.
- * platform specific assembly optimized implementations). Keep in mind that
- * the function prototypes should remain the same.
+ * alternate core implementation of a symmetric crypto, an arithmetic or hash
+ * module (e.g. platform specific assembly optimized implementations). Keep
+ * in mind that the function prototypes should remain the same.
*
* This replaces the whole module. If you only want to replace one of the
* functions, use one of the MBEDTLS__FUNCTION_NAME__ALT flags.
@@ -246,6 +246,14 @@
//#define MBEDTLS_SHA1_ALT
//#define MBEDTLS_SHA256_ALT
//#define MBEDTLS_SHA512_ALT
+/*
+ * When replacing the elliptic curve module, pleace consider, that it is
+ * implemented with two .c files:
+ * - ecp.c
+ * - ecp_curves.c
+ * Please make sure that you provide functionality for both of them.
+ */
+//#define MBEDTLS_ECP_ALT
/**
* \def MBEDTLS_MD2_PROCESS_ALT
@@ -286,6 +294,60 @@
//#define MBEDTLS_AES_DECRYPT_ALT
/**
+ * \def MBEDTLS_ECP_FUNCTION_ALT
+ *
+ * MBEDTLS_ECP__FUNCTION_NAME__ALT: Uncomment a macro to let mbed TLS use your
+ * alternate core implementation of elliptic curve arithmetic. Keep in mind that
+ * function prototypes should remain the same.
+ *
+ * This partially replaces one function. The header file from mbed TLS is still
+ * used, in contrast to the MBEDTLS_ECP_ALT flag. The original implementation
+ * is still present and it is used for group structures not supported by the
+ * alternative.
+ *
+ * Any of these options become available by turning MBEDTLS_ECP_FUNCTION_ALT and
+ * implementing the following function:
+ * unsigned char ecp_alt_grp_capable( const mbedtls_ecp_group *grp )
+ * This should return 1 if the replacement functions implement arithmetic for
+ * the given group and 0 otherwise.
+ *
+ * The functions
+ * int ecp_alt_init( const mbedtls_ecp_group *grp )
+ * void ecp_alt_deinit( const mbedtls_ecp_group *grp )
+ * can be turned on by MBEDTLS_ECP_ALT_INIT and MBEDTLS_ECP_ALT_DEINIT.
+ * They are called before and after each point operation and provide an
+ * opportunity to implement optimized set up and tear down instructions.
+ *
+ * Example: In case you uncomment MBEDTLS_ECP_FUNCTION_ALT and
+ * MBEDTLS_ECP_DOUBLE_JAC, mbed TLS will still provide the ecp_double_jac
+ * function, but will use your ecp_double_jac_alt if the group is supported
+ * (your ecp_alt_grp_capable function returns 1 when receives it as an
+ * argument). If the group is not supported then the original implementation is
+ * used. The other functions and the definition of mbedtls_ecp_group and
+ * mbedtls_ecp_point will not change, so your implementation of
+ * ecp_double_jac_alt and ecp_alt_grp_capable must be compatible with
+ * this definition.
+ *
+ * Uncomment a macro to enable alternate implementation of the corresponding
+ * function.
+ */
+/* Required for all the functions in this section */
+//#define MBEDTLS_ECP_FUNCTION_ALT
+/* Utility functions for setup and cleanup */
+//#define MBEDTLS_ECP_ALT_INIT
+//#define MBEDTLS_ECP_ALT_DEINIT
+/* Support for Weierstrass curves with Jacobi representation */
+//#define MBEDTLS_ECP_RANDOMIZE_JAC_ALT
+//#define MBEDTLS_ECP_ADD_MIXED_ALT
+//#define MBEDTLS_ECP_DOUBLE_JAC_ALT
+//#define MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT
+//#define MBEDTLS_ECP_NORMALIZE_JAC_ALT
+/* Support for curves with Montgomery arithmetic */
+//#define MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT
+//#define MBEDTLS_ECP_RANDOMIZE_MXZ_ALT
+//#define MBEDTLS_ECP_NORMALIZE_MXZ_ALT
+
+/**
* \def MBEDTLS_TEST_NULL_ENTROPY
*
* Enables testing and use of mbed TLS without any configured entropy sources.
diff --git a/include/mbedtls/ecp.h b/include/mbedtls/ecp.h
index 5246c78..d75f038 100644
--- a/include/mbedtls/ecp.h
+++ b/include/mbedtls/ecp.h
@@ -37,6 +37,10 @@
#define MBEDTLS_ERR_ECP_INVALID_KEY -0x4C80 /**< Invalid private or public key. */
#define MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH -0x4C00 /**< Signature is valid but shorter than the user-supplied length. */
+#if !defined(MBEDTLS_ECP_ALT)
+// Regular implementation
+//
+
#ifdef __cplusplus
extern "C" {
#endif
@@ -654,16 +658,22 @@
int mbedtls_ecp_check_pub_priv( const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv );
#if defined(MBEDTLS_SELF_TEST)
+
/**
* \brief Checkup routine
*
* \return 0 if successful, or 1 if a test failed
*/
int mbedtls_ecp_self_test( int verbose );
-#endif
+
+#endif // MBEDTLS_SELF_TEST
#ifdef __cplusplus
}
#endif
+#else /* MBEDTLS_ECP_ALT */
+#include "ecp_alt.h"
+#endif /* MBEDTLS_ECP_ALT */
+
#endif /* ecp.h */