From 483a24a69950b8be7ae924e28a7504150378c8ee Mon Sep 17 00:00:00 2001 From: Taylor Cramer Date: Wed, 8 Jan 2025 15:00:51 -0800 Subject: [PATCH] pw_crypto: Remove micro_ecc support MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit micro_ecc hasn't received upstream updates in more than a year, and there are no remaining easily-discoverable usages of this backend. Bug: b/359924206 Change-Id: Iee10cec1ffd568cbccd3acc2c5f720922875e37f Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/229672 Reviewed-by: Anthony DiGirolamo Lint: Lint 🤖 Presubmit-Verified: CQ Bot Account Commit-Queue: Aaron Green --- BUILD.gn | 1 - MODULE.bazel | 10 -- pw_crypto/BUILD.bazel | 14 --- pw_crypto/BUILD.gn | 32 ------ pw_crypto/docs.rst | 28 ----- pw_crypto/ecdsa_uecc.cc | 105 ------------------ pw_package/py/BUILD.bazel | 1 - pw_package/py/BUILD.gn | 1 - .../py/pw_package/packages/micro_ecc.py | 49 -------- pw_package/py/pw_package/pigweed_packages.py | 1 - .../py/pw_presubmit/pigweed_presubmit.py | 32 +----- third_party/micro_ecc/BUILD.gn | 69 ------------ third_party/micro_ecc/BUILD.micro_ecc | 29 ----- third_party/micro_ecc/OWNERS | 1 - third_party/micro_ecc/micro_ecc.gni | 18 --- 15 files changed, 4 insertions(+), 387 deletions(-) delete mode 100644 pw_crypto/ecdsa_uecc.cc delete mode 100644 pw_package/py/pw_package/packages/micro_ecc.py delete mode 100644 third_party/micro_ecc/BUILD.gn delete mode 100644 third_party/micro_ecc/BUILD.micro_ecc delete mode 100644 third_party/micro_ecc/OWNERS delete mode 100644 third_party/micro_ecc/micro_ecc.gni diff --git a/BUILD.gn b/BUILD.gn index 259debf09d..d0ca93cf02 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -116,7 +116,6 @@ group("default") { # boringssl # freertos # mbedtls -# micro-ecc # nanopb # pico_sdk # protobuf diff --git a/MODULE.bazel b/MODULE.bazel index 03f56f666b..a9f7614535 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -280,16 +280,6 @@ use_repo( # are NOT in the BCR that downstream users may need to override/unify to their # expected version. -new_git_repository = use_repo_rule("@bazel_tools//tools/build_defs/repo:git.bzl", "new_git_repository") - -# TODO: https://pwbug.dev/378531541 - Get micro_ecc from the BCR. -new_git_repository( - name = "micro_ecc", - build_file = "//:third_party/micro_ecc/BUILD.micro_ecc", - commit = "b335ee812bfcca4cd3fb0e2a436aab39553a555a", - remote = "https://github.com/kmackay/micro-ecc.git", -) - # TODO: https://pwbug.dev/354749299 - Use the BCR version of mbedtls. http_archive( name = "mbedtls", diff --git a/pw_crypto/BUILD.bazel b/pw_crypto/BUILD.bazel index 062ce3f20c..4234358828 100644 --- a/pw_crypto/BUILD.bazel +++ b/pw_crypto/BUILD.bazel @@ -130,20 +130,6 @@ cc_library( ], ) -cc_library( - name = "ecdsa_uecc", - srcs = [ - "ecdsa_uecc.cc", - ], - # TODO: b/261603269 - Get this to build. - tags = ["manual"], - deps = [ - ":ecdsa.facade", - "//pw_log", - "@micro_ecc//:uecc", - ], -) - pw_cc_test( name = "ecdsa_test", srcs = ["ecdsa_test.cc"], diff --git a/pw_crypto/BUILD.gn b/pw_crypto/BUILD.gn index 1e686aee60..28581123aa 100644 --- a/pw_crypto/BUILD.gn +++ b/pw_crypto/BUILD.gn @@ -19,7 +19,6 @@ import("$dir_pw_build/facade.gni") import("$dir_pw_build/target_types.gni") import("$dir_pw_crypto/backend.gni") import("$dir_pw_docgen/docs.gni") -import("$dir_pw_third_party/micro_ecc/micro_ecc.gni") import("$dir_pw_unit_test/test.gni") config("default_config") { @@ -86,9 +85,6 @@ pw_test_group("tests") { ":sha256_mock_test", ":ecdsa_test", ] - if (dir_pw_third_party_micro_ecc != "") { - tests += [ ":ecdsa_uecc_little_endian_test" ] - } } # Sha256 tests against the selected real backend. @@ -185,34 +181,6 @@ pw_source_set("ecdsa_mbedtls_v3") { public_deps = [ ":ecdsa.facade" ] } -pw_source_set("ecdsa_uecc") { - sources = [ "ecdsa_uecc.cc" ] - deps = [ - "$dir_pw_log", - "$dir_pw_third_party/micro_ecc", - ] - public_deps = [ ":ecdsa.facade" ] -} - -if (dir_pw_third_party_micro_ecc != "") { - pw_source_set("ecdsa_uecc_little_endian") { - sources = [ "ecdsa_uecc.cc" ] - deps = [ - "$dir_pw_log", - "$dir_pw_third_party/micro_ecc:micro_ecc_little_endian", - ] - public_deps = [ ":ecdsa.facade" ] - } - - # This test targets the micro_ecc little endian backend specifically. - # - # TODO: b/273819841 - deduplicate all backend tests. - pw_test("ecdsa_uecc_little_endian_test") { - sources = [ "ecdsa_test.cc" ] - deps = [ ":ecdsa_uecc_little_endian" ] - } -} - # This test targets the specific backend pointed to by # pw_crypto_ECDSA_BACKEND. pw_test("ecdsa_test") { diff --git a/pw_crypto/docs.rst b/pw_crypto/docs.rst index 77537aab22..9b24813176 100644 --- a/pw_crypto/docs.rst +++ b/pw_crypto/docs.rst @@ -181,36 +181,8 @@ a code size of ~12KiB. #define MBEDTLS_ECP_NO_INTERNAL_RNG #define MBEDTLS_ECP_DP_SECP256R1_ENABLED -Micro ECC -========= - -.. Warning:: - Micro ECC's upstream hasn't received any updates since April 2023. - Please investigate to make sure that it meets your product's security - requirements before use. - -To select Micro ECC, the library needs to be installed and configured. - -.. code-block:: sh - - # Install and configure Micro ECC - pw package install micro-ecc - gn gen out --args=' - dir_pw_third_party_micro_ecc=getenv("PW_PACKAGE_ROOT")+"/micro-ecc" - pw_crypto_ECDSA_BACKEND="//pw_crypto:ecdsa_uecc" - ' - -The default micro-ecc backend uses big endian as is standard practice. It also -has a little-endian configuration which can be used to slightly reduce call -stack frame use and/or when non pw_crypto clients use the same micro-ecc -with a little-endian configuration. The little-endian version of micro-ecc -can be selected with ``pw_crypto_ECDSA_BACKEND="//pw_crypto:ecdsa_uecc_little_endian"`` - -Note Micro-ECC does not implement any hashing functions, so you will need to use other backends for SHA256 functionality if needed. - BoringSSL ========= - The BoringSSL project (`source `_, `GitHub mirror `_) is a fork of OpenSSL maintained by diff --git a/pw_crypto/ecdsa_uecc.cc b/pw_crypto/ecdsa_uecc.cc deleted file mode 100644 index 912a28b445..0000000000 --- a/pw_crypto/ecdsa_uecc.cc +++ /dev/null @@ -1,105 +0,0 @@ -// Copyright 2021 The Pigweed Authors -// -// 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 -// -// https://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. -#define PW_LOG_MODULE_NAME "ECDSA-UECC" -#define PW_LOG_LEVEL PW_LOG_LEVEL_WARN - -#include - -#include "pw_crypto/ecdsa.h" -#include "pw_log/log.h" -#include "uECC.h" - -namespace pw::crypto::ecdsa { - -constexpr size_t kP256CurveOrderBytes = 32; -constexpr size_t kP256PublicKeySize = 2 * kP256CurveOrderBytes + 1; -constexpr size_t kP256SignatureSize = kP256CurveOrderBytes * 2; - -Status VerifyP256Signature(ConstByteSpan public_key, - ConstByteSpan digest, - ConstByteSpan signature) { - // Signature expected in raw format (r||s) - if (signature.size() != kP256SignatureSize) { - PW_LOG_DEBUG("Bad signature format"); - return Status::InvalidArgument(); - } - - // Supports SEC 1 uncompressed form (04||X||Y) only. - if (public_key.size() != kP256PublicKeySize || - std::to_integer(public_key.data()[0]) != 0x04) { - PW_LOG_DEBUG("Bad public key format"); - return Status::InvalidArgument(); - } - -#if defined(uECC_VLI_NATIVE_LITTLE_ENDIAN) && uECC_VLI_NATIVE_LITTLE_ENDIAN - // uECC_VLI_NATIVE_LITTLE_ENDIAN is defined with a non-zero value when - // pw_crypto_ECDSA_BACKEND is set to "//pw_crypto:ecdsa_uecc_little_endian". - // - // Since pw_crypto APIs are big endian only (standard practice), here we - // need to convert input parameters to little endian. - // - // Additionally uECC requires these little endian buffers to be word aligned - // in case unaligned accesses are not supported by the hardware. We choose - // the maximum 8-byte alignment to avoid referrencing internal uECC headers. - alignas(8) uint8_t signature_bytes[kP256SignatureSize]; - memcpy(signature_bytes, signature.data(), sizeof(signature_bytes)); - std::reverse(signature_bytes, signature_bytes + kP256CurveOrderBytes); // r - std::reverse(signature_bytes + kP256CurveOrderBytes, - signature_bytes + sizeof(signature_bytes)); // s - - alignas(8) uint8_t public_key_bytes[kP256PublicKeySize - 1]; - memcpy(public_key_bytes, public_key.data() + 1, sizeof(public_key_bytes)); - std::reverse(public_key_bytes, public_key_bytes + kP256CurveOrderBytes); // X - std::reverse(public_key_bytes + kP256CurveOrderBytes, - public_key_bytes + sizeof(public_key_bytes)); // Y - - alignas(8) uint8_t digest_bytes[kP256CurveOrderBytes]; - memcpy(digest_bytes, digest.data(), sizeof(digest_bytes)); - std::reverse(digest_bytes, digest_bytes + sizeof(digest_bytes)); -#else - const uint8_t* public_key_bytes = - reinterpret_cast(public_key.data()) + 1; - const uint8_t* digest_bytes = reinterpret_cast(digest.data()); - const uint8_t* signature_bytes = - reinterpret_cast(signature.data()); -#endif // uECC_VLI_NATIVE_LITTLE_ENDIAN - - uECC_Curve curve = uECC_secp256r1(); - // Make sure the public key is on the curve. - if (!uECC_valid_public_key(public_key_bytes, curve)) { - PW_LOG_DEBUG("Bad public key curve"); - return Status::InvalidArgument(); - } - - // Digests must be at least 32 bytes. Digests longer than 32 - // bytes are truncated to 32 bytes. - if (digest.size() < kP256CurveOrderBytes) { - PW_LOG_DEBUG("Digest is too short"); - return Status::InvalidArgument(); - } - - // Verify the signature. - if (!uECC_verify(public_key_bytes, - digest_bytes, - digest.size(), - signature_bytes, - curve)) { - PW_LOG_DEBUG("Signature verification failed"); - return Status::Unauthenticated(); - } - - return OkStatus(); -} - -} // namespace pw::crypto::ecdsa diff --git a/pw_package/py/BUILD.bazel b/pw_package/py/BUILD.bazel index 038c5b6b9e..93f9e9ee04 100644 --- a/pw_package/py/BUILD.bazel +++ b/pw_package/py/BUILD.bazel @@ -37,7 +37,6 @@ py_library( "pw_package/packages/googletest.py", "pw_package/packages/mbedtls.py", "pw_package/packages/mcuxpresso.py", - "pw_package/packages/micro_ecc.py", "pw_package/packages/nanopb.py", "pw_package/packages/pico_sdk.py", "pw_package/packages/picotool.py", diff --git a/pw_package/py/BUILD.gn b/pw_package/py/BUILD.gn index 9d30589870..20b226dcd2 100644 --- a/pw_package/py/BUILD.gn +++ b/pw_package/py/BUILD.gn @@ -38,7 +38,6 @@ pw_python_package("py") { "pw_package/packages/googletest.py", "pw_package/packages/mbedtls.py", "pw_package/packages/mcuxpresso.py", - "pw_package/packages/micro_ecc.py", "pw_package/packages/nanopb.py", "pw_package/packages/pico_sdk.py", "pw_package/packages/picotool.py", diff --git a/pw_package/py/pw_package/packages/micro_ecc.py b/pw_package/py/pw_package/packages/micro_ecc.py deleted file mode 100644 index 9d8e827cd4..0000000000 --- a/pw_package/py/pw_package/packages/micro_ecc.py +++ /dev/null @@ -1,49 +0,0 @@ -# Copyright 2021 The Pigweed Authors -# -# 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 -# -# https://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. -"""Install and check the status of the Micro-ECC cryptography library.""" - -import pathlib -from typing import Sequence - -import pw_package.git_repo -import pw_package.package_manager - - -class MicroECC(pw_package.git_repo.GitRepo): - """Install and check the status of the Micro-ECC cryptography library.""" - - def __init__(self, *args, **kwargs): - super().__init__( - *args, - name='micro-ecc', - url="".join( - [ - "https://pigweed.googlesource.com", - "/third_party/github/kmackay/micro-ecc.git", - ] - ), - # pigweed branch with Google-internal fixes. - commit='df3398e0c550f323afc00afd8faa3e08869f8874', - **kwargs, - ) - - def info(self, path: pathlib.Path) -> Sequence[str]: - return ( - f'{self.name} installed in: {path}', - "Enable by running 'gn args out' and adding this line:", - f' dir_pw_third_party_micro_ecc = "{path}"', - ) - - -pw_package.package_manager.register(MicroECC) diff --git a/pw_package/py/pw_package/pigweed_packages.py b/pw_package/py/pw_package/pigweed_packages.py index 4a41964460..531545472e 100644 --- a/pw_package/py/pw_package/pigweed_packages.py +++ b/pw_package/py/pw_package/pigweed_packages.py @@ -30,7 +30,6 @@ from pw_package.packages import googletest from pw_package.packages import mbedtls from pw_package.packages import mcuxpresso -from pw_package.packages import micro_ecc from pw_package.packages import nanopb from pw_package.packages import pico_sdk from pw_package.packages import picotool diff --git a/pw_presubmit/py/pw_presubmit/pigweed_presubmit.py b/pw_presubmit/py/pw_presubmit/pigweed_presubmit.py index ea7ea6b121..2049e452ea 100755 --- a/pw_presubmit/py/pw_presubmit/pigweed_presubmit.py +++ b/pw_presubmit/py/pw_presubmit/pigweed_presubmit.py @@ -334,26 +334,6 @@ def gn_arm_build(ctx: PresubmitContext): ), ) -gn_crypto_micro_ecc_build = PigweedGnGenNinja( - name='gn_crypto_micro_ecc_build', - path_filter=_BUILD_FILE_FILTER, - packages=('micro-ecc',), - gn_args={ - 'dir_pw_third_party_micro_ecc': lambda ctx: '"{}"'.format( - ctx.package_root / 'micro-ecc' - ), - 'pw_crypto_ECDSA_BACKEND': lambda ctx: '"{}"'.format( - ctx.root / 'pw_crypto:ecdsa_uecc' - ), - 'pw_C_OPTIMIZATION_LEVELS': _OPTIMIZATION_LEVELS, - }, - ninja_targets=( - *_at_all_optimization_levels(f'host_{_HOST_COMPILER}'), - # TODO: b/240982565 - SocketStream currently requires Linux. - *(('integration_tests',) if sys.platform.startswith('linux') else ()), - ), -) - gn_teensy_build = PigweedGnGenNinja( name='gn_teensy_build', path_filter=_BUILD_FILE_FILTER, @@ -428,7 +408,7 @@ def gn_arm_build(ctx: PresubmitContext): gn_software_update_build = PigweedGnGenNinja( name='gn_software_update_build', path_filter=_BUILD_FILE_FILTER, - packages=('nanopb', 'protobuf', 'mbedtls', 'micro-ecc'), + packages=('nanopb', 'protobuf', 'mbedtls'), gn_args={ 'dir_pw_third_party_protobuf': lambda ctx: '"{}"'.format( ctx.package_root / 'protobuf' @@ -436,18 +416,15 @@ def gn_arm_build(ctx: PresubmitContext): 'dir_pw_third_party_nanopb': lambda ctx: '"{}"'.format( ctx.package_root / 'nanopb' ), - 'dir_pw_third_party_micro_ecc': lambda ctx: '"{}"'.format( - ctx.package_root / 'micro-ecc' - ), - 'pw_crypto_ECDSA_BACKEND': lambda ctx: '"{}"'.format( - ctx.root / 'pw_crypto:ecdsa_uecc' - ), 'dir_pw_third_party_mbedtls': lambda ctx: '"{}"'.format( ctx.package_root / 'mbedtls' ), 'pw_crypto_SHA256_BACKEND': lambda ctx: '"{}"'.format( ctx.root / 'pw_crypto:sha256_mbedtls_v3' ), + 'pw_crypto_ECDSA_BACKEND': lambda ctx: '"{}"'.format( + ctx.root / 'pw_crypto:ecdsa_mbedtls_v3' + ), 'pw_C_OPTIMIZATION_LEVELS': _OPTIMIZATION_LEVELS, }, ninja_targets=_at_all_optimization_levels('host_clang'), @@ -1792,7 +1769,6 @@ def owners_lint_checks(ctx: PresubmitContext): SECURITY = ( # keep-sorted: start gn_crypto_mbedtls_build, - gn_crypto_micro_ecc_build, gn_software_update_build, # keep-sorted: end ) diff --git a/third_party/micro_ecc/BUILD.gn b/third_party/micro_ecc/BUILD.gn deleted file mode 100644 index 39cfd09ea2..0000000000 --- a/third_party/micro_ecc/BUILD.gn +++ /dev/null @@ -1,69 +0,0 @@ -# Copyright 2021 The Pigweed Authors -# -# 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 -# -# https://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("//build_overrides/pigweed.gni") -import("$dir_pw_build/target_types.gni") -import("micro_ecc.gni") - -if (dir_pw_third_party_micro_ecc != "") { - config("public_config") { - include_dirs = [ "$dir_pw_third_party_micro_ecc/" ] - } - - config("internal_config") { - # Suppress all upstream introduced warnings. - cflags = [ "-w" ] - - # Disabling point compression saves 200 bytes. - defines = [ "uECC_SUPPORT_COMPRESSED_POINT=0" ] - } - - # Endianess is a public configuration for uECC as it determines how large - # integers are interpreted in uECC public APIs. - # - # Big endian is a lot more common and thus is recommended unless you are - # really resource-constrained or another uECC client expects little - # endian. - config("big_endian_config") { - defines = [ "uECC_VLI_NATIVE_LITTLE_ENDIAN=0" ] - } - - # Little endian can reduce call stack usage in native little endian - # execution environments (as determined by processor state, memory - # access config etc.) - config("little_endian_config") { - defines = [ "uECC_VLI_NATIVE_LITTLE_ENDIAN=1" ] - } - - pw_source_set("micro_ecc") { - public_configs = [ - ":big_endian_config", - ":public_config", - ] - configs = [ ":internal_config" ] - sources = [ "$dir_pw_third_party_micro_ecc/uECC.c" ] - } - - pw_source_set("micro_ecc_little_endian") { - public_configs = [ - ":little_endian_config", - ":public_config", - ] - configs = [ ":internal_config" ] - sources = [ "$dir_pw_third_party_micro_ecc/uECC.c" ] - } -} else { - group("micro_ecc") { - } -} diff --git a/third_party/micro_ecc/BUILD.micro_ecc b/third_party/micro_ecc/BUILD.micro_ecc deleted file mode 100644 index 2b9be56553..0000000000 --- a/third_party/micro_ecc/BUILD.micro_ecc +++ /dev/null @@ -1,29 +0,0 @@ -# Copyright 2022 The Pigweed Authors -# -# 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 -# -# https://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. - -cc_library( - name = "uecc", - srcs = [ - "asm_arm.inc", - "curve-specific.inc", - "platform-specific.inc", - "uECC.c", - ], - hdrs = [ - "types.h", - "uECC.h", - "uECC_vli.h", - ], - visibility = ["//visibility:public"], -) diff --git a/third_party/micro_ecc/OWNERS b/third_party/micro_ecc/OWNERS deleted file mode 100644 index b01d16c5f5..0000000000 --- a/third_party/micro_ecc/OWNERS +++ /dev/null @@ -1 +0,0 @@ -alizhang@google.com diff --git a/third_party/micro_ecc/micro_ecc.gni b/third_party/micro_ecc/micro_ecc.gni deleted file mode 100644 index 8b523c7782..0000000000 --- a/third_party/micro_ecc/micro_ecc.gni +++ /dev/null @@ -1,18 +0,0 @@ -# Copyright 2021 The Pigweed Authors -# -# 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 -# -# https://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. - -declare_args() { - # Points to where the upstream code resides. - dir_pw_third_party_micro_ecc = "" -}