Skip to content

Commit

Permalink
optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
spaniakos committed Feb 10, 2015
1 parent f86723c commit afa534b
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 19 deletions.
47 changes: 43 additions & 4 deletions AES.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,11 +369,13 @@ byte AES::cbc_encrypt (byte * plain, byte * cipher, int n_block, byte iv [N_BLOC
return SUCCESS ;
}

/******************************************************************************/

byte AES::cbc_encrypt (byte * plain, byte * cipher, int n_block)
{
while (n_block--)
{
xor_block (iv, plain) ;
xor_block (iv, plain) ;
if (encrypt (iv, iv) != SUCCESS)
return FAILURE ;
copy_n_bytes (cipher, iv, N_BLOCK) ;
Expand Down Expand Up @@ -424,6 +426,24 @@ byte AES::cbc_decrypt (byte * cipher, byte * plain, int n_block, byte iv [N_BLOC
return SUCCESS ;
}

/******************************************************************************/

byte AES::cbc_decrypt (byte * cipher, byte * plain, int n_block)
{
while (n_block--)
{
byte tmp [N_BLOCK] ;
copy_n_bytes (tmp, cipher, N_BLOCK) ;
if (decrypt (cipher, plain) != SUCCESS)
return FAILURE ;
xor_block (plain, iv) ;
copy_n_bytes (iv, tmp, N_BLOCK) ;
plain += N_BLOCK ;
cipher += N_BLOCK;
}
return SUCCESS ;
}

/*****************************************************************************/

void AES::set_IV(unsigned long long int IVCl){
Expand Down Expand Up @@ -527,10 +547,20 @@ void AES::printArray(byte output[],int sizel)
}


/******************************************************************************/

void AES::do_aes_encrypt(byte *plain,int size_p,byte *cipher,byte *key, int bits, byte ivl [N_BLOCK]){
calc_size_n_pad(size_p);
byte plain_p[get_size()];
padPlaintext(plain,plain_p);
int blocks = get_size() / N_BLOCK;
set_key (key, bits) ;
cbc_encrypt (plain_p, cipher, blocks, ivl);
}

/******************************************************************************/

void AES::do_aes_encrypt(byte *plain,int size_p,byte *cipher,byte *key, int bits){
iv_inc();
calc_size_n_pad(size_p);
byte plain_p[get_size()];
padPlaintext(plain,plain_p);
Expand All @@ -541,13 +571,22 @@ void AES::do_aes_encrypt(byte *plain,int size_p,byte *cipher,byte *key, int bits

/******************************************************************************/

void AES::do_aes_dencrypt(byte *cipher,int size_c,byte *plain,byte *key, int bits, byte ivl [N_BLOCK]){
void AES::do_aes_decrypt(byte *cipher,int size_c,byte *plain,byte *key, int bits, byte ivl [N_BLOCK]){
set_size(size_c);
int blocks = size_c / N_BLOCK;
set_key (key, bits);
cbc_decrypt (cipher,plain, blocks, ivl);
}

/******************************************************************************/

void AES::do_aes_decrypt(byte *cipher,int size_c,byte *plain,byte *key, int bits){
set_size(size_c);
int blocks = size_c / N_BLOCK;
set_key (key, bits);
cbc_decrypt (cipher,plain, blocks);
}


/******************************************************************************/

Expand All @@ -556,4 +595,4 @@ double AES::millis(){
gettimeofday(&tv, NULL);
return (tv.tv_sec + 0.000001 * tv.tv_usec);
}
#endif
#endif
35 changes: 34 additions & 1 deletion AES.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,16 @@ class AES
*
*/
byte cbc_decrypt (byte * cipher, byte * plain, int n_block, byte iv [N_BLOCK]) ;

/** CBC decrypt a number of blocks (input and return an IV)
*
* @param *cipher Pointer, points to the ciphertext that will be created.
* @param *plain Pointer, points to the plaintex.
* @param n_block integer, indicated the number of blocks to be ciphered.
* @Return 0 if SUCCESS or -1 if FAILURE
*
*/
byte cbc_decrypt (byte * cipher, byte * plain, int n_block) ;

/** Sets IV (initialization vector) and IVC (IV counter).
* This function changes the ivc and iv variables needed for AES.
Expand Down Expand Up @@ -229,6 +239,18 @@ class AES
*/
void printArray(byte output[],int sizel);

/** User friendly implementation of AES-CBC encryption.
*
* @param *plain pointer to the plaintext
* @param size_p size of the plaintext
* @param *cipher pointer to the ciphertext
* @param *key pointer to the key that will be used.
* @param bits bits of the encryption/decrpytion
* @param ivl[N_BLOCK] the initialization vector IV that will be used for encryption.
* @note The key will be stored in class variable.
*/
void do_aes_encrypt(byte *plain,int size_p,byte *cipher,byte *key, int bits, byte ivl [N_BLOCK]);

/** User friendly implementation of AES-CBC encryption.
*
* @param *plain pointer to the plaintext
Expand All @@ -250,7 +272,18 @@ class AES
* @param ivl[N_BLOCK] the initialization vector IV that will be used for decryption.
* @note The key will be stored in class variable.
*/
void do_aes_dencrypt(byte *cipher,int size_c,byte *plain,byte *key, int bits, byte ivl [N_BLOCK]);
void do_aes_decrypt(byte *cipher,int size_c,byte *plain,byte *key, int bits, byte ivl [N_BLOCK]);

/** User friendly implementation of AES-CBC decryption.
*
* @param *cipher pointer to the ciphertext
* @param size_c size of the ciphertext
* @param *plain pointer to the plaintext
* @param *key pointer to the key that will be used.
* @param bits bits of the encryption/decrpytion
* @note The key will be stored in class variable.
*/
void do_aes_decrypt(byte *cipher,int size_c,byte *plain,byte *key, int bits);

#if defined(AES_LINUX)
/**
Expand Down
39 changes: 25 additions & 14 deletions examples/aes/aes.pde
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@

AES aes ;

byte key[] = "01234567899876543210012345678998";
byte *key = (unsigned char*)"0123456789010123";

byte plain[] = "TESTTESTTESTTESTTESTTESTTESTTESTTESTTEST";
byte plain[] = "Add NodeAdd NodeAdd NodeAdd NodeAdd Node";

//real iv = iv x2 ex: 01234567 = 0123456701234567
unsigned long long int my_iv = 01234567;
unsigned long long int my_iv = 36753562;

void setup ()
{
Serial.begin (57600) ;
printf_begin();
delay(1000);
delay(500);
printf("\n===testng mode\n") ;

// otfly_test () ;
Expand All @@ -29,20 +29,31 @@ void loop ()

void prekey (int bits)
{
aes.iv_inc();
byte iv [N_BLOCK] ;
byte plain_p[sizeof(plain) + (N_BLOCK - (sizeof(plain) % 16)) - 1];
byte cipher[sizeof(plain_p)];
aes.do_aes_encrypt(plain,sizeof(plain),cipher,key,bits);
byte plain_p[48];
byte cipher [48] ;
byte check [48] ;
unsigned long ms = micros ();
aes.set_IV(my_iv);
aes.get_IV(iv);
aes.do_aes_dencrypt(cipher,aes.get_size(),plain_p,key,bits,iv);
//normally u have sizeof(cipher) but if its in the same sketch you cannot determin it dynamically

aes.do_aes_encrypt(plain,41,cipher,key,bits,iv);
Serial.print("Encryption took: ");
Serial.println(micros() - ms);
ms = micros ();
aes.set_IV(my_iv);
aes.get_IV(iv);
aes.do_aes_decrypt(cipher,48,check,key,bits,iv);
Serial.print("Decryption took: ");
Serial.println(micros() - ms);
printf("\n\nPLAIN :");
aes.printArray(plain);
aes.printArray(plain,(bool)true);
printf("\nCIPHER:");
aes.printArray(cipher);
printf("\nPlain2:");
aes.printArray(plain_p);
aes.printArray(cipher,(bool)false);
printf("\nCHECK :");
aes.printArray(check,(bool)true);
printf("\nIV :");
aes.printArray(iv,16);
printf("\n============================================================\n");
}

Expand Down

0 comments on commit afa534b

Please sign in to comment.