1 /** 2 * \file timing.h 3 * 4 * \brief Portable interface to the CPU cycle counter 5 * 6 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 7 * SPDX-License-Identifier: Apache-2.0 8 * 9 * Licensed under the Apache License, Version 2.0 (the "License"); you may 10 * not use this file except in compliance with the License. 11 * You may obtain a copy of the License at 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, software 16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 * See the License for the specific language governing permissions and 19 * limitations under the License. 20 * 21 * This file is part of mbed TLS (https://tls.mbed.org) 22 */ 23 #ifndef MBEDTLS_TIMING_ALT_H 24 #define MBEDTLS_TIMING_ALT_H 25 26 #if !defined(MBEDTLS_CONFIG_FILE) 27 #include "config.h" 28 #else 29 #include MBEDTLS_CONFIG_FILE 30 #endif 31 32 #include <stdint.h> 33 34 #ifdef __cplusplus 35 extern "C" { 36 #endif 37 38 /** 39 * \brief timer structure 40 */ 41 struct mbedtls_timing_hr_time 42 { 43 unsigned char opaque[32]; 44 }; 45 46 /** 47 * \brief Context for mbedtls_timing_set/get_delay() 48 */ 49 typedef struct 50 { 51 struct mbedtls_timing_hr_time timer; 52 uint32_t int_ms; 53 uint32_t fin_ms; 54 } mbedtls_timing_delay_context; 55 56 extern volatile int mbedtls_timing_alarmed; 57 58 /** 59 * \brief Return the CPU cycle counter value 60 * 61 * \warning This is only a best effort! Do not rely on this! 62 * In particular, it is known to be unreliable on virtual 63 * machines. 64 */ 65 unsigned long mbedtls_timing_hardclock( void ); 66 67 /** 68 * \brief Return the elapsed time in milliseconds 69 * 70 * \param val points to a timer structure 71 * \param reset if set to 1, the timer is restarted 72 */ 73 unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset ); 74 75 /** 76 * \brief Setup an alarm clock 77 * 78 * \param seconds delay before the "mbedtls_timing_alarmed" flag is set 79 * 80 * \warning Only one alarm at a time is supported. In a threaded 81 * context, this means one for the whole process, not one per 82 * thread. 83 */ 84 void mbedtls_set_alarm( int seconds ); 85 86 /** 87 * \brief Set a pair of delays to watch 88 * (See \c mbedtls_timing_get_delay().) 89 * 90 * \param data Pointer to timing data 91 * Must point to a valid \c mbedtls_timing_delay_context struct. 92 * \param int_ms First (intermediate) delay in milliseconds. 93 * \param fin_ms Second (final) delay in milliseconds. 94 * Pass 0 to cancel the current delay. 95 */ 96 void mbedtls_timing_set_delay( void *data, uint32_t int_ms, uint32_t fin_ms ); 97 98 /** 99 * \brief Get the status of delays 100 * (Memory helper: number of delays passed.) 101 * 102 * \param data Pointer to timing data 103 * Must point to a valid \c mbedtls_timing_delay_context struct. 104 * 105 * \return -1 if cancelled (fin_ms = 0) 106 * 0 if none of the delays are passed, 107 * 1 if only the intermediate delay is passed, 108 * 2 if the final delay is passed. 109 */ 110 int mbedtls_timing_get_delay( void *data ); 111 112 #ifdef __cplusplus 113 extern "C" { 114 #endif 115 116 #ifdef __cplusplus 117 } 118 #endif 119 120 #endif /* timing.h */ 121