Botan  2.6.0
Crypto and TLS for C++11
stream_mode.h
Go to the documentation of this file.
1 /*
2 * (C) 2015 Jack Lloyd
3 * (C) 2016 Daniel Neus, Rohde & Schwarz Cybersecurity
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #ifndef BOTAN_STREAM_MODE_H_
9 #define BOTAN_STREAM_MODE_H_
10 
11 #include <botan/cipher_mode.h>
12 
13 #if defined(BOTAN_HAS_STREAM_CIPHER)
14  #include <botan/stream_cipher.h>
15 #endif
16 
17 namespace Botan {
18 
19 #if defined(BOTAN_HAS_STREAM_CIPHER)
20 
21 class BOTAN_PUBLIC_API(2,0) Stream_Cipher_Mode final : public Cipher_Mode
22  {
23  public:
24  /**
25  * @param cipher underyling stream cipher
26  */
27  explicit Stream_Cipher_Mode(StreamCipher* cipher) : m_cipher(cipher) {}
28 
29  size_t process(uint8_t buf[], size_t sz) override
30  {
31  m_cipher->cipher1(buf, sz);
32  return sz;
33  }
34 
35  void finish(secure_vector<uint8_t>& buf, size_t offset) override
36  { return update(buf, offset); }
37 
38  size_t output_length(size_t input_length) const override { return input_length; }
39 
40  size_t update_granularity() const override { return 1; }
41 
42  size_t minimum_final_size() const override { return 0; }
43 
44  size_t default_nonce_length() const override { return 0; }
45 
46  bool valid_nonce_length(size_t nonce_len) const override
47  { return m_cipher->valid_iv_length(nonce_len); }
48 
49  Key_Length_Specification key_spec() const override { return m_cipher->key_spec(); }
50 
51  std::string name() const override { return m_cipher->name(); }
52 
53  void clear() override
54  {
55  m_cipher->clear();
56  reset();
57  }
58 
59  void reset() override { /* no msg state */ }
60 
61  private:
62  void start_msg(const uint8_t nonce[], size_t nonce_len) override
63  {
64  if(nonce_len > 0)
65  {
66  m_cipher->set_iv(nonce, nonce_len);
67  }
68  }
69 
70  void key_schedule(const uint8_t key[], size_t length) override
71  {
72  m_cipher->set_key(key, length);
73  }
74 
75  std::unique_ptr<StreamCipher> m_cipher;
76  };
77 
78 #endif
79 
80 }
81 
82 #endif
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:27
Definition: alg_id.cpp:13