CryptoSys API Library Manual

CIPHER_KeyUnwrap

Unwraps (decrypts) a content-encryption key with a key-encryption key.

VBA/VB6 Syntax

Public Declare Function CIPHER_KeyUnwrap Lib "diCryptoSys.dll" (ByRef lpOutput As Byte, ByVal nOutBytes As Long, ByRef lpData As Byte, ByVal nDataLen As Long, ByRef lpKek As Byte, ByVal nKekLen As Long, ByVal nOptions As Long) As Long

nRet = CIPHER_KeyUnwrap(lpOutput(0), nOutBytes, abInput(0), nDataLen, abKek(0), nKekLen, nOptions)

C/C++ Syntax

long __stdcall CIPHER_KeyUnwrap(unsigned char *lpOutput, long nOutBytes, const unsigned char *lpData, long nDataLen, const unsigned char *lpKEK, long nKekLen, long nOptions);

Parameters

lpOutput
[out] array of sufficient length to receive the output.
nOutBytes
[in] specifying the maximum length of the output array.
lpData
[in] array containing the input data (wrapped key).
nDataLen
[in] specifying the length of the input data in bytes.
lpKEK
[in] array containing the key encryption key.
nKekLen
[in] specifying the length of the key encryption key.
nOptions
[in] option flags:
Select the key wrap algorithm from one of the following:
API_BC_AES128 to use AES128-Wrap
API_BC_AES192 to use AES192-Wrap
API_BC_AES256 to use AES256-Wrap
API_BC_3DES to use cms3DESWrap

Returns (VBA/C)

If successful, the return value is the number of bytes in the output; otherwise it returns a negative error code.

VBA Wrapper Syntax

Public Function cipherKeyUnwrap(lpData() As Byte, lpKek() As Byte, nOptions As Long) As Byte()

.NET Equivalent

Cipher.KeyUnwrap Method

Python Equivalent

static Cipher.key_unwrap(data, kek, alg)

Remarks

This unwraps (decrypts) key material using a key encryption key (KEK) and uses either the AES Key Wrap Algorithm from RFC 3394 [AES-WRAP] or the Triple-DES Key Wrap algorithm from RFC 3217 [WRAP-3DES]. There is no default algorithm. The algorithm must be specified in the nOptions parameter. To find the required length for the output key material, pass zero as the nOutBytes parameter. This will be 8 bytes less than the input length for AES and 16 bytes less for triple DES. No parity bit checks or changes are made for a Triple-DES key.

Example (VBA core function)

Dim abWK() As Byte
Dim abKeyData() As Byte
Dim abKek() As Byte
Dim nWkLen As Long
Dim nKdLen As Long
Dim nKekLen As Long

abWK = cnvBytesFromHexStr("503D75C73630A7B02ECF51B9B29B907749310B77B0B2E054")
abKek = cnvBytesFromHexStr("c17a44e8 e28d7d64 81d1ddd5 0a3b8914")
nWkLen = UBound(abWK) + 1
nKekLen = UBound(abKek) + 1
Debug.Print "INPUT:"
Debug.Print "KEK=" & cnvHexStrFromBytes(abKek)
Debug.Print "WK =" & cnvHexStrFromBytes(abWK)
Debug.Print "ALG=AES-128"
   
nKdLen = CIPHER_KeyUnwrap(0, 0, abWK(0), nWkLen, abKek(0), nKekLen, API_BC_AES128)
If nKdLen <= 0 Then
    Debug.Print "CIPHER_KeyUnwrap returns " & nKdLen & ": " & apiErrorLookup(nKdLen)
    Exit Sub
End If
ReDim abKeyData(nKdLen - 1)
nKdLen = CIPHER_KeyUnwrap(abKeyData(0), nKdLen, abWK(0), nWkLen, abKek(0), nKekLen, API_BC_AES128)
Debug.Print "OUTPUT:"
Debug.Print "KD =" & cnvHexStrFromBytes(abKeyData)
Debug.Print "OK =00112233445566778899AABBCCDDEEFF"

This should result in output as follows:

INPUT:
KEK=C17A44E8E28D7D6481D1DDD50A3B8914
WK =503D75C73630A7B02ECF51B9B29B907749310B77B0B2E054
ALG=AES-128
OUTPUT:
KD =00112233445566778899AABBCCDDEEFF
OK =00112233445566778899AABBCCDDEEFF

Example (VBA wrapper function)

See example for VBA wrapper cipherKeyWrap in CIPHER_KeyWrap.

See Also

CIPHER_KeyWrap

[Contents] [Index]

[PREV: CIPHER_InitHex...]   [Contents]   [Index]   
   [NEXT: CIPHER_KeyWrap...]

Copyright © 2001-24 D.I. Management Services Pty Ltd. All rights reserved. Generated 2024-01-07T07:42:00Z.