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: sensitive data compare should use constant time compare to avoid timing attack #3508

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

nan01ab
Copy link
Contributor

@nan01ab nan01ab commented Oct 4, 2024

Description

RpcServer.CheckAuth use direct compare to determine two strings equal or not, but sensitive data compare should use constant time compare to avoid timing attack

Type of change

  • Optimization (the change is only an optimization)
  • Style (the change is only a code style for better maintenance or standard purpose)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

Copy link
Member

@vncoelho vncoelho left a comment

Choose a reason for hiding this comment

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

I understand this timing attack. It looks correct and the XOR is quite efficient.
Although it does not look much critical to me in this specific case.

vncoelho
vncoelho previously approved these changes Oct 4, 2024
Jim8y
Jim8y previously approved these changes Oct 4, 2024
@Jim8y
Copy link
Contributor

Jim8y commented Oct 4, 2024

side-channel attack is sort of hot in the research area~~~~ but not sure how industry see it.

return false;

if (left.Length != right.Length)
return false;
Copy link
Member

Choose a reason for hiding this comment

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

This is no constant, maybe we should do a dummy for for the maxLength string

Copy link
Contributor Author

@nan01ab nan01ab Oct 5, 2024

Choose a reason for hiding this comment

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

This is no constant, maybe we should do a dummy for for the maxLength string

Implementations in Rust subtle and Golang crypto/subtle are similar to this one. Length is not sensitive.

Copy link
Member

@shargon shargon Oct 5, 2024

Choose a reason for hiding this comment

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

This is no constant, maybe we should do a dummy for for the maxLength string

Implementations in Rust subtle and Golang crypto/subtle are similar to this one. Length is not sensitive.

Then these implementations are wrong 🙃

/// <param name="left">The left <see cref="string"/>.</param>
/// <param name="right">The right <see cref="string"/>.</param>
/// <returns>True if the two <see cref="string"/>s are equal, false otherwise.</returns>
public static bool ConstantTimeEquals(this string left, string right)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we please not introduce yet another constant time comparison function? This was discussed already in #3472 (review), there are standard solutions to this that should be used.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can we please not introduce yet another constant time comparison function? This was discussed already in #3472 (review), there are standard solutions to this that should be used.

The implementation in ConstantTimeUtility has some limits.
For example:
public static bool ConstantTimeEq<T>(in T a, in T b) **where T : unmanaged**

Copy link
Member

Choose a reason for hiding this comment

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

        string string1 = "hello";
        string string2 = "hello";

        // Convert to bytes
        byte[] bytes1 = Encoding.UTF8.GetBytes(string1);
        byte[] bytes2 = Encoding.UTF8.GetBytes(string2);

        // Use FixedTimeEquals
        bool result = CryptographicOperations.FixedTimeEquals(bytes1, bytes2);

That's not working?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

        string string1 = "hello";
        string string2 = "hello";

        // Convert to bytes
        byte[] bytes1 = Encoding.UTF8.GetBytes(string1);
        byte[] bytes2 = Encoding.UTF8.GetBytes(string2);

        // Use FixedTimeEquals
        bool result = CryptographicOperations.FixedTimeEquals(bytes1, bytes2);

That's not working?

This needs extra data copies?

Copy link
Member

Choose a reason for hiding this comment

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

First rule of cryptography, don't write your own cryptography. I'm with @roman-khimov, better to use the official one

Copy link
Contributor Author

Choose a reason for hiding this comment

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

First rule of cryptography, don't write your own cryptography. I'm with @roman-khimov, better to use the official one

Has changed to CryptographicOperations.FixedTimeEquals, and also eliminated the two memory allocations(Encoding.UTF8.GetString(Convert.FromBase64String(reqauth.Replace("Basic ", "").Trim())); and authstring.Split), so it should have better performance.

@nan01ab nan01ab dismissed stale reviews from Jim8y and vncoelho via 0a5e52c October 6, 2024 02:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants