blob: 99e9daeb5e9a9912e2fde5310e7e517eedc0a0c0 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file timing.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakker37ca75d2011-01-06 12:28:03 +00004 * \brief Portable interface to the CPU cycle counter
5 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02006 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00008 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +00009 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 * 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 Bakker5121ce52009-01-03 21:22:43 +000023 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#ifndef MBEDTLS_TIMING_H
25#define MBEDTLS_TIMING_H
Paul Bakker5121ce52009-01-03 21:22:43 +000026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakkerf2561b32014-02-06 15:11:55 +010028#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#endif
Paul Bakkerf2561b32014-02-06 15:11:55 +010032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#if !defined(MBEDTLS_TIMING_ALT)
Paul Bakkerf2561b32014-02-06 15:11:55 +010034// Regular implementation
35//
36
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +020037#include <stdint.h>
38
Paul Bakker407a0da2013-06-27 14:29:21 +020039#ifdef __cplusplus
40extern "C" {
41#endif
42
Paul Bakker5121ce52009-01-03 21:22:43 +000043/**
44 * \brief timer structure
45 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046struct mbedtls_timing_hr_time
Paul Bakker5121ce52009-01-03 21:22:43 +000047{
48 unsigned char opaque[32];
49};
50
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +020051/**
52 * \brief Context for mbedtls_timing_set/get_delay()
53 */
54typedef 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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020061extern volatile int mbedtls_timing_alarmed;
Paul Bakker5121ce52009-01-03 21:22:43 +000062
63/**
64 * \brief Return the CPU cycle counter value
Manuel Pégourié-Gonnard57911092015-07-01 19:19:49 +020065 *
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 Bakker5121ce52009-01-03 21:22:43 +000069 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070unsigned long mbedtls_timing_hardclock( void );
Paul Bakker5121ce52009-01-03 21:22:43 +000071
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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020078unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset );
Paul Bakker5121ce52009-01-03 21:22:43 +000079
80/**
81 * \brief Setup an alarm clock
82 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083 * \param seconds delay before the "mbedtls_timing_alarmed" flag is set
Manuel Pégourié-Gonnarddda52132015-02-11 11:36:31 +000084 *
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 Bakker5121ce52009-01-03 21:22:43 +000088 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089void mbedtls_set_alarm( int seconds );
Paul Bakker5121ce52009-01-03 21:22:43 +000090
91/**
Manuel Pégourié-Gonnardca3bdc52015-05-12 20:17:06 +020092 * \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 */
101void 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 */
115int mbedtls_timing_get_delay( void *data );
116
Manuel Pégourié-Gonnard8903fe02015-05-12 19:30:45 +0200117#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
126extern "C" {
127#endif
128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100130/**
131 * \brief Checkup routine
132 *
133 * \return 0 if successful, or 1 if a test failed
134 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135int mbedtls_timing_self_test( int verbose );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100136#endif
137
Paul Bakker5121ce52009-01-03 21:22:43 +0000138#ifdef __cplusplus
139}
140#endif
141
Paul Bakker5121ce52009-01-03 21:22:43 +0000142#endif /* timing.h */