BCryptGenerateSymmetricKey function

The BCryptGenerateSymmetricKey function creates a key object for use with a symmetrical key encryption algorithm from a supplied key.

Syntax


NTSTATUS WINAPI BCryptGenerateSymmetricKey(
  _Inout_    BCRYPT_ALG_HANDLE hAlgorithm,
  _Out_      BCRYPT_KEY_HANDLE *phKey,
  _Out_opt_  PUCHAR pbKeyObject,
  _In_       ULONG cbKeyObject,
  _In_       PUCHAR pbSecret,
  _In_       ULONG cbSecret,
  _In_       ULONG dwFlags
);

Parameters

hAlgorithm [in, out]

The handle of an algorithm provider created with the BCryptOpenAlgorithmProvider function. The algorithm specified when the provider was created must support symmetric key encryption.

phKey [out]

A pointer to a BCRYPT_KEY_HANDLE that receives the handle of the key. This handle is used in subsequent functions that require a key, such as BCryptEncrypt. This handle must be released when it is no longer needed by passing it to the BCryptDestroyKey function.

pbKeyObject [out, optional]

A pointer to a buffer that receives the key object. The cbKeyObject parameter contains the size of this buffer. The required size of this buffer can be obtained by calling the BCryptGetProperty function to get the BCRYPT_OBJECT_LENGTH property. This will provide the size of the key object for the specified algorithm.

This memory can only be freed after the phKey key handle is destroyed.

If the value of this parameter is NULL and the value of the cbKeyObject parameter is zero, the memory for the key object is allocated and freed by this function.

Windows 7:  This memory management functionality is available beginning with Windows 7.

cbKeyObject [in]

The size, in bytes, of the pbKeyObject buffer.

If the value of this parameter is zero and the value of the pbKeyObject parameter is NULL, the memory for the key object is allocated and freed by this function.

Windows 7:  This memory management functionality is available beginning with Windows 7.

pbSecret [in]

Pointer to a buffer that contains the key from which to create the key object. The cbSecret parameter contains the size of this buffer. This is normally a hash of a password or some other reproducible data. If the data passed in exceeds the target key size, the data will be truncated and the excess will be ignored.

Note  We strongly recommended that applications pass in the exact number of bytes required by the target key.

cbSecret [in]

The size, in bytes, of the pbSecret buffer.

dwFlags [in]

A set of flags that modify the behavior of this function. No flags are currently defined, so this parameter should be zero.

Return value

Returns a status code that indicates the success or failure of the function.

Possible return codes include, but are not limited to, the following.

Return codeDescription
STATUS_SUCCESS

The function was successful.

STATUS_BUFFER_TOO_SMALL

The size of the key object specified by the cbKeyObject parameter is not large enough to hold the key object.

STATUS_INVALID_HANDLE

The algorithm handle in the hAlgorithm parameter is not valid.

STATUS_INVALID_PARAMETER

One or more parameters are not valid.

 

Remarks

Depending on what processor modes a provider supports, BCryptGenerateSymmetricKey can be called either from user mode or kernel mode. Kernel mode callers can execute either at PASSIVE_LEVEL IRQL or DISPATCH_LEVEL IRQL. If the current IRQL level is DISPATCH_LEVEL, the handle provided in the hAlgorithm parameter must have been opened by using the BCRYPT_PROV_DISPATCH flag, and any pointers passed to the BCryptGenerateSymmetricKey function must refer to nonpaged (or locked) memory.

To call this function in kernel mode, use Cng.lib, which is part of the Driver Development Kit (DDK). For more information, see WDK and Developer Tools.

Windows Server 2008 and Windows Vista:  To call this function in kernel mode, use Ksecdd.lib.

Requirements

Minimum supported client

Windows Vista [desktop apps only]

Minimum supported server

Windows Server 2008 [desktop apps only]

Header

Bcrypt.h

Library

Bcrypt.lib

DLL

Bcrypt.dll

See also

BCryptDestroyKey

 

 

Community Additions

ADD
Show:
© 2015 Microsoft