verification seems to work now...

This commit is contained in:
bg 2009-11-05 05:33:56 +00:00
parent 27e7d731ff
commit 302f07c974
53 changed files with 273 additions and 273 deletions

View File

@ -1,6 +1,6 @@
MCU_TARGET = atmega644
OPTIMIZE = -Os
OPTIMIZE = -Os # -Os
PROGRAMMER = avr911
DEFS = -D$(call uc, $(MCU_TARGET))
FLASHCMD = avrdude -p $(MCU_TARGET) -P /dev/ttyUSB0 -c $(PROGRAMMER) -U flash:w:# no space at the end

View File

@ -36,13 +36,18 @@
#define ROTL32(a,n) (((a)<<(n))|((a)>>(32-(n))))
#define ROTR32(a,n) (((a)>>(n))|((a)<<(32-(n))))
#define BUG24 0
#define BUG_ROT 1
#define TWEAK 1
#if TWEAK
# define BUG24 0
#else
# define BUG24 1
#endif
#define F0_HACK 1
#define DEBUG 0
#if DEBUG
#include "cli.h"
@ -198,19 +203,11 @@ uint32_t bmw_small_expand1(uint8_t j, const uint32_t* q, const void* m, const vo
r += s[i%4](q[j+i]);
}
#if TWEAK
# if BUG_ROT
r += ( ROTL32(((uint32_t*)m)[j&0xf], ((j+0)&0xf)+1 )
+ ROTL32(((uint32_t*)m)[(j+3)&0xf], ((j+3)&0xf)+1 )
- ROTL32(((uint32_t*)m)[(j+10)&0xf], ((j+10)&0xf)+1 )
+ pgm_read_dword(k_lut+j)
) ^ ((uint32_t*)h)[(j+7)&0xf];
# else
r += ( ROTL32(((uint32_t*)m)[j&0xf], (j+1)&0xf )
+ ROTL32(((uint32_t*)m)[(j+3)&0xf], (j+4)&0xf )
- ROTL32(((uint32_t*)m)[(j+10)&0xf], (j+11)&0xf )
+ pgm_read_dword(k_lut+j)
) ^ ((uint32_t*)h)[(j+7)&0xf];
# endif
#else
r += pgm_read_dword(k_lut+j);
r += ((uint32_t*)m)[j&0xf];
@ -240,20 +237,11 @@ uint32_t bmw_small_expand2(uint8_t j, const uint32_t* q, const void* m, const vo
r += bmw_small_s4(q[j+15]);
#endif
#if TWEAK
# if BUG_ROT
r += ( ROTL32(((uint32_t*)m)[j&0xf], ((j+0)&0xf)+1 )
+ ROTL32(((uint32_t*)m)[(j+3)&0xf], ((j+3)&0xf)+1 )
- ROTL32(((uint32_t*)m)[(j+10)&0xf], ((j+10)&0xf)+1 )
+ pgm_read_dword(k_lut+j)
) ^ ((uint32_t*)h)[(j+7)&0xf];
# else
r += ( ROTL32(((uint32_t*)m)[j&0xf], (j+1)&0xf )
+ ROTL32(((uint32_t*)m)[(j+3)&0xf], (j+4)&0xf )
- ROTL32(((uint32_t*)m)[(j+10)&0xf], (j+11)&0xf )
+ pgm_read_dword(k_lut+j)
) ^ ((uint32_t*)h)[(j+7)&0xf];
#endif
#else
r += pgm_read_dword(k_lut+j);
r += ((uint32_t*)m)[j&0xf];
@ -284,7 +272,7 @@ uint8_t f0_lut[] PROGMEM = {
12<<1, ( 4<<1)+1, ( 6<<1)+1, ( 9<<1)+1, (13<<1)+0
};
void bmw_small_f0(uint32_t* q, const uint32_t* h, const void* m){
void bmw_small_f0(uint32_t* q, uint32_t* h, const void* m){
uint8_t i,j=-1,v,sign,l=0;
uint32_t(*s[])(uint32_t)={ bmw_small_s0, bmw_small_s1, bmw_small_s2,
bmw_small_s3, bmw_small_s4 };
@ -324,7 +312,7 @@ void bmw_small_f0(uint32_t* q, const uint32_t* h, const void* m){
}
#else
void bmw_small_f0(uint32_t* q, const uint32_t* h, const void* m){
void bmw_small_f0(uint32_t* q, uint32_t* h, const void* m){
uint8_t i;
uint32_t(*s[])(uint32_t)={ bmw_small_s0, bmw_small_s1, bmw_small_s2,
bmw_small_s3, bmw_small_s4 };

View File

@ -24,21 +24,23 @@
uint8_t hfal_hash_init(const hfdesc_t* hash_descriptor, hfgen_ctx_t* ctx){
hf_init_fpt f;
uint16_t tmp;
ctx->desc_ptr = (hfdesc_t*)hash_descriptor;
if(!(ctx->ctx=malloc(pgm_read_word(&(hash_descriptor->ctxsize_B)))))
tmp = pgm_read_word(&(hash_descriptor->ctxsize_B));
if(!(ctx->ctx=malloc(tmp)))
return 3;
f= (hf_init_fpt)pgm_read_word(&(hash_descriptor->init));
f(ctx->ctx);
return 0;
}
void hfal_hash_nextBlock(hfgen_ctx_t* ctx, const void* block){
hf_nextBlock_fpt f;
hfdesc_t* x=(ctx->desc_ptr);
f =(hf_nextBlock_fpt)pgm_read_word(&(x->nextBlock));
f(ctx->ctx, block);
}
void hfal_hash_lastBlock(hfgen_ctx_t* ctx, const void* block, uint16_t length_b){
hf_lastBlock_fpt f;
hfdesc_t* x=ctx->desc_ptr;
@ -68,7 +70,7 @@ void hfal_hash_mem(const hfdesc_t* hash_descriptor, void* dest, const void* msg,
if(f){
((hf_mem_fpt)f)(dest, msg, length_b);
}else{
uint16_t bs,bsb;
uint8_t ctx[pgm_read_word(&(hash_descriptor->ctxsize_B))];
f=(void_fpt)pgm_read_word(&(hash_descriptor->init));
@ -86,7 +88,7 @@ void hfal_hash_mem(const hfdesc_t* hash_descriptor, void* dest, const void* msg,
f=(void_fpt)pgm_read_word(&(hash_descriptor->ctx2hash));
((hf_ctx2hash_fpt)f)(dest, ctx);
}
}
}
uint16_t hfal_hash_getBlocksize(const hfdesc_t* hash_descriptor){
uint16_t ret;

View File

@ -19,16 +19,16 @@
=end
$debug = true;
$debug = false;
#$debug = false;
require 'rubygems'
require 'serialport'
def init_system
# sleep 1
$sp.print("exit\r")
sleep 0.1
sleep 0.5
$sp.print("exit\r")
sleep 0.1
sleep 0.5
$sp.print("echo off \r")
print("DBG i: " + "echo off \r"+"\n") if $debug
# line = $sp.readlines()
@ -52,16 +52,17 @@ def get_md
begin
line = $sp.gets()
line = "" if line==nil
puts("DBG g: "+line) if $debug
puts("DBG got: "+line) if $debug && line!=""
end while not /[\s]*MD[\s]*=.*/.match(line)
return line
end
def send_md(md_string)
sleep(0.15)
for i in 0..md_string.length-1
$sp.print(md_string[i].chr)
# print("DBG s: "+ md_string[i].chr) if $debug
if(i%20==19)
if(i%5==4)
sleep(0.15)
end
end
@ -100,6 +101,7 @@ def run_test(filename)
b.upcase!
printf("\n%4d (%4d): ", line, (line-1)*$linewidth) if (pos%$linewidth==0 and $linewidth!=0)
line += 1 if (pos%$linewidth==0 and $linewidth!=0)
sleep(1)
#putc((a==b)?'*':'!')
if(a==b)
putc('*')

View File

@ -56,13 +56,13 @@ cli_getc_cecho:
ser r25
ser r24
ret
1:
1:
icall
lds r20, cli_echo
tst r20
brne 2f
ret
2:
2:
push r2
mov r2, r24
rcall cli_putc
@ -70,7 +70,7 @@ cli_getc_cecho:
clr r25
pop r2
ret
/******************************************************************************/
.global cli_putstr
cli_putstr:
@ -84,7 +84,7 @@ cli_putstr:
breq 2f
rcall cli_putc
rjmp 1b
2:
2:
pop r29
pop r28
ret
@ -104,7 +104,7 @@ cli_putstr_P:
rcall cli_putc
adiw r28, 1
rjmp 1b
2:
2:
pop r29
pop r28
ret
@ -113,7 +113,7 @@ cli_putstr_P:
/*
* param s: r24:r25
* param n: r22:r23
*/
*/
.global cli_getsn
cli_getsn:
push r28
@ -137,14 +137,46 @@ cli_getsn:
st Y+, r24
movw r26, r16
sbiw r26, 1
movw r16, r24
movw r16, r26
brne 2b
9:
9:
clr r25
mov r24, r20
st Y, r1
pop r17
pop r16
pop r28
pop r29
pop r28
ret
/******************************************************************************/
/*
* param s: r24:r25
* param n: r22:r23
*/
.global cli_getsn_cecho
cli_getsn_cecho:
push r28
push r29
push r16
push r17
movw r28, r24
ldi r20, 2
movw r24, r22
adiw r24, 0
breq 9b
1:
movw r16, r22
2: clr r20
rcall cli_getc_cecho
cpi r24, '\r'
breq 9b
ldi r20, 1
tst r24
breq 9b
st Y+, r24
movw r26, r16
sbiw r26, 1
movw r16, r26
brne 2b
rjmp 9b

View File

@ -19,7 +19,7 @@
#include "avr-asm-macros.S"
/******************************************************************************/
/* cli_hexdump_byte
/* cli_hexdump_byte
* param data: r24
*/
.global cli_hexdump_byte
@ -43,10 +43,10 @@ cli_hexdump_byte:
clr r25
lpm r24, Z
rcall cli_putc
ret
ret
/******************************************************************************/
/* cli_hexdump
/* cli_hexdump
* param data: r24:r25
* param length: r22:r23
*/
@ -63,19 +63,19 @@ cli_hexdump:
breq hexdump_exit
2:
ld r24, Y+
rcall cli_hexdump_byte
rcall cli_hexdump_byte
subi r16, 1
sbc r17, r1
sbci r17, 0
brne 2b
hexdump_exit:
hexdump_exit:
pop r17
pop r16
pop r29
pop r28
ret
/******************************************************************************/
/* cli_hexdump_rev
/* cli_hexdump_rev
* param data: r24:r25
* param length: r22:r23
*/
@ -94,11 +94,11 @@ cli_hexdump_rev:
1:
breq hexdump_exit
ld r24, -Y
rcall cli_hexdump_byte
rcall cli_hexdump_byte
subi r16, 1
sbci r17, 0
rjmp 1b
/******************************************************************************/
/* cli_hexdump2
* param data: r24:r25
@ -114,14 +114,14 @@ cli_hexdump2:
movw r16, r22
movw r26, r16
adiw r26, 0
1:
1:
breq hexdump_exit
ld r24, Y+
rcall cli_hexdump_byte
clr r25
ldi r24,' '
rcall cli_putc
subi r16, 1
sbci r17, 0
rjmp 1b
@ -158,7 +158,7 @@ cli_hexdump_block:
mov WIDTH, r18
mov INDENT, r20
movw DATA_0, r24
movw LENG_0, r22
movw LENG_0, r22
2:
clr r25
ldi r24, '\r'
@ -184,15 +184,15 @@ cli_hexdump_block:
breq 6f
brmi 7f
mov r22, LENG_0
6: inc r4
7:
6: inc r4
7:
rcall cli_hexdump2
add DATA_0, WIDTH
adc DATA_1, r1
sub LENG_0, WIDTH
sbc LENG_0, r1
tst r4
breq 2b
breq 2b
pop r4
pop LENG_1
pop LENG_0
@ -201,5 +201,5 @@ cli_hexdump_block:
pop INDENT
pop WIDTH
ret

View File

@ -102,11 +102,12 @@ int8_t cmd_interface(PGM_VOID_P cmd_desc){
free(cli_buffer);
return exit_code;
}
cli_putstr(cli_buffer);
/* cli_putstr(cli_buffer); */
memset(cli_buffer, 0, cli_buffer_size);
cli_buffer_index=0;
cli_putstr_P(PSTR(" DONE\r\n>"));
/* cli_putstr_P(PSTR(" DONE\r\n>")); */
cli_putstr_P(PSTR("\r\n>"));
completion_failed=0;
break;
case CLI_BACKSPACE:

View File

@ -58,6 +58,7 @@ 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);
uint8_t cli_getsn_cecho(char* s, uint16_t n);
void cli_putstr(const char* s);
void cli_putstr_P(PGM_P s);
void cli_hexdump(const void* data, uint16_t length);

View File

@ -22,7 +22,7 @@
*/
#include "config.h"
#include "serial-tools.h"
#include "uart_i.h"
#include "debug.h"

View File

@ -22,7 +22,7 @@
*/
#include "config.h"
#include "serial-tools.h"
#include "uart_i.h"
#include "debug.h"

View File

@ -22,7 +22,7 @@
*/
#include "config.h"
#include "serial-tools.h"
#include "uart_i.h"
#include "debug.h"

View File

@ -22,7 +22,7 @@
*/
#include "config.h"
#include "serial-tools.h"
#include "uart_i.h"
#include "debug.h"

View File

@ -22,7 +22,7 @@
*/
#include "config.h"
#include "serial-tools.h"
#include "uart_i.h"
#include "debug.h"

View File

@ -22,7 +22,7 @@
*/
#include "config.h"
#include "serial-tools.h"
#include "uart_i.h"
#include "debug.h"

View File

@ -22,7 +22,7 @@
*/
#include "config.h"
#include "serial-tools.h"
#include "uart_i.h"
#include "debug.h"

View File

@ -22,7 +22,7 @@
*/
#include "config.h"
#include "serial-tools.h"
#include "uart_i.h"
#include "debug.h"

View File

@ -22,7 +22,6 @@
*/
#include "config.h"
#include "serial-tools.h"
#include "uart_i.h"
#include "debug.h"

View File

@ -22,7 +22,7 @@
*/
#include "config.h"
#include "serial-tools.h"
#include "uart_i.h"
#include "debug.h"

View File

@ -22,7 +22,7 @@
*/
#include "config.h"
#include "serial-tools.h"
#include "uart_i.h"
#include "debug.h"

View File

@ -4,7 +4,7 @@
*/
#include "config.h"
#include "serial-tools.h"
#include "uart_i.h"
#include "debug.h"

View File

@ -22,7 +22,7 @@
*/
#include "config.h"
#include "serial-tools.h"
#include "uart_i.h"
#include "debug.h"

View File

@ -22,7 +22,7 @@
*/
#include "config.h"
#include "serial-tools.h"
#include "uart_i.h"
#include "debug.h"

View File

@ -22,7 +22,7 @@
*/
#include "config.h"
#include "serial-tools.h"
#include "uart_i.h"
#include "debug.h"
#include "cli.h"

View File

@ -22,7 +22,7 @@
*/
#include "config.h"
#include "serial-tools.h"
#include "uart_i.h"
#include "debug.h"

View File

@ -22,7 +22,7 @@
*/
#include "config.h"
#include "serial-tools.h"
#include "uart_i.h"
#include "debug.h"

View File

@ -22,7 +22,7 @@
*/
#include "config.h"
#include "serial-tools.h"
#include "uart_i.h"
#include "debug.h"

View File

@ -22,7 +22,7 @@
*/
#include "config.h"
#include "serial-tools.h"
#include "uart_i.h"
#include "debug.h"

View File

@ -22,7 +22,7 @@
*/
#include "config.h"
#include "serial-tools.h"
#include "uart_i.h"
#include "debug.h"

View File

@ -4,7 +4,7 @@
*/
#include "config.h"
#include "serial-tools.h"
#include "uart_i.h"
#include "debug.h"
#include "cli.h"

View File

@ -22,7 +22,7 @@
*/
#include "config.h"
#include "serial-tools.h"
#include "uart_i.h"
#include "debug.h"

View File

@ -22,7 +22,7 @@
*/
#include "config.h"
#include "serial-tools.h"
#include "uart_i.h"
#include "debug.h"

View File

@ -22,7 +22,7 @@
*/
#include "config.h"
#include "serial-tools.h"
#include "uart_i.h"
#include "debug.h"

View File

@ -22,7 +22,7 @@
*/
#include "config.h"
#include "serial-tools.h"
#include "uart_i.h"
#include "debug.h"

View File

@ -22,7 +22,7 @@
*/
#include "config.h"
#include "serial-tools.h"
#include "uart_i.h"
#include "debug.h"

View File

@ -22,7 +22,7 @@
*/
#include "config.h"
#include "serial-tools.h"
#include "uart_i.h"
#include "debug.h"

View File

@ -27,7 +27,7 @@
*
*/
#include "config.h"
#include "serial-tools.h"
#include "uart_i.h"
#include "debug.h"

View File

@ -22,7 +22,7 @@
*/
#include "config.h"
#include "serial-tools.h"
#include "uart_i.h"
#include "debug.h"

View File

@ -22,7 +22,7 @@
*/
#include "config.h"
#include "serial-tools.h"
#include "uart_i.h"
#include "debug.h"

View File

@ -22,7 +22,7 @@
*/
#include "config.h"
#include "serial-tools.h"
#include "uart_i.h"
#include "debug.h"

View File

@ -22,7 +22,7 @@
*/
#include "config.h"
#include "serial-tools.h"
#include "uart_i.h"
#include "debug.h"

View File

@ -26,7 +26,7 @@
*
*/
#include "config.h"
#include "serial-tools.h"
#include "uart_i.h"
#include "debug.h"

View File

@ -22,7 +22,7 @@
*/
#include "config.h"
#include "serial-tools.h"
#include "uart_i.h"
#include "debug.h"

View File

@ -22,7 +22,7 @@
*/
#include "config.h"
#include "serial-tools.h"
#include "uart_i.h"
#include "debug.h"

View File

@ -22,7 +22,7 @@
*/
#include "config.h"
#include "serial-tools.h"
#include "uart_i.h"
#include "debug.h"

View File

@ -22,7 +22,7 @@
*/
#include "config.h"
#include "serial-tools.h"
#include "uart_i.h"
#include "debug.h"

View File

@ -22,7 +22,7 @@
*/
#include "config.h"
#include "serial-tools.h"
#include "uart_i.h"
#include "debug.h"

View File

@ -22,7 +22,7 @@
*/
#include "config.h"
#include "serial-tools.h"
#include "uart_i.h"
#include "debug.h"

View File

@ -22,7 +22,7 @@
*/
#include "config.h"
#include "serial-tools.h"
#include "uart_i.h"
#include "debug.h"
#include "cli.h"

View File

@ -22,7 +22,7 @@
*/
#include "config.h"
#include "serial-tools.h"
#include "uart_i.h"
#include "debug.h"

View File

@ -22,7 +22,7 @@
*/
#include "config.h"
#include "serial-tools.h"
#include "uart_i.h"
#include "debug.h"

View File

@ -22,7 +22,7 @@
*/
#include "config.h"
#include "serial-tools.h"
#include "uart_i.h"
#include "debug.h"

View File

@ -1,84 +0,0 @@
/* serial-tools.c */
/*
This file is part of the AVR-Crypto-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/>.
*/
/**
*
* Author: Daniel Otte
* Date: 16.05.2006
*
* This tools should help to parse some input.
*
*/
#include "config.h"
#include "uart_i.h"
#include <string.h>
#include <stdint.h>
int getnextwordn(char *s, int n){ /* words are seperated by spaces, lf or cr */
char c = ' ';
do{
c=uart0_getc();
}while(c==' ' || c=='\r' || c=='\n');
*s++ = c;
do{
*s++ = c = uart0_getc();
}while(c!=' ' && c!='\r' && c!='\n' && --n);
*(s-1) = '\0';
return n;
}
void readhex2buffer(void* buffer, int n){
char c;
uint8_t i;
// DEBUG_S("\r\nDBG: n="); DEBUG_B(n&0xff); DEBUG_S("\r\n");
for(i=0; i<n; ++i){
c = uart0_getc();
if ('0'<= c && '9'>=c){
((uint8_t*)buffer)[i] = c - '0';
} else {
c &= ~('A' ^ 'a'); /* make all uppercase */
if ('A'<= c && 'F'>=c){
((uint8_t*)buffer)[i] = c - 'A' + 10;
} else {
/* oh shit, wrong char */
}
}
((uint8_t*)buffer)[i] <<= 4;
c = uart0_getc();
if ('0'<= c && '9'>=c){
((uint8_t*)buffer)[i] |= c - '0';
} else {
c &= ~('A' ^ 'a'); /* make all uppercase */
if ('A'<= c && 'F'>=c){
((uint8_t*)buffer)[i] |= c - 'A' + 10;
} else {
/* oh shit, wrong char */
}
}
} /* for i=0 .. n */
}
void uart0_putptr(void* p){
uart0_hexdump((void*) &p,2);
}

View File

@ -1,7 +1,7 @@
/* shavs.c */
/*
This file is part of the AVR-Crypto-Lib.
Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de)
Copyright (C) 2006 2007 2008 2009 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
@ -27,6 +27,7 @@
#include <avr/pgmspace.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "hashfunction_descriptor.h"
#include "hfal-basic.h"
@ -34,6 +35,18 @@
#include "string-extras.h"
#include "cli.h"
#ifdef DEBUG
# undef DEBUG
#endif
#define DEBUG 0
#if DEBUG
# include "config.h"
# include <util/delay.h>
#endif
hfdesc_t* shavs_algo=NULL;
hfdesc_t** shavs_algolist=NULL;
@ -86,144 +99,175 @@ void shavs_setalgo(char* param){
}
}
static uint16_t buffer_idx=0;
static uint8_t in_byte=0;
static uint16_t blocks=0;
static uint8_t* buffer;
static uint16_t buffersize_B;
static hfgen_ctx_t ctx;
typedef struct {
uint16_t buffer_idx;
uint16_t buffersize_B;
uint32_t blocks;
hfgen_ctx_t ctx;
uint8_t* buffer;
uint8_t in_byte;
} shavs_ctx_t;
static shavs_ctx_t shavs_ctx;
static
uint8_t buffer_add(char c){
uint8_t v,t;
if(buffer_idx==buffersize_B){
hfal_hash_nextBlock(&ctx, buffer);
++blocks;
buffer_idx=0;
in_byte=0;
if(shavs_ctx.buffer_idx==shavs_ctx.buffersize_B){
hfal_hash_nextBlock(&(shavs_ctx.ctx), shavs_ctx.buffer);
++shavs_ctx.blocks;
shavs_ctx.buffer_idx=0;
shavs_ctx.in_byte=0;
cli_putc('.');
}
if(c>='0' && c<='9'){
v=c-'0';
}else{
if(c>='a' && c<='f'){
v=c-'a'+10;
c &= (uint8_t)~('a' ^ 'A');
if(c>='A' && c<='F'){
v=c-'A'+10;
}else{
if(c>='A' && c<='F'){
v=c-'A'+10;
}else{
return 1;
}
return 1;
}
}
t=buffer[buffer_idx];
if(in_byte){
t=shavs_ctx.buffer[shavs_ctx.buffer_idx];
if(shavs_ctx.in_byte){
t = (t&0xF0) | v;
buffer[buffer_idx]=t;
buffer_idx++;
shavs_ctx.buffer[shavs_ctx.buffer_idx]=t;
shavs_ctx.buffer_idx++;
}else{
t = (t&0x0F) | (v<<4);
buffer[buffer_idx]=t;
shavs_ctx.buffer[shavs_ctx.buffer_idx]=t;
}
in_byte ^= 1;
shavs_ctx.in_byte ^= 1;
return 0;
}
void shavs_test1(void){
int32_t getLength(void){
uint32_t len=0;
char lenstr[21];
char* len2;
for(;;){
memset(lenstr, 0, 21);
cli_getsn_cecho(lenstr, 20);
len2 = strstrip(lenstr);
if(!strncasecmp_P(len2, PSTR("LEN"), 3)){
while(*len2 && *len2!='=')
len2++;
if(*len2=='='){
do{
len2++;
}while(*len2 && !isdigit(*len2));
len=(uint32_t)strtoul(len2, NULL, 10);
return len;
}
} else {
if(!strncasecmp_P(len2, PSTR("EXIT"), 4)){
return -1;
}
}
}
}
void shavs_test1(void){
uint32_t length=0;
uint8_t len_set=0;
int32_t expect_input=0;
if(!shavs_algo){
cli_putstr_P(PSTR("\r\nERROR: select algorithm first!"));
return;
}
buffersize_B=pgm_read_word(&(shavs_algo->blocksize_b))/8;
cli_putstr_P(PSTR("\r\nbuffer allocated for 0x"));
cli_hexdump(&buffersize_B, 2);
uint8_t diggest[pgm_read_word(shavs_algo->hashsize_b)/8];
shavs_ctx.buffersize_B=pgm_read_word(&(shavs_algo->blocksize_b))/8;
uint8_t buffer[shavs_ctx.buffersize_B];
shavs_ctx.buffer = buffer;
cli_putstr_P(PSTR("\r\nbuffer_size = 0x"));
cli_hexdump_rev(&(shavs_ctx.buffersize_B), 2);
cli_putstr_P(PSTR(" bytes"));
buffer = malloc(buffersize_B);
if(buffer==NULL){
cli_putstr_P(PSTR("\r\n allocating memory for buffer failed!"));
return;
}
for(;;){
blocks = 0;
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)){
free(buffer);
return;
}
}
}while(!len_set);
volatile int32_t expect_input;
shavs_ctx.blocks = 0;
char c;
length = getLength();
if(length<0){
return;
}
#if DEBUG
cli_putstr_P(PSTR("\r\nLen == "));
cli_hexdump_rev(&length, 4);
#endif
if(length==0){
expect_input=2;
}else{
expect_input=((length+7)/8)*2;
expect_input=((length+7)>>2)&(~1L);
}
buffer_idx = 0;
in_byte=0;
len_set = 0;
#if DEBUG
cli_putstr_P(PSTR("\r\nexpected_input == "));
cli_hexdump_rev(&expect_input, 4);
if(expect_input==0)
cli_putstr_P(PSTR("\r\nexpected_input == 0 !!!"));
#endif
shavs_ctx.buffer_idx = 0;
shavs_ctx.in_byte = 0;
shavs_ctx.blocks = 0;
uint8_t ret;
// cli_putstr_P(PSTR("\r\n HFAL init"));
ret = hfal_hash_init(shavs_algo, &ctx);
#if DEBUG
cli_putstr_P(PSTR("\r\n HFAL init"));
cli_putstr_P(PSTR("\r\n (2) expected_input == "));
cli_hexdump_rev(&expect_input, 4);
#endif
ret = hfal_hash_init(shavs_algo, &(shavs_ctx.ctx));
//ret=0;
if(ret){
cli_putstr_P(PSTR("\r\n HFAL init returned with: "));
cli_hexdump(&ret, 1);
free(buffer);
return;
}
// cli_putstr_P(PSTR("\r\n"));
#if DEBUG
cli_putstr_P(PSTR("\r\n (3) expected_input == "));
cli_hexdump_rev(&expect_input, 4);
cli_putstr_P(PSTR("\r\n"));
#endif
while((c=cli_getc_cecho())!='M' && c!='m'){
if(!isblank(c)){
cli_putstr_P(PSTR("\r\nERROR: wrong input (1) [0x"));
cli_hexdump(&c, 1);
cli_putstr_P(PSTR("]!\r\n"));
free(buffer);
hfal_hash_free(&(shavs_ctx.ctx));
return;
}
}
if((c=cli_getc_cecho())!='s' && c!='S'){
cli_putstr_P(PSTR("\r\nERROR: wrong input (2)!\r\n"));
free(buffer);
hfal_hash_free(&(shavs_ctx.ctx));
return;
}
if((c=cli_getc_cecho())!='g' && c!='G'){
cli_putstr_P(PSTR("\r\nERROR: wrong input (3)!\r\n"));
free(buffer);
hfal_hash_free(&(shavs_ctx.ctx));
return;
}
while((c=cli_getc_cecho())!='='){
if(!isblank(c)){
cli_putstr_P(PSTR("\r\nERROR: wrong input (4)!\r\n"));
free(buffer);
hfal_hash_free(&(shavs_ctx.ctx));
return;
}
}
buffer_idx=0;
#if DEBUG
cli_putstr_P(PSTR("\r\nparsing started"));
#endif
shavs_ctx.buffer_idx = 0;
shavs_ctx.in_byte = 0;
shavs_ctx.blocks = 0;
while(expect_input>0){
c=cli_getc_cecho();
cli_putstr_P(PSTR("+("));
cli_hexdump_rev((uint8_t*)&expect_input, 4);
#if DEBUG
cli_putstr_P(PSTR("\r\n\t("));
cli_hexdump_rev(&expect_input, 4);
cli_putstr_P(PSTR(") "));
_delay_ms(500);
#endif
if(buffer_add(c)==0){
--expect_input;
}else{
@ -231,23 +275,38 @@ void shavs_test1(void){
cli_putstr_P(PSTR("\r\nERROR: wrong input (5) ("));
cli_putc(c);
cli_putstr_P(PSTR(")!\r\n"));
free(buffer);
hfal_hash_free(&(shavs_ctx.ctx));
return;
}
}
}
// cli_putstr_P(PSTR("\r\n starting finalisation"));
uint8_t diggest[pgm_read_word(shavs_algo->hashsize_b)/8];
// cli_putstr_P(PSTR("\r\n starting last block"));
hfal_hash_lastBlock(&ctx, buffer, length-blocks*(buffersize_B*8));
// cli_putstr_P(PSTR("\r\n starting ctx2hash"));
hfal_hash_ctx2hash(diggest, &ctx);
// cli_putstr_P(PSTR("\r\n starting hash free"));
hfal_hash_free(&ctx);
#if DEBUG
cli_putstr_P(PSTR("\r\n starting finalisation"));
cli_putstr_P(PSTR("\r\n\tblocks == "));
cli_hexdump_rev(&(shavs_ctx.blocks),4);
cli_putstr_P(PSTR("\r\n\tbuffer_idx == "));
cli_hexdump_rev(&(shavs_ctx.buffer_idx),2);
cli_putstr_P(PSTR("\r\n\tin_byte == "));
cli_hexdump_rev(&(shavs_ctx.in_byte),1);
_delay_ms(500);
cli_putstr_P(PSTR("\r\n starting last block"));
#endif
hfal_hash_lastBlock( &(shavs_ctx.ctx),
shavs_ctx.buffer,
length-(shavs_ctx.blocks)*((shavs_ctx.buffersize_B)*8));
#if DEBUG
cli_putstr_P(PSTR("\r\n starting ctx2hash"));
_delay_ms(500);
#endif
hfal_hash_ctx2hash(diggest, &(shavs_ctx.ctx));
#if DEBUG
cli_putstr_P(PSTR("\r\n starting hash free"));
#endif
hfal_hash_free(&(shavs_ctx.ctx));
cli_putstr_P(PSTR("\r\n MD = "));
cli_hexdump(diggest, pgm_read_word(&(shavs_algo->hashsize_b))/8);
}
free(buffer);
}