]> git.cryptolib.org Git - avr-crypto-lib.git/blobdiff - hfal-performance.c
updated Makefile
[avr-crypto-lib.git] / hfal-performance.c
index 70f2ef15eb37ed2736669bcef2c5f69b37370db2..5371f80d9f255594524fee8023a3eb37e3b9a9ad 100644 (file)
@@ -27,6 +27,7 @@
 
 #include "hfal-performance.h"
 #include "hashfunction_descriptor.h"
+#include "stack_measuring.h"
 #include "cli.h"
 #include "performance_test.h"
 #include <stdint.h>
 #include <string.h>
 #include <avr/pgmspace.h>
 
+#define PATTERN_A 0xAA
+#define PATTERN_B 0x55
 
 static
-void printvalue(unsigned int v){
+void printvalue(unsigned long v){
        char str[20];
        int i;
        ultoa(v, str, 10);
@@ -53,6 +56,7 @@ void hfal_performance(const hfdesc_t* hd){
        uint8_t data[(hf.blocksize_b+7)/8];
        uint8_t digest[(hf.hashsize_b+7)/8];
        uint64_t t;
+       uint8_t i;
 
        if(hf.type!=HFDESC_TYPE_HASHFUNCTION)
                return;
@@ -71,39 +75,133 @@ void hfal_performance(const hfdesc_t* hd){
        cli_putstr_P(PSTR("\r\n    blocksize (bits):   "));
        printvalue(hf.blocksize_b);
 
-       startTimer(0);
-       START_TIMER;
-       hf.init(&ctx);
-       STOP_TIMER;
-       t = stopTimer();
+       t=0;
+       for(i=0; i<32; ++i){
+               startTimer(0);
+               START_TIMER;
+               hf.init(&ctx);
+               STOP_TIMER;
+               t += stopTimer();
+               if(i!=31 && hf.free){
+                       hf.free(&ctx);
+               }
+       }
+       t>>=5;
        cli_putstr_P(PSTR("\r\n    init (cycles):      "));
        printvalue(t);
 
-       startTimer(0);
-       START_TIMER;
-       hf.nextBlock(&ctx, data);
-       STOP_TIMER;
-       t = stopTimer();
+       t=0;
+       for(i=0; i<32; ++i){
+               startTimer(0);
+               START_TIMER;
+               hf.nextBlock(&ctx, data);
+               STOP_TIMER;
+               t += stopTimer();
+       }
+       t>>=5;
        cli_putstr_P(PSTR("\r\n    nextBlock (cycles): "));
        printvalue(t);
 
-       startTimer(0);
-       START_TIMER;
-       hf.lastBlock(&ctx, data, 0);
-       STOP_TIMER;
-       t = stopTimer();
+       t=0;
+       for(i=0; i<32; ++i){
+               startTimer(0);
+               START_TIMER;
+               hf.lastBlock(&ctx, data, 0);
+               STOP_TIMER;
+               t += stopTimer();
+       }
+       t>>=5;
        cli_putstr_P(PSTR("\r\n    lastBlock (cycles): "));
        printvalue(t);
 
-       startTimer(0);
-       START_TIMER;
-       hf.ctx2hash(digest, &ctx);
-       STOP_TIMER;
-       t = stopTimer();
+       t=0;
+       for(i=0; i<32; ++i){
+               startTimer(0);
+               START_TIMER;
+               hf.ctx2hash(digest, &ctx);
+               STOP_TIMER;
+               t += stopTimer();
+       }
+       t>>=5;
        cli_putstr_P(PSTR("\r\n    ctx2hash (cycles):  "));
        printvalue(t);
+
+       if(hf.free){
+               hf.free(&ctx);
+       }
 }
 
+void hfal_stacksize(const hfdesc_t* hd){
+       hfdesc_t hf;
+       stack_measuring_ctx_t smctx;
+       memcpy_P(&hf, hd, sizeof(hfdesc_t));
+       uint8_t ctx[hf.ctxsize_B];
+       uint8_t data[(hf.blocksize_b+7)/8];
+       uint8_t digest[(hf.hashsize_b+7)/8];
+       uint16_t t1, t2;
+
+       if(hf.type!=HFDESC_TYPE_HASHFUNCTION)
+               return;
+       cli_putstr_P(PSTR("\r\n\r\n === "));
+       cli_putstr_P(hf.name);
+       cli_putstr_P(PSTR(" stack-usage === "));
+
+       cli();
+       stack_measure_init(&smctx, PATTERN_A);
+       hf.init(&ctx);
+       t1 = stack_measure_final(&smctx);
+       stack_measure_init(&smctx, PATTERN_B);
+       hf.init(&ctx);
+       t2 = stack_measure_final(&smctx);
+       sei();
+
+       t1 = (t1>t2)?t1:t2;
+       cli_putstr_P(PSTR("\r\n    init (bytes):       "));
+       printvalue((unsigned long)t1);
+
+       cli();
+       stack_measure_init(&smctx, PATTERN_A);
+       hf.nextBlock(&ctx, data);
+       t1 = stack_measure_final(&smctx);
+       stack_measure_init(&smctx, PATTERN_B);
+       hf.nextBlock(&ctx, data);
+       t2 = stack_measure_final(&smctx);
+       sei();
+
+       t1 = (t1>t2)?t1:t2;
+       cli_putstr_P(PSTR("\r\n    nextBlock (bytes):  "));
+       printvalue((unsigned long)t1);
+
+       cli();
+       stack_measure_init(&smctx, PATTERN_A);
+       hf.lastBlock(&ctx, data, 0);
+       t1 = stack_measure_final(&smctx);
+       stack_measure_init(&smctx, PATTERN_B);
+       hf.lastBlock(&ctx, data, 0);
+       t2 = stack_measure_final(&smctx);
+       sei();
+
+       t1 = (t1>t2)?t1:t2;
+       cli_putstr_P(PSTR("\r\n    lastBlock (bytes):  "));
+       printvalue((unsigned long)t1);
+
+       cli();
+       stack_measure_init(&smctx, PATTERN_A);
+       hf.ctx2hash(digest, &ctx);
+       t1 = stack_measure_final(&smctx);
+       stack_measure_init(&smctx, PATTERN_B);
+       hf.ctx2hash(digest, &ctx);
+       t2 = stack_measure_final(&smctx);
+       sei();
+
+       t1 = (t1>t2)?t1:t2;
+       cli_putstr_P(PSTR("\r\n    ctx2hash (bytes):   "));
+       printvalue((unsigned long)t1);
+
+       if(hf.free){
+               hf.free(&ctx);
+       }
+}
 
 void hfal_performance_multiple(const hfdesc_t** hd_list){
        const hfdesc_t* hd;
@@ -114,6 +212,7 @@ void hfal_performance_multiple(const hfdesc_t** hd_list){
                        return;
                }
                hfal_performance(hd);
+               hfal_stacksize(hd);
                hd_list = (void*)((uint8_t*)hd_list + 2);
        }
 }