-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Implement md dispatch through PSA (3.2 edition) #6474
Implement md dispatch through PSA (3.2 edition) #6474
Conversation
de45407
to
2a63a25
Compare
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
These new symbols will allow code to call the md module and benefit from PSA accelerator drivers. Code must use MBEDTLS_MD_CAN_xxx instead of MBEDTLS_xxx_C to check for support for a particular algorithm. This commit only defines the symbols. Subsequent commits will implement those symbols in the md module, and in users of the md module. Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
psa_cipher_encrypt() and psa_cipher_decrypt() sometimes add a zero offset to a null pointer when the cipher does not use an IV. This is undefined behavior, although it works as naively expected on most platforms. This can cause a crash under modern Asan (depending on compiler optimizations). Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
When MBEDTLS_MD_xxx_VIA_PSA is enabled (by mbdetls/md.h), route calls to xxx over PSA rather than through the built-in implementation. Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
Avoid repeating the exact logic to choose whether to use PSA or not, and which hashes. In this commit, I have not changed the logic: the choice of hash API is determined by MBEDTLS_USE_PSA_CRYPTO, and the choice of hash algorithm is determined by MBEDTLS_xxx_C (which is wrong when using PSA, since an algorithm could be provied by drivers instead; this will be fixed later). Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
With respect to hashes, most modules fall into one of two buckets: the high-level bucket only uses hashes through the md module or through psa, and the low-level bucket only uses hashes through direct calls to a specific sha module. There are a few exceptions where a high-level module makes direct calls to a built-in hash implementation. Annotate those to protect them from a systematic rewrite of hash algorithm dependencies that will enable support of PSA hash implementations via the MD interface. Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
These won't work if MD5 is only available via PSA, even when md.c can route via PSA. Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
In code that call the md module directly or indirectly, use the new MBEDTLS_MD_CAN_xxx symbols instead of MBEDTLS_xxx_C to detect the availability of hash algorithms. This way the code will work even when md routes via PSA, so it will benefit from PSA hash accelerators. ``` perl -i -pe 'next if /notPSA/; s/\bMBEDTLS_(MD[245]|SHA1|SHA[0-9][0-9][0-9]|RIPEMD160)_C\b/MBEDTLS_MD_CAN_$1/g' include/mbedtls/psa_util.h library/md_wrap.h library/psa_crypto_hash.c library/@(ec*|hmac_drbg|oid|pk*|rsa*|ssl*|x509*).[hc] tests/suites/test_suite_@(ec*|hmac_drbg|oid|pk*|rsa*|ssl*|x509*).@(function|data) programs/!(test)/*.c ``` Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
* - MBEDTLS_MD_SOME_PSA is defined if at least one algorithm is performed | ||
* via PSA. | ||
* - MBEDTLS_MD_SOME_LEGACY is defined if at least one algorithm is performed | ||
* via a direct legacy call. | ||
* | ||
* The md module performs an algorithm via PSA if there is a PSA hash | ||
* accelerator, and makes a direct legacy call otherwise. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On second thoughts, I've made this overly complicated. I made it so that dispatch only goes through PSA if there's a driver for the particular algorithm. But why? If there's no driver, PSA just calls the software implementation. So this can be made a lot simpler: either all algorithms dispatch through PSA, or no algorithms dispatch through PSA.
This only impacts the code in md.c
and a bit of the setup in md.h
, not the modules that use md.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Calling psa_hash_xxx
doesn't work in two cases which are currently possible and reasonable (though they won't be possible once PSA is the only interface):
- A hash algorithm is implemented in software, but deliberately excluded from the PSA configuration (possible with
MBEDTLS_PSA_CRYPTO_CONFIG
) because algorithms based on it (e.g. RSA-PSS) are accelerated and the accelerator doesn't support that hash. - The PSA function names make RPC calls to a server (
MBEDTLS_PSA_CRYPTO_CLIENT
), but there is also some local crypto including hashes (for example, there's hashes plus signature verification plus X.509).
@@ -3469,7 +3469,8 @@ psa_status_t psa_cipher_encrypt( mbedtls_svc_key_id_t key, | |||
status = psa_driver_wrapper_cipher_encrypt( | |||
&attributes, slot->key.data, slot->key.bytes, | |||
alg, local_iv, default_iv_length, input, input_length, | |||
output + default_iv_length, output_size - default_iv_length, | |||
( output == NULL ? NULL : output + default_iv_length ), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sure I've talked with someone about debugging this, but it's not fixed in development. Maybe it's in another pending PR?
We really need to get around running a recent UBSan on the CI.
One of the points of the current design is so that things work also when Also, I'm sorry but I'm not convinced about the priority-high label. I understand you're not convinced by the current design, but I don't think "re-consider the design and possibly redo a significant part of an EPIC from the previous quarter" is something you can single-handedly decide we need to do now. This will remain low on my review list until we had a team discussion about it. |
I'm not deciding this single-handedly! But we need to have this discussion quickly, because we're about to release an API extension (allowing a bunch of modules to be built without |
Hmmm, or actually we can still have the minimal glue without |
I disagree that it's not particularly useful to users: compiling without I also disagree about the urgency: now that we have the ability to extend the config, if in the future we decided to switch back to using |
To put some numbers on the code size claim (on Cortex-M0)
The difference between |
There are four parts in md:
The optimizations I mention earlier include:
It's with those optimizations that I think we can get to roughly the same code size as with |
I only had a quick look, but I think an important bit that's missing from this prototype/POC is how this impacts other modules in terms of dependency management. That is, what would it take to have something the current Obviously doing all of it would be too much to ask for a POC/prototype (unless it turns out your approach really makes it so much easier), but I think doing a subset, like PK + RSA (PKCS#1 v1.5 & v2.1), as in #6065 plus #6141, plus a reasonable idea how to generalize, would be a good starting point. |
Closing as superseded by #6977 (which cherry-picked most of this and re-created the rest). |
Actually closing as superseded by #6977. |
In the md module, dispatch via PSA rather than to the built-in hash implementations when a hash is available through a PSA accelerator. This extends the ability to use PSA accelerators to most of the library (everything that's calling md), including TLS, X.509, HMAC_DRBG, deterministic ECDSA, …
This implements #6471, starting from Mbed TLS 3.2.1. I started to implement this from the development version, but it got too complicated due to the
hash_info
module. It was easier to avoid thehash_info
module altogether.Status: work in progress.
development
.MBEDTLS_SHAxxx_C
:MBEDTLS_USE_PSA_CRYPTO
is enabled: seems that no hash would be available, yet we can apparently establish TLS connections? (Or maybe we can't, and we just aren't testing a configuration where this is the case.)PSA_WANT_ALG_xxx
, so the test cases should be failing?hash_info
ifmd.c
is included, though.To me, even the current status is enough to demonstrate that dispatching through md is simpler than
hash_info
, and also that it has similar if not better code size gains possibilities.Size estimate: S so far, but I suspect it'll grow to M to pass the CI.