1 /*
2 * Benchmark demonstration program
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20 #define MBEDTLS_ALLOW_PRIVATE_ACCESS
21
22 #include "mbedtls/build_info.h"
23
24 #include "mbedtls/platform.h"
25
26 #if !defined(MBEDTLS_HAVE_TIME)
main(void)27 int main(void)
28 {
29 mbedtls_printf("MBEDTLS_HAVE_TIME not defined.\n");
30 mbedtls_exit(0);
31 }
32 #else
33
34 #include <string.h>
35 #include <stdlib.h>
36
37 #include "mbedtls/md5.h"
38 #include "mbedtls/ripemd160.h"
39 #include "mbedtls/sha1.h"
40 #include "mbedtls/sha256.h"
41 #include "mbedtls/sha512.h"
42
43 #include "mbedtls/des.h"
44 #include "mbedtls/aes.h"
45 #include "mbedtls/aria.h"
46 #include "mbedtls/camellia.h"
47 #include "mbedtls/chacha20.h"
48 #include "mbedtls/gcm.h"
49 #include "mbedtls/ccm.h"
50 #include "mbedtls/chachapoly.h"
51 #include "mbedtls/cmac.h"
52 #include "mbedtls/poly1305.h"
53
54 #include "mbedtls/ctr_drbg.h"
55 #include "mbedtls/hmac_drbg.h"
56
57 #include "mbedtls/rsa.h"
58 #include "mbedtls/dhm.h"
59 #include "mbedtls/ecdsa.h"
60 #include "mbedtls/ecdh.h"
61
62 #include "mbedtls/error.h"
63
64 /* *INDENT-OFF* */
65 #ifndef asm
66 #define asm __asm
67 #endif
68 /* *INDENT-ON* */
69
70 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
71
72 #include <windows.h>
73 #include <process.h>
74
75 struct _hr_time {
76 LARGE_INTEGER start;
77 };
78
79 #else
80
81 #include <unistd.h>
82 #include <sys/types.h>
83 #include <sys/time.h>
84 #include <signal.h>
85 #include <time.h>
86
87 struct _hr_time {
88 struct timeval start;
89 };
90
91 #endif /* _WIN32 && !EFIX64 && !EFI32 */
92
93 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
94 #include "mbedtls/memory_buffer_alloc.h"
95 #endif
96
97 static void mbedtls_set_alarm(int seconds);
98
99 /*
100 * For heap usage estimates, we need an estimate of the overhead per allocated
101 * block. ptmalloc2/3 (used in gnu libc for instance) uses 2 size_t per block,
102 * so use that as our baseline.
103 */
104 #define MEM_BLOCK_OVERHEAD (2 * sizeof(size_t))
105
106 /*
107 * Size to use for the alloc buffer if MEMORY_BUFFER_ALLOC_C is defined.
108 */
109 #define HEAP_SIZE (1u << 16) /* 64k */
110
111 #define BUFSIZE 1024
112 #define HEADER_FORMAT " %-24s : "
113 #define TITLE_LEN 25
114
115 #define OPTIONS \
116 "md5, ripemd160, sha1, sha256, sha512,\n" \
117 "des3, des, camellia, chacha20,\n" \
118 "aes_cbc, aes_gcm, aes_ccm, aes_xts, chachapoly,\n" \
119 "aes_cmac, des3_cmac, poly1305\n" \
120 "ctr_drbg, hmac_drbg\n" \
121 "rsa, dhm, ecdsa, ecdh.\n"
122
123 #if defined(MBEDTLS_ERROR_C)
124 #define PRINT_ERROR \
125 mbedtls_strerror(ret, (char *) tmp, sizeof(tmp)); \
126 mbedtls_printf("FAILED: %s\n", tmp);
127 #else
128 #define PRINT_ERROR \
129 mbedtls_printf("FAILED: -0x%04x\n", (unsigned int) -ret);
130 #endif
131
132 #define TIME_AND_TSC(TITLE, CODE) \
133 do { \
134 unsigned long ii, jj, tsc; \
135 int ret = 0; \
136 \
137 mbedtls_printf(HEADER_FORMAT, TITLE); \
138 fflush(stdout); \
139 \
140 mbedtls_set_alarm(1); \
141 for (ii = 1; ret == 0 && !mbedtls_timing_alarmed; ii++) \
142 { \
143 ret = CODE; \
144 } \
145 \
146 tsc = mbedtls_timing_hardclock(); \
147 for (jj = 0; ret == 0 && jj < 1024; jj++) \
148 { \
149 ret = CODE; \
150 } \
151 \
152 if (ret != 0) \
153 { \
154 PRINT_ERROR; \
155 } \
156 else \
157 { \
158 mbedtls_printf("%9lu KiB/s, %9lu cycles/byte\n", \
159 ii * BUFSIZE / 1024, \
160 (mbedtls_timing_hardclock() - tsc) \
161 / (jj * BUFSIZE)); \
162 } \
163 } while (0)
164
165 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_MEMORY_DEBUG)
166
167 /* How much space to reserve for the title when printing heap usage results.
168 * Updated manually as the output of the following command:
169 *
170 * sed -n 's/.*[T]IME_PUBLIC.*"\(.*\)",/\1/p' programs/test/benchmark.c |
171 * awk '{print length+3}' | sort -rn | head -n1
172 *
173 * This computes the maximum length of a title +3, because we appends "/s" and
174 * want at least one space. (If the value is too small, the only consequence
175 * is poor alignment.) */
176 #define TITLE_SPACE 17
177
178 #define MEMORY_MEASURE_INIT \
179 size_t max_used, max_blocks, max_bytes; \
180 size_t prv_used, prv_blocks; \
181 size_t alloc_cnt, free_cnt, prv_alloc, prv_free; \
182 mbedtls_memory_buffer_alloc_cur_get(&prv_used, &prv_blocks); \
183 mbedtls_memory_buffer_alloc_max_reset();
184
185 #define MEMORY_MEASURE_RESET \
186 mbedtls_memory_buffer_alloc_count_get(&prv_alloc, &prv_free);
187
188 #define MEMORY_MEASURE_PRINT(title_len) \
189 mbedtls_memory_buffer_alloc_max_get(&max_used, &max_blocks); \
190 mbedtls_memory_buffer_alloc_count_get(&alloc_cnt, &free_cnt); \
191 ii = TITLE_SPACE > (title_len) ? TITLE_SPACE - (title_len) : 1; \
192 while (ii--) mbedtls_printf(" "); \
193 max_used -= prv_used; \
194 max_blocks -= prv_blocks; \
195 max_bytes = max_used + MEM_BLOCK_OVERHEAD * max_blocks; \
196 mbedtls_printf("%6u heap bytes, %6u allocs", \
197 (unsigned) max_bytes, \
198 (unsigned) (alloc_cnt - prv_alloc));
199
200 #else
201 #define MEMORY_MEASURE_INIT
202 #define MEMORY_MEASURE_RESET
203 #define MEMORY_MEASURE_PRINT(title_len)
204 #endif
205
206 #define TIME_PUBLIC(TITLE, TYPE, CODE) \
207 do { \
208 unsigned long ii; \
209 int ret; \
210 MEMORY_MEASURE_INIT; \
211 \
212 mbedtls_printf(HEADER_FORMAT, TITLE); \
213 fflush(stdout); \
214 mbedtls_set_alarm(3); \
215 \
216 ret = 0; \
217 for (ii = 1; !mbedtls_timing_alarmed && !ret; ii++) \
218 { \
219 MEMORY_MEASURE_RESET; \
220 CODE; \
221 } \
222 \
223 if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) \
224 { \
225 mbedtls_printf("Feature Not Supported. Skipping.\n"); \
226 ret = 0; \
227 } \
228 else if (ret != 0) \
229 { \
230 PRINT_ERROR; \
231 } \
232 else \
233 { \
234 mbedtls_printf("%6lu " TYPE "/s", ii / 3); \
235 MEMORY_MEASURE_PRINT(sizeof(TYPE) + 1); \
236 mbedtls_printf("\n"); \
237 } \
238 } while (0)
239
240 #if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
241 (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
242
243 #define HAVE_HARDCLOCK
244
mbedtls_timing_hardclock(void)245 static unsigned long mbedtls_timing_hardclock(void)
246 {
247 unsigned long tsc;
248 __asm rdtsc
249 __asm mov[tsc], eax
250 return tsc;
251 }
252 #endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
253 ( _MSC_VER && _M_IX86 ) || __WATCOMC__ */
254
255 /* some versions of mingw-64 have 32-bit longs even on x84_64 */
256 #if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
257 defined(__GNUC__) && (defined(__i386__) || ( \
258 (defined(__amd64__) || defined(__x86_64__)) && __SIZEOF_LONG__ == 4))
259
260 #define HAVE_HARDCLOCK
261
mbedtls_timing_hardclock(void)262 static unsigned long mbedtls_timing_hardclock(void)
263 {
264 unsigned long lo, hi;
265 asm volatile ("rdtsc" : "=a" (lo), "=d" (hi));
266 return lo;
267 }
268 #endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
269 __GNUC__ && __i386__ */
270
271 #if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
272 defined(__GNUC__) && (defined(__amd64__) || defined(__x86_64__))
273
274 #define HAVE_HARDCLOCK
275
mbedtls_timing_hardclock(void)276 static unsigned long mbedtls_timing_hardclock(void)
277 {
278 unsigned long lo, hi;
279 asm volatile ("rdtsc" : "=a" (lo), "=d" (hi));
280 return lo | (hi << 32);
281 }
282 #endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
283 __GNUC__ && ( __amd64__ || __x86_64__ ) */
284
285 #if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
286 defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
287
288 #define HAVE_HARDCLOCK
289
mbedtls_timing_hardclock(void)290 static unsigned long mbedtls_timing_hardclock(void)
291 {
292 unsigned long tbl, tbu0, tbu1;
293
294 do {
295 asm volatile ("mftbu %0" : "=r" (tbu0));
296 asm volatile ("mftb %0" : "=r" (tbl));
297 asm volatile ("mftbu %0" : "=r" (tbu1));
298 } while (tbu0 != tbu1);
299
300 return tbl;
301 }
302 #endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
303 __GNUC__ && ( __powerpc__ || __ppc__ ) */
304
305 #if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
306 defined(__GNUC__) && defined(__sparc64__)
307
308 #if defined(__OpenBSD__)
309 #warning OpenBSD does not allow access to tick register using software version instead
310 #else
311 #define HAVE_HARDCLOCK
312
mbedtls_timing_hardclock(void)313 static unsigned long mbedtls_timing_hardclock(void)
314 {
315 unsigned long tick;
316 asm volatile ("rdpr %%tick, %0;" : "=&r" (tick));
317 return tick;
318 }
319 #endif /* __OpenBSD__ */
320 #endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
321 __GNUC__ && __sparc64__ */
322
323 #if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
324 defined(__GNUC__) && defined(__sparc__) && !defined(__sparc64__)
325
326 #define HAVE_HARDCLOCK
327
mbedtls_timing_hardclock(void)328 static unsigned long mbedtls_timing_hardclock(void)
329 {
330 unsigned long tick;
331 asm volatile (".byte 0x83, 0x41, 0x00, 0x00");
332 asm volatile ("mov %%g1, %0" : "=r" (tick));
333 return tick;
334 }
335 #endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
336 __GNUC__ && __sparc__ && !__sparc64__ */
337
338 #if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
339 defined(__GNUC__) && defined(__alpha__)
340
341 #define HAVE_HARDCLOCK
342
mbedtls_timing_hardclock(void)343 static unsigned long mbedtls_timing_hardclock(void)
344 {
345 unsigned long cc;
346 asm volatile ("rpcc %0" : "=r" (cc));
347 return cc & 0xFFFFFFFF;
348 }
349 #endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
350 __GNUC__ && __alpha__ */
351
352 #if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
353 defined(__GNUC__) && defined(__ia64__)
354
355 #define HAVE_HARDCLOCK
356
mbedtls_timing_hardclock(void)357 static unsigned long mbedtls_timing_hardclock(void)
358 {
359 unsigned long itc;
360 asm volatile ("mov %0 = ar.itc" : "=r" (itc));
361 return itc;
362 }
363 #endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
364 __GNUC__ && __ia64__ */
365
366 #if !defined(HAVE_HARDCLOCK) && defined(_MSC_VER) && \
367 !defined(EFIX64) && !defined(EFI32)
368
369 #define HAVE_HARDCLOCK
370
mbedtls_timing_hardclock(void)371 static unsigned long mbedtls_timing_hardclock(void)
372 {
373 LARGE_INTEGER offset;
374
375 QueryPerformanceCounter(&offset);
376
377 return (unsigned long) (offset.QuadPart);
378 }
379 #endif /* !HAVE_HARDCLOCK && _MSC_VER && !EFIX64 && !EFI32 */
380
381 #if !defined(HAVE_HARDCLOCK)
382
383 #define HAVE_HARDCLOCK
384
385 static int hardclock_init = 0;
386 static struct timeval tv_init;
387
mbedtls_timing_hardclock(void)388 static unsigned long mbedtls_timing_hardclock(void)
389 {
390 struct timeval tv_cur;
391
392 if (hardclock_init == 0) {
393 gettimeofday(&tv_init, NULL);
394 hardclock_init = 1;
395 }
396
397 gettimeofday(&tv_cur, NULL);
398 return (tv_cur.tv_sec - tv_init.tv_sec) * 1000000U
399 + (tv_cur.tv_usec - tv_init.tv_usec);
400 }
401 #endif /* !HAVE_HARDCLOCK */
402
403 volatile int mbedtls_timing_alarmed = 0;
404
405 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
406
407 /* It's OK to use a global because alarm() is supposed to be global anyway */
408 static DWORD alarmMs;
409
TimerProc(void * TimerContext)410 static void TimerProc(void *TimerContext)
411 {
412 (void) TimerContext;
413 Sleep(alarmMs);
414 mbedtls_timing_alarmed = 1;
415 /* _endthread will be called implicitly on return
416 * That ensures execution of thread function's epilogue */
417 }
418
mbedtls_set_alarm(int seconds)419 static void mbedtls_set_alarm(int seconds)
420 {
421 if (seconds == 0) {
422 /* No need to create a thread for this simple case.
423 * Also, this shorcut is more reliable at least on MinGW32 */
424 mbedtls_timing_alarmed = 1;
425 return;
426 }
427
428 mbedtls_timing_alarmed = 0;
429 alarmMs = seconds * 1000;
430 (void) _beginthread(TimerProc, 0, NULL);
431 }
432
433 #else /* _WIN32 && !EFIX64 && !EFI32 */
434
sighandler(int signum)435 static void sighandler(int signum)
436 {
437 mbedtls_timing_alarmed = 1;
438 signal(signum, sighandler);
439 }
440
mbedtls_set_alarm(int seconds)441 static void mbedtls_set_alarm(int seconds)
442 {
443 mbedtls_timing_alarmed = 0;
444 signal(SIGALRM, sighandler);
445 alarm(seconds);
446 if (seconds == 0) {
447 /* alarm(0) cancelled any previous pending alarm, but the
448 handler won't fire, so raise the flag straight away. */
449 mbedtls_timing_alarmed = 1;
450 }
451 }
452
453 #endif /* _WIN32 && !EFIX64 && !EFI32 */
454
myrand(void * rng_state,unsigned char * output,size_t len)455 static int myrand(void *rng_state, unsigned char *output, size_t len)
456 {
457 size_t use_len;
458 int rnd;
459
460 if (rng_state != NULL) {
461 rng_state = NULL;
462 }
463
464 while (len > 0) {
465 use_len = len;
466 if (use_len > sizeof(int)) {
467 use_len = sizeof(int);
468 }
469
470 rnd = rand();
471 memcpy(output, &rnd, use_len);
472 output += use_len;
473 len -= use_len;
474 }
475
476 return 0;
477 }
478
479 #define CHECK_AND_CONTINUE(R) \
480 { \
481 int CHECK_AND_CONTINUE_ret = (R); \
482 if (CHECK_AND_CONTINUE_ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) { \
483 mbedtls_printf("Feature not supported. Skipping.\n"); \
484 continue; \
485 } \
486 else if (CHECK_AND_CONTINUE_ret != 0) { \
487 mbedtls_exit(1); \
488 } \
489 }
490
491 #if defined(MBEDTLS_ECP_C)
set_ecp_curve(const char * string,mbedtls_ecp_curve_info * curve)492 static int set_ecp_curve(const char *string, mbedtls_ecp_curve_info *curve)
493 {
494 const mbedtls_ecp_curve_info *found =
495 mbedtls_ecp_curve_info_from_name(string);
496 if (found != NULL) {
497 *curve = *found;
498 return 1;
499 } else {
500 return 0;
501 }
502 }
503 #endif
504
505 unsigned char buf[BUFSIZE];
506
507 typedef struct {
508 char md5, ripemd160, sha1, sha256, sha512,
509 des3, des,
510 aes_cbc, aes_gcm, aes_ccm, aes_xts, chachapoly,
511 aes_cmac, des3_cmac,
512 aria, camellia, chacha20,
513 poly1305,
514 ctr_drbg, hmac_drbg,
515 rsa, dhm, ecdsa, ecdh;
516 } todo_list;
517
518
main(int argc,char * argv[])519 int main(int argc, char *argv[])
520 {
521 int i;
522 unsigned char tmp[200];
523 char title[TITLE_LEN];
524 todo_list todo;
525 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
526 unsigned char alloc_buf[HEAP_SIZE] = { 0 };
527 #endif
528 #if defined(MBEDTLS_ECP_C)
529 mbedtls_ecp_curve_info single_curve[2] = {
530 { MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
531 { MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
532 };
533 const mbedtls_ecp_curve_info *curve_list = mbedtls_ecp_curve_list();
534 #endif
535
536 #if defined(MBEDTLS_ECP_C)
537 (void) curve_list; /* Unused in some configurations where no benchmark uses ECC */
538 #endif
539
540 if (argc <= 1) {
541 memset(&todo, 1, sizeof(todo));
542 } else {
543 memset(&todo, 0, sizeof(todo));
544
545 for (i = 1; i < argc; i++) {
546 if (strcmp(argv[i], "md5") == 0) {
547 todo.md5 = 1;
548 } else if (strcmp(argv[i], "ripemd160") == 0) {
549 todo.ripemd160 = 1;
550 } else if (strcmp(argv[i], "sha1") == 0) {
551 todo.sha1 = 1;
552 } else if (strcmp(argv[i], "sha256") == 0) {
553 todo.sha256 = 1;
554 } else if (strcmp(argv[i], "sha512") == 0) {
555 todo.sha512 = 1;
556 } else if (strcmp(argv[i], "des3") == 0) {
557 todo.des3 = 1;
558 } else if (strcmp(argv[i], "des") == 0) {
559 todo.des = 1;
560 } else if (strcmp(argv[i], "aes_cbc") == 0) {
561 todo.aes_cbc = 1;
562 } else if (strcmp(argv[i], "aes_xts") == 0) {
563 todo.aes_xts = 1;
564 } else if (strcmp(argv[i], "aes_gcm") == 0) {
565 todo.aes_gcm = 1;
566 } else if (strcmp(argv[i], "aes_ccm") == 0) {
567 todo.aes_ccm = 1;
568 } else if (strcmp(argv[i], "chachapoly") == 0) {
569 todo.chachapoly = 1;
570 } else if (strcmp(argv[i], "aes_cmac") == 0) {
571 todo.aes_cmac = 1;
572 } else if (strcmp(argv[i], "des3_cmac") == 0) {
573 todo.des3_cmac = 1;
574 } else if (strcmp(argv[i], "aria") == 0) {
575 todo.aria = 1;
576 } else if (strcmp(argv[i], "camellia") == 0) {
577 todo.camellia = 1;
578 } else if (strcmp(argv[i], "chacha20") == 0) {
579 todo.chacha20 = 1;
580 } else if (strcmp(argv[i], "poly1305") == 0) {
581 todo.poly1305 = 1;
582 } else if (strcmp(argv[i], "ctr_drbg") == 0) {
583 todo.ctr_drbg = 1;
584 } else if (strcmp(argv[i], "hmac_drbg") == 0) {
585 todo.hmac_drbg = 1;
586 } else if (strcmp(argv[i], "rsa") == 0) {
587 todo.rsa = 1;
588 } else if (strcmp(argv[i], "dhm") == 0) {
589 todo.dhm = 1;
590 } else if (strcmp(argv[i], "ecdsa") == 0) {
591 todo.ecdsa = 1;
592 } else if (strcmp(argv[i], "ecdh") == 0) {
593 todo.ecdh = 1;
594 }
595 #if defined(MBEDTLS_ECP_C)
596 else if (set_ecp_curve(argv[i], single_curve)) {
597 curve_list = single_curve;
598 }
599 #endif
600 else {
601 mbedtls_printf("Unrecognized option: %s\n", argv[i]);
602 mbedtls_printf("Available options: " OPTIONS);
603 }
604 }
605 }
606
607 mbedtls_printf("\n");
608
609 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
610 mbedtls_memory_buffer_alloc_init(alloc_buf, sizeof(alloc_buf));
611 #endif
612 memset(buf, 0xAA, sizeof(buf));
613 memset(tmp, 0xBB, sizeof(tmp));
614
615 /* Avoid "unused static function" warning in configurations without
616 * symmetric crypto. */
617 (void) mbedtls_timing_hardclock;
618
619 #if defined(MBEDTLS_MD5_C)
620 if (todo.md5) {
621 TIME_AND_TSC("MD5", mbedtls_md5(buf, BUFSIZE, tmp));
622 }
623 #endif
624
625 #if defined(MBEDTLS_RIPEMD160_C)
626 if (todo.ripemd160) {
627 TIME_AND_TSC("RIPEMD160", mbedtls_ripemd160(buf, BUFSIZE, tmp));
628 }
629 #endif
630
631 #if defined(MBEDTLS_SHA1_C)
632 if (todo.sha1) {
633 TIME_AND_TSC("SHA-1", mbedtls_sha1(buf, BUFSIZE, tmp));
634 }
635 #endif
636
637 #if defined(MBEDTLS_SHA256_C)
638 if (todo.sha256) {
639 TIME_AND_TSC("SHA-256", mbedtls_sha256(buf, BUFSIZE, tmp, 0));
640 }
641 #endif
642
643 #if defined(MBEDTLS_SHA512_C)
644 if (todo.sha512) {
645 TIME_AND_TSC("SHA-512", mbedtls_sha512(buf, BUFSIZE, tmp, 0));
646 }
647 #endif
648
649 #if defined(MBEDTLS_DES_C)
650 #if defined(MBEDTLS_CIPHER_MODE_CBC)
651 if (todo.des3) {
652 mbedtls_des3_context des3;
653 mbedtls_des3_init(&des3);
654 if (mbedtls_des3_set3key_enc(&des3, tmp) != 0) {
655 mbedtls_exit(1);
656 }
657 TIME_AND_TSC("3DES",
658 mbedtls_des3_crypt_cbc(&des3, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf));
659 mbedtls_des3_free(&des3);
660 }
661
662 if (todo.des) {
663 mbedtls_des_context des;
664 mbedtls_des_init(&des);
665 if (mbedtls_des_setkey_enc(&des, tmp) != 0) {
666 mbedtls_exit(1);
667 }
668 TIME_AND_TSC("DES",
669 mbedtls_des_crypt_cbc(&des, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf));
670 mbedtls_des_free(&des);
671 }
672
673 #endif /* MBEDTLS_CIPHER_MODE_CBC */
674 #if defined(MBEDTLS_CMAC_C)
675 if (todo.des3_cmac) {
676 unsigned char output[8];
677 const mbedtls_cipher_info_t *cipher_info;
678
679 memset(buf, 0, sizeof(buf));
680 memset(tmp, 0, sizeof(tmp));
681
682 cipher_info = mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_DES_EDE3_ECB);
683
684 TIME_AND_TSC("3DES-CMAC",
685 mbedtls_cipher_cmac(cipher_info, tmp, 192, buf,
686 BUFSIZE, output));
687 }
688 #endif /* MBEDTLS_CMAC_C */
689 #endif /* MBEDTLS_DES_C */
690
691 #if defined(MBEDTLS_AES_C)
692 #if defined(MBEDTLS_CIPHER_MODE_CBC)
693 if (todo.aes_cbc) {
694 int keysize;
695 mbedtls_aes_context aes;
696 mbedtls_aes_init(&aes);
697 for (keysize = 128; keysize <= 256; keysize += 64) {
698 mbedtls_snprintf(title, sizeof(title), "AES-CBC-%d", keysize);
699
700 memset(buf, 0, sizeof(buf));
701 memset(tmp, 0, sizeof(tmp));
702 CHECK_AND_CONTINUE(mbedtls_aes_setkey_enc(&aes, tmp, keysize));
703
704 TIME_AND_TSC(title,
705 mbedtls_aes_crypt_cbc(&aes, MBEDTLS_AES_ENCRYPT, BUFSIZE, tmp, buf, buf));
706 }
707 mbedtls_aes_free(&aes);
708 }
709 #endif
710 #if defined(MBEDTLS_CIPHER_MODE_XTS)
711 if (todo.aes_xts) {
712 int keysize;
713 mbedtls_aes_xts_context ctx;
714
715 mbedtls_aes_xts_init(&ctx);
716 for (keysize = 128; keysize <= 256; keysize += 128) {
717 mbedtls_snprintf(title, sizeof(title), "AES-XTS-%d", keysize);
718
719 memset(buf, 0, sizeof(buf));
720 memset(tmp, 0, sizeof(tmp));
721 CHECK_AND_CONTINUE(mbedtls_aes_xts_setkey_enc(&ctx, tmp, keysize * 2));
722
723 TIME_AND_TSC(title,
724 mbedtls_aes_crypt_xts(&ctx, MBEDTLS_AES_ENCRYPT, BUFSIZE,
725 tmp, buf, buf));
726
727 mbedtls_aes_xts_free(&ctx);
728 }
729 }
730 #endif
731 #if defined(MBEDTLS_GCM_C)
732 if (todo.aes_gcm) {
733 int keysize;
734 mbedtls_gcm_context gcm;
735
736 mbedtls_gcm_init(&gcm);
737 for (keysize = 128; keysize <= 256; keysize += 64) {
738 mbedtls_snprintf(title, sizeof(title), "AES-GCM-%d", keysize);
739
740 memset(buf, 0, sizeof(buf));
741 memset(tmp, 0, sizeof(tmp));
742 mbedtls_gcm_setkey(&gcm, MBEDTLS_CIPHER_ID_AES, tmp, keysize);
743
744 TIME_AND_TSC(title,
745 mbedtls_gcm_crypt_and_tag(&gcm, MBEDTLS_GCM_ENCRYPT, BUFSIZE, tmp,
746 12, NULL, 0, buf, buf, 16, tmp));
747
748 mbedtls_gcm_free(&gcm);
749 }
750 }
751 #endif
752 #if defined(MBEDTLS_CCM_C)
753 if (todo.aes_ccm) {
754 int keysize;
755 mbedtls_ccm_context ccm;
756
757 mbedtls_ccm_init(&ccm);
758 for (keysize = 128; keysize <= 256; keysize += 64) {
759 mbedtls_snprintf(title, sizeof(title), "AES-CCM-%d", keysize);
760
761 memset(buf, 0, sizeof(buf));
762 memset(tmp, 0, sizeof(tmp));
763 mbedtls_ccm_setkey(&ccm, MBEDTLS_CIPHER_ID_AES, tmp, keysize);
764
765 TIME_AND_TSC(title,
766 mbedtls_ccm_encrypt_and_tag(&ccm, BUFSIZE, tmp,
767 12, NULL, 0, buf, buf, tmp, 16));
768
769 mbedtls_ccm_free(&ccm);
770 }
771 }
772 #endif
773 #if defined(MBEDTLS_CHACHAPOLY_C)
774 if (todo.chachapoly) {
775 mbedtls_chachapoly_context chachapoly;
776
777 mbedtls_chachapoly_init(&chachapoly);
778 memset(buf, 0, sizeof(buf));
779 memset(tmp, 0, sizeof(tmp));
780
781 mbedtls_snprintf(title, sizeof(title), "ChaCha20-Poly1305");
782
783 mbedtls_chachapoly_setkey(&chachapoly, tmp);
784
785 TIME_AND_TSC(title,
786 mbedtls_chachapoly_encrypt_and_tag(&chachapoly,
787 BUFSIZE, tmp, NULL, 0, buf, buf, tmp));
788
789 mbedtls_chachapoly_free(&chachapoly);
790 }
791 #endif
792 #if defined(MBEDTLS_CMAC_C)
793 if (todo.aes_cmac) {
794 unsigned char output[16];
795 const mbedtls_cipher_info_t *cipher_info;
796 mbedtls_cipher_type_t cipher_type;
797 int keysize;
798
799 for (keysize = 128, cipher_type = MBEDTLS_CIPHER_AES_128_ECB;
800 keysize <= 256;
801 keysize += 64, cipher_type++) {
802 mbedtls_snprintf(title, sizeof(title), "AES-CMAC-%d", keysize);
803
804 memset(buf, 0, sizeof(buf));
805 memset(tmp, 0, sizeof(tmp));
806
807 cipher_info = mbedtls_cipher_info_from_type(cipher_type);
808
809 TIME_AND_TSC(title,
810 mbedtls_cipher_cmac(cipher_info, tmp, keysize,
811 buf, BUFSIZE, output));
812 }
813
814 memset(buf, 0, sizeof(buf));
815 memset(tmp, 0, sizeof(tmp));
816 TIME_AND_TSC("AES-CMAC-PRF-128",
817 mbedtls_aes_cmac_prf_128(tmp, 16, buf, BUFSIZE,
818 output));
819 }
820 #endif /* MBEDTLS_CMAC_C */
821 #endif /* MBEDTLS_AES_C */
822
823 #if defined(MBEDTLS_ARIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
824 if (todo.aria) {
825 int keysize;
826 mbedtls_aria_context aria;
827 mbedtls_aria_init(&aria);
828 for (keysize = 128; keysize <= 256; keysize += 64) {
829 mbedtls_snprintf(title, sizeof(title), "ARIA-CBC-%d", keysize);
830
831 memset(buf, 0, sizeof(buf));
832 memset(tmp, 0, sizeof(tmp));
833 mbedtls_aria_setkey_enc(&aria, tmp, keysize);
834
835 TIME_AND_TSC(title,
836 mbedtls_aria_crypt_cbc(&aria, MBEDTLS_ARIA_ENCRYPT,
837 BUFSIZE, tmp, buf, buf));
838 }
839 mbedtls_aria_free(&aria);
840 }
841 #endif
842
843 #if defined(MBEDTLS_CAMELLIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
844 if (todo.camellia) {
845 int keysize;
846 mbedtls_camellia_context camellia;
847 mbedtls_camellia_init(&camellia);
848 for (keysize = 128; keysize <= 256; keysize += 64) {
849 mbedtls_snprintf(title, sizeof(title), "CAMELLIA-CBC-%d", keysize);
850
851 memset(buf, 0, sizeof(buf));
852 memset(tmp, 0, sizeof(tmp));
853 mbedtls_camellia_setkey_enc(&camellia, tmp, keysize);
854
855 TIME_AND_TSC(title,
856 mbedtls_camellia_crypt_cbc(&camellia, MBEDTLS_CAMELLIA_ENCRYPT,
857 BUFSIZE, tmp, buf, buf));
858 }
859 mbedtls_camellia_free(&camellia);
860 }
861 #endif
862
863 #if defined(MBEDTLS_CHACHA20_C)
864 if (todo.chacha20) {
865 TIME_AND_TSC("ChaCha20", mbedtls_chacha20_crypt(buf, buf, 0U, BUFSIZE, buf, buf));
866 }
867 #endif
868
869 #if defined(MBEDTLS_POLY1305_C)
870 if (todo.poly1305) {
871 TIME_AND_TSC("Poly1305", mbedtls_poly1305_mac(buf, buf, BUFSIZE, buf));
872 }
873 #endif
874
875 #if defined(MBEDTLS_CTR_DRBG_C)
876 if (todo.ctr_drbg) {
877 mbedtls_ctr_drbg_context ctr_drbg;
878
879 mbedtls_ctr_drbg_init(&ctr_drbg);
880 if (mbedtls_ctr_drbg_seed(&ctr_drbg, myrand, NULL, NULL, 0) != 0) {
881 mbedtls_exit(1);
882 }
883 TIME_AND_TSC("CTR_DRBG (NOPR)",
884 mbedtls_ctr_drbg_random(&ctr_drbg, buf, BUFSIZE));
885 mbedtls_ctr_drbg_free(&ctr_drbg);
886
887 mbedtls_ctr_drbg_init(&ctr_drbg);
888 if (mbedtls_ctr_drbg_seed(&ctr_drbg, myrand, NULL, NULL, 0) != 0) {
889 mbedtls_exit(1);
890 }
891 mbedtls_ctr_drbg_set_prediction_resistance(&ctr_drbg, MBEDTLS_CTR_DRBG_PR_ON);
892 TIME_AND_TSC("CTR_DRBG (PR)",
893 mbedtls_ctr_drbg_random(&ctr_drbg, buf, BUFSIZE));
894 mbedtls_ctr_drbg_free(&ctr_drbg);
895 }
896 #endif
897
898 #if defined(MBEDTLS_HMAC_DRBG_C) && \
899 (defined(MBEDTLS_SHA1_C) || defined(MBEDTLS_SHA256_C))
900 if (todo.hmac_drbg) {
901 mbedtls_hmac_drbg_context hmac_drbg;
902 const mbedtls_md_info_t *md_info;
903
904 mbedtls_hmac_drbg_init(&hmac_drbg);
905
906 #if defined(MBEDTLS_SHA1_C)
907 if ((md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA1)) == NULL) {
908 mbedtls_exit(1);
909 }
910
911 if (mbedtls_hmac_drbg_seed(&hmac_drbg, md_info, myrand, NULL, NULL, 0) != 0) {
912 mbedtls_exit(1);
913 }
914 TIME_AND_TSC("HMAC_DRBG SHA-1 (NOPR)",
915 mbedtls_hmac_drbg_random(&hmac_drbg, buf, BUFSIZE));
916
917 if (mbedtls_hmac_drbg_seed(&hmac_drbg, md_info, myrand, NULL, NULL, 0) != 0) {
918 mbedtls_exit(1);
919 }
920 mbedtls_hmac_drbg_set_prediction_resistance(&hmac_drbg,
921 MBEDTLS_HMAC_DRBG_PR_ON);
922 TIME_AND_TSC("HMAC_DRBG SHA-1 (PR)",
923 mbedtls_hmac_drbg_random(&hmac_drbg, buf, BUFSIZE));
924 #endif
925
926 #if defined(MBEDTLS_SHA256_C)
927 if ((md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256)) == NULL) {
928 mbedtls_exit(1);
929 }
930
931 if (mbedtls_hmac_drbg_seed(&hmac_drbg, md_info, myrand, NULL, NULL, 0) != 0) {
932 mbedtls_exit(1);
933 }
934 TIME_AND_TSC("HMAC_DRBG SHA-256 (NOPR)",
935 mbedtls_hmac_drbg_random(&hmac_drbg, buf, BUFSIZE));
936
937 if (mbedtls_hmac_drbg_seed(&hmac_drbg, md_info, myrand, NULL, NULL, 0) != 0) {
938 mbedtls_exit(1);
939 }
940 mbedtls_hmac_drbg_set_prediction_resistance(&hmac_drbg,
941 MBEDTLS_HMAC_DRBG_PR_ON);
942 TIME_AND_TSC("HMAC_DRBG SHA-256 (PR)",
943 mbedtls_hmac_drbg_random(&hmac_drbg, buf, BUFSIZE));
944 #endif
945 mbedtls_hmac_drbg_free(&hmac_drbg);
946 }
947 #endif /* MBEDTLS_HMAC_DRBG_C && ( MBEDTLS_SHA1_C || MBEDTLS_SHA256_C ) */
948
949 #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
950 if (todo.rsa) {
951 int keysize;
952 mbedtls_rsa_context rsa;
953 for (keysize = 2048; keysize <= 4096; keysize *= 2) {
954 mbedtls_snprintf(title, sizeof(title), "RSA-%d", keysize);
955
956 mbedtls_rsa_init(&rsa);
957 mbedtls_rsa_gen_key(&rsa, myrand, NULL, keysize, 65537);
958
959 TIME_PUBLIC(title, " public",
960 buf[0] = 0;
961 ret = mbedtls_rsa_public(&rsa, buf, buf));
962
963 TIME_PUBLIC(title, "private",
964 buf[0] = 0;
965 ret = mbedtls_rsa_private(&rsa, myrand, NULL, buf, buf));
966
967 mbedtls_rsa_free(&rsa);
968 }
969 }
970 #endif
971
972 #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_BIGNUM_C)
973 if (todo.dhm) {
974 int dhm_sizes[] = { 2048, 3072 };
975 static const unsigned char dhm_P_2048[] =
976 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
977 static const unsigned char dhm_P_3072[] =
978 MBEDTLS_DHM_RFC3526_MODP_3072_P_BIN;
979 static const unsigned char dhm_G_2048[] =
980 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
981 static const unsigned char dhm_G_3072[] =
982 MBEDTLS_DHM_RFC3526_MODP_3072_G_BIN;
983
984 const unsigned char *dhm_P[] = { dhm_P_2048, dhm_P_3072 };
985 const size_t dhm_P_size[] = { sizeof(dhm_P_2048),
986 sizeof(dhm_P_3072) };
987
988 const unsigned char *dhm_G[] = { dhm_G_2048, dhm_G_3072 };
989 const size_t dhm_G_size[] = { sizeof(dhm_G_2048),
990 sizeof(dhm_G_3072) };
991
992 mbedtls_dhm_context dhm;
993 size_t olen;
994 size_t n;
995 for (i = 0; (size_t) i < sizeof(dhm_sizes) / sizeof(dhm_sizes[0]); i++) {
996 mbedtls_dhm_init(&dhm);
997
998 if (mbedtls_mpi_read_binary(&dhm.P, dhm_P[i],
999 dhm_P_size[i]) != 0 ||
1000 mbedtls_mpi_read_binary(&dhm.G, dhm_G[i],
1001 dhm_G_size[i]) != 0) {
1002 mbedtls_exit(1);
1003 }
1004
1005 n = mbedtls_mpi_size(&dhm.P);
1006 mbedtls_dhm_make_public(&dhm, (int) n, buf, n, myrand, NULL);
1007 if (mbedtls_mpi_copy(&dhm.GY, &dhm.GX) != 0) {
1008 mbedtls_exit(1);
1009 }
1010
1011 mbedtls_snprintf(title, sizeof(title), "DHE-%d", dhm_sizes[i]);
1012 TIME_PUBLIC(title, "handshake",
1013 ret |= mbedtls_dhm_make_public(&dhm, (int) n, buf, n,
1014 myrand, NULL);
1015 ret |=
1016 mbedtls_dhm_calc_secret(&dhm, buf, sizeof(buf), &olen, myrand, NULL));
1017
1018 mbedtls_snprintf(title, sizeof(title), "DH-%d", dhm_sizes[i]);
1019 TIME_PUBLIC(title, "handshake",
1020 ret |=
1021 mbedtls_dhm_calc_secret(&dhm, buf, sizeof(buf), &olen, myrand, NULL));
1022
1023 mbedtls_dhm_free(&dhm);
1024 }
1025 }
1026 #endif
1027
1028 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C)
1029 if (todo.ecdsa) {
1030 mbedtls_ecdsa_context ecdsa;
1031 const mbedtls_ecp_curve_info *curve_info;
1032 size_t sig_len;
1033
1034 memset(buf, 0x2A, sizeof(buf));
1035
1036 for (curve_info = curve_list;
1037 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
1038 curve_info++) {
1039 if (!mbedtls_ecdsa_can_do(curve_info->grp_id)) {
1040 continue;
1041 }
1042
1043 mbedtls_ecdsa_init(&ecdsa);
1044
1045 if (mbedtls_ecdsa_genkey(&ecdsa, curve_info->grp_id, myrand, NULL) != 0) {
1046 mbedtls_exit(1);
1047 }
1048
1049 mbedtls_snprintf(title, sizeof(title), "ECDSA-%s",
1050 curve_info->name);
1051 TIME_PUBLIC(title,
1052 "sign",
1053 ret =
1054 mbedtls_ecdsa_write_signature(&ecdsa, MBEDTLS_MD_SHA256, buf,
1055 curve_info->bit_size,
1056 tmp, sizeof(tmp), &sig_len, myrand,
1057 NULL));
1058
1059 mbedtls_ecdsa_free(&ecdsa);
1060 }
1061
1062 for (curve_info = curve_list;
1063 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
1064 curve_info++) {
1065 if (!mbedtls_ecdsa_can_do(curve_info->grp_id)) {
1066 continue;
1067 }
1068
1069 mbedtls_ecdsa_init(&ecdsa);
1070
1071 if (mbedtls_ecdsa_genkey(&ecdsa, curve_info->grp_id, myrand, NULL) != 0 ||
1072 mbedtls_ecdsa_write_signature(&ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
1073 tmp, sizeof(tmp), &sig_len, myrand, NULL) != 0) {
1074 mbedtls_exit(1);
1075 }
1076
1077 mbedtls_snprintf(title, sizeof(title), "ECDSA-%s",
1078 curve_info->name);
1079 TIME_PUBLIC(title, "verify",
1080 ret = mbedtls_ecdsa_read_signature(&ecdsa, buf, curve_info->bit_size,
1081 tmp, sig_len));
1082
1083 mbedtls_ecdsa_free(&ecdsa);
1084 }
1085 }
1086 #endif
1087
1088 #if defined(MBEDTLS_ECDH_C) && defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
1089 if (todo.ecdh) {
1090 mbedtls_ecdh_context ecdh;
1091 mbedtls_mpi z;
1092 const mbedtls_ecp_curve_info montgomery_curve_list[] = {
1093 #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
1094 { MBEDTLS_ECP_DP_CURVE25519, 0, 0, "Curve25519" },
1095 #endif
1096 #if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
1097 { MBEDTLS_ECP_DP_CURVE448, 0, 0, "Curve448" },
1098 #endif
1099 { MBEDTLS_ECP_DP_NONE, 0, 0, 0 }
1100 };
1101 const mbedtls_ecp_curve_info *curve_info;
1102 size_t olen;
1103 const mbedtls_ecp_curve_info *selected_montgomery_curve_list =
1104 montgomery_curve_list;
1105
1106 if (curve_list == (const mbedtls_ecp_curve_info *) &single_curve) {
1107 mbedtls_ecp_group grp;
1108 mbedtls_ecp_group_init(&grp);
1109 if (mbedtls_ecp_group_load(&grp, curve_list->grp_id) != 0) {
1110 mbedtls_exit(1);
1111 }
1112 if (mbedtls_ecp_get_type(&grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
1113 selected_montgomery_curve_list = single_curve;
1114 } else { /* empty list */
1115 selected_montgomery_curve_list = single_curve + 1;
1116 }
1117 mbedtls_ecp_group_free(&grp);
1118 }
1119
1120 for (curve_info = curve_list;
1121 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
1122 curve_info++) {
1123 if (!mbedtls_ecdh_can_do(curve_info->grp_id)) {
1124 continue;
1125 }
1126
1127 mbedtls_ecdh_init(&ecdh);
1128
1129 CHECK_AND_CONTINUE(mbedtls_ecp_group_load(&ecdh.grp, curve_info->grp_id));
1130 CHECK_AND_CONTINUE(mbedtls_ecdh_make_public(&ecdh, &olen, buf, sizeof(buf),
1131 myrand, NULL));
1132 CHECK_AND_CONTINUE(mbedtls_ecp_copy(&ecdh.Qp, &ecdh.Q));
1133
1134 mbedtls_snprintf(title, sizeof(title), "ECDHE-%s",
1135 curve_info->name);
1136 TIME_PUBLIC(title, "handshake",
1137 CHECK_AND_CONTINUE(mbedtls_ecdh_make_public(&ecdh, &olen, buf, sizeof(buf),
1138 myrand, NULL));
1139 CHECK_AND_CONTINUE(mbedtls_ecdh_calc_secret(&ecdh, &olen, buf, sizeof(buf),
1140 myrand, NULL)));
1141 mbedtls_ecdh_free(&ecdh);
1142 }
1143
1144 /* Montgomery curves need to be handled separately */
1145 for (curve_info = selected_montgomery_curve_list;
1146 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
1147 curve_info++) {
1148 mbedtls_ecdh_init(&ecdh);
1149 mbedtls_mpi_init(&z);
1150
1151 CHECK_AND_CONTINUE(mbedtls_ecp_group_load(&ecdh.grp, curve_info->grp_id));
1152 CHECK_AND_CONTINUE(mbedtls_ecdh_gen_public(&ecdh.grp, &ecdh.d, &ecdh.Qp, myrand, NULL));
1153
1154 mbedtls_snprintf(title, sizeof(title), "ECDHE-%s",
1155 curve_info->name);
1156 TIME_PUBLIC(title, "handshake",
1157 CHECK_AND_CONTINUE(mbedtls_ecdh_gen_public(&ecdh.grp, &ecdh.d, &ecdh.Q,
1158 myrand, NULL));
1159 CHECK_AND_CONTINUE(mbedtls_ecdh_compute_shared(&ecdh.grp, &z, &ecdh.Qp,
1160 &ecdh.d,
1161 myrand, NULL)));
1162
1163 mbedtls_ecdh_free(&ecdh);
1164 mbedtls_mpi_free(&z);
1165 }
1166
1167 for (curve_info = curve_list;
1168 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
1169 curve_info++) {
1170 if (!mbedtls_ecdh_can_do(curve_info->grp_id)) {
1171 continue;
1172 }
1173
1174 mbedtls_ecdh_init(&ecdh);
1175
1176 CHECK_AND_CONTINUE(mbedtls_ecp_group_load(&ecdh.grp, curve_info->grp_id));
1177 CHECK_AND_CONTINUE(mbedtls_ecdh_make_public(&ecdh, &olen, buf, sizeof(buf),
1178 myrand, NULL));
1179 CHECK_AND_CONTINUE(mbedtls_ecp_copy(&ecdh.Qp, &ecdh.Q));
1180 CHECK_AND_CONTINUE(mbedtls_ecdh_make_public(&ecdh, &olen, buf, sizeof(buf),
1181 myrand, NULL));
1182
1183 mbedtls_snprintf(title, sizeof(title), "ECDH-%s",
1184 curve_info->name);
1185 TIME_PUBLIC(title, "handshake",
1186 CHECK_AND_CONTINUE(mbedtls_ecdh_calc_secret(&ecdh, &olen, buf, sizeof(buf),
1187 myrand, NULL)));
1188 mbedtls_ecdh_free(&ecdh);
1189 }
1190
1191 /* Montgomery curves need to be handled separately */
1192 for (curve_info = selected_montgomery_curve_list;
1193 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
1194 curve_info++) {
1195 mbedtls_ecdh_init(&ecdh);
1196 mbedtls_mpi_init(&z);
1197
1198 CHECK_AND_CONTINUE(mbedtls_ecp_group_load(&ecdh.grp, curve_info->grp_id));
1199 CHECK_AND_CONTINUE(mbedtls_ecdh_gen_public(&ecdh.grp, &ecdh.d, &ecdh.Qp,
1200 myrand, NULL));
1201 CHECK_AND_CONTINUE(mbedtls_ecdh_gen_public(&ecdh.grp, &ecdh.d, &ecdh.Q, myrand, NULL));
1202
1203 mbedtls_snprintf(title, sizeof(title), "ECDH-%s",
1204 curve_info->name);
1205 TIME_PUBLIC(title, "handshake",
1206 CHECK_AND_CONTINUE(mbedtls_ecdh_compute_shared(&ecdh.grp, &z, &ecdh.Qp,
1207 &ecdh.d,
1208 myrand, NULL)));
1209
1210 mbedtls_ecdh_free(&ecdh);
1211 mbedtls_mpi_free(&z);
1212 }
1213 }
1214 #endif
1215
1216 #if defined(MBEDTLS_ECDH_C)
1217 if (todo.ecdh) {
1218 mbedtls_ecdh_context ecdh_srv, ecdh_cli;
1219 unsigned char buf_srv[BUFSIZE], buf_cli[BUFSIZE];
1220 const mbedtls_ecp_curve_info *curve_info;
1221 size_t olen;
1222
1223 for (curve_info = curve_list;
1224 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
1225 curve_info++) {
1226 if (!mbedtls_ecdh_can_do(curve_info->grp_id)) {
1227 continue;
1228 }
1229
1230 mbedtls_ecdh_init(&ecdh_srv);
1231 mbedtls_ecdh_init(&ecdh_cli);
1232
1233 mbedtls_snprintf(title, sizeof(title), "ECDHE-%s", curve_info->name);
1234 TIME_PUBLIC(title,
1235 "full handshake",
1236 const unsigned char *p_srv = buf_srv;
1237
1238 CHECK_AND_CONTINUE(mbedtls_ecdh_setup(&ecdh_srv, curve_info->grp_id));
1239 CHECK_AND_CONTINUE(mbedtls_ecdh_make_params(&ecdh_srv, &olen, buf_srv,
1240 sizeof(buf_srv), myrand, NULL));
1241
1242 CHECK_AND_CONTINUE(mbedtls_ecdh_read_params(&ecdh_cli, &p_srv,
1243 p_srv + olen));
1244 CHECK_AND_CONTINUE(mbedtls_ecdh_make_public(&ecdh_cli, &olen, buf_cli,
1245 sizeof(buf_cli), myrand, NULL));
1246
1247 CHECK_AND_CONTINUE(mbedtls_ecdh_read_public(&ecdh_srv, buf_cli, olen));
1248 CHECK_AND_CONTINUE(mbedtls_ecdh_calc_secret(&ecdh_srv, &olen, buf_srv,
1249 sizeof(buf_srv), myrand, NULL));
1250
1251 CHECK_AND_CONTINUE(mbedtls_ecdh_calc_secret(&ecdh_cli, &olen, buf_cli,
1252 sizeof(buf_cli), myrand, NULL));
1253 mbedtls_ecdh_free(&ecdh_cli);
1254
1255 mbedtls_ecdh_free(&ecdh_srv);
1256 );
1257
1258 }
1259 }
1260 #endif
1261
1262 mbedtls_printf("\n");
1263
1264 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
1265 mbedtls_memory_buffer_alloc_free();
1266 #endif
1267
1268 mbedtls_exit(0);
1269 }
1270
1271 #endif /* MBEDTLS_HAVE_TIME */
1272