2  * Copyright (c) 2002-2014 Apple Inc. All Rights Reserved. 
   4  * @APPLE_LICENSE_HEADER_START@ 
   6  * This file contains Original Code and/or Modifications of Original Code 
   7  * as defined in and that are subject to the Apple Public Source License 
   8  * Version 2.0 (the 'License'). You may not use this file except in 
   9  * compliance with the License. Please obtain a copy of the License at 
  10  * http://www.opensource.apple.com/apsl/ and read it before using this 
  13  * The Original Code and all software distributed under the License are 
  14  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 
  15  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 
  16  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 
  17  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 
  18  * Please see the License for the specific language governing rights and 
  19  * limitations under the License. 
  21  * @APPLE_LICENSE_HEADER_END@ 
  26         The functions provided in SecKey.h implement and manage a particular 
  27     type of keychain item that represents a key.  A key can be stored in a 
  28     keychain, but a key can also be a transient object. 
  30         You can use a key as a keychain item in most functions. 
  33 #ifndef _SECURITY_SECKEY_H_ 
  34 #define _SECURITY_SECKEY_H_ 
  36 #include <dispatch/dispatch.h> 
  37 #include <Security/SecBase.h> 
  38 #include <Security/SecAccess.h> 
  39 #include <Security/cssmtype.h> 
  40 #include <CoreFoundation/CFBase.h> 
  41 #include <CoreFoundation/CFDictionary.h> 
  42 #include <sys/types.h> 
  44 #if defined(__cplusplus) 
  48 CF_ASSUME_NONNULL_BEGIN
 
  49 CF_IMPLICIT_BRIDGING_ENABLED
 
  52         @enum KeyItemAttributeConstants 
  53         @abstract Specifies keychain item attributes for keys. 
  54     @constant kSecKeyKeyClass type uint32 (CSSM_KEYCLASS), value 
  55     is one of CSSM_KEYCLASS_PUBLIC_KEY, CSSM_KEYCLASS_PRIVATE_KEY 
  56     or CSSM_KEYCLASS_SESSION_KEY. 
  57     @constant kSecKeyPrintName type blob, human readable name of 
  58     the key.  Same as kSecLabelItemAttr for normal keychain items. 
  59     @constant kSecKeyAlias type blob, currently unused. 
  60     @constant kSecKeyPermanent type uint32, value is nonzero iff 
  61     this key is permanent (stored in some keychain).  This is always 
  63     @constant kSecKeyPrivate type uint32, value is nonzero iff this 
  64     key is protected by a user login or a password, or both. 
  65     @constant kSecKeyModifiable type uint32, value is nonzero iff 
  66     attributes of this key can be modified. 
  67     @constant kSecKeyLabel type blob, for private and public keys 
  68     this contains the hash of the public key.  This is used to 
  69     associate certificates and keys.  Its value matches the value 
  70     of the kSecPublicKeyHashItemAttr of a certificate and it's used 
  71     to construct an identity from a certificate and a key. 
  72     For symmetric keys this is whatever the creator of the key 
  73     passed in during the generate key call. 
  74     @constant kSecKeyApplicationTag type blob, currently unused. 
  75     @constant kSecKeyKeyCreator type data, the data points to a 
  76     CSSM_GUID structure representing the moduleid of the csp owning 
  78     @constant kSecKeyKeyType type uint32, value is a CSSM_ALGORITHMS 
  79     representing the algorithm associated with this key. 
  80     @constant kSecKeyKeySizeInBits type uint32, value is the number 
  82     @constant kSecKeyEffectiveKeySize type uint32, value is the 
  83     effective number of bits in this key.  For example a des key 
  84     has a kSecKeyKeySizeInBits of 64 but a kSecKeyEffectiveKeySize 
  86     @constant kSecKeyStartDate type CSSM_DATE.  Earliest date from 
  87     which this key may be used.  If the value is all zeros or not 
  88     present, no restriction applies. 
  89     @constant kSecKeyEndDate type CSSM_DATE.  Latest date at 
  90     which this key may be used.  If the value is all zeros or not 
  91     present, no restriction applies. 
  92     @constant kSecKeySensitive type uint32, iff value is nonzero 
  93     this key cannot be wrapped with CSSM_ALGID_NONE. 
  94     @constant kSecKeyAlwaysSensitive type uint32, value is nonzero 
  95     iff this key has always been marked sensitive. 
  96     @constant kSecKeyExtractable type uint32, value is nonzero iff 
  97     this key can be wrapped. 
  98     @constant kSecKeyNeverExtractable type uint32, value is nonzero 
  99     iff this key was never marked extractable. 
 100     @constant kSecKeyEncrypt type uint32, value is nonzero iff this 
 101     key can be used in an encrypt operation. 
 102     @constant kSecKeyDecrypt type uint32, value is nonzero iff this 
 103     key can be used in a decrypt operation. 
 104     @constant kSecKeyDerive type uint32, value is nonzero iff this 
 105     key can be used in a deriveKey operation. 
 106     @constant kSecKeySign type uint32, value is nonzero iff this 
 107     key can be used in a sign operation. 
 108     @constant kSecKeyVerify type uint32, value is nonzero iff this 
 109     key can be used in a verify operation. 
 110     @constant kSecKeySignRecover type uint32. 
 111     @constant kSecKeyVerifyRecover type uint32. 
 112     key can unwrap other keys. 
 113     @constant kSecKeyWrap type uint32, value is nonzero iff this 
 114     key can wrap other keys. 
 115     @constant kSecKeyUnwrap type uint32, value is nonzero iff this 
 116     key can unwrap other keys. 
 118         The use of these enumerations has been deprecated.  Please 
 119         use the equivalent items defined in SecItem.h 
 125     kSecKeyPrintName 
