]> git.cryptolib.org Git - arm-crypto-lib.git/blob - hfal/hfal-performance.c
improving present
[arm-crypto-lib.git] / hfal / hfal-performance.c
1 /* hfal-performance.c */
2 /*
3     This file is part of the ARM-Crypto-Lib.
4     Copyright (C) 2006-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  * \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 "string-extras.h"
34 #include <stdint.h>
35 #include <stdlib.h>
36 #include <string.h>
37
38
39 #define PATTERN_A 0xAA
40 #define PATTERN_B 0x55
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 hfal_performance(const hfdesc_t* hd){
54         hfdesc_t hf;
55         memcpy(&hf, hd, sizeof(hfdesc_t));
56         uint8_t ctx[hf.ctxsize_B];
57         uint8_t data[(hf.blocksize_b+7)/8];
58         uint8_t digest[(hf.hashsize_b+7)/8];
59         uint64_t t;
60         uint8_t i;
61
62         if(hf.type!=HFDESC_TYPE_HASHFUNCTION)
63                 return;
64         calibrateTimer();
65         print_overhead();
66         cli_putstr("\r\n\r\n === ");
67         cli_putstr(hf.name);
68         cli_putstr(" performance === "
69                           "\r\n    type:             hashfunction"
70                           "\r\n    hashsize (bits):    ");
71         printvalue(hf.hashsize_b);
72
73         cli_putstr("\r\n    ctxsize (bytes):    ");
74         printvalue(hf.ctxsize_B);
75
76         cli_putstr("\r\n    blocksize (bits):   ");
77         printvalue(hf.blocksize_b);
78
79         t=0;
80         for(i=0; i<32; ++i){
81                 startTimer(0);
82                 START_TIMER;
83                 hf.init(&ctx);
84                 STOP_TIMER;
85                 t += stopTimer();
86                 if(i!=31 && hf.free){
87                         hf.free(&ctx);
88                 }
89         }
90         t>>=5;
91         cli_putstr("\r\n    init (cycles):      ");
92         printvalue(t);
93
94         t=0;
95         for(i=0; i<32; ++i){
96                 startTimer(0);
97                 START_TIMER;
98                 hf.nextBlock(&ctx, data);
99                 STOP_TIMER;
100                 t += stopTimer();
101         }
102         t>>=5;
103         cli_putstr("\r\n    nextBlock (cycles): ");
104         printvalue(t);
105
106         t=0;
107         for(i=0; i<32; ++i){
108                 startTimer(0);
109                 START_TIMER;
110                 hf.lastBlock(&ctx, data, 0);
111                 STOP_TIMER;
112                 t += stopTimer();
113         }
114         t>>=5;
115         cli_putstr("\r\n    lastBlock (cycles): ");
116         printvalue(t);
117
118         t=0;
119         for(i=0; i<32; ++i){
120                 startTimer(0);
121                 START_TIMER;
122                 hf.ctx2hash(digest, &ctx);
123                 STOP_TIMER;
124                 t += stopTimer();
125         }
126         t>>=5;
127         cli_putstr("\r\n    ctx2hash (cycles):  ");
128         printvalue(t);
129
130         if(hf.free){
131                 hf.free(&ctx);
132         }
133 }
134 /*
135 void hfal_stacksize(const hfdesc_t* hd){
136         hfdesc_t hf;
137         stack_measuring_ctx_t smctx;
138         memcpy(&hf, hd, sizeof(hfdesc_t));
139         uint8_t ctx[hf.ctxsize_B];
140         uint8_t data[(hf.blocksize_b+7)/8];
141         uint8_t digest[(hf.hashsize_b+7)/8];
142         uint16_t t1, t2;
143
144         if(hf.type!=HFDESC_TYPE_HASHFUNCTION)
145                 return;
146         cli_putstr("\r\n\r\n === ");
147         cli_putstr_P(hf.name);
148         cli_putstr(" stack-usage === ");
149
150         cli();
151         stack_measure_init(&smctx, PATTERN_A);
152         hf.init(&ctx);
153         t1 = stack_measure_final(&smctx);
154         stack_measure_init(&smctx, PATTERN_B);
155         hf.init(&ctx);
156         t2 = stack_measure_final(&smctx);
157         sei();
158
159         t1 = (t1>t2)?t1:t2;
160         cli_putstr("\r\n    init (bytes):       ");
161         printvalue((unsigned long)t1);
162
163         cli();
164         stack_measure_init(&smctx, PATTERN_A);
165         hf.nextBlock(&ctx, data);
166         t1 = stack_measure_final(&smctx);
167         stack_measure_init(&smctx, PATTERN_B);
168         hf.nextBlock(&ctx, data);
169         t2 = stack_measure_final(&smctx);
170         sei();
171
172         t1 = (t1>t2)?t1:t2;
173         cli_putstr("\r\n    nextBlock (bytes):  ");
174         printvalue((unsigned long)t1);
175
176         cli();
177         stack_measure_init(&smctx, PATTERN_A);
178         hf.lastBlock(&ctx, data, 0);
179         t1 = stack_measure_final(&smctx);
180         stack_measure_init(&smctx, PATTERN_B);
181         hf.lastBlock(&ctx, data, 0);
182         t2 = stack_measure_final(&smctx);
183         sei();
184
185         t1 = (t1>t2)?t1:t2;
186         cli_putstr("\r\n    lastBlock (bytes):  ");
187         printvalue((unsigned long)t1);
188
189         cli();
190         stack_measure_init(&smctx, PATTERN_A);
191         hf.ctx2hash(digest, &ctx);
192         t1 = stack_measure_final(&smctx);
193         stack_measure_init(&smctx, PATTERN_B);
194         hf.ctx2hash(digest, &ctx);
195         t2 = stack_measure_final(&smctx);
196         sei();
197
198         t1 = (t1>t2)?t1:t2;
199         cli_putstr("\r\n    ctx2hash (bytes):   ");
200         printvalue((unsigned long)t1);
201
202         if(hf.free){
203                 hf.free(&ctx);
204         }
205 }
206 */
207
208 void hfal_performance_multiple(const hfdesc_t** hd_list){
209         const hfdesc_t* hd;
210         for(;;){
211                 hd = *hd_list;
212                 if(!hd){
213                         cli_putstr("\r\n\r\n End of performance figures\r\n");
214                         return;
215                 }
216                 hfal_performance(hd);
217 //              hfal_stacksize(hd);
218                 hd_list += 1;
219         }
220 }
221