Arduino Cryptography Library
AES192.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 
27 #if defined(CRYPTO_AES_DEFAULT) || defined(CRYPTO_DOC)
28 
43 {
44  rounds = 12;
45  schedule = sched;
46 }
47 
48 AES192::~AES192()
49 {
50  clean(sched);
51 }
52 
57 size_t AES192::keySize() const
58 {
59  return 24;
60 }
61 
62 bool AES192::setKey(const uint8_t *key, size_t len)
63 {
64  if (len != 24)
65  return false;
66 
67  // Copy the key itself into the first 24 bytes of the schedule.
68  uint8_t *schedule = sched;
69  memcpy(schedule, key, 24);
70 
71  // Expand the key schedule until we have 208 bytes of expanded key.
72  uint8_t iteration = 1;
73  uint8_t n = 24;
74  uint8_t w = 6;
75  while (n < 208) {
76  if (w == 6) {
77  // Every 24 bytes (6 words) we need to apply the key schedule core.
78  keyScheduleCore(schedule + 24, schedule + 20, iteration);
79  schedule[24] ^= schedule[0];
80  schedule[25] ^= schedule[1];
81  schedule[26] ^= schedule[2];
82  schedule[27] ^= schedule[3];
83  ++iteration;
84  w = 0;
85  } else {
86  // Otherwise just XOR the word with the one 24 bytes previous.
87  schedule[24] = schedule[20] ^ schedule[0];
88  schedule[25] = schedule[21] ^ schedule[1];
89  schedule[26] = schedule[22] ^ schedule[2];
90  schedule[27] = schedule[23] ^ schedule[3];
91  }
92 
93  // Advance to the next word in the schedule.
94  schedule += 4;
95  n += 4;
96  ++w;
97  }
98 
99  return true;
100 }
101 
102 #endif // CRYPTO_AES_DEFAULT
size_t keySize() const
Size of a 192-bit AES key in bytes.
Definition: AES192.cpp:57
bool setKey(const uint8_t *key, size_t len)
Sets the key to use for future encryption and decryption operations.
Definition: AES192.cpp:62
AES192()
Constructs an AES 192-bit block cipher with no initial key.
Definition: AES192.cpp:42