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: Fix build problems #19992

Merged
merged 2 commits into from
Dec 1, 2023

Conversation

Einhornhool
Copy link
Contributor

@Einhornhool Einhornhool commented Oct 19, 2023

Contribution description

This fixes several problems:

1. Empty union in cipher context when MODULE_PSA_CIPHER is not selected.

PSA operations are now separated into modules. Functions and contexts are only built when the corresponding module is selected. This way there won't be problems with missing or unitialized structures in unused modules anymore.

2. Zero-size array when using secure elements and PSA_MAX_KEY_DATA_SIZE == 0

I added a condition to the psa_key_slot_t structure in psa_key_slot_management.h.
Also the existence of key slot management functions and key slot structures now depends on the number of allocated key slots instead of selected modules.
This way key structures will not exist unless they are used.

Testing procedure

Add the following to examples/hello_world/Makefile and call make :

USEMODULE += psa_crypto
USEMODULE += psa_hash
USEMODULE += psa_hash_sha_256
USEMODULE += psa_secure_element

Output on Master:

"make" -C /home/lena/work/RIOT/boards/common/init
"make" -C /home/lena/work/RIOT/boards/native
"make" -C /home/lena/work/RIOT/boards/native/drivers
"make" -C /home/lena/work/RIOT/core
"make" -C /home/lena/work/RIOT/core/lib
"make" -C /home/lena/work/RIOT/cpu/native
"make" -C /home/lena/work/RIOT/cpu/native/periph
"make" -C /home/lena/work/RIOT/cpu/native/stdio_native
"make" -C /home/lena/work/RIOT/drivers
"make" -C /home/lena/work/RIOT/drivers/periph_common
"make" -C /home/lena/work/RIOT/sys
"make" -C /home/lena/work/RIOT/sys/auto_init
"make" -C /home/lena/work/RIOT/sys/libc
"make" -C /home/lena/work/RIOT/sys/luid
"make" -C /home/lena/work/RIOT/sys/preprocessor
"make" -C /home/lena/work/RIOT/sys/psa_crypto
In file included from /home/lena/work/RIOT/sys/include/psa_crypto/psa/crypto.h:39,
                 from /home/lena/work/RIOT/sys/psa_crypto/psa_crypto_algorithm_dispatch.c:23:
