CCF
Loading...
Searching...
No Matches
entropy.h
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the Apache 2.0 License.
3#pragma once
4
7
8#include <functional>
9#include <memory>
10#include <openssl/rand.h>
11#include <vector>
12
13namespace ccf::crypto
14{
15 class Entropy_OpenSSL : public Entropy
16 {
17 private:
18 static bool gen(uint64_t& v);
19
20 public:
21 Entropy_OpenSSL() = default;
22
23 std::vector<uint8_t> random(size_t len) override
24 {
25 std::vector<uint8_t> data(len);
26
27 if (RAND_bytes(data.data(), data.size()) != 1)
28 throw std::logic_error("Couldn't create random data");
29
30 return data;
31 }
32
33 uint64_t random64() override
34 {
35 uint64_t rnd;
36
37 if (RAND_bytes((unsigned char*)&rnd, sizeof(uint64_t)) != 1)
38 {
39 throw std::logic_error("Couldn't create random data");
40 }
41
42 return rnd;
43 }
44
45 void random(unsigned char* data, size_t len) override
46 {
47 if (RAND_bytes(data, len) != 1)
48 {
49 throw std::logic_error("Couldn't create random data");
50 }
51 }
52 };
53}
Definition entropy.h:16
uint64_t random64() override
Definition entropy.h:33
void random(unsigned char *data, size_t len) override
Definition entropy.h:45
std::vector< uint8_t > random(size_t len) override
Definition entropy.h:23
Definition entropy.h:13
Definition base64.h:10