=         1, 
 127     kSecKeyPermanent 
=         3, 
 129     kSecKeyModifiable 
=        5, 
 131     kSecKeyApplicationTag 
=    7, 
 132     kSecKeyKeyCreator 
=        8, 
 134     kSecKeyKeySizeInBits 
=    10, 
 135     kSecKeyEffectiveKeySize 
= 11, 
 136     kSecKeyStartDate 
=        12, 
 138     kSecKeySensitive 
=        14, 
 139     kSecKeyAlwaysSensitive 
=  15, 
 140     kSecKeyExtractable 
=      16, 
 141     kSecKeyNeverExtractable 
= 17, 
 147     kSecKeySignRecover 
=      23, 
 148     kSecKeyVerifyRecover 
=    24, 
 154     @enum SecCredentialType 
 155     @abstract Determines the type of credential returned by SecKeyGetCredentials. 
 156     @constant kSecCredentialTypeWithUI Operations with this key are allowed to present UI if required. 
 157     @constant kSecCredentialTypeNoUI Operations with this key are not allowed to present UI, and will fail if UI is required. 
 158     @constant kSecCredentialTypeDefault The default setting for determining whether to present UI is used. This setting can be changed with a call to SecKeychainSetUserInteractionAllowed. 
 160 typedef CF_ENUM(uint32
, SecCredentialType
) 
 162         kSecCredentialTypeDefault 
= 0, 
 163         kSecCredentialTypeWithUI
, 
 164         kSecCredentialTypeNoUI
 
 169     @abstract Supported padding types. 
 171 typedef CF_ENUM(uint32_t, SecPadding
) 
 174     kSecPaddingPKCS1     
= 1, 
 176     /* For SecKeyRawSign/SecKeyRawVerify only, 
 177      ECDSA signature is raw byte format {r,s}, big endian. 
 178      First half is r, second half is s */ 
 179     kSecPaddingSigRaw  
= 0x4000, 
 181     /* For SecKeyRawSign/SecKeyRawVerify only, data to be signed is an MD2 
 182        hash; standard ASN.1 padding will be done, as well as PKCS1 padding 
 183        of the underlying RSA operation. */ 
 184     kSecPaddingPKCS1MD2  
