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.
|
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
|
|
|
|
|
2021-01-12 09:25:55 +01:00
|
|
|
#include <AK/Span.h>
|
2020-04-27 21:58:04 +04:30
|
|
|
#include <AK/Types.h>
|
|
|
|
|
2023-07-11 13:49:08 -04:00
|
|
|
namespace Crypto::Cipher {
|
2020-04-27 21:58:04 +04:30
|
|
|
|
2020-04-23 02:53:11 +04:30
|
|
|
enum class Intent {
|
|
|
|
Encryption,
|
|
|
|
Decryption,
|
|
|
|
};
|
2020-04-27 21:58:04 +04:30
|
|
|
|
2020-04-23 02:53:11 +04:30
|
|
|
enum class PaddingMode {
|
2020-05-29 19:52:30 +04:30
|
|
|
CMS, // RFC 1423
|
2020-04-23 02:53:11 +04:30
|
|
|
RFC5246, // very similar to CMS, but filled with |length - 1|, instead of |length|
|
|
|
|
Null,
|
2024-10-26 01:55:05 +02:00
|
|
|
ZeroLength,
|
2020-04-23 02:53:11 +04:30
|
|
|
// FIXME: We do not implement these yet
|
|
|
|
Bit,
|
|
|
|
Random,
|
|
|
|
Space,
|
|
|
|
};
|
2020-04-27 21:58:04 +04:30
|
|
|
|
2024-10-26 01:55:05 +02:00
|
|
|
bool padding_always_needs_extra_block(PaddingMode);
|
|
|
|
|
2020-04-23 02:53:11 +04:30
|
|
|
template<typename B, typename T>
|
|
|
|
class Cipher;
|
2020-04-27 21:58:04 +04:30
|
|
|
|
2020-04-23 02:53:11 +04:30
|
|
|
struct CipherBlock {
|
|
|
|
public:
|
|
|
|
explicit CipherBlock(PaddingMode mode)
|
|
|
|
: m_padding_mode(mode)
|
|
|
|
{
|
|
|
|
}
|
2020-04-27 21:58:04 +04:30
|
|
|
|
2021-01-12 09:25:55 +01:00
|
|
|
virtual ReadonlyBytes bytes() const = 0;
|
2020-04-27 21:58:04 +04:30
|
|
|
|
2020-08-15 18:47:25 +02:00
|
|
|
virtual void overwrite(ReadonlyBytes) = 0;
|
2022-04-01 20:58:27 +03:00
|
|
|
virtual void overwrite(u8 const* data, size_t size) { overwrite({ data, size }); }
|
2020-04-27 21:58:04 +04:30
|
|
|
|
2021-05-14 09:32:24 +04:30
|
|
|
virtual void apply_initialization_vector(ReadonlyBytes ivec) = 0;
|
2020-04-27 21:58:04 +04:30
|
|
|
|
2020-04-23 02:53:11 +04:30
|
|
|
PaddingMode padding_mode() const { return m_padding_mode; }
|
2020-08-11 23:30:49 +04:30
|
|
|
void set_padding_mode(PaddingMode mode) { m_padding_mode = mode; }
|
2020-04-27 21:58:04 +04:30
|
|
|
|
2020-04-23 02:53:11 +04:30
|
|
|
template<typename T>
|
|
|
|
void put(size_t offset, T value)
|
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(offset + sizeof(T) <= bytes().size());
|
2021-01-12 09:25:55 +01:00
|
|
|
auto* ptr = bytes().offset_pointer(offset);
|
2020-04-23 02:53:11 +04:30
|
|
|
auto index { 0 };
|
2020-04-27 21:58:04 +04:30
|
|
|
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(sizeof(T) <= 4);
|
2020-04-27 21:58:04 +04:30
|
|
|
|
2020-04-23 02:53:11 +04:30
|
|
|
if constexpr (sizeof(T) > 3)
|
|
|
|
ptr[index++] = (u8)(value >> 24);
|
2020-04-27 21:58:04 +04:30
|
|
|
|
2020-04-23 02:53:11 +04:30
|
|
|
if constexpr (sizeof(T) > 2)
|
|
|
|
ptr[index++] = (u8)(value >> 16);
|
2020-04-27 21:58:04 +04:30
|
|
|
|
2020-04-23 02:53:11 +04:30
|
|
|
if constexpr (sizeof(T) > 1)
|
|
|
|
ptr[index++] = (u8)(value >> 8);
|
2020-04-27 21:58:04 +04:30
|
|
|
|
2020-04-23 02:53:11 +04:30
|
|
|
ptr[index] = (u8)value;
|
|
|
|
}
|
2020-04-27 21:58:04 +04:30
|
|
|
|
2021-04-15 10:43:29 -07:00
|
|
|
protected:
|
|
|
|
virtual ~CipherBlock() = default;
|
|
|
|
|
2020-04-23 02:53:11 +04:30
|
|
|
private:
|
2021-01-12 09:25:55 +01:00
|
|
|
virtual Bytes bytes() = 0;
|
2020-04-23 02:53:11 +04:30
|
|
|
PaddingMode m_padding_mode;
|
|
|
|
};
|
2020-04-27 21:58:04 +04:30
|
|
|
|
2020-04-23 02:53:11 +04:30
|
|
|
struct CipherKey {
|
2021-01-12 09:25:55 +01:00
|
|
|
virtual ReadonlyBytes bytes() const = 0;
|
2023-07-07 22:48:11 -04:00
|
|
|
static bool is_valid_key_size(size_t) { return false; }
|
2020-04-27 21:58:04 +04:30
|
|
|
|
2022-02-26 10:32:08 -07:00
|
|
|
virtual ~CipherKey() = default;
|
2020-04-27 21:58:04 +04:30
|
|
|
|
2020-04-23 02:53:11 +04:30
|
|
|
protected:
|
2020-12-19 15:07:09 +01:00
|
|
|
virtual void expand_encrypt_key(ReadonlyBytes user_key, size_t bits) = 0;
|
|
|
|
virtual void expand_decrypt_key(ReadonlyBytes user_key, size_t bits) = 0;
|
2020-04-23 02:53:11 +04:30
|
|
|
size_t bits { 0 };
|
|
|
|
};
|
2020-04-27 21:58:04 +04:30
|
|
|
|
2020-04-23 02:53:11 +04:30
|
|
|
template<typename KeyT = CipherKey, typename BlockT = CipherBlock>
|
|
|
|
class Cipher {
|
|
|
|
public:
|
|
|
|
using KeyType = KeyT;
|
|
|
|
using BlockType = BlockT;
|
2020-04-27 21:58:04 +04:30
|
|
|
|
2024-04-07 14:21:52 -04:00
|
|
|
explicit Cipher(PaddingMode mode)
|
2020-04-23 02:53:11 +04:30
|
|
|
: m_padding_mode(mode)
|
|
|
|
{
|
|
|
|
}
|
2020-04-27 21:58:04 +04:30
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
virtual KeyType const& key() const = 0;
|
2020-04-23 02:53:11 +04:30
|
|
|
virtual KeyType& key() = 0;
|
2020-04-27 21:58:04 +04:30
|
|
|
|
2021-05-13 12:13:11 +04:30
|
|
|
constexpr static size_t block_size() { return BlockType::block_size(); }
|
2020-04-27 21:58:04 +04:30
|
|
|
|
2020-04-23 02:53:11 +04:30
|
|
|
PaddingMode padding_mode() const { return m_padding_mode; }
|
2020-04-27 21:58:04 +04:30
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
virtual void encrypt_block(BlockType const& in, BlockType& out) = 0;
|
|
|
|
virtual void decrypt_block(BlockType const& in, BlockType& out) = 0;
|
2020-04-27 21:58:04 +04:30
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
virtual ByteString class_name() const = 0;
|
2020-04-08 01:54:50 +04:30
|
|
|
|
2021-04-15 10:43:29 -07:00
|
|
|
protected:
|
|
|
|
virtual ~Cipher() = default;
|
|
|
|
|
2020-04-23 02:53:11 +04:30
|
|
|
private:
|
|
|
|
PaddingMode m_padding_mode;
|
|
|
|
};
|
2020-04-27 21:58:04 +04:30
|
|
|
}
|