monotone

monotone Mtn Source Tree

Root/botan/es_egd.cpp

1/*************************************************
2* EGD EntropySource Source File *
3* (C) 1999-2005 The Botan Project *
4*************************************************/
5
6#include <botan/es_egd.h>
7#include <botan/conf.h>
8#include <botan/bit_ops.h>
9#include <botan/parsing.h>
10#include <cstring>
11
12#include <sys/types.h>
13#include <sys/socket.h>
14#include <sys/un.h>
15#include <unistd.h>
16
17#ifndef PF_LOCAL
18 #define PF_LOCAL PF_UNIX
19#endif
20
21namespace Botan {
22
23/*************************************************
24* EGD_EntropySource Constructor *
25*************************************************/
26EGD_EntropySource::EGD_EntropySource(const std::string& egd_paths)
27 {
28 std::vector<std::string> path_list = split_on(egd_paths, ':');
29 std::vector<std::string> defaults = Config::get_list("rng/egd_path");
30
31 for(u32bit j = 0; j != path_list.size(); j++)
32 paths.push_back(path_list[j]);
33 for(u32bit j = 0; j != defaults.size(); j++)
34 paths.push_back(defaults[j]);
35 }
36
37/*************************************************
38* Gather Entropy from EGD *
39*************************************************/
40u32bit EGD_EntropySource::do_poll(byte output[], u32bit length,
41 const std::string& path) const
42 {
43 if(length > 128)
44 length = 128;
45
46 sockaddr_un addr;
47 std::memset(&addr, 0, sizeof(addr));
48 addr.sun_family = PF_LOCAL;
49
50 if(sizeof(addr.sun_path) < path.length() + 1)
51 throw Exception("EGD_EntropySource: Socket path is too long");
52 std::strcpy(addr.sun_path, path.c_str());
53
54 int fd = socket(addr.sun_family, SOCK_STREAM, 0);
55 if(fd == -1) return 0;
56
57 int len = sizeof(addr.sun_family) + std::strlen(addr.sun_path) + 1;
58 if(connect(fd, (struct sockaddr*)&addr, len))
59 { close(fd); return 0; }
60
61 byte buffer[2];
62 buffer[0] = 1;
63 buffer[1] = (byte)length;
64
65 if(write(fd, buffer, 2) != 2) { close(fd); return 0; }
66 if(read(fd, buffer, 1) != 1) { close(fd); return 0; }
67
68 ssize_t count = read(fd, output, buffer[0]);
69
70 if(count == -1) { close(fd); return 0; }
71
72 close(fd);
73
74 return count;
75 }
76
77/*************************************************
78* Gather Entropy from EGD *
79*************************************************/
80u32bit EGD_EntropySource::slow_poll(byte output[], u32bit length)
81 {
82 for(u32bit j = 0; j != paths.size(); j++)
83 {
84 u32bit got = do_poll(output, length, paths[j]);
85 if(got)
86 return got;
87 }
88 return 0;
89 }
90
91}

Archive Download this file

Branches

Tags

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