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

Alternative random generator support for PSA #3895

Merged

Conversation

gilles-peskine-arm
Copy link
Contributor

@gilles-peskine-arm gilles-peskine-arm commented Nov 18, 2020

Allow the PSA subsystem to use a random generator other than what is provided by ctr_drbg.c + entropy.c.

Future work, out of scope of this PR:

  • Allow non-PSA code (such as TLS with MBEDTLS_USE_PSA_CRYPTO) to use the PSA RNG no matter how it's implemented. (Define an RNG instance based on the PSA RNG #3883).
  • Full support for random drivers, including add_entropy.
  • Support the PSA get_entropy interface.

@gilles-peskine-arm gilles-peskine-arm added enhancement needs-review Every commit must be reviewed by at least two team members, component-crypto Crypto primitives and low-level interfaces PSA compliance needs-reviewer This PR needs someone to pick it up for review labels Nov 18, 2020
@gilles-peskine-arm gilles-peskine-arm self-assigned this Nov 18, 2020
@gabor-mezei-arm gabor-mezei-arm self-requested a review November 19, 2020 09:43
library/psa_crypto.c Show resolved Hide resolved
library/psa_crypto.c Show resolved Hide resolved
Subsequent commits will move declarations and definitions there.

Signed-off-by: Gilles Peskine <[email protected]>
Create wrapper functions around calls to CTR_DRBG and around calls to
entropy+DRBG. This is in preparation for allowing alternative DRBG
implementations that use the Mbed TLS entropy module, or complete RNG
implementations that bypass the entropy module as well.

This is purely a refactoring commit. No behavior change.

Signed-off-by: Gilles Peskine <[email protected]>
Create a configuration option for autonomous random drivers, i.e. PSA
crypto drivers that provide a random generator, that have their own
entropy source and do not support injecting entropy from another
source.

This commit only creates the configuration option. Subsequent commits
will add the implementation and tests.

Signed-off-by: Gilles Peskine <[email protected]>
Define a sample type mbedtls_psa_external_random_context_t in
psa/crypto_platform.h and define the prototype of
mbedtls_psa_external_get_random() in a public header.

Signed-off-by: Gilles Peskine <[email protected]>
psa_crypto must be able to convert error codes even from modules that
it doesn't call directly.

Signed-off-by: Gilles Peskine <[email protected]>
@gilles-peskine-arm
Copy link
Contributor Author

I rebased on top of development because there was a (trivial) conflict. The original version is at https://github.com/gilles-peskine-arm/mbedtls/tree/psa-external-random-1. The merge of that with development at 9aaa3e1 is https://github.com/gilles-peskine-arm/mbedtls/tree/psa-external-random-1-merge which is identical to the rebased version at https://github.com/gilles-peskine-arm/mbedtls/tree/psa-external-random-2.

Next up: another rebase for all.sh changes.

Implement support for MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG.

For test purposes, write an implementation that uses libc rand().

Signed-off-by: Gilles Peskine <[email protected]>
Support using HMAC_DRBG instead of CTR_DRBG in the PSA subsystem.

Use HMAC_DRBG if CTR_DRBG is available. Choose between SHA-256 and
SHA-512 based on availability.

Signed-off-by: Gilles Peskine <[email protected]>
Allow the user to configure PSA to use HMAC_DRBG even if CTR_DRBG is
available, or to explicitly select the hash algorithm to use for
HMAC_DRBG, by setting MBEDTLS_PSA_HMAC_DRBG_MD_TYPE in config.h.

Signed-off-by: Gilles Peskine <[email protected]>
We generate the Doxygen documentation in a configuration where part of
config.h is excluded. See
Mbed-TLS#520
```
/var/lib/build/include/mbedtls/config.h:3635: warning: documentation for unknown define MBEDTLS_PSA_HMAC_DRBG_MD_TYPE found.
```

This is a more general issue and fixing it is out of scope of my
current work. Therefore, just do something simple to silence Doxygen,
and never mind that this causes the documentation of
`MBEDTLS_PSA_HMAC_DRBG_MD_TYPE` to be omitted from the rendered
documentation. We'll fix that when we fix all the configuration macros
with a similar problem.

