r1046 - in trunk/code: client qcommon unix win32

DONOTREPLY at icculus.org DONOTREPLY at icculus.org
Fri Feb 16 18:50:38 EST 2007


Author: tjw
Date: 2007-02-16 18:50:37 -0500 (Fri, 16 Feb 2007)
New Revision: 1046

Modified:
   trunk/code/client/cl_main.c
   trunk/code/qcommon/common.c
   trunk/code/qcommon/md5.c
   trunk/code/qcommon/q_shared.h
   trunk/code/qcommon/qcommon.h
   trunk/code/unix/unix_shared.c
   trunk/code/win32/win_shared.c
Log:
* (bug 3019) use the operating system's random number generator if possible
  when generating the qkey file


Modified: trunk/code/client/cl_main.c
===================================================================
--- trunk/code/client/cl_main.c	2007-02-15 15:50:14 UTC (rev 1045)
+++ trunk/code/client/cl_main.c	2007-02-16 23:50:37 UTC (rev 1046)
@@ -2534,18 +2534,14 @@
 		return;
 	}
 	else {
-		int i;
-
 		if( len > 0 ) {
 			Com_Printf( "QKEY file size != %d, regenerating\n",
 				QKEY_SIZE );
 		}
 
-		srand(time(0));
-		for(i = 0; i < sizeof(buff) - 1; i++) {
-			buff[i] = (unsigned char)(rand() % 255);
-		}
-		buff[i] = 0;
+		Com_Printf( "QKEY building random string\n" );
+		Com_RandomBytes( buff, sizeof(buff) );
+
 		f = FS_SV_FOpenFileWrite( QKEY_FILE );
 		if( !f ) {
 			Com_Printf( "QKEY could not open %s for write\n",

Modified: trunk/code/qcommon/common.c
===================================================================
--- trunk/code/qcommon/common.c	2007-02-15 15:50:14 UTC (rev 1045)
+++ trunk/code/qcommon/common.c	2007-02-16 23:50:37 UTC (rev 1046)
@@ -3217,3 +3217,24 @@
 
 	Field_CompleteCommand( completionField->buffer, qtrue, qtrue );
 }
+
+/*
+==================
+Com_RandomBytes
+
+fills string array with len radom bytes, peferably from the OS randomizer
+==================
+*/
+void Com_RandomBytes( byte *string, int len )
+{
+	int i;
+
+	if( Sys_RandomBytes( string, len ) )
+		return;
+
+	Com_Printf( "Com_RandomBytes: using weak randomization\n" );
+	srand( time( 0 ) );
+	for( i = 0; i < len; i++ )
+		string[i] = (unsigned char)( rand() % 255 );
+}
+

Modified: trunk/code/qcommon/md5.c
===================================================================
--- trunk/code/qcommon/md5.c	2007-02-15 15:50:14 UTC (rev 1045)
+++ trunk/code/qcommon/md5.c	2007-02-16 23:50:37 UTC (rev 1046)
@@ -263,7 +263,7 @@
 	unsigned char digest[16] = {""}; 
 	fileHandle_t f;
 	MD5_CTX md5;
-	char buffer[2048];
+	byte buffer[2048];
 	int i;
 	int filelen = 0;
 	int r = 0;
@@ -296,7 +296,7 @@
 		if(r + total > length)
 			r = length - total;
 		total += r;
-		MD5Update(&md5 , (unsigned char *)buffer, r);
+		MD5Update(&md5 , buffer, r);
 		if(r < sizeof(buffer) || total >= length)
 			break;
 	}

Modified: trunk/code/qcommon/q_shared.h
===================================================================
--- trunk/code/qcommon/q_shared.h	2007-02-15 15:50:14 UTC (rev 1045)
+++ trunk/code/qcommon/q_shared.h	2007-02-16 23:50:37 UTC (rev 1046)
@@ -640,6 +640,8 @@
 char *Com_SkipTokens( char *s, int numTokens, char *sep );
 char *Com_SkipCharset( char *s, char *sep );
 
+void Com_RandomBytes( byte *string, int len );
+
 // mode parm for FS_FOpenFile
 typedef enum {
 	FS_READ,

Modified: trunk/code/qcommon/qcommon.h
===================================================================
--- trunk/code/qcommon/qcommon.h	2007-02-15 15:50:14 UTC (rev 1045)
+++ trunk/code/qcommon/qcommon.h	2007-02-16 23:50:37 UTC (rev 1046)
@@ -1001,6 +1001,8 @@
 
 void	Sys_SnapVector( float *v );
 
+qboolean Sys_RandomBytes( byte *string, int len );
+
 // the system console is shown when a dedicated server is running
 void	Sys_DisplaySystemConsole( qboolean show );
 

Modified: trunk/code/unix/unix_shared.c
===================================================================
--- trunk/code/unix/unix_shared.c	2007-02-15 15:50:14 UTC (rev 1045)
+++ trunk/code/unix/unix_shared.c	2007-02-16 23:50:37 UTC (rev 1046)
@@ -174,6 +174,22 @@
   return s; // bk001204 - duh
 }
 
+qboolean Sys_RandomBytes( byte *string, int len )
+{
+  FILE *fp;
+
+  fp = fopen( "/dev/urandom", "r" );
+  if( !fp )
+    return qfalse;
+
+  if( !fread( string, sizeof( byte ), len, fp ) ) {
+	fclose( fp );
+	return qfalse;
+  }
+  fclose( fp );
+  return qtrue; 
+}
+
 //============================================
 
 #define	MAX_FOUND_FILES	0x1000

Modified: trunk/code/win32/win_shared.c
===================================================================
--- trunk/code/win32/win_shared.c	2007-02-15 15:50:14 UTC (rev 1045)
+++ trunk/code/win32/win_shared.c	2007-02-16 23:50:37 UTC (rev 1046)
@@ -32,6 +32,7 @@
 #include <direct.h>
 #include <io.h>
 #include <conio.h>
+#include <wincrypt.h>
 
 /*
 ================
@@ -81,7 +82,25 @@
 }
 #endif
 
+qboolean Sys_RandomBytes( byte *string, int len )
+{
+	HCRYPTPROV  prov;
 
+	if( !CryptAcquireContext( &prov, NULL, NULL,
+		PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) )  {
+
+		return qfalse;
+	}
+
+	if( !CryptGenRandom( prov, len, (BYTE *)string ) )  {
+		CryptReleaseContext( prov, 0 );
+		return qfalse;
+	}
+	CryptReleaseContext( prov, 0 );
+	return qtrue;
+}
+
+
 /*
 **
 ** Disable all optimizations temporarily so this code works correctly!




More information about the quake3-commits mailing list