= 0x8000, 
 186     /* For SecKeyRawSign/SecKeyRawVerify only, data to be signed is an MD5 
 187        hash; standard ASN.1 padding will be done, as well as PKCS1 padding 
 188        of the underlying RSA operation. */ 
 189     kSecPaddingPKCS1MD5  
= 0x8001, 
 191     /* For SecKeyRawSign/SecKeyRawVerify only, data to be signed is a SHA1 
 192        hash; standard ASN.1 padding will be done, as well as PKCS1 padding 
 193        of the underlying RSA operation. */ 
 194     kSecPaddingPKCS1SHA1 
= 0x8002, 
 199     @abstract Supported key lengths. 
 201 typedef CF_ENUM(uint32_t, SecKeySizes
) 
 203     kSecDefaultKeySize  
= 0, 
 205     // Symmetric Keysizes - default is currently kSecAES128 for AES. 
 211     // Supported ECC Keys for Suite-B from RFC 4492 section 5.1.1. 
 212     // default is currently kSecp256r1 
 216     kSecp521r1          
= 521,  // Yes, 521 
 218     // Boundaries for RSA KeySizes - default is currently 2048 
 219     // RSA keysizes must be multiples of 8 
 225         @enum Key Parameter Constants 
 226         @discussion Predefined key constants used to get or set values in a dictionary. 
 227         These are used to provide explicit parameters to key generation functions 
 228         when non-default values are desired. See the description of the 
 229         SecKeyGeneratePair API for usage information. 
 230         @constant kSecPrivateKeyAttrs The value for this key is a CFDictionaryRef 
 231          containing attributes specific for the private key to be generated. 
 232         @constant kSecPublicKeyAttrs The value for this key is a CFDictionaryRef 
 233          containing attributes specific for the public key to be generated. 
 235 extern const CFStringRef kSecPrivateKeyAttrs
 
 236     __OSX_AVAILABLE_STARTING(__MAC_10_8
, __IPHONE_2_0
); 
 237 extern const CFStringRef kSecPublicKeyAttrs
 
 238     __OSX_AVAILABLE_STARTING(__MAC_10_8
, __IPHONE_2_0
); 
 242         @function SecKeyGetTypeID 
 243         @abstract Returns the type identifier of SecKey instances. 
 244         @result The CFTypeID of SecKey instances. 
 246 CFTypeID 
SecKeyGetTypeID(void) 
 247         __OSX_AVAILABLE_STARTING(__MAC_10_3
, __IPHONE_2_0
); 
 250         @function SecKeyCreatePair 
 251         @abstract Creates an asymmetric key pair and stores it in a specified keychain. 
 252         @param keychainRef A reference to the keychain in which to store the private and public key items. Specify NULL for the default keychain. 
 253         @param algorithm An algorithm for the key pair. This parameter is ignored if a valid (non-zero) contextHandle is supplied. 
 254         @param keySizeInBits A key size for the key pair. This parameter is ignored if a valid (non-zero) contextHandle is supplied. 
 255         @param contextHandle (optional) A CSSM_CC_HANDLE, or 0. If this argument is supplied, the algorithm and keySizeInBits parameters are ignored. If extra parameters are needed to generate a key (some algorithms require this), you should create a context using CSSM_CSP_CreateKeyGenContext, using the CSPHandle obtained by calling SecKeychainGetCSPHandle. Then use CSSM_UpdateContextAttributes to add parameters, and dispose of the context using CSSM_DeleteContext after calling this function. 
 256         @param publicKeyUsage A bit mask indicating all permitted uses for the new public key. CSSM_KEYUSE bit mask values are defined in cssmtype.h. 
 257         @param publicKeyAttr A bit mask defining attribute values for the new public key. The bit mask values are equivalent to a CSSM_KEYATTR_FLAGS and are defined in cssmtype.h. 
 258         @param privateKeyUsage A bit mask indicating all permitted uses for the new private key. CSSM_KEYUSE bit mask values are defined in cssmtype.h. 
 259         @param privateKeyAttr A bit mask defining attribute values for the new private key. The bit mask values are equivalent to a CSSM_KEYATTR_FLAGS and are defined in cssmtype.h. 
 260         @param initialAccess (optional) A SecAccess object that determines the initial access rights to the private key. The public key is given "any/any" access rights by default. 
 261         @param publicKey (optional) On return, the keychain item reference of the generated public key. Use the SecKeyGetCSSMKey function to obtain the CSSM_KEY. The caller must call CFRelease on this value if it is returned. Pass NULL if a reference to this key is not required. 
 262         @param privateKey (optional) On return, the keychain item reference of the generated private key. Use the SecKeyGetCSSMKey function to obtain the CSSM_KEY. The caller must call CFRelease on this value if it is returned. Pass NULL if a reference to this key is not required. 
 263         @result A result code. See "Security Error Codes" (SecBase.h). 
 264         @discussion This API is deprecated for 10.7. Please use the SecKeyGeneratePair API instead. 
 266 OSStatus 
