Changeset 51bbec0 for lib/misc.c


Ignore:
Timestamp:
2008-05-21T10:27:28Z (16 years ago)
Author:
Jelmer Vernooij <jelmer@…>
Branches:
master
Children:
783e9b7
Parents:
a8d28d9
Message:

Revert move of random_bytes() to unix.c

File:
1 edited

Legend:

Unmodified
Added
Removed
  • lib/misc.c

    ra8d28d9 r51bbec0  
    392392}
    393393
     394/* A pretty reliable random number generator. Tries to use the /dev/random
     395   devices first, and falls back to the random number generator from libc
     396   when it fails. Opens randomizer devices with O_NONBLOCK to make sure a
     397   lack of entropy won't halt BitlBee. */
     398void random_bytes( unsigned char *buf, int count )
     399{
     400        static int use_dev = -1;
     401       
     402        /* Actually this probing code isn't really necessary, is it? */
     403        if( use_dev == -1 )
     404        {
     405                if( access( "/dev/random", R_OK ) == 0 || access( "/dev/urandom", R_OK ) == 0 )
     406                        use_dev = 1;
     407                else
     408                {
     409                        use_dev = 0;
     410                        srand( ( getpid() << 16 ) ^ time( NULL ) );
     411                }
     412        }
     413       
     414        if( use_dev )
     415        {
     416                int fd;
     417               
     418                /* At least on Linux, /dev/random can block if there's not
     419                   enough entropy. We really don't want that, so if it can't
     420                   give anything, use /dev/urandom instead. */
     421                if( ( fd = open( "/dev/random", O_RDONLY | O_NONBLOCK ) ) >= 0 )
     422                        if( read( fd, buf, count ) == count )
     423                        {
     424                                close( fd );
     425                                return;
     426                        }
     427                close( fd );
     428               
     429                /* urandom isn't supposed to block at all, but just to be
     430                   sure. If it blocks, we'll disable use_dev and use the libc
     431                   randomizer instead. */
     432                if( ( fd = open( "/dev/urandom", O_RDONLY | O_NONBLOCK ) ) >= 0 )
     433                        if( read( fd, buf, count ) == count )
     434                        {
     435                                close( fd );
     436                                return;
     437                        }
     438                close( fd );
     439               
     440                /* If /dev/random blocks once, we'll still try to use it
     441                   again next time. If /dev/urandom also fails for some
     442                   reason, stick with libc during this session. */
     443               
     444                use_dev = 0;
     445                srand( ( getpid() << 16 ) ^ time( NULL ) );
     446        }
     447       
     448        if( !use_dev )
     449        {
     450                int i;
     451               
     452                /* Possibly the LSB of rand() isn't very random on some
     453                   platforms. Seems okay on at least Linux and OSX though. */
     454                for( i = 0; i < count; i ++ )
     455                        buf[i] = rand() & 0xff;
     456        }
     457}
     458
    394459int is_bool( char *value )
    395460{
Note: See TracChangeset for help on using the changeset viewer.