@@ -110,10 +110,10 @@ def _install_liboqs(target_directory, oqs_version=None):
110110def _load_liboqs ():
111111 if "OQS_INSTALL_PATH" in os .environ :
112112 oqs_install_dir = os .path .abspath (os .environ ["OQS_INSTALL_PATH" ])
113- else :
113+ else :
114114 home_dir = os .path .expanduser ("~" )
115115 oqs_install_dir = os .path .abspath (home_dir + os .path .sep + "_oqs" ) # $HOME/_oqs
116-
116+
117117 oqs_lib_dir = (
118118 os .path .abspath (oqs_install_dir + os .path .sep + "bin" ) # $HOME/_oqs/bin
119119 if platform .system () == "Windows"
@@ -122,10 +122,14 @@ def _load_liboqs():
122122 oqs_lib64_dir = (
123123 os .path .abspath (oqs_install_dir + os .path .sep + "bin" ) # $HOME/_oqs/bin
124124 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
126128 )
127129 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+ )
129133 assert _liboqs
130134 except RuntimeError :
131135 # We don't have liboqs, so we try to install it automatically
@@ -462,18 +466,18 @@ def sign(self, message):
462466 c_signature = ct .create_string_buffer (self ._sig .contents .length_signature )
463467
464468 # 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 )
466470
467471 rv = native ().OQS_SIG_sign (
468472 self ._sig ,
469473 ct .byref (c_signature ),
470- ct .byref (signature_len ),
474+ ct .byref (c_signature_len ),
471475 c_message ,
472476 c_message_len ,
473477 self .secret_key ,
474478 )
475479 if rv == OQS_SUCCESS :
476- return bytes (c_signature [: signature_len .value ])
480+ return bytes (c_signature [: c_signature_len .value ])
477481 else :
478482 raise RuntimeError ("Can not sign message" )
479483
@@ -489,7 +493,7 @@ def verify(self, message, signature, public_key):
489493 c_message = ct .create_string_buffer (message , len (message ))
490494 c_message_len = ct .c_size_t (len (c_message ))
491495 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 ))
493497 c_public_key = ct .create_string_buffer (
494498 public_key , self ._sig .contents .length_public_key
495499 )
@@ -499,7 +503,7 @@ def verify(self, message, signature, public_key):
499503 c_message ,
500504 c_message_len ,
501505 c_signature ,
502- signature_len ,
506+ c_signature_len ,
503507 c_public_key ,
504508 )
505509 return True if rv == OQS_SUCCESS else False
@@ -511,24 +515,30 @@ def sign_with_ctx_str(self, message, context):
511515 :param context: the context string.
512516 :param message: the message to sign.
513517 """
518+ if context and not self ._sig .contents .sig_with_ctx_support :
519+ raise RuntimeError ("Signing with context string not supported" )
520+
514521 # Provide length to avoid extra null char
515522 c_message = ct .create_string_buffer (message , len (message ))
516523 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 ))
519530 c_signature = ct .create_string_buffer (self ._sig .contents .length_signature )
520531
521532 # Initialize to maximum signature size
522533 c_signature_len = ct .c_size_t (self ._sig .contents .length_signature )
523-
524534 rv = native ().OQS_SIG_sign_with_ctx_str (
525535 self ._sig ,
526536 ct .byref (c_signature ),
527537 ct .byref (c_signature_len ),
528538 c_message ,
529539 c_message_len ,
530540 c_context ,
531- context_len ,
541+ c_context_len ,
532542 self .secret_key ,
533543 )
534544 if rv == OQS_SUCCESS :
@@ -545,13 +555,20 @@ def verify_with_ctx_str(self, message, signature, context, public_key):
545555 :param context: the context string.
546556 :param public_key: the signer's public key.
547557 """
558+ if context and not self ._sig .contents .sig_with_ctx_support :
559+ raise RuntimeError ("Verifying with context string not supported" )
560+
548561 # Provide length to avoid extra null char
549562 c_message = ct .create_string_buffer (message , len (message ))
550563 c_message_len = ct .c_size_t (len (c_message ))
551564 c_signature = ct .create_string_buffer (signature , len (signature ))
552565 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 ))
555572 c_public_key = ct .create_string_buffer (
556573 public_key , self ._sig .contents .length_public_key
557574 )
0 commit comments