@@ -110,10 +110,10 @@ def _install_liboqs(target_directory, oqs_version=None):
110
110
def _load_liboqs ():
111
111
if "OQS_INSTALL_PATH" in os .environ :
112
112
oqs_install_dir = os .path .abspath (os .environ ["OQS_INSTALL_PATH" ])
113
- else :
113
+ else :
114
114
home_dir = os .path .expanduser ("~" )
115
115
oqs_install_dir = os .path .abspath (home_dir + os .path .sep + "_oqs" ) # $HOME/_oqs
116
-
116
+
117
117
oqs_lib_dir = (
118
118
os .path .abspath (oqs_install_dir + os .path .sep + "bin" ) # $HOME/_oqs/bin
119
119
if platform .system () == "Windows"
@@ -122,10 +122,14 @@ def _load_liboqs():
122
122
oqs_lib64_dir = (
123
123
os .path .abspath (oqs_install_dir + os .path .sep + "bin" ) # $HOME/_oqs/bin
124
124
if platform .system () == "Windows"
125
- else os .path .abspath (oqs_install_dir + os .path .sep + "lib64" ) # $HOME/_oqs/lib64
125
+ else os .path .abspath (
126
+ oqs_install_dir + os .path .sep + "lib64"
127
+ ) # $HOME/_oqs/lib64
126
128
)
127
129
try :
128
- _liboqs = _load_shared_obj (name = "oqs" , additional_searching_paths = [oqs_lib_dir , oqs_lib64_dir ])
130
+ _liboqs = _load_shared_obj (
131
+ name = "oqs" , additional_searching_paths = [oqs_lib_dir , oqs_lib64_dir ]
132
+ )
129
133
assert _liboqs
130
134
except RuntimeError :
131
135
# We don't have liboqs, so we try to install it automatically
@@ -462,18 +466,18 @@ def sign(self, message):
462
466
c_signature = ct .create_string_buffer (self ._sig .contents .length_signature )
463
467
464
468
# Initialize to maximum signature size
465
- signature_len = ct .c_size_t (self ._sig .contents .length_signature )
469
+ c_signature_len = ct .c_size_t (self ._sig .contents .length_signature )
466
470
467
471
rv = native ().OQS_SIG_sign (
468
472
self ._sig ,
469
473
ct .byref (c_signature ),
470
- ct .byref (signature_len ),
474
+ ct .byref (c_signature_len ),
471
475
c_message ,
472
476
c_message_len ,
473
477
self .secret_key ,
474
478
)
475
479
if rv == OQS_SUCCESS :
476
- return bytes (c_signature [: signature_len .value ])
480
+ return bytes (c_signature [: c_signature_len .value ])
477
481
else :
478
482
raise RuntimeError ("Can not sign message" )
479
483
@@ -489,7 +493,7 @@ def verify(self, message, signature, public_key):
489
493
c_message = ct .create_string_buffer (message , len (message ))
490
494
c_message_len = ct .c_size_t (len (c_message ))
491
495
c_signature = ct .create_string_buffer (signature , len (signature ))
492
- signature_len = ct .c_size_t (len (c_signature ))
496
+ c_signature_len = ct .c_size_t (len (c_signature ))
493
497
c_public_key = ct .create_string_buffer (
494
498
public_key , self ._sig .contents .length_public_key
495
499
)
@@ -499,7 +503,7 @@ def verify(self, message, signature, public_key):
499
503
c_message ,
500
504
c_message_len ,
501
505
c_signature ,
502
- signature_len ,
506
+ c_signature_len ,
503
507
c_public_key ,
504
508
)
505
509
return True if rv == OQS_SUCCESS else False
@@ -511,24 +515,30 @@ def sign_with_ctx_str(self, message, context):
511
515
:param context: the context string.
512
516
:param message: the message to sign.
513
517
"""
518
+ if context and not self ._sig .contents .sig_with_ctx_support :
519
+ raise RuntimeError ("Signing with context string not supported" )
520
+
514
521
# Provide length to avoid extra null char
515
522
c_message = ct .create_string_buffer (message , len (message ))
516
523
c_message_len = ct .c_size_t (len (c_message ))
517
- c_context = ct .create_string_buffer (context , len (context ))
518
- context_len = ct .c_size_t (len (c_context ))
524
+ if len (context ) == 0 :
525
+ c_context = None
526
+ c_context_len = 0
527
+ else :
528
+ c_context = ct .create_string_buffer (context , len (context ))
529
+ c_context_len = ct .c_size_t (len (c_context ))
519
530
c_signature = ct .create_string_buffer (self ._sig .contents .length_signature )
520
531
521
532
# Initialize to maximum signature size
522
533
c_signature_len = ct .c_size_t (self ._sig .contents .length_signature )
523
-
524
534
rv = native ().OQS_SIG_sign_with_ctx_str (
525
535
self ._sig ,
526
536
ct .byref (c_signature ),
527
537
ct .byref (c_signature_len ),
528
538
c_message ,
529
539
c_message_len ,
530
540
c_context ,
531
- context_len ,
541
+ c_context_len ,
532
542
self .secret_key ,
533
543
)
534
544
if rv == OQS_SUCCESS :
@@ -545,13 +555,20 @@ def verify_with_ctx_str(self, message, signature, context, public_key):
545
555
:param context: the context string.
546
556
:param public_key: the signer's public key.
547
557
"""
558
+ if context and not self ._sig .contents .sig_with_ctx_support :
559
+ raise RuntimeError ("Verifying with context string not supported" )
560
+
548
561
# Provide length to avoid extra null char
549
562
c_message = ct .create_string_buffer (message , len (message ))
550
563
c_message_len = ct .c_size_t (len (c_message ))
551
564
c_signature = ct .create_string_buffer (signature , len (signature ))
552
565
c_signature_len = ct .c_size_t (len (c_signature ))
553
- c_context = ct .create_string_buffer (context , len (context ))
554
- c_context_len = ct .c_size_t (len (c_context ))
566
+ if len (context ) == 0 :
567
+ c_context = None
568
+ c_context_len = 0
569
+ else :
570
+ c_context = ct .create_string_buffer (context , len (context ))
571
+ c_context_len = ct .c_size_t (len (c_context ))
555
572
c_public_key = ct .create_string_buffer (
556
573
public_key , self ._sig .contents .length_public_key
557
574
)
0 commit comments