[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

PC: Using BIOS Wait function as a source of entropy?




In some older versions of the NOISE.SYS random driver I experimented 
with calling the BIOS Wait function which uses the CMOS timer to 
pause, using the "drift" between timing differences.  There appears 
to be some variation here, but I don't have enough documentation (and 
have yet to hack with the BIOS myself) to figure out what goes on 
exactly when one calls Int 15h/AH=86h, so I don't know if this is 
"real" clock drift of if the variation is caused by somehting else 
unsuitable for an RNG.

Still, it seems interesting.

Does anyone have decently detailed tech specs for this function?

Source is enclosed below for reference.  No copyrights on it.

  ---Rob <[email protected]>

----- Attachment begins here -----

{$F-}
const
  timer0 = $40;
  timercntl = $43;

  WaitInterval = 977;

function SampleTimerWord: Word; assembler;
asm
  mov	al, 0c2h
  out	timercntl, al {	Latch status and count for timer 0 }
  in	al, timer0  { Get status word }
  test	al, 2	    { Remember mode 2 v. mode 3 for later }
  mov	ch, al
  in	al, timer0  { Get count low byte }
  mov	ah, al
  in	al, timer0  { Get count high byte }
  xchg	ah, al
  jz	@GotSample  { If mode 2, skip this last bit...}
  add	ch, ch	    { Top bit of status	byte into CF}
  rcr	ax, 1	    { Shift data down and accumulate}
  @GotSample:
end;

function Sample: Integer; assembler;
asm
{ From Ralph Brown's Interrupt List:
--------B-1586-------------------------------
INT 15 - BIOS - WAIT (AT,PS)
 AH = 86h
 CX:DX = interval in microseconds
Return: CF clear if successful (wait interval elapsed)
 CF set on error or AH=83h wait already in progress
     AH = status (see #0390)
Note:	the resolution of the wait period is 977 microseconds on most
systems
   because most BIOSes use the 1/1024 second fast interrupt from the
   AT real-time clock chip which is available on INT 70
SeeAlso: AH=41h,AH=83h,INT 1A/AX=FF01h,INT 70
}
        call SampleTimerWord
        push ax
        xor  cx, cx
        mov  dx, WaitInterval
        mov  ah, 86h
        int  15h
        jnc  @NoError        { does this affect timings much? }
        xor  ax, ax
        jmp  @Abort
@NoError:
         call SampleTimerWord
         pop  bx
         sub  ax, bx
@Abort:
end;

begin
{ Note: repeated/rapid calls to Sample() crashes the system or causes
BOUND
  interrupts (which triggers the Print Screen function on PCs). }
  WriteLn(Sample:6);
end.
Rob. 

---
Send a blank message with the subject "send pgp-key" (not in
quotes) to <[email protected]> for a copy of my PGP key.