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

sys/psa_crypto: Add generic HMAC implementation #20758

Open
wants to merge 11 commits into
base: master
Choose a base branch
from

Conversation

Wer-Wolf
Copy link
Contributor

@Wer-Wolf Wer-Wolf commented Jun 22, 2024

Contribution description

This PR adds a generic HMAC implementation for the PSA crypto API. This HMAC implementation can work with all
hashing algorithms already supported by the PSA crypto API.

This means that in order to add support for a new HMAC algorithm, only the hashing algorithm implementation is necessary,
the rest will be handled by the generic HMAC.

In order to support the full PSA MAC API, this PR also adds support for the multi-part MAC API. It also removes support for hardware acceleration of the SHA-256 HMAC, since a full-fledged hardware acceleration is expected to be provided in the near future.

Last but not least, a unittest for the generic HMAC is provided.

Testing procedure

The test-hashes unittest also tests the generic HMAC, so running those tests should be suitable to test the generic HMAC.

This PR depends on #20698, and also depends on a follow-up PR which will reintroduce proper hardware acceleration.

@Wer-Wolf Wer-Wolf requested a review from miri64 as a code owner June 22, 2024 21:36
@github-actions github-actions bot added Area: doc Area: Documentation Area: tests Area: tests and testing framework Area: sys Area: System labels Jun 22, 2024
@mguetschow mguetschow self-requested a review June 24, 2024 07:39
@mguetschow mguetschow added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Jun 24, 2024
@riot-ci
Copy link

riot-ci commented Jun 24, 2024

Murdock results

✔️ PASSED

3606e9b tests/sys/psa_crypto_mac: Add tests for generic HMAC implementations

Success Failures Total Runtime
146606 0 146606 03h:17m:36s

Artifacts

Copy link
Contributor

@mguetschow mguetschow left a comment

Choose a reason for hiding this comment

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

Thorough implementation, good coding style overall, well done! I have some comments inline below :)

Comment on lines 409 to 413
ifneq (,$(filter psa_mac_hmac_sha_512_backend_riot,$(USEMODULE)))
USEMODULE += psa_hash
USEMODULE += psa_hash_sha_512
USEMODULE += psa_riot_mac_hmac_generic
endif
Copy link
Contributor

Choose a reason for hiding this comment

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

This is (obviously) still missing the logic for SHA-3 support. Will need to be added after #20698 is merged. Just leaving the note here to not forget about it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, i should have mentioned that.

sys/psa_crypto/doc.txt Outdated Show resolved Hide resolved
sys/psa_crypto/doc.txt Outdated Show resolved Hide resolved
sys/psa_crypto/doc.txt Show resolved Hide resolved
sys/psa_crypto/include/psa_hmac.h Outdated Show resolved Hide resolved
tests/unittests/tests-hashes/tests-hashes-sha384-hmac.c Outdated Show resolved Hide resolved
tests/unittests/tests-hashes/tests-hashes-sha384-hmac.c Outdated Show resolved Hide resolved
tests/unittests/tests-hashes/tests-hashes-sha384-hmac.c Outdated Show resolved Hide resolved
tests/unittests/tests-hashes/tests-hashes-sha384-hmac.c Outdated Show resolved Hide resolved
tests/unittests/tests-hashes/tests-hashes.h Outdated Show resolved Hide resolved
Copy link
Contributor

@Einhornhool Einhornhool left a comment

Choose a reason for hiding this comment

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

This looks nice and it's a good idea to have a generic implementation.
I added a few comments :)

sys/psa_crypto/psa_hmac.c Outdated Show resolved Hide resolved
sys/psa_crypto/doc.txt Outdated Show resolved Hide resolved
sys/psa_crypto/doc.txt Outdated Show resolved Hide resolved
sys/psa_crypto/psa_hmac.c Outdated Show resolved Hide resolved
@mguetschow
Copy link
Contributor

Thanks @Wer-Wolf for addressing all the comments, looks good to me from the implementation side. There are still some open change requests on @daria-gauster's test code, would someone of you mind addressing those too?

@Wer-Wolf
Copy link
Contributor Author

Wer-Wolf commented Jul 9, 2024

I will take care of this too.

Copy link
Contributor

@mguetschow mguetschow left a comment

Choose a reason for hiding this comment

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

Some more comments below :)

tests/unittests/tests-hashes/Makefile.include Outdated Show resolved Hide resolved
tests/unittests/tests-hashes/tests-hashes.c Outdated Show resolved Hide resolved
tests/unittests/tests-hashes/tests-hashes.h Outdated Show resolved Hide resolved
tests/sys/psa_crypto_mac/Makefile Outdated Show resolved Hide resolved
tests/sys/psa_crypto_mac/Makefile Show resolved Hide resolved
tests/sys/psa_crypto_mac/Makefile Outdated Show resolved Hide resolved
tests/sys/psa_crypto_mac/Makefile Outdated Show resolved Hide resolved
@mguetschow
Copy link
Contributor

Also the current test implementation fails on the CI: https://ci.riot-os.org/details/f921c2a1211244a0b9ee984ef457e915

I'd propose to mimic the other tests, then it should work.

@mguetschow mguetschow added CI: full build disable CI build filter CI: no fast fail don't abort PR build after first error CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR and removed CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR labels Jul 26, 2024
Copy link
Contributor

