aboutsummaryrefslogtreecommitdiffstats
path: root/modules/es_egd/es_egd.cpp
blob: adc71b9c025da8f8deeeace7837e7cdcb9ae027c (plain)
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
/*************************************************
* EGD EntropySource Source File                  *
* (C) 1999-2006 The Botan Project                *
*************************************************/

#include <botan/es_egd.h>
#include <botan/config.h>
#include <botan/bit_ops.h>
#include <botan/parsing.h>
#include <cstring>

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>

#ifndef PF_LOCAL
  #define PF_LOCAL PF_UNIX
#endif

namespace Botan {

/*************************************************
* EGD_EntropySource Constructor                  *
*************************************************/
EGD_EntropySource::EGD_EntropySource(const std::string& egd_paths)
   {
   std::vector<std::string> path_list = split_on(egd_paths, ':');
   std::vector<std::string> defaults =
      global_config().option_as_list("rng/egd_path");

   for(u32bit j = 0; j != path_list.size(); j++)
      paths.push_back(path_list[j]);
   for(u32bit j = 0; j != defaults.size(); j++)
      paths.push_back(defaults[j]);
   }

/*************************************************
* Gather Entropy from EGD                        *
*************************************************/
u32bit EGD_EntropySource::do_poll(byte output[], u32bit length,
                                  const std::string& path) const
   {
   if(length > 128)
      length = 128;

   sockaddr_un addr;
   std::memset(&addr, 0, sizeof(addr));
   addr.sun_family = PF_LOCAL;

   if(sizeof(addr.sun_path) < path.length() + 1)
      throw Exception("EGD_EntropySource: Socket path is too long");
   std::strcpy(addr.sun_path, path.c_str());

   int fd = socket(addr.sun_family, SOCK_STREAM, 0);
   if(fd == -1) return 0;

   int len = sizeof(addr.sun_family) + std::strlen(addr.sun_path) + 1;
   if(connect(fd, (struct sockaddr*)&addr, len))
      { close(fd); return 0; }

   byte buffer[2];
   buffer[0] = 1;
   buffer[1] = (byte)length;

   if(write(fd, buffer, 2) != 2) { close(fd); return 0; }
   if(read(fd, buffer, 1) != 1)  { close(fd); return 0; }

   ssize_t count = read(fd, output, buffer[0]);

   if(count == -1) { close(fd); return 0; }

   close(fd);

   return count;
   }

/*************************************************
* Gather Entropy from EGD                        *
*************************************************/
u32bit EGD_EntropySource::slow_poll(byte output[], u32bit length)
   {
   for(u32bit j = 0; j != paths.size(); j++)
      {
      u32bit got = do_poll(output, length, paths[j]);
      if(got)
         return got;
      }
   return 0;
   }

}