2009-01-03 22:22:43 +01:00
|
|
|
/*
|
|
|
|
* Benchmark demonstration program
|
|
|
|
*
|
2020-08-07 13:07:28 +02:00
|
|
|
* Copyright The Mbed TLS Contributors
|
2023-11-02 20:47:20 +01:00
|
|
|
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
2009-01-03 22:22:43 +01:00
|
|
|
*/
|
|
|
|
|
2021-05-19 17:54:54 +02:00
|
|
|
#define MBEDTLS_ALLOW_PRIVATE_ACCESS
|
|
|
|
|
2021-05-27 11:25:03 +02:00
|
|
|
#include "mbedtls/build_info.h"
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/platform.h"
|
2015-01-19 15:26:37 +01:00
|
|
|
|
2022-03-02 18:01:10 +01:00
|
|
|
#if !defined(MBEDTLS_HAVE_TIME)
|
2023-01-11 14:50:10 +01:00
|
|
|
int main(void)
|
2015-02-16 18:32:47 +01:00
|
|
|
{
|
2022-03-02 18:01:10 +01:00
|
|
|
mbedtls_printf("MBEDTLS_HAVE_TIME not defined.\n");
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_exit(0);
|
2015-02-16 18:32:47 +01:00
|
|
|
}
|
|
|
|
#else
|
2014-12-19 12:10:37 +01:00
|
|
|
|
2015-02-16 18:32:47 +01:00
|
|
|
#include <string.h>
|
2015-05-26 16:04:06 +02:00
|
|
|
#include <stdlib.h>
|
2014-12-19 12:10:37 +01:00
|
|
|
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/md5.h"
|
|
|
|
#include "mbedtls/ripemd160.h"
|
|
|
|
#include "mbedtls/sha1.h"
|
|
|
|
#include "mbedtls/sha256.h"
|
|
|
|
#include "mbedtls/sha512.h"
|
2022-05-20 14:26:00 +02:00
|
|
|
#include "mbedtls/sha3.h"
|
2018-02-21 10:47:47 +01:00
|
|
|
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/des.h"
|
|
|
|
#include "mbedtls/aes.h"
|
2018-02-21 10:47:47 +01:00
|
|
|
#include "mbedtls/aria.h"
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/camellia.h"
|
2016-05-15 22:28:08 +02:00
|
|
|
#include "mbedtls/chacha20.h"
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/gcm.h"
|
|
|
|
#include "mbedtls/ccm.h"
|
2018-05-09 10:21:28 +02:00
|
|
|
#include "mbedtls/chachapoly.h"
|
2016-10-05 15:14:19 +02:00
|
|
|
#include "mbedtls/cmac.h"
|
2016-05-16 23:25:45 +02:00
|
|
|
#include "mbedtls/poly1305.h"
|
2018-02-21 10:47:47 +01:00
|
|
|
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/ctr_drbg.h"
|
|
|
|
#include "mbedtls/hmac_drbg.h"
|
2018-02-21 10:47:47 +01:00
|
|
|
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/rsa.h"
|
|
|
|
#include "mbedtls/dhm.h"
|
|
|
|
#include "mbedtls/ecdsa.h"
|
|
|
|
#include "mbedtls/ecdh.h"
|
2018-02-21 10:47:47 +01:00
|
|
|
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/error.h"
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-03 12:07:09 +01:00
|
|
|
/* *INDENT-OFF* */
|
2021-06-11 11:56:47 +02:00
|
|
|
#ifndef asm
|
|
|
|
#define asm __asm
|
|
|
|
#endif
|
2023-01-03 12:07:09 +01:00
|
|
|
/* *INDENT-ON* */
|
2021-06-11 11:56:47 +02:00
|
|
|
|
2021-06-10 15:16:50 +02:00
|
|
|
#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
#include <process.h>
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
struct _hr_time {
|
2021-06-10 15:16:50 +02:00
|
|
|
LARGE_INTEGER start;
|
|
|
|
};
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
struct _hr_time {
|
2021-06-10 15:16:50 +02:00
|
|
|
struct timeval start;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* _WIN32 && !EFIX64 && !EFI32 */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/memory_buffer_alloc.h"
|
2015-02-11 15:06:19 +01:00
|
|
|
#endif
|
|
|
|
|
2023-11-16 17:39:43 +01:00
|
|
|
#ifdef MBEDTLS_TIMING_ALT
|
|
|
|
void mbedtls_set_alarm(int seconds);
|
|
|
|
unsigned long mbedtls_timing_hardclock(void);
|
|
|
|
extern volatile int mbedtls_timing_alarmed;
|
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
static void mbedtls_set_alarm(int seconds);
|
2023-11-16 17:39:43 +01:00
|
|
|
static unsigned long mbedtls_timing_hardclock(void);
|
|
|
|
#endif
|
2021-06-10 15:16:50 +02:00
|
|
|
|
2015-02-16 18:32:47 +01:00
|
|
|
/*
|
|
|
|
* For heap usage estimates, we need an estimate of the overhead per allocated
|
|
|
|
* block. ptmalloc2/3 (used in gnu libc for instance) uses 2 size_t per block,
|
|
|
|
* so use that as our baseline.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
#define MEM_BLOCK_OVERHEAD (2 * sizeof(size_t))
|
2015-02-16 18:32:47 +01:00
|
|
|
|
|
|
|
/*
|
2015-05-27 16:29:56 +02:00
|
|
|
* Size to use for the alloc buffer if MEMORY_BUFFER_ALLOC_C is defined.
|
2015-02-16 18:32:47 +01:00
|
|
|
*/
|
2018-10-25 13:47:18 +02:00
|
|
|
#define HEAP_SIZE (1u << 16) /* 64k */
|
2015-02-16 18:32:47 +01:00
|
|
|
|
2011-11-29 12:23:58 +01:00
|
|
|
#define BUFSIZE 1024
|
2014-01-30 20:59:00 +01:00
|
|
|
#define HEADER_FORMAT " %-24s : "
|
2014-01-27 23:27:06 +01:00
|
|
|
#define TITLE_LEN 25
|
2015-02-13 15:12:07 +01:00
|
|
|
|
2023-09-12 03:45:37 +02:00
|
|
|
#define OPTIONS \
|
|
|
|
"md5, ripemd160, sha1, sha256, sha512,\n" \
|
|
|
|
"sha3_224, sha3_256, sha3_384, sha3_512,\n" \
|
|
|
|
"des3, des, camellia, chacha20,\n" \
|
|
|
|
"aes_cbc, aes_cfb128, aes_cfb8, aes_gcm, aes_ccm, aes_xts, chachapoly\n" \
|
|
|
|
"aes_cmac, des3_cmac, poly1305\n" \
|
|
|
|
"ctr_drbg, hmac_drbg\n" \
|
2015-02-12 12:37:29 +01:00
|
|
|
"rsa, dhm, ecdsa, ecdh.\n"
|
2013-09-17 14:57:55 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_ERROR_C)
|
2015-02-12 12:37:29 +01:00
|
|
|
#define PRINT_ERROR \
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_strerror(ret, (char *) tmp, sizeof(tmp)); \
|
|
|
|
mbedtls_printf("FAILED: %s\n", tmp);
|
2013-09-17 14:57:55 +02:00
|
|
|
#else
|
2015-02-12 12:37:29 +01:00
|
|
|
#define PRINT_ERROR \
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_printf("FAILED: -0x%04x\n", (unsigned int) -ret);
|
2015-02-12 12:37:29 +01:00
|
|
|
#endif
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
#define TIME_AND_TSC(TITLE, CODE) \
|
|
|
|
do { \
|
|
|
|
unsigned long ii, jj, tsc; \
|
|
|
|
int ret = 0; \
|
2013-09-17 14:57:55 +02:00
|
|
|
\
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_printf(HEADER_FORMAT, TITLE); \
|
|
|
|
fflush(stdout); \
|
2013-09-17 14:57:55 +02:00
|
|
|
\
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_set_alarm(1); \
|
|
|
|
for (ii = 1; ret == 0 && !mbedtls_timing_alarmed; ii++) \
|
|
|
|
{ \
|
|
|
|
ret = CODE; \
|
|
|
|
} \
|
2013-09-17 14:57:55 +02:00
|
|
|
\
|
2023-01-11 14:50:10 +01:00
|
|
|
tsc = mbedtls_timing_hardclock(); \
|
|
|
|
for (jj = 0; ret == 0 && jj < 1024; jj++) \
|
|
|
|
{ \
|
|
|
|
ret = CODE; \
|
|
|
|
} \
|
2013-09-17 14:57:55 +02:00
|
|
|
\
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) \
|
|
|
|
{ \
|
|
|
|
PRINT_ERROR; \
|
|
|
|
} \
|
|
|
|
else \
|
|
|
|
{ \
|
|
|
|
mbedtls_printf("%9lu KiB/s, %9lu cycles/byte\n", \
|
|
|
|
ii * BUFSIZE / 1024, \
|
|
|
|
(mbedtls_timing_hardclock() - tsc) \
|
|
|
|
/ (jj * BUFSIZE)); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2013-09-17 14:57:55 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_MEMORY_DEBUG)
|
2014-12-19 12:10:37 +01:00
|
|
|
|
2020-04-09 10:40:03 +02:00
|
|
|
/* How much space to reserve for the title when printing heap usage results.
|
|
|
|
* Updated manually as the output of the following command:
|
|
|
|
*
|
|
|
|
* sed -n 's/.*[T]IME_PUBLIC.*"\(.*\)",/\1/p' programs/test/benchmark.c |
|
2022-01-05 10:34:17 +01:00
|
|
|
* awk '{print length+3}' | sort -rn | head -n1
|
2020-04-09 10:40:03 +02:00
|
|
|
*
|
2022-01-05 10:34:17 +01:00
|
|
|
* This computes the maximum length of a title +3, because we appends "/s" and
|
|
|
|
* want at least one space. (If the value is too small, the only consequence
|
2021-12-21 06:14:10 +01:00
|
|
|
* is poor alignment.) */
|
2022-01-05 10:34:17 +01:00
|
|
|
#define TITLE_SPACE 17
|
2020-04-09 10:40:03 +02:00
|
|
|
|
2014-12-19 12:10:37 +01:00
|
|
|
#define MEMORY_MEASURE_INIT \
|
|
|
|
size_t max_used, max_blocks, max_bytes; \
|
|
|
|
size_t prv_used, prv_blocks; \
|
2022-01-05 10:05:54 +01:00
|
|
|
size_t alloc_cnt, free_cnt, prv_alloc, prv_free; \
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_memory_buffer_alloc_cur_get(&prv_used, &prv_blocks); \
|
|
|
|
mbedtls_memory_buffer_alloc_max_reset();
|
2014-12-19 12:10:37 +01:00
|
|
|
|
2022-01-04 10:24:01 +01:00
|
|
|
#define MEMORY_MEASURE_RESET \
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_memory_buffer_alloc_count_get(&prv_alloc, &prv_free);
|
2022-01-04 10:24:01 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
#define MEMORY_MEASURE_PRINT(title_len) \
|
|
|
|
mbedtls_memory_buffer_alloc_max_get(&max_used, &max_blocks); \
|
|
|
|
mbedtls_memory_buffer_alloc_count_get(&alloc_cnt, &free_cnt); \
|
2020-04-09 10:40:03 +02:00
|
|
|
ii = TITLE_SPACE > (title_len) ? TITLE_SPACE - (title_len) : 1; \
|
2023-01-11 14:50:10 +01:00
|
|
|
while (ii--) mbedtls_printf(" "); \
|
2014-12-19 12:10:37 +01:00
|
|
|
max_used -= prv_used; \
|
|
|
|
max_blocks -= prv_blocks; \
|
|
|
|
max_bytes = max_used + MEM_BLOCK_OVERHEAD * max_blocks; \
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_printf("%6u heap bytes, %6u allocs", \
|
|
|
|
(unsigned) max_bytes, \
|
|
|
|
(unsigned) (alloc_cnt - prv_alloc));
|
2014-12-19 12:10:37 +01:00
|
|
|
|
|
|
|
#else
|
2015-01-29 17:28:44 +01:00
|
|
|
#define MEMORY_MEASURE_INIT
|
2022-01-04 10:24:01 +01:00
|
|
|
#define MEMORY_MEASURE_RESET
|
2023-01-11 14:50:10 +01:00
|
|
|
#define MEMORY_MEASURE_PRINT(title_len)
|
2014-12-19 12:10:37 +01:00
|
|
|
#endif
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
#define TIME_PUBLIC(TITLE, TYPE, CODE) \
|
|
|
|
do { \
|
|
|
|
unsigned long ii; \
|
|
|
|
int ret; \
|
|
|
|
MEMORY_MEASURE_INIT; \
|
2013-09-17 14:57:55 +02:00
|
|
|
\
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_printf(HEADER_FORMAT, TITLE); \
|
|
|
|
fflush(stdout); \
|
|
|
|
mbedtls_set_alarm(3); \
|
2013-09-17 14:57:55 +02:00
|
|
|
\
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = 0; \
|
|
|
|
for (ii = 1; !mbedtls_timing_alarmed && !ret; ii++) \
|
|
|
|
{ \
|
|
|
|
MEMORY_MEASURE_RESET; \
|
|
|
|
CODE; \
|
|
|
|
} \
|
2013-09-17 14:57:55 +02:00
|
|
|
\
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) \
|
|
|
|
{ \
|
|
|
|
mbedtls_printf("Feature Not Supported. Skipping.\n"); \
|
|
|
|
ret = 0; \
|
|
|
|
} \
|
|
|
|
else if (ret != 0) \
|
|
|
|
{ \
|
|
|
|
PRINT_ERROR; \
|
|
|
|
} \
|
|
|
|
else \
|
|
|
|
{ \
|
|
|
|
mbedtls_printf("%6lu " TYPE "/s", ii / 3); \
|
|
|
|
MEMORY_MEASURE_PRINT(sizeof(TYPE) + 1); \
|
|
|
|
mbedtls_printf("\n"); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2013-09-17 14:57:55 +02:00
|
|
|
|
2023-11-16 17:39:43 +01:00
|
|
|
#if !defined(MBEDTLS_TIMING_ALT)
|
2021-06-10 15:16:50 +02:00
|
|
|
#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
|
2023-01-11 14:50:10 +01:00
|
|
|
(defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
|
2021-06-10 15:16:50 +02:00
|
|
|
|
|
|
|
#define HAVE_HARDCLOCK
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static unsigned long mbedtls_timing_hardclock(void)
|
2021-06-10 15:16:50 +02:00
|
|
|
{
|
|
|
|
unsigned long tsc;
|
|
|
|
__asm rdtsc
|
2023-01-11 14:50:10 +01:00
|
|
|
__asm mov[tsc], eax
|
|
|
|
return tsc;
|
2021-06-10 15:16:50 +02:00
|
|
|
}
|
|
|
|
#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
|
|
|
|
( _MSC_VER && _M_IX86 ) || __WATCOMC__ */
|
|
|
|
|
|
|
|
/* some versions of mingw-64 have 32-bit longs even on x84_64 */
|
|
|
|
#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
|
2023-01-11 14:50:10 +01:00
|
|
|
defined(__GNUC__) && (defined(__i386__) || ( \
|
|
|
|
(defined(__amd64__) || defined(__x86_64__)) && __SIZEOF_LONG__ == 4))
|
2021-06-10 15:16:50 +02:00
|
|
|
|
|
|
|
#define HAVE_HARDCLOCK
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static unsigned long mbedtls_timing_hardclock(void)
|
2021-06-10 15:16:50 +02:00
|
|
|
{
|
|
|
|
unsigned long lo, hi;
|
2023-01-11 14:50:10 +01:00
|
|
|
asm volatile ("rdtsc" : "=a" (lo), "=d" (hi));
|
|
|
|
return lo;
|
2021-06-10 15:16:50 +02:00
|
|
|
}
|
|
|
|
#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
|
|
|
|
__GNUC__ && __i386__ */
|
|
|
|
|
|
|
|
#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
|
2023-01-11 14:50:10 +01:00
|
|
|
defined(__GNUC__) && (defined(__amd64__) || defined(__x86_64__))
|
2021-06-10 15:16:50 +02:00
|
|
|
|
|
|
|
#define HAVE_HARDCLOCK
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static unsigned long mbedtls_timing_hardclock(void)
|
2021-06-10 15:16:50 +02:00
|
|
|
{
|
|
|
|
unsigned long lo, hi;
|
2023-01-11 14:50:10 +01:00
|
|
|
asm volatile ("rdtsc" : "=a" (lo), "=d" (hi));
|
|
|
|
return lo | (hi << 32);
|
2021-06-10 15:16:50 +02:00
|
|
|
}
|
|
|
|
#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
|
|
|
|
__GNUC__ && ( __amd64__ || __x86_64__ ) */
|
|
|
|
|
|
|
|
#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
|
2023-01-11 14:50:10 +01:00
|
|
|
defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
|
2021-06-10 15:16:50 +02:00
|
|
|
|
|
|
|
#define HAVE_HARDCLOCK
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static unsigned long mbedtls_timing_hardclock(void)
|
2021-06-10 15:16:50 +02:00
|
|
|
{
|
|
|
|
unsigned long tbl, tbu0, tbu1;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
do {
|
|
|
|
asm volatile ("mftbu %0" : "=r" (tbu0));
|
|
|
|
asm volatile ("mftb %0" : "=r" (tbl));
|
|
|
|
asm volatile ("mftbu %0" : "=r" (tbu1));
|
|
|
|
} while (tbu0 != tbu1);
|
2021-06-10 15:16:50 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return tbl;
|
2021-06-10 15:16:50 +02:00
|
|
|
}
|
|
|
|
#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
|
|
|
|
__GNUC__ && ( __powerpc__ || __ppc__ ) */
|
|
|
|
|
|
|
|
#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
|
|
|
|
defined(__GNUC__) && defined(__sparc64__)
|
|
|
|
|
|
|
|
#if defined(__OpenBSD__)
|
|
|
|
#warning OpenBSD does not allow access to tick register using software version instead
|
|
|
|
#else
|
|
|
|
#define HAVE_HARDCLOCK
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static unsigned long mbedtls_timing_hardclock(void)
|
2021-06-10 15:16:50 +02:00
|
|
|
{
|
|
|
|
unsigned long tick;
|
2023-01-11 14:50:10 +01:00
|
|
|
asm volatile ("rdpr %%tick, %0;" : "=&r" (tick));
|
|
|
|
return tick;
|
2021-06-10 15:16:50 +02:00
|
|
|
}
|
|
|
|
#endif /* __OpenBSD__ */
|
|
|
|
#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
|
|
|
|
__GNUC__ && __sparc64__ */
|
|
|
|
|
|
|
|
#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
|
|
|
|
defined(__GNUC__) && defined(__sparc__) && !defined(__sparc64__)
|
|
|
|
|
|
|
|
#define HAVE_HARDCLOCK
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static unsigned long mbedtls_timing_hardclock(void)
|
2021-06-10 15:16:50 +02:00
|
|
|
{
|
|
|
|
unsigned long tick;
|
2023-01-11 14:50:10 +01:00
|
|
|
asm volatile (".byte 0x83, 0x41, 0x00, 0x00");
|
|
|
|
asm volatile ("mov %%g1, %0" : "=r" (tick));
|
|
|
|
return tick;
|
2021-06-10 15:16:50 +02:00
|
|
|
}
|
|
|
|
#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
|
|
|
|
__GNUC__ && __sparc__ && !__sparc64__ */
|
|
|
|
|
|
|
|
#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
|
|
|
|
defined(__GNUC__) && defined(__alpha__)
|
|
|
|
|
|
|
|
#define HAVE_HARDCLOCK
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static unsigned long mbedtls_timing_hardclock(void)
|
2021-06-10 15:16:50 +02:00
|
|
|
{
|
|
|
|
unsigned long cc;
|
2023-01-11 14:50:10 +01:00
|
|
|
asm volatile ("rpcc %0" : "=r" (cc));
|
|
|
|
return cc & 0xFFFFFFFF;
|
2021-06-10 15:16:50 +02:00
|
|
|
}
|
|
|
|
#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
|
|
|
|
__GNUC__ && __alpha__ */
|
|
|
|
|
|
|
|
#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
|
|
|
|
defined(__GNUC__) && defined(__ia64__)
|
|
|
|
|
|
|
|
#define HAVE_HARDCLOCK
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static unsigned long mbedtls_timing_hardclock(void)
|
2021-06-10 15:16:50 +02:00
|
|
|
{
|
|
|
|
unsigned long itc;
|
2023-01-11 14:50:10 +01:00
|
|
|
asm volatile ("mov %0 = ar.itc" : "=r" (itc));
|
|
|
|
return itc;
|
2021-06-10 15:16:50 +02:00
|
|
|
}
|
|
|
|
#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
|
|
|
|
__GNUC__ && __ia64__ */
|
|
|
|
|
Use QueryPerformanceCounter as fallback timer on non-x86 mingw
The QueryPerformanceCounter implementation previously was within
defined(_MSC_VER), but it works just as well on other Windows
toolchains, like mingw.
For most common mingw x86 build configurations, one of the earlier
inline assembly implementations would end up used, but for non-x86
(arm, aarch64), it would end up falling back on the gettimeofday
implementation.
This implementation did build successfully (as mingw toolchains do
provide gettimeofday, contrary to MSVC), but the header providing
gettimeofday, <sys/time.h>, wasn't ever included when building
targeting Windows - thus the function was called without a proper
declaration.
Clang 15 changes such implicit function declarations into a hard
error by default, when building in C99 mode (or newer) [1].
(While Clang 15 still is under development, this may still change
before it's released, but it's a valid issue in any case.)
[1] https://github.com/llvm/llvm-project/commit/7d644e1215b376ec5e915df9ea2eeb56e2d94626
Signed-off-by: Martin Storsjö <martin@martin.st>
2022-04-22 12:11:42 +02:00
|
|
|
#if !defined(HAVE_HARDCLOCK) && defined(_WIN32) && \
|
2021-06-10 15:16:50 +02:00
|
|
|
!defined(EFIX64) && !defined(EFI32)
|
|
|
|
|
|
|
|
#define HAVE_HARDCLOCK
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static unsigned long mbedtls_timing_hardclock(void)
|
2021-06-10 15:16:50 +02:00
|
|
|
{
|
|
|
|
LARGE_INTEGER offset;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
QueryPerformanceCounter(&offset);
|
2021-06-10 15:16:50 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return (unsigned long) (offset.QuadPart);
|
2021-06-10 15:16:50 +02:00
|
|
|
}
|
Use QueryPerformanceCounter as fallback timer on non-x86 mingw
The QueryPerformanceCounter implementation previously was within
defined(_MSC_VER), but it works just as well on other Windows
toolchains, like mingw.
For most common mingw x86 build configurations, one of the earlier
inline assembly implementations would end up used, but for non-x86
(arm, aarch64), it would end up falling back on the gettimeofday
implementation.
This implementation did build successfully (as mingw toolchains do
provide gettimeofday, contrary to MSVC), but the header providing
gettimeofday, <sys/time.h>, wasn't ever included when building
targeting Windows - thus the function was called without a proper
declaration.
Clang 15 changes such implicit function declarations into a hard
error by default, when building in C99 mode (or newer) [1].
(While Clang 15 still is under development, this may still change
before it's released, but it's a valid issue in any case.)
[1] https://github.com/llvm/llvm-project/commit/7d644e1215b376ec5e915df9ea2eeb56e2d94626
Signed-off-by: Martin Storsjö <martin@martin.st>
2022-04-22 12:11:42 +02:00
|
|
|
#endif /* !HAVE_HARDCLOCK && _WIN32 && !EFIX64 && !EFI32 */
|
2021-06-10 15:16:50 +02:00
|
|
|
|
|
|
|
#if !defined(HAVE_HARDCLOCK)
|
|
|
|
|
|
|
|
#define HAVE_HARDCLOCK
|
|
|
|
|
|
|
|
static int hardclock_init = 0;
|
|
|
|
static struct timeval tv_init;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static unsigned long mbedtls_timing_hardclock(void)
|
2021-06-10 15:16:50 +02:00
|
|
|
{
|
|
|
|
struct timeval tv_cur;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (hardclock_init == 0) {
|
|
|
|
gettimeofday(&tv_init, NULL);
|
2021-06-10 15:16:50 +02:00
|
|
|
hardclock_init = 1;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
gettimeofday(&tv_cur, NULL);
|
|
|
|
return (tv_cur.tv_sec - tv_init.tv_sec) * 1000000U
|
|
|
|
+ (tv_cur.tv_usec - tv_init.tv_usec);
|
2021-06-10 15:16:50 +02:00
|
|
|
}
|
|
|
|
#endif /* !HAVE_HARDCLOCK */
|
|
|
|
|
|
|
|
volatile int mbedtls_timing_alarmed = 0;
|
|
|
|
|
|
|
|
#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
|
|
|
|
|
|
|
|
/* It's OK to use a global because alarm() is supposed to be global anyway */
|
|
|
|
static DWORD alarmMs;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static void TimerProc(void *TimerContext)
|
2021-06-10 15:16:50 +02:00
|
|
|
{
|
|
|
|
(void) TimerContext;
|
2023-01-11 14:50:10 +01:00
|
|
|
Sleep(alarmMs);
|
2021-06-10 15:16:50 +02:00
|
|
|
mbedtls_timing_alarmed = 1;
|
|
|
|
/* _endthread will be called implicitly on return
|
2022-12-04 18:19:59 +01:00
|
|
|
* That ensures execution of thread function's epilogue */
|
2021-06-10 15:16:50 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static void mbedtls_set_alarm(int seconds)
|
2021-06-10 15:16:50 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (seconds == 0) {
|
2021-06-10 15:16:50 +02:00
|
|
|
/* No need to create a thread for this simple case.
|
|
|
|
* Also, this shorcut is more reliable at least on MinGW32 */
|
|
|
|
mbedtls_timing_alarmed = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mbedtls_timing_alarmed = 0;
|
|
|
|
alarmMs = seconds * 1000;
|
2023-01-11 14:50:10 +01:00
|
|
|
(void) _beginthread(TimerProc, 0, NULL);
|
2021-06-10 15:16:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#else /* _WIN32 && !EFIX64 && !EFI32 */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static void sighandler(int signum)
|
2021-06-10 15:16:50 +02:00
|
|
|
{
|
|
|
|
mbedtls_timing_alarmed = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
signal(signum, sighandler);
|
2021-06-10 15:16:50 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static void mbedtls_set_alarm(int seconds)
|
2021-06-10 15:16:50 +02:00
|
|
|
{
|
|
|
|
mbedtls_timing_alarmed = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
signal(SIGALRM, sighandler);
|
|
|
|
alarm(seconds);
|
|
|
|
if (seconds == 0) {
|
2021-06-10 15:16:50 +02:00
|
|
|
/* alarm(0) cancelled any previous pending alarm, but the
|
|
|
|
handler won't fire, so raise the flag straight away. */
|
|
|
|
mbedtls_timing_alarmed = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* _WIN32 && !EFIX64 && !EFI32 */
|
2023-11-16 17:39:43 +01:00
|
|
|
#endif /* !MBEDTLS_TIMING_ALT */
|
2021-06-10 15:16:50 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static int myrand(void *rng_state, unsigned char *output, size_t len)
|
2015-02-12 12:37:29 +01:00
|
|
|
{
|
|
|
|
size_t use_len;
|
|
|
|
int rnd;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (rng_state != NULL) {
|
2015-02-12 12:37:29 +01:00
|
|
|
rng_state = NULL;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2015-02-12 12:37:29 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
while (len > 0) {
|
2015-02-12 12:37:29 +01:00
|
|
|
use_len = len;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (use_len > sizeof(int)) {
|
2015-02-12 12:37:29 +01:00
|
|
|
use_len = sizeof(int);
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2015-02-12 12:37:29 +01:00
|
|
|
|
|
|
|
rnd = rand();
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(output, &rnd, use_len);
|
2015-02-12 12:37:29 +01:00
|
|
|
output += use_len;
|
|
|
|
len -= use_len;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2015-02-12 12:37:29 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
#define CHECK_AND_CONTINUE(R) \
|
2019-02-06 19:06:15 +01:00
|
|
|
{ \
|
2023-01-11 14:50:10 +01:00
|
|
|
int CHECK_AND_CONTINUE_ret = (R); \
|
|
|
|
if (CHECK_AND_CONTINUE_ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) { \
|
|
|
|
mbedtls_printf("Feature not supported. Skipping.\n"); \
|
2019-02-06 19:06:15 +01:00
|
|
|
continue; \
|
|
|
|
} \
|
2023-01-11 14:50:10 +01:00
|
|
|
else if (CHECK_AND_CONTINUE_ret != 0) { \
|
|
|
|
mbedtls_exit(1); \
|
2019-02-06 19:06:15 +01:00
|
|
|
} \
|
|
|
|
}
|
2018-12-14 12:54:59 +01:00
|
|
|
|
2020-07-24 02:06:46 +02:00
|
|
|
#if defined(MBEDTLS_ECP_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
static int set_ecp_curve(const char *string, mbedtls_ecp_curve_info *curve)
|
2020-07-24 02:06:46 +02:00
|
|
|
{
|
|
|
|
const mbedtls_ecp_curve_info *found =
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecp_curve_info_from_name(string);
|
|
|
|
if (found != NULL) {
|
2020-07-24 02:06:46 +02:00
|
|
|
*curve = *found;
|
2023-01-11 14:50:10 +01:00
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
2020-07-24 02:06:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
unsigned char buf[BUFSIZE];
|
|
|
|
|
2013-09-17 15:30:51 +02:00
|
|
|
typedef struct {
|
2021-05-31 17:58:57 +02:00
|
|
|
char md5, ripemd160, sha1, sha256, sha512,
|
2022-05-20 14:26:00 +02:00
|
|
|
sha3_224, sha3_256, sha3_384, sha3_512,
|
2021-05-31 17:58:57 +02:00
|
|
|
des3, des,
|
2024-01-12 17:37:07 +01:00
|
|
|
aes_cbc, aes_cfb128, aes_cfb8, aes_ctr, aes_gcm, aes_ccm, aes_xts, chachapoly,
|
2018-05-09 10:21:28 +02:00
|
|
|
aes_cmac, des3_cmac,
|
2021-05-31 17:58:57 +02:00
|
|
|
aria, camellia, chacha20,
|
2016-05-16 23:25:45 +02:00
|
|
|
poly1305,
|
2021-01-29 16:46:31 +01:00
|
|
|
ctr_drbg, hmac_drbg,
|
2013-09-17 16:04:08 +02:00
|
|
|
rsa, dhm, ecdsa, ecdh;
|
2013-09-17 15:30:51 +02:00
|
|
|
} todo_list;
|
|
|
|
|
2018-12-06 18:43:31 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int main(int argc, char *argv[])
|
2011-05-26 15:16:06 +02:00
|
|
|
{
|
2014-12-19 18:05:43 +01:00
|
|
|
int i;
|
2013-09-17 16:04:08 +02:00
|
|
|
unsigned char tmp[200];
|
2013-09-17 14:57:55 +02:00
|
|
|
char title[TITLE_LEN];
|
2013-09-17 15:30:51 +02:00
|
|
|
todo_list todo;
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
|
2015-05-27 16:29:56 +02:00
|
|
|
unsigned char alloc_buf[HEAP_SIZE] = { 0 };
|
2014-12-18 17:35:52 +01:00
|
|
|
#endif
|
2020-07-24 02:06:46 +02:00
|
|
|
#if defined(MBEDTLS_ECP_C)
|
|
|
|
mbedtls_ecp_curve_info single_curve[2] = {
|
|
|
|
{ MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
|
|
|
|
{ MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
|
|
|
|
};
|
2023-01-11 14:50:10 +01:00
|
|
|
const mbedtls_ecp_curve_info *curve_list = mbedtls_ecp_curve_list();
|
2020-07-24 02:06:46 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_ECP_C)
|
|
|
|
(void) curve_list; /* Unused in some configurations where no benchmark uses ECC */
|
|
|
|
#endif
|
2011-11-18 15:26:47 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (argc <= 1) {
|
|
|
|
memset(&todo, 1, sizeof(todo));
|
|
|
|
} else {
|
|
|
|
memset(&todo, 0, sizeof(todo));
|
2013-09-17 15:30:51 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 1; i < argc; i++) {
|
|
|
|
if (strcmp(argv[i], "md5") == 0) {
|
2015-05-11 19:21:39 +02:00
|
|
|
todo.md5 = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(argv[i], "ripemd160") == 0) {
|
2015-05-11 19:21:39 +02:00
|
|
|
todo.ripemd160 = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(argv[i], "sha1") == 0) {
|
2015-05-11 19:21:39 +02:00
|
|
|
todo.sha1 = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(argv[i], "sha256") == 0) {
|
2015-05-11 19:21:39 +02:00
|
|
|
todo.sha256 = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(argv[i], "sha512") == 0) {
|
2015-05-11 19:21:39 +02:00
|
|
|
todo.sha512 = 1;
|
2023-02-07 19:55:31 +01:00
|
|
|
} else if (strcmp(argv[i], "sha3_224") == 0) {
|
2022-05-20 14:26:00 +02:00
|
|
|
todo.sha3_224 = 1;
|
2023-02-07 19:55:31 +01:00
|
|
|
} else if (strcmp(argv[i], "sha3_256") == 0) {
|
2022-05-20 14:26:00 +02:00
|
|
|
todo.sha3_256 = 1;
|
2023-02-07 19:55:31 +01:00
|
|
|
} else if (strcmp(argv[i], "sha3_384") == 0) {
|
2022-05-20 14:26:00 +02:00
|
|
|
todo.sha3_384 = 1;
|
2023-02-07 19:55:31 +01:00
|
|
|
} else if (strcmp(argv[i], "sha3_512") == 0) {
|
2022-05-20 14:26:00 +02:00
|
|
|
todo.sha3_512 = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(argv[i], "des3") == 0) {
|
2013-09-17 15:30:51 +02:00
|
|
|
todo.des3 = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(argv[i], "des") == 0) {
|
2013-09-17 15:30:51 +02:00
|
|
|
todo.des = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(argv[i], "aes_cbc") == 0) {
|
2013-09-17 15:30:51 +02:00
|
|
|
todo.aes_cbc = 1;
|
2023-09-12 03:03:50 +02:00
|
|
|
} else if (strcmp(argv[i], "aes_cfb128") == 0) {
|
|
|
|
todo.aes_cfb128 = 1;
|
2023-09-12 03:45:37 +02:00
|
|
|
} else if (strcmp(argv[i], "aes_cfb8") == 0) {
|
|
|
|
todo.aes_cfb8 = 1;
|
2024-01-12 17:37:07 +01:00
|
|
|
} else if (strcmp(argv[i], "aes_ctr") == 0) {
|
|
|
|
todo.aes_ctr = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(argv[i], "aes_xts") == 0) {
|
2016-06-09 23:22:58 +02:00
|
|
|
todo.aes_xts = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(argv[i], "aes_gcm") == 0) {
|
2013-09-17 15:30:51 +02:00
|
|
|
todo.aes_gcm = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(argv[i], "aes_ccm") == 0) {
|
2014-05-07 12:03:02 +02:00
|
|
|
todo.aes_ccm = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(argv[i], "chachapoly") == 0) {
|
2018-05-09 10:21:28 +02:00
|
|
|
todo.chachapoly = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(argv[i], "aes_cmac") == 0) {
|
2016-10-05 15:14:19 +02:00
|
|
|
todo.aes_cmac = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(argv[i], "des3_cmac") == 0) {
|
2016-10-05 15:14:19 +02:00
|
|
|
todo.des3_cmac = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(argv[i], "aria") == 0) {
|
2018-02-21 10:47:47 +01:00
|
|
|
todo.aria = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(argv[i], "camellia") == 0) {
|
2013-09-17 15:30:51 +02:00
|
|
|
todo.camellia = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(argv[i], "chacha20") == 0) {
|
2016-05-15 22:28:08 +02:00
|
|
|
todo.chacha20 = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(argv[i], "poly1305") == 0) {
|
2016-05-16 23:25:45 +02:00
|
|
|
todo.poly1305 = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(argv[i], "ctr_drbg") == 0) {
|
2013-09-17 15:30:51 +02:00
|
|
|
todo.ctr_drbg = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(argv[i], "hmac_drbg") == 0) {
|
2014-01-30 20:59:00 +01:00
|
|
|
todo.hmac_drbg = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(argv[i], "rsa") == 0) {
|
2013-09-17 15:30:51 +02:00
|
|
|
todo.rsa = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(argv[i], "dhm") == 0) {
|
2013-09-17 15:30:51 +02:00
|
|
|
todo.dhm = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(argv[i], "ecdsa") == 0) {
|
2013-09-17 16:04:08 +02:00
|
|
|
todo.ecdsa = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(argv[i], "ecdh") == 0) {
|
2013-09-17 16:04:08 +02:00
|
|
|
todo.ecdh = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-07-24 02:06:46 +02:00
|
|
|
#if defined(MBEDTLS_ECP_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
else if (set_ecp_curve(argv[i], single_curve)) {
|
2020-07-24 02:06:46 +02:00
|
|
|
curve_list = single_curve;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-07-24 02:06:46 +02:00
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
else {
|
|
|
|
mbedtls_printf("Unrecognized option: %s\n", argv[i]);
|
|
|
|
mbedtls_printf("Available options: " OPTIONS);
|
2013-09-17 15:30:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_printf("\n");
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_memory_buffer_alloc_init(alloc_buf, sizeof(alloc_buf));
|
2014-12-18 17:35:52 +01:00
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(buf, 0xAA, sizeof(buf));
|
|
|
|
memset(tmp, 0xBB, sizeof(tmp));
|
2013-09-17 15:30:51 +02:00
|
|
|
|
2022-01-04 09:47:54 +01:00
|
|
|
/* Avoid "unused static function" warning in configurations without
|
|
|
|
* symmetric crypto. */
|
2022-01-05 09:54:37 +01:00
|
|
|
(void) mbedtls_timing_hardclock;
|
2022-01-04 09:47:54 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_MD5_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (todo.md5) {
|
|
|
|
TIME_AND_TSC("MD5", mbedtls_md5(buf, BUFSIZE, tmp));
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
#endif
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_RIPEMD160_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (todo.ripemd160) {
|
|
|
|
TIME_AND_TSC("RIPEMD160", mbedtls_ripemd160(buf, BUFSIZE, tmp));
|
|
|
|
}
|
2014-01-17 14:29:46 +01:00
|
|
|
#endif
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SHA1_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (todo.sha1) {
|
|
|
|
TIME_AND_TSC("SHA-1", mbedtls_sha1(buf, BUFSIZE, tmp));
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
#endif
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SHA256_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (todo.sha256) {
|
|
|
|
TIME_AND_TSC("SHA-256", mbedtls_sha256(buf, BUFSIZE, tmp, 0));
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
#endif
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SHA512_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (todo.sha512) {
|
|
|
|
TIME_AND_TSC("SHA-512", mbedtls_sha512(buf, BUFSIZE, tmp, 0));
|
|
|
|
}
|
2009-02-09 23:33:30 +01:00
|
|
|
#endif
|
2022-05-20 14:26:00 +02:00
|
|
|
#if defined(MBEDTLS_SHA3_C)
|
2023-02-08 00:50:04 +01:00
|
|
|
if (todo.sha3_224) {
|
|
|
|
TIME_AND_TSC("SHA3-224", mbedtls_sha3(MBEDTLS_SHA3_224, buf, BUFSIZE, tmp, 28));
|
|
|
|
}
|
|
|
|
if (todo.sha3_256) {
|
|
|
|
TIME_AND_TSC("SHA3-256", mbedtls_sha3(MBEDTLS_SHA3_256, buf, BUFSIZE, tmp, 32));
|
|
|
|
}
|
|
|
|
if (todo.sha3_384) {
|
|
|
|
TIME_AND_TSC("SHA3-384", mbedtls_sha3(MBEDTLS_SHA3_384, buf, BUFSIZE, tmp, 48));
|
|
|
|
}
|
|
|
|
if (todo.sha3_512) {
|
|
|
|
TIME_AND_TSC("SHA3-512", mbedtls_sha3(MBEDTLS_SHA3_512, buf, BUFSIZE, tmp, 64));
|
|
|
|
}
|
2022-05-20 14:26:00 +02:00
|
|
|
#endif
|
2009-02-09 23:33:30 +01:00
|
|
|
|
2016-10-05 15:14:19 +02:00
|
|
|
#if defined(MBEDTLS_DES_C)
|
|
|
|
#if defined(MBEDTLS_CIPHER_MODE_CBC)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (todo.des3) {
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_des3_context des3;
|
2023-10-09 12:40:17 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_des3_init(&des3);
|
|
|
|
if (mbedtls_des3_set3key_enc(&des3, tmp) != 0) {
|
|
|
|
mbedtls_exit(1);
|
|
|
|
}
|
|
|
|
TIME_AND_TSC("3DES",
|
|
|
|
mbedtls_des3_crypt_cbc(&des3, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf));
|
|
|
|
mbedtls_des3_free(&des3);
|
2013-09-17 14:57:55 +02:00
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (todo.des) {
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_des_context des;
|
2023-10-09 12:40:17 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_des_init(&des);
|
|
|
|
if (mbedtls_des_setkey_enc(&des, tmp) != 0) {
|
|
|
|
mbedtls_exit(1);
|
|
|
|
}
|
|
|
|
TIME_AND_TSC("DES",
|
|
|
|
mbedtls_des_crypt_cbc(&des, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf));
|
|
|
|
mbedtls_des_free(&des);
|
2013-09-17 14:57:55 +02:00
|
|
|
}
|
2016-10-05 15:14:19 +02:00
|
|
|
|
|
|
|
#endif /* MBEDTLS_CIPHER_MODE_CBC */
|
|
|
|
#if defined(MBEDTLS_CMAC_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (todo.des3_cmac) {
|
2016-10-05 15:14:19 +02:00
|
|
|
unsigned char output[8];
|
|
|
|
const mbedtls_cipher_info_t *cipher_info;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
memset(tmp, 0, sizeof(tmp));
|
2016-10-05 15:14:19 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
cipher_info = mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_DES_EDE3_ECB);
|
2016-10-05 15:14:19 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TIME_AND_TSC("3DES-CMAC",
|
|
|
|
mbedtls_cipher_cmac(cipher_info, tmp, 192, buf,
|
|
|
|
BUFSIZE, output));
|
2016-10-05 15:14:19 +02:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_CMAC_C */
|
|
|
|
#endif /* MBEDTLS_DES_C */
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_AES_C)
|
|
|
|
#if defined(MBEDTLS_CIPHER_MODE_CBC)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (todo.aes_cbc) {
|
2014-12-19 18:05:43 +01:00
|
|
|
int keysize;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_aes_context aes;
|
2023-10-09 12:40:17 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_aes_init(&aes);
|
|
|
|
for (keysize = 128; keysize <= 256; keysize += 64) {
|
|
|
|
mbedtls_snprintf(title, sizeof(title), "AES-CBC-%d", keysize);
|
2013-09-17 14:57:55 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
memset(tmp, 0, sizeof(tmp));
|
|
|
|
CHECK_AND_CONTINUE(mbedtls_aes_setkey_enc(&aes, tmp, keysize));
|
2013-09-17 14:57:55 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TIME_AND_TSC(title,
|
|
|
|
mbedtls_aes_crypt_cbc(&aes, MBEDTLS_AES_ENCRYPT, BUFSIZE, tmp, buf, buf));
|
2013-09-17 14:57:55 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_aes_free(&aes);
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
2013-09-17 14:57:55 +02:00
|
|
|
#endif
|
2023-09-12 03:03:50 +02:00
|
|
|
#if defined(MBEDTLS_CIPHER_MODE_CFB)
|
|
|
|
if (todo.aes_cfb128) {
|
|
|
|
int keysize;
|
|
|
|
size_t iv_off = 0;
|
|
|
|
mbedtls_aes_context aes;
|
2023-10-09 12:40:17 +02:00
|
|
|
|
2023-09-12 03:03:50 +02:00
|
|
|
mbedtls_aes_init(&aes);
|
|
|
|
for (keysize = 128; keysize <= 256; keysize += 64) {
|
|
|
|
mbedtls_snprintf(title, sizeof(title), "AES-CFB128-%d", keysize);
|
|
|
|
|
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
memset(tmp, 0, sizeof(tmp));
|
|
|
|
CHECK_AND_CONTINUE(mbedtls_aes_setkey_enc(&aes, tmp, keysize));
|
|
|
|
|
|
|
|
TIME_AND_TSC(title,
|
|
|
|
mbedtls_aes_crypt_cfb128(&aes, MBEDTLS_AES_ENCRYPT, BUFSIZE,
|
|
|
|
&iv_off, tmp, buf, buf));
|
|
|
|
}
|
|
|
|
mbedtls_aes_free(&aes);
|
|
|
|
}
|
2023-09-12 03:45:37 +02:00
|
|
|
if (todo.aes_cfb8) {
|
|
|
|
int keysize;
|
|
|
|
mbedtls_aes_context aes;
|
2023-10-09 12:40:17 +02:00
|
|
|
|
2023-09-12 03:45:37 +02:00
|
|
|
mbedtls_aes_init(&aes);
|
|
|
|
for (keysize = 128; keysize <= 256; keysize += 64) {
|
|
|
|
mbedtls_snprintf(title, sizeof(title), "AES-CFB8-%d", keysize);
|
|
|
|
|
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
memset(tmp, 0, sizeof(tmp));
|
|
|
|
CHECK_AND_CONTINUE(mbedtls_aes_setkey_enc(&aes, tmp, keysize));
|
|
|
|
|
|
|
|
TIME_AND_TSC(title,
|
|
|
|
mbedtls_aes_crypt_cfb8(&aes, MBEDTLS_AES_ENCRYPT, BUFSIZE, tmp, buf, buf));
|
|
|
|
}
|
|
|
|
mbedtls_aes_free(&aes);
|
|
|
|
}
|
2023-09-12 03:03:50 +02:00
|
|
|
#endif
|
2024-01-12 17:37:07 +01:00
|
|
|
#if defined(MBEDTLS_CIPHER_MODE_CTR)
|
|
|
|
if (todo.aes_ctr) {
|
|
|
|
int keysize;
|
|
|
|
mbedtls_aes_context aes;
|
|
|
|
|
|
|
|
uint8_t stream_block[16];
|
|
|
|
size_t nc_off;
|
|
|
|
|
|
|
|
mbedtls_aes_init(&aes);
|
|
|
|
for (keysize = 128; keysize <= 256; keysize += 64) {
|
|
|
|
mbedtls_snprintf(title, sizeof(title), "AES-CTR-%d", keysize);
|
|
|
|
|
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
memset(tmp, 0, sizeof(tmp));
|
|
|
|
memset(stream_block, 0, sizeof(stream_block));
|
|
|
|
nc_off = 0;
|
|
|
|
|
|
|
|
CHECK_AND_CONTINUE(mbedtls_aes_setkey_enc(&aes, tmp, keysize));
|
|
|
|
|
|
|
|
TIME_AND_TSC(title, mbedtls_aes_crypt_ctr(&aes, BUFSIZE, &nc_off, tmp, stream_block,
|
|
|
|
buf, buf));
|
|
|
|
}
|
|
|
|
mbedtls_aes_free(&aes);
|
|
|
|
}
|
|
|
|
#endif
|
2016-06-09 23:22:58 +02:00
|
|
|
#if defined(MBEDTLS_CIPHER_MODE_XTS)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (todo.aes_xts) {
|
2016-06-09 23:22:58 +02:00
|
|
|
int keysize;
|
2018-05-29 19:55:17 +02:00
|
|
|
mbedtls_aes_xts_context ctx;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_aes_xts_init(&ctx);
|
|
|
|
for (keysize = 128; keysize <= 256; keysize += 128) {
|
|
|
|
mbedtls_snprintf(title, sizeof(title), "AES-XTS-%d", keysize);
|
2016-06-09 23:22:58 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
memset(tmp, 0, sizeof(tmp));
|
|
|
|
CHECK_AND_CONTINUE(mbedtls_aes_xts_setkey_enc(&ctx, tmp, keysize * 2));
|
2016-06-09 23:22:58 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TIME_AND_TSC(title,
|
|
|
|
mbedtls_aes_crypt_xts(&ctx, MBEDTLS_AES_ENCRYPT, BUFSIZE,
|
|
|
|
tmp, buf, buf));
|
2018-05-29 19:55:17 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_aes_xts_free(&ctx);
|
2016-06-09 23:22:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_GCM_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (todo.aes_gcm) {
|
2014-12-19 18:05:43 +01:00
|
|
|
int keysize;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_gcm_context gcm;
|
2015-04-28 21:42:17 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_gcm_init(&gcm);
|
|
|
|
for (keysize = 128; keysize <= 256; keysize += 64) {
|
|
|
|
mbedtls_snprintf(title, sizeof(title), "AES-GCM-%d", keysize);
|
2013-09-17 14:57:55 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
memset(tmp, 0, sizeof(tmp));
|
|
|
|
mbedtls_gcm_setkey(&gcm, MBEDTLS_CIPHER_ID_AES, tmp, keysize);
|
2013-09-17 14:57:55 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TIME_AND_TSC(title,
|
|
|
|
mbedtls_gcm_crypt_and_tag(&gcm, MBEDTLS_GCM_ENCRYPT, BUFSIZE, tmp,
|
|
|
|
12, NULL, 0, buf, buf, 16, tmp));
|
2013-12-16 16:43:10 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_gcm_free(&gcm);
|
2013-09-17 14:57:55 +02:00
|
|
|
}
|
2012-03-20 14:50:09 +01:00
|
|
|
}
|
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_CCM_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (todo.aes_ccm) {
|
2014-12-19 18:05:43 +01:00
|
|
|
int keysize;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ccm_context ccm;
|
2015-04-28 18:02:54 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ccm_init(&ccm);
|
|
|
|
for (keysize = 128; keysize <= 256; keysize += 64) {
|
|
|
|
mbedtls_snprintf(title, sizeof(title), "AES-CCM-%d", keysize);
|
2014-05-07 12:03:02 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
memset(tmp, 0, sizeof(tmp));
|
|
|
|
mbedtls_ccm_setkey(&ccm, MBEDTLS_CIPHER_ID_AES, tmp, keysize);
|
2014-05-07 12:03:02 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TIME_AND_TSC(title,
|
|
|
|
mbedtls_ccm_encrypt_and_tag(&ccm, BUFSIZE, tmp,
|
|
|
|
12, NULL, 0, buf, buf, tmp, 16));
|
2014-05-07 12:03:02 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ccm_free(&ccm);
|
2014-05-07 12:03:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2018-05-09 10:21:28 +02:00
|
|
|
#if defined(MBEDTLS_CHACHAPOLY_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (todo.chachapoly) {
|
2018-05-09 10:21:28 +02:00
|
|
|
mbedtls_chachapoly_context chachapoly;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_chachapoly_init(&chachapoly);
|
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
memset(tmp, 0, sizeof(tmp));
|
2018-05-09 10:21:28 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_snprintf(title, sizeof(title), "ChaCha20-Poly1305");
|
2018-05-09 10:21:28 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_chachapoly_setkey(&chachapoly, tmp);
|
2018-05-09 10:21:28 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TIME_AND_TSC(title,
|
|
|
|
mbedtls_chachapoly_encrypt_and_tag(&chachapoly,
|
|
|
|
BUFSIZE, tmp, NULL, 0, buf, buf, tmp));
|
2018-05-09 10:21:28 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_chachapoly_free(&chachapoly);
|
2018-05-09 10:21:28 +02:00
|
|
|
}
|
|
|
|
#endif
|
2016-10-05 15:14:19 +02:00
|
|
|
#if defined(MBEDTLS_CMAC_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (todo.aes_cmac) {
|
2016-10-05 15:14:19 +02:00
|
|
|
unsigned char output[16];
|
|
|
|
const mbedtls_cipher_info_t *cipher_info;
|
|
|
|
mbedtls_cipher_type_t cipher_type;
|
|
|
|
int keysize;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (keysize = 128, cipher_type = MBEDTLS_CIPHER_AES_128_ECB;
|
2016-10-05 15:14:19 +02:00
|
|
|
keysize <= 256;
|
2023-01-11 14:50:10 +01:00
|
|
|
keysize += 64, cipher_type++) {
|
|
|
|
mbedtls_snprintf(title, sizeof(title), "AES-CMAC-%d", keysize);
|
2016-10-05 15:14:19 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
memset(tmp, 0, sizeof(tmp));
|
2016-10-05 15:14:19 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
cipher_info = mbedtls_cipher_info_from_type(cipher_type);
|
2016-10-05 15:14:19 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TIME_AND_TSC(title,
|
|
|
|
mbedtls_cipher_cmac(cipher_info, tmp, keysize,
|
|
|
|
buf, BUFSIZE, output));
|
2016-10-05 15:14:19 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
memset(tmp, 0, sizeof(tmp));
|
|
|
|
TIME_AND_TSC("AES-CMAC-PRF-128",
|
|
|
|
mbedtls_aes_cmac_prf_128(tmp, 16, buf, BUFSIZE,
|
|
|
|
output));
|
2016-10-05 15:14:19 +02:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_CMAC_C */
|
|
|
|
#endif /* MBEDTLS_AES_C */
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2018-02-21 10:47:47 +01:00
|
|
|
#if defined(MBEDTLS_ARIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (todo.aria) {
|
2018-02-21 10:47:47 +01:00
|
|
|
int keysize;
|
|
|
|
mbedtls_aria_context aria;
|
2023-10-09 12:40:17 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_aria_init(&aria);
|
|
|
|
for (keysize = 128; keysize <= 256; keysize += 64) {
|
|
|
|
mbedtls_snprintf(title, sizeof(title), "ARIA-CBC-%d", keysize);
|
|
|
|
|
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
memset(tmp, 0, sizeof(tmp));
|
|
|
|
mbedtls_aria_setkey_enc(&aria, tmp, keysize);
|
|
|
|
|
|
|
|
TIME_AND_TSC(title,
|
|
|
|
mbedtls_aria_crypt_cbc(&aria, MBEDTLS_ARIA_ENCRYPT,
|
|
|
|
BUFSIZE, tmp, buf, buf));
|
2018-02-21 10:47:47 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_aria_free(&aria);
|
2018-02-21 10:47:47 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_CAMELLIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (todo.camellia) {
|
2014-12-19 18:05:43 +01:00
|
|
|
int keysize;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_camellia_context camellia;
|
2023-10-09 12:40:17 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_camellia_init(&camellia);
|
|
|
|
for (keysize = 128; keysize <= 256; keysize += 64) {
|
|
|
|
mbedtls_snprintf(title, sizeof(title), "CAMELLIA-CBC-%d", keysize);
|
|
|
|
|
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
memset(tmp, 0, sizeof(tmp));
|
|
|
|
mbedtls_camellia_setkey_enc(&camellia, tmp, keysize);
|
|
|
|
|
|
|
|
TIME_AND_TSC(title,
|
|
|
|
mbedtls_camellia_crypt_cbc(&camellia, MBEDTLS_CAMELLIA_ENCRYPT,
|
|
|
|
BUFSIZE, tmp, buf, buf));
|
2013-09-17 14:57:55 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_camellia_free(&camellia);
|
2009-01-11 00:31:23 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-05-15 22:28:08 +02:00
|
|
|
#if defined(MBEDTLS_CHACHA20_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (todo.chacha20) {
|
|
|
|
TIME_AND_TSC("ChaCha20", mbedtls_chacha20_crypt(buf, buf, 0U, BUFSIZE, buf, buf));
|
2016-05-15 22:28:08 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-05-16 23:25:45 +02:00
|
|
|
#if defined(MBEDTLS_POLY1305_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (todo.poly1305) {
|
|
|
|
TIME_AND_TSC("Poly1305", mbedtls_poly1305_mac(buf, buf, BUFSIZE, buf));
|
2016-05-16 23:25:45 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_CTR_DRBG_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (todo.ctr_drbg) {
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ctr_drbg_context ctr_drbg;
|
2011-11-29 12:23:58 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ctr_drbg_init(&ctr_drbg);
|
|
|
|
if (mbedtls_ctr_drbg_seed(&ctr_drbg, myrand, NULL, NULL, 0) != 0) {
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_exit(1);
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
TIME_AND_TSC("CTR_DRBG (NOPR)",
|
|
|
|
mbedtls_ctr_drbg_random(&ctr_drbg, buf, BUFSIZE));
|
|
|
|
mbedtls_ctr_drbg_free(&ctr_drbg);
|
2011-11-29 12:23:58 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ctr_drbg_init(&ctr_drbg);
|
|
|
|
if (mbedtls_ctr_drbg_seed(&ctr_drbg, myrand, NULL, NULL, 0) != 0) {
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_exit(1);
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
mbedtls_ctr_drbg_set_prediction_resistance(&ctr_drbg, MBEDTLS_CTR_DRBG_PR_ON);
|
|
|
|
TIME_AND_TSC("CTR_DRBG (PR)",
|
|
|
|
mbedtls_ctr_drbg_random(&ctr_drbg, buf, BUFSIZE));
|
|
|
|
mbedtls_ctr_drbg_free(&ctr_drbg);
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
2013-09-17 14:57:55 +02:00
|
|
|
#endif
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2022-10-03 12:18:18 +02:00
|
|
|
#if defined(MBEDTLS_HMAC_DRBG_C) && \
|
2023-01-11 14:50:10 +01:00
|
|
|
(defined(MBEDTLS_SHA1_C) || defined(MBEDTLS_SHA256_C))
|
|
|
|
if (todo.hmac_drbg) {
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_hmac_drbg_context hmac_drbg;
|
|
|
|
const mbedtls_md_info_t *md_info;
|
2014-01-30 20:59:00 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_hmac_drbg_init(&hmac_drbg);
|
2015-04-28 22:07:14 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SHA1_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA1)) == NULL) {
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_exit(1);
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2014-01-30 20:59:00 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_hmac_drbg_seed(&hmac_drbg, md_info, myrand, NULL, NULL, 0) != 0) {
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_exit(1);
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
TIME_AND_TSC("HMAC_DRBG SHA-1 (NOPR)",
|
|
|
|
mbedtls_hmac_drbg_random(&hmac_drbg, buf, BUFSIZE));
|
2015-04-08 12:49:31 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_hmac_drbg_seed(&hmac_drbg, md_info, myrand, NULL, NULL, 0) != 0) {
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_exit(1);
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
mbedtls_hmac_drbg_set_prediction_resistance(&hmac_drbg,
|
|
|
|
MBEDTLS_HMAC_DRBG_PR_ON);
|
|
|
|
TIME_AND_TSC("HMAC_DRBG SHA-1 (PR)",
|
|
|
|
mbedtls_hmac_drbg_random(&hmac_drbg, buf, BUFSIZE));
|
2014-01-30 20:59:00 +01:00
|
|
|
#endif
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SHA256_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256)) == NULL) {
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_exit(1);
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2014-01-30 20:59:00 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_hmac_drbg_seed(&hmac_drbg, md_info, myrand, NULL, NULL, 0) != 0) {
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_exit(1);
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
TIME_AND_TSC("HMAC_DRBG SHA-256 (NOPR)",
|
|
|
|
mbedtls_hmac_drbg_random(&hmac_drbg, buf, BUFSIZE));
|
2015-04-08 12:49:31 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_hmac_drbg_seed(&hmac_drbg, md_info, myrand, NULL, NULL, 0) != 0) {
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_exit(1);
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
mbedtls_hmac_drbg_set_prediction_resistance(&hmac_drbg,
|
|
|
|
MBEDTLS_HMAC_DRBG_PR_ON);
|
|
|
|
TIME_AND_TSC("HMAC_DRBG SHA-256 (PR)",
|
|
|
|
mbedtls_hmac_drbg_random(&hmac_drbg, buf, BUFSIZE));
|
2014-01-30 20:59:00 +01:00
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_hmac_drbg_free(&hmac_drbg);
|
2014-01-30 20:59:00 +01:00
|
|
|
}
|
2022-10-03 12:18:18 +02:00
|
|
|
#endif /* MBEDTLS_HMAC_DRBG_C && ( MBEDTLS_SHA1_C || MBEDTLS_SHA256_C ) */
|
2014-01-30 20:59:00 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (todo.rsa) {
|
2014-12-19 18:05:43 +01:00
|
|
|
int keysize;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_rsa_context rsa;
|
2023-10-09 12:40:17 +02:00
|
|
|
|
2023-11-16 15:31:32 +01:00
|
|
|
for (keysize = 2048; keysize <= 4096; keysize += 1024) {
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_snprintf(title, sizeof(title), "RSA-%d", keysize);
|
2009-10-25 13:36:53 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_rsa_init(&rsa);
|
|
|
|
mbedtls_rsa_gen_key(&rsa, myrand, NULL, keysize, 65537);
|
2009-10-25 13:36:53 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TIME_PUBLIC(title, " public",
|
|
|
|
buf[0] = 0;
|
|
|
|
ret = mbedtls_rsa_public(&rsa, buf, buf));
|
2009-10-25 13:36:53 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TIME_PUBLIC(title, "private",
|
|
|
|
buf[0] = 0;
|
|
|
|
ret = mbedtls_rsa_private(&rsa, myrand, NULL, buf, buf));
|
2009-10-25 13:36:53 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_rsa_free(&rsa);
|
2013-09-17 14:57:55 +02:00
|
|
|
}
|
2009-10-25 13:36:53 +01:00
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
#endif
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_BIGNUM_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (todo.dhm) {
|
2015-07-19 15:01:28 +02:00
|
|
|
int dhm_sizes[] = { 2048, 3072 };
|
2018-03-09 02:40:56 +01:00
|
|
|
static const unsigned char dhm_P_2048[] =
|
2017-10-04 14:13:34 +02:00
|
|
|
MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
|
2018-03-09 02:40:56 +01:00
|
|
|
static const unsigned char dhm_P_3072[] =
|
2017-10-04 14:13:34 +02:00
|
|
|
MBEDTLS_DHM_RFC3526_MODP_3072_P_BIN;
|
2018-03-09 02:40:56 +01:00
|
|
|
static const unsigned char dhm_G_2048[] =
|
2017-10-04 14:13:34 +02:00
|
|
|
MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
|
2018-03-09 02:40:56 +01:00
|
|
|
static const unsigned char dhm_G_3072[] =
|
2017-10-04 14:13:34 +02:00
|
|
|
MBEDTLS_DHM_RFC3526_MODP_3072_G_BIN;
|
|
|
|
|
|
|
|
const unsigned char *dhm_P[] = { dhm_P_2048, dhm_P_3072 };
|
2023-01-11 14:50:10 +01:00
|
|
|
const size_t dhm_P_size[] = { sizeof(dhm_P_2048),
|
|
|
|
sizeof(dhm_P_3072) };
|
2017-10-04 14:13:34 +02:00
|
|
|
|
|
|
|
const unsigned char *dhm_G[] = { dhm_G_2048, dhm_G_3072 };
|
2023-01-11 14:50:10 +01:00
|
|
|
const size_t dhm_G_size[] = { sizeof(dhm_G_2048),
|
|
|
|
sizeof(dhm_G_3072) };
|
2013-09-17 14:57:55 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_dhm_context dhm;
|
2013-09-17 14:57:55 +02:00
|
|
|
size_t olen;
|
2021-05-27 22:17:07 +02:00
|
|
|
size_t n;
|
2023-10-09 12:40:17 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 0; (size_t) i < sizeof(dhm_sizes) / sizeof(dhm_sizes[0]); i++) {
|
|
|
|
mbedtls_dhm_init(&dhm);
|
|
|
|
|
2023-11-16 17:43:58 +01:00
|
|
|
if (mbedtls_mpi_read_binary(&dhm.MBEDTLS_PRIVATE(P), dhm_P[i],
|
2023-01-11 14:50:10 +01:00
|
|
|
dhm_P_size[i]) != 0 ||
|
2023-11-16 17:43:58 +01:00
|
|
|
mbedtls_mpi_read_binary(&dhm.MBEDTLS_PRIVATE(G), dhm_G[i],
|
2023-01-11 14:50:10 +01:00
|
|
|
dhm_G_size[i]) != 0) {
|
|
|
|
mbedtls_exit(1);
|
2014-04-17 16:00:59 +02:00
|
|
|
}
|
|
|
|
|
2023-11-16 17:43:58 +01:00
|
|
|
n = mbedtls_mpi_size(&dhm.MBEDTLS_PRIVATE(P));
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_dhm_make_public(&dhm, (int) n, buf, n, myrand, NULL);
|
2023-11-16 17:43:58 +01:00
|
|
|
if (mbedtls_mpi_copy(&dhm.MBEDTLS_PRIVATE(GY), &dhm.MBEDTLS_PRIVATE(GX)) != 0) {
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_exit(1);
|
|
|
|
}
|
2013-09-17 14:57:55 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_snprintf(title, sizeof(title), "DHE-%d", dhm_sizes[i]);
|
|
|
|
TIME_PUBLIC(title, "handshake",
|
|
|
|
ret |= mbedtls_dhm_make_public(&dhm, (int) n, buf, n,
|
|
|
|
myrand, NULL);
|
|
|
|
ret |=
|
|
|
|
mbedtls_dhm_calc_secret(&dhm, buf, sizeof(buf), &olen, myrand, NULL));
|
2013-09-17 14:57:55 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_snprintf(title, sizeof(title), "DH-%d", dhm_sizes[i]);
|
|
|
|
TIME_PUBLIC(title, "handshake",
|
|
|
|
ret |=
|
|
|
|
mbedtls_dhm_calc_secret(&dhm, buf, sizeof(buf), &olen, myrand, NULL));
|
2013-09-17 14:57:55 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_dhm_free(&dhm);
|
2013-09-17 14:57:55 +02:00
|
|
|
}
|
2012-11-08 11:31:48 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (todo.ecdsa) {
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecdsa_context ecdsa;
|
|
|
|
const mbedtls_ecp_curve_info *curve_info;
|
2013-09-17 16:04:08 +02:00
|
|
|
size_t sig_len;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(buf, 0x2A, sizeof(buf));
|
2013-09-17 16:04:08 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (curve_info = curve_list;
|
2015-04-08 12:49:31 +02:00
|
|
|
curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
|
2023-01-11 14:50:10 +01:00
|
|
|
curve_info++) {
|
|
|
|
if (!mbedtls_ecdsa_can_do(curve_info->grp_id)) {
|
2019-01-07 14:47:30 +01:00
|
|
|
continue;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2019-01-07 14:47:30 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecdsa_init(&ecdsa);
|
2013-09-17 16:04:08 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_ecdsa_genkey(&ecdsa, curve_info->grp_id, myrand, NULL) != 0) {
|
|
|
|
mbedtls_exit(1);
|
|
|
|
}
|
2013-09-17 17:23:07 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_snprintf(title, sizeof(title), "ECDSA-%s",
|
|
|
|
curve_info->name);
|
|
|
|
TIME_PUBLIC(title,
|
|
|
|
"sign",
|
|
|
|
ret =
|
|
|
|
mbedtls_ecdsa_write_signature(&ecdsa, MBEDTLS_MD_SHA256, buf,
|
|
|
|
curve_info->bit_size,
|
|
|
|
tmp, sizeof(tmp), &sig_len, myrand,
|
|
|
|
NULL));
|
|
|
|
|
|
|
|
mbedtls_ecdsa_free(&ecdsa);
|
2014-12-19 12:10:37 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (curve_info = curve_list;
|
2015-04-08 12:49:31 +02:00
|
|
|
curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
|
2023-01-11 14:50:10 +01:00
|
|
|
curve_info++) {
|
|
|
|
if (!mbedtls_ecdsa_can_do(curve_info->grp_id)) {
|
2019-01-07 14:47:30 +01:00
|
|
|
continue;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2019-01-07 14:47:30 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecdsa_init(&ecdsa);
|
2014-12-19 12:10:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_ecdsa_genkey(&ecdsa, curve_info->grp_id, myrand, NULL) != 0 ||
|
|
|
|
mbedtls_ecdsa_write_signature(&ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
|
|
|
|
tmp, sizeof(tmp), &sig_len, myrand, NULL) != 0) {
|
|
|
|
mbedtls_exit(1);
|
2014-12-19 12:10:37 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_snprintf(title, sizeof(title), "ECDSA-%s",
|
|
|
|
curve_info->name);
|
|
|
|
TIME_PUBLIC(title, "verify",
|
|
|
|
ret = mbedtls_ecdsa_read_signature(&ecdsa, buf, curve_info->bit_size,
|
|
|
|
tmp, sig_len));
|
2013-09-17 16:04:08 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecdsa_free(&ecdsa);
|
2013-09-17 16:04:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-08-15 11:19:16 +02:00
|
|
|
#if defined(MBEDTLS_ECDH_C) && defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (todo.ecdh) {
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecdh_context ecdh;
|
|
|
|
mbedtls_mpi z;
|
2015-11-10 14:10:01 +01:00
|
|
|
const mbedtls_ecp_curve_info montgomery_curve_list[] = {
|
|
|
|
#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
|
|
|
|
{ MBEDTLS_ECP_DP_CURVE25519, 0, 0, "Curve25519" },
|
2015-02-05 10:54:48 +01:00
|
|
|
#endif
|
2015-11-10 14:10:01 +01:00
|
|
|
#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
|
|
|
|
{ MBEDTLS_ECP_DP_CURVE448, 0, 0, "Curve448" },
|
|
|
|
#endif
|
|
|
|
{ MBEDTLS_ECP_DP_NONE, 0, 0, 0 }
|
|
|
|
};
|
2015-04-08 12:49:31 +02:00
|
|
|
const mbedtls_ecp_curve_info *curve_info;
|
2013-09-17 16:04:08 +02:00
|
|
|
size_t olen;
|
2020-07-24 02:06:46 +02:00
|
|
|
const mbedtls_ecp_curve_info *selected_montgomery_curve_list =
|
|
|
|
montgomery_curve_list;
|
2013-09-17 16:04:08 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (curve_list == (const mbedtls_ecp_curve_info *) &single_curve) {
|
2020-07-24 02:06:46 +02:00
|
|
|
mbedtls_ecp_group grp;
|
2023-10-09 12:40:17 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecp_group_init(&grp);
|
|
|
|
if (mbedtls_ecp_group_load(&grp, curve_list->grp_id) != 0) {
|
|
|
|
mbedtls_exit(1);
|
|
|
|
}
|
|
|
|
if (mbedtls_ecp_get_type(&grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
|
2020-07-24 02:06:46 +02:00
|
|
|
selected_montgomery_curve_list = single_curve;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else { /* empty list */
|
2020-07-24 02:06:46 +02:00
|
|
|
selected_montgomery_curve_list = single_curve + 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
mbedtls_ecp_group_free(&grp);
|
2020-07-24 02:06:46 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (curve_info = curve_list;
|
2015-04-08 12:49:31 +02:00
|
|
|
curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
|
2023-01-11 14:50:10 +01:00
|
|
|
curve_info++) {
|
|
|
|
if (!mbedtls_ecdh_can_do(curve_info->grp_id)) {
|
2019-02-11 18:41:27 +01:00
|
|
|
continue;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2019-02-11 18:41:27 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecdh_init(&ecdh);
|
|
|
|
|
|
|
|
CHECK_AND_CONTINUE(mbedtls_ecp_group_load(&ecdh.grp, curve_info->grp_id));
|
|
|
|
CHECK_AND_CONTINUE(mbedtls_ecdh_make_public(&ecdh, &olen, buf, sizeof(buf),
|
|
|
|
myrand, NULL));
|
|
|
|
CHECK_AND_CONTINUE(mbedtls_ecp_copy(&ecdh.Qp, &ecdh.Q));
|
|
|
|
|
|
|
|
mbedtls_snprintf(title, sizeof(title), "ECDHE-%s",
|
|
|
|
curve_info->name);
|
|
|
|
TIME_PUBLIC(title, "handshake",
|
|
|
|
CHECK_AND_CONTINUE(mbedtls_ecdh_make_public(&ecdh, &olen, buf, sizeof(buf),
|
|
|
|
myrand, NULL));
|
|
|
|
CHECK_AND_CONTINUE(mbedtls_ecdh_calc_secret(&ecdh, &olen, buf, sizeof(buf),
|
|
|
|
myrand, NULL)));
|
|
|
|
mbedtls_ecdh_free(&ecdh);
|
2014-12-19 12:10:37 +01:00
|
|
|
}
|
|
|
|
|
2015-11-10 14:10:01 +01:00
|
|
|
/* Montgomery curves need to be handled separately */
|
2023-01-11 14:50:10 +01:00
|
|
|
for (curve_info = selected_montgomery_curve_list;
|
|
|
|
curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
|
|
|
|
curve_info++) {
|
|
|
|
mbedtls_ecdh_init(&ecdh);
|
|
|
|
mbedtls_mpi_init(&z);
|
|
|
|
|
|
|
|
CHECK_AND_CONTINUE(mbedtls_ecp_group_load(&ecdh.grp, curve_info->grp_id));
|
|
|
|
CHECK_AND_CONTINUE(mbedtls_ecdh_gen_public(&ecdh.grp, &ecdh.d, &ecdh.Qp, myrand, NULL));
|
|
|
|
|
|
|
|
mbedtls_snprintf(title, sizeof(title), "ECDHE-%s",
|
|
|
|
curve_info->name);
|
|
|
|
TIME_PUBLIC(title, "handshake",
|
|
|
|
CHECK_AND_CONTINUE(mbedtls_ecdh_gen_public(&ecdh.grp, &ecdh.d, &ecdh.Q,
|
|
|
|
myrand, NULL));
|
|
|
|
CHECK_AND_CONTINUE(mbedtls_ecdh_compute_shared(&ecdh.grp, &z, &ecdh.Qp,
|
|
|
|
&ecdh.d,
|
|
|
|
myrand, NULL)));
|
|
|
|
|
|
|
|
mbedtls_ecdh_free(&ecdh);
|
|
|
|
mbedtls_mpi_free(&z);
|
2015-11-10 14:10:01 +01:00
|
|
|
}
|
2015-02-05 10:54:48 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (curve_info = curve_list;
|
2015-04-08 12:49:31 +02:00
|
|
|
curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
|
2023-01-11 14:50:10 +01:00
|
|
|
curve_info++) {
|
|
|
|
if (!mbedtls_ecdh_can_do(curve_info->grp_id)) {
|
2019-02-11 18:41:27 +01:00
|
|
|
continue;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2019-02-11 18:41:27 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecdh_init(&ecdh);
|
|
|
|
|
|
|
|
CHECK_AND_CONTINUE(mbedtls_ecp_group_load(&ecdh.grp, curve_info->grp_id));
|
|
|
|
CHECK_AND_CONTINUE(mbedtls_ecdh_make_public(&ecdh, &olen, buf, sizeof(buf),
|
|
|
|
myrand, NULL));
|
|
|
|
CHECK_AND_CONTINUE(mbedtls_ecp_copy(&ecdh.Qp, &ecdh.Q));
|
|
|
|
CHECK_AND_CONTINUE(mbedtls_ecdh_make_public(&ecdh, &olen, buf, sizeof(buf),
|
|
|
|
myrand, NULL));
|
|
|
|
|
|
|
|
mbedtls_snprintf(title, sizeof(title), "ECDH-%s",
|
|
|
|
curve_info->name);
|
|
|
|
TIME_PUBLIC(title, "handshake",
|
|
|
|
CHECK_AND_CONTINUE(mbedtls_ecdh_calc_secret(&ecdh, &olen, buf, sizeof(buf),
|
|
|
|
myrand, NULL)));
|
|
|
|
mbedtls_ecdh_free(&ecdh);
|
2013-09-17 16:04:08 +02:00
|
|
|
}
|
2015-02-05 10:54:48 +01:00
|
|
|
|
2015-11-10 14:10:01 +01:00
|
|
|
/* Montgomery curves need to be handled separately */
|
2023-01-11 14:50:10 +01:00
|
|
|
for (curve_info = selected_montgomery_curve_list;
|
|
|
|
curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
|
|
|
|
curve_info++) {
|
|
|
|
mbedtls_ecdh_init(&ecdh);
|
|
|
|
mbedtls_mpi_init(&z);
|
|
|
|
|
|
|
|
CHECK_AND_CONTINUE(mbedtls_ecp_group_load(&ecdh.grp, curve_info->grp_id));
|
|
|
|
CHECK_AND_CONTINUE(mbedtls_ecdh_gen_public(&ecdh.grp, &ecdh.d, &ecdh.Qp,
|
|
|
|
myrand, NULL));
|
|
|
|
CHECK_AND_CONTINUE(mbedtls_ecdh_gen_public(&ecdh.grp, &ecdh.d, &ecdh.Q, myrand, NULL));
|
|
|
|
|
|
|
|
mbedtls_snprintf(title, sizeof(title), "ECDH-%s",
|
|
|
|
curve_info->name);
|
|
|
|
TIME_PUBLIC(title, "handshake",
|
|
|
|
CHECK_AND_CONTINUE(mbedtls_ecdh_compute_shared(&ecdh.grp, &z, &ecdh.Qp,
|
|
|
|
&ecdh.d,
|
|
|
|
myrand, NULL)));
|
|
|
|
|
|
|
|
mbedtls_ecdh_free(&ecdh);
|
|
|
|
mbedtls_mpi_free(&z);
|
2015-11-10 14:10:01 +01:00
|
|
|
}
|
2013-09-17 16:04:08 +02:00
|
|
|
}
|
|
|
|
#endif
|
2014-12-19 12:10:37 +01:00
|
|
|
|
2018-12-14 12:03:02 +01:00
|
|
|
#if defined(MBEDTLS_ECDH_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (todo.ecdh) {
|
2018-10-25 13:47:18 +02:00
|
|
|
mbedtls_ecdh_context ecdh_srv, ecdh_cli;
|
|
|
|
unsigned char buf_srv[BUFSIZE], buf_cli[BUFSIZE];
|
|
|
|
const mbedtls_ecp_curve_info *curve_info;
|
|
|
|
size_t olen;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (curve_info = curve_list;
|
|
|
|
curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
|
|
|
|
curve_info++) {
|
|
|
|
if (!mbedtls_ecdh_can_do(curve_info->grp_id)) {
|
2019-02-11 18:41:27 +01:00
|
|
|
continue;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2019-02-11 18:41:27 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecdh_init(&ecdh_srv);
|
|
|
|
mbedtls_ecdh_init(&ecdh_cli);
|
2018-10-25 13:47:18 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_snprintf(title, sizeof(title), "ECDHE-%s", curve_info->name);
|
|
|
|
TIME_PUBLIC(title,
|
|
|
|
"full handshake",
|
|
|
|
const unsigned char *p_srv = buf_srv;
|
2019-02-26 13:26:04 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
CHECK_AND_CONTINUE(mbedtls_ecdh_setup(&ecdh_srv, curve_info->grp_id));
|
|
|
|
CHECK_AND_CONTINUE(mbedtls_ecdh_make_params(&ecdh_srv, &olen, buf_srv,
|
|
|
|
sizeof(buf_srv), myrand, NULL));
|
2018-10-25 13:47:18 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
CHECK_AND_CONTINUE(mbedtls_ecdh_read_params(&ecdh_cli, &p_srv,
|
|
|
|
p_srv + olen));
|
|
|
|
CHECK_AND_CONTINUE(mbedtls_ecdh_make_public(&ecdh_cli, &olen, buf_cli,
|
|
|
|
sizeof(buf_cli), myrand, NULL));
|
2018-10-25 13:47:18 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
CHECK_AND_CONTINUE(mbedtls_ecdh_read_public(&ecdh_srv, buf_cli, olen));
|
|
|
|
CHECK_AND_CONTINUE(mbedtls_ecdh_calc_secret(&ecdh_srv, &olen, buf_srv,
|
|
|
|
sizeof(buf_srv), myrand, NULL));
|
2018-10-25 13:47:18 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
CHECK_AND_CONTINUE(mbedtls_ecdh_calc_secret(&ecdh_cli, &olen, buf_cli,
|
|
|
|
sizeof(buf_cli), myrand, NULL));
|
|
|
|
mbedtls_ecdh_free(&ecdh_cli);
|
2019-02-26 13:26:04 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecdh_free(&ecdh_srv);
|
|
|
|
);
|
2018-10-25 13:47:18 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_printf("\n");
|
2009-10-25 13:36:53 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
|
|
|
|
mbedtls_memory_buffer_alloc_free();
|
2014-12-18 17:35:52 +01:00
|
|
|
#endif
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_exit(0);
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
2013-09-17 14:57:55 +02:00
|
|
|
|
2022-03-02 18:01:10 +01:00
|
|
|
#endif /* MBEDTLS_HAVE_TIME */
|