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

Make ECDSA sign/verify format spec-compliant #8236

Conversation

tcarmelveilleux
Copy link
Contributor

@tcarmelveilleux tcarmelveilleux commented Jul 9, 2021

Problem

Change overview

  • Use raw <r,s> concatenated point format for input/output
    to ECDSA signature primitives, so that format on the wire
    matches spec. This will allow CASE to follow spec encoding.
    This is also needed for device attestation and CSR response
    spec compliance.
  • Refactors OpenSSL/mbedTLS to use EC primitives rather than ASN.1
    TLS wrappers.
  • Fixed potential crash in SHA256 when nullptr passed-in (zero length buffers
    should be non-nullptr, but with size zero)
  • Update some utilities to add missing bits/pieces.
  • Updated CHIPCert library to use raw signatures.
  • Added custom DER encoding/decoding to avoid pulling-in
    more code, more heap and more ASN.1 handling dependencies
    if a hardware-based TLS accelerator does not support raw
    signatures.

Testing

  • Added necessary unit tests for new code
  • Ran ninja -C out/host check against latest merge with master with both OpenSSL and mbedTLS on Linux, all tests still pass (after fixes)

- Use raw <r,s> concatenated point format for input/output
  to ECDSA signature primitives, so that format on the wire
  matches spec. This will allow CASE to follow spec encoding.
- Update some utilities to add missign bits/pieces.
- Add associated unit tests

Fixes project-chip#8209
tcarmelveilleux and others added 2 commits July 9, 2021 12:47
- Method is cheaper and more direct than conversions.
- Overall code is smaller.
- Refactor the message versions to defer to the sign_hash/verify_hash
@tcarmelveilleux tcarmelveilleux marked this pull request as ready for review July 9, 2021 16:52
@project-chip project-chip deleted a comment from github-actions bot Jul 9, 2021
@project-chip project-chip deleted a comment from github-actions bot Jul 9, 2021
@tcarmelveilleux tcarmelveilleux marked this pull request as draft July 9, 2021 19:20
@tcarmelveilleux tcarmelveilleux marked this pull request as ready for review July 9, 2021 20:32
src/crypto/CHIPCryptoPALOpenSSL.cpp Outdated Show resolved Hide resolved
@pan-apple pan-apple requested a review from balducci-apple July 13, 2021 15:33
@todo
Copy link

todo bot commented Jul 13, 2021

Need to make this large number (1k+) to catch some signature serialization corner cases

