1 kumpf 1.2 /*
2 //%2006////////////////////////////////////////////////////////////////////////
3 //
4 // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
5 // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
6 // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
7 // IBM Corp.; EMC Corporation, The Open Group.
8 // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
9 // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
10 // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
11 // EMC Corporation; VERITAS Software Corporation; The Open Group.
12 // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
13 // EMC Corporation; Symantec Corporation; The Open Group.
14 //
15 // Permission is hereby granted, free of charge, to any person obtaining a copy
16 // of this software and associated documentation files (the "Software"), to
17 // deal in the Software without restriction, including without limitation the
18 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
19 // sell copies of the Software, and to permit persons to whom the Software is
20 // furnished to do so, subject to the following conditions:
21 //
22 kumpf 1.2 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
23 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
24 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
25 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
26 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
27 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31 //%/////////////////////////////////////////////////////////////////////////////
32 */
33
34 #include "Random.h"
35 #include <stdlib.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <sys/time.h>
39 #include <fcntl.h>
40 #include <unistd.h>
41 #include <errno.h>
42
43 kumpf 1.2 /*
44 **==============================================================================
45 **
46 ** FillRandomBytes1()
47 **
48 ** Fills *data* with *size* random bytes using the high-quality random
49 ** number generator device (/dev/urandom).
50 **
51 **==============================================================================
52 */
53
54 static int _fillRandomBytes1(unsigned char* data, size_t size)
55 {
56 size_t numErrors = 0;
57 const size_t MAX_ERRORS_TOLERATED = 20;
58
59 int fd = open("/dev/urandom", O_RDONLY | O_NONBLOCK);
60
61 if (fd < 0)
62 return -1;
63
64 kumpf 1.2 while (size)
65 {
66 ssize_t n = read(fd, data, size);
67
68 if (n < 1)
69 {
70 if (++numErrors == MAX_ERRORS_TOLERATED)
71 break;
72
73 if (errno == EAGAIN)
74 continue;
75
76 break;
77 }
78
79 size -= n;
80 data += n;
81 }
82
83 close(fd);
84 return size == 0 ? 0 : -1;
85 kumpf 1.2 }
86
87 /*
88 **==============================================================================
89 **
90 ** FillRandomBytes2()
91 **
92 ** Fills *data* with *size* random bytes using the standard rand() function.
93 ** Note: this function uses srand(), which is generally not considered
94 ** "random" enough for security purposes but it is only used if
95 ** FillRandomBytes1() fails.
96 **
97 **==============================================================================
98 */
99
100 static void _fillRandomBytes2(unsigned char* data, size_t size)
101 {
102 struct timeval tv;
103
104 /* Seed the random number generator. */
105
106 kumpf 1.2 gettimeofday(&tv, 0);
107
108 /* Flawfinder: ignore */
109 srand(tv.tv_usec);
110
111 /* Fill data with random bytes. */
112
113 while (size--)
114 *data++ = rand();
115 }
116
117 /*
118 **==============================================================================
119 **
120 ** FillRandomBytes()
121 **
122 **==============================================================================
123 */
124
125 void FillRandomBytes(unsigned char* data, size_t size)
126 {
127 kumpf 1.2 if (_fillRandomBytes1(data, size) != 0)
128 _fillRandomBytes2(data, size);
129 }
130
131 /*
132 **==============================================================================
133 **
134 ** RandBytesToHexASCII()
135 **
136 ** Converts the bytes given by *data* to a hexidecimal sequence of ASCII
137 ** characters. The *ascii* parameter must be twice size plus one (for the
138 ** null terminator).
139 **
140 **==============================================================================
141 */
142
143 void RandBytesToHexASCII(
144 const unsigned char* data,
145 size_t size,
146 char* ascii)
147 {
148 kumpf 1.2 static char _hexDigits[] =
149 {
150 '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
151 };
152 size_t i;
153 size_t j = 0;
154
155 for (i = 0; i < size; i++)
156 {
157 unsigned char hi = data[i] >> 4;
158 unsigned char lo = 0x0F & data[i];
159 ascii[j++] = _hexDigits[hi];
160 ascii[j++] = _hexDigits[lo];
161 }
162
163 ascii[j] = '\0';
164 }
|