7 /* seed OpenSSL PRNG from entropy device
9 /* #include <tls_prng_src.h>
11 /* TLS_PRNG_SRC *tls_prng_dev_open(name, timeout)
15 /* ssize_t tls_prng_dev_read(dev, length)
19 /* int tls_prng_dev_close(dev)
22 /* tls_prng_dev_open() opens the specified entropy device
23 /* and returns a handle that should be used with all subsequent
26 /* tls_prng_dev_read() reads the requested number of bytes from
27 /* the entropy device and updates the OpenSSL PRNG.
29 /* tls_prng_dev_close() closes the specified entropy device
30 /* and releases memory that was allocated for the handle.
34 /* The pathname of the entropy device.
36 /* The number of bytes to read from the entropy device.
37 /* Request lengths will be truncated at 255 bytes.
39 /* Time limit on individual I/O operations.
41 /* tls_prng_dev_open() returns a null pointer on error.
43 /* tls_prng_dev_read() returns -1 on error, the number
44 /* of bytes received on success.
46 /* tls_prng_dev_close() returns -1 on error, 0 on success.
48 /* In all cases the errno variable indicates the type of error.
52 /* The Secure Mailer license must be distributed with this software.
55 /* IBM T.J. Watson Research
57 /* Yorktown Heights, NY 10598, USA
69 #define UCHAR_MAX 0xff
72 /* OpenSSL library. */
75 #include <openssl/rand.h> /* For the PRNG */
77 /* Utility library. */
88 /* tls_prng_dev_open - open entropy device */
90 TLS_PRNG_SRC
*tls_prng_dev_open(const char *name
, int timeout
)
92 const char *myname
= "tls_prng_dev_open";
96 if ((fd
= open(name
, O_RDONLY
, 0)) < 0) {
98 msg_info("%s: cannot open entropy device %s: %m", myname
, name
);
101 dev
= (TLS_PRNG_SRC
*) mymalloc(sizeof(*dev
));
103 dev
->name
= mystrdup(name
);
104 dev
->timeout
= timeout
;
106 msg_info("%s: opened entropy device %s", myname
, name
);
111 /* tls_prng_dev_read - update internal PRNG from device */
113 ssize_t
tls_prng_dev_read(TLS_PRNG_SRC
*dev
, size_t len
)
115 const char *myname
= "tls_prng_dev_read";
116 unsigned char buffer
[UCHAR_MAX
];
121 msg_panic("%s: bad read length: %ld", myname
, (long) len
);
123 if (len
> sizeof(buffer
))
124 rand_bytes
= sizeof(buffer
);
128 count
= timed_read(dev
->fd
, buffer
, rand_bytes
, dev
->timeout
, (void *) 0);
131 msg_info("%s: read %ld bytes from entropy device %s",
132 myname
, (long) count
, dev
->name
);
133 RAND_seed(buffer
, count
);
136 msg_info("%s: cannot read %ld bytes from entropy device %s: %m",
137 myname
, (long) rand_bytes
, dev
->name
);
142 /* tls_prng_dev_close - disconnect from EGD server */
144 int tls_prng_dev_close(TLS_PRNG_SRC
*dev
)
146 const char *myname
= "tls_prng_dev_close";
150 msg_info("%s: close entropy device %s", myname
, dev
->name
);
151 err
= close(dev
->fd
);
153 myfree((char *) dev
);