Skip to content

Commit 7314b2f

Browse files
committed
Clean gen_or_load_stateful_signature_key.
Signed-off-by: Guiliano99 <[email protected]>
1 parent 1188132 commit 7314b2f

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

oqs/serialize.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,30 @@ def deserialize_stateful_signature_key(
8888
msg = f"Unsupported stateful signature OID: {oid}"
8989
raise ValueError(msg)
9090

91+
def _may_generate_stfl_key(
92+
key_name: str, dir_name: str
93+
) -> tuple[Optional[bytes], Optional[bytes]]:
94+
"""
95+
Decide whether to generate a stateful signature key for the given algorithm name.
96+
97+
Currently, this function allows opportunistic generation only for fast XMSS parameter sets
98+
used in tests, specifically those starting with "XMSS-" and containing "_16_".
99+
100+
:param key_name: The name of the stateful signature mechanism.
101+
:param dir_name: The directory where the key files are stored.
102+
:return: A tuple (private_key_bytes, public_key_bytes) if generated, else (None, None).
103+
"""
104+
alt_path = Path(str(dir_name).replace("xmss_xmssmt_keys", "tmp_keys", 1))
105+
alt_fpath = alt_path / f"{key_name.replace('/', '_layers_', 1).lower()}.der"
106+
if key_name.startswith("XMSS-") and "_16_" in key_name:
107+
Path(alt_path).mkdir(parents=True, exist_ok=True)
108+
with oqs.StatefulSignature(key_name) as stfl_sig:
109+
public_key_bytes = stfl_sig.generate_keypair()
110+
private_key_bytes = stfl_sig.export_secret_key()
111+
serialize_stateful_signature_key(stfl_sig, public_key_bytes, str(alt_fpath))
112+
return private_key_bytes, public_key_bytes
113+
114+
return None, None
91115
private_key_bytes = one_asym_key["privateKey"].asOctets()
92116
public_key_bytes = one_asym_key["publicKey"].asOctets()
93117
return private_key_bytes, public_key_bytes
@@ -118,16 +142,8 @@ def gen_or_load_stateful_signature_key(
118142
)
119143
return private_key_bytes, public_key_bytes
120144

121-
return None, None
122145
# Opportunistic generation for fast XMSS parameter sets used in tests
123-
if key_name.startswith("XMSS-") and "_16_" in key_name:
124-
Path(alt_path).mkdir(parents=True, exist_ok=True)
125-
with oqs.StatefulSignature(key_name) as stfl_sig:
126-
public_key_bytes = stfl_sig.generate_keypair()
127-
private_key_bytes = stfl_sig.export_secret_key()
128-
serialize_stateful_signature_key(stfl_sig, public_key_bytes, str(alt_fpath))
129-
return private_key_bytes, public_key_bytes
130-
return None, None
146+
return _may_generate_stfl_key(key_name, dir_name)
131147

132148

133149
if __name__ == "__main__":

0 commit comments

Comments
 (0)