|
| 1 | +--- |
| 2 | +UID: NF:bcrypt.BCryptDecapsulate |
| 3 | +title: BCryptDecapsulate function (bcrypt.h) |
| 4 | +description: Performs the Decapsulation operation of a KEM algorithm, decrypting a ciphertext and returning the shared secret key. |
| 5 | +helpviewer_keywords: ["BCryptDecapsulate","BCryptDecapsulate function [Security]","bcrypt/BCryptDecapsulate","security.bcryptdecapsulate", "BCrypt KEM"] |
| 6 | +old-location: security\bcryptdecapsulate.htm |
| 7 | +tech.root: security |
| 8 | +ms.assetid: |
| 9 | +ms.date: 5/22/2025 |
| 10 | +ms.keywords: BCryptDecapsulate, BCryptDecapsulate function [Security], bcrypt/BCryptDecapsulate, security.bcryptdecapsulate, BCrypt KEM |
| 11 | +req.header: bcrypt.h |
| 12 | +req.include-header: |
| 13 | +req.target-type: Windows |
| 14 | +req.target-min-winverclnt: Windows 11 24H2 |
| 15 | +req.target-min-winversvr: Windows Server 2025 |
| 16 | +req.kmdf-ver: |
| 17 | +req.umdf-ver: |
| 18 | +req.ddi-compliance: |
| 19 | +req.unicode-ansi: |
| 20 | +req.idl: |
| 21 | +req.max-support: |
| 22 | +req.namespace: |
| 23 | +req.assembly: |
| 24 | +req.type-library: |
| 25 | +req.lib: Bcrypt.lib |
| 26 | +req.dll: Bcrypt.dll |
| 27 | +req.irql: |
| 28 | +targetos: Windows |
| 29 | +req.typenames: |
| 30 | +req.redist: |
| 31 | +ms.custom: 24H2 |
| 32 | +f1_keywords: |
| 33 | + - BCryptDecapsulate |
| 34 | + - bcrypt/BCryptDecapsulate |
| 35 | +dev_langs: |
| 36 | + - c++ |
| 37 | +topic_type: |
| 38 | + - APIRef |
| 39 | + - kbSyntax |
| 40 | +api_type: |
| 41 | + - DllExport |
| 42 | +api_location: |
| 43 | + - Bcrypt.dll |
| 44 | +api_name: |
| 45 | + - BCryptDecapsulate |
| 46 | +--- |
| 47 | + |
| 48 | +# BCryptDecapsulate function |
| 49 | + |
| 50 | +The **BCryptDecapsulate** function performs the Decapsulation operation of a Key Encapsulation Mechanism (KEM). |
| 51 | +It takes a KEM ciphertext and it decrypts with the provided private key, returning the shared secret key. |
| 52 | + |
| 53 | +If the ciphertext is correctly formatted but does not match the decapsulation key, this API will succeed but a random shared secret key will be generated. |
| 54 | + |
| 55 | +## Syntax |
| 56 | + |
| 57 | +```cpp |
| 58 | +NTSTATUS BCryptDecapsulate( |
| 59 | + _In_ BCRYPT_KEY_HANDLE hKey, |
| 60 | + _In_reads_bytes_(cbCipherText) |
| 61 | + PUCHAR pbCipherText, |
| 62 | + _In_ ULONG cbCipherText, |
| 63 | + _Out_writes_bytes_to_opt_(cbSecretKey, *pcbSecretKey) |
| 64 | + PUCHAR pbSecretKey, |
| 65 | + _In_ ULONG cbSecretKey, |
| 66 | + _Out_ ULONG *pcbSecretKey, |
| 67 | + _In_ ULONG dwFlags |
| 68 | +); |
| 69 | +``` |
| 70 | + |
| 71 | +## Parameters |
| 72 | + |
| 73 | +*hKey* `[in]` |
| 74 | + |
| 75 | +The handle of the key to use to decapsulate the KEM ciphertext. This must be the private (decapsulation) key which corresponds to the public (encapsulation) key used to produce the KEM ciphertext. The key handle is obtained from one of the keypair creation functions, such as [BCryptGenerateKeyPair](/windows/desktop/api/Bcrypt/nf-bcrypt-bcryptgeneratekeypair) or [BCryptImportKeyPair](/windows/desktop/api/Bcrypt/nf-bcrypt-bcryptimportkeypair). |
| 76 | + |
| 77 | +*pbCipherText* `[in]` |
| 78 | + |
| 79 | +A pointer to a buffer that contains the KEM ciphertext. The [BCryptEncapsulate](nf-bcrypt-bcryptencapsulate.md) function may be used to create a KEM ciphertext. |
| 80 | + |
| 81 | +*cbCipherText* `[in]` |
| 82 | + |
| 83 | +The size, in bytes, of the *pbCipherText* buffer. |
| 84 | + |
| 85 | +*pbSecretKey* `[out]` |
| 86 | + |
| 87 | +A pointer to a buffer that receives the shared secret key. See [remarks](#remarks) for more information. |
| 88 | + |
| 89 | +*cbSecretKey* `[in]` |
| 90 | + |
| 91 | +The size, in bytes, of the *pbSecretKey* buffer. |
| 92 | + |
| 93 | +*pcbSecretKey* `[out]` |
| 94 | + |
| 95 | +A pointer to a **ULONG** variable that the receives the number of bytes written to *pbSecretKey* buffer. |
| 96 | + |
| 97 | +If *pbSecretKey* is `NULL`, this receives the size, in bytes, required for the shared secret key. |
| 98 | +See [remarks](#remarks) for more information. |
| 99 | + |
| 100 | +*dwFlags* `[in]` |
| 101 | + |
| 102 | +Reserved, must be zero. |
| 103 | + |
| 104 | +## Return value |
| 105 | + |
| 106 | +Returns a status code that indicates the success or failure of the function. |
| 107 | + |
| 108 | +Possible return codes include, but are not limited to, the following. |
| 109 | + |
| 110 | +| Return Code | Description | |
| 111 | +|--|--| |
| 112 | +| `STATUS_SUCCESS` | The function was successful. | |
| 113 | +| `STATUS_INVALID_PARAMETER` | One or more required parameters (*hKey*, *pcbSecretKey*, *pbCipherText*) is `NULL`, or one of the parameters has an invalid value. | |
| 114 | +| `STATUS_INVALID_BUFFER_SIZE` | A buffer size (*cbSecretKey*, *cbCipherText*) does not match the expected size for the KEM parameters associated with the decapsulation key. | |
| 115 | +| `STATUS_BUFFER_TOO_SMALL` | An output buffer size (*cbSecretKey*) is too small for the result decapsulation operation for the KEM parameters associated with the decapsulation key. *pcbSecretKey* receives the number of bytes required for *pbSecretKey*. | |
| 116 | + |
| 117 | +## Remarks |
| 118 | + |
| 119 | +To query the required size of the *pbSecretKey* buffer needed for the KEM shared secret key, call **BCryptDecapsulate** with a `NULL` *pbSecretKey*. The required size will be returned in *pcbSecretKey*. This query is efficient and returns the size without performing the decapsulation. |
| 120 | +Equivalently, use [BCryptGetProperty](/windows/desktop/api/Bcrypt/nf-bcrypt-bcryptgetproperty) to query the **BCRYPT_KEM_SHARED_SECRET_LENGTH** property of the algorithm or key handle. |
| 121 | +For currently supported KEM algorithms (ML-KEM), the shared secret length is a constant size for a given algorithm. |
| 122 | + |
| 123 | +### Additional remarks |
| 124 | + |
| 125 | +Given an invalid, but correctly-sized, ciphertext, the ML-KEM decapsulation operation will *implicitly reject* the ciphertext by returning success in equal time to a valid decapsulation operation, with pseudo-random shared secret key output. This forces higher-level protocols to fail later when symmetric keys of peers don't match. So, decapsulate will only ever fail if there are programming errors (i.e. incorrect size, use of uninitialized *hKey*), or something fundamentally goes wrong with the environment (i.e. internal memory allocation fails, or and internal consistency test detects hardware error). |
| 126 | + |
| 127 | +## Requirements |
| 128 | + |
| 129 | +| Requirement | Value | |
| 130 | +| ---- | ---- | |
| 131 | +| **Minimum supported client** | **Windows 11 24H2:** Support for ML-KEM begins. [desktop apps only] | |
| 132 | +| **Minimum supported server** | **Windows Server 2025:** Support for ML-KEM begins. [desktop apps only] | |
| 133 | +| **Library** | `Bcrypt.lib` | |
| 134 | +| **DLL** | `Bcrypt.dll` | |
0 commit comments