Skip to content

Commit 199cce5

Browse files
authored
Merge pull request #48 from Guiliano99/FixAddCompositeNames
Fix Addtional Composite Version Name Inconsistencies
2 parents 0df21a6 + b973611 commit 199cce5

36 files changed

+605
-622
lines changed

Makefile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ test-pq-hybrid:
113113

114114
test-pq-hybrid-verbose:
115115
# Start the tests for PQ and Hybrid algorithms/mechanisms.
116-
robot --pythonpath=./ --outputdir=reports --variable environment:$(env) tests_pq_and_hybrid
116+
robot --pythonpath=./ --exclude "composite-kemANDfrodokem" --outputdir=reports --variable environment:$(env) tests_pq_and_hybrid
117117

118118
start-mock-ca:
119119
python ./mock_ca/ca_handler.py
@@ -128,4 +128,3 @@ test-mock-ca-verbose:
128128
# The results will be stored in the reports/ directory.
129129
# This will run all tests, including those marked as verbose-tests.
130130
robot --pythonpath=./ --outputdir=reports --variable environment:mock_ca tests tests_mock_ca tests_pq_and_hybrid
131-
File renamed without changes.
File renamed without changes.

pq_logic/combined_factory.py

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@
5757
from pq_logic.keys.xwing import XWingPrivateKey, XWingPublicKey
5858
from pq_logic.tmp_oids import (
5959
CHEMPAT_OID_2_NAME,
60-
COMPOSITE_KEM07_NAME_2_OID,
61-
COMPOSITE_KEM07_OID_2_NAME,
60+
COMPOSITE_KEM_NAME_2_OID,
61+
COMPOSITE_KEM_OID_2_NAME,
6262
COMPOSITE_SIG_OID_TO_NAME,
6363
id_rsa_kem_spki,
6464
)
@@ -94,7 +94,11 @@ def _any_string_in_string(string: str, options: List[str]) -> str:
9494
class CombinedKeyFactory:
9595
"""Factory for creating all known key types."""
9696

97-
_composite_prefixes = ["sig-13", "kem-07", "kem07", "dhkem", "kem", "sig"]
97+
_composite_prefixes = [
98+
"dhkem",
99+
"kem",
100+
"sig",
101+
]
98102

