Skip to content

Commit 099292d

Browse files
M-AlNoaimivsoftco
andauthored
Use OQS_SIG_supports_ctx_str for context support detection and fix CI build (#111)
* Add PEP 561 type stubs for oqs package The following files have been added: - oqs/py.typed: Marker file for PEP 561 compliance. - oqs/__init__.pyi: Stub for the main package entry point. - oqs/oqs.pyi: Stubs for core OQS functionalities (KeyEncapsulation, Signature, related functions, and exceptions). - oqs/rand.pyi: Stubs for random number generation functions. Signed-off-by: M-AlNoaimi <26318936+M-AlNoaimi@users.noreply.github.com> * Improved oqs stub to use @Final for classes and TypedDict for details attributes & Removed init stub to be more flexible (when making edits directly down the line). Signed-off-by: M-AlNoaimi <26318936+M-AlNoaimi@users.noreply.github.com> * Use new C API for context string support detection Signed-off-by: M-AlNoaimi <26318936+M-AlNoaimi@users.noreply.github.com> --------- Signed-off-by: M-AlNoaimi <26318936+M-AlNoaimi@users.noreply.github.com> Signed-off-by: Vlad Gheorghiu <vsoftco@gmail.com> Co-authored-by: Vlad Gheorghiu <vsoftco@gmail.com>
1 parent a663807 commit 099292d

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

oqs/oqs.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,11 @@ def get_supported_kem_mechanisms() -> tuple[str, ...]:
567567
return _supported_KEMs
568568

569569

570+
# Register the OQS_SIG_supports_ctx_str function from the C library
571+
native().OQS_SIG_supports_ctx_str.restype = ct.c_bool
572+
native().OQS_SIG_supports_ctx_str.argtypes = [ct.c_char_p]
573+
574+
570575
class Signature(ct.Structure):
571576
"""
572577
An OQS Signature wraps native/C liboqs OQS_SIG structs.
@@ -788,7 +793,7 @@ def verify_with_ctx_str(
788793
:param context: the context string.
789794
:param public_key: the signer's public key.
790795
"""
791-
if context and not self._sig.contents.sig_with_ctx_support:
796+
if context and not self.sig_with_ctx_support:
792797
msg = "Verifying with context string not supported"
793798
raise RuntimeError(msg)
794799

tests/test_sig.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import random
33

44
import oqs
5-
from oqs.oqs import Signature
5+
from oqs.oqs import Signature, native
66

77
# Sigs for which unit testing is disabled
88
disabled_sig_patterns = []
@@ -44,6 +44,31 @@ def check_correctness_with_ctx_str(alg_name: str) -> None:
4444
assert sig.verify_with_ctx_str(message, signature, context, public_key) # noqa: S101
4545

4646

47+
def test_sig_with_ctx_support_detection() -> None:
48+
"""
49+
Test that sig_with_ctx_support matches the C API and that sign_with_ctx_str
50+
raises on unsupported algorithms.
51+
"""
52+
for alg_name in oqs.get_enabled_sig_mechanisms():
53+
with Signature(alg_name) as sig:
54+
# Check Python attribute matches C API
55+
c_api_result = native().OQS_SIG_supports_ctx_str(sig.method_name)
56+
assert bool(sig.sig_with_ctx_support) == bool(c_api_result), ( # noqa: S101
57+
f"sig_with_ctx_support mismatch for {alg_name}"
58+
)
59+
# If not supported, sign_with_ctx_str should raise
60+
if not sig.sig_with_ctx_support:
61+
try:
62+
sig.sign_with_ctx_str(b"msg", b"context")
63+
except RuntimeError as e:
64+
if "not supported" not in str(e):
65+
msg = f"Unexpected exception message: {e}"
66+
raise AssertionError(msg) from e
67+
else:
68+
msg = f"sign_with_ctx_str did not raise for {alg_name} without context support"
69+
raise AssertionError(msg)
70+
71+
4772
def test_wrong_message() -> tuple[None, str]:
4873
for alg_name in oqs.get_enabled_sig_mechanisms():
4974
if any(item in alg_name for item in disabled_sig_patterns):

0 commit comments

Comments
 (0)