---
title: "SecKeyCreateRandomKey(_:_:)"
framework: security
role: symbol
role_heading: Function
path: "security/seckeycreaterandomkey(_:_:)"
---

# SecKeyCreateRandomKey(_:_:)

Generates a new public-private key pair.

## Declaration

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

## Parameters

- `parameters`: A dictionary you use to specify the attributes of the generated keys. See doc://com.apple.security/documentation/Security/key-generation-attributes for details.
- `error`: An error reference pointer that doc://com.apple.security/documentation/Security/SecKeyCreateRandomKey(_:_:) populates with a suitable error instance on failure.

## Mentioned in

Generating New Cryptographic Keys Protecting keys with the Secure Enclave

## Return Value

Return Value The newly generated private key, or NULL on failure. In Objective-C, call the CFRelease function to free the key when you are done with it.

## Discussion

Discussion To get the associated public key, use SecKeyCopyPublicKey(_:). SecKeyCreateRandomKey(_:_:) fails and returns errSecInteractionNotAllowed if you call it in the background on iPhone or iPad while the device is locked.
