Repository navigation
perf: remove CryptoSwift, add streaming file TDF I/O, and fix hot-path bottlenecks - #71
Conversation
There was a problem hiding this comment.
Lines 91-93
💡 Quality: KeyStore.keyPairs/totalBytesStored lost private(set) in this commit
This commit changes keyPairs and totalBytesStored from public private(set) var to public var. The PR description says both should stay public private(set). As public var, code in any module can write them from an actor extension or an isolated context. Something like keyPairs[id] = nil then skips recalculateTotalBytesStored(), so the memory accounting goes stale, and the stored key material can be overwritten without the store's validation. Put the private(set) back on both properties.
Restore read-only external access:
public private(set) var keyPairs: [KeyPairIdentifier: StoredKeyPair]
/// Track total memory used
public private(set) var totalBytesStored: Int = 0
Was this helpful? React with 👍 / 👎
Code Review 👍 Approved with suggestions 🔴 High risk 1 closed / 2 findingsReplacing AES-CBC and changing streaming TDF encryption/decryption alters cryptographic confidentiality and integrity boundaries; implementation errors could expose or corrupt protected payloads. Removes CryptoSwift in favor of CommonCrypto, adds single-pass streaming file TDF decryption and encryption, and applies hot-path performance and deduplication refactors across OpenTDFKit. The streaming decryptFile 512 MiB payload limit was resolved. One open minor finding remains:
💡 Quality: KeyStore.keyPairs/totalBytesStored lost private(set) in this commit📄 OpenTDFKit/KeyStore.swift:91-93 This commit changes Restore read-only external access✅ 1 closed✅ Edge Case: Streaming decryptFile still rejects payloads over 512 MiB
🤖 Prompt for agentsVerificationRules No rules evaluated Functional validation Not enabledAuto-approval Approved For more verification details, comment OptionsAuto-apply is off → Gitar will not commit updates to this branch. Comment with these commands to change the behavior for this request:
Was this helpful? React with 👍 / 👎 | Gitar |
Summary
Implements the prioritized performance, memory, dependency, and deduplication refactor across OpenTDFKit:
P0 — Remove
CryptoSwiftDependencyCryptoSwift.AESCBC encrypt/decrypt inTDFCryptowith hardware-acceleratedCommonCrypto(CCCrypt) and branchless constant-time PKCS#7 unpadding validation (zeroing buffers on failure and eliminatingArray(...)payload copies).CryptoSwiftfromPackage.swiftandPackage.resolved.P1 — Hot-Path Crypto, Concurrency & Algorithmic Fixes
nonceBufferfromNanoTDFCollectionDecryptor, markeddecryptItem/decryptBatch/tryDecryptItemnonisolated(preservingasyncsource compatibility + addingdecryptItemSync), and eliminated per-itemDatanonce allocations inNanoTDFCollectionandNanoTDFCollectionDecryptor.KASRewrapClient.rewrapAndUnwrapTDFSharesnow derives the session KEK once per response instead of per share.KASService.processKeyAccess(header:...)reuses the already-derived primary key instead of running ECDH + HKDF twice.keys.removeFirst()inPublicKeyStorewith an amortized O(1) head-cursor queue.KeyStore.generateAndStoreKeyPairsbyactiveProcessorCount, serialized directly fromSecureKeyPairStoragewithout intermediate unzeroed private-keyDatacopies, and replacedString(format:)hex encoding with a lookup table.standardTDFSessionSaltasstatic let, pre-parsedkasIdentityURLinKASRewrapClient.init, fast ASCII/hexnormalizedPercentEncoding,Date().formatted(.iso8601)inTDFJSONBuilder, zero-copyStrictJSONbyte scanning, direct big-endian reads inBinaryParser, and capacity pre-reservation inNanoTDF/TDFProcessor.P2 — True Streaming File I/O
0600) atomic staging file inStreamingTDFCrypto(halving disk I/O and AES-GCM CPU vs. two-pass) and eliminated per-segmentDatacopies indecryptPayloadMultiSegmentFromMemory.TDFDecryptor.decryptFile/decryptFileMultiSegmentstream viaTDFArchiveReader.writePayload(to:)+ single-pass decryption while honoringchunkSize.TDFEncryptor.encryptFileStreaming+TDFStreamingFileEncryptionResultfor bounded O(segmentSize) file encryption.P3 — Deduplication & Cleanup
NoRedirectDelegate, deduplicatedcreateSignedJWT, removed duplicateconstantTimeEqualshelpers, unified PEM body decoding and EC curve wrapping while eliminatingsealed.combined!force-unwraps, routed file encryptors through sharedmakeManifest/makeResult, and shared single-split EC unwrap logic acrossTDFCBORDecryptorandTDFJSONDecryptor.Verification
swiftformat --lint .— 0/80 files require formattingswift test— 480 tests executed, 0 failures (15 env-gated integration tests skipped)swift build -c release --product OpenTDFKitCLI— succeededscripts/interop/ec-wrappedGoverifyon Swift-generated fixture —OKSwiftandX-Test(Swift ↔ Go cross-SDK) workflows triggered on this PR