Repository Public
1{2 "schema": "qyvora-review/1",3 "title": "Memory hygiene of the cryptographic primitives: where secrets live, what is erased, what cannot be",4 "reviewed_revision_base": "b3e95a0",5 "method": "Source reading of QyvoraMath, QyvoraKeccak, QyvoraSymmetric, QyvoraMLKEM, QyvoraMLDSA, QyvoraSLHDSA and QyvoraSecp256k1. No memory dump or heap inspection was performed: the statements below describe what the source does, not what a debugger shows.",6 "threat": "An attacker who can read the process memory after the fact (crash dump, memory-disclosure bug elsewhere in the process, cold device image, swap is not used on iOS). An attacker who reads memory while a key is in use is out of reach of any erasure.",7 "mechanism": {8 "primitive": "SecureMemory.zero (Sources/QyvoraMath/ConstantTime.swift:75 and :83) calls memset_s on the array's storage; the C standard forbids removing that call, unlike a plain loop.",9 "limits": [10 "It erases `count` elements. Spare capacity of the array is not touched (a buffer emptied with removeAll(keepingCapacity:) keeps its old bytes unless it was erased first).",11 "Swift arrays are copy-on-write. If the array still shares its storage with another value, the mutable access copies it first and the copy is what is erased; the shared storage lives on with the other owner. Each call site was read for this: at every site the array is uniquely owned when it is erased, except PBKDF2 with exactly one iteration, where `t` and `u` share one buffer (the first erasure works on a copy, the second one erases the original: the end state is clean).",12 "An array that grows by reallocation leaves its earlier, smaller buffers in freed memory. The SHA-2 padding tail was changed to reserve its full size up front for that reason (QYV-CRY-002).",13 "Values of fixed-size types (UInt64 limbs, U256, structs) live in registers and on the stack; Swift offers no way to erase a stack frame or a register."14 ]15 },16 "erased": [17 {"where": "ML-KEM", "what": "seeds d and z, encapsulation randomness m, the G input and output, sigma, the secret vectors s, e, y, e1, e2, the message polynomial, u, v, w, m', K', r', the copy of z, K-bar, the PRF buffers and SHAKE states of SamplePolyCBD", "how": "SecureMemory.zero / sponge.wipe at the end of each routine (MLKEM.swift, MLKEMArithmetic.swift:300-324)"},18 {"where": "ML-DSA", "what": "every secret polynomial (s1, s2, t0, y, z, w, hint work) in one Workspace block; xi, the expanded seed, rho', K, rho'', rnd; the XOF states and buffers of RejBoundedPoly and ExpandMask", "how": "Workspace.deinit memset_s (MLDSAArithmetic.swift:502) on every exit path; SecureMemory.zero for the byte strings"},19 {"where": "SLH-DSA", "what": "the three key-generation seeds, SK.prf copy, the FORS secret leaf values, the working block / message schedule / Keccak state of every PRF call (since QYV-CRY-002), the PRF_msg SHAKE state", "how": "memset_s and SecureMemory.zero (SLHDSA.swift:233-238 and :290, Core.swift:340, TweakableHash.swift:127 and :141, SHA2Core.swift:127)"},20 {"where": "secp256k1", "what": "RFC 6979 K and V at every step, the two HMAC messages containing the private key, the retry message (since QYV-CRY-002); the masked key t of BIP-340", "how": "SecureMemory.zero (Secp256k1.swift:297-327, :447)"},21 {"where": "AES / AES-GCM", "what": "expanded key schedule (32-bit form at once, 64-bit round keys on wipe()), GHASH table and accumulator, hash subkey, the recomputed tag on open", "how": "AES.wipe, GHASH.wipe, AESGCM.wipe called through defer by AES256GCM.seal/open"},22 {"where": "ChaCha20-Poly1305", "what": "ChaCha state, scratch and keystream block; Poly1305 r, s, accumulator and buffer; block 0 and the one-time key; the recomputed tag", "how": "SecureMemory.zero, field resets in Poly1305.finalize"},23 {"where": "HMAC / HKDF / PBKDF2", "what": "padded key block, ipad and opad blocks, the outer key, the inner digest, HKDF T(i) and PRK, PBKDF2 U and T", "how": "SecureMemory.zero (SHA2.swift:266-278, KDF.swift)"},24 {"where": "SHA-256 / SHA-512", "what": "the pending input buffer when it is compressed and at finalize, the padded tail (since QYV-CRY-002)", "how": "SecureMemory.zero (SHA2.swift:95, :126-127, :188, :218-219)"},25 {"where": "SHA-3 / SHAKE / cSHAKE one-shots, SHA3_xxx.finalize, KMAC", "what": "the 200-byte sponge state (invertible: it gives back the last absorbed block)", "how": "KeccakSponge.squeezeAndWipe (Keccak.swift:241, since QYV-CRY-002), KMAC.finalize"},26 {"where": "Argon2id / BLAKE2b", "what": "the whole memory matrix, H0, the seed blocks, the final block, the four scratch blocks; BLAKE2b state, buffer, message words and working vector, padded key block", "how": "SecureMemory.zero and memset_s (Argon2.swift:138, :171; BLAKE2b.swift:209; the last two were plain loops before QYV-CRY-002)"}27 ],28 "not_erased": [29 {"item": "Keys, seeds and shared secrets returned to the caller as [UInt8] (private keys of every algorithm, ML-KEM shared secret, HKDF/PBKDF2/Argon2 output, RFC 6979 is internal)", "location": "public API of every module", "reason": "The value belongs to the caller; Bytes is a copy-on-write value type that the callee cannot track. Each assignment or slice by the caller creates another reference or copy.", "residual_risk": "medium", "note": "The dominant residual: the lifetime of key material is decided by the service layer (QyvoraVault.SecretBytes exists there; reviewed by REVIEW-B)."},30 {"item": "secp256k1 scalars and field elements: the private key d, the nonce k, k^-1, intermediate products, the projective coordinates of k*G", "location": "Sources/QyvoraSecp256k1/Secp256k1.swift:339-362 (sign), :437-457 (schnorrSign), U256.swift", "reason": "U256 is a four-limb struct held in registers and stack slots; Swift cannot erase them.", "residual_risk": "medium", "note": "A leaked nonce k with its signature gives the private key. The stack slots are overwritten by later calls but nothing guarantees when. Recommended: move scalar arithmetic of the signing path to a heap workspace erased with memset_s (as ML-DSA does), or accept."},31 {"item": "SHA-256 / SHA-512 / RIPEMD-160 message schedule (w[0..15] repeats the block) and working variables", "location": "Sources/QyvoraSymmetric/SHA2.swift:135 and :227 (compress), withUnsafeTemporaryAllocation scratch", "reason": "Stack scratch released without erasure; erasing 256 or 640 bytes per compression was judged too costly for the generic hash (it is done on the SLH-DSA PRF path only).", "residual_risk": "low", "note": "Holds the last block hashed: for HMAC the last block of the message or the outer block (inner digest), never the raw key block for long because the next compression overwrites the same stack depth. Not guaranteed."},32 {"item": "Chaining state h of SHA-256 / SHA-512 after finalize and of the copied inner/outer states of PBKDF2 and HMAC", "location": "Sources/QyvoraSymmetric/SHA2.swift (h), KDF.swift:96-99 (innerBase, outerBase)", "reason": "The HashFunction protocol has no erase operation; the states are arrays owned by struct copies.", "residual_risk": "low", "note": "The state after the ipad or opad block is equivalent to the HMAC key for that key (it allows computing MACs, not recovering the password)."},33 {"item": "ML-KEM: the decapsulation key and its slices, the re-encrypted ciphertext c', t-hat; the shared secret `key` inside encapsCore", "location": "Sources/QyvoraMLKEM/MLKEM.swift:233, :253", "reason": "dk belongs to the caller; c' and t-hat are public when the ciphertext is valid.", "residual_risk": "low"},34 {"item": "ML-DSA: the packed private key, mu, the w1 encoding of the accepted attempt, the signature", "location": "Sources/QyvoraMLDSA/MLDSA.swift:480", "reason": "Private key owned by the caller; the rest is public once the signature is released. The w1 buffer of a rejected attempt is overwritten in place by the next attempt.", "residual_risk": "low"},35 {"item": "SLH-DSA: WOTS+ chain values in the scratch of wotsPkGen and the XMSS / FORS node buffers", "location": "Sources/QyvoraSLHDSA/Core.swift:119, :215, :270, :325", "reason": "Chains are run to their public end value in place; node buffers hold public tree nodes. The secret start of each chain exists only until its first F call overwrites it.", "residual_risk": "low"},36 {"item": "SLH-DSA SHA-512 path (H and T in categories 3 and 5) and the Keccak H / T scratch", "location": "Sources/QyvoraSLHDSA/TweakableHash.swift:149-240", "reason": "Inputs are public tree nodes and public keys.", "residual_risk": "none"},37 {"item": "AES-GCM / ChaCha20 plaintext and ciphertext buffers; AESGCM instances created directly by a caller and not wiped", "location": "Sources/QyvoraSymmetric/AESGCM.swift:117", "reason": "Plaintext belongs to the caller. The AES256GCM convenience functions wipe their cipher through defer; a caller holding an AESGCM value must call wipe() itself.", "residual_risk": "low"},38 {"item": "Poly1305 struct copies", "location": "Sources/QyvoraSymmetric/ChaCha20Poly1305.swift:154", "reason": "A copied Poly1305 value keeps r and s in its own stored properties; only the instance that runs finalize is reset. The AEAD code makes no copy.", "residual_risk": "low"},39 {"item": "Strings", "location": "none in the reviewed modules", "reason": "No reviewed primitive takes a secret as String (hex helpers excepted, used on public data). Passwords and mnemonic words as String are a service-layer matter (REVIEW-B).", "residual_risk": "not applicable here"}40 ],41 "changes_made": {42 "finding": "QYV-CRY-002",43 "summary": "Erasure added where a secret was left in a released heap buffer or in an invertible form: Keccak one-shot sponge states, SHA-2 padding tail and pending buffer, HMAC inner key block and inner digest, RFC 6979 intermediates and key-bearing messages, ML-KEM G inputs (d || k, m || H(ek)) and the split seed of keyPair(seed:), ML-DSA seed input (xi || k || l), SLH-DSA PRF working memory, Argon2 and BLAKE2b scratch (plain loops replaced by memset_s)."44 },45 "overall_residual_risk": "medium-low: the engine erases its own temporaries where the language allows it; key material returned to callers and fixed-size values on the stack cannot be erased from here. Mitigation outside this layer: short key lifetimes in the vault service, iOS process isolation and data protection, no crash reporter that captures memory."46}47