Skip to content

Fix XTS-AES in-place operation with partial final block - #841

Open
Harshal5 wants to merge 2 commits into
Mbed-TLS:developmentfrom
Harshal5:fix/aes-xts-in-place-cts
Open

Fix XTS-AES in-place operation with partial final block#841
Harshal5 wants to merge 2 commits into
Mbed-TLS:developmentfrom
Harshal5:fix/aes-xts-in-place-cts

Conversation

@Harshal5

Copy link
Copy Markdown
Contributor

Description

mbedtls_aes_crypt_xts() produces wrong output for in-place operation (input == output) whenever the data unit length is not a multiple of 16 bytes: the "ciphertext-stealing" step writes the stolen cipher text bytes to output[0..leftover) before reading input[0..leftover), and in-place the two alias. The clobbered bytes are then fed into the final block encryption instead of the remaining input, so both encryption and decryption of the partial-block tail are corrupted. Block-aligned lengths are unaffected.

This PR reads the input tail into the scratch block before the stolen bytes are written (one statement moved), and adds in-place regression tests reusing the IEEE P1619/D16 vectors: vector 1 as a block-aligned control and vectors 15–18 for the "ciphertext-stealing" lengths (17–20 bytes). Vectors 15–18 fail without the fix.

PR checklist

Please remove the segment/s on either side of the | symbol as appropriate, and add any relevant link/s to the end of the line.
If the provided content is part of the present PR remove the # symbol.

  • changelog provided | not required because:
  • framework PR provided Mbed-TLS/mbedtls-framework# | not required
  • TF-PSA-Crypto development PR provided # | not required because:
  • TF-PSA-Crypto 1.1 PR provided # | not required because:
  • mbedtls development PR provided Mbed-TLS/mbedtls# | not required because:
  • mbedtls 4.1 PR provided Mbed-TLS/mbedtls# | not required because:
  • mbedtls 3.6 PR provided Mbed-TLS/mbedtls# | not required because:
  • tests provided

Notes for the submitter

Please refer to the contributing guidelines, especially the
checklist for PR contributors.

Help make review efficient:

  • Multiple simple commits
    • please structure your PR into a series of small commits, each of which does one thing
  • Avoid force-push
    • please do not force-push to update your PR - just add new commit(s)
  • See our Guidelines for Contributors for more details about the review process.

Harshal5 added 2 commits July 21, 2026 15:45
The ciphertext-stealing step wrote the stolen ciphertext bytes to the
output before reading the input tail; with input == output this fed
the clobbered bytes into the final block encryption. Read the input
tail into the scratch block first.

Signed-off-by: harshal.patil <harshal.patil@espressif.com>
Run the IEEE P1619/D16 XTS vectors with input == output: vector 1
(block-aligned control) and vectors 15-18 (partial final block, i.e.
ciphertext stealing). Vectors 15-18 fail without the previous commit.

Signed-off-by: harshal.patil <harshal.patil@espressif.com>
@Harshal5

Copy link
Copy Markdown
Contributor Author

Might solve Mbed-TLS/mbedtls#4302

@bensze01 bensze01 self-assigned this Jul 23, 2026
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.

2 participants