]> git.cryptolib.org Git - avr-crypto-lib.git/blob - hfal/hfal-performance.c
fixing E-Mail-Address & Copyright
[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) 2006-2015 Daniel Otte (bg@nerilex.org)
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   bg@nerilex.org
23  * \date    2009-05-10
24  * \license GPLv3 or later
25  *
26  */
27
28 #include <stdint.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdio.h>
32 #include <avr/pgmspace.h>
33 #include "hfal-performance.h"
34 #include "hashfunction_descriptor.h"
35 #include "stack_measuring.h"
36 #include "performance_test.h"
37 #include "uart.h"
38
39 #define PATTERN_A 0xAA
40 #define PATTERN_B 0x55
41
42 void hfal_performance(const hfdesc_t *hd){
43         hfdesc_t hf;
44         memcpy_P(&hf, hd, sizeof(hfdesc_t));
45         uint8_t ctx[hf.ctxsize_B];
46         uint8_t data[(hf.blocksize_b + 7) / 8];
47         uint8_t digest[(hf.hashsize_b + 7) / 8];
48         uint32_t t;
49         uint8_t i;
50
51         if(hf.type != HFDESC_TYPE_HASHFUNCTION)
52                 return;
53         calibrateTimer();
54         print_overhead();
55         printf_P(PSTR("\n\n === %S performance ===\n"
56                       "\ttype:             hashfunction\n"
57                       "\thashsize (bits):    %10"PRIu16"\n"
58                       "\tctxsize (bytes):    %10"PRIu16"\n"
59                       "\tblocksize (bits):   %10"PRIu16"\n"),
60                  hf.name, hf.hashsize_b, hf.ctxsize_B, hf.blocksize_b);
61         uart0_flush();
62         t = 0;
63         for(i = 0; i < 32; ++i){
64                 startTimer(0);
65                 START_TIMER;
66                 hf.init(&ctx);
67                 STOP_TIMER;
68                 t += stopTimer();
69                 if(i != 31 && hf.free){
70                         hf.free(&ctx);
71                 }
72         }
73         t >>= 5;
74         printf_P(PSTR("\tinit (cycles):      %10"PRIu32"\n"), t);
75
76         t = 0;
77         for(i = 0; i < 32; ++i){
78                 startTimer(0);
79                 START_TIMER;
80                 hf.nextBlock(&ctx, data);
81                 STOP_TIMER;
82                 t += stopTimer();
83         }
84         t >>= 5;
85         printf_P(PSTR("\tnextBlock (cycles): %10"PRIu32"\n"), t);
86
87         t = 0;
88         for(i = 0; i < 32; ++i){
89                 startTimer(0);
90                 START_TIMER;
91                 hf.lastBlock(&ctx, data, 0);
92                 STOP_TIMER;
93                 t += stopTimer();
94         }
95         t >>= 5;
96         printf_P(PSTR("\tlastBlock (cycles): %10"PRIu32"\n"), t);
97
98         t = 0;
99         for(i = 0; i < 32; ++i){
100                 startTimer(0);
101                 START_TIMER;
102                 hf.ctx2hash(digest, &ctx);
103                 STOP_TIMER;
104                 t += stopTimer();
105         }
106         t >>= 5;
107         printf_P(PSTR("\tctx2hash (cycles):  %10"PRIu32"\n"), t);
108
109         if(hf.free){
110                 hf.free(&ctx);
111         }
112 }
113
114 void hfal_stacksize(const hfdesc_t *hd){
115         hfdesc_t hf;
116         stack_measuring_ctx_t smctx;
117         memcpy_P(&hf, hd, sizeof(hfdesc_t));
118         uint8_t ctx[hf.ctxsize_B];
119         uint8_t data[(hf.blocksize_b + 7) / 8];
120         uint8_t digest[(hf.hashsize_b + 7) / 8];
121         size_t t1, t2;
122
123         if(hf.type!=HFDESC_TYPE_HASHFUNCTION)
124                 return;
125         printf_P(PSTR("\n === %S stack-usage ===\n"), hf.name);
126
127         cli();
128         stack_measure_init(&smctx, PATTERN_A);
129         hf.init(&ctx);
130         t1 = stack_measure_final(&smctx);
131         stack_measure_init(&smctx, PATTERN_B);
132         hf.init(&ctx);
133         t2 = stack_measure_final(&smctx);
134         sei();
135
136         t1 = (t1 > t2) ? t1 : t2;
137         printf_P(PSTR("\tinit (bytes):       %10"PRIu16"\n"), t1);
138
139         cli();
140         stack_measure_init(&smctx, PATTERN_A);
141         hf.nextBlock(&ctx, data);
142         t1 = stack_measure_final(&smctx);
143         stack_measure_init(&smctx, PATTERN_B);
144         hf.nextBlock(&ctx, data);
145         t2 = stack_measure_final(&smctx);
146         sei();
147
148         t1 = (t1 > t2) ? t1 : t2;
149         printf_P(PSTR("\tnextBlock (bytes):  %10"PRIu16"\n"), t1);
150
151         cli();
152         stack_measure_init(&smctx, PATTERN_A);
153         hf.lastBlock(&ctx, data, 0);
154         t1 = stack_measure_final(&smctx);
155         stack_measure_init(&smctx, PATTERN_B);
156         hf.lastBlock(&ctx, data, 0);
157         t2 = stack_measure_final(&smctx);
158         sei();
159
160         t1 = (t1 > t2) ? t1 : t2;
161         printf_P(PSTR("\tlastBlock (bytes):  %10"PRIu16"\n"), t1);
162
163         cli();
164         stack_measure_init(&smctx, PATTERN_A);
165         hf.ctx2hash(digest, &ctx);
166         t1 = stack_measure_final(&smctx);
167         stack_measure_init(&smctx, PATTERN_B);
168         hf.ctx2hash(digest, &ctx);
169         t2 = stack_measure_final(&smctx);
170         sei();
171
172         t1 = (t1 > t2) ? t1 : t2;
173         printf_P(PSTR("\tctx2hash (bytes):   %10"PRIu16"\n"));
174
175         if(hf.free){
176                 hf.free(&ctx);
177         }
178 }
179
180 void hfal_performance_multiple(const hfdesc_t *const *hd_list){
181         const hfdesc_t *hd;
182         for(;;){
183                 hd = (void*)pgm_read_word(hd_list);
184                 if(!hd){
185                         puts_P(PSTR("\n End of performance figures\n"));
186                         return;
187                 }
188                 hfal_performance(hd);
189                 hfal_stacksize(hd);
190                 hd_list = (void*)((uint8_t*)hd_list + 2);
191         }
192 }
193