rt.py: add possibility to parse PKCS#1 v1.5 EMSA testvectors
This commit is contained in:
parent
95f9d527f6
commit
25fcd4c70f
@ -17,10 +17,10 @@ def md5_for_file(path, block_size=256*128):
|
|||||||
f.close()
|
f.close()
|
||||||
return md5.hexdigest()
|
return md5.hexdigest()
|
||||||
|
|
||||||
def read_until_eq(f, s):
|
def read_until_ends(f, s):
|
||||||
while True:
|
while True:
|
||||||
l = f.readline()
|
l = f.readline()
|
||||||
if l.strip() == s:
|
if l.strip().endswith(s):
|
||||||
break
|
break
|
||||||
return l
|
return l
|
||||||
|
|
||||||
@ -69,6 +69,8 @@ class RsaKey(object):
|
|||||||
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)
|
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)
|
||||||
|
|
||||||
def read_key(f):
|
def read_key(f):
|
||||||
|
if ftype.version == 1:
|
||||||
|
read_until_start(f, '# Private key')
|
||||||
n = read_part(f, ftype.n)
|
n = read_part(f, ftype.n)
|
||||||
e = read_part(f, ftype.e)
|
e = read_part(f, ftype.e)
|
||||||
d = read_part(f, ftype.d)
|
d = read_part(f, ftype.d)
|
||||||
@ -88,12 +90,18 @@ class Data(object):
|
|||||||
self.obj3 = obj3
|
self.obj3 = obj3
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
|
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)
|
return "{{\n \"{0}\",\n{1},\n{2},\n{3}\n}}\n,".format(self.name, self.obj1, self.obj2, self.obj3)
|
||||||
|
|
||||||
def read_data(f):
|
def read_data(f):
|
||||||
name = read_until_start(f, ftype.o).strip().lstrip('# ')
|
name = read_until_start(f, ftype.o).strip().lstrip('# ')
|
||||||
obj1 = read_part(f, ftype.o1)
|
obj1 = read_part(f, ftype.o1)
|
||||||
obj2 = read_part(f, ftype.o2)
|
obj2 = read_part(f, ftype.o2)
|
||||||
|
if ftype.name == 'emsa':
|
||||||
|
obj3 = None
|
||||||
|
else:
|
||||||
obj3 = read_part(f, ftype.o3)
|
obj3 = read_part(f, ftype.o3)
|
||||||
s = Data(name, obj1, obj2, obj3)
|
s = Data(name, obj1, obj2, obj3)
|
||||||
return s
|
return s
|
||||||
@ -114,9 +122,11 @@ class Example(object):
|
|||||||
def read_example(f):
|
def read_example(f):
|
||||||
name = read_until_start(f, '# Example').strip().lstrip('# ')
|
name = read_until_start(f, '# Example').strip().lstrip('# ')
|
||||||
key = read_key(f)
|
key = read_key(f)
|
||||||
l = read_until_start(f, '#')
|
l = read_until_start(f, ftype.sod)
|
||||||
d = []
|
d = []
|
||||||
while l.strip().startswith('# --------------------------------'):
|
while l.strip().startswith(ftype.sod):
|
||||||
|
if ftype.version == 1:
|
||||||
|
f.seek(-len(l), os.SEEK_CUR)
|
||||||
data = read_data(f)
|
data = read_data(f)
|
||||||
d.append(data)
|
d.append(data)
|
||||||
l = read_until_start(f, '#')
|
l = read_until_start(f, '#')
|
||||||
@ -138,9 +148,16 @@ class PkcsType(object):
|
|||||||
self.o1 = '# Message to be encrypted'
|
self.o1 = '# Message to be encrypted'
|
||||||
self.o2 = '# Seed'
|
self.o2 = '# Seed'
|
||||||
self.o3 = '# Encryption'
|
self.o3 = '# Encryption'
|
||||||
|
elif name == 'emsa':
|
||||||
|
self.o = '# PKCS#1 v1.5 Signature Example'
|
||||||
|
self.o1 = '# Message to be signed'
|
||||||
|
self.o2 = '# Signature'
|
||||||
else:
|
else:
|
||||||
raise ValueError('Type unknown: ' + name)
|
raise ValueError('Type unknown: ' + name)
|
||||||
|
|
||||||
if name == 'pss' or name == 'oaep':
|
if name == 'pss' or name == 'oaep':
|
||||||
|
self.version = 2
|
||||||
|
self.numcases = 6
|
||||||
self.n = '# RSA modulus n'
|
self.n = '# RSA modulus n'
|
||||||
self.e = '# RSA public exponent e'
|
self.e = '# RSA public exponent e'
|
||||||
self.d = '# RSA private exponent d'
|
self.d = '# RSA private exponent d'
|
||||||
@ -149,6 +166,19 @@ class PkcsType(object):
|
|||||||
self.dP = '# p\'s CRT exponent dP'
|
self.dP = '# p\'s CRT exponent dP'
|
||||||
self.dQ = '# q\'s CRT exponent dQ'
|
self.dQ = '# q\'s CRT exponent dQ'
|
||||||
self.qInv = '# CRT coefficient qInv'
|
self.qInv = '# CRT coefficient qInv'
|
||||||
|
self.sod = '# --------------------------------'
|
||||||
|
elif name == 'emsa':
|
||||||
|
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
|
||||||
self.name = name
|
self.name = name
|
||||||
|
|
||||||
ftype = PkcsType(sys.argv[2])
|
ftype = PkcsType(sys.argv[2])
|
||||||
@ -179,26 +209,30 @@ typedef struct rsaData {
|
|||||||
int o1_l;
|
int o1_l;
|
||||||
unsigned char o1[256];
|
unsigned char o1[256];
|
||||||
int o2_l;
|
int o2_l;
|
||||||
unsigned char o2[256];
|
unsigned char o2[256];''')
|
||||||
int o3_l;
|
|
||||||
unsigned char o3[256];
|
if ftype.name != 'emsa':
|
||||||
} rsaData_t;
|
print(''' int o3_l;
|
||||||
|
unsigned char o3[256];''')
|
||||||
|
|
||||||
|
print('''} rsaData_t;
|
||||||
|
|
||||||
typedef struct testcase {
|
typedef struct testcase {
|
||||||
const char* name;
|
const char* name;
|
||||||
rsaKey_t rsa;
|
rsaKey_t rsa;
|
||||||
rsaData_t data[6];
|
rsaData_t data[%d];
|
||||||
} testcase_t;
|
} testcase_t;
|
||||||
|
|
||||||
testcase_t testcases_%s[] =
|
testcase_t testcases_%s[] =
|
||||||
{''' % sys.argv[2])
|
{''' % (ftype.numcases, sys.argv[2]))
|
||||||
|
|
||||||
with open(sys.argv[1], 'rb') as f:
|
with open(sys.argv[1], 'rb') as f:
|
||||||
ex = []
|
ex = []
|
||||||
while read_until_eq(f, '# ============================================='):
|
while read_until_ends(f, '============================================='):
|
||||||
if f.tell() == os.path.getsize(sys.argv[1]):
|
if f.tell() == os.path.getsize(sys.argv[1]):
|
||||||
break
|
break
|
||||||
ex.append(read_example(f))
|
e = read_example(f)
|
||||||
|
ex.append(e)
|
||||||
|
|
||||||
for i in ex:
|
for i in ex:
|
||||||
print(i)
|
print(i)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user