Cuberite
A lightweight, fast and extensible game server for Minecraft
CtrDrbgContext.cpp
Go to the documentation of this file.
1 
2 // CtrDrbgContext.cpp
3 
4 // Implements the cCtrDrbgContext class representing a wrapper over CTR-DRBG implementation in mbedTLS
5 
6 #include "Globals.h"
7 #include "CtrDrbgContext.h"
8 #include "EntropyContext.h"
9 
10 
11 
12 
13 
15  m_EntropyContext(std::make_shared<cEntropyContext>()),
16  m_IsValid(false)
17 {
18  mbedtls_ctr_drbg_init(&m_CtrDrbg);
19 }
20 
21 
22 
23 
24 
25 cCtrDrbgContext::cCtrDrbgContext(const std::shared_ptr<cEntropyContext> & a_EntropyContext) :
26  m_EntropyContext(a_EntropyContext),
27  m_IsValid(false)
28 {
29  mbedtls_ctr_drbg_init(&m_CtrDrbg);
30 }
31 
32 
33 
34 
35 
36 int cCtrDrbgContext::Initialize(const void * a_Custom, size_t a_CustomSize)
37 {
38  if (m_IsValid)
39  {
40  // Already initialized
41  return 0;
42  }
43 
44  int res = mbedtls_ctr_drbg_seed(&m_CtrDrbg, mbedtls_entropy_func, &(m_EntropyContext->m_Entropy), static_cast<const unsigned char *>(a_Custom), a_CustomSize);
45  m_IsValid = (res == 0);
46  return res;
47 }
48 
49 
50 
51 
Definition: FastNBT.h:132
bool m_IsValid
Set to true if the object is valid (has been initialized properly)
cCtrDrbgContext(void)
Constructs the context with a new entropy context.
int Initialize(const void *a_Custom, size_t a_CustomSize)
Initializes the context.
mbedtls_ctr_drbg_context m_CtrDrbg
The random generator context.
std::shared_ptr< cEntropyContext > m_EntropyContext
The entropy source used for generating the random.