2014-08-04 17:57:27 -04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import hashlib
|
|
|
|
|
|
|
|
def md5_for_file(path, block_size=256*128):
|
|
|
|
'''
|
|
|
|
Block size directly depends on the block size of your filesystem
|
|
|
|
to avoid performances issues
|
|
|
|
Here I have blocks of 4096 octets (Default NTFS)
|
|
|
|
'''
|
|
|
|
md5 = hashlib.md5()
|
|
|
|
with open(path,'rb') as f:
|
|
|
|
for chunk in iter(lambda: f.read(block_size), b''):
|
|
|
|
md5.update(chunk)
|
|
|
|
f.close()
|
|
|
|
return md5.hexdigest()
|
|
|
|
|
2014-08-05 15:53:36 -04:00
|
|
|
def read_until_ends(f, s):
|
2014-08-04 17:57:27 -04:00
|
|
|
while True:
|
|
|
|
l = f.readline()
|
2014-08-05 15:53:36 -04:00
|
|
|
if l.strip().endswith(s):
|
2014-08-04 17:57:27 -04:00
|
|
|
break
|
|
|
|
return l
|
|
|
|
|
|
|
|
def read_until_start(f, s):
|
|
|
|
while True:
|
|
|
|
l = f.readline()
|
|
|
|
if l.startswith(s):
|
|
|
|
break
|
|
|
|
return l
|
|
|
|
|
|
|
|
def read_hex(f):
|
|
|
|
t = []
|
|
|
|
while True:
|
|
|
|
l = f.readline()
|
|
|
|
if l.strip() == '':
|
|
|
|
break
|
|
|
|
t.extend(l.strip().split(' '))
|
|
|
|
return t
|
|
|
|
|
|
|
|
class NamedData(object):
|
|
|
|
def __init__(self, name, data):
|
|
|
|
self.name = name
|
|
|
|
self.data = data
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return " /* {0} */\n {1},\n {{ {2} }}\n".format(self.name, len(self.data), ', '.join('0x' + x for x in self.data))
|
|
|
|
|
|
|
|
def read_part(f, s):
|
|
|
|
name = read_until_start(f, s).strip().lstrip('# ').rstrip(':')
|
|
|
|
data = read_hex(f)
|
|
|
|
e = NamedData(name, data)
|
|
|
|
return e
|
|
|
|
|
|
|
|
class RsaKey(object):
|
2014-08-05 13:26:20 -04:00
|
|
|
def __init__(self, n, e, d, p, q, dP, dQ, qInv):
|
2014-08-04 17:57:27 -04:00
|
|
|
self.n = n
|
|
|
|
self.e = e
|
|
|
|
self.d = d
|
|
|
|
self.p = p
|
2014-08-05 13:26:20 -04:00
|
|
|
self.q = q
|
2014-08-04 17:57:27 -04:00
|
|
|
self.dP = dP
|
|
|
|
self.dQ = dQ
|
|
|
|
self.qInv = qInv
|
|
|
|
|
|
|
|
def __str__(self):
|
2014-08-05 13:26:20 -04:00
|
|
|
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)
|
2014-08-04 17:57:27 -04:00
|
|
|
|
|
|
|
def read_key(f):
|
2014-08-05 15:53:36 -04:00
|
|
|
if ftype.version == 1:
|
|
|
|
read_until_start(f, '# Private key')
|
2014-08-05 13:26:20 -04:00
|
|
|
n = read_part(f, ftype.n)
|
|
|
|
e = read_part(f, ftype.e)
|
|
|
|
d = read_part(f, ftype.d)
|
|
|
|
p = read_part(f, ftype.p)
|
|
|
|
q = read_part(f, ftype.q)
|
|
|
|
dP = read_part(f, ftype.dP)
|
|
|
|
dQ = read_part(f, ftype.dQ)
|
|
|
|
qInv = read_part(f, ftype.qInv)
|
|
|
|
k = RsaKey(n, e, d, p, q, dP, dQ, qInv)
|
2014-08-04 17:57:27 -04:00
|
|
|
return k
|
|
|
|
|
2014-08-05 13:13:07 -04:00
|
|
|
class Data(object):
|
|
|
|
def __init__(self, name, obj1, obj2, obj3):
|
2014-08-04 17:57:27 -04:00
|
|
|
self.name = name
|
2014-08-05 13:13:07 -04:00
|
|
|
self.obj1 = obj1
|
|
|
|
self.obj2 = obj2
|
|
|
|
self.obj3 = obj3
|
2014-08-04 17:57:27 -04:00
|
|
|
|
|
|
|
def __str__(self):
|
2014-08-05 15:53:36 -04:00
|
|
|
if self.obj3 == None:
|
|
|
|
return "{{\n \"{0}\",\n{1},\n{2}\n}}\n,".format(self.name, self.obj1, self.obj2)
|
|
|
|
else:
|
|
|
|
return "{{\n \"{0}\",\n{1},\n{2},\n{3}\n}}\n,".format(self.name, self.obj1, self.obj2, self.obj3)
|
2014-08-05 13:13:07 -04:00
|
|
|
|
|
|
|
def read_data(f):
|
|
|
|
name = read_until_start(f, ftype.o).strip().lstrip('# ')
|
|
|
|
obj1 = read_part(f, ftype.o1)
|
|
|
|
obj2 = read_part(f, ftype.o2)
|
2014-08-05 15:53:36 -04:00
|
|
|
if ftype.name == 'emsa':
|
|
|
|
obj3 = None
|
|
|
|
else:
|
|
|
|
obj3 = read_part(f, ftype.o3)
|
2014-08-05 13:13:07 -04:00
|
|
|
s = Data(name, obj1, obj2, obj3)
|
2014-08-04 17:57:27 -04:00
|
|
|
return s
|
|
|
|
|
|
|
|
class Example(object):
|
2014-08-05 13:13:07 -04:00
|
|
|
def __init__(self, name, key, data):
|
2014-08-04 17:57:27 -04:00
|
|
|
self.name = name
|
|
|
|
self.key = key
|
2014-08-05 13:13:07 -04:00
|
|
|
self.data = data
|
2014-08-04 17:57:27 -04:00
|
|
|
|
|
|
|
def __str__(self):
|
2014-08-05 11:15:47 -04:00
|
|
|
res = "{{\n \"{0}\",\n{1},\n{{".format(self.name, str(self.key))
|
2014-08-05 13:13:07 -04:00
|
|
|
for i in self.data:
|
2014-08-04 17:57:27 -04:00
|
|
|
res += str(i) + '\n'
|
|
|
|
res += '}\n},'
|
|
|
|
return res
|
|
|
|
|
|
|
|
def read_example(f):
|
|
|
|
name = read_until_start(f, '# Example').strip().lstrip('# ')
|
|
|
|
key = read_key(f)
|
2014-08-05 15:53:36 -04:00
|
|
|
l = read_until_start(f, ftype.sod)
|
2014-08-05 13:13:07 -04:00
|
|
|
d = []
|
2014-08-05 15:53:36 -04:00
|
|
|
while l.strip().startswith(ftype.sod):
|
|
|
|
if ftype.version == 1:
|
|
|
|
f.seek(-len(l), os.SEEK_CUR)
|
2014-08-05 13:13:07 -04:00
|
|
|
data = read_data(f)
|
|
|
|
d.append(data)
|
2014-08-04 17:57:27 -04:00
|
|
|
l = read_until_start(f, '#')
|
|
|
|
|
2014-08-05 13:13:07 -04:00
|
|
|
e = Example(name, key, d)
|
2014-08-04 17:57:27 -04:00
|
|
|
f.seek(-len(l), os.SEEK_CUR)
|
|
|
|
return e
|
|
|
|
|
2014-08-05 13:13:07 -04:00
|
|
|
|
|
|
|
class PkcsType(object):
|
|
|
|
def __init__(self, name):
|
|
|
|
if name == 'pss':
|
|
|
|
self.o = '# RSASSA-PSS Signature Example'
|
|
|
|
self.o1 = '# Message to be signed'
|
|
|
|
self.o2 = '# Salt'
|
|
|
|
self.o3 = '# Signature'
|
2014-08-05 13:14:36 -04:00
|
|
|
elif name == 'oaep':
|
|
|
|
self.o = '# RSAES-OAEP Encryption Example'
|
|
|
|
self.o1 = '# Message to be encrypted'
|
|
|
|
self.o2 = '# Seed'
|
|
|
|
self.o3 = '# Encryption'
|
2014-08-05 15:53:36 -04:00
|
|
|
elif name == 'emsa':
|
|
|
|
self.o = '# PKCS#1 v1.5 Signature Example'
|
|
|
|
self.o1 = '# Message to be signed'
|
|
|
|
self.o2 = '# Signature'
|
2014-08-05 17:49:17 -04:00
|
|
|
elif name == 'eme':
|
|
|
|
self.o = '# PKCS#1 v1.5 Encryption Example'
|
|
|
|
self.o1 = '# Message'
|
|
|
|
self.o2 = '# Seed'
|
|
|
|
self.o3 = '# Encryption'
|
2014-08-05 13:13:07 -04:00
|
|
|
else:
|
|
|
|
raise ValueError('Type unknown: ' + name)
|
2014-08-05 15:53:36 -04:00
|
|
|
|
2014-08-05 13:26:20 -04:00
|
|
|
if name == 'pss' or name == 'oaep':
|
2014-08-05 15:53:36 -04:00
|
|
|
self.version = 2
|
|
|
|
self.numcases = 6
|
2014-08-05 13:26:20 -04:00
|
|
|
self.n = '# RSA modulus n'
|
|
|
|
self.e = '# RSA public exponent e'
|
|
|
|
self.d = '# RSA private exponent d'
|
|
|
|
self.p = '# Prime p'
|
|
|
|
self.q = '# Prime q'
|
|
|
|
self.dP = '# p\'s CRT exponent dP'
|
|
|
|
self.dQ = '# q\'s CRT exponent dQ'
|
|
|
|
self.qInv = '# CRT coefficient qInv'
|
2014-08-05 15:53:36 -04:00
|
|
|
self.sod = '# --------------------------------'
|
2014-08-05 17:49:17 -04:00
|
|
|
elif name == 'emsa' or name == 'eme':
|
2014-08-05 15:53:36 -04:00
|
|
|
self.version = 1
|
|
|
|
self.numcases = 20
|
|
|
|
self.n = '# Modulus'
|
|
|
|
self.e = '# Public exponent'
|
|
|
|
self.d = '# Exponent'
|
|
|
|
self.p = '# Prime 1'
|
|
|
|
self.q = '# Prime 2'
|
|
|
|
self.dP = '# Prime exponent 1'
|
|
|
|
self.dQ = '# Prime exponent 2'
|
|
|
|
self.qInv = '# Coefficient'
|
|
|
|
self.sod = self.o
|
2014-08-05 13:13:07 -04:00
|
|
|
self.name = name
|
|
|
|
|
|
|
|
ftype = PkcsType(sys.argv[2])
|
|
|
|
|
2014-08-04 17:57:27 -04:00
|
|
|
print('/* Generated from file: %s\n * with md5 hash: %s\n */\n' % (sys.argv[1], md5_for_file(sys.argv[1])))
|
|
|
|
print('''
|
|
|
|
typedef struct rsaKey {
|
|
|
|
int n_l;
|
|
|
|
unsigned char n[256];
|
|
|
|
int e_l;
|
|
|
|
unsigned char e[256];
|
|
|
|
int d_l;
|
|
|
|
unsigned char d[256];
|
|
|
|
int p_l;
|
|
|
|
unsigned char p[256];
|
|
|
|
int q_l;
|
|
|
|
unsigned char q[256];
|
|
|
|
int dP_l;
|
|
|
|
unsigned char dP[256];
|
|
|
|
int dQ_l;
|
|
|
|
unsigned char dQ[256];
|
|
|
|
int qInv_l;
|
|
|
|
unsigned char qInv[256];
|
|
|
|
} rsaKey_t;
|
|
|
|
|
2014-08-05 13:13:07 -04:00
|
|
|
typedef struct rsaData {
|
2014-08-04 17:57:27 -04:00
|
|
|
const char* name;
|
2014-08-05 13:13:07 -04:00
|
|
|
int o1_l;
|
|
|
|
unsigned char o1[256];
|
|
|
|
int o2_l;
|
2014-08-05 15:53:36 -04:00
|
|
|
unsigned char o2[256];''')
|
|
|
|
|
|
|
|
if ftype.name != 'emsa':
|
|
|
|
print(''' int o3_l;
|
|
|
|
unsigned char o3[256];''')
|
|
|
|
|
|
|
|
print('''} rsaData_t;
|
2014-08-04 17:57:27 -04:00
|
|
|
|
|
|
|
typedef struct testcase {
|
2014-08-05 11:15:47 -04:00
|
|
|
const char* name;
|
2014-08-04 17:57:27 -04:00
|
|
|
rsaKey_t rsa;
|
2014-08-05 15:53:36 -04:00
|
|
|
rsaData_t data[%d];
|
2014-08-04 17:57:27 -04:00
|
|
|
} testcase_t;
|
|
|
|
|
2014-08-05 13:13:07 -04:00
|
|
|
testcase_t testcases_%s[] =
|
2014-08-05 15:53:36 -04:00
|
|
|
{''' % (ftype.numcases, sys.argv[2]))
|
2014-08-04 17:57:27 -04:00
|
|
|
|
|
|
|
with open(sys.argv[1], 'rb') as f:
|
|
|
|
ex = []
|
2014-08-05 15:53:36 -04:00
|
|
|
while read_until_ends(f, '============================================='):
|
2014-08-04 17:57:27 -04:00
|
|
|
if f.tell() == os.path.getsize(sys.argv[1]):
|
|
|
|
break
|
2014-08-05 15:53:36 -04:00
|
|
|
e = read_example(f)
|
|
|
|
ex.append(e)
|
2014-08-04 17:57:27 -04:00
|
|
|
|
|
|
|
for i in ex:
|
|
|
|
print(i)
|
|
|
|
f.close()
|
|
|
|
print('};\n')
|