23 #include "ChaChaPoly.h"
25 #include "utility/EndianUtil.h"
49 state.dataStarted =
false;
67 size_t ChaChaPoly::ivSize()
const
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;
107 if (!state.dataStarted) {
109 state.dataStarted =
true;
111 chacha.
encrypt(output, input, len);
112 poly1305.
update(output, len);
113 state.dataSize += len;
118 if (!state.dataStarted) {
120 state.dataStarted =
true;
122 poly1305.
update(input, len);
123 chacha.
encrypt(output, input, len);
124 state.dataSize += len;
129 if (!state.dataStarted) {
130 poly1305.
update(data, len);
131 state.authSize += len;
141 sizes[0] = htole64(state.authSize);
142 sizes[1] = htole64(state.dataSize);
143 poly1305.
update(sizes,
sizeof(sizes));
146 poly1305.
finalize(state.nonce, tag, len);
159 bool equal = secure_compare(temp, tag, len);
ChaChaPoly()
Constructs a new ChaChaPoly authenticated cipher.
void clear()
Clears all security-sensitive state from this cipher.
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 decrypt(uint8_t *output, const uint8_t *input, size_t len)
Decrypts an input buffer and writes the plaintext to an output buffer.
void addAuthData(const void *data, size_t len)
Adds extra data that will be authenticated but not encrypted.
virtual ~ChaChaPoly()
Destroys this 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.
size_t tagSize() const
Returns the size of the authentication tag.
void computeTag(void *tag, size_t len)
Finalizes the encryption process and computes the authentication tag.
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.
bool setKey(const uint8_t *key, size_t len)
Sets the key 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.
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.
void reset(const void *key)
Resets the Poly1305 message authenticator for a new session.
void finalize(const void *nonce, void *token, size_t len)
Finalizes the authentication process and returns the token.
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.
void update(const void *data, size_t len)
Updates the message authenticator with more data.