Returns true if test result is expected: we consider 5% "same". */
| 78 | |
| 79 | /* Returns true if test result is expected: we consider 5% "same". */ |
| 80 | static bool secret_time_test(struct timerel (*test)(struct secret *s1, |
| 81 | struct secret *s2, |
| 82 | size_t off), |
| 83 | bool should_be_const) |
| 84 | { |
| 85 | struct timerel firstbyte_time, lastbyte_time, diff; |
| 86 | |
| 87 | firstbyte_time = test(s1, s2, 0); |
| 88 | lastbyte_time = test(s1, s2, sizeof(s1->data)-1); |
| 89 | |
| 90 | if (verbose) |
| 91 | printf("First byte %u psec vs last byte %u psec\n", |
| 92 | (int)time_to_nsec(time_divide(firstbyte_time, RUNS/1000)), |
| 93 | (int)time_to_nsec(time_divide(lastbyte_time, RUNS/1000))); |
| 94 | |
| 95 | /* If they differ by more than 5%, get upset. */ |
| 96 | if (time_less(firstbyte_time, lastbyte_time)) |
| 97 | diff = time_sub(lastbyte_time, firstbyte_time); |
| 98 | else { |
| 99 | /* If the lastbyte test was faster, that's a fail it we expected |
| 100 | * it to be slower... */ |
| 101 | if (!should_be_const) |
| 102 | return false; |
| 103 | diff = time_sub(firstbyte_time, lastbyte_time); |
| 104 | } |
| 105 | |
| 106 | return time_less(time_multiply(diff, 20), firstbyte_time) == should_be_const; |
| 107 | } |
| 108 | |
| 109 | #define ITERATIONS 1000 |
| 110 |
no test coverage detected