4 This file is part of the AVR-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/>.
29 $conffile_check = Hash.new
30 $conffile_check.default = 0
32 ################################################################################
34 ################################################################################
36 def readconfigfile(fname, conf)
37 return conf if $conffile_check[fname]==1
38 $conffile_check[fname]=1
40 if not File.exists?(fname)
43 file = File.open(fname, "r")
46 next if /[\s]*#/.match(line)
47 if m=/\[[\s]*([^\s]*)[\s]*\]/.match(line)
52 next if not /=/.match(line)
53 m=/[\s]*([^\s]*)[\s]*=[\s]*([^\s]*)/.match(line)
55 Dir.glob(m[2]){ |fn| conf = readconfigfile(fn, conf) }
57 conf[section][m[1]] = m[2]
64 ################################################################################
66 ################################################################################
68 def expmod(base, power, mod)
71 result = (result * base) % mod if power & 1 == 1
72 base = (base * base) % mod
78 ################################################################################
80 ################################################################################
84 while(x&1==0 && y&1==0) do
89 u=x; v=y; a=1; b=0; c=0; d=1
114 ################################################################################
116 ################################################################################
125 ################################################################################
127 ################################################################################
129 def init_system(test_prog)
130 $sp.print("echo off \r")
131 print("DBG i: " + "echo off \r"+"\n") if $debug
133 $sp.print("#{test_prog}\r")
134 print("DBG i: " + "#{test_prog} \r"+"\n") if $debug
139 ################################################################################
141 ################################################################################
142 def screen_progress(v)
144 printf("\n%5d [%04d]: ", $testno, $size)
146 putc((v)?('*'):('!'))
148 $linepos = ($linepos+1)%$linewidth
151 ################################################################################
153 ################################################################################
158 line = "" if line==nil
159 puts("DBG got: "+line) if $debug
160 if /^Error:.*/.match(line)
164 end while not /[\s]*enter a:[\s]*/.match(line)
165 $sp.print(a.to_s(16)+" ")
168 line = "" if line==nil
169 puts("DBG got: "+line) if $debug
170 if /^Error:.*/.match(line)
174 end while not /[\s]*enter b:[\s]*/.match(line)
175 $sp.print(b.to_s(16)+" ")
178 line = "" if line==nil
179 puts("DBG got: "+line) if $debug
180 if /^Error:.*/.match(line)
184 end while not m=/[\s]*([-]?[0-9a-fA-F]*)[\s]+\+[\s]+([+-]?[0-9a-fA-F]*)[\s]*=[\s]*([+-]?[0-9a-fA-F]*)/.match(line)
189 if(a_== a && b_ == b && c_ == (a+b))
190 $logfile.printf("[pass]: %s\n", line)
193 $logfile.printf("[fail (%s%s%s)]: %s", (a==a_)?"":"a", (b==b_)?"":"b", (c_==a+b)?"":"c",line)
194 $logfile.printf(" ; should %s + %s = %s\n", a.to_s(16), b.to_s(16), (a+b).to_s(16))
200 ################################################################################
202 ################################################################################
207 line = "" if line==nil
208 puts("DBG got: "+line) if $debug
209 if /^Error:.*/.match(line)
213 end while not /[\s]*enter a:[\s]*/.match(line)
214 $sp.print(a.to_s(16)+" ")
217 line = "" if line==nil
218 puts("DBG got: "+line) if $debug
219 if /^Error:.*/.match(line)
223 end while not /[\s]*enter b:[\s]*/.match(line)
224 $sp.print(b.to_s(16)+" ")
227 line = "" if line==nil
228 puts("DBG got: "+line) if $debug
229 if /^Error:.*/.match(line)
233 end while not m=/[\s]*([+-]?[0-9a-fA-F]*)[\s]+\*[\s]+([+-]?[0-9a-fA-F]*)[\s]*=[\s]*([+-]?[0-9a-fA-F]*)/.match(line)
238 if(a_== a && b_ == b && c_ == (a*b))
239 $logfile.printf("[pass]: %s\n", line)
242 $logfile.printf("[fail (%s%s%s)]: %s", (a==a_)?"":"a", (b==b_)?"":"b", (c_==a*b)?"":"c",line)
243 $logfile.printf(" ; should %s * %s = %s\n", a.to_s(16), b.to_s(16), (a*b).to_s(16))
249 ################################################################################
251 ################################################################################
256 line = "" if line==nil
257 puts("DBG got: "+line) if $debug
258 if /^Error:.*/.match(line)
262 end while not /[\s]*enter a:[\s]*/.match(line)
263 $sp.print(a.to_s(16)+" ")
266 line = "" if line==nil
267 puts("DBG got: "+line) if $debug
268 if /^Error:.*/.match(line)
272 end while not m=/[\s]*([+-]?[0-9a-fA-F]*)[\s]*\*\*2[\s]*=[\s]*([+-]?[0-9a-fA-F]*)/.match(line)
276 if(a_== a && c_ == (a**2))
277 $logfile.printf("[pass]: %s\n", line)
280 $logfile.printf("[fail (%s%s)]: %s", (a==a_)?"":"a", (c_==a**2)?"":"c",line)
281 $logfile.printf(" ; should %s **2 = %s\n", a.to_s(16), (a**2).to_s(16))
287 ################################################################################
289 ################################################################################
294 line = "" if line==nil
295 puts("DBG got: "+line) if $debug
296 if /^Error:.*/.match(line)
300 end while not /[\s]*enter a:[\s]*/.match(line)
301 $sp.print(a.to_s(16)+" ")
304 line = "" if line==nil
305 puts("DBG got: "+line) if $debug
306 if /^Error:.*/.match(line)
310 end while not /[\s]*enter b:[\s]*/.match(line)
311 $sp.print(b.to_s(16)+" ")
314 line_tmp = $sp.gets()
315 line_tmp = '' if line_tmp==nil
317 puts("DBG got: "+line) if $debug
318 if /^Error:.*/.match(line)
322 end while not m=/[\s]*([+-]?[0-9a-fA-F]*)[\s]+%[\s]+([+-]?[0-9a-fA-F]*)[\s]*=[\s]*([+-]?[0-9a-fA-F]+)/.match(line)
327 if(a_== a && b_ == b && c_ == (a%b))
328 $logfile.printf("[pass]: %s\n", line)
331 $logfile.printf("[fail (%s%s%s)]: %s", (a==a_)?"":"a", (b==b_)?"":"b", (c_==a%b)?"":"c",line)
332 $logfile.printf(" ; should %s %% %s = %s\n", a.to_s(16), b.to_s(16), (a%b).to_s(16))
338 ################################################################################
340 ################################################################################
342 def expmod_test(a,b,c)
345 line = "" if line==nil
346 puts("DBG got: "+line) if $debug
347 if /^Error:.*/.match(line)
351 end while not /[\s]*enter a:[\s]*/.match(line)
352 $sp.print(a.to_s(16)+" ")
355 line = "" if line==nil
356 puts("DBG got: "+line) if $debug
357 if /^Error:.*/.match(line)
361 end while not /[\s]*enter b:[\s]*/.match(line)
362 $sp.print(b.to_s(16)+" ")
365 line = "" if line==nil
366 puts("DBG got: "+line) if $debug
367 if /^Error:.*/.match(line)
371 end while not /[\s]*enter c:[\s]*/.match(line)
372 $sp.print(c.to_s(16)+" ")
375 line_tmp = $sp.gets()
376 line_tmp = '' if line_tmp==nil
378 puts("DBG got: "+line) if $debug
379 if /^Error:.*/.match(line)
383 end while not m=/[\s]*([+-]?[0-9a-fA-F]*)\*\*([+-]?[0-9a-fA-F]*)[\s]+%[\s]+([+-]?[0-9a-fA-F]*)[\s]*=[\s]*([+-]?[0-9a-fA-F]+)/.match(line)
389 if(a_== a && b_ == b && c_ == c && d_ ==expmod(a,b,c) )
390 $logfile.printf("[pass]: %s\n", line)
393 $logfile.printf("[fail (%s%s%s%s)]: %s", (a==a_)?'':'a', (b==b_)?'':'b', (c_==c)?'':'c', (d_==expmod(a,b,c))?'':'d',line)
394 $logfile.printf(" ; should %s**%s %% %s = %s\n", a.to_s(16), b.to_s(16), c.to_s(16), expmod(a,b,c).to_s(16))
400 ################################################################################
402 ################################################################################
407 line = "" if line==nil
408 puts("DBG got: "+line) if $debug
409 if /^Error:.*/.match(line)
413 end while not /[\s]*enter a:[\s]*/.match(line)
414 $sp.print(a.to_s(16)+" ")
417 line = "" if line==nil
418 puts("DBG got: "+line) if $debug
419 if /^Error:.*/.match(line)
423 end while not /[\s]*enter b:[\s]*/.match(line)
424 $sp.print(b.to_s(16)+" ")
427 line_tmp = $sp.gets()
428 line_tmp = '' if line_tmp==nil
429 line = '' if line.end_with?('\n')
431 puts("DBG got: "+line) if $debug
432 if /^Error:.*/.match(line)
436 end while not m=/gcdext\([\s]*([+-]?[0-9a-fA-F]*)[\s]*,[\s]*([+-]?[0-9a-fA-F]*)[\s]*\)[\s]*=> a = ([+-]?[0-9a-fA-F]+); b = ([+-]?[0-9a-fA-F]+); gcd = ([+-]?[0-9a-fA-F]+)/.match(line)
446 if(a_== a && b_ == b && c_ == ref[1] && d_ == ref[2] && e_== ref[0])
447 $logfile.printf("[pass]: %s\n", line)
450 $logfile.printf("[fail (%s%s%s%s%s)]: %s", (a==a_)?'':'a', (b==b_)?'':'b',
451 (c_==ref[1])?'':'c', (d_==ref[2])?'':'d', (e_==ref[0])?'':'e', line)
452 $logfile.printf(" ; should gcdext( %s, %s) => a = %s; b = %s; gcd = %s\n",
453 a.to_s(16), b.to_s(16), ref[1].to_s(16), ref[2].to_s(16), ref[0].to_s(16))
459 ################################################################################
461 ################################################################################
463 def run_test_add(skip=0)
471 a = rand(256**length_a_B) * s_a
472 b = rand(256**length_a_B) * s_b
481 b_size = rand(length_b_B+1)
482 a = rand(256**length_a_B) * s_a
483 b = rand(256**b_size) * s_b
492 end while length_a_B<4096/8
495 ################################################################################
497 ################################################################################
499 def run_test_mul(skip=0)
507 a = rand(256**length_a_B) * s_a
508 b = rand(256**length_a_B) * s_b
517 b_size = rand(length_b_B+1)
518 a = rand(256**length_a_B) * s_a
519 b = rand(256**b_size) * s_b
527 end while length_a_B<4096/8
530 ################################################################################
532 ################################################################################
534 def run_test_square(skip=0)
539 a = rand(256**length_a_B)
544 end while length_a_B<4096/8
547 ################################################################################
549 ################################################################################
551 def run_test_reduce(skip=0)
557 a = rand(256**length_a_B)
558 b = rand(256**length_a_B)+1
559 v = reduce_test(a, b)
563 b_size = rand(length_b_B+1)
564 a = rand(256**length_a_B)
565 b = rand(256**b_size)+1
566 v = reduce_test(a, b)
571 end while length_a_B<4096/8
574 ################################################################################
576 ################################################################################
578 def run_test_expmod(skip=0)
585 a = rand(256**length_a_B)
586 b = rand(256**length_b_B)+1
587 c = rand(256**length_c_B)+1
588 v = expmod_test(a, b, c)
592 b_size = rand(length_b_B+1)
593 a = rand(256**length_a_B)
594 b = rand(256**b_size)+1
595 c = rand(256**b_size)+1
596 v = expmod_test(a, b, c)
601 end while length_a_B<4096/8
604 ################################################################################
606 ################################################################################
608 def run_test_gcdext(skip=0)
614 a = rand(256**length_a_B)
615 b = rand(256**length_a_B)+1
616 v = gcdext_test(a, b)
621 b_size = rand(length_b_B+1)
622 a = rand(256**length_a_B)
623 b = rand(256**b_size)+1
624 v = gcdext_test(a, b)
630 end while length_a_B<4096/8
633 ################################################################################
635 ################################################################################
637 opts = Getopt::Std.getopts("s:f:i:a:hd")
640 conf = readconfigfile("/etc/testport.conf", conf)
641 conf = readconfigfile("~/.testport.conf", conf)
642 conf = readconfigfile("testport.conf", conf)
643 conf = readconfigfile(opts["f"], conf) if opts["f"]
647 puts("serial port interface version: " + SerialPort::VERSION);
651 params = { "baud" => conf["PORT"]["baud"].to_i,
652 "data_bits" => conf["PORT"]["databits"].to_i,
653 "stop_bits" => conf["PORT"]["stopbits"].to_i,
654 "parity" => SerialPort::NONE }
655 params["paraty"] = SerialPort::ODD if conf["PORT"]["paraty"].downcase == "odd"
656 params["paraty"] = SerialPort::EVEN if conf["PORT"]["paraty"].downcase == "even"
657 params["paraty"] = SerialPort::MARK if conf["PORT"]["paraty"].downcase == "mark"
658 params["paraty"] = SerialPort::SPACE if conf["PORT"]["paraty"].downcase == "space"
660 puts("\nPort: "+conf["PORT"]["port"]+"@" +
661 params["baud"].to_s +
663 params["data_bits"].to_s +
664 conf["PORT"]["paraty"][0,1].upcase +
665 params["stop_bits"].to_s +
668 $sp = SerialPort.new(conf["PORT"]["port"], params)
670 $sp.read_timeout=1000; # 5 minutes
671 $sp.flow_control = SerialPort::SOFT
679 logfilename = conf['PORT']['testlogbase']+'bigint.txt'
680 if File.exists?(logfilename)
683 logfilename = sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i,'.txt')
685 end while(File.exists?(logfilename))
687 File.move(sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i-2,'.txt'),
688 sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i-1,'.txt'), true)
691 File.move(sprintf('%s%s', conf['PORT']['testlogbase'],'bigint.txt'),
692 sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',1,'.txt'), true)
693 logfilename = conf['PORT']['testlogbase']+'bigint.txt'
695 $logfile = File.open(logfilename, 'w')
696 printf("logfile: %s\n", logfilename)
698 $logfile.printf("bigint test from: %s\n", Time.now.to_s)
699 $logfile.printf("skip = %s\n", opts['s']) if opts['s']
700 $logfile.printf("seed = 0x%X\n", 0xdeadbeef)
703 tests['a'] = proc {|x| run_test_add(x) }
704 tests['m'] = proc {|x| run_test_mul(x) }
705 tests['s'] = proc {|x| run_test_square(x) }
706 tests['r'] = proc {|x| run_test_reduce(x) }
707 tests['e'] = proc {|x| run_test_expmod(x) }
708 tests['g'] = proc {|x| run_test_gcdext(x) }
710 init_str['a'] = 'add-test'
711 init_str['m'] = 'mul-test'
712 init_str['s'] = 'square-test'
713 init_str['r'] = 'reduce-test'
714 init_str['e'] = 'expmod-test'
715 init_str['g'] = 'gcdext-test'
720 opts['a'].each_char do |x|
723 init_system(init_str[x])
724 tests[x].call(opts['s']?opts['s'].to_i():0)
726 puts "no test defiened for '#{x}'"
730 'amsre'.each_char do |x|
733 init_system(init_str[x])
734 tests[x].call(opts['s']?opts['s'].to_i():0)
736 puts "no test defiened for '#{x}'"