3 This file is part of the AVR-Crypto-Lib.
4 Copyright (C) 2008 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/>.
23 * \license GPLv3 or later
27 #include <avr/pgmspace.h>
31 #include "hashfunction_descriptor.h"
32 #include "hfal-basic.h"
34 #include "string-extras.h"
37 hfdesc_t* shavs_algo=NULL;
38 hfdesc_t** shavs_algolist=NULL;
40 void shavs_listalgos(void){
45 cli_putstr_P(PSTR("\r\nthe following algorithms are available:\r\n"));
46 while(option<='z' && (t=(hfdesc_t*)pgm_read_word(&(shavs_algolist[i])))){
48 cli_putc((t==shavs_algo)?'*':' ');
50 cli_putstr_P(PSTR(":\t"));
51 cli_putstr_P((void*)(pgm_read_word(&(t->name))));
52 cli_putstr_P(PSTR("\r\n"));
57 void shavs_setalgo(char* param){
58 param = strstrip(param);
59 if(param[1]=='\0'){ /* single letter specified */
60 uint8_t i,option = param[0]-'a';
63 cli_putstr_P(PSTR("\r\nERROR: shavs_algolist not set!"));
66 for(i=0; i<=option; ++i){
67 if((void*)pgm_read_word(&(shavs_algolist[i]))==NULL){
68 cli_putstr_P(PSTR("\r\nERROR: invalid selection!"));
72 shavs_algo=(hfdesc_t*)pgm_read_word(&(shavs_algolist[option]));
73 } else { /* name specifyed */
76 while((t=(hfdesc_t*)pgm_read_word(&(shavs_algolist[i]))) &&
77 strcasecmp_P(param, (void*)pgm_read_word(&(t->name))))
82 cli_putstr_P(PSTR("\r\nERROR: could not find \""));
84 cli_putstr_P(PSTR("\"!"));
89 static uint16_t buffer_idx=0;
90 static uint8_t in_byte=0;
91 static uint8_t* buffer;
92 static uint16_t buffersize_B;
93 static hfgen_ctx_t ctx;
96 uint8_t buffer_add(char c){
101 if(c>='a' && c<='f'){
104 if(c>='A' && c<='F'){
112 if(buffer_idx==buffersize_B){
113 hfal_hash_nextBlock(&ctx, buffer);
118 t=buffer[buffer_idx];
121 buffer[buffer_idx]=t;
124 t = (t&0x0F) | (v<<4);
125 buffer[buffer_idx]=t;
131 void shavs_test1(void){
137 cli_putstr_P(PSTR("\r\nERROR: select algorithm first!"));
141 buffersize_B=pgm_read_word(&(shavs_algo->blocksize_b))/8;
142 buffer = malloc(buffersize_B);
146 cli_putstr_P(PSTR("\r\n"));
147 cli_getsn(lenstr, 20);
148 len2 = strstrip(lenstr);
149 if(!strncasecmp_P(len2, PSTR("LEN"), 3)){
150 while(*len2 && *len2!='=')
154 length=strtoul(len2, NULL, 0);
158 if(!strncasecmp_P(len2, PSTR("EXIT"), 4)){
163 volatile int16_t expect_input;
169 expect_input=((length+7)/8)*2;
176 hfal_hash_init(shavs_algo, &ctx);
177 cli_putstr_P(PSTR("\r\n"));
178 while((c=cli_getc_cecho())!='M' && c!='m'){
180 cli_putstr_P(PSTR("\r\nERROR: wrong input (1) [0x"));
182 cli_putstr_P(PSTR("]!\r\n"));
186 if((c=cli_getc_cecho())!='s' && c!='S'){
187 cli_putstr_P(PSTR("\r\nERROR: wrong input (2)!\r\n"));
190 if((c=cli_getc_cecho())!='g' && c!='G'){
191 cli_putstr_P(PSTR("\r\nERROR: wrong input (3)!\r\n"));
194 while((c=cli_getc_cecho())!='='){
196 cli_putstr_P(PSTR("\r\nERROR: wrong input (4)!\r\n"));
202 while(expect_input>0){
204 if(buffer_add(c)==0){
207 if(!isblank((uint16_t)c)){
208 cli_putstr_P(PSTR("\r\nERROR: wrong input (5) ("));
210 cli_putstr_P(PSTR(")!\r\n"));
216 uint8_t diggest[pgm_read_word(shavs_algo->hashsize_b)/8];
217 if(length && length%(buffersize_B*8)==0)
218 hfal_hash_nextBlock(&ctx, buffer);
219 hfal_hash_lastBlock(&ctx, buffer, length%(buffersize_B*8));
220 hfal_hash_ctx2hash(diggest, &ctx);
221 hfal_hash_free(&ctx);
222 cli_putstr_P(PSTR("\r\n MD = "));
223 cli_hexdump(diggest, pgm_read_word(&(shavs_algo->hashsize_b))/8);