MOTION  0.01
Framework for mixed-protocol multi-party computation
aes128_ctr_rng.h
Go to the documentation of this file.
1 // MIT License
2 //
3 // Copyright (c) 2019 Lennart Braun
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 
23 #pragma once
24 
25 #include <cstddef>
26 #include <memory>
27 #include "rng.h"
28 
29 namespace encrypto::motion {
30 
31 // RNG implemented using AES128 in CTR mode
32 class Aes128CtrRng : public Rng {
33  public:
34  Aes128CtrRng();
35  virtual ~Aes128CtrRng();
36 
37  // delete copy/move constructors/assignment operators
38  Aes128CtrRng(const Aes128CtrRng&) = delete;
39  Aes128CtrRng(Aes128CtrRng&&) = delete;
40  Aes128CtrRng& operator=(const Aes128CtrRng&) = delete;
41  Aes128CtrRng& operator=(Aes128CtrRng&&) = delete;
42 
43  // (re)initialize the PRG with a randomly chosen key
44  virtual void SampleKey() override;
45 
46  // fill the output buffer with number_of_bytes random bytes
47  virtual void RandomBytes(std::byte* output, std::size_t number_of_bytes) override;
48 
49  // fill the output buffer with number_of_blocks random blocks of size kBlockSize
50  virtual void RandomBlocks(std::byte* output, std::size_t number_of_blocks) override;
51 
52  // fill the output buffer with number_of_blocks random blocks of size kBlockSize
53  // where the buffer needs to be aligned at a multiple of kBlockSize
54  virtual void RandomBlocksAligned(std::byte* output, std::size_t number_of_blocks) override;
55 
56  static Aes128CtrRng& GetThreadInstance() { return thread_instance_; }
57 
58  static constexpr std::size_t kBlockSize = 16;
59 
60  private:
61  struct Aes128CtrRngState;
62  std::unique_ptr<Aes128CtrRngState> state_;
63  static thread_local Aes128CtrRng thread_instance_;
64 };
65 
66 } // namespace encrypto::motion
encrypto::motion::kAesBlockSize
constexpr std::size_t kAesBlockSize
Definition: constants.h:54
AesniKeyExpansion128
void AesniKeyExpansion128(void *round_keys_input)
Definition: aesni_primitives.cpp:55
encrypto::motion::Aes128CtrRng::Aes128CtrRngState
Definition: aes128_ctr_rng.cpp:32
encrypto::motion::Aes128CtrRng::Aes128CtrRngState::round_keys
std::array< std::byte, kAesRoundKeysSize128 > round_keys
Definition: aes128_ctr_rng.cpp:33
encrypto::motion::Aes128CtrRng::SampleKey
virtual void SampleKey() override
Definition: aes128_ctr_rng.cpp:41
encrypto::motion::Aes128CtrRng::GetThreadInstance
static Aes128CtrRng & GetThreadInstance()
Definition: aes128_ctr_rng.h:56
encrypto::motion::Aes128CtrRng::kBlockSize
static constexpr std::size_t kBlockSize
Definition: aes128_ctr_rng.h:58
aes128_ctr_rng.h
aesni_primitives.h
encrypto::motion::Aes128CtrRng
Definition: aes128_ctr_rng.h:32
encrypto::motion::Aes128CtrRng::RandomBytes
virtual void RandomBytes(std::byte *output, std::size_t number_of_bytes) override
Definition: aes128_ctr_rng.cpp:66
encrypto::motion
Definition: algorithm_description.cpp:35
encrypto::motion::Aes128CtrRng::~Aes128CtrRng
virtual ~Aes128CtrRng()
AesniCtrStreamSingleBlock128Unaligned
void AesniCtrStreamSingleBlock128Unaligned(const void *round_keys_input, std::uint64_t *counter, void *output)
Definition: aesni_primitives.cpp:214
encrypto::motion::Aes128CtrRng::RandomBlocksAligned
virtual void RandomBlocksAligned(std::byte *output, std::size_t number_of_blocks) override
Definition: aes128_ctr_rng.cpp:55
AesniCtrStreamBlocks128Unaligned
void AesniCtrStreamBlocks128Unaligned(const void *round_keys_input, std::uint64_t *counter_input_pointer, void *output_input_pointer, std::size_t number_of_blocks)
Definition: aesni_primitives.cpp:151
encrypto::motion::Aes128CtrRng::operator=
Aes128CtrRng & operator=(const Aes128CtrRng &)=delete
rng.h
encrypto::motion::Aes128CtrRng::RandomBlocks
virtual void RandomBlocks(std::byte *output, std::size_t number_of_blocks) override
Definition: aes128_ctr_rng.cpp:61
encrypto::motion::Aes128CtrRng::Aes128CtrRng
Aes128CtrRng()
Definition: aes128_ctr_rng.cpp:37
encrypto::motion::Aes128CtrRng::Aes128CtrRngState::counter
std::uint64_t counter
Definition: aes128_ctr_rng.cpp:34
encrypto::motion::Rng
Definition: rng.h:30
AesniCtrStreamBlocks128
void AesniCtrStreamBlocks128(const void *round_keys_input, std::uint64_t *counter_input_pointer, void *output_input_pointer, std::size_t number_of_blocks)
Definition: aesni_primitives.cpp:92