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

fix(dht): updates to message padding #4594

Merged

Conversation

AaronFeickert
Copy link
Collaborator

@AaronFeickert AaronFeickert commented Aug 31, 2022

Description

PR 4362 mitigates a metadata leak whereby encrypted messages are the same length as plaintext messages due to the use of a stream cipher. This work adds more complete length checks, such that padding can fail. It also more efficiently handles the edge case where no padding is needed.

Motivation and Context

To avoid directly leaking the length of plaintext messages after stream cipher encryption, PR 4362 pads such messages to a multiple of a fixed base length after first prepending the original message length using a fixed encoding.

However, the following cases do not appear to be handled by the padding and unpadding code:

  • The plaintext message length exceeds the fixed encoding length
  • The ciphertext message is not long enough for extraction of the fixed encoding length
  • The ciphertext message is not a multiple of the base length

Further, in the case where the message length (after length prepending) is exactly a multiple of the base length, an entire base length of padding is unnecessarily applied.

This work addresses these issues. The padding process now checks that the plaintext message does not exceed the limit enforced by the length encoding; as a result, it can now return an error that propagates to the encryption function caller. The padding algorithm has been simplified and now handles the multiple-of-the-base-length edge case by correctly applying no padding. The unpadding process now checks that it can safely extract the message length, and checks that the ciphertext message is a multiple of the base length.

How Has This Been Tested?

No test has been added for the case where the message length exceeds the limit allowed by the encoding, as this would imply very high memory usage (or swapping) exceeding 4 GB. Existing tests pass. A new test exercises the other failure modes.

Adds better length checks. Simplifies the padding algorithm and handles an edge case hitting a base length multiple.
@AaronFeickert AaronFeickert changed the title Updates to message padding fix(dht): updates to message padding Aug 31, 2022
@AaronFeickert
Copy link
Collaborator Author

Manually tested the case where the plaintext message is too large for the length encoding.

@jorgeantonio21
Copy link
Contributor

Very nice ! LGTM :)

PS: the failing tests seem to be unrelated with the current code refactoring, and probably are due to some previous PR merge to development.

@AaronFeickert
Copy link
Collaborator Author

Is it worth it (or even possible) to add the plaintext-is-too-long test in a way that won't horribly clog up the CI runners?

Copy link
Member

@sdbondi sdbondi left a comment

Choose a reason for hiding this comment

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

Thanks looks good. Some fairly minor comments.

comms/dht/src/crypt.rs Outdated Show resolved Hide resolved
comms/dht/src/crypt.rs Outdated Show resolved Hide resolved
comms/dht/src/crypt.rs Outdated Show resolved Hide resolved
comms/dht/src/crypt.rs Outdated Show resolved Hide resolved
@stringhandler stringhandler merged commit cf4f9bf into tari-project:development Sep 2, 2022
@AaronFeickert AaronFeickert deleted the message-padding branch September 2, 2022 14:48
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.

5 participants