CryptUnProtectData() returns ERROR_INSUFFICIENT_BUFFER(122) error on Windows 7 EMBEDDED COMPACT

M

Mahesh_001

When I execute CryptUnProtectData() as follwed, it errors with ERROR_INSUFFICIENT_BUFFER (122 ). Note that size of the input byte array(encrypted data) and output byte array (decrypted byte data) is same. OS is Windows 7 embedded compact.


BYTE encryptedValue[612] = // get encrypted value

DATA_BLOB encryptedData;

encryptedData.cbData = 612;

encryptedData.pbData = encryptedValue;


BYTE unencryptedValue[612];

DATA_BLOB unencryptedData1;

unencryptedData1.cbData = 612;

unencryptedData1.pbData = unencryptedValue;


if (CryptUnprotectData( &(encryptedData),

NULL,

NULL,

NULL,

NULL,

CRYPTPROTECT_LOCAL_MACHINE,

&(unencryptedData) == FALSE)

{

// Get windows error

}



Note : if I increase the buffer size of unencryptedData to around 900, API starts working. Why is this behavior ?

For your reference, complete code is as below.



UINT32 ExecuteCryptDecrypt()

{

const UINT32 unencryptedLength = 612;

UINT32 cryptProtectError = 1;

UINT32 cryptUnProtectError = 2;

BYTE unencryptedValue[unencryptedLength] = {0xA3, 0x38, 0x2C, 0x5D, 0x5E, 0x13, 0xC7, 0xF3, 0x94, 0xA9, 0x3B, 0x0A, 0x57, 0xBA, 0x10, 0x9B, 0x0D, 0x3D, 0x31, 0x5A, 0x7C, 0xA4, 0x61, 0x78, 0xA8, 0x55, 0x37, 0x8E, 0xD4, 0x76, 0x0D, 0x24, 0xB4, 0x06, 0x67, 0xBA, 0xC4, 0xAD, 0xFC, 0x37, 0x12, 0x3F, 0x0D, 0xA1, 0xF3, 0x30, 0x35, 0xEC, 0x80, 0x3F, 0x35, 0xD3, 0x60, 0xA9, 0xEF, 0x30, 0x63, 0xE5, 0xFE, 0xEB, 0x5A, 0x2A, 0xFF, 0xA7, 0x4F, 0x92, 0x8B, 0xB0, 0xB0, 0xD7, 0x33, 0x06, 0x17, 0x9B, 0x35, 0xCB, 0xAA, 0x3F, 0x15, 0x26, 0x69, 0xB9, 0x9C, 0xEF, 0xA6, 0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0xC9, 0x59, 0x46, 0x6A, 0x5D, 0x24, 0xFA, 0x12, 0xF4, 0x19, 0x80, 0x87, 0x7E, 0xB1, 0x10, 0xFF, 0xF1, 0xC5, 0x47, 0xBF, 0x9A, 0x9F, 0xF2, 0xED, 0x93, 0xAC, 0xAC, 0xAF, 0x97, 0x6E, 0xD0, 0x1C, 0x96, 0xDA, 0xAE, 0x93, 0x4C, 0xAF, 0x11, 0xF6, 0x63, 0xF0, 0x1E, 0xD8, 0x34, 0x01, 0x28, 0xE4, 0x91, 0x79, 0x7F, 0xBC, 0x43, 0x43, 0x41, 0xEF, 0x4F, 0xCB, 0xA1, 0xFE, 0xE1, 0x33, 0x7C, 0xEE, 0xD5, 0x76, 0x4F, 0x2B, 0x77, 0xD8, 0x25, 0x4C, 0x2C, 0x62, 0x40, 0x07, 0x7E, 0x51, 0xBA, 0x9C, 0xB4, 0x68, 0xCD, 0x63, 0xFD, 0x9E, 0xBF, 0x03, 0x58, 0x8F, 0x58, 0xF9, 0x8B, 0x6A, 0xC7, 0xCF, 0x1E, 0xCD, 0x01, 0x09, 0x89, 0xFB, 0xE0, 0x85, 0xB4, 0x73, 0x20, 0xDE, 0xFE, 0x0D, 0xA6, 0x81, 0xCC, 0xB0, 0xD2, 0x8A, 0xF0, 0x80, 0x51, 0xCE, 0x77, 0x1C, 0xC7, 0xEF, 0x68, 0x86, 0x84, 0x94, 0xCA, 0x91, 0x67, 0x2D, 0xAB, 0xB9, 0xE0, 0x7A, 0xC7, 0x98, 0x15, 0xA5, 0x08, 0x19, 0xD1, 0x82, 0x48, 0xDC, 0xC7, 0xC9, 0xBD, 0x7F, 0xCA, 0xB4, 0x6E, 0xD4, 0x3D, 0xF3, 0x0F, 0x8B, 0x9D, 0x7E, 0x56, 0xF3, 0x8B};


DATA_BLOB unencryptedData;

unencryptedData.cbData = unencryptedLength;

unencryptedData.pbData = unencryptedValue;


DATA_BLOB encryptedData;

encryptedData.cbData = 0;

encryptedData.pbData = NULL;


BOOL isProtectSuccessful = FALSE;

isProtectSuccessful = CryptProtectData( &(unencryptedData),

NULL,

NULL,

NULL,

NULL,

CRYPTPROTECT_LOCAL_MACHINE,

&(encryptedData) );

if ( isProtectSuccessful == FALSE )

{

return cryptProtectError;

}


DATA_BLOB encryptedData1;

encryptedData1.cbData = encryptedData.cbData;

encryptedData1.pbData = encryptedData.pbData;


const UINT32 unencryptedLength1 = 612;

BYTE unencryptedValue1[unencryptedLength1];

DATA_BLOB unencryptedData1;

unencryptedData1.cbData = unencryptedLength1;

unencryptedData1.pbData = unencryptedValue1;


BOOL isUnprotectSuccessful = FALSE;

isUnprotectSuccessful = CryptUnprotectData( &(encryptedData),

NULL,
// optional pointer to readable description of encrypted data

NULL,
// optional pointer to additional entropy

NULL,
// reserved, must be NULL

NULL,
// pPromptStruct, must be NULL (not used)

CRYPTPROTECT_LOCAL_MACHINE,

&(unencryptedData) );

if ( isUnprotectSuccessful == FALSE )

{

return cryptUnProtectError;
//errors out here

}

return 0;

}


Continue reading...
 
Back
Top Bottom