Mercurial > dropbear
comparison libtomcrypt/notes/rsa-testvectors/rt.py @ 1511:5916af64acd4 fuzz
merge from main
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sat, 17 Feb 2018 19:29:51 +0800 |
parents | 6dba84798cd5 |
children |
comparison
equal
deleted
inserted
replaced
1457:32f990cc96b1 | 1511:5916af64acd4 |
---|---|
1 #!/usr/bin/env python | |
2 | |
3 import sys | |
4 import os | |
5 import hashlib | |
6 | |
7 def md5_for_file(path, block_size=256*128): | |
8 ''' | |
9 Block size directly depends on the block size of your filesystem | |
10 to avoid performances issues | |
11 Here I have blocks of 4096 octets (Default NTFS) | |
12 ''' | |
13 md5 = hashlib.md5() | |
14 with open(path,'rb') as f: | |
15 for chunk in iter(lambda: f.read(block_size), b''): | |
16 md5.update(chunk) | |
17 f.close() | |
18 return md5.hexdigest() | |
19 | |
20 def read_until_ends(f, s): | |
21 while True: | |
22 l = f.readline() | |
23 if l.strip().endswith(s): | |
24 break | |
25 return l | |
26 | |
27 def read_until_start(f, s): | |
28 while True: | |
29 l = f.readline() | |
30 if l.startswith(s): | |
31 break | |
32 return l | |
33 | |
34 def read_hex(f): | |
35 t = [] | |
36 while True: | |
37 l = f.readline() | |
38 if l.strip() == '': | |
39 break | |
40 t.extend(l.strip().split(' ')) | |
41 return t | |
42 | |
43 class NamedData(object): | |
44 def __init__(self, name, data): | |
45 self.name = name | |
46 self.data = data | |
47 | |
48 def __str__(self): | |
49 return " /* {0} */\n {1},\n {{ {2} }}\n".format(self.name, len(self.data), ', '.join('0x' + x for x in self.data)) | |
50 | |
51 def read_part(f, s): | |
52 name = read_until_start(f, s).strip().lstrip('# ').rstrip(':') | |
53 data = read_hex(f) | |
54 e = NamedData(name, data) | |
55 return e | |
56 | |
57 class RsaKey(object): | |
58 def __init__(self, n, e, d, p, q, dP, dQ, qInv): | |
59 self.n = n | |
60 self.e = e | |
61 self.d = d | |
62 self.p = p | |
63 self.q = q | |
64 self.dP = dP | |
65 self.dQ = dQ | |
66 self.qInv = qInv | |
67 | |
68 def __str__(self): | |
69 return "{{\n{0},\n{1},\n{2},\n{3},\n{4},\n{5},\n{6},\n{7}\n}}\n".format(self.n, self.e, self.d, self.p, self.q, self.dP, self.dQ, self.qInv) | |
70 | |
71 def read_key(f): | |
72 if ftype.version == 1: | |
73 read_until_start(f, '# Private key') | |
74 n = read_part(f, ftype.n) | |
75 e = read_part(f, ftype.e) | |
76 d = read_part(f, ftype.d) | |
77 p = read_part(f, ftype.p) | |
78 q = read_part(f, ftype.q) | |
79 dP = read_part(f, ftype.dP) | |
80 dQ = read_part(f, ftype.dQ) | |
81 qInv = read_part(f, ftype.qInv) | |
82 k = RsaKey(n, e, d, p, q, dP, dQ, qInv) | |
83 return k | |
84 | |
85 class Data(object): | |
86 def __init__(self, name, obj1, obj2, obj3): | |
87 self.name = name | |
88 self.obj1 = obj1 | |
89 self.obj2 = obj2 | |
90 self.obj3 = obj3 | |
91 | |
92 def __str__(self): | |
93 if self.obj3 == None: | |
94 return "{{\n \"{0}\",\n{1},\n{2}\n}}\n,".format(self.name, self.obj1, self.obj2) | |
95 else: | |
96 return "{{\n \"{0}\",\n{1},\n{2},\n{3}\n}}\n,".format(self.name, self.obj1, self.obj2, self.obj3) | |
97 | |
98 def read_data(f): | |
99 name = read_until_start(f, ftype.o).strip().lstrip('# ') | |
100 obj1 = read_part(f, ftype.o1) | |
101 obj2 = read_part(f, ftype.o2) | |
102 if ftype.name == 'emsa': | |
103 obj3 = None | |
104 else: | |
105 obj3 = read_part(f, ftype.o3) | |
106 s = Data(name, obj1, obj2, obj3) | |
107 return s | |
108 | |
109 class Example(object): | |
110 def __init__(self, name, key, data): | |
111 self.name = name | |
112 self.key = key | |
113 self.data = data | |
114 | |
115 def __str__(self): | |
116 res = "{{\n \"{0}\",\n{1},\n{{".format(self.name, str(self.key)) | |
117 for idx, d in enumerate(self.data, 1): | |
118 if idx == 2: | |
119 res += '#ifdef LTC_TEST_EXT\n' | |
120 res += str(d) + '\n' | |
121 if idx == ftype.numcases: | |
122 res += '#endif /* LTC_TEST_EXT */\n' | |
123 res += '}\n},' | |
124 return res | |
125 | |
126 def read_example(f): | |
127 name = read_until_start(f, '# Example').strip().lstrip('# ') | |
128 key = read_key(f) | |
129 l = read_until_start(f, ftype.sod) | |
130 d = [] | |
131 while l.strip().startswith(ftype.sod): | |
132 if ftype.version == 1: | |
133 f.seek(-len(l), os.SEEK_CUR) | |
134 data = read_data(f) | |
135 d.append(data) | |
136 l = read_until_start(f, '#') | |
137 | |
138 e = Example(name, key, d) | |
139 f.seek(-len(l), os.SEEK_CUR) | |
140 return e | |
141 | |
142 | |
143 class PkcsType(object): | |
144 def __init__(self, name): | |
145 if name == 'pss': | |
146 self.o = '# RSASSA-PSS Signature Example' | |
147 self.o1 = '# Message to be signed' | |
148 self.o2 = '# Salt' | |
149 self.o3 = '# Signature' | |
150 elif name == 'oaep': | |
151 self.o = '# RSAES-OAEP Encryption Example' | |
152 self.o1 = '# Message to be encrypted' | |
153 self.o2 = '# Seed' | |
154 self.o3 = '# Encryption' | |
155 elif name == 'emsa': | |
156 self.o = '# PKCS#1 v1.5 Signature Example' | |
157 self.o1 = '# Message to be signed' | |
158 self.o2 = '# Signature' | |
159 elif name == 'eme': | |
160 self.o = '# PKCS#1 v1.5 Encryption Example' | |
161 self.o1 = '# Message' | |
162 self.o2 = '# Seed' | |
163 self.o3 = '# Encryption' | |
164 else: | |
165 raise ValueError('Type unknown: ' + name) | |
166 | |
167 if name == 'pss' or name == 'oaep': | |
168 self.version = 2 | |
169 self.numcases = 6 | |
170 self.n = '# RSA modulus n' | |
171 self.e = '# RSA public exponent e' | |
172 self.d = '# RSA private exponent d' | |
173 self.p = '# Prime p' | |
174 self.q = '# Prime q' | |
175 self.dP = '# p\'s CRT exponent dP' | |
176 self.dQ = '# q\'s CRT exponent dQ' | |
177 self.qInv = '# CRT coefficient qInv' | |
178 self.sod = '# --------------------------------' | |
179 elif name == 'emsa' or name == 'eme': | |
180 self.version = 1 | |
181 self.numcases = 20 | |
182 self.n = '# Modulus' | |
183 self.e = '# Public exponent' | |
184 self.d = '# Exponent' | |
185 self.p = '# Prime 1' | |
186 self.q = '# Prime 2' | |
187 self.dP = '# Prime exponent 1' | |
188 self.dQ = '# Prime exponent 2' | |
189 self.qInv = '# Coefficient' | |
190 self.sod = self.o | |
191 self.name = name | |
192 | |
193 ftype = PkcsType(sys.argv[2]) | |
194 | |
195 print('/* Generated from file: %s\n * with md5 hash: %s\n */\n' % (sys.argv[1], md5_for_file(sys.argv[1]))) | |
196 print(''' | |
197 typedef struct rsaKey { | |
198 int n_l; | |
199 unsigned char n[256]; | |
200 int e_l; | |
201 unsigned char e[256]; | |
202 int d_l; | |
203 unsigned char d[256]; | |
204 int p_l; | |
205 unsigned char p[256]; | |
206 int q_l; | |
207 unsigned char q[256]; | |
208 int dP_l; | |
209 unsigned char dP[256]; | |
210 int dQ_l; | |
211 unsigned char dQ[256]; | |
212 int qInv_l; | |
213 unsigned char qInv[256]; | |
214 } rsaKey_t; | |
215 | |
216 typedef struct rsaData { | |
217 const char* name; | |
218 int o1_l; | |
219 unsigned char o1[256]; | |
220 int o2_l; | |
221 unsigned char o2[256];''') | |
222 | |
223 if ftype.name != 'emsa': | |
224 print(''' int o3_l; | |
225 unsigned char o3[256];''') | |
226 | |
227 print('''} rsaData_t; | |
228 | |
229 typedef struct testcase { | |
230 const char* name; | |
231 rsaKey_t rsa; | |
232 #ifdef LTC_TEST_EXT | |
233 rsaData_t data[%d]; | |
234 #else | |
235 rsaData_t data[1]; | |
236 #endif /* LTC_TEST_EXT */ | |
237 } testcase_t; | |
238 | |
239 testcase_t testcases_%s[] = | |
240 {''' % (ftype.numcases, sys.argv[2])) | |
241 | |
242 with open(sys.argv[1], 'rb') as f: | |
243 ex = [] | |
244 while read_until_ends(f, '============================================='): | |
245 if f.tell() == os.path.getsize(sys.argv[1]): | |
246 break | |
247 e = read_example(f) | |
248 ex.append(e) | |
249 | |
250 for i in ex: | |
251 print(i) | |
252 f.close() | |
253 print('};\n') |