Coverage Report

Created: 2020-06-30 13:58

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