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) if $debug
258 if /^Error:.*/.match(line)
262 end while not /[\s]*enter a:[\s]*/.match(line)
263 $sp.print(a.to_s(16)+" ")
266 line = "" if line==nil
267 puts("DBG got: "+line) if $debug
268 if /^Error:.*/.match(line)
272 end while not /[\s]*enter b:[\s]*/.match(line)
273 $sp.print(b.to_s(16)+" ")
276 line = "" if line==nil
277 puts("DBG got: "+line) if $debug
278 if /^Error:.*/.match(line)
282 end while not /[\s]*enter scale:[\s]*/.match(line)
283 $sp.print(scale.to_s(16)+"\n")
286 line = "" if line==nil
287 puts("DBG got: "+line) if $debug
288 if /^Error:.*/.match(line)
292 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)
298 if(a_== a && b_ == b && c_ == (a+b))
299 $logfile.printf("[pass]: %s\n", line)
302 $logfile.printf("[fail (%s%s%s)]: %s", (a==a_)?"":"a", (b==b_)?"":"b", (c_==a+b)?"":"c",line)
303 $logfile.printf(" ; should %s + %s = %s\n", a.to_s(16), b.to_s(16), (a+b).to_s(16))
309 ################################################################################
311 ################################################################################
316 line = "" if line==nil
317 puts("DBG got: "+line) if $debug
318 if /^Error:.*/.match(line)
322 end while not /[\s]*enter a:[\s]*/.match(line)
323 $sp.print(a.to_s(16)+" ")
326 line = "" if line==nil
327 puts("DBG got: "+line) if $debug
328 if /^Error:.*/.match(line)
332 end while not m=/[\s]*([+-]?[0-9a-fA-F]*)[\s]*\*\*2[\s]*=[\s]*([+-]?[0-9a-fA-F]*)/.match(line)
336 if(a_== a && c_ == (a**2))
337 $logfile.printf("[pass]: %s\n", line)
340 $logfile.printf("[fail (%s%s)]: %s", (a==a_)?"":"a", (c_==a**2)?"":"c",line)
341 $logfile.printf(" ; should %s **2 = %s\n", a.to_s(16), (a**2).to_s(16))
347 ################################################################################
349 ################################################################################
354 line = "" if line==nil
355 puts("DBG got: "+line) if $debug
356 if /^Error:.*/.match(line)
360 end while not /[\s]*enter a:[\s]*/.match(line)
361 $sp.print(a.to_s(16)+" ")
364 line = "" if line==nil
365 puts("DBG got: "+line) if $debug
366 if /^Error:.*/.match(line)
370 end while not /[\s]*enter b:[\s]*/.match(line)
371 $sp.print(b.to_s(16)+" ")
374 line_tmp = $sp.gets()
375 line_tmp = '' if line_tmp==nil
377 puts("DBG got: "+line) if $debug
378 if /^Error:.*/.match(line)
382 end while not m=/[\s]*([+-]?[0-9a-fA-F]*)[\s]+%[\s]+([+-]?[0-9a-fA-F]*)[\s]*=[\s]*([+-]?[0-9a-fA-F]+)/.match(line)
387 if(a_== a && b_ == b && c_ == (a%b))
388 $logfile.printf("[pass]: %s\n", line)
391 $logfile.printf("[fail (%s%s%s)]: %s", (a==a_)?"":"a", (b==b_)?"":"b", (c_==a%b)?"":"c",line)
392 $logfile.printf(" ; should %s %% %s = %s\n", a.to_s(16), b.to_s(16), (a%b).to_s(16))
398 ################################################################################
400 ################################################################################
402 def expmod_test(a,b,c)
405 line = "" if line==nil
406 puts("DBG got: "+line) if $debug
407 if /^Error:.*/.match(line)
411 end while not /[\s]*enter a:[\s]*/.match(line)
412 $sp.print(a.to_s(16)+" ")
415 line = "" if line==nil
416 puts("DBG got: "+line) if $debug
417 if /^Error:.*/.match(line)
421 end while not /[\s]*enter b:[\s]*/.match(line)
422 $sp.print(b.to_s(16)+" ")
425 line = "" if line==nil
426 puts("DBG got: "+line) if $debug
427 if /^Error:.*/.match(line)
431 end while not /[\s]*enter c:[\s]*/.match(line)
432 $sp.print(c.to_s(16)+" ")
435 line_tmp = $sp.gets()
436 line_tmp = '' if line_tmp==nil
438 puts("DBG got: "+line) if $debug
439 if /^Error:.*/.match(line)
443 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)
449 if(a_== a && b_ == b && c_ == c && d_ ==expmod(a,b,c) )
450 $logfile.printf("[pass]: %s\n", line)
453 $logfile.printf("[fail (%s%s%s%s)]: %s", (a==a_)?'':'a', (b==b_)?'':'b', (c_==c)?'':'c', (d_==expmod(a,b,c))?'':'d',line)
454 $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))
460 ################################################################################
462 ################################################################################
467 line = "" if line==nil
468 puts("DBG got: "+line) if $debug
469 if /^Error:.*/.match(line)
473 end while not /[\s]*enter a:[\s]*/.match(line)
474 $sp.print(a.to_s(16)+" ")
477 line = "" if line==nil
478 puts("DBG got: "+line) if $debug
479 if /^Error:.*/.match(line)
483 end while not /[\s]*enter b:[\s]*/.match(line)
484 $sp.print(b.to_s(16)+" ")
487 line_tmp = $sp.gets()
488 line_tmp = '' if line_tmp==nil
489 line = '' if line.end_with?('\n')
491 puts("DBG got: "+line) if $debug
492 if /^Error:.*/.match(line)
496 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)
506 if(a_== a && b_ == b && c_ == ref[1] && d_ == ref[2] && e_== ref[0])
507 $logfile.printf("[pass]: %s\n", line)
510 $logfile.printf("[fail (%s%s%s%s%s)]: %s", (a==a_)?'':'a', (b==b_)?'':'b',
511 (c_==ref[1])?'':'c', (d_==ref[2])?'':'d', (e_==ref[0])?'':'e', line)
512 $logfile.printf(" ; should gcdext( %s, %s) => a = %s; b = %s; gcd = %s\n",
513 a.to_s(16), b.to_s(16), ref[1].to_s(16), ref[2].to_s(16), ref[0].to_s(16))
519 ################################################################################
521 ################################################################################
523 def run_test_add(skip=0)
531 a = rand(256**length_a_B) * s_a
532 b = rand(256**length_a_B) * s_b
541 b_size = rand(length_b_B+1)
542 a = rand(256**length_a_B) * s_a
543 b = rand(256**b_size) * s_b
552 end while length_a_B<4096/8
555 ################################################################################
557 ################################################################################
559 def run_test_mul(skip=0)
567 a = rand(256**length_a_B) * s_a
568 b = rand(256**length_a_B) * s_b
577 b_size = rand(length_b_B+1)
578 a = rand(256**length_a_B) * s_a
579 b = rand(256**b_size) * s_b
587 end while length_a_B<4096/8
590 ################################################################################
592 ################################################################################
594 def run_test_square(skip=0)
599 a = rand(256**length_a_B)
604 end while length_a_B<4096/8
607 ################################################################################
609 ################################################################################
611 def run_test_reduce(skip=0)
617 a = rand(256**length_a_B)
618 b = rand(256**length_a_B)+1
619 v = reduce_test(a, b)
623 b_size = rand(length_b_B+1)
624 a = rand(256**length_a_B)
625 b = rand(256**b_size)+1
626 v = reduce_test(a, b)
631 end while length_a_B<4096/8
634 ################################################################################
636 ################################################################################
638 def run_test_expmod(skip=0)
645 a = rand(256**length_a_B)
646 b = rand(256**length_b_B)+1
647 c = rand(256**length_c_B)+1
648 v = expmod_test(a, b, c)
652 b_size = rand(length_b_B+1)
653 a = rand(256**length_a_B)
654 b = rand(256**b_size)+1
655 c = rand(256**b_size)+1
656 v = expmod_test(a, b, c)
661 end while length_a_B<4096/8
664 ################################################################################
666 ################################################################################
668 def run_test_gcdext(skip=0)
674 a = rand(256**length_a_B)
675 b = rand(256**length_a_B)+1
676 v = gcdext_test(a, b)
681 b_size = rand(length_b_B+1)
682 a = rand(256**length_a_B)
683 b = rand(256**b_size)+1
684 v = gcdext_test(a, b)
690 end while length_a_B<4096/8
693 ################################################################################
695 ################################################################################
697 opts = Getopt::Std.getopts("s:f:i:a:hd")
700 conf = readconfigfile("/etc/testport.conf", conf)
701 conf = readconfigfile("~/.testport.conf", conf)
702 conf = readconfigfile("testport.conf", conf)
703 conf = readconfigfile(opts["f"], conf) if opts["f"]
707 puts("serial port interface version: " + SerialPort::VERSION);
711 params = { "baud" => conf["PORT"]["baud"].to_i,
712 "data_bits" => conf["PORT"]["databits"].to_i,
713 "stop_bits" => conf["PORT"]["stopbits"].to_i,
714 "parity" => SerialPort::NONE }
715 params["paraty"] = SerialPort::ODD if conf["PORT"]["paraty"].downcase == "odd"
716 params["paraty"] = SerialPort::EVEN if conf["PORT"]["paraty"].downcase == "even"
717 params["paraty"] = SerialPort::MARK if conf["PORT"]["paraty"].downcase == "mark"
718 params["paraty"] = SerialPort::SPACE if conf["PORT"]["paraty"].downcase == "space"
720 puts("\nPort: "+conf["PORT"]["port"]+"@" +
721 params["baud"].to_s +
723 params["data_bits"].to_s +
724 conf["PORT"]["paraty"][0,1].upcase +
725 params["stop_bits"].to_s +
728 $sp = SerialPort.new(conf["PORT"]["port"], params)
730 $sp.read_timeout=1000; # 5 minutes
731 $sp.flow_control = SerialPort::SOFT
739 logfilename = conf['PORT']['testlogbase']+'bigint.txt'
740 if File.exists?(logfilename)
743 logfilename = sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i,'.txt')
745 end while(File.exists?(logfilename))
747 File.move(sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i-2,'.txt'),
748 sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i-1,'.txt'), true)
751 File.move(sprintf('%s%s', conf['PORT']['testlogbase'],'bigint.txt'),
752 sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',1,'.txt'), true)
753 logfilename = conf['PORT']['testlogbase']+'bigint.txt'
755 $logfile = File.open(logfilename, 'w')
756 printf("logfile: %s\n", logfilename)
758 $logfile.printf("bigint test from: %s\n", Time.now.to_s)
759 $logfile.printf("skip = %s\n", opts['s']) if opts['s']
760 $logfile.printf("seed = 0x%X\n", 0xdeadbeef)
763 tests['a'] = proc {|x| run_test_add(x) }
764 tests['m'] = proc {|x| run_test_mul(x) }
765 tests['s'] = proc {|x| run_test_square(x) }
766 tests['r'] = proc {|x| run_test_reduce(x) }
767 tests['e'] = proc {|x| run_test_expmod(x) }
768 tests['g'] = proc {|x| run_test_gcdext(x) }
770 init_str['a'] = 'add-test'
771 init_str['m'] = 'mul-test'
772 init_str['s'] = 'square-test'
773 init_str['r'] = 'reduce-test'
774 init_str['e'] = 'expmod-test'
775 init_str['g'] = 'gcdext-test'
780 opts['a'].each_char do |x|
783 init_system(init_str[x])
784 tests[x].call(opts['s']?opts['s'].to_i():0)
786 puts "no test defiened for '#{x}'"
790 'amsre'.each_char do |x|
793 init_system(init_str[x])
794 tests[x].call(opts['s']?opts['s'].to_i():0)
796 puts "no test defiened for '#{x}'"