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)+" ")
375 line = '' if line==nil
376 puts("DBG got: "+line) if $debug
377 if /^Error:.*/.match(line)
381 end while not m=/[\s]*([+-]?[0-9a-fA-F]*)[\s]+%[\s]+([+-]?[0-9a-fA-F]*)[\s]*=[\s]*([+-]?[0-9a-fA-F]+)/.match(line)
386 if(a_== a && b_ == b && c_ == (a%b))
387 $logfile.printf("[pass]: %s\n", line)
390 $logfile.printf("[fail (%s%s%s)]: %s", (a==a_)?"":"a", (b==b_)?"":"b", (c_==a%b)?"":"c",line)
391 $logfile.printf(" ; should %s %% %s = %s\n", a.to_s(16), b.to_s(16), (a%b).to_s(16))
397 ################################################################################
399 ################################################################################
401 def expmod_test(a,b,c)
404 line = "" if line==nil
405 puts("DBG got: "+line) if $debug
406 if /^Error:.*/.match(line)
410 end while not /[\s]*enter a:[\s]*/.match(line)
411 $sp.print(a.to_s(16)+" ")
414 line = "" if line==nil
415 puts("DBG got: "+line) if $debug
416 if /^Error:.*/.match(line)
420 end while not /[\s]*enter b:[\s]*/.match(line)
421 $sp.print(b.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 c:[\s]*/.match(line)
431 $sp.print(c.to_s(16)+" ")
435 line = '' if line==nil
436 puts("DBG got: "+line) if $debug
437 if /^Error:.*/.match(line)
441 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)
447 if(a_== a && b_ == b && c_ == c && d_ ==expmod(a,b,c) )
448 $logfile.printf("[pass]: %s\n", line)
451 $logfile.printf("[fail (%s%s%s%s)]: %s", (a==a_)?'':'a', (b==b_)?'':'b', (c_==c)?'':'c', (d_==expmod(a,b,c))?'':'d',line)
452 $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))
458 ################################################################################
460 ################################################################################
465 line = "" if line==nil
466 puts("DBG got: "+line) if $debug
467 if /^Error:.*/.match(line)
471 end while not /[\s]*enter a:[\s]*/.match(line)
472 $sp.print(a.to_s(16)+" ")
475 line = "" if line==nil
476 puts("DBG got: "+line) if $debug
477 if /^Error:.*/.match(line)
481 end while not /[\s]*enter b:[\s]*/.match(line)
482 $sp.print(b.to_s(16)+" ")
486 line = '' if line==nil
487 puts("DBG got: "+line) if $debug
488 if /^Error:.*/.match(line)
492 end while not m=/gcdext\([\s]*([+-]?[0-9a-fA-F]*)[\s]*,[\s]*([+-]?[0-9a-fA-F]*)[\s]*\)[\s]*=>[\s]*a[\s]*=[\s]*([+-]?[0-9a-fA-F]+);[\s]*b[\s]*=[\s]*([+-]?[0-9a-fA-F]+);[\s]*gcd[\s]*=[\s]*([+-]?[0-9a-fA-F]+)/.match(line)
502 if(a_== a && b_ == b && c_ == ref[1] && d_ == ref[2] && e_== ref[0])
503 $logfile.printf("[pass]: %s\n", line)
506 $logfile.printf("[fail (%s%s%s%s%s)]: %s", (a==a_)?'':'a', (b==b_)?'':'b',
507 (c_==ref[1])?'':'c', (d_==ref[2])?'':'d', (e_==ref[0])?'':'e', line)
508 $logfile.printf(" ; should gcdext( %s, %s) => a = %s; b = %s; gcd = %s\n",
509 a.to_s(16), b.to_s(16), ref[1].to_s(16), ref[2].to_s(16), ref[0].to_s(16))
515 ################################################################################
517 ################################################################################
519 def run_test_add(skip=0)
527 a = rand(256**length_a_B) * s_a
528 b = rand(256**length_a_B) * s_b
537 b_size = rand(length_b_B+1)
538 a = rand(256**length_a_B) * s_a
539 b = rand(256**b_size) * s_b
548 end while length_a_B<4096/8
551 ################################################################################
553 ################################################################################
555 def run_test_mul(skip=0)
563 a = rand(256**length_a_B) * s_a
564 b = rand(256**length_a_B) * s_b
573 b_size = rand(length_b_B+1)
574 a = rand(256**length_a_B) * s_a
575 b = rand(256**b_size) * s_b
583 end while length_a_B<4096/8
586 ################################################################################
588 ################################################################################
590 def run_test_square(skip=0)
595 a = rand(256**length_a_B)
600 end while length_a_B<4096/8
603 ################################################################################
605 ################################################################################
607 def run_test_reduce(skip=0)
613 a = rand(256**length_a_B)
614 b = rand(256**length_a_B)+1
615 v = reduce_test(a, b)
619 b_size = rand(length_b_B+1)
620 a = rand(256**length_a_B)
621 b = rand(256**b_size)+1
622 v = reduce_test(a, b)
627 end while length_a_B<4096/8
630 ################################################################################
632 ################################################################################
634 def run_test_expmod(skip=0)
641 a = rand(256**length_a_B)
642 b = rand(256**length_b_B)+1
643 c = rand(256**length_c_B)+1
644 v = expmod_test(a, b, c)
648 b_size = rand(length_b_B+1)
649 a = rand(256**length_a_B)
650 b = rand(256**b_size)+1
651 c = rand(256**b_size)+1
652 v = expmod_test(a, b, c)
657 end while length_a_B<4096/8
660 ################################################################################
662 ################################################################################
664 def run_test_gcdext(skip=0)
670 a = rand(256**length_a_B)+1
671 b = rand(256**length_a_B)+1
672 v = gcdext_test(a, b)
677 b_size = rand(length_b_B+1)
678 a = rand(256**length_a_B)+1
679 b = rand(256**b_size)+1
680 v = gcdext_test(a, b)
686 end while length_a_B<4096/8
689 ################################################################################
691 ################################################################################
693 opts = Getopt::Std.getopts("s:f:i:a:hd")
696 conf = readconfigfile("/etc/testport.conf", conf)
697 conf = readconfigfile("~/.testport.conf", conf)
698 conf = readconfigfile("testport.conf", conf)
699 conf = readconfigfile(opts["f"], conf) if opts["f"]
703 puts("serial port interface version: " + SerialPort::VERSION);
707 params = { "baud" => conf["PORT"]["baud"].to_i,
708 "data_bits" => conf["PORT"]["databits"].to_i,
709 "stop_bits" => conf["PORT"]["stopbits"].to_i,
710 "parity" => SerialPort::NONE }
711 params["paraty"] = SerialPort::ODD if conf["PORT"]["paraty"].downcase == "odd"
712 params["paraty"] = SerialPort::EVEN if conf["PORT"]["paraty"].downcase == "even"
713 params["paraty"] = SerialPort::MARK if conf["PORT"]["paraty"].downcase == "mark"
714 params["paraty"] = SerialPort::SPACE if conf["PORT"]["paraty"].downcase == "space"
716 puts("\nPort: "+conf["PORT"]["port"]+"@" +
717 params["baud"].to_s +
719 params["data_bits"].to_s +
720 conf["PORT"]["paraty"][0,1].upcase +
721 params["stop_bits"].to_s +
724 $sp = SerialPort.new(conf["PORT"]["port"], params)
726 $sp.read_timeout=1000; # 5 minutes
727 $sp.flow_control = SerialPort::SOFT
735 logfilename = conf['PORT']['testlogbase']+'bigint.txt'
736 if File.exists?(logfilename)
739 logfilename = sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i,'.txt')
741 end while(File.exists?(logfilename))
743 File.move(sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i-2,'.txt'),
744 sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i-1,'.txt'), true)
747 File.move(sprintf('%s%s', conf['PORT']['testlogbase'],'bigint.txt'),
748 sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',1,'.txt'), true)
749 logfilename = conf['PORT']['testlogbase']+'bigint.txt'
751 $logfile = File.open(logfilename, 'w')
752 printf("logfile: %s\n", logfilename)
754 $logfile.printf("bigint test from: %s\n", Time.now.to_s)
755 $logfile.printf("skip = %s\n", opts['s']) if opts['s']
756 $logfile.printf("seed = 0x%X\n", 0xdeadbeef)
759 tests['a'] = proc {|x| run_test_add(x) }
760 tests['m'] = proc {|x| run_test_mul(x) }
761 tests['s'] = proc {|x| run_test_square(x) }
762 tests['r'] = proc {|x| run_test_reduce(x) }
763 tests['e'] = proc {|x| run_test_expmod(x) }
764 tests['g'] = proc {|x| run_test_gcdext(x) }
766 init_str['a'] = 'add-test'
767 init_str['m'] = 'mul-test'
768 init_str['s'] = 'square-test'
769 init_str['r'] = 'reduce-test'
770 init_str['e'] = 'expmod-test'
771 init_str['g'] = 'gcdext-test'
776 opts['a'].each_char do |x|
779 init_system(init_str[x])
780 tests[x].call(opts['s']?opts['s'].to_i():0)
782 puts "no test defiened for '#{x}'"
786 'amsre'.each_char do |x|
789 init_system(init_str[x])
790 tests[x].call(opts['s']?opts['s'].to_i():0)
792 puts "no test defiened for '#{x}'"