aboutsummaryrefslogtreecommitdiffstats
path: root/modules/es_egd/es_egd.cpp
blob: 6b32d1d7fbc76d8606bcbda7aa772ebe88ae93f4 (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
/*************************************************
* EGD EntropySource Source File                  *
* (C) 1999-2007 Jack Lloyd                       *
*************************************************/

#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 {

/*************************************************
* 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, reinterpret_cast<struct ::sockaddr*>(&addr), len))
      { ::close(fd); return 0; }

   byte buffer[2];
   buffer[0] = 1;
   buffer[1] = static_cast<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;
   }

}