1 /* hfal-performance.c */
3 This file is part of the AVR-Crypto-Lib.
4 Copyright (C) 2009 Daniel Otte (daniel.otte@rub.de)
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.
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.
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/>.
20 * \file hfal-performance.c
22 * \email daniel.otte@rub.de
24 * \license GPLv3 or later
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"
39 #define PATTERN_A 0xAA
40 #define PATTERN_B 0x55
42 void hfal_performance(const hfdesc_t *hd){
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];
51 if(hf.type!=HFDESC_TYPE_HASHFUNCTION)
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);
74 printf_P(PSTR("\tinit (cycles): %10"PRIu32"\n"), t);
80 hf.nextBlock(&ctx, data);
85 printf_P(PSTR("\tnextBlock (cycles): %10"PRIu32"\n"), t);
91 hf.lastBlock(&ctx, data, 0);
96 printf_P(PSTR("\tlastBlock (cycles): %10"PRIu32"\n"), t);
102 hf.ctx2hash(digest, &ctx);
107 printf_P(PSTR("\tctx2hash (cycles): %10"PRIu32"\n"), t);
114 void hfal_stacksize(const hfdesc_t *hd){
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];
123 if(hf.type!=HFDESC_TYPE_HASHFUNCTION)
125 printf_P(PSTR("\n === %S stack-usage ===\n"), hf.name);
128 stack_measure_init(&smctx, PATTERN_A);
130 t1 = stack_measure_final(&smctx);
131 stack_measure_init(&smctx, PATTERN_B);
133 t2 = stack_measure_final(&smctx);
136 t1 = (t1 > t2) ? t1 : t2;
137 printf_P(PSTR("\tinit (bytes): %10"PRIu16"\n"), t1);
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);
148 t1 = (t1 > t2) ? t1 : t2;
149 printf_P(PSTR("\tnextBlock (bytes): %10"PRIu16"\n"), t1);
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);
160 t1 = (t1 > t2) ? t1 : t2;
161 printf_P(PSTR("\tlastBlock (bytes): %10"PRIu16"\n"), t1);
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);
173 printf_P(PSTR("\tctx2hash (bytes): %10"PRIu16"\n"));
180 void hfal_performance_multiple(const hfdesc_t *const *hd_list){
183 hd = (void*)pgm_read_word(hd_list);
185 puts_P(PSTR("\n End of performance figures\n"));
188 hfal_performance(hd);
190 hd_list = (void*)((uint8_t*)hd_list + 2);