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 {
29 throw std::logic_error("Couldn't create random data");
30 }
31
32 return data;
33 }
34
35 uint64_t random64() override
36 {
37 uint64_t rnd = 0;
38
39 if (
40 RAND_bytes(reinterpret_cast<unsigned char*>(&rnd), sizeof(uint64_t)) !=
41 1)
42 {
43 throw std::logic_error("Couldn't create random data");
44 }
45
46 return rnd;
47 }
48
49 void random(unsigned char* data, size_t len) override
50 {
51 if (RAND_bytes(data, len) != 1)
52 {
53 throw std::logic_error("Couldn't create random data");
54 }
55 }
56 };
57}
Definition entropy.h:16
uint64_t random64() override
Definition entropy.h:35
void random(unsigned char *data, size_t len) override
Definition entropy.h:49
std::vector< uint8_t > random(size_t len) override
Definition entropy.h:23
Definition entropy.h:13
Definition base64.h:10