4 This file is part of the AVR-Crypto-Lib.
5 Copyright (C) 2008 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/>.
25 $buffer_size = 0 # set automatically in init_system
26 $conffile_check = Hash.new
27 $conffile_check.default = 0
31 ################################################################################
33 ################################################################################
35 def read_line_from_device()
36 repeat_counter = 10000
42 end while !l && repeat_counter > 0
44 $logfile.printf("DBG: (%02d:%02d:%02d)<< %s\n", t.hour, t.min, t.sec, l.inspect) if $debug
45 if l && l.include?("AVR-Crypto-Lib")
46 $logfile.printf("DBG: system crashed !!!\n")
52 def readconfigfile(fname, conf)
53 return conf if $conffile_check[fname]==1
54 $conffile_check[fname]=1
56 if not File.exists?(fname)
59 file = File.open(fname, "r")
62 next if /[\s]*#/.match(line)
63 if m=/\[[\s]*([^\s]*)[\s]*\]/.match(line)
68 next if ! /=/.match(line)
69 m=/[\s]*([^\s]*)[\s]*=[\s]*([^\s]*)/.match(line)
71 Dir.glob(m[2]){ |fn| conf = readconfigfile(fn, conf) }
73 conf[section][m[1]] = m[2]
80 ################################################################################
82 ################################################################################
89 $sp.print("echo off\r")
98 x = l.split.collect { |e| e.to_i(16) }
100 end while x.length == 16
121 def get_next_block(f)
126 end while l && ! m= l.match(/^#[\s](.*):[\s]*$/)
129 ret['line'] = f.lineno
139 'Public exponent', # 3
143 'Prime exponent 1', # 7
144 'Prime exponent 2', # 8
148 def key_consitency_check(k)
152 def process_file(f, skip_key=1, skip_vec=1)
153 a = get_next_block(f)
158 if !a || ! a['tag'] == 'Modulus'
159 printf("ERROR: a = %s %d\n", a.inspect, __LINE__)
164 (1..($key_sequence.length-1)).each do |i|
165 a = get_next_block(f)
166 if ! a || a['tag'] != $key_sequence[i]
167 printf("ERROR: (expecting: %s) a = %s %d\n", $key_sequence[i], a.inspect, __LINE__)
171 key = convert_key(k_seq)
172 printf("ERROR: %d\n", __LINE__) if ! key
175 printf("\n run %3d: ", key_no)
176 skip_key_flag = (key_no < skip_key)
177 load_key(key) if ! skip_key_flag
179 a = get_next_block(f)
180 printf("ERROR: %d\n", __LINE__) if ! a
183 b = get_next_block(f)
184 c = get_next_block(f)
186 tv['msg'] = a['data']
187 tv['seed'] = b['data']
188 tv['enc'] = c['data']
189 skip_vec_flag = (skip_key_flag || (key_no == skip_key && vec_no < skip_vec))
196 $logfile.printf("[[Test %2d.%02d = OK]]\n", key_no, vec_no)
199 printf('%c', v ? '*' : '!')
200 $logfile.printf("[[Test %2d.%02d = FAIL]]\n", key_no, vec_no)
204 a = get_next_block(f)
205 end while a && a['tag'] == 'Message'
206 end while a && a['tag'] = 'Modulus'
207 # printf("\nResult: %d OK / %d FAIL ==> %s \nFinished\n", ok_counter, fail_counter, fail_counter==0 ? ':-)' : ':-(')
208 return ok_counter,fail_counter
211 def convert_key(k_seq)
212 l = ['n', 'e', 'd', 'p', 'q', 'dP', 'dQ', 'qInv']
214 return nil if k_seq[0]['data'] != k_seq[2]['data']
215 return nil if k_seq[1]['data'] != k_seq[3]['data']
217 r[l[i]] = k_seq[2 + i]['data']
225 end while !s || !s.include?('.')
229 $sp.printf("%d\r", d.length)
230 while l = read_line_from_device()
231 break if /data:/.match(l)
233 printf "ERROR: got no answer from system!" if !l
236 $sp.printf("%02x", e)
239 # we should now wait for incomming dot
249 printf("\n\t") if i % 16 == 0
253 puts('') if i % 16 != 1
260 s += "\n\t" if i % 16 == 0
261 s += sprintf('%02x ', e)
264 s += "\n" if i % 16 != 1
269 $sp.print("load-key\r")
271 v = ['n', 'e', 'p', 'q', 'dP', 'dQ', 'qInv']
274 $logfile.printf("DBG: loaded %s\n", e) if $debug
276 while l = read_line_from_device()
277 break if />/.match(l)
281 def strip_leading_zeros(a)
283 return [] if a.length == 0
284 return a if a[0] != 0
291 $sp.print("seed-test\r")
293 load_bigint(tv['msg'])
294 $logfile.printf("DBG: loaded %s\n", 'msg') if $debug
296 tv['seed'].each { |e| $sp.printf(" %02x", e) }
297 while l = read_line_from_device()
298 break if /ciphertext:/.match(l)
302 l = read_line_from_device()
303 break if ! /([0-9A-Fa-f]{2}\s*)+/.match(l)
306 test_enc_a = Array.new
307 test_enc = test_enc.split(/[\W\r\n]+/)
309 v = e.sub(/[^0-9A-Fa-f]/, '')
310 test_enc_a << v if v.length == 2
312 test_enc_a.collect!{ |e| e.to_i(16) }
313 strip_leading_zeros(test_enc_a)
314 strip_leading_zeros(tv['enc'])
315 enc_ok = (test_enc_a == tv['enc'])
317 $logfile.printf("DBG: ref = %s test = %s\n", str_hexdump(tv['enc']) , str_hexdump(test_enc_a))
321 l = read_line_from_device()
322 m = /(>>OK<<|ERROR)/.match(l)
325 return true if enc_ok && (m[1] == '>>OK<<')
329 ########################################
331 ########################################
334 opts = Getopt::Std.getopts('dc:f:il:s:')
337 conf = readconfigfile("/etc/testport.conf", conf)
338 conf = readconfigfile("~/.testport.conf", conf)
339 conf = readconfigfile("testport.conf", conf)
340 conf = readconfigfile(opts["c"], conf) if opts["c"]
344 puts("serial port interface version: " + SerialPort::VERSION);
346 params = { "baud" => conf["PORT"]["baud"].to_i,
347 "data_bits" => conf["PORT"]["databits"].to_i,
348 "stop_bits" => conf["PORT"]["stopbits"].to_i,
349 "parity" => SerialPort::NONE }
350 params["paraty"] = SerialPort::ODD if conf["PORT"]["paraty"].downcase == "odd"
351 params["paraty"] = SerialPort::EVEN if conf["PORT"]["paraty"].downcase == "even"
352 params["paraty"] = SerialPort::MARK if conf["PORT"]["paraty"].downcase == "mark"
353 params["paraty"] = SerialPort::SPACE if conf["PORT"]["paraty"].downcase == "space"
355 puts("\nPort: "+conf["PORT"]["port"]+"@" +
356 params["baud"].to_s +
358 params["data_bits"].to_s +
359 conf["PORT"]["paraty"][0,1].upcase +
360 params["stop_bits"].to_s +
363 $sp = SerialPort.new(conf["PORT"]["port"], params)
365 $sp.read_timeout=1000; # 5 minutes
366 $sp.flow_control = SerialPort::SOFT
368 $debug = true if opts['d']
371 $logfile = File.open(opts['l'], 'w')
374 $logfile = STDOUT if ! $logfile
377 if opts['s'] && m = opts['s'].match(/([\d]+\.([\d]+))/)
385 f = File.open(opts['f'], "r")
387 ok,fail = process_file(f,sk,sv)
388 printf("\nOK: %d FAIL: %d :-%s\n",ok,fail, fail==0 ? ')':'(')