ArduinoLibs
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
AES256.cpp
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 #include "AES.h"
24 #include "Crypto.h"
25 #include <string.h>
26 
41 {
42  rounds = 14;
43  schedule = sched;
44 }
45 
46 AES256::~AES256()
47 {
48  clean(sched);
49 }
50 
55 size_t AES256::keySize() const
56 {
57  return 32;
58 }
59 
60 bool AES256::setKey(const uint8_t *key, size_t len)
61 {
62  if (len != 32)
63  return false;
64 
65  // Copy the key itself into the first 32 bytes of the schedule.
66  uint8_t *schedule = sched;
67  memcpy(schedule, key, 32);
68 
69  // Expand the key schedule until we have 240 bytes of expanded key.
70  uint8_t iteration = 1;
71  uint8_t n = 32;
72  uint8_t w = 8;
73  while (n < 240) {
74  if (w == 8) {
75  // Every 32 bytes (8 words) we need to apply the key schedule core.
76  keyScheduleCore(schedule + 32, schedule + 28, iteration);
77  schedule[32] ^= schedule[0];
78  schedule[33] ^= schedule[1];
79  schedule[34] ^= schedule[2];
80  schedule[35] ^= schedule[3];
81  ++iteration;
82  w = 0;
83  } else if (w == 4) {
84  // At the 16 byte mark we need to apply the S-box.
85  applySbox(schedule + 32, schedule + 28);
86  schedule[32] ^= schedule[0];
87  schedule[33] ^= schedule[1];
88  schedule[34] ^= schedule[2];
89  schedule[35] ^= schedule[3];
90  } else {
91  // Otherwise just XOR the word with the one 32 bytes previous.
92  schedule[32] = schedule[28] ^ schedule[0];
93  schedule[33] = schedule[29] ^ schedule[1];
94  schedule[34] = schedule[30] ^ schedule[2];
95  schedule[35] = schedule[31] ^ schedule[3];
96  }
97 
98  // Advance to the next word in the schedule.
99  schedule += 4;
100  n += 4;
101  ++w;
102  }
103 
104  return true;
105 }
AES256()
Constructs an AES 256-bit block cipher with no initial key.
Definition: AES256.cpp:40
bool setKey(const uint8_t *key, size_t len)
Sets the key to use for future encryption and decryption operations.
Definition: AES256.cpp:60
size_t keySize() const
Size of a 256-bit AES key in bytes.
Definition: AES256.cpp:55