99103
@staticmethod
100104
def get_stateful_sig_algorithms() -> Dict[str, List[str]]:
@@ -278,16 +282,16 @@ def _get_pq_and_trad_names(
278282
return pq_name, trad_name, curve, length
279283

280284
@staticmethod
281-
def _load_composite_kem07_public_key(oid: univ.ObjectIdentifier, public_key: bytes):
282-
"""Load a composite KEM 06 public key from the provided OID and public key bytes.
285+
def _load_composite_kem_public_key(oid: univ.ObjectIdentifier, public_key: bytes):
286+
"""Load a composite KEM public key from the provided OID and public key bytes.
283287
284288
:param oid: The OID of the key.
285289
:param public_key: The public key bytes.
286290
:return: The loaded public key.
287291
:raises BadAsn1Data: If the public key structure is invalid or cannot be decoded.
288292
:raises InvalidKeyCombination: If the key is invalid or the combination is not supported.
289293
"""
290-
orig_name = COMPOSITE_KEM07_OID_2_NAME[oid]
294+
orig_name = COMPOSITE_KEM_OID_2_NAME[oid]
291295

292296
pq_name, trad_name = CombinedKeyFactory.get_pq_and_trad_name_form_hybrid_name(orig_name)
293297
pq_key, rest = PQKeyFactory.from_public_bytes(pq_name, public_key, allow_rest=True)
@@ -338,8 +342,8 @@ def _load_hybrid_key_from_spki(spki: rfc5280.SubjectPublicKeyInfo):
338342
public_key_bytes=spki["subjectPublicKey"].asOctets(),
339343
)
340344

341-
if oid in COMPOSITE_KEM07_OID_2_NAME:
342-
return CombinedKeyFactory._load_composite_kem07_public_key(oid, spki["subjectPublicKey"].asOctets())
345+
if oid in COMPOSITE_KEM_OID_2_NAME:
346+
return CombinedKeyFactory._load_composite_kem_public_key(oid, spki["subjectPublicKey"].asOctets())
343347

344348
if oid in CHEMPAT_OID_2_NAME or oid in CHEMPAT_OID_2_NAME:
345349
return CombinedKeyFactory.load_chempat_key(spki)
@@ -363,7 +367,7 @@ def load_public_key_from_spki(spki: Union[rfc5280.SubjectPublicKeyInfo, bytes]):
363367
if str(oid) == XWING_OID_STR:
364368
return XWingPublicKey.from_public_bytes(spki["subjectPublicKey"].asOctets())
365369

366-
if oid in COMPOSITE_SIG_OID_TO_NAME or oid in COMPOSITE_KEM07_OID_2_NAME or oid in CHEMPAT_OID_2_NAME:
370+
if oid in COMPOSITE_SIG_OID_TO_NAME or oid in COMPOSITE_KEM_OID_2_NAME or oid in CHEMPAT_OID_2_NAME:
367371
return CombinedKeyFactory._load_hybrid_key_from_spki(spki)
368372

369373
if oid in PQ_STATEFUL_HASH_SIG_OID_2_NAME:
@@ -444,14 +448,14 @@ def _load_trad_private_key_from_data(name, trad_data, curve: Optional[str] = Non
444448
return trad_key
445449

446450
@staticmethod
447-
def _load_composite_kem07_from_private_bytes(algorithm: str, private_key: bytes) -> CompositeKEMPrivateKey:
448-
"""Load a Composite KEM v7 public key from private key bytes.
451+
def _load_composite_kem_from_private_bytes(algorithm: str, private_key: bytes) -> CompositeKEMPrivateKey:
452+
"""Load a Composite KEM public key from private key bytes.
449453
450454
:param algorithm: The name of the algorithm.
451455
:param private_key: The private key bytes.
452456
:return: A CompositeKEMPublicKey instance.
453457
"""
454-
logging.info("Loading composite KEM-07 private key: %s", algorithm)
458+
logging.info("Loading composite KEM private key: %s", algorithm)
455459

456460
pq_name, trad_name = CombinedKeyFactory.get_pq_and_trad_name_form_hybrid_name(algorithm)
457461
tmp_pq_key = PQKeyFactory.generate_pq_key(pq_name)
@@ -468,7 +472,7 @@ def _load_composite_kem07_from_private_bytes(algorithm: str, private_key: bytes)
468472
trad_key = CombinedKeyFactory._load_trad_composite_private_key(
469473
trad_name=trad_name,
470474
trad_key_bytes=trad_bytes,
471-
prefix="KEM v7" if "dhkem" not in algorithm.lower() else "dhkem v7",
475+
prefix="KEM" if "dhkem" not in algorithm.lower() else "DHKEM",
472476
)
473477

474478
if not isinstance(trad_key, rsa.RSAPrivateKey):
@@ -496,20 +500,20 @@ def _load_composite_kem07_from_private_bytes(algorithm: str, private_key: bytes)
496500
return composite_key
497501

498502
@staticmethod
499-
def _decode_composite_kem07(
503+
def _decode_composite_kem(
500504
name: str,
501505
private_key_bytes: bytes,
502506
public_key: Optional[bytes],
503507
) -> CompositeKEMPrivateKey:
504-
"""Decode a composite KEM-07 private key."""
505-
private_key = CombinedKeyFactory._load_composite_kem07_from_private_bytes(
508+
"""Decode a composite KEM private key."""
509+
private_key = CombinedKeyFactory._load_composite_kem_from_private_bytes(
506510
algorithm=name,
507511
private_key=private_key_bytes,
508512
)
509513

510514
if public_key is not None:
511515
spki = rfc5280.SubjectPublicKeyInfo()
512-
spki["algorithm"]["algorithm"] = COMPOSITE_KEM07_NAME_2_OID[name]
516+
spki["algorithm"]["algorithm"] = COMPOSITE_KEM_NAME_2_OID[name]
513517
spki["subjectPublicKey"] = univ.BitString.fromOctetString(public_key)
514518
try:
515519
pub_key = CombinedKeyFactory.load_public_key_from_spki(spki)
@@ -575,9 +579,9 @@ def load_private_key_from_one_asym_key(
575579
name = COMPOSITE_SIG_OID_TO_NAME[oid]
576580
return CombinedKeyFactory._load_composite_sig_key(name, private_bytes, public_bytes)
577581

578-
if oid in COMPOSITE_KEM07_OID_2_NAME:
579-
_name = COMPOSITE_KEM07_OID_2_NAME[oid]
580-
return CombinedKeyFactory._decode_composite_kem07(_name, private_bytes, public_bytes)
582+
if oid in COMPOSITE_KEM_OID_2_NAME:
583+
_name = COMPOSITE_KEM_OID_2_NAME[oid]
584+
return CombinedKeyFactory._decode_composite_kem(_name, private_bytes, public_bytes)
581585

582586
if oid == id_rsa_kem_spki:
583587
return RSADecapKey.from_pkcs8(one_asym_key)
@@ -826,14 +830,8 @@ def get_pq_and_trad_name_form_hybrid_name(hybrid_name: str) -> Tuple[str, str]:
826830
# Determine the prefix based on the algorithm name
827831
if alg.startswith("chempat-"):
828832
prefix = "chempat-"
829-
elif alg.startswith("composite-sig-13-"):
830-
prefix = "composite-sig-13-"
831833
elif alg.startswith("composite-sig-"):
832834
prefix = "composite-sig-"
833-
elif alg.startswith("composite-kem-07-"):
834-
prefix = "composite-kem-07-"
835-
elif alg.startswith("composite-kem07-"):
836-
prefix = "composite-kem07-"
837835
elif alg.startswith("composite-dhkem-"):
838836
prefix = "composite-dhkem-"
839837
elif alg.startswith("composite-kem-"):
@@ -877,7 +875,7 @@ def _load_composite_sig_from_private_bytes(algorithm: str, private_key: bytes) -
877875
pq_key = MLDSAPrivateKey.from_private_bytes(pq_bytes, name=pq_name)
878876

879877
trad_key = CombinedKeyFactory._load_trad_composite_private_key(
880-
trad_name=trad_name, trad_key_bytes=trad_bytes, prefix="Sig v13"
878+
trad_name=trad_name, trad_key_bytes=trad_bytes, prefix="Sig"
881879
)
882880

883881
use_pss = trad_name.endswith("-pss")
@@ -943,15 +941,15 @@ def _load_trad_raw_key(
943941

944942
@staticmethod
945943
def _load_trad_composite_private_key(
946-
trad_name: str, trad_key_bytes: bytes, prefix: str = "Sig v13"
944+
trad_name: str, trad_key_bytes: bytes, prefix: str = "Sig"
947945
) -> Union[
948946
RSAPrivateKey, Ed25519PrivateKey, Ed448PrivateKey, X25519PrivateKey, X448PrivateKey, ec.EllipticCurvePrivateKey
949947
]:
950948
"""Load a composite signature key from the given bytes.
951949
952950
:param trad_name: The name of the algorithm, e.g., "rsa2048-pss".
953951
:param trad_key_bytes: The traditional key bytes for RSA, ECDH, ECDSA, or EdDSA keys.
954-
:param prefix: The prefix for the algorithm, e.g., "Sig v13".
952+
:param prefix: The prefix for the algorithm, e.g., "Sig".
955953
"""
956954
try:
957955
if trad_name.startswith("ecdsa") or trad_name.startswith("ecdh"):

pq_logic/hybrid_issuing.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
from pq_logic.keys.composite_sig import CompositeSigPrivateKey, CompositeSigPublicKey
3939
from pq_logic.keys.sig_keys import MLDSAPrivateKey
4040
from pq_logic.tmp_oids import (
41-
COMPOSITE_SIG13_OID_TO_NAME,
41+
COMPOSITE_SIG_OID_TO_NAME,
4242
id_altSignatureExt,
4343
id_ce_deltaCertificateDescriptor,
4444
id_relatedCert,
@@ -331,7 +331,7 @@ def build_cert_from_catalyst_request( # noqa: D417 Missing argument description
331331

332332
popo: rfc4211.ProofOfPossession = cert_req_msg["popo"]
333333

334-
if cert_req_msg["certReq"]["certTemplate"]["publicKey"]["algorithm"]["algorithm"] in COMPOSITE_SIG13_OID_TO_NAME:
334+
if cert_req_msg["certReq"]["certTemplate"]["publicKey"]["algorithm"]["algorithm"] in COMPOSITE_SIG_OID_TO_NAME:
335335
raise ValueError("Composite keys are not supported for Catalyst certificates.")
336336

337337
cert_template = cert_req_msg["certReq"]["certTemplate"]
@@ -546,7 +546,7 @@ def verify_sig_popo_catalyst_cert_req_msg( # noqa: D417 Missing argument descri
546546
sig_alg_oid = cert_req_msg["popo"]["signature"]["algorithmIdentifier"]
547547
oid = sig_alg_oid["algorithm"]
548548

549-
if oid in COMPOSITE_SIG13_OID_TO_NAME:
549+
if oid in COMPOSITE_SIG_OID_TO_NAME:
550550
if not isinstance(first_key, PQSignaturePublicKey):
551551
first_key, alt_pub_key = alt_pub_key, first_key
552552

@@ -1276,8 +1276,8 @@ def is_hybrid_cert(cert: rfc9480.CMPCertificate) -> Optional[str]:
12761276
"""
12771277
alg_oid = cert["tbsCertificate"]["subjectPublicKeyInfo"]["algorithm"]["algorithm"]
12781278

1279-
if alg_oid in COMPOSITE_SIG13_OID_TO_NAME:
1280-
return "composite-sig-13"
1279+
if alg_oid in COMPOSITE_SIG_OID_TO_NAME:
1280+
return "composite-sig"
12811281

12821282
dcd = certextractutils.get_extension(cert["tbsCertificate"]["extensions"], id_ce_deltaCertificateDescriptor)
12831283
if dcd is not None:

pq_logic/keys/composite_kem.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
TradKEMPublicKey,
3030
)
3131
from pq_logic.keys.trad_kem_keys import DHKEMPrivateKey, DHKEMPublicKey, RSADecapKey, RSAEncapKey
32-
from pq_logic.tmp_oids import COMPOSITE_KEM07_NAME_2_OID
32+
from pq_logic.tmp_oids import COMPOSITE_KEM_NAME_2_OID
3333
from resources.exceptions import InvalidKeyCombination
3434
from resources.typingutils import ECDHPrivateKey, ECDHPublicKey
3535

@@ -47,11 +47,11 @@ def _get_kdf_algorithm(pq_name: str, trad_key: TradKEMPublicKey) -> str:
4747

4848

4949
class CompositeKEMPublicKey(HybridKEMPublicKey, AbstractCompositePublicKey):
50-
"""A Composite KEM public key for the Composite KEM 07."""
50+
"""A Composite KEM public key for the current draft."""
5151

5252
_trad_key: TradKEMPublicKey
5353
_pq_key: PQKEMPublicKey
54-
_name = "composite-kem07"
54+
_name = "composite-kem"
5555

5656
def __init__(self, pq_key: PQKEMPublicKey, trad_key: Union[TradKEMPublicKey, ECDHPublicKey, RSAPublicKey]):
5757
"""Initialize the composite KEM public key."""
@@ -88,9 +88,9 @@ def name(self) -> str:
8888

8989
def get_oid(self) -> univ.ObjectIdentifier:
9090
"""Return the OID of the composite KEM."""
91-
if COMPOSITE_KEM07_NAME_2_OID.get(self.name) is None:
91+
if COMPOSITE_KEM_NAME_2_OID.get(self.name) is None:
9292
raise InvalidKeyCombination(f"Unsupported composite KEM combination: {self.name}")
93-
return COMPOSITE_KEM07_NAME_2_OID[self.name]
93+
return COMPOSITE_KEM_NAME_2_OID[self.name]
9494

9595
def kem_combiner(
9696
self, mlkem_ss: bytes, trad_ss: bytes, trad_ct: bytes, trad_pk: bytes, use_in_cms: bool = False
@@ -185,11 +185,11 @@ def public_bytes_raw(self) -> bytes:
185185

186186

187187
class CompositeKEMPrivateKey(HybridKEMPrivateKey, AbstractCompositePrivateKey):
188-
"""A Composite KEM private key for the Composite KEM 07."""
188+
"""A Composite KEM private key for the current draft."""
189189

190190
_trad_key: TradKEMPrivateKey
191191
_pq_key: PQKEMPrivateKey
192-
_name = "composite-kem07"
192+
_name = "composite-kem"
193193

194194
def __init__(self, pq_key: PQKEMPrivateKey, trad_key: Union[TradKEMPrivateKey, ECDHPrivateKey, RSAPrivateKey]):
195195
"""Initialize the composite KEM private key."""
@@ -250,9 +250,9 @@ def get_oid(self) -> univ.ObjectIdentifier:
250250
name = f"{self._name}-{self.pq_key.name}-rsa{value}"
251251
else:
252252
name = self.name
253-
if COMPOSITE_KEM07_NAME_2_OID.get(name) is None:
253+
if COMPOSITE_KEM_NAME_2_OID.get(name) is None:
254254
raise InvalidKeyCombination(f"Unsupported composite KEM combination: {name}")
255-
return COMPOSITE_KEM07_NAME_2_OID[name]
255+
return COMPOSITE_KEM_NAME_2_OID[name]
256256

257257
def public_key(self) -> CompositeKEMPublicKey:
258258
"""Return the public key associated with this private key."""

0 commit comments

Comments
 (0)