--- libzrtpcpp-0.9.0/src/libzrtpcpp/ZIDFile.h.bak 2006-09-10 21:40:24.000000000 +0300 +++ libzrtpcpp-0.9.0/src/libzrtpcpp/ZIDFile.h 2007-08-09 23:41:15.525789913 +0300 @@ -134,6 +134,13 @@ public: * Pointer to the ZID */ const unsigned char* getZid() { return associatedZid; }; + + /** + * Get random uint32_t + * @return + * uint32_t + */ + uint32_t get_random32 (void); }; #endif --- libzrtpcpp-0.9.0/src/ZIDFile.cxx.bak 2006-10-01 17:43:43.000000000 +0300 +++ libzrtpcpp-0.9.0/src/ZIDFile.cxx 2007-08-09 23:55:36.417391033 +0300 @@ -23,6 +23,9 @@ #include #include +#include +#include +#include #include @@ -39,6 +42,86 @@ ZIDFile* ZIDFile::getInstance() { return instance; } +#define R(a,b) (((a) << (b)) | ((a) >> (32 - (b)))) + +void salsa20 (uint32_t * __out, uint32_t *__in) +{ + int i; + uint32_t x[16]; + uint32_t *in = __in; + uint32_t *out = __out; + + 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]; +} + +uint32_t ZIDFile::get_random32 (void) +{ + static uint32_t salsa20_out[16]; + static uint32_t salsa20_in[16]; + static int nrints; + static int rndinit; + FILE *fd; + + if (!rndinit) { + rndinit = 1; + salsa20_in[0] = time(NULL); + salsa20_in[1] = getpid(); + fd = fopen("/dev/urandom", "r"); + if (fd) { + setvbuf(fd, NULL , _IOFBF , sizeof(salsa20_in)); + fread(&salsa20_in, sizeof(salsa20_in), 1, fd); + fclose(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]; + salsa20(salsa20_out, salsa20_in); + nrints = 16; + } + + return salsa20_out[--nrints]; +} + int ZIDFile::open(char *name) { zidrecord_t rec; unsigned int* ip; @@ -54,15 +137,15 @@ int ZIDFile::open(char *name) { if (zidFile != NULL) { ip = (unsigned int*)associatedZid; memset(&rec, 0, sizeof(zidrecord_t)); - srandom(time(NULL)); - *ip++ = random(); - *ip++ = random(); - *ip = random(); + *ip++ = ZIDFile::get_random32(); + *ip++ = ZIDFile::get_random32(); + *ip = ZIDFile::get_random32(); memcpy(rec.identifier, associatedZid, IDENTIFIER_LEN); - fseek(zidFile, 0L, SEEK_SET); + if (fseek(zidFile, 0L, SEEK_SET) != 0) return -1; rec.ownZid = 1; - fwrite(&rec, sizeof(zidrecord_t), 1, zidFile); - fflush(zidFile); + if (fwrite(&rec, sizeof(zidrecord_t), 1, zidFile) != 1) return -1; + if (fflush(zidFile) == EOF) return -1; + if (fsync(fileno(zidFile)) == -1) return -1; } } else {