AES Encryption Library for Arduino and Raspberry Pi
Spaniakos - AES Encryption Library for Arduino and Raspberry Pi
AES.h
1 #ifndef __AES_H__
2 #define __AES_H__
3 
4 #include "AES_config.h"
5 /*
6  ---------------------------------------------------------------------------
7  Copyright (c) 1998-2008, Brian Gladman, Worcester, UK. All rights reserved.
8 
9  LICENSE TERMS
10 
11  The redistribution and use of this software (with or without changes)
12  is allowed without the payment of fees or royalties provided that:
13 
14  1. source code distributions include the above copyright notice, this
15  list of conditions and the following disclaimer;
16 
17  2. binary distributions include the above copyright notice, this list
18  of conditions and the following disclaimer in their documentation;
19 
20  3. the name of the copyright holder is not used to endorse products
21  built using this software without specific written permission.
22 
23  DISCLAIMER
24 
25  This software is provided 'as is' with no explicit or implied warranties
26  in respect of its properties, including, but not limited to, correctness
27  and/or fitness for purpose.
28  ---------------------------------------------------------------------------
29  Issue 09/09/2006
30 
31  This is an AES implementation that uses only 8-bit byte operations on the
32  cipher state.
33  */
34 
35  /* code was modified by george spanos <spaniakos@gmail.com>
36  * 16/12/14
37  */
38 
39 class AES
40 {
41  public:
42 
43 /* The following calls are for a precomputed key schedule
44 
45  NOTE: If the length_type used for the key length is an
46  unsigned 8-bit character, a key length of 256 bits must
47  be entered as a length in bytes (valid inputs are hence
48  128, 192, 16, 24 and 32).
49 */
55  AES();
56 
65  byte set_key (byte key[], int keylen) ;
66 
70  void clean () ; // delete key schedule after use
71 
80  void copy_n_bytes (byte * AESt, byte * src, byte n) ;
81 
94  byte encrypt (byte plain [N_BLOCK], byte cipher [N_BLOCK]) ;
95 
105  byte cbc_encrypt (byte * plain, byte * cipher, int n_block, byte iv [N_BLOCK]) ;
106 
115  byte cbc_encrypt (byte * plain, byte * cipher, int n_block) ;
116 
117 
130  byte decrypt (byte cipher [N_BLOCK], byte plain [N_BLOCK]) ;
131 
141  byte cbc_decrypt (byte * cipher, byte * plain, int n_block, byte iv [N_BLOCK]) ;
142 
151  byte cbc_decrypt (byte * cipher, byte * plain, int n_block) ;
152 
160  void set_IV(unsigned long long int IVCl);
161 
167  void iv_inc();
168 
175  int get_size();
176 
182  void set_size(int sizel);
183 
190  void get_IV(byte *out);
191 
199  void calc_size_n_pad(int p_size);
200 
211  void padPlaintext(void* in,byte* out);
212 
221  bool CheckPad(byte* in,int size);
222 
231  void printArray(byte output[],bool p_pad = true);
232 
240  void printArray(byte output[],int sizel);
241 
252  void do_aes_encrypt(byte *plain,int size_p,byte *cipher,byte *key, int bits, byte ivl [N_BLOCK]);
253 
263  void do_aes_encrypt(byte *plain,int size_p,byte *cipher,byte *key, int bits);
264 
275  void do_aes_decrypt(byte *cipher,int size_c,byte *plain,byte *key, int bits, byte ivl [N_BLOCK]);
276 
286  void do_aes_decrypt(byte *cipher,int size_c,byte *plain,byte *key, int bits);
287 
288  #if defined(AES_LINUX)
289 
294  double millis();
295  #endif
296  private:
297  int round ;
298  byte key_sched [KEY_SCHEDULE_BYTES] ;
299  unsigned long long int IVC;
300  byte iv[16];
301  int pad;
302  int size;
303  #if defined(AES_LINUX)
304  timeval tv;
305  byte arr_pad[15];
306  #else
307  byte arr_pad[15] = { 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f };
308  #endif
309 } ;
310 
311 
312 #endif
313 
bool CheckPad(byte *in, int size)
Definition: AES.cpp:507
void do_aes_encrypt(byte *plain, int size_p, byte *cipher, byte *key, int bits, byte ivl[N_BLOCK])
Definition: AES.cpp:552
Definition: AES.h:39
byte cbc_decrypt(byte *cipher, byte *plain, int n_block, byte iv[N_BLOCK])
Definition: AES.cpp:413
byte key_sched[KEY_SCHEDULE_BYTES]
Definition: AES.h:298
byte encrypt(byte plain[N_BLOCK], byte cipher[N_BLOCK])
Definition: AES.cpp:335
unsigned long long int IVC
Definition: AES.h:299
void do_aes_decrypt(byte *cipher, int size_c, byte *plain, byte *key, int bits, byte ivl[N_BLOCK])
Definition: AES.cpp:574
byte decrypt(byte cipher[N_BLOCK], byte plain[N_BLOCK])
Definition: AES.cpp:390
void iv_inc()
Definition: AES.cpp:457
void clean()
Definition: AES.cpp:310
double millis()
Definition: AES.cpp:594
byte arr_pad[15]
Definition: AES.h:305
void padPlaintext(void *in, byte *out)
Definition: AES.cpp:497
timeval tv
Definition: AES.h:304
byte iv[16]
Definition: AES.h:300
void calc_size_n_pad(int p_size)
Definition: AES.cpp:485
int size
Definition: AES.h:302
void set_IV(unsigned long long int IVCl)
Definition: AES.cpp:449
int pad
Definition: AES.h:301
void get_IV(byte *out)
Definition: AES.cpp:478
int round
Definition: AES.h:297
void set_size(int sizel)
Definition: AES.cpp:471
int get_size()
Definition: AES.cpp:465
void copy_n_bytes(byte *AESt, byte *src, byte n)
Definition: AES.cpp:319
void printArray(byte output[], bool p_pad=true)
Definition: AES.cpp:523
AES()
AES constructor.
Definition: AES.cpp:231
byte set_key(byte key[], int keylen)
Definition: AES.cpp:254
byte cbc_encrypt(byte *plain, byte *cipher, int n_block, byte iv[N_BLOCK])
Definition: AES.cpp:358