]> git.cryptolib.org Git - avr-crypto-lib.git/blob - host/shavs_test.rb
grøstl, a first impression
[avr-crypto-lib.git] / host / shavs_test.rb
1 #!/usr/bin/ruby 
2 # shavs_test.rb
3 =begin
4     This file is part of the AVR-Crypto-Lib.
5     Copyright (C) 2008  Daniel Otte (daniel.otte@rub.de)
6
7     This program is free software: you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation, either version 3 of the License, or
10     (at your option) any later version.
11
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16
17     You should have received a copy of the GNU General Public License
18     along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 =end
20
21 $debug = false
22 require 'rubygems'
23 require 'serialport'
24
25 def init_system
26   sleep 1
27   $sp.print("echo  off \r")
28   print("DBG i: " + "echo  off \r"+"\n") if $debug
29 #  line = $sp.readlines()
30 #  print("DBG 0.0: ")
31 #  print(line)
32 #  sleep 1
33   $sp.print("shavs_set #{$algo_select} \r")
34   print("DBG i: " + "shavs_set #{$algo_select} \r"+"\n") if $debug
35 #  line = $sp.readlines()
36 #  print("DBG 0.1: ")
37 #  print(line)
38 #  sleep 1
39   $sp.print("shavs_test1 \r")
40   print("DBG i: " + "shavs_test1 \r"+"\n") if $debug
41 #  line = $sp.readlines()
42 #  print("DBG 0.2: ")
43 #  print(line)
44 end
45
46 def get_md
47   begin
48     line = $sp.gets()
49         line = "" if line==nil
50         puts("DBG g: "+line) if $debug
51   end while not /[\s]*MD[\s]*=.*/.match(line)
52   return line   
53 end
54
55 def send_md(md_string)
56   for i in 0..md_string.length-1
57     $sp.print(md_string[i].chr)
58 #       print("DBG s: "+ md_string[i].chr) if $debug
59         if(i%20==19)
60                 sleep(0.015)
61         end             
62   end
63 end
64
65 def run_test(filename)
66   errors = 0
67   line=1
68   if not File.exist?(filename)
69         puts("ERROR file "+filename+" does not exist!")
70   end
71   pos = 0
72   file = File.new(filename, "r");
73   until file.eof
74     begin
75       lb=file.gets()
76     end while not (file.eof or (/[\s]*Len[\s]*=.*/.match(lb)))
77     puts("DBG sending: "+lb) if $debug
78         return if file.eof
79         $sp.print(lb.strip)
80         $sp.print("\r")
81     begin
82           lb=file.gets()
83     end while not (file.eof or (/[\s]*Msg[\s]*=.*/.match(lb)))
84     return if file.eof
85     puts("DBG sending: "+lb) if $debug
86         send_md(lb.strip)
87         avr_md = get_md()
88     begin
89           lb=file.gets()
90     end while not /[\s]*MD[\s]*=.*/.match(lb)
91         a = (/[\s]*MD[\s]*=[\s]*([0-9a-fA-F]*).*/.match(lb))[1];
92         b = (/[\s]*MD[\s]*=[\s]*([0-9a-fA-F]*).*/.match(avr_md))[1];
93         a.upcase!
94         b.upcase!
95         printf("\n%4d (%4d): ", line, (line-1)*$linewidth) if (pos%$linewidth==0 and $linewidth!=0)
96         line += 1               if (pos%$linewidth==0 and $linewidth!=0)
97         #putc((a==b)?'*':'!')
98         if(a==b)
99           putc('*')
100         else
101           putc('!')
102         #  printf("\nshould: %s\ngot:   %s\n",lb,avr_md)
103           errors += 1;
104         end  
105         pos += 1
106   end
107   return errors
108 end
109
110 if ARGV.size < 6
111   STDERR.print <<EOF
112   Usage: ruby #{$0} port bps nbits stopb algo_select testfile ...
113 EOF
114   exit(1)
115 end
116
117 puts("\nPort: "+ARGV[0]+ "@"+ARGV[1]+" "+ARGV[2]+"N"+ARGV[3]+"\n");
118 puts("serial port interface version: " + SerialPort::VERSION);
119 $linewidth = 64
120 $params = { "baud"      => ARGV[1].to_i, 
121             "data_bits" => ARGV[2].to_i, 
122             "stop_bits" => ARGV[3].to_i, 
123             "parity"    => SerialPort::NONE }
124 $sp = SerialPort.new(ARGV[0], $params)
125 #$sp = SerialPort.new(ARGV[0], ARGV[1].to_i, ARGV[2].to_i, ARGV[3].to_i, SerialPort::NONE);
126
127 $sp.read_timeout=1000; # 5 minutes
128 $sp.flow_control = SerialPort::SOFT
129 $algo_select = ARGV[4]
130 #irb
131
132 init_system()
133
134 for i in (5..(ARGV.size-1))
135   errors = run_test(ARGV[i])
136   if errors == 0
137     puts("\n[ok]")
138   else
139     puts("\n[errors: "+errors.to_s+"]")
140   end
141 end
142  $sp.print("EXIT\r");
143
144 #exit(0);
145
146