7 /* seed OpenSSL PRNG from entropy file
9 /* #include <tls_prng_src.h>
11 /* TLS_PRNG_SRC *tls_prng_file_open(name, timeout)
15 /* ssize_t tls_prng_file_read(fh, length)
19 /* int tls_prng_file_close(fh)
22 /* tls_prng_file_open() open the specified file and returns
23 /* a handle that should be used with all subsequent access.
25 /* tls_prng_file_read() reads the requested number of bytes from
26 /* the entropy file and updates the OpenSSL PRNG. The file is not
27 /* locked for shared or exclusive access.
29 /* tls_prng_file_close() closes the specified entropy file
30 /* and releases memory that was allocated for the handle.
34 /* The pathname of the entropy file.
36 /* The number of bytes to read from the entropy file.
38 /* Time limit on individual I/O operations.
40 /* tls_prng_file_open() returns a null pointer on error.
42 /* tls_prng_file_read() returns -1 on error, the number
43 /* of bytes received on success.
45 /* tls_prng_file_close() returns -1 on error, 0 on success.
47 /* In all cases the errno variable indicates the type of error.
51 /* The Secure Mailer license must be distributed with this software.
54 /* IBM T.J. Watson Research
56 /* Yorktown Heights, NY 10598, USA
67 /* OpenSSL library. */
70 #include <openssl/rand.h> /* For the PRNG */
72 /* Utility library. */
83 /* tls_prng_file_open - open entropy file */
85 TLS_PRNG_SRC
*tls_prng_file_open(const char *name
, int timeout
)
87 const char *myname
= "tls_prng_file_open";
91 if ((fd
= open(name
, O_RDONLY
, 0)) < 0) {
93 msg_info("%s: cannot open entropy file %s: %m", myname
, name
);
96 fh
= (TLS_PRNG_SRC
*) mymalloc(sizeof(*fh
));
98 fh
->name
= mystrdup(name
);
99 fh
->timeout
= timeout
;
101 msg_info("%s: opened entropy file %s", myname
, name
);
106 /* tls_prng_file_read - update internal PRNG from entropy file */
108 ssize_t
tls_prng_file_read(TLS_PRNG_SRC
*fh
, size_t len
)
110 const char *myname
= "tls_prng_file_read";
116 msg_info("%s: seed internal pool from file %s", myname
, fh
->name
);
118 if (lseek(fh
->fd
, 0, SEEK_SET
) < 0) {
120 msg_info("cannot seek entropy file %s: %m", fh
->name
);
124 for (to_read
= len
; to_read
> 0; to_read
-= count
) {
125 if ((count
= timed_read(fh
->fd
, buffer
, to_read
> sizeof(buffer
) ?
126 sizeof(buffer
) : to_read
,
127 fh
->timeout
, (void *) 0)) < 0) {
129 msg_info("cannot read entropy file %s: %m", fh
->name
);
134 RAND_seed(buffer
, count
);
137 msg_info("read %ld bytes from entropy file %s: %m",
138 (long) (len
- to_read
), fh
->name
);
139 return (len
- to_read
);
142 /* tls_prng_file_close - close entropy file */
144 int tls_prng_file_close(TLS_PRNG_SRC
*fh
)
146 const char *myname
= "tls_prng_file_close";
150 msg_info("%s: close entropy file %s", myname
, fh
->name
);