Botan  1.11.4
cbc.h
Go to the documentation of this file.
1 /*
2 * CBC Mode
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7 
8 #ifndef BOTAN_CBC_H__
9 #define BOTAN_CBC_H__
10 
11 #include <botan/block_cipher.h>
12 #include <botan/key_filt.h>
13 #include <botan/mode_pad.h>
14 #include <botan/buf_filt.h>
15 
16 namespace Botan {
17 
18 /**
19 * CBC Encryption
20 */
21 class BOTAN_DLL CBC_Encryption : public Keyed_Filter,
22  private Buffered_Filter
23  {
24  public:
25  std::string name() const;
26 
27  void set_iv(const InitializationVector& iv);
28 
29  void set_key(const SymmetricKey& key) { cipher->set_key(key); }
30 
31  Key_Length_Specification key_spec() const override { return cipher->key_spec(); }
32 
33  bool valid_iv_length(size_t iv_len) const
34  { return (iv_len == cipher->block_size()); }
35 
38 
41  const SymmetricKey& key,
42  const InitializationVector& iv);
43 
44  ~CBC_Encryption() { delete cipher; delete padder; }
45  private:
46  void buffered_block(const byte input[], size_t input_length);
47  void buffered_final(const byte input[], size_t input_length);
48 
49  void write(const byte input[], size_t input_length);
50  void end_msg();
51 
52  BlockCipher* cipher;
53  const BlockCipherModePaddingMethod* padder;
55  };
56 
57 /**
58 * CBC Decryption
59 */
60 class BOTAN_DLL CBC_Decryption : public Keyed_Filter,
61  private Buffered_Filter
62  {
63  public:
64  std::string name() const;
65 
66  void set_iv(const InitializationVector& iv);
67 
68  void set_key(const SymmetricKey& key) { cipher->set_key(key); }
69 
70  Key_Length_Specification key_spec() const override { return cipher->key_spec(); }
71 
72  bool valid_iv_length(size_t iv_len) const
73  { return (iv_len == cipher->block_size()); }
74 
77 
80  const SymmetricKey& key,
81  const InitializationVector& iv);
82 
83  ~CBC_Decryption() { delete cipher; delete padder; }
84  private:
85  void buffered_block(const byte input[], size_t input_length);
86  void buffered_final(const byte input[], size_t input_length);
87 
88  void write(const byte[], size_t);
89  void end_msg();
90 
91  BlockCipher* cipher;
92  const BlockCipherModePaddingMethod* padder;
94  };
95 
96 }
97 
98 #endif