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 uint16_t blocks=0;
92 static uint8_t* buffer;
93 static uint16_t buffersize_B;
94 static hfgen_ctx_t ctx;
97 uint8_t buffer_add(char c){
99 if(buffer_idx==buffersize_B){
100 hfal_hash_nextBlock(&ctx, buffer);
105 if(c>='0' && c<='9'){
108 if(c>='a' && c<='f'){
111 if(c>='A' && c<='F'){
119 t=buffer[buffer_idx];
122 buffer[buffer_idx]=t;
125 t = (t&0x0F) | (v<<4);
126 buffer[buffer_idx]=t;
132 void shavs_test1(void){
138 cli_putstr_P(PSTR("\r\nERROR: select algorithm first!"));
142 buffersize_B=pgm_read_word(&(shavs_algo->blocksize_b))/8;
143 buffer = malloc(buffersize_B);
148 cli_putstr_P(PSTR("\r\n"));
149 cli_getsn(lenstr, 20);
150 len2 = strstrip(lenstr);
151 if(!strncasecmp_P(len2, PSTR("LEN"), 3)){
152 while(*len2 && *len2!='=')
156 length=strtoul(len2, NULL, 0);
160 if(!strncasecmp_P(len2, PSTR("EXIT"), 4)){
166 volatile int32_t expect_input;
172 expect_input=((length+7)/8)*2;
179 hfal_hash_init(shavs_algo, &ctx);
180 cli_putstr_P(PSTR("\r\n"));
181 while((c=cli_getc_cecho())!='M' && c!='m'){
183 cli_putstr_P(PSTR("\r\nERROR: wrong input (1) [0x"));
185 cli_putstr_P(PSTR("]!\r\n"));
190 if((c=cli_getc_cecho())!='s' && c!='S'){
191 cli_putstr_P(PSTR("\r\nERROR: wrong input (2)!\r\n"));
195 if((c=cli_getc_cecho())!='g' && c!='G'){
196 cli_putstr_P(PSTR("\r\nERROR: wrong input (3)!\r\n"));
200 while((c=cli_getc_cecho())!='='){
202 cli_putstr_P(PSTR("\r\nERROR: wrong input (4)!\r\n"));
209 while(expect_input>0){
211 if(buffer_add(c)==0){
214 if(!isblank((uint16_t)c)){
215 cli_putstr_P(PSTR("\r\nERROR: wrong input (5) ("));
217 cli_putstr_P(PSTR(")!\r\n"));
223 uint8_t diggest[pgm_read_word(shavs_algo->hashsize_b)/8];
224 hfal_hash_lastBlock(&ctx, buffer, length-blocks*(buffersize_B*8));
225 hfal_hash_ctx2hash(diggest, &ctx);
226 hfal_hash_free(&ctx);
227 cli_putstr_P(PSTR("\r\n MD = "));
228 cli_hexdump(diggest, pgm_read_word(&(shavs_algo->hashsize_b))/8);