Callbacks - CryptoCb
Functions
| Name | |
|---|---|
| int | wc_BufferKeyDecrypt(struct EncryptedInfo * info, byte * der, word32 derSz, const byte * password, int passwordSz, int hashType) This function decrypts an encrypted key buffer using the provided password. It supports encryption algorithms DES, 3DES, and AES. The encryption information is provided in the EncryptedInfo structure. |
| int | wc_BufferKeyEncrypt(struct EncryptedInfo * info, byte * der, word32 derSz, const byte * password, int passwordSz, int hashType) This function encrypts a key buffer using the provided password. It supports encryption algorithms DES, 3DES, and AES. The encryption information is provided in the EncryptedInfo structure. |
Functions Documentation
function wc_BufferKeyDecrypt
int wc_BufferKeyDecrypt(
struct EncryptedInfo * info,
byte * der,
word32 derSz,
const byte * password,
int passwordSz,
int hashType
)
This function decrypts an encrypted key buffer using the provided password. It supports encryption algorithms DES, 3DES, and AES. The encryption information is provided in the EncryptedInfo structure.
Parameters:
- info pointer to EncryptedInfo structure containing encryption algorithm and parameters
- der pointer to the encrypted key buffer
- derSz size of the encrypted key buffer
- password pointer to the password buffer
- passwordSz size of the password
- hashType hash algorithm to use for key derivation
See: wc_BufferKeyEncrypt
Return:
- 0 on success
- BAD_FUNC_ARG if der is NULL
- BAD_FUNC_ARG if password is NULL
- BAD_FUNC_ARG if info is NULL
- BAD_FUNC_ARG if info->keySz is 0
- BAD_FUNC_ARG if info->keySz > WC_MAX_SYM_KEY_SIZE
- ALGO_ID_E if info->cipherType is not one of the cipher types this function handles
- BUFFER_E if IV buffer is too small
- NOT_COMPILED_IN if info->cipherType is a cipher type this function handles, but support for it was disabled in this build
- Negative value on error
Example
EncryptedInfo info;
byte encryptedKey[32]; // encrypted key data, decrypted in place
byte password[] = "mypassword";
XMEMSET(&info, 0, sizeof(info));
info.cipherType = WC_CIPHER_AES_CBC;
info.keySz = 16; // size of the key derived from the password
// info.iv holds the IV as hex characters, the way it appears in a PEM
// DEK-Info header. It is Base16-decoded in place, and must decode to
// at least PKCS5_SALT_SZ bytes. The decoded bytes are used both as the
// key derivation salt and as the cipher IV.
XMEMCPY(info.iv, "0123456789ABCDEF0123456789ABCDEF", 32);
info.ivSz = 32; // length of the hex string, not the decoded IV
int ret = wc_BufferKeyDecrypt(&info, encryptedKey,
sizeof(encryptedKey), password,
sizeof(password)-1, WC_SHA256);
if (ret != 0) {
// decryption error
}
function wc_BufferKeyEncrypt
int wc_BufferKeyEncrypt(
struct EncryptedInfo * info,
byte * der,
word32 derSz,
const byte * password,
int passwordSz,
int hashType
)
This function encrypts a key buffer using the provided password. It supports encryption algorithms DES, 3DES, and AES. The encryption information is provided in the EncryptedInfo structure.
Parameters:
- info pointer to EncryptedInfo structure containing encryption algorithm and parameters
- der pointer to the key buffer to encrypt
- derSz size of the key buffer
- password pointer to the password buffer
- passwordSz size of the password
- hashType hash algorithm to use for key derivation
See: wc_BufferKeyDecrypt
Return:
- 0 on success
- BAD_FUNC_ARG if der is NULL
- BAD_FUNC_ARG if password is NULL
- BAD_FUNC_ARG if info is NULL
- BAD_FUNC_ARG if info->keySz is 0
- BAD_FUNC_ARG if info->keySz > WC_MAX_SYM_KEY_SIZE
- BAD_FUNC_ARG if info->ivSz < PKCS5_SALT_SZ
- ALGO_ID_E if info->cipherType is not one of the cipher types this function handles
- NOT_COMPILED_IN if info->cipherType is a cipher type this function handles, but support for it was disabled in this build
- Negative value on error
Example
EncryptedInfo info;
byte key[32]; // key data to encrypt, encrypted in place
byte password[] = "mypassword";
XMEMSET(&info, 0, sizeof(info));
info.cipherType = WC_CIPHER_AES_CBC;
info.keySz = 16; // size of the key derived from the password
info.ivSz = 16; // must be at least PKCS5_SALT_SZ
// Unlike wc_BufferKeyDecrypt, info.iv holds raw bytes here. Fill it
// with info.ivSz random bytes; the first PKCS5_SALT_SZ of them are used
// as the key derivation salt, and the buffer is also used as the
// cipher IV.
WC_RNG rng;
wc_InitRng(&rng);
wc_RNG_GenerateBlock(&rng, info.iv, info.ivSz);
int ret = wc_BufferKeyEncrypt(&info, key, sizeof(key), password,
sizeof(password)-1, WC_SHA256);
if (ret != 0) {
// encryption error
}
wc_FreeRng(&rng);
Updated on 2026-08-23 at 13:29:48 +0000