{"slug": "swift-p-384-ne-mldsa87-examples", "title": "swift p-384-ne/mldsa87 examples", "summary": "A developer published Swift sample code for producing composite ML-DSA-87 and ECDSA P-384 signatures backed by the Secure Enclave, using a single Touch ID prompt for both halves. The code creates a P-384 key through the SecKey API and an ML-DSA-87 key through CryptoKit, stores only opaque handles, and shares one LAContext across both signing operations, setting interactionNotAllowed so the P-384 half cannot trigger a second biometric prompt. The composite message format prefixes the label COMPSIG-MLDSA87-ECDSA-P384-SHA512 with a domain separator before signing the SHA-512 digest.", "body_md": "|  | ``` swift | \n|  | // Both keys use the same rule: Touch ID, current fingerprints only. | \n|  | let accessControl = SecAccessControlCreateWithFlags( | \n|  | nil, | \n|  | kSecAttrAccessibleWhenUnlockedThisDeviceOnly, | \n|  | [.privateKeyUsage, .biometryCurrentSet], | \n|  | nil | \n|  | )! | \n|  | // P-384 half: SecKey API (CryptoKit has no enclave P-384 type). | \n|  | let p384Attributes: [String: Any] = [ | \n|  | kSecAttrKeyType as String: kSecAttrKeyTypeECSECPrimeRandom, | \n|  | kSecAttrKeySizeInBits as String: 384, | \n|  | kSecAttrTokenID as String: kSecAttrTokenIDSecureEnclave, | \n|  | kSecPrivateKeyAttrs as String: [ | \n|  | kSecAttrIsPermanent as String: false, | \n|  | kSecAttrAccessControl as String: accessControl, | \n|  | ], | \n|  | ] | \n|  | let p384 = SecKeyCreateRandomKey(p384Attributes as CFDictionary, nil)! | \n|  | let p384Handle = (SecKeyCopyAttributes(p384) as! [String: Any])[\"toid\"] as! Data | \n|  | // ML-DSA-87 half: CryptoKit. | \n|  | let mldsa = try SecureEnclave.MLDSA87.PrivateKey(accessControl: accessControl) | \n|  | let mldsaHandle = mldsa.dataRepresentation | \n|  | // Store p384Handle and mldsaHandle. They are opaque handles, not key material. | \n|  | Signing (one Touch ID prompt) | \n|  | func compositeSign(_ data: Data) throws -> (mldsa: Data, ecdsaDER: Data) { | \n|  | let label = Data(\"COMPSIG-MLDSA87-ECDSA-P384-SHA512\".utf8) | \n|  | let mPrime = Data(\"CompositeAlgorithmSignatures2025\".utf8) | \n|  | + label | \n|  | + Data([0]) | \n|  | + Data(SHA512.hash(data: data)) | \n|  | // 1. One new context per signature, shared by both halves. | \n|  | let context = LAContext() | \n|  | context.localizedReason = \"sign SSH login (ML-DSA-87 + P-384)\" | \n|  | // 2. ML-DSA-87: open with the context and sign. This shows the one prompt. | \n|  | let mldsaKey = try SecureEnclave.MLDSA87.PrivateKey( | \n|  | dataRepresentation: mldsaHandle, | \n|  | authenticationContext: context | \n|  | ) | \n|  | let mldsaSignature = try mldsaKey.signature(for: mPrime, context: label) | \n|  | // 3. Forbid any further prompt. P-384 must use the approval we already have, | \n|  | // or fail (LAError -1004). It must never ask again. | \n|  | context.interactionNotAllowed = true | \n|  | // 4. P-384: open from the handle with the SAME context. | \n|  | // Empty data, handle in \"toid\". (Handle as data = a different key!) | \n|  | let openAttributes: [String: Any] = [ | \n|  | kSecAttrKeyType as String: kSecAttrKeyTypeECSECPrimeRandom, | \n|  | kSecAttrKeyClass as String: kSecAttrKeyClassPrivate, | \n|  | kSecAttrTokenID as String: kSecAttrTokenIDSecureEnclave, | \n|  | \"toid\": p384Handle, | \n|  | kSecUseAuthenticationContext as String: context, | \n|  | ] | \n|  | var error: Unmanaged<CFError>? | \n|  | guard let p384Key = SecKeyCreateWithData( | \n|  | Data() as CFData, openAttributes as CFDictionary, &error | \n|  | ) else { | \n|  | throw error!.takeRetainedValue() | \n|  | } | \n|  | guard let ecdsaSignature = SecKeyCreateSignature( | \n|  | p384Key, .ecdsaSignatureMessageX962SHA384, mPrime as CFData, &error | \n|  | ) as Data? else { | \n|  | throw error!.takeRetainedValue() | \n|  | } | \n|  | return (mldsaSignature, ecdsaSignature) | \n|  | } | \n|  | ``` |", "url": "https://wpnews.pro/news/swift-p-384-ne-mldsa87-examples", "canonical_source": "https://gist.github.com/DrWhax/819561c95d3c5af51a3e0cdc78143798", "published_at": "2026-12-05 23:27:32+00:00", "updated_at": "2026-12-06 10:13:08.113717+00:00", "lang": "en", "topics": ["artificial-intelligence", "ai-safety", "developer-tools"], "entities": ["Swift", "CryptoKit", "Secure Enclave", "SecKey", "ML-DSA-87", "ECDSA P-384", "Touch ID"], "also_reported_by": [], "takeaway": "Share one LAContext across the ML-DSA-87 and P-384 signing calls and set interactionNotAllowed before the SecKey half so both signatures come from a single Touch ID approval instead of a second prompt or LAError -1004.", "recommendation": {"advice": "When combining Secure Enclave keys for composite signatures, reuse one LAContext and set interactionNotAllowed before the second sign so both halves rely on a single biometric approval.", "playbook": "agent-security", "playbook_url": "https://wpnews.pro/playbooks/agent-security"}, "alternates": {"html": "https://wpnews.pro/news/swift-p-384-ne-mldsa87-examples", "markdown": "https://wpnews.pro/news/swift-p-384-ne-mldsa87-examples.md", "text": "https://wpnews.pro/news/swift-p-384-ne-mldsa87-examples.txt", "jsonld": "https://wpnews.pro/news/swift-p-384-ne-mldsa87-examples.jsonld"}}