| | 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) | | | } | | | |
swift p-384-ne/mldsa87 examples
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.
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 playbook: Securing agents and AI toolsRun your AI side-project on zahid.host
EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.