1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 14:17:36 +00:00

LibCrypto: Add Chacha20Poly1305

This commit is contained in:
stelar7 2023-09-25 19:46:21 +02:00 committed by Ali Mohammad Pur
parent 4c5b9fa6a2
commit 73ef102b01
6 changed files with 430 additions and 1 deletions

View file

@ -0,0 +1,35 @@
/*
* Copyright (c) 2023, stelar7 <dudedbz@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/ByteBuffer.h>
namespace Crypto::AEAD {
class ChaCha20Poly1305 {
public:
explicit ChaCha20Poly1305(ReadonlyBytes key, ReadonlyBytes nonce)
{
m_key = MUST(ByteBuffer::copy(key));
m_nonce = MUST(ByteBuffer::copy(nonce));
}
ErrorOr<ByteBuffer> encrypt(ReadonlyBytes aad, ReadonlyBytes plaintext);
ErrorOr<ByteBuffer> decrypt(ReadonlyBytes aad, ReadonlyBytes ciphertext);
ErrorOr<ByteBuffer> poly1305_key();
static bool verify_tag(ReadonlyBytes encrypted, ReadonlyBytes decrypted);
private:
u8 pad_to_16(ReadonlyBytes data)
{
return 16 - (data.size() % 16);
}
ByteBuffer m_key;
ByteBuffer m_nonce;
};
}