4 This file is part of the ARM-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
29 ################################################################################
31 ################################################################################
33 def read_line_from_device()
34 repeat_counter = 10000
40 end while !l && repeat_counter > 0
41 # printf("DBG: << %s\n", l.inspect)
45 def readconfigfile(fname, conf)
46 return conf if $conffile_check[fname]==1
47 $conffile_check[fname]=1
49 if not File.exists?(fname)
52 file = File.open(fname, "r")
55 next if /[\s]*#/.match(line)
56 if m=/\[[\s]*([^\s]*)[\s]*\]/.match(line)
61 next if ! /=/.match(line)
62 m=/[\s]*([^\s]*)[\s]*=[\s]*([^\s]*)/.match(line)
64 Dir.glob(m[2]){ |fn| conf = readconfigfile(fn, conf) }
66 conf[section][m[1]] = m[2]
73 ################################################################################
75 ################################################################################
82 $sp.print("echo off\r")
91 x = l.split.collect { |e| e.to_i(16) }
93 end while x.length == 16
97 def goto_next_header(f)
99 m = /^#\ (=|-)*[=-]{5}/.match(l)
100 t = :subblock if m && m[1] == '-'
101 t = :mainblock if m && m[1] == '='
102 if !m && n = /^#\ (.*)$/.match(l)
114 def skip_file_header(f)
116 return if m = /^#\ [=]{40}/.match(l)
123 a,b = goto_next_header(f)
129 printf(">>%sblock: %s\n", a==:mainblock ? "main":"sub", b)
132 printf(">item: %s\n", b)
134 printf(">length: %d (0x%x)\n>data:", d.length, d.length)
137 printf("\n>") if i % 16 == 0
145 >item: RSA modulus n:
146 >item: RSA public exponent e:
147 >item: RSA private exponent d:
150 >item: p's CRT exponent dP:
151 >item: q's CRT exponent dQ:
152 >item: CRT coefficient qInv:
158 q,id = goto_next_header(f)
160 m = /[\ \t]([^\ \t]*):[\ \t]*$/.match(id)
166 req_items = ['n', 'e', 'd', 'p', 'q', 'dP', 'dQ', 'qInv']
167 req_items.each do |e|
168 printf("ERROR: key component %s is missing!\n", e) if !h[e]
171 printf("ERROR: unknown item '%s'!\n", e) if !req_items.index(e)
177 >item: Message to be encrypted:
184 'Message to be encrypted:' => 'msg',
186 'Encryption:' => 'enc'}
189 q,id = goto_next_header(f)
192 printf("ERROR: unknown item '%s'!\n", id) if !n
195 req_items = ['msg', 'seed', 'enc']
196 req_items.each do |e|
197 printf("ERROR: testvector component %s is missing!\n", e) if !h[e]
199 while h['enc'][0] == 0
200 h['enc'].delete_at(0)
206 $sp.printf("%d\r", d.length)
207 while l = read_line_from_device()
208 break if /data:/.match(l)
210 printf "ERROR: got no answer from system!" if !l
212 $sp.printf(" %02x", e)
217 $sp.print("load-key\r")
219 v = ['n', 'e', 'p', 'q', 'dP', 'dQ', 'qInv']
222 # printf("DBG: loaded %s\n", e)
224 while l = read_line_from_device()
225 break if />/.match(l)
231 $sp.print("seed-test\r")
233 load_bigint(tv['msg'])
234 # printf("DBG: loaded %s\n", 'msg')
236 tv['seed'].each { |e| $sp.printf(" %02x", e) }
237 while l = read_line_from_device()
238 break if /ciphertext:/.match(l)
242 l = read_line_from_device()
243 break if /decrypting/.match(l)
246 test_enc_a = Array.new
247 test_enc = test_enc.split(/[\W\r\n]+/)
249 v = e.sub(/[^0-9A-Fa-f]/, '')
250 test_enc_a << v if v.length == 2
252 test_enc_a.collect!{ |e| e.to_i(16) }
253 enc_ok = (test_enc_a == tv['enc'])
255 printf("DBG: ref = %s test = %s\n", tv['enc'].inspect , test_enc_a.inspect)
259 l = read_line_from_device()
260 m = /(>>OK<<|ERROR)/.match(l)
263 return true if enc_ok && (m[1] == '>>OK<<')
272 a,b = goto_next_header(f)
273 # printf("DBG: a=%s b=%s\n", a.inspect, b.inspect)
276 # Example 1: A 1024-bit RSA Key Pair
277 b.sub!(/[\d]+:/) { |s| sprintf("%3d,", s.to_i)}
278 printf("\n>> %s: ", b)
279 # (35-b.length).times { putc(' ')}
282 if b == 'Components of the RSA Key Pair'
300 ########################################
302 ########################################
305 opts = Getopt::Std.getopts("c:f:")
308 conf = readconfigfile("/etc/testport.conf", conf)
309 conf = readconfigfile("~/.testport.conf", conf)
310 conf = readconfigfile("testport.conf", conf)
311 conf = readconfigfile(opts["c"], conf) if opts["c"]
315 puts("serial port interface version: " + SerialPort::VERSION);
317 params = { "baud" => conf["PORT"]["baud"].to_i,
318 "data_bits" => conf["PORT"]["databits"].to_i,
319 "stop_bits" => conf["PORT"]["stopbits"].to_i,
320 "parity" => SerialPort::NONE }
321 params["paraty"] = SerialPort::ODD if conf["PORT"]["paraty"].downcase == "odd"
322 params["paraty"] = SerialPort::EVEN if conf["PORT"]["paraty"].downcase == "even"
323 params["paraty"] = SerialPort::MARK if conf["PORT"]["paraty"].downcase == "mark"
324 params["paraty"] = SerialPort::SPACE if conf["PORT"]["paraty"].downcase == "space"
326 puts("\nPort: "+conf["PORT"]["port"]+"@" +
327 params["baud"].to_s +
329 params["data_bits"].to_s +
330 conf["PORT"]["paraty"][0,1].upcase +
331 params["stop_bits"].to_s +
334 $sp = SerialPort.new(conf["PORT"]["port"], params)
336 $sp.read_timeout=1000; # 5 minutes
337 $sp.flow_control = SerialPort::SOFT
341 f = File.open(opts['f'], "r")
343 ok,fail = run_test(f)
344 printf("\nOK: %d FAIL: %d :-%s\n",ok,fail, fail==0 ? ')':'(')