[cryptography] Intel RNG

Peter Gutmann pgut001 at cs.auckland.ac.nz
Tue Jun 28 04:52:29 EDT 2011


In case this is useful to anyone, here's the Windows code to use rdrand, to
complement the gcc version for Unix systems.  It'll also be present in the
next release of the cryptlib RNG code, available under a GPL, LGPL, or BSD
license, depending on which you prefer.

#if defined( _MSC_VER )
  #define rdrand_eax	__asm _emit 0x0F __asm _emit 0xC7 __asm _emit 0xF0
#endif /* VC++ */
#if defined __BORLANDC__
  #define rdrand_eax	} __emit__( 0x0F, 0xC7, 0xF0 ); __asm {
#endif /* BC++ */

	{
	unsigned long buffer[ 8 + 8 ];
	int byteCount = 0;

	__asm {
		xor eax, eax		/* Tell VC++ that EAX will be trashed */
		xor ecx, ecx
	trngLoop:
		rdrand_eax
		jnc trngExit		/* TRNG result bad, exit with byteCount = 0 */
		mov [buffer+ecx], eax
		add ecx, 4
		cmp ecx, 32			/* Fill 32 bytes worth */
		jl trngLoop
		mov [byteCount], ecx
	trngExit:
		}
	if( byteCount > 0 )
		{
		/* buffer[ 0 ... byteCount ] contains random bytes */
		}
	}

This has been verified under XP, Vista, and Win7 using the Intel Software
Development Emulator.

Peter.



More information about the cryptography mailing list