DES and 3DES Encryption Library for Arduino and Raspberry Pi
Spaniakos - DES and 3DES Encryption Library for Arduino and Raspberry Pi
Public Member Functions | Private Member Functions | Private Attributes | List of all members
DES Class Reference

#include <DES.h>

Public Member Functions

void encrypt (void *out, const void *in, const void *key)
 decrypt a block with DES More...
 
void decrypt (void *out, const void *in, const uint8_t *key)
 encrypt a block with DES More...
 
void tripleEncrypt (void *out, void *in, const void *key)
 encrypt a block with Tripple-DES More...
 
void tripleDecrypt (void *out, void *in, const uint8_t *key)
 decrypt a block with Tripple-DES More...
 
 DES ()
 DES constructor. More...
 
void init (const void *m_key, unsigned long long int IVCl)
 initiallize the key and IVC and IV array More...
 
void init (const void *m_key)
 initiallize the key More...
 
void change_key (const void *m_key)
 change the key for DEs and 3DES More...
 
void set_IV (unsigned long long int IVCl)
 
void get_IV (byte *out)
 
unsigned long long int get_IV_int ()
 
void iv_inc ()
 
byte * get_key ()
 getter method for key More...
 
int get_size ()
 getter method for size More...
 
void set_size (int sizel)
 
void calc_size_n_pad (int p_size)
 calculates the size of the plaintext and the padding More...
 
void padPlaintext (void *in, byte *out)
 pads the plaintext More...
 
bool CheckPad (byte *in, int size)
 check the if the padding is correct More...
 
void tdesCbcEncipher (byte *in, byte *out)
 the main encrypt 3DES with IV function More...
 
void tdesCbcDecipher (byte *in, byte *out)
 the main decrypt 3DES with IV function More...
 
void printArray (byte output[], bool p_pad=true)
 
void printArray (byte output[], int sizel)
 
void do_3des_encrypt (byte *plain, int size_p, byte *cipher, const void *key, bool inc=false)
 
void do_3des_decrypt (byte *cipher, int size_c, byte *plain, const void *key, unsigned long long int ivl)
 
double millis ()
 

Private Member Functions

void permute (const uint8_t *ptable, const uint8_t *in, uint8_t *out)
 
void changeendian32 (uint32_t *a)
 
void shiftkey (uint8_t *key)
 
void shiftkey_inv (uint8_t *key)
 
uint64_t splitin6bitwords (uint64_t a)
 
uint8_t substitute (uint8_t a, uint8_t *sbp)
 
uint32_t des_f (uint32_t r, uint8_t *kr)
 

Private Attributes

byte key [24]
 
unsigned long long int IVC
 
byte iv [8]
 
int pad
 
int size
 
timeval tv
 
byte arr_pad [7]
 

Detailed Description

Examples:
DesedeCBC-easy.cpp, DesedeCBCexample.ino, DESexample.cpp, and DESexample.ino.

Constructor & Destructor Documentation

DES::DES ( )

DES constructor.

This function initialized an instance of DES

Member Function Documentation

void DES::encrypt ( void *  out,
const void *  in,
const void *  key 
)

decrypt a block with DES

This function decrypts a block of 64 bits (8 bytes) with the DES algorithm. Key expansion is done automatically. The key is 64 bits long, but note that only 56 bits are used (the LSB of each byte is dropped). The input and output blocks may overlap.

Parameters
outpointer to the block (64 bit = 8 byte) where the plaintext is written to
inpointer to the block (64 bit = 8 byte) where the ciphertext is read from
keypointer to the key (64 bit = 8 byte)
Examples:
DESexample.cpp, and DESexample.ino.
void DES::decrypt ( void *  out,
const void *  in,
const uint8_t *  key 
)

encrypt a block with DES

This function encrypts a block of 64 bits (8 bytes) with the DES algorithm. Key expansion is done automatically. The key is 64 bits long, but note that only 56 bits are used (the LSB of each byte is dropped). The input and output blocks may overlap.