SecKeyCreatePair( 
 267         SecKeychainRef __nullable keychainRef
, 
 268         CSSM_ALGORITHMS algorithm
, 
 269         uint32 keySizeInBits
, 
 270         CSSM_CC_HANDLE contextHandle
, 
 271         CSSM_KEYUSE publicKeyUsage
, 
 272         uint32 publicKeyAttr
, 
 273         CSSM_KEYUSE privateKeyUsage
, 
 274         uint32 privateKeyAttr
, 
 275         SecAccessRef __nullable initialAccess
, 
 276         SecKeyRef
* __nullable CF_RETURNS_RETAINED publicKey
, 
 277         SecKeyRef
* __nullable CF_RETURNS_RETAINED privateKey
) 
 278                 DEPRECATED_IN_MAC_OS_X_VERSION_10_7_AND_LATER
; 
 281         @function SecKeyGenerate 
 282         @abstract Creates a symmetric key and optionally stores it in a specified keychain. 
 283         @param keychainRef (optional) A reference to the keychain in which to store the generated key. Specify NULL to generate a transient key. 
 284         @param algorithm An algorithm for the symmetric key. This parameter is ignored if a valid (non-zero) contextHandle is supplied. 
 285         @param keySizeInBits A key size for the key pair. This parameter is ignored if a valid (non-zero) contextHandle is supplied. 
 286         @param contextHandle (optional) A CSSM_CC_HANDLE, or 0. If this argument is supplied, the algorithm and keySizeInBits parameters are ignored. If extra parameters are needed to generate a key (some algorithms require this), you should create a context using CSSM_CSP_CreateKeyGenContext, using the CSPHandle obtained by calling SecKeychainGetCSPHandle. Then use CSSM_UpdateContextAttributes to add parameters, and dispose of the context using CSSM_DeleteContext after calling this function. 
 287         @param keyUsage A bit mask indicating all permitted uses for the new key. CSSM_KEYUSE bit mask values are defined in cssmtype.h. 
 288         @param keyAttr A bit mask defining attribute values for the new key. The bit mask values are equivalent to a CSSM_KEYATTR_FLAGS and are defined in cssmtype.h. 
 289         @param initialAccess (optional) A SecAccess object that determines the initial access rights for the key. This parameter is ignored if the keychainRef is NULL. 
 290         @param keyRef On return, a reference to the generated key. Use the SecKeyGetCSSMKey function to obtain the CSSM_KEY. The caller must call CFRelease on this value if it is returned. 
 291         @result A result code.  See "Security Error Codes" (SecBase.h). 
 292         @discussion This API is deprecated for 10.7.  Please use the SecKeyGenerateSymmetric API instead. 
 294 OSStatus 
SecKeyGenerate( 
 295         SecKeychainRef __nullable keychainRef
, 
 296         CSSM_ALGORITHMS algorithm
, 
 297         uint32 keySizeInBits
, 
 298         CSSM_CC_HANDLE contextHandle
, 
 299         CSSM_KEYUSE keyUsage
, 
 301         SecAccessRef __nullable initialAccess
, 
 302         SecKeyRef
* __nullable CF_RETURNS_RETAINED keyRef
) 
 303                 DEPRECATED_IN_MAC_OS_X_VERSION_10_7_AND_LATER
