]> git.cryptolib.org Git - avr-crypto-lib.git/blob - host/shavs_test2.rb
fixing E-Mail-Address & Copyright
[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 ARM-Crypto-Lib.
5     Copyright (C) 2006-2015 Daniel Otte (bg@nerilex.org)
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 #  -o               use just one testfile
245 #  -h ???
246 #  -d               enable debug mode
247 #  -c ???
248 #  -a ???
249
250 opts = Getopt::Std.getopts("s:f:i:j:hdcao")
251
252 conf = Hash.new
253 conf = readconfigfile("/etc/testport.conf", conf)
254 conf = readconfigfile("~/.testport.conf", conf)
255 conf = readconfigfile("testport.conf", conf)
256 conf = readconfigfile(opts["f"], conf) if opts["f"]
257
258 #puts conf.inspect
259
260 puts("serial port interface version: " + SerialPort::VERSION);
261 $linewidth = 64
262 params = { "baud"       => conf["PORT"]["baud"].to_i,
263             "data_bits" => conf["PORT"]["databits"].to_i,
264             "stop_bits" => conf["PORT"]["stopbits"].to_i,
265             "parity"    => SerialPort::NONE }
266 params["paraty"] = SerialPort::ODD   if conf["PORT"]["paraty"].downcase == "odd"
267 params["paraty"] = SerialPort::EVEN  if conf["PORT"]["paraty"].downcase == "even"
268 params["paraty"] = SerialPort::MARK  if conf["PORT"]["paraty"].downcase == "mark"
269 params["paraty"] = SerialPort::SPACE if conf["PORT"]["paraty"].downcase == "space"
270
271 puts("\nPort: "+conf["PORT"]["port"]+"@"    +
272                 params["baud"].to_s      +
273                 " "                      +
274                 params["data_bits"].to_s +
275                 conf["PORT"]["paraty"][0,1].upcase +
276                 params["stop_bits"].to_s +
277                 "\n")
278
279 $sp = SerialPort.new(conf["PORT"]["port"], params)
280
281 $sp.read_timeout=1000; # 5 minutes
282 $sp.flow_control = SerialPort::SOFT
283
284 reset_system()
285 algos=scan_system()
286 #puts algos.inspect
287
288 if opts["d"]
289   $debug = true
290 end
291
292 if opts["s"]
293   algos_rev = algos.invert
294   algo_tasks = Array.new
295   opts["s"].each_byte{ |x|
296     if algos_rev[x.chr]
297       algo_tasks << [algos_rev[x.chr],x.chr]
298     end
299   }
300 else
301   algo_tasks=algos.sort
302 end
303
304 algo_tasks.each do |algoa|
305   algo = algoa[0]
306   if conf[algo]==nil
307     puts("No test-set defined for #{algo} \r\n")
308     next
309   else
310     i=0
311     i = opts["j"].to_i if opts["j"]
312     logfile=File.open(conf["PORT"]["testlogbase"]+algo+".txt", "a")
313     while conf[algo]["file_#{i}"] != nil
314       puts("Testing #{algo} with #{conf[algo]["file_#{i}"]}")
315       reset_system()
316       init_system(algoa[1])
317       skip=0
318       skip=opts["i"].to_i if opts["i"]
319       nerrors=run_test(conf[algo]["file_#{i}"], skip)
320       if nerrors == 0
321         puts("\n[ok]")
322         logfile.puts("[ok] "+conf[algo]["file_#{i}"]+ " ("+Time.now.to_s()+")")
323       else
324         puts("\n[errors: "+ nerrors.to_s() +"]")
325         logfile.puts("[error] "+nerrors.to_s+" "+conf[algo]["file_#{i}"]+ " ("+Time.now.to_s()+")")
326       end
327       i = i+1
328       break if opts["o"]
329     end
330     logfile.close()
331   end
332 end
333
334 =begin
335 nerrors = 0
336 for i in (5..(ARGV.size-1))
337   nerrors = run_test(ARGV[i])
338   if nerrors == 0
339     puts("\n[ok]")
340   else
341     puts("\n[errors: "+ nerrors.to_s() +"]")
342   end
343 end
344  $sp.print("EXIT\r");
345
346 #exit(0);
347 =end
348