Ignore:
Timestamp:
Jan 24, 2008 3:27:30 PM (15 years ago)
Author:
bennylp
Message:

More ticket #61: SRTP will try to use /dev/urandom as RNG if fcntl.h and unistd.h is present. If it fails, it will fallback to using rand()

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/third_party/srtp/crypto/rng/rand_source.c

    r1730 r1738  
    4545#include "srtp_config.h" 
    4646 
    47 #ifdef DEV_URANDOM 
     47#if defined(DEV_URANDOM) || defined(PJ_DEV_URANDOM) 
    4848# include <fcntl.h>          /* for open()  */ 
    4949# include <unistd.h>         /* for close() */ 
     
    8888  if (dev_random_fdes < 0) 
    8989    return err_status_init_fail; 
     90#elif defined(PJ_DEV_URANDOM) 
     91  /* open random source for reading */ 
     92  dev_random_fdes = open(PJ_DEV_URANDOM, O_RDONLY); 
     93  if (dev_random_fdes < 0) { 
     94    err_report(3,"Ugh: /dev/urandom not present, using rand() instead"); 
     95    return err_status_ok;  /* it's ok, it'll fallback to using rand() */ 
     96  } 
    9097#elif (_MSC_VER >= 1400) 
    9198  dev_random_fdes = RAND_SOURCE_READY; 
     
    124131  } 
    125132#else 
     133  uint8_t *dst = (uint8_t *)dest; 
     134 
     135  /* First try with /dev/urandom, if it's opened */ 
     136  if (dev_random_fdes >= 0) { 
     137    if (read(dev_random_fdes, dest, len) == len) 
     138        return err_status_ok;   /* success */ 
     139  } 
     140 
    126141  /* Generic C-library (rand()) version */ 
    127142  /* This is a random source of last resort */ 
    128   uint8_t *dst = (uint8_t *)dest; 
    129143  while (len) 
    130144  { 
     
    142156err_status_t 
    143157rand_source_deinit(void) { 
     158#ifndef PJ_DEV_URANDOM 
    144159  if (dev_random_fdes < 0) 
    145160    return err_status_dealloc_fail;  /* well, we haven't really failed, * 
    146161                                      * but there is something wrong    */ 
    147 #ifdef DEV_URANDOM 
    148   close(dev_random_fdes);   
    149162#endif 
     163 
     164  if (dev_random_fdes >= 0) 
     165    close(dev_random_fdes);   
     166 
    150167  dev_random_fdes = RAND_SOURCE_NOT_READY; 
    151168   
    152169  return err_status_ok;   
    153170} 
     171 
Note: See TracChangeset for help on using the changeset viewer.