@mguetschow mguetschow left a comment

Choose a reason for hiding this comment

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

Nice work on the tests as well! Just minor suggestions.

After updating the boards in the Makefile.ci that don't fit the test application anymore (CI is currently generating a list of compile failures), you may squash the commits together before a last round of review.

tests/sys/psa_crypto_mac/Makefile Show resolved Hide resolved
tests/sys/psa_crypto_mac/Makefile Outdated Show resolved Hide resolved
tests/sys/psa_crypto_mac/test_hmac_sha384.c Outdated Show resolved Hide resolved
tests/sys/psa_crypto_mac/test_hmac_sha384.c Outdated Show resolved Hide resolved
tests/sys/psa_crypto_mac/test_hmac_sha384.c Outdated Show resolved Hide resolved
tests/sys/psa_crypto_mac/test_hmac_sha512.c Outdated Show resolved Hide resolved
tests/sys/psa_crypto_mac/tests_gen_hmac.h Outdated Show resolved Hide resolved
@mguetschow
Copy link
Contributor

Ah, and now that #20698 has been merged, this one probably needs some updates to support SHA-3 as well.

@Wer-Wolf
Copy link
Contributor Author

Ah, and now that #20698 has been merged, this one probably needs some updates to support SHA-3 as well.

I suggest that this happens in a separate PR.

@Wer-Wolf
Copy link
Contributor Author

Are the reworked tests ok?

Copy link
Contributor

@mguetschow mguetschow left a comment

Choose a reason for hiding this comment

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

Just had a quick look at your changes on the test code. Two comments below.

tests/sys/psa_crypto_mac/test_hmac_sha384.c Show resolved Hide resolved
tests/sys/psa_crypto_mac/test_hmac_sha384.c Show resolved Hide resolved
@mguetschow
Copy link
Contributor

Ah, and now that #20698 has been merged, this one probably needs some updates to support SHA-3 as well.

I suggest that this happens in a separate PR.

Sounds like a good idea to keep PRs small :)

@mguetschow
Copy link
Contributor

Feel free to squash the changes together for a hopefully last round of review :)

@Wer-Wolf
Copy link
Contributor Author

Wer-Wolf commented Aug 7, 2024

I found some issues while working on the HMAC hardware acceleration. If the fixup commits are fine, then i can squash them into the other commits too.

Wer-Wolf and others added 11 commits August 22, 2024 18:59
The PSA crypto specification states that when creating keys,
the usage flags PSA_KEY_USAGE_SIGN_HASH/PSA_KEY_USAGE_VERIFY_HASH
automatically set the usage flags
PSA_KEY_USAGE_SIGN_MESSAGE/PSA_KEY_USAGE_VERIFY_MESSAGE on the key.

Signed-off-by: Armin Wolf <W_Armin@gmx.de>
Prepare to support the multi-part MAC API by creating appropriate
dispatchers for both algorithm and location backends.

Since there are no supported backends at the moment, the dispatcher
always returns PSA_ERROR_NOT_SUPPORTED for now.

Signed-off-by: Armin Wolf <W_Armin@gmx.de>
The initial implementation was inspired by MbedTLS, with the
addition of the MD2 and MD4 algorithms.

Signed-off-by: Armin Wolf <W_Armin@gmx.de>
This support macro will be needed by the generic hmac
implementation.

Signed-off-by: Armin Wolf <W_Armin@gmx.de>
This additional macro will be used by the generic hmac
implementation to calculate the size of the internal
buffers.

Signed-off-by: Armin Wolf <W_Armin@gmx.de>
Add a generic HMAC implementation based on the PSA hashing API.
In order to support a specific HMAC algorithm, all what has to be
implemented is a backend for the PSA hashing API.

Signed-off-by: Armin Wolf <W_Armin@gmx.de>
The generic HMAC implementation can only be used by going through
the dispatcher. Do the necessary wire-up so that applications using
the PSA crypto API can use the generic HMAC implementation.

Signed-off-by: Armin Wolf <W_Armin@gmx.de>
The old HMAC implementation only supported the SHA256 hashing algorithm
and only implemented the single-part MAC function.
Replace it with the generic HMAC implementation which supports all
hashing algorithms and is already used for the multi-part MAC functions.

A side effect of this commit is that the cryptocell HMAC implementation
is not used anymore. This will be fixed in a later commit which
introduces broad hardare-acceleration for HMAC.

Signed-off-by: Armin Wolf <W_Armin@gmx.de>
Implement the PSA MAC verification API. Currently only the generic
HMAC backend is available for MAC verification, but hardware-accelerated
backends can be added later.

Signed-off-by: Armin Wolf <W_Armin@gmx.de>
Add some documentation regarding the steps for adding support
for new HMAC algorithms to the generic HMAC implementation.

Signed-off-by: Armin Wolf <W_Armin@gmx.de>
Add tests for the generic HMAC implementation.

Authored-by: Daria Zatokovenko <daria.zatokovenko@gmail.com>
Signed-off-by: Armin Wolf <W_Armin@gmx.de>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: doc Area: Documentation Area: sys Area: System Area: tests Area: tests and testing framework CI: full build disable CI build filter CI: no fast fail don't abort PR build after first error CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants