DES and 3DES Encryption Library for Arduino and Raspberry Pi
Spaniakos - DES and 3DES Encryption Library for Arduino and Raspberry Pi
|
#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] |
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.
out | pointer to the block (64 bit = 8 byte) where the plaintext is written to |
in | pointer to the block (64 bit = 8 byte) where the ciphertext is read from |
key | pointer to the key (64 bit = 8 byte) |
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.
out | pointer to the block (64 bit = 8 byte) where the ciphertext is written to |
in | pointer to the block (64 bit = 8 byte) where the plaintext is read from |
key | pointer to the key (64 bit = 8 byte) |
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.
out | pointer to the block (64 bit = 8 byte) where the ciphertext is written to |
in | pointer to the block (64 bit = 8 byte) where the plaintext is read from |
key | pointer to the key (192 bit = 24 byte) |
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.
out | pointer to the block (64 bit = 8 byte) where the plaintext is written to |
in | pointer to the block (64 bit = 8 byte) where the ciphertext is read from |
key | pointer to the key (192 bit = 24 byte) |
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
m_key | (64 bit = 8 byte) |
IVCl | int or hex value of iv , ex. 0x0000000000000001 |
void DES::init | ( | const void * | m_key | ) |
initiallize the key
This function initialized the basic variables needed for DES
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
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
*out | byte pointer that gets the IV. |
unsigned long long int DES::get_IV_int | ( | ) |
Getter method for IV
This function return the IV
out | byte pointer that gets the IV. |
void DES::iv_inc | ( | ) |
DES::get_key | ( | ) |
getter method for key
This function return the key
DES::get_size | ( | ) |
getter method for size
This function return the size
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.
p_size | the size of the byte array ex sizeof(plaintext) |
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
in | the string of the plaintext in a byte array |
out | the string of the padded plaintext in a byte array |
DES::CheckPad | ( | byte * | in, |
int | size | ||
) |
check the if the padding is correct
This functions checks the padding of the plaintext.
*in | the string of the plaintext in a byte array |
size | the size of the string |
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
in | the string of the plaintext in a byte array |
out | the string of the ciphertext in a byte array |
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
in | the string of the ciphertext in a byte array |
out | the string of the plaintext in a byte array |
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.
output[] | the string of the text in a byte array |
sizel | the 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.
*plain | pointer to the plaintext |
size_p | size of the plaintext |
*cipher | pointer to the ciphertext |
*key | pointer to the key that will be used. |
inc | optional parameter to automaticaly increase IV |
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.
*cipher | pointer to the ciphertext |
size_c | size of the ciphertext |
*plain | pointer to the plaintext |
*key | pointer to the key that will be used. |
ivl | the initialization vector IV that will be used for decryption. |
double DES::millis | ( | ) |
used in linux in order to retrieve the time in milliseconds.
|
private |
Permutation for DES.
*ptable | the permutaion table to be used. |
*in | the pointer that holds the data before the permutations. |
*out | the pointer that holds the data after the permutation. |
|
private |
change endian form.
*a | the byte to change endian |
|
inlineprivate |
used to shift the key. Used the shiftkey_permtab permutation table.
*key | the key to be shifted. |
|
inlineprivate |
used to shift the key back. Used the shiftkeyinv_permtab permutation table.
*key | the key to be shifted. |
|
inlineprivate |
splits in 6 bit words
a | uint64_t size that will be slitted, permuted and returned. |
|
inlineprivate |
main function for the substitutions
a | array position |
*sbp | array to read byte from |
|
private |
Primary function of DES. Used in encryption and decryption process.
r | unsigned intefer 32 bit, Data to be permuted. |
kr | pointer of unsigned integers 8 bit, Data to be XOR with r after permutation |
|
private |
holds the key for the encryption
|
private |
holds the initialization vector counter in numerical format.
|
private |
holds the initialization vector that will be used in the cipher.
|
private |
holds the size of the padding.
|
private |
hold the size of the plaintext to be ciphered
and pad equal
|
private |
holds the time value on linux
|
private |
holds the hexadecimal padding values, initialisation in the constructor