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 API implementation #18547

Merged
merged 8 commits into from Sep 4, 2023

Conversation

Einhornhool
Copy link
Contributor

@Einhornhool Einhornhool commented Sep 2, 2022

Contribution description

This adds an implementation of the ARM PSA Crypto API specification to RIOT.

It is a cryptographic API that supports software and hardware backends as well as the use of multiple secure elements, which can be configured with Kconfig.
It integrates indirect, identifier based key management to support persistent storage of key material in local memory and devices with protected key storage.

A description of the implementation design and an evaluation of the processing time and memory overhead in RIOT has been published here: Usable Security for an IoT OS: Integrating the Zoo of Embedded Crypto Components Below a Common API

Implementation status

So far this implementation supports the following operations:

  • Volatile key storage
  • AES in CBC mode
  • Hashes (MD5, SHA1, SHA224, SHA256)
  • HMAC SHA256
  • ECDSA with NIST P192 and P256 curves

The following backends are supported so far:

  • RIOT Cipher Module
  • RIOT Hash Module
  • Micro ECC library package
  • Cryptocell 310 hardware accelerator on the Nordic NRF52840dk
  • Microchip ATECC608A secure element

Other operations and backends as well as persistent key storage can and will be implemented by me and anyone who wants to contribute in the future.

Testing procedure

So far there is a show case application in examples/psa_crypto to demonstrate the usage and configuration of different backends of the API (refer to the application README for more information).

@github-actions github-actions bot added Area: boards Area: Board ports Area: build system Area: Build system Area: doc Area: Documentation Area: examples Area: Example Applications Area: Kconfig Area: Kconfig integration Area: pkg Area: External package ports Area: sys Area: System labels Sep 2, 2022
@benpicco
Copy link
Contributor

benpicco commented Sep 2, 2022

Nice work!

But a single 20kSLOC commit is a bit hard to handle - can you please split this up into more manageable chunks? Ideally even multiple PRs since there seem to be several things in there that could be merged independently.

@Einhornhool
Copy link
Contributor Author

Thank you!
I know it's big and I can definitely split it up for reviewing.
If it's okay, I would like to keep it this way until after the summit next week (there probably is going to be a discussion about PSA Crypto in RIOT), so people who are interested can take a look at how everything is supposed to work together.

@benpicco
Copy link
Contributor

benpicco commented Sep 2, 2022

You can of course do this in whatever time scale suits you best, but splitting this into multiple commits would be a good first step independent of whether the PR should be split or not.

@Einhornhool
Copy link
Contributor Author

Alright, I will do that

Copy link
Member

@maribu maribu left a comment

Choose a reason for hiding this comment

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

Lots of style nitpicks. I didn't look at all and event failed to grasp the high level picture yet, as it is really hard to read code on a mobile phone :( I will provide better feadback one I am back at a keyboard.

boards/common/nrf52/include/cfg_i2c_default.h Outdated Show resolved Hide resolved
examples/atecc608a_configure_and_lock/main.c Outdated Show resolved Hide resolved
examples/atecc608a_configure_and_lock/main.c Outdated Show resolved Hide resolved
examples/atecc608a_configure_and_lock/main.c Outdated Show resolved Hide resolved
examples/psa_crypto/aes_cbc.c Outdated Show resolved Hide resolved
pkg/driver_cryptocell/psa_cc/hashes_common.c Outdated Show resolved Hide resolved
pkg/driver_cryptocell/psa_cc/hashes_common.c Outdated Show resolved Hide resolved
pkg/driver_cryptocell/psa_cc/hashes_sha1.c Outdated Show resolved Hide resolved
pkg/driver_cryptocell/psa_cc/hashes_sha1.c Outdated Show resolved Hide resolved
pkg/driver_cryptocell/psa_cc/hashes_sha1.c Outdated Show resolved Hide resolved
Copy link
Contributor

@MrKevinWeiss MrKevinWeiss left a comment

Choose a reason for hiding this comment

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

Maybe it would be nice to add a simple automatic test to the CI can test it as well?

