Skip to content

Algorithms - 3DES

Functions

Name
int wc_Des_SetKey(Des * des, const byte * key, const byte * iv, int dir)
This function sets the key and initialization vector (iv) for the Des structure given as argument. It also initializes and allocates space for the buffers needed for encryption and decryption, if these have not yet been initialized. Note: If no iv is provided (i.e. iv == NULL) the initialization vector defaults to an iv of 0.
void wc_Des_SetIV(Des * des, const byte * iv)
This function sets the initialization vector (iv) for the Des structure given as argument. When passed a NULL iv, it sets the initialization vector to 0.
int wc_Des_CbcEncrypt(Des * des, byte * out, const byte * in, word32 sz)
This function encrypts the input message, in, and stores the result in the output buffer, out. It uses DES encryption with cipher block chaining (CBC) mode.
int wc_Des_CbcDecrypt(Des * des, byte * out, const byte * in, word32 sz)
This function decrypts the input ciphertext, in, and stores the resulting plaintext in the output buffer, out. It uses DES encryption with cipher block chaining (CBC) mode.
int wc_Des_EcbEncrypt(Des * des, byte * out, const byte * in, word32 sz)
This function encrypts the input message, in, and stores the result in the output buffer, out. It uses Des encryption with Electronic Codebook (ECB) mode.
int wc_Des3_EcbEncrypt(Des3 * des, byte * out, const byte * in, word32 sz)
This function encrypts the input message, in, and stores the result in the output buffer, out. It uses Des3 encryption with Electronic Codebook (ECB) mode. Warning: In nearly all use cases ECB mode is considered to be less secure. Please avoid using ECB API’s directly whenever possible.
int wc_Des3_SetKey(Des3 * des, const byte * key, const byte * iv, int dir)
This function sets the key and initialization vector (iv) for the Des3 structure given as argument. It also initializes and allocates space for the buffers needed for encryption and decryption, if these have not yet been initialized. Note: If no iv is provided (i.e. iv == NULL) the initialization vector defaults to an iv of 0.
int wc_Des3_SetIV(Des3 * des, const byte * iv)
This function sets the initialization vector (iv) for the Des3 structure given as argument. When passed a NULL iv, it sets the initialization vector to 0.
int wc_Des3_CbcEncrypt(Des3 * des, byte * out, const byte * in, word32 sz)
This function encrypts the input message, in, and stores the result in the output buffer, out. It uses Triple Des (3DES) encryption with cipher block chaining (CBC) mode.
int wc_Des3_CbcDecrypt(Des3 * des, byte * out, const byte * in, word32 sz)
This function decrypts the input ciphertext, in, and stores the resulting plaintext in the output buffer, out. It uses Triple Des (3DES) encryption with cipher block chaining (CBC) mode.
int wc_Des_CbcDecryptWithKey(byte * out, const byte * in, word32 sz, const byte * key, const byte * iv)
This function decrypts the input ciphertext, in, and stores the resulting plaintext in the output buffer, out. It uses DES encryption with cipher block chaining (CBC) mode. This function is a substitute for wc_Des_CbcDecrypt, allowing the user to decrypt a message without directly instantiating a Des structure.
int wc_Des_CbcEncryptWithKey(byte * out, const byte * in, word32 sz, const byte * key, const byte * iv)
This function encrypts the input plaintext, in, and stores the resulting ciphertext in the output buffer, out. It uses DES encryption with cipher block chaining (CBC) mode. This function is a substitute for wc_Des_CbcEncrypt, allowing the user to encrypt a message without directly instantiating a Des structure.
int wc_Des3_CbcEncryptWithKey(byte * out, const byte * in, word32 sz, const byte * key, const byte * iv)
This function encrypts the input plaintext, in, and stores the resulting ciphertext in the output buffer, out. It uses Triple DES (3DES) encryption with cipher block chaining (CBC) mode. This function is a substitute for wc_Des3_CbcEncrypt, allowing the user to encrypt a message without directly instantiating a Des3 structure.
int wc_Des3_CbcDecryptWithKey(byte * out, const byte * in, word32 sz, const byte * key, const byte * iv)
This function decrypts the input ciphertext, in, and stores the resulting plaintext in the output buffer, out. It uses Triple Des (3DES) encryption with cipher block chaining (CBC) mode. This function is a substitute for wc_Des3_CbcDecrypt, allowing the user to decrypt a message without directly instantiating a Des3 structure.