; 
 306     @function SecKeyGetCSSMKey 
 307     @abstract Returns a pointer to the CSSM_KEY for the given key item reference. 
 308     @param key A keychain key item reference. The key item must be of class type kSecPublicKeyItemClass, kSecPrivateKeyItemClass, or kSecSymmetricKeyItemClass. 
 309     @param cssmKey On return, a pointer to a CSSM_KEY structure for the given key. This pointer remains valid until the key reference is released. The caller should not attempt to modify or free this data. 
 310     @result A result code. See "Security Error Codes" (SecBase.h). 
 311     @discussion  The CSSM_KEY is valid until the key item reference is released. This API is deprecated in 10.7. Its use should no longer be needed. 
 313 OSStatus 
SecKeyGetCSSMKey(SecKeyRef key
, const CSSM_KEY 
* __nullable 
* __nonnull cssmKey
) 
 314         DEPRECATED_IN_MAC_OS_X_VERSION_10_7_AND_LATER
;; 
 317     @function SecKeyGetCSPHandle 
 318     @abstract Returns the CSSM_CSP_HANDLE for the given key reference. The handle is valid until the key reference is released. 
 319     @param keyRef A key reference. 
 320     @param cspHandle On return, the CSSM_CSP_HANDLE for the given keychain. 
 321     @result A result code. See "Security Error Codes" (SecBase.h). 
 322         @discussion This API is deprecated in 10.7. Its use should no longer be needed. 
 324 OSStatus 
SecKeyGetCSPHandle(SecKeyRef keyRef
, CSSM_CSP_HANDLE 
*cspHandle
) 
 325         DEPRECATED_IN_MAC_OS_X_VERSION_10_7_AND_LATER
; 
 328     @function SecKeyGetCredentials 
 329     @abstract For a given key, return a pointer to a CSSM_ACCESS_CREDENTIALS structure which will allow the key to be used. 
 330     @param keyRef The key for which a credential is requested. 
 331     @param operation The type of operation to be performed with this key. See "Authorization tag type" for defined operations (cssmtype.h). 
 332     @param credentialType The type of credential requested. 
 333     @param outCredentials On return, a pointer to a CSSM_ACCESS_CREDENTIALS structure. This pointer remains valid until the key reference is released. The caller should not attempt to modify or free this data. 
 334     @result A result code. See "Security Error Codes" (SecBase.h). 
 336 OSStatus 
SecKeyGetCredentials( 
 338         CSSM_ACL_AUTHORIZATION_TAG operation
, 
 339         SecCredentialType credentialType
, 
 340         const CSSM_ACCESS_CREDENTIALS 
* __nullable 
* __nonnull outCredentials
) 
 341                 DEPRECATED_IN_MAC_OS_X_VERSION_10_7_AND_LATER