Signed-off-by: Gilles Peskine <[email protected]>
Signed-off-by: Gilles Peskine <[email protected]>
@daverodgman daverodgman removed the needs-reviewer This PR needs someone to pick it up for review label Dec 1, 2020
include/psa/crypto_extra.h Outdated Show resolved Hide resolved
library/psa_crypto_random.h Outdated Show resolved Hide resolved
Add and document PSA_ERROR_INSUFFICIENT_ENTROPY.

Signed-off-by: Gilles Peskine <[email protected]>
@mpg
Copy link
Contributor

mpg commented Dec 14, 2020

Coming back at this from my "review requested" list, I can see that both Ronald and Gabor already did some review on this PR, while I don't think I did yet, so I'm removing myself from the reviewers list. If it turns out that Ronald or Gabor's no longer available, or that for some reason a review from me specifically was desired, please let me know.

@mpg mpg removed their request for review December 14, 2020 10:21
Hide the obtention of the pointer to the RNG state behind a macro.

To make it possible to use this macro in a module other than
psa_crypto.c, which will happen in the future, make sure that the
definition of the macro does not reference internal variables of
psa_crypto.c. For this purpose, in the internal-DRBG case, export a
symbol containing the address of the DRBG state.

When the RNG state is a pointer a DRBG state, just keep this pointer
in a variable: there's no need to store a pointer to a larger structure.

Signed-off-by: Gilles Peskine <[email protected]>
In the external RNG case, don't make mbedtls_psa_get_random() a
static inline function: this would likely result in identical
instances of this function in every module that uses it. Instead, make
it a single function with external linkage.

In the non-external case, instead of a trivial wrapper function, make
mbedtls_psa_get_random a constant pointer to whichever DRBG function
is being used.

Signed-off-by: Gilles Peskine <[email protected]>
Make it clear that this is an abstraction of the random generator
abstraction, and not an abstraction of the PSA random generator.

mbedtls_psa_get_random and MBEDTLS_PSA_RANDOM_STATE are public-facing
definitions and will be moved in a subsequent commit.

Signed-off-by: Gilles Peskine <[email protected]>
Signed-off-by: Gilles Peskine <[email protected]>
library/psa_crypto_random.h Outdated Show resolved Hide resolved
include/psa/crypto_extra.h Show resolved Hide resolved
include/psa/crypto_platform.h Outdated Show resolved Hide resolved
library/psa_crypto.c Outdated Show resolved Hide resolved
library/psa_crypto.c Show resolved Hide resolved
library/psa_crypto_random_impl.h Show resolved Hide resolved
library/psa_crypto_random_impl.h Show resolved Hide resolved
Signed-off-by: Gilles Peskine <[email protected]>
Make the code slightly more readable and slightly smaller.

Signed-off-by: Gilles Peskine <[email protected]>
MBEDTLS_PSA_RANDOM_MAX_REQUEST :
output_size );
ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
output, request_size );
Copy link
Contributor

Choose a reason for hiding this comment

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

We have to get out of the loop if ret is not equal to zero.

library/psa_crypto.c Show resolved Hide resolved
If a call to mbedtls_psa_get_random other than the last one failed,
this went undetected.

Signed-off-by: Gilles Peskine <[email protected]>
Copy link
Contributor

@ronald-cron-arm ronald-cron-arm left a comment

Choose a reason for hiding this comment

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

Thanks for the changes, additional comments and discussions. This looks good to me now.

* The PSA crypto subsystem can now use HMAC_DRBG instead of CTR_DRBG.
CTR_DRBG is used by default if it is available, but you can override
this choice by setting MBEDTLS_PSA_HMAC_DRBG_MD_TYPE at compile time.
Fix #3354.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

By the way, I'm deliberately not mentioning external RNG support in the changelog in this PR. I'll add a changelog entry in the follow-up that makes an external PSA RNG usable from TLS code.

@gilles-peskine-arm gilles-peskine-arm merged commit a51e1db into Mbed-TLS:development Jan 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component-crypto Crypto primitives and low-level interfaces enhancement needs-review Every commit must be reviewed by at least two team members,
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Break the dependency of psa_crypto on software AES Support hmac_drbg in PSA crypto
6 participants