Coverage Report

Created: 2025-04-11 06:34

/src/botan/build/include/internal/botan/internal/cshake_xof.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * cSHAKE-128 and cSHAKE-256 as XOFs
3
 *
4
 * (C) 2016-2023 Jack Lloyd
5
 *     2022-2023 René Meusel - Rohde & Schwarz Cybersecurity
6
 *
7
 * Botan is released under the Simplified BSD License (see license.txt)
8
 */
9
10
#ifndef BOTAN_CSHAKE_XOF_H_
11
#define BOTAN_CSHAKE_XOF_H_
12
13
#include <botan/xof.h>
14
#include <botan/internal/keccak_perm.h>
15
16
#include <vector>
17
18
namespace Botan {
19
20
/**
21
 * Base class for cSHAKE-based XOFs
22
 */
23
class BOTAN_TEST_API cSHAKE_XOF : public XOF {
24
   protected:
25
      /**
26
       * Defines a concrete instance of a cSHAKE XOF.
27
       *
28
       * @param capacity      either 256 or 512
29
       * @param function_name a domain separator for Keccak-based functions
30
       *                      derived from cSHAKE
31
       */
32
      cSHAKE_XOF(size_t capacity, std::vector<uint8_t> function_name);
33
      cSHAKE_XOF(size_t capacity, std::span<const uint8_t> function_name);
34
      cSHAKE_XOF(size_t capacity, std::string_view function_name);
35
36
   public:
37
      std::string provider() const final;
38
39
      bool valid_salt_length(size_t salt_length) const final;
40
41
      size_t block_size() const final;
42
43
0
      bool accepts_input() const final { return !m_output_generated; }
44
45
   protected:
46
0
      const std::vector<uint8_t>& function_name() const { return m_function_name; }
47
48
   private:
49
      /**
50
       * @param salt  the S value for cSHAKE (see NIST SP.800-185)
51
       * @param key   not supported, must be an empty buffer
52
       */
53
      void start_msg(std::span<const uint8_t> salt, std::span<const uint8_t> key) final;
54
      void add_data(std::span<const uint8_t> input) final;
55
      void generate_bytes(std::span<uint8_t> output) final;
56
      void reset() final;
57
58
   private:
59
      Keccak_Permutation m_keccak;
60
      std::vector<uint8_t> m_function_name;
61
      bool m_output_generated;
62
};
63
64
/**
65
 * customizable SHAKE-128 as defined in NIST SP.800-185
66
 * This class is meant for internal use only and is not exposed via XOF::create().
67
 */
68
class BOTAN_TEST_API cSHAKE_128_XOF final : public cSHAKE_XOF {
69
   public:
70
0
      cSHAKE_128_XOF(std::vector<uint8_t> function_name) : cSHAKE_XOF(256, std::move(function_name)) {}
71
72
0
      cSHAKE_128_XOF(std::span<const uint8_t> function_name) : cSHAKE_XOF(256, function_name) {}
73
74
0
      cSHAKE_128_XOF(std::string_view function_name) : cSHAKE_XOF(256, function_name) {}
75
76
0
      std::string name() const final { return "cSHAKE-128"; }
77
78
0
      std::unique_ptr<XOF> copy_state() const final { return std::make_unique<cSHAKE_128_XOF>(*this); }
79
80
0
      std::unique_ptr<XOF> new_object() const final { return std::make_unique<cSHAKE_128_XOF>(function_name()); }
81
};
82
83
/**
84
 * customizable SHAKE-256 as defined in NIST SP.800-185
85
 * This class is meant for internal use only and is not exposed via XOF::create().
86
 */
87
class BOTAN_TEST_API cSHAKE_256_XOF final : public cSHAKE_XOF {
88
   public:
89
0
      cSHAKE_256_XOF(std::vector<uint8_t> function_name) : cSHAKE_XOF(512, std::move(function_name)) {}
90
91
0
      cSHAKE_256_XOF(std::span<const uint8_t> function_name) : cSHAKE_XOF(512, function_name) {}
92
93
0
      cSHAKE_256_XOF(std::string_view function_name) : cSHAKE_XOF(512, function_name) {}
94
95
0
      std::string name() const final { return "cSHAKE-256"; }
96
97
0
      std::unique_ptr<XOF> copy_state() const final { return std::make_unique<cSHAKE_256_XOF>(*this); }
98
99
0
      std::unique_ptr<XOF> new_object() const final { return std::make_unique<cSHAKE_256_XOF>(function_name()); }
100
};
101
102
}  // namespace Botan
103
104
#endif