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

Timing safe string compare function #778

Closed

Conversation

prateeknischal
Copy link

@prateeknischal prateeknischal commented Feb 6, 2020

The implementation for CompareStrings function initially used strcmp, which is not timing safe, i.e. it breaks at the first non-equal character which allows information to be extracted about the prefix that is matching. This could allow to guess the number of chars matching or the size of the hash.

To avoid that, we use the modified CompareStrings, that will iterate over all the character until the very end of the supplied string only. So, in this way all operations will happen exactly the length of supplied string s1 times, so no information gets leaked about the number of characters matched and the length of the unknown string s2.

The implementation for `CompareStrings` function initially used strcmp,
which is not timing safe, i.e. it breaks at the first non-equal
character which allows information to be extracted about the prefix that
is matching, which is know as the timing attack. To avoid that, we use
the modified `CompareStrings`, that will iterate over all the
character until the very end of the supplied password only. So, in this
way all operations will happen exactly the length of supplied string s1
times.
@recrsn
Copy link
Collaborator

recrsn commented Feb 11, 2020

#720

See the discussion about why this is unnecessary in bcrypt

@prateeknischal
Copy link
Author

Hi @agathver , yes, practically brcypt isn't affected by timing attacks because of the part that it hashes the input before comparing which will always generate a string string of constant length. Unless you have the exact string whose hashes will match exactly, it's equivalent to traversing the whole string space. Thanks for taking a look at it. I will be closing this PR.

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.

None yet

2 participants