blob: 5ad4f9e7c082ceb8bd95eeaff7dd98e29e1efdef [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é-Gonnarda658a402015-01-23 09:45:19 +00006 * Copyright (C) 2006-2014, 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
Paul Bakker407a0da2013-06-27 14:29:21 +020037#ifdef __cplusplus
38extern "C" {
39#endif
40
Paul Bakker5121ce52009-01-03 21:22:43 +000041/**
42 * \brief timer structure
43 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044struct mbedtls_timing_hr_time
Paul Bakker5121ce52009-01-03 21:22:43 +000045{
46 unsigned char opaque[32];
47};
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049extern volatile int mbedtls_timing_alarmed;
Paul Bakker5121ce52009-01-03 21:22:43 +000050
51/**
52 * \brief Return the CPU cycle counter value
53 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054unsigned long mbedtls_timing_hardclock( void );
Paul Bakker5121ce52009-01-03 21:22:43 +000055
56/**
57 * \brief Return the elapsed time in milliseconds
58 *
59 * \param val points to a timer structure
60 * \param reset if set to 1, the timer is restarted
61 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset );
Paul Bakker5121ce52009-01-03 21:22:43 +000063
64/**
65 * \brief Setup an alarm clock
66 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067 * \param seconds delay before the "mbedtls_timing_alarmed" flag is set
Manuel Pégourié-Gonnarddda52132015-02-11 11:36:31 +000068 *
69 * \warning Only one alarm at a time is supported. In a threaded
70 * context, this means one for the whole process, not one per
71 * thread.
Paul Bakker5121ce52009-01-03 21:22:43 +000072 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073void mbedtls_set_alarm( int seconds );
Paul Bakker5121ce52009-01-03 21:22:43 +000074
75/**
76 * \brief Sleep for a certain amount of time
Paul Bakker13e2dfe2009-07-28 07:18:38 +000077 *
Paul Bakker37ca75d2011-01-06 12:28:03 +000078 * \param milliseconds delay in milliseconds
Paul Bakker5121ce52009-01-03 21:22:43 +000079 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080void mbedtls_timing_m_sleep( int milliseconds );
Paul Bakker5121ce52009-01-03 21:22:43 +000081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +010083/**
84 * \brief Checkup routine
85 *
86 * \return 0 if successful, or 1 if a test failed
87 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088int mbedtls_timing_self_test( int verbose );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +010089#endif
90
Paul Bakker5121ce52009-01-03 21:22:43 +000091#ifdef __cplusplus
92}
93#endif
94
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095#else /* MBEDTLS_TIMING_ALT */
Paul Bakkerf2561b32014-02-06 15:11:55 +010096#include "timing_alt.h"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097#endif /* MBEDTLS_TIMING_ALT */
Paul Bakkerf2561b32014-02-06 15:11:55 +010098
Paul Bakker5121ce52009-01-03 21:22:43 +000099#endif /* timing.h */