51 broke -E logic completely, rewise it
[tfcrypt.git] / tfc_bench.c
1 /*
2  * tfcrypt -- high security Threefish encryption tool.
3  *
4  * tfcrypt is copyrighted:
5  * Copyright (C) 2012-2019 Andrey Rys. All rights reserved.
6  *
7  * tfcrypt is licensed to you under the terms of std. MIT/X11 license:
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining
10  * a copy of this software and associated documentation files (the
11  * "Software"), to deal in the Software without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
24  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27  */
28
29 #include "tfcrypt.h"
30
31 void do_benchmark(tfc_useconds useconds, double dseconds)
32 {
33         size_t x, lblock;
34
35         for (x = 1; x < NSIG; x++) signal(x, SIG_IGN);
36         memset(&sigact, 0, sizeof(sigact));
37         sigact.sa_flags = SA_RESTART;
38         sigact.sa_handler = print_crypt_status;
39         sigaction(SIGINT, &sigact, NULL);
40         sigaction(SIGTERM, &sigact, NULL);
41         sigaction(SIGTSTP, &sigact, NULL);
42         sigaction(SIGALRM, &sigact, NULL);
43         setup_next_alarm(useconds);
44         memset(&sigact, 0, sizeof(struct sigaction));
45
46         tfc_getcurtime(&delta_time);
47
48         tfc_getrandom(key, sizeof(key));
49         tfc_getrandom(ctr, sizeof(ctr));
50         if (do_mac != NO) {
51                 tfc_getrandom(mackey, sizeof(mackey));
52                 skein_init_key(&sk, mackey, macbits);
53         }
54         if (ctr_mode == TFC_MODE_STREAM) tfe_init_iv(&tfe, key, ctr);
55         if (ctr_mode == TFC_MODE_XTS) tfc_getrandom(xtskey, sizeof(xtskey));
56
57         tfc_nfsay(stdout, "%s: doing %s benchmark for %.4f seconds ... ", tfc_format_pid(progname), tfc_modename(ctr_mode), dseconds);
58
59         do_stop = NO;
60         while (1) {
61                 if (do_stop) break;
62
63                 lblock = blk_len_adj(NOFSIZE, total_processed_src, blksize);
64                 total_processed_src += lblock;
65
66                 if (do_mac != NO) skein_update(&sk, srcblk, lblock);
67
68                 if (ctr_mode == TFC_MODE_CTR) tf_ctr_crypt(key, ctr, srcblk, srcblk, lblock);
69                 else if (ctr_mode == TFC_MODE_STREAM) tf_stream_crypt(&tfe, srcblk, srcblk, lblock);
70                 else if (ctr_mode == TFC_MODE_XTS && do_edcrypt == TFC_DO_ENCRYPT)
71                         tf_xts_encrypt(key, xtskey, ctr, srcblk, srcblk, lblock, xtsblocks);
72                 else if (ctr_mode == TFC_MODE_XTS && do_edcrypt == TFC_DO_DECRYPT)
73                         tf_xts_decrypt(key, xtskey, ctr, srcblk, srcblk, lblock, xtsblocks);
74                 else if (ctr_mode == TFC_MODE_ECB && do_edcrypt == TFC_DO_ENCRYPT)
75                         tf_ecb_encrypt(key, srcblk, srcblk, lblock);
76                 else if (ctr_mode == TFC_MODE_ECB && do_edcrypt == TFC_DO_DECRYPT)
77                         tf_ecb_decrypt(key, srcblk, srcblk, lblock);
78                 else if (ctr_mode == TFC_MODE_CBC && do_edcrypt == TFC_DO_ENCRYPT)
79                         tf_cbc_encrypt(key, ctr, srcblk, srcblk, lblock);
80                 else if (ctr_mode == TFC_MODE_CBC && do_edcrypt == TFC_DO_DECRYPT)
81                         tf_cbc_decrypt(key, ctr, srcblk, srcblk, lblock);
82                 else if (ctr_mode == TFC_MODE_PCBC && do_edcrypt == TFC_DO_ENCRYPT)
83                         tf_pcbc_encrypt(key, ctr, srcblk, srcblk, lblock);
84                 else if (ctr_mode == TFC_MODE_PCBC && do_edcrypt == TFC_DO_DECRYPT)
85                         tf_pcbc_decrypt(key, ctr, srcblk, srcblk, lblock);
86
87                 delta_processed += lblock;
88         }
89 }