Annotation of embedaddon/hping2/random.c, revision 1.1.1.1

1.1       misho       1: /* rc4-based pseudo-random number generator for hping.
                      2:  * Copyright (C) 2003 Salvatore Sanfilippo
                      3:  * This software is released under the GPL license
                      4:  * All rights reserved */
                      5: 
                      6: #include <sys/types.h>
                      7: #include <sys/stat.h>
                      8: #include <fcntl.h>
                      9: #include <unistd.h>
                     10: #include <sys/time.h>
                     11: #include <time.h>
                     12: 
                     13: u_int32_t hp_rand(void);
                     14: 
                     15: /* The rc4 sbox */
                     16: static unsigned char rc4_sbox[256];
                     17: /* This flags is used to initialize the sbox the first time,
                     18:  * without an explicit intialization step outside this file. */
                     19: static int rc4_seedflag = 0;
                     20: 
                     21: /* Initialize the sbox with pseudo random data */
                     22: static void hp_rand_init(void)
                     23: {
                     24:        int i, fd;
                     25: 
                     26:        /* Strong sbox initialization */
                     27:        fd = open("/dev/urandom", O_RDONLY);
                     28:        if (fd != -1) {
                     29:                read(fd, rc4_sbox, 256);
                     30:                close(fd);
                     31:        }
                     32:        /* Weaker sbox initialization */
                     33:        for (i = 0; i < 256; i++) {
                     34:                struct timeval tv;
                     35:                gettimeofday(&tv, NULL);
                     36:                if (i&1)
                     37:                        rc4_sbox[i] ^= (tv.tv_usec >> (i&0xF)) & 0xFF;
                     38:                else
                     39:                        rc4_sbox[i] ^= (tv.tv_sec >> (i&0xF)) & 0xFF;
                     40:        }
                     41:        rc4_seedflag = 1;
                     42: }
                     43: 
                     44: #if 0
                     45: /* Re-seed the generator with user-provided bytes. Not used for now. */
                     46: static void hp_rand_seed(void *seed, size_t len)
                     47: {
                     48:        int i;
                     49: 
                     50:        if (len > 256) len = 256;
                     51:        memcpy(rc4_sbox, seed, len);
                     52:        /* discard the first 256 bytes of output after the reseed */
                     53:        for (i = 0; i < 32; i++)
                     54:                (void) hp_rand();
                     55: }
                     56: #endif
                     57: 
                     58: /* Generates a 32bit random number using an RC4-like algorithm */
                     59: u_int32_t hp_rand(void)
                     60: {
                     61:        u_int32_t r = 0;
                     62:        unsigned char *rc = (unsigned char*) &r;
                     63:        static unsigned int i = 0, j = 0;
                     64:        unsigned int si, sj, x;
                     65: 
                     66:        /* initialization, only needed the first time */
                     67:        if (!rc4_seedflag)
                     68:                hp_rand_init();
                     69:        /* generates 4 bytes of pseudo-random data using RC4 */
                     70:        for (x = 0; x < 4; x++) {
                     71:                i = (i+1) & 0xff;
                     72:                si = rc4_sbox[i];
                     73:                j = (j + si) & 0xff;
                     74:                sj = rc4_sbox[j];
                     75:                rc4_sbox[i] = sj;
                     76:                rc4_sbox[j] = si;
                     77:                *rc++ = rc4_sbox[(si+sj)&0xff];
                     78:        }
                     79:        return r;
                     80: }
                     81: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>