Botan 3.13.0
Crypto and TLS for C&
ctr.h
Go to the documentation of this file.
1/*
2* CTR-BE Mode
3* (C) 1999-2007 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#ifndef BOTAN_CTR_BE_H_
9#define BOTAN_CTR_BE_H_
10
11#include <botan/block_cipher.h>
12#include <botan/stream_cipher.h>
13
14namespace Botan {
15
16/**
17* CTR-BE (Counter mode, big-endian)
18*/
19class CTR_BE final : public StreamCipher {
20 public:
21 size_t default_iv_length() const override;
22
23 bool valid_iv_length(size_t iv_len) const override;
24
25 Key_Length_Specification key_spec() const override;
26
27 std::string name() const override;
28
29 std::unique_ptr<StreamCipher> new_object() const override;
30
31 void clear() override;
32
33 bool has_keying_material() const override;
34
35 size_t buffer_size() const override;
36
37 /**
38 * @param cipher the block cipher to use
39 */
40 explicit CTR_BE(std::unique_ptr<BlockCipher> cipher);
41
42 CTR_BE(std::unique_ptr<BlockCipher> cipher, size_t ctr_size);
43
44 void seek(uint64_t offset) override;
45
46 bool supports_seek() const override { return true; }
47
48 std::optional<uint64_t> remaining_keystream_bytes() const override;
49
50 private:
51 void key_schedule(std::span<const uint8_t> key) override;
52 void cipher_bytes(const uint8_t in[], uint8_t out[], size_t length) override;
53 void generate_keystream(uint8_t out[], size_t length) override;
54 void set_iv_bytes(const uint8_t iv[], size_t iv_len) override;
55 void add_counter(uint64_t counter);
56
57#if defined(BOTAN_HAS_CTR_BE_AVX2)
58 size_t ctr_proc_bs16_ctr4_avx2(const uint8_t in[], uint8_t out[], size_t length);
59#endif
60
61#if defined(BOTAN_HAS_CTR_BE_SIMD32)
62 size_t ctr_proc_bs16_ctr4_simd32(const uint8_t in[], uint8_t out[], size_t length);
63#endif
64
65 std::unique_ptr<BlockCipher> m_cipher;
66
67 const size_t m_block_size;
68 const size_t m_ctr_size;
69 const size_t m_ctr_blocks;
70
71 secure_vector<uint8_t> m_counter, m_pad;
72 std::vector<uint8_t> m_iv;
73 size_t m_pad_pos;
74 // Valid only when m_ctr_size < 8: bytes the user can still
75 // generate before the narrow counter would wrap.
76 uint64_t m_bytes_remaining = 0;
77};
78
79} // namespace Botan
80
81#endif
void clear() override
Definition ctr.cpp:41
bool supports_seek() const override
Definition ctr.h:46
size_t default_iv_length() const override
Definition ctr.cpp:57
std::optional< uint64_t > remaining_keystream_bytes() const override
Definition ctr.cpp:50
bool has_keying_material() const override
Definition ctr.cpp:77
size_t buffer_size() const override
Definition ctr.cpp:65
Key_Length_Specification key_spec() const override
Definition ctr.cpp:69
bool valid_iv_length(size_t iv_len) const override
Definition ctr.cpp:61
void seek(uint64_t offset) override
Definition ctr.cpp:263
std::unique_ptr< StreamCipher > new_object() const override
Definition ctr.cpp:73
std::string name() const override
Definition ctr.cpp:88
CTR_BE(std::unique_ptr< BlockCipher > cipher)
Definition ctr.cpp:21
void cipher(const uint8_t in[], uint8_t out[], size_t len)
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128