Swift Security
Use when working with iOS/macOS Keychain Services (SecItem queries, kSecClass, OSStatus errors), biometric authentication (LAContext, Face ID, Touch ID), CryptoKit (AES-GCM, ChaChaPoly, ECDSA, ECDH, HPKE, ML-KEM), Secure Enclave, secure credential storage (OAuth tokens, API keys), certificate pinning (SecTrust, SPKI), keychain sharing across apps/extensions, migrating secrets from UserDefaults or plists, or OWASP MASVS/MASTG mobile compliance on Apple platforms.
- Skill ID
- dpearson2699/swift-ios-skills/swift-security
- Publisher
- dpearson2699
- Repository
- swift-ios-skills
- Installs
- 0
- Files
- 16
- License
- MIT
- Synced
- Sep 23, 2026
Open any RiverX project, open the Skills panel in the chat, and search for this identifier. The files are fetched from the source repository at install time.
dpearson2699/swift-ios-skills/swift-securityInstalls these files- SKILL.md
- evals/evals.json
- references/biometric-authentication.md
- references/certificate-trust.md
- references/common-anti-patterns.md
- references/compliance-owasp-mapping.md
- references/credential-storage-patterns.md
- references/cryptokit-public-key.md
- references/cryptokit-symmetric.md
- references/keychain-access-control.md
- references/keychain-fundamentals.md
- references/keychain-item-classes.md
- references/keychain-sharing.md
- references/migration-legacy-stores.md
- references/secure-enclave.md
- references/testing-security-code.md
What this skill tells the agent
Swift Security
Use this skill for client-side Apple platform security work: Keychain Services, access control, biometric-gated secrets, CryptoKit, Secure Enclave keys, credential storage, certificate trust, keychain sharing, legacy secret migration, security testing, and OWASP mobile compliance mapping.
Default to iOS 17+ and Swift concurrency examples when the deployment target is unknown. Keep iOS 13+ compatibility notes when the user asks for older targets. Treat iOS 26 CryptoKit post-quantum APIs as availability-gated.
Contents
- Workflow
- Reference Loading
- Security Invariants
- Sibling Boundaries
- Review Checklist
- Common Mistakes
- Output Rules
- References
Workflow
Classify the request before loading references.
- Review existing code: run the Review Checklist, then load common-anti-patterns.md plus the domain reference for each failing area. Report severity, evidence, and the corrected pattern.
- Improve or migrate code: identify the migration type, load the migration and target-domain references, preserve existing data, verify the new item, then remove legacy storage only after success.
- Implement new security code: load the minimum domain references, use the provided correct patterns, include OSStatus handling and tests, then run the relevant checklist.
Do not load every reference file by default. This skill is intentionally split for progressive disclosure; load only the files needed by the user's task.
Minimum Safe Keychain Write
Use separate add, identity, and update dictionaries; handle every OSStatus:
func saveSecret(_ data: Data, account: String) throws {
let identity: [CFString: Any] = [
kSecClass: kSecClassGenericPassword,
kSecAttrService: "com.example.app",
kSecAttrAccount: account,
]
var add = identity
add[kSecValueData] = data
add[kSecAttrAccessible] = kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly
switch SecItemAdd(add as CFDictionary, nil) {
case errSecSuccess:
return
case errSecDuplicateItem:
let status = SecItemUpdate(
identity as CFDictionary,
[kSecValueData: data] as CFDictionary
)
guard status == errSecSuccess else { throw KeychainError(status: status) }
case let status:
throw KeychainError(status: status)
}
}Load keychain-fundamentals.md for read, delete, access-control, locked-device, and test patterns.
Reference Loading
| If the task involves | Load |
|---|---|
| General keychain CRUD or OSStatus handling | keychain-fundamentals.md |
Choosing kSecClass or item identity | keychain-item-classes.md |
Accessibility classes or SecAccessControl | keychain-access-control.md |
| Face ID, Touch ID, or biometric-gated secrets | biometric-authentication.md |
| Secure Enclave keys | secure-enclave.md |
| Hashing, HMAC, AES-GCM, ChaChaPoly, HKDF, PBKDF2 | cryptokit-symmetric.md |
| Signing, ECDH, HPKE, ML-KEM, ML-DSA | cryptokit-public-key.md |
| OAuth tokens, API keys, logout, refresh rotation | credential-storage-patterns.md |
| App/extension keychain sharing | keychain-sharing.md |
| Certificate trust, SPKI pinning, mTLS | certificate-trust.md |
| UserDefaults/plist/NSCoding migration | migration-legacy-stores.md |
| Unit, integration, simulator, device, or CI tests | testing-security-code.md |
| OWASP MASVS/MASTG or enterprise audit mapping | compliance-owasp-mapping.md |
| Full security review | common-anti-patterns.md, then each touched domain reference |
Security Invariants
Use directive language only for these security invariants and the matching anti-patterns in common-anti-patterns.md. For architecture choices outside this list, use advisory language.
- Never store tokens, passwords, API keys, signing keys, or refresh tokens in
UserDefaults,Info.plist,.xcconfig, source code, logs, files, orNSCodingarchives. Use Keychain or fetch secrets at runtime. - Never ignore
OSStatus. EverySecItemAdd,SecItemCopyMatching,SecItemUpdate, andSecItemDeletepath must handle success and expected failures such aserrSecDuplicateItem,errSecItemNotFound, anderrSecInteractionNotAllowed. - Never use
LAContext.evaluatePolicy()as the only gate for a secret. Bind protected secrets to keychain items withSecAccessControl, then let keychain access trigger LocalAuthentication. - Always set
kSecAttrAccessibleorkSecAttrAccessControlexplicitly when adding keychain items. - Always use add-or-update for persistent keychain writes. Do not delete-then-add as a normal update path.
- Keep
SecItem*work off the main actor. Use an actor or serial queue for keychain access. - On macOS AppKit targets, target the data protection keychain with
kSecUseDataProtectionKeychain: trueunless deliberately working with legacy file-based keychain items. - Never reuse an AES-GCM nonce with the same key.
- Never use raw ECDH
SharedSecretbytes as a symmetric key. Derive with HKDF or X9.63 derivation. - Never use
Insecure.MD5orInsecure.SHA1for security purposes.
Sibling Boundaries
This skill owns client-side storage, cryptographic primitives, hardware-backed keys, and trust evaluation. Route adjacent work deliberately:
- Use
authenticationfor Sign in with Apple, passkeys, OAuth UI flows,ASAuthorizationController, credential state, and account sign-in UX. - Use
cryptokitfor primitive CryptoKit API syntax and examples when storage, key lifecycle, protocol/trust design, Secure Enclave policy, certificate trust, misuse review, or compliance is not part of the task. - Keep application-level E2E encryption security reviews here when the work involves key ownership, derivation, storage, rotation/recovery, Secure Enclave, HPKE/PQC migration, protocol trust boundaries, or misuse analysis.
- Use
device-integrityfor DeviceCheck and App Attest attestation/assertion flows. - Use
ios-networkingfor URLSession, request pipelines, ATS configuration, retries, caching, reachability, and transport architecture. - Use
app-store-reviewfor privacy manifests, ATT, App Review guideline compliance, and submission readiness.
This skill may mention those areas only to identify a security handoff.
Review Checklist
Use this checklist for code reviews and migration plans. Mark each item pass, fail, or not applicable; for each failure, cite the reference file and severity.
- Secrets are not stored in
UserDefaults, plists, source, logs, files, or archives. - Every
SecItem*call checksOSStatusand handles common recoverable errors. - Biometric access to secrets is keychain-bound with
SecAccessControl, not a standaloneBoolfromLAContext.evaluatePolicy(). - Keychain add dictionaries set an explicit accessibility policy.
- Keychain writes use add-or-update rather than delete-then-add.
- Keychain work is isolated from UI/main-actor code.
- The selected
kSecClassmatches the item type and primary-key attributes. - CryptoKit code avoids nonce reuse, raw shared-secret use, weak hashes, and hardcoded keys.
More from dpearson2699
Use this skill in a project
Open any RiverX project, search the Skills panel for dpearson2699/swift-ios-skills/swift-security, and install it in one click.
