Botan 3.13.0
Crypto and TLS for C&
shake_cipher.h
Go to the documentation of this file.
1/*
2 * SHAKE-128 and SHAKE-256 as a stream ciphers
3 * (C) 2016 Jack Lloyd
4 * 2022 René Meusel, Michael Boric - Rohde & Schwarz Cybersecurity
5 *
6 * Botan is released under the Simplified BSD License (see license.txt)
7 */
8
9#ifndef BOTAN_SHAKE_CIPHER_H_
10#define BOTAN_SHAKE_CIPHER_H_
11
12#include <botan/stream_cipher.h>
13#include <botan/internal/keccak_perm.h>
14
15namespace Botan {
16
17/**
18* Base class for SHAKE-based XOFs presented as a stream cipher
19*/
20class SHAKE_Cipher : public StreamCipher {
21 protected:
22 explicit SHAKE_Cipher(size_t keccak_capacity);
23
24 public:
25 /**
26 * Seeking is not supported, this function will throw
27 */
28 void seek(uint64_t offset) final;
29
30 bool supports_seek() const final { return false; }
31
32 std::optional<uint64_t> remaining_keystream_bytes() const override { return {}; }
33
34 void clear() final;
35
37
38 bool has_keying_material() const final { return m_has_keying_material; }
39
40 size_t buffer_size() const final { return m_keccak.byte_rate(); }
41
42 private:
43 void key_schedule(std::span<const uint8_t> key) final;
44 /**
45 * Produce more XOF output
46 */
47 void cipher_bytes(const uint8_t in[], uint8_t out[], size_t length) final;
48 void generate_keystream(uint8_t out[], size_t length) override;
49
50 void generate_keystream_internal(std::span<uint8_t> out);
51
52 /**
53 * IV not supported, this function will throw unless iv_len == 0
54 */
55 void set_iv_bytes(const uint8_t iv[], size_t iv_len) final;
56
57 private:
58 Keccak_Permutation m_keccak;
59 bool m_has_keying_material;
60 secure_vector<uint8_t> m_keystream_buffer;
61 size_t m_bytes_generated;
62};
63
64class SHAKE_128_Cipher final : public SHAKE_Cipher {
65 public:
67
68 std::string name() const override { return "SHAKE-128"; }
69
70 std::unique_ptr<StreamCipher> new_object() const override { return std::make_unique<SHAKE_128_Cipher>(); }
71};
72
73class SHAKE_256_Cipher final : public SHAKE_Cipher {
74 public:
76
77 std::string name() const override { return "SHAKE-256"; }
78
79 std::unique_ptr<StreamCipher> new_object() const override { return std::make_unique<SHAKE_256_Cipher>(); }
80};
81
82} // namespace Botan
83
84#endif
std::unique_ptr< StreamCipher > new_object() const override
std::string name() const override
std::unique_ptr< StreamCipher > new_object() const override
std::string name() const override
bool has_keying_material() const final
std::optional< uint64_t > remaining_keystream_bytes() const override
Key_Length_Specification key_spec() const final
size_t buffer_size() const final
SHAKE_Cipher(size_t keccak_capacity)
void seek(uint64_t offset) final
bool supports_seek() const final
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128