Skip to content

Forbid zero-length keys #257

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

Conversation

gilles-peskine-arm
Copy link
Collaborator

@gilles-peskine-arm gilles-peskine-arm commented Sep 12, 2019

Keys of size 0 generally don't make sense: a key is supposed to be secret. There is one edge case which is "raw data" keys, which are useful to store non-key objects in the same storage location as keys. However those are also problematic because they involve a zero-length buffer. Manipulating zero-length buffers in C requires special cases with functions like malloc() and memcpy(). Additionally, 0 as a key size already has a meaning "unspecified", which does not always overlap seamlessly with the meaning "0".

Therefore, forbid keys of size 0. No PSA implementation may accept them. Reject them in Mbed Crypto.

Clarify how key creation functions use attributes. Explain the meaning
of attribute values, espcially what 0 means in each field where it has
a special meaning. Explain what an algorithm usage policy can be (an
algorithm, a wildcard with ANY_HASH, or 0).
Keys of size 0 generally don't make sense: a key is supposed to be
secret. There is one edge case which is "raw data" keys, which are
useful to store non-key objects in the same storage location as keys.
However those are also problematic because they involve a zero-length
buffer. Manipulating zero-length buffers in C requires special cases
with functions like malloc() and memcpy(). Additionally, 0 as a key
size already has a meaning "unspecified", which does not always
overlap seamlessly with the meaning "0".

Therefore, forbid keys of size 0. No implementation may accept them.
If there isn't already a test with a raw data key of the now-minimal
length (1 byte), change the test case to a 1-byte key.
Check that zero-length keys cannot be imported, generated or derived.
Implement the prohibition on keys of size 0.
Add tests for derivation.

Test both 7 bits and 9 bits, in case the implementation truncated the
bit size down and 7 was rejected as 0 rather than because it isn't a
multiple of 8.

There is no corresponding test for import because import determines
the key size from the key data, which is always a whole number of bytes.
Zero-length keys are rejected at creation time, so we don't need any
special handling internally.

When exporting a key, we do need to take care of the case where the
output buffer is empty, but this is easy: an empty output buffer is
never valid.
@gilles-peskine-arm gilles-peskine-arm added bug Something isn't working enhancement New feature or request needs: review The pull request is ready for review. This generally means that it has no known issues. labels Sep 12, 2019
In psa_asymmetric_sign, immediately reject an empty signature buffer.
This can never be right.

Add test cases (one RSA and one ECDSA).

Change the SE HAL mock tests not to use an empty signature buffer.
@Patater
Copy link
Contributor

Patater commented Sep 13, 2019

Is there any code from #48 that we'd like to remove now that we disallow zero-length keys?

@gilles-peskine-arm
Copy link
Collaborator Author

Regarding #48, I didn't look at it specifically, but “Remove special handling for zero-length keys” effectively reverts it as well as removing similar checks in other functions.

The signature must have exactly the same length as the key, it can't
be longer. Fix ARMmbed#258

If the signature doesn't have the correct size, that's an invalid
signature, not a problem with an output buffer size. Fix the error code.

Add test cases.
@athoelke
Copy link
Contributor

I'm happy with the updates to the documentation..

@Patater
Copy link
Contributor

Patater commented Sep 13, 2019

When this is merged, should we also close #50 now that it's a feature and no longer a bug?

@Patater
Copy link
Contributor

Patater commented Sep 13, 2019

When this is merged, should we also close #50 now that it's a feature and no longer a bug?

Yes. I see you've address this in your PR description. Steps ahead of me. :)

Copy link
Contributor

@Patater Patater left a comment

Choose a reason for hiding this comment

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

LGTM

@k-stachowiak k-stachowiak self-requested a review September 19, 2019 09:15
@gilles-peskine-arm gilles-peskine-arm removed the needs: review The pull request is ready for review. This generally means that it has no known issues. label Sep 19, 2019
@gilles-peskine-arm gilles-peskine-arm merged commit a291413 into ARMmbed:development Sep 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

psa_asymmetric_verify accepts RSA signatures with trailing garbage Zero length keys not supported when generating persistent keys
4 participants