Interesting question.
Definitions
Just to make sure we're on the same page about definitions, I'm taking "timing attack" according to the wikipedia definition:
Every logical operation in a computer takes time to execute, and the time can differ based on the input; with precise measurements of the time for each operation, an attacker can work backwards to the input.
The way I think about this is that a naively-implemented strcmp
will walk through the strings and quit as soon as it finds a char that's different. Something like (pseudocode)
bool strcmp(char[] stringA, char[] stringB) {
for (i=0; i<len(stringA); i++) {
if (stringA[i] != stringB[i])
return false;
}
return true;
}
That means that strcmp("aaaaaa", "aaaaab")
will take longer than strcmp("aaaaaa", "bbbbbb")
. Basically the longer it takes, the "closer" you are to the secret string. Using a "timing attack oracle" (a server that knows the secret value and gives you timing info), an attacker can figure out the first letter, then the second letter, then eventually the whole string.
Timing safe functions for passwords
I feel required to ask why you need a timing safe password compare function? You're not storing passwords in plaintext are you?
If you're storing password hashes and doing a strcmp
on two hash values, then timing safety is not a thing you need to think about. (Maybe I'm wrong and you can still do effective timing attack on hashes?)
Timing safe functions for usernames / email addresses
Now to your actual question. Let's start with the threat model. I suppose you're worried about user enumeration attacks.
With something like a password or a cryptographic key, there's one valid value and being able to guess it one character at a time, rather than the whole string at a time, really really speeds up the brute-force.
With usernames / email addresses, there are many valid values. I wonder how much a "closeness" meter actually helps? And close to what? In what order is postgres going to iterate through the keys? I honestly don't know, but my intuition is that
it probably doesn't make nearly as big a difference as it does for a cryptographic key.
That said, if we're going to worry about timing attacks, I assume you have already mitigated every other aspect of user enumeration attacks?
- When a user submits a username & password, you give absolutely no indication whether the username or the password was wrong. Rapid7 has a great article on this topic.
- You use some sort of rate-limiting or IP banning to prevent repeated login attempts.
In fact, if you have an effective rate-limiting mechanism, then an attacker probably can't do the thousands of queries they need in order to do statistical timing analysis.