forked from open-quantum-safe/liboqs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_kat.py
55 lines (50 loc) · 2.28 KB
/
test_kat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# SPDX-License-Identifier: MIT
import helpers
import os
import os.path
import pytest
import platform
@helpers.filtered_test
@pytest.mark.parametrize('kem_name', helpers.available_kems_by_name())
def test_kem(kem_name):
if kem_name.startswith('SIDH'): pytest.skip('KATs not available for SIDH')
if not(helpers.is_kem_enabled_by_name(kem_name)): pytest.skip('Not enabled')
output = helpers.run_subprocess(
[helpers.path_to_executable('kat_kem'), kem_name],
)
output = output.replace("\r\n", "\n")
kats = []
for filename in os.listdir(os.path.join('tests', 'KATs', 'kem')):
if filename.startswith(kem_name + '.') and filename.endswith('.kat'):
with open(os.path.join('tests', 'KATs', 'kem', filename), 'r') as myfile:
kats.append(myfile.read())
assert(output in kats)
@helpers.filtered_test
@pytest.mark.parametrize('sig_name', helpers.available_sigs_by_name())
def test_sig(sig_name):
if not(helpers.is_sig_enabled_by_name(sig_name)): pytest.skip('Not enabled')
output = helpers.run_subprocess(
[helpers.path_to_executable('kat_sig'), sig_name],
)
output = output.replace("\r\n", "\n")
kats = []
avx2_aes_enabled_on_linux = helpers.is_use_option_enabled_by_name('AVX2_INSTRUCTIONS') and helpers.is_use_option_enabled_by_name('AES_INSTRUCTIONS') and platform.system() == 'Linux'
for filename in os.listdir(os.path.join('tests', 'KATs', 'sig')):
if filename.startswith(sig_name + '.') and filename.endswith('.kat'):
# qtesla's avx2 implementation uses an optimized sampling method that results in different KAT values; we use the correct one (if avx2/aes instructions are available)
append_kat = False
if sig_name.startswith('qTesla'):
if 'avx2' in filename:
if avx2_aes_enabled_on_linux and not helpers.is_build_portable():
append_kat = True
else:
append_kat = True
else:
append_kat = True
if append_kat:
with open(os.path.join('tests', 'KATs', 'sig', filename), 'r') as myfile:
kats.append(myfile.read())
assert(output in kats)
if __name__ == "__main__":
import sys
pytest.main(sys.argv)