MOTION  0.01
Framework for mixed-protocol multi-party computation
openssl_rng.h
Go to the documentation of this file.
1 // MIT License
2 //
3 // Copyright (c) 2021 Arianne Roselina Prananto
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 // TODO: This class is needed because MinGW has some problems with thread_local in Aes128CtrRng.
32 // Aes128CtrRng should be used, once this problem is fixed.
33 
34 // OpenSSL's RAND_bytes() function
35 // This class is thread-safe, since RAND_bytes() is thread-safe:
36 // https://mta.openssl.org/pipermail/openssl-users/2020-November/013146.html RNG implemented using
37 class OpenSslRng : public Rng {
38  public:
39  OpenSslRng();
40  virtual ~OpenSslRng();
41 
42  // delete copy/move constructors/assignment operators
43  OpenSslRng(const OpenSslRng&) = delete;
44  OpenSslRng(OpenSslRng&&) = delete;
45  OpenSslRng& operator=(const OpenSslRng&) = delete;
46  OpenSslRng& operator=(OpenSslRng&&) = delete;
47 
48  // empty function
49  virtual void SampleKey() override;
50 
51  // fill the output buffer with number_of_bytes random bytes
52  virtual void RandomBytes(std::byte* output, std::size_t number_of_bytes) override;
53 
54  // fill the output buffer with number_of_blocks random blocks of size kBlockSize
55  virtual void RandomBlocks(std::byte* output, std::size_t number_of_blocks) override;
56 
57  // fill the output buffer with number_of_blocks random blocks of size kBlockSize
58  // where the buffer needs to be aligned at a multiple of kBlockSize
59  virtual void RandomBlocksAligned(std::byte* output, std::size_t number_of_blocks) override;
60 
61  static constexpr std::size_t kBlockSize = 16;
62 
63  static Rng& GetThreadInstance() { return instance_; }
64 
65  private:
66  static OpenSslRng instance_;
67 };
68 
69 } // namespace encrypto::motion
encrypto::motion::OpenSslRng::RandomBlocksAligned
virtual void RandomBlocksAligned(std::byte *output, std::size_t number_of_blocks) override
Definition: openssl_rng.cpp:37
encrypto::motion::OpenSslRng::SampleKey
virtual void SampleKey() override
Definition: openssl_rng.cpp:35
encrypto::motion::OpenSslRng::~OpenSslRng
virtual ~OpenSslRng()
encrypto::motion::OpenSslRng::OpenSslRng
OpenSslRng()
encrypto::motion::OpenSslRng
Definition: openssl_rng.h:37
encrypto::motion::OpenSslRng::GetThreadInstance
static Rng & GetThreadInstance()
Definition: openssl_rng.h:63
encrypto::motion::OpenSslRng::kBlockSize
static constexpr std::size_t kBlockSize
Definition: openssl_rng.h:61
encrypto::motion
Definition: algorithm_description.cpp:35
encrypto::motion::OpenSslRng::RandomBytes
virtual void RandomBytes(std::byte *output, std::size_t number_of_bytes) override
Definition: openssl_rng.cpp:53
encrypto::motion::OpenSslRng::operator=
OpenSslRng & operator=(const OpenSslRng &)=delete
openssl_rng.h
rng.h
encrypto::motion::Rng
Definition: rng.h:30
encrypto::motion::OpenSslRng::RandomBlocks
virtual void RandomBlocks(std::byte *output, std::size_t number_of_blocks) override
Definition: openssl_rng.cpp:46