1/* rndhw.c - Access to the external random daemon 2 * Copyright (C) 2007 Free Software Foundation, Inc. 3 * 4 * This file is part of Libgcrypt. 5 * 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. 10 * 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. 15 * 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/>. 18 */ 19 20#include <config.h> 21#include <stdio.h> 22#include <stdlib.h> 23 24#include "types.h" 25#include "g10lib.h" 26#include "rand-internal.h" 27 28#undef USE_PADLOCK 29#ifdef ENABLE_PADLOCK_SUPPORT 30# if defined (__i386__) && SIZEOF_UNSIGNED_LONG == 4 && defined (__GNUC__) 31# define USE_PADLOCK 32# endif 33#endif /*ENABLE_PADLOCK_SUPPORT*/ 34 35/* Keep track on whether the RNG has problems. */ 36static volatile int rng_failed; 37 38 39#ifdef USE_PADLOCK 40static size_t 41poll_padlock (void (*add)(const void*, size_t, enum random_origins), 42 enum random_origins origin, int fast) 43{ 44 volatile char buffer[64+8] __attribute__ ((aligned (8))); 45 volatile char *p; 46 unsigned int nbytes, status; 47 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 53 is running. */ 54 p = buffer; 55 nbytes = 0; 56 while (nbytes < 64) 57 { 58 asm volatile 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 : "=a" (status) 63 : "g" (p) 64 : "%edx", "%edi", "cc" 65 ); 66 if ((status & (1<<6)) /* RNG still enabled. */ 67 && !(status & (1<<13)) /* von Neumann corrector is enabled. */ 68 && !(status & (1<<14)) /* String filter is disabled. */ 69 && !(status & 0x1c00) /* BIAS voltage at default. */ 70 && (!(status & 0x1f) || (status & 0x1f) == 8) /* Sanity check. */ 71 ) 72 { 73 nbytes += (status & 0x1f); 74 if (fast) 75 break; /* Don't get into the loop with the fast flag set. */ 76 p += (status & 0x1f); 77 } 78 else 79 { 80 /* If there was an error we need to break the loop and 81 record that there is something wrong with the padlock 82 RNG. */ 83 rng_failed = 1; 84 break; 85 } 86 } 87 88 if (nbytes) 89 { 90 (*add) ((void*)buffer, nbytes, origin); 91 wipememory (buffer, nbytes); 92 } 93 return nbytes; 94} 95#endif /*USE_PADLOCK*/ 96 97 98int 99_gcry_rndhw_failed_p (void) 100{ 101 return rng_failed; 102} 103 104 105/* Try to read random from a hardware RNG if a fast one is 106 available. */ 107void 108_gcry_rndhw_poll_fast (void (*add)(const void*, size_t, enum random_origins), 109 enum random_origins origin) 110{ 111 (void)add; 112 (void)origin; 113 114#ifdef USE_PADLOCK 115 if ((_gcry_get_hw_features () & HWF_PADLOCK_RNG)) 116 poll_padlock (add, origin, 1); 117#endif 118} 119 120 121/* Read 64 bytes from a hardware RNG and return the number of bytes 122 actually read. */ 123size_t 124_gcry_rndhw_poll_slow (void (*add)(const void*, size_t, enum random_origins), 125 enum random_origins origin) 126{ 127 size_t nbytes = 0; 128 129 (void)add; 130 (void)origin; 131 132#ifdef USE_PADLOCK 133 if ((_gcry_get_hw_features () & HWF_PADLOCK_RNG)) 134 nbytes += poll_padlock (add, origin, 0); 135#endif 136 137 return nbytes; 138} 139