<!--
{
  "availability" : [
    "macOS: 10.7.0 - 12.0.0"
  ],
  "documentType" : "symbol",
  "framework" : "Security",
  "identifier" : "/documentation/Security/SecKeyGenerateSymmetric(_:_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Function",
  "symbol" : {
    "kind" : "Function",
    "modules" : [
      "Security"
    ],
    "preciseIdentifier" : "c:@F@SecKeyGenerateSymmetric"
  },
  "title" : "SecKeyGenerateSymmetric(_:_:)"
}
-->

# SecKeyGenerateSymmetric(_:_:)

Generates a random symmetric key.

```
func SecKeyGenerateSymmetric(_ parameters: CFDictionary, _ error: UnsafeMutablePointer<Unmanaged<CFError>?>?) -> SecKey?
```

## Parameters

`parameters`

A key generation parameter dictionary. At minimum, this must contain [`kSecAttrKeyType`](/documentation/Security/kSecAttrKeyType) and [`kSecAttrKeySizeInBits`](/documentation/Security/kSecAttrKeySizeInBits). In addition, this function assumes default values for the following keys:

- [`kSecAttrLabel`](/documentation/Security/kSecAttrLabel) defaults to `NULL`.
- [`kSecAttrIsPermanent`](/documentation/Security/kSecAttrIsPermanent) if this key is present and has a value of <doc://com.apple.documentation/documentation/CoreFoundation/kCFBooleanTrue>, the key or key pair will be added to the default keychain.
- [`kSecAttrApplicationTag`](/documentation/Security/kSecAttrApplicationTag) defaults to `NULL`.
- [`kSecAttrEffectiveKeySize`](/documentation/Security/kSecAttrEffectiveKeySize) defaults to `NULL`, which means the effective key size is the same as the key size ([`kSecAttrKeySizeInBits`](/documentation/Security/kSecAttrKeySizeInBits)).
- [`kSecAttrCanEncrypt`](/documentation/Security/kSecAttrCanEncrypt) defaults to <doc://com.apple.documentation/documentation/CoreFoundation/kCFBooleanFalse> for private keys, <doc://com.apple.documentation/documentation/CoreFoundation/kCFBooleanTrue> for public keys.
- [`kSecAttrCanDecrypt`](/documentation/Security/kSecAttrCanDecrypt) defaults to <doc://com.apple.documentation/documentation/CoreFoundation/kCFBooleanTrue> for private keys, <doc://com.apple.documentation/documentation/CoreFoundation/kCFBooleanFalse> for public keys.
- [`kSecAttrCanDerive`](/documentation/Security/kSecAttrCanDerive) defaults to <doc://com.apple.documentation/documentation/CoreFoundation/kCFBooleanTrue>.
- [`kSecAttrCanSign`](/documentation/Security/kSecAttrCanSign) defaults to <doc://com.apple.documentation/documentation/CoreFoundation/kCFBooleanTrue> for private keys, <doc://com.apple.documentation/documentation/CoreFoundation/kCFBooleanFalse> for public keys.
- [`kSecAttrCanVerify`](/documentation/Security/kSecAttrCanVerify) defaults to <doc://com.apple.documentation/documentation/CoreFoundation/kCFBooleanFalse> for private keys, <doc://com.apple.documentation/documentation/CoreFoundation/kCFBooleanTrue> for public keys.
- [`kSecAttrCanWrap`](/documentation/Security/kSecAttrCanWrap) defaults to <doc://com.apple.documentation/documentation/CoreFoundation/kCFBooleanFalse> for private keys, <doc://com.apple.documentation/documentation/CoreFoundation/kCFBooleanTrue> for public keys.
- [`kSecAttrCanUnwrap`](/documentation/Security/kSecAttrCanUnwrap) defaults to <doc://com.apple.documentation/documentation/CoreFoundation/kCFBooleanTrue> for private keys, <doc://com.apple.documentation/documentation/CoreFoundation/kCFBooleanFalse> for public keys.

    These default values can be overridden by adding a value for the associated key in the parameter dictionary.

    When used as a replacement for [`SecKeyGenerate`](/documentation/Security/SecKeyGenerate)    , set the [`kSecUseKeychain`](/documentation/Security/kSecUseKeychain)     key to the keychain ([`SecKeychain`](/documentation/Security/SecKeychain)    ) into which the key should be stored, [`kSecAttrLabel`](/documentation/Security/kSecAttrLabel)     to a user-visible label for the key, and [`kSecAttrApplicationLabel`](/documentation/Security/kSecAttrApplicationLabel)     to an identifier defined by your application, for subsequent use in calls to [`SecItemCopyMatching(_:_:)`](/documentation/Security/SecItemCopyMatching(_:_:))    . Additionally, you can specify keychain access controls for the key by setting [`kSecAttrAccess`](/documentation/Security/kSecAttrAccess)     to a [`SecAccess`](/documentation/Security/SecAccess)     object.

`error`

A pointer to a <doc://com.apple.documentation/documentation/CoreFoundation/CFError> variable where an error object is stored upon failure. If not `NULL`, the caller is responsible for checking this variable and releasing the resulting object if it exists.

## Return Value

A newly generated symmetric key, or `NULL` on failure. In Objective-C, call the <doc://com.apple.documentation/documentation/CoreFoundation/CFRelease> function to free the key’s memory when you are done with it.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)