21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

A more portable but less accurate way of getting timing information on Unix-based<br />

platforms is to ask the operating system for the clock using the gettimeofday( ) function.<br />

The resolution varies depending on your underlying hardware, but it’s usually<br />

very good. It can be implemented using RDTSC but might have additional overhead.<br />

Nonetheless, on most operating systems, gettimeofday( ) is very accurate.<br />

Other Ways to Get the Time<br />

On many machines, there are other ways to get the time. One way is to use the POSIX<br />

times( ) function, which has the advantage that you can separate the time your process<br />

spends in the kernel from the time spent in user space running your code. While times(<br />

) is obsoleted on many systems, getrusage( ) does the same thing.<br />

Another alternative is the ISO C89 standard function, clock( ). However, other timers<br />

we discuss generally provide resolution that is as good as or better than this function.<br />

Here’s a macro that will use gettimeofday( ) to put the number of microseconds<br />

since January 1, 1970 into an unsigned 64-bit integer (if your compiler does not support<br />

a 64-bit integer type, you’ll have to store the two 32-bit values separately and<br />

diff them properly; see below).<br />

#include <br />

#define current_time_as_int64(a) { \<br />

struct timeval t; \<br />

gettimeofday(&t, 0); \<br />

*a = (spc_uint64_t)((t.tv_sec * 1000000) + t.tv_usec); \<br />

}<br />

Attackers can often force the worst-case performance for functionality with well-chosen<br />

inputs. Therefore, you should always be sure to determine the worst-case performance<br />

characteristics of whatever it is you’re doing, and plan accordingly.<br />

The gettimeofday( )-based macro does not compute the same thing<br />

the RDTSC version does! The former returns the number of microseconds<br />

elapsed, while the latter returns the number of cycles elapsed.<br />

You’ll usually be interested in the number of seconds elapsed. Therefore, you’ll need<br />

to convert the result of the gettimeofday( ) call to a number of cycles. To perform<br />

this conversion, divide by the clock speed, represented as a floating-point number in<br />

gigahertz.<br />

Because you care about elapsed time, you’ll want to subtract the starting time from<br />

the ending time to come up with elapsed time. You can transform a per-second representation<br />

to a per-cycle representation once you’ve calculated the total running<br />

Timing Cryptographic Primitives | 153<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!