]>
git.ipfire.org Git - thirdparty/rng-tools.git/blob - rngd_entsource.c
2 * rngd_entsource.c -- Entropy source and conditioning
4 * Copyright (C) 2001 Philipp Rumpf
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #error Invalid or missing autoconf build environment
27 #include "rng-tools-config.h"
32 #include <sys/types.h>
42 #include "rngd_entsource.h"
45 /* The overhead incured when tpm returns the random nos as per TCG spec
47 #define TPM_GET_RNG_OVERHEAD 14
49 /* Read data from the entropy source */
50 int xread(void *buf
, size_t size
, struct rng
*ent_src
)
57 r
= read(ent_src
->rng_fd
, buf
+ off
, size
);
58 } while ((r
== -1) && (errno
== EINTR
));
66 message(LOG_DAEMON
|LOG_ERR
, "read error\n");
72 /* tpm rng read call to kernel has 13 bytes of overhead
73 * the logic to process this involves reading to a temporary_buf
74 * and copying the no generated to buf */
75 int xread_tpm(void *buf
, size_t size
, struct rng
*ent_src
)
77 size_t bytes_read
= 0;
80 unsigned char *temp_buf
= NULL
;
81 unsigned char rng_cmd
[] = {
82 0, 193, /* TPM_TAG_RQU_COMMAND */
83 0, 0, 0, 14, /* length */
84 0, 0, 0, 70, /* TPM_ORD_GetRandom */
85 0, 0, 0, 0, /* number of bytes to return */
89 ent_src
->rng_fd
= open(ent_src
->rng_name
, O_RDWR
);
90 if (ent_src
->rng_fd
== -1) {
94 temp_buf
= (unsigned char *) malloc(size
+ TPM_GET_RNG_OVERHEAD
);
95 memset(temp_buf
, 0, (size
+TPM_GET_RNG_OVERHEAD
));
96 if (temp_buf
== NULL
) {
97 message(LOG_ERR
|LOG_INFO
,"No memory");
100 /* 32 bits has been reserved for random byte size */
101 rng_cmd
[13] = (unsigned char)(size
& 0xFF);
102 rng_cmd
[12] = (unsigned char)((size
>> 8) & 0xFF);
103 rng_cmd
[11] = (unsigned char)((size
>> 16) & 0xFF);
104 rng_cmd
[10] = (unsigned char)((size
>> 24) & 0xFF);
106 while (bytes_read
< size
) {
108 while (r
< sizeof(rng_cmd
)) {
109 retval
= write(ent_src
->rng_fd
,
111 sizeof(rng_cmd
) - r
);
113 message(LOG_ERR
|LOG_INFO
,
114 "Error writing %s\n",
121 if (r
< sizeof(rng_cmd
)) {
122 message(LOG_ERR
|LOG_INFO
,
123 "Error writing %s\n", ent_src
->rng_name
);
127 r
= read(ent_src
->rng_fd
, temp_buf
,size
);
128 r
= (r
- TPM_GET_RNG_OVERHEAD
);
129 bytes_read
= bytes_read
+ r
;
130 if (bytes_read
> size
) {
131 memcpy(offset
,temp_buf
+ TPM_GET_RNG_OVERHEAD
,
132 r
- (bytes_read
- size
));
135 memcpy(offset
, temp_buf
+ TPM_GET_RNG_OVERHEAD
, r
);
140 close(ent_src
->rng_fd
);
145 /* Initialize entropy source */
146 static int discard_initial_data(struct rng
*ent_src
)
148 /* Trash 32 bits of what is probably stale (non-random)
149 * initial state from the RNG. For Intel's, 8 bits would
150 * be enough, but since AMD's generates 32 bits at a time...
152 * The kernel drivers should be doing this at device powerup,
153 * but at least up to 2.4.24, it doesn't. */
154 unsigned char tempbuf
[4];
155 xread(tempbuf
, sizeof tempbuf
, ent_src
);
157 /* Return 32 bits of bootstrap data */
158 xread(tempbuf
, sizeof tempbuf
, ent_src
);
160 return tempbuf
[0] | (tempbuf
[1] << 8) |
161 (tempbuf
[2] << 16) | (tempbuf
[3] << 24);
165 * Open entropy source, and initialize it
167 int init_entropy_source(struct rng
*ent_src
)
169 ent_src
->rng_fd
= open(ent_src
->rng_name
, O_RDONLY
);
170 if (ent_src
->rng_fd
== -1) {
173 src_list_add(ent_src
);
174 /* Bootstrap FIPS tests */
175 ent_src
->fipsctx
= malloc(sizeof(fips_ctx_t
));
176 fips_init(ent_src
->fipsctx
, discard_initial_data(ent_src
));
181 * Open tpm entropy source, and initialize it
183 int init_tpm_entropy_source(struct rng
*ent_src
)
185 ent_src
->rng_fd
= open(ent_src
->rng_name
, O_RDWR
);
186 if (ent_src
->rng_fd
== -1) {
189 src_list_add(ent_src
);
190 /* Bootstrap FIPS tests */
191 ent_src
->fipsctx
= malloc(sizeof(fips_ctx_t
));
192 fips_init(ent_src
->fipsctx
, 0);
193 close(ent_src
->rng_fd
);