MOTION  0.01
Framework for mixed-protocol multi-party computation
pseudo_random_generator.h
Go to the documentation of this file.
1 // MIT License
2 //
3 // Copyright (c) 2019 Oleksandr Tkachenko
4 // Cryptography and Privacy Engineering Group (ENCRYPTO)
5 // TU Darmstadt, Germany
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a copy
8 // of this software and associated documentation files (the "Software"), to deal
9 // in the Software without restriction, including without limitation the rights
10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 // copies of the Software, and to permit persons to whom the Software is
12 // furnished to do so, subject to the following conditions:
13 //
14 // The above copyright notice and this permission notice shall be included in all
15 // copies or substantial portions of the Software.
16 //
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 // SOFTWARE.
24 
25 #pragma once
26 
27 #include <cstddef>
28 #include <memory>
29 #include <vector>
30 
31 #include <openssl/aes.h>
32 #include <openssl/evp.h>
33 
34 #include "aes/aesni_primitives.h"
35 #include "utility/helpers.h"
36 
37 using uint128_t = __uint128_t;
38 
40 
41 class Prg {
42  public:
43  Prg() = default;
44 
45  void SetKey(const std::uint8_t* key);
46 
47  void SetKey(const std::byte* key);
48 
49  bool ContainsKey() { return contains_key_; }
50 
51  const void* GetRoundKeys() const { return round_keys_.data(); }
52 
53  std::size_t SetOffset(std::size_t new_offset) {
54  std::swap(offset_, new_offset);
55  return new_offset;
56  }
57 
58  std::vector<std::byte> Encrypt(const std::size_t bytes);
59 
60  std::vector<std::byte> Encrypt(const std::byte* input, const std::size_t bytes);
61 
62  std::vector<std::byte> FixedKeyAes(const std::byte* x, const std::uint64_t i,
63  const std::size_t num = 1);
64 
65  std::vector<std::byte> FixedKeyAes(const std::byte* x, const uint128_t i);
66  void Mmo(std::byte* input);
67 
68  // Implementation of TMMO^\pi
69  // of https://eprint.iacr.org/2019/074
70  // with input x and tweak i
71  // input and output have to point into a buffer with AES_BLOCK_SIZE bytes
72  void FixedKeyAes(const std::byte* input, const uint128_t tweak, std::byte* output);
73 
74  ~Prg() = default;
75 
76  private:
77  alignas(16) std::array<std::byte, kAesRoundKeysSize128> round_keys_;
78  using EvpCipherCtxPointer = std::unique_ptr<EVP_CIPHER_CTX, decltype(&EVP_CIPHER_CTX_free)>;
79  static constexpr auto MakeCipherCtx = []() {
80  return EvpCipherCtxPointer(EVP_CIPHER_CTX_new(), &EVP_CIPHER_CTX_free);
81  };
82 
83  EvpCipherCtxPointer ctx_ = MakeCipherCtx();
84 
85  std::array<std::uint8_t, AES_BLOCK_SIZE> key_;
86  bool contains_key_{false};
87  std::size_t offset_{0};
88 };
89 
90 } // namespace encrypto::motion::primitives
encrypto::motion::primitives::Prg::~Prg
~Prg()=default
helpers.h
AesniKeyExpansion128
void AesniKeyExpansion128(void *round_keys_input)
Definition: aesni_primitives.cpp:55
pseudo_random_generator.h
encrypto::motion::primitives::Prg::SetOffset
std::size_t SetOffset(std::size_t new_offset)
Definition: pseudo_random_generator.h:53
encrypto::motion::primitives::Prg::SetKey
void SetKey(const std::uint8_t *key)
Definition: pseudo_random_generator.cpp:32
encrypto::motion::primitives::EncryptBlock
static void EncryptBlock(EVP_CIPHER_CTX *ctx, const std::byte *in, std::byte *output)
Definition: pseudo_random_generator.cpp:128
AesniMmoSingle
void AesniMmoSingle(const void *round_keys_input, void *input)
Definition: aesni_primitives.cpp:280
aesni_primitives.h
kAesKeySize128
constexpr std::size_t kAesKeySize128
Definition: aesni_primitives.h:28
encrypto::motion::primitives
Definition: motion_base_provider.h:35
uint128_t
__uint128_t uint128_t
Definition: pseudo_random_generator.h:37
encrypto::motion::primitives::Prg::ContainsKey
bool ContainsKey()
Definition: pseudo_random_generator.h:49
encrypto::motion::primitives::Prg
Definition: pseudo_random_generator.h:41
encrypto::motion::primitives::Prg::Encrypt
std::vector< std::byte > Encrypt(const std::size_t bytes)
Definition: pseudo_random_generator.cpp:46
encrypto::motion::primitives::Prg::Prg
Prg()=default
encrypto::motion::primitives::Prg::FixedKeyAes
std::vector< std::byte > FixedKeyAes(const std::byte *x, const std::uint64_t i, const std::size_t num=1)
Definition: pseudo_random_generator.cpp:94
encrypto::motion::primitives::Prg::Mmo
void Mmo(std::byte *input)
Definition: pseudo_random_generator.cpp:148
encrypto::motion::primitives::Prg::GetRoundKeys
const void * GetRoundKeys() const
Definition: pseudo_random_generator.h:51
encrypto::motion::swap
void swap(ReusablePromise< R, MutexType, ConditionVariableType > &lhs, ReusablePromise< R, MutexType, ConditionVariableType > &rhs) noexcept
Definition: reusable_future.h:270