cryptlib  3.4.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros
palmos.c
Go to the documentation of this file.
1 /****************************************************************************
2 * *
3 * PalmOS Randomness-Gathering Code *
4 * Copyright Peter Gutmann 2003-2004 *
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 /* General includes */
12 
13 #include <sys/time.h>
14 #include "crypt.h"
15 #include "random/random.h"
16 
17 /* OS-specific includes */
18 
19 #include <AppMgr.h>
20 #include <Event.h>
21 #include <FeatureMgr.h>
22 #include <Font.h>
23 #include <Form.h>
24 #include <Menu.h>
25 #include <MemoryMgr.h>
26 #include <SystemMgr.h>
27 #include <SysThread.h>
28 #include <TimeMgr.h>
29 #include <VFSMgr.h>
30 #include <Window.h>
31 
32 /* The size of the intermediate buffer used to accumulate polled data. We
33  make this somewhat smaller than the default 4K used for other entropy-
34  polling modules because the default PalmOS thread stack size is a
35  somewhat pathetic 4K, and we don't want to use all of it just on the
36  entropy buffer */
37 
38 #define RANDOM_BUFSIZE 1024
39 
40 void fastPoll( void )
41  {
42  RANDOM_STATE randomState;
43  BYTE buffer[ RANDOM_BUFSIZE + 8 ];
44  WinHandle winHandle;
45  Coord xCoord, yCoord;
46  Boolean flag;
47  uint64_t ticks;
48  nsecs_t nsTime;
49 
50  initRandomData( randomState, buffer, RANDOM_BUFSIZE );
51 
52  /* Get the event-available and low-level event-available flag, current
53  pen status, and handle of the window with the input focus */
54  flag = EvtEventAvail();
55  addRandomValue( randomState, flag );
56  flag = EvtSysEventAvail( TRUE );
57  addRandomValue( randomState, flag );
58  EvtGetPen( &xCoord, &yCoord, &flag );
59  addRandomValue( randomState, xCoord );
60  addRandomValue( randomState, yCoord );
61  winHandle = EvtGetFocusWindow();
62  addRandomValue( randomState, winHandle );
63 
64  /* Get the number of ticks of the (software) millisecond clock used
65  by the scheduler, and the length of time in nanoseconds since the
66  last reset */
67  ticks = TimGetTicks();
68  addRandomData( randomState, &ticks, sizeof( uint64_t ) );
69  nsTime = SysGetRunTime();
70  addRandomData( randomState, &nsTime, sizeof( nsecs_t ) );
71 
72  /* Get the value of the real-time and runtime clocks in nanoseconds.
73  One of these may just be a wrapper for SysGetRunTime(), in addition
74  it's likely that they're hardware-specific, being CPU-level cycle
75  counters of some kind */
76  nsTime = system_real_time();
77  addRandomData( randomState, &nsTime, sizeof( nsecs_t ) );
78  nsTime = system_time();
79  addRandomData( randomState, &nsTime, sizeof( nsecs_t ) );
80 
81  /* Flush any remaining data through */
82  endRandomData( randomState, 5 );
83  }
84 
85 void slowPoll( void )
86  {
87  static BOOLEAN addedFixedItems = FALSE;
88  RANDOM_STATE randomState;
89  BYTE buffer[ RANDOM_BUFSIZE + 8 ];
90  struct batteryInfoType {
91  uint16_t warnThreshold; /* Percent left for warn */
92  uint16_t criticalThreshold; /* Percent left for critical warn */
93  uint16_t shutdownThreshold; /* Percent left for shutdown */
94  uint32_t timeout; /* Battery timeout */
95  SysBatteryKind type; /* Battery type */
96  Boolean pluggedIn; /* Whether battery plugged in */
97  uint8_t powerLevel; /* Percent power remaining */
98  } batteryInfo;
99  const FontType *fontPtr;
100  MenuBarType *menuPtr;
101  void *stackStart, *stackEnd;
102  MemHeapInfoType memInfo;
103  RectangleType rectangleInfo;
104  EvtQueueHandle evtQueueHandle;
105  DatabaseID databaseID;
106  FontID fontID;
107  WinHandle winHandle;
108  WinFlagsType winFlags;
109  PatternType pattern;
110  uint32_t version;
111  uint16_t formID;
112  uint8_t value;
113 
114  initRandomData( randomState, buffer, RANDOM_BUFSIZE );
115 
116  /* Get the handle of the current thread's event queue, current resource
117  database ID, start and end of the current thread's stack, ID and
118  pointer to the current font, ID and pointer to the currently active
119  form, and pointer to the currently active menu */
120  evtQueueHandle = EvtGetThreadEventQueue();
121  addRandomValue( randomState, evtQueueHandle );
122  SysCurAppDatabase( &databaseID );
123  addRandomValue( randomState, databaseID );
124  SysGetStackInfo( &stackStart, &stackEnd );
125  addRandomData( randomState, &stackStart, sizeof( void * ) );
126  addRandomData( randomState, &stackEnd, sizeof( void * ) );
127  fontID = FntGetFont();
128  addRandomValue( randomState, fontID );
129  fontPtr = FntGetFontPtr();
130  addRandomData( randomState, &fontPtr, sizeof( FontType * ) );
131  formID = FrmGetActiveFormID();
132  addRandomValue( randomState, formID );
133  if( formID > 0 )
134  {
135  FormType *formPtr;
136 
137  formPtr = FrmGetFormPtr( formID );
138  addRandomData( randomState, &formPtr, sizeof( FormType * ) );
139  }
140  menuPtr = MenuGetActiveMenu();
141  addRandomData( randomState, &menuPtr, sizeof( MenuBarType * ) );
142 
143  /* Get system memory info: heap base address, total memory, memory in
144  use, number of chunks allocated/free and chunk memory used/free,
145  available memory block info */
146  MemDynHeapGetInfo( &memInfo );
147  addRandomData( randomState, &memInfo, sizeof( MemHeapInfoType ) );
148 
149  /* Get the handle, creation flags, and size of the active window, the
150  screen window created at startup, and the current draw window, the
151  size and clipping rectangle of the draw window, the current pattern
152  type, and the current scaling mode */
153  winHandle = WinGetActiveWindow();
154  addRandomValue( randomState, winHandle );
155  winFlags = WinGetWindowFlags( winHandle );
156  addRandomValue( randomState, winFlags );
157  WinGetWindowFrameRect( winHandle, &rectangleInfo );
158  addRandomData( randomState, &rectangleInfo, sizeof( RectangleType ) );
159  winHandle = WinGetDisplayWindow();
160  addRandomValue( randomState, winHandle );
161  winHandle = WinGetDrawWindow();
162  addRandomValue( randomState, winHandle );
163  winFlags = WinGetWindowFlags( winHandle );
164  addRandomValue( randomState, winFlags );
165  WinGetDrawWindowBounds( &rectangleInfo );
166  addRandomData( randomState, &rectangleInfo, sizeof( RectangleType ) );
167  WinGetClip( &rectangleInfo );
168  addRandomData( randomState, &rectangleInfo, sizeof( RectangleType ) );
169  pattern = WinGetPatternType();
170  addRandomValue( randomState, pattern );
171  if( FtrGet( sysFtrCreator, sysFtrNumWinVersion, &version ) == errNone && \
172  version >= 5 )
173  {
174  uint32_t scaleType;
175 
176  /* Not implemented before PalmOS 5.3, requires the 1.5x Display
177  Feature Set to avoid generating a fatal alert */
178  scaleType = WinGetScalingMode();
179  addRandomValue( randomState, scaleType );
180  }
181 
182  /* Get expansiode card info (capability flags, manufacturer, product,
183  and device info including unique serial number if available), and
184  media info (disk space, partition info, pseudo-HDD metrics) for all
185  expansion slots */
186  if( FtrGet( sysFileCExpansionMgr,expFtrIDVersion, &version ) == errNone )
187  {
188  uint32_t slotIterator = expIteratorStart;
189  uint16_t slotRefNum;
190 
191  while( slotIterator != expIteratorStop && \
192  ExpSlotEnumerate( &slotRefNum, &slotIterator ) == errNone )
193  {
194  ExpCardInfoType cardInfo;
195  CardMetricsType cardMetrics;
196 
197  addRandomValue( randomState, slotRefNum );
198  ExpCardInfo( slotRefNum, &cardInfo );
199  addRandomData( randomState, &cardInfo, sizeof( ExpCardInfoType ) );
200  ExpCardMetrics( slotRefNum, &cardMetrics );
201  addRandomData( randomState, &cardMetrics, sizeof( CardMetricsType ) );
202  }
203  }
204 
205  /* Get attributes, filesystem type, mount info, media type, space used,
206  and total space for all mounted volumes */
207  if( FtrGet( sysFileCVFSMgr, vfsFtrIDVersion, &version ) == errNone )
208  {
209  uint32_t volIterator = vfsIteratorStart;
210  uint16_t volRefNum;
211 
212  while( volIterator != vfsIteratorStop && \
213  VFSVolumeEnumerate( &volRefNum, &volIterator ) == errNone )
214  {
215  VolumeInfoType volInfo;
216  uint32_t volUsed, volTotal;
217 
218  addRandomValue( randomState, volRefNum );
219  VFSVolumeInfo( volRefNum, &volInfo );
220  addRandomData( randomState, &volInfo, sizeof( VolumeInfoType ) );
221  VFSVolumeSize( volRefNum, &volUsed, &volTotal );
222  addRandomValue( randomState, volUsed );
223  addRandomValue( randomState, volTotal );
224  }
225  }
226 
227  /* Get battery state info */
228  if( SysBatteryInfo( FALSE, &batteryInfo.warnThreshold,
229  &batteryInfo.criticalThreshold,
230  &batteryInfo.shutdownThreshold,
231  &batteryInfo.timeout, &batteryInfo.type,
232  &batteryInfo.pluggedIn,
233  &batteryInfo.powerLevel ) == errNone )
234  addRandomData( randomState, &batteryInfo,
235  sizeof( struct batteryInfoType ) );
236 
237  /* Get the LCD brightness and contrast level */
238  value = SysLCDBrightness( FALSE, 0 );
239  addRandomValue( randomState, value );
240  value = SysLCDContrast( FALSE, 0 );
241  addRandomValue( randomState, value );
242 
243  /* The following are fixed for the lifetime of the process so we only
244  add them once */
245  if( !addedFixedItems )
246  {
247  struct ftrInfoType {
248  uint32_t creator; /* Feature creator */
249  uint16_t number; /* Feature number */
250  uint32_t value; /* Feature value */
251  } ftrInfo;
252  uint16_t ftrIterator, romTokenSize;
253  uint8_t *romToken;
254 
255  /* Get system features. This includes a large amount of
256  information ranging from fairly static (extensive hardware
257  capability info, OS version/configuration data) through to
258  variable (default font, locale, etc) */
259  for( ftrIterator = 0; \
260  FtrGetByIndex( ftrIterator, FALSE, &ftrInfo.creator, \
261  &ftrInfo.number, &ftrInfo.value ) == errNone; \
262  ftrIterator++ )
263  addRandomData( randomState, &ftrInfo, sizeof( struct ftrInfoType ) );
264 
265  /* Get the ROM serial number. This is somewhat complex, for it to
266  be valid the function call has to succeed and the returned
267  pointer has to be non-null and the first byte of the returned
268  data can't be 0xFF */
269  if( SysGetROMToken( sysROMTokenSnum, &romToken, \
270  &romTokenSize ) == errNone && \
271  romToken != NULL && *romToken != 0xFF )
272  addRandomData( randomState, &romToken, romTokenSize );
273 
274  addedFixedItems = TRUE;
275  }
276 
277  /* Flush any remaining data through */
278  endRandomData( randomState, 100 );
279  }