X-Git-Url: http://www.lcore.org/git/lcore.git/blobdiff_plain/2c3a30778bb5a281c10736470058b5a63575a516..803cf6668d9e89c244e5fbc7f996a460470338dd:/lcorernd.pas?ds=sidebyside diff --git a/lcorernd.pas b/lcorernd.pas index b435305..aa3d253 100644 --- a/lcorernd.pas +++ b/lcorernd.pas @@ -144,6 +144,9 @@ uses baseunix,unix,unixutil,sockets, {$endif} {$endif} + {$ifdef linux} + syscall, + {$endif} fastmd5,sysutils; {$endif} @@ -282,6 +285,24 @@ begin end; +{$ifdef bsd} +function arc4random: cardinal; cdecl; external 'c' name 'arc4random'; +procedure arc4random_buf(buf: Pointer; nbytes: SizeUInt); cdecl; external 'c' name 'arc4random_buf'; +function arc4random_uniform(upper_bound: cardinal): cardinal; cdecl; external 'c' name 'arc4random_uniform'; +{$endif} + + +{$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; @@ -298,24 +319,35 @@ 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} + + {$ifdef bsd} + a := sizeof(l.devrnd); + arc4random_buf(@l.devrnd, a); + {$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);