X-Git-Url: http://www.lcore.org/git/lcore.git/blobdiff_plain/fed3bacaa7e5da92ab4cf44995b5a2bc26a81faa..69598fec083b67c8567293c7b8a397b64175bd45:/lcorernd.pas?ds=inline diff --git a/lcorernd.pas b/lcorernd.pas index 6f1fe48..b7a3bf2 100644 --- a/lcorernd.pas +++ b/lcorernd.pas @@ -144,7 +144,17 @@ uses baseunix,unix,unixutil,sockets, {$endif} {$endif} + {$ifdef linux} + syscall, + {$endif} fastmd5,sysutils; +{$endif} + +const + wordsizeshift=2; + wordsize=1 shl wordsizeshift; + +{$ifndef nolcorernd} {$ifdef unix}{$include unixstuff.inc}{$endif} @@ -170,8 +180,6 @@ type hashtype=tmd5; const - wordsizeshift=2; - wordsize=1 shl wordsizeshift; //wordsize check commented out for d3 compatibility //{ $if (wordsize <> sizeof(wordtype))}'wordsizeshift must be setcorrectly'{ $ifend} hashsize=sizeof(hashtype); @@ -277,6 +285,17 @@ begin end; +{$ifdef linux} + {$ifdef i386} + const sys_getrandom = 355; + {$endif} + + {$ifdef cpux64} + const sys_getrandom = 318; + {$endif} +{$endif} + + function collect_seeding(var output;const bufsize:integer):integer; var f:file; @@ -293,24 +312,30 @@ begin if (bufsize < sizeof(l)) then exit; result := sizeof(l); - {/DEV/URANDOM} - a := 1; - assignfile(f,'/dev/urandom'); - filemode := 0; - {$i-}reset(f,1);{$i+} - a := ioresult; - if (a <> 0) then begin - assignfile(f,'/dev/random'); + a := -1; + {$ifdef linux} + a := do_syscall(sys_getrandom,tsysparam(@l.devrnd),sizeof(l.devrnd),0); + {$endif} + + if (a < sizeof(l.devrnd)) then begin + {if syscall misses or fails, fall back to /dev/urandom} + assignfile(f,'/dev/urandom'); + filemode := 0; {$i-}reset(f,1);{$i+} a := ioresult; - end; - if (a = 0) then begin - blockread(f,l.devrnd,sizeof(l.devrnd)); - closefile(f); - end else begin - {the OS we are on has no /dev/random or /dev/urandom, get a hash from /var/log/wtmp} - wtmphash; - move(wtmpcached,l.devrnd,sizeof(l.devrnd)); + if (a <> 0) then begin + assignfile(f,'/dev/random'); + {$i-}reset(f,1);{$i+} + a := ioresult; + end; + if (a = 0) then begin + blockread(f,l.devrnd,sizeof(l.devrnd)); + closefile(f); + end else begin + {the OS we are on has no /dev/random or /dev/urandom, get a hash from /var/log/wtmp} + wtmphash; + move(wtmpcached,l.devrnd,sizeof(l.devrnd)); + end; end; {get more randomness in case there's no /dev/random} rdtsc(@l.rdtscbuf);