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)
415 printf("[testing] expmod(%#x, %#x, %#x)\n",a,b,c) if $debug
417 line = "" if line==nil
418 puts("DBG got: "+line) if $debug
419 if /^Error:.*/.match(line)
423 end while ! /[\s]*enter a:[\s]*/.match(line)
424 $sp.print(a.to_s(16)+" ")
427 line = "" if line==nil
428 puts("DBG got: "+line) if $debug
429 if /^Error:.*/.match(line)
433 end while ! /[\s]*enter b:[\s]*/.match(line)
434 $sp.print(b.to_s(16)+" ")
437 line = "" if line==nil
438 puts("DBG got: "+line) if $debug
439 if /^Error:.*/.match(line)
443 end while ! /[\s]*enter c:[\s]*/.match(line)
444 $sp.print(c.to_s(16)+" ")
447 line_tmp = $sp.gets()
448 line_tmp = '' if line_tmp==nil
450 puts("DBG got: "+line) if $debug
451 if /^Error:.*/.match(line)
455 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)
461 if(a_== a && b_ == b && c_ == c && d_ ==expmod(a,b,c) )
462 $logfile.printf("[pass]: %s\n", line)
465 $logfile.printf("[fail (%s%s%s%s)]: %s", (a==a_)?'':'a', (b==b_)?'':'b', (c_==c)?'':'c', (d_==expmod(a,b,c))?'':'d',line)
466 $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))
472 ################################################################################
474 ################################################################################
479 line = "" if line==nil
480 puts("DBG got: "+line) if $debug
481 if /^Error:.*/.match(line)
485 end while ! /[\s]*enter a:[\s]*/.match(line)
486 $sp.print(a.to_s(16)+" ")
489 line = "" if line==nil
490 puts("DBG got: "+line) if $debug
491 if /^Error:.*/.match(line)
495 end while ! /[\s]*enter b:[\s]*/.match(line)
496 $sp.print(b.to_s(16)+" ")
499 line_tmp = $sp.gets()
500 line_tmp = '' if line_tmp==nil
501 line = '' if line.end_with?('\n')
503 puts("DBG got: "+line) if $debug
504 if /^Error:.*/.match(line)
508 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)
518 if(a_== a && b_ == b && c_ == ref[1] && d_ == ref[2] && e_== ref[0])
519 $logfile.printf("[pass]: %s\n", line)
522 $logfile.printf("[fail (%s%s%s%s%s)]: %s", (a==a_)?'':'a', (b==b_)?'':'b',
523 (c_==ref[1])?'':'c', (d_==ref[2])?'':'d', (e_==ref[0])?'':'e', line)
524 $logfile.printf(" ; should gcdext( %s, %s) => a = %s; b = %s; gcd = %s\n",
525 a.to_s(16), b.to_s(16), ref[1].to_s(16), ref[2].to_s(16), ref[0].to_s(16))
531 ################################################################################
533 ################################################################################
535 def run_test_add(skip=0)
543 a = rand(256**length_a_B) * s_a
544 b = rand(256**length_a_B) * s_b
553 b_size = rand(length_b_B+1)
554 a = rand(256**length_a_B) * s_a
555 b = rand(256**b_size) * s_b
564 end while length_a_B<4096/8
567 ################################################################################
568 # run_test_add_scale #
569 ################################################################################
571 def run_test_add_scale(skip=0)
578 16.times { scales << rand(301) }
580 scales.each do |scale|
581 a = rand(256**length_a_B)
582 b = rand(256**length_a_B)
583 v = add_scale_test(a, b, scale)
585 v = add_scale_test(b, a, scale)
591 16.times { scales << rand(301) }
593 scales.each do |scale|
594 b_size = rand(length_b_B+1)+1
595 a = rand(256**length_a_B)
596 b = rand(256**b_size)
597 v = add_scale_test(a, b, scale)
599 v = add_scale_test(b, a, scale)
605 end while length_a_B<4096/8
608 def run_test_add_scale_dummy(skip=0)
615 16.times { scales << rand(301) }
617 scales.each do |scale|
618 a = rand(256**length_a_B)
619 b = rand(256**length_a_B)
620 v = add_scale_test_dummy(a, b, scale)
621 v = add_scale_test_dummy(b, a, scale)
626 16.times { scales << rand(301) }
628 scales.each do |scale|
629 b_size = rand(length_b_B+1)
630 a = rand(256**length_a_B)
631 b = rand(256**b_size)
632 v = add_scale_test_dummy(a, b, scale)
633 v = add_scale_test_dummy(b, a, scale)
638 end while length_a_B<4096/8
641 ################################################################################
643 ################################################################################
645 def run_test_mul(skip=0)
653 a = rand(256**length_a_B) * s_a
654 b = rand(256**length_a_B) * s_b
663 b_size = rand(length_b_B+1)
664 a = rand(256**length_a_B) * s_a
665 b = rand(256**b_size) * s_b
673 end while length_a_B<4096/8
676 ################################################################################
678 ################################################################################
680 def run_test_square(skip=0)
685 a = rand(256**length_a_B)
690 end while length_a_B<4096/8
693 ################################################################################
695 ################################################################################
697 def run_test_reduce(skip=0)
703 a = rand(256**length_a_B)
704 b = rand(256**length_a_B)+1
705 v = reduce_test(a, b)
709 b_size = rand(length_b_B+1)
710 a = rand(256**length_a_B)
711 b = rand(256**b_size)+1
712 v = reduce_test(a, b)
717 end while length_a_B<4096/8
720 ################################################################################
722 ################################################################################
724 def run_test_expmod(skip=0)
731 a = rand(256**length_a_B)
732 b = rand(256**length_b_B)+1
733 c = rand(256**length_c_B)+1
734 v = expmod_test(a, b, c)
738 b_size = rand(length_b_B+1)
739 a = rand(256**length_a_B)
740 b = rand(256**b_size)+1
741 c = rand(256**b_size)+1
742 v = expmod_test(a, b, c)
747 end while length_a_B<4096/8
750 ################################################################################
752 ################################################################################
754 def run_test_gcdext(skip=0)
760 a = rand(256**length_a_B)
761 b = rand(256**length_a_B)+1
762 v = gcdext_test(a, b)
767 b_size = rand(length_b_B+1)
768 a = rand(256**length_a_B)
769 b = rand(256**b_size)+1
770 v = gcdext_test(a, b)
776 end while length_a_B<4096/8
779 def init_serialport(conf)
780 puts("serial port interface version: " + SerialPort::VERSION);
784 params = { "baud" => conf["PORT"]["baud"].to_i,
785 "data_bits" => conf["PORT"]["databits"].to_i,
786 "stop_bits" => conf["PORT"]["stopbits"].to_i,
787 "parity" => SerialPort::NONE }
788 params["paraty"] = SerialPort::ODD if conf["PORT"]["paraty"].downcase == "odd"
789 params["paraty"] = SerialPort::EVEN if conf["PORT"]["paraty"].downcase == "even"
790 params["paraty"] = SerialPort::MARK if conf["PORT"]["paraty"].downcase == "mark"
791 params["paraty"] = SerialPort::SPACE if conf["PORT"]["paraty"].downcase == "space"
793 puts("\nPort: "+conf["PORT"]["port"]+"@" +
794 params["baud"].to_s +
796 params["data_bits"].to_s +
797 conf["PORT"]["paraty"][0,1].upcase +
798 params["stop_bits"].to_s +
801 $sp = SerialPort.new(conf["PORT"]["port"], params)
803 $sp.read_timeout=1000; # 5 minutes
804 $sp.flow_control = SerialPort::SOFT
809 ################################################################################
811 ################################################################################
813 opts = Getopt::Std.getopts("s:f:i:a:hd")
816 conf = readconfigfile("/etc/testport.conf", conf)
817 conf = readconfigfile("~/.testport.conf", conf)
818 conf = readconfigfile("testport.conf", conf)
819 conf = readconfigfile(opts["f"], conf) if opts["f"]
822 init_serialport(conf)
824 $max_timeout = 5 * 60
830 logfilename = conf['PORT']['testlogbase']+'bigint.txt'
831 if File.exists?(logfilename)
834 logfilename = sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i,'.txt')
836 end while(File.exists?(logfilename))
838 n1 = sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i-2,'.txt')
839 n2 = sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i-1,'.txt')
841 printf("%s -> %s\n", n1, n2)
844 n1 = sprintf('%s%s', conf['PORT']['testlogbase'],'bigint.txt')
845 n2 = sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',1,'.txt')
847 printf("%s -> %s\n", n1, n2)
848 logfilename = conf['PORT']['testlogbase']+'bigint.txt'
850 $logfile = File.open(logfilename, 'w')
851 printf("logfile: %s\n", logfilename)
853 $logfile.printf("bigint test from: %s\n", Time.now.to_s)
854 $logfile.printf("skip = %s\n", opts['s']) if opts['s']
855 $logfile.printf("seed = 0x%X\n", 0xdeadbeef)
858 tests['a'] = proc {|x| run_test_add(x) }
859 tests['m'] = proc {|x| run_test_mul(x) }
860 tests['x'] = proc {|x| run_test_add_scale(x) }
861 tests['s'] = proc {|x| run_test_square(x) }
862 tests['r'] = proc {|x| run_test_reduce(x) }
863 tests['e'] = proc {|x| run_test_expmod(x) }
864 tests['g'] = proc {|x| run_test_gcdext(x) }
866 init_str['a'] = 'add-test'
867 init_str['x'] = 'add-scale-test'
868 init_str['m'] = 'mul-test'
869 init_str['s'] = 'square-test'
870 init_str['r'] = 'reduce-test'
871 init_str['e'] = 'expmod-test'
872 init_str['g'] = 'gcdext-test'
877 opts['a'].each_char do |x|
880 init_system(init_str[x])
881 tests[x].call(opts['s']?opts['s'].to_i():0)
883 puts "no test defiened for '#{x}'"
887 'amsre'.each_char do |x|
890 init_system(init_str[x])
891 tests[x].call(opts['s']?opts['s'].to_i():0)
893 puts "no test defiened for '#{x}'"