]> git.cryptolib.org Git - avr-crypto-lib.git/blob - hfal-performance.c
changing performance measurment of blockciphers to bcal
[avr-crypto-lib.git] / 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 "cli.h"
31 #include "performance_test.h"
32 #include <stdint.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <avr/pgmspace.h>
36
37
38 static
39 void printvalue(unsigned long v){
40         char str[20];
41         int i;
42         ultoa(v, str, 10);
43         for(i=0; i<10-strlen(str); ++i){
44                 cli_putc(' ');
45         }
46         cli_putstr(str);
47 }
48
49 void hfal_performance(const hfdesc_t* hd){
50         hfdesc_t hf;
51         memcpy_P(&hf, hd, sizeof(hfdesc_t));
52         uint8_t ctx[hf.ctxsize_B];
53         uint8_t data[(hf.blocksize_b+7)/8];
54         uint8_t digest[(hf.hashsize_b+7)/8];
55         uint64_t t;
56         uint8_t i;
57
58         if(hf.type!=HFDESC_TYPE_HASHFUNCTION)
59                 return;
60         calibrateTimer();
61         print_overhead();
62         cli_putstr_P(PSTR("\r\n\r\n === "));
63         cli_putstr_P(hf.name);
64         cli_putstr_P(PSTR(" performance === "
65                           "\r\n    type:             hashfunction"
66                           "\r\n    hashsize (bits):    "));
67         printvalue(hf.hashsize_b);
68
69         cli_putstr_P(PSTR("\r\n    ctxsize (bytes):    "));
70         printvalue(hf.ctxsize_B);
71
72         cli_putstr_P(PSTR("\r\n    blocksize (bits):   "));
73         printvalue(hf.blocksize_b);
74
75         t=0;
76         for(i=0; i<32; ++i){
77                 startTimer(0);
78                 START_TIMER;
79                 hf.init(&ctx);
80                 STOP_TIMER;
81                 t += stopTimer();
82                 if(i!=31 && hf.free){
83                         hf.free(&ctx)
84                 }
85         }
86         t>>=5;
87         cli_putstr_P(PSTR("\r\n    init (cycles):      "));
88         printvalue(t);
89
90         t=0;
91         for(i=0; i<32; ++i){
92                 startTimer(0);
93                 START_TIMER;
94                 hf.nextBlock(&ctx, data);
95                 STOP_TIMER;
96                 t += stopTimer();
97         }
98         t>>=5;
99         cli_putstr_P(PSTR("\r\n    nextBlock (cycles): "));
100         printvalue(t);
101
102         t=0;
103         for(i=0; i<32; ++i){
104                 startTimer(0);
105                 START_TIMER;
106                 hf.lastBlock(&ctx, data, 0);
107                 STOP_TIMER;
108                 t += stopTimer();
109         }
110         t>>=5;
111         cli_putstr_P(PSTR("\r\n    lastBlock (cycles): "));
112         printvalue(t);
113
114         t=0;
115         for(i=0; i<32; ++i){
116                 startTimer(0);
117                 START_TIMER;
118                 hf.ctx2hash(digest, &ctx);
119                 STOP_TIMER;
120                 t += stopTimer();
121         }
122         t>>=5;
123         cli_putstr_P(PSTR("\r\n    ctx2hash (cycles):  "));
124         printvalue(t);
125
126         if(hf.free){
127                 hf.free(&ctx);
128         }
129 }
130
131
132 void hfal_performance_multiple(const hfdesc_t** hd_list){
133         const hfdesc_t* hd;
134         for(;;){
135                 hd = (void*)pgm_read_word(hd_list);
136                 if(!hd){
137                         cli_putstr_P(PSTR("\r\n\r\n End of performance figures\r\n"));
138                         return;
139                 }
140                 hfal_performance(hd);
141                 hd_list = (void*)((uint8_t*)hd_list + 2);
142         }
143 }
144