Coverage Report

Created: 2021-02-21 07:20

/src/botan/build/include/botan/internal/cbc.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
* CBC 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_CBC_H_
10
#define BOTAN_MODE_CBC_H_
11
12
#include <botan/cipher_mode.h>
13
#include <botan/block_cipher.h>
14
#include <botan/internal/mode_pad.h>
15
16
namespace Botan {
17
18
/**
19
* CBC Mode
20
*/
21
class CBC_Mode : public Cipher_Mode
22
   {
23
   public:
24
      std::string name() const override;
25
26
      size_t update_granularity() const override;
27
28
      Key_Length_Specification key_spec() const override;
29
30
      size_t default_nonce_length() const override;
31
32
      bool valid_nonce_length(size_t n) const override;
33
34
      void clear() override;
35
36
      void reset() override;
37
38
   protected:
39
      CBC_Mode(BlockCipher* cipher, BlockCipherModePaddingMethod* padding);
40
41
10.0k
      const BlockCipher& cipher() const { return *m_cipher; }
42
43
      const BlockCipherModePaddingMethod& padding() const
44
0
         {
45
0
         BOTAN_ASSERT_NONNULL(m_padding);
46
0
         return *m_padding;
47
0
         }
48
49
2.13k
      size_t block_size() const { return m_block_size; }
50
51
1.92k
      secure_vector<uint8_t>& state() { return m_state; }
52
53
11.4k
      uint8_t* state_ptr() { return m_state.data(); }
54
55
   private:
56
      void start_msg(const uint8_t nonce[], size_t nonce_len) override;
57
58
      void key_schedule(const uint8_t key[], size_t length) override;
59
60
      std::unique_ptr<BlockCipher> m_cipher;
61
      std::unique_ptr<BlockCipherModePaddingMethod> m_padding;
62
      secure_vector<uint8_t> m_state;
63
      size_t m_block_size;
64
   };
65
66
/**
67
* CBC Encryption
68
*/
69
class CBC_Encryption : public CBC_Mode
70
   {
71
   public:
72
      /**
73
      * @param cipher block cipher to use
74
      * @param padding padding method to use
75
      */
76
      CBC_Encryption(BlockCipher* cipher, BlockCipherModePaddingMethod* padding) :
77
573
         CBC_Mode(cipher, padding) {}
78
79
      size_t process(uint8_t buf[], size_t size) override;
80
81
      void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
82
83
      size_t output_length(size_t input_length) const override;
84
85
      size_t minimum_final_size() const override;
86
   };
87
88
/**
89
* CBC Encryption with ciphertext stealing (CBC-CS3 variant)
90
*/
91
class CTS_Encryption final : public CBC_Encryption
92
   {
93
   public:
94
      /**
95
      * @param cipher block cipher to use
96
      */
97
0
      explicit CTS_Encryption(BlockCipher* cipher) : CBC_Encryption(cipher, nullptr) {}
98
99
      size_t output_length(size_t input_length) const override;
100
101
      void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
102
103
      size_t minimum_final_size() const override;
104
105
      bool valid_nonce_length(size_t n) const override;
106
   };
107
108
/**
109
* CBC Decryption
110
*/
111
class CBC_Decryption : public CBC_Mode
112
   {
113
   public:
114
      /**
115
      * @param cipher block cipher to use
116
      * @param padding padding method to use
117
      */
118
      CBC_Decryption(BlockCipher* cipher, BlockCipherModePaddingMethod* padding) :
119
454
         CBC_Mode(cipher, padding), m_tempbuf(update_granularity()) {}
120
121
      size_t process(uint8_t buf[], size_t size) override;
122
123
      void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
124
125
      size_t output_length(size_t input_length) const override;
126
127
      size_t minimum_final_size() const override;
128
129
      void reset() override;
130
131
   private:
132
      secure_vector<uint8_t> m_tempbuf;
133
   };
134
135
/**
136
* CBC Decryption with ciphertext stealing (CBC-CS3 variant)
137
*/
138
class CTS_Decryption final : public CBC_Decryption
139
   {
140
   public:
141
      /**
142
      * @param cipher block cipher to use
143
      */
144
0
      explicit CTS_Decryption(BlockCipher* cipher) : CBC_Decryption(cipher, nullptr) {}
145
146
      void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
147
148
      size_t minimum_final_size() const override;
149
150
      bool valid_nonce_length(size_t n) const override;
151
   };
152
153
}
154
155
#endif