blob: 2cdaf49d3a00e56185f12556b5dd2040d64833c7 [file]
import oqs
import random
def test_correctness():
for alg_name in oqs.get_enabled_sig_mechanisms():
yield check_correctness, alg_name
def check_correctness(alg_name):
message = bytes(random.getrandbits(8) for _ in range(100))
sig = oqs.Signature(alg_name)
public_key = sig.generate_keypair()
signature = sig.sign(message)
assert sig.verify(message, signature, public_key)
sig.free()
def test_wrong_message():
for alg_name in oqs.get_enabled_sig_mechanisms():
yield check_wrong_message, alg_name
def check_wrong_message(alg_name):
message = bytes(random.getrandbits(8) for _ in range(100))
sig = oqs.Signature(alg_name)
public_key = sig.generate_keypair()
signature = sig.sign(message)
wrong_message = bytes(random.getrandbits(8) for _ in range(100))
assert not (sig.verify(wrong_message, signature, public_key))
sig.free()
def test_wrong_signature():
for alg_name in oqs.get_enabled_sig_mechanisms():
yield check_wrong_signature, alg_name
def check_wrong_signature(alg_name):
message = bytes(random.getrandbits(8) for _ in range(100))
sig = oqs.Signature(alg_name)
public_key = sig.generate_keypair()
signature = sig.sign(message)
wrong_signature = bytes(random.getrandbits(8) for _ in range(sig.details['length_signature']))
assert not (sig.verify(message, wrong_signature, public_key))
sig.free()
def test_wrong_public_key():
for alg_name in oqs.get_enabled_sig_mechanisms():
yield check_wrong_public_key, alg_name
def check_wrong_public_key(alg_name):
message = bytes(random.getrandbits(8) for _ in range(100))
sig = oqs.Signature(alg_name)
public_key = sig.generate_keypair()
signature = sig.sign(message)
wrong_public_key = bytes(random.getrandbits(8) for _ in range(sig.details['length_public_key']))
assert not (sig.verify(message, signature, wrong_public_key))
sig.free()
def test_not_supported():
try:
sig = oqs.Signature('bogus')
raise AssertionError("oqs.MechanismNotSupportedError was not raised.")
except oqs.MechanismNotSupportedError:
pass
except E:
raise AssertionError("An unexpected exception was raised. " + E)
def test_not_enabled():
# TODO: test broken as the compiled lib determines which algorithms are
# supported and enabled
for alg_name in oqs.get_supported_sig_mechanisms():
if alg_name not in oqs.get_enabled_sig_mechanisms():
# found an non-enabled but supported alg
try:
sig = oqs.Signature(alg_name)
raise AssertionError("oqs.MechanismNotEnabledError was not raised.")
except oqs.MechanismNotEnabledError:
pass
except E:
raise AssertionError("An unexpected exception was raised. " + E)
if __name__ == '__main__':
try:
import nose2
nose2.main()
except ImportError:
import nose
nose.runmodule()