]> git.cryptolib.org Git - avr-crypto-lib.git/blob - host/shavs_test2.rb
fixing some warnings (AES); simplifyning AES headers (now simply include "aes.h"...
[avr-crypto-lib.git] / host / shavs_test2.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, 2009  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 = true
22 $debug = false
23 require 'rubygems'
24 require 'serialport'
25 require 'getopt/std'
26
27 $buffer_size = 0
28 $conffile_check = Hash.new
29 $conffile_check.default = 0
30
31 def readconfigfile(fname, conf)
32   return conf if $conffile_check[fname]==1
33   $conffile_check[fname]=1
34   section = "default"
35   if not File.exists?(fname)
36     return conf
37   end
38   file = File.open(fname, "r")
39   until file.eof
40     line = file.gets()
41         next if /[\s]*#/.match(line)
42         if m=/\[[\s]*([^\s]*)[\s]*\]/.match(line)
43           section=m[1]
44           conf[m[1]] = Hash.new
45           next
46         end
47         next if not /=/.match(line)
48         m=/[\s]*([^\s]*)[\s]*=[\s]*([^\s]*)/.match(line)
49         if m[1]=="include"
50           Dir.glob(m[2]){ |fn| conf = readconfigfile(fn, conf) }
51         else
52           conf[section][m[1]] = m[2]
53         end
54   end
55   file.close()
56   return conf
57 end
58
59 def reset_system
60   $sp.print("exit\r")
61   sleep 0.1
62   $sp.print("exit\r")
63   sleep 0.1
64 end
65
66 def scan_system
67   algos = Hash.new
68   $sp.print("shavs_list\r")
69   while true
70     line=$sp.gets()
71     return algos if /^>$/.match(line)
72     if m = /[\*\ ]([a-z]):[\s]*([a-zA-Z0-9+_-]+)/.match(line)
73       algos[m[2]]=m[1]
74     end
75   end
76 end
77
78 def init_system(algo_select)
79   $sp.print("echo off \r")
80   print("DBG i: " + "echo off \r"+"\n") if $debug
81  sleep 1
82   $sp.print("shavs_set #{algo_select}\r")
83   print("DBG i: " + "shavs_set #{$algo_select} \r"+"\n") # if $debug
84   sleep 1
85   $sp.print("shavs_test1 \r")
86   print("DBG i: " + "shavs_test1 \r"+"\n") if $debug
87   begin
88     line=$sp.gets()
89   end while not m=/buffer_size[\s]*=[\s]*0x([0-9A-Fa-f]*)/.match(line)
90   $buffer_size = m[1].to_i(16)
91 end
92
93 def get_md
94   begin
95     line = $sp.gets()
96         line = "" if line==nil
97         puts("DBG got: "+line) if $debug
98   end while not /[\s]*MD[\s]*=.*/.match(line)
99   return line
100 end
101
102 def send_md(md_string)
103   $sp.print("Msg = ")
104   for i in 0..md_string.length-1
105     $sp.print(md_string[i].chr)
106 #       print("DBG s: "+ md_string[i].chr) if $debug
107     sleep(0.01)
108         if((i%($buffer_size*2)==0)&&(i!=0))
109           begin
110                 line=$sp.gets()
111           end while not /\./.match(line)
112         end
113   end
114 end
115
116 def run_test(filename, skip=0)
117   nerrors = 0
118   line=1
119   if not File.exist?(filename)
120         puts("ERROR file "+filename+" does not exist!")
121         return nerrors
122   end
123   pos = 0
124   file = File.new(filename, "r");
125   until file.eof
126     begin
127       lb=file.gets()
128     end while not (file.eof or (/[\s]*Len[\s]*=.*/.match(lb)))
129     len = /[\s]*Len[\s]*=[\s]*([0-9]*)/.match(lb)[1].to_i
130     puts("DBG sending: "+lb) if $debug
131         return if file.eof
132         if(skip>0)
133           skip -= 1
134           redo
135         end
136         $sp.print(lb.strip)
137         $sp.print("\r")
138     begin
139           lb=file.gets()
140     end while not (file.eof or (m=/[\s]*Msg[\s]*=[\s]*([0-9a-fA-F]*)/.match(lb)))
141     return if file.eof
142     puts("DBG sending: "+lb) if $debug
143         send_md(m[1])
144         avr_md = get_md()
145     begin
146           lb=file.gets()
147     end while not /[\s]*MD[\s]*=.*/.match(lb)
148         a = (/[\s]*MD[\s]*=[\s]*([0-9a-fA-F]*).*/.match(lb))[1];
149         b = (/[\s]*MD[\s]*=[\s]*([0-9a-fA-F]*).*/.match(avr_md))[1];
150         a.upcase!
151         b.upcase!
152         printf("\n%4d (%4d) [%5d]: ", line, (line-1)*$linewidth, len) if (pos%$linewidth==0 and $linewidth!=0)
153         line += 1               if (pos%$linewidth==0 and $linewidth!=0)
154         #sleep(1)
155         #putc((a==b)?'*':'!')
156         if(a==b)
157           putc('*')
158         else
159           putc('!')
160         #  printf("<%d>",len)
161           printf("\nError @%05d: %s \n           != %s - ",len, a, b)
162           nerrors += 1
163         end
164         pos += 1
165   end
166   return nerrors.to_i
167 end
168
169 opts = Getopt::Std.getopts("s:f:i:hdca")
170
171 conf = Hash.new
172 conf = readconfigfile("/etc/testport.conf", conf)
173 conf = readconfigfile("~/.testport.conf", conf)
174 conf = readconfigfile("testport.conf", conf)
175 conf = readconfigfile(opts["f"], conf) if opts["f"]
176
177 #puts conf.inspect
178
179 puts("serial port interface version: " + SerialPort::VERSION);
180 $linewidth = 64
181 params = { "baud"       => conf["PORT"]["baud"].to_i,
182             "data_bits" => conf["PORT"]["databits"].to_i,
183             "stop_bits" => conf["PORT"]["stopbits"].to_i,
184             "parity"    => SerialPort::NONE }
185 params["paraty"] = SerialPort::ODD   if conf["PORT"]["paraty"].downcase == "odd"
186 params["paraty"] = SerialPort::EVEN  if conf["PORT"]["paraty"].downcase == "even"
187 params["paraty"] = SerialPort::MARK  if conf["PORT"]["paraty"].downcase == "mark"
188 params["paraty"] = SerialPort::SPACE if conf["PORT"]["paraty"].downcase == "space"
189
190 puts("\nPort: "+conf["PORT"]["port"]+"@"    +
191                 params["baud"].to_s      +
192                 " "                      +
193                 params["data_bits"].to_s +
194                 conf["PORT"]["paraty"][0,1].upcase +
195                 params["stop_bits"].to_s +
196                 "\n")
197
198 $sp = SerialPort.new(conf["PORT"]["port"], params)
199
200 $sp.read_timeout=1000; # 5 minutes
201 $sp.flow_control = SerialPort::SOFT
202
203 reset_system()
204 algos=scan_system()
205 #puts algos.inspect
206
207 if opts["s"]
208   algos_rev = algos.invert
209   algo_tasks = Array.new
210   opts["s"].each_byte{ |x|
211     if algos_rev[x.chr]
212       algo_tasks << [algos_rev[x.chr],x.chr]
213     end
214   }
215 else
216   algo_tasks=algos.sort
217 end
218
219 algo_tasks.each do |algoa|
220   algo = algoa[0]
221   if conf[algo]==nil
222     puts("No test-set defined for #{algo} \r\n")
223     next
224   else
225         i=0
226         logfile=File.open(conf["PORT"]["testlogbase"]+algo+".txt", "a")
227         while conf[algo]["file_#{i}"] != nil
228           puts("Testing #{algo} with #{conf[algo]["file_#{i}"]}")
229           reset_system()
230           init_system(algoa[1])
231           skip=0
232           skip=opts["i"].to_i if opts["i"]
233           nerrors=run_test(conf[algo]["file_#{i}"], skip)
234       if nerrors == 0
235         puts("\n[ok]")
236         logfile.puts("[ok] "+conf[algo]["file_#{i}"]+ " ("+Time.now.to_s()+")")
237       else
238         puts("\n[errors: "+ nerrors.to_s() +"]")
239         logfile.puts("[error] "+nerrors.to_s+" "+conf[algo]["file_#{i}"]+ " ("+Time.now.to_s()+")")
240       end
241       i += 1
242     end
243     logfile.close()
244   end
245 end
246
247 =begin
248 nerrors = 0
249 for i in (5..(ARGV.size-1))
250   nerrors = run_test(ARGV[i])
251   if nerrors == 0
252     puts("\n[ok]")
253   else
254     puts("\n[errors: "+ nerrors.to_s() +"]")
255   end
256 end
257  $sp.print("EXIT\r");
258
259 #exit(0);
260 =end
261