23 #include "ChaChaPoly.h"
25 #include "utility/EndianUtil.h"
49 state.dataStarted =
false;
81 return chacha.
setKey(key, len);
87 if (!chacha.
setIV(iv, len))
92 chacha.keystreamBlock(data);
94 memcpy(state.nonce, data + 4, 16);
100 state.dataStarted =
false;
106 if (!state.dataStarted) {
108 state.dataStarted =
true;
110 chacha.
encrypt(output, input, len);
111 poly1305.
update(output, len);
112 state.dataSize += len;
117 if (!state.dataStarted) {
119 state.dataStarted =
true;
121 poly1305.
update(input, len);
122 chacha.
encrypt(output, input, len);
123 state.dataSize += len;
128 if (!state.dataStarted) {
129 poly1305.
update(data, len);
130 state.authSize += len;
140 sizes[0] = htole64(state.authSize);
141 sizes[1] = htole64(state.dataSize);
142 poly1305.
update(sizes,
sizeof(sizes));
145 poly1305.
finalize(state.nonce, tag, len);
158 bool equal = secure_compare(temp, tag, len);
void finalize(const void *nonce, void *token, size_t len)
Finalizes the authentication process and returns the token.
void reset(const void *key)
Resets the Poly1305 message authenticator for a new session.
bool setKey(const uint8_t *key, size_t len)
Sets the key to use for future encryption and decryption operations.
virtual ~ChaChaPoly()
Destroys this ChaChaPoly authenticated cipher.
size_t tagSize() const
Returns the size of the authentication tag.
bool setIV(const uint8_t *iv, size_t len)
Sets the initialization vector to use for future encryption and decryption operations.
bool setIV(const uint8_t *iv, size_t len)
Sets the initialization vector to use for future encryption and decryption operations.
size_t keySize() const
Default size of the key for this cipher, in bytes.
void computeTag(void *tag, size_t len)
Finalizes the encryption process and computes the authentication tag.
ChaChaPoly()
Constructs a new ChaChaPoly authenticated cipher.
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 addAuthData(const void *data, size_t len)
Adds extra data that will be authenticated but not encrypted.
void encrypt(uint8_t *output, const uint8_t *input, size_t len)
Encrypts an input buffer and writes the ciphertext to an output buffer.
size_t ivSize() const
Size of the initialization vector for this cipher, in bytes.
void update(const void *data, size_t len)
Updates the message authenticator with more data.
void clear()
Clears all security-sensitive state from this cipher.
void clear()
Clears all security-sensitive state from this cipher.
void decrypt(uint8_t *output, const uint8_t *input, size_t len)
Decrypts an input buffer and writes the plaintext to an output buffer.
void pad()
Pads the input stream with zero bytes to a multiple of 16.
void clear()
Clears the authenticator's state, removing all sensitive data.
bool setKey(const uint8_t *key, size_t len)
Sets the key to use for future encryption and decryption operations.
bool checkTag(const void *tag, size_t len)
Finalizes the decryption process and checks the authentication tag.