Smart-Home am Beispiel der Präsenzerkennung im Raum Projektarbeit Lennart Heimbs, Johannes Krug, Sebastian Dohle und Kevin Holzschuh bei Prof. Oliver Hofmann SS2019
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AES.h 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. #ifndef __AES_H__
  2. #define __AES_H__
  3. #include "AES_config.h"
  4. /*
  5. ---------------------------------------------------------------------------
  6. Copyright (c) 1998-2008, Brian Gladman, Worcester, UK. All rights reserved.
  7. LICENSE TERMS
  8. The redistribution and use of this software (with or without changes)
  9. is allowed without the payment of fees or royalties provided that:
  10. 1. source code distributions include the above copyright notice, this
  11. list of conditions and the following disclaimer;
  12. 2. binary distributions include the above copyright notice, this list
  13. of conditions and the following disclaimer in their documentation;
  14. 3. the name of the copyright holder is not used to endorse products
  15. built using this software without specific written permission.
  16. DISCLAIMER
  17. This software is provided 'as is' with no explicit or implied warranties
  18. in respect of its properties, including, but not limited to, correctness
  19. and/or fitness for purpose.
  20. ---------------------------------------------------------------------------
  21. Issue 09/09/2006
  22. This is an AES implementation that uses only 8-bit byte operations on the
  23. cipher state.
  24. */
  25. /* code was modified by george spanos <spaniakos@gmail.com>
  26. * 16/12/14
  27. */
  28. /** AES class */
  29. class AES
  30. {
  31. public:
  32. /* The following calls are for a precomputed key schedule
  33. NOTE: If the length_type used for the key length is an
  34. unsigned 8-bit character, a key length of 256 bits must
  35. be entered as a length in bytes (valid inputs are hence
  36. 128, 192, 16, 24 and 32).
  37. */
  38. /** \fn AES()
  39. * \brief AES constructor
  40. *
  41. * This function initialized an instance of AES.
  42. */
  43. AES();
  44. /** Set the cipher key for the pre-keyed version.
  45. * @param key[] pointer to the key string.
  46. * @param keylen Integer that indicates the length of the key.
  47. * @note NOTE: If the length_type used for the key length is an unsigned 8-bit character,
  48. * a key length of 256 bits must be entered as a length in bytes
  49. * (valid inputs are hence 128, 192, 16, 24 and 32).
  50. *
  51. */
  52. byte set_key (byte key[], int keylen) ;
  53. /** clean up subkeys after use.
  54. *
  55. */
  56. void clean () ; // delete key schedule after use
  57. /** copying and xoring utilities.
  58. * @param *AESt byte pointer of the AEStination array.
  59. * @param *src byte pointer of the source array.
  60. * @param n byte, indicating the sizeof the bytes to be copied.
  61. * @note this is an alternative for memcpy(void *s1,const void *s2, site_t n),
  62. * i have not updated the function in the implementation yet, but it is considered a future plan.
  63. *
  64. */
  65. void copy_n_bytes (byte * AESt, byte * src, byte n) ;
  66. /** Encrypt a single block of 16 bytes .
  67. * @param plain Array of the plaintext.
  68. * @param cipher Array of the ciphertext.
  69. * @note The N_BLOCK is defined in AES_config.h as,
  70. * @code #define N_ROW 4
  71. * #define N_COL 4
  72. * #define N_BLOCK (N_ROW * N_COL)
  73. * @endcode
  74. * Changed to that will change the Block_size.
  75. * @return 0 if SUCCESS or -1 if FAILURE
  76. *
  77. */
  78. byte encrypt (byte plain [N_BLOCK], byte cipher [N_BLOCK]) ;
  79. /** CBC encrypt a number of blocks (input and return an IV).
  80. *
  81. * @param *plain Pointer, points to the plaintex.
  82. * @param *cipher Pointer, points to the ciphertext that will be created.
  83. * @param n_block integer, indicated the number of blocks to be ciphered.
  84. * @param iv byte Array that holds the IV (initialization vector).
  85. * @return 0 if SUCCESS or -1 if FAILURE
  86. *
  87. */
  88. byte cbc_encrypt (byte * plain, byte * cipher, int n_block, byte iv [N_BLOCK]) ;
  89. /** CBC encrypt a number of blocks (input and return an IV).
  90. *
  91. * @param *plain Pointer, points to the plaintex.
  92. * @param *cipher Pointer, points to the ciphertext that will be created.
  93. * @param n_block integer, indicated the number of blocks to be ciphered.
  94. * @return 0 if SUCCESS or -1 if FAILURE
  95. *
  96. */
  97. byte cbc_encrypt (byte * plain, byte * cipher, int n_block) ;
  98. /** Decrypt a single block of 16 bytes
  99. * @param cipher Array of the ciphertext.
  100. * @param plain Array of the plaintext.
  101. * @note The N_BLOCK is defined in AES_config.h as,
  102. * @code #define N_ROW 4
  103. * #define N_COL 4
  104. * #define N_BLOCK (N_ROW * N_COL)
  105. * @endcode
  106. * Changed to that will change the Block_size.
  107. * @return 0 if SUCCESS or -1 if FAILURE
  108. *
  109. */
  110. byte decrypt (byte cipher [N_BLOCK], byte plain [N_BLOCK]) ;
  111. /** CBC decrypt a number of blocks (input and return an IV)
  112. *
  113. * @param *cipher Pointer, points to the ciphertext that will be created.
  114. * @param *plain Pointer, points to the plaintex.
  115. * @param n_block integer, indicated the number of blocks to be ciphered.
  116. * @param iv byte Array that holds the IV (initialization vector).
  117. * @return 0 if SUCCESS or -1 if FAILURE
  118. *
  119. */
  120. byte cbc_decrypt (byte * cipher, byte * plain, int n_block, byte iv [N_BLOCK]) ;
  121. /** CBC decrypt a number of blocks (input and return an IV)
  122. *
  123. * @param *cipher Pointer, points to the ciphertext that will be created.
  124. * @param *plain Pointer, points to the plaintex.
  125. * @param n_block integer, indicated the number of blocks to be ciphered.
  126. * @return 0 if SUCCESS or -1 if FAILURE
  127. *
  128. */
  129. byte cbc_decrypt (byte * cipher, byte * plain, int n_block) ;
  130. /** Sets IV (initialization vector) and IVC (IV counter).
  131. * This function changes the ivc and iv variables needed for AES.
  132. *
  133. * @param IVCl int or hex value of iv , ex. 0x0000000000000001
  134. * @note example:
  135. * @code unsigned long long int my_iv = 01234567; @endcode
  136. */
  137. void set_IV(unsigned long long int IVCl);
  138. /** increase the iv (initialization vector) and IVC (IV counter) by 1
  139. *
  140. * This function increased the VI by one step in order to have a different IV each time
  141. *
  142. */
  143. void iv_inc();
  144. /** Getter method for size
  145. *
  146. * This function return the size
  147. * @return an integer, that is the size of the of the padded plaintext,
  148. * thus, the size of the ciphertext.
  149. */
  150. int get_size();
  151. /** Setter method for size
  152. *
  153. * This function sets the size of the plaintext+pad
  154. *
  155. */
  156. void set_size(int sizel);
  157. /** Getter method for IV
  158. *
  159. * This function return the IV
  160. * @param out byte pointer that gets the IV.
  161. * @return none, the IV is writed to the out pointer.
  162. */
  163. void get_IV(byte *out);
  164. /** Calculates the size of the plaintext and the padding.
  165. *
  166. * Calculates the size of theplaintext with the padding
  167. * and the size of the padding needed. Moreover it stores them in their class variables.
  168. *
  169. * @param p_size the size of the byte array ex sizeof(plaintext)
  170. */
  171. void calc_size_n_pad(int p_size);
  172. /** Pads the plaintext
  173. *
  174. * This function pads the plaintext and returns an char array with the
  175. * plaintext and the padding in order for the plaintext to be compatible with
  176. * 16bit size blocks required by AES
  177. *
  178. * @param in the string of the plaintext in a byte array
  179. * @param out The string of the out array.
  180. * @return no return, The padded plaintext is stored in the out pointer.
  181. */
  182. void padPlaintext(void* in,byte* out);
  183. /** Check the if the padding is correct.
  184. *
  185. * This functions checks the padding of the plaintext.
  186. *
  187. * @param in the string of the plaintext in a byte array
  188. * @param size the size of the string
  189. * @return true if correct / false if not
  190. */
  191. bool CheckPad(byte* in,int size);
  192. /** Prints the array given.
  193. *
  194. * This function prints the given array and pad,
  195. * It is mainlly used for debugging purpuses or to output the string.
  196. *
  197. * @param output[] the string of the text in a byte array
  198. * @param p_pad optional, used to print with out the padding characters
  199. */
  200. void printArray(byte output[],bool p_pad = true);
  201. /** Prints the array given.
  202. *
  203. * This function prints the given array in Hexadecimal.
  204. *
  205. * @param output[] the string of the text in a byte array
  206. * @param sizel the size of the array.
  207. */
  208. void printArray(byte output[],int sizel);
  209. /** User friendly implementation of AES-CBC encryption.
  210. *
  211. * @param *plain pointer to the plaintext
  212. * @param size_p size of the plaintext
  213. * @param *cipher pointer to the ciphertext
  214. * @param *key pointer to the key that will be used.
  215. * @param bits bits of the encryption/decrpytion
  216. * @param ivl the initialization vector IV that will be used for encryption.
  217. * @note The key will be stored in class variable.
  218. */
  219. void do_aes_encrypt(byte *plain,int size_p,byte *cipher,byte *key, int bits, byte ivl [N_BLOCK]);
  220. /** User friendly implementation of AES-CBC encryption.
  221. *
  222. * @param *plain pointer to the plaintext
  223. * @param size_p size of the plaintext
  224. * @param *cipher pointer to the ciphertext
  225. * @param *key pointer to the key that will be used.
  226. * @param bits bits of the encryption/decrpytion
  227. * @note The key will be stored in class variable.
  228. */
  229. void do_aes_encrypt(byte *plain,int size_p,byte *cipher,byte *key, int bits);
  230. /** User friendly implementation of AES-CBC decryption.
  231. *
  232. * @param *cipher pointer to the ciphertext
  233. * @param size_c size of the ciphertext
  234. * @param *plain pointer to the plaintext
  235. * @param *key pointer to the key that will be used.
  236. * @param bits bits of the encryption/decrpytion
  237. * @param ivl the initialization vector IV that will be used for decryption.
  238. * @note The key will be stored in class variable.
  239. */
  240. void do_aes_decrypt(byte *cipher,int size_c,byte *plain,byte *key, int bits, byte ivl [N_BLOCK]);
  241. /** User friendly implementation of AES-CBC decryption.
  242. *
  243. * @param *cipher pointer to the ciphertext
  244. * @param size_c size of the ciphertext
  245. * @param *plain pointer to the plaintext
  246. * @param *key pointer to the key that will be used.
  247. * @param bits bits of the encryption/decrpytion
  248. * @note The key will be stored in class variable.
  249. */
  250. void do_aes_decrypt(byte *cipher,int size_c,byte *plain,byte *key, int bits);
  251. #if defined(AES_LINUX)
  252. /**
  253. * used in linux in order to retrieve the time in milliseconds.
  254. *
  255. * @return returns the milliseconds in a double format.
  256. */
  257. double millis();
  258. #endif
  259. private:
  260. int round ;/**< holds the number of rounds to be used. */
  261. byte key_sched [KEY_SCHEDULE_BYTES]
  262. ;/**< holds the pre-computed key for the encryption/decrpytion. */
  263. unsigned long long int IVC;/**< holds the initialization vector counter in numerical format. */
  264. byte iv[16];/**< holds the initialization vector that will be used in the cipher. */
  265. int pad;/**< holds the size of the padding. */
  266. int size;/**< hold the size of the plaintext to be ciphered */
  267. #if defined(AES_LINUX)
  268. timeval tv;/**< holds the time value on linux */
  269. byte arr_pad[15];/**< holds the hexadecimal padding values on linux */
  270. #else
  271. byte arr_pad[15];// = { 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f };/**< holds the hexadecimal padding values */
  272. #endif
  273. } ;
  274. #endif
  275. /**
  276. * @defgroup aeslib AES library for Arduino and Raspberry pi
  277. * @ingroup internals
  278. *
  279. * @section AesGoals design Goals
  280. *
  281. * This library is AESigned to be...
  282. * @li Fast and efficient.
  283. * @li Able to effectively encrypt and decrypt any size of string.
  284. * @li Able to encrypt and decrypt using AES
  285. * @li Able to encrypt and decrypt using AES-CBC
  286. * @li Easy for the user to use in his programs.
  287. *
  288. * @section Acknowledgements Acknowledgements
  289. * This is an AES library for the Arduino, based on tzikis's AES library, which you can find <a href= "https://github.com/tzikis/arduino">here:</a>.<br />
  290. * Tzikis library was based on scottmac`s library, which you can find <a href="https://github.com/scottmac/arduino">here:</a><br />
  291. *
  292. * @section Installation Installation
  293. * <h3>Arduino</h3>
  294. * Create a folder named _AES_ in the _libraries_ folder inside your Arduino sketch folder. If the
  295. * libraries folder doesn't exist, create it. Then copy everything inside. (re)launch the Arduino IDE.<br />
  296. * You're done. Time for a mojito
  297. *
  298. * <h3>Raspberry pi</h3>
  299. * <b>install</b><br /><br />
  300. *
  301. * sudo make install<br />
  302. * cd examples_Rpi<br />
  303. * make<br /><br />
  304. *
  305. * <b>What to do after changes to the library</b><br /><br />
  306. * sudo make clean<br />
  307. * sudo make install<br />
  308. * cd examples_Rpi<br />
  309. * make clean<br />
  310. * make<br /><br />
  311. * <b>What to do after changes to a sketch</b><br /><br />
  312. * cd examples_Rpi<br />
  313. * make \<sketch\><br /><br />
  314. * or <br />
  315. * make clean<br />
  316. * make<br /><br /><br />
  317. * <b>How to start a sketch</b><br /><br />
  318. * cd examples_Rpi<br />
  319. * sudo ./\<sketch\><br /><br />
  320. *
  321. * @section AesNews News
  322. *
  323. * If issues are discovered with the documentation, please report them <a href="https://github.com/spaniakos/spaniakos.github.io/issues"> here</a>
  324. * @section AesUseful Useful References
  325. *
  326. * Please refer to:
  327. *
  328. * @li <a href="http://spaniakos.github.io/AES/classAES.html"><b>AES</b> Class Documentation</a>
  329. * @li <a href="https://github.com/spaniakos/AES/archive/master.zip"><b>Download</b></a>
  330. * @li <a href="https://github.com/spaniakos/AES/"><b>Source Code</b></a>
  331. * @li <a href="http://spaniakos.github.io/">All spaniakos Documentation Main Page</a>
  332. *
  333. * @section AesBoard_Support Board Support
  334. *
  335. * Most standard Arduino based boards are supported:
  336. * - Arduino
  337. * - Intel Galileo support
  338. * - Raspberry Pi Support
  339. *
  340. * - The library has not been tested to other boards, but it should suppport ATMega 328 based boards,Mega Boards,Arduino Due,ATTiny board
  341. */