~ubuntu-branches/ubuntu/utopic/dropbear/utopic-proposed

« back to all changes in this revision

Viewing changes to libtomcrypt/src/prngs/rng_get_bytes.c

  • Committer: Bazaar Package Importer
  • Author(s): Matt Johnston
  • Date: 2005-12-08 19:20:21 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051208192021-nyp9rwnt77nsg6ty
Tags: 0.47-1
* New upstream release.
* SECURITY: Fix incorrect buffer sizing.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
 
2
 *
 
3
 * LibTomCrypt is a library that provides various cryptographic
 
4
 * algorithms in a highly modular and flexible manner.
 
5
 *
 
6
 * The library is free for all purposes without any express
 
7
 * guarantee it works.
 
8
 *
 
9
 * Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.org
 
10
 */
 
11
#include "tomcrypt.h"
 
12
 
 
13
/** 
 
14
   @file rng_get_bytes.c
 
15
   portable way to get secure random bits to feed a PRNG (Tom St Denis)
 
16
*/
 
17
 
 
18
#ifdef DEVRANDOM
 
19
/* on *NIX read /dev/random */
 
20
static unsigned long rng_nix(unsigned char *buf, unsigned long len, 
 
21
                             void (*callback)(void))
 
22
{
 
23
#ifdef LTC_NO_FILE
 
24
    return 0;
 
25
#else
 
26
    FILE *f;
 
27
    unsigned long x;
 
28
#ifdef TRY_URANDOM_FIRST
 
29
    f = fopen("/dev/urandom", "rb");
 
30
    if (f == NULL)
 
31
#endif /* TRY_URANDOM_FIRST */
 
32
       f = fopen("/dev/random", "rb");
 
33
 
 
34
    if (f == NULL) {
 
35
       return 0;
 
36
    }
 
37
    
 
38
    /* disable buffering */
 
39
    if (setvbuf(f, NULL, _IONBF, 0) != 0) {
 
40
       fclose(f);
 
41
       return 0;
 
42
    }   
 
43
 
 
44
    x = (unsigned long)fread(buf, 1, (size_t)len, f);
 
45
    fclose(f);
 
46
    return x;
 
47
#endif /* LTC_NO_FILE */
 
48
}
 
49
 
 
50
#endif /* DEVRANDOM */
 
51
 
 
52
/* on ANSI C platforms with 100 < CLOCKS_PER_SEC < 10000 */
 
53
#if defined(CLOCKS_PER_SEC)
 
54
 
 
55
#define ANSI_RNG
 
56
 
 
57
static unsigned long rng_ansic(unsigned char *buf, unsigned long len, 
 
58
                               void (*callback)(void))
 
59
{
 
60
   clock_t t1;
 
61
   int l, acc, bits, a, b;
 
62
 
 
63
   if (XCLOCKS_PER_SEC < 100 || XCLOCKS_PER_SEC > 10000) {
 
64
      return 0;
 
65
   }
 
66
 
 
67
   l = len;
 
68
   bits = 8;
 
69
   acc  = a = b = 0;
 
70
   while (len--) {
 
71
       if (callback != NULL) callback();
 
72
       while (bits--) {
 
73
          do {
 
74
             t1 = XCLOCK(); while (t1 == XCLOCK()) a ^= 1;
 
75
             t1 = XCLOCK(); while (t1 == XCLOCK()) b ^= 1;
 
76
          } while (a == b);
 
77
          acc = (acc << 1) | a;
 
78
       }
 
79
       *buf++ = acc; 
 
80
       acc  = 0;
 
81
       bits = 8;
 
82
   }
 
83
   acc = bits = a = b = 0;
 
84
   return l;
 
85
}
 
86
 
 
87
#endif 
 
88
 
 
89
/* Try the Microsoft CSP */
 
90
#ifdef WIN32
 
91
#define _WIN32_WINNT 0x0400
 
92
#include <windows.h>
 
93
#include <wincrypt.h>
 
94
 
 
95
static unsigned long rng_win32(unsigned char *buf, unsigned long len, 
 
96
                               void (*callback)(void))
 
97
{
 
98
   HCRYPTPROV hProv = 0;
 
99
   if (!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, 
 
100
                            (CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET)) && 
 
101
       !CryptAcquireContext (&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, 
 
102
                            CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET | CRYPT_NEWKEYSET))
 
103
      return 0;
 
104
 
 
105
   if (CryptGenRandom(hProv, len, buf) == TRUE) {
 
106
      CryptReleaseContext(hProv, 0);
 
107
      return len;
 
108
   } else {
 
109
      CryptReleaseContext(hProv, 0);
 
110
      return 0;
 
111
   }
 
112
}
 
113
 
 
114
#endif /* WIN32 */
 
115
 
 
116
/**
 
117
  Read the system RNG
 
118
  @param out       Destination
 
119
  @param outlen    Length desired (octets)
 
120
  @param callback  Pointer to void function to act as "callback" when RNG is slow.  This can be NULL
 
121
  @return Number of octets read
 
122
*/     
 
123
unsigned long rng_get_bytes(unsigned char *out, unsigned long outlen, 
 
124
                            void (*callback)(void))
 
125
{
 
126
   unsigned long x;
 
127
 
 
128
   LTC_ARGCHK(out != NULL);
 
129
 
 
130
#if defined(DEVRANDOM)
 
131
   x = rng_nix(out, outlen, callback);   if (x != 0) { return x; }
 
132
#endif
 
133
#ifdef WIN32
 
134
   x = rng_win32(out, outlen, callback); if (x != 0) { return x; }
 
135
#endif
 
136
#ifdef ANSI_RNG
 
137
   x = rng_ansic(out, outlen, callback); if (x != 0) { return x; }
 
138
#endif
 
139
   return 0;
 
140
}
 
141
 
 
142
/* $Source: /cvs/libtom/libtomcrypt/src/prngs/rng_get_bytes.c,v $ */
 
143
/* $Revision: 1.3 $ */
 
144
/* $Date: 2005/05/05 14:35:59 $ */