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 ################################################################################
253 def add_scale_test(a, b, scale)
256 line = "" if line==nil
257 puts("DBG got (#{__LINE__}): "+line) if $debug
258 if /^Error:.*/.match(line)
262 end while not /[\s]*enter a:[\s]*/.match(line)
263 puts("DBG put (#{__LINE__}): "+a.to_s(16)+" ") if $debug
264 $sp.print(a.to_s(16)+" ")
267 line = "" if line==nil
268 puts("DBG got (#{__LINE__}): "+line) if $debug
269 if /^Error:.*/.match(line)
273 end while not /[\s]*enter b:[\s]*/.match(line)
274 $sp.print(b.to_s(16)+" ")
277 line = "" if line==nil
278 puts("DBG got (#{__LINE__}): "+line) if $debug
279 if /^Error:.*/.match(line)
283 end while not /[\s]*enter scale:[\s]*/.match(line)
284 $sp.print(scale.to_s(10)+"\r")
287 line = "" if line==nil
288 puts("DBG got (#{__LINE__}): "+line) if $debug
289 if /^Error:.*/.match(line)
293 end while not m=/[\s]*([-]?[0-9a-fA-F]*)[\s]+\+[\s]+([+-]?[0-9a-fA-F]*)[\s]*<<8\*[\s]*([+-]?[0-9a-fA-F]*)[\s]*=[\s]*([+-]?[0-9a-fA-F]*)/.match(line)
299 should = a + (b<<(8*scale))
300 if(a_== a && b_ == b && s_ == scale && c_ == should )
301 $logfile.printf("[pass]: %s\n", line)
304 $logfile.printf("[fail (%s%s%s)]: %s", (a==a_)?"":"a", (b==b_)?"":"b", (scale==s_)?"":"s",(c_==should)?"":"c",line)
305 $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))
311 ################################################################################
313 ################################################################################
318 line = "" if line==nil
319 puts("DBG got: "+line) if $debug
320 if /^Error:.*/.match(line)
324 end while not /[\s]*enter a:[\s]*/.match(line)
325 $sp.print(a.to_s(16)+" ")
328 line = "" if line==nil
329 puts("DBG got: "+line) if $debug
330 if /^Error:.*/.match(line)
334 end while not m=/[\s]*([+-]?[0-9a-fA-F]*)[\s]*\*\*2[\s]*=[\s]*([+-]?[0-9a-fA-F]*)/.match(line)
338 if(a_== a && c_ == (a**2))
339 $logfile.printf("[pass]: %s\n", line)
342 $logfile.printf("[fail (%s%s)]: %s", (a==a_)?"":"a", (c_==a**2)?"":"c",line)
343 $logfile.printf(" ; should %s **2 = %s\n", a.to_s(16), (a**2).to_s(16))
349 ################################################################################
351 ################################################################################
356 line = "" if line==nil
357 puts("DBG got: "+line) if $debug
358 if /^Error:.*/.match(line)
362 end while not /[\s]*enter a:[\s]*/.match(line)
363 $sp.print(a.to_s(16)+" ")
366 line = "" if line==nil
367 puts("DBG got: "+line) if $debug
368 if /^Error:.*/.match(line)
372 end while not /[\s]*enter b:[\s]*/.match(line)
373 $sp.print(b.to_s(16)+" ")
376 line_tmp = $sp.gets()
377 line_tmp = '' if line_tmp==nil
379 puts("DBG got: "+line) if $debug
380 if /^Error:.*/.match(line)
384 end while not m=/[\s]*([+-]?[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_ == (a%b))
390 $logfile.printf("[pass]: %s\n", line)
393 $logfile.printf("[fail (%s%s%s)]: %s", (a==a_)?"":"a", (b==b_)?"":"b", (c_==a%b)?"":"c",line)
394 $logfile.printf(" ; should %s %% %s = %s\n", a.to_s(16), b.to_s(16), (a%b).to_s(16))
400 ################################################################################
402 ################################################################################
404 def expmod_test(a,b,c)
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 = "" if line==nil
428 puts("DBG got: "+line) if $debug
429 if /^Error:.*/.match(line)
433 end while not /[\s]*enter c:[\s]*/.match(line)
434 $sp.print(c.to_s(16)+" ")
437 line_tmp = $sp.gets()
438 line_tmp = '' if line_tmp==nil
440 puts("DBG got: "+line) if $debug
441 if /^Error:.*/.match(line)
445 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)
451 if(a_== a && b_ == b && c_ == c && d_ ==expmod(a,b,c) )
452 $logfile.printf("[pass]: %s\n", line)
455 $logfile.printf("[fail (%s%s%s%s)]: %s", (a==a_)?'':'a', (b==b_)?'':'b', (c_==c)?'':'c', (d_==expmod(a,b,c))?'':'d',line)
456 $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))
462 ################################################################################
464 ################################################################################
469 line = "" if line==nil
470 puts("DBG got: "+line) if $debug
471 if /^Error:.*/.match(line)
475 end while not /[\s]*enter a:[\s]*/.match(line)
476 $sp.print(a.to_s(16)+" ")
479 line = "" if line==nil
480 puts("DBG got: "+line) if $debug
481 if /^Error:.*/.match(line)
485 end while not /[\s]*enter b:[\s]*/.match(line)
486 $sp.print(b.to_s(16)+" ")
489 line_tmp = $sp.gets()
490 line_tmp = '' if line_tmp==nil
491 line = '' if line.end_with?('\n')
493 puts("DBG got: "+line) if $debug
494 if /^Error:.*/.match(line)
498 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)
508 if(a_== a && b_ == b && c_ == ref[1] && d_ == ref[2] && e_== ref[0])
509 $logfile.printf("[pass]: %s\n", line)
512 $logfile.printf("[fail (%s%s%s%s%s)]: %s", (a==a_)?'':'a', (b==b_)?'':'b',
513 (c_==ref[1])?'':'c', (d_==ref[2])?'':'d', (e_==ref[0])?'':'e', line)
514 $logfile.printf(" ; should gcdext( %s, %s) => a = %s; b = %s; gcd = %s\n",
515 a.to_s(16), b.to_s(16), ref[1].to_s(16), ref[2].to_s(16), ref[0].to_s(16))
521 ################################################################################
523 ################################################################################
525 def run_test_add(skip=0)
533 a = rand(256**length_a_B) * s_a
534 b = rand(256**length_a_B) * s_b
543 b_size = rand(length_b_B+1)
544 a = rand(256**length_a_B) * s_a
545 b = rand(256**b_size) * s_b
554 end while length_a_B<4096/8
557 ################################################################################
558 # run_test_add_scale #
559 ################################################################################
561 def run_test_add_scale(skip=0)
568 16.times { scales << rand(301) }
570 scales.each do |scale|
571 a = rand(256**length_a_B)
572 b = rand(256**length_a_B)
573 v = add_scale_test(a, b, scale)
575 v = add_scale_test(b, a, scale)
581 16.times { scales << rand(301) }
583 scales.each do |scale|
584 b_size = rand(length_b_B+1)
585 a = rand(256**length_a_B)
586 b = rand(256**b_size)
587 v = add_scale_test(a, b, scale)
589 v = add_scale_test(b, a, scale)
595 end while length_a_B<4096/8
598 ################################################################################
600 ################################################################################
602 def run_test_mul(skip=0)
610 a = rand(256**length_a_B) * s_a
611 b = rand(256**length_a_B) * s_b
620 b_size = rand(length_b_B+1)
621 a = rand(256**length_a_B) * s_a
622 b = rand(256**b_size) * s_b
630 end while length_a_B<4096/8
633 ################################################################################
635 ################################################################################
637 def run_test_square(skip=0)
642 a = rand(256**length_a_B)
647 end while length_a_B<4096/8
650 ################################################################################
652 ################################################################################
654 def run_test_reduce(skip=0)
660 a = rand(256**length_a_B)
661 b = rand(256**length_a_B)+1
662 v = reduce_test(a, b)
666 b_size = rand(length_b_B+1)
667 a = rand(256**length_a_B)
668 b = rand(256**b_size)+1
669 v = reduce_test(a, b)
674 end while length_a_B<4096/8
677 ################################################################################
679 ################################################################################
681 def run_test_expmod(skip=0)
688 a = rand(256**length_a_B)
689 b = rand(256**length_b_B)+1
690 c = rand(256**length_c_B)+1
691 v = expmod_test(a, b, c)
695 b_size = rand(length_b_B+1)
696 a = rand(256**length_a_B)
697 b = rand(256**b_size)+1
698 c = rand(256**b_size)+1
699 v = expmod_test(a, b, c)
704 end while length_a_B<4096/8
707 ################################################################################
709 ################################################################################
711 def run_test_gcdext(skip=0)
717 a = rand(256**length_a_B)
718 b = rand(256**length_a_B)+1
719 v = gcdext_test(a, b)
724 b_size = rand(length_b_B+1)
725 a = rand(256**length_a_B)
726 b = rand(256**b_size)+1
727 v = gcdext_test(a, b)
733 end while length_a_B<4096/8
736 ################################################################################
738 ################################################################################
740 opts = Getopt::Std.getopts("s:f:i:a:hd")
743 conf = readconfigfile("/etc/testport.conf", conf)
744 conf = readconfigfile("~/.testport.conf", conf)
745 conf = readconfigfile("testport.conf", conf)
746 conf = readconfigfile(opts["f"], conf) if opts["f"]
750 puts("serial port interface version: " + SerialPort::VERSION);
754 params = { "baud" => conf["PORT"]["baud"].to_i,
755 "data_bits" => conf["PORT"]["databits"].to_i,
756 "stop_bits" => conf["PORT"]["stopbits"].to_i,
757 "parity" => SerialPort::NONE }
758 params["paraty"] = SerialPort::ODD if conf["PORT"]["paraty"].downcase == "odd"
759 params["paraty"] = SerialPort::EVEN if conf["PORT"]["paraty"].downcase == "even"
760 params["paraty"] = SerialPort::MARK if conf["PORT"]["paraty"].downcase == "mark"
761 params["paraty"] = SerialPort::SPACE if conf["PORT"]["paraty"].downcase == "space"
763 puts("\nPort: "+conf["PORT"]["port"]+"@" +
764 params["baud"].to_s +
766 params["data_bits"].to_s +
767 conf["PORT"]["paraty"][0,1].upcase +
768 params["stop_bits"].to_s +
771 $sp = SerialPort.new(conf["PORT"]["port"], params)
773 $sp.read_timeout=1000; # 5 minutes
774 $sp.flow_control = SerialPort::SOFT
782 logfilename = conf['PORT']['testlogbase']+'bigint.txt'
783 if File.exists?(logfilename)
786 logfilename = sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i,'.txt')
788 end while(File.exists?(logfilename))
790 File.move(sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i-2,'.txt'),
791 sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i-1,'.txt'), true)
794 File.move(sprintf('%s%s', conf['PORT']['testlogbase'],'bigint.txt'),
795 sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',1,'.txt'), true)
796 logfilename = conf['PORT']['testlogbase']+'bigint.txt'
798 $logfile = File.open(logfilename, 'w')
799 printf("logfile: %s\n", logfilename)
801 $logfile.printf("bigint test from: %s\n", Time.now.to_s)
802 $logfile.printf("skip = %s\n", opts['s']) if opts['s']
803 $logfile.printf("seed = 0x%X\n", 0xdeadbeef)
806 tests['a'] = proc {|x| run_test_add(x) }
807 tests['m'] = proc {|x| run_test_mul(x) }
808 tests['x'] = proc {|x| run_test_add_scale(x) }
809 tests['s'] = proc {|x| run_test_square(x) }
810 tests['r'] = proc {|x| run_test_reduce(x) }
811 tests['e'] = proc {|x| run_test_expmod(x) }
812 tests['g'] = proc {|x| run_test_gcdext(x) }
814 init_str['a'] = 'add-test'
815 init_str['x'] = 'add-scale-test'
816 init_str['m'] = 'mul-test'
817 init_str['s'] = 'square-test'
818 init_str['r'] = 'reduce-test'
819 init_str['e'] = 'expmod-test'
820 init_str['g'] = 'gcdext-test'
825 opts['a'].each_char do |x|
828 init_system(init_str[x])
829 tests[x].call(opts['s']?opts['s'].to_i():0)
831 puts "no test defiened for '#{x}'"
835 'amsre'.each_char do |x|
838 init_system(init_str[x])
839 tests[x].call(opts['s']?opts['s'].to_i():0)
841 puts "no test defiened for '#{x}'"