--- libzrtp-0.4.6/src/zrtp_rng.c.bak 2007-11-08 14:50:14.000000000 +0200 +++ libzrtp-0.4.6/src/zrtp_rng.c 2007-11-23 23:56:06.924247184 +0200 @@ -86,41 +86,38 @@ int zrtp_add_system_state(zrtp_global_ct int zrtp_add_system_state(zrtp_global_ctx_t* zrtp_global, MD_CTX *ctx) { uint8_t buffer[64]; - size_t bytes_read = 0; - static size_t length= sizeof(buffer); - FILE *fp = NULL; - + size_t bytes_read; + FILE *fp; + fp = fopen("/dev/urandom", "rb"); if ( !fp ) { - zrtp_print_log(ZRTP_LOG_ERROR, "zrtp_randstr: can't get access to /dev/urandom - using /dev/random.\n"); + zrtp_print_log(ZRTP_LOG_ERROR, "zrtp_randstr: can't get access /dev/urandom - trying /dev/random.\n"); fp = fopen("/dev/random", "rb"); } if ( fp ) - { - int number_of_retries = 1024; - - while ((bytes_read < length) && (number_of_retries-- > 0)) - { - bytes_read += fread(buffer+bytes_read, 1, length-bytes_read, fp); - } + { + setbuf(fp, NULL); /* Otherwise fread() tries to read() 4096 bytes */ + bytes_read = fread(buffer, 1, sizeof(buffer), fp); if ( 0 != fclose(fp) ) - zrtp_print_log(ZRTP_LOG_FATAL, "zrtp_randstr: Unable to cloas /dev/random\n"); + zrtp_print_log(ZRTP_LOG_FATAL, "zrtp_randstr: Unable to close /dev/random\n"); } else { zrtp_print_log(ZRTP_LOG_FATAL, "zrtp_randstr: Can't open /dev/random\n"); + return -1; } - if ( bytes_read < length ) + if ( bytes_read < sizeof(buffer)) { - zrtp_print_log(ZRTP_LOG_FATAL, "zrtp_randstr: Can't read random string!. Ņurrent session must be closed.\n"); + zrtp_print_log(ZRTP_LOG_FATAL, "zrtp_randstr: Can't read random string! Current session must be closed.\n"); return -1; } - MD_Update(ctx, buffer, length); + MD_Update(ctx, buffer, sizeof(buffer)); + zrtp_memset(buffer, 0, sizeof(buffer)); return 0; }