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)
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
150 puts("DBG got (#{__LINE__}): "+line) if $debug && line
151 line = "" if line==nil
152 if /^(Error:|Crypto-VS).*/.match(line)
156 if (Time.now.to_i- start_time) > $max_timeout
159 end while not m=prompt.match(line)
163 ################################################################################
165 ################################################################################
166 def screen_progress(v)
168 printf("\n%5d [%04d]: ", $testno, $size)
170 putc((v)?('*'):('!'))
172 $linepos = ($linepos+1)%$linewidth
175 ################################################################################
177 ################################################################################
182 line = "" if line==nil
183 puts("DBG got: "+line) if $debug
184 if /^Error:.*/.match(line)
188 end while not /[\s]*enter a:[\s]*/.match(line)
189 $sp.print(a.to_s(16)+" ")
192 line = "" if line==nil
193 puts("DBG got: "+line) if $debug
194 if /^Error:.*/.match(line)
198 end while not /[\s]*enter b:[\s]*/.match(line)
199 $sp.print(b.to_s(16)+" ")
202 line = "" if line==nil
203 puts("DBG got: "+line) if $debug
204 if /^Error:.*/.match(line)
208 end while not m=/[\s]*([-]?[0-9a-fA-F]*)[\s]+\+[\s]+([+-]?[0-9a-fA-F]*)[\s]*=[\s]*([+-]?[0-9a-fA-F]*)/.match(line)
213 if(a_== a && b_ == b && c_ == (a+b))
214 $logfile.printf("[pass]: %s\n", line)
217 $logfile.printf("[fail (%s%s%s)]: %s", (a==a_)?"":"a", (b==b_)?"":"b", (c_==a+b)?"":"c",line)
218 $logfile.printf(" ; should %s + %s = %s\n", a.to_s(16), b.to_s(16), (a+b).to_s(16))
224 ################################################################################
226 ################################################################################
231 line = "" if line==nil
232 puts("DBG got: "+line) if $debug
233 if /^Error:.*/.match(line)
237 end while not /[\s]*enter a:[\s]*/.match(line)
238 $sp.print(a.to_s(16)+" ")
241 line = "" if line==nil
242 puts("DBG got: "+line) if $debug
243 if /^Error:.*/.match(line)
247 end while not /[\s]*enter b:[\s]*/.match(line)
248 $sp.print(b.to_s(16)+" ")
251 line = "" if line==nil
252 puts("DBG got: "+line) if $debug
253 if /^Error:.*/.match(line)
257 end while not m=/[\s]*([+-]?[0-9a-fA-F]*)[\s]+\*[\s]+([+-]?[0-9a-fA-F]*)[\s]*=[\s]*([+-]?[0-9a-fA-F]*)/.match(line)
262 if(a_== a && b_ == b && c_ == (a*b))
263 $logfile.printf("[pass]: %s\n", line)
266 $logfile.printf("[fail (%s%s%s)]: %s", (a==a_)?"":"a", (b==b_)?"":"b", (c_==a*b)?"":"c",line)
267 $logfile.printf(" ; should %s * %s = %s\n", a.to_s(16), b.to_s(16), (a*b).to_s(16))
273 ################################################################################
275 ################################################################################
276 def add_scale_test_dummy(a, b, scale)
277 should = a + (b<<(8*scale))
278 printf("[dummy] %s + %s <<8*%04x = %s\n",a.to_s(16), b.to_s(16), scale, should.to_s(16))
281 def add_scale_test(a, b, scale)
282 m = wait_for_prompt("enter a:")
284 puts("DBG put (#{__LINE__}): "+a.to_s(16)+" ") if $debug
285 $sp.print(a.to_s(16)+" ")
286 m = wait_for_prompt("enter b:")
288 puts("DBG put (#{__LINE__}): "+b.to_s(16)+" ") if $debug
289 $sp.print(b.to_s(16)+" ")
290 m = wait_for_prompt("enter scale:")
292 puts("DBG put (#{__LINE__}): "+scale.to_s(10)+" ") if $debug
293 $sp.print(scale.to_s(10)+"\r")
294 should = a + (b<<(8*scale))
295 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]+)/)
297 $logfile.printf("[fail (CRASH)]:")
298 $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))
307 if(a_== a && b_ == b && s_ == scale && c_ == should )
308 $logfile.printf("[pass]: %s\n", line)
311 $logfile.printf("[fail (%s%s%s%s)]: %s", (a==a_)?"":"a", (b==b_)?"":"b", (scale==s_)?"":"s",(c_==should)?"":"c",line)
312 $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))
318 ################################################################################
320 ################################################################################
325 line = "" if line==nil
326 puts("DBG got: "+line) if $debug
327 if /^Error:.*/.match(line)
331 end while not /[\s]*enter a:[\s]*/.match(line)
332 $sp.print(a.to_s(16)+" ")
335 line = "" if line==nil
336 puts("DBG got: "+line) if $debug
337 if /^Error:.*/.match(line)
341 end while not m=/[\s]*([+-]?[0-9a-fA-F]*)[\s]*\*\*2[\s]*=[\s]*([+-]?[0-9a-fA-F]*)/.match(line)
345 if(a_== a && c_ == (a**2))
346 $logfile.printf("[pass]: %s\n", line)
349 $logfile.printf("[fail (%s%s)]: %s", (a==a_)?"":"a", (c_==a**2)?"":"c",line)
350 $logfile.printf(" ; should %s **2 = %s\n", a.to_s(16), (a**2).to_s(16))
356 ################################################################################
358 ################################################################################
363 line = "" if line==nil
364 puts("DBG got: "+line) if $debug
365 if /^Error:.*/.match(line)
369 end while not /[\s]*enter a:[\s]*/.match(line)
370 $sp.print(a.to_s(16)+" ")
373 line = "" if line==nil
374 puts("DBG got: "+line) if $debug
375 if /^Error:.*/.match(line)
379 end while not /[\s]*enter b:[\s]*/.match(line)
380 $sp.print(b.to_s(16)+" ")
383 line_tmp = $sp.gets()
384 line_tmp = '' if line_tmp==nil
386 puts("DBG got: "+line) if $debug
387 if /^Error:.*/.match(line)
391 end while not m=/[\s]*([+-]?[0-9a-fA-F]*)[\s]+%[\s]+([+-]?[0-9a-fA-F]*)[\s]*=[\s]*([+-]?[0-9a-fA-F]+)/.match(line)
396 if(a_== a && b_ == b && c_ == (a%b))
397 $logfile.printf("[pass]: %s\n", line)
400 $logfile.printf("[fail (%s%s%s)]: %s", (a==a_)?"":"a", (b==b_)?"":"b", (c_==a%b)?"":"c",line)
401 $logfile.printf(" ; should %s %% %s = %s\n", a.to_s(16), b.to_s(16), (a%b).to_s(16))
407 ################################################################################
409 ################################################################################
411 def expmod_test(a,b,c)
414 line = "" if line==nil
415 puts("DBG got: "+line) if $debug
416 if /^Error:.*/.match(line)
420 end while not /[\s]*enter a:[\s]*/.match(line)
421 $sp.print(a.to_s(16)+" ")
424 line = "" if line==nil
425 puts("DBG got: "+line) if $debug
426 if /^Error:.*/.match(line)
430 end while not /[\s]*enter b:[\s]*/.match(line)
431 $sp.print(b.to_s(16)+" ")
434 line = "" if line==nil
435 puts("DBG got: "+line) if $debug
436 if /^Error:.*/.match(line)
440 end while not /[\s]*enter c:[\s]*/.match(line)
441 $sp.print(c.to_s(16)+" ")
444 line_tmp = $sp.gets()
445 line_tmp = '' if line_tmp==nil
447 puts("DBG got: "+line) if $debug
448 if /^Error:.*/.match(line)
452 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)
458 if(a_== a && b_ == b && c_ == c && d_ ==expmod(a,b,c) )
459 $logfile.printf("[pass]: %s\n", line)
462 $logfile.printf("[fail (%s%s%s%s)]: %s", (a==a_)?'':'a', (b==b_)?'':'b', (c_==c)?'':'c', (d_==expmod(a,b,c))?'':'d',line)
463 $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))
469 ################################################################################
471 ################################################################################
476 line = "" if line==nil
477 puts("DBG got: "+line) if $debug
478 if /^Error:.*/.match(line)
482 end while not /[\s]*enter a:[\s]*/.match(line)
483 $sp.print(a.to_s(16)+" ")
486 line = "" if line==nil
487 puts("DBG got: "+line) if $debug
488 if /^Error:.*/.match(line)
492 end while not /[\s]*enter b:[\s]*/.match(line)
493 $sp.print(b.to_s(16)+" ")
496 line_tmp = $sp.gets()
497 line_tmp = '' if line_tmp==nil
498 line = '' if line.end_with?('\n')
500 puts("DBG got: "+line) if $debug
501 if /^Error:.*/.match(line)
505 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)
515 if(a_== a && b_ == b && c_ == ref[1] && d_ == ref[2] && e_== ref[0])
516 $logfile.printf("[pass]: %s\n", line)
519 $logfile.printf("[fail (%s%s%s%s%s)]: %s", (a==a_)?'':'a', (b==b_)?'':'b',
520 (c_==ref[1])?'':'c', (d_==ref[2])?'':'d', (e_==ref[0])?'':'e', line)
521 $logfile.printf(" ; should gcdext( %s, %s) => a = %s; b = %s; gcd = %s\n",
522 a.to_s(16), b.to_s(16), ref[1].to_s(16), ref[2].to_s(16), ref[0].to_s(16))
528 ################################################################################
530 ################################################################################
532 def run_test_add(skip=0)
540 a = rand(256**length_a_B) * s_a
541 b = rand(256**length_a_B) * s_b
550 b_size = rand(length_b_B+1)
551 a = rand(256**length_a_B) * s_a
552 b = rand(256**b_size) * s_b
561 end while length_a_B<4096/8
564 ################################################################################
565 # run_test_add_scale #
566 ################################################################################
568 def run_test_add_scale(skip=0)
575 16.times { scales << rand(301) }
577 scales.each do |scale|
578 a = rand(256**length_a_B)
579 b = rand(256**length_a_B)
580 v = add_scale_test(a, b, scale)
582 v = add_scale_test(b, a, scale)
588 16.times { scales << rand(301) }
590 scales.each do |scale|
591 b_size = rand(length_b_B+1)+1
592 a = rand(256**length_a_B)
593 b = rand(256**b_size)
594 v = add_scale_test(a, b, scale)
596 v = add_scale_test(b, a, scale)
602 end while length_a_B<4096/8
605 def run_test_add_scale_dummy(skip=0)
612 16.times { scales << rand(301) }
614 scales.each do |scale|
615 a = rand(256**length_a_B)
616 b = rand(256**length_a_B)
617 v = add_scale_test_dummy(a, b, scale)
618 v = add_scale_test_dummy(b, a, scale)
623 16.times { scales << rand(301) }
625 scales.each do |scale|
626 b_size = rand(length_b_B+1)
627 a = rand(256**length_a_B)
628 b = rand(256**b_size)
629 v = add_scale_test_dummy(a, b, scale)
630 v = add_scale_test_dummy(b, a, scale)
635 end while length_a_B<4096/8
638 ################################################################################
640 ################################################################################
642 def run_test_mul(skip=0)
650 a = rand(256**length_a_B) * s_a
651 b = rand(256**length_a_B) * s_b
660 b_size = rand(length_b_B+1)
661 a = rand(256**length_a_B) * s_a
662 b = rand(256**b_size) * s_b
670 end while length_a_B<4096/8
673 ################################################################################
675 ################################################################################
677 def run_test_square(skip=0)
682 a = rand(256**length_a_B)
687 end while length_a_B<4096/8
690 ################################################################################
692 ################################################################################
694 def run_test_reduce(skip=0)
700 a = rand(256**length_a_B)
701 b = rand(256**length_a_B)+1
702 v = reduce_test(a, b)
706 b_size = rand(length_b_B+1)
707 a = rand(256**length_a_B)
708 b = rand(256**b_size)+1
709 v = reduce_test(a, b)
714 end while length_a_B<4096/8
717 ################################################################################
719 ################################################################################
721 def run_test_expmod(skip=0)
728 a = rand(256**length_a_B)
729 b = rand(256**length_b_B)+1
730 c = rand(256**length_c_B)+1
731 v = expmod_test(a, b, c)
735 b_size = rand(length_b_B+1)
736 a = rand(256**length_a_B)
737 b = rand(256**b_size)+1
738 c = rand(256**b_size)+1
739 v = expmod_test(a, b, c)
744 end while length_a_B<4096/8
747 ################################################################################
749 ################################################################################
751 def run_test_gcdext(skip=0)
757 a = rand(256**length_a_B)
758 b = rand(256**length_a_B)+1
759 v = gcdext_test(a, b)
764 b_size = rand(length_b_B+1)
765 a = rand(256**length_a_B)
766 b = rand(256**b_size)+1
767 v = gcdext_test(a, b)
773 end while length_a_B<4096/8
776 def init_serialport(conf)
777 puts("serial port interface version: " + SerialPort::VERSION);
781 params = { "baud" => conf["PORT"]["baud"].to_i,
782 "data_bits" => conf["PORT"]["databits"].to_i,
783 "stop_bits" => conf["PORT"]["stopbits"].to_i,
784 "parity" => SerialPort::NONE }
785 params["paraty"] = SerialPort::ODD if conf["PORT"]["paraty"].downcase == "odd"
786 params["paraty"] = SerialPort::EVEN if conf["PORT"]["paraty"].downcase == "even"
787 params["paraty"] = SerialPort::MARK if conf["PORT"]["paraty"].downcase == "mark"
788 params["paraty"] = SerialPort::SPACE if conf["PORT"]["paraty"].downcase == "space"
790 puts("\nPort: "+conf["PORT"]["port"]+"@" +
791 params["baud"].to_s +
793 params["data_bits"].to_s +
794 conf["PORT"]["paraty"][0,1].upcase +
795 params["stop_bits"].to_s +
798 $sp = SerialPort.new(conf["PORT"]["port"], params)
800 $sp.read_timeout=1000; # 5 minutes
801 $sp.flow_control = SerialPort::SOFT
806 ################################################################################
808 ################################################################################
810 opts = Getopt::Std.getopts("s:f:i:a:hd")
813 conf = readconfigfile("/etc/testport.conf", conf)
814 conf = readconfigfile("~/.testport.conf", conf)
815 conf = readconfigfile("testport.conf", conf)
816 conf = readconfigfile(opts["f"], conf) if opts["f"]
819 init_serialport(conf)
821 $max_timeout = 2 * 60
827 logfilename = conf['PORT']['testlogbase']+'bigint.txt'
828 if File.exists?(logfilename)
831 logfilename = sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i,'.txt')
833 end while(File.exists?(logfilename))
835 File.move(sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i-2,'.txt'),
836 sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i-1,'.txt'), true)
839 File.move(sprintf('%s%s', conf['PORT']['testlogbase'],'bigint.txt'),
840 sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',1,'.txt'), true)
841 logfilename = conf['PORT']['testlogbase']+'bigint.txt'
843 $logfile = File.open(logfilename, 'w')
844 printf("logfile: %s\n", logfilename)
846 $logfile.printf("bigint test from: %s\n", Time.now.to_s)
847 $logfile.printf("skip = %s\n", opts['s']) if opts['s']
848 $logfile.printf("seed = 0x%X\n", 0xdeadbeef)
851 tests['a'] = proc {|x| run_test_add(x) }
852 tests['m'] = proc {|x| run_test_mul(x) }
853 tests['x'] = proc {|x| run_test_add_scale(x) }
854 tests['s'] = proc {|x| run_test_square(x) }
855 tests['r'] = proc {|x| run_test_reduce(x) }
856 tests['e'] = proc {|x| run_test_expmod(x) }
857 tests['g'] = proc {|x| run_test_gcdext(x) }
859 init_str['a'] = 'add-test'
860 init_str['x'] = 'add-scale-test'
861 init_str['m'] = 'mul-test'
862 init_str['s'] = 'square-test'
863 init_str['r'] = 'reduce-test'
864 init_str['e'] = 'expmod-test'
865 init_str['g'] = 'gcdext-test'
870 opts['a'].each_char do |x|
873 init_system(init_str[x])
874 tests[x].call(opts['s']?opts['s'].to_i():0)
876 puts "no test defiened for '#{x}'"
880 'amsre'.each_char do |x|
883 init_system(init_str[x])
884 tests[x].call(opts['s']?opts['s'].to_i():0)
886 puts "no test defiened for '#{x}'"