harvest.c (67365) | harvest.c (69168) |
---|---|
1/*- 2 * Copyright (c) 2000 Mark R V Murray 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 9 unchanged lines hidden (view full) --- 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 * | 1/*- 2 * Copyright (c) 2000 Mark R V Murray 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 9 unchanged lines hidden (view full) --- 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 * |
26 * $FreeBSD: head/sys/dev/random/harvest.c 67365 2000-10-20 07:58:15Z jhb $ | 26 * $FreeBSD: head/sys/dev/random/harvest.c 69168 2000-11-25 17:09:01Z markm $ |
27 */ 28 29#include <sys/param.h> 30#include <sys/systm.h> 31#include <sys/types.h> 32#include <sys/queue.h> 33#include <sys/kthread.h> 34#include <sys/mutex.h> 35#include <sys/poll.h> 36#include <sys/select.h> 37#include <sys/random.h> | 27 */ 28 29#include <sys/param.h> 30#include <sys/systm.h> 31#include <sys/types.h> 32#include <sys/queue.h> 33#include <sys/kthread.h> 34#include <sys/mutex.h> 35#include <sys/poll.h> 36#include <sys/select.h> 37#include <sys/random.h> |
38#include <sys/time.h> | 38 39#include <machine/cpu.h> 40 |
39#include <crypto/blowfish/blowfish.h> 40 41#include <dev/random/hash.h> 42#include <dev/random/yarrow.h> 43 44static u_int read_random_phony(void *, u_int); 45 46/* hold the address of the routine which is actually called if | 41#include <crypto/blowfish/blowfish.h> 42 43#include <dev/random/hash.h> 44#include <dev/random/yarrow.h> 45 46static u_int read_random_phony(void *, u_int); 47 48/* hold the address of the routine which is actually called if |
47 * the ramdomdev is loaded | 49 * the randomdev is loaded |
48 */ | 50 */ |
49static void (*reap_func)(struct timespec *, void *, u_int, u_int, u_int, u_int) = NULL; | 51static void (*reap_func)(u_int64_t, void *, u_int, u_int, u_int, u_int) = NULL; |
50static u_int (*read_func)(void *, u_int) = read_random_phony; 51 52/* Initialise the harvester at load time */ 53void | 52static u_int (*read_func)(void *, u_int) = read_random_phony; 53 54/* Initialise the harvester at load time */ 55void |
54random_init_harvester(void (*reaper)(struct timespec *, void *, u_int, u_int, u_int, u_int), u_int (*reader)(void *, u_int)) | 56random_init_harvester(void (*reaper)(u_int64_t, void *, u_int, u_int, u_int, u_int), u_int (*reader)(void *, u_int)) |
55{ 56 reap_func = reaper; 57 read_func = reader; 58} 59 60/* Deinitialise the harvester at unload time */ 61void 62random_deinit_harvester(void) --- 5 unchanged lines hidden (view full) --- 68/* Entropy harvesting routine. This is supposed to be fast; do 69 * not do anything slow in here! 70 * Implemented as in indirect call to allow non-inclusion of 71 * the entropy device. 72 */ 73void 74random_harvest(void *entropy, u_int count, u_int bits, u_int frac, u_int origin) 75{ | 57{ 58 reap_func = reaper; 59 read_func = reader; 60} 61 62/* Deinitialise the harvester at unload time */ 63void 64random_deinit_harvester(void) --- 5 unchanged lines hidden (view full) --- 70/* Entropy harvesting routine. This is supposed to be fast; do 71 * not do anything slow in here! 72 * Implemented as in indirect call to allow non-inclusion of 73 * the entropy device. 74 */ 75void 76random_harvest(void *entropy, u_int count, u_int bits, u_int frac, u_int origin) 77{ |
76 struct timespec timebuf; 77 78 if (reap_func) { 79 nanotime(&timebuf); 80 (*reap_func)(&timebuf, entropy, count, bits, frac, origin); 81 } | 78 if (reap_func) 79 (*reap_func)(get_cyclecount(), entropy, count, bits, frac, origin); |
82} 83 84/* Userland-visible version of read_random */ 85u_int 86read_random(void *buf, u_int count) 87{ 88 return (*read_func)(buf, count); 89} 90 91/* If the entropy device is not loaded, make a token effort to 92 * provide _some_ kind of randomness. This should only be used 93 * inside other RNG's, like arc4random(9). 94 */ 95static u_int 96read_random_phony(void *buf, u_int count) 97{ | 80} 81 82/* Userland-visible version of read_random */ 83u_int 84read_random(void *buf, u_int count) 85{ 86 return (*read_func)(buf, count); 87} 88 89/* If the entropy device is not loaded, make a token effort to 90 * provide _some_ kind of randomness. This should only be used 91 * inside other RNG's, like arc4random(9). 92 */ 93static u_int 94read_random_phony(void *buf, u_int count) 95{ |
98 struct timespec timebuf; | |
99 u_long randval; 100 int size, i; 101 static int initialised = 0; 102 103 /* Try to give random(9) a half decent initialisation | 96 u_long randval; 97 int size, i; 98 static int initialised = 0; 99 100 /* Try to give random(9) a half decent initialisation |
104 * DO not make the mistake of thinking this is secure!! | 101 * DO NOT make the mistake of thinking this is secure!! |
105 */ | 102 */ |
106 if (!initialised) { 107 nanotime(&timebuf); 108 srandom((u_long)(timebuf.tv_sec ^ timebuf.tv_nsec)); 109 } | 103 if (!initialised) 104 srandom((u_long)get_cyclecount()); |
110 111 /* Fill buf[] with random(9) output */ 112 for (i = 0; i < count; i+= sizeof(u_long)) { 113 randval = random(); 114 size = (count - i) < sizeof(u_long) ? (count - i) : sizeof(u_long); 115 memcpy(&((char *)buf)[i], &randval, size); 116 } 117 --- 17 unchanged lines hidden --- | 105 106 /* Fill buf[] with random(9) output */ 107 for (i = 0; i < count; i+= sizeof(u_long)) { 108 randval = random(); 109 size = (count - i) < sizeof(u_long) ? (count - i) : sizeof(u_long); 110 memcpy(&((char *)buf)[i], &randval, size); 111 } 112 --- 17 unchanged lines hidden --- |