Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding shortcut for all-bits-zero payloads (mbedtls_ecp_mul_shortcuts… #4172

Closed
wants to merge 29 commits into from

Conversation

TRodziewicz
Copy link
Contributor

@TRodziewicz TRodziewicz commented Feb 24, 2021

…()) and returning proper error code (MBEDTLS_ERR_ECP_INVALID_KEY) for that case (ecjpake_zkp_read()).

Description

Resolves: #1792

Short issue #1792 description:
"ECDSA verification (mbedtls_ecdsa_verify or mbedtls_ecdsa_read_signature) returns MBEDTLS_ERR_ECP_INVALID_KEY when the payload (which should be a hash) is all-bits zero. The curve is a short Weierstrass curve."

root cause: ecp_mul_restartable() function is very strict and don't handle situation like that.

Fix: There was a new shortcut added to the mbedtls_ecp_mul_shortcuts() It is a shortcut for a situation when m == 0.
Additionally ecjpake_zkp_read() returns proper error code (MBEDTLS_ERR_ECP_INVALID_KEY) for that case.

Status

READY

Requires Backporting

NO

Migrations

NO

Todos

  • Tests
  • Documentation
  • Changelog updated

Steps to test or reproduce

Please check: #1792

@paul-elliott-arm paul-elliott-arm added component-crypto Crypto primitives and low-level interfaces enhancement needs-review Every commit must be reviewed by at least two team members, needs-reviewer This PR needs someone to pick it up for review needs-work size-m Estimated task size: medium (~1w) labels Feb 24, 2021
Copy link
Member

@paul-elliott-arm paul-elliott-arm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix the DCO check by committing with the --signoff flag - see the 'Checks' tab for details.

Also I would suggest adding a changelog entry.

@paul-elliott-arm paul-elliott-arm added bug needs-backports Backports are missing or are pending review and approval. needs: changelog needs-ci Needs to pass CI tests size-s Estimated task size: small (~2d) and removed enhancement size-m Estimated task size: medium (~1w) labels Feb 24, 2021
Signed-off-by: Gilles Peskine <[email protected]>
Signed-off-by: TRodziewicz <[email protected]>
The code will be moved in a subsequent commit.

Signed-off-by: Gilles Peskine <[email protected]>
Signed-off-by: TRodziewicz <[email protected]>
Rename functions to mbedtls_test_psa_xxx if they're going to be
exported. Declare functions as static if they're aren't meant to be
called directly from test code.

Signed-off-by: Gilles Peskine <[email protected]>
Signed-off-by: TRodziewicz <[email protected]>
exercise_export_key() exports the key and does sanity checks on the
result. Here we've already just exported the key, so just run the
sanity checks.

Signed-off-by: Gilles Peskine <[email protected]>
Signed-off-by: TRodziewicz <[email protected]>
The code will be moved in a subsequent commit.

Signed-off-by: Gilles Peskine <[email protected]>
Signed-off-by: TRodziewicz <[email protected]>
Signed-off-by: Gilles Peskine <[email protected]>
Signed-off-by: TRodziewicz <[email protected]>
Move mbedtls_test_psa_exercise_key() (formerly exercise_key()) and
related functions to its own module. Export the few auxiliary
functions that are also called directly.

Signed-off-by: Gilles Peskine <[email protected]>
Signed-off-by: TRodziewicz <[email protected]>
Signed-off-by: Gilles Peskine <[email protected]>
Signed-off-by: TRodziewicz <[email protected]>
Remove a conditional imbrication level. Get rid of some minor overhead
for ECC public keys dating back from when they had ASN.1 wrapping.

No behavior change.

