2020-04-27 21:58:04 +04:30
|
|
|
/*
|
2021-04-23 00:43:01 +04:30
|
|
|
* Copyright (c) 2020, Ali Mohammad Pur <mpfard@serenityos.org>
|
2022-02-26 10:32:08 -07:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2025-02-23 19:26:35 +01:00
|
|
|
* Copyright (c) 2025, Altomani Gianluca <altomanigianluca@gmail.com>
|
2020-04-27 21:58:04 +04:30
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-04-27 21:58:04 +04:30
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2024-06-17 23:12:53 +01:00
|
|
|
#include <AK/ByteString.h>
|
2025-02-23 19:26:35 +01:00
|
|
|
#include <LibCrypto/OpenSSLForward.h>
|
2020-04-27 21:58:04 +04:30
|
|
|
|
2023-07-11 13:49:08 -04:00
|
|
|
namespace Crypto::Cipher {
|
2021-12-17 14:11:16 +01:00
|
|
|
|
2025-02-23 19:26:35 +01:00
|
|
|
class AESCipher {
|
2020-04-23 02:53:11 +04:30
|
|
|
public:
|
2025-02-23 19:26:35 +01:00
|
|
|
size_t block_size() const;
|
2020-04-27 21:58:04 +04:30
|
|
|
|
2025-02-23 19:26:35 +01:00
|
|
|
protected:
|
|
|
|
|
explicit AESCipher(EVP_CIPHER const* cipher, ReadonlyBytes key)
|
|
|
|
|
: m_cipher(cipher)
|
|
|
|
|
, m_key(key)
|
2020-04-23 02:53:11 +04:30
|
|
|
{
|
|
|
|
|
}
|
2020-04-27 21:58:04 +04:30
|
|
|
|
2025-02-23 19:26:35 +01:00
|
|
|
EVP_CIPHER const* m_cipher;
|
|
|
|
|
ReadonlyBytes m_key;
|
2020-04-23 02:53:11 +04:30
|
|
|
};
|
|
|
|
|
|
2025-02-23 19:26:35 +01:00
|
|
|
class AESCBCCipher final : public AESCipher {
|
|
|
|
|
public:
|
|
|
|
|
explicit AESCBCCipher(ReadonlyBytes key, bool no_padding = false);
|
2020-04-27 21:58:04 +04:30
|
|
|
|
2025-02-23 19:26:35 +01:00
|
|
|
ErrorOr<ByteBuffer> encrypt(ReadonlyBytes plaintext, ReadonlyBytes iv) const;
|
|
|
|
|
ErrorOr<ByteBuffer> decrypt(ReadonlyBytes ciphertext, ReadonlyBytes iv) const;
|
2020-04-27 21:58:04 +04:30
|
|
|
|
2020-04-23 02:53:11 +04:30
|
|
|
private:
|
2025-02-23 19:26:35 +01:00
|
|
|
bool m_no_padding { false };
|
2020-04-23 02:53:11 +04:30
|
|
|
};
|
2020-04-27 21:58:04 +04:30
|
|
|
|
2025-02-23 19:26:35 +01:00
|
|
|
class AESCTRCipher final : public AESCipher {
|
2020-04-23 02:53:11 +04:30
|
|
|
public:
|
2025-02-23 19:26:35 +01:00
|
|
|
explicit AESCTRCipher(ReadonlyBytes key);
|
2020-04-27 21:58:04 +04:30
|
|
|
|
2025-02-23 19:26:35 +01:00
|
|
|
ErrorOr<ByteBuffer> encrypt(ReadonlyBytes plaintext, ReadonlyBytes iv) const;
|
|
|
|
|
ErrorOr<ByteBuffer> decrypt(ReadonlyBytes ciphertext, ReadonlyBytes iv) const;
|
|
|
|
|
};
|
2020-04-27 21:58:04 +04:30
|
|
|
|
2025-02-23 19:26:35 +01:00
|
|
|
class AESGCMCipher final : public AESCipher {
|
|
|
|
|
public:
|
|
|
|
|
explicit AESGCMCipher(ReadonlyBytes key);
|
2020-04-27 21:58:04 +04:30
|
|
|
|
2025-02-23 19:26:35 +01:00
|
|
|
struct EncryptedData {
|
|
|
|
|
ByteBuffer ciphertext;
|
|
|
|
|
ByteBuffer tag;
|
|
|
|
|
};
|
2020-04-27 21:58:04 +04:30
|
|
|
|
2025-02-23 19:26:35 +01:00
|
|
|
ErrorOr<EncryptedData> encrypt(ReadonlyBytes plaintext, ReadonlyBytes iv, ReadonlyBytes aad, size_t taglen) const;
|
|
|
|
|
ErrorOr<ByteBuffer> decrypt(ReadonlyBytes ciphertext, ReadonlyBytes iv, ReadonlyBytes aad, ReadonlyBytes tag) const;
|
|
|
|
|
};
|
2020-04-27 21:58:04 +04:30
|
|
|
|
2025-02-23 19:26:35 +01:00
|
|
|
class AESKWCipher final : public AESCipher {
|
|
|
|
|
public:
|
|
|
|
|
explicit AESKWCipher(ReadonlyBytes key);
|
2020-04-08 01:54:50 +04:30
|
|
|
|
2025-02-23 19:26:35 +01:00
|
|
|
ErrorOr<ByteBuffer> wrap(ReadonlyBytes) const;
|
|
|
|
|
ErrorOr<ByteBuffer> unwrap(ReadonlyBytes) const;
|
2020-04-23 02:53:11 +04:30
|
|
|
};
|
2020-04-27 21:58:04 +04:30
|
|
|
|
|
|
|
|
}
|