---
title: "SecKeyGenerateSymmetric(_:_:)"
framework: security
role: symbol
role_heading: Function
path: "security/seckeygeneratesymmetric(_:_:)"
---

# SecKeyGenerateSymmetric(_:_:)

Generates a random symmetric key.

## Declaration

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

## Parameters

- `parameters`: A key generation parameter dictionary. At minimum, this must contain doc://com.apple.security/documentation/Security/kSecAttrKeyType and doc://com.apple.security/documentation/Security/kSecAttrKeySizeInBits. In addition, this function assumes default values for the following keys: doc://com.apple.security/documentation/Security/kSecAttrLabel defaults to NULL. doc://com.apple.security/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. doc://com.apple.security/documentation/Security/kSecAttrApplicationTag defaults to NULL. doc://com.apple.security/documentation/Security/kSecAttrEffectiveKeySize defaults to NULL, which means the effective key size is the same as the key size (doc://com.apple.security/documentation/Security/kSecAttrKeySizeInBits). doc://com.apple.security/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. doc://com.apple.security/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. doc://com.apple.security/documentation/Security/kSecAttrCanDerive defaults to doc://com.apple.documentation/documentation/CoreFoundation/kCFBooleanTrue. doc://com.apple.security/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. doc://com.apple.security/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. doc://com.apple.security/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. doc://com.apple.security/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 doc://com.apple.security/documentation/Security/SecKeyGenerate, set the doc://com.apple.security/documentation/Security/kSecUseKeychain key to the keychain (doc://com.apple.security/documentation/Security/SecKeychain) into which the key should be stored, doc://com.apple.security/documentation/Security/kSecAttrLabel to a user-visible label for the key, and doc://com.apple.security/documentation/Security/kSecAttrApplicationLabel to an identifier defined by your application, for subsequent use in calls to doc://com.apple.security/documentation/Security/SecItemCopyMatching(_:_:). Additionally, you can specify keychain access controls for the key by setting doc://com.apple.security/documentation/Security/kSecAttrAccess to a doc://com.apple.security/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

Return Value A newly generated symmetric key, or NULL on failure. In Objective-C, call the CFRelease function to free the key’s memory when you are done with it.