diff --git a/examples/psa_crypto/tests/01-run.py b/examples/psa_crypto/tests/01-run.py
new file mode 100755
index 0000000000..25257b8ca8
--- /dev/null
+++ b/examples/psa_crypto/tests/01-run.py
@@ -0,0 +1,13 @@
+#!/usr/bin/env python3
+
+import sys
+from testrunner import run
+
+
+def testfunc(child):
+    child.expect_exact('All Done')
+    print("[TEST PASSED]")
+
+
+if __name__ == "__main__":
+    sys.exit(run(testfunc))

@@ -0,0 +1,14 @@
PKG_NAME=driver_cryptocell
PKG_URL=https://github.com/Einhornhool/RIOT-cryptocell-driver.git
Copy link
Contributor

Choose a reason for hiding this comment

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

This package points to an archive (.a) only, with headers repository. That's quite limited: only nrf52840 cpu is supported. Is there a possibility and a plan to provide an open-source implementation ?

Also note that the package is too much limited IMHO: it can only be built for nrf52840dk board but I think any nrf52840 CPU based board should work with 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.

Unfortunately this driver is closed source and there don't seem to be any plans to make it open source.
I got this from the Nordic SDK, where they only provide the archives and header files. There is a Git Repositiory (nrfxlib), which also provides archive files, but only headers with SHA 256 and ECC P256 support.

The limitation is my mistake, I did not realize that the nrf52840 also had an accelerator, I will fix that.

boards/nrf52840dk/Kconfig Outdated Show resolved Hide resolved
boards/common/nrf52/include/cfg_i2c_default.h Outdated Show resolved Hide resolved
@github-actions github-actions bot added Area: cpu Area: CPU/MCU ports Platform: ARM Platform: This PR/issue effects ARM-based platforms labels Sep 7, 2022
Copy link
Contributor

@kfessel kfessel left a comment

Choose a reason for hiding this comment

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

wow this is a great PR
since you already said you want to split it, I just had a first glance and found some small nit picks.

@@ -21,6 +21,7 @@
#define CFG_I2C_DEFAULT_H

#include "periph_cpu.h"
#include "kernel_defines.h"
Copy link
Contributor

Choose a reason for hiding this comment

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

are the "kernel_defines.h" needed in this header? (cant see anything being added that requires them)

Copy link
Contributor

Choose a reason for hiding this comment

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

resolved in 18582

Copy link
Contributor

Choose a reason for hiding this comment

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

No resolved here... Please remove

boards/common/nrf52/include/cfg_i2c_default.h Outdated Show resolved Hide resolved
pkg/driver_cryptocell/include/psa_error.h Outdated Show resolved Hide resolved
pkg/driver_cryptocell/psa_cc/ecc_p192.c Outdated Show resolved Hide resolved
pkg/driver_cryptocell/psa_cc/ecc_p256.c Outdated Show resolved Hide resolved
@Einhornhool
Copy link
Contributor Author

I think I'm done.

@MrKevinWeiss
Copy link
Contributor

bors try

bors bot added a commit that referenced this pull request Aug 31, 2023
@MrKevinWeiss
Copy link
Contributor

just to see everything :)

@bors
Copy link
Contributor

bors bot commented Aug 31, 2023

try

Build failed:

@miri64
Copy link
Member

miri64 commented Aug 31, 2023

Just one ATmega being too small for this :-)

Copy link
Contributor

@MrKevinWeiss MrKevinWeiss left a comment

Choose a reason for hiding this comment

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

If everything works I would give it my ACK. It is just a matter of @Teufelchen1 to finish checking the changes!

@MrKevinWeiss
Copy link
Contributor

If everyone is happy then.

@MrKevinWeiss
Copy link
Contributor

bors merge

@bors
Copy link
Contributor

bors bot commented Sep 4, 2023

🕐 Waiting for PR status (GitHub check) to be set, probably by CI. Bors will automatically try to run when all required PR statuses are set.

@bors
Copy link
Contributor

bors bot commented Sep 4, 2023

Build succeeded!

The publicly hosted instance of bors-ng is deprecated and will go away soon.

If you want to self-host your own instance, instructions are here.
For more help, visit the forum.

If you want to switch to GitHub's built-in merge queue, visit their help page.

@bors bors bot merged commit 9be022a into RIOT-OS:master Sep 4, 2023
25 checks passed
@Ollrogge
Copy link
Contributor

