]> git.cryptolib.org Git - avr-crypto-lib.git/commitdiff
now with shavs tests
authorbg <bg@b1d182e4-1ff8-0310-901f-bddb46175740>
Thu, 5 Feb 2009 22:50:42 +0000 (22:50 +0000)
committerbg <bg@b1d182e4-1ff8-0310-901f-bddb46175740>
Thu, 5 Feb 2009 22:50:42 +0000 (22:50 +0000)
aes.c [deleted file]
hfal-basic.c
test_src/cli.c
test_src/cli.h
test_src/main-sha1-test.c
test_src/main-sha256-test.c
test_src/shavs.c
test_src/shavs.h

diff --git a/aes.c b/aes.c
deleted file mode 100644 (file)
index 5f68a5d..0000000
--- a/aes.c
+++ /dev/null
@@ -1,31 +0,0 @@
-/* aes.c */
-/*
-    This file is part of the Crypto-avr-lib/microcrypt-lib.
-    Copyright (C) 2008  Daniel Otte (daniel.otte@rub.de)
-
-    This program is free software: you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation, either version 3 of the License, or
-    (at your option) any later version.
-
-    This program is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with this program.  If not, see <http://www.gnu.org/licenses/>.
-*/
-/**
- * \file     aes.c
- * \email    daniel.otte@rub.de
- * \author   Daniel Otte 
- * \date     2008-12-29
- * \license  GPLv3 or later
- * 
- */
-
-#include <stdint.h>
-#include "aes.h"
-
-
index cd6c026115293c8198f45e7efa06a555aa4cb8c5..3bd41e5cba92c0b18323434cbd7de9dd5d0fc6e8 100644 (file)
@@ -27,7 +27,7 @@ uint8_t hfal_hash_init(const hfdesc_t* hash_descriptor, hfgen_ctx_t* ctx){
        ctx->desc_ptr = (hfdesc_t*)hash_descriptor;
        if(!(ctx->ctx=malloc(pgm_read_word(&(hash_descriptor->ctxsize_B)))))
                return 3;
-       f= (hf_init_fpt)pgm_read_word(hash_descriptor->init);
+       f= (hf_init_fpt)pgm_read_word(&(hash_descriptor->init));
        f(ctx->ctx);
        return 0;
 }
index 5fe4c8eac3e6257aa92b50c1d729fa22735a8b3e..b9659fe2cd544fe1a9814a31343d8faf6cdb75ad 100644 (file)
@@ -81,6 +81,17 @@ void cli_putstr_P(PGM_P s){
        }
 }
 
