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

Add invalid padding_len check in get_pkcs_padding #9082

Open
wants to merge 3 commits into
base: development
Choose a base branch
from

Conversation

andre-rosa
Copy link

@andre-rosa andre-rosa commented May 1, 2024

When trying to decrypt data with an invalid key, we found that mbedtls returned 0x6200 (-25088), which means "CIPHER - Input data contains invalid padding and is rejected" from mbedtls_cipher_finish, but it also set the output len as 18446744073709551516.

In case we detect an error with padding, we leave the output len zero'ed and return MBEDTLS_ERR_CIPHER_INVALID_PADDING. I believe that the current test cases are sufficient, as they fail if I return the alternative code MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, so they do already expect a padding failure, but now we don't change the output len in the error case.

Here's a reference for the way openssl checks the padding length:

Description

Adds a missing check when doing the padding check in the case of MBEDTLS_CIPHER_AES_128_CBC decryption with padding. Even thought mbedtls was returning an error, the output parameter was being set instead of leaving it with zero. This check exists in openssl, so this makes the behavior closer between both libraries.

Closes #9083.

PR checklist

Please tick as appropriate and edit the reasons (e.g.: "backport: not needed because this is a new feature")

When trying to decrypt data with an invalid key, we found that `mbedtls`
returned `0x6200` (`-25088`), which means "_CIPHER - Input data contains
invalid padding and is rejected_" from `mbedtls_cipher_finish`, but it also
set the output len as `18446744073709551516`.

In case we detect an error with padding, we leave the output len zero'ed
and return `MBEDTLS_ERR_CIPHER_INVALID_PADDING`. I believe that the current
test cases are sufficient, as they fail if I return the alternative code
`MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA`, so they do already expect a padding
failure, but now we don't change the output len in the error case.

Here's a reference for the way `openssl` checks the padding length:
  - https://github.com/openssl/openssl/blob/1848c561ec39a9ea91ff1bf740a554be274f98b0/crypto/evp/evp_enc.c#L1023
  - openssl/openssl@b554eef

Signed-off-by: Andre Goddard Rosa <andre.goddard@gmail.com>
Signed-off-by: Andre Goddard Rosa <agoddardrosa@roku.com>
Signed-off-by: Andre Goddard Rosa <andre.goddard@gmail.com>
Signed-off-by: Andre Goddard Rosa <agoddardrosa@roku.com>
@tgonzalezorlandoarm tgonzalezorlandoarm added enhancement needs-review Every commit must be reviewed by at least two team members, component-crypto Crypto primitives and low-level interfaces needs-reviewer This PR needs someone to pick it up for review priority-medium Medium priority - this can be reviewed as time permits size-xs Estimated task size: extra small (a few hours at most) labels May 2, 2024
…cases

With the robustness fix:
`PASSED (125 suites, 26639 tests run)`

Without the robustness fix:
`FAILED (125 suites, 26639 tests run)`

Signed-off-by: Andre Goddard Rosa <andre.goddard@gmail.com>
Signed-off-by: Andre Goddard Rosa <agoddardrosa@roku.com>
Copy link
Contributor

@gilles-peskine-arm gilles-peskine-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 adding a non-regression test. LGTM

Copy link
Contributor

@tgonzalezorlandoarm tgonzalezorlandoarm left a comment

Choose a reason for hiding this comment

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

LGTM

@tgonzalezorlandoarm tgonzalezorlandoarm added approved Design and code approved - may be waiting for CI or backports and removed needs-review Every commit must be reviewed by at least two team members, needs-reviewer This PR needs someone to pick it up for review labels May 8, 2024
@gilles-peskine-arm gilles-peskine-arm added the needs-backports Backports are missing or are pending review and approval. label May 12, 2024
@gilles-peskine-arm
Copy link
Contributor

Can you please make a pull request with the same patch to the mbedtls-3.6 branch? It's not a bug fix, but it's a small and cheap robustness improvement that can prevent a buffer overflow in the calling code, so I think it's worth backporting to the 3.6 long-time support branch.

@andre-rosa
Copy link
Author

Can you please make a pull request with the same patch to the mbedtls-3.6 branch? It's not a bug fix, but it's a small and cheap robustness improvement that can prevent a buffer overflow in the calling code, so I think it's worth backporting to the 3.6 long-time support branch.

Please find it at #9132. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Design and code approved - may be waiting for CI or backports component-crypto Crypto primitives and low-level interfaces enhancement needs-backports Backports are missing or are pending review and approval. priority-medium Medium priority - this can be reviewed as time permits size-xs Estimated task size: extra small (a few hours at most)
Projects
Status: In Development
Development

Successfully merging this pull request may close these issues.

When doing MBEDTLS_CIPHER_AES_128_CBC decryption with a zeroed key, output is set to a random number
3 participants