Random number generator

This commit is contained in:
Philip Homburg
2005-07-18 15:40:24 +00:00
parent 3eb405c3bf
commit 7d4e914618
20 changed files with 3739 additions and 36 deletions

View File

@@ -297,11 +297,11 @@ PUBLIC unsigned long read_clock()
*/
unsigned count;
lock(10, "read_clock");
/* lock(10, "read_clock"); */
outb(TIMER_MODE, LATCH_COUNT);
count = inb(TIMER0);
count |= (inb(TIMER0) << 8);
unlock(10);
/* unlock(10); */
return count;
}

View File

@@ -27,6 +27,9 @@
#define _SRC_ 0
#define _DST_ 1
/* Number of random sources */
#define RANDOM_SOURCES 16
/* Constants and macros for bit map manipulation. */
#define BITCHUNK_BITS (sizeof(bitchunk_t) * CHAR_BIT)
#define BITMAP_CHUNKS(nr_bits) (((nr_bits)+BITCHUNK_BITS-1)/BITCHUNK_BITS)

View File

@@ -56,7 +56,7 @@ _PROTOTYPE( void clear_proc, (int proc_nr) );
_PROTOTYPE( phys_bytes numap_local, (int proc_nr, vir_bytes vir_addr,
vir_bytes bytes) );
_PROTOTYPE( void sys_task, (void) );
_PROTOTYPE( void get_randomness, (void) );
_PROTOTYPE( void get_randomness, (int source) );
_PROTOTYPE( int virtual_copy, (struct vir_addr *src, struct vir_addr *dst,
vir_bytes bytes) );
_PROTOTYPE( phys_bytes umap_local, (struct proc *rp, int seg,

View File

@@ -273,11 +273,13 @@ int proc_nr; /* slot of process to clean up */
/*===========================================================================*
* get_randomness *
*===========================================================================*/
PUBLIC void get_randomness()
PUBLIC void get_randomness(source)
int source;
{
/* Gather random information with help of the CPU's cycle counter. Only use
* the lowest bytes because the highest bytes won't differ that much.
*/
int r_next;
unsigned long tsc_high;
/* On machines with the RDTSC (cycle counter read instruction - pentium
@@ -287,15 +289,17 @@ PUBLIC void get_randomness()
* Unfortunately this test is run-time - we don't want to bother with
* compiling different kernels for different machines..
*
* On machines without RDTSC, we use the get_uptime() - read_clock()
* has a higher resolution, but would involve I/O calls.
* On machines without RDTSC, we use read_clock().
*/
if (machine.processor > 486)
read_tsc(&tsc_high, &krandom.r_buf[krandom.r_next]);
source %= RANDOM_SOURCES;
r_next= krandom.bin[source].r_next;
if(machine.processor > 486 && 0)
read_tsc(&tsc_high, &krandom.bin[source].r_buf[r_next]);
else
krandom.r_buf[krandom.r_next] = get_uptime();
if (krandom.r_size < RANDOM_ELEMENTS) krandom.r_size ++;
krandom.r_next = (krandom.r_next + 1 ) % RANDOM_ELEMENTS;
krandom.bin[source].r_buf[r_next] = read_clock();
if (krandom.bin[source].r_size < RANDOM_ELEMENTS)
krandom.bin[source].r_size ++;
krandom.bin[source].r_next = (r_next + 1 ) % RANDOM_ELEMENTS;
}
@@ -313,7 +317,7 @@ irq_hook_t *hook;
/* As a side-effect, the interrupt handler gathers random information by
* timestamping the interrupt events. This is used for /dev/random.
*/
get_randomness();
get_randomness(hook->irq);
/* Add a bit for this interrupt to the process' pending interrupts. When
* sending the notification message, this bit map will be magically set

View File

@@ -87,8 +87,15 @@ register message *m_ptr; /* pointer to request message */
break;
}
case GET_RANDOMNESS: {
struct randomness copy = krandom; /* copy to keep counters */
krandom.r_next = krandom.r_size = 0; /* invalidate random data */
static struct randomness copy; /* copy to keep counters */
int i;
copy = krandom;
for (i= 0; i<RANDOM_SOURCES; i++) {
krandom.bin[i].r_size = 0; /* invalidate random data */
krandom.bin[i].r_next = 0;
}
length = sizeof(struct randomness);
src_phys = vir2phys(&copy);
break;

View File

@@ -53,9 +53,11 @@ struct kmessages {
};
struct randomness {
int r_next; /* next index to write */
int r_size; /* number of random elements */
unsigned long r_buf[RANDOM_ELEMENTS]; /* buffer for random info */
struct {
int r_next; /* next index to write */
int r_size; /* number of random elements */
unsigned long r_buf[RANDOM_ELEMENTS]; /* buffer for random info */
} bin[RANDOM_SOURCES];
};
#if (CHIP == INTEL)