ArduinoLibs
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
AES.h
1 /*
2  * Copyright (C) 2015,2018 Southern Storm Software, Pty Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #ifndef CRYPTO_AES_h
24 #define CRYPTO_AES_h
25 
26 #include "BlockCipher.h"
27 
28 class AESTiny128;
29 class AESTiny256;
30 class AESSmall128;
31 class AESSmall256;
32 
33 class AESCommon : public BlockCipher
34 {
35 public:
36  virtual ~AESCommon();
37 
38  size_t blockSize() const;
39 
40  void encryptBlock(uint8_t *output, const uint8_t *input);
41  void decryptBlock(uint8_t *output, const uint8_t *input);
42 
43  void clear();
44 
45 protected:
46  AESCommon();
47 
49  uint8_t rounds;
50  uint8_t *schedule;
51 
52  static void subBytesAndShiftRows(uint8_t *output, const uint8_t *input);
53  static void inverseShiftRowsAndSubBytes(uint8_t *output, const uint8_t *input);
54  static void mixColumn(uint8_t *output, uint8_t *input);
55  static void inverseMixColumn(uint8_t *output, const uint8_t *input);
56  static void keyScheduleCore(uint8_t *output, const uint8_t *input, uint8_t iteration);
57  static void applySbox(uint8_t *output, const uint8_t *input);
60  friend class AESTiny128;
61  friend class AESTiny256;
62  friend class AESSmall128;
63  friend class AESSmall256;
64 };
65 
66 class AES128 : public AESCommon
67 {
68 public:
69  AES128();
70  virtual ~AES128();
71 
72  size_t keySize() const;
73 
74  bool setKey(const uint8_t *key, size_t len);
75 
76 private:
77  uint8_t sched[176];
78 };
79 
80 class AES192 : public AESCommon
81 {
82 public:
83  AES192();
84  virtual ~AES192();
85 
86  size_t keySize() const;
87 
88  bool setKey(const uint8_t *key, size_t len);
89 
90 private:
91  uint8_t sched[208];
92 };
93 
94 class AES256 : public AESCommon
95 {
96 public:
97  AES256();
98  virtual ~AES256();
99 
100  size_t keySize() const;
101 
102  bool setKey(const uint8_t *key, size_t len);
103 
104 private:
105  uint8_t sched[240];
106 };
107 
108 class AESTiny256 : public BlockCipher
109 {
110 public:
111  AESTiny256();
112  virtual ~AESTiny256();
113 
114  size_t blockSize() const;
115  size_t keySize() const;
116 
117  bool setKey(const uint8_t *key, size_t len);
118 
119  void encryptBlock(uint8_t *output, const uint8_t *input);
120  void decryptBlock(uint8_t *output, const uint8_t *input);
121 
122  void clear();
123 
124 private:
125  uint8_t schedule[32];
126 };
127 
128 class AESSmall256 : public AESTiny256
129 {
130 public:
131  AESSmall256();
132  virtual ~AESSmall256();
133 
134  bool setKey(const uint8_t *key, size_t len);
135 
136  void decryptBlock(uint8_t *output, const uint8_t *input);
137 
138  void clear();
139 
140 private:
141  uint8_t reverse[32];
142 };
143 
144 class AESTiny128 : public BlockCipher
145 {
146 public:
147  AESTiny128();
148  virtual ~AESTiny128();
149 
150  size_t blockSize() const;
151  size_t keySize() const;
152 
153  bool setKey(const uint8_t *key, size_t len);
154 
155  void encryptBlock(uint8_t *output, const uint8_t *input);
156  void decryptBlock(uint8_t *output, const uint8_t *input);
157 
158  void clear();
159 
160 private:
161  uint8_t schedule[16];
162 };
163 
164 class AESSmall128 : public AESTiny128
165 {
166 public:
167  AESSmall128();
168  virtual ~AESSmall128();
169 
170  bool setKey(const uint8_t *key, size_t len);
171 
172  void decryptBlock(uint8_t *output, const uint8_t *input);
173 
174  void clear();
175 
176 private:
177  uint8_t reverse[16];
178 };
179 
180 #endif
void clear()
Clears all security-sensitive state from this block cipher.
Definition: AES256.cpp:275
size_t keySize() const
Size of a 256-bit AES key in bytes.
Definition: AES256.cpp:209
AES block cipher with 256-bit keys and tiny memory usage.
Definition: AES.h:108
size_t keySize() const
Size of a 128-bit AES key in bytes.
Definition: AES128.cpp:172
void clear()
Clears all security-sensitive state from this block cipher.
Definition: AES128.cpp:238
void decryptBlock(uint8_t *output, const uint8_t *input)
Decrypts a single block using this cipher.
Definition: AESCommon.cpp:299
AES block cipher with 256-bit keys.
Definition: AES.h:94
AES block cipher with 128-bit keys and tiny memory usage.
Definition: AES.h:144
Abstract base class for block ciphers.
Definition: BlockCipher.h:29
void decryptBlock(uint8_t *output, const uint8_t *input)
Decrypts a single block using this cipher.
Definition: AES128.cpp:304
AESSmall256()
Constructs an AES 256-bit block cipher with no initial key.
Definition: AES256.cpp:306
AESCommon()
Constructs an AES block cipher object.
Definition: AESCommon.cpp:125
void encryptBlock(uint8_t *output, const uint8_t *input)
Encrypts a single block using this cipher.
Definition: AES128.cpp:187
size_t keySize() const
Size of a 128-bit AES key in bytes.
Definition: AES128.cpp:55
bool setKey(const uint8_t *key, size_t len)
Sets the key to use for future encryption and decryption operations.
Definition: AES192.cpp:60
size_t blockSize() const
Size of an AES block in bytes.
Definition: AESCommon.cpp:142
void decryptBlock(uint8_t *output, const uint8_t *input)
Decrypts a single block using this cipher.
Definition: AES128.cpp:233
void clear()
Clears all security-sensitive state from this block cipher.
Definition: AES128.cpp:348
virtual ~AESCommon()
Destroys this AES block cipher object after clearing sensitive information.
Definition: AESCommon.cpp:134
void clear()
Clears all security-sensitive state from this block cipher.
Definition: AESCommon.cpp:330
void decryptBlock(uint8_t *output, const uint8_t *input)
Decrypts a single block using this cipher.
Definition: AES256.cpp:270
bool setKey(const uint8_t *key, size_t len)
Sets the key to use for future encryption and decryption operations.
Definition: AES128.cpp:60
void encryptBlock(uint8_t *output, const uint8_t *input)
Encrypts a single block using this cipher.
Definition: AES256.cpp:224
size_t keySize() const
Size of a 192-bit AES key in bytes.
Definition: AES192.cpp:55
void decryptBlock(uint8_t *output, const uint8_t *input)
Decrypts a single block using this cipher.
Definition: AES256.cpp:349
size_t blockSize() const
Size of an AES block in bytes.
Definition: AES128.cpp:163
AESTiny128()
Constructs an AES 128-bit block cipher with no initial key.
Definition: AES128.cpp:150
void encryptBlock(uint8_t *output, const uint8_t *input)
Encrypts a single block using this cipher.
Definition: AESCommon.cpp:268
AES256()
Constructs an AES 256-bit block cipher with no initial key.
Definition: AES256.cpp:40
Abstract base class for AES block ciphers.
Definition: AES.h:33
bool setKey(const uint8_t *key, size_t len)
Sets the key to use for future encryption and decryption operations.
Definition: AES256.cpp:60
AES block cipher with 128-bit keys.
Definition: AES.h:66
bool setKey(const uint8_t *key, size_t len)
Sets the key to use for future encryption and decryption operations.
Definition: AES256.cpp:214
void clear()
Clears all security-sensitive state from this block cipher.
Definition: AES256.cpp:393
AESTiny256()
Constructs an AES 256-bit block cipher with no initial key.
Definition: AES256.cpp:187
bool setKey(const uint8_t *key, size_t len)
Sets the key to use for future encryption and decryption operations.
Definition: AES128.cpp:177
size_t keySize() const
Size of a 256-bit AES key in bytes.
Definition: AES256.cpp:55
bool setKey(const uint8_t *key, size_t len)
Sets the key to use for future encryption and decryption operations.
Definition: AES256.cpp:315
AES128()
Constructs an AES 128-bit block cipher with no initial key.
Definition: AES128.cpp:40
AES block cipher with 128-bit keys and reduced memory usage.
Definition: AES.h:164
size_t blockSize() const
Size of an AES block in bytes.
Definition: AES256.cpp:200
AES block cipher with 192-bit keys.
Definition: AES.h:80
AES block cipher with 256-bit keys and reduced memory usage.
Definition: AES.h:128
bool setKey(const uint8_t *key, size_t len)
Sets the key to use for future encryption and decryption operations.
Definition: AES128.cpp:278
AES192()
Constructs an AES 192-bit block cipher with no initial key.
Definition: AES192.cpp:40
AESSmall128()
Constructs an AES 128-bit block cipher with no initial key.
Definition: AES128.cpp:269