From f86d93494eed1e0ae00d7ffdc23ea70259272194 Mon Sep 17 00:00:00 2001 From: George Spanos Date: Sun, 25 Jan 2015 20:27:41 +0100 Subject: [PATCH] changes and documentation --- AES.cpp | 54 +++++++++------- AES.h | 147 +++++++++++++++++++++++++++++++++++++++++-- examples/aes/aes.pde | 15 ++--- 3 files changed, 180 insertions(+), 36 deletions(-) diff --git a/AES.cpp b/AES.cpp index efa3395..85aca03 100644 --- a/AES.cpp +++ b/AES.cpp @@ -369,6 +369,20 @@ 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) ; + if (encrypt (iv, iv) != SUCCESS) + return FAILURE ; + copy_n_bytes (cipher, iv, N_BLOCK) ; + plain += N_BLOCK ; + cipher += N_BLOCK ; + } + return SUCCESS ; +} + /******************************************************************************/ byte AES::decrypt (byte plain [N_BLOCK], byte cipher [N_BLOCK]) @@ -432,6 +446,13 @@ int AES::get_size(){ return size; } +/******************************************************************************/ + +void AES::set_size(int sizel){ + size = sizel; +} + + /******************************************************************************/ void AES::get_IV(byte *out){ @@ -505,14 +526,8 @@ void AES::printArray(byte output[],int sizel) printf_P(PSTR("\n")); } -/******************************************************************************/ -#if defined(AES_LINUX) -double AES::millis(){ - gettimeofday(&tv, NULL); - return (tv.tv_sec + 0.000001 * tv.tv_usec); -} -#endif +/******************************************************************************/ void AES::do_aes_encrypt(byte *plain,int size_p,byte *cipher,byte *key, int bits){ iv_inc(); @@ -524,6 +539,8 @@ void AES::do_aes_encrypt(byte *plain,int size_p,byte *cipher,byte *key, int bits cbc_encrypt (plain_p, cipher, blocks); } +/******************************************************************************/ + void AES::do_aes_dencrypt(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; @@ -531,21 +548,12 @@ void AES::do_aes_dencrypt(byte *cipher,int size_c,byte *plain,byte *key, int bit cbc_decrypt (cipher,plain, blocks, ivl); } -byte AES::cbc_encrypt (byte * plain, byte * cipher, int n_block) -{ - while (n_block--) - { - xor_block (iv, plain) ; - if (encrypt (iv, iv) != SUCCESS) - return FAILURE ; - copy_n_bytes (cipher, iv, N_BLOCK) ; - plain += N_BLOCK ; - cipher += N_BLOCK ; - } - return SUCCESS ; -} -void AES::set_size(int sizel){ - size = sizel; -} +/******************************************************************************/ +#if defined(AES_LINUX) +double AES::millis(){ + gettimeofday(&tv, NULL); + return (tv.tv_sec + 0.000001 * tv.tv_usec); +} +#endif \ No newline at end of file diff --git a/AES.h b/AES.h index 02da5f9..b1bb0b0 100644 --- a/AES.h +++ b/AES.h @@ -70,14 +70,14 @@ class AES void clean () ; // delete key schedule after use /** copying and xoring utilities. - * @param *dest byte pointer of the destination array. + * @param *AESt byte pointer of the AEStination array. * @param *src byte pointer of the source array. * @param n byte, indicating the sizeof the bytes to be copied. * @note this is an alternative for memcpy(void *s1,const void *s2, site_t n), * i have not updated the function in the implementation yet, but it is considered a future plan. * */ - void copy_n_bytes (byte * dest, byte * src, byte n) ; + void copy_n_bytes (byte * AESt, byte * src, byte n) ; /** Encrypt a single block of 16 bytes . * @param plain[N_BLOCK] Array of the plaintext. @@ -103,6 +103,16 @@ class AES * */ byte cbc_encrypt (byte * plain, byte * cipher, int n_block, byte iv [N_BLOCK]) ; + + /** CBC encrypt a number of blocks (input and return an IV). + * + * @param *plain Pointer, points to the plaintex. + * @param *cipher Pointer, points to the ciphertext that will be created. + * @param n_block integer, indicated the number of blocks to be ciphered. + * @Return 0 if SUCCESS or -1 if FAILURE + * + */ + byte cbc_encrypt (byte * plain, byte * cipher, int n_block) ; /** Decrypt a single block of 16 bytes @@ -154,6 +164,13 @@ class AES */ int get_size(); + /** Setter method for size + * + * This function sets the size of the plaintext+pad + * + */ + void set_size(int sizel); + /** Getter method for IV * * This function return the IV @@ -212,10 +229,29 @@ 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 + * @note The key will be stored in class variable. + */ void do_aes_encrypt(byte *plain,int size_p,byte *cipher,byte *key, int bits); + + /** 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 + * @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 set_size(int sizel); - byte cbc_encrypt (byte * plain, byte * cipher, int n_block) ; + #if defined(AES_LINUX) /** * used in linux in order to retrieve the time in milliseconds. @@ -241,3 +277,106 @@ class AES #endif + +/** + * @example aes.ino + * For Arduino
+ * Updated: spaniakos 2015
+ * + * This is an example of how to use AES in CBC mode easily. + * The text and keys can be either in HEX or String format.
+ */ + + /** + * @example aes.cpp + * For Rasberry pi
+ * Updated: spaniakos 2015
+ * + * This is an example of how to use AES in CBC mode easily. + * The text and keys can be either in HEX or String format.
+ */ + + /** + * @example test_vectors.ino + * For Arduino
+ * Updated: spaniakos 2015
+ * + * This is an example of monte carlo test vectors, in order to justify the effectiveness of the algorithm.
+ * plus is a classical approach to the AES encryption library with out the easy to use add-on modifications. + */ + + /** + * @example test_vectors.cpp + * For Rasberry pi
+ * Updated: spaniakos 2015
+ * + * This is an example of monte carlo test vectors, in order to justify the effectiveness of the algorithm.
+ * plus is a classical approach to the AES encryption library with out the easy to use add-on modifications. + */ + +/** + * @mainpage AES library for Arduino and Raspberry pi. + * + * @section Goals design Goals + * + * This library is AESigned to be... + * @li Fast and efficient. + * @li Able to effectivelly encrypt and decrypt any size of string. + * @li Able to encrypt and decrypt using AES + * @li Able to encrypt and decrypt using AES-CBC + * @li Easy for the user to use in his programs. + * + * @section Acknowlegments Acknowlegments + * This is an AES library for the Arduino, based on tzikis's AES library, which you can find here:.
+ * Tzikis library was based on scottmac`s library, which you can find here:
+ * + * @section Installation Installation + *

Arduino

+ * Create a folder named _AES_ in the _libraries_ folder inside your Arduino sketch folder. If the + * libraries folder doesn't exist, create it. Then copy everything inside. (re)launch the Arduino IDE.
+ * You're done. Time for a mojito + * + *

Raspberry pi

+ * install

+ * + * sudo make install
+ * cd examples_Rpi
+ * make

+ * + * What to do after changes to the library

+ * sudo make clean
+ * sudo make install
+ * cd examples_Rpi
+ * make clean
+ * make

+ * What to do after changes to a sketch

+ * cd examples_Rpi
+ * make

+ * or
+ * make clean
+ * make


+ * How to start a sketch

+ * cd examples_Rpi
+ * sudo ./

+ * + * @section News News + * + * If issues are discovered with the documentation, please report them here + * @section Useful Useful References + * + * Please refer to: + * + * @li AES Class Documentation + * @li Download + * @li Source Code + * @li All spaniakos Documentation Main Page + * + * @section Board_Support Board Support + * + * Most standard Arduino based boards are supported: + * - Arduino + * - Intel Galileo support + * - Raspberry Pi Support + * + * - The library has not been tested to other boards, but it should suppport ATMega 328 based boards,Mega Boards,Arduino Due,ATTiny board + */ diff --git a/examples/aes/aes.pde b/examples/aes/aes.pde index 99088ee..502492c 100644 --- a/examples/aes/aes.pde +++ b/examples/aes/aes.pde @@ -29,23 +29,20 @@ void loop () void prekey (int bits) { - aes.iv_inc(); byte iv [N_BLOCK] ; - byte plain_p[sizeof(plain) + N_BLOCK]; + byte plain_p[sizeof(plain) + (N_BLOCK - (sizeof(plain) % 8)) - 1]; byte cipher[sizeof(plain_p)]; - aes.do_aes_encrypt(plain,sizeof(plain),cipher,key,bits); aes.get_IV(iv); - aes.do_aes_dencrypt(cipher,aes.get_size(),plain,key,bits,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 - -Serial.println(sizeof(plain_p)); -Serial.println(sizeof(cipher)); - + printf("\n\nPLAIN :"); - aes.printArray(plain_p); + aes.printArray(plain); printf("\nCIPHER:"); aes.printArray(cipher); + printf("\nPlain2:"); + aes.printArray(plain_p); printf("\n============================================================\n"); }