The Wayback Machine - https://web.archive.org/web/20201109182232/https://github.com/ARMmbed/mbedtls/pull/3787
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

PSA: Add support macros for IV/nonce sizes #3787

Merged
merged 1 commit into from Oct 30, 2020

Conversation

@bensze01
Copy link
Contributor

@bensze01 bensze01 commented Oct 14, 2020

Description

This commit defines the following new macros added to the PSA Crypto API between versions 1.0 beta 3 and 1.0.0 final:

PSA_AEAD_NONCE_LENGTH
PSA_AEAD_NONCE_MAX_SIZE
PSA_CIPHER_IV_LENGTH
PSA_CIPHER_IV_MAX_SIZE

The in-code documentation was adapted from the PSA Cryptography API spec (see above the links to the individual macros)

Closes #3270

Status

READY

Requires Backporting

NO

Migrations

NO

Copy link
Contributor

@yanesca yanesca left a comment

I reviewed the PR and left comments with a suggestion for improvement and a request for change.

* and AEAD algorithm that it recognizes, but does not support.
*/
#define PSA_AEAD_NONCE_LENGTH(key_type, alg) \
(((key_type) == PSA_KEY_TYPE_AES || \

This comment has been minimized.

@yanesca

yanesca Oct 15, 2020
Contributor

Maybe we could make it more simple and future proof by using PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER() here.

This comment has been minimized.

@bensze01

bensze01 Oct 16, 2020
Author Contributor

Done.

This comment has been minimized.

@gilles-peskine-arm

gilles-peskine-arm Oct 26, 2020
Contributor

On the contrary, I don't think PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER should appear here. Different algorithms have different nonces. Currently the two algorithms that it matches are CCM and GCM, but we might add other algorithms with a different nonce size.

And isn't the maximum nonce size for CCM 13?

This comment has been minimized.

@yanesca

yanesca Oct 26, 2020
Contributor

On the contrary, I don't think PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER should appear here.

I don't have strong opinion on this, I am happy either way.

And isn't the maximum nonce size for CCM 13?

Yes, it is, but in the other discussion @athoelke said that these macros are about the default PSA sizes and not the maximum:
#3787 (comment)
and we assumed that to be true.

This comment has been minimized.

@bensze01

bensze01 Oct 29, 2020
Author Contributor

@gilles-peskine-arm I've changed the check back to the original version, checking for CCM and GCM explicitly. I've left the size for CCM at 12, as per @yanesca 's comment.

*
* See also #PSA_AEAD_NONCE_LENGTH().
*/
#define PSA_AEAD_NONCE_MAX_SIZE 12

This comment has been minimized.

@yanesca

yanesca Oct 15, 2020
Contributor

The maximum size for CCM is 13:

* \param iv_len The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12,
* or 13. The length L of the message length field is
* 15 - \p iv_len.

For GCM the upper limit is 2^61 bytes:

/* IV and AD are limited to 2^64 bits, so 2^61 bytes */

But of course this is far off from what anybody would need in practice and using this value would severely limit the utility of this macro. To do justice to GCM and still preserve the utility of this macro, I suggest we limit the IV for GCM to 14 bytes. Of course we need to explain this in the comment and hope that it does not break anybody's use case. @gilles-peskine-arm what do you think?

This comment has been minimized.

@bensze01

bensze01 Oct 15, 2020
Author Contributor

@yanesca @gilles-peskine-arm While I thought the wording in the spec as to what PSA_AEAD_NONCE_MAX_SIZE should mean was a bit vague, I took it to mean MAX(PSA_AEAD_NONCE_LENGTH(key_type, alg), for all key_type, for all alg), ie. the maximum of the default nonce lengths for the supported key and algorithm pairs.

I arrived to this conclusion due to the spec repeatedly suggesting one uses either PSA_AEAD_NONCE_LENGTH or PSA_AEAD_NONCE_MAX_SIZE to allocate a buffer large enough to hold the nonce generated by the PSA Crypto implementation, which I took to be the macro's main use case

This comment has been minimized.

@gilles-peskine-arm

gilles-peskine-arm Oct 15, 2020
Contributor

Indeed, the primary intended use for these macros is to determine an output buffer size that's guaranteed to be large enough. This does warrant a clarification in the specification, I'll file it for a 1.0.x update.

This comment has been minimized.

@yanesca

yanesca Oct 15, 2020
Contributor

@gilles-peskine-arm am I interpreting this correctly, do you mean that 12 is the correct value for this macro here?

This comment has been minimized.

@gilles-peskine-arm

gilles-peskine-arm Oct 15, 2020
Contributor

I'm sure that 16 is correct. I'd have to check exactly how we generate nonces for supported algorithms to see if 12 is correct.

This comment has been minimized.

@yanesca

yanesca Oct 16, 2020
Contributor

@athoelke Thank you for the clarification! I think I still have a question: what would be the benefit/justification of psa_aead_generate_nonce() generating longer nonces than the maximum size of any AEAD nonce supported by the implementation?

This comment has been minimized.

@athoelke

athoelke Oct 16, 2020
Contributor

what would be the benefit/justification of psa_aead_generate_nonce() generating longer nonces than the maximum size of any AEAD nonce supported by the implementation?

That wouldn't make sense. psa_aead_generate_nonce() is defined to output PSA_AEAD_NONE_LENGTH(key_type, alg).

@bensze01's came to the correct conclusion that PSA_AEAD_NONCE_MAX_SIZE must be a value equal to (or greater than)

max { PSA_AEAD_NONCE_LENGTH(key_type, alg), for all supported AEAD (key_type, alg) }

PSA_AEAD_NONCE_LENGTH(key_type, alg) is not the maximum size of nonce supported as input to psa_aead_set_nonce(), psa_aead_encrypt() or psa_aead_decrypt(), just the default size that is generated by psa_aead_generate_nonce().

This comment has been minimized.

@bensze01

bensze01 Oct 16, 2020
Author Contributor

👀! - it says this precisely once, in psa_aead_generate_nonce().

Yeah, sorry. I remembered seeing this sentence multiple times when working on this patch, but those were other xxx_MAX_SIZE macros.

This comment has been minimized.

@yanesca

yanesca Oct 16, 2020
Contributor

Oh I see. Thank you @athoelke for the clarification! This means that the current value of the macro is correct.

@bensze01 Could you please extend the comment with Andrew's explanation?

This comment has been minimized.

@bensze01

bensze01 Oct 16, 2020
Author Contributor

@yanesca Done

Mbed TLS Unified Board automation moved this from To Do to In Progress Oct 15, 2020
@bensze01 bensze01 requested a review from gilles-peskine-arm Oct 15, 2020
@bensze01 bensze01 force-pushed the bensze01:iv_nonce_size branch from af3bfe8 to 688322a Oct 16, 2020
@bensze01 bensze01 requested a review from yanesca Oct 16, 2020
Copy link
Contributor

@yanesca yanesca left a comment

LGTM.

@yanesca yanesca moved this from In Progress to Has Approval in Mbed TLS Unified Board Oct 20, 2020
@daverodgman daverodgman removed the request for review from gilles-peskine-arm Oct 23, 2020
include/psa/crypto_sizes.h Outdated Show resolved Hide resolved
include/psa/crypto_sizes.h Outdated Show resolved Hide resolved
* and AEAD algorithm that it recognizes, but does not support.
*/
#define PSA_AEAD_NONCE_LENGTH(key_type, alg) \
(((key_type) == PSA_KEY_TYPE_AES || \

This comment has been minimized.

@gilles-peskine-arm

gilles-peskine-arm Oct 26, 2020
Contributor

On the contrary, I don't think PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER should appear here. Different algorithms have different nonces. Currently the two algorithms that it matches are CCM and GCM, but we might add other algorithms with a different nonce size.

And isn't the maximum nonce size for CCM 13?

include/psa/crypto_sizes.h Outdated Show resolved Hide resolved
include/psa/crypto_sizes.h Outdated Show resolved Hide resolved
Mbed TLS Unified Board automation moved this from Has Approval to In Progress Oct 26, 2020
This commit defines the following new macros added to the PSA Crypto API
between versions 1.0 beta 3 and 1.0.0 final:

PSA_AEAD_NONCE_LENGTH
PSA_AEAD_NONCE_MAX_SIZE
PSA_CIPHER_IV_LENGTH
PSA_CIPHER_IV_MAX_SIZE

Signed-off-by: Bence Szépkúti <[email protected]>
@bensze01 bensze01 force-pushed the bensze01:iv_nonce_size branch from 688322a to 423d3e7 Oct 29, 2020
@bensze01 bensze01 requested review from yanesca and gilles-peskine-arm Oct 29, 2020
Copy link
Contributor

@yanesca yanesca left a comment

Looks good to me.

@bensze01 bensze01 moved this from In Progress to Has Approval in Mbed TLS Unified Board Oct 29, 2020
@gilles-peskine-arm gilles-peskine-arm merged commit 52f32c9 into ARMmbed:development Oct 30, 2020
10 checks passed
10 checks passed
DCO DCO
Details
PR-3787-head Pre Test Checks OK
Details
PR-3787-head Result analysis OK
Details
PR-3787-head TLS Testing All tests passed
Details
PR-3787-merge Pre Test Checks OK
Details
PR-3787-merge Result analysis OK
Details
PR-3787-merge TLS Testing All tests passed
Details
continuous-integration/jenkins/pr-head This commit looks good
Details
continuous-integration/jenkins/pr-merge This commit looks good
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
Mbed TLS Unified Board automation moved this from Has Approval to Done Oct 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Linked issues

Successfully merging this pull request may close these issues.

4 participants
You can’t perform that action at this time.