Ignore:
Timestamp:
Mar 15, 2016 3:57:39 AM (7 years ago)
Author:
nanang
Message:

Close #1847: Upgraded libsrtp version to 1.5.4 and added support for AES-CM-256 crypto.

File:
1 edited

Legend:

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

    r1807 r5261  
    4343 */ 
    4444 
    45 #include "srtp_config.h" 
     45#include "config.h" 
    4646 
    47 #if defined(DEV_URANDOM) || defined(PJ_DEV_URANDOM) 
     47#ifdef DEV_URANDOM 
    4848# include <fcntl.h>          /* for open()  */ 
    4949# include <unistd.h>         /* for close() */ 
    50 #elif (_MSC_VER >= 1400) 
    51 #define _CRT_RAND_S 
     50#elif defined(HAVE_RAND_S) 
     51# define _CRT_RAND_S 
    5252# include <stdlib.h>          
    53 # include <stdio.h> 
    5453#else 
    5554# include <stdio.h> 
     
    8887  if (dev_random_fdes < 0) 
    8988    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   } 
    97 #elif (_MSC_VER >= 1400) 
     89#elif defined(HAVE_RAND_S) 
    9890  dev_random_fdes = RAND_SOURCE_READY; 
    9991#else 
    10092  /* no random source available; let the user know */ 
    101   err_report(err_level_info, "WARNING: no real random source present!\n"); 
     93  fprintf(stderr, "WARNING: no real random source present!\n"); 
    10294  dev_random_fdes = RAND_SOURCE_READY; 
    10395#endif 
     
    114106   */ 
    115107#ifdef DEV_URANDOM 
    116   if (read(dev_random_fdes, dest, len) != len) 
    117     return err_status_fail; 
    118 #elif 0 && (_MSC_VER >= 1400) /* disabled rand_s, causing assertion 'rand_s not supported' in vs8 */ 
    119   unsigned int *dst = dest; 
     108  uint8_t *dst = (uint8_t *)dest; 
    120109  while (len) 
    121110  { 
    122       unsigned int val = 0; 
    123           errno_t err = rand_s(&val); 
    124       if (err != 0) 
    125               { 
    126               return err_status_fail; 
    127           } 
     111    ssize_t num_read = read(dev_random_fdes, dst, len); 
     112    if (num_read <= 0 || num_read > len) 
     113      return err_status_fail; 
     114    len -= num_read; 
     115    dst += num_read; 
     116  } 
     117#elif defined(HAVE_RAND_S) 
     118  uint8_t *dst = (uint8_t *)dest; 
     119  while (len) 
     120  { 
     121    unsigned int val; 
     122    errno_t err = rand_s(&val); 
     123 
     124    if (err != 0) 
     125      return err_status_fail; 
    128126   
    129       *dst++ = val; 
    130           len--; 
     127    *dst++ = val & 0xff; 
     128    len--; 
    131129  } 
    132130#else 
    133   uint8_t *dst = (uint8_t *)dest; 
    134  
    135 #ifdef PJ_DEV_URANDOM 
    136   /* First try with /dev/urandom, if it's opened */ 
    137   if (dev_random_fdes >= 0) { 
    138     if (read(dev_random_fdes, dest, len) == len) 
    139         return err_status_ok;   /* success */ 
    140   } 
    141 #endif 
    142  
    143131  /* Generic C-library (rand()) version */ 
    144132  /* This is a random source of last resort */ 
     133  uint8_t *dst = (uint8_t *)dest; 
    145134  while (len) 
    146135  { 
     
    158147err_status_t 
    159148rand_source_deinit(void) { 
    160 #ifndef PJ_DEV_URANDOM 
    161149  if (dev_random_fdes < 0) 
    162150    return err_status_dealloc_fail;  /* well, we haven't really failed, * 
    163151                                      * but there is something wrong    */ 
     152#ifdef DEV_URANDOM 
     153  close(dev_random_fdes);   
    164154#endif 
    165  
    166 #if defined(DEV_URANDOM) || defined(PJ_DEV_URANDOM) 
    167   if (dev_random_fdes >= 0) 
    168     close(dev_random_fdes);   
    169  
    170155  dev_random_fdes = RAND_SOURCE_NOT_READY; 
    171 #endif 
    172156   
    173157  return err_status_ok;   
    174158} 
    175  
Note: See TracChangeset for help on using the changeset viewer.