; 
 344     @function SecKeyGetBlockSize 
 345     @abstract Decrypt a block of ciphertext. 
 346     @param key The key for which the block length is requested. 
 347     @result The block length of the key in bytes. 
 348     @discussion If for example key is an RSA key the value returned by 
 349     this function is the size of the modulus. 
 351 size_t SecKeyGetBlockSize(SecKeyRef key
) 
 352         __OSX_AVAILABLE_STARTING(__MAC_10_6
, __IPHONE_2_0
); 
 355  @function      SecKeyGenerateSymmetric 
 356  @abstract      Generates a random symmetric key with the specified length 
 359  @param parameters A dictionary containing one or more key-value pairs. 
 360  See the discussion sections below for a complete overview of options. 
 361  @param error An optional pointer to a CFErrorRef. This value is set 
 362  if an error occurred. If not NULL, the caller is responsible for 
 363  releasing the CFErrorRef. 
 364  @result On return, a SecKeyRef reference to the symmetric key, or 
 365  NULL if the key could not be created. 
 367  @discussion In order to generate a symmetric key, the parameters dictionary 
 368  must at least contain the following keys: 
 370  * kSecAttrKeyType with a value of kSecAttrKeyTypeAES or any other 
 371  kSecAttrKeyType defined in SecItem.h 
 372  * kSecAttrKeySizeInBits with a value being a CFNumberRef containing 
 373  the requested key size in bits.  Example sizes for AES keys are: 
 376  To store the generated symmetric key in a keychain, set these keys: 
 377  * kSecUseKeychain (value is a SecKeychainRef) 
 378  * kSecAttrLabel (a user-visible label whose value is a CFStringRef, 
 379  e.g. "My App's Encryption Key") 
 380  * kSecAttrApplicationLabel (a label defined by your application, whose 
 381  value is a CFStringRef and which can be used to find this key in a 
 382  subsequent call to SecItemCopyMatching, e.g. "ID-1234567890-9876-0151") 
 384  To specify the generated key's access control settings, set this key: 
 385  * kSecAttrAccess (value is a SecAccessRef) 
 387  The keys below may be optionally set in the parameters dictionary 
 388  (with a CFBooleanRef value) to override the default usage values: 
 390  * kSecAttrCanEncrypt (defaults to true if not explicitly specified) 
 391  * kSecAttrCanDecrypt (defaults to true if not explicitly specified) 
 392  * kSecAttrCanWrap (defaults to true if not explicitly specified) 
 393  * kSecAttrCanUnwrap (defaults to true if not explicitly specified) 
 397 SecKeyRef 
SecKeyGenerateSymmetric(CFDictionaryRef parameters
, CFErrorRef 
*error
) 
 398         __OSX_AVAILABLE_STARTING(__MAC_10_7
, __IPHONE_NA
); 
 402  @function SecKeyCreateFromData 
 403  @abstract Creates a symmetric key with the given data and sets the 
 404  algorithm type specified. 
 406  @param parameters A dictionary containing one or more key-value pairs. 
 407  See the discussion sections below for a complete overview of options. 
 408  @result On return, a SecKeyRef reference to the symmetric key. 
 410  @discussion In order to generate a symmetric key the parameters dictionary must 
 411  at least contain the following keys: 
 413  * kSecAttrKeyType with a value of kSecAttrKeyTypeAES or any other 
 414  kSecAttrKeyType defined in SecItem.h 
 416  The keys below may be optionally set in the parameters dictionary 
 417  (with a CFBooleanRef value) to override the default usage values: 
 419  * kSecAttrCanEncrypt (defaults to true if not explicitly specified) 
 420  * kSecAttrCanDecrypt (defaults to true if not explicitly specified) 
 421  * kSecAttrCanWrap (defaults to true if not explicitly specified) 
 422  * kSecAttrCanUnwrap (defaults to true if not explicitly specified) 
 426 SecKeyRef 
SecKeyCreateFromData(CFDictionaryRef parameters
, 
 427         CFDataRef keyData
, CFErrorRef 
*error
) 
 428         __OSX_AVAILABLE_STARTING(__MAC_10_7
, __IPHONE_NA
); 
 432  @function SecKeyGeneratePair 
 433  @abstract Generate a private/public keypair. 
 434  @param parameters A dictionary containing one or more key-value pairs. 
 435  @result A result code. See "Security Error Codes" (SecBase.h). On success, 
 436  the result code will be errSecSuccess, and the output parameters will 
 437  contain the public SecKeyRef and private SecKeyRef. It is the caller's 
 438  responsibility to CFRelease these key references when finished with them. 
 440  @discussion In order to generate a keypair the parameters dictionary must 
 441  at least contain the following keys: 
 443  * kSecAttrKeyType with a value of kSecAttrKeyTypeRSA or any other 
 444  kSecAttrKeyType defined in SecItem.h 
 445  * kSecAttrKeySizeInBits with a value being a CFNumberRef containing 
 446  the requested key size in bits.  Example sizes for RSA keys are: 
 447  512, 768, 1024, 2048. 
 449  The values below may be set either in the top-level dictionary or in a 
 450  dictionary that is the value of the kSecPrivateKeyAttrs or 
 451  kSecPublicKeyAttrs key in the top-level dictionary.  Setting these 
 452  attributes explicitly will override the defaults below.  See SecItem.h 
 453  for detailed information on these attributes including the types of 
 456  * kSecAttrLabel default NULL 
 457  * kSecUseKeychain default NULL, which specifies the default keychain 
 458  * kSecAttrApplicationTag default NULL 
 459  * kSecAttrEffectiveKeySize default NULL same as kSecAttrKeySizeInBits 
 460  * kSecAttrCanEncrypt default false for private keys, true for public keys 
 461  * kSecAttrCanDecrypt default true for private keys, false for public keys 
 462  * kSecAttrCanDerive default true 
 463  * kSecAttrCanSign default true for private keys, false for public keys 
 464  * kSecAttrCanVerify default false for private keys, true for public keys 
 465  * kSecAttrCanWrap default false for private keys, true for public keys 
 466  * kSecAttrCanUnwrap default true for private keys, false for public keys 
 469 OSStatus 
