Skip to content

Commit 720f2cc

Browse files
authored
Fix Python attributes (#103)
* Fix typo error message Signed-off-by: Iyán Méndez Veiga <[email protected]> * Populate attributes in Signature and KeyEncapsulation ctypes.Structure.__init__() initializes attributes using the list _fields_. However, these are empty because this happens before native().OQS_SIG_new() or native().OQS_KEM_new() are called. The dict self.details was showing correct values by reading and decoding directly from self._sig/self._kem. However, all the other public attributes like self.method_name, self.alg_version or the keys and signature lengths were incorrect. Either None or 0 depending on the ctype defined in _fields_. Instead, let's manually populate all relevant attributes from _fields_ after OQS_SIG_new() or OQS_KEM_new() are called, and let's use those values to create the dict self.details. Signed-off-by: Iyán Méndez Veiga <[email protected]> * Add unit test to check python attributes Signed-off-by: Iyán Méndez Veiga <[email protected]> --------- Signed-off-by: Iyán Méndez Veiga <[email protected]>
1 parent dc1f0a9 commit 720f2cc

File tree

3 files changed

+73
-18
lines changed

3 files changed

+73
-18
lines changed

oqs/oqs.py

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -246,15 +246,24 @@ def __init__(self, alg_name, secret_key=None):
246246

247247
self._kem = native().OQS_KEM_new(ct.create_string_buffer(alg_name.encode()))
248248

249+
self.method_name = self._kem.contents.method_name
250+
self.alg_version = self._kem.contents.alg_version
251+
self.claimed_nist_level = self._kem.contents.claimed_nist_level
252+
self.ind_cca = self._kem.contents.ind_cca
253+
self.length_public_key = self._kem.contents.length_public_key
254+
self.length_secret_key = self._kem.contents.length_secret_key
255+
self.length_ciphertext = self._kem.contents.length_ciphertext
256+
self.length_shared_secret = self._kem.contents.length_shared_secret
257+
249258
self.details = {
250-
"name": self._kem.contents.method_name.decode(),
251-
"version": self._kem.contents.alg_version.decode(),
252-
"claimed_nist_level": int(self._kem.contents.claimed_nist_level),
253-
"is_ind_cca": bool(self._kem.contents.ind_cca),
254-
"length_public_key": int(self._kem.contents.length_public_key),
255-
"length_secret_key": int(self._kem.contents.length_secret_key),
256-
"length_ciphertext": int(self._kem.contents.length_ciphertext),
257-
"length_shared_secret": int(self._kem.contents.length_shared_secret),
259+
"name": self.method_name.decode(),
260+
"version": self.alg_version.decode(),
261+
"claimed_nist_level": int(self.claimed_nist_level),
262+
"is_ind_cca": bool(self.ind_cca),
263+
"length_public_key": int(self.length_public_key),
264+
"length_secret_key": int(self.length_secret_key),
265+
"length_ciphertext": int(self.length_ciphertext),
266+
"length_shared_secret": int(self.length_shared_secret),
258267
}
259268

260269
if secret_key:
@@ -412,15 +421,25 @@ def __init__(self, alg_name, secret_key=None):
412421
raise MechanismNotSupportedError(alg_name)
413422

414423
self._sig = native().OQS_SIG_new(ct.create_string_buffer(alg_name.encode()))
424+
425+
self.method_name = self._sig.contents.method_name
426+
self.alg_version = self._sig.contents.alg_version
427+
self.claimed_nist_level = self._sig.contents.claimed_nist_level
428+
self.euf_cma = self._sig.contents.euf_cma
429+
self.sig_with_ctx_support = self._sig.contents.sig_with_ctx_support
430+
self.length_public_key = self._sig.contents.length_public_key
431+
self.length_secret_key = self._sig.contents.length_secret_key
432+
self.length_signature = self._sig.contents.length_signature
433+
415434
self.details = {
416-
"name": self._sig.contents.method_name.decode(),
417-
"version": self._sig.contents.alg_version.decode(),
418-
"claimed_nist_level": int(self._sig.contents.claimed_nist_level),
419-
"is_euf_cma": bool(self._sig.contents.euf_cma),
420-
"sig_with_ctx_support": bool(self._sig.contents.sig_with_ctx_support),
421-
"length_public_key": int(self._sig.contents.length_public_key),
422-
"length_secret_key": int(self._sig.contents.length_secret_key),
423-
"length_signature": int(self._sig.contents.length_signature),
435+
"name": self.method_name.decode(),
436+
"version": self.alg_version.decode(),
437+
"claimed_nist_level": int(self.claimed_nist_level),
438+
"is_euf_cma": bool(self.euf_cma),
439+
"sig_with_ctx_support": bool(self.sig_with_ctx_support),
440+
"length_public_key": int(self.length_public_key),
441+
"length_secret_key": int(self.length_secret_key),
442+
"length_signature": int(self.length_signature),
424443
}
425444

426445
if secret_key:

tests/test_kem.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,31 @@ def test_not_enabled():
6868
raise AssertionError(f"An unexpected exception was raised: {ex}")
6969

7070

71+
def test_python_attributes():
72+
for alg_name in oqs.get_enabled_kem_mechanisms():
73+
with oqs.KeyEncapsulation(alg_name) as kem:
74+
if kem.method_name.decode() != alg_name:
75+
raise AssertionError("Incorrect oqs.KeyEncapsulation.method_name")
76+
if kem.alg_version is None:
77+
raise AssertionError("Undefined oqs.KeyEncapsulation.alg_version")
78+
if not 1 <= kem.claimed_nist_level <= 5:
79+
raise AssertionError("Invalid oqs.KeyEncapsulation.claimed_nist_level")
80+
if kem.length_public_key == 0:
81+
raise AssertionError("Incorrect oqs.KeyEncapsulation.length_public_key")
82+
if kem.length_secret_key == 0:
83+
raise AssertionError("Incorrect oqs.KeyEncapsulation.length_secret_key")
84+
if kem.length_ciphertext == 0:
85+
raise AssertionError("Incorrect oqs.KeyEncapsulation.length_signature")
86+
if kem.length_shared_secret == 0:
87+
raise AssertionError("Incorrect oqs.KeyEncapsulation.length_shared_secret")
88+
89+
7190
if __name__ == "__main__":
7291
try:
7392
import nose2
7493

7594
nose2.main()
7695
except ImportError:
7796
raise RuntimeError(
78-
"nose2 module not found. Please install it with 'pip install node2'."
97+
"nose2 module not found. Please install it with 'pip install nose2'."
7998
)

tests/test_sig.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,29 @@ def test_not_enabled():
115115
raise AssertionError(f"An unexpected exception was raised: {ex}")
116116

117117

118+
def test_python_attributes():
119+
for alg_name in oqs.get_enabled_sig_mechanisms():
120+
with oqs.Signature(alg_name) as sig:
121+
if sig.method_name.decode() != alg_name:
122+
raise AssertionError("Incorrect oqs.Signature.method_name")
123+
if sig.alg_version is None:
124+
raise AssertionError("Undefined oqs.Signature.alg_version")
125+
if not 1 <= sig.claimed_nist_level <= 5:
126+
raise AssertionError("Invalid oqs.Signature.claimed_nist_level")
127+
if sig.length_public_key == 0:
128+
raise AssertionError("Incorrect oqs.Signature.length_public_key")
129+
if sig.length_secret_key == 0:
130+
raise AssertionError("Incorrect oqs.Signature.length_secret_key")
131+
if sig.length_signature == 0:
132+
raise AssertionError("Incorrect oqs.Signature.length_signature")
133+
134+
118135
if __name__ == "__main__":
119136
try:
120137
import nose2
121138

122139
nose2.main()
123140
except ImportError:
124141
raise RuntimeError(
125-
"nose2 module not found. Please install it with 'pip install node2'."
142+
"nose2 module not found. Please install it with 'pip install nose2'."
126143
)

0 commit comments

Comments
 (0)