]> git.cryptolib.org Git - arm-crypto-lib.git/blob - host/shavs_test2.rb
4d554fbcf39b37f0287c46015f544886e572215a
[arm-crypto-lib.git] / host / shavs_test2.rb
1 #!/usr/bin/ruby
2 # shavs_test.rb
3 =begin
4     This file is part of the ARM-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 # set automatically in init_system
28 $conffile_check = Hash.new
29 $conffile_check.default = 0
30
31 ################################################################################
32 # readconfigfile                                                               #
33 ################################################################################
34
35 def readconfigfile(fname, conf)
36   return conf if $conffile_check[fname]==1
37   $conffile_check[fname]=1
38   section = "default"
39   if not File.exists?(fname)
40     return conf
41   end
42   file = File.open(fname, "r")
43   until file.eof
44     line = file.gets()
45         next if /[\s]*#/.match(line)
46         if m=/\[[\s]*([^\s]*)[\s]*\]/.match(line)
47           section=m[1]
48           conf[m[1]] = Hash.new
49           next
50         end
51         next if ! /=/.match(line)
52         m=/[\s]*([^\s]*)[\s]*=[\s]*([^\s]*)/.match(line)
53         if m[1]=="include"
54           Dir.glob(m[2]){ |fn| conf = readconfigfile(fn, conf) }
55         else
56           conf[section][m[1]] = m[2]
57         end
58   end
59   file.close()
60   return conf
61 end
62
63 ################################################################################
64 # reset_system                                                                 #
65 ################################################################################
66
67 def reset_system
68   $sp.print("exit\r")
69   sleep 0.1
70   $sp.print("exit\r")
71   sleep 0.1
72 end
73
74 ################################################################################
75 # scan_system                                                                  #
76 ################################################################################
77
78 def scan_system
79   algos = Hash.new
80   $sp.print("shavs_list\r")
81   while true
82     line=$sp.gets()
83     return algos if /^>$/.match(line)
84     if m = /[\*\ ]([a-z]):[\s]*([a-zA-Z0-9+_-]+)/.match(line)
85       algos[m[2]]=m[1]
86     end
87   end
88 end
89
90 ################################################################################
91 # init_system                                                                  #
92 ################################################################################
93
94 def init_system(algo_select)
95   $sp.print("echo off \r")
96   print("DBG i: " + "echo off \r"+"\n") if $debug
97  sleep 0.1
98   $sp.print("shavs_set #{algo_select}\r")
99   print("DBG i: " + "shavs_set #{$algo_select} \r"+"\n") if $debug
100   sleep 0.1
101   $sp.print("shavs_test1 \r")
102   print("DBG i: " + "shavs_test1 \r"+"\n") if $debug
103   begin
104     line=$sp.gets()
105   end while not m=/buffer_size[\s]*=[\s]*0x([0-9A-Fa-f]*)/.match(line)
106   $buffer_size = m[1].to_i(16)
107   printf("buffer_size = %d\n", $buffer_size)
108 end
109
110 ################################################################################
111 # get_md                                                                       #
112 ################################################################################
113
114 def get_md
115   begin
116     line = $sp.gets()
117         puts("DBG got: " + line.inspect) if $debug
118         line = "" if line==nil
119    #    puts("DBG got: "+line) if $debug
120   end while not /[\s]*MD[\s]*=.*/.match(line)
121   return line
122 end
123
124 ################################################################################
125 # send_md                                                                      #
126 ################################################################################
127 =begin
128 def send_md(md_string)
129   $sp.print("Msg = ")
130   for i in 0..md_string.length-1
131     $sp.print(md_string[i].chr)
132           if((i%($buffer_size*2)==0)&&(i!=0))
133             begin
134                     line=$sp.gets()
135             end while not /\./.match(line)
136           end
137   end
138 end
139 =end
140 def send_md(md_string)
141 #  puts 'DBG: send_md; md_string.length = '+md_string.length.to_s+'; buffer_size = '+$buffer_size.to_s
142   bs = $buffer_size*2
143   $sp.print("Msg = ")
144   $sp.print(md_string[0])
145   md_string = md_string[1..-1]
146   for i in 0..((md_string.length)/bs)
147 #    puts 'DBG bulk send'
148     if(md_string.length-i*bs<=bs)
149  #     puts "DBG: i="+i.to_s()
150       t = md_string[(i*bs)..-1]
151       printf("sending final %d chars: %s\n", t.length, t[-10..-1]) if $debug
152       $sp.print(t) 
153       return
154     end
155     t = md_string[(i*bs)..((i+1)*bs-1)]
156     printf("sending %d chars: %s\n", t.length, t[-10..-1]) if $debug
157     $sp.print(t)
158     sleep(0.1)
159     print("going to wait ... : ") if $debug
160     begin
161      line=$sp.gets()
162      puts(line.inspect) if $debug
163      line='' if !line
164     end while ! /\./.match(line) 
165   end
166 end
167
168
169 ################################################################################
170 # run_test                                                                     #
171 ################################################################################
172
173 def run_test(filename, skip=0)
174   nerrors = 0
175   line=1
176   if not File.exist?(filename)
177         puts("ERROR file "+filename+" does not exist!")
178         return nerrors
179   end
180   pos = 0
181   file = File.new(filename, "r");
182   until file.eof
183     begin
184       lb=file.gets()
185 #         printf("DBG info: file read: %s", lb)
186     end while not (file.eof or (/[\s]*Len[\s]*=/.match(lb)))
187 #       puts("got ya")
188         if file.eof
189           file.close()
190           return nerrors
191         end
192         len = /[\s]*Len[\s]*=[\s]*([0-9]*)/.match(lb)[1].to_i
193         if(skip>0)
194           skip -= 1
195           redo
196         end
197     puts("DBG sending: "+lb) if $debug
198         $sp.print(lb.strip)
199         $sp.print("\r")
200     begin
201             lb=file.gets()
202     end while not (file.eof or (m=/[\s]*Msg[\s]*=[\s]*([0-9a-fA-F]*)/.match(lb)))
203     return if file.eof
204     puts("DBG sending: "+lb) if $debug
205         send_md(m[1])
206     puts("DBG sending [done] getting...") if $debug
207         avr_md = get_md()
208     puts("DBG getting [done]") if $debug
209     begin
210             lb=file.gets()
211     end while not /[\s]*MD[\s]*=.*/.match(lb)
212         a = (/[\s]*MD[\s]*=[\s]*([0-9a-fA-F]*).*/.match(lb))[1];
213         b = (/[\s]*MD[\s]*=[\s]*([0-9a-fA-F]*).*/.match(avr_md))[1];
214         a.upcase!
215         b.upcase!
216         printf("\n%4d (%4d) [%5d]: ", line, (line-1)*$linewidth, len) if (pos%$linewidth==0 and $linewidth!=0)
217         line += 1               if (pos%$linewidth==0 and $linewidth!=0)
218         #sleep(1)
219         #putc((a==b)?'*':'!')
220         if(a==b)
221           putc('*')
222         else
223           putc('!')
224         #  printf("<%d>",len)
225           printf("\nError @%05d: %s [should]\n           != %s [is]- ",len, a, b)
226           nerrors += 1
227         end
228         pos += 1
229   end
230   file.close()
231   return nerrors
232 end
233
234
235 ################################################################################
236 # MAIN                                                                         #
237 ################################################################################
238 #
239 # Options:
240 #  -s {algo_letter} run only specified algos
241 #  -f <file>        also read config from <file>
242 #  -i <n>           skip until test nr. <n>
243 #  -j <n>           start with testfile <n>
244 #  -h ???
245 #  -d               enable debug mode
246 #  -c ???
247 #  -a ???
248
249 opts = Getopt::Std.getopts("s:f:i:j:hdca")
250
251 conf = Hash.new
252 conf = readconfigfile("/etc/testport.conf", conf)
253 conf = readconfigfile("~/.testport.conf", conf)
254 conf = readconfigfile("testport.conf", conf)
255 conf = readconfigfile(opts["f"], conf) if opts["f"]
256
257 #puts conf.inspect
258
259 puts("serial port interface version: " + SerialPort::VERSION);
260 $linewidth = 64
261 params = { "baud"       => conf["PORT"]["baud"].to_i,
262             "data_bits" => conf["PORT"]["databits"].to_i,
263             "stop_bits" => conf["PORT"]["stopbits"].to_i,
264             "parity"    => SerialPort::NONE }
265 params["paraty"] = SerialPort::ODD   if conf["PORT"]["paraty"].downcase == "odd"
266 params["paraty"] = SerialPort::EVEN  if conf["PORT"]["paraty"].downcase == "even"
267 params["paraty"] = SerialPort::MARK  if conf["PORT"]["paraty"].downcase == "mark"
268 params["paraty"] = SerialPort::SPACE if conf["PORT"]["paraty"].downcase == "space"
269
270 puts("\nPort: "+conf["PORT"]["port"]+"@"    +
271                 params["baud"].to_s      +
272                 " "                      +
273                 params["data_bits"].to_s +
274                 conf["PORT"]["paraty"][0,1].upcase +
275                 params["stop_bits"].to_s +
276                 "\n")
277
278 $sp = SerialPort.new(conf["PORT"]["port"], params)
279
280 $sp.read_timeout=1000; # 5 minutes
281 $sp.flow_control = SerialPort::SOFT
282
283 reset_system()
284 algos=scan_system()
285 #puts algos.inspect
286
287 if opts["d"]
288   $debug = true
289 end
290
291 if opts["s"]
292   algos_rev = algos.invert
293   algo_tasks = Array.new
294   opts["s"].each_byte{ |x|
295     if algos_rev[x.chr]
296       algo_tasks << [algos_rev[x.chr],x.chr]
297     end
298   }
299 else
300   algo_tasks=algos.sort
301 end
302
303 algo_tasks.each do |algoa|
304   algo = algoa[0]
305   if conf[algo]==nil
306     puts("No test-set defined for #{algo} \r\n")
307     next
308   else
309         i=0
310         i = opts["j"].to_i if opts["j"]
311         logfile=File.open(conf["PORT"]["testlogbase"]+algo+".txt", "a")
312         while conf[algo]["file_#{i}"] != nil
313           puts("Testing #{algo} with #{conf[algo]["file_#{i}"]}")
314           reset_system()
315           init_system(algoa[1])
316           skip=0
317           skip=opts["i"].to_i if opts["i"]
318           nerrors=run_test(conf[algo]["file_#{i}"], skip)
319       if nerrors == 0
320         puts("\n[ok]")
321         logfile.puts("[ok] "+conf[algo]["file_#{i}"]+ " ("+Time.now.to_s()+")")
322       else
323         puts("\n[errors: "+ nerrors.to_s() +"]")
324         logfile.puts("[error] "+nerrors.to_s+" "+conf[algo]["file_#{i}"]+ " ("+Time.now.to_s()+")")
325       end
326       i = i+1
327     end
328     logfile.close()
329   end
330 end
331
332 =begin
333 nerrors = 0
334 for i in (5..(ARGV.size-1))
335   nerrors = run_test(ARGV[i])
336   if nerrors == 0
337     puts("\n[ok]")
338   else
339     puts("\n[errors: "+ nerrors.to_s() +"]")
340   end
341 end
342  $sp.print("EXIT\r");
343
344 #exit(0);
345 =end
346