Botan 3.0.0
Crypto and TLS for C&
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
17namespace Botan {
18
19#if defined(BOTAN_HAS_STREAM_CIPHER)
20
21class Stream_Cipher_Mode final : public Cipher_Mode
22 {
23 public:
24 /**
25 * @param cipher underyling stream cipher
26 */
27 explicit Stream_Cipher_Mode(std::unique_ptr<StreamCipher> cipher) :
28 m_cipher(std::move(cipher)) {}
29
30 size_t output_length(size_t input_length) const override { return input_length; }
31
32 size_t update_granularity() const override { return 1; }
33
34 size_t ideal_granularity() const override
35 {
36 const size_t buf_size = m_cipher->buffer_size();
37 BOTAN_ASSERT_NOMSG(buf_size > 0);
38 if(buf_size >= 256)
39 return buf_size;
40 return buf_size * (256 / buf_size);
41 }
42
43 size_t minimum_final_size() const override { return 0; }
44
45 size_t default_nonce_length() const override { return 0; }
46
47 bool valid_nonce_length(size_t nonce_len) const override
48 { return m_cipher->valid_iv_length(nonce_len); }
49
50 Key_Length_Specification key_spec() const override { return m_cipher->key_spec(); }
51
52 std::string name() const override { return m_cipher->name(); }
53
54 void clear() override
55 {
56 m_cipher->clear();
57 reset();
58 }
59
60 void reset() override { /* no msg state */ }
61
62 bool has_keying_material() const override { return m_cipher->has_keying_material(); }
63
64 private:
65 void start_msg(const uint8_t nonce[], size_t nonce_len) override
66 {
67 if(nonce_len > 0)
68 {
69 m_cipher->set_iv(nonce, nonce_len);
70 }
71 }
72
73 size_t process_msg(uint8_t buf[], size_t sz) override
74 {
75 m_cipher->cipher1(buf, sz);
76 return sz;
77 }
78
79 void finish_msg(secure_vector<uint8_t>& buf, size_t offset) override
80 { return update(buf, offset); }
81
82 void key_schedule(const uint8_t key[], size_t length) override
83 {
84 m_cipher->set_key(key, length);
85 }
86
87 std::unique_ptr<StreamCipher> m_cipher;
88 };
89
90#endif
91
92}
93
94#endif
#define BOTAN_ASSERT_NOMSG(expr)
Definition: assert.h:67
std::string name
int(* update)(CTX *, const void *, CC_LONG len)
int(* final)(unsigned char *, CTX *)
Definition: alg_id.cpp:12
Definition: bigint.h:1092