moved type and constants for random data to include file;

added consistency check in random; added source of randomness
internal to random using timing; only retrieve random bins that are full.
This commit is contained in:
Ben Gras
2009-04-02 15:24:44 +00:00
parent 51596bc608
commit 9647fbc94e
19 changed files with 177 additions and 80 deletions

View File

@@ -36,7 +36,7 @@ register message *m_ptr; /* pointer to request message */
* not very random.
*/
lock;
get_randomness(CLOCK_IRQ);
get_randomness(&krandom, CLOCK_IRQ);
unlock;
return(OK);

View File

@@ -49,11 +49,6 @@
*/
#define P_NAME_LEN 8
/* Buffer to gather randomness. This is used to generate a random stream by
* the MEMORY driver when reading from /dev/random.
*/
#define RANDOM_ELEMENTS 32
/* This section contains defines for valuable system resources that are used
* by device drivers. The number of elements of the vectors is determined by
* the maximum needed by any given driver. The number of interrupt hooks may

View File

@@ -25,9 +25,6 @@
#define _SRC_ 0
#define _DST_ 1
/* Number of random sources */
#define RANDOM_SOURCES 16
#define get_sys_bit(map,bit) \
( MAP_CHUNK(map.chunk,bit) & (1 << CHUNK_OFFSET(bit) )
#define get_sys_bits(map,bit) \

View File

@@ -25,7 +25,7 @@ EXTERN char shutdown_started; /* TRUE after shutdowns / reboots */
EXTERN struct kinfo kinfo; /* kernel information for users */
EXTERN struct machine machine; /* machine information for users */
EXTERN struct kmessages kmess; /* diagnostic messages in kernel */
EXTERN struct randomness krandom; /* gather kernel random information */
EXTERN struct k_randomness krandom; /* gather kernel random information */
EXTERN struct loadinfo kloadinfo; /* status of load average */
/* Process scheduling information and the kernel reentry count. */

View File

@@ -175,6 +175,8 @@ PUBLIC void main()
cprof_procs_no = 0; /* init nr of hash table slots used */
vm_running = 0;
krandom.random_sources = RANDOM_SOURCES;
krandom.random_elements = RANDOM_ELEMENTS;
/* MINIX is now ready. All boot image processes are on the ready queue.
* Return to the assembly code to start running the current process.

View File

@@ -58,7 +58,6 @@ _PROTOTYPE( int get_priv, (register struct proc *rc, int proc_type) );
_PROTOTYPE( void send_sig, (int proc_nr, int sig_nr) );
_PROTOTYPE( void cause_sig, (int proc_nr, int sig_nr) );
_PROTOTYPE( void sys_task, (void) );
_PROTOTYPE( void get_randomness, (int source) );
#define numap_local(proc_nr, vir_addr, bytes) \
umap_local(proc_addr(proc_nr), D, (vir_addr), (bytes))
_PROTOTYPE( phys_bytes umap_grant, (struct proc *, cp_grant_id_t,

View File

@@ -290,28 +290,6 @@ int proc_type; /* system or user process flag */
return(OK);
}
/*===========================================================================*
* get_randomness *
*===========================================================================*/
PUBLIC void get_randomness(source)
int source;
{
/* Use architecture-dependent high-resolution clock for
* raw entropy gathering.
*/
int r_next;
unsigned long tsc_high, tsc_low;
source %= RANDOM_SOURCES;
r_next= krandom.bin[source].r_next;
read_tsc(&tsc_high, &tsc_low);
krandom.bin[source].r_buf[r_next] = tsc_low;
if (krandom.bin[source].r_size < RANDOM_ELEMENTS) {
krandom.bin[source].r_size ++;
}
krandom.bin[source].r_next = (r_next + 1 ) % RANDOM_ELEMENTS;
}
/*===========================================================================*
* send_sig *
*===========================================================================*/

View File

@@ -31,6 +31,7 @@ register message *m_ptr; /* pointer to request message */
int proc_nr, nr_e, nr;
struct proc *caller;
phys_bytes ph;
int wipe_rnd_bin = -1;
caller = proc_addr(who_p);
@@ -112,7 +113,7 @@ register message *m_ptr; /* pointer to request message */
break;
}
case GET_RANDOMNESS: {
static struct randomness copy; /* copy to keep counters */
static struct k_randomness copy; /* copy to keep counters */
int i;
copy = krandom;
@@ -120,10 +121,28 @@ register message *m_ptr; /* pointer to request message */
krandom.bin[i].r_size = 0; /* invalidate random data */
krandom.bin[i].r_next = 0;
}
length = sizeof(struct randomness);
length = sizeof(copy);
src_vir = (vir_bytes) &copy;
break;
}
case GET_RANDOMNESS_BIN: {
int i, bin = m_ptr->I_VAL_LEN2_E;
if(bin < 0 || bin >= RANDOM_SOURCES) {
kprintf("SYSTEM: GET_RANDOMNESS_BIN: %d out of range\n", bin);
return EINVAL;
}
if(krandom.bin[bin].r_size < RANDOM_ELEMENTS)
return ENOENT;
length = sizeof(krandom.bin[bin]);
src_vir = (vir_bytes) &krandom.bin[bin];
wipe_rnd_bin = bin;
break;
}
case GET_KMESSAGES: {
length = sizeof(struct kmessages);
src_vir = (vir_bytes) &kmess;
@@ -158,7 +177,12 @@ register message *m_ptr; /* pointer to request message */
if((ph=umap_local(caller, D, (vir_bytes) m_ptr->I_VAL_PTR,length)) == 0)
return EFAULT;
CHECKRANGE_OR_SUSPEND(caller, ph, length, 1);
data_copy(SYSTEM, src_vir, who_e, (vir_bytes) m_ptr->I_VAL_PTR, length);
if(data_copy(SYSTEM, src_vir, who_e, (vir_bytes) m_ptr->I_VAL_PTR, length) == OK) {
if(wipe_rnd_bin >= 0 && wipe_rnd_bin < RANDOM_SOURCES) {
krandom.bin[wipe_rnd_bin].r_size = 0;
krandom.bin[wipe_rnd_bin].r_next = 0;
}
}
return(OK);
}

View File

@@ -142,7 +142,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(hook->irq);
get_randomness(&krandom, hook->irq);
/* Check if the handler is still alive.
* If it's dead, this should never happen, as processes that die

View File

@@ -2,6 +2,7 @@
#define TYPE_H
#include <minix/com.h>
#include <ibm/interrupt.h>
typedef _PROTOTYPE( void task_t, (void) );
@@ -27,14 +28,6 @@ struct boot_image {
endpoint_t endpoint; /* endpoint number when started */
};
struct randomness {
struct {
int r_next; /* next index to write */
int r_size; /* number of random elements */
unsigned short r_buf[RANDOM_ELEMENTS]; /* buffer for random info */
} bin[RANDOM_SOURCES];
};
typedef unsigned long irq_policy_t;
typedef unsigned long irq_id_t;