Update function return codes #213
No reviewers
Labels
No labels
api-spec
bug
CLA requested
CLA valid
compliance
DO NOT MERGE
duplicate
enhancement
feature branch
good first issue
HEADER FORMATTING ISSUE
help wanted
Information
invalid
Jira status: AWAITING REVIEW
Jira status: CLOSED
Jira status: DONE
Jira status: IN PROGRESS
Jira status: IN REVIEW
Jira status: OPEN
Jira status: REOPENED
Jira status: RESOLVED
mirrored
mirror internally
needs: backports
needs: ci
needs: design review
needs: preceding PR
needs: review
needs: work
notabug
open for community contribution
Product Backlog
question
ready for merge
security
stale
type: bug
type: enhancement
type: question
web pages
wontfix
writing
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
gh-ARMmbed/mbed-crypto!213
Loading…
Reference in a new issue
No description provided.
Delete branch "psa-api-1.0-beta"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
The Beta version of this API is missing return codes for various functions. Conversely, with some cases it's debatable whether some of the existing return codes could ever be returned by particular function implementations.
This PR tries to harmonise the return codes by adding expected return codes where they are possible. It does not attempt to document existing return codes. There are also a number of functions which need more thought (abort/close/(get|set)_capacity), which are not fully addressed here. These two drawbacks ought to be addressed in a future pull request.
Here is a summary of the changes I've made (I've excluded the error prefixes for readability):
psa_open_key(). With the exception of that function, INVALID_HANDLE ought to be returned instead.For non-obvious changes see the commit messages. I'm happy to squash the commits if needed.
There's trailing whitespace. Please make sure
tests/scripts/check-files.pypasses.Looks good so far. Based on the parameter- and name-based rules I wrote in https://github.com/ARMmbed/psa-crypto/pull/227 , which are far from perfect, some more error codes should be documented:
Please let me know which ones you agree/disagree with.
I can share some of my thought so far:
Bad search/replace?
psa_close_key:CORRUPTION_DETECTEDis possible.NOT_PERMITTEDisn't.psa_hash_setup: I expect it to returnBAD_STATEif given an operation that's already in use,INVALID_ARGUMENTif given an algorithm that isn't a hash algorithm.INSUFFICIENT_ENTROPY. But now that you mention it, the PRNG state could be in storage, and in that caseSTORAGE_FAILUREwould make sense. I'd still disallowINSUFFICIENT_STORAGEthough: if you store the PRNG outside RAM, make sure you always have enough room.psa_cipher_generate_ivandpsa_aead_generate_nonceinvolve the key, hence key-related errors includingSTORAGE_FAILURE.Apart from this, the PR is ok to merge as far as I'm concerned. It might not be perfect, but it's a major improvement.
Thanks. Not sure how I managed to paste that bit of text here, but it has been removed now.
I would say PSA_ERROR_NOT_SUPPORTED is more appropriate for this case. The documentation already has the following "alg is not supported or is not a hash algorithm"
Having thought more about your comments on psa_cipher_generate_iv...I would say that storage failure isn't really that important, as what matters to the caller is the fact that there is insufficient entropy. I think it might only be worth distinguishing between storage failure and insufficient entropy if one case is fatal (i.e. the caller shouldn't bother to wait for the system to gain enough entropy).
I'm not sure how this can occur. Some sort of metadata that is written back to storage?
NOT_SUPPORTEDusually means: the argument is architecturally valid (might work on some implementations) but this implementation does not support that argument.INVALID_ARGUMENTusually means: the argument is not within the domain specified by the architecture/specification.psa_close_key() might encounter a key object in the implementation that is corrupted (e.g. due to memory scribbling). In a library implementation, detection of this could report
CORRUPTION_DETECTED. In a isolated implementation, this is unlikely to be reported to the caller as memory scribbling within the back end should be treated as a fatal condition within the service.In general,
CORRUPTION_DETECTEDin a library implementation warns that something really bad has happened.Thanks for the feedback, I'm now convinced. I've pushed the remaining changes.
Noticed an editing glitch.
Line break lost in pasting new error codes.
Oops, thanks for spotting this
Fixed. @Patater Gilles says this is OK to merge. Can you take a look?
@adrianlshaw Could you please rebase on
developmentand change the target branch of this PR todevelopment? We can do another once over review and merge afterwards should all look good. Thanks!Thanks for the heads up @Patater. I've rebased and changed the PR target.
Please fix the trailing whitespace.
Otherwise the changes look good. I did not look at each change in detail or review for exhaustivity, but it's definitely a major improvement. We can make further fixes later.
With the current version of https://github.com/ARMmbed/psa-crypto/pull/227, I get the following warnings:
I think they're all warranted.
Thanks for the review. These issues should be fixed with the latest commits.
@adrianlshaw It doesn't look like this was rebased properly. The pr-head tests are failing, and they'd be fixed if you rebased on the latest development branch. Also, I see a merge commit in the commit history which should go away on a rebase.
Got into a weird state so I'm closing in favour of a new PR (#244)
@adrianlshaw Could you try using https://github.com/Patater/mbed-crypto/tree/adrian-update-function-return-codes as the branch for this PR? We can re-open this PR and use that branch, so we have the review comment history associated with what we merge.
Hi @Patater I just noticed that all checks have passed. Does that mean that this is good to merge? Otherwise I'll use your suggestion.
Looks like your rebase from before worked, at first glance. What weirdness had you run into?
We should review and then we can merge this.
Thanks for the review!
(I thought there was some weirdness because I thought I saw some CI failures...apparently not).
The rebase has led to some new warnings from my script:
At a glance these look warranted.
Do we know those weren't present before the rebase?
Odd that the list is different though, so seems something uncouth happened.
Some error codes changed since my last pre-rebase review. I didn't investigate where those changes came from.
I'd welcome your thoughts on the following... for generate_random these errors could legitimately return if you have a PRNG implementation with a storage dependency.
For operations that involve a key, such as the psa_mac functions, there could be storage failures if the key is lazily loaded by an implementation.
For abort and hash functions I think the warnings are valid...there doesn't seem to be a sensible scenario for those errors.
psa_mac_xxxcan fail withSTORAGE_FAILUREbecause it needs to (re)load the key, but I don't thinkpsa_mac_abortshould be allowed to fail due to storage.abortshould never fail unless it's given dud arguments or it detects corruption that can only be due to bad arguments or an attack.For
psa_generate_random, I'm not sure. The RNG initialization may load and update a stored seed, but that's done inpsa_crypto_init. I don't think the RNG state should be in storage, so functions that need the RNG shouldn't fail due to a storage error (unless it's for a non-RNG-related reason).Thanks for the feedback. The latest commits should resolve these warnings (I'm unable to reproduce them locally atm).
How about for implementations like Mbed TLS's DRBGs with NV Seed, where storage is involved in both initialization and reseeding? I believe this is what you'd refer to as failing for a non-RNG-related reason. If my understanding is correct, with NV Seed,
psa_generate_random()may need to reseed itself, which may involve storage, which may involve storage failure.In addition to @Patater's comment, I'll say that it might be appropriate to distinguish the STORAGE_FAILURE case from INSUFFICIENT_ENTROPY because the former seems fatal whereas the latter (to me) implies that it's possible to recover by trying again.
That's a limitation of treating the stored seed as “entropy”. It makes zero sense to use the NV seed when reseeding. The point of reseeding is to bring new non-deterministic input so that an adversary who could obtain a past RNG state is no longer able to find out the current RNG state. If the adversary knows the RNG state from before saving the NV seed, or could observe the NV seed, then “reseeding” from the NV seed does not achieve anything. Thus
psa_generate_randomshould not access the stored seed. This is supposed to be done solely inpsa_crypto_init.That Mbed Crypto doesn't work that way is a bug that we'll resolve when we replace the Mbed TLS entropy module by new code that uses PSA entropy drivers.
Indeed, but that only affects
psa_crypto_init, notpsa_generate_random.@ -805,2 +831,4 @@* \retval #PSA_ERROR_BUFFER_TOO_SMALL* \p hash_size is too small* \retval #PSA_ERROR_INSUFFICIENT_MEMORY* \retval #PSA_ERROR_COMMUNICATION_FAILUREYou've removed
STORAGE_FAILUREfrom too many functions.psa_copy_keymust access storage if either the source or destination key is persistent.verify/finish functions for MAC, cipher and AEAD use a key and therefore can access storage in implementations where key material is not systematically kept in RAM during multipart operations.
@Patater I don't want to block merging this before the upcoming Mbed Crypto release, because it's a major improvement as it is. But @adrianlshaw if you can update this before the PR is merged, it would be better.
@ -805,2 +831,4 @@* \retval #PSA_ERROR_BUFFER_TOO_SMALL* \p hash_size is too small* \retval #PSA_ERROR_INSUFFICIENT_MEMORY* \retval #PSA_ERROR_COMMUNICATION_FAILURESorry I'm confused. According to this PR and my copy of the source,
STORAGE_FAILUREhasn't been removed at all from psa_copy_key, or any of the mac verify/finish functions. I can see that Github has a little Outdated label on your comment above. I might be mistaken but perhaps you're looking at a stale snapshot?@ -805,2 +831,4 @@* \retval #PSA_ERROR_BUFFER_TOO_SMALL* \p hash_size is too small* \retval #PSA_ERROR_INSUFFICIENT_MEMORY* \retval #PSA_ERROR_COMMUNICATION_FAILUREOh, my bad. When I posted this, I was just looking at the next-to-last commit via the git diff, and git diffs are misleading with documentation above the declaration: git infers the name of the function containing a diff fragment but that's the last declared function, not the function that's being documented. So the function after
psa_copy_keyhadSTORAGE_FAILUREremoved, etc., which is not a problem. Sorry about that.@ -805,2 +831,4 @@* \retval #PSA_ERROR_BUFFER_TOO_SMALL* \p hash_size is too small* \retval #PSA_ERROR_INSUFFICIENT_MEMORY* \retval #PSA_ERROR_COMMUNICATION_FAILURENo problem, I've also found it misleading when reviewing diffs.