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)
61 memcpy_P(&bc, bcd, sizeof(bcdesc_t));
62 uint8_t ctx[bc.ctxsize_B];
63 uint8_t data[(bc.blocksize_b + 7) / 8];
64 uint16_t keysize = get_keysize(bc.valid_keysize_desc);
65 uint8_t key[(keysize + 7) / 8];
69 if (bc.type != BCDESC_TYPE_BLOCKCIPHER)
73 printf_P(PSTR("\n\n === %S performance === \n"
74 "\ttype: blockcipher\n"
75 "\tkeysize (bits): %5"PRIu16"\n"
76 "\tctxsize (bytes): %5"PRIu16"\n"
77 "\tblocksize (bits): %5"PRIu16"\n"),
78 bc.name, keysize, bc.ctxsize_B, bc.blocksize_b);
82 if ((bc.flags & BC_INIT_TYPE) == BC_INIT_TYPE_1) {
83 for (i = 0; i < 32; ++i) {
86 (bc.init.init1)(key, &ctx);
89 if (i != 31 && bc.free) {
94 for (i = 0; i < 32; ++i) {
97 (bc.init.init2)(key, keysize, &ctx);
100 if (i != 31 && bc.free) {
106 printf_P(PSTR(" init (cycles): %5"PRIu16"\n"), t);
111 for (i = 0; i < 32; ++i) {
114 bc.enc.enc1(data, &ctx);
119 printf_P(PSTR(" encrypt (cycles): %5"PRIu16"\n"), t);
123 for (i = 0; i < 32; ++i) {
126 bc.dec.dec1(data, &ctx);
131 printf_P(PSTR(" decrypt (cycles): %5"PRIu16"\n"), t);
140 void bcal_stacksize(const bcdesc_t *bcd)
143 stack_measuring_ctx_t smctx;
144 memcpy_P(&bc, bcd, sizeof(bcdesc_t));
145 uint8_t ctx[bc.ctxsize_B];
146 uint8_t data[(bc.blocksize_b + 7) / 8];
147 uint16_t keysize = get_keysize(bc.valid_keysize_desc);
148 uint8_t key[(keysize + 7) / 8];
149 uint16_t t1 = 0, t2 = 0;
151 if (bc.type != BCDESC_TYPE_BLOCKCIPHER)
153 printf_P(PSTR("\n === %S stack-usage ===\n"), bc.name);
158 if ((bc.flags & BC_INIT_TYPE) == BC_INIT_TYPE_1) {
160 stack_measure_init(&smctx, PATTERN_A);
161 bc.init.init1(&ctx, key);
162 t1 = stack_measure_final(&smctx);
163 stack_measure_init(&smctx, PATTERN_B);
164 bc.init.init1(&ctx, key);
165 t2 = stack_measure_final(&smctx);
169 stack_measure_init(&smctx, PATTERN_A);
170 bc.init.init2(&ctx, keysize, key);
171 t1 = stack_measure_final(&smctx);
172 stack_measure_init(&smctx, PATTERN_B);
173 bc.init.init2(&ctx, keysize, key);
174 t2 = stack_measure_final(&smctx);
178 t1 = (t1 > t2) ? t1 : t2;
179 printf_P(PSTR(" init (bytes): %5"PRIu16"\n"), t1);
182 stack_measure_init(&smctx, PATTERN_A);
183 bc.enc.enc1(data, &ctx);
184 t1 = stack_measure_final(&smctx);
185 stack_measure_init(&smctx, PATTERN_B);
186 bc.enc.enc1(data, &ctx);
187 t2 = stack_measure_final(&smctx);
190 t1 = (t1 > t2) ? t1 : t2;
191 printf_P(PSTR(" encBlock (bytes): %5"PRIu16"\n"), t1);
194 stack_measure_init(&smctx, PATTERN_A);
195 bc.dec.dec1(data, &ctx);
196 t1 = stack_measure_final(&smctx);
197 stack_measure_init(&smctx, PATTERN_B);
198 bc.dec.dec1(data, &ctx);
199 t2 = stack_measure_final(&smctx);
202 t1 = (t1 > t2) ? t1 : t2;
203 printf_P(PSTR(" decBlock (bytes): %5"PRIu16"\n"), t1);
210 void bcal_performance_multiple(const bcdesc_t * const *bcd_list)
214 bcd = (void*) pgm_read_word(bcd_list);
216 puts_P(PSTR("\n End of performance figures\n"));
219 bcal_performance(bcd);
221 bcd_list = (void*) ((uint8_t*) bcd_list + 2);