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 ################################################################################
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)
133 $sp.print("echo off \r")
134 print("DBG i: " + "echo off \r"+"\n") if $debug
136 $sp.print("#{test_prog}\r")
137 print("DBG i: " + "#{test_prog} \r"+"\n") if $debug
141 ################################################################################
143 ################################################################################
145 def wait_for_prompt(prompt)
146 prompt = /[\s]*#{prompt}[\s]*/ if(prompt.class == String)
147 start_time = Time.now.to_i
151 puts("DBG got (#{__LINE__}): "+line) if $debug && line
152 line = "" if line==nil
153 if /^(Error:|Crypto-VS).*/.match(line)
157 if (Time.now.to_i- start_time) > $max_timeout
161 end while ! m=prompt.match(acc)
165 ################################################################################
167 ################################################################################
168 def screen_progress(v)
170 printf("\n%5d [%04d]: ", $testno, $size)
172 putc((v)?('*'):('!'))
174 $linepos = ($linepos+1)%$linewidth
177 ################################################################################
179 ################################################################################
184 line = "" if line==nil
185 puts("DBG got: "+line) if $debug
186 if /^Error:.*/.match(line)
190 end while ! /[\s]*enter a:[\s]*/.match(line)
191 $sp.print(a.to_s(16)+" ")
194 line = "" if line==nil
195 puts("DBG got: "+line) if $debug
196 if /^Error:.*/.match(line)
200 end while ! /[\s]*enter b:[\s]*/.match(line)
201 $sp.print(b.to_s(16)+" ")
204 line = "" if line==nil
205 puts("DBG got: "+line) if $debug
206 if /^Error:.*/.match(line)
210 end while ! m=/[\s]*([-]?[0-9a-fA-F]*)[\s]+\+[\s]+([+-]?[0-9a-fA-F]*)[\s]*=[\s]*([+-]?[0-9a-fA-F]*)/.match(line)
215 if(a_== a && b_ == b && c_ == (a+b))
216 $logfile.printf("[pass]: %s\n", line)
219 $logfile.printf("[fail (%s%s%s)]: %s", (a==a_)?"":"a", (b==b_)?"":"b", (c_==a+b)?"":"c",line)
220 $logfile.printf(" ; should %s + %s = %s\n", a.to_s(16), b.to_s(16), (a+b).to_s(16))
226 ################################################################################
228 ################################################################################
233 line = "" if line==nil
234 puts("DBG got: "+line) if $debug
235 if /^Error:.*/.match(line)
239 end while ! /[\s]*enter a:[\s]*/.match(line)
240 $sp.print(a.to_s(16)+" ")
243 line = "" if line==nil
244 puts("DBG got: "+line) if $debug
245 if /^Error:.*/.match(line)
249 end while ! /[\s]*enter b:[\s]*/.match(line)
250 $sp.print(b.to_s(16)+" ")
253 line = "" if line==nil
254 puts("DBG got: "+line) if $debug
255 if /^Error:.*/.match(line)
259 end while ! m=/[\s]*([+-]?[0-9a-fA-F]*)[\s]+\*[\s]+([+-]?[0-9a-fA-F]*)[\s]*=[\s]*([+-]?[0-9a-fA-F]*)/.match(line)
264 if(a_== a && b_ == b && c_ == (a*b))
265 $logfile.printf("[pass]: %s\n", line)
268 $logfile.printf("[fail (%s%s%s)]: %s", (a==a_)?"":"a", (b==b_)?"":"b", (c_==a*b)?"":"c",line)
269 $logfile.printf(" ; should %s * %s = %s\n", a.to_s(16), b.to_s(16), (a*b).to_s(16))
275 ################################################################################
277 ################################################################################
278 def add_scale_test_dummy(a, b, scale)
279 should = a + (b<<(8*scale))
280 printf("[dummy] %s + %s <<8*%04x = %s\n",a.to_s(16), b.to_s(16), scale, should.to_s(16))
283 def add_scale_test(a, b, scale)
284 m = wait_for_prompt("enter a:")
286 puts("DBG put (#{__LINE__}): "+a.to_s(16)+" ") if $debug
287 $sp.print(a.to_s(16)+" ")
288 m = wait_for_prompt("enter b:")
290 puts("DBG put (#{__LINE__}): "+b.to_s(16)+" ") if $debug
291 $sp.print(b.to_s(16)+" ")
292 m = wait_for_prompt("enter scale:")
294 puts("DBG put (#{__LINE__}): "+scale.to_s(10)+" ") if $debug
295 $sp.print(scale.to_s(10)+"\r")
296 should = a + (b<<(8*scale))
297 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]+)/)
299 $logfile.printf("[fail (CRASH)]:")
300 $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))
309 if(a_== a && b_ == b && s_ == scale && c_ == should )
310 $logfile.printf("[pass]: %s\n", line)
313 $logfile.printf("[fail (%s%s%s%s)]: %s", (a==a_)?"":"a", (b==b_)?"":"b", (scale==s_)?"":"s",(c_==should)?"":"c",line)
314 $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))
320 ################################################################################
322 ################################################################################
327 line = "" if line==nil
328 puts("DBG got: "+line) if $debug
329 if /^Error:.*/.match(line)
333 end while ! /[\s]*enter a:[\s]*/.match(line)
334 $sp.print(a.to_s(16)+" ")
337 line = "" if line==nil
338 puts("DBG got: "+line) if $debug
339 if /^Error:.*/.match(line)
343 end while ! m=/[\s]*([+-]?[0-9a-fA-F]*)[\s]*\*\*2[\s]*=[\s]*([+-]?[0-9a-fA-F]*)/.match(line)
347 if(a_== a && c_ == (a**2))
348 $logfile.printf("[pass]: %s\n", line)
351 $logfile.printf("[fail (%s%s)]: %s", (a==a_)?"":"a", (c_==a**2)?"":"c",line)
352 $logfile.printf(" ; should %s **2 = %s\n", a.to_s(16), (a**2).to_s(16))
358 ################################################################################
360 ################################################################################
365 line = "" if line==nil
366 puts("DBG got: "+line) if $debug
367 if /^Error:.*/.match(line)
371 end while ! /[\s]*enter a:[\s]*/.match(line)
372 $sp.print(a.to_s(16)+" ")
375 line = "" if line==nil
376 puts("DBG got: "+line) if $debug
377 if /^Error:.*/.match(line)
381 end while ! /[\s]*enter b:[\s]*/.match(line)
382 $sp.print(b.to_s(16)+" ")
385 line_tmp = $sp.gets()
386 line_tmp = '' if line_tmp==nil
388 puts("DBG got: "+line) if $debug
389 if /^Error:.*/.match(line)
393 end while ! m=/[\s]*([+-]?[0-9a-fA-F]*)[\s]+%[\s]+([+-]?[0-9a-fA-F]*)[\s]*=[\s]*([+-]?[0-9a-fA-F]+)/.match(line)
398 if(a_== a && b_ == b && c_ == (a%b))
399 $logfile.printf("[pass]: %s\n", line)
402 $logfile.printf("[fail (%s%s%s)]: %s", (a==a_)?"":"a", (b==b_)?"":"b", (c_==a%b)?"":"c",line)
403 $logfile.printf(" ; should %s %% %s = %s\n", a.to_s(16), b.to_s(16), (a%b).to_s(16))
409 ################################################################################
411 ################################################################################
413 def expmod_test(a,b,c)
416 line = "" if line==nil
417 puts("DBG got: "+line) if $debug
418 if /^Error:.*/.match(line)
422 end while ! /[\s]*enter a:[\s]*/.match(line)
423 $sp.print(a.to_s(16)+" ")
426 line = "" if line==nil
427 puts("DBG got: "+line) if $debug
428 if /^Error:.*/.match(line)
432 end while ! /[\s]*enter b:[\s]*/.match(line)
433 $sp.print(b.to_s(16)+" ")
436 line = "" if line==nil
437 puts("DBG got: "+line) if $debug
438 if /^Error:.*/.match(line)
442 end while ! /[\s]*enter c:[\s]*/.match(line)
443 $sp.print(c.to_s(16)+" ")
446 line_tmp = $sp.gets()
447 line_tmp = '' if line_tmp==nil
449 puts("DBG got: "+line) if $debug
450 if /^Error:.*/.match(line)
454 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)
460 if(a_== a && b_ == b && c_ == c && d_ ==expmod(a,b,c) )
461 $logfile.printf("[pass]: %s\n", line)
464 $logfile.printf("[fail (%s%s%s%s)]: %s", (a==a_)?'':'a', (b==b_)?'':'b', (c_==c)?'':'c', (d_==expmod(a,b,c))?'':'d',line)
465 $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))
471 ################################################################################
473 ################################################################################
478 line = "" if line==nil
479 puts("DBG got: "+line) if $debug
480 if /^Error:.*/.match(line)
484 end while ! /[\s]*enter a:[\s]*/.match(line)
485 $sp.print(a.to_s(16)+" ")
488 line = "" if line==nil
489 puts("DBG got: "+line) if $debug
490 if /^Error:.*/.match(line)
494 end while ! /[\s]*enter b:[\s]*/.match(line)
495 $sp.print(b.to_s(16)+" ")
498 line_tmp = $sp.gets()
499 line_tmp = '' if line_tmp==nil
500 line = '' if line.end_with?('\n')
502 puts("DBG got: "+line) if $debug
503 if /^Error:.*/.match(line)
507 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)
517 if(a_== a && b_ == b && c_ == ref[1] && d_ == ref[2] && e_== ref[0])
518 $logfile.printf("[pass]: %s\n", line)
521 $logfile.printf("[fail (%s%s%s%s%s)]: %s", (a==a_)?'':'a', (b==b_)?'':'b',
522 (c_==ref[1])?'':'c', (d_==ref[2])?'':'d', (e_==ref[0])?'':'e', line)
523 $logfile.printf(" ; should gcdext( %s, %s) => a = %s; b = %s; gcd = %s\n",
524 a.to_s(16), b.to_s(16), ref[1].to_s(16), ref[2].to_s(16), ref[0].to_s(16))
530 ################################################################################
532 ################################################################################
534 def run_test_add(skip=0)
542 a = rand(256**length_a_B) * s_a
543 b = rand(256**length_a_B) * s_b
552 b_size = rand(length_b_B+1)
553 a = rand(256**length_a_B) * s_a
554 b = rand(256**b_size) * s_b
563 end while length_a_B<4096/8
566 ################################################################################
567 # run_test_add_scale #
568 ################################################################################
570 def run_test_add_scale(skip=0)
577 16.times { scales << rand(301) }
579 scales.each do |scale|
580 a = rand(256**length_a_B)
581 b = rand(256**length_a_B)
582 v = add_scale_test(a, b, scale)
584 v = add_scale_test(b, a, scale)
590 16.times { scales << rand(301) }
592 scales.each do |scale|
593 b_size = rand(length_b_B+1)+1
594 a = rand(256**length_a_B)
595 b = rand(256**b_size)
596 v = add_scale_test(a, b, scale)
598 v = add_scale_test(b, a, scale)
604 end while length_a_B<4096/8
607 def run_test_add_scale_dummy(skip=0)
614 16.times { scales << rand(301) }
616 scales.each do |scale|
617 a = rand(256**length_a_B)
618 b = rand(256**length_a_B)
619 v = add_scale_test_dummy(a, b, scale)
620 v = add_scale_test_dummy(b, a, scale)
625 16.times { scales << rand(301) }
627 scales.each do |scale|
628 b_size = rand(length_b_B+1)
629 a = rand(256**length_a_B)
630 b = rand(256**b_size)
631 v = add_scale_test_dummy(a, b, scale)
632 v = add_scale_test_dummy(b, a, scale)
637 end while length_a_B<4096/8
640 ################################################################################
642 ################################################################################
644 def run_test_mul(skip=0)
652 a = rand(256**length_a_B) * s_a
653 b = rand(256**length_a_B) * s_b
662 b_size = rand(length_b_B+1)
663 a = rand(256**length_a_B) * s_a
664 b = rand(256**b_size) * s_b
672 end while length_a_B<4096/8
675 ################################################################################
677 ################################################################################
679 def run_test_square(skip=0)
684 a = rand(256**length_a_B)
689 end while length_a_B<4096/8
692 ################################################################################
694 ################################################################################
696 def run_test_reduce(skip=0)
702 a = rand(256**length_a_B)
703 b = rand(256**length_a_B)+1
704 v = reduce_test(a, b)
708 b_size = rand(length_b_B+1)
709 a = rand(256**length_a_B)
710 b = rand(256**b_size)+1
711 v = reduce_test(a, b)
716 end while length_a_B<4096/8
719 ################################################################################
721 ################################################################################
723 def run_test_expmod(skip=0)
730 a = rand(256**length_a_B)
731 b = rand(256**length_b_B)+1
732 c = rand(256**length_c_B)+1
733 v = expmod_test(a, b, c)
737 b_size = rand(length_b_B+1)
738 a = rand(256**length_a_B)
739 b = rand(256**b_size)+1
740 c = rand(256**b_size)+1
741 v = expmod_test(a, b, c)
746 end while length_a_B<4096/8
749 ################################################################################
751 ################################################################################
753 def run_test_gcdext(skip=0)
759 a = rand(256**length_a_B)
760 b = rand(256**length_a_B)+1
761 v = gcdext_test(a, b)
766 b_size = rand(length_b_B+1)
767 a = rand(256**length_a_B)
768 b = rand(256**b_size)+1
769 v = gcdext_test(a, b)
775 end while length_a_B<4096/8
778 def init_serialport(conf)
779 puts("serial port interface version: " + SerialPort::VERSION);
783 params = { "baud" => conf["PORT"]["baud"].to_i,
784 "data_bits" => conf["PORT"]["databits"].to_i,
785 "stop_bits" => conf["PORT"]["stopbits"].to_i,
786 "parity" => SerialPort::NONE }
787 params["paraty"] = SerialPort::ODD if conf["PORT"]["paraty"].downcase == "odd"
788 params["paraty"] = SerialPort::EVEN if conf["PORT"]["paraty"].downcase == "even"
789 params["paraty"] = SerialPort::MARK if conf["PORT"]["paraty"].downcase == "mark"
790 params["paraty"] = SerialPort::SPACE if conf["PORT"]["paraty"].downcase == "space"
792 puts("\nPort: "+conf["PORT"]["port"]+"@" +
793 params["baud"].to_s +
795 params["data_bits"].to_s +
796 conf["PORT"]["paraty"][0,1].upcase +
797 params["stop_bits"].to_s +
800 $sp = SerialPort.new(conf["PORT"]["port"], params)
802 $sp.read_timeout=1000; # 5 minutes
803 $sp.flow_control = SerialPort::SOFT
808 ################################################################################
810 ################################################################################
812 opts = Getopt::Std.getopts("s:f:i:a:hd")
815 conf = readconfigfile("/etc/testport.conf", conf)
816 conf = readconfigfile("~/.testport.conf", conf)
817 conf = readconfigfile("testport.conf", conf)
818 conf = readconfigfile(opts["f"], conf) if opts["f"]
821 init_serialport(conf)
823 $max_timeout = 5 * 60
829 logfilename = conf['PORT']['testlogbase']+'bigint.txt'
830 if File.exists?(logfilename)
833 logfilename = sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i,'.txt')
835 end while(File.exists?(logfilename))
837 n1 = sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i-2,'.txt')
838 n2 = sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i-1,'.txt')
840 printf("%s -> %s\n", n1, n2)
843 n1 = sprintf('%s%s', conf['PORT']['testlogbase'],'bigint.txt')
844 n2 = sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',1,'.txt')
846 printf("%s -> %s\n", n1, n2)
847 logfilename = conf['PORT']['testlogbase']+'bigint.txt'
849 $logfile = File.open(logfilename, 'w')
850 printf("logfile: %s\n", logfilename)
852 $logfile.printf("bigint test from: %s\n", Time.now.to_s)
853 $logfile.printf("skip = %s\n", opts['s']) if opts['s']
854 $logfile.printf("seed = 0x%X\n", 0xdeadbeef)
857 tests['a'] = proc {|x| run_test_add(x) }
858 tests['m'] = proc {|x| run_test_mul(x) }
859 tests['x'] = proc {|x| run_test_add_scale(x) }
860 tests['s'] = proc {|x| run_test_square(x) }
861 tests['r'] = proc {|x| run_test_reduce(x) }
862 tests['e'] = proc {|x| run_test_expmod(x) }
863 tests['g'] = proc {|x| run_test_gcdext(x) }
865 init_str['a'] = 'add-test'
866 init_str['x'] = 'add-scale-test'
867 init_str['m'] = 'mul-test'
868 init_str['s'] = 'square-test'
869 init_str['r'] = 'reduce-test'
870 init_str['e'] = 'expmod-test'
871 init_str['g'] = 'gcdext-test'
876 opts['a'].each_char do |x|
879 init_system(init_str[x])
880 tests[x].call(opts['s']?opts['s'].to_i():0)
882 puts "no test defiened for '#{x}'"
886 'amsre'.each_char do |x|
889 init_system(init_str[x])
890 tests[x].call(opts['s']?opts['s'].to_i():0)
892 puts "no test defiened for '#{x}'"