# swift p-384-ne/mldsa87 examples

> Source: <https://gist.github.com/DrWhax/819561c95d3c5af51a3e0cdc78143798>
> Published: 2026-12-05 23:27:32+00:00

**Recommendation:** 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.
More in the playbook "Securing agents and AI tools": <https://wpnews.pro/playbooks/agent-security>

|  | ``` swift | 
|  | // Both keys use the same rule: Touch ID, current fingerprints only. | 
|  | let accessControl = SecAccessControlCreateWithFlags( | 
|  | nil, | 
|  | kSecAttrAccessibleWhenUnlockedThisDeviceOnly, | 
|  | [.privateKeyUsage, .biometryCurrentSet], | 
|  | nil | 
|  | )! | 
|  | // P-384 half: SecKey API (CryptoKit has no enclave P-384 type). | 
|  | let p384Attributes: [String: Any] = [ | 
|  | kSecAttrKeyType as String: kSecAttrKeyTypeECSECPrimeRandom, | 
|  | kSecAttrKeySizeInBits as String: 384, | 
|  | kSecAttrTokenID as String: kSecAttrTokenIDSecureEnclave, | 
|  | kSecPrivateKeyAttrs as String: [ | 
|  | kSecAttrIsPermanent as String: false, | 
|  | kSecAttrAccessControl as String: accessControl, | 
|  | ], | 
|  | ] | 
|  | let p384 = SecKeyCreateRandomKey(p384Attributes as CFDictionary, nil)! | 
|  | let p384Handle = (SecKeyCopyAttributes(p384) as! [String: Any])["toid"] as! Data | 
|  | // ML-DSA-87 half: CryptoKit. | 
|  | let mldsa = try SecureEnclave.MLDSA87.PrivateKey(accessControl: accessControl) | 
|  | let mldsaHandle = mldsa.dataRepresentation | 
|  | // Store p384Handle and mldsaHandle. They are opaque handles, not key material. | 
|  | Signing (one Touch ID prompt) | 
|  | func compositeSign(_ data: Data) throws -> (mldsa: Data, ecdsaDER: Data) { | 
|  | let label = Data("COMPSIG-MLDSA87-ECDSA-P384-SHA512".utf8) | 
|  | let mPrime = Data("CompositeAlgorithmSignatures2025".utf8) | 
|  | + label | 
|  | + Data([0]) | 
|  | + Data(SHA512.hash(data: data)) | 
|  | // 1. One new context per signature, shared by both halves. | 
|  | let context = LAContext() | 
|  | context.localizedReason = "sign SSH login (ML-DSA-87 + P-384)" | 
|  | // 2. ML-DSA-87: open with the context and sign. This shows the one prompt. | 
|  | let mldsaKey = try SecureEnclave.MLDSA87.PrivateKey( | 
|  | dataRepresentation: mldsaHandle, | 
|  | authenticationContext: context | 
|  | ) | 
|  | let mldsaSignature = try mldsaKey.signature(for: mPrime, context: label) | 
|  | // 3. Forbid any further prompt. P-384 must use the approval we already have, | 
|  | // or fail (LAError -1004). It must never ask again. | 
|  | context.interactionNotAllowed = true | 
|  | // 4. P-384: open from the handle with the SAME context. | 
|  | // Empty data, handle in "toid". (Handle as data = a different key!) | 
|  | let openAttributes: [String: Any] = [ | 
|  | kSecAttrKeyType as String: kSecAttrKeyTypeECSECPrimeRandom, | 
|  | kSecAttrKeyClass as String: kSecAttrKeyClassPrivate, | 
|  | kSecAttrTokenID as String: kSecAttrTokenIDSecureEnclave, | 
|  | "toid": p384Handle, | 
|  | kSecUseAuthenticationContext as String: context, | 
|  | ] | 
|  | var error: Unmanaged<CFError>? | 
|  | guard let p384Key = SecKeyCreateWithData( | 
|  | Data() as CFData, openAttributes as CFDictionary, &error | 
|  | ) else { | 
|  | throw error!.takeRetainedValue() | 
|  | } | 
|  | guard let ecdsaSignature = SecKeyCreateSignature( | 
|  | p384Key, .ecdsaSignatureMessageX962SHA384, mPrime as CFData, &error | 
|  | ) as Data? else { | 
|  | throw error!.takeRetainedValue() | 
|  | } | 
|  | return (mldsaSignature, ecdsaSignature) | 
|  | } | 
|  | ``` |
