forked from dkales/dpf-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPRNG.cpp
62 lines (49 loc) · 1.21 KB
/
PRNG.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "PRNG.h"
#include <algorithm>
#include <cstring>
PRNG::PRNG(const block& seed, uint64_t bufferSize)
:
mBytesIdx(0),
mBlockIdx(0)
{
SetSeed(seed, bufferSize);
}
PRNG::PRNG(PRNG && s) :
mBuffer(std::move(s.mBuffer)),
mAes(std::move(s.mAes)),
mBytesIdx(s.mBytesIdx),
mBlockIdx(s.mBlockIdx),
mBufferByteCapacity(s.mBufferByteCapacity)
{
s.mBuffer.resize(0);
memset(&s.mAes, 0, sizeof(AES));
s.mBytesIdx = 0;
s.mBlockIdx = 0;
s.mBufferByteCapacity = 0;
}
void PRNG::SetSeed(const block& seed, uint64_t bufferSize)
{
mAes.setKey(seed);
mBlockIdx = 0;
if (mBuffer.size() == 0)
{
mBuffer.resize(bufferSize);
mBufferByteCapacity = (sizeof(block) * bufferSize);
}
refillBuffer();
}
uint8_t PRNG::getBit() { return get<bool>(); }
const block PRNG::getSeed() const
{
if(mBuffer.size())
return mAes.key;
throw std::runtime_error("PRNG has not been keyed " LOCATION);
}
void PRNG::refillBuffer()
{
if (mBuffer.size() == 0)
throw std::runtime_error("PRNG has not been keyed " LOCATION);
mAes.encryptCTR(mBlockIdx, mBuffer.size(), mBuffer.data());
mBlockIdx += mBuffer.size();
mBytesIdx = 0;
}