1 /* bcal-performance.c */
3 This file is part of the AVR-Crypto-Lib.
4 Copyright (C) 2010 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/>.
21 * \file bcal-performance.c
23 * \email daniel.otte@rub.de
25 * \license GPLv3 or later
29 #include "bcal-performance.h"
30 #include "keysize_descriptor.h"
31 #include "blockcipher_descriptor.h"
32 #include "performance_test.h"
33 #include "stack_measuring.h"
38 #include <avr/pgmspace.h>
40 #define PATTERN_A 0xAA
41 #define PATTERN_B 0x55
45 void printvalue(unsigned long v){
49 for(i=0; i<10-strlen(str); ++i){
55 void bcal_performance(const bcdesc_t* bcd){
57 memcpy_P(&bc, bcd, sizeof(bcdesc_t));
58 uint8_t ctx[bc.ctxsize_B];
59 uint8_t data[(bc.blocksize_b+7)/8];
60 uint16_t keysize = get_keysize(bc.valid_keysize_desc);
61 uint8_t key[(keysize+7)/8];
65 if(bc.type!=BCDESC_TYPE_BLOCKCIPHER)
69 cli_putstr_P(PSTR("\r\n\r\n === "));
70 cli_putstr_P(bc.name);
71 cli_putstr_P(PSTR(" performance === "
72 "\r\n type: blockcipher"
73 "\r\n keysize (bits): "));
76 cli_putstr_P(PSTR("\r\n ctxsize (bytes): "));
77 printvalue(bc.ctxsize_B);
79 cli_putstr_P(PSTR("\r\n blocksize (bits): "));
80 printvalue(bc.blocksize_b);
86 if((bc.flags&BC_INIT_TYPE)==BC_INIT_TYPE_1){
90 (bc.init.init1)(key, &ctx);
101 (bc.init.init2)(key, keysize, &ctx);
104 if(i!=31 && bc.free){
110 cli_putstr_P(PSTR("\r\n init (cycles): "));
117 bc.enc.enc1(data, &ctx);
122 cli_putstr_P(PSTR("\r\n encrypt (cycles): "));
129 bc.dec.dec1(data, &ctx);
134 cli_putstr_P(PSTR("\r\n decrypt (cycles): "));
142 void bcal_stacksize(const bcdesc_t* bcd){
144 stack_measuring_ctx_t smctx;
145 memcpy_P(&bc, bcd, sizeof(bcdesc_t));
146 uint8_t ctx[bc.ctxsize_B];
147 uint8_t data[(bc.blocksize_b+7)/8];
148 uint16_t keysize = get_keysize(bc.valid_keysize_desc);
149 uint8_t key[(keysize+7)/8];
152 if(bc.type!=BCDESC_TYPE_BLOCKCIPHER)
154 cli_putstr_P(PSTR("\r\n\r\n === "));
155 cli_putstr_P(bc.name);
156 cli_putstr_P(PSTR(" stack-usage === "));
159 if((bc.flags&BC_INIT_TYPE)==BC_INIT_TYPE_1){
161 stack_measure_init(&smctx, PATTERN_A);
162 bc.init.init1(&ctx, key);
163 t1 = stack_measure_final(&smctx);
164 stack_measure_init(&smctx, PATTERN_B);
165 bc.init.init1(&ctx, key);
166 t2 = stack_measure_final(&smctx);
170 stack_measure_init(&smctx, PATTERN_A);
171 bc.init.init2(&ctx, keysize, key);
172 t1 = stack_measure_final(&smctx);
173 stack_measure_init(&smctx, PATTERN_B);
174 bc.init.init2(&ctx, keysize, key);
175 t2 = stack_measure_final(&smctx);
179 cli_putstr_P(PSTR("\r\n init (bytes): "));
180 printvalue((unsigned long)t1);
183 stack_measure_init(&smctx, PATTERN_A);
184 bc.enc.enc1(data, &ctx);
185 t1 = stack_measure_final(&smctx);
186 stack_measure_init(&smctx, PATTERN_B);
187 bc.enc.enc1(data, &ctx);
188 t2 = stack_measure_final(&smctx);
192 cli_putstr_P(PSTR("\r\n encBlock (bytes): "));
193 printvalue((unsigned long)t1);
196 stack_measure_init(&smctx, PATTERN_A);
197 bc.dec.dec1(data, &ctx);
198 t1 = stack_measure_final(&smctx);
199 stack_measure_init(&smctx, PATTERN_B);
200 bc.dec.dec1(data, &ctx);
201 t2 = stack_measure_final(&smctx);
205 cli_putstr_P(PSTR("\r\n decBlock (bytes): "));
206 printvalue((unsigned long)t1);
213 void bcal_performance_multiple(const bcdesc_t* const* bcd_list){
216 bcd = (void*)pgm_read_word(bcd_list);
218 cli_putstr_P(PSTR("\r\n\r\n End of performance figures\r\n"));
221 bcal_performance(bcd);
223 bcd_list = (void*)((uint8_t*)bcd_list + 2);