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 MCU_TARGET = atmega644
OPTIMIZE = -Os OPTIMIZE = -Os # -Os
PROGRAMMER = avr911 PROGRAMMER = avr911
DEFS = -D$(call uc, $(MCU_TARGET)) DEFS = -D$(call uc, $(MCU_TARGET))
FLASHCMD = avrdude -p $(MCU_TARGET) -P /dev/ttyUSB0 -c $(PROGRAMMER) -U flash:w:# no space at the end 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 ROTL32(a,n) (((a)<<(n))|((a)>>(32-(n))))
#define ROTR32(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 #define TWEAK 1
#if TWEAK
# define BUG24 0
#else
# define BUG24 1
#endif
#define F0_HACK 1 #define F0_HACK 1
#define DEBUG 0 #define DEBUG 0
#if DEBUG #if DEBUG
#include "cli.h" #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]); r += s[i%4](q[j+i]);
} }
#if TWEAK #if TWEAK
# if BUG_ROT
r += ( ROTL32(((uint32_t*)m)[j&0xf], ((j+0)&0xf)+1 ) 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+3)&0xf], ((j+3)&0xf)+1 )
- ROTL32(((uint32_t*)m)[(j+10)&0xf], ((j+10)&0xf)+1 ) - ROTL32(((uint32_t*)m)[(j+10)&0xf], ((j+10)&0xf)+1 )
+ pgm_read_dword(k_lut+j) + pgm_read_dword(k_lut+j)
) ^ ((uint32_t*)h)[(j+7)&0xf]; ) ^ ((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 #else
r += pgm_read_dword(k_lut+j); r += pgm_read_dword(k_lut+j);
r += ((uint32_t*)m)[j&0xf]; 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]); r += bmw_small_s4(q[j+15]);
#endif #endif
#if TWEAK #if TWEAK
# if BUG_ROT
r += ( ROTL32(((uint32_t*)m)[j&0xf], ((j+0)&0xf)+1 ) 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+3)&0xf], ((j+3)&0xf)+1 )
- ROTL32(((uint32_t*)m)[(j+10)&0xf], ((j+10)&0xf)+1 ) - ROTL32(((uint32_t*)m)[(j+10)&0xf], ((j+10)&0xf)+1 )
+ pgm_read_dword(k_lut+j) + pgm_read_dword(k_lut+j)
) ^ ((uint32_t*)h)[(j+7)&0xf]; ) ^ ((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 #else
r += pgm_read_dword(k_lut+j); r += pgm_read_dword(k_lut+j);
r += ((uint32_t*)m)[j&0xf]; 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 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; uint8_t i,j=-1,v,sign,l=0;
uint32_t(*s[])(uint32_t)={ bmw_small_s0, bmw_small_s1, bmw_small_s2, uint32_t(*s[])(uint32_t)={ bmw_small_s0, bmw_small_s1, bmw_small_s2,
bmw_small_s3, bmw_small_s4 }; 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 #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; uint8_t i;
uint32_t(*s[])(uint32_t)={ bmw_small_s0, bmw_small_s1, bmw_small_s2, uint32_t(*s[])(uint32_t)={ bmw_small_s0, bmw_small_s1, bmw_small_s2,
bmw_small_s3, bmw_small_s4 }; bmw_small_s3, bmw_small_s4 };

View File

@ -24,8 +24,10 @@
uint8_t hfal_hash_init(const hfdesc_t* hash_descriptor, hfgen_ctx_t* ctx){ uint8_t hfal_hash_init(const hfdesc_t* hash_descriptor, hfgen_ctx_t* ctx){
hf_init_fpt f; hf_init_fpt f;
uint16_t tmp;
ctx->desc_ptr = (hfdesc_t*)hash_descriptor; 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; 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); f(ctx->ctx);

View File

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

View File

@ -137,7 +137,7 @@ cli_getsn:
st Y+, r24 st Y+, r24
movw r26, r16 movw r26, r16
sbiw r26, 1 sbiw r26, 1
movw r16, r24 movw r16, r26
brne 2b brne 2b
9: 9:
clr r25 clr r25
@ -145,6 +145,38 @@ cli_getsn:
st Y, r1 st Y, r1
pop r17 pop r17
pop r16 pop r16
pop r28
pop r29 pop r29
pop r28
ret 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

@ -65,7 +65,7 @@ cli_hexdump:
ld r24, Y+ ld r24, Y+
rcall cli_hexdump_byte rcall cli_hexdump_byte
subi r16, 1 subi r16, 1
sbc r17, r1 sbci r17, 0
brne 2b brne 2b
hexdump_exit: hexdump_exit:
pop r17 pop r17

View File

@ -102,11 +102,12 @@ int8_t cmd_interface(PGM_VOID_P cmd_desc){
free(cli_buffer); free(cli_buffer);
return exit_code; return exit_code;
} }
cli_putstr(cli_buffer); /* cli_putstr(cli_buffer); */
memset(cli_buffer, 0, cli_buffer_size); memset(cli_buffer, 0, cli_buffer_size);
cli_buffer_index=0; 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; completion_failed=0;
break; break;
case CLI_BACKSPACE: case CLI_BACKSPACE:

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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