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