1 /** 2 * \file timing.h 3 * 4 * \brief Portable interface to timeouts and to the CPU cycle counter 5 */ 6 /* 7 * Copyright The Mbed TLS Contributors 8 * SPDX-License-Identifier: Apache-2.0 9 * 10 * Licensed under the Apache License, Version 2.0 (the "License"); you may 11 * not use this file except in compliance with the License. 12 * You may obtain a copy of the License at 13 * 14 * http://www.apache.org/licenses/LICENSE-2.0 15 * 16 * Unless required by applicable law or agreed to in writing, software 17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 * See the License for the specific language governing permissions and 20 * limitations under the License. 21 */ 22 #ifndef MBEDTLS_TIMING_H 23 #define MBEDTLS_TIMING_H 24 #include "mbedtls/private_access.h" 25 26 #include "mbedtls/build_info.h" 27 28 #include <stdint.h> 29 30 #ifdef __cplusplus 31 extern "C" { 32 #endif 33 34 #if !defined(MBEDTLS_TIMING_ALT) 35 // Regular implementation 36 // 37 38 /** 39 * \brief timer structure 40 */ 41 struct mbedtls_timing_hr_time { 42 unsigned char MBEDTLS_PRIVATE(opaque)[32]; 43 }; 44 45 /** 46 * \brief Context for mbedtls_timing_set/get_delay() 47 */ 48 typedef struct mbedtls_timing_delay_context { 49 struct mbedtls_timing_hr_time MBEDTLS_PRIVATE(timer); 50 uint32_t MBEDTLS_PRIVATE(int_ms); 51 uint32_t MBEDTLS_PRIVATE(fin_ms); 52 } mbedtls_timing_delay_context; 53 54 #else /* MBEDTLS_TIMING_ALT */ 55 #include "timing_alt.h" 56 #endif /* MBEDTLS_TIMING_ALT */ 57 58 /* Internal use */ 59 unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset); 60 61 /** 62 * \brief Set a pair of delays to watch 63 * (See \c mbedtls_timing_get_delay().) 64 * 65 * \param data Pointer to timing data. 66 * Must point to a valid \c mbedtls_timing_delay_context struct. 67 * \param int_ms First (intermediate) delay in milliseconds. 68 * The effect if int_ms > fin_ms is unspecified. 69 * \param fin_ms Second (final) delay in milliseconds. 70 * Pass 0 to cancel the current delay. 71 * 72 * \note To set a single delay, either use \c mbedtls_timing_set_timer 73 * directly or use this function with int_ms == fin_ms. 74 */ 75 void mbedtls_timing_set_delay(void *data, uint32_t int_ms, uint32_t fin_ms); 76 77 /** 78 * \brief Get the status of delays 79 * (Memory helper: number of delays passed.) 80 * 81 * \param data Pointer to timing data 82 * Must point to a valid \c mbedtls_timing_delay_context struct. 83 * 84 * \return -1 if cancelled (fin_ms = 0), 85 * 0 if none of the delays are passed, 86 * 1 if only the intermediate delay is passed, 87 * 2 if the final delay is passed. 88 */ 89 int mbedtls_timing_get_delay(void *data); 90 91 /** 92 * \brief Get the final timing delay 93 * 94 * \param data Pointer to timing data 95 * Must point to a valid \c mbedtls_timing_delay_context struct. 96 * 97 * \return Final timing delay in milliseconds. 98 */ 99 uint32_t mbedtls_timing_get_final_delay( 100 const mbedtls_timing_delay_context *data); 101 102 #ifdef __cplusplus 103 } 104 #endif 105 106 #endif /* timing.h */ 107