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 ! 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 ! /=/.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 ################################################################################
83 return [0, 0, 0] if(x == 0 || y == 0)
85 while(x&1==0 && y&1==0) do
90 u=x; v=y; a=1; b=0; c=0; d=1
115 ################################################################################
117 ################################################################################
126 ################################################################################
128 ################################################################################
130 def init_system(test_prog)
134 $sp.print("echo off \r")
135 print("DBG i: " + "echo off \r"+"\n") if $debug
137 $sp.print("#{test_prog}\r")
138 print("DBG i: " + "#{test_prog} \r"+"\n") if $debug
142 ################################################################################
144 ################################################################################
146 def wait_for_prompt(prompt)
147 prompt = /[\s]*#{prompt}[\s]*/ if(prompt.class == String)
148 start_time = Time.now.to_i
152 puts("DBG got (#{__LINE__}): "+line) if $debug && line
153 line = "" if line==nil
154 if /^(Error:|Crypto-VS).*/.match(line)
158 if (Time.now.to_i- start_time) > $max_timeout
162 end while ! m=prompt.match(acc)
166 ################################################################################
168 ################################################################################
169 def screen_progress(v)
171 printf("\n%5d [%04d]: ", $testno, $size)
173 putc((v)?('*'):('!'))
175 $linepos = ($linepos+1)%$linewidth
178 ################################################################################
180 ################################################################################
185 line = "" if line==nil
186 puts("DBG got: "+line) if $debug
187 if /^Error:.*/.match(line)
191 end while ! /[\s]*enter a:[\s]*/.match(line)
192 $sp.print(a.to_s(16)+" ")
195 line = "" if line==nil
196 puts("DBG got: "+line) if $debug
197 if /^Error:.*/.match(line)
201 end while ! /[\s]*enter b:[\s]*/.match(line)
202 $sp.print(b.to_s(16)+" ")
205 line = "" if line==nil
206 puts("DBG got: "+line) if $debug
207 if /^Error:.*/.match(line)
211 end while ! m=/[\s]*([-]?[0-9a-fA-F]*)[\s]+\+[\s]+([+-]?[0-9a-fA-F]*)[\s]*=[\s]*([+-]?[0-9a-fA-F]*)/.match(line)
216 if(a_== a && b_ == b && c_ == (a+b))
217 $logfile.printf("[pass]: %s\n", line)
220 $logfile.printf("[fail (%s%s%s)]: %s", (a==a_)?"":"a", (b==b_)?"":"b", (c_==a+b)?"":"c",line)
221 $logfile.printf(" ; should %s + %s = %s\n", a.to_s(16), b.to_s(16), (a+b).to_s(16))
227 ################################################################################
229 ################################################################################
234 line = "" if line==nil
235 puts("DBG got: "+line) if $debug
236 if /^Error:.*/.match(line)
240 end while ! /[\s]*enter a:[\s]*/.match(line)
241 $sp.print(a.to_s(16)+" ")
244 line = "" if line==nil
245 puts("DBG got: "+line) if $debug
246 if /^Error:.*/.match(line)
250 end while ! /[\s]*enter b:[\s]*/.match(line)
251 $sp.print(b.to_s(16)+" ")
254 line = "" if line==nil
255 puts("DBG got: "+line) if $debug
256 if /^Error:.*/.match(line)
260 end while ! m=/[\s]*([+-]?[0-9a-fA-F]*)[\s]+\*[\s]+([+-]?[0-9a-fA-F]*)[\s]*=[\s]*([+-]?[0-9a-fA-F]*)/.match(line)
265 if(a_== a && b_ == b && c_ == (a*b))
266 $logfile.printf("[pass]: %s\n", line)
269 $logfile.printf("[fail (%s%s%s)]: %s", (a==a_)?"":"a", (b==b_)?"":"b", (c_==a*b)?"":"c",line)
270 $logfile.printf(" ; should %s * %s = %s\n", a.to_s(16), b.to_s(16), (a*b).to_s(16))
276 ################################################################################
278 ################################################################################
279 def add_scale_test_dummy(a, b, scale)
280 should = a + (b<<(8*scale))
281 printf("[dummy] %s + %s <<8*%04x = %s\n",a.to_s(16), b.to_s(16), scale, should.to_s(16))
284 def add_scale_test(a, b, scale)
285 m = wait_for_prompt("enter a:")
287 puts("DBG put (#{__LINE__}): "+a.to_s(16)+" ") if $debug
288 $sp.print(a.to_s(16)+" ")
289 m = wait_for_prompt("enter b:")
291 puts("DBG put (#{__LINE__}): "+b.to_s(16)+" ") if $debug
292 $sp.print(b.to_s(16)+" ")
293 m = wait_for_prompt("enter scale:")
295 puts("DBG put (#{__LINE__}): "+scale.to_s(10)+" ") if $debug
296 $sp.print(scale.to_s(10)+"\r")
297 should = a + (b<<(8*scale))
298 m = wait_for_prompt(/[\s]*([-]?[0-9a-fA-F]+)[\s]+\+[\s]+([+-]?[0-9a-fA-F]+)[\s]*<<8\*[\s]*([+-]?[0-9a-fA-F]+)[\s]*=[\s]*([+-]?[0-9a-fA-F]+)/)
300 $logfile.printf("[fail (CRASH)]:")
301 $logfile.printf(" ; should %s + %s << 8*%s = %s\n", a.to_s(16), b.to_s(16), scale.to_s(16), should.to_s(16))
310 if(a_== a && b_ == b && s_ == scale && c_ == should )
311 $logfile.printf("[pass]: %s\n", line)
314 $logfile.printf("[fail (%s%s%s%s)]: %s", (a==a_)?"":"a", (b==b_)?"":"b", (scale==s_)?"":"s",(c_==should)?"":"c",line)
315 $logfile.printf(" ; should %s + %s << 8*%s = %s\n", a.to_s(16), b.to_s(16), scale.to_s(16), should.to_s(16))
321 ################################################################################
323 ################################################################################
328 line = "" if line==nil
329 puts("DBG got: "+line) if $debug
330 if /^Error:.*/.match(line)
334 end while ! /[\s]*enter a:[\s]*/.match(line)
335 $sp.print(a.to_s(16)+" ")
338 line = "" if line==nil
339 puts("DBG got: "+line) if $debug
340 if /^Error:.*/.match(line)
344 end while ! m=/[\s]*([+-]?[0-9a-fA-F]*)[\s]*\*\*2[\s]*=[\s]*([+-]?[0-9a-fA-F]*)/.match(line)
348 if(a_== a && c_ == (a**2))
349 $logfile.printf("[pass]: %s\n", line)
352 $logfile.printf("[fail (%s%s)]: %s", (a==a_)?"":"a", (c_==a**2)?"":"c",line)
353 $logfile.printf(" ; should %s **2 = %s\n", a.to_s(16), (a**2).to_s(16))
359 ################################################################################
361 ################################################################################
366 line = "" if line==nil
367 puts("DBG got: "+line) if $debug
368 if /^Error:.*/.match(line)
372 end while ! /[\s]*enter a:[\s]*/.match(line)
373 $sp.print(a.to_s(16)+" ")
376 line = "" if line==nil
377 puts("DBG got: "+line) if $debug
378 if /^Error:.*/.match(line)
382 end while ! /[\s]*enter b:[\s]*/.match(line)
383 $sp.print(b.to_s(16)+" ")
386 line_tmp = $sp.gets()
387 line_tmp = '' if line_tmp==nil
389 puts("DBG got: "+line) if $debug
390 if /^Error:.*/.match(line)
394 end while ! m=/[\s]*([+-]?[0-9a-fA-F]*)[\s]+%[\s]+([+-]?[0-9a-fA-F]*)[\s]*=[\s]*([+-]?[0-9a-fA-F]+)/.match(line)
399 if(a_== a && b_ == b && c_ == (a%b))
400 $logfile.printf("[pass]: %s\n", line)
403 $logfile.printf("[fail (%s%s%s)]: %s", (a==a_)?"":"a", (b==b_)?"":"b", (c_==a%b)?"":"c",line)
404 $logfile.printf(" ; should %s %% %s = %s\n", a.to_s(16), b.to_s(16), (a%b).to_s(16))
410 ################################################################################
412 ################################################################################
414 def expmod_test(a,b,c)
416 printf("[testing] expmod(%#x, %#x, %#x)\n",a,b,c) if $debug
418 line = "" if line==nil
419 puts("DBG got: "+line) if $debug
420 if /^Error:.*/.match(line)
424 end while ! /[\s]*enter a:[\s]*/.match(line)
425 $sp.print(a.to_s(16)+" ")
428 line = "" if line==nil
429 puts("DBG got: "+line) if $debug
430 if /^Error:.*/.match(line)
434 end while ! /[\s]*enter b:[\s]*/.match(line)
435 $sp.print(b.to_s(16)+" ")
438 line = "" if line==nil
439 puts("DBG got: "+line) if $debug
440 if /^Error:.*/.match(line)
444 end while ! /[\s]*enter c:[\s]*/.match(line)
445 $sp.print(c.to_s(16)+" ")
448 line_tmp = $sp.gets()
449 line_tmp = '' if line_tmp==nil
451 puts("DBG got: "+line) if $debug
452 if /^Error:.*/.match(line)
456 end while ! m=/[\s]*([+-]?[0-9a-fA-F]*)\*\*([+-]?[0-9a-fA-F]*)[\s]+%[\s]+([+-]?[0-9a-fA-F]*)[\s]*=[\s]*([+-]?[0-9a-fA-F]+)/.match(line)
462 if(a_== a && b_ == b && c_ == c && d_ ==expmod(a,b,c) )
463 $logfile.printf("[pass]: %s\n", line)
466 $logfile.printf("[fail (%s%s%s%s)]: %s", (a==a_)?'':'a', (b==b_)?'':'b', (c_==c)?'':'c', (d_==expmod(a,b,c))?'':'d',line)
467 $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))
473 ################################################################################
475 ################################################################################
478 $logfile.printf("[testing] gcdext(%s, %s)\n", a.to_s(16), b.to_s(16))
481 line = "" if line==nil
482 puts("DBG got: "+line) if $debug
483 if /^Error:.*/.match(line)
487 end while ! /[\s]*enter a:[\s]*/.match(line)
488 $sp.print(a.to_s(16)+" ")
491 line = "" if line==nil
492 puts("DBG got: "+line) if $debug
493 if /^Error:.*/.match(line)
497 end while ! /[\s]*enter b:[\s]*/.match(line)
498 $sp.print(b.to_s(16)+" ")
501 line_tmp = $sp.gets()
502 line_tmp = '' if line_tmp==nil
503 line = '' if line.end_with?('\n')
505 puts("DBG got: "+line) if $debug
506 if /^Error:.*/.match(line)
510 end while ! 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)
520 if(a_== a && b_ == b && c_ == ref[1] && d_ == ref[2] && e_== ref[0])
521 $logfile.printf("[pass]: %s\n", line)
524 $logfile.printf("[fail (%s%s%s%s%s)]: %s", (a==a_)?'':'a', (b==b_)?'':'b',
525 (c_==ref[1])?'':'c', (d_==ref[2])?'':'d', (e_==ref[0])?'':'e', line)
526 $logfile.printf(" ; should gcdext( %s, %s) => a = %s; b = %s; gcd = %s\n",
527 a.to_s(16), b.to_s(16), ref[1].to_s(16), ref[2].to_s(16), ref[0].to_s(16))
533 ################################################################################
535 ################################################################################
537 def run_test_add(skip=0)
545 a = rand(256**length_a_B) * s_a
546 b = rand(256**length_a_B) * s_b
555 b_size = rand(length_b_B+1)
556 a = rand(256**length_a_B) * s_a
557 b = rand(256**b_size) * s_b
566 end while length_a_B<4096/8
569 ################################################################################
570 # run_test_add_scale #
571 ################################################################################
573 def run_test_add_scale(skip=0)
580 16.times { scales << rand(301) }
582 scales.each do |scale|
583 a = rand(256**length_a_B)
584 b = rand(256**length_a_B)
585 v = add_scale_test(a, b, scale)
587 v = add_scale_test(b, a, scale)
593 16.times { scales << rand(301) }
595 scales.each do |scale|
596 b_size = rand(length_b_B+1)+1
597 a = rand(256**length_a_B)
598 b = rand(256**b_size)
599 v = add_scale_test(a, b, scale)
601 v = add_scale_test(b, a, scale)
607 end while length_a_B<4096/8
610 def run_test_add_scale_dummy(skip=0)
617 16.times { scales << rand(301) }
619 scales.each do |scale|
620 a = rand(256**length_a_B)
621 b = rand(256**length_a_B)
622 v = add_scale_test_dummy(a, b, scale)
623 v = add_scale_test_dummy(b, a, scale)
628 16.times { scales << rand(301) }
630 scales.each do |scale|
631 b_size = rand(length_b_B+1)
632 a = rand(256**length_a_B)
633 b = rand(256**b_size)
634 v = add_scale_test_dummy(a, b, scale)
635 v = add_scale_test_dummy(b, a, scale)
640 end while length_a_B<4096/8
643 ################################################################################
645 ################################################################################
647 def run_test_mul(skip=0)
655 a = rand(256**length_a_B) * s_a
656 b = rand(256**length_a_B) * s_b
665 b_size = rand(length_b_B+1)
666 a = rand(256**length_a_B) * s_a
667 b = rand(256**b_size) * s_b
675 end while length_a_B<4096/8
678 ################################################################################
680 ################################################################################
682 def run_test_square(skip=0)
687 a = rand(256**length_a_B)
692 end while length_a_B<4096/8
695 ################################################################################
697 ################################################################################
699 def run_test_reduce(skip=0)
705 a = rand(256**length_a_B)
706 b = rand(256**length_a_B)+1
707 v = reduce_test(a, b)
711 b_size = rand(length_b_B+1)
712 a = rand(256**length_a_B)
713 b = rand(256**b_size)+1
714 v = reduce_test(a, b)
719 end while length_a_B<4096/8
722 ################################################################################
724 ################################################################################
726 def run_test_expmod(skip=0)
733 a = rand(256**length_a_B)
734 b = rand(256**length_b_B)+1
735 c = rand(256**length_c_B)+1
736 v = expmod_test(a, b, c)
740 b_size = rand(length_b_B+1)
741 a = rand(256**length_a_B)
742 b = rand(256**b_size)+1
743 c = rand(256**b_size)+1
744 v = expmod_test(a, b, c)
749 end while length_a_B<4096/8
752 ################################################################################
754 ################################################################################
756 def run_test_gcdext(skip=0)
762 a = rand(256**length_a_B)
763 b = rand(256**length_a_B)+1
764 v = gcdext_test(a, b)
769 b_size = rand(length_b_B+1)
770 a = rand(256**length_a_B)
771 b = rand(256**b_size)+1
772 v = gcdext_test(a, b)
778 end while length_a_B<4096/8
781 def init_serialport(conf)
782 puts("serial port interface version: " + SerialPort::VERSION);
786 params = { "baud" => conf["PORT"]["baud"].to_i,
787 "data_bits" => conf["PORT"]["databits"].to_i,
788 "stop_bits" => conf["PORT"]["stopbits"].to_i,
789 "parity" => SerialPort::NONE }
790 params["paraty"] = SerialPort::ODD if conf["PORT"]["paraty"].downcase == "odd"
791 params["paraty"] = SerialPort::EVEN if conf["PORT"]["paraty"].downcase == "even"
792 params["paraty"] = SerialPort::MARK if conf["PORT"]["paraty"].downcase == "mark"
793 params["paraty"] = SerialPort::SPACE if conf["PORT"]["paraty"].downcase == "space"
795 puts("\nPort: "+conf["PORT"]["port"]+"@" +
796 params["baud"].to_s +
798 params["data_bits"].to_s +
799 conf["PORT"]["paraty"][0,1].upcase +
800 params["stop_bits"].to_s +
803 $sp = SerialPort.new(conf["PORT"]["port"], params)
805 $sp.read_timeout=1000; # 5 minutes
806 $sp.flow_control = SerialPort::SOFT
811 ################################################################################
813 ################################################################################
815 opts = Getopt::Std.getopts("s:f:i:a:hd")
818 conf = readconfigfile("/etc/testport.conf", conf)
819 conf = readconfigfile("~/.testport.conf", conf)
820 conf = readconfigfile("testport.conf", conf)
821 conf = readconfigfile(opts["f"], conf) if opts["f"]
824 init_serialport(conf)
826 $max_timeout = 5 * 60
832 logfilename = conf['PORT']['testlogbase']+'bigint.txt'
833 if File.exists?(logfilename)
836 logfilename = sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i,'.txt')
838 end while(File.exists?(logfilename))
840 n1 = sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i-2,'.txt')
841 n2 = sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i-1,'.txt')
843 printf("%s -> %s\n", n1, n2)
846 n1 = sprintf('%s%s', conf['PORT']['testlogbase'],'bigint.txt')
847 n2 = sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',1,'.txt')
849 printf("%s -> %s\n", n1, n2)
850 logfilename = conf['PORT']['testlogbase']+'bigint.txt'
852 $logfile = File.open(logfilename, 'w')
853 printf("logfile: %s\n", logfilename)
855 $logfile.printf("bigint test from: %s\n", Time.now.to_s)
856 $logfile.printf("skip = %s\n", opts['s']) if opts['s']
857 $logfile.printf("seed = 0x%X\n", 0xdeadbeef)
860 tests['a'] = proc {|x| run_test_add(x) }
861 tests['m'] = proc {|x| run_test_mul(x) }
862 tests['x'] = proc {|x| run_test_add_scale(x) }
863 tests['s'] = proc {|x| run_test_square(x) }
864 tests['r'] = proc {|x| run_test_reduce(x) }
865 tests['e'] = proc {|x| run_test_expmod(x) }
866 tests['g'] = proc {|x| run_test_gcdext(x) }
868 init_str['a'] = 'add-test'
869 init_str['x'] = 'add-scale-test'
870 init_str['m'] = 'mul-test'
871 init_str['s'] = 'square-test'
872 init_str['r'] = 'reduce-test'
873 init_str['e'] = 'expmod-test'
874 init_str['g'] = 'gcdext-test'
879 opts['a'].each_char do |x|
882 init_system(init_str[x])
883 tests[x].call(opts['s']?opts['s'].to_i():0)
885 puts "no test defiened for '#{x}'"
889 'amsre'.each_char do |x|
892 init_system(init_str[x])
893 tests[x].call(opts['s']?opts['s'].to_i():0)
895 puts "no test defiened for '#{x}'"