monotone

monotone Mtn Source Tree

Root/botan/es_dev.cpp

1/*************************************************
2* Device EntropySource Source File *
3* (C) 1999-2008 The Botan Project *
4*************************************************/
5
6#include <botan/es_dev.h>
7#include <botan/config.h>
8#include <sys/select.h>
9#include <sys/stat.h>
10#include <sys/types.h>
11#include <sys/fcntl.h>
12#include <unistd.h>
13
14namespace Botan {
15
16namespace {
17
18/*************************************************
19* A class handling reading from a device *
20*************************************************/
21class Device_Reader
22 {
23 public:
24 typedef int fd_type;
25
26 Device_Reader(fd_type device_fd) : fd(device_fd) {}
27 ~Device_Reader() { if(fd > 0) { ::close(fd); } }
28 u32bit get(byte out[], u32bit length);
29
30 static fd_type open(const std::string& pathname);
31 private:
32 fd_type fd;
33 };
34
35/*************************************************
36* Read from a device file *
37*************************************************/
38u32bit Device_Reader::get(byte out[], u32bit length)
39 {
40 if(fd < 0)
41 return 0;
42
43 if(fd >= FD_SETSIZE)
44 return 0;
45
46 const u32bit READ_WAIT_MS = 10;
47
48 fd_set read_set;
49 FD_ZERO(&read_set);
50 FD_SET(fd, &read_set);
51
52 struct ::timeval timeout;
53 timeout.tv_sec = 0;
54 timeout.tv_usec = READ_WAIT_MS * 1000;
55
56 if(::select(fd + 1, &read_set, 0, 0, &timeout) < 0)
57 return 0;
58
59 if(!(FD_ISSET(fd, &read_set)))
60 return 0;
61
62 const ssize_t got = ::read(fd, out, length);
63 if(got <= 0)
64 return 0;
65
66 const u32bit ret = static_cast<u32bit>(got);
67
68 if(ret > length)
69 return 0;
70
71 return ret;
72 }
73
74/*************************************************
75* Attempt to open a device *
76*************************************************/
77int Device_Reader::open(const std::string& pathname)
78 {
79#ifndef O_NONBLOCK
80 #define O_NONBLOCK 0
81#endif
82
83#ifndef O_NOCTTY
84 #define O_NOCTTY 0
85#endif
86
87 const int flags = O_RDONLY | O_NONBLOCK | O_NOCTTY;
88 return ::open(pathname.c_str(), flags);
89 }
90
91}
92
93/*************************************************
94* Gather entropy from a RNG device *
95*************************************************/
96u32bit Device_EntropySource::slow_poll(byte output[], u32bit length)
97 {
98 std::vector<std::string> sources =
99 global_config().option_as_list("rng/es_files");
100
101 u32bit read = 0;
102
103 for(size_t j = 0; j != sources.size(); ++j)
104 {
105 const std::string source = sources[j];
106
107 Device_Reader reader(Device_Reader::open(source));
108
109 read += reader.get(output + read, length - read);
110
111 if(read == length)
112 break;
113 }
114
115 return read;
116 }
117
118}

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status