ArduinoLibs
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
AES.h
1 /*
2  * Copyright (C) 2015 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 AESCommon : public BlockCipher
29 {
30 public:
31  virtual ~AESCommon();
32 
33  size_t blockSize() const;
34 
35  void encryptBlock(uint8_t *output, const uint8_t *input);
36  void decryptBlock(uint8_t *output, const uint8_t *input);
37 
38  void clear();
39 
40 protected:
41  AESCommon();
42 
44  uint8_t rounds;
45  uint8_t *schedule;
46 
47  void keyScheduleCore(uint8_t *output, const uint8_t *input, uint8_t iteration);
48  void applySbox(uint8_t *output, const uint8_t *input);
51 private:
52  uint8_t state1[16];
53  uint8_t state2[16];
54 };
55 
56 class AES128 : public AESCommon
57 {
58 public:
59  AES128();
60  virtual ~AES128();
61 
62  size_t keySize() const;
63 
64  bool setKey(const uint8_t *key, size_t len);
65 
66 private:
67  uint8_t sched[176];
68 };
69 
70 class AES192 : public AESCommon
71 {
72 public:
73  AES192();
74  virtual ~AES192();
75 
76  size_t keySize() const;
77 
78  bool setKey(const uint8_t *key, size_t len);
79 
80 private:
81  uint8_t sched[208];
82 };
83 
84 class AES256 : public AESCommon
85 {
86 public:
87  AES256();
88  virtual ~AES256();
89 
90  size_t keySize() const;
91 
92  bool setKey(const uint8_t *key, size_t len);
93 
94 private:
95  uint8_t sched[240];
96 };
97 
98 #endif
void decryptBlock(uint8_t *output, const uint8_t *input)
Decrypts a single block using this cipher.
Definition: AESCommon.cpp:295
AES block cipher with 256-bit keys.
Definition: AES.h:84
Abstract base class for block ciphers.
Definition: BlockCipher.h:29
AESCommon()
Constructs an AES block cipher object.
Definition: AESCommon.cpp:125
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:144
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:324
bool setKey(const uint8_t *key, size_t len)
Sets the key to use for future encryption and decryption operations.
Definition: AES128.cpp:60
size_t keySize() const
Size of a 192-bit AES key in bytes.
Definition: AES192.cpp:55
void encryptBlock(uint8_t *output, const uint8_t *input)
Encrypts a single block using this cipher.
Definition: AESCommon.cpp:266
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:28
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:56
size_t keySize() const
Size of a 256-bit AES key in bytes.
Definition: AES256.cpp:55
AES128()
Constructs an AES 128-bit block cipher with no initial key.
Definition: AES128.cpp:40
AES block cipher with 192-bit keys.
Definition: AES.h:70
AES192()
Constructs an AES 192-bit block cipher with no initial key.
Definition: AES192.cpp:40