]> git.cryptolib.org Git - avr-crypto-lib.git/blob - hfal-performance.c
e9ec7707a427141ea1d78ec33e840b2183918964
[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 int 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         
57         if(hf.type!=HFDESC_TYPE_HASHFUNCTION)
58                 return;
59         calibrateTimer();
60         cli_putstr_P(PSTR("\r\n\r\n === "));
61         cli_putstr_P(hf.name);
62         cli_putstr_P(PSTR(" performance === "
63                           "\r\n    type:             hashfunction"
64                           "\r\n    hashsize (bits):    "));
65         printvalue(hf.hashsize_b);
66         
67         cli_putstr_P(PSTR("\r\n    ctxsize (bytes):    "));
68         printvalue(hf.ctxsize_B);
69         
70         cli_putstr_P(PSTR("\r\n    blocksize (bits):   "));
71         printvalue(hf.blocksize_b);
72         
73         startTimer(0);
74         START_TIMER;
75         hf.init(&ctx);
76         STOP_TIMER;
77         t = stopTimer();
78         cli_putstr_P(PSTR("\r\n    init (cycles):      "));
79         printvalue(t);
80         
81         startTimer(0);
82         START_TIMER;
83         hf.nextBlock(&ctx, data);
84         STOP_TIMER;
85         t = stopTimer();
86         cli_putstr_P(PSTR("\r\n    nextBlock (cycles): "));
87         printvalue(t);
88         
89         startTimer(0);
90         START_TIMER;
91         hf.lastBlock(&ctx, data, 0);
92         STOP_TIMER;
93         t = stopTimer();
94         cli_putstr_P(PSTR("\r\n    lastBlock (cycles): "));
95         printvalue(t);
96         
97         startTimer(0);
98         START_TIMER;
99         hf.ctx2hash(digest, &ctx);
100         STOP_TIMER;
101         t = stopTimer();
102         cli_putstr_P(PSTR("\r\n    ctx2hash (cycles):  "));
103         printvalue(t);
104 }
105
106
107 void hfal_performance_multiple(const hfdesc_t** hd_list){
108         const hfdesc_t* hd;
109         for(;;){
110                 hd = (void*)pgm_read_word(hd_list);
111                 if(!hd){
112                         cli_putstr_P(PSTR("\r\n\r\n End of performance figures\r\n"));
113                         return;
114                 }
115                 hfal_performance(hd);
116                 hd_list = (void*)((uint8_t*)hd_list + 2);
117         }
118 }
119