4 This file is part of the ARM-Crypto-Lib.
5 Copyright (C) 2008, 2009 Daniel Otte (daniel.otte@rub.de)
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.
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.
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/>.
27 $buffer_size = 0 # set automatically in init_system
28 $conffile_check = Hash.new
29 $conffile_check.default = 0
31 ################################################################################
33 ################################################################################
35 def readconfigfile(fname, conf)
36 return conf if $conffile_check[fname]==1
37 $conffile_check[fname]=1
39 if not File.exists?(fname)
42 file = File.open(fname, "r")
45 next if /[\s]*#/.match(line)
46 if m=/\[[\s]*([^\s]*)[\s]*\]/.match(line)
51 next if ! /=/.match(line)
52 m=/[\s]*([^\s]*)[\s]*=[\s]*([^\s]*)/.match(line)
54 Dir.glob(m[2]){ |fn| conf = readconfigfile(fn, conf) }
56 conf[section][m[1]] = m[2]
63 ################################################################################
65 ################################################################################
74 ################################################################################
76 ################################################################################
80 $sp.print("shavs_list\r")
83 return algos if /^>$/.match(line)
84 if m = /[\*\ ]([a-z]):[\s]*([a-zA-Z0-9+_-]+)/.match(line)
90 ################################################################################
92 ################################################################################
94 def init_system(algo_select)
95 $sp.print("echo off \r")
96 print("DBG i: " + "echo off \r"+"\n") if $debug
98 $sp.print("shavs_set #{algo_select}\r")
99 print("DBG i: " + "shavs_set #{$algo_select} \r"+"\n") if $debug
101 $sp.print("shavs_test1 \r")
102 print("DBG i: " + "shavs_test1 \r"+"\n") if $debug
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)
110 ################################################################################
112 ################################################################################
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)
124 ################################################################################
126 ################################################################################
128 def send_md(md_string)
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))
135 end while not /\./.match(line)
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
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
155 t = md_string[(i*bs)..((i+1)*bs-1)]
156 printf("sending %d chars: %s\n", t.length, t[-10..-1]) if $debug
159 print("going to wait ... : ") if $debug
162 puts(line.inspect) if $debug
164 end while ! /\./.match(line)
169 ################################################################################
171 ################################################################################
173 def run_test(filename, skip=0)
176 if not File.exist?(filename)
177 puts("ERROR file "+filename+" does not exist!")
181 file = File.new(filename, "r");
185 # printf("DBG info: file read: %s", lb)
186 end while not (file.eof or (/[\s]*Len[\s]*=/.match(lb)))
192 len = /[\s]*Len[\s]*=[\s]*([0-9]*)/.match(lb)[1].to_i
197 puts("DBG sending: "+lb) if $debug
202 end while not (file.eof or (m=/[\s]*Msg[\s]*=[\s]*([0-9a-fA-F]*)/.match(lb)))
204 puts("DBG sending: "+lb) if $debug
206 puts("DBG sending [done] getting...") if $debug
208 puts("DBG getting [done]") if $debug
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];
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)
219 #putc((a==b)?'*':'!')
225 printf("\nError @%05d: %s [should]\n != %s [is]- ",len, a, b)
235 ################################################################################
237 ################################################################################
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
246 # -d enable debug mode
250 opts = Getopt::Std.getopts("s:f:i:j:hdcao")
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"]
260 puts("serial port interface version: " + SerialPort::VERSION);
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"
271 puts("\nPort: "+conf["PORT"]["port"]+"@" +
272 params["baud"].to_s +
274 params["data_bits"].to_s +
275 conf["PORT"]["paraty"][0,1].upcase +
276 params["stop_bits"].to_s +
279 $sp = SerialPort.new(conf["PORT"]["port"], params)
281 $sp.read_timeout=1000; # 5 minutes
282 $sp.flow_control = SerialPort::SOFT
293 algos_rev = algos.invert
294 algo_tasks = Array.new
295 opts["s"].each_byte{ |x|
297 algo_tasks << [algos_rev[x.chr],x.chr]
301 algo_tasks=algos.sort
304 algo_tasks.each do |algoa|
307 puts("No test-set defined for #{algo} \r\n")
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}"]}")
316 init_system(algoa[1])
318 skip=opts["i"].to_i if opts["i"]
319 nerrors=run_test(conf[algo]["file_#{i}"], skip)
322 logfile.puts("[ok] "+conf[algo]["file_#{i}"]+ " ("+Time.now.to_s()+")")
324 puts("\n[errors: "+ nerrors.to_s() +"]")
325 logfile.puts("[error] "+nerrors.to_s+" "+conf[algo]["file_#{i}"]+ " ("+Time.now.to_s()+")")
336 for i in (5..(ARGV.size-1))
337 nerrors = run_test(ARGV[i])
341 puts("\n[errors: "+ nerrors.to_s() +"]")