]> git.cryptolib.org Git - avr-crypto-lib.git/blob - host/get_test.rb
host tools for verification +testvectors
[avr-crypto-lib.git] / host / get_test.rb
1 #!/usr/bin/ruby 
2 # get_test.rb
3 =begin
4     This file is part of the AVR-Crypto-Lib.
5     Copyright (C) 2008  Daniel Otte (daniel.otte@rub.de)
6
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.
11
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.
16
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/>.
19 =end
20
21 require 'serialport'
22
23 def read_line
24   s = $sp.gets()
25   if s==nil
26     puts "ERROR: read timeout!\n";
27         return nil
28   end   
29   s.gsub(/\006/, '');   
30 end
31
32 def readTestVector(param)
33   fname=$dir;
34   lb="";
35   buffer="";
36   set=0;
37   vector=0;
38   begin
39     lb = read_line()
40         if (m=/unknown command/.match(lb) || m=/[Ee][Rr]{2}[Oo][Rr]/.match(lb))
41       puts("ERROR: "+lb);
42       exit(2);
43     end
44     if(lb==nil)
45       return false;
46     end
47   end while(m=/\*+/.match(lb));
48   
49   buffer += lb;
50   begin
51     lb = read_line()
52     if(lb==nil)
53       return false;
54     end
55     buffer+=lb;
56   end while(m=/\*.*/.match(lb));
57
58   while(!(m=/Test vectors/.match(lb))) 
59     m=/[^:]*:[\s]([A-Za-z0-9_-]*)/.match(lb);
60     if(m) 
61       fname+=m[1]+".";
62     end
63     buffer+=lb;
64         lb = read_line();
65   end
66   if(param!="")
67     fname+=param+".";
68   end
69   puts("-> "+fname+"txt");
70   file=File.new(fname+"txt", "w");
71     buffer+=lb;
72     file.write(buffer);
73     begin
74       if (m=/Test\ vectors\ \-\-\ set[\s]+([0-9]+)/.match(lb))
75         set=m[1].to_i;
76         print("\nSet "+m[1]+":");
77       end
78       if (m=/Set [0-9]*, vector#[\s]*([0-9]+):/.match(lb))
79         vector=m[1].to_i;
80         #print(" "+m[1]);
81         if(vector!=0 && vector % $linewidth==0)
82           print("\n      ")
83         end
84         printf(" %4u", vector);
85       end
86       lb=read_line();
87       if(lb==nil)
88         file.close();
89         return false;
90       end
91       file.write(lb);
92     end while(!m=/End of test vectors/.match(lb));
93     puts("\n");
94   file.close();
95   return true
96 end
97
98
99 if ARGV.size < 5
100   STDERR.print <<EOF
101   Usage: ruby #{$0} port bps nbits stopb command [target_dir] [additional specifier]
102 EOF
103   exit(1)
104 end
105
106 command=ARGV[4]+"\r";
107 $dir=(ARGV.size>=6)?ARGV[5]:"";
108 param=(ARGV.size>=7)?ARGV[6]:"";
109
110 puts("\nPort: "+ARGV[0]+ "@"+ARGV[1]+" "+ARGV[2]+"N"+ARGV[3]+"\n");
111 $linewidth = 16
112 $sp = SerialPort.new(ARGV[0], ARGV[1].to_i, ARGV[2].to_i, ARGV[3].to_i, SerialPort::NONE);
113 $sp.read_timeout=1*60*1000; # 5 minutes
114 $extended_wait=100;
115 $sp.write(command);
116
117 if(readTestVector(param)==false)
118   puts("ERROR: test seems not to be implemented");
119   exit(3);
120 end
121
122 while(readTestVector(param))
123 end
124
125 exit(0);
126
127