+uint8_t cli_getsn(char* s, uint16_t n){
+       char c;
+       if(n==0)
+               return 2;
+       while((c=cli_getc_cecho())!='\0' && c!='\r' && n--){
+               *s++=c;
+       }
+       *s='\0';
+       return (c=='\r')?0:1;
+}
+
 void cli_hexdump(void* data, uint16_t length){
        char hex_tab[] = {'0', '1', '2', '3', 
                          '4', '5', '6', '7', 
index 3fbe8a7949ef1055d26f23a324512cd8088bdaf1..acb68521c44dafd55e9d2ec2db5997dbeb9514d6 100644 (file)
@@ -47,6 +47,7 @@ extern uint8_t cli_echo;
 void cli_putc(char c);
 uint16_t cli_getc(void);
 uint16_t cli_getc_cecho(void);
+uint8_t cli_getsn(char* s, uint16_t n);
 void cli_putstr(char* s);
 void cli_putstr_P(PGM_P s);
 void cli_hexdump(void* data, uint16_t length);
index 1ba4bf04bf311c8bcd9f6e9d715f7a61dd21ae1a..e8ef85f7d54d6a069cdcc1b88723fd8bbeb367af 100644 (file)
@@ -104,6 +104,24 @@ void testrun_sha1(void){
        uart_putstr("\r\nx");
 }
 
+
+void testrun_sha1_2(void){
+       sha1_ctx_t ctx;
+       sha1_hash_t hash;
+       sha1(&hash,"",0);
+       uart_putstr("\r\nsha1(NULL) = \r\n\t");
+       uart_hexdump(hash,SHA1_HASH_BYTES);
+
+       memset(hash, 0, SHA1_HASH_BYTES);
+
+       sha1_init(&ctx);
+       sha1_lastBlock(&ctx, "", 0);
+       sha1_ctx2hash(hash, &ctx); 
+       uart_putstr("\r\nsha1(NULL) = \r\n\t");
+       uart_hexdump(hash,SHA1_HASH_BYTES);
+}
+
+
 void testrun_performance_sha1(void){
        uint64_t t;
        char str[16];
@@ -148,12 +166,14 @@ void testrun_performance_sha1(void){
 
 const char nessie_str[]      PROGMEM = "nessie";
 const char test_str[]        PROGMEM = "test";
+const char test2_str[]       PROGMEM = "test2";
 const char performance_str[] PROGMEM = "performance";
 const char echo_str[]        PROGMEM = "echo";
 
 cmdlist_entry_t cmdlist[] PROGMEM = {
        { nessie_str,      NULL, testrun_nessie_sha1},
        { test_str,        NULL, testrun_sha1},
+       { test2_str,       NULL, testrun_sha1_2},
        { performance_str, NULL, testrun_performance_sha1},
        { echo_str,    (void*)1, (void_fpt)echo_ctrl},
        { NULL,            NULL, NULL}
index d1b3c5063057edf749b9f980d1624b9b962ccc93..f6f48b13d7aadfc36024696f2fe44827a929125d 100644 (file)
@@ -111,9 +111,10 @@ const char performance_str[] PROGMEM = "performance";
 const char echo_str[]        PROGMEM = "echo";
 const char shavs_list_str[]  PROGMEM = "shavs_list";
 const char shavs_set_str[]   PROGMEM = "shavs_set";
+const char shavs_test1_str[] PROGMEM = "shavs_test1";
 const char dump_str[]        PROGMEM = "dump";
 
-const hfdesc_t* algo_list[] PROGMEM = {
+const hfdesc_t* algolist[] PROGMEM = {
        (hfdesc_t*)&sha256_desc,
        NULL
 };
@@ -125,6 +126,7 @@ cmdlist_entry_t cmdlist[] PROGMEM = {
        { echo_str,        (void*)1, (void_fpt)echo_ctrl},
        { shavs_list_str,      NULL, shavs_listalgos},
        { shavs_set_str,   (void*)1, (void_fpt)shavs_setalgo},
+       { shavs_test1_str,     NULL, shavs_test1},
        { dump_str,        (void*)1, (void_fpt)dump},
        { NULL,                NULL, NULL}
 };
@@ -134,7 +136,8 @@ int main (void){
        uart_putstr("\r\n");
        cli_rx = uart_getc;
        cli_tx = uart_putc;             
-       algolist=(hfdesc_t**)algo_list;
+       shavs_algolist=(hfdesc_t**)algolist;
+       shavs_algo=(hfdesc_t*)&sha256_desc;
        for(;;){
                uart_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
                uart_putstr(algo_name);
index bde453b710a430678d3d942ef145d162a49966c5..39c87bb754932354391233e4e4a9220b6de2bdbd 100644 (file)
 #include <avr/pgmspace.h>
 #include <stdint.h>
 #include <stdlib.h>
+#include <ctype.h>
 #include "hashfunction_descriptor.h"
+#include "hfal-basic.h"
 #include "shavs.h"
 #include "string-extras.h"
 #include "cli.h"
 
-hfdesc_t*  algo=NULL;
-hfdesc_t** algolist=NULL;
+hfdesc_t*  shavs_algo=NULL;
+hfdesc_t** shavs_algolist=NULL;
 
 void shavs_listalgos(void){
        char option = 'a';
-       
-       cli_putstr_P(PSTR("\r\nDBG: &algolist: "));
-       cli_hexdump(&algolist, 2);
-       cli_putstr_P(PSTR("\r\nDBG: algolist[0]: "));
-       cli_hexdump(algolist, 8);
-       
+               
        hfdesc_t* t;
        uint8_t i=0;
        cli_putstr_P(PSTR("\r\nthe following algorithms are available:\r\n"));
-       while(option<='z' && (t=(hfdesc_t*)pgm_read_word(&(algolist[i])))){
+       while(option<='z' && (t=(hfdesc_t*)pgm_read_word(&(shavs_algolist[i])))){
                cli_putc('\t');
-               cli_putc((t==algo)?'*':' ');
+               cli_putc((t==shavs_algo)?'*':' ');
                cli_putc(option++);
                cli_putstr_P(PSTR(":\t"));
                cli_putstr_P((void*)(pgm_read_word(&(t->name))));
@@ -62,25 +59,25 @@ void shavs_setalgo(char* param){
        if(param[1]=='\0'){ /* single letter specified */
                uint8_t i,option = param[0]-'a';
                
-               if(!algolist){
-                       cli_putstr_P(PSTR("\r\nERROR: algolist not set!"));
+               if(!shavs_algolist){
+                       cli_putstr_P(PSTR("\r\nERROR: shavs_algolist not set!"));
                        return;
                }
                for(i=0; i<=option; ++i){
-                       if((void*)pgm_read_word(&(algolist[i]))==NULL){
+                       if((void*)pgm_read_word(&(shavs_algolist[i]))==NULL){
                                cli_putstr_P(PSTR("\r\nERROR: invalid selection!"));
                                return;
                        }
                }
-               algo=(hfdesc_t*)pgm_read_word(&(algolist[option]));
+               shavs_algo=(hfdesc_t*)pgm_read_word(&(shavs_algolist[option]));
        } else { /* name specifyed */ 
                hfdesc_t* t=NULL;
                uint8_t i=0;
-               while((t=(hfdesc_t*)pgm_read_word(&(algolist[i]))) &&
+               while((t=(hfdesc_t*)pgm_read_word(&(shavs_algolist[i]))) &&
                       strcasecmp_P(param, (void*)pgm_read_word(&(t->name))))
                        ++i;
                if(t){
-                       algo=t;
+                       shavs_algo=t;
                }else{
                        cli_putstr_P(PSTR("\r\nERROR: could not find \""));
                        cli_putstr(param);
@@ -89,3 +86,141 @@ void shavs_setalgo(char* param){
        }
 }
 
+static uint16_t buffer_idx=0;
+static uint8_t  in_byte=0;
+static uint8_t* buffer;
+static uint16_t buffersize_B;
+static hfgen_ctx_t ctx;
+
+static
+uint8_t buffer_add(char c){
+       uint8_t v,t;
+       if(c>='0' && c<='9'){
+               v=c-'0';
+       }else{
+               if(c>='a' && c<='f'){
+                       v=c-'a'+10;
+               }else{
+                       if(c>='A' && c<='F'){
+                               v=c-'A'+10;
+                       }else{
+                               return 1;
+                       }
+               }
+                       
+       }
+       if(buffer_idx==buffersize_B){
+               hfal_hash_nextBlock(&ctx, buffer);
+               buffer_idx=0;
+               in_byte=0;
+       }
+
+       t=buffer[buffer_idx];
+       if(in_byte){
+               t = (t&0xF0) | v;
+               buffer[buffer_idx]=t;
+               buffer_idx++;
+       }else{
+               t = (t&0x0F) | (v<<4);
+               buffer[buffer_idx]=t;
+       }
+       in_byte ^= 1;
+       return 0;
+}
+
+void shavs_test1(void){
+       char lenstr[21];
+       char* len2;
+       uint32_t length=0;
+       uint8_t len_set=0;
+       if(!shavs_algo){
+               cli_putstr_P(PSTR("\r\nERROR: select algorithm first!"));
+               return;
+       }
+       
+       buffersize_B=pgm_read_word(&(shavs_algo->blocksize_b))/8;
+       buffer = malloc(buffersize_B);
+
+       for(;;){
+               do{     
+                       cli_putstr_P(PSTR("\r\n"));
+                       cli_getsn(lenstr, 20);
+                       len2 = strstrip(lenstr);
+                       if(!strncasecmp_P(len2, PSTR("LEN"), 3)){
+                               while(*len2 && *len2!='=')
+                                       len2++;
+                               if(*len2=='='){
+                                       len2++;
+                                       length=strtoul(len2, NULL, 0);
+                                       len_set=1;
+                               }
+                       } else {
+                               if(!strncasecmp_P(len2, PSTR("EXIT"), 4)){
+                                       return;
+                               }
+                       }               
+               }while(!len_set);
+               volatile int16_t expect_input;
+               char c;
+                               
+               if(length==0){
+                       expect_input=2;
+               }else{
+                       expect_input=((length+7)/8)*2;
+               }
+               
+               buffer_idx = 0;
+               in_byte=0;
+               len_set = 0;
+
+               hfal_hash_init(shavs_algo, &ctx);
+               cli_putstr_P(PSTR("\r\n"));
+               while((c=cli_getc_cecho())!='M' && c!='m'){
+                       if(!isblank(c)){
+                               cli_putstr_P(PSTR("\r\nERROR: wrong input (1)!\r\n"));
+                               return;
+                       }
+               }
+               if((c=cli_getc_cecho())!='s' && c!='S'){
+                               cli_putstr_P(PSTR("\r\nERROR: wrong input (2)!\r\n"));
+                               return;
+               }
+               if((c=cli_getc_cecho())!='g' && c!='G'){
+                               cli_putstr_P(PSTR("\r\nERROR: wrong input (3)!\r\n"));
+                               return;
+               }
+               while((c=cli_getc_cecho())!='='){
+                       if(!isblank(c)){
+                               cli_putstr_P(PSTR("\r\nERROR: wrong input (4)!\r\n"));
+                               return;
+                       }
+               }
+               
+               buffer_idx=0;
+               while(expect_input>0){
+                       c=cli_getc_cecho();
+                       if(buffer_add(c)==0){
+                               --expect_input;
+                       }else{
+                               if(!isblank((uint16_t)c)){
+                                       cli_putstr_P(PSTR("\r\nERROR: wrong input (5) ("));
+                                       cli_putc(c);
+                                       cli_putstr_P(PSTR(")!\r\n"));
+                                       return;
+                               }
+                       }
+               }
+               
+               uint8_t diggest[pgm_read_word(shavs_algo->hashsize_b)/8];
+               if(length%(buffersize_B*8)==0)
+                       hfal_hash_nextBlock(&ctx, buffer);
+               hfal_hash_lastBlock(&ctx, buffer, length%(buffersize_B*8));
+               hfal_hash_ctx2hash(diggest, &ctx);
+               hfal_hash_free(&ctx);
+               cli_putstr_P(PSTR("\r\n MD = "));
+               cli_hexdump(diggest, pgm_read_word(&(shavs_algo->hashsize_b))/8);
+               
+       }
+       
+}
+
index a64f3a648d96450f490b50963854cddd87280633..11c03802b82ab8b42cacb4ca7c3c0bef391f2a18 100644 (file)
 #include <stdlib.h>
 #include "hashfunction_descriptor.h"
 
-extern hfdesc_t*  algo;
-extern hfdesc_t** algolist;
+extern hfdesc_t*  shavs_algo;
+extern hfdesc_t** shavs_algolist;
 
 void shavs_listalgos(void);
 void shavs_setalgo(char* param);
+void shavs_test1(void);
 
 #endif /* SHAVS */