34
34
logger .setLevel (logging .INFO )
35
35
logger .addHandler (logging .StreamHandler (stdout ))
36
36
37
+ # Expected return value from native OQS functions
38
+ OQS_SUCCESS : Final [int ] = 0
39
+ OQS_ERROR : Final [int ] = - 1
40
+
37
41
38
42
def oqs_python_version () -> Union [str , None ]:
39
43
"""liboqs-python version string."""
@@ -50,12 +54,14 @@ def oqs_python_version() -> Union[str, None]:
50
54
OQS_VERSION = oqs_python_version ()
51
55
52
56
53
- def _countdown (seconds : int ) -> None :
54
- while seconds > 0 :
55
- logger .info ("Installing in %s seconds..." , seconds )
56
- stdout .flush ()
57
- seconds -= 1
58
- time .sleep (1 )
57
+ def version (version_str : str ) -> tuple [str , str , str ]:
58
+ parts = version_str .split ("." )
59
+
60
+ major = parts [0 ] if len (parts ) > 0 else ""
61
+ minor = parts [1 ] if len (parts ) > 1 else ""
62
+ patch = parts [2 ] if len (parts ) > 2 else ""
63
+
64
+ return major , minor , patch
59
65
60
66
61
67
def _load_shared_obj (
@@ -100,6 +106,14 @@ def _load_shared_obj(
100
106
raise RuntimeError (msg )
101
107
102
108
109
+ def _countdown (seconds : int ) -> None :
110
+ while seconds > 0 :
111
+ logger .info ("Installing in %s seconds..." , seconds )
112
+ stdout .flush ()
113
+ seconds -= 1
114
+ time .sleep (1 )
115
+
116
+
103
117
def _install_liboqs (
104
118
target_directory : Path ,
105
119
oqs_version_to_install : Union [str , None ] = None ,
@@ -188,7 +202,9 @@ def _load_liboqs() -> ct.CDLL:
188
202
assert liboqs # noqa: S101
189
203
except RuntimeError :
190
204
# We don't have liboqs, so we try to install it automatically
191
- _install_liboqs (target_directory = oqs_install_dir , oqs_version_to_install = OQS_VERSION )
205
+ _install_liboqs (
206
+ target_directory = oqs_install_dir , oqs_version_to_install = OQS_VERSION
207
+ )
192
208
# Try loading it again
193
209
try :
194
210
liboqs = _load_shared_obj (
@@ -206,11 +222,6 @@ def _load_liboqs() -> ct.CDLL:
206
222
_liboqs = _load_liboqs ()
207
223
208
224
209
- # Expected return value from native OQS functions
210
- OQS_SUCCESS : Final [int ] = 0
211
- OQS_ERROR : Final [int ] = - 1
212
-
213
-
214
225
def native () -> ct .CDLL :
215
226
"""Handle to native liboqs handler."""
216
227
return _liboqs
@@ -226,13 +237,24 @@ def oqs_version() -> str:
226
237
return ct .c_char_p (native ().OQS_version ()).value .decode ("UTF-8" ) # type: ignore[union-attr]
227
238
228
239
229
- # Warn the user if the liboqs version differs from liboqs-python version
230
- if oqs_version () != oqs_python_version ():
231
- warnings .warn (
232
- f"liboqs version { oqs_version ()} differs from liboqs-python version "
233
- f"{ oqs_python_version ()} " ,
234
- stacklevel = 2 ,
240
+ oqs_ver = oqs_version ()
241
+ oqs_ver_major , oqs_ver_minor , oqs_ver_patch = version (oqs_ver )
242
+
243
+
244
+ oqs_python_ver = oqs_python_version ()
245
+ if oqs_python_ver :
246
+ oqs_python_ver_major , oqs_python_ver_minor , oqs_python_ver_patch = version (
247
+ oqs_python_ver
235
248
)
249
+ # Warn the user if the liboqs version differs from liboqs-python version
250
+ if not (
251
+ oqs_ver_major == oqs_python_ver_major and oqs_ver_minor == oqs_python_ver_minor
252
+ ):
253
+ warnings .warn (
254
+ f"liboqs version (major, minor) { oqs_version ()} differs from liboqs-python version "
255
+ f"{ oqs_python_version ()} " ,
256
+ stacklevel = 2 ,
257
+ )
236
258
237
259
238
260
class MechanismNotSupportedError (Exception ):
@@ -281,7 +303,9 @@ class KeyEncapsulation(ct.Structure):
281
303
("decaps_cb" , ct .c_void_p ),
282
304
]
283
305
284
- def __init__ (self , alg_name : str , secret_key : Union [int , bytes , None ] = None ) -> None :
306
+ def __init__ (
307
+ self , alg_name : str , secret_key : Union [int , bytes , None ] = None
308
+ ) -> None :
285
309
"""
286
310
Create new KeyEncapsulation with the given algorithm.
287
311
@@ -435,9 +459,15 @@ def is_kem_enabled(alg_name: str) -> bool:
435
459
return native ().OQS_KEM_alg_is_enabled (ct .create_string_buffer (alg_name .encode ()))
436
460
437
461
438
- _KEM_alg_ids = [native ().OQS_KEM_alg_identifier (i ) for i in range (native ().OQS_KEM_alg_count ())]
439
- _supported_KEMs : tuple [str , ...] = tuple ([i .decode () for i in _KEM_alg_ids ]) # noqa: N816
440
- _enabled_KEMs : tuple [str , ...] = tuple ([i for i in _supported_KEMs if is_kem_enabled (i )]) # noqa: N816
462
+ _KEM_alg_ids = [
463
+ native ().OQS_KEM_alg_identifier (i ) for i in range (native ().OQS_KEM_alg_count ())
464
+ ]
465
+ _supported_KEMs : tuple [str , ...] = tuple (
466
+ [i .decode () for i in _KEM_alg_ids ]
467
+ ) # noqa: N816
468
+ _enabled_KEMs : tuple [str , ...] = tuple (
469
+ [i for i in _supported_KEMs if is_kem_enabled (i )]
470
+ ) # noqa: N816
441
471
442
472
443
473
def get_enabled_kem_mechanisms () -> tuple [str , ...]:
@@ -478,7 +508,9 @@ class Signature(ct.Structure):
478
508
("verify_cb" , ct .c_void_p ),
479
509
]
480
510
481
- def __init__ (self , alg_name : str , secret_key : Union [int , bytes , None ] = None ) -> None :
511
+ def __init__ (
512
+ self , alg_name : str , secret_key : Union [int , bytes , None ] = None
513
+ ) -> None :
482
514
"""
483
515
Create new Signature with the given algorithm.
484
516
@@ -723,9 +755,13 @@ def is_sig_enabled(alg_name: str) -> bool:
723
755
return native ().OQS_SIG_alg_is_enabled (ct .create_string_buffer (alg_name .encode ()))
724
756
725
757
726
- _sig_alg_ids = [native ().OQS_SIG_alg_identifier (i ) for i in range (native ().OQS_SIG_alg_count ())]
758
+ _sig_alg_ids = [
759
+ native ().OQS_SIG_alg_identifier (i ) for i in range (native ().OQS_SIG_alg_count ())
760
+ ]
727
761
_supported_sigs : tuple [str , ...] = tuple ([i .decode () for i in _sig_alg_ids ])
728
- _enabled_sigs : tuple [str , ...] = tuple ([i for i in _supported_sigs if is_sig_enabled (i )])
762
+ _enabled_sigs : tuple [str , ...] = tuple (
763
+ [i for i in _supported_sigs if is_sig_enabled (i )]
764
+ )
729
765
730
766
731
767
def get_enabled_sig_mechanisms () -> tuple [str , ...]:
0 commit comments