25 #include "utility/EndianUtil.h"
49 state.dataStarted =
false;
80 return blockCipher->
setKey(key, len);
88 memcpy(state.counter, iv, 12);
89 state.counter[12] = 0;
90 state.counter[13] = 0;
91 state.counter[14] = 0;
92 state.counter[15] = 1;
95 memset(state.nonce, 0, 16);
97 ghash.
reset(state.nonce);
100 uint64_t sizes[2] = {0, htobe64(((uint64_t)len) * 8)};
101 ghash.
update(sizes,
sizeof(sizes));
109 state.dataStarted =
false;
113 memset(state.nonce, 0, 16);
115 ghash.
reset(state.nonce);
129 static inline void increment(uint8_t counter[16])
132 carry += counter[15];
133 counter[15] = (uint8_t)carry;
134 carry = (carry >> 8) + counter[14];
135 counter[14] = (uint8_t)carry;
136 carry = (carry >> 8) + counter[13];
137 counter[13] = (uint8_t)carry;
138 carry = (carry >> 8) + counter[12];
139 counter[12] = (uint8_t)carry;
145 if (!state.dataStarted) {
147 state.dataStarted =
true;
151 uint8_t *out = output;
155 if (state.posn >= 16) {
156 increment(state.counter);
162 uint8_t temp = 16 - state.posn;
165 uint8_t *stream = state.stream + state.posn;
169 *out++ = *input++ ^ *stream++;
175 ghash.
update(output, len);
176 state.dataSize += len;
182 if (!state.dataStarted) {
184 state.dataStarted =
true;
189 state.dataSize += len;
194 if (state.posn >= 16) {
195 increment(state.counter);
201 uint8_t temp = 16 - state.posn;
204 uint8_t *stream = state.stream + state.posn;
208 *output++ = *input++ ^ *stream++;
216 if (!state.dataStarted) {
218 state.authSize += len;
226 uint64_t sizes[2] = {
227 htobe64(state.authSize * 8),
228 htobe64(state.dataSize * 8)
230 ghash.
update(sizes,
sizeof(sizes));
235 for (uint8_t posn = 0; posn < 16; ++posn)
236 state.stream[posn] ^= state.nonce[posn];
239 memcpy(tag, state.stream, len);
250 return secure_compare(state.counter, tag, len);
255 blockCipher->
clear();
virtual void clear()=0
Clears all security-sensitive state from this block cipher.
virtual bool setKey(const uint8_t *key, size_t len)=0
Sets the key to use for future encryption and decryption operations.
virtual void encryptBlock(uint8_t *output, const uint8_t *input)=0
Encrypts a single block using this cipher.
virtual size_t keySize() const =0
Default size of the key for this block cipher, in bytes.
void encrypt(uint8_t *output, const uint8_t *input, size_t len)
Encrypts an input buffer and writes the ciphertext to an output buffer.
void clear()
Clears all security-sensitive state from this cipher.
virtual ~GCMCommon()
Destroys this cipher object after clearing sensitive information.
size_t keySize() const
Default size of the key for this cipher, in bytes.
GCMCommon()
Constructs a new cipher in GCM mode.
bool setIV(const uint8_t *iv, size_t len)
Sets the initialization vector to use for future encryption and decryption operations.
size_t ivSize() const
Size of the initialization vector for this cipher, in bytes.
size_t tagSize() const
Returns the size of the authentication tag.
bool setKey(const uint8_t *key, size_t len)
Sets the key to use for future encryption and decryption operations.
void computeTag(void *tag, size_t len)
Finalizes the encryption process and computes the authentication tag.
void decrypt(uint8_t *output, const uint8_t *input, size_t len)
Decrypts an input buffer and writes the plaintext to an output buffer.
bool checkTag(const void *tag, size_t len)
Finalizes the decryption process and checks the authentication tag.
void addAuthData(const void *data, size_t len)
Adds extra data that will be authenticated but not encrypted.
void update(const void *data, size_t len)
Updates the message authenticator with more data.
void reset(const void *key)
Resets the GHASH message authenticator for a new session.
void clear()
Clears the authenticator's state, removing all sensitive data.
void pad()
Pads the input stream with zero bytes to a multiple of 16.
void finalize(void *token, size_t len)
Finalizes the authentication process and returns the token.