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

constantTimeEq not constant #10

Closed
jedrivisser opened this issue May 9, 2015 · 3 comments
Closed

constantTimeEq not constant #10

jedrivisser opened this issue May 9, 2015 · 3 comments

Comments

@jedrivisser
Copy link

Hi, the constantTimeEq starts with:

if (a.length != b.length) {
return false;
}

this kind of makes it not equal time or am I missing something?

If I remember correctly, the correct way to handle this is with fake comparissons, something like:

int result = 0;
if (a.length != b.length) {
for (int i = 0; i < a.length; i++) {
result |= a[i] ^ a[i];
}
return false;
}

@swenson
Copy link
Contributor

swenson commented May 9, 2015

It is not a problem if the lengths are different: we aren't assuming that the length is hard to guess.

Even if you added fake comparisons, you'd still be able to guess the length, because you could just try increasing b.length until it exceeds a.length. This will add in fake comparisons, which will cause a noticeable increase in time, and so you will then know a.length.

See, for example, https://golang.org/src/crypto/subtle/constant_time.go

Also, there's a decent chance that the compiler would just optimize out the a[i] ^ a[i] operation, and remove the fake comparisons altogether. Any time we do constant time comparison, we have to be careful about how smart the compiler is. :)

@swenson swenson closed this as completed May 9, 2015
@jedrivisser
Copy link
Author

true, thanks for the response

@swenson
Copy link
Contributor

swenson commented May 9, 2015

Thanks for the suggestion — keep 'em coming. I definitely had the exact same thought the first time I saw it. :)

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

No branches or pull requests

2 participants