| 1 |
/* |
| 2 |
* FrodoKEM matrix generator based on AES |
| 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_AES_GENERATOR_H_ |
| 12 |
#define BOTAN_FRODOKEM_AES_GENERATOR_H_ |
| 13 |
|
| 14 |
#include <botan/internal/aes.h> |
| 15 |
#include <botan/internal/buffer_stuffer.h> |
| 16 |
#include <botan/internal/frodo_constants.h> |
| 17 |
#include <botan/internal/frodo_types.h> |
| 18 |
#include <botan/internal/loadstor.h> |
| 19 |
#include <span> |
| 20 |
|
| 21 |
namespace Botan { |
| 22 |
|
| 23 |
inline auto create_aes_row_generator(const FrodoKEMConstants& constants, StrongSpan<const FrodoSeedA> seed_a) { |
| 24 |
BOTAN_ASSERT_NOMSG(constants.mode().is_aes()); |
| 25 |
|
| 26 |
auto setup_aes = [](StrongSpan<const FrodoSeedA> seed) { |
| 27 |
AES_128 aes; |
| 28 |
aes.set_key(seed); |
| 29 |
return aes; |
| 30 |
}; |
| 31 |
|
| 32 |
return [n = static_cast<uint16_t>(constants.n()), aes = setup_aes(seed_a)](std::span<uint8_t> out, uint16_t i) { |
| 33 |
BufferStuffer out_bs(out); |
| 34 |
|
| 35 |
BOTAN_DEBUG_ASSERT(out_bs.remaining_capacity() % AES_128::BLOCK_SIZE == 0); |
| 36 |
|
| 37 |
for(uint16_t j = 0; j < n; j += AES_128::BLOCK_SIZE / 2) { |
| 38 |
// set up the to-be-encrypted 'b' value in the out variable |
| 39 |
// for in-place encryption of the block cipher |
| 40 |
// b = i || j || 0000... |
| 41 |
out_bs.append(store_le(i, j)); |
| 42 |
clear_mem(out_bs.next<AES_128::BLOCK_SIZE - sizeof(i) - sizeof(j)>()); |
| 43 |
} |
| 44 |
|
| 45 |
BOTAN_DEBUG_ASSERT(out_bs.full()); |
| 46 |
|
| 47 |
aes.encrypt(out); |
| 48 |
}; |
| 49 |
} |
| 50 |
|
| 51 |
} // namespace Botan |
| 52 |
|
| 53 |
#endif |