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);
50 };
51 
52 class AES128 : public AESCommon
53 {
54 public:
55  AES128();
56  virtual ~AES128();
57 
58  size_t keySize() const;
59 
60  bool setKey(const uint8_t *key, size_t len);
61 
62 private:
63  uint8_t sched[176];
64 };
65 
66 class AES192 : public AESCommon
67 {
68 public:
69  AES192();
70  virtual ~AES192();
71 
72  size_t keySize() const;
73 
74  bool setKey(const uint8_t *key, size_t len);
75 
76 private:
77  uint8_t sched[208];
78 };
79 
80 class AES256 : public AESCommon
81 {
82 public:
83  AES256();
84  virtual ~AES256();
85 
86  size_t keySize() const;
87 
88  bool setKey(const uint8_t *key, size_t len);
89 
90 private:
91  uint8_t sched[240];
92 };
93 
94 #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:80
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:142
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:326
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:264
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:52
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:66
AES192()
Constructs an AES 192-bit block cipher with no initial key.
Definition: AES192.cpp:40