From 9684d4dc58a413d46caf9190edec2497130f71d0 Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Mon, 16 Jan 2023 16:50:11 +0100 Subject: [PATCH 01/22] Add quasi-reduction function for ecp Signed-off-by: Gabor Mezei --- library/ecp.c | 16 ++++++++++++++++ library/ecp_internal.h | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 library/ecp_internal.h diff --git a/library/ecp.c b/library/ecp.c index d9d5425ed..835bfa3bc 100644 --- a/library/ecp.c +++ b/library/ecp.c @@ -79,6 +79,8 @@ #include "bn_mul.h" #include "ecp_invasive.h" +#include "ecp_internal.h" +#include "bignum_core.h" #include @@ -1029,6 +1031,20 @@ cleanup: return ret; } +int mbedtls_ecp_quasi_reduction(mbedtls_mpi_uint *X, + const mbedtls_mpi_mod_modulus *N) +{ + if (N->limbs == 0) { + return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; + } + + mbedtls_mpi_uint c = mbedtls_mpi_core_sub(X, X, N->p, N->limbs); + + (void) mbedtls_mpi_core_add_if(X, N->p, N->limbs, (unsigned) c); + + return 0; +} + /* * Fast mod-p functions expect their argument to be in the 0..p^2 range. * diff --git a/library/ecp_internal.h b/library/ecp_internal.h new file mode 100644 index 000000000..07bf3861a --- /dev/null +++ b/library/ecp_internal.h @@ -0,0 +1,34 @@ +/** + * \file ecp_internal.h + * + * \brief Function declarations for internal functions of elliptic curve + * point arithmetic. + */ +/** + * Copyright The Mbed TLS Contributors + * 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. + */ + +#ifndef MBEDTLS_ECP_INTERNAL_H +#define MBEDTLS_ECP_INTERNAL_H + +#include "common.h" +#include "mbedtls/bignum.h" +#include "bignum_mod.h" + +int mbedtls_ecp_quasi_reduction(mbedtls_mpi_uint *X, + const mbedtls_mpi_mod_modulus *N); + +#endif /* MBEDTLS_ECP_INTERNAL_H */ From 65fc9f78d49caa69c57c4f2074a4067937028599 Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Mon, 16 Jan 2023 16:51:12 +0100 Subject: [PATCH 02/22] Add tests for ecp quasi-reduction Signed-off-by: Gabor Mezei --- tests/suites/test_suite_ecp.data | 2 + tests/suites/test_suite_ecp.function | 93 +++++++++++++++++++++++++++- 2 files changed, 93 insertions(+), 2 deletions(-) diff --git a/tests/suites/test_suite_ecp.data b/tests/suites/test_suite_ecp.data index 93112002e..bbc8ea242 100644 --- a/tests/suites/test_suite_ecp.data +++ b/tests/suites/test_suite_ecp.data @@ -1039,3 +1039,5 @@ ECP check order for CURVE448 depends_on:MBEDTLS_ECP_DP_CURVE448_ENABLED ecp_check_order:MBEDTLS_ECP_DP_CURVE448:"3fffffffffffffffffffffffffffffffffffffffffffffffffffffff7cca23e9c44edb49aed63690216cc2728dc58f552378c292ab5844f3" +ECP quasi-reduction: uninitialized modulus +ecp_quasi_reduction_neg:"11":"12":"1" diff --git a/tests/suites/test_suite_ecp.function b/tests/suites/test_suite_ecp.function index 394253de5..f5b581158 100644 --- a/tests/suites/test_suite_ecp.function +++ b/tests/suites/test_suite_ecp.function @@ -2,8 +2,7 @@ #include "mbedtls/ecp.h" #include "mbedtls/ecdsa.h" #include "mbedtls/ecdh.h" - -#include "ecp_invasive.h" +#include "ecp_internal.h" #if defined(MBEDTLS_TEST_HOOKS) && \ (defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || \ @@ -1295,3 +1294,93 @@ exit: mbedtls_mpi_free(&expected_n); } /* END_CASE */ + +/* BEGIN_CASE */ +void ecp_quasi_reduction(char *input_N, + char *input_A, + char *result) +{ + mbedtls_mpi_uint *A = NULL; + mbedtls_mpi_uint *N = NULL; + mbedtls_mpi_uint *res = NULL; + size_t limbs_A; + size_t limbs_N; + size_t limbs_res; + + mbedtls_mpi_mod_modulus m; + mbedtls_mpi_mod_modulus_init(&m); + + TEST_EQUAL(mbedtls_test_read_mpi_core(&A, &limbs_A, input_A), 0); + TEST_EQUAL(mbedtls_test_read_mpi_core(&N, &limbs_N, input_N), 0); + TEST_EQUAL(mbedtls_test_read_mpi_core(&res, &limbs_res, result), 0); + + size_t limbs = limbs_N; + size_t bytes = limbs * sizeof(mbedtls_mpi_uint); + + TEST_EQUAL(limbs_A, limbs); + TEST_EQUAL(limbs_res, limbs); + + TEST_EQUAL(mbedtls_mpi_mod_modulus_setup( + &m, N, limbs, + MBEDTLS_MPI_MOD_REP_MONTGOMERY), 0); + + TEST_EQUAL(mbedtls_ecp_quasi_reduction(A, &m), 0); + ASSERT_COMPARE(A, bytes, res, bytes); + +exit: + mbedtls_free(A); + mbedtls_free(res); + + mbedtls_mpi_mod_modulus_free(&m); + mbedtls_free(N); +} +/* END_CASE */ + +/* BEGIN_CASE */ +void ecp_quasi_reduction_neg(char *input_N, + char *input_A, + char *result) +{ + mbedtls_mpi_uint *A = NULL; + mbedtls_mpi_uint *N = NULL; + mbedtls_mpi_uint *res = NULL; + size_t limbs_A; + size_t limbs_N; + size_t limbs_res; + + mbedtls_mpi_mod_modulus m; + mbedtls_mpi_mod_modulus_init(&m); + + mbedtls_mpi_mod_modulus fake_m; + mbedtls_mpi_mod_modulus_init(&fake_m); + + TEST_EQUAL(mbedtls_test_read_mpi_core(&A, &limbs_A, input_A), 0); + TEST_EQUAL(mbedtls_test_read_mpi_core(&N, &limbs_N, input_N), 0); + TEST_EQUAL(mbedtls_test_read_mpi_core(&res, &limbs_res, result), 0); + + size_t limbs = limbs_N; + size_t bytes = limbs * sizeof(mbedtls_mpi_uint); + + TEST_EQUAL(limbs_A, limbs); + TEST_EQUAL(limbs_res, limbs); + + TEST_EQUAL(mbedtls_mpi_mod_modulus_setup( + &m, N, limbs, + MBEDTLS_MPI_MOD_REP_MONTGOMERY), 0); + + TEST_EQUAL(mbedtls_ecp_quasi_reduction(A, &m), 0); + ASSERT_COMPARE(A, bytes, res, bytes); + + /* Check when m is not initialized */ + TEST_EQUAL(mbedtls_ecp_quasi_reduction(A, &fake_m), + MBEDTLS_ERR_ECP_BAD_INPUT_DATA); + +exit: + mbedtls_free(A); + mbedtls_free(res); + + mbedtls_mpi_mod_modulus_free(&fake_m); + mbedtls_mpi_mod_modulus_free(&m); + mbedtls_free(N); +} +/* END_CASE */ From 308132f641997391968d3f2d3f593080e0fbfbe9 Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Mon, 16 Jan 2023 16:53:29 +0100 Subject: [PATCH 03/22] Add test generation support for the ecp module Signed-off-by: Gabor Mezei --- scripts/mbedtls_dev/ecp.py | 26 ++++++++++ scripts/mbedtls_dev/ecp_common.py | 23 +++++++++ tests/CMakeLists.txt | 16 ++++++ tests/Makefile | 22 ++++++++- tests/scripts/check-generated-files.sh | 1 + tests/scripts/generate_ecp_tests.py | 68 ++++++++++++++++++++++++++ 6 files changed, 154 insertions(+), 2 deletions(-) create mode 100644 scripts/mbedtls_dev/ecp.py create mode 100644 scripts/mbedtls_dev/ecp_common.py create mode 100755 tests/scripts/generate_ecp_tests.py diff --git a/scripts/mbedtls_dev/ecp.py b/scripts/mbedtls_dev/ecp.py new file mode 100644 index 000000000..59a0f31d2 --- /dev/null +++ b/scripts/mbedtls_dev/ecp.py @@ -0,0 +1,26 @@ +"""Framework classes for generation of ecp test cases.""" +# Copyright The Mbed TLS Contributors +# 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. + +from typing import List + +from . import test_case +from . import test_data_generation +from . import ecp_common + +class EcpTarget(test_data_generation.BaseTarget): + #pylint: disable=abstract-method, too-few-public-methods + """Target for ecp test case generation.""" + target_basename = 'test_suite_ecp.generated' diff --git a/scripts/mbedtls_dev/ecp_common.py b/scripts/mbedtls_dev/ecp_common.py new file mode 100644 index 000000000..1f2ba0aae --- /dev/null +++ b/scripts/mbedtls_dev/ecp_common.py @@ -0,0 +1,23 @@ +"""Common features for ecp in test generation framework.""" +# Copyright The Mbed TLS Contributors +# 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. + +from . import test_case +from . import test_data_generation +from . import bignum_common + +class EcpOperationCommon(bignum_common.ModOperationCommon): + """Target for ecp test case generation.""" + pass \ No newline at end of file diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 4a7de820a..8276bba65 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -29,6 +29,18 @@ execute_process( string(REGEX REPLACE "[^;]*/" "" base_bignum_generated_data_files "${base_bignum_generated_data_files}") +execute_process( + COMMAND + ${MBEDTLS_PYTHON_EXECUTABLE} + ${CMAKE_CURRENT_SOURCE_DIR}/../tests/scripts/generate_ecp_tests.py + --list-for-cmake + WORKING_DIRECTORY + ${CMAKE_CURRENT_SOURCE_DIR}/.. + OUTPUT_VARIABLE + base_ecp_generated_data_files) +string(REGEX REPLACE "[^;]*/" "" + base_ecp_generated_data_files "${base_ecp_generated_data_files}") + execute_process( COMMAND ${MBEDTLS_PYTHON_EXECUTABLE} @@ -52,6 +64,9 @@ set(psa_generated_data_files "") foreach(file ${base_bignum_generated_data_files}) list(APPEND bignum_generated_data_files ${CMAKE_CURRENT_BINARY_DIR}/suites/${file}) endforeach() +foreach(file ${base_ecp_generated_data_files}) + list(APPEND ecp_generated_data_files ${CMAKE_CURRENT_BINARY_DIR}/suites/${file}) +endforeach() foreach(file ${base_psa_generated_data_files}) list(APPEND psa_generated_data_files ${CMAKE_CURRENT_BINARY_DIR}/suites/${file}) endforeach() @@ -72,6 +87,7 @@ if(GEN_FILES) ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/mbedtls_dev/bignum_core.py ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/mbedtls_dev/bignum_mod_raw.py ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/mbedtls_dev/bignum_mod.py + ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/mbedtls_dev/ecp.py ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/mbedtls_dev/test_case.py ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/mbedtls_dev/test_data_generation.py ) diff --git a/tests/Makefile b/tests/Makefile index f0373381e..fa5ec4926 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -73,6 +73,13 @@ GENERATED_BIGNUM_DATA_FILES := $(patsubst tests/%,%,$(shell \ ifeq ($(GENERATED_BIGNUM_DATA_FILES),FAILED) $(error "$(PYTHON) scripts/generate_bignum_tests.py --list" failed) endif +GENERATED_ECP_DATA_FILES := $(patsubst tests/%,%,$(shell \ + $(PYTHON) scripts/generate_ecp_tests.py --list || \ + echo FAILED \ +)) +ifeq ($(GENERATED_ECP_DATA_FILES),FAILED) +$(error "$(PYTHON) scripts/generate_ecp_tests.py --list" failed) +endif GENERATED_PSA_DATA_FILES := $(patsubst tests/%,%,$(shell \ $(PYTHON) scripts/generate_psa_tests.py --list || \ echo FAILED \ @@ -80,7 +87,7 @@ GENERATED_PSA_DATA_FILES := $(patsubst tests/%,%,$(shell \ ifeq ($(GENERATED_PSA_DATA_FILES),FAILED) $(error "$(PYTHON) scripts/generate_psa_tests.py --list" failed) endif -GENERATED_FILES := $(GENERATED_PSA_DATA_FILES) $(GENERATED_BIGNUM_DATA_FILES) +GENERATED_FILES := $(GENERATED_PSA_DATA_FILES) $(GENERATED_ECP_DATA_FILES) $(GENERATED_BIGNUM_DATA_FILES) generated_files: $(GENERATED_FILES) # generate_bignum_tests.py and generate_psa_tests.py spend more time analyzing @@ -89,7 +96,7 @@ generated_files: $(GENERATED_FILES) # It's rare not to want all the outputs. So always generate all of its outputs. # Use an intermediate phony dependency so that parallel builds don't run # a separate instance of the recipe for each output file. -.SECONDARY: generated_bignum_test_data generated_psa_test_data +.SECONDARY: generated_bignum_test_data generated_ecp_test_data generated_psa_test_data $(GENERATED_BIGNUM_DATA_FILES): generated_bignum_test_data generated_bignum_test_data: scripts/generate_bignum_tests.py generated_bignum_test_data: ../scripts/mbedtls_dev/bignum_common.py @@ -102,6 +109,17 @@ generated_bignum_test_data: echo " Gen $(GENERATED_BIGNUM_DATA_FILES)" $(PYTHON) scripts/generate_bignum_tests.py +$(GENERATED_ECP_DATA_FILES): generated_ecp_test_data +generated_ecp_test_data: scripts/generate_ecp_tests.py +generated_ecp_test_data: ../scripts/mbedtls_dev/bignum_common.py +generated_ecp_test_data: ../scripts/mbedtls_dev/ecp_common.py +generated_ecp_test_data: ../scripts/mbedtls_dev/ecp.py +generated_ecp_test_data: ../scripts/mbedtls_dev/test_case.py +generated_ecp_test_data: ../scripts/mbedtls_dev/test_data_generation.py +generated_ecp_test_data: + echo " Gen $(GENERATED_ECP_DATA_FILES)" + $(PYTHON) scripts/generate_ecp_tests.py + $(GENERATED_PSA_DATA_FILES): generated_psa_test_data generated_psa_test_data: scripts/generate_psa_tests.py generated_psa_test_data: ../scripts/mbedtls_dev/crypto_knowledge.py diff --git a/tests/scripts/check-generated-files.sh b/tests/scripts/check-generated-files.sh index 2bb9fea7c..4d6f93079 100755 --- a/tests/scripts/check-generated-files.sh +++ b/tests/scripts/check-generated-files.sh @@ -137,4 +137,5 @@ check scripts/generate_ssl_debug_helpers.py library/ssl_debug_helpers_generated. check scripts/generate_visualc_files.pl visualc/VS2013 check scripts/generate_psa_constants.py programs/psa/psa_constant_names_generated.c check tests/scripts/generate_bignum_tests.py $(tests/scripts/generate_bignum_tests.py --list) +check tests/scripts/generate_ecp_tests.py $(tests/scripts/generate_ecp_tests.py --list) check tests/scripts/generate_psa_tests.py $(tests/scripts/generate_psa_tests.py --list) diff --git a/tests/scripts/generate_ecp_tests.py b/tests/scripts/generate_ecp_tests.py new file mode 100755 index 000000000..89186c131 --- /dev/null +++ b/tests/scripts/generate_ecp_tests.py @@ -0,0 +1,68 @@ +#!/usr/bin/env python3 +"""Generate test data for ecp functions. + +With no arguments, generate all test data. With non-option arguments, +generate only the specified files. + +Class structure: + +Child classes of test_data_generation.BaseTarget (file targets) represent an output +file. These indicate where test cases will be written to, for all subclasses of +this target. Multiple file targets should not reuse a `target_basename`. + +Each subclass derived from a file target can either be: + - A concrete class, representing a test function, which generates test cases. + - An abstract class containing shared methods and attributes, not associated + with a test function. + +Both concrete and abstract subclasses can be derived from, to implement +additional test cases (see BignumCmp and BignumCmpAbs for examples of deriving +from abstract and concrete classes). + + +Adding test case generation for a function: + +A subclass representing the test function should be added, deriving from a +file target such as BignumTarget. This test class must set/implement the +following: + - test_function: the function name from the associated .function file. + - test_name: a descriptive name or brief summary to refer to the test + function. + - arguments(): a method to generate the list of arguments required for the + test_function. + - generate_function_tests(): a method to generate TestCases for the function. + This should create instances of the class with required input data, and + call `.create_test_case()` to yield the TestCase. + +Additional details and other attributes/methods are given in the documentation +of BaseTarget in test_data_generation.py. +""" + +# Copyright The Mbed TLS Contributors +# 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. + +import sys + +import scripts_path # pylint: disable=unused-import +from mbedtls_dev import test_data_generation +from mbedtls_dev import ecp_common +# Import modules containing additional test classes +# Test function classes in these modules will be registered by +# the framework +from mbedtls_dev import ecp # pylint: disable=unused-import + +if __name__ == '__main__': + # Use the section of the docstring relevant to the CLI as description + test_data_generation.main(sys.argv[1:], "\n".join(__doc__.splitlines()[:4])) From 3c6f89b46acc705dd6cfffdf34ee270365b4bcc0 Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Mon, 16 Jan 2023 16:54:48 +0100 Subject: [PATCH 04/22] Add generated test for ecp quasi-reduction Signed-off-by: Gabor Mezei --- scripts/mbedtls_dev/ecp.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/scripts/mbedtls_dev/ecp.py b/scripts/mbedtls_dev/ecp.py index 59a0f31d2..713dd96df 100644 --- a/scripts/mbedtls_dev/ecp.py +++ b/scripts/mbedtls_dev/ecp.py @@ -24,3 +24,30 @@ class EcpTarget(test_data_generation.BaseTarget): #pylint: disable=abstract-method, too-few-public-methods """Target for ecp test case generation.""" target_basename = 'test_suite_ecp.generated' + +class EcpQuasiReduction(ecp_common.EcpOperationCommon, + EcpTarget): + """Test cases for ecp quasi_reduction().""" + symbol = "-" + test_function = "ecp_quasi_reduction" + test_name = "mbedtls_ecp_quasi_reduction" + input_style = "fixed" + arity = 1 + + # Extend the default values with n < x < 2n + input_values = ecp_common.EcpOperationCommon.input_values + [ + "73", + "ebeddd7b4fefae8755bbfb9c181a73347096b3ec70d1a021", + ("1f4e1d074d0b50e8d8818f9a9e5df9959f902bb955fd24fd3d791175226ad8c1" + "fcb6d59fa41a3dcb25412009e5e356eb65b50ca67782285290420b45b32f0d63" + "7c9ee549a52ad8d631ba4945435c9aec77227ec59faff878b71b920a3d631929" + "d636c9a409d6ffdcd95e2568e128596811fb9ade15e69f6efd509381ebbf3599") + ] # type: List[str] + + def result(self) -> List[str]: + result = self.int_a % self.int_n + return [self.format_result(result)] + + @property + def is_valid(self) -> bool: + return bool(self.int_a < 2 * self.int_n) From c83f792c18163346f9ffbed892a87c6368698013 Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Tue, 17 Jan 2023 13:28:06 +0100 Subject: [PATCH 05/22] Add documentation Signed-off-by: Gabor Mezei --- library/ecp_internal.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/library/ecp_internal.h b/library/ecp_internal.h index 07bf3861a..b028225e6 100644 --- a/library/ecp_internal.h +++ b/library/ecp_internal.h @@ -28,6 +28,18 @@ #include "mbedtls/bignum.h" #include "bignum_mod.h" +/** Convert an MPI to its canonical representative. + * + * \note Currently handles the case when `N->int_rep` is + * MBEDTLS_MPI_MOD_REP_MONTGOMERY. + * + * \param[in,out] X The address of the MPI to be converted. Must have the + * same number of limbs as \p N. + * \param[in] N The address of the modulus. + * + * \return \c 0 if successful. + * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p N is invalid. + */ int mbedtls_ecp_quasi_reduction(mbedtls_mpi_uint *X, const mbedtls_mpi_mod_modulus *N); From aec3eea0645bbebdc4395152b899bab8e0f1222e Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Tue, 17 Jan 2023 16:34:24 +0100 Subject: [PATCH 06/22] Fix pylint issues Signed-off-by: Gabor Mezei --- scripts/mbedtls_dev/ecp.py | 15 +++++++-------- scripts/mbedtls_dev/ecp_common.py | 4 +--- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/scripts/mbedtls_dev/ecp.py b/scripts/mbedtls_dev/ecp.py index 713dd96df..f9d6af51d 100644 --- a/scripts/mbedtls_dev/ecp.py +++ b/scripts/mbedtls_dev/ecp.py @@ -16,7 +16,6 @@ from typing import List -from . import test_case from . import test_data_generation from . import ecp_common @@ -36,13 +35,13 @@ class EcpQuasiReduction(ecp_common.EcpOperationCommon, # Extend the default values with n < x < 2n input_values = ecp_common.EcpOperationCommon.input_values + [ - "73", - "ebeddd7b4fefae8755bbfb9c181a73347096b3ec70d1a021", - ("1f4e1d074d0b50e8d8818f9a9e5df9959f902bb955fd24fd3d791175226ad8c1" - "fcb6d59fa41a3dcb25412009e5e356eb65b50ca67782285290420b45b32f0d63" - "7c9ee549a52ad8d631ba4945435c9aec77227ec59faff878b71b920a3d631929" - "d636c9a409d6ffdcd95e2568e128596811fb9ade15e69f6efd509381ebbf3599") - ] # type: List[str] + "73", + "ebeddd7b4fefae8755bbfb9c181a73347096b3ec70d1a021", + ("1f4e1d074d0b50e8d8818f9a9e5df9959f902bb955fd24fd3d791175226ad8c1" + "fcb6d59fa41a3dcb25412009e5e356eb65b50ca67782285290420b45b32f0d63" + "7c9ee549a52ad8d631ba4945435c9aec77227ec59faff878b71b920a3d631929" + "d636c9a409d6ffdcd95e2568e128596811fb9ade15e69f6efd509381ebbf3599") + ] # type: List[str] def result(self) -> List[str]: result = self.int_a % self.int_n diff --git a/scripts/mbedtls_dev/ecp_common.py b/scripts/mbedtls_dev/ecp_common.py index 1f2ba0aae..5b10a5dfc 100644 --- a/scripts/mbedtls_dev/ecp_common.py +++ b/scripts/mbedtls_dev/ecp_common.py @@ -14,10 +14,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -from . import test_case -from . import test_data_generation from . import bignum_common class EcpOperationCommon(bignum_common.ModOperationCommon): """Target for ecp test case generation.""" - pass \ No newline at end of file + pass From a38db2a55bd8383d78ef20d3a2a2190c8563984e Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Tue, 17 Jan 2023 16:34:49 +0100 Subject: [PATCH 07/22] Add missing inlcude Signed-off-by: Gabor Mezei --- tests/suites/test_suite_ecp.function | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/suites/test_suite_ecp.function b/tests/suites/test_suite_ecp.function index f5b581158..eb1f37554 100644 --- a/tests/suites/test_suite_ecp.function +++ b/tests/suites/test_suite_ecp.function @@ -2,6 +2,7 @@ #include "mbedtls/ecp.h" #include "mbedtls/ecdsa.h" #include "mbedtls/ecdh.h" +#include "ecp_invasive.h" #include "ecp_internal.h" #if defined(MBEDTLS_TEST_HOOKS) && \ From 7e14c66c4dc9f49f64ae9680a6b30350e9dc98aa Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Wed, 18 Jan 2023 10:56:13 +0100 Subject: [PATCH 08/22] Fix lint issues Signed-off-by: Gabor Mezei --- scripts/mbedtls_dev/ecp_common.py | 1 + tests/scripts/generate_ecp_tests.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/mbedtls_dev/ecp_common.py b/scripts/mbedtls_dev/ecp_common.py index 5b10a5dfc..311b9121c 100644 --- a/scripts/mbedtls_dev/ecp_common.py +++ b/scripts/mbedtls_dev/ecp_common.py @@ -17,5 +17,6 @@ from . import bignum_common class EcpOperationCommon(bignum_common.ModOperationCommon): + #pylint: disable=abstract-method """Target for ecp test case generation.""" pass diff --git a/tests/scripts/generate_ecp_tests.py b/tests/scripts/generate_ecp_tests.py index 89186c131..73434d970 100755 --- a/tests/scripts/generate_ecp_tests.py +++ b/tests/scripts/generate_ecp_tests.py @@ -57,7 +57,7 @@ import sys import scripts_path # pylint: disable=unused-import from mbedtls_dev import test_data_generation -from mbedtls_dev import ecp_common + # Import modules containing additional test classes # Test function classes in these modules will be registered by # the framework From aaa1d2a276cc78de112f1c38477614fa1e718eca Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Mon, 23 Jan 2023 16:13:43 +0100 Subject: [PATCH 09/22] Move the quasi reduction fixing function to bignum_mod_raw Rename the function to 'fix_quasi_reduction' to better suite its functionality. Also changed the name prefix to suite for the new module. Signed-off-by: Gabor Mezei --- library/bignum_mod_raw.c | 15 +++ ...p_internal.h => bignum_mod_raw_invasive.h} | 17 ++-- library/ecp.c | 15 --- scripts/mbedtls_dev/bignum_mod_raw.py | 27 ++++++ scripts/mbedtls_dev/ecp.py | 52 ----------- scripts/mbedtls_dev/ecp_common.py | 22 ----- tests/CMakeLists.txt | 16 ---- tests/Makefile | 22 +---- tests/scripts/check-generated-files.sh | 1 - tests/scripts/generate_ecp_tests.py | 68 -------------- tests/suites/test_suite_bignum_mod_raw.data | 3 + .../suites/test_suite_bignum_mod_raw.function | 92 +++++++++++++++++++ tests/suites/test_suite_ecp.data | 3 - tests/suites/test_suite_ecp.function | 92 +------------------ 14 files changed, 149 insertions(+), 296 deletions(-) rename library/{ecp_internal.h => bignum_mod_raw_invasive.h} (73%) delete mode 100644 scripts/mbedtls_dev/ecp.py delete mode 100644 scripts/mbedtls_dev/ecp_common.py delete mode 100755 tests/scripts/generate_ecp_tests.py diff --git a/library/bignum_mod_raw.c b/library/bignum_mod_raw.c index aa2bd46cc..b23218200 100644 --- a/library/bignum_mod_raw.c +++ b/library/bignum_mod_raw.c @@ -128,6 +128,21 @@ void mbedtls_mpi_mod_raw_mul(mbedtls_mpi_uint *X, N->rep.mont.mm, T); } +MBEDTLS_STATIC_TESTABLE +int mbedtls_mpi_mod_raw_fix_quasi_reduction(mbedtls_mpi_uint *X, + const mbedtls_mpi_mod_modulus *N) +{ + if (N->limbs == 0) { + return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; + } + + mbedtls_mpi_uint c = mbedtls_mpi_core_sub(X, X, N->p, N->limbs); + + (void) mbedtls_mpi_core_add_if(X, N->p, N->limbs, (unsigned) c); + + return 0; +} + /* END MERGE SLOT 2 */ /* BEGIN MERGE SLOT 3 */ diff --git a/library/ecp_internal.h b/library/bignum_mod_raw_invasive.h similarity index 73% rename from library/ecp_internal.h rename to library/bignum_mod_raw_invasive.h index b028225e6..3d4afedff 100644 --- a/library/ecp_internal.h +++ b/library/bignum_mod_raw_invasive.h @@ -1,8 +1,8 @@ /** - * \file ecp_internal.h + * \file bignum_mod_raw_invasive.h * - * \brief Function declarations for internal functions of elliptic curve - * point arithmetic. + * \brief Function declarations for invasive functions of Low-level + * modular bignum. */ /** * Copyright The Mbed TLS Contributors @@ -21,8 +21,8 @@ * limitations under the License. */ -#ifndef MBEDTLS_ECP_INTERNAL_H -#define MBEDTLS_ECP_INTERNAL_H +#ifndef MBEDTLS_BIGNUM_MOD_RAW_INVASIVE_H +#define MBEDTLS_BIGNUM_MOD_RAW_INVASIVE_H #include "common.h" #include "mbedtls/bignum.h" @@ -40,7 +40,8 @@ * \return \c 0 if successful. * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p N is invalid. */ -int mbedtls_ecp_quasi_reduction(mbedtls_mpi_uint *X, - const mbedtls_mpi_mod_modulus *N); +MBEDTLS_STATIC_TESTABLE +int mbedtls_mpi_mod_raw_fix_quasi_reduction(mbedtls_mpi_uint *X, + const mbedtls_mpi_mod_modulus *N); -#endif /* MBEDTLS_ECP_INTERNAL_H */ +#endif /* MBEDTLS_BIGNUM_MOD_RAW_INVASIVE_H */ diff --git a/library/ecp.c b/library/ecp.c index 835bfa3bc..6f53c1bdc 100644 --- a/library/ecp.c +++ b/library/ecp.c @@ -79,7 +79,6 @@ #include "bn_mul.h" #include "ecp_invasive.h" -#include "ecp_internal.h" #include "bignum_core.h" #include @@ -1031,20 +1030,6 @@ cleanup: return ret; } -int mbedtls_ecp_quasi_reduction(mbedtls_mpi_uint *X, - const mbedtls_mpi_mod_modulus *N) -{ - if (N->limbs == 0) { - return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; - } - - mbedtls_mpi_uint c = mbedtls_mpi_core_sub(X, X, N->p, N->limbs); - - (void) mbedtls_mpi_core_add_if(X, N->p, N->limbs, (unsigned) c); - - return 0; -} - /* * Fast mod-p functions expect their argument to be in the 0..p^2 range. * diff --git a/scripts/mbedtls_dev/bignum_mod_raw.py b/scripts/mbedtls_dev/bignum_mod_raw.py index f9d9899f6..5645685a5 100644 --- a/scripts/mbedtls_dev/bignum_mod_raw.py +++ b/scripts/mbedtls_dev/bignum_mod_raw.py @@ -51,6 +51,33 @@ class BignumModRawSub(bignum_common.ModOperationCommon, result = (self.int_a - self.int_b) % self.int_n return [self.format_result(result)] +class BignumModRawFixQuasiReduction(bignum_common.ModOperationCommon, + BignumModRawTarget): + """Test cases for ecp quasi_reduction().""" + symbol = "-" + test_function = "mpi_mod_raw_fix_quasi_reduction" + test_name = "mbedtls_mpi_mod_raw_fix_quasi_reduction" + input_style = "fixed" + arity = 1 + + # Extend the default values with n < x < 2n + input_values = bignum_common.ModOperationCommon.input_values + [ + "73", + "ebeddd7b4fefae8755bbfb9c181a73347096b3ec70d1a021", + ("1f4e1d074d0b50e8d8818f9a9e5df9959f902bb955fd24fd3d791175226ad8c1" + "fcb6d59fa41a3dcb25412009e5e356eb65b50ca67782285290420b45b32f0d63" + "7c9ee549a52ad8d631ba4945435c9aec77227ec59faff878b71b920a3d631929" + "d636c9a409d6ffdcd95e2568e128596811fb9ade15e69f6efd509381ebbf3599") + ] # type: List[str] + + def result(self) -> List[str]: + result = self.int_a % self.int_n + return [self.format_result(result)] + + @property + def is_valid(self) -> bool: + return bool(self.int_a < 2 * self.int_n) + class BignumModRawMul(bignum_common.ModOperationCommon, BignumModRawTarget): """Test cases for bignum mpi_mod_raw_mul().""" diff --git a/scripts/mbedtls_dev/ecp.py b/scripts/mbedtls_dev/ecp.py deleted file mode 100644 index f9d6af51d..000000000 --- a/scripts/mbedtls_dev/ecp.py +++ /dev/null @@ -1,52 +0,0 @@ -"""Framework classes for generation of ecp test cases.""" -# Copyright The Mbed TLS Contributors -# 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. - -from typing import List - -from . import test_data_generation -from . import ecp_common - -class EcpTarget(test_data_generation.BaseTarget): - #pylint: disable=abstract-method, too-few-public-methods - """Target for ecp test case generation.""" - target_basename = 'test_suite_ecp.generated' - -class EcpQuasiReduction(ecp_common.EcpOperationCommon, - EcpTarget): - """Test cases for ecp quasi_reduction().""" - symbol = "-" - test_function = "ecp_quasi_reduction" - test_name = "mbedtls_ecp_quasi_reduction" - input_style = "fixed" - arity = 1 - - # Extend the default values with n < x < 2n - input_values = ecp_common.EcpOperationCommon.input_values + [ - "73", - "ebeddd7b4fefae8755bbfb9c181a73347096b3ec70d1a021", - ("1f4e1d074d0b50e8d8818f9a9e5df9959f902bb955fd24fd3d791175226ad8c1" - "fcb6d59fa41a3dcb25412009e5e356eb65b50ca67782285290420b45b32f0d63" - "7c9ee549a52ad8d631ba4945435c9aec77227ec59faff878b71b920a3d631929" - "d636c9a409d6ffdcd95e2568e128596811fb9ade15e69f6efd509381ebbf3599") - ] # type: List[str] - - def result(self) -> List[str]: - result = self.int_a % self.int_n - return [self.format_result(result)] - - @property - def is_valid(self) -> bool: - return bool(self.int_a < 2 * self.int_n) diff --git a/scripts/mbedtls_dev/ecp_common.py b/scripts/mbedtls_dev/ecp_common.py deleted file mode 100644 index 311b9121c..000000000 --- a/scripts/mbedtls_dev/ecp_common.py +++ /dev/null @@ -1,22 +0,0 @@ -"""Common features for ecp in test generation framework.""" -# Copyright The Mbed TLS Contributors -# 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. - -from . import bignum_common - -class EcpOperationCommon(bignum_common.ModOperationCommon): - #pylint: disable=abstract-method - """Target for ecp test case generation.""" - pass diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 8276bba65..4a7de820a 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -29,18 +29,6 @@ execute_process( string(REGEX REPLACE "[^;]*/" "" base_bignum_generated_data_files "${base_bignum_generated_data_files}") -execute_process( - COMMAND - ${MBEDTLS_PYTHON_EXECUTABLE} - ${CMAKE_CURRENT_SOURCE_DIR}/../tests/scripts/generate_ecp_tests.py - --list-for-cmake - WORKING_DIRECTORY - ${CMAKE_CURRENT_SOURCE_DIR}/.. - OUTPUT_VARIABLE - base_ecp_generated_data_files) -string(REGEX REPLACE "[^;]*/" "" - base_ecp_generated_data_files "${base_ecp_generated_data_files}") - execute_process( COMMAND ${MBEDTLS_PYTHON_EXECUTABLE} @@ -64,9 +52,6 @@ set(psa_generated_data_files "") foreach(file ${base_bignum_generated_data_files}) list(APPEND bignum_generated_data_files ${CMAKE_CURRENT_BINARY_DIR}/suites/${file}) endforeach() -foreach(file ${base_ecp_generated_data_files}) - list(APPEND ecp_generated_data_files ${CMAKE_CURRENT_BINARY_DIR}/suites/${file}) -endforeach() foreach(file ${base_psa_generated_data_files}) list(APPEND psa_generated_data_files ${CMAKE_CURRENT_BINARY_DIR}/suites/${file}) endforeach() @@ -87,7 +72,6 @@ if(GEN_FILES) ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/mbedtls_dev/bignum_core.py ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/mbedtls_dev/bignum_mod_raw.py ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/mbedtls_dev/bignum_mod.py - ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/mbedtls_dev/ecp.py ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/mbedtls_dev/test_case.py ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/mbedtls_dev/test_data_generation.py ) diff --git a/tests/Makefile b/tests/Makefile index fa5ec4926..f0373381e 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -73,13 +73,6 @@ GENERATED_BIGNUM_DATA_FILES := $(patsubst tests/%,%,$(shell \ ifeq ($(GENERATED_BIGNUM_DATA_FILES),FAILED) $(error "$(PYTHON) scripts/generate_bignum_tests.py --list" failed) endif -GENERATED_ECP_DATA_FILES := $(patsubst tests/%,%,$(shell \ - $(PYTHON) scripts/generate_ecp_tests.py --list || \ - echo FAILED \ -)) -ifeq ($(GENERATED_ECP_DATA_FILES),FAILED) -$(error "$(PYTHON) scripts/generate_ecp_tests.py --list" failed) -endif GENERATED_PSA_DATA_FILES := $(patsubst tests/%,%,$(shell \ $(PYTHON) scripts/generate_psa_tests.py --list || \ echo FAILED \ @@ -87,7 +80,7 @@ GENERATED_PSA_DATA_FILES := $(patsubst tests/%,%,$(shell \ ifeq ($(GENERATED_PSA_DATA_FILES),FAILED) $(error "$(PYTHON) scripts/generate_psa_tests.py --list" failed) endif -GENERATED_FILES := $(GENERATED_PSA_DATA_FILES) $(GENERATED_ECP_DATA_FILES) $(GENERATED_BIGNUM_DATA_FILES) +GENERATED_FILES := $(GENERATED_PSA_DATA_FILES) $(GENERATED_BIGNUM_DATA_FILES) generated_files: $(GENERATED_FILES) # generate_bignum_tests.py and generate_psa_tests.py spend more time analyzing @@ -96,7 +89,7 @@ generated_files: $(GENERATED_FILES) # It's rare not to want all the outputs. So always generate all of its outputs. # Use an intermediate phony dependency so that parallel builds don't run # a separate instance of the recipe for each output file. -.SECONDARY: generated_bignum_test_data generated_ecp_test_data generated_psa_test_data +.SECONDARY: generated_bignum_test_data generated_psa_test_data $(GENERATED_BIGNUM_DATA_FILES): generated_bignum_test_data generated_bignum_test_data: scripts/generate_bignum_tests.py generated_bignum_test_data: ../scripts/mbedtls_dev/bignum_common.py @@ -109,17 +102,6 @@ generated_bignum_test_data: echo " Gen $(GENERATED_BIGNUM_DATA_FILES)" $(PYTHON) scripts/generate_bignum_tests.py -$(GENERATED_ECP_DATA_FILES): generated_ecp_test_data -generated_ecp_test_data: scripts/generate_ecp_tests.py -generated_ecp_test_data: ../scripts/mbedtls_dev/bignum_common.py -generated_ecp_test_data: ../scripts/mbedtls_dev/ecp_common.py -generated_ecp_test_data: ../scripts/mbedtls_dev/ecp.py -generated_ecp_test_data: ../scripts/mbedtls_dev/test_case.py -generated_ecp_test_data: ../scripts/mbedtls_dev/test_data_generation.py -generated_ecp_test_data: - echo " Gen $(GENERATED_ECP_DATA_FILES)" - $(PYTHON) scripts/generate_ecp_tests.py - $(GENERATED_PSA_DATA_FILES): generated_psa_test_data generated_psa_test_data: scripts/generate_psa_tests.py generated_psa_test_data: ../scripts/mbedtls_dev/crypto_knowledge.py diff --git a/tests/scripts/check-generated-files.sh b/tests/scripts/check-generated-files.sh index 4d6f93079..2bb9fea7c 100755 --- a/tests/scripts/check-generated-files.sh +++ b/tests/scripts/check-generated-files.sh @@ -137,5 +137,4 @@ check scripts/generate_ssl_debug_helpers.py library/ssl_debug_helpers_generated. check scripts/generate_visualc_files.pl visualc/VS2013 check scripts/generate_psa_constants.py programs/psa/psa_constant_names_generated.c check tests/scripts/generate_bignum_tests.py $(tests/scripts/generate_bignum_tests.py --list) -check tests/scripts/generate_ecp_tests.py $(tests/scripts/generate_ecp_tests.py --list) check tests/scripts/generate_psa_tests.py $(tests/scripts/generate_psa_tests.py --list) diff --git a/tests/scripts/generate_ecp_tests.py b/tests/scripts/generate_ecp_tests.py deleted file mode 100755 index 73434d970..000000000 --- a/tests/scripts/generate_ecp_tests.py +++ /dev/null @@ -1,68 +0,0 @@ -#!/usr/bin/env python3 -"""Generate test data for ecp functions. - -With no arguments, generate all test data. With non-option arguments, -generate only the specified files. - -Class structure: - -Child classes of test_data_generation.BaseTarget (file targets) represent an output -file. These indicate where test cases will be written to, for all subclasses of -this target. Multiple file targets should not reuse a `target_basename`. - -Each subclass derived from a file target can either be: - - A concrete class, representing a test function, which generates test cases. - - An abstract class containing shared methods and attributes, not associated - with a test function. - -Both concrete and abstract subclasses can be derived from, to implement -additional test cases (see BignumCmp and BignumCmpAbs for examples of deriving -from abstract and concrete classes). - - -Adding test case generation for a function: - -A subclass representing the test function should be added, deriving from a -file target such as BignumTarget. This test class must set/implement the -following: - - test_function: the function name from the associated .function file. - - test_name: a descriptive name or brief summary to refer to the test - function. - - arguments(): a method to generate the list of arguments required for the - test_function. - - generate_function_tests(): a method to generate TestCases for the function. - This should create instances of the class with required input data, and - call `.create_test_case()` to yield the TestCase. - -Additional details and other attributes/methods are given in the documentation -of BaseTarget in test_data_generation.py. -""" - -# Copyright The Mbed TLS Contributors -# 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. - -import sys - -import scripts_path # pylint: disable=unused-import -from mbedtls_dev import test_data_generation - -# Import modules containing additional test classes -# Test function classes in these modules will be registered by -# the framework -from mbedtls_dev import ecp # pylint: disable=unused-import - -if __name__ == '__main__': - # Use the section of the docstring relevant to the CLI as description - test_data_generation.main(sys.argv[1:], "\n".join(__doc__.splitlines()[:4])) diff --git a/tests/suites/test_suite_bignum_mod_raw.data b/tests/suites/test_suite_bignum_mod_raw.data index 8cbd918f8..2b9b1edb4 100644 --- a/tests/suites/test_suite_bignum_mod_raw.data +++ b/tests/suites/test_suite_bignum_mod_raw.data @@ -179,6 +179,9 @@ mpi_mod_raw_cond_swap:"000000001111111122222222333333334444444455555555666666667 mbedtls_mpi_mod_raw_cond_swap: copy half of the limbs mpi_mod_raw_cond_swap:"00000000FFFFFFFF55555555AAAAAAAA":"FEDCBA9876543210FEDCBA9876543210":8 +Bignum mod raw quasi-reduction: uninitialized modulus +mpi_mod_raw_fix_quasi_reduction_neg:"11":"12":"1" + # BEGIN MERGE SLOT 1 # END MERGE SLOT 1 diff --git a/tests/suites/test_suite_bignum_mod_raw.function b/tests/suites/test_suite_bignum_mod_raw.function index 9310b0e65..2c4a0e66c 100644 --- a/tests/suites/test_suite_bignum_mod_raw.function +++ b/tests/suites/test_suite_bignum_mod_raw.function @@ -6,6 +6,8 @@ #include "constant_time_internal.h" #include "test/constant_flow.h" +#include "bignum_mod_raw_invasive.h" + /* END_HEADER */ /* BEGIN_DEPENDENCIES @@ -338,6 +340,96 @@ exit: } /* END_CASE */ +/* BEGIN_CASE */ +void mpi_mod_raw_fix_quasi_reduction(char *input_N, + char *input_A, + char *result) +{ + mbedtls_mpi_uint *A = NULL; + mbedtls_mpi_uint *N = NULL; + mbedtls_mpi_uint *res = NULL; + size_t limbs_A; + size_t limbs_N; + size_t limbs_res; + + mbedtls_mpi_mod_modulus m; + mbedtls_mpi_mod_modulus_init(&m); + + TEST_EQUAL(mbedtls_test_read_mpi_core(&A, &limbs_A, input_A), 0); + TEST_EQUAL(mbedtls_test_read_mpi_core(&N, &limbs_N, input_N), 0); + TEST_EQUAL(mbedtls_test_read_mpi_core(&res, &limbs_res, result), 0); + + size_t limbs = limbs_N; + size_t bytes = limbs * sizeof(mbedtls_mpi_uint); + + TEST_EQUAL(limbs_A, limbs); + TEST_EQUAL(limbs_res, limbs); + + TEST_EQUAL(mbedtls_mpi_mod_modulus_setup( + &m, N, limbs, + MBEDTLS_MPI_MOD_REP_MONTGOMERY), 0); + + TEST_EQUAL(mbedtls_mpi_mod_raw_fix_quasi_reduction(A, &m), 0); + ASSERT_COMPARE(A, bytes, res, bytes); + +exit: + mbedtls_free(A); + mbedtls_free(res); + + mbedtls_mpi_mod_modulus_free(&m); + mbedtls_free(N); +} +/* END_CASE */ + +/* BEGIN_CASE */ +void mpi_mod_raw_fix_quasi_reduction_neg(char *input_N, + char *input_A, + char *result) +{ + mbedtls_mpi_uint *A = NULL; + mbedtls_mpi_uint *N = NULL; + mbedtls_mpi_uint *res = NULL; + size_t limbs_A; + size_t limbs_N; + size_t limbs_res; + + mbedtls_mpi_mod_modulus m; + mbedtls_mpi_mod_modulus_init(&m); + + mbedtls_mpi_mod_modulus fake_m; + mbedtls_mpi_mod_modulus_init(&fake_m); + + TEST_EQUAL(mbedtls_test_read_mpi_core(&A, &limbs_A, input_A), 0); + TEST_EQUAL(mbedtls_test_read_mpi_core(&N, &limbs_N, input_N), 0); + TEST_EQUAL(mbedtls_test_read_mpi_core(&res, &limbs_res, result), 0); + + size_t limbs = limbs_N; + size_t bytes = limbs * sizeof(mbedtls_mpi_uint); + + TEST_EQUAL(limbs_A, limbs); + TEST_EQUAL(limbs_res, limbs); + + TEST_EQUAL(mbedtls_mpi_mod_modulus_setup( + &m, N, limbs, + MBEDTLS_MPI_MOD_REP_MONTGOMERY), 0); + + TEST_EQUAL(mbedtls_mpi_mod_raw_fix_quasi_reduction(A, &m), 0); + ASSERT_COMPARE(A, bytes, res, bytes); + + /* Check when m is not initialized */ + TEST_EQUAL(mbedtls_mpi_mod_raw_fix_quasi_reduction(A, &fake_m), + MBEDTLS_ERR_MPI_BAD_INPUT_DATA); + +exit: + mbedtls_free(A); + mbedtls_free(res); + + mbedtls_mpi_mod_modulus_free(&fake_m); + mbedtls_mpi_mod_modulus_free(&m); + mbedtls_free(N); +} +/* END_CASE */ + /* BEGIN_CASE */ void mpi_mod_raw_mul(char *input_A, char *input_B, diff --git a/tests/suites/test_suite_ecp.data b/tests/suites/test_suite_ecp.data index bbc8ea242..9a1379389 100644 --- a/tests/suites/test_suite_ecp.data +++ b/tests/suites/test_suite_ecp.data @@ -1038,6 +1038,3 @@ ecp_check_order:MBEDTLS_ECP_DP_SECP256K1:"fffffffffffffffffffffffffffffffebaaedc ECP check order for CURVE448 depends_on:MBEDTLS_ECP_DP_CURVE448_ENABLED ecp_check_order:MBEDTLS_ECP_DP_CURVE448:"3fffffffffffffffffffffffffffffffffffffffffffffffffffffff7cca23e9c44edb49aed63690216cc2728dc58f552378c292ab5844f3" - -ECP quasi-reduction: uninitialized modulus -ecp_quasi_reduction_neg:"11":"12":"1" diff --git a/tests/suites/test_suite_ecp.function b/tests/suites/test_suite_ecp.function index eb1f37554..394253de5 100644 --- a/tests/suites/test_suite_ecp.function +++ b/tests/suites/test_suite_ecp.function @@ -2,8 +2,8 @@ #include "mbedtls/ecp.h" #include "mbedtls/ecdsa.h" #include "mbedtls/ecdh.h" + #include "ecp_invasive.h" -#include "ecp_internal.h" #if defined(MBEDTLS_TEST_HOOKS) && \ (defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || \ @@ -1295,93 +1295,3 @@ exit: mbedtls_mpi_free(&expected_n); } /* END_CASE */ - -/* BEGIN_CASE */ -void ecp_quasi_reduction(char *input_N, - char *input_A, - char *result) -{ - mbedtls_mpi_uint *A = NULL; - mbedtls_mpi_uint *N = NULL; - mbedtls_mpi_uint *res = NULL; - size_t limbs_A; - size_t limbs_N; - size_t limbs_res; - - mbedtls_mpi_mod_modulus m; - mbedtls_mpi_mod_modulus_init(&m); - - TEST_EQUAL(mbedtls_test_read_mpi_core(&A, &limbs_A, input_A), 0); - TEST_EQUAL(mbedtls_test_read_mpi_core(&N, &limbs_N, input_N), 0); - TEST_EQUAL(mbedtls_test_read_mpi_core(&res, &limbs_res, result), 0); - - size_t limbs = limbs_N; - size_t bytes = limbs * sizeof(mbedtls_mpi_uint); - - TEST_EQUAL(limbs_A, limbs); - TEST_EQUAL(limbs_res, limbs); - - TEST_EQUAL(mbedtls_mpi_mod_modulus_setup( - &m, N, limbs, - MBEDTLS_MPI_MOD_REP_MONTGOMERY), 0); - - TEST_EQUAL(mbedtls_ecp_quasi_reduction(A, &m), 0); - ASSERT_COMPARE(A, bytes, res, bytes); - -exit: - mbedtls_free(A); - mbedtls_free(res); - - mbedtls_mpi_mod_modulus_free(&m); - mbedtls_free(N); -} -/* END_CASE */ - -/* BEGIN_CASE */ -void ecp_quasi_reduction_neg(char *input_N, - char *input_A, - char *result) -{ - mbedtls_mpi_uint *A = NULL; - mbedtls_mpi_uint *N = NULL; - mbedtls_mpi_uint *res = NULL; - size_t limbs_A; - size_t limbs_N; - size_t limbs_res; - - mbedtls_mpi_mod_modulus m; - mbedtls_mpi_mod_modulus_init(&m); - - mbedtls_mpi_mod_modulus fake_m; - mbedtls_mpi_mod_modulus_init(&fake_m); - - TEST_EQUAL(mbedtls_test_read_mpi_core(&A, &limbs_A, input_A), 0); - TEST_EQUAL(mbedtls_test_read_mpi_core(&N, &limbs_N, input_N), 0); - TEST_EQUAL(mbedtls_test_read_mpi_core(&res, &limbs_res, result), 0); - - size_t limbs = limbs_N; - size_t bytes = limbs * sizeof(mbedtls_mpi_uint); - - TEST_EQUAL(limbs_A, limbs); - TEST_EQUAL(limbs_res, limbs); - - TEST_EQUAL(mbedtls_mpi_mod_modulus_setup( - &m, N, limbs, - MBEDTLS_MPI_MOD_REP_MONTGOMERY), 0); - - TEST_EQUAL(mbedtls_ecp_quasi_reduction(A, &m), 0); - ASSERT_COMPARE(A, bytes, res, bytes); - - /* Check when m is not initialized */ - TEST_EQUAL(mbedtls_ecp_quasi_reduction(A, &fake_m), - MBEDTLS_ERR_ECP_BAD_INPUT_DATA); - -exit: - mbedtls_free(A); - mbedtls_free(res); - - mbedtls_mpi_mod_modulus_free(&fake_m); - mbedtls_mpi_mod_modulus_free(&m); - mbedtls_free(N); -} -/* END_CASE */ From e81a2b85c91a660ed7fc8322fdad8f57016a6e20 Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Mon, 23 Jan 2023 18:56:38 +0100 Subject: [PATCH 10/22] Change the fix_quasi_reduction function to static Signed-off-by: Gabor Mezei --- library/bignum_mod_raw_invasive.h | 4 ++++ tests/suites/test_suite_bignum_mod_raw.function | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/library/bignum_mod_raw_invasive.h b/library/bignum_mod_raw_invasive.h index 3d4afedff..7770c0a95 100644 --- a/library/bignum_mod_raw_invasive.h +++ b/library/bignum_mod_raw_invasive.h @@ -28,6 +28,8 @@ #include "mbedtls/bignum.h" #include "bignum_mod.h" +#if defined(MBEDTLS_TEST_HOOKS) + /** Convert an MPI to its canonical representative. * * \note Currently handles the case when `N->int_rep` is @@ -44,4 +46,6 @@ MBEDTLS_STATIC_TESTABLE int mbedtls_mpi_mod_raw_fix_quasi_reduction(mbedtls_mpi_uint *X, const mbedtls_mpi_mod_modulus *N); +#endif /* MBEDTLS_TEST_HOOKS */ + #endif /* MBEDTLS_BIGNUM_MOD_RAW_INVASIVE_H */ diff --git a/tests/suites/test_suite_bignum_mod_raw.function b/tests/suites/test_suite_bignum_mod_raw.function index 2c4a0e66c..d59cddecd 100644 --- a/tests/suites/test_suite_bignum_mod_raw.function +++ b/tests/suites/test_suite_bignum_mod_raw.function @@ -340,7 +340,7 @@ exit: } /* END_CASE */ -/* BEGIN_CASE */ +/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */ void mpi_mod_raw_fix_quasi_reduction(char *input_N, char *input_A, char *result) @@ -381,7 +381,7 @@ exit: } /* END_CASE */ -/* BEGIN_CASE */ +/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */ void mpi_mod_raw_fix_quasi_reduction_neg(char *input_N, char *input_A, char *result) From 9073f7dd3b71d7a27c9112de2871f3a98173fb5d Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Mon, 23 Jan 2023 19:05:37 +0100 Subject: [PATCH 11/22] Remove unneeded check The fix_quasi_reduction function changed to static so checking the invalid arguments are not needed anymore. Signed-off-by: Gabor Mezei --- library/bignum_mod_raw.c | 10 +--- library/bignum_mod_raw_invasive.h | 4 +- tests/suites/test_suite_bignum_mod_raw.data | 3 -- .../suites/test_suite_bignum_mod_raw.function | 51 +------------------ 4 files changed, 5 insertions(+), 63 deletions(-) diff --git a/library/bignum_mod_raw.c b/library/bignum_mod_raw.c index b23218200..049cdb2fb 100644 --- a/library/bignum_mod_raw.c +++ b/library/bignum_mod_raw.c @@ -129,18 +129,12 @@ void mbedtls_mpi_mod_raw_mul(mbedtls_mpi_uint *X, } MBEDTLS_STATIC_TESTABLE -int mbedtls_mpi_mod_raw_fix_quasi_reduction(mbedtls_mpi_uint *X, - const mbedtls_mpi_mod_modulus *N) +void mbedtls_mpi_mod_raw_fix_quasi_reduction(mbedtls_mpi_uint *X, + const mbedtls_mpi_mod_modulus *N) { - if (N->limbs == 0) { - return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; - } - mbedtls_mpi_uint c = mbedtls_mpi_core_sub(X, X, N->p, N->limbs); (void) mbedtls_mpi_core_add_if(X, N->p, N->limbs, (unsigned) c); - - return 0; } /* END MERGE SLOT 2 */ diff --git a/library/bignum_mod_raw_invasive.h b/library/bignum_mod_raw_invasive.h index 7770c0a95..3550eaff1 100644 --- a/library/bignum_mod_raw_invasive.h +++ b/library/bignum_mod_raw_invasive.h @@ -43,8 +43,8 @@ * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p N is invalid. */ MBEDTLS_STATIC_TESTABLE -int mbedtls_mpi_mod_raw_fix_quasi_reduction(mbedtls_mpi_uint *X, - const mbedtls_mpi_mod_modulus *N); +void mbedtls_mpi_mod_raw_fix_quasi_reduction(mbedtls_mpi_uint *X, + const mbedtls_mpi_mod_modulus *N); #endif /* MBEDTLS_TEST_HOOKS */ diff --git a/tests/suites/test_suite_bignum_mod_raw.data b/tests/suites/test_suite_bignum_mod_raw.data index 2b9b1edb4..8cbd918f8 100644 --- a/tests/suites/test_suite_bignum_mod_raw.data +++ b/tests/suites/test_suite_bignum_mod_raw.data @@ -179,9 +179,6 @@ mpi_mod_raw_cond_swap:"000000001111111122222222333333334444444455555555666666667 mbedtls_mpi_mod_raw_cond_swap: copy half of the limbs mpi_mod_raw_cond_swap:"00000000FFFFFFFF55555555AAAAAAAA":"FEDCBA9876543210FEDCBA9876543210":8 -Bignum mod raw quasi-reduction: uninitialized modulus -mpi_mod_raw_fix_quasi_reduction_neg:"11":"12":"1" - # BEGIN MERGE SLOT 1 # END MERGE SLOT 1 diff --git a/tests/suites/test_suite_bignum_mod_raw.function b/tests/suites/test_suite_bignum_mod_raw.function index d59cddecd..9e29130fb 100644 --- a/tests/suites/test_suite_bignum_mod_raw.function +++ b/tests/suites/test_suite_bignum_mod_raw.function @@ -369,7 +369,7 @@ void mpi_mod_raw_fix_quasi_reduction(char *input_N, &m, N, limbs, MBEDTLS_MPI_MOD_REP_MONTGOMERY), 0); - TEST_EQUAL(mbedtls_mpi_mod_raw_fix_quasi_reduction(A, &m), 0); + mbedtls_mpi_mod_raw_fix_quasi_reduction(A, &m); ASSERT_COMPARE(A, bytes, res, bytes); exit: @@ -381,55 +381,6 @@ exit: } /* END_CASE */ -/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */ -void mpi_mod_raw_fix_quasi_reduction_neg(char *input_N, - char *input_A, - char *result) -{ - mbedtls_mpi_uint *A = NULL; - mbedtls_mpi_uint *N = NULL; - mbedtls_mpi_uint *res = NULL; - size_t limbs_A; - size_t limbs_N; - size_t limbs_res; - - mbedtls_mpi_mod_modulus m; - mbedtls_mpi_mod_modulus_init(&m); - - mbedtls_mpi_mod_modulus fake_m; - mbedtls_mpi_mod_modulus_init(&fake_m); - - TEST_EQUAL(mbedtls_test_read_mpi_core(&A, &limbs_A, input_A), 0); - TEST_EQUAL(mbedtls_test_read_mpi_core(&N, &limbs_N, input_N), 0); - TEST_EQUAL(mbedtls_test_read_mpi_core(&res, &limbs_res, result), 0); - - size_t limbs = limbs_N; - size_t bytes = limbs * sizeof(mbedtls_mpi_uint); - - TEST_EQUAL(limbs_A, limbs); - TEST_EQUAL(limbs_res, limbs); - - TEST_EQUAL(mbedtls_mpi_mod_modulus_setup( - &m, N, limbs, - MBEDTLS_MPI_MOD_REP_MONTGOMERY), 0); - - TEST_EQUAL(mbedtls_mpi_mod_raw_fix_quasi_reduction(A, &m), 0); - ASSERT_COMPARE(A, bytes, res, bytes); - - /* Check when m is not initialized */ - TEST_EQUAL(mbedtls_mpi_mod_raw_fix_quasi_reduction(A, &fake_m), - MBEDTLS_ERR_MPI_BAD_INPUT_DATA); - -exit: - mbedtls_free(A); - mbedtls_free(res); - - mbedtls_mpi_mod_modulus_free(&fake_m); - mbedtls_mpi_mod_modulus_free(&m); - mbedtls_free(N); -} -/* END_CASE */ - /* BEGIN_CASE */ void mpi_mod_raw_mul(char *input_A, char *input_B, From a24fd06451a9c18afa037e09dd93605f78b4a871 Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Mon, 23 Jan 2023 19:10:26 +0100 Subject: [PATCH 12/22] Update documentation Signed-off-by: Gabor Mezei --- library/bignum_mod_raw_invasive.h | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/library/bignum_mod_raw_invasive.h b/library/bignum_mod_raw_invasive.h index 3550eaff1..ead83942c 100644 --- a/library/bignum_mod_raw_invasive.h +++ b/library/bignum_mod_raw_invasive.h @@ -30,17 +30,12 @@ #if defined(MBEDTLS_TEST_HOOKS) -/** Convert an MPI to its canonical representative. - * - * \note Currently handles the case when `N->int_rep` is - * MBEDTLS_MPI_MOD_REP_MONTGOMERY. +/** Convert the result of a quasi-reduction to its canonical representative. * * \param[in,out] X The address of the MPI to be converted. Must have the - * same number of limbs as \p N. + * same number of limbs as \p N. The input value must + * be in range 0 <= X < 2N. * \param[in] N The address of the modulus. - * - * \return \c 0 if successful. - * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p N is invalid. */ MBEDTLS_STATIC_TESTABLE void mbedtls_mpi_mod_raw_fix_quasi_reduction(mbedtls_mpi_uint *X, From ee2aff209375a5ad6214ae2dd469e2c38c84f6ae Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Tue, 24 Jan 2023 12:48:15 +0100 Subject: [PATCH 13/22] Add check for test input The input for fix_quasi_reduction must be in range 0 <= X < 2N. Signed-off-by: Gabor Mezei --- tests/suites/test_suite_bignum_mod_raw.function | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/suites/test_suite_bignum_mod_raw.function b/tests/suites/test_suite_bignum_mod_raw.function index 9e29130fb..587c832a8 100644 --- a/tests/suites/test_suite_bignum_mod_raw.function +++ b/tests/suites/test_suite_bignum_mod_raw.function @@ -348,6 +348,7 @@ void mpi_mod_raw_fix_quasi_reduction(char *input_N, mbedtls_mpi_uint *A = NULL; mbedtls_mpi_uint *N = NULL; mbedtls_mpi_uint *res = NULL; + mbedtls_mpi_uint *tmp = NULL; size_t limbs_A; size_t limbs_N; size_t limbs_res; @@ -365,6 +366,13 @@ void mpi_mod_raw_fix_quasi_reduction(char *input_N, TEST_EQUAL(limbs_A, limbs); TEST_EQUAL(limbs_res, limbs); + ASSERT_ALLOC(tmp, limbs); + memcpy(tmp, A, bytes); + + /* Check that 0 <= X < 2N */ + mbedtls_mpi_uint c = mbedtls_mpi_core_sub(tmp, A, N, limbs); + TEST_ASSERT(c || mbedtls_mpi_core_lt_ct(tmp, N, limbs)); + TEST_EQUAL(mbedtls_mpi_mod_modulus_setup( &m, N, limbs, MBEDTLS_MPI_MOD_REP_MONTGOMERY), 0); @@ -375,6 +383,7 @@ void mpi_mod_raw_fix_quasi_reduction(char *input_N, exit: mbedtls_free(A); mbedtls_free(res); + mbedtls_free(tmp); mbedtls_mpi_mod_modulus_free(&m); mbedtls_free(N); From 86c90fca3e228a23e03675adefda148634e47737 Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Tue, 24 Jan 2023 12:53:24 +0100 Subject: [PATCH 14/22] Rename variables to follow the naming convention Signed-off-by: Gabor Mezei --- .../suites/test_suite_bignum_mod_raw.function | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/suites/test_suite_bignum_mod_raw.function b/tests/suites/test_suite_bignum_mod_raw.function index 587c832a8..24ecba326 100644 --- a/tests/suites/test_suite_bignum_mod_raw.function +++ b/tests/suites/test_suite_bignum_mod_raw.function @@ -342,46 +342,46 @@ exit: /* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */ void mpi_mod_raw_fix_quasi_reduction(char *input_N, - char *input_A, + char *input_X, char *result) { - mbedtls_mpi_uint *A = NULL; + mbedtls_mpi_uint *X = NULL; mbedtls_mpi_uint *N = NULL; mbedtls_mpi_uint *res = NULL; mbedtls_mpi_uint *tmp = NULL; - size_t limbs_A; + size_t limbs_X; size_t limbs_N; size_t limbs_res; mbedtls_mpi_mod_modulus m; mbedtls_mpi_mod_modulus_init(&m); - TEST_EQUAL(mbedtls_test_read_mpi_core(&A, &limbs_A, input_A), 0); + TEST_EQUAL(mbedtls_test_read_mpi_core(&X, &limbs_X, input_X), 0); TEST_EQUAL(mbedtls_test_read_mpi_core(&N, &limbs_N, input_N), 0); TEST_EQUAL(mbedtls_test_read_mpi_core(&res, &limbs_res, result), 0); size_t limbs = limbs_N; size_t bytes = limbs * sizeof(mbedtls_mpi_uint); - TEST_EQUAL(limbs_A, limbs); + TEST_EQUAL(limbs_X, limbs); TEST_EQUAL(limbs_res, limbs); ASSERT_ALLOC(tmp, limbs); - memcpy(tmp, A, bytes); + memcpy(tmp, X, bytes); /* Check that 0 <= X < 2N */ - mbedtls_mpi_uint c = mbedtls_mpi_core_sub(tmp, A, N, limbs); + mbedtls_mpi_uint c = mbedtls_mpi_core_sub(tmp, X, N, limbs); TEST_ASSERT(c || mbedtls_mpi_core_lt_ct(tmp, N, limbs)); TEST_EQUAL(mbedtls_mpi_mod_modulus_setup( &m, N, limbs, MBEDTLS_MPI_MOD_REP_MONTGOMERY), 0); - mbedtls_mpi_mod_raw_fix_quasi_reduction(A, &m); - ASSERT_COMPARE(A, bytes, res, bytes); + mbedtls_mpi_mod_raw_fix_quasi_reduction(X, &m); + ASSERT_COMPARE(X, bytes, res, bytes); exit: - mbedtls_free(A); + mbedtls_free(X); mbedtls_free(res); mbedtls_free(tmp); From 6f96c89fc369c45e72b8194bcd0d16326d843346 Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Tue, 24 Jan 2023 17:38:26 +0100 Subject: [PATCH 15/22] Fix pylint issues Signed-off-by: Gabor Mezei --- scripts/mbedtls_dev/bignum_mod_raw.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/scripts/mbedtls_dev/bignum_mod_raw.py b/scripts/mbedtls_dev/bignum_mod_raw.py index 5645685a5..4a9459460 100644 --- a/scripts/mbedtls_dev/bignum_mod_raw.py +++ b/scripts/mbedtls_dev/bignum_mod_raw.py @@ -62,13 +62,13 @@ class BignumModRawFixQuasiReduction(bignum_common.ModOperationCommon, # Extend the default values with n < x < 2n input_values = bignum_common.ModOperationCommon.input_values + [ - "73", - "ebeddd7b4fefae8755bbfb9c181a73347096b3ec70d1a021", - ("1f4e1d074d0b50e8d8818f9a9e5df9959f902bb955fd24fd3d791175226ad8c1" - "fcb6d59fa41a3dcb25412009e5e356eb65b50ca67782285290420b45b32f0d63" - "7c9ee549a52ad8d631ba4945435c9aec77227ec59faff878b71b920a3d631929" - "d636c9a409d6ffdcd95e2568e128596811fb9ade15e69f6efd509381ebbf3599") - ] # type: List[str] + "73", + "ebeddd7b4fefae8755bbfb9c181a73347096b3ec70d1a021", + ("1f4e1d074d0b50e8d8818f9a9e5df9959f902bb955fd24fd3d791175226ad8c1" + "fcb6d59fa41a3dcb25412009e5e356eb65b50ca67782285290420b45b32f0d63" + "7c9ee549a52ad8d631ba4945435c9aec77227ec59faff878b71b920a3d631929" + "d636c9a409d6ffdcd95e2568e128596811fb9ade15e69f6efd509381ebbf3599") + ] # type: List[str] def result(self) -> List[str]: result = self.int_a % self.int_n From 246d66bb5fd0df3ced5c738607a2be3dd8636717 Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Tue, 24 Jan 2023 18:02:52 +0100 Subject: [PATCH 16/22] Use reproductable random numbers Signed-off-by: Gabor Mezei --- scripts/mbedtls_dev/bignum_mod_raw.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/scripts/mbedtls_dev/bignum_mod_raw.py b/scripts/mbedtls_dev/bignum_mod_raw.py index 4a9459460..69fe7b8e6 100644 --- a/scripts/mbedtls_dev/bignum_mod_raw.py +++ b/scripts/mbedtls_dev/bignum_mod_raw.py @@ -63,11 +63,15 @@ class BignumModRawFixQuasiReduction(bignum_common.ModOperationCommon, # Extend the default values with n < x < 2n input_values = bignum_common.ModOperationCommon.input_values + [ "73", - "ebeddd7b4fefae8755bbfb9c181a73347096b3ec70d1a021", - ("1f4e1d074d0b50e8d8818f9a9e5df9959f902bb955fd24fd3d791175226ad8c1" - "fcb6d59fa41a3dcb25412009e5e356eb65b50ca67782285290420b45b32f0d63" - "7c9ee549a52ad8d631ba4945435c9aec77227ec59faff878b71b920a3d631929" - "d636c9a409d6ffdcd95e2568e128596811fb9ade15e69f6efd509381ebbf3599") + + # First number generated by random.getrandbits(1024) - seed(3,2) + "ea7b5bf55eb561a4216363698b529b4a97b750923ceb3ffd", + + # First number generated by random.getrandbits(1024) - seed(1,2) + ("cd447e35b8b6d8fe442e3d437204e52db2221a58008a05a6c4647159c324c985" + "9b810e766ec9d28663ca828dd5f4b3b2e4b06ce60741c7a87ce42c8218072e8c" + "35bf992dc9e9c616612e7696a6cecc1b78e510617311d8a3c2ce6f447ed4d57b" + "1e2feb89414c343c1027c4d1c386bbc4cd613e30d8f16adf91b7584a2265b1f5") ] # type: List[str] def result(self) -> List[str]: From 627e5b1f91749cdd1c59891eb4bdb49a000578f2 Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Tue, 24 Jan 2023 18:13:24 +0100 Subject: [PATCH 17/22] Only enable fix_quasi_reduction when testing Avoid compiler error due to the fix_quasi_reduction function is static and has not been used. Signed-off-by: Gabor Mezei --- library/bignum_mod_raw.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/library/bignum_mod_raw.c b/library/bignum_mod_raw.c index 049cdb2fb..6ac9e72db 100644 --- a/library/bignum_mod_raw.c +++ b/library/bignum_mod_raw.c @@ -118,15 +118,7 @@ void mbedtls_mpi_mod_raw_sub(mbedtls_mpi_uint *X, (void) mbedtls_mpi_core_add_if(X, N->p, N->limbs, (unsigned) c); } -void mbedtls_mpi_mod_raw_mul(mbedtls_mpi_uint *X, - const mbedtls_mpi_uint *A, - const mbedtls_mpi_uint *B, - const mbedtls_mpi_mod_modulus *N, - mbedtls_mpi_uint *T) -{ - mbedtls_mpi_core_montmul(X, A, B, N->limbs, N->p, N->limbs, - N->rep.mont.mm, T); -} +#if defined(MBEDTLS_TEST_HOOKS) MBEDTLS_STATIC_TESTABLE void mbedtls_mpi_mod_raw_fix_quasi_reduction(mbedtls_mpi_uint *X, @@ -137,6 +129,18 @@ void mbedtls_mpi_mod_raw_fix_quasi_reduction(mbedtls_mpi_uint *X, (void) mbedtls_mpi_core_add_if(X, N->p, N->limbs, (unsigned) c); } +#endif /* MBEDTLS_TEST_HOOKS */ + +void mbedtls_mpi_mod_raw_mul(mbedtls_mpi_uint *X, + const mbedtls_mpi_uint *A, + const mbedtls_mpi_uint *B, + const mbedtls_mpi_mod_modulus *N, + mbedtls_mpi_uint *T) +{ + mbedtls_mpi_core_montmul(X, A, B, N->limbs, N->p, N->limbs, + N->rep.mont.mm, T); +} + /* END MERGE SLOT 2 */ /* BEGIN MERGE SLOT 3 */ From 9a66ab180c42251afd38c2eae8064bde74d747b3 Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Wed, 25 Jan 2023 13:23:38 +0100 Subject: [PATCH 18/22] Fix missing declarration Signed-off-by: Gabor Mezei --- library/bignum_mod_raw.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/library/bignum_mod_raw.c b/library/bignum_mod_raw.c index 6ac9e72db..0d6810c6f 100644 --- a/library/bignum_mod_raw.c +++ b/library/bignum_mod_raw.c @@ -33,6 +33,8 @@ #include "bignum_mod.h" #include "constant_time_internal.h" +#include "bignum_mod_raw_invasive.h" + void mbedtls_mpi_mod_raw_cond_assign(mbedtls_mpi_uint *X, const mbedtls_mpi_uint *A, const mbedtls_mpi_mod_modulus *N, From 1e8c210b9d1f370c9724edbd7d86089fb4b58c68 Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Thu, 26 Jan 2023 12:27:29 +0100 Subject: [PATCH 19/22] Add dependency for generated test cases Signed-off-by: Gabor Mezei --- scripts/mbedtls_dev/bignum_mod_raw.py | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/mbedtls_dev/bignum_mod_raw.py b/scripts/mbedtls_dev/bignum_mod_raw.py index 69fe7b8e6..e1ff0cd98 100644 --- a/scripts/mbedtls_dev/bignum_mod_raw.py +++ b/scripts/mbedtls_dev/bignum_mod_raw.py @@ -59,6 +59,7 @@ class BignumModRawFixQuasiReduction(bignum_common.ModOperationCommon, test_name = "mbedtls_mpi_mod_raw_fix_quasi_reduction" input_style = "fixed" arity = 1 + dependencies = ["MBEDTLS_TEST_HOOKS"] # Extend the default values with n < x < 2n input_values = bignum_common.ModOperationCommon.input_values + [ From 26439bf692d00414e41443c0980df3ee89ebdae5 Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Fri, 27 Jan 2023 14:33:50 +0100 Subject: [PATCH 20/22] Revert "Add dependency for generated test cases" The 'MBEDTLS_TEST_HOOKS' belongs to a test function and not to a test case. This reverts commit 1e8c210b9d1f370c9724edbd7d86089fb4b58c68. Signed-off-by: Gabor Mezei --- scripts/mbedtls_dev/bignum_mod_raw.py | 1 - 1 file changed, 1 deletion(-) diff --git a/scripts/mbedtls_dev/bignum_mod_raw.py b/scripts/mbedtls_dev/bignum_mod_raw.py index e1ff0cd98..69fe7b8e6 100644 --- a/scripts/mbedtls_dev/bignum_mod_raw.py +++ b/scripts/mbedtls_dev/bignum_mod_raw.py @@ -59,7 +59,6 @@ class BignumModRawFixQuasiReduction(bignum_common.ModOperationCommon, test_name = "mbedtls_mpi_mod_raw_fix_quasi_reduction" input_style = "fixed" arity = 1 - dependencies = ["MBEDTLS_TEST_HOOKS"] # Extend the default values with n < x < 2n input_values = bignum_common.ModOperationCommon.input_values + [ From b57c90885591d86cff49be5560476173b56a0dc2 Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Fri, 27 Jan 2023 14:37:42 +0100 Subject: [PATCH 21/22] Shorten the prefix of the test case belongs to the fix quasi-reduction function Signed-off-by: Gabor Mezei --- scripts/mbedtls_dev/bignum_mod_raw.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/mbedtls_dev/bignum_mod_raw.py b/scripts/mbedtls_dev/bignum_mod_raw.py index 69fe7b8e6..d197b5491 100644 --- a/scripts/mbedtls_dev/bignum_mod_raw.py +++ b/scripts/mbedtls_dev/bignum_mod_raw.py @@ -56,7 +56,7 @@ class BignumModRawFixQuasiReduction(bignum_common.ModOperationCommon, """Test cases for ecp quasi_reduction().""" symbol = "-" test_function = "mpi_mod_raw_fix_quasi_reduction" - test_name = "mbedtls_mpi_mod_raw_fix_quasi_reduction" + test_name = "fix_quasi_reduction" input_style = "fixed" arity = 1 From db1607fa690a106bf23bdf7316245e442c1a3e7c Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Mon, 30 Jan 2023 16:27:48 +0100 Subject: [PATCH 22/22] Remove unneeded include Signed-off-by: Gabor Mezei --- library/ecp.c | 1 - 1 file changed, 1 deletion(-) diff --git a/library/ecp.c b/library/ecp.c index 6f53c1bdc..d9d5425ed 100644 --- a/library/ecp.c +++ b/library/ecp.c @@ -79,7 +79,6 @@ #include "bn_mul.h" #include "ecp_invasive.h" -#include "bignum_core.h" #include