]> git.cryptolib.org Git - avr-crypto-lib.git/blob - bcal-performance.c
36cc82317948677733cee2f3b8f58dc61d6ff1c6
[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 "stack_measuring.h"
34 #include "cli.h"
35 #include <stdint.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <avr/pgmspace.h>
39
40 #define PATTERN_A 0xAA
41 #define PATTERN_B 0x55
42
43
44 static
45 void printvalue(unsigned long v){
46         char str[20];
47         int i;
48         ultoa(v, str, 10);
49         for(i=0; i<10-strlen(str); ++i){
50                 cli_putc(' ');
51         }
52         cli_putstr(str);
53 }
54
55 void bcal_performance(const bcdesc_t* bcd){
56         bcdesc_t bc;
57         memcpy_P(&bc, bcd, sizeof(bcdesc_t));
58         uint8_t ctx[bc.ctxsize_B];
59         uint8_t data[(bc.blocksize_b+7)/8];
60         uint16_t keysize = get_keysize(bc.valid_keysize_desc);
61         uint8_t key[(keysize+7)/8];
62         uint64_t t;
63         uint8_t i;
64
65         if(bc.type!=BCDESC_TYPE_BLOCKCIPHER)
66                 return;
67         calibrateTimer();
68         print_overhead();
69         cli_putstr_P(PSTR("\r\n\r\n === "));
70         cli_putstr_P(bc.name);
71         cli_putstr_P(PSTR(" performance === "
72                           "\r\n    type:             blockcipher"
73                           "\r\n    keysize (bits):     "));
74         printvalue(keysize);
75
76         cli_putstr_P(PSTR("\r\n    ctxsize (bytes):    "));
77         printvalue(bc.ctxsize_B);
78
79         cli_putstr_P(PSTR("\r\n    blocksize (bits):   "));
80         printvalue(bc.blocksize_b);
81
82
83
84         t=0;
85         if(bc.init.init1){
86                 if((bc.flags&BC_INIT_TYPE)==BC_INIT_TYPE_1){
87                         for(i=0; i<32; ++i){
88                                 startTimer(0);
89                                 START_TIMER;
90                                 (bc.init.init1)(key, &ctx);
91                                 STOP_TIMER;
92                                 t += stopTimer();
93                                 if(i!=31 && bc.free){
94                                         bc.free(&ctx);
95                                 }
96                         }
97                 } else {
98                         for(i=0; i<32; ++i){
99                                 startTimer(0);
100                                 START_TIMER;
101                                 (bc.init.init2)(key, keysize, &ctx);
102                                 STOP_TIMER;
103                                 t += stopTimer();
104                                 if(i!=31 && bc.free){
105                                         bc.free(&ctx);
106                                 }
107                         }
108                 }
109                 t>>=5;
110                 cli_putstr_P(PSTR("\r\n    init (cycles):      "));
111                 printvalue(t);
112         }
113         t=0;
114         for(i=0; i<32; ++i){
115                 startTimer(0);
116                 START_TIMER;
117                 bc.enc.enc1(data, &ctx);
118                 STOP_TIMER;
119                 t += stopTimer();
120         }
121         t>>=5;
122         cli_putstr_P(PSTR("\r\n    encrypt (cycles):   "));
123         printvalue(t);
124
125         t=0;
126         for(i=0; i<32; ++i){
127                 startTimer(0);
128                 START_TIMER;
129                 bc.dec.dec1(data, &ctx);
130                 STOP_TIMER;
131                 t += stopTimer();
132         }
133         t>>=5;
134         cli_putstr_P(PSTR("\r\n    decrypt (cycles):   "));
135         printvalue(t);
136
137         if(bc.free){
138                 bc.free(&ctx);
139         }
140 }
141
142 void bcal_stacksize(const bcdesc_t* bcd){
143         bcdesc_t bc;
144         stack_measuring_ctx_t smctx;
145         memcpy_P(&bc, bcd, sizeof(bcdesc_t));
146         uint8_t ctx[bc.ctxsize_B];
147         uint8_t data[(bc.blocksize_b+7)/8];
148         uint16_t keysize = get_keysize(bc.valid_keysize_desc);
149         uint8_t key[(keysize+7)/8];
150         uint16_t t1, t2;
151
152         if(bc.type!=BCDESC_TYPE_BLOCKCIPHER)
153                 return;
154         cli_putstr_P(PSTR("\r\n\r\n === "));
155         cli_putstr_P(bc.name);
156         cli_putstr_P(PSTR(" stack-usage === "));
157
158         if(bc.init.init1){
159                 if((bc.flags&BC_INIT_TYPE)==BC_INIT_TYPE_1){
160                         cli();
161                         stack_measure_init(&smctx, PATTERN_A);
162                         bc.init.init1(&ctx, key);
163                         t1 = stack_measure_final(&smctx);
164                         stack_measure_init(&smctx, PATTERN_B);
165                         bc.init.init1(&ctx, key);
166                         t2 = stack_measure_final(&smctx);
167                         sei();
168                 } else {
169                         cli();
170                         stack_measure_init(&smctx, PATTERN_A);
171                         bc.init.init2(&ctx, keysize, key);
172                         t1 = stack_measure_final(&smctx);
173                         stack_measure_init(&smctx, PATTERN_B);
174                         bc.init.init2(&ctx, keysize, key);
175                         t2 = stack_measure_final(&smctx);
176                         sei();
177                 }
178                 t1 = (t1>t2)?t1:t2;
179                 cli_putstr_P(PSTR("\r\n    init (bytes):       "));
180                 printvalue((unsigned long)t1);
181         }
182         cli();
183         stack_measure_init(&smctx, PATTERN_A);
184         bc.enc.enc1(data, &ctx);
185         t1 = stack_measure_final(&smctx);
186         stack_measure_init(&smctx, PATTERN_B);
187         bc.enc.enc1(data, &ctx);
188         t2 = stack_measure_final(&smctx);
189         sei();
190
191         t1 = (t1>t2)?t1:t2;
192         cli_putstr_P(PSTR("\r\n    encBlock (bytes):   "));
193         printvalue((unsigned long)t1);
194
195         cli();
196         stack_measure_init(&smctx, PATTERN_A);
197         bc.dec.dec1(data, &ctx);
198         t1 = stack_measure_final(&smctx);
199         stack_measure_init(&smctx, PATTERN_B);
200         bc.dec.dec1(data, &ctx);
201         t2 = stack_measure_final(&smctx);
202         sei();
203
204         t1 = (t1>t2)?t1:t2;
205         cli_putstr_P(PSTR("\r\n    decBlock (bytes):   "));
206         printvalue((unsigned long)t1);
207
208         if(bc.free){
209                 bc.free(&ctx);
210         }
211 }
212
213 void bcal_performance_multiple(const bcdesc_t** bcd_list){
214         const bcdesc_t* bcd;
215         for(;;){
216                 bcd = (void*)pgm_read_word(bcd_list);
217                 if(!bcd){
218                         cli_putstr_P(PSTR("\r\n\r\n End of performance figures\r\n"));
219                         return;
220                 }
221                 bcal_performance(bcd);
222                 bcal_stacksize(bcd);
223                 bcd_list = (void*)((uint8_t*)bcd_list + 2);
224         }
225 }