Parameters
outpointer to the block (64 bit = 8 byte) where the ciphertext is written to
inpointer to the block (64 bit = 8 byte) where the plaintext is read from
keypointer to the key (64 bit = 8 byte)
Examples:
DESexample.cpp, and DESexample.ino.
void DES::tripleEncrypt ( void *  out,
void *  in,
const void *  key 
)

encrypt a block with Tripple-DES

This function encrypts a block of 64 bits (8 bytes) with the Tripple-DES (EDE) algorithm. Key expansion is done automatically. The key is 192 bits long, but note that only 178 bits are used (the LSB of each byte is dropped). The input and output blocks may overlap.

Parameters
outpointer to the block (64 bit = 8 byte) where the ciphertext is written to
inpointer to the block (64 bit = 8 byte) where the plaintext is read from
keypointer to the key (192 bit = 24 byte)
Examples:
DESexample.cpp, and DESexample.ino.
void DES::tripleDecrypt ( void *  out,
void *  in,
const uint8_t *  key 
)

decrypt a block with Tripple-DES

This function decrypts a block of 64 bits (8 bytes) with the Tripple-DES (EDE) algorithm. Key expansion is done automatically. The key is 192 bits long, but note that only 178 bits are used (the LSB of each byte is dropped). The input and output blocks may overlap.

Parameters
outpointer to the block (64 bit = 8 byte) where the plaintext is written to
inpointer to the block (64 bit = 8 byte) where the ciphertext is read from
keypointer to the key (192 bit = 24 byte)
Examples:
DESexample.cpp, and DESexample.ino.
void DES::init ( const void *  m_key,
unsigned long long int  IVCl 
)

initiallize the key and IVC and IV array

This function initialized the basic variables needed for 3DES

Parameters
m_key(64 bit = 8 byte)
IVClint or hex value of iv , ex. 0x0000000000000001
Examples:
DesedeCBC-easy.cpp, and DesedeCBCexample.ino.
void DES::init ( const void *  m_key)

initiallize the key

This function initialized the basic variables needed for DES

Parameters
m_key(64 bit = 8 byte)
void DES::change_key ( const void *  m_key)

change the key for DEs and 3DES

This function changes the key variable needed for DES

Parameters
m_key(64 bit = 8 byte)
void DES::set_IV ( unsigned long long int  IVCl)
void DES::get_IV ( byte *  out)

Getter method for IV

This function return the IV

Parameters
*outbyte pointer that gets the IV.
Returns
none, the IV is writed to the out pointer.
unsigned long long int DES::get_IV_int ( )

Getter method for IV

This function return the IV

Parameters
outbyte pointer that gets the IV.
Returns
none, the IV is writed to the out pointer.
Examples:
DesedeCBC-easy.cpp.
void DES::iv_inc ( )
DES::get_key ( )

getter method for key

This function return the key

Returns
returns the key
DES::get_size ( )

getter method for size

This function return the size

Returns
returns the size
Examples:
DesedeCBCexample.ino.
void DES::set_size ( int  sizel)

Setter method for size

This function sets the size of the plaintext+pad

DES::calc_size_n_pad ( int  p_size)

calculates the size of the plaintext and the padding

calculates the size of theplaintext with the padding and the size of the padding needed. Moreover it stores them in their variables.

Parameters
p_sizethe size of the byte array ex sizeof(plaintext)
Examples:
DesedeCBCexample.ino.
DES::padPlaintext ( void *  in,
byte *  out 
)

pads the plaintext

This function pads the plaintext and returns an char array with the plaintext and the padding in order for the plaintext to be compatible with 8bit size blocks required by 3DES

Parameters
inthe string of the plaintext in a byte array
outthe string of the padded plaintext in a byte array
Examples:
DesedeCBCexample.ino.
DES::CheckPad ( byte *  in,
int  size 
)

check the if the padding is correct

This functions checks the padding of the plaintext.

Parameters
*inthe string of the plaintext in a byte array
sizethe size of the string
Returns
true if correct / false if not
Examples:
DesedeCBCexample.ino.
DES::tdesCbcEncipher ( byte *  in,
byte *  out 
)

the main encrypt 3DES with IV function

