Skip to content

Conversation

@SparkiDev
Copy link
Contributor

Description

Encrypted PEM with a block cipher has padding.
Remove padding.

Fixes zd#13803

Testing

POC with customer data.

Checklist

  • added tests
  • updated/added doxygen
  • updated appropriate READMEs
  • Updated manual and documentation

@SparkiDev SparkiDev self-assigned this Mar 2, 2022
Copy link
Member

@embhorn embhorn left a comment

Choose a reason for hiding this comment

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

Excellent, thanks Sean. Fix is also confirmed by customer.

#if !defined(NO_AES) && defined(HAVE_AES_CBC) && \
defined(HAVE_AES_DECRYPT)
if (info->cipherType == WC_CIPHER_AES_CBC) {
if (der->length > AES_BLOCK_SIZE) {
Copy link
Contributor

Choose a reason for hiding this comment

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

What if der->length is a multiple of AES_BLOCK_SIZE? Will there still be padding? If not this logic isn't right. Perhaps the check should be if ((der->length % AES_BLOCK_SIZE) != 0) {?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The check is to see whether there is enough bytes for at least one block - a padded block. Must have a full block of padding if the plaintext is a multiple of the block size - otherwise you don't know whether there is padding there or not.
The decryption will fail if the encrypted data isn't a multiple of the block size.

if (info->cipherType == WC_CIPHER_AES_CBC) {
if (der->length > AES_BLOCK_SIZE) {
padVal = der->buffer[der->length-1];
if (padVal <= AES_BLOCK_SIZE) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Then use if (padVal <= der->length)?

Copy link
Contributor Author

@SparkiDev SparkiDev Mar 2, 2022

Choose a reason for hiding this comment

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

The padding value cannot be more than the block size if using PKCS #5/#7 padding.

@SparkiDev SparkiDev assigned dgarske and unassigned SparkiDev Mar 2, 2022
@dgarske dgarske merged commit 5d0614c into wolfSSL:master Mar 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants