-
Notifications
You must be signed in to change notification settings - Fork 0
/
GCM.cpp
335 lines (305 loc) · 9.8 KB
/
GCM.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
* Copyright (C) 2015 Southern Storm Software, Pty Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include "GCM.h"
#include "Crypto.h"
#include "utility/EndianUtil.h"
#include <string.h>
/**
* \class GCMCommon GCM.h <GCM.h>
* \brief Concrete base class to assist with implementing GCM for
* 128-bit block ciphers.
*
* References: <a href="http://csrc.nist.gov/publications/nistpubs/800-38D/SP-800-38D.pdf">NIST SP 800-38D</a>,
* http://en.wikipedia.org/wiki/Galois/Counter_Mode
*
* \sa GCM
*/
/**
* \brief Constructs a new cipher in GCM mode.
*
* This constructor must be followed by a call to setBlockCipher().
*/
GCMCommon::GCMCommon()
: blockCipher(0)
{
state.authSize = 0;
state.dataSize = 0;
state.dataStarted = false;
state.posn = 16;
}
/**
* \brief Destroys this cipher object after clearing sensitive information.
*/
GCMCommon::~GCMCommon()
{
clean(state);
}
size_t GCMCommon::keySize() const
{
return blockCipher->keySize();
}
size_t GCMCommon::ivSize() const
{
// The GCM specification recommends an IV size of 96 bits.
return 12;
}
size_t GCMCommon::tagSize() const
{
return 16;
}
bool GCMCommon::setKey(const uint8_t *key, size_t len)
{
// Set the encryption key for the block cipher.
return blockCipher->setKey(key, len);
}
bool GCMCommon::setIV(const uint8_t *iv, size_t len)
{
// Format the counter block from the IV.
if (len == 12) {
// IV's of exactly 96 bits are used directly as the counter block.
memcpy(state.counter, iv, 12);
state.counter[12] = 0;
state.counter[13] = 0;
state.counter[14] = 0;
state.counter[15] = 1;
} else {
// IV's of other sizes are hashed to produce the counter block.
memset(state.nonce, 0, 16);
blockCipher->encryptBlock(state.nonce, state.nonce);
ghash.reset(state.nonce);
ghash.update(iv, len);
ghash.pad();
uint64_t sizes[2] = {0, htobe64(((uint64_t)len) * 8)};
ghash.update(sizes, sizeof(sizes));
clean(sizes);
ghash.finalize(state.counter, 16);
}
// Reset the GCM object ready to process auth or payload data.
state.authSize = 0;
state.dataSize = 0;
state.dataStarted = false;
state.posn = 16;
// Construct the hashing key by encrypting a zero block.
memset(state.nonce, 0, 16);
blockCipher->encryptBlock(state.nonce, state.nonce);
ghash.reset(state.nonce);
// Replace the hash key in "nonce" with the encrypted counter.
// This value will be XOR'ed with the final authentication hash
// value in computeTag().
blockCipher->encryptBlock(state.nonce, state.counter);
return true;
}
/**
* \brief Increments the least significant 32 bits of the counter block.
*
* \param counter The counter block to increment.
*/
static inline void increment(uint8_t counter[16])
{
uint16_t carry = 1;
carry += counter[15];
counter[15] = (uint8_t)carry;
carry = (carry >> 8) + counter[14];
counter[14] = (uint8_t)carry;
carry = (carry >> 8) + counter[13];
counter[13] = (uint8_t)carry;
carry = (carry >> 8) + counter[12];
counter[12] = (uint8_t)carry;
}
void GCMCommon::encrypt(uint8_t *output, const uint8_t *input, size_t len)
{
// Finalize the authenticated data if necessary.
if (!state.dataStarted) {
ghash.pad();
state.dataStarted = true;
}
// Encrypt the plaintext using the block cipher in counter mode.
uint8_t *out = output;
size_t size = len;
while (size > 0) {
// Create a new keystream block if necessary.
if (state.posn >= 16) {
increment(state.counter);
blockCipher->encryptBlock(state.stream, state.counter);
state.posn = 0;
}
// Encrypt as many bytes as we can using the keystream block.
uint8_t temp = 16 - state.posn;
if (temp > size)
temp = size;
uint8_t *stream = state.stream + state.posn;
state.posn += temp;
size -= temp;
while (temp > 0) {
*out++ = *input++ ^ *stream++;
--temp;
}
}
// Feed the ciphertext into the hash.
ghash.update(output, len);
state.dataSize += len;
}
void GCMCommon::decrypt(uint8_t *output, const uint8_t *input, size_t len)
{
// Finalize the authenticated data if necessary.
if (!state.dataStarted) {
ghash.pad();
state.dataStarted = true;
}
// Feed the ciphertext into the hash before we decrypt it.
ghash.update(input, len);
state.dataSize += len;
// Decrypt the plaintext using the block cipher in counter mode.
while (len > 0) {
// Create a new keystream block if necessary.
if (state.posn >= 16) {
increment(state.counter);
blockCipher->encryptBlock(state.stream, state.counter);
state.posn = 0;
}
// Decrypt as many bytes as we can using the keystream block.
uint8_t temp = 16 - state.posn;
if (temp > len)
temp = len;
uint8_t *stream = state.stream + state.posn;
state.posn += temp;
len -= temp;
while (temp > 0) {
*output++ = *input++ ^ *stream++;
--temp;
}
}
}
void GCMCommon::addAuthData(const void *data, size_t len)
{
if (!state.dataStarted) {
ghash.update(data, len);
state.authSize += len;
}
}
void GCMCommon::computeTag(void *tag, size_t len)
{
// Pad the hashed data and add the sizes.
ghash.pad();
uint64_t sizes[2] = {
htobe64(state.authSize * 8),
htobe64(state.dataSize * 8)
};
ghash.update(sizes, sizeof(sizes));
clean(sizes);
// Get the finalized hash, encrypt it with the nonce, and return the tag.
ghash.finalize(state.stream, 16);
for (uint8_t posn = 0; posn < 16; ++posn)
state.stream[posn] ^= state.nonce[posn];
if (len > 16)
len = 16;
memcpy(tag, state.stream, len);
}
bool GCMCommon::checkTag(const void *tag, size_t len)
{
// Can never match if the expected tag length is too long.
if (len > 16)
return false;
// Compute the tag and check it.
computeTag(state.counter, 16);
return secure_compare(state.counter, tag, len);
}
void GCMCommon::clear()
{
blockCipher->clear();
ghash.clear();
clean(state);
state.posn = 16;
}
/**
* \fn void GCMCommon::setBlockCipher(BlockCipher *cipher)
* \brief Sets the block cipher to use for this GCM object.
*
* \param cipher The block cipher to use to implement GCM mode.
* This object must have a block size of 128 bits (16 bytes).
*/
/**
* \class GCM GCM.h <GCM.h>
* \brief Implementation of the Galois Counter Mode (GCM).
*
* GCM mode converts a block cipher into an authenticated cipher
* that uses the block cipher T to encrypt and GHASH to authenticate.
*
* The size of the key is determined by the underlying block cipher T.
* The IV is recommended to be 96 bits (12 bytes) in length, but other
* lengths are supported as well. The default tagSize() is 128 bits
* (16 bytes) but the GCM specification does allow other tag sizes:
* 32, 64, 96, 104, 112, 120, or 128 bits (4, 8, 12, 13, 14, 15, or 16 bytes).
*
* The template parameter T must be a concrete subclass of BlockCipher
* indicating the specific block cipher to use. The block cipher must
* have a block size of 128 bits. For example, the following creates a
* GCM object using AES256 as the underlying cipher and then uses it
* to encrypt and authenticate a \c plaintext block:
*
* \code
* GCM<AES256> gcm;
* gcm.setKey(key, sizeof(key));
* gcm.setIV(iv, sizeof(iv));
* gcm.addAuthData(adata, sizeof(adata));
* gcm.encrypt(ciphertext, plaintext, sizeof(plaintext));
* gcm.computeTag(tag, sizeof(tag));
* \endcode
*
* The decryption process is almost identical to convert a \c ciphertext and
* \a tag back into plaintext and then check the tag:
*
* \code
* GCM<AES256> gcm;
* gcm.setKey(key, sizeof(key));
* gcm.setIV(iv, sizeof(iv));
* gcm.addAuthData(adata, sizeof(adata));
* gcm.decrypt(plaintext, ciphertext, sizeof(ciphertext));
* if (!gcm.checkTag(tag, sizeof(tag))) {
* // The data was invalid - do not use it.
* ...
* }
* \endcode
*
* The GCM class can also be used to implement GMAC message authentication
* by omitting the plaintext:
*
* \code
* GCM<AES256> gcm;
* gcm.setKey(key, sizeof(key));
* gcm.setIV(iv, sizeof(iv));
* gcm.addAuthData(adata1, sizeof(adata1));
* gcm.addAuthData(adata2, sizeof(adata1));
* ...
* gcm.addAuthData(adataN, sizeof(adataN));
* gcm.computeTag(tag, sizeof(tag));
* \endcode
*
* References: <a href="http://csrc.nist.gov/publications/nistpubs/800-38D/SP-800-38D.pdf">NIST SP 800-38D</a>,
* http://en.wikipedia.org/wiki/Galois/Counter_Mode
*
* \sa GCMCommon, GHASH
*/
/**
* \fn GCM::GCM()
* \brief Constructs a new GCM object for the block cipher T.
*/