1 /*
2 * Benchmark demonstration program
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 */
7
8 #if !defined(MBEDTLS_CONFIG_FILE)
9 #include "mbedtls/config.h"
10 #else
11 #include MBEDTLS_CONFIG_FILE
12 #endif
13
14 #include "mbedtls/platform.h"
15
16 #if !defined(MBEDTLS_TIMING_C)
main(void)17 int main(void)
18 {
19 mbedtls_printf("MBEDTLS_TIMING_C not defined.\n");
20 mbedtls_exit(0);
21 }
22 #else
23
24 #include <string.h>
25 #include <stdlib.h>
26
27 #include "mbedtls/timing.h"
28
29 #include "mbedtls/md4.h"
30 #include "mbedtls/md5.h"
31 #include "mbedtls/ripemd160.h"
32 #include "mbedtls/sha1.h"
33 #include "mbedtls/sha256.h"
34 #include "mbedtls/sha512.h"
35
36 #include "mbedtls/arc4.h"
37 #include "mbedtls/des.h"
38 #include "mbedtls/aes.h"
39 #include "mbedtls/aria.h"
40 #include "mbedtls/blowfish.h"
41 #include "mbedtls/camellia.h"
42 #include "mbedtls/chacha20.h"
43 #include "mbedtls/gcm.h"
44 #include "mbedtls/ccm.h"
45 #include "mbedtls/chachapoly.h"
46 #include "mbedtls/cmac.h"
47 #include "mbedtls/poly1305.h"
48
49 #include "mbedtls/havege.h"
50 #include "mbedtls/ctr_drbg.h"
51 #include "mbedtls/hmac_drbg.h"
52
53 #include "mbedtls/rsa.h"
54 #include "mbedtls/dhm.h"
55 #include "mbedtls/ecdsa.h"
56 #include "mbedtls/ecdh.h"
57
58 #include "mbedtls/error.h"
59
60 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
61 #include "mbedtls/memory_buffer_alloc.h"
62 #endif
63
64 /*
65 * For heap usage estimates, we need an estimate of the overhead per allocated
66 * block. ptmalloc2/3 (used in gnu libc for instance) uses 2 size_t per block,
67 * so use that as our baseline.
68 */
69 #define MEM_BLOCK_OVERHEAD (2 * sizeof(size_t))
70
71 /*
72 * Size to use for the alloc buffer if MEMORY_BUFFER_ALLOC_C is defined.
73 */
74 #define HEAP_SIZE (1u << 16) /* 64k */
75
76 #define BUFSIZE 1024
77 #define HEADER_FORMAT " %-24s : "
78 #define TITLE_LEN 25
79
80 #define OPTIONS \
81 "md4, md5, ripemd160, sha1, sha256, sha512,\n" \
82 "arc4, des3, des, camellia, blowfish, chacha20,\n" \
83 "aes_cbc, aes_gcm, aes_ccm, aes_xts, chachapoly,\n" \
84 "aes_cmac, des3_cmac, poly1305\n" \
85 "havege, ctr_drbg, hmac_drbg\n" \
86 "rsa, dhm, ecdsa, ecdh.\n"
87
88 #if defined(MBEDTLS_ERROR_C)
89 #define PRINT_ERROR \
90 mbedtls_strerror(ret, (char *) tmp, sizeof(tmp)); \
91 mbedtls_printf("FAILED: %s\n", tmp);
92 #else
93 #define PRINT_ERROR \
94 mbedtls_printf("FAILED: -0x%04x\n", (unsigned int) -ret);
95 #endif
96
97 #define TIME_AND_TSC(TITLE, CODE) \
98 do { \
99 unsigned long ii, jj, tsc; \
100 int ret = 0; \
101 \
102 mbedtls_printf(HEADER_FORMAT, TITLE); \
103 fflush(stdout); \
104 \
105 mbedtls_set_alarm(1); \
106 for (ii = 1; ret == 0 && !mbedtls_timing_alarmed; ii++) \
107 { \
108 ret = CODE; \
109 } \
110 \
111 tsc = mbedtls_timing_hardclock(); \
112 for (jj = 0; ret == 0 && jj < 1024; jj++) \
113 { \
114 ret = CODE; \
115 } \
116 \
117 if (ret != 0) \
118 { \
119 PRINT_ERROR; \
120 } \
121 else \
122 { \
123 mbedtls_printf("%9lu KiB/s, %9lu cycles/byte\n", \
124 ii * BUFSIZE / 1024, \
125 (mbedtls_timing_hardclock() - tsc) \
126 / (jj * BUFSIZE)); \
127 } \
128 } while (0)
129
130 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_MEMORY_DEBUG)
131
132 /* How much space to reserve for the title when printing heap usage results.
133 * Updated manually as the output of the following command:
134 *
135 * sed -n 's/.*[T]IME_PUBLIC.*"\(.*\)",/\1/p' programs/test/benchmark.c |
136 * awk '{print length+3}' | sort -rn | head -n1
137 *
138 * This computes the maximum length of a title +3, because we appends "/s" and
139 * want at least one space. (If the value is too small, the only consequence
140 * is poor alignment.) */
141 #define TITLE_SPACE 17
142
143 #define MEMORY_MEASURE_INIT \
144 size_t max_used, max_blocks, max_bytes; \
145 size_t prv_used, prv_blocks; \
146 mbedtls_memory_buffer_alloc_cur_get(&prv_used, &prv_blocks); \
147 mbedtls_memory_buffer_alloc_max_reset();
148
149 #define MEMORY_MEASURE_PRINT(title_len) \
150 mbedtls_memory_buffer_alloc_max_get(&max_used, &max_blocks); \
151 ii = TITLE_SPACE > (title_len) ? TITLE_SPACE - (title_len) : 1; \
152 while (ii--) mbedtls_printf(" "); \
153 max_used -= prv_used; \
154 max_blocks -= prv_blocks; \
155 max_bytes = max_used + MEM_BLOCK_OVERHEAD * max_blocks; \
156 mbedtls_printf("%6u heap bytes", (unsigned) max_bytes);
157
158 #else
159 #define MEMORY_MEASURE_INIT
160 #define MEMORY_MEASURE_PRINT(title_len)
161 #endif
162
163 #define TIME_PUBLIC(TITLE, TYPE, CODE) \
164 do { \
165 unsigned long ii; \
166 int ret; \
167 MEMORY_MEASURE_INIT; \
168 \
169 mbedtls_printf(HEADER_FORMAT, TITLE); \
170 fflush(stdout); \
171 mbedtls_set_alarm(3); \
172 \
173 ret = 0; \
174 for (ii = 1; !mbedtls_timing_alarmed && !ret; ii++) \
175 { \
176 CODE; \
177 } \
178 \
179 if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) \
180 { \
181 mbedtls_printf("Feature Not Supported. Skipping.\n"); \
182 ret = 0; \
183 } \
184 else if (ret != 0) \
185 { \
186 PRINT_ERROR; \
187 } \
188 else \
189 { \
190 mbedtls_printf("%6lu " TYPE "/s", ii / 3); \
191 MEMORY_MEASURE_PRINT(sizeof(TYPE) + 1); \
192 mbedtls_printf("\n"); \
193 } \
194 } while (0)
195
myrand(void * rng_state,unsigned char * output,size_t len)196 static int myrand(void *rng_state, unsigned char *output, size_t len)
197 {
198 size_t use_len;
199 int rnd;
200
201 if (rng_state != NULL) {
202 rng_state = NULL;
203 }
204
205 while (len > 0) {
206 use_len = len;
207 if (use_len > sizeof(int)) {
208 use_len = sizeof(int);
209 }
210
211 rnd = rand();
212 memcpy(output, &rnd, use_len);
213 output += use_len;
214 len -= use_len;
215 }
216
217 return 0;
218 }
219
220 #define CHECK_AND_CONTINUE(R) \
221 { \
222 int CHECK_AND_CONTINUE_ret = (R); \
223 if (CHECK_AND_CONTINUE_ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) { \
224 mbedtls_printf("Feature not supported. Skipping.\n"); \
225 continue; \
226 } \
227 else if (CHECK_AND_CONTINUE_ret != 0) { \
228 mbedtls_exit(1); \
229 } \
230 }
231
232 /*
233 * Clear some memory that was used to prepare the context
234 */
235 #if defined(MBEDTLS_ECP_C)
ecp_clear_precomputed(mbedtls_ecp_group * grp)236 void ecp_clear_precomputed(mbedtls_ecp_group *grp)
237 {
238 if (grp->T != NULL) {
239 size_t i;
240 for (i = 0; i < grp->T_size; i++) {
241 mbedtls_ecp_point_free(&grp->T[i]);
242 }
243 mbedtls_free(grp->T);
244 }
245 grp->T = NULL;
246 grp->T_size = 0;
247 }
248 #else
249 #define ecp_clear_precomputed(g)
250 #endif
251
252 #if defined(MBEDTLS_ECP_C)
set_ecp_curve(const char * string,mbedtls_ecp_curve_info * curve)253 static int set_ecp_curve(const char *string, mbedtls_ecp_curve_info *curve)
254 {
255 const mbedtls_ecp_curve_info *found =
256 mbedtls_ecp_curve_info_from_name(string);
257 if (found != NULL) {
258 *curve = *found;
259 return 1;
260 } else {
261 return 0;
262 }
263 }
264 #endif
265
266 unsigned char buf[BUFSIZE];
267
268 typedef struct {
269 char md4, md5, ripemd160, sha1, sha256, sha512,
270 arc4, des3, des,
271 aes_cbc, aes_gcm, aes_ccm, aes_xts, chachapoly,
272 aes_cmac, des3_cmac,
273 aria, camellia, blowfish, chacha20,
274 poly1305,
275 havege, ctr_drbg, hmac_drbg,
276 rsa, dhm, ecdsa, ecdh;
277 } todo_list;
278
279
main(int argc,char * argv[])280 int main(int argc, char *argv[])
281 {
282 int i;
283 unsigned char tmp[200];
284 char title[TITLE_LEN];
285 todo_list todo;
286 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
287 unsigned char alloc_buf[HEAP_SIZE] = { 0 };
288 #endif
289 #if defined(MBEDTLS_ECP_C)
290 mbedtls_ecp_curve_info single_curve[2] = {
291 { MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
292 { MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
293 };
294 const mbedtls_ecp_curve_info *curve_list = mbedtls_ecp_curve_list();
295 #endif
296
297 #if defined(MBEDTLS_ECP_C)
298 (void) curve_list; /* Unused in some configurations where no benchmark uses ECC */
299 #endif
300
301 if (argc <= 1) {
302 memset(&todo, 1, sizeof(todo));
303 } else {
304 memset(&todo, 0, sizeof(todo));
305
306 for (i = 1; i < argc; i++) {
307 if (strcmp(argv[i], "md4") == 0) {
308 todo.md4 = 1;
309 } else if (strcmp(argv[i], "md5") == 0) {
310 todo.md5 = 1;
311 } else if (strcmp(argv[i], "ripemd160") == 0) {
312 todo.ripemd160 = 1;
313 } else if (strcmp(argv[i], "sha1") == 0) {
314 todo.sha1 = 1;
315 } else if (strcmp(argv[i], "sha256") == 0) {
316 todo.sha256 = 1;
317 } else if (strcmp(argv[i], "sha512") == 0) {
318 todo.sha512 = 1;
319 } else if (strcmp(argv[i], "arc4") == 0) {
320 todo.arc4 = 1;
321 } else if (strcmp(argv[i], "des3") == 0) {
322 todo.des3 = 1;
323 } else if (strcmp(argv[i], "des") == 0) {
324 todo.des = 1;
325 } else if (strcmp(argv[i], "aes_cbc") == 0) {
326 todo.aes_cbc = 1;
327 } else if (strcmp(argv[i], "aes_xts") == 0) {
328 todo.aes_xts = 1;
329 } else if (strcmp(argv[i], "aes_gcm") == 0) {
330 todo.aes_gcm = 1;
331 } else if (strcmp(argv[i], "aes_ccm") == 0) {
332 todo.aes_ccm = 1;
333 } else if (strcmp(argv[i], "chachapoly") == 0) {
334 todo.chachapoly = 1;
335 } else if (strcmp(argv[i], "aes_cmac") == 0) {
336 todo.aes_cmac = 1;
337 } else if (strcmp(argv[i], "des3_cmac") == 0) {
338 todo.des3_cmac = 1;
339 } else if (strcmp(argv[i], "aria") == 0) {
340 todo.aria = 1;
341 } else if (strcmp(argv[i], "camellia") == 0) {
342 todo.camellia = 1;
343 } else if (strcmp(argv[i], "blowfish") == 0) {
344 todo.blowfish = 1;
345 } else if (strcmp(argv[i], "chacha20") == 0) {
346 todo.chacha20 = 1;
347 } else if (strcmp(argv[i], "poly1305") == 0) {
348 todo.poly1305 = 1;
349 } else if (strcmp(argv[i], "havege") == 0) {
350 todo.havege = 1;
351 } else if (strcmp(argv[i], "ctr_drbg") == 0) {
352 todo.ctr_drbg = 1;
353 } else if (strcmp(argv[i], "hmac_drbg") == 0) {
354 todo.hmac_drbg = 1;
355 } else if (strcmp(argv[i], "rsa") == 0) {
356 todo.rsa = 1;
357 } else if (strcmp(argv[i], "dhm") == 0) {
358 todo.dhm = 1;
359 } else if (strcmp(argv[i], "ecdsa") == 0) {
360 todo.ecdsa = 1;
361 } else if (strcmp(argv[i], "ecdh") == 0) {
362 todo.ecdh = 1;
363 }
364 #if defined(MBEDTLS_ECP_C)
365 else if (set_ecp_curve(argv[i], single_curve)) {
366 curve_list = single_curve;
367 }
368 #endif
369 else {
370 mbedtls_printf("Unrecognized option: %s\n", argv[i]);
371 mbedtls_printf("Available options: " OPTIONS);
372 }
373 }
374 }
375
376 mbedtls_printf("\n");
377
378 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
379 mbedtls_memory_buffer_alloc_init(alloc_buf, sizeof(alloc_buf));
380 #endif
381 memset(buf, 0xAA, sizeof(buf));
382 memset(tmp, 0xBB, sizeof(tmp));
383
384 #if defined(MBEDTLS_MD4_C)
385 if (todo.md4) {
386 TIME_AND_TSC("MD4", mbedtls_md4_ret(buf, BUFSIZE, tmp));
387 }
388 #endif
389
390 #if defined(MBEDTLS_MD5_C)
391 if (todo.md5) {
392 TIME_AND_TSC("MD5", mbedtls_md5_ret(buf, BUFSIZE, tmp));
393 }
394 #endif
395
396 #if defined(MBEDTLS_RIPEMD160_C)
397 if (todo.ripemd160) {
398 TIME_AND_TSC("RIPEMD160", mbedtls_ripemd160_ret(buf, BUFSIZE, tmp));
399 }
400 #endif
401
402 #if defined(MBEDTLS_SHA1_C)
403 if (todo.sha1) {
404 TIME_AND_TSC("SHA-1", mbedtls_sha1_ret(buf, BUFSIZE, tmp));
405 }
406 #endif
407
408 #if defined(MBEDTLS_SHA256_C)
409 if (todo.sha256) {
410 TIME_AND_TSC("SHA-256", mbedtls_sha256_ret(buf, BUFSIZE, tmp, 0));
411 }
412 #endif
413
414 #if defined(MBEDTLS_SHA512_C)
415 if (todo.sha512) {
416 TIME_AND_TSC("SHA-512", mbedtls_sha512_ret(buf, BUFSIZE, tmp, 0));
417 }
418 #endif
419
420 #if defined(MBEDTLS_ARC4_C)
421 if (todo.arc4) {
422 mbedtls_arc4_context arc4;
423 mbedtls_arc4_init(&arc4);
424 mbedtls_arc4_setup(&arc4, tmp, 32);
425 TIME_AND_TSC("ARC4", mbedtls_arc4_crypt(&arc4, BUFSIZE, buf, buf));
426 mbedtls_arc4_free(&arc4);
427 }
428 #endif
429
430 #if defined(MBEDTLS_DES_C)
431 #if defined(MBEDTLS_CIPHER_MODE_CBC)
432 if (todo.des3) {
433 mbedtls_des3_context des3;
434 mbedtls_des3_init(&des3);
435 if (mbedtls_des3_set3key_enc(&des3, tmp) != 0) {
436 mbedtls_exit(1);
437 }
438 TIME_AND_TSC("3DES",
439 mbedtls_des3_crypt_cbc(&des3, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf));
440 mbedtls_des3_free(&des3);
441 }
442
443 if (todo.des) {
444 mbedtls_des_context des;
445 mbedtls_des_init(&des);
446 if (mbedtls_des_setkey_enc(&des, tmp) != 0) {
447 mbedtls_exit(1);
448 }
449 TIME_AND_TSC("DES",
450 mbedtls_des_crypt_cbc(&des, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf));
451 mbedtls_des_free(&des);
452 }
453
454 #endif /* MBEDTLS_CIPHER_MODE_CBC */
455 #if defined(MBEDTLS_CMAC_C)
456 if (todo.des3_cmac) {
457 unsigned char output[8];
458 const mbedtls_cipher_info_t *cipher_info;
459
460 memset(buf, 0, sizeof(buf));
461 memset(tmp, 0, sizeof(tmp));
462
463 cipher_info = mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_DES_EDE3_ECB);
464
465 TIME_AND_TSC("3DES-CMAC",
466 mbedtls_cipher_cmac(cipher_info, tmp, 192, buf,
467 BUFSIZE, output));
468 }
469 #endif /* MBEDTLS_CMAC_C */
470 #endif /* MBEDTLS_DES_C */
471
472 #if defined(MBEDTLS_AES_C)
473 #if defined(MBEDTLS_CIPHER_MODE_CBC)
474 if (todo.aes_cbc) {
475 int keysize;
476 mbedtls_aes_context aes;
477 mbedtls_aes_init(&aes);
478 for (keysize = 128; keysize <= 256; keysize += 64) {
479 mbedtls_snprintf(title, sizeof(title), "AES-CBC-%d", keysize);
480
481 memset(buf, 0, sizeof(buf));
482 memset(tmp, 0, sizeof(tmp));
483 CHECK_AND_CONTINUE(mbedtls_aes_setkey_enc(&aes, tmp, keysize));
484
485 TIME_AND_TSC(title,
486 mbedtls_aes_crypt_cbc(&aes, MBEDTLS_AES_ENCRYPT, BUFSIZE, tmp, buf, buf));
487 }
488 mbedtls_aes_free(&aes);
489 }
490 #endif
491 #if defined(MBEDTLS_CIPHER_MODE_XTS)
492 if (todo.aes_xts) {
493 int keysize;
494 mbedtls_aes_xts_context ctx;
495
496 mbedtls_aes_xts_init(&ctx);
497 for (keysize = 128; keysize <= 256; keysize += 128) {
498 mbedtls_snprintf(title, sizeof(title), "AES-XTS-%d", keysize);
499
500 memset(buf, 0, sizeof(buf));
501 memset(tmp, 0, sizeof(tmp));
502 CHECK_AND_CONTINUE(mbedtls_aes_xts_setkey_enc(&ctx, tmp, keysize * 2));
503
504 TIME_AND_TSC(title,
505 mbedtls_aes_crypt_xts(&ctx, MBEDTLS_AES_ENCRYPT, BUFSIZE,
506 tmp, buf, buf));
507
508 mbedtls_aes_xts_free(&ctx);
509 }
510 }
511 #endif
512 #if defined(MBEDTLS_GCM_C)
513 if (todo.aes_gcm) {
514 int keysize;
515 mbedtls_gcm_context gcm;
516
517 mbedtls_gcm_init(&gcm);
518 for (keysize = 128; keysize <= 256; keysize += 64) {
519 mbedtls_snprintf(title, sizeof(title), "AES-GCM-%d", keysize);
520
521 memset(buf, 0, sizeof(buf));
522 memset(tmp, 0, sizeof(tmp));
523 mbedtls_gcm_setkey(&gcm, MBEDTLS_CIPHER_ID_AES, tmp, keysize);
524
525 TIME_AND_TSC(title,
526 mbedtls_gcm_crypt_and_tag(&gcm, MBEDTLS_GCM_ENCRYPT, BUFSIZE, tmp,
527 12, NULL, 0, buf, buf, 16, tmp));
528
529 mbedtls_gcm_free(&gcm);
530 }
531 }
532 #endif
533 #if defined(MBEDTLS_CCM_C)
534 if (todo.aes_ccm) {
535 int keysize;
536 mbedtls_ccm_context ccm;
537
538 mbedtls_ccm_init(&ccm);
539 for (keysize = 128; keysize <= 256; keysize += 64) {
540 mbedtls_snprintf(title, sizeof(title), "AES-CCM-%d", keysize);
541
542 memset(buf, 0, sizeof(buf));
543 memset(tmp, 0, sizeof(tmp));
544 mbedtls_ccm_setkey(&ccm, MBEDTLS_CIPHER_ID_AES, tmp, keysize);
545
546 TIME_AND_TSC(title,
547 mbedtls_ccm_encrypt_and_tag(&ccm, BUFSIZE, tmp,
548 12, NULL, 0, buf, buf, tmp, 16));
549
550 mbedtls_ccm_free(&ccm);
551 }
552 }
553 #endif
554 #if defined(MBEDTLS_CHACHAPOLY_C)
555 if (todo.chachapoly) {
556 mbedtls_chachapoly_context chachapoly;
557
558 mbedtls_chachapoly_init(&chachapoly);
559 memset(buf, 0, sizeof(buf));
560 memset(tmp, 0, sizeof(tmp));
561
562 mbedtls_snprintf(title, sizeof(title), "ChaCha20-Poly1305");
563
564 mbedtls_chachapoly_setkey(&chachapoly, tmp);
565
566 TIME_AND_TSC(title,
567 mbedtls_chachapoly_encrypt_and_tag(&chachapoly,
568 BUFSIZE, tmp, NULL, 0, buf, buf, tmp));
569
570 mbedtls_chachapoly_free(&chachapoly);
571 }
572 #endif
573 #if defined(MBEDTLS_CMAC_C)
574 if (todo.aes_cmac) {
575 unsigned char output[16];
576 const mbedtls_cipher_info_t *cipher_info;
577 mbedtls_cipher_type_t cipher_type;
578 int keysize;
579
580 for (keysize = 128, cipher_type = MBEDTLS_CIPHER_AES_128_ECB;
581 keysize <= 256;
582 keysize += 64, cipher_type++) {
583 mbedtls_snprintf(title, sizeof(title), "AES-CMAC-%d", keysize);
584
585 memset(buf, 0, sizeof(buf));
586 memset(tmp, 0, sizeof(tmp));
587
588 cipher_info = mbedtls_cipher_info_from_type(cipher_type);
589
590 TIME_AND_TSC(title,
591 mbedtls_cipher_cmac(cipher_info, tmp, keysize,
592 buf, BUFSIZE, output));
593 }
594
595 memset(buf, 0, sizeof(buf));
596 memset(tmp, 0, sizeof(tmp));
597 TIME_AND_TSC("AES-CMAC-PRF-128",
598 mbedtls_aes_cmac_prf_128(tmp, 16, buf, BUFSIZE,
599 output));
600 }
601 #endif /* MBEDTLS_CMAC_C */
602 #endif /* MBEDTLS_AES_C */
603
604 #if defined(MBEDTLS_ARIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
605 if (todo.aria) {
606 int keysize;
607 mbedtls_aria_context aria;
608 mbedtls_aria_init(&aria);
609 for (keysize = 128; keysize <= 256; keysize += 64) {
610 mbedtls_snprintf(title, sizeof(title), "ARIA-CBC-%d", keysize);
611
612 memset(buf, 0, sizeof(buf));
613 memset(tmp, 0, sizeof(tmp));
614 mbedtls_aria_setkey_enc(&aria, tmp, keysize);
615
616 TIME_AND_TSC(title,
617 mbedtls_aria_crypt_cbc(&aria, MBEDTLS_ARIA_ENCRYPT,
618 BUFSIZE, tmp, buf, buf));
619 }
620 mbedtls_aria_free(&aria);
621 }
622 #endif
623
624 #if defined(MBEDTLS_CAMELLIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
625 if (todo.camellia) {
626 int keysize;
627 mbedtls_camellia_context camellia;
628 mbedtls_camellia_init(&camellia);
629 for (keysize = 128; keysize <= 256; keysize += 64) {
630 mbedtls_snprintf(title, sizeof(title), "CAMELLIA-CBC-%d", keysize);
631
632 memset(buf, 0, sizeof(buf));
633 memset(tmp, 0, sizeof(tmp));
634 mbedtls_camellia_setkey_enc(&camellia, tmp, keysize);
635
636 TIME_AND_TSC(title,
637 mbedtls_camellia_crypt_cbc(&camellia, MBEDTLS_CAMELLIA_ENCRYPT,
638 BUFSIZE, tmp, buf, buf));
639 }
640 mbedtls_camellia_free(&camellia);
641 }
642 #endif
643
644 #if defined(MBEDTLS_CHACHA20_C)
645 if (todo.chacha20) {
646 TIME_AND_TSC("ChaCha20", mbedtls_chacha20_crypt(buf, buf, 0U, BUFSIZE, buf, buf));
647 }
648 #endif
649
650 #if defined(MBEDTLS_POLY1305_C)
651 if (todo.poly1305) {
652 TIME_AND_TSC("Poly1305", mbedtls_poly1305_mac(buf, buf, BUFSIZE, buf));
653 }
654 #endif
655
656 #if defined(MBEDTLS_BLOWFISH_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
657 if (todo.blowfish) {
658 int keysize;
659 mbedtls_blowfish_context blowfish;
660 mbedtls_blowfish_init(&blowfish);
661
662 for (keysize = 128; keysize <= 256; keysize += 64) {
663 mbedtls_snprintf(title, sizeof(title), "BLOWFISH-CBC-%d", keysize);
664
665 memset(buf, 0, sizeof(buf));
666 memset(tmp, 0, sizeof(tmp));
667 mbedtls_blowfish_setkey(&blowfish, tmp, keysize);
668
669 TIME_AND_TSC(title,
670 mbedtls_blowfish_crypt_cbc(&blowfish, MBEDTLS_BLOWFISH_ENCRYPT, BUFSIZE,
671 tmp, buf, buf));
672 }
673
674 mbedtls_blowfish_free(&blowfish);
675 }
676 #endif
677
678 #if defined(MBEDTLS_HAVEGE_C)
679 if (todo.havege) {
680 mbedtls_havege_state hs;
681 mbedtls_havege_init(&hs);
682 TIME_AND_TSC("HAVEGE", mbedtls_havege_random(&hs, buf, BUFSIZE));
683 mbedtls_havege_free(&hs);
684 }
685 #endif
686
687 #if defined(MBEDTLS_CTR_DRBG_C)
688 if (todo.ctr_drbg) {
689 mbedtls_ctr_drbg_context ctr_drbg;
690
691 mbedtls_ctr_drbg_init(&ctr_drbg);
692 if (mbedtls_ctr_drbg_seed(&ctr_drbg, myrand, NULL, NULL, 0) != 0) {
693 mbedtls_exit(1);
694 }
695 TIME_AND_TSC("CTR_DRBG (NOPR)",
696 mbedtls_ctr_drbg_random(&ctr_drbg, buf, BUFSIZE));
697 mbedtls_ctr_drbg_free(&ctr_drbg);
698
699 mbedtls_ctr_drbg_init(&ctr_drbg);
700 if (mbedtls_ctr_drbg_seed(&ctr_drbg, myrand, NULL, NULL, 0) != 0) {
701 mbedtls_exit(1);
702 }
703 mbedtls_ctr_drbg_set_prediction_resistance(&ctr_drbg, MBEDTLS_CTR_DRBG_PR_ON);
704 TIME_AND_TSC("CTR_DRBG (PR)",
705 mbedtls_ctr_drbg_random(&ctr_drbg, buf, BUFSIZE));
706 mbedtls_ctr_drbg_free(&ctr_drbg);
707 }
708 #endif
709
710 #if defined(MBEDTLS_HMAC_DRBG_C) && \
711 (defined(MBEDTLS_SHA1_C) || defined(MBEDTLS_SHA256_C))
712 if (todo.hmac_drbg) {
713 mbedtls_hmac_drbg_context hmac_drbg;
714 const mbedtls_md_info_t *md_info;
715
716 mbedtls_hmac_drbg_init(&hmac_drbg);
717
718 #if defined(MBEDTLS_SHA1_C)
719 if ((md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA1)) == NULL) {
720 mbedtls_exit(1);
721 }
722
723 if (mbedtls_hmac_drbg_seed(&hmac_drbg, md_info, myrand, NULL, NULL, 0) != 0) {
724 mbedtls_exit(1);
725 }
726 TIME_AND_TSC("HMAC_DRBG SHA-1 (NOPR)",
727 mbedtls_hmac_drbg_random(&hmac_drbg, buf, BUFSIZE));
728
729 if (mbedtls_hmac_drbg_seed(&hmac_drbg, md_info, myrand, NULL, NULL, 0) != 0) {
730 mbedtls_exit(1);
731 }
732 mbedtls_hmac_drbg_set_prediction_resistance(&hmac_drbg,
733 MBEDTLS_HMAC_DRBG_PR_ON);
734 TIME_AND_TSC("HMAC_DRBG SHA-1 (PR)",
735 mbedtls_hmac_drbg_random(&hmac_drbg, buf, BUFSIZE));
736 #endif
737
738 #if defined(MBEDTLS_SHA256_C)
739 if ((md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256)) == NULL) {
740 mbedtls_exit(1);
741 }
742
743 if (mbedtls_hmac_drbg_seed(&hmac_drbg, md_info, myrand, NULL, NULL, 0) != 0) {
744 mbedtls_exit(1);
745 }
746 TIME_AND_TSC("HMAC_DRBG SHA-256 (NOPR)",
747 mbedtls_hmac_drbg_random(&hmac_drbg, buf, BUFSIZE));
748
749 if (mbedtls_hmac_drbg_seed(&hmac_drbg, md_info, myrand, NULL, NULL, 0) != 0) {
750 mbedtls_exit(1);
751 }
752 mbedtls_hmac_drbg_set_prediction_resistance(&hmac_drbg,
753 MBEDTLS_HMAC_DRBG_PR_ON);
754 TIME_AND_TSC("HMAC_DRBG SHA-256 (PR)",
755 mbedtls_hmac_drbg_random(&hmac_drbg, buf, BUFSIZE));
756 #endif
757 mbedtls_hmac_drbg_free(&hmac_drbg);
758 }
759 #endif /* MBEDTLS_HMAC_DRBG_C && ( MBEDTLS_SHA1_C || MBEDTLS_SHA256_C ) */
760
761 #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
762 if (todo.rsa) {
763 int keysize;
764 mbedtls_rsa_context rsa;
765 for (keysize = 2048; keysize <= 4096; keysize *= 2) {
766 mbedtls_snprintf(title, sizeof(title), "RSA-%d", keysize);
767
768 mbedtls_rsa_init(&rsa, MBEDTLS_RSA_PKCS_V15, 0);
769 mbedtls_rsa_gen_key(&rsa, myrand, NULL, keysize, 65537);
770
771 TIME_PUBLIC(title, " public",
772 buf[0] = 0;
773 ret = mbedtls_rsa_public(&rsa, buf, buf));
774
775 TIME_PUBLIC(title, "private",
776 buf[0] = 0;
777 ret = mbedtls_rsa_private(&rsa, myrand, NULL, buf, buf));
778
779 mbedtls_rsa_free(&rsa);
780 }
781 }
782 #endif
783
784 #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_BIGNUM_C)
785 if (todo.dhm) {
786 int dhm_sizes[] = { 2048, 3072 };
787 static const unsigned char dhm_P_2048[] =
788 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
789 static const unsigned char dhm_P_3072[] =
790 MBEDTLS_DHM_RFC3526_MODP_3072_P_BIN;
791 static const unsigned char dhm_G_2048[] =
792 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
793 static const unsigned char dhm_G_3072[] =
794 MBEDTLS_DHM_RFC3526_MODP_3072_G_BIN;
795
796 const unsigned char *dhm_P[] = { dhm_P_2048, dhm_P_3072 };
797 const size_t dhm_P_size[] = { sizeof(dhm_P_2048),
798 sizeof(dhm_P_3072) };
799
800 const unsigned char *dhm_G[] = { dhm_G_2048, dhm_G_3072 };
801 const size_t dhm_G_size[] = { sizeof(dhm_G_2048),
802 sizeof(dhm_G_3072) };
803
804 mbedtls_dhm_context dhm;
805 size_t olen;
806 for (i = 0; (size_t) i < sizeof(dhm_sizes) / sizeof(dhm_sizes[0]); i++) {
807 mbedtls_dhm_init(&dhm);
808
809 if (mbedtls_mpi_read_binary(&dhm.P, dhm_P[i],
810 dhm_P_size[i]) != 0 ||
811 mbedtls_mpi_read_binary(&dhm.G, dhm_G[i],
812 dhm_G_size[i]) != 0) {
813 mbedtls_exit(1);
814 }
815
816 dhm.len = mbedtls_mpi_size(&dhm.P);
817 mbedtls_dhm_make_public(&dhm, (int) dhm.len, buf, dhm.len, myrand, NULL);
818 if (mbedtls_mpi_copy(&dhm.GY, &dhm.GX) != 0) {
819 mbedtls_exit(1);
820 }
821
822 mbedtls_snprintf(title, sizeof(title), "DHE-%d", dhm_sizes[i]);
823 TIME_PUBLIC(title, "handshake",
824 ret |= mbedtls_dhm_make_public(&dhm, (int) dhm.len, buf, dhm.len,
825 myrand, NULL);
826 ret |=
827 mbedtls_dhm_calc_secret(&dhm, buf, sizeof(buf), &olen, myrand, NULL));
828
829 mbedtls_snprintf(title, sizeof(title), "DH-%d", dhm_sizes[i]);
830 TIME_PUBLIC(title, "handshake",
831 ret |=
832 mbedtls_dhm_calc_secret(&dhm, buf, sizeof(buf), &olen, myrand, NULL));
833
834 mbedtls_dhm_free(&dhm);
835 }
836 }
837 #endif
838
839 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C)
840 if (todo.ecdsa) {
841 mbedtls_ecdsa_context ecdsa;
842 const mbedtls_ecp_curve_info *curve_info;
843 size_t sig_len;
844
845 memset(buf, 0x2A, sizeof(buf));
846
847 for (curve_info = curve_list;
848 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
849 curve_info++) {
850 if (!mbedtls_ecdsa_can_do(curve_info->grp_id)) {
851 continue;
852 }
853
854 mbedtls_ecdsa_init(&ecdsa);
855
856 if (mbedtls_ecdsa_genkey(&ecdsa, curve_info->grp_id, myrand, NULL) != 0) {
857 mbedtls_exit(1);
858 }
859 ecp_clear_precomputed(&ecdsa.grp);
860
861 mbedtls_snprintf(title, sizeof(title), "ECDSA-%s",
862 curve_info->name);
863 TIME_PUBLIC(title, "sign",
864 ret =
865 mbedtls_ecdsa_write_signature(&ecdsa, MBEDTLS_MD_SHA256, buf,
866 curve_info->bit_size,
867 tmp, &sig_len, myrand, NULL));
868
869 mbedtls_ecdsa_free(&ecdsa);
870 }
871
872 for (curve_info = curve_list;
873 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
874 curve_info++) {
875 if (!mbedtls_ecdsa_can_do(curve_info->grp_id)) {
876 continue;
877 }
878
879 mbedtls_ecdsa_init(&ecdsa);
880
881 if (mbedtls_ecdsa_genkey(&ecdsa, curve_info->grp_id, myrand, NULL) != 0 ||
882 mbedtls_ecdsa_write_signature(&ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
883 tmp, &sig_len, myrand, NULL) != 0) {
884 mbedtls_exit(1);
885 }
886 ecp_clear_precomputed(&ecdsa.grp);
887
888 mbedtls_snprintf(title, sizeof(title), "ECDSA-%s",
889 curve_info->name);
890 TIME_PUBLIC(title, "verify",
891 ret = mbedtls_ecdsa_read_signature(&ecdsa, buf, curve_info->bit_size,
892 tmp, sig_len));
893
894 mbedtls_ecdsa_free(&ecdsa);
895 }
896 }
897 #endif
898
899 #if defined(MBEDTLS_ECDH_C) && defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
900 if (todo.ecdh) {
901 mbedtls_ecdh_context ecdh;
902 mbedtls_mpi z;
903 const mbedtls_ecp_curve_info montgomery_curve_list[] = {
904 #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
905 { MBEDTLS_ECP_DP_CURVE25519, 0, 0, "Curve25519" },
906 #endif
907 #if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
908 { MBEDTLS_ECP_DP_CURVE448, 0, 0, "Curve448" },
909 #endif
910 { MBEDTLS_ECP_DP_NONE, 0, 0, 0 }
911 };
912 const mbedtls_ecp_curve_info *curve_info;
913 size_t olen;
914 const mbedtls_ecp_curve_info *selected_montgomery_curve_list =
915 montgomery_curve_list;
916
917 if (curve_list == (const mbedtls_ecp_curve_info *) &single_curve) {
918 mbedtls_ecp_group grp;
919 mbedtls_ecp_group_init(&grp);
920 if (mbedtls_ecp_group_load(&grp, curve_list->grp_id) != 0) {
921 mbedtls_exit(1);
922 }
923 if (mbedtls_ecp_get_type(&grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
924 selected_montgomery_curve_list = single_curve;
925 } else { /* empty list */
926 selected_montgomery_curve_list = single_curve + 1;
927 }
928 mbedtls_ecp_group_free(&grp);
929 }
930
931 for (curve_info = curve_list;
932 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
933 curve_info++) {
934 if (!mbedtls_ecdh_can_do(curve_info->grp_id)) {
935 continue;
936 }
937
938 mbedtls_ecdh_init(&ecdh);
939
940 CHECK_AND_CONTINUE(mbedtls_ecp_group_load(&ecdh.grp, curve_info->grp_id));
941 CHECK_AND_CONTINUE(mbedtls_ecdh_make_public(&ecdh, &olen, buf, sizeof(buf),
942 myrand, NULL));
943 CHECK_AND_CONTINUE(mbedtls_ecp_copy(&ecdh.Qp, &ecdh.Q));
944 ecp_clear_precomputed(&ecdh.grp);
945
946 mbedtls_snprintf(title, sizeof(title), "ECDHE-%s",
947 curve_info->name);
948 TIME_PUBLIC(title, "handshake",
949 CHECK_AND_CONTINUE(mbedtls_ecdh_make_public(&ecdh, &olen, buf, sizeof(buf),
950 myrand, NULL));
951 CHECK_AND_CONTINUE(mbedtls_ecdh_calc_secret(&ecdh, &olen, buf, sizeof(buf),
952 myrand, NULL)));
953 mbedtls_ecdh_free(&ecdh);
954 }
955
956 /* Montgomery curves need to be handled separately */
957 for (curve_info = selected_montgomery_curve_list;
958 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
959 curve_info++) {
960 mbedtls_ecdh_init(&ecdh);
961 mbedtls_mpi_init(&z);
962
963 CHECK_AND_CONTINUE(mbedtls_ecp_group_load(&ecdh.grp, curve_info->grp_id));
964 CHECK_AND_CONTINUE(mbedtls_ecdh_gen_public(&ecdh.grp, &ecdh.d, &ecdh.Qp, myrand, NULL));
965
966 mbedtls_snprintf(title, sizeof(title), "ECDHE-%s",
967 curve_info->name);
968 TIME_PUBLIC(title, "handshake",
969 CHECK_AND_CONTINUE(mbedtls_ecdh_gen_public(&ecdh.grp, &ecdh.d, &ecdh.Q,
970 myrand, NULL));
971 CHECK_AND_CONTINUE(mbedtls_ecdh_compute_shared(&ecdh.grp, &z, &ecdh.Qp,
972 &ecdh.d,
973 myrand, NULL)));
974
975 mbedtls_ecdh_free(&ecdh);
976 mbedtls_mpi_free(&z);
977 }
978
979 for (curve_info = curve_list;
980 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
981 curve_info++) {
982 if (!mbedtls_ecdh_can_do(curve_info->grp_id)) {
983 continue;
984 }
985
986 mbedtls_ecdh_init(&ecdh);
987
988 CHECK_AND_CONTINUE(mbedtls_ecp_group_load(&ecdh.grp, curve_info->grp_id));
989 CHECK_AND_CONTINUE(mbedtls_ecdh_make_public(&ecdh, &olen, buf, sizeof(buf),
990 myrand, NULL));
991 CHECK_AND_CONTINUE(mbedtls_ecp_copy(&ecdh.Qp, &ecdh.Q));
992 CHECK_AND_CONTINUE(mbedtls_ecdh_make_public(&ecdh, &olen, buf, sizeof(buf),
993 myrand, NULL));
994 ecp_clear_precomputed(&ecdh.grp);
995
996 mbedtls_snprintf(title, sizeof(title), "ECDH-%s",
997 curve_info->name);
998 TIME_PUBLIC(title, "handshake",
999 CHECK_AND_CONTINUE(mbedtls_ecdh_calc_secret(&ecdh, &olen, buf, sizeof(buf),
1000 myrand, NULL)));
1001 mbedtls_ecdh_free(&ecdh);
1002 }
1003
1004 /* Montgomery curves need to be handled separately */
1005 for (curve_info = selected_montgomery_curve_list;
1006 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
1007 curve_info++) {
1008 mbedtls_ecdh_init(&ecdh);
1009 mbedtls_mpi_init(&z);
1010
1011 CHECK_AND_CONTINUE(mbedtls_ecp_group_load(&ecdh.grp, curve_info->grp_id));
1012 CHECK_AND_CONTINUE(mbedtls_ecdh_gen_public(&ecdh.grp, &ecdh.d, &ecdh.Qp,
1013 myrand, NULL));
1014 CHECK_AND_CONTINUE(mbedtls_ecdh_gen_public(&ecdh.grp, &ecdh.d, &ecdh.Q, myrand, NULL));
1015
1016 mbedtls_snprintf(title, sizeof(title), "ECDH-%s",
1017 curve_info->name);
1018 TIME_PUBLIC(title, "handshake",
1019 CHECK_AND_CONTINUE(mbedtls_ecdh_compute_shared(&ecdh.grp, &z, &ecdh.Qp,
1020 &ecdh.d,
1021 myrand, NULL)));
1022
1023 mbedtls_ecdh_free(&ecdh);
1024 mbedtls_mpi_free(&z);
1025 }
1026 }
1027 #endif
1028
1029 #if defined(MBEDTLS_ECDH_C)
1030 if (todo.ecdh) {
1031 mbedtls_ecdh_context ecdh_srv, ecdh_cli;
1032 unsigned char buf_srv[BUFSIZE], buf_cli[BUFSIZE];
1033 const mbedtls_ecp_curve_info *curve_info;
1034 size_t olen;
1035
1036 for (curve_info = curve_list;
1037 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
1038 curve_info++) {
1039 if (!mbedtls_ecdh_can_do(curve_info->grp_id)) {
1040 continue;
1041 }
1042
1043 mbedtls_ecdh_init(&ecdh_srv);
1044 mbedtls_ecdh_init(&ecdh_cli);
1045
1046 mbedtls_snprintf(title, sizeof(title), "ECDHE-%s", curve_info->name);
1047 TIME_PUBLIC(title,
1048 "full handshake",
1049 const unsigned char *p_srv = buf_srv;
1050
1051 CHECK_AND_CONTINUE(mbedtls_ecdh_setup(&ecdh_srv, curve_info->grp_id));
1052 CHECK_AND_CONTINUE(mbedtls_ecdh_make_params(&ecdh_srv, &olen, buf_srv,
1053 sizeof(buf_srv), myrand, NULL));
1054
1055 CHECK_AND_CONTINUE(mbedtls_ecdh_read_params(&ecdh_cli, &p_srv,
1056 p_srv + olen));
1057 CHECK_AND_CONTINUE(mbedtls_ecdh_make_public(&ecdh_cli, &olen, buf_cli,
1058 sizeof(buf_cli), myrand, NULL));
1059
1060 CHECK_AND_CONTINUE(mbedtls_ecdh_read_public(&ecdh_srv, buf_cli, olen));
1061 CHECK_AND_CONTINUE(mbedtls_ecdh_calc_secret(&ecdh_srv, &olen, buf_srv,
1062 sizeof(buf_srv), myrand, NULL));
1063
1064 CHECK_AND_CONTINUE(mbedtls_ecdh_calc_secret(&ecdh_cli, &olen, buf_cli,
1065 sizeof(buf_cli), myrand, NULL));
1066 mbedtls_ecdh_free(&ecdh_cli);
1067
1068 mbedtls_ecdh_free(&ecdh_srv);
1069 );
1070
1071 }
1072 }
1073 #endif
1074
1075 mbedtls_printf("\n");
1076
1077 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
1078 mbedtls_memory_buffer_alloc_free();
1079 #endif
1080
1081 #if defined(_WIN32)
1082 mbedtls_printf(" Press Enter to exit this program.\n");
1083 fflush(stdout); getchar();
1084 #endif
1085
1086 mbedtls_exit(0);
1087 }
1088
1089 #endif /* MBEDTLS_TIMING_C */
1090