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