Botan 2.13.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

p9_darn.cpp
Go to the documentation of this file.
1 /*
2 * (C) 2019 Jack Lloyd
3 *
4 * Botan is released under the Simplified BSD License (see license.txt)
5 */
6
7 #include <botan/internal/p9_darn.h>
8 #include <botan/cpuid.h>
9
10 namespace Botan {
11
12 namespace {
13
14 bool read_darn(secure_vector<uint64_t>& seed)
15 {
16 const size_t DARN_RETRIES = 512;
17
18 for(size_t i = 0; i != DARN_RETRIES; ++i)
19 {
20 uint64_t r = 0;
21
22 // DARN 0: 32-bit conditioned, 1: 64-bit condition, 2: 64-bit raw (ala RDSEED)
23 asm volatile("darn %0, 2" : "=r" (r));
24
25 // DARN indicates error by 0xFF..FF, ie is biased (!?!?)
26 if((~r) != 0)
27 {
28 seed.push_back(r);
29 return true;
30 }
31 }
32
33 return false; // failed to produce an output after many attempts
34 }
35
36 }
37
38 size_t POWER9_DARN::poll(RandomNumberGenerator& rng)
39 {
40 const size_t DARN_BYTES = 1024;
41 static_assert(DARN_BYTES % 8 == 0, "Bad DARN configuration");
42
43 if(CPUID::has_darn_rng())
44 {
45 secure_vector<uint64_t> seed;
46 seed.reserve(DARN_BYTES / 8);
47
48 for(size_t p = 0; p != DARN_BYTES / 8; ++p)
49 {
50 if(!read_darn(seed))
51 break;
52 }
53
54 if(seed.size() > 0)
55 {
56 rng.add_entropy(reinterpret_cast<const uint8_t*>(seed.data()),
57 seed.size() * sizeof(uint32_t));
58 }
59 }
60
61 // DARN is used but not trusted
62 return 0;
63 }
64
65 }
Botan::RandomNumberGenerator::add_entropy
virtual void add_entropy(const uint8_t input[], size_t length)=0
Botan::RandomNumberGenerator
Definition: rng.h:24
Botan::secure_vector
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:65
Botan
Definition: alg_id.cpp:13
Botan::POWER9_DARN::poll
size_t poll(RandomNumberGenerator &rng) override
Definition: p9_darn.cpp:38