SecKeyGeneratePair(CFDictionaryRef parameters
, 
 470         SecKeyRef 
* __nullable CF_RETURNS_RETAINED publicKey
, SecKeyRef 
* __nullable CF_RETURNS_RETAINED privateKey
) 
 471         __OSX_AVAILABLE_STARTING(__MAC_10_7
, __IPHONE_2_0
); 
 474  @typedef SecKeyGeneratePairBlock 
 475  @abstract Delivers the result from an asynchronous key pair generation. 
 476  @param publicKey - the public key generated.   You must retain publicKey if you wish to use it after your block returns. 
 477  @param privateKey - the private key generated.  You must retain publicKey if you wish to use it after your block returns. 
 478  @param error - Any errors returned.   You must retain error if you wish to use it after your block returns. 
 482 typedef void (^SecKeyGeneratePairBlock
)(SecKeyRef publicKey
, SecKeyRef privateKey
,  CFErrorRef error
); 
 486  @function SecKeyGeneratePairAsync 
 487  @abstract Generate a private/public keypair returning the values in a callback. 
 488  @param parameters A dictionary containing one or more key-value pairs. 
 489  @param deliveryQueue A dispatch queue to be used to deliver the results. 
 490  @param result A callback function to result when the operation has completed. 
 491  @result On success the function returns NULL. 
 493  @discussion In order to generate a keypair the parameters dictionary must 
 494  at least contain the following keys: 
 496  * kSecAttrKeyType with a value being kSecAttrKeyTypeRSA or any other 
 497  kSecAttrKeyType defined in SecItem.h 
 498  * kSecAttrKeySizeInBits with a value being a CFNumberRef or CFStringRef 
 499  containing the requested key size in bits.  Example sizes for RSA 
 500  keys are: 512, 768, 1024, 2048. 
 502  Setting the following attributes explicitly will override the defaults below. 
 503  See SecItem.h for detailed information on these attributes including the types 
 506  * kSecAttrLabel default NULL 
 507  * kSecAttrIsPermanent if this key is present and has a Boolean 
 508  value of true, the key or key pair will be added to the default 
 510  * kSecAttrApplicationTag default NULL 
 511  * kSecAttrEffectiveKeySize default NULL same as kSecAttrKeySizeInBits 
 512  * kSecAttrCanEncrypt default false for private keys, true for public keys 
 513  * kSecAttrCanDecrypt default true for private keys, false for public keys 
 514  * kSecAttrCanDerive default true 
 515  * kSecAttrCanSign default true for private keys, false for public keys 
 516  * kSecAttrCanVerify default false for private keys, true for public keys 
 517  * kSecAttrCanWrap default false for private keys, true for public keys 
 518  * kSecAttrCanUnwrap default true for private keys, false for public keys 
 521 void SecKeyGeneratePairAsync(CFDictionaryRef parameters
, 
 522         dispatch_queue_t deliveryQueue
, SecKeyGeneratePairBlock result
) 
 523         __OSX_AVAILABLE_STARTING(__MAC_10_7
, __IPHONE_NA
); 
 525 #endif /* __BLOCKS__ */ 
 527 // Derive, Wrap, and Unwrap 
 530  @function SecKeyDeriveFromPassword 
 531  @abstract Derives a symmetric key from a password. 
 533  @param password The password from which the keyis to be derived. 
 534  @param parameters A dictionary containing one or more key-value pairs. 
 535  @param error If the call fails this will contain the error code. 
 537  @discussion In order to derive a key the parameters dictionary must contain at least contain the following keys: 
 538  * kSecAttrSalt - a CFData for the salt value for mixing in the pseudo-random rounds. 
 539  * kSecAttrPRF - the algorithm to use for the pseudo-random-function. 
 540    If 0, this defaults to kSecAttrPRFHmacAlgSHA1. Possible values are: 
 542  * kSecAttrPRFHmacAlgSHA1 
 543  * kSecAttrPRFHmacAlgSHA224 
 544  * kSecAttrPRFHmacAlgSHA256 
 545  * kSecAttrPRFHmacAlgSHA384 
 546  * kSecAttrPRFHmacAlgSHA512 
 548  * kSecAttrRounds - the number of rounds to call the pseudo random function. 
 549    If 0, a count will be computed to average 1/10 of a second. 
 550  * kSecAttrKeySizeInBits with a value being a CFNumberRef 
 551    containing the requested key size in bits.  Example sizes for RSA keys are: 
 552    512, 768, 1024, 2048. 
 554  @result On success a SecKeyRef is returned.  On failure this result is NULL and the 
 555  error parameter contains the reason. 
 559 SecKeyRef 