This function uses the IV to xor (^) the first block of the string and encrypts it using tripleEncrypt function

Parameters
inthe string of the plaintext in a byte array
outthe string of the ciphertext in a byte array
Examples:
DesedeCBCexample.ino.
DES::tdesCbcDecipher ( byte *  in,
byte *  out 
)

the main decrypt 3DES with IV function

This function if the reverse of the tdesCbcEncipher function. used the IV and then the tripleDecrypt

Parameters
inthe string of the ciphertext in a byte array
outthe string of the plaintext in a byte array
Examples:
DesedeCBCexample.ino.
void DES::printArray ( byte  output[],
bool  p_pad = true 
)
void DES::printArray ( byte  output[],
int  sizel 
)

Prints the array given.

This function prints the given array in Hexadecimal.

Parameters
output[]the string of the text in a byte array
sizelthe size of the array.
void DES::do_3des_encrypt ( byte *  plain,
int  size_p,
byte *  cipher,
const void *  key,
bool  inc = false 
)

User friendly implementation of AES-CBC encryption.

Parameters
*plainpointer to the plaintext
size_psize of the plaintext
*cipherpointer to the ciphertext
*keypointer to the key that will be used.
incoptional parameter to automaticaly increase IV
Note
The key will be stored in class variable.
Examples:
DesedeCBC-easy.cpp.
void DES::do_3des_decrypt ( byte *  cipher,
int  size_c,
byte *  plain,
const void *  key,
unsigned long long int  ivl 
)

User friendly implementation of AES-CBC decryption.

Parameters
*cipherpointer to the ciphertext
size_csize of the ciphertext
*plainpointer to the plaintext
*keypointer to the key that will be used.
ivlthe initialization vector IV that will be used for decryption.
Note
The key will be stored in class variable.
Examples:
DesedeCBC-easy.cpp.
double DES::millis ( )

used in linux in order to retrieve the time in milliseconds.

Returns
returns the milliseconds in a double format.
Examples:
DesedeCBC-easy.cpp, and DESexample.cpp.
void DES::permute ( const uint8_t *  ptable,
const uint8_t *  in,
uint8_t *  out 
)
private

Permutation for DES.

Parameters
*ptablethe permutaion table to be used.
*inthe pointer that holds the data before the permutations.
*outthe pointer that holds the data after the permutation.
Returns
none, the output is inside the out pointer
void DES::changeendian32 ( uint32_t *  a)
private

change endian form.

Parameters
*athe byte to change endian
void DES::shiftkey ( uint8_t *  key)
inlineprivate

used to shift the key. Used the shiftkey_permtab permutation table.

Parameters
*keythe key to be shifted.
void DES::shiftkey_inv ( uint8_t *  key)
inlineprivate

used to shift the key back. Used the shiftkeyinv_permtab permutation table.

Parameters
*keythe key to be shifted.
uint64_t DES::splitin6bitwords ( uint64_t  a)
inlineprivate

splits in 6 bit words

Parameters
auint64_t size that will be slitted, permuted and returned.
Returns
the permuted a.
uint8_t DES::substitute ( uint8_t  a,
uint8_t *  sbp 
)
inlineprivate

main function for the substitutions

Parameters
aarray position
*sbparray to read byte from
Returns
the substituted byte.
uint32_t DES::des_f ( uint32_t  r,
uint8_t *  kr 
)
private

Primary function of DES. Used in encryption and decryption process.

Parameters
runsigned intefer 32 bit, Data to be permuted.
krpointer of unsigned integers 8 bit, Data to be XOR with r after permutation
Returns

Member Data Documentation

byte DES::key[24]
private

holds the key for the encryption

unsigned long long int DES::IVC
private

holds the initialization vector counter in numerical format.

byte DES::iv[8]
private

holds the initialization vector that will be used in the cipher.

int DES::pad
private

holds the size of the padding.

DES::size
private

hold the size of the plaintext to be ciphered

and pad equal

timeval DES::tv
private

holds the time value on linux

byte DES::arr_pad[7]
private

holds the hexadecimal padding values, initialisation in the constructor