summaryrefslogtreecommitdiff
path: root/boost/beast/websocket/detail/prng.ipp
blob: 2d4e7f2771d5b590b5b6687b587340b21dd3241c (plain)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//
// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/boostorg/beast
//

#ifndef BOOST_BEAST_WEBSOCKET_DETAIL_PRNG_IPP
#define BOOST_BEAST_WEBSOCKET_DETAIL_PRNG_IPP

#include <boost/beast/websocket/detail/prng.hpp>
#include <boost/beast/core/detail/chacha.hpp>
#include <boost/beast/core/detail/pcg.hpp>
#include <atomic>
#include <cstdlib>
#include <mutex>
#include <random>

namespace boost {
namespace beast {
namespace websocket {
namespace detail {

//------------------------------------------------------------------------------

std::uint32_t const*
prng_seed(std::seed_seq* ss)
{
    struct data
    {
        std::uint32_t v[8];

        explicit
        data(std::seed_seq* pss)
        {
            if(! pss)
            {
                std::random_device g;
                std::seed_seq ss{
                    g(), g(), g(), g(),
                    g(), g(), g(), g()};
                ss.generate(v, v+8);
            }
            else
            {
                pss->generate(v, v+8);
            }
        }
    };
    static data const d(ss);
    return d.v;
}

//------------------------------------------------------------------------------

inline
std::uint32_t
make_nonce()
{
    static std::atomic<std::uint32_t> nonce{0};
    return ++nonce;
}

inline
beast::detail::pcg make_pcg()
{
    auto const pv = prng_seed();
    return beast::detail::pcg{
        ((static_cast<std::uint64_t>(pv[0])<<32)+pv[1]) ^
        ((static_cast<std::uint64_t>(pv[2])<<32)+pv[3]) ^
        ((static_cast<std::uint64_t>(pv[4])<<32)+pv[5]) ^
        ((static_cast<std::uint64_t>(pv[6])<<32)+pv[7]), make_nonce()};
}

#ifdef BOOST_NO_CXX11_THREAD_LOCAL

inline
std::uint32_t
secure_generate()
{
    struct generator
    {
        std::uint32_t operator()()
        {
            std::lock_guard<std::mutex> guard{mtx};
            return gen();
        }

        beast::detail::chacha<20> gen;
        std::mutex mtx;
    };
    static generator gen{beast::detail::chacha<20>{prng_seed(), make_nonce()}};
    return gen();
}

inline
std::uint32_t
fast_generate()
{
    struct generator
    {
        std::uint32_t operator()()
        {
            std::lock_guard<std::mutex> guard{mtx};
            return gen();
        }

        beast::detail::pcg gen;
        std::mutex mtx;
    };
    static generator gen{make_pcg()};
    return gen();
}

#else

inline
std::uint32_t
secure_generate()
{
    thread_local static beast::detail::chacha<20> gen{prng_seed(), make_nonce()};
    return gen();
}

inline
std::uint32_t
fast_generate()
{
    thread_local static beast::detail::pcg gen{make_pcg()};
    return gen();
}

#endif

generator
make_prng(bool secure)
{
    if (secure)
        return &secure_generate;
    else
        return &fast_generate;
}

} // detail
} // websocket
} // beast
} // boost

#endif