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"
40 #include <avr/pgmspace.h>
42 #define PATTERN_A 0xAA
43 #define PATTERN_B 0x55
47 void printvalue(unsigned long v){
51 for(i=0; i<10-strlen(str); ++i){
58 void bcal_performance(const bcdesc_t *bcd){
60 memcpy_P(&bc, bcd, sizeof(bcdesc_t));
61 uint8_t ctx[bc.ctxsize_B];
62 uint8_t data[(bc.blocksize_b+7)/8];
63 uint16_t keysize = get_keysize(bc.valid_keysize_desc);
64 uint8_t key[(keysize+7)/8];
68 if(bc.type != BCDESC_TYPE_BLOCKCIPHER)
72 printf_P(PSTR("\n\n === %S performance === \n"
73 "\ttype: blockcipher\n"
74 "\tkeysize (bits): %5"PRIu16"\n"
75 "\tctxsize (bytes): %5"PRIu16"\n"
76 "\tblocksize (bits): %5"PRIu16"\n"),
77 bc.name, keysize, bc.ctxsize_B, bc.blocksize_b);
81 if((bc.flags & BC_INIT_TYPE) == BC_INIT_TYPE_1){
85 (bc.init.init1)(key, &ctx);
96 (bc.init.init2)(key, keysize, &ctx);
105 printf_P(PSTR(" init (cycles): %5"PRIu16"\n"), t);
113 bc.enc.enc1(data, &ctx);
118 printf_P(PSTR(" encrypt (cycles): %5"PRIu16"\n"), t);
125 bc.dec.dec1(data, &ctx);
130 printf_P(PSTR(" decrypt (cycles): %5"PRIu16"\n"), t);
139 void bcal_stacksize(const bcdesc_t *bcd){
141 stack_measuring_ctx_t smctx;
142 memcpy_P(&bc, bcd, sizeof(bcdesc_t));
143 uint8_t ctx[bc.ctxsize_B];
144 uint8_t data[(bc.blocksize_b+7)/8];
145 uint16_t keysize = get_keysize(bc.valid_keysize_desc);
146 uint8_t key[(keysize+7)/8];
147 uint16_t t1 = 0, t2 = 0;
149 if(bc.type != BCDESC_TYPE_BLOCKCIPHER)
151 printf_P(PSTR("\n === %S stack-usage ===\n"),bc.name);
156 if((bc.flags & BC_INIT_TYPE) == BC_INIT_TYPE_1){
158 stack_measure_init(&smctx, PATTERN_A);
159 bc.init.init1(&ctx, key);
160 t1 = stack_measure_final(&smctx);
161 stack_measure_init(&smctx, PATTERN_B);
162 bc.init.init1(&ctx, key);
163 t2 = stack_measure_final(&smctx);
167 stack_measure_init(&smctx, PATTERN_A);
168 bc.init.init2(&ctx, keysize, key);
169 t1 = stack_measure_final(&smctx);
170 stack_measure_init(&smctx, PATTERN_B);
171 bc.init.init2(&ctx, keysize, key);
172 t2 = stack_measure_final(&smctx);
177 printf_P(PSTR(" init (bytes): %5"PRIu16"\n"), t1);
180 stack_measure_init(&smctx, PATTERN_A);
181 bc.enc.enc1(data, &ctx);
182 t1 = stack_measure_final(&smctx);
183 stack_measure_init(&smctx, PATTERN_B);
184 bc.enc.enc1(data, &ctx);
185 t2 = stack_measure_final(&smctx);
189 printf_P(PSTR(" encBlock (bytes): %5"PRIu16"\n"), t1);
192 stack_measure_init(&smctx, PATTERN_A);
193 bc.dec.dec1(data, &ctx);
194 t1 = stack_measure_final(&smctx);
195 stack_measure_init(&smctx, PATTERN_B);
196 bc.dec.dec1(data, &ctx);
197 t2 = stack_measure_final(&smctx);
201 printf_P(PSTR(" decBlock (bytes): %5"PRIu16"\n"), t1);
208 void bcal_performance_multiple(const bcdesc_t *const *bcd_list){
211 bcd = (void*)pgm_read_word(bcd_list);
213 puts_P(PSTR("\n End of performance figures\n"));
216 bcal_performance(bcd);
218 bcd_list = (void*)((uint8_t*)bcd_list + 2);