News for hackers. Stuff that matters.
Re:A Lesson In Timing Attacks (or, Don't use MessageDigest.i... by naz on Thursday August 13, 2009 @12:26PM (#761102)
Incredible, but nothing a little sleep(rand()) won't solve.
Re:A Lesson In Timing Attacks (or, Don't use MessageDigest.i... by sophacles on Thursday August 13, 2009 @12:41PM (#761147)
Not really, if this attack is doable over the internet, which will have latencies randomly[1] distributed (in a normal curve), rand does not actually do anything, well maybe require a few more sample points to get that noise out.
Re:A Lesson In Timing Attacks (or, Don't use MessageDigest.i... by naz on Thursday August 13, 2009 @12:56PM (#761190)
That makes sense, maybe sleep(rand(1.0 - time_spent))
Re:A Lesson In Timing Attacks (or, Don't use MessageDigest.i... by naz on Thursday August 13, 2009 @12:57PM (#761192)
Or just sleep(1.0 - time_spent)
Re:A Lesson In Timing Attacks (or, Don't use MessageDigest.i... by JacobK on Thursday August 13, 2009 @07:06PM (#761953)
Or just don't short circuit ever, and do the entire computation every time.
Re:A Lesson In Timing Attacks (or, Don't use MessageDigest.i... by codahale on Thursday August 13, 2009 @12:42PM (#761155)
You may want to read this:

http://www.cs.rice.edu/~dwallach/pub/crosby-timing2009.pdf

Adding jitter will not defeat timing attacks unless the jitter is dependent on the input, like blinded RSA.

Re:A Lesson In Timing Attacks (or, Don't use MessageDigest.i... by commiebob on Thursday August 13, 2009 @06:44PM (#761903)
What if you did something like (using the example from the article) A value which shares no bytes in common with the secret digest will return immediately; a value which shares the first 15 bytes will return 15 compares later.

You keep track of the time while doing the comparison, then before spitting back the result you feed the amount of time to a separate function that will sleep (or whatever) a certain amount of predetermined time so that all results process in the same time.

I realize this is a bit excessive, and will slow down your code quite a bit, but if you are really that worried about this type of attack, why not?

Re:A Lesson In Timing Attacks (or, Don't use MessageDigest.i... by codahale on Thursday August 13, 2009 @07:22PM (#761972)
You don't really need to. You can guarantee that the right HMAC and the wrong HMAC both take exactly the same amount of time to compare by using a constant-time algorithm like the one in the article without dealing with non-portable, unreliable timers.

(In some languages—hi Ruby!—you might not even have access to reliable millisecond-resolution timing.)

Re:A Lesson In Timing Attacks (or, Don't use MessageDigest.i... by eru on Thursday August 13, 2009 @12:42PM (#761157)
Sleeping for a random amount of time will make it more difficult to mount a timing attack. But not impossible in theory. You just have to do more statistics --- significance tests and the like.
Re:A Lesson In Timing Attacks (or, Don't use MessageDigest.i... by anamax on Thursday August 13, 2009 @05:35PM (#761747)
Why not sleep a variable amount of time, so all calls take the same amount of time?
Re:A Lesson In Timing Attacks (or, Don't use MessageDigest.i... by niyazpk on Thursday August 13, 2009 @11:30PM (#762293)
As naz pointed out, you can use sleep(1.0 - time_spent) to accomplish this.

The downside is that while the solution given in the OP will use a constant amount of time, that constant amount of time itself is not fixed. When over the years processors get faster, the code can execute faster, but in the above case it cannot.

Re:A Lesson In Timing Attacks (or, Don't use MessageDigest.i... by anamax on Friday August 14, 2009 @02:02AM (#762473)
> When over the years processors get faster, the code can execute faster, but in the above case it cannot.

Sure it can. You can drop the limit whenever you change processors. In fact, you can tune the limit for a given call to be the max time taken by that call. As the implementation or resources change, the limit changes.

Re:A Lesson In Timing Attacks (or, Don't use MessageDigest.i... by codahale on Friday August 14, 2009 @02:58AM (#762517)
You're better off just using a constant-time algorithm which uses the same number of operations for all inputs. Few systems, if any, have sub-nanosecond-resolution timers, let alone thread schedulers which have quanta of that size.
Re:A Lesson In Timing Attacks (or, Don't use MessageDigest.i... by anamax on Friday August 14, 2009 @12:51PM (#763357)
> You're better off just using a constant-time algorithm which uses the same number of operations for all inputs.

That's easier said that done. Also, you have to come up with one for every security-relevant operation.

And, "algorithm" isn't the issue, implementation is. Given caching and the like, that's really hard to do.

An easy way to implement the "sleep to worst case" idea is to simply time each call and keep track of the max. If a given call instance returns in less time, sleep the difference. (It's probably good to add a decay mechanism.)

Re:A Lesson In Timing Attacks (or, Don't use MessageDigest.i... by codahale on Friday August 14, 2009 @03:43PM (#763697)
Perhaps it'll mean more if someone else says it:

Although nanosleep() takes a timespec struct that allows you to give an accuracy with nanosecond precision, nanosleep doesn't really give anywhere near that level of accuracy. Under Linux, the kernel only checks on timers once every "jiffie," which defaults to 1 ms under the 2.6 kernel (on older kernels it was 10 ms, and 10 ms is still a compile option in the 2.6 kernel). Once upon a time, nanosleep would busy-wait if the sleep time given was less than 2 ms, but not anymore. On the other hand, don't be too discouraged—as long as you don't need submillisecond accuracy, nanosleep() is very good…

http://www.ddj.com/linux-open-source/184402031

Re:A Lesson In Timing Attacks (or, Don't use MessageDigest.i... by anamax on Monday August 17, 2009 @08:49AM (#767963)
That means that any sleep will disguise a call's duration to the jiffie, which is a good thing. (If the call's variance is small enough, it may mean that there's no need to figure out how long to sleep.)

Note that caching and branch prediction and the like make "constant time" implementations almost impossible.

Re:A Lesson In Timing Attacks (or, Don't use MessageDigest.i... by codahale on Sunday August 23, 2009 @05:11PM (#781013)
No, it will add millisecond-accuracy jitter to the timing of your algorithm. And the attacker will continue to be able to extract microsecond-accuracy data from your timing attack. You need profoundly accurate hardware and a real-time operating system. Your attacker needs… more measurements.

Caching is equally unlikely to play a role. You're probably thinking of DJB's timing attacks on AES (http://cr.yp.to/antiforgery/cachetiming-20050414.pdf), but that attack resolves around the timing of S-box lookups in which the index of the lookup is key-dependent. In the constant-time algorithm proposed by Nate Lawson and others, the only array lookup is the incremented value of a for loop (i.e., constant-time). The other operations are XOR and OR, both of which are constant-time.

Branch prediction doesn't come into play because a constant-time comparison doesn't involve conditionals until all the data has been compared.

A constant-time implementation of AES? Hard. A constant-time implementation of an array comparison? Dead simple.

Not sure why you're so resistant to the idea.

Re:A Lesson In Timing Attacks (or, Don't use MessageDigest.i... by carey on Friday August 14, 2009 @04:21AM (#762612)
Are you sure that (1.0 - time_spent) executes in constant time? Or that sleep() is precise enough?