Functions Documentation

function wc_Des_SetKey

int wc_Des_SetKey(
    Des * des,
    const byte * key,
    const byte * iv,
    int dir
)

This function sets the key and initialization vector (iv) for the Des structure given as argument. It also initializes and allocates space for the buffers needed for encryption and decryption, if these have not yet been initialized. Note: If no iv is provided (i.e. iv == NULL) the initialization vector defaults to an iv of 0.

Parameters:

  • des pointer to the Des structure to initialize
  • key pointer to the buffer containing the 8 byte key with which to initialize the Des structure
  • iv pointer to the buffer containing the 8 byte iv with which to initialize the Des structure. If this is not provided, the iv defaults to 0
  • dir direction of encryption. Valid options are: DES_ENCRYPTION, and DES_DECRYPTION

See:

Return: 0 On successfully setting the key and initialization vector for the Des structure

3

Example

Des enc; // Des structure used for encryption
int ret;
byte key[] = { // initialize with 8 byte key };
byte iv[]  = { // initialize with 8 byte iv };

ret = wc_Des_SetKey(&des, key, iv, DES_ENCRYPTION);
if (ret != 0) {
    // error initializing des structure
}

function wc_Des_SetIV

void wc_Des_SetIV(
    Des * des,
    const byte * iv
)

This function sets the initialization vector (iv) for the Des structure given as argument. When passed a NULL iv, it sets the initialization vector to 0.

Parameters:

  • des pointer to the Des structure for which to set the iv
  • iv pointer to the buffer containing the 8 byte iv with which to initialize the Des structure. If this is not provided, the iv defaults to 0

See: wc_Des_SetKey

Return: none No returns.

3

Example

Des enc; // Des structure used for encryption
// initialize enc with wc_Des_SetKey
byte iv[]  = { // initialize with 8 byte iv };
wc_Des_SetIV(&enc, iv);
}

function wc_Des_CbcEncrypt

int wc_Des_CbcEncrypt(
    Des * des,
    byte * out,
    const byte * in,
    word32 sz
)

This function encrypts the input message, in, and stores the result in the output buffer, out. It uses DES encryption with cipher block chaining (CBC) mode.

Parameters:

  • des pointer to the Des structure to use for encryption
  • out pointer to the buffer in which to store the encrypted ciphertext
  • in pointer to the input buffer containing the message to encrypt
  • sz length of the message to encrypt

See:

Return: 0 Returned upon successfully encrypting the given input message

3

Example

Des enc; // Des structure used for encryption
// initialize enc with wc_Des_SetKey, use mode DES_ENCRYPTION

byte plain[]  = { // initialize with message };
byte cipher[sizeof(plain)];

if ( wc_Des_CbcEncrypt(&enc, cipher, plain, sizeof(plain)) != 0) {
    // error encrypting message
}

function wc_Des_CbcDecrypt

int wc_Des_CbcDecrypt(
    Des * des,
    byte * out,
    const byte * in,
    word32 sz
)

This function decrypts the input ciphertext, in, and stores the resulting plaintext in the output buffer, out. It uses DES encryption with cipher block chaining (CBC) mode.

Parameters:

  • des pointer to the Des structure to use for decryption
  • out pointer to the buffer in which to store the decrypted plaintext
  • in pointer to the input buffer containing the encrypted ciphertext
  • sz length of the ciphertext to decrypt

See:

Return: 0 Returned upon successfully decrypting the given ciphertext

3

Example

Des dec; // Des structure used for decryption
// initialize dec with wc_Des_SetKey, use mode DES_DECRYPTION

byte cipher[]  = { // initialize with ciphertext };
byte decoded[sizeof(cipher)];

if ( wc_Des_CbcDecrypt(&dec, decoded, cipher, sizeof(cipher)) != 0) {
    // error decrypting message
}

function wc_Des_EcbEncrypt

int wc_Des_EcbEncrypt(
    Des * des,
    byte * out,
    const byte * in,
    word32 sz
)

This function encrypts the input message, in, and stores the result in the output buffer, out. It uses Des encryption with Electronic Codebook (ECB) mode.

Parameters:

  • des pointer to the Des structure to use for encryption
  • out pointer to the buffer in which to store the encrypted message
  • in pointer to the input buffer containing the plaintext to encrypt
  • sz length of the plaintext to encrypt

See: wc_Des_SetKe

Return: 0: Returned upon successfully encrypting the given plaintext.

3

Example

Des enc; // Des structure used for encryption
// initialize enc with wc_Des_SetKey, use mode DES_ENCRYPTION

byte plain[]  = { // initialize with message to encrypt };
byte cipher[sizeof(plain)];

if ( wc_Des_EcbEncrypt(&enc,cipher, plain, sizeof(plain)) != 0) {
    // error encrypting message
}

function wc_Des3_EcbEncrypt

int wc_Des3_EcbEncrypt(
    Des3 * des,
    byte * out,
    const byte * in,
    word32 sz
)

This function encrypts the input message, in, and stores the result in the output buffer, out. It uses Des3 encryption with Electronic Codebook (ECB) mode. Warning: In nearly all use cases ECB mode is considered to be less secure. Please avoid using ECB API’s directly whenever possible.

Parameters:

  • des3 pointer to the Des3 structure to use for encryption
  • out pointer to the buffer in which to store the encrypted message
  • in pointer to the input buffer containing the plaintext to encrypt
  • sz length of the plaintext to encrypt

See: wc_Des3_SetKey

Return: 0 Returned upon successfully encrypting the given plaintext

3

Example

Des3 enc; // Des3 structure used for encryption
// initialize enc with wc_Des3_SetKey, use mode DES_ENCRYPTION

byte plain[]  = { // initialize with message to encrypt };
byte cipher[sizeof(plain)];

if ( wc_Des3_EcbEncrypt(&enc,cipher, plain, sizeof(plain)) != 0) {
    // error encrypting message
}

function wc_Des3_SetKey

int wc_Des3_SetKey(
    Des3 * des,
    const byte * key,
    const byte * iv,
    int dir
)

This function sets the key and initialization vector (iv) for the Des3 structure given as argument. It also initializes and allocates space for the buffers needed for encryption and decryption, if these have not yet been initialized. Note: If no iv is provided (i.e. iv == NULL) the initialization vector defaults to an iv of 0.

Parameters:

  • des3 pointer to the Des3 structure to initialize
  • key pointer to the buffer containing the 24 byte key with which to initialize the Des3 structure
  • iv pointer to the buffer containing the 8 byte iv with which to initialize the Des3 structure. If this is not provided, the iv defaults to 0
  • dir direction of encryption. Valid options are: DES_ENCRYPTION, and DES_DECRYPTION

See:

Return: 0 On successfully setting the key and initialization vector for the Des structure

3

Example

Des3 enc; // Des3 structure used for encryption
int ret;
byte key[] = { // initialize with 24 byte key };
byte iv[]  = { // initialize with 8 byte iv };

ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
if (ret != 0) {
    // error initializing des structure
}

function wc_Des3_SetIV

int wc_Des3_SetIV(
    Des3 * des,
    const byte * iv
)

This function sets the initialization vector (iv) for the Des3 structure given as argument. When passed a NULL iv, it sets the initialization vector to 0.

Parameters:

  • des pointer to the Des3 structure for which to set the iv
  • iv pointer to the buffer containing the 8 byte iv with which to initialize the Des3 structure. If this is not provided, the iv defaults to 0

See: wc_Des3_SetKey

Return: none No returns.

3

Example

Des3 enc; // Des3 structure used for encryption
// initialize enc with wc_Des3_SetKey

byte iv[]  = { // initialize with 8 byte iv };

wc_Des3_SetIV(&enc, iv);
}

function wc_Des3_CbcEncrypt

int wc_Des3_CbcEncrypt(
    Des3 * des,
    byte * out,
    const byte * in,
    word32 sz
)

This function encrypts the input message, in, and stores the result in the output buffer, out. It uses Triple Des (3DES) encryption with cipher block chaining (CBC) mode.

Parameters:

  • des pointer to the Des3 structure to use for encryption
  • out pointer to the buffer in which to store the encrypted ciphertext
  • in pointer to the input buffer containing the message to encrypt
  • sz length of the message to encrypt

See:

Return: 0 Returned upon successfully encrypting the given input message

3

Example

Des3 enc; // Des3 structure used for encryption
// initialize enc with wc_Des3_SetKey, use mode DES_ENCRYPTION

byte plain[]  = { // initialize with message };
byte cipher[sizeof(plain)];

if ( wc_Des3_CbcEncrypt(&enc, cipher, plain, sizeof(plain)) != 0) {
    // error encrypting message
}

function wc_Des3_CbcDecrypt

int wc_Des3_CbcDecrypt(
    Des3 * des,
    byte * out,
    const byte * in,
    word32 sz
)

This function decrypts the input ciphertext, in, and stores the resulting plaintext in the output buffer, out. It uses Triple Des (3DES) encryption with cipher block chaining (CBC) mode.

Parameters:

  • des pointer to the Des3 structure to use for decryption
  • out pointer to the buffer in which to store the decrypted plaintext
  • in pointer to the input buffer containing the encrypted ciphertext
  • sz length of the ciphertext to decrypt

See:

Return: 0 Returned upon successfully decrypting the given ciphertext

3

Example

Des3 dec; // Des structure used for decryption
// initialize dec with wc_Des3_SetKey, use mode DES_DECRYPTION

byte cipher[]  = { // initialize with ciphertext };
byte decoded[sizeof(cipher)];

if ( wc_Des3_CbcDecrypt(&dec, decoded, cipher, sizeof(cipher)) != 0) {
    // error decrypting message
}

function wc_Des_CbcDecryptWithKey

int wc_Des_CbcDecryptWithKey(
    byte * out,
    const byte * in,
    word32 sz,
    const byte * key,
    const byte * iv
)

This function decrypts the input ciphertext, in, and stores the resulting plaintext in the output buffer, out. It uses DES encryption with cipher block chaining (CBC) mode. This function is a substitute for wc_Des_CbcDecrypt, allowing the user to decrypt a message without directly instantiating a Des structure.

Parameters:

  • out pointer to the buffer in which to store the decrypted plaintext
  • in pointer to the input buffer containing the encrypted ciphertext
  • sz length of the ciphertext to decrypt
  • key pointer to the buffer containing the 8 byte key to use for decryption
  • iv pointer to the buffer containing the 8 byte iv to use for decryption. If no iv is provided, the iv defaults to 0

See: wc_Des_CbcDecrypt

Return:

  • 0 Returned upon successfully decrypting the given ciphertext
  • MEMORY_E Returned if there is an error allocating space for a Des structure

3

Example

int ret;
byte key[] = { // initialize with 8 byte key };
byte iv[]  = { // initialize with 8 byte iv };

byte cipher[]  = { // initialize with ciphertext };
byte decoded[sizeof(cipher)];

if ( wc_Des_CbcDecryptWithKey(decoded, cipher, sizeof(cipher), key,
iv) != 0) {
    // error decrypting message
}

function wc_Des_CbcEncryptWithKey

int wc_Des_CbcEncryptWithKey(
    byte * out,
    const byte * in,
    word32 sz,
    const byte * key,
    const byte * iv
)

This function encrypts the input plaintext, in, and stores the resulting ciphertext in the output buffer, out. It uses DES encryption with cipher block chaining (CBC) mode. This function is a substitute for wc_Des_CbcEncrypt, allowing the user to encrypt a message without directly instantiating a Des structure.

Parameters:

  • out Final encrypted data
  • in Data to be encrypted, must be padded to Des block size.
  • sz Size of input buffer.
  • key Pointer to the key to use for encryption.
  • iv Initialization vector

See:

Return:

  • 0 Returned after successfully encrypting data.
  • MEMORY_E Returned if there's an error allocating memory for a Des structure.
  • <0 Returned on any error during encryption.

3

Example

byte key[] = { // initialize with 8 byte key };
byte iv[]  = { // initialize with 8 byte iv };
byte in[] = { // Initialize with plaintext };
byte out[sizeof(in)];
if ( wc_Des_CbcEncryptWithKey(&out, in, sizeof(in), key, iv) != 0)
{
    // error encrypting message
}

function wc_Des3_CbcEncryptWithKey

int wc_Des3_CbcEncryptWithKey(
    byte * out,
    const byte * in,
    word32 sz,
    const byte * key,
    const byte * iv
)

This function encrypts the input plaintext, in, and stores the resulting ciphertext in the output buffer, out. It uses Triple DES (3DES) encryption with cipher block chaining (CBC) mode. This function is a substitute for wc_Des3_CbcEncrypt, allowing the user to encrypt a message without directly instantiating a Des3 structure.

Parameters:

  • out Final encrypted data
  • in Data to be encrypted, must be padded to Des block size.
  • sz Size of input buffer.
  • key Pointer to the key to use for encryption.
  • iv Initialization vector

See:

Return:

  • 0 Returned after successfully encrypting data.
  • MEMORY_E Returned if there's an error allocating memory for a Des structure.
  • <0 Returned on any error during encryption.

3

Example

byte key[] = { // initialize with 8 byte key };
byte iv[]  = { // initialize with 8 byte iv };

byte in[] = { // Initialize with plaintext };
byte out[sizeof(in)];

if ( wc_Des3_CbcEncryptWithKey(&out, in, sizeof(in), key, iv) != 0)
{
    // error encrypting message
}

function wc_Des3_CbcDecryptWithKey

int wc_Des3_CbcDecryptWithKey(
    byte * out,
    const byte * in,
    word32 sz,
    const byte * key,
    const byte * iv
)

This function decrypts the input ciphertext, in, and stores the resulting plaintext in the output buffer, out. It uses Triple Des (3DES) encryption with cipher block chaining (CBC) mode. This function is a substitute for wc_Des3_CbcDecrypt, allowing the user to decrypt a message without directly instantiating a Des3 structure.

Parameters:

  • out pointer to the buffer in which to store the decrypted plaintext
  • in pointer to the input buffer containing the encrypted ciphertext
  • sz length of the ciphertext to decrypt
  • key pointer to the buffer containing the 24 byte key to use for decryption
  • iv pointer to the buffer containing the 8 byte iv to use for decryption. If no iv is provided, the iv defaults to 0

See: wc_Des3_CbcDecrypt

Return:

  • 0 Returned upon successfully decrypting the given ciphertext
  • MEMORY_E Returned if there is an error allocating space for a Des structure

3

Example

int ret;
byte key[] = { // initialize with 24 byte key };
byte iv[]  = { // initialize with 8 byte iv };

byte cipher[]  = { // initialize with ciphertext };
byte decoded[sizeof(cipher)];

if ( wc_Des3_CbcDecryptWithKey(decoded, cipher, sizeof(cipher),
key, iv) != 0) {
    // error decrypting message
}

Updated on 2024-11-21 at 01:17:07 +0000