/home/lena/work/RIOT/sys/include/psa_crypto/psa/crypto_struct.h:137:11: error: union has no members [-Werror=pedantic]
  137 |     union cipher_context {
      |           ^~~~~~~~~~~~~~
In file included from /home/lena/work/RIOT/sys/psa_crypto/include/psa_crypto_operation_encoder.h:32,
                 from /home/lena/work/RIOT/sys/psa_crypto/psa_crypto_algorithm_dispatch.c:28:
/home/lena/work/RIOT/sys/psa_crypto/include/psa_crypto_slot_management.h:82:17: error: ISO C forbids zero-size array ‘data’ [-Werror=pedantic]
   82 |         uint8_t data[PSA_MAX_KEY_DATA_SIZE];    /**< Key data buffer */
      |                 ^~~~
cc1: all warnings being treated as errors
make[3]: *** [/home/lena/work/RIOT/Makefile.base:146: /home/lena/work/RIOT/examples/hello-world/bin/native/psa_crypto/psa_crypto_algorithm_dispatch.o] Error 1
make[2]: *** [/home/lena/work/RIOT/Makefile.base:31: ALL--/home/lena/work/RIOT/sys/psa_crypto] Error 2
make[1]: *** [/home/lena/work/RIOT/Makefile.base:31: ALL--/home/lena/work/RIOT/sys] Error 2
make: *** [/home/lena/work/RIOT/examples/hello-world/../../Makefile.include:761: application_hello-world.module] Error 2

Output with fixes:

"make" -C /home/lena/work/RIOT/boards/common/init
"make" -C /home/lena/work/RIOT/boards/native
"make" -C /home/lena/work/RIOT/boards/native/drivers
"make" -C /home/lena/work/RIOT/core
"make" -C /home/lena/work/RIOT/core/lib
"make" -C /home/lena/work/RIOT/cpu/native
"make" -C /home/lena/work/RIOT/cpu/native/periph
"make" -C /home/lena/work/RIOT/cpu/native/stdio_native
"make" -C /home/lena/work/RIOT/drivers
"make" -C /home/lena/work/RIOT/drivers/periph_common
"make" -C /home/lena/work/RIOT/sys
"make" -C /home/lena/work/RIOT/sys/auto_init
"make" -C /home/lena/work/RIOT/sys/libc
"make" -C /home/lena/work/RIOT/sys/luid
"make" -C /home/lena/work/RIOT/sys/preprocessor
"make" -C /home/lena/work/RIOT/sys/psa_crypto
"make" -C /home/lena/work/RIOT/sys/psa_crypto/psa_key_slot_mgmt
"make" -C /home/lena/work/RIOT/sys/psa_crypto/psa_se_mgmt
"make" -C /home/lena/work/RIOT/sys/random
/usr/bin/ld: warning: /home/lena/work/RIOT/examples/hello-world/bin/native/hello-world.elf has a LOAD segment with RWX permissions
   text	   data	    bss	    dec	    hex	filename
  29764	    584	  47856	  78204	  1317c	/home/lena/work/RIOT/examples/hello-world/bin/native/hello-world.elf

@github-actions github-actions bot added Area: doc Area: Documentation Area: pkg Area: External package ports Area: sys Area: System Area: examples Area: Example Applications Area: Kconfig Area: Kconfig integration labels Oct 19, 2023
@Einhornhool Einhornhool added Type: bug The issue reports a bug / The PR fixes a bug (including spelling errors) and removed Area: doc Area: Documentation Area: pkg Area: External package ports Area: examples Area: Example Applications Area: Kconfig Area: Kconfig integration labels Oct 19, 2023
@Einhornhool Einhornhool changed the title Fix PSA Crypto Bugs sys/psa_crypto: Fix build problems and Oct 19, 2023
@Einhornhool Einhornhool changed the title sys/psa_crypto: Fix build problems and sys/psa_crypto: Fix build problems Oct 19, 2023
@github-actions github-actions bot added Area: doc Area: Documentation Area: pkg Area: External package ports Area: examples Area: Example Applications Area: Kconfig Area: Kconfig integration labels Oct 19, 2023
@Einhornhool Einhornhool force-pushed the dev/psa-crypto-fixes branch 2 times, most recently from e669c7d to 52a7186 Compare October 19, 2023 14:56
@MrKevinWeiss MrKevinWeiss added the Process: needs backport Integration Process: The PR is required to be backported to a release or feature branch label Oct 20, 2023
@benpicco benpicco added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Oct 20, 2023
@riot-ci
Copy link

riot-ci commented Oct 20, 2023

Murdock results

✔️ PASSED

98e9016 tests/sys/psa_crypto_*: Update BOARD_INSUFFICIENT_MEMORY

Success Failures Total Runtime
8082 0 8082 11m:51s

Artifacts

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.

Though I am not really in the PSA field I think it looks OK, and this does fix known issues. only nitpick comments there... Maybe it would be good to post the configurations that were tested. If anyone else wants to go more in-depth that would be appreciated but it would be nice to get this backported for the release. ping @Teufelchen1 maybe?

@@ -29,5 +29,7 @@ endif

ifneq (,$(filter psa_crypto,$(USEMODULE)))
Copy link
Contributor

Choose a reason for hiding this comment

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

I actually don't think we need the ifneq for PSEUDOMODULES, this can be done in another PR but also... there is a lot of cleanup happening anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

Comment on lines -31 to -58
/**
* @brief Structure containing a hash context and algorithm
*/
struct psa_hash_operation_s {
psa_algorithm_t alg; /**< Operation algorithm */
#if IS_USED(MODULE_PSA_HASH)
psa_hash_context_t ctx; /**< Operation hash context */
#endif
};

/**
* @brief This macro returns a suitable initializer for a hash operation object of type
* @ref psa_hash_operation_t.
*/
#define PSA_HASH_OPERATION_INIT { 0 }

/**
* @brief Return an initial value for a hash operation object.
*
* @return struct psa_hash_operation_s
*/
static inline struct psa_hash_operation_s psa_hash_operation_init(void)
{
const struct psa_hash_operation_s v = PSA_HASH_OPERATION_INIT;

return v;
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Did you need to move this chunk below? Less changes that better (at least from the review perspective).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wanted to sort them by alphabet, but of course I can change this back

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it makes sense to maintain the new order to only have one continuous block of hash-related things to be guarded by the if-directive.

@mguetschow
Copy link
Contributor

mguetschow commented Oct 23, 2023

I get a lot of compile errors with the following configuration (copied from the initial post without the secure element):

USEMODULE += psa_crypto
USEMODULE += psa_hash
USEMODULE += psa_hash_sha_256

psa/crypto.h uses struct psa_key_attributes_s which is dependent on MODULE_PSA_KEY_MANAGEMENT.

Maybe the functions in psa/crypto.h should also be guarded by #ifs, following the example of psa_crypto.c? That would also prevent users to reference a function that is not compiled (i.e. of which the respective module is not selected).

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.

Here a somewhat more in-depth review. As discussed (off-thread) before, I am in favor of the suggested changes which compile more code only on-demand, hidden behind a small set of PSA_<FEATURE> module flags, which are quite easy to reason about.

The example of the failed compilation above is just another pointer to the fact that we definitely need some sort of configuration tests in a future PR.

Comment on lines -259 to +282
USEMODULE += psa_key_slot_mgmt
USEMODULE += psa_key_management
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we have both psa_key_slot_mgmt and psa_key_management now? Seems to me like the latter is only sort of an alias for the former?

Copy link
Contributor Author

@Einhornhool Einhornhool Oct 24, 2023

Choose a reason for hiding this comment

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

It's not really the same. I separate between the PSA key management functions which are described by the specification and are exposed to the user in psa_crypto.c and psa/crypto.h vs. the key slot management module, which contains the whole key slot logic and is its own backend module.

They are dependent on each other, so if you build the key management functions, the key slot management is built, but they are separate things.

@@ -438,10 +439,10 @@
* In RIOT, module names are generated from path names, so if you create a directory for
* your sourcefiles, the module name will be the same as the directory name. It is possible
* to change that by declaring a new module name in the Makefile by adding the line
* your_module_name`.
* `MODULE := your_module_name`.
Copy link
Contributor

Choose a reason for hiding this comment

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

Kind of unrelated here, but while reading this I was wondering if there is more information about how paths are matched to module names in RIOT. Is it path/to/module => path_to_module in general? Maybe linking to further documentation, if existing, or giving an example might be worth 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.

It is indirectly mentioned here, but I haven't found a more detailed explanation, yet. I kind of figured this out on my own by using modules and submodules.

Copy link
Contributor

Choose a reason for hiding this comment

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

according to @miri64 and how I understand the wiki, it's actually path/to/module => module (just for future reference)

* @endcode
* This needs to be done for all other supported operations (e.g. ATECCX08 operations in
* `pkg/cryptoauthlib/Makefile.include`, `pkg/cryptoauthlib/Makefile.dep` and
* `sys/psa_crypto/psa_se_mgmt/Kconfig` Now the secure element should be available for use
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* `sys/psa_crypto/psa_se_mgmt/Kconfig` Now the secure element should be available for use
* `sys/psa_crypto/psa_se_mgmt/Kconfig`. Now the secure element should be available for use

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

default 0

config PSA_SINGLE_KEY_COUNT
int "Specifies number of allocated single key slots"
default 5 if PSA_MAX_KEY_SIZE != 0
default 5 if MODULE_PSA_KEY_SLOT_MGMT
Copy link
Contributor

Choose a reason for hiding this comment

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

in crypto_sizes.h, you've changed the default to 0 (same as for PSA_ASYMMETRIC_KEYPAIR_COUNT).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

@@ -1996,3 +2020,4 @@ psa_status_t psa_verify_message(psa_key_id_t key,
unlock_status = psa_unlock_key_slot(slot);
return ((status == PSA_SUCCESS) ? unlock_status : status);
}
#endif /* MODULE_PSA_ASYMMETRIC */
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe not something for this PR, but since the CI is complaining about the long file: Should we maybe split the implementation files psa_crypto.c and also psa_crypto_{algorithm,location}_dispatch.c into smaller files, each only covering the implementation of one psa module such as MODULE_PSA_ASYMMETRIC? Since we have them already separated in blocks with the #if directives.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have thought about it at one point and I am totally not opposed to it. But you're right, it's a thing for the future.

Copy link
Contributor

Choose a reason for hiding this comment

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

Tracked in #20014

@@ -7,4 +7,5 @@

config MODULE_PSA_KEY_SLOT_MGMT
bool
default y if MODULE_PSA_KEY_MANAGEMENT
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't maybe MODULE_PSA_KEY_MANAGEMENT depend on MODULE_PSA_KEY_SLOT_MGMT instead? (If we want to keep the two aliases (?))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

@@ -7,4 +7,5 @@

config MODULE_PSA_KEY_SLOT_MGMT
bool
default y if MODULE_PSA_KEY_MANAGEMENT
default y if PACKAGE_PSA_ARCH_TESTS
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this package actually exist?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

@benpicco benpicco added this pull request to the merge queue Nov 27, 2023
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Nov 28, 2023
@MrKevinWeiss
Copy link
Contributor

Looks like you need to add new boards (at least the stm32f030f4-demo to the psa_crypto_ecdasa and probably all but hashes... This moving target is really an issue. Worst case just whitelist boards instead.

@benpicco
Copy link
Contributor

You can also run

make generate-Makefile.ci

@MrKevinWeiss MrKevinWeiss added this pull request to the merge queue Nov 28, 2023
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Nov 28, 2023
@MrKevinWeiss MrKevinWeiss added this pull request to the merge queue Nov 29, 2023
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Nov 29, 2023
@MrKevinWeiss MrKevinWeiss added this pull request to the merge queue Nov 30, 2023
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to a conflict with the base branch Nov 30, 2023
@MrKevinWeiss
Copy link
Contributor

whyyyyyyy!

@MrKevinWeiss MrKevinWeiss added this pull request to the merge queue Nov 30, 2023
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Nov 30, 2023
@kaspar030 kaspar030 added this pull request to the merge queue Nov 30, 2023
Merged via the queue into RIOT-OS:master with commit a38ab98 Dec 1, 2023
25 checks passed
@MrKevinWeiss
Copy link
Contributor

Finally! Well done @Einhornhool

@mguetschow
Copy link
Contributor

whyyyyyyy!

Sorry that was me and the SHA512 support I guess :P congrats everyone that we have it in now!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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 CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR CI: ready for merge train 🚃 PR is ready to be merged and awaiting the next merge train Type: bug The issue reports a bug / The PR fixes a bug (including spelling errors)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants