]> git.cryptolib.org Git - avr-crypto-lib.git/blob - hfal/hfal-performance.c
global style change (now * is attached to identifier not to type)
[avr-crypto-lib.git] / hfal / hfal-performance.c
1 /* hfal-performance.c */
2 /*
3     This file is part of the AVR-Crypto-Lib.
4     Copyright (C) 2009  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  * \file    hfal-performance.c
21  * \author  Daniel Otte
22  * \email   daniel.otte@rub.de
23  * \date    2009-05-10
24  * \license GPLv3 or later
25  *
26  */
27
28 #include "hfal-performance.h"
29 #include "hashfunction_descriptor.h"
30 #include "stack_measuring.h"
31 #include "cli.h"
32 #include "performance_test.h"
33 #include <stdint.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <avr/pgmspace.h>
37
38 #define PATTERN_A 0xAA
39 #define PATTERN_B 0x55
40
41 static
42 void printvalue(unsigned long v){
43         char str[20];
44         int i;
45         ultoa(v, str, 10);
46         for(i=0; i<10-strlen(str); ++i){
47                 cli_putc(' ');
48         }
49         cli_putstr(str);
50 }
51
52 void hfal_performance(const hfdesc_t *hd){
53         hfdesc_t hf;
54         memcpy_P(&hf, hd, sizeof(hfdesc_t));
55         uint8_t ctx[hf.ctxsize_B];
56         uint8_t data[(hf.blocksize_b+7)/8];
57         uint8_t digest[(hf.hashsize_b+7)/8];
58         uint64_t t;
59         uint8_t i;
60
61         if(hf.type!=HFDESC_TYPE_HASHFUNCTION)
62                 return;
63         calibrateTimer();
64         print_overhead();
65         cli_putstr_P(PSTR("\r\n\r\n === "));
66         cli_putstr_P(hf.name);
67         cli_putstr_P(PSTR(" performance === "
68                           "\r\n    type:             hashfunction"
69                           "\r\n    hashsize (bits):    "));
70         printvalue(hf.hashsize_b);
71
72         cli_putstr_P(PSTR("\r\n    ctxsize (bytes):    "));
73         printvalue(hf.ctxsize_B);
74
75         cli_putstr_P(PSTR("\r\n    blocksize (bits):   "));
76         printvalue(hf.blocksize_b);
77
78         t=0;
79         for(i=0; i<32; ++i){
80                 startTimer(0);
81                 START_TIMER;
82                 hf.init(&ctx);
83                 STOP_TIMER;
84                 t += stopTimer();
85                 if(i!=31 && hf.free){
86                         hf.free(&ctx);
87                 }
88         }
89         t>>=5;
90         cli_putstr_P(PSTR("\r\n    init (cycles):      "));
91         printvalue(t);
92
93         t=0;
94         for(i=0; i<32; ++i){
95                 startTimer(0);
96                 START_TIMER;
97                 hf.nextBlock(&ctx, data);
98                 STOP_TIMER;
99                 t += stopTimer();
100         }
101         t>>=5;
102         cli_putstr_P(PSTR("\r\n    nextBlock (cycles): "));
103         printvalue(t);
104
105         t=0;
106         for(i=0; i<32; ++i){
107                 startTimer(0);
108                 START_TIMER;
109                 hf.lastBlock(&ctx, data, 0);
110                 STOP_TIMER;
111                 t += stopTimer();
112         }
113         t>>=5;
114         cli_putstr_P(PSTR("\r\n    lastBlock (cycles): "));
115         printvalue(t);
116
117         t=0;
118         for(i=0; i<32; ++i){
119                 startTimer(0);
120                 START_TIMER;
121                 hf.ctx2hash(digest, &ctx);
122                 STOP_TIMER;
123                 t += stopTimer();
124         }
125         t>>=5;
126         cli_putstr_P(PSTR("\r\n    ctx2hash (cycles):  "));
127         printvalue(t);
128
129         if(hf.free){
130                 hf.free(&ctx);
131         }
132 }
133
134 void hfal_stacksize(const hfdesc_t *hd){
135         hfdesc_t hf;
136         stack_measuring_ctx_t smctx;
137         memcpy_P(&hf, hd, sizeof(hfdesc_t));
138         uint8_t ctx[hf.ctxsize_B];
139         uint8_t data[(hf.blocksize_b+7)/8];
140         uint8_t digest[(hf.hashsize_b+7)/8];
141         uint16_t t1, t2;
142
143         if(hf.type!=HFDESC_TYPE_HASHFUNCTION)
144                 return;
145         cli_putstr_P(PSTR("\r\n\r\n === "));
146         cli_putstr_P(hf.name);
147         cli_putstr_P(PSTR(" stack-usage === "));
148
149         cli();
150         stack_measure_init(&smctx, PATTERN_A);
151         hf.init(&ctx);
152         t1 = stack_measure_final(&smctx);
153         stack_measure_init(&smctx, PATTERN_B);
154         hf.init(&ctx);
155         t2 = stack_measure_final(&smctx);
156         sei();
157
158         t1 = (t1>t2)?t1:t2;
159         cli_putstr_P(PSTR("\r\n    init (bytes):       "));
160         printvalue((unsigned long)t1);
161
162         cli();
163         stack_measure_init(&smctx, PATTERN_A);
164         hf.nextBlock(&ctx, data);
165         t1 = stack_measure_final(&smctx);
166         stack_measure_init(&smctx, PATTERN_B);
167         hf.nextBlock(&ctx, data);
168         t2 = stack_measure_final(&smctx);
169         sei();
170
171         t1 = (t1>t2)?t1:t2;
172         cli_putstr_P(PSTR("\r\n    nextBlock (bytes):  "));
173         printvalue((unsigned long)t1);
174
175         cli();
176         stack_measure_init(&smctx, PATTERN_A);
177         hf.lastBlock(&ctx, data, 0);
178         t1 = stack_measure_final(&smctx);
179         stack_measure_init(&smctx, PATTERN_B);
180         hf.lastBlock(&ctx, data, 0);
181         t2 = stack_measure_final(&smctx);
182         sei();
183
184         t1 = (t1>t2)?t1:t2;
185         cli_putstr_P(PSTR("\r\n    lastBlock (bytes):  "));
186         printvalue((unsigned long)t1);
187
188         cli();
189         stack_measure_init(&smctx, PATTERN_A);
190         hf.ctx2hash(digest, &ctx);
191         t1 = stack_measure_final(&smctx);
192         stack_measure_init(&smctx, PATTERN_B);
193         hf.ctx2hash(digest, &ctx);
194         t2 = stack_measure_final(&smctx);
195         sei();
196
197         t1 = (t1>t2)?t1:t2;
198         cli_putstr_P(PSTR("\r\n    ctx2hash (bytes):   "));
199         printvalue((unsigned long)t1);
200
201         if(hf.free){
202                 hf.free(&ctx);
203         }
204 }
205
206 void hfal_performance_multiple(const hfdesc_t *const *hd_list){
207         const hfdesc_t *hd;
208         for(;;){
209                 hd = (void*)pgm_read_word(hd_list);
210                 if(!hd){
211                         cli_putstr_P(PSTR("\r\n\r\n End of performance figures\r\n"));
212                         return;
213                 }
214                 hfal_performance(hd);
215                 hfal_stacksize(hd);
216                 hd_list = (void*)((uint8_t*)hd_list + 2);
217         }
218 }
219