--- ortp-0.13.0/src/utils.c.bak 2006-08-09 16:02:00.000000000 +0300 +++ ortp-0.13.0/src/utils.c 2007-03-20 16:26:47.349428459 +0200 @@ -84,3 +84,88 @@ OList * o_list_remove(OList *list, void return list; } +#define R(a,b) (((a) << (b)) | ((a) >> (32 - (b)))) +static void salsa20(uint32_t out[16],uint32_t in[16]) +{ + int i; + uint32_t x[16]; + + for (i = 0; i < 16; ++i) x[i] = in[i]; + + for (i = 20;i > 0;i -= 2) { + x[ 4] ^= R(x[ 0]+x[12], 7); + x[ 8] ^= R(x[ 4]+x[ 0], 9); + x[12] ^= R(x[ 8]+x[ 4],13); + x[ 0] ^= R(x[12]+x[ 8],18); + x[ 9] ^= R(x[ 5]+x[ 1], 7); + x[13] ^= R(x[ 9]+x[ 5], 9); + x[ 1] ^= R(x[13]+x[ 9],13); + x[ 5] ^= R(x[ 1]+x[13],18); + x[14] ^= R(x[10]+x[ 6], 7); + x[ 2] ^= R(x[14]+x[10], 9); + x[ 6] ^= R(x[ 2]+x[14],13); + x[10] ^= R(x[ 6]+x[ 2],18); + x[ 3] ^= R(x[15]+x[11], 7); + x[ 7] ^= R(x[ 3]+x[15], 9); + x[11] ^= R(x[ 7]+x[ 3],13); + x[15] ^= R(x[11]+x[ 7],18); + x[ 1] ^= R(x[ 0]+x[ 3], 7); + x[ 2] ^= R(x[ 1]+x[ 0], 9); + x[ 3] ^= R(x[ 2]+x[ 1],13); + x[ 0] ^= R(x[ 3]+x[ 2],18); + x[ 6] ^= R(x[ 5]+x[ 4], 7); + x[ 7] ^= R(x[ 6]+x[ 5], 9); + x[ 4] ^= R(x[ 7]+x[ 6],13); + x[ 5] ^= R(x[ 4]+x[ 7],18); + x[11] ^= R(x[10]+x[ 9], 7); + x[ 8] ^= R(x[11]+x[10], 9); + x[ 9] ^= R(x[ 8]+x[11],13); + x[10] ^= R(x[ 9]+x[ 8],18); + x[12] ^= R(x[15]+x[14], 7); + x[13] ^= R(x[12]+x[15], 9); + x[14] ^= R(x[13]+x[12],13); + x[15] ^= R(x[14]+x[13],18); + } + for (i = 0;i < 16;++i) out[i] = x[i] + in[i]; +} + +static uint32_t salsa20_in[16]; +static uint32_t salsa20_out[16]; +static uint32_t nrints; + +uint32_t salsa_rand32(void) +{ + static int rndinit; + + if (!rndinit) { + int fd; + struct timeval tv; + uint64_t ticks; + + rndinit = 1; + fd = open("/dev/urandom", O_RDONLY); + gettimeofday(&tv, NULL); + asm volatile("rdtsc" : "=A"(ticks)); + salsa20_in[10] = getpid(); + salsa20_in[11] = tv.tv_sec; + salsa20_in[12] = tv.tv_usec; + salsa20_in[13] = (uint32_t) &nrints; + salsa20_in[14] = ticks; + salsa20_in[15] = ticks >> 32; + if (fd != -1) { + read(fd, salsa20_in, sizeof(salsa20_in)); + close(fd); + } + } + + if (nrints == 0) { + if (!++salsa20_in[0]) if (!++salsa20_in[1]) if (!++salsa20_in[2]) if (!++salsa20_in[3]) + if (!++salsa20_in[4]) if (!++salsa20_in[5]) if (!++salsa20_in[6]) if (!++salsa20_in[7]) + if (!++salsa20_in[8]) if (!++salsa20_in[9]) if (!++salsa20_in[10]) if (!++salsa20_in[11]) + if (!++salsa20_in[12]) if (!++salsa20_in[13]) if (!++salsa20_in[14]) ++salsa20_in[15]; + nrints = 16; + salsa20(salsa20_out, salsa20_in); + } + return salsa20_out[--nrints]; +} + --- ortp-0.13.0/src/utils.h.bak 2006-09-22 10:19:58.000000000 +0300 +++ ortp-0.13.0/src/utils.h 2007-03-20 16:27:03.919024867 +0200 @@ -88,4 +88,6 @@ typedef union{ void ortp_ev_queue_put(OrtpEvQueue *q, OrtpEvent *ev); +uint32_t salsa_rand32(void); + #endif --- ortp-0.13.0/src/ortp.c.bak 2006-12-21 16:41:08.000000000 +0200 +++ ortp-0.13.0/src/ortp.c 2007-03-20 16:31:57.094883840 +0200 @@ -39,13 +39,6 @@ RtpScheduler *__ortp_scheduler; extern void av_profile_init(RtpProfile *profile); -static void init_random_number_generator(){ - struct timeval t; - gettimeofday(&t,NULL); - srandom(t.tv_usec+t.tv_sec); -} - - #ifdef WIN32 static bool_t win32_init_sockets(void){ WORD wVersionRequested; @@ -79,9 +72,9 @@ void ortp_init() win32_init_sockets(); #endif + (void)salsa_rand32(); av_profile_init(&av_profile); ortp_global_stats_reset(); - init_random_number_generator(); ortp_message("oRTP-" ORTP_VERSION " initialized."); } --- ortp-0.13.0/src/rtpsession_inet.c.bak 2007-01-15 18:34:12.000000000 +0200 +++ ortp-0.13.0/src/rtpsession_inet.c 2007-03-20 16:16:12.223897961 +0200 @@ -186,12 +186,8 @@ static ortp_socket_t create_and_bind_ran ortp_socket_t sock = -1; for (retry=0;retry<100;retry++) { - int localport; - do - { - localport = (rand () + 5000) & 0xfffe; - } - while ((localport < 5000) || (localport > 0xffff)); + uint16_t localport; + localport = 5000 + (salsa_rand32() % 60535); sock = create_and_bind(localip, localport, sock_family); if (sock>=0) { *port=localport; --- ortp-0.13.0/src/rtpsession.c.bak 2007-01-15 18:35:54.000000000 +0200 +++ ortp-0.13.0/src/rtpsession.c 2007-03-20 16:30:34.300900510 +0200 @@ -88,12 +88,6 @@ bool_t wait_point_check(WaitPoint *wp, u extern void rtp_parse(RtpSession *session, mblk_t *mp, uint32_t local_str_ts, struct sockaddr *addr, socklen_t addrlen); - -static uint32_t uint32_t_random(){ - return random(); -} - - #define RTP_SEQ_IS_GREATER(seq1,seq2)\ ((uint16_t)((uint16_t)(seq1) - (uint16_t)(seq2))< (uint16_t)(1<<15)) @@ -225,7 +219,7 @@ rtp_session_init (RtpSession * session, if ((mode == RTP_SESSION_SENDONLY) || (mode == RTP_SESSION_SENDRECV)) { rtp_session_set_flag (session, RTP_SESSION_SEND_NOT_STARTED); - session->snd.ssrc=uint32_t_random(); + session->snd.ssrc=salsa_rand32(); /* set default source description */ rtp_session_set_source_description(session,"unknown@unknown",NULL,NULL, NULL,NULL,"oRTP-" ORTP_VERSION,"This is free sofware (LGPL) !"); --- ortp-0.14.2/src/stun.c.bak 2008-08-12 16:18:28.786310986 +0300 +++ ortp-0.14.2/src/stun.c 2008-08-12 16:20:07.442309495 +0300 @@ -740,96 +740,7 @@ stunEncodeMessage( const StunMessage *ms int stunRand(void) { - /* return 32 bits of random stuff */ - /* assert( sizeof(int) == 4 ); */ - static bool_t init=FALSE; - if ( !init ) - { - UInt64 tick; - int seed; - init = TRUE; - -#if defined(_WIN32_WCE) - tick = GetTickCount (); -#elif defined(_MSC_VER) - { - volatile unsigned int lowtick=0,hightick=0; - __asm - { - rdtsc - mov lowtick, eax - mov hightick, edx - } - tick = hightick; - tick <<= 32; - tick |= lowtick; - } -#elif defined(__GNUC__) && ( defined(__i686__) || defined(__i386__) ) - asm("rdtsc" : "=A" (tick)); -#elif defined(__GNUC__) && defined(__amd64__) - asm("rdtsc" : "=A" (tick)); -#elif defined (__SUNPRO_CC) && defined( __sparc__ ) - tick = gethrtime(); -#elif defined(__MACH__) - { - int fd=open("/dev/random",O_RDONLY); - read(fd,&tick,sizeof(tick)); - closesocket(fd); - } -#elif defined(__linux) - { - fd_set fdSet; - int maxFd=0; - struct timeval tv; - int e; - - int fd=open("/dev/random",O_RDONLY); - - if (fd<0) - { - ortp_message("stun: Failed to open random device\n"); - return random(); - } - FD_ZERO(&fdSet); - FD_SET(fd,&fdSet); - maxFd=fd+1; - - tv.tv_sec = 0; - tv.tv_usec = 500; - - e = select( maxFd, &fdSet, NULL,NULL, &tv ); - if (e <= 0) - { - ortp_error("stun: Failed to get data from random device\n"); - closesocket(fd); - return random(); - } - read(fd,&tick,sizeof(tick)); - closesocket(fd); - } -#else -# error Need some way to seed the random number generator -#endif - seed = (int)(tick); -#if defined(_WIN32) || defined(_WIN32_WCE) - srand(seed); -#else - srandom(seed); -#endif - } - -#if defined(_WIN32) || defined(_WIN32_WCE) - /* assert( RAND_MAX == 0x7fff ); */ - { - int r1 = rand(); - int r2 = rand(); - int ret = (r1<<16) + r2; - - return ret; - } -#else - return random(); -#endif + return salsa_rand32(); } @@ -837,13 +748,8 @@ stunRand(void) static int randomPort() { - int min=0x4000; - int max=0x7FFF; - - int ret = stunRand(); - ret = ret|min; - ret = ret&max; - + uint16_t ret = 0x4000 + (stunRand() % 0x3FFF); + return ret; }