cryptlib  3.4.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros
ecos.c
Go to the documentation of this file.
1 /****************************************************************************
2 * *
3 * eCOS Randomness-Gathering Code *
4 * Copyright Peter Gutmann 1996-2005 *
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 randomness via an external source. In its
13  current form it does not provide any usable entropy and should not be
14  used as an entropy source */
15 
16 /* General includes */
17 
18 #include "crypt.h"
19 #include "random/random.h"
20 
21 /* OS-specific includes */
22 
23 #include <cyg/hal/hal_arch.h>
24 #include <cyg/kernel/kapi.h>
25 #ifdef CYGPKG_POWER
26  #include <cyg/power/power.h>
27 #endif /* CYGPKG_POWER */
28 #ifdef CYGPKG_IO_PCI
29  #include <cyg/io/pci.h>
30 #endif /* CYGPKG_IO_PCI */
31 
32 /* The size of the intermediate buffer used to accumulate polled data */
33 
34 #define RANDOM_BUFSIZE 256
35 
36 void slowPoll( void )
37  {
38  RANDOM_STATE randomState;
40  cyg_handle_t hThread = 0;
41  cyg_uint16 threadID = 0;
42 #ifdef CYGPKG_IO_PCI
43  cyg_pci_device_id pciDeviceID;
44 #endif /* CYGPKG_IO_PCI */
45 #ifdef CYGPKG_POWER
46  PowerController *powerControllerInfo;
47 #endif /* CYGPKG_POWER */
48  int itemsAdded = 0, iterationCount;
49 
50  initRandomData( randomState, buffer, RANDOM_BUFSIZE );
51 
52  /* Get the thread handle, ID, state, priority, and stack usage for
53  every thread in the system */
54  for( iterationCount = 0;
55  cyg_thread_get_next( &hThread, &threadID ) && \
56  iterationCount < FAILSAFE_ITERATIONS_MED;
57  iterationCount++ )
58  {
59  cyg_thread_info threadInfo;
60 
61  if( !cyg_thread_get_info( hThread, threadID, &threadInfo ) )
62  continue;
63  addRandomData( randomState, &threadInfo, sizeof( cyg_thread_info ) );
64  itemsAdded++;
65  }
66 
67  /* Walk the power-management info getting the power-management state for
68  each device. This works a bit strangely, the power controller
69  information is a static table created at system build time so all that
70  we're doing is walking down an array getting one entry after another */
71 #ifdef CYGPKG_POWER
72  for( powerControllerInfo = &( __POWER__[ 0 ] ), iterationCount = 0;
73  powerControllerInfo != &( __POWER_END__ ) && \
74  iterationCount < FAILSAFE_ITERATIONS_MED;
75  powerControllerInfo++, iterationCount++ )
76  {
77  const PowerMode power_get_controller_mode( powerControllerInfo );
78 
79  addRandomValue( randomState, PowerMode );
80  }
81 #endif /* CYGPKG_POWER */
82 
83  /* Add PCI device information if there's PCI support present */
84 #ifdef CYGPKG_IO_PCI
85  if( cyg_pci_find_next( CYG_PCI_NULL_DEVID, &pciDeviceID ) )
86  {
87  iterationCount = 0;
88  do
89  {
90  cyg_pci_device pciDeviceInfo;
91 
92  cyg_pci_get_device_info( pciDeviceID, &pciDeviceInfo );
93  addRandomValue( randomState, PowerMode );
94  addRandomData( randomState, &pciDeviceInfo,
95  sizeof( cyg_pci_device ) );
96  itemsAdded++;
97  }
98  while( cyg_pci_find_next( pciDeviceID, &pciDeviceID ) && \
99  iterationCount++ < FAILSAFE_ITERATIONS_MED );
100  }
101 #endif /* CYGPKG_IO_PCI */
102 
103  /* eCOS also has a CPU load-monitoring facility that we could in theory
104  use as a source of entropy but this is really meant for performance-
105  monitoring and isn't very suitable for use as an entropy source. The
106  way this works is that your first call a calibration function
107  cyg_cpuload_calibrate() and then when it you want to get load
108  statistics call cyg_cpuload_create()/cyg_cpuload_get()/
109  cyg_cpuload_delete(), with get() returning the load over a 0.1s, 1s,
110  and 10s interval. The only one of these capabilities that's even
111  potentially usable is cyg_cpuload_calibrate() and even that's rather
112  dubious for general use since it runs a thread at the highest priority
113  level for 0.1s for calibration purposes and measures the elapsed tick
114  count, which will hardly endear us to other threads in the system.
115  It's really meant for development-mode load measurements and can't
116  safely be used as an entropy source */
117 
118  /* Flush any remaining data through and produce an estimate of its
119  value. Unlike its use in standard OSes this isn't really a true
120  estimate since virtually all of the entropy is coming from the seed
121  file, all this does is complete the seed-file quality estimate to
122  make sure that we don't fail the entropy test */
123  endRandomData( randomState, ( itemsAdded > 5 ) ? 20 : 0 );
124  }
125 
126 void fastPoll( void )
127  {
128  RANDOM_STATE randomState;
130  cyg_uint32 clockTicks;
131 
132  initRandomData( randomState, buffer, RANDOM_BUFSIZE );
133 
134  hal_clock_read( &clockTicks );
135 
136  endRandomData( randomState, 1 );
137  }