]> git.cryptolib.org Git - arm-crypto-lib.git/blob - host/shavs_test2.rb
Adding Khazad
[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
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 not /=/.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 end
108
109 ################################################################################
110 # get_md                                                                       #
111 ################################################################################
112
113 def get_md
114   begin
115     line = $sp.gets()
116         line = "" if line==nil
117         puts("DBG got: "+line) if $debug
118   end while not /[\s]*MD[\s]*=.*/.match(line)
119   return line
120 end
121
122 ################################################################################
123 # send_md                                                                      #
124 ################################################################################
125 =begin
126 def send_md(md_string)
127   $sp.print("Msg = ")
128   for i in 0..md_string.length-1
129     $sp.print(md_string[i].chr)
130           if((i%($buffer_size*2)==0)&&(i!=0))
131             begin
132                     line=$sp.gets()
133             end while not /\./.match(line)
134           end
135   end
136 end
137 =end
138 def send_md(md_string)
139 #  puts 'DBG: send_md; md_string.length = '+md_string.length.to_s+'; buffer_size = '+$buffer_size.to_s
140   bs = $buffer_size*2
141   $sp.print("Msg = ")
142   for i in 0..((md_string.length-1)/bs)
143 #    puts 'DBG bulk send'
144     if(md_string.length-i*bs<bs)
145  #     puts "DBG: i="+i.to_s()
146       $sp.print(md_string[(i*bs)..-1]) 
147       return
148     end
149     $sp.print(md_string[(i*bs)..((i+1)*bs-1)])
150     begin
151       line=$sp.gets()
152     end while not /\./.match(line) 
153   end
154 end
155
156
157 ################################################################################
158 # run_test                                                                     #
159 ################################################################################
160
161 def run_test(filename, skip=0)
162   nerrors = 0
163   line=1
164   if not File.exist?(filename)
165         puts("ERROR file "+filename+" does not exist!")
166         return nerrors
167   end
168   pos = 0
169   file = File.new(filename, "r");
170   until file.eof
171     begin
172       lb=file.gets()
173 #         printf("DBG info: file read: %s", lb)
174     end while not (file.eof or (/[\s]*Len[\s]*=/.match(lb)))
175 #       puts("got ya")
176         if file.eof
177           file.close()
178           return nerrors
179         end
180         len = /[\s]*Len[\s]*=[\s]*([0-9]*)/.match(lb)[1].to_i
181         if(skip>0)
182           skip -= 1
183           redo
184         end
185     puts("DBG sending: "+lb) if $debug
186         $sp.print(lb.strip)
187         $sp.print("\r")
188     begin
189           lb=file.gets()
190     end while not (file.eof or (m=/[\s]*Msg[\s]*=[\s]*([0-9a-fA-F]*)/.match(lb)))
191     return if file.eof
192     puts("DBG sending: "+lb) if $debug
193         send_md(m[1])
194         avr_md = get_md()
195     begin
196           lb=file.gets()
197     end while not /[\s]*MD[\s]*=.*/.match(lb)
198         a = (/[\s]*MD[\s]*=[\s]*([0-9a-fA-F]*).*/.match(lb))[1];
199         b = (/[\s]*MD[\s]*=[\s]*([0-9a-fA-F]*).*/.match(avr_md))[1];
200         a.upcase!
201         b.upcase!
202         printf("\n%4d (%4d) [%5d]: ", line, (line-1)*$linewidth, len) if (pos%$linewidth==0 and $linewidth!=0)
203         line += 1               if (pos%$linewidth==0 and $linewidth!=0)
204         #sleep(1)
205         #putc((a==b)?'*':'!')
206         if(a==b)
207           putc('*')
208         else
209           putc('!')
210         #  printf("<%d>",len)
211           printf("\nError @%05d: %s [should]\n           != %s [is]- ",len, a, b)
212           nerrors += 1
213         end
214         pos += 1
215   end
216   file.close()
217   return nerrors
218 end
219
220
221 ################################################################################
222 # MAIN                                                                         #
223 ################################################################################
224
225 opts = Getopt::Std.getopts("s:f:i:j:hdca")
226
227 conf = Hash.new
228 conf = readconfigfile("/etc/testport.conf", conf)
229 conf = readconfigfile("~/.testport.conf", conf)
230 conf = readconfigfile("testport.conf", conf)
231 conf = readconfigfile(opts["f"], conf) if opts["f"]
232
233 #puts conf.inspect
234
235 puts("serial port interface version: " + SerialPort::VERSION);
236 $linewidth = 64
237 params = { "baud"       => conf["PORT"]["baud"].to_i,
238             "data_bits" => conf["PORT"]["databits"].to_i,
239             "stop_bits" => conf["PORT"]["stopbits"].to_i,
240             "parity"    => SerialPort::NONE }
241 params["paraty"] = SerialPort::ODD   if conf["PORT"]["paraty"].downcase == "odd"
242 params["paraty"] = SerialPort::EVEN  if conf["PORT"]["paraty"].downcase == "even"
243 params["paraty"] = SerialPort::MARK  if conf["PORT"]["paraty"].downcase == "mark"
244 params["paraty"] = SerialPort::SPACE if conf["PORT"]["paraty"].downcase == "space"
245
246 puts("\nPort: "+conf["PORT"]["port"]+"@"    +
247                 params["baud"].to_s      +
248                 " "                      +
249                 params["data_bits"].to_s +
250                 conf["PORT"]["paraty"][0,1].upcase +
251                 params["stop_bits"].to_s +
252                 "\n")
253
254 $sp = SerialPort.new(conf["PORT"]["port"], params)
255
256 $sp.read_timeout=1000; # 5 minutes
257 $sp.flow_control = SerialPort::SOFT
258
259 reset_system()
260 algos=scan_system()
261 #puts algos.inspect
262
263 if opts["d"]
264   $debug = true
265 end
266
267 if opts["s"]
268   algos_rev = algos.invert
269   algo_tasks = Array.new
270   opts["s"].each_byte{ |x|
271     if algos_rev[x.chr]
272       algo_tasks << [algos_rev[x.chr],x.chr]
273     end
274   }
275 else
276   algo_tasks=algos.sort
277 end
278
279 algo_tasks.each do |algoa|
280   algo = algoa[0]
281   if conf[algo]==nil
282     puts("No test-set defined for #{algo} \r\n")
283     next
284   else
285         i=0
286         i = opts["j"] if opts["j"]
287         logfile=File.open(conf["PORT"]["testlogbase"]+algo+".txt", "a")
288         while conf[algo]["file_#{i}"] != nil
289           puts("Testing #{algo} with #{conf[algo]["file_#{i}"]}")
290           reset_system()
291           init_system(algoa[1])
292           skip=0
293           skip=opts["i"].to_i if opts["i"]
294           nerrors=run_test(conf[algo]["file_#{i}"], skip)
295       if nerrors == 0
296         puts("\n[ok]")
297         logfile.puts("[ok] "+conf[algo]["file_#{i}"]+ " ("+Time.now.to_s()+")")
298       else
299         puts("\n[errors: "+ nerrors.to_s() +"]")
300         logfile.puts("[error] "+nerrors.to_s+" "+conf[algo]["file_#{i}"]+ " ("+Time.now.to_s()+")")
301       end
302       i += 1
303     end
304     logfile.close()
305   end
306 end
307
308 =begin
309 nerrors = 0
310 for i in (5..(ARGV.size-1))
311   nerrors = run_test(ARGV[i])
312   if nerrors == 0
313     puts("\n[ok]")
314   else
315     puts("\n[errors: "+ nerrors.to_s() +"]")
316   end
317 end
318  $sp.print("EXIT\r");
319
320 #exit(0);
321 =end
322