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
|
/*
* Unix EntropySource Source File
* (C) 1999-2009 Jack Lloyd
*/
#include <botan/es_unix.h>
#include <botan/unix_cmd.h>
#include <botan/parsing.h>
#include <algorithm>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/resource.h>
#include <unistd.h>
namespace Botan {
namespace {
/**
* Sort ordering by priority
*/
bool Unix_Program_Cmp(const Unix_Program& a, const Unix_Program& b)
{ return (a.priority < b.priority); }
}
/**
* Unix_EntropySource Constructor
*/
Unix_EntropySource::Unix_EntropySource(const std::vector<std::string>& path) :
PATH(path)
{
add_default_sources(sources);
}
/**
* Add sources to the list
*/
void Unix_EntropySource::add_sources(const Unix_Program srcs[], u32bit count)
{
sources.insert(sources.end(), srcs, srcs + count);
std::sort(sources.begin(), sources.end(), Unix_Program_Cmp);
}
/**
* Poll for entropy on a generic Unix system, first by grabbing various
* statistics (stat on common files, getrusage, etc), and then, if more
* is required, by exec'ing various programs like uname and rpcinfo and
* reading the output.
*/
void Unix_EntropySource::poll(Entropy_Accumulator& accum)
{
const char* stat_targets[] = {
"/",
"/tmp",
"/var/tmp",
"/usr",
"/home",
"/etc/passwd",
".",
"..",
0 };
for(u32bit j = 0; stat_targets[j]; j++)
{
struct stat statbuf;
clear_mem(&statbuf, 1);
::stat(stat_targets[j], &statbuf);
accum.add(&statbuf, sizeof(statbuf), .005);
}
accum.add(::getpid(), 0);
accum.add(::getppid(), 0);
accum.add(::getuid(), 0);
accum.add(::geteuid(), 0);
accum.add(::getegid(), 0);
accum.add(::getpgrp(), 0);
accum.add(::getsid(0), 0);
struct ::rusage usage;
::getrusage(RUSAGE_SELF, &usage);
accum.add(usage, .005);
::getrusage(RUSAGE_CHILDREN, &usage);
accum.add(usage, .005);
const u32bit MINIMAL_WORKING = 16;
MemoryRegion<byte>& io_buffer = accum.get_io_buffer(DEFAULT_BUFFERSIZE);
for(u32bit j = 0; j != sources.size(); j++)
{
DataSource_Command pipe(sources[j].name_and_args, PATH);
u32bit got_from_src = 0;
while(!pipe.end_of_data())
{
u32bit got_this_loop = pipe.read(io_buffer, io_buffer.size());
got_from_src += got_this_loop;
accum.add(io_buffer.begin(), got_this_loop, .005);
}
sources[j].working = (got_from_src >= MINIMAL_WORKING) ? true : false;
if(accum.polling_goal_achieved())
break;
}
}
}
|