Coverage Report

Created: 2026-09-26 06:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/serenity/Userland/Libraries/LibCrypto/Cipher/Cipher.h
Line
Count
Source
1
/*
2
 * Copyright (c) 2020, Ali Mohammad Pur <mpfard@serenityos.org>
3
 * Copyright (c) 2022, the SerenityOS developers.
4
 *
5
 * SPDX-License-Identifier: BSD-2-Clause
6
 */
7
8
#pragma once
9
10
#include <AK/Optional.h>
11
#include <AK/Span.h>
12
#include <AK/Types.h>
13
14
namespace Crypto::Cipher {
15
16
enum class Intent {
17
    Encryption,
18
    Decryption,
19
};
20
21
enum class PaddingMode {
22
    CMS,     // RFC 1423
23
    RFC5246, // very similar to CMS, but filled with |length - 1|, instead of |length|
24
    Null,
25
    ZeroLength,
26
    // FIXME: We do not implement these yet
27
    Bit,
28
    Random,
29
    Space,
30
};
31
32
bool padding_always_needs_extra_block(PaddingMode);
33
34
template<typename B, typename T>
35
class Cipher;
36
37
struct CipherBlock {
38
public:
39
    explicit CipherBlock(PaddingMode mode)
40
0
        : m_padding_mode(mode)
41
0
    {
42
0
    }
43
44
    virtual ReadonlyBytes bytes() const = 0;
45
46
    virtual void overwrite(ReadonlyBytes) = 0;
47
0
    virtual void overwrite(u8 const* data, size_t size) { overwrite({ data, size }); }
48
49
    virtual void apply_initialization_vector(ReadonlyBytes ivec) = 0;
50
51
0
    PaddingMode padding_mode() const { return m_padding_mode; }
52
0
    void set_padding_mode(PaddingMode mode) { m_padding_mode = mode; }
53
54
    template<typename T>
55
    void put(size_t offset, T value)
56
0
    {
57
0
        VERIFY(offset + sizeof(T) <= bytes().size());
58
0
        auto* ptr = bytes().offset_pointer(offset);
59
0
        auto index { 0 };
60
61
0
        VERIFY(sizeof(T) <= 4);
62
63
        if constexpr (sizeof(T) > 3)
64
0
            ptr[index++] = (u8)(value >> 24);
65
66
        if constexpr (sizeof(T) > 2)
67
0
            ptr[index++] = (u8)(value >> 16);
68
69
        if constexpr (sizeof(T) > 1)
70
0
            ptr[index++] = (u8)(value >> 8);
71
72
0
        ptr[index] = (u8)value;
73
0
    }
74
75
protected:
76
0
    virtual ~CipherBlock() = default;
77
78
private:
79
    virtual Bytes bytes() = 0;
80
    PaddingMode m_padding_mode;
81
};
82
83
struct CipherKey {
84
    virtual ReadonlyBytes bytes() const = 0;
85
0
    static bool is_valid_key_size(size_t) { return false; }
86
87
0
    virtual ~CipherKey() = default;
88
89
protected:
90
    virtual void expand_encrypt_key(ReadonlyBytes user_key, size_t bits) = 0;
91
    virtual void expand_decrypt_key(ReadonlyBytes user_key, size_t bits) = 0;
92
    size_t bits { 0 };
93
};
94
95
template<typename KeyT = CipherKey, typename BlockT = CipherBlock>
96
class Cipher {
97
public:
98
    using KeyType = KeyT;
99
    using BlockType = BlockT;
100
101
    explicit Cipher(PaddingMode mode)
102
0
        : m_padding_mode(mode)
103
0
    {
104
0
    }
105
106
    virtual KeyType const& key() const = 0;
107
    virtual KeyType& key() = 0;
108
109
0
    constexpr static size_t block_size() { return BlockType::block_size(); }
110
111
0
    PaddingMode padding_mode() const { return m_padding_mode; }
112
113
    virtual void encrypt_block(BlockType const& in, BlockType& out) = 0;
114
    virtual void decrypt_block(BlockType const& in, BlockType& out) = 0;
115
116
#ifndef KERNEL
117
    virtual ByteString class_name() const = 0;
118
#endif
119
120
protected:
121
0
    virtual ~Cipher() = default;
122
123
private:
124
    PaddingMode m_padding_mode;
125
};
126
}