source: box/trunk/lib/crypto/Random.cpp @ 1789

Revision 1789, 2.9 KB checked in by chris, 5 years ago (diff)

Convert most printf() and fprintf() calls to use logging framework
instead. (refs #3)

  • Property svn:eol-style set to native
Line 
1// --------------------------------------------------------------------------
2//
3// File
4//              Name:    Random.cpp
5//              Purpose: Random numbers
6//              Created: 31/12/03
7//
8// --------------------------------------------------------------------------
9
10#include "Box.h"
11
12#include <openssl/rand.h>
13#include <stdio.h>
14
15#include "Random.h"
16#include "CipherException.h"
17
18#include "MemLeakFindOn.h"
19
20
21// --------------------------------------------------------------------------
22//
23// Function
24//              Name:    Random::Initialise()
25//              Purpose: Add additional randomness to the standard library initialisation
26//              Created: 18/6/04
27//
28// --------------------------------------------------------------------------
29void Random::Initialise()
30{
31#ifdef HAVE_RANDOM_DEVICE
32        if(::RAND_load_file(RANDOM_DEVICE, 1024) != 1024)
33        {
34                THROW_EXCEPTION(CipherException, RandomInitFailed)
35        }
36#else
37        BOX_ERROR("No random device -- additional seeding of random number "
38                "generator not performed.");
39#endif
40}
41
42
43// --------------------------------------------------------------------------
44//
45// Function
46//              Name:    Random::Generate(void *, int)
47//              Purpose: Generate Length bytes of random data
48//              Created: 31/12/03
49//
50// --------------------------------------------------------------------------
51void Random::Generate(void *pOutput, int Length)
52{
53        if(RAND_pseudo_bytes((uint8_t*)pOutput, Length) == -1)
54        {
55                THROW_EXCEPTION(CipherException, PseudoRandNotAvailable)
56        }
57}
58
59
60// --------------------------------------------------------------------------
61//
62// Function
63//              Name:    Random::GenerateHex(int)
64//              Purpose: Generate Length bytes of hex encoded data. Note that the
65//                               maximum length requested is limited. (Returns a string
66//                               2 x Length characters long.)
67//              Created: 1/11/04
68//
69// --------------------------------------------------------------------------
70std::string Random::GenerateHex(int Length)
71{
72        uint8_t r[256];
73        if(Length > (int)sizeof(r))
74        {
75                THROW_EXCEPTION(CipherException, LengthRequestedTooLongForRandomHex)
76        }
77        Random::Generate(r, Length);
78       
79        std::string o;
80        static const char *h = "0123456789abcdef";
81        for(int l = 0; l < Length; ++l)
82        {
83                o += h[r[l] >> 4];
84                o += h[r[l] & 0xf];
85        }
86       
87        return o;
88}
89
90
91// --------------------------------------------------------------------------
92//
93// Function
94//              Name:    Random::RandomInt(int)
95//              Purpose: Return a random integer between 0 and MaxValue inclusive.
96//              Created: 21/1/04
97//
98// --------------------------------------------------------------------------
99uint32_t Random::RandomInt(uint32_t MaxValue)
100{
101        uint32_t v = 0;
102
103        // Generate a mask
104        uint32_t mask = 0;
105        while(mask < MaxValue)
106        {
107                mask = (mask << 1) | 1;
108        }
109
110        do
111        {
112                // Generate a random number
113                uint32_t r = 0;
114                Random::Generate(&r, sizeof(r));
115               
116                // Mask off relevant bits
117                v = r & mask;
118               
119                // Check that it's in the right range.
120        } while(v > MaxValue);
121       
122        // NOTE: don't do a mod, because this doesn't give a correct random distribution
123       
124        return v;
125}
126
127
128
Note: See TracBrowser for help on using the repository browser.