Coverage Report

Created: 2024-11-29 06:10

/src/botan/build/include/internal/botan/internal/cfb.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
* CFB mode
3
* (C) 1999-2007,2013 Jack Lloyd
4
* (C) 2016 Daniel Neus, Rohde & Schwarz Cybersecurity
5
*
6
* Botan is released under the Simplified BSD License (see license.txt)
7
*/
8
9
#ifndef BOTAN_MODE_CFB_H_
10
#define BOTAN_MODE_CFB_H_
11
12
#include <botan/block_cipher.h>
13
#include <botan/cipher_mode.h>
14
15
namespace Botan {
16
17
/**
18
* CFB Mode
19
*/
20
class CFB_Mode : public Cipher_Mode {
21
   public:
22
      std::string name() const final;
23
24
      size_t update_granularity() const final;
25
26
      size_t ideal_granularity() const final;
27
28
      size_t minimum_final_size() const final;
29
30
      Key_Length_Specification key_spec() const final;
31
32
      size_t output_length(size_t input_length) const final;
33
34
      size_t default_nonce_length() const final;
35
36
      bool valid_nonce_length(size_t n) const final;
37
38
      void clear() final;
39
40
      void reset() final;
41
42
      bool has_keying_material() const final;
43
44
   protected:
45
      CFB_Mode(std::unique_ptr<BlockCipher> cipher, size_t feedback_bits);
46
47
      void shift_register();
48
49
0
      size_t feedback() const { return m_feedback_bytes; }
50
51
0
      const BlockCipher& cipher() const { return *m_cipher; }
52
53
0
      size_t block_size() const { return m_block_size; }
54
55
      secure_vector<uint8_t> m_state;
56
      secure_vector<uint8_t> m_keystream;
57
      size_t m_keystream_pos = 0;
58
59
   private:
60
      void start_msg(const uint8_t nonce[], size_t nonce_len) override;
61
      void key_schedule(std::span<const uint8_t> key) override;
62
63
      std::unique_ptr<BlockCipher> m_cipher;
64
      const size_t m_block_size;
65
      const size_t m_feedback_bytes;
66
};
67
68
/**
69
* CFB Encryption
70
*/
71
class CFB_Encryption final : public CFB_Mode {
72
   public:
73
      /**
74
      * If feedback_bits is zero, cipher->block_size() bytes will be used.
75
      * @param cipher block cipher to use
76
      * @param feedback_bits number of bits fed back into the shift register,
77
      * must be a multiple of 8
78
      */
79
      CFB_Encryption(std::unique_ptr<BlockCipher> cipher, size_t feedback_bits) :
80
0
            CFB_Mode(std::move(cipher), feedback_bits) {}
81
82
   private:
83
      size_t process_msg(uint8_t buf[], size_t size) override;
84
      void finish_msg(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
85
};
86
87
/**
88
* CFB Decryption
89
*/
90
class CFB_Decryption final : public CFB_Mode {
91
   public:
92
      /**
93
      * If feedback_bits is zero, cipher->block_size() bytes will be used.
94
      * @param cipher block cipher to use
95
      * @param feedback_bits number of bits fed back into the shift register,
96
      * must be a multiple of 8
97
      */
98
      CFB_Decryption(std::unique_ptr<BlockCipher> cipher, size_t feedback_bits) :
99
0
            CFB_Mode(std::move(cipher), feedback_bits) {}
100
101
   private:
102
      size_t process_msg(uint8_t buf[], size_t size) override;
103
      void finish_msg(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
104
};
105
106
}  // namespace Botan
107
108
#endif