~ubuntu-branches/ubuntu/natty/libgcrypt11/natty-proposed

« back to all changes in this revision

Viewing changes to cipher/rndhw.c

  • Committer: Bazaar Package Importer
  • Author(s): Bhavani Shankar
  • Date: 2009-05-16 20:13:32 UTC
  • mfrom: (1.1.6 upstream) (2.1.3 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090516201332-czkobpu32w318i16
Tags: 1.4.4-2ubuntu1
* Merge from Debian unstable (LP: #364535), remaining changes:
  - Add libgcrypt11-udeb for use by cryptsetup-udeb.
  - Add clean-la.mk, and add a symlink for the .la
  - Install to /lib.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
#include <assert.h>
24
 
 
25
 
#include "types.h"
26
 
#include "g10lib.h"
27
 
#include "rand-internal.h"
28
 
 
29
 
#undef USE_PADLOCK
30
 
#ifdef ENABLE_PADLOCK_SUPPORT
31
 
# if defined (__i386__) && SIZEOF_UNSIGNED_LONG == 4 && defined (__GNUC__)
32
 
# define USE_PADLOCK
33
 
# endif
34
 
#endif /*ENABLE_PADLOCK_SUPPORT*/
35
 
 
36
 
/* Keep track on whether the RNG has problems.  */
37
 
static volatile int rng_failed;
38
 
 
39
 
 
40
 
#ifdef USE_PADLOCK
41
 
static size_t
42
 
poll_padlock (void (*add)(const void*, size_t, enum random_origins),
43
 
              enum random_origins origin, int fast)
44
 
{
45
 
  char buffer[64+8] __attribute__ ((aligned (8)));
46
 
  char *p;
47
 
  unsigned int nbytes, status;
48
 
  
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
54
 
     is running.  */
55
 
  p = buffer;
56
 
  nbytes = 0;
57
 
  while (nbytes < 64)
58
 
    {
59
 
      asm volatile 
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.  */
64
 
         : "=g" (status)
65
 
         : "g" (p)
66
 
         : "%edx", "%edi", "cc"
67
 
         );
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.  */
73
 
          )
74
 
        {
75
 
          nbytes += (status & 0x1f);
76
 
          if (fast)
77
 
            break; /* Don't get into the loop with the fast flag set.  */
78
 
          p += (status & 0x1f);
79
 
        }
80
 
      else 
81
 
        {
82
 
          /* If there was an error we need to break the loop and
83
 
             record that there is something wrong with the padlock
84
 
             RNG.  */
85
 
          rng_failed = 1;
86
 
          break; 
87
 
        }
88
 
    }
89
 
 
90
 
  if (nbytes)
91
 
    {
92
 
      (*add) (buffer, nbytes, origin);
93
 
      wipememory (buffer, nbytes);
94
 
    }
95
 
  return nbytes;
96
 
}
97
 
#endif /*USE_PADLOCK*/
98
 
 
99
 
 
100
 
int
101
 
_gcry_rndhw_failed_p (void)
102
 
{
103
 
  return rng_failed;
104
 
}
105
 
 
106
 
 
107
 
/* Try to read random from a hardware RNG if a fast one is
108
 
   available.  */
109
 
void
110
 
_gcry_rndhw_poll_fast (void (*add)(const void*, size_t, enum random_origins),
111
 
                       enum random_origins origin)
112
 
{
113
 
  (void)add;
114
 
  (void)origin;
115
 
 
116
 
#ifdef USE_PADLOCK
117
 
  if ((_gcry_get_hw_features () & HWF_PADLOCK_RNG))
118
 
    poll_padlock (add, origin, 1);
119
 
#endif  
120
 
}
121
 
 
122
 
 
123
 
/* Read 64 bytes from a hardware RNG and return the number of bytes
124
 
   actually read.  */
125
 
size_t
126
 
_gcry_rndhw_poll_slow (void (*add)(const void*, size_t, enum random_origins),
127
 
                       enum random_origins origin)
128
 
{
129
 
  size_t nbytes = 0;
130
 
 
131
 
  (void)add;
132
 
  (void)origin;
133
 
 
134
 
#ifdef USE_PADLOCK
135
 
  if ((_gcry_get_hw_features () & HWF_PADLOCK_RNG))
136
 
    nbytes += poll_padlock (add, origin, 0);
137
 
#endif  
138
 
 
139
 
  return nbytes;
140
 
}