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 #if !defined(MBEDTLS_ALLOW_PRIVATE_ACCESS)
23 #define MBEDTLS_ALLOW_PRIVATE_ACCESS
24 #endif /* MBEDTLS_ALLOW_PRIVATE_ACCESS */
25
26 #if !defined(CONFIG_MBEDTLS_CFG_FILE)
27 #include "mbedtls/config.h"
28 #else
29 #include CONFIG_MBEDTLS_CFG_FILE
30 #endif /* CONFIG_MBEDTLS_CFG_FILE */
31
32 #if defined(MBEDTLS_PLATFORM_C)
33 #include "mbedtls/platform.h"
34 #else
35 #include <stdio.h>
36 #define mbedtls_exit exit
37 #define mbedtls_printf printf
38 #define mbedtls_snprintf snprintf
39 #define mbedtls_free free
40 #endif
41
42 #include <string.h>
43 #include <stdlib.h>
44
45 #include "mbedtls/ssl.h"
46 #include "mbedtls/debug.h"
47 #include "mbedtls/timing.h"
48 #include "mbedtls/md5.h"
49 #include "mbedtls/ripemd160.h"
50 #include "mbedtls/sha1.h"
51 #include "mbedtls/sha256.h"
52 #include "mbedtls/sha512.h"
53 #include "mbedtls/des.h"
54 #include "mbedtls/aes.h"
55 #include "mbedtls/aria.h"
56 #include "mbedtls/camellia.h"
57 #include "mbedtls/chacha20.h"
58 #include "mbedtls/gcm.h"
59 #include "mbedtls/ccm.h"
60 #include "mbedtls/chachapoly.h"
61 #include "mbedtls/cmac.h"
62 #include "mbedtls/poly1305.h"
63 #include "mbedtls/ctr_drbg.h"
64 #include "mbedtls/hmac_drbg.h"
65 #include "mbedtls/rsa.h"
66 #include "mbedtls/dhm.h"
67 #include "mbedtls/ecdsa.h"
68 #include "mbedtls/ecdh.h"
69 #include "mbedtls/error.h"
70
71 #include <zephyr/types.h>
72 #include <zephyr/sys/byteorder.h>
73 #include <zephyr/random/random.h>
74
75 #include <zephyr/kernel.h>
76
77 #include <zephyr/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 "md5, ripemd160, sha1, sha256, sha512,\n" \
143 "des3, des, camellia, 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 if (rng_state != NULL) {
248 rng_state = NULL;
249 }
250
251 sys_rand_get(output, len);
252
253 return(0);
254 }
255
256 /*
257 * Clear some memory that was used to prepare the context
258 */
259 #if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_ALLOW_PRIVATE_ACCESS)
ecp_clear_precomputed(mbedtls_ecp_group * grp)260 void ecp_clear_precomputed(mbedtls_ecp_group *grp)
261 {
262 if (grp->T != NULL) {
263 size_t i;
264
265 for (i = 0; i < grp->T_size; i++) {
266 mbedtls_ecp_point_free(&grp->T[i]);
267 }
268
269 mbedtls_free(grp->T);
270 }
271
272 grp->T = NULL;
273 grp->T_size = 0;
274 }
275 #else
276 #define ecp_clear_precomputed(g)
277 #endif
278
279 unsigned char buf[BUFSIZE];
280
281 typedef struct {
282 char md5, ripemd160, sha1, sha256, sha512, des3, des,
283 aes_cbc, aes_gcm, aes_ccm, aes_xts, chachapoly, aes_cmac,
284 des3_cmac, aria, camellia, chacha20, poly1305,
285 havege, ctr_drbg, hmac_drbg, rsa, dhm, ecdsa, ecdh;
286 } todo_list;
287
main(void)288 int main(void)
289 {
290 mbedtls_ssl_config conf;
291 unsigned char tmp[200];
292 char title[TITLE_LEN];
293 todo_list todo;
294 int i;
295
296 printk("\tMBEDTLS Benchmark sample\n");
297
298 #if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
299 mbedtls_platform_set_printf(MBEDTLS_PRINT);
300 #endif
301 mbedtls_ssl_conf_dbg(&conf, my_debug, NULL);
302
303 k_work_init_delayable(&mbedtls_alarm, mbedtls_alarm_timeout);
304 memset(&todo, 1, sizeof(todo));
305
306 memset(buf, 0xAA, sizeof(buf));
307 memset(tmp, 0xBB, sizeof(tmp));
308
309
310 #if defined(MBEDTLS_MD5_C)
311 if (todo.md5) {
312 TIME_AND_TSC("MD5", mbedtls_md5(buf, BUFSIZE, tmp));
313 }
314 #endif
315
316 #if defined(MBEDTLS_RIPEMD160_C)
317 if (todo.ripemd160) {
318 TIME_AND_TSC("RIPEMD160",
319 mbedtls_ripemd160(buf, BUFSIZE, tmp));
320 }
321 #endif
322
323 #if defined(MBEDTLS_SHA1_C)
324 if (todo.sha1) {
325 TIME_AND_TSC("SHA-1", mbedtls_sha1(buf, BUFSIZE, tmp));
326 }
327 #endif
328
329 #if defined(MBEDTLS_SHA256_C)
330 if (todo.sha256) {
331 TIME_AND_TSC("SHA-256", mbedtls_sha256(buf,
332 BUFSIZE, tmp, 0));
333 }
334 #endif
335
336 #if defined(MBEDTLS_SHA512_C)
337 if (todo.sha512) {
338 TIME_AND_TSC("SHA-512", mbedtls_sha512(buf,
339 BUFSIZE, tmp, 0));
340 }
341 #endif
342
343 #if defined(MBEDTLS_DES_C)
344 #if defined(MBEDTLS_CIPHER_MODE_CBC)
345 if (todo.des3) {
346 mbedtls_des3_context des3;
347
348 mbedtls_des3_init(&des3);
349 mbedtls_des3_set3key_enc(&des3, tmp);
350
351 TIME_AND_TSC("3DES",
352 mbedtls_des3_crypt_cbc(
353 &des3,
354 MBEDTLS_DES_ENCRYPT,
355 BUFSIZE, tmp, buf, buf));
356 mbedtls_des3_free(&des3);
357 }
358
359 if (todo.des) {
360 mbedtls_des_context des;
361
362 mbedtls_des_init(&des);
363 mbedtls_des_setkey_enc(&des, tmp);
364
365 TIME_AND_TSC("DES",
366 mbedtls_des_crypt_cbc(&des,
367 MBEDTLS_DES_ENCRYPT,
368 BUFSIZE, tmp, buf, buf));
369 mbedtls_des_free(&des);
370
371 }
372 #endif /* MBEDTLS_CIPHER_MODE_CBC */
373 #if defined(MBEDTLS_CMAC_C)
374 if (todo.des3_cmac) {
375 unsigned char output[8];
376 const mbedtls_cipher_info_t *cipher_info;
377
378 memset(buf, 0, sizeof(buf));
379 memset(tmp, 0, sizeof(tmp));
380
381 cipher_info = mbedtls_cipher_info_from_type(
382 MBEDTLS_CIPHER_DES_EDE3_ECB);
383
384 TIME_AND_TSC("3DES-CMAC",
385 mbedtls_cipher_cmac(cipher_info, tmp, 192, buf,
386 BUFSIZE, output));
387 }
388 #endif /* MBEDTLS_CMAC_C */
389 #endif /* MBEDTLS_DES_C */
390
391 #if defined(MBEDTLS_AES_C)
392 #if defined(MBEDTLS_CIPHER_MODE_CBC)
393 if (todo.aes_cbc) {
394 int keysize;
395 mbedtls_aes_context aes;
396
397 mbedtls_aes_init(&aes);
398
399 for (keysize = 128; keysize <= 256; keysize += 64) {
400 snprintk(title, sizeof(title),
401 "AES-CBC-%d", keysize);
402
403 memset(buf, 0, sizeof(buf));
404 memset(tmp, 0, sizeof(tmp));
405 mbedtls_aes_setkey_enc(&aes, tmp, keysize);
406
407 TIME_AND_TSC(title,
408 mbedtls_aes_crypt_cbc(&aes,
409 MBEDTLS_AES_ENCRYPT,
410 BUFSIZE, tmp, buf, buf));
411 }
412
413 mbedtls_aes_free(&aes);
414 }
415 #endif
416 #if defined(MBEDTLS_CIPHER_MODE_XTS)
417 if (todo.aes_xts) {
418 int keysize;
419 mbedtls_aes_xts_context ctx;
420
421 mbedtls_aes_xts_init(&ctx);
422
423 for (keysize = 128; keysize <= 256; keysize += 128) {
424 snprintk(title, sizeof(title),
425 "AES-XTS-%d", keysize);
426
427 memset(buf, 0, sizeof(buf));
428 memset(tmp, 0, sizeof(tmp));
429
430 mbedtls_aes_xts_setkey_enc(&ctx, tmp, keysize * 2);
431
432 TIME_AND_TSC(title,
433 mbedtls_aes_crypt_xts(&ctx,
434 MBEDTLS_AES_ENCRYPT, BUFSIZE,
435 tmp, buf, buf));
436
437 mbedtls_aes_xts_free(&ctx);
438 }
439 }
440 #endif
441 #if defined(MBEDTLS_GCM_C)
442 if (todo.aes_gcm) {
443 int keysize;
444 mbedtls_gcm_context gcm;
445
446 mbedtls_gcm_init(&gcm);
447
448 for (keysize = 128; keysize <= 256; keysize += 64) {
449 snprintk(title, sizeof(title), "AES-GCM-%d",
450 keysize);
451
452 memset(buf, 0, sizeof(buf));
453 memset(tmp, 0, sizeof(tmp));
454 mbedtls_gcm_setkey(&gcm, MBEDTLS_CIPHER_ID_AES, tmp,
455 keysize);
456
457 TIME_AND_TSC(title,
458 mbedtls_gcm_crypt_and_tag(&gcm,
459 MBEDTLS_GCM_ENCRYPT,
460 BUFSIZE, tmp,
461 12, NULL, 0, buf, buf,
462 16, tmp));
463 mbedtls_gcm_free(&gcm);
464 }
465 }
466 #endif
467 #if defined(MBEDTLS_CCM_C)
468 if (todo.aes_ccm) {
469 int keysize;
470 mbedtls_ccm_context ccm;
471
472 mbedtls_ccm_init(&ccm);
473
474 for (keysize = 128; keysize <= 256; keysize += 64) {
475 snprintk(title, sizeof(title), "AES-CCM-%d",
476 keysize);
477
478 memset(buf, 0, sizeof(buf));
479 memset(tmp, 0, sizeof(tmp));
480 mbedtls_ccm_setkey(&ccm, MBEDTLS_CIPHER_ID_AES, tmp,
481 keysize);
482
483 TIME_AND_TSC(title,
484 mbedtls_ccm_encrypt_and_tag(&ccm, BUFSIZE,
485 tmp, 12, NULL, 0, buf,
486 buf, tmp, 16));
487
488 mbedtls_ccm_free(&ccm);
489 }
490 }
491 #endif
492 #if defined(MBEDTLS_CHACHAPOLY_C)
493 if (todo.chachapoly) {
494 mbedtls_chachapoly_context chachapoly;
495
496 mbedtls_chachapoly_init(&chachapoly);
497
498 memset(buf, 0, sizeof(buf));
499 memset(tmp, 0, sizeof(tmp));
500
501 snprintk(title, sizeof(title), "ChaCha20-Poly1305");
502
503 mbedtls_chachapoly_setkey(&chachapoly, tmp);
504
505 TIME_AND_TSC(title,
506 mbedtls_chachapoly_encrypt_and_tag(&chachapoly,
507 BUFSIZE, tmp, NULL, 0, buf, buf, tmp));
508
509 mbedtls_chachapoly_free(&chachapoly);
510 }
511 #endif
512 #if defined(MBEDTLS_CMAC_C)
513 if (todo.aes_cmac) {
514 unsigned char output[16];
515 const mbedtls_cipher_info_t *cipher_info;
516 mbedtls_cipher_type_t cipher_type;
517 int keysize;
518
519 for (keysize = 128, cipher_type = MBEDTLS_CIPHER_AES_128_ECB;
520 keysize <= 256; keysize += 64, cipher_type++) {
521 snprintk(title, sizeof(title), "AES-CMAC-%d",
522 keysize);
523
524 memset(buf, 0, sizeof(buf));
525 memset(tmp, 0, sizeof(tmp));
526
527 cipher_info = mbedtls_cipher_info_from_type(
528 cipher_type);
529
530 TIME_AND_TSC(title,
531 mbedtls_cipher_cmac(cipher_info,
532 tmp, keysize,
533 buf, BUFSIZE,
534 output));
535 }
536
537 memset(buf, 0, sizeof(buf));
538 memset(tmp, 0, sizeof(tmp));
539
540 TIME_AND_TSC("AES-CMAC-PRF-128",
541 mbedtls_aes_cmac_prf_128(tmp, 16, buf, BUFSIZE,
542 output));
543 }
544 #endif /* MBEDTLS_CMAC_C */
545 #endif /* MBEDTLS_AES_C */
546
547 #if defined(MBEDTLS_ARIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
548 if (todo.aria) {
549 int keysize;
550 mbedtls_aria_context aria;
551
552 mbedtls_aria_init(&aria);
553
554 for (keysize = 128; keysize <= 256; keysize += 64) {
555 snprintk(title, sizeof(title),
556 "ARIA-CBC-%d", keysize);
557
558 memset(buf, 0, sizeof(buf));
559 memset(tmp, 0, sizeof(tmp));
560
561 mbedtls_aria_setkey_enc(&aria, tmp, keysize);
562
563 TIME_AND_TSC(title,
564 mbedtls_aria_crypt_cbc(&aria,
565 MBEDTLS_ARIA_ENCRYPT,
566 BUFSIZE, tmp, buf, buf));
567 }
568
569 mbedtls_aria_free(&aria);
570 }
571 #endif
572
573 #if defined(MBEDTLS_CAMELLIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
574 if (todo.camellia) {
575 int keysize;
576 mbedtls_camellia_context camellia;
577
578 mbedtls_camellia_init(&camellia);
579
580 for (keysize = 128; keysize <= 256; keysize += 64) {
581 snprintk(title, sizeof(title),
582 "CAMELLIA-CBC-%d", keysize);
583
584 memset(buf, 0, sizeof(buf));
585 memset(tmp, 0, sizeof(tmp));
586
587 mbedtls_camellia_setkey_enc(&camellia, tmp, keysize);
588
589 TIME_AND_TSC(title,
590 mbedtls_camellia_crypt_cbc(&camellia,
591 MBEDTLS_CAMELLIA_ENCRYPT,
592 BUFSIZE, tmp, buf, buf));
593 }
594
595 mbedtls_camellia_free(&camellia);
596 }
597 #endif
598
599 #if defined(MBEDTLS_CHACHA20_C)
600 if (todo.chacha20) {
601 TIME_AND_TSC("ChaCha20", mbedtls_chacha20_crypt(
602 buf, buf, 0U, BUFSIZE,
603 buf, buf));
604 }
605 #endif
606
607 #if defined(MBEDTLS_POLY1305_C)
608 if (todo.poly1305) {
609 TIME_AND_TSC("Poly1305", mbedtls_poly1305_mac(
610 buf, buf, BUFSIZE,
611 buf));
612 }
613 #endif
614
615 #if defined(MBEDTLS_HAVEGE_C)
616 if (todo.havege) {
617 mbedtls_havege_state hs;
618
619 mbedtls_havege_init(&hs);
620
621 TIME_AND_TSC("HAVEGE", mbedtls_havege_random(&hs,
622 buf, BUFSIZE));
623 mbedtls_havege_free(&hs);
624 }
625 #endif
626
627 #if defined(MBEDTLS_CTR_DRBG_C)
628 if (todo.ctr_drbg) {
629 mbedtls_ctr_drbg_context ctr_drbg;
630
631 mbedtls_ctr_drbg_init(&ctr_drbg);
632
633 if (mbedtls_ctr_drbg_seed(&ctr_drbg, myrand,
634 NULL, NULL, 0) != 0) {
635 mbedtls_exit(1);
636 }
637
638 TIME_AND_TSC("CTR_DRBG (NOPR)",
639 mbedtls_ctr_drbg_random(&ctr_drbg, buf, BUFSIZE));
640
641 if (mbedtls_ctr_drbg_seed(&ctr_drbg, myrand,
642 NULL, NULL, 0) != 0) {
643 mbedtls_exit(1);
644 }
645
646 mbedtls_ctr_drbg_set_prediction_resistance(&ctr_drbg,
647 MBEDTLS_CTR_DRBG_PR_ON);
648
649 TIME_AND_TSC("CTR_DRBG (PR)",
650 mbedtls_ctr_drbg_random(&ctr_drbg, buf, BUFSIZE));
651
652 mbedtls_ctr_drbg_free(&ctr_drbg);
653 }
654 #endif
655
656 #if defined(MBEDTLS_HMAC_DRBG_C)
657 if (todo.hmac_drbg) {
658 mbedtls_hmac_drbg_context hmac_drbg;
659 const mbedtls_md_info_t *md_info;
660
661 mbedtls_hmac_drbg_init(&hmac_drbg);
662
663 #if defined(MBEDTLS_SHA1_C)
664 md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA1);
665 if (md_info == NULL) {
666 mbedtls_exit(1);
667 }
668
669 if (mbedtls_hmac_drbg_seed(&hmac_drbg, md_info,
670 myrand, NULL, NULL, 0) != 0) {
671 mbedtls_exit(1);
672 }
673
674 TIME_AND_TSC("HMAC_DRBG SHA-1 (NOPR)",
675 mbedtls_hmac_drbg_random(&hmac_drbg, buf,
676 BUFSIZE));
677
678 if (mbedtls_hmac_drbg_seed(&hmac_drbg, md_info, myrand,
679 NULL, NULL, 0) != 0) {
680 mbedtls_exit(1);
681 }
682
683 mbedtls_hmac_drbg_set_prediction_resistance(&hmac_drbg,
684 MBEDTLS_HMAC_DRBG_PR_ON);
685
686 TIME_AND_TSC("HMAC_DRBG SHA-1 (PR)",
687 mbedtls_hmac_drbg_random(&hmac_drbg, buf,
688 BUFSIZE));
689 #endif
690
691 #if defined(MBEDTLS_SHA256_C)
692 md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256);
693 if (md_info == NULL) {
694 mbedtls_exit(1);
695 }
696
697 if (mbedtls_hmac_drbg_seed(&hmac_drbg, md_info,
698 myrand, NULL, NULL, 0) != 0) {
699 mbedtls_exit(1);
700 }
701
702 TIME_AND_TSC("HMAC_DRBG SHA-256 (NOPR)",
703 mbedtls_hmac_drbg_random(&hmac_drbg, buf,
704 BUFSIZE));
705
706 if (mbedtls_hmac_drbg_seed(&hmac_drbg, md_info, myrand,
707 NULL, NULL, 0) != 0) {
708 mbedtls_exit(1);
709 }
710
711 mbedtls_hmac_drbg_set_prediction_resistance(&hmac_drbg,
712 MBEDTLS_HMAC_DRBG_PR_ON);
713
714 TIME_AND_TSC("HMAC_DRBG SHA-256 (PR)",
715 mbedtls_hmac_drbg_random(&hmac_drbg, buf,
716 BUFSIZE));
717 #endif
718 mbedtls_hmac_drbg_free(&hmac_drbg);
719 }
720 #endif
721
722 #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
723 if (todo.rsa) {
724 int keysize;
725 mbedtls_rsa_context rsa;
726
727 for (keysize = 2048; keysize <= 4096; keysize *= 2) {
728 snprintk(title, sizeof(title), "RSA-%d",
729 keysize);
730
731 mbedtls_rsa_init(&rsa);
732 mbedtls_rsa_gen_key(&rsa, myrand, NULL, keysize,
733 65537);
734
735 TIME_PUBLIC(title, " public",
736 buf[0] = 0;
737 ret = mbedtls_rsa_public(&rsa, buf, buf));
738
739 TIME_PUBLIC(title, "private",
740 buf[0] = 0;
741 ret = mbedtls_rsa_private(&rsa, myrand,
742 NULL, buf, buf));
743
744 mbedtls_rsa_free(&rsa);
745 }
746 }
747 #endif
748
749 #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_BIGNUM_C)
750 if (todo.dhm) {
751 int dhm_sizes[] = {2048, 3072};
752 static const unsigned char dhm_P_2048[] =
753 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
754 static const unsigned char dhm_P_3072[] =
755 MBEDTLS_DHM_RFC3526_MODP_3072_P_BIN;
756 static const unsigned char dhm_G_2048[] =
757 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
758 static const unsigned char dhm_G_3072[] =
759 MBEDTLS_DHM_RFC3526_MODP_3072_G_BIN;
760
761 const unsigned char *dhm_P[] = { dhm_P_2048, dhm_P_3072 };
762 const size_t dhm_P_size[] = { sizeof(dhm_P_2048),
763 sizeof(dhm_P_3072) };
764 const unsigned char *dhm_G[] = { dhm_G_2048, dhm_G_3072 };
765 const size_t dhm_G_size[] = { sizeof(dhm_G_2048),
766 sizeof(dhm_G_3072) };
767 mbedtls_dhm_context dhm;
768 size_t olen;
769 size_t n;
770
771 for (i = 0; i < ARRAY_SIZE(dhm_sizes); i++) {
772 mbedtls_dhm_init(&dhm);
773
774 if (mbedtls_mpi_read_binary(&dhm.P, dhm_P[i],
775 dhm_P_size[i]) != 0 ||
776 mbedtls_mpi_read_binary(&dhm.G, dhm_G[i],
777 dhm_G_size[i]) != 0) {
778 mbedtls_exit(1);
779 }
780
781 n = mbedtls_mpi_size(&dhm.P);
782 mbedtls_dhm_make_public(&dhm, (int)n, buf,
783 n, myrand, NULL);
784 if (mbedtls_mpi_copy(&dhm.GY, &dhm.GX) != 0) {
785 mbedtls_exit(1);
786 }
787
788 snprintk(title, sizeof(title), "DHE-%d", dhm_sizes[i]);
789
790 TIME_PUBLIC(title, "handshake",
791 ret |= mbedtls_dhm_make_public(&dhm,
792 (int)n, buf, n,
793 myrand, NULL);
794 ret |= mbedtls_dhm_calc_secret(&dhm, buf,
795 sizeof(buf), &olen, myrand,
796 NULL));
797
798 snprintk(title, sizeof(title), "DH-%d", dhm_sizes[i]);
799
800 TIME_PUBLIC(title, "handshake",
801 ret |= mbedtls_dhm_calc_secret(&dhm, buf,
802 sizeof(buf), &olen, myrand,
803 NULL));
804
805 mbedtls_dhm_free(&dhm);
806 }
807 }
808 #endif
809
810 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C)
811 if (todo.ecdsa) {
812 size_t sig_len;
813 const mbedtls_ecp_curve_info *curve_info;
814 mbedtls_ecdsa_context ecdsa;
815
816 memset(buf, 0x2A, sizeof(buf));
817
818 for (curve_info = mbedtls_ecp_curve_list();
819 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
820 curve_info++) {
821 mbedtls_ecdsa_init(&ecdsa);
822
823 if (mbedtls_ecdsa_genkey(&ecdsa, curve_info->grp_id,
824 myrand, NULL) != 0) {
825 mbedtls_exit(1);
826 }
827
828 ecp_clear_precomputed(&ecdsa.grp);
829
830 snprintk(title, sizeof(title), "ECDSA-%s",
831 curve_info->name);
832
833 TIME_PUBLIC(title, "sign",
834 ret = mbedtls_ecdsa_write_signature(
835 &ecdsa, MBEDTLS_MD_SHA256,
836 buf, curve_info->bit_size,
837 tmp, sizeof(tmp), &sig_len,
838 myrand, NULL));
839
840 mbedtls_ecdsa_free(&ecdsa);
841 }
842
843 for (curve_info = mbedtls_ecp_curve_list();
844 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
845 curve_info++) {
846 mbedtls_ecdsa_init(&ecdsa);
847
848 if (mbedtls_ecdsa_genkey(&ecdsa, curve_info->grp_id,
849 myrand, NULL) != 0 ||
850 mbedtls_ecdsa_write_signature(&ecdsa,
851 MBEDTLS_MD_SHA256, buf,
852 curve_info->bit_size,
853 tmp, sizeof(tmp),
854 &sig_len, myrand,
855 NULL) != 0) {
856 mbedtls_exit(1);
857 }
858
859 ecp_clear_precomputed(&ecdsa.grp);
860
861 snprintk(title, sizeof(title), "ECDSA-%s",
862 curve_info->name);
863
864 TIME_PUBLIC(title, "verify",
865 ret = mbedtls_ecdsa_read_signature(&ecdsa,
866 buf, curve_info->bit_size,
867 tmp, sig_len));
868
869 mbedtls_ecdsa_free(&ecdsa);
870 }
871 }
872 #endif
873
874 #if defined(MBEDTLS_ECDH_C) && defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
875 if (todo.ecdh) {
876 mbedtls_ecdh_context ecdh;
877 mbedtls_mpi z;
878 const mbedtls_ecp_curve_info montgomery_curve_list[] = {
879 #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
880 { MBEDTLS_ECP_DP_CURVE25519, 0, 0, "Curve25519" },
881 #endif
882 #if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
883 { MBEDTLS_ECP_DP_CURVE448, 0, 0, "Curve448" },
884 #endif
885 { MBEDTLS_ECP_DP_NONE, 0, 0, 0 }
886 };
887 const mbedtls_ecp_curve_info *curve_info;
888 size_t olen;
889
890 for (curve_info = mbedtls_ecp_curve_list();
891 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
892 curve_info++) {
893 mbedtls_ecdh_init(&ecdh);
894
895 if (mbedtls_ecp_group_load(&ecdh.grp,
896 curve_info->grp_id) != 0 ||
897 mbedtls_ecdh_make_public(&ecdh, &olen, buf,
898 sizeof(buf),
899 myrand, NULL) != 0 ||
900 mbedtls_ecp_copy(&ecdh.Qp, &ecdh.Q) != 0) {
901 mbedtls_exit(1);
902 }
903
904 ecp_clear_precomputed(&ecdh.grp);
905
906 snprintk(title, sizeof(title), "ECDHE-%s",
907 curve_info->name);
908
909 TIME_PUBLIC(title, "handshake",
910 ret |= mbedtls_ecdh_make_public(&ecdh,
911 &olen, buf, sizeof(buf),
912 myrand, NULL);
913 ret |= mbedtls_ecdh_calc_secret(&ecdh,
914 &olen, buf, sizeof(buf),
915 myrand, NULL));
916 mbedtls_ecdh_free(&ecdh);
917 }
918
919 /* Montgomery curves need to be handled separately */
920 for (curve_info = montgomery_curve_list;
921 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
922 curve_info++) {
923 mbedtls_ecdh_init(&ecdh);
924 mbedtls_mpi_init(&z);
925
926 if (mbedtls_ecp_group_load(&ecdh.grp,
927 curve_info->grp_id) != 0 ||
928 mbedtls_ecdh_gen_public(&ecdh.grp, &ecdh.d,
929 &ecdh.Qp, myrand, NULL)
930 != 0) {
931 mbedtls_exit(1);
932 }
933
934 snprintk(title, sizeof(title), "ECDHE-%s",
935 curve_info->name);
936
937 TIME_PUBLIC(title, "handshake",
938 ret |= mbedtls_ecdh_gen_public(&ecdh.grp,
939 &ecdh.d,
940 &ecdh.Q,
941 myrand,
942 NULL);
943 ret |= mbedtls_ecdh_compute_shared(
944 &ecdh.grp, &z,
945 &ecdh.Qp,
946 &ecdh.d,
947 myrand,
948 NULL));
949 mbedtls_ecdh_free(&ecdh);
950 mbedtls_mpi_free(&z);
951 }
952
953 for (curve_info = mbedtls_ecp_curve_list();
954 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
955 curve_info++) {
956 mbedtls_ecdh_init(&ecdh);
957
958 if (mbedtls_ecp_group_load(&ecdh.grp,
959 curve_info->grp_id) != 0 ||
960 mbedtls_ecdh_make_public(&ecdh, &olen, buf,
961 sizeof(buf), myrand,
962 NULL) != 0 ||
963 mbedtls_ecp_copy(&ecdh.Qp, &ecdh.Q) != 0 ||
964 mbedtls_ecdh_make_public(&ecdh, &olen, buf,
965 sizeof(buf), myrand,
966 NULL) != 0) {
967 mbedtls_exit(1);
968 }
969
970 ecp_clear_precomputed(&ecdh.grp);
971
972 snprintk(title, sizeof(title), "ECDH-%s",
973 curve_info->name);
974
975 TIME_PUBLIC(title, "handshake",
976 ret |= mbedtls_ecdh_calc_secret(&ecdh,
977 &olen,
978 buf, sizeof(buf),
979 myrand, NULL));
980 mbedtls_ecdh_free(&ecdh);
981 }
982
983 /* Montgomery curves need to be handled separately */
984 for (curve_info = montgomery_curve_list;
985 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
986 curve_info++) {
987 mbedtls_ecdh_init(&ecdh);
988 mbedtls_mpi_init(&z);
989
990 if (mbedtls_ecp_group_load(&ecdh.grp,
991 curve_info->grp_id) != 0 ||
992 mbedtls_ecdh_gen_public(&ecdh.grp, &ecdh.d,
993 &ecdh.Qp, myrand,
994 NULL) != 0 ||
995 mbedtls_ecdh_gen_public(&ecdh.grp, &ecdh.d,
996 &ecdh.Q, myrand,
997 NULL) != 0) {
998 mbedtls_exit(1);
999 }
1000
1001 snprintk(title, sizeof(title), "ECDH-%s",
1002 curve_info->name);
1003
1004 TIME_PUBLIC(title, "handshake",
1005 ret |= mbedtls_ecdh_compute_shared(
1006 &ecdh.grp,
1007 &z, &ecdh.Qp,
1008 &ecdh.d,
1009 myrand,
1010 NULL));
1011
1012 mbedtls_ecdh_free(&ecdh);
1013 mbedtls_mpi_free(&z);
1014 }
1015 }
1016 #endif
1017 mbedtls_printf("\n Done\n");
1018 return 0;
1019 }
1020