Skip to content

Commit 32ac147

Browse files
author
b-long
committed
continue fixing NanoTDF support
1 parent e9a76b4 commit 32ac147

2 files changed

Lines changed: 19 additions & 17 deletions

File tree

src/otdf_python/header.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,16 @@ def set_policy_info(self, policy_info: PolicyInfo):
107107
def get_policy_info(self) -> PolicyInfo | None:
108108
return self.policy_info
109109

110+
def set_policy_binding(self, policy_binding: bytes):
111+
if len(policy_binding) != 8:
112+
raise ValueError(
113+
f"Policy binding must be exactly 8 bytes (GMAC), got {len(policy_binding)}"
114+
)
115+
self.policy_binding = policy_binding
116+
117+
def get_policy_binding(self) -> bytes | None:
118+
return self.policy_binding
119+
110120
def set_ephemeral_key(self, ephemeral_key: bytes):
111121
if self.ecc_mode is not None:
112122
expected_size = ECCMode.get_ec_compressed_pubkey_size(
@@ -148,8 +158,12 @@ def write_into_buffer(self, buffer: bytearray) -> int:
148158
offset += n
149159
# Policy binding (GMAC - 8 bytes)
150160
if self.policy_binding:
151-
buffer[offset : offset + len(self.policy_binding)] = self.policy_binding
152-
offset += len(self.policy_binding)
161+
if len(self.policy_binding) != 8:
162+
raise ValueError(
163+
f"Policy binding must be exactly 8 bytes (GMAC), got {len(self.policy_binding)}"
164+
)
165+
buffer[offset : offset + 8] = self.policy_binding
166+
offset += 8
153167
else:
154168
# Write zeros if no binding provided
155169
buffer[offset : offset + 8] = b"\x00" * 8

src/otdf_python/nanotdf.py

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from cryptography.hazmat.primitives.asymmetric import ec
1010
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
1111

12-
from otdf_python.asym_crypto import AsymDecryption, AsymEncryption
12+
from otdf_python.asym_crypto import AsymDecryption
1313
from otdf_python.collection_store import CollectionStore, NoOpCollectionStore
1414
from otdf_python.config import KASInfo, NanoTDFConfig
1515
from otdf_python.constants import MAGIC_NUMBER_AND_VERSION
@@ -435,25 +435,13 @@ def create_nano_tdf(
435435
(
436436
derived_key,
437437
ephemeral_public_key_compressed,
438-
kas_public_key,
438+
kas_public_key, # noqa: RUF059
439439
) = self._derive_key_with_ecdh(config)
440440

441-
# Determine if we're using RSA wrapping or ECDH
442-
use_rsa_wrapping = False
443-
444-
if kas_public_key and not ephemeral_public_key_compressed:
445-
# We have a KAS key but no ephemeral key - this means RSA mode
446-
use_rsa_wrapping = True
447-
448-
# If ECDH or RSA worked, use the derived key; otherwise use/generate symmetric key
441+
# Use ECDH-derived key if available; otherwise use/generate symmetric key
449442
# Fallback to symmetric key (for testing or when KAS is not available)
450443
key = derived_key or self._prepare_encryption_key(config)
451444

452-
# If using RSA wrapping, wrap the symmetric key
453-
if use_rsa_wrapping and kas_public_key:
454-
asym_enc = AsymEncryption(kas_public_key)
455-
asym_enc.encrypt(key)
456-
457445
# Create header with ephemeral public key (if ECDH was used)
458446
header_bytes = self._create_header(
459447
policy_body, policy_type, config, ephemeral_public_key_compressed

0 commit comments

Comments
 (0)