Skip to content

Commit 9e78588

Browse files
committed
add tests (openssl and nessie)
1 parent be3ca69 commit 9e78588

5 files changed

Lines changed: 7657 additions & 0 deletions

File tree

setup.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ def long_description(short=description):
5252
author='Simon Biewald',
5353
author_email='simon.biewald@homtail.de',
5454
url='https://github.com/var-sec/python-camellia',
55+
project_urls={
56+
'Documentation': 'https://sbiewald.de/docs/python-camellia',
57+
'Tracker': 'https://github.com/Varbin/python-camellia/issues'
58+
},
5559
packages=['camellia'],
5660
package_dir={'camellia': 'src/camellia'},
5761

tests/test_vectors_nessie.py

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
#!/usr/local/bin/env python3
2+
"""Test python-camellia against test vectors from NESSIE.
3+
4+
As vectors are read from are read from a file
5+
(test_vectors_openssl.txt) this is required to be in
6+
the same folder.
7+
8+
This script defines tests in the pytest format.
9+
It may be run standalone or by a testrunner (nose, pytest).
10+
"""
11+
12+
13+
import binascii
14+
import collections
15+
import os
16+
17+
import camellia
18+
19+
VECTOR_FILENAME = os.path.join(os.path.dirname(__file__),
20+
"test_vectors_nessie.txt")
21+
22+
23+
TestvectorNessie = collections.namedtuple(
24+
"TestvectorNessie",
25+
["name", "key", "plain", "cipher", "decrypted", "i100", "i1000"])
26+
27+
28+
def _get_test_vectors_nessie(filename=VECTOR_FILENAME):
29+
vectors = []
30+
with open(VECTOR_FILENAME) as vector_file:
31+
for line in vector_file:
32+
line = line.strip()
33+
if "Set " in line:
34+
name = line[:-1] # colone at lineend
35+
if "key=" in line:
36+
_, key = line.split("=")
37+
if "plain=" in line:
38+
_, plain = line.split("=")
39+
if "cipher=" in line:
40+
_, cipher = line.split("=")
41+
if "decrypted=" in line:
42+
_, decrypted = line.split("=")
43+
if "Iterated 100 times=" in line:
44+
_, i100 = line.split("=")
45+
if "Iterated 1000 times=" in line:
46+
_, i1000 = line.split("=")
47+
vectors.append(
48+
TestvectorNessie(
49+
name, key, plain, cipher, decrypted, i100,
50+
i1000))
51+
return vectors
52+
53+
54+
CODE_TEST = """\
55+
def test_set{set}_vector{vector}():
56+
\"""Test python-camellia against Set \
57+
{set}, vector# {vector} of NESSIE tests.
58+
59+
This function is dynamically created - the vectors file
60+
is required to be in the same folder as the the script.\"""
61+
vector = {tuple}
62+
(key, plain, cipher, decrypted, i100, i1000) = map(
63+
binascii.unhexlify, vector[1:])
64+
65+
cam = camellia.new(key, camellia.MODE_ECB)
66+
67+
cipher_result = cam.encrypt(plain)
68+
decrypted_result = cam.decrypt(cipher)
69+
70+
i100_result = plain
71+
for i in range(100):
72+
i100_result = cam.encrypt(i100_result)
73+
74+
i1000_result = plain
75+
for i in range(1000):
76+
i1000_result = cam.encrypt(i1000_result)
77+
78+
try:
79+
assert cipher == cipher_result
80+
assert decrypted == decrypted_result
81+
assert i100 == i100_result
82+
assert i1000 == i1000_result
83+
except AssertionError:
84+
print(vector.name, "failed")
85+
print()
86+
print("Key:")
87+
print(vector.key)
88+
print()
89+
90+
print("Ciphertext (expected, result):")
91+
print(vector.cipher)
92+
print(binascii.hexlify(cipher_result).upper().decode())
93+
print()
94+
95+
print("Decrypted ciphertext (expected, result):")
96+
print(vector.decrypted)
97+
print(binascii.hexlify(
98+
decrypted_result).upper().decode())
99+
print()
100+
101+
print("Iterated 100 times (expected, result):")
102+
print(vector.i100)
103+
print(binascii.hexlify(
104+
i100_result).upper().decode())
105+
print()
106+
107+
print("Ciphertext (expected, result):")
108+
print(vector.i1000)
109+
print(binascii.hexlify(
110+
i1000_result).upper().decode())
111+
print()
112+
113+
raise"""
114+
115+
# Quick and dirty test generation for progress
116+
# in test runners (nose, pytest), allows easier debugging
117+
118+
for vector in _get_test_vectors_nessie():
119+
split = vector.name.split(", ")
120+
set_n = split[0].split(" ")[-1]
121+
vector_n = split[-1].split("#")[-1].split(" ")[-1]
122+
123+
code = CODE_TEST.format(
124+
set=set_n,
125+
vector=vector_n,
126+
tuple=repr(tuple(vector)))
127+
128+
exec(code)
129+
130+
131+
if __name__ == '__main__':
132+
import __main__
133+
for name in dir(__main__):
134+
if name.startswith("test_") and callable(eval(name)):
135+
print(name, end=': ')
136+
eval(name)()
137+
print("ok")

0 commit comments

Comments
 (0)