SecKeyDeriveFromPassword(CFStringRef password
, 
 560         CFDictionaryRef parameters
, CFErrorRef 
*error
) 
 561         __OSX_AVAILABLE_STARTING(__MAC_10_7
, __IPHONE_NA
); 
 564  @function SecKeyWrapSymmetric 
 565  @abstract Wraps a symmetric key with a symmetric key. 
 567  @param keyToWrap The key which is to be wrapped. 
 568  @param wrappingKey The key wrapping key. 
 569  @param parameters The parameter list to use for wrapping the key. 
 570  @param error If the call fails this will contain the error code. 
 572  @result On success a CFDataRef is returned.  On failure this result is NULL and the 
 573  error parameter contains the reason. 
 575  @discussion In order to wrap a key the parameters dictionary may contain the following key: 
 576  * kSecSalt     - a CFData for the salt value for the encrypt. 
 580 CFDataRef 
SecKeyWrapSymmetric(SecKeyRef keyToWrap
, 
 581         SecKeyRef wrappingKey
, CFDictionaryRef parameters
, CFErrorRef 
*error
) 
 582         __OSX_AVAILABLE_STARTING(__MAC_10_7
, __IPHONE_NA
); 
 585  @function SecKeyUnwrapSymmetric 
 586  @abstract Unwrap a wrapped symmetric key. 
 588  @param keyToUnwrap The wrapped key to unwrap. 
 589  @param unwrappingKey The key unwrapping key. 
 590  @param parameters The parameter list to use for unwrapping the key. 
 591  @param error If the call fails this will contain the error code. 
 593  @result On success a SecKeyRef is returned.  On failure this result is NULL and the 
 594  error parameter contains the reason. 
 596  @discussion In order to unwrap a key the parameters dictionary may contain the following key: 
 597  * kSecSalt     - a CFData for the salt value for the decrypt. 
 601 SecKeyRef 
SecKeyUnwrapSymmetric(CFDataRef __nullable 
* __nonnull keyToUnwrap
, 
 602         SecKeyRef unwrappingKey
, CFDictionaryRef parameters
, CFErrorRef 
*error
) 
 603         __OSX_AVAILABLE_STARTING(__MAC_10_7
, __IPHONE_NA
); 
 605 CF_IMPLICIT_BRIDGING_DISABLED
 
 606 CF_ASSUME_NONNULL_END
 
 608 #if defined(__cplusplus) 
 612 #endif /* !_SECURITY_SECKEY_H_ */