Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file timing.h |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 3 | * |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 4 | * \brief Portable interface to the CPU cycle counter |
| 5 | * |
Manuel Pégourié-Gonnard | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 6 | * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 7 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 8 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 9 | * |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License along |
| 21 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 22 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 23 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 24 | #ifndef MBEDTLS_TIMING_H |
| 25 | #define MBEDTLS_TIMING_H |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 26 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 27 | #if !defined(MBEDTLS_CONFIG_FILE) |
Paul Bakker | f2561b3 | 2014-02-06 15:11:55 +0100 | [diff] [blame] | 28 | #include "config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 29 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 30 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 31 | #endif |
Paul Bakker | f2561b3 | 2014-02-06 15:11:55 +0100 | [diff] [blame] | 32 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 33 | #if !defined(MBEDTLS_TIMING_ALT) |
Paul Bakker | f2561b3 | 2014-02-06 15:11:55 +0100 | [diff] [blame] | 34 | // Regular implementation |
| 35 | // |
| 36 | |
Manuel Pégourié-Gonnard | ca3bdc5 | 2015-05-12 20:17:06 +0200 | [diff] [blame] | 37 | #include <stdint.h> |
| 38 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 39 | #ifdef __cplusplus |
| 40 | extern "C" { |
| 41 | #endif |
| 42 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 43 | /** |
| 44 | * \brief timer structure |
| 45 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 46 | struct mbedtls_timing_hr_time |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 47 | { |
| 48 | unsigned char opaque[32]; |
| 49 | }; |
| 50 | |
Manuel Pégourié-Gonnard | ca3bdc5 | 2015-05-12 20:17:06 +0200 | [diff] [blame] | 51 | /** |
| 52 | * \brief Context for mbedtls_timing_set/get_delay() |
| 53 | */ |
| 54 | typedef struct |
| 55 | { |
| 56 | struct mbedtls_timing_hr_time timer; |
| 57 | uint32_t int_ms; |
| 58 | uint32_t fin_ms; |
| 59 | } mbedtls_timing_delay_context; |
| 60 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 61 | extern volatile int mbedtls_timing_alarmed; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 62 | |
| 63 | /** |
| 64 | * \brief Return the CPU cycle counter value |
Manuel Pégourié-Gonnard | 5791109 | 2015-07-01 19:19:49 +0200 | [diff] [blame^] | 65 | * |
| 66 | * \warning This is only a best effort! Do not rely on this! |
| 67 | * In particular, it is known to be unreliable on virtual |
| 68 | * machines. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 69 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 70 | unsigned long mbedtls_timing_hardclock( void ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 71 | |
| 72 | /** |
| 73 | * \brief Return the elapsed time in milliseconds |
| 74 | * |
| 75 | * \param val points to a timer structure |
| 76 | * \param reset if set to 1, the timer is restarted |
| 77 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 78 | unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 79 | |
| 80 | /** |
| 81 | * \brief Setup an alarm clock |
| 82 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 83 | * \param seconds delay before the "mbedtls_timing_alarmed" flag is set |
Manuel Pégourié-Gonnard | dda5213 | 2015-02-11 11:36:31 +0000 | [diff] [blame] | 84 | * |
| 85 | * \warning Only one alarm at a time is supported. In a threaded |
| 86 | * context, this means one for the whole process, not one per |
| 87 | * thread. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 88 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 89 | void mbedtls_set_alarm( int seconds ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 90 | |
| 91 | /** |
Manuel Pégourié-Gonnard | ca3bdc5 | 2015-05-12 20:17:06 +0200 | [diff] [blame] | 92 | * \brief Set a pair of delays to watch |
| 93 | * (See \c mbedtls_timing_get_delay().) |
| 94 | * |
| 95 | * \param data Pointer to timing data |
| 96 | * Must point to a valid \c mbetls_timing_delay_context struct. |
| 97 | * \param int_ms First (intermediate) delay in milliseconds. |
| 98 | * \param fin_ms Second (final) delay in milliseconds. |
| 99 | * Pass 0 to cancel the current delay. |
| 100 | */ |
| 101 | void mbedtls_timing_set_delay( void *data, uint32_t int_ms, uint32_t fin_ms ); |
| 102 | |
| 103 | /** |
| 104 | * \brief Get the status of delays |
| 105 | * (Memory helper: number of delays passed.) |
| 106 | * |
| 107 | * \param data Pointer to timing data |
| 108 | * Must point to a valid \c mbetls_timing_delay_context struct. |
| 109 | * |
| 110 | * \return -1 if cancelled (fin_ms = 0) |
| 111 | * 0 if none of the delays are passed, |
| 112 | * 1 if only the intermediate delay is passed, |
| 113 | * 2 if the final delay is passed. |
| 114 | */ |
| 115 | int mbedtls_timing_get_delay( void *data ); |
| 116 | |
Manuel Pégourié-Gonnard | 8903fe0 | 2015-05-12 19:30:45 +0200 | [diff] [blame] | 117 | #ifdef __cplusplus |
| 118 | } |
| 119 | #endif |
| 120 | |
| 121 | #else /* MBEDTLS_TIMING_ALT */ |
| 122 | #include "timing_alt.h" |
| 123 | #endif /* MBEDTLS_TIMING_ALT */ |
| 124 | |
| 125 | #ifdef __cplusplus |
| 126 | extern "C" { |
| 127 | #endif |
| 128 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 129 | #if defined(MBEDTLS_SELF_TEST) |
Manuel Pégourié-Gonnard | 470fc93 | 2014-03-27 20:07:08 +0100 | [diff] [blame] | 130 | /** |
| 131 | * \brief Checkup routine |
| 132 | * |
| 133 | * \return 0 if successful, or 1 if a test failed |
| 134 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 135 | int mbedtls_timing_self_test( int verbose ); |
Manuel Pégourié-Gonnard | 470fc93 | 2014-03-27 20:07:08 +0100 | [diff] [blame] | 136 | #endif |
| 137 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 138 | #ifdef __cplusplus |
| 139 | } |
| 140 | #endif |
| 141 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 142 | #endif /* timing.h */ |