Botan 3.14.1
Crypto and TLS for C&
frodo_shake_generator.h
Go to the documentation of this file.
1/*
2 * FrodoKEM matrix generator based on SHAKE
3 *
4 * The Fellowship of the FrodoKEM:
5 * (C) 2023 Jack Lloyd
6 * 2023 René Meusel, Amos Treiber - Rohde & Schwarz Cybersecurity
7 *
8 * Botan is released under the Simplified BSD License (see license.txt)
9 */
10
11#ifndef BOTAN_FRODOKEM_SHAKE_GENERATOR_H_
12#define BOTAN_FRODOKEM_SHAKE_GENERATOR_H_
13
14#include <botan/internal/fmt.h>
15#include <botan/internal/frodo_constants.h>
16#include <botan/internal/frodo_types.h>
17#include <botan/internal/hash_engine.h>
18#include <botan/internal/loadstor.h>
19
20#include <span>
21
22namespace Botan {
23
25 BOTAN_ASSERT_NOMSG(constants.mode().is_shake());
26
27 // Each row i of A is SHAKE-128(le16(i) || seed_a), n 16-bit values
28 auto engine = std::shared_ptr<Hash_Engine>(Hash_Engine::create_or_throw(fmt("SHAKE-128({})", 16 * constants.n())));
29
30 return
31 [engine = std::move(engine), a = FrodoSeedA(seed_a)](std::span<uint8_t> out, uint16_t first_row, size_t nrows) {
32 const size_t row_bytes = engine->output_length();
33 BOTAN_ASSERT_NOMSG(out.size() == nrows * row_bytes);
34
35 std::vector<uint8_t> row_ids(sizeof(uint16_t) * nrows);
36 std::vector<std::span<uint8_t>> outputs(nrows);
37 std::vector<std::span<const uint8_t>> ids(nrows);
38 std::vector<std::span<const uint8_t>> seeds(nrows);
39
40 for(size_t r = 0; r != nrows; ++r) {
41 const auto id_span = std::span(row_ids).subspan(sizeof(uint16_t) * r).first<sizeof(uint16_t)>();
42 store_le(static_cast<uint16_t>(first_row + r), id_span);
43 ids[r] = id_span;
44 seeds[r] = a;
45 outputs[r] = out.subspan(r * row_bytes, row_bytes);
46 }
47
48 engine->batch_hash(outputs, ids, seeds);
49 };
50}
51
52} // namespace Botan
53
54#endif
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:82
FrodoKEMMode mode() const
bool is_shake() const
Definition frodo_mode.h:56
static std::unique_ptr< Hash_Engine > create_or_throw(std::string_view hash_fn, std::span< const uint8_t > common_prefix={}, std::string_view provider="")
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
auto create_shake_row_generator(const FrodoKEMConstants &constants, StrongSpan< const FrodoSeedA > seed_a)
constexpr auto store_le(ParamTs &&... params)
Definition loadstor.h:736
Strong< std::vector< uint8_t >, struct FrodoSeedA_ > FrodoSeedA
Definition frodo_types.h:23