Ignore:
Timestamp:
Feb 21, 2008 9:36:34 PM (17 years ago)
Author:
bennylp
Message:

Ticket #412: increased randomness of guid_simple.c to 192-bits, and seed random number generator in pjsua_core.c

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjlib/src/pj/guid_simple.c

    r1417 r1815  
    1818 */ 
    1919#include <pj/guid.h> 
     20#include <pj/assert.h> 
     21#include <pj/rand.h> 
    2022#include <pj/os.h> 
    21 #include <pj/rand.h> 
    2223#include <pj/string.h> 
    2324 
    24 PJ_DEF_DATA(const unsigned) PJ_GUID_STRING_LENGTH=20; 
     25PJ_DEF_DATA(const unsigned) PJ_GUID_STRING_LENGTH=32; 
     26 
     27static char guid_chars[64]; 
    2528 
    2629PJ_DEF(unsigned) pj_GUID_STRING_LENGTH() 
     
    2932} 
    3033 
    31 static void init_mac_address(unsigned char mac_addr[16]) 
     34static void init_guid_chars(void) 
    3235{ 
    33     unsigned long *ulval1 = (unsigned long*) &mac_addr[0]; 
    34     unsigned short *usval1 = (unsigned short*) &mac_addr[4]; 
     36    char *p = guid_chars; 
     37    unsigned i; 
    3538 
    36     *ulval1 = pj_rand(); 
    37     *usval1 = (unsigned short) pj_rand(); 
     39    for (i=0; i<10; ++i) 
     40        *p++ = '0'+i; 
     41 
     42    for (i=0; i<26; ++i) { 
     43        *p++ = 'a'+i; 
     44        *p++ = 'A'+i; 
     45    } 
     46 
     47    *p++ = '-'; 
     48    *p++ = '.'; 
    3849} 
    3950 
    4051PJ_DEF(pj_str_t*) pj_generate_unique_string(pj_str_t *str) 
    4152{ 
    42     static int guid_initialized; 
    43     static unsigned pid; 
    44     static char str_pid[32]; 
    45     static unsigned char mac_addr[32]; 
    46     static char str_mac_addr[32]; 
    47     static unsigned clock_seq; 
     53    char *p, *end; 
    4854 
    4955    PJ_CHECK_STACK(); 
    5056 
    51     if (guid_initialized == 0) { 
    52         pid = pj_getpid(); 
    53         init_mac_address(mac_addr); 
    54         clock_seq = 0; 
    55  
    56         sprintf(str_pid, "%04x", pid); 
    57         sprintf(str_mac_addr, "%02x%02x%02x%02x%02x%02x", 
    58             mac_addr[0], mac_addr[1], mac_addr[2], 
    59             mac_addr[3], mac_addr[4], mac_addr[5]); 
    60  
    61         guid_initialized = 1; 
     57    if (guid_chars[0] == '\0') { 
     58        pj_enter_critical_section(); 
     59        if (guid_chars[0] == '\0') { 
     60            init_guid_chars(); 
     61        } 
     62        pj_enter_critical_section(); 
    6263    } 
    6364 
    64     strcpy(str->ptr, str_pid); 
    65     sprintf(str->ptr+4, "%08x", clock_seq++); 
    66     pj_memcpy(str->ptr+12, str_mac_addr, 8); 
    67     str->slen = 20; 
     65    /* This would only work if PJ_GUID_STRING_LENGTH is multiple of 2 bytes */ 
     66    pj_assert(PJ_GUID_STRING_LENGTH % 2 == 0); 
    6867 
     68    for (p=str->ptr, end=p+PJ_GUID_STRING_LENGTH; p<end; ) { 
     69        /* Assumes rand() only has 16bit randomness */ 
     70        unsigned short val = pj_rand(); 
     71        *p++ = guid_chars[(val >> 8)   & 63]; 
     72        *p++ = guid_chars[(val & 0xFF) & 63]; 
     73    } 
     74 
     75    str->slen = PJ_GUID_STRING_LENGTH; 
    6976    return str; 
    7077} 
Note: See TracChangeset for help on using the changeset viewer.