1
/* rndhw.c - Access to the external random daemon
2
* Copyright (C) 2007 Free Software Foundation, Inc.
4
* This file is part of Libgcrypt.
6
* Libgcrypt is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU Lesser General Public License as
8
* published by the Free Software Foundation; either version 2.1 of
9
* the License, or (at your option) any later version.
11
* Libgcrypt 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 Lesser General Public License for more details.
16
* You should have received a copy of the GNU Lesser General Public
17
* License along with this program; if not, see <http://www.gnu.org/licenses/>.
27
#include "rand-internal.h"
30
#ifdef ENABLE_PADLOCK_SUPPORT
31
# if defined (__i386__) && SIZEOF_UNSIGNED_LONG == 4 && defined (__GNUC__)
34
#endif /*ENABLE_PADLOCK_SUPPORT*/
36
/* Keep track on whether the RNG has problems. */
37
static volatile int rng_failed;
42
poll_padlock (void (*add)(const void*, size_t, enum random_origins),
43
enum random_origins origin, int fast)
45
char buffer[64+8] __attribute__ ((aligned (8)));
47
unsigned int nbytes, status;
49
/* Peter Gutmann's cryptlib tests again whether the RNG is enabled
50
but we don't do so. We would have to do this also for our AES
51
implementaion and that is definitely too time consuming. There
52
would be a race condition anyway. Thus we assume that the OS
53
does not change the Padlock initialization while a user process
60
("movl %1, %%edi\n\t" /* Set buffer. */
61
"xorl %%edx, %%edx\n\t" /* Request up to 8 bytes. */
62
".byte 0x0f, 0xa7, 0xc0\n\t" /* XSTORE RNG. */
63
"movl %%eax, %0\n" /* Return the status. */
66
: "%edx", "%edi", "cc"
68
if ((status & (1<<6)) /* RNG still enabled. */
69
&& !(status & (1<<13)) /* von Neumann corrector is enabled. */
70
&& !(status & (1<<14)) /* String filter is disabled. */
71
&& !(status & 0x1c00) /* BIAS voltage at default. */
72
&& (!(status & 0x1f) || (status & 0x1f) == 8) /* Sanity check. */
75
nbytes += (status & 0x1f);
77
break; /* Don't get into the loop with the fast flag set. */
82
/* If there was an error we need to break the loop and
83
record that there is something wrong with the padlock
92
(*add) (buffer, nbytes, origin);
93
wipememory (buffer, nbytes);
97
#endif /*USE_PADLOCK*/
101
_gcry_rndhw_failed_p (void)
107
/* Try to read random from a hardware RNG if a fast one is
110
_gcry_rndhw_poll_fast (void (*add)(const void*, size_t, enum random_origins),
111
enum random_origins origin)
117
if ((_gcry_get_hw_features () & HWF_PADLOCK_RNG))
118
poll_padlock (add, origin, 1);
123
/* Read 64 bytes from a hardware RNG and return the number of bytes
126
_gcry_rndhw_poll_slow (void (*add)(const void*, size_t, enum random_origins),
127
enum random_origins origin)
135
if ((_gcry_get_hw_features () & HWF_PADLOCK_RNG))
136
nbytes += poll_padlock (add, origin, 0);