Signed-off-by: Gilles Peskine <[email protected]>
Signed-off-by: TRodziewicz <[email protected]>
The const-ness has to be cast away when calling mbedtls_asn1_xxx
parsing functions. This is a known flaw in the mbedtls API
(Mbed-TLS#803).

Signed-off-by: Gilles Peskine <[email protected]>
Signed-off-by: TRodziewicz <[email protected]>
Shuffle the logic in mbedtls_test_psa_exported_key_sanity_check()
somewhat. The resulting behavior changes are:

* Always check the exported length against PSA_EXPORT_KEY_OUTPUT_SIZE,
  even for unstructured key types.
* Always complain if a key type is not explicitly covered, not just
  for public keys.

Signed-off-by: Gilles Peskine <[email protected]>
Signed-off-by: TRodziewicz <[email protected]>
mbedtls_test_fail does not copy the failure explanation string, so
passing a string on the stack doesn't work. This fixes a garbage
message that would appear if a test triggered a non-implemented code
path.

More generally, just use TEST_ASSERT instead of explicitly calling
mbedtls_test_fail, since we aren't playing any tricks with the error
location.

Signed-off-by: Gilles Peskine <[email protected]>
Signed-off-by: TRodziewicz <[email protected]>
Rename functions to mbedtls_test_xxx and make them non-static if
they're going to be exported.

Signed-off-by: Gilles Peskine <[email protected]>
Signed-off-by: TRodziewicz <[email protected]>
Persistent storage common code from
test_suite_psa_crypto_slot_management.function had been duplicated in
test_suite_psa_crypto_se_driver_hal.function and the copy had slightly
diverged. Re-align the copy in preparation from moving the code to a
common module and using that sole copy in both test suites.

Signed-off-by: Gilles Peskine <[email protected]>
Signed-off-by: TRodziewicz <[email protected]>
gilles-peskine-arm and others added 14 commits February 25, 2021 11:02
Merge the two identical definitions of TEST_USES_KEY_ID and
mbedtls_test_psa_purge_key_storage from
test_suite_psa_crypto_slot_management.function and
test_suite_psa_crypto_se_driver_hal.function into a single copy in
common test code so that it can be used in all test suites.

No semantic change.

Signed-off-by: Gilles Peskine <[email protected]>
Signed-off-by: TRodziewicz <[email protected]>
Signed-off-by: Gilles Peskine <[email protected]>
Signed-off-by: TRodziewicz <[email protected]>
This ensures that test cases won't leave persistent files behind even
on failure, provided they use TEST_USES_KEY_ID(). Test cases that
don't use this macro are unaffected.

Tests that use PSA_DONE() midway and expect persistent keys to survive
must use PSA_SESSION_DONE() instead.

Signed-off-by: Gilles Peskine <[email protected]>
Signed-off-by: TRodziewicz <[email protected]>
This makes failure messages easier to understand.

Signed-off-by: Gilles Peskine <[email protected]>
Signed-off-by: TRodziewicz <[email protected]>
MSVC started (rightfully) complaining after moving the code to a
separate .c file.

Signed-off-by: Gilles Peskine <[email protected]>
Signed-off-by: TRodziewicz <[email protected]>
ARRAY_LENGTH has a portable but unsafe implementation, and a
non-portable implementation that causes a compile-time error if the
macro is accidentally used on a pointer.

The safety check was only implemented for __GCC__-defining compilers,
but the part that triggered the compile-time error was always used. It
turns out that this part triggers a build warning with MSVC (at least
with some versions: observed with Visual Studio 2013).
```
C:\builds\workspace\mbed-tls-pr-head_PR-4141-head\src\tests\src\psa_crypto_helpers.c(52): error C2220: warning treated as error - no 'object' file generated [C:\builds\workspace\mbed-tls-pr-head_PR-4141-head\src\mbedtls_test.vcxproj]
C:\builds\workspace\mbed-tls-pr-head_PR-4141-head\src\tests\src\psa_crypto_helpers.c(52): warning C4116: unnamed type definition in parentheses [C:\builds\workspace\mbed-tls-pr-head_PR-4141-head\src\mbedtls_test.vcxproj]
```

Since a compile-time error is never triggered when the compile-time
check for the argument type is not implemented, just use the unsafe
macro directly when there's no safety check.

Signed-off-by: Gilles Peskine <[email protected]>
Signed-off-by: TRodziewicz <[email protected]>
The primary goal of this commit is to fix various comments where
`clang -Wdocumentation` identified a discrepancy between the actual
function parameters and the documented parameters. The discrepancies
were due to copypasta, formatting issues or documentation that had
diverged from the implementation.

Signed-off-by: Gilles Peskine <[email protected]>
Signed-off-by: TRodziewicz <[email protected]>
Signed-off-by: Gilles Peskine <[email protected]>
Signed-off-by: TRodziewicz <[email protected]>
Time stamps are useful when the document gets shared around, but they
tend to lead to merge conflicts.

Signed-off-by: Gilles Peskine <[email protected]>
Signed-off-by: TRodziewicz <[email protected]>
Signed-off-by: Gilles Peskine <[email protected]>
Signed-off-by: TRodziewicz <[email protected]>
Signed-off-by: Gilles Peskine <[email protected]>
Signed-off-by: TRodziewicz <[email protected]>
Signed-off-by: Gilles Peskine <[email protected]>
Signed-off-by: TRodziewicz <[email protected]>
…()) and returning proper error code (MBEDTLS_ERR_ECP_INVALID_KEY) for that case (ecjpake_zkp_read()).

Signed-off-by: TRodziewicz <[email protected]>
@TRodziewicz TRodziewicz marked this pull request as draft March 1, 2021 09:57
@TRodziewicz
Copy link
Contributor Author

I'm closing this PR because it was easier to open a new one than fixing the multiple commits it caught.

@TRodziewicz TRodziewicz closed this Mar 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug component-crypto Crypto primitives and low-level interfaces needs-backports Backports are missing or are pending review and approval. needs-ci Needs to pass CI tests needs-review Every commit must be reviewed by at least two team members, needs-reviewer This PR needs someone to pick it up for review needs-work size-s Estimated task size: small (~2d)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ECDSA verification fails when the payload is all-bits-zero
3 participants