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/>.
26
#include "rand-internal.h"
29
#ifdef ENABLE_PADLOCK_SUPPORT
30
# if defined (__i386__) && SIZEOF_UNSIGNED_LONG == 4 && defined (__GNUC__)
33
#endif /*ENABLE_PADLOCK_SUPPORT*/
35
/* Keep track on whether the RNG has problems. */
36
static volatile int rng_failed;
41
poll_padlock (void (*add)(const void*, size_t, enum random_origins),
42
enum random_origins origin, int fast)
44
char buffer[64+8] __attribute__ ((aligned (8)));
46
unsigned int nbytes, status;
48
/* Peter Gutmann's cryptlib tests again whether the RNG is enabled
49
but we don't do so. We would have to do this also for our AES
50
implementaion and that is definitely too time consuming. There
51
would be a race condition anyway. Thus we assume that the OS
52
does not change the Padlock initialization while a user process
59
("movl %1, %%edi\n\t" /* Set buffer. */
60
"xorl %%edx, %%edx\n\t" /* Request up to 8 bytes. */
61
".byte 0x0f, 0xa7, 0xc0\n\t" /* XSTORE RNG. */
62
"movl %%eax, %0\n" /* Return the status. */
65
: "%edx", "%edi", "cc"
67
if ((status & (1<<6)) /* RNG still enabled. */
68
&& !(status & (1<<13)) /* von Neumann corrector is enabled. */
69
&& !(status & (1<<14)) /* String filter is disabled. */
70
&& !(status & 0x1c00) /* BIAS voltage at default. */
71
&& (!(status & 0x1f) || (status & 0x1f) == 8) /* Sanity check. */
74
nbytes += (status & 0x1f);
76
break; /* Don't get into the loop with the fast flag set. */
81
/* If there was an error we need to break the loop and
82
record that there is something wrong with the padlock
91
(*add) (buffer, nbytes, origin);
92
wipememory (buffer, nbytes);
96
#endif /*USE_PADLOCK*/
100
_gcry_rndhw_failed_p (void)
106
/* Try to read random from a hardware RNG if a fast one is
109
_gcry_rndhw_poll_fast (void (*add)(const void*, size_t, enum random_origins),
110
enum random_origins origin)
116
if ((_gcry_get_hw_features () & HWF_PADLOCK_RNG))
117
poll_padlock (add, origin, 1);
122
/* Read 64 bytes from a hardware RNG and return the number of bytes
125
_gcry_rndhw_poll_slow (void (*add)(const void*, size_t, enum random_origins),
126
enum random_origins origin)
134
if ((_gcry_get_hw_features () & HWF_PADLOCK_RNG))
135
nbytes += poll_padlock (add, origin, 0);