Paul Bakker | 50157ff | 2016-07-19 14:57:00 +0100 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
Gilles Peskine | 8064bf3 | 2017-10-10 19:56:06 +0200 | [diff] [blame] | 2 | |
k-stachowiak | 21daa3c | 2019-01-29 10:19:49 +0100 | [diff] [blame] | 3 | /* This test module exercises the timing module. Since, depending on the |
| 4 | * underlying operating system, the timing routines are not always reliable, |
| 5 | * this suite only performs very basic sanity checks of the timing API. |
| 6 | */ |
Gilles Peskine | 8064bf3 | 2017-10-10 19:56:06 +0200 | [diff] [blame] | 7 | |
| 8 | #include <limits.h> |
| 9 | |
Paul Bakker | 50157ff | 2016-07-19 14:57:00 +0100 | [diff] [blame] | 10 | #include "mbedtls/timing.h" |
Gilles Peskine | 8064bf3 | 2017-10-10 19:56:06 +0200 | [diff] [blame] | 11 | |
Paul Bakker | 50157ff | 2016-07-19 14:57:00 +0100 | [diff] [blame] | 12 | /* END_HEADER */ |
| 13 | |
| 14 | /* BEGIN_DEPENDENCIES |
| 15 | * depends_on:MBEDTLS_TIMING_C |
| 16 | * END_DEPENDENCIES |
| 17 | */ |
| 18 | |
Gilles Peskine | 8064bf3 | 2017-10-10 19:56:06 +0200 | [diff] [blame] | 19 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 20 | void timing_hardclock() |
Gilles Peskine | 078f1a1 | 2017-10-11 16:13:13 +0200 | [diff] [blame] | 21 | { |
k-stachowiak | 21daa3c | 2019-01-29 10:19:49 +0100 | [diff] [blame] | 22 | (void) mbedtls_timing_hardclock(); |
| 23 | /* This goto is added to avoid warnings from the generated code. */ |
| 24 | goto exit; |
| 25 | } |
| 26 | /* END_CASE */ |
Gilles Peskine | 078f1a1 | 2017-10-11 16:13:13 +0200 | [diff] [blame] | 27 | |
k-stachowiak | 21daa3c | 2019-01-29 10:19:49 +0100 | [diff] [blame] | 28 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 29 | void timing_get_timer() |
k-stachowiak | 21daa3c | 2019-01-29 10:19:49 +0100 | [diff] [blame] | 30 | { |
| 31 | struct mbedtls_timing_hr_time time; |
Dave Rodgman | 3fead76 | 2023-03-31 16:43:34 +0100 | [diff] [blame] | 32 | |
| 33 | memset(&time, 0, sizeof(time)); |
| 34 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 35 | (void) mbedtls_timing_get_timer(&time, 1); |
Dave Rodgman | 3fead76 | 2023-03-31 16:43:34 +0100 | [diff] [blame] | 36 | |
| 37 | /* Check that a non-zero time was written back */ |
| 38 | int all_zero = 1; |
| 39 | for (size_t i = 0; i < sizeof(time); i++) { |
Dave Rodgman | 2497c6b | 2023-03-31 18:04:34 +0100 | [diff] [blame] | 40 | all_zero &= ((unsigned char *) &time)[i] == 0; |
Dave Rodgman | 3fead76 | 2023-03-31 16:43:34 +0100 | [diff] [blame] | 41 | } |
| 42 | TEST_ASSERT(!all_zero); |
| 43 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 44 | (void) mbedtls_timing_get_timer(&time, 0); |
Dave Rodgman | 3fead76 | 2023-03-31 16:43:34 +0100 | [diff] [blame] | 45 | |
k-stachowiak | 21daa3c | 2019-01-29 10:19:49 +0100 | [diff] [blame] | 46 | /* This goto is added to avoid warnings from the generated code. */ |
| 47 | goto exit; |
| 48 | } |
| 49 | /* END_CASE */ |
Gilles Peskine | 2a26d62 | 2017-10-18 20:00:32 +0200 | [diff] [blame] | 50 | |
k-stachowiak | 21daa3c | 2019-01-29 10:19:49 +0100 | [diff] [blame] | 51 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 52 | void timing_set_alarm(int seconds) |
k-stachowiak | 21daa3c | 2019-01-29 10:19:49 +0100 | [diff] [blame] | 53 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 54 | if (seconds == 0) { |
| 55 | mbedtls_set_alarm(seconds); |
| 56 | TEST_ASSERT(mbedtls_timing_alarmed == 1); |
| 57 | } else { |
| 58 | mbedtls_set_alarm(seconds); |
| 59 | TEST_ASSERT(mbedtls_timing_alarmed == 0 || |
| 60 | mbedtls_timing_alarmed == 1); |
k-stachowiak | 21daa3c | 2019-01-29 10:19:49 +0100 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | /* END_CASE */ |
Gilles Peskine | 078f1a1 | 2017-10-11 16:13:13 +0200 | [diff] [blame] | 64 | |
k-stachowiak | 21daa3c | 2019-01-29 10:19:49 +0100 | [diff] [blame] | 65 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 66 | void timing_delay(int fin_ms) |
k-stachowiak | 21daa3c | 2019-01-29 10:19:49 +0100 | [diff] [blame] | 67 | { |
| 68 | mbedtls_timing_delay_context ctx; |
| 69 | int result; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 70 | if (fin_ms == 0) { |
| 71 | mbedtls_timing_set_delay(&ctx, 0, 0); |
| 72 | result = mbedtls_timing_get_delay(&ctx); |
| 73 | TEST_ASSERT(result == -1); |
| 74 | } else { |
| 75 | mbedtls_timing_set_delay(&ctx, fin_ms / 2, fin_ms); |
| 76 | result = mbedtls_timing_get_delay(&ctx); |
| 77 | TEST_ASSERT(result >= 0 && result <= 2); |
k-stachowiak | 21daa3c | 2019-01-29 10:19:49 +0100 | [diff] [blame] | 78 | } |
Gilles Peskine | 078f1a1 | 2017-10-11 16:13:13 +0200 | [diff] [blame] | 79 | } |
| 80 | /* END_CASE */ |