]> git.cryptolib.org Git - avr-crypto-lib.git/blob - bcal-performance.c
changing performance measurment of blockciphers to bcal
[avr-crypto-lib.git] / bcal-performance.c
1 /* bcal-performance.c */
2 /*
3     This file is part of the AVR-Crypto-Lib.
4     Copyright (C) 2010 Daniel Otte (daniel.otte@rub.de)
5
6     This program is free software: you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation, either version 3 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 /*
21  * \file    bcal-performance.c
22  * \author  Daniel Otte
23  * \email   daniel.otte@rub.de
24  * \date    2010-02-16
25  * \license GPLv3 or later
26  *
27  */
28
29 #include "bcal-performance.h"
30 #include "keysize_descriptor.h"
31 #include "blockcipher_descriptor.h"
32 #include "performance_test.h"
33 #include "cli.h"
34 #include <stdint.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <avr/pgmspace.h>
38
39
40
41
42 static
43 void printvalue(unsigned long v){
44         char str[20];
45         int i;
46         ultoa(v, str, 10);
47         for(i=0; i<10-strlen(str); ++i){
48                 cli_putc(' ');
49         }
50         cli_putstr(str);
51 }
52
53 void bcal_performance(const bcdesc_t* bcd){
54         bcdesc_t bc;
55         memcpy_P(&bc, bcd, sizeof(bcdesc_t));
56         uint8_t ctx[bc.ctxsize_B];
57         uint8_t data[(bc.blocksize_b+7)/8];
58         uint16_t keysize = get_keysize(bc.valid_keysize_desc);
59         uint8_t key[(keysize+7)/8];
60         uint64_t t;
61         uint8_t i;
62
63         if(bc.type!=BCDESC_TYPE_BLOCKCIPHER)
64                 return;
65         calibrateTimer();
66         print_overhead();
67         cli_putstr_P(PSTR("\r\n\r\n === "));
68         cli_putstr_P(bc.name);
69         cli_putstr_P(PSTR(" performance === "
70                           "\r\n    type:             blockcipher"
71                           "\r\n    keysize (bits):     "));
72         printvalue(keysize);
73
74         cli_putstr_P(PSTR("\r\n    ctxsize (bytes):    "));
75         printvalue(bc.ctxsize_B);
76
77         cli_putstr_P(PSTR("\r\n    blocksize (bits):   "));
78         printvalue(bc.blocksize_b);
79
80
81
82         t=0;
83         if(bc.init.init1){
84                 if((bc.flags&BC_INIT_TYPE)==BC_INIT_TYPE_1){
85                         for(i=0; i<32; ++i){
86                                 startTimer(0);
87                                 START_TIMER;
88                                 (bc.init.init1)(key, &ctx);
89                                 STOP_TIMER;
90                                 t += stopTimer();
91                                 if(i!=31 && bc.free){
92                                         bc.free(&ctx);
93                                 }
94                         }
95                 } else {
96                         for(i=0; i<32; ++i){
97                                 startTimer(0);
98                                 START_TIMER;
99                                 (bc.init.init2)(key, keysize, &ctx);
100                                 STOP_TIMER;
101                                 t += stopTimer();
102                                 if(i!=31 && bc.free){
103                                         bc.free(&ctx);
104                                 }
105                         }
106                 }
107                 t>>=5;
108                 cli_putstr_P(PSTR("\r\n    init (cycles):      "));
109                 printvalue(t);
110         }
111         t=0;
112         for(i=0; i<32; ++i){
113                 startTimer(0);
114                 START_TIMER;
115                 bc.enc.enc1(data, &ctx);
116                 STOP_TIMER;
117                 t += stopTimer();
118         }
119         t>>=5;
120         cli_putstr_P(PSTR("\r\n    encrypt (cycles):   "));
121         printvalue(t);
122
123         t=0;
124         for(i=0; i<32; ++i){
125                 startTimer(0);
126                 START_TIMER;
127                 bc.dec.dec1(data, &ctx);
128                 STOP_TIMER;
129                 t += stopTimer();
130         }
131         t>>=5;
132         cli_putstr_P(PSTR("\r\n    decrypt (cycles):   "));
133         printvalue(t);
134
135         if(bc.free){
136                 bc.free(&ctx);
137         }
138 }
139
140
141 void bcal_performance_multiple(const bcdesc_t** bcd_list){
142         const bcdesc_t* bcd;
143         for(;;){
144                 bcd = (void*)pgm_read_word(bcd_list);
145                 if(!bcd){
146                         cli_putstr_P(PSTR("\r\n\r\n End of performance figures\r\n"));
147                         return;
148                 }
149                 bcal_performance(bcd);
150                 bcd_list = (void*)((uint8_t*)bcd_list + 2);
151         }
152 }