// TODO: Need to make this large number (1k+) to catch some signature serialization corner cases
// but this is too slow on QEMU/embedded, so we need to parametrize. Signing with ECDSA
// is non-deterministic by design (since knowledge of the value `k` used allows recovery
// of the private key).
constexpr int kNumSigningIterations = 3;
for (int i = 0; i < kNumSigningIterations; ++i)
{
P256ECDSASignature signature;
CHIP_ERROR signing_error = keypair.ECDSA_sign_hash(hash, hash_length, signature);
NL_TEST_ASSERT(inSuite, signing_error == CHIP_NO_ERROR);


This comment was generated by todo based on a TODO comment in 57e6a60 in #8236. cc @tcarmelveilleux.

{
EC_POINT_clear_free(key_point);
key_point = nullptr;
BN_clear_free(s);
Copy link
Contributor

Choose a reason for hiding this comment

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

ScopedBigNum and ScopedPoint would be so nice. I think some OpenSSL C++ headers have unique_ptr implementations for this.

@andy31415
Copy link
Contributor

scripts/tests/test_suites.sh Show resolved Hide resolved
src/crypto/CHIPCryptoPAL.cpp Show resolved Hide resolved
src/crypto/CHIPCryptoPAL.cpp Show resolved Hide resolved
src/crypto/CHIPCryptoPAL.cpp Show resolved Hide resolved
src/crypto/CHIPCryptoPAL.cpp Show resolved Hide resolved
src/crypto/CHIPCryptoPAL.cpp Show resolved Hide resolved
src/crypto/CHIPCryptoPAL.cpp Show resolved Hide resolved
src/crypto/CHIPCryptoPAL.cpp Show resolved Hide resolved
src/crypto/CHIPCryptoPAL.cpp Show resolved Hide resolved
src/crypto/CHIPCryptoPAL.h Show resolved Hide resolved
@bzbarsky-apple
Copy link
Contributor

@tcarmelveilleux will deal with my comments in a followup; merging to get other things unblocked, since none of those comments are critical.

tcarmelveilleux added a commit to tcarmelveilleux/connectedhomeip that referenced this pull request Jul 15, 2021
…ort Span

- Fix leftover nits from @bzbarsky-apple's review of project-chip#8236
- In order to add span support cleanly, added Span support to
  Reader and BufferWriter, and fixed all necessary breakage.

Testing done: pass all unit tests and CASE cert tests
@tcarmelveilleux
Copy link
Contributor Author

@tcarmelveilleux will deal with my comments in a followup; merging to get other things unblocked, since none of those comments are critical.

See #8408 for follow-up.

bzbarsky-apple pushed a commit that referenced this pull request Jul 20, 2021
)

* Fix leftover from #8236 and make BufferReader/Writer support Span

- Fix leftover nits from @bzbarsky-apple's review of #8236
- In order to add span support cleanly, added Span support to
  Reader and BufferWriter, and fixed all necessary breakage.

Testing done: pass all unit tests and CASE cert tests

* Fix mbedTLS usage of EcdsaAsn1SignatureToRaw

* Apply review comments from @bzbarsky-apple

* Use some span reassign instead of out_size ref

* Restyled by clang-format

* Remove unnecessary nullptr checks handled by Span::size()

* Improve IntegerToDer test coverage

- Assign to output spans
- Use new span function for validity checks (`is_span_usable`)
- Replace an untested CHIPCert.cpp usage with tested version

* Add is_span_usable() tests

* Restyled by clang-format

* Grammar fix to kick CI

* Commit forgotten removal of obsolete ConvertIntegerRawToDER from CHIPCert.h

* Apply review comments from @mspang

* Fix clang

Co-authored-by: Restyled.io <[email protected]>
nikita-s-wrk pushed a commit to nikita-s-wrk/connectedhomeip that referenced this pull request Sep 23, 2021
* Make ECDSA sign/verify format spec-compliant

- Use raw <r,s> concatenated point format for input/output
  to ECDSA signature primitives, so that format on the wire
  matches spec. This will allow CASE to follow spec encoding.
- Update some utilities to add missign bits/pieces.
- Add associated unit tests

Fixes project-chip#8209

* Fix some comment typos

* Make OpenSSL and mbedTLS use raw signatures directly

- Method is cheaper and more direct than conversions.
- Overall code is smaller.
- Refactor the message versions to defer to the sign_hash/verify_hash

* Restyled by clang-format

* Fix issue with cert conversion

* Use CHIPCert module for raw to DER conversion

* Ran clang-format

* Restyled by clang-format

* Fix unit tests

* Remove debug logging left by mistake

* Restyled by shellharden

* Restyled by shfmt

* Address review comments from @andy31415

* Fix merge issues

* Restyled by clang-format

* Fix test script and improve signing unit test

* Restyled by shellharden

* Reduce signing unit test case numbers for now

* Apply review from @pan-apple

* Fix test_suites.sh

* Please the gods of shell-harden

* Please the gods of shell-harden some more

Co-authored-by: Restyled.io <[email protected]>
nikita-s-wrk pushed a commit to nikita-s-wrk/connectedhomeip that referenced this pull request Sep 23, 2021
…ort Span (project-chip#8408)

* Fix leftover from project-chip#8236 and make BufferReader/Writer support Span

- Fix leftover nits from @bzbarsky-apple's review of project-chip#8236
- In order to add span support cleanly, added Span support to
  Reader and BufferWriter, and fixed all necessary breakage.

Testing done: pass all unit tests and CASE cert tests

* Fix mbedTLS usage of EcdsaAsn1SignatureToRaw

* Apply review comments from @bzbarsky-apple

* Use some span reassign instead of out_size ref

* Restyled by clang-format

* Remove unnecessary nullptr checks handled by Span::size()

* Improve IntegerToDer test coverage

- Assign to output spans
- Use new span function for validity checks (`is_span_usable`)
- Replace an untested CHIPCert.cpp usage with tested version

* Add is_span_usable() tests

* Restyled by clang-format

* Grammar fix to kick CI

* Commit forgotten removal of obsolete ConvertIntegerRawToDER from CHIPCert.h

* Apply review comments from @mspang

* Fix clang

Co-authored-by: Restyled.io <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Discrepancy in P256 signature format between impl and spec
8 participants