Coverage Report

Created: 2025-04-11 06:34

/src/botan/build/include/internal/botan/internal/stream_mode.h
Line
Count
Source (jump to first uncovered line)
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
#include <botan/assert.h>
14
15
#if defined(BOTAN_HAS_STREAM_CIPHER)
16
   #include <botan/stream_cipher.h>
17
#endif
18
19
namespace Botan {
20
21
#if defined(BOTAN_HAS_STREAM_CIPHER)
22
23
class Stream_Cipher_Mode final : public Cipher_Mode {
24
   public:
25
      /**
26
      * @param cipher underyling stream cipher
27
      */
28
0
      explicit Stream_Cipher_Mode(std::unique_ptr<StreamCipher> cipher) : m_cipher(std::move(cipher)) {}
29
30
0
      size_t output_length(size_t input_length) const override { return input_length; }
31
32
0
      size_t update_granularity() const override { return 1; }
33
34
0
      size_t ideal_granularity() const override {
35
0
         const size_t buf_size = m_cipher->buffer_size();
36
0
         BOTAN_ASSERT_NOMSG(buf_size > 0);
37
0
         if(buf_size >= 256) {
38
0
            return buf_size;
39
0
         }
40
0
         return buf_size * (256 / buf_size);
41
0
      }
42
43
0
      size_t minimum_final_size() const override { return 0; }
44
45
0
      size_t default_nonce_length() const override { return 0; }
46
47
0
      bool valid_nonce_length(size_t nonce_len) const override { return m_cipher->valid_iv_length(nonce_len); }
48
49
0
      Key_Length_Specification key_spec() const override { return m_cipher->key_spec(); }
50
51
0
      std::string name() const override { return m_cipher->name(); }
52
53
0
      void clear() override {
54
0
         m_cipher->clear();
55
0
         reset();
56
0
      }
57
58
0
      void reset() override { /* no msg state */
59
0
      }
60
61
0
      bool has_keying_material() const override { return m_cipher->has_keying_material(); }
62
63
   private:
64
0
      void start_msg(const uint8_t nonce[], size_t nonce_len) override {
65
0
         if(nonce_len > 0) {
66
0
            m_cipher->set_iv(nonce, nonce_len);
67
0
         }
68
0
      }
69
70
0
      size_t process_msg(uint8_t buf[], size_t sz) override {
71
0
         m_cipher->cipher1(buf, sz);
72
0
         return sz;
73
0
      }
74
75
0
      void finish_msg(secure_vector<uint8_t>& buf, size_t offset) override { return update(buf, offset); }
76
77
0
      void key_schedule(std::span<const uint8_t> key) override { m_cipher->set_key(key); }
78
79
      std::unique_ptr<StreamCipher> m_cipher;
80
};
81
82
#endif
83
84
}  // namespace Botan
85
86
#endif