cryptlib  3.4.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros
dos.c
Go to the documentation of this file.
1 /****************************************************************************
2 * *
3 * MSDOS Randomness-Gathering Code *
4 * Copyright Peter Gutmann 1996-2002 *
5 * *
6 ****************************************************************************/
7 
8 /* This module is part of the cryptlib continuously seeded pseudorandom
9  number generator. For usage conditions, see random.c.
10 
11  This code represents a template for randomness-gathering only and will
12  need to be modified to provide OS-specific randomness. In its current
13  form it does not provide any usable entropy and should not be used as an
14  entropy source */
15 
16 /* General includes */
17 
18 #include "crypt.h"
19 
20 /* OS-specific includes */
21 
22 #include <fcntl.h>
23 #include <io.h>
24 #include <time.h>
25 
26 void fastPoll( void )
27  {
29  time_t timeStamp = time( NULL );
30 
31  /* There's not much we can do under DOS, we rely entirely on the
32  /dev/random read for information */
33  setMessageData( &msgData, &timeStamp, sizeof( time_t ) );
35  &msgData, CRYPT_IATTRIBUTE_ENTROPY );
36  }
37 
38 void slowPoll( void )
39  {
41  BYTE buffer[ 128 ];
42  int quality = 100, fd, count, total;
43 
44  /* Read 128 bytes from /dev/random and add it to the buffer. Since DOS
45  doesn't swap we don't need to be as careful about copying data to
46  temporary buffers as we usually are. We also have to use unbuffered
47  I/O, since the high-level functions will read BUFSIZ bytes at once
48  from the input, comletely draining the driver of any randomness */
49  if( ( fd = open( "/dev/random$", O_RDONLY | O_BINARY) ) == -1 && \
50  ( fd = open( "/dev/random", O_RDONLY | O_BINARY) ) == -1 )
51  return;
52  for( total = 0; total < sizeof( buffer ); )
53  {
54  count = read( fd, buffer + total, sizeof( buffer ) - total );
55  if( count <= 0 )
56  break;
57  total += count;
58  }
59  close( fd );
60  setMessageData( &msgData, buffer, total );
62  &msgData, CRYPT_IATTRIBUTE_ENTROPY );
63  zeroise( buffer, sizeof( buffer ) );
65  &quality, CRYPT_IATTRIBUTE_ENTROPY_QUALITY );
66  }