Ollrogge commented Sep 4, 2023

Congrats @Einhornhool !!

@miri64
Copy link
Member

miri64 commented Sep 4, 2023

🎉

bors bot added a commit that referenced this pull request Oct 10, 2023
19954: sys/psa_crypto: Ed25519 (EdDSA) support r=benpicco a=mguetschow

### Contribution description

- implement [`psa_sign_message()`](https://armmbed.github.io/mbed-crypto/html/api/ops/sign.html#c.psa_sign_message) and [`psa_verify_message()`](https://armmbed.github.io/mbed-crypto/html/api/ops/sign.html#c.psa_verify_message) for the two already supported [`PSA_ALG_ECDSA`](https://armmbed.github.io/mbed-crypto/html/api/ops/sign.html#c.PSA_ALG_ECDSA) algorithms, together with the CryptoCell and `micro-ecc` backends (*not* for the SE backend)
- add support for [`PSA_ALG_PURE_EDDSA`](https://armmbed.github.io/mbed-crypto/html/api/ops/sign.html#c.PSA_ALG_PURE_EDDSA), together with the CryptoCell hardware and `c25519` software backend (*not* for the SE backend)
- wipe private key data from stack for both ECDSA and EdDSA algorithms using `explicit_bzero()` (opinions from experienced Riot maintainers about usage of `goto` to avoid duplicating that function call before every `return`?)


### Testing procedure

- `examples/psa_crypto` has been updated to include EdDSA
- successfully tested configurations:
  - `nrf52840dk` with cryptocell (hardware) and `c25519` (software) backend
  - `native` with software backend


### Issues/PRs references

Thanks `@Einhornhool` for the PSA Crypto framework implementation #18547  which is great to work with!

19966: sys/event: add event_is_queued() r=benpicco a=fabian18



Co-authored-by: Mikolai Gütschow <mikolai.guetschow@tu-dresden.de>
Co-authored-by: Fabian Hüßler <fabian.huessler@ml-pa.com>
bors bot added a commit that referenced this pull request Oct 11, 2023
19954: sys/psa_crypto: Ed25519 (EdDSA) support r=miri64 a=mguetschow

### Contribution description

- implement [`psa_sign_message()`](https://armmbed.github.io/mbed-crypto/html/api/ops/sign.html#c.psa_sign_message) and [`psa_verify_message()`](https://armmbed.github.io/mbed-crypto/html/api/ops/sign.html#c.psa_verify_message) for the two already supported [`PSA_ALG_ECDSA`](https://armmbed.github.io/mbed-crypto/html/api/ops/sign.html#c.PSA_ALG_ECDSA) algorithms, together with the CryptoCell and `micro-ecc` backends (*not* for the SE backend)
- add support for [`PSA_ALG_PURE_EDDSA`](https://armmbed.github.io/mbed-crypto/html/api/ops/sign.html#c.PSA_ALG_PURE_EDDSA), together with the CryptoCell hardware and `c25519` software backend (*not* for the SE backend)
- wipe private key data from stack for both ECDSA and EdDSA algorithms using `explicit_bzero()` (opinions from experienced Riot maintainers about usage of `goto` to avoid duplicating that function call before every `return`?)


### Testing procedure

- `examples/psa_crypto` has been updated to include EdDSA
- successfully tested configurations:
  - `nrf52840dk` with cryptocell (hardware) and `c25519` (software) backend
  - `native` with software backend


### Issues/PRs references

Thanks `@Einhornhool` for the PSA Crypto framework implementation #18547  which is great to work with!

Co-authored-by: Mikolai Gütschow <mikolai.guetschow@tu-dresden.de>
@MrKevinWeiss MrKevinWeiss added this to the Release 2023.10 milestone Nov 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: boards Area: Board ports Area: build system Area: Build system Area: cpu Area: CPU/MCU ports Area: doc Area: Documentation Area: examples Area: Example Applications Area: Kconfig Area: Kconfig integration Area: pkg Area: External package ports Area: sys Area: System Area: tests Area: tests and testing framework Area: tools Area: Supplementary tools CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Platform: ARM Platform: This PR/issue effects ARM-based platforms
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet