True hard random generator lazarus / 64-FPC development ---------------------------------------------------------------------- sam707 · 2017-12-14 03:11 As i read wiki I understood FPC uses Mersenne Twister algo to get pseudo random numbers. While I was looking for alternate algorithms, I found out that some processors have an inbuilt hardware (true) random number generator. My question is simple : Will FPC use this feature in a near future, or should I make ASMs functions and procedure to use it? namely RdSeed and RdRand ASM instructions described on following link https://en.wikipedia.org/wiki/RdRand these ASM instructions are quiet slow compared to Mersenne Twister, as explained in the above article, BUT, they exist to give TRUE random, giving high level cryptographic compliance. So, in many projects, it should be USEFUL ---------------------------------------------------------------------- sam707 · 2017-12-14 03:18 making asm proc/func is not big deal tho, I'm still investigating algoritms, even will try to mix some ---------------------------------------------------------------------- Eugene Loza · 2017-12-14 03:32 You may try this one: http://wiki.freepascal.org/Dev_random (Linux- only) ---------------------------------------------------------------------- Thaddy · 2017-12-14 07:16 I will add or add to/modify the following three randoms to the wiki, I already have such code including code that uses rdseed/rdrand or hwrng to seed the standard random: /dev/random, /dev/urandom  that take entropy from several sources on nixes. Including the use of hwrng if available. There is already an entry. https://en.wikipedia.org/wiki//dev/random Microsoft's cryptgenrandom that take entropy from several sources on nixes. Including the use of hwrng if available. https://en.wikipedia.org/wiki/CryptGenRandom hwrng if availabale on a system (e.g. Raspberry Pi, modern intel. This is true hardware random that takes entropy from physics behavior on the chip e.g. https://sites.google.com/site/astudyofentropy/project- definition/raspberry-pi-internal-hardware-random-number-generator I have an article ready but my health failed badly and I needed to add a few more examples. Note the standard random functions will always be a PRNG and possible always the mersenne twister. This is because its purpose also lies in statstics and must be reproducable: Given the same seed it *must* produce the same random series. True random is available through other means already and is not repeatable. Its purpose lies e.g. in cryptography. I already added a number of alternative PRNG's to the wiki for e.g. Delphi compatible random and e.g. superfast prng's for gaming etc. I also provided a patch in trunk for a generic version of RandomFrom Further note the the OS, both Windows and nixes (IIRC except BSD), already use hwrng/rdrand/rdseed as a source of entropy for resp. CryptGenRandom and /dev/random /dev/urandom so to some extend a separate function is not required. Also note hwrnd/rdrand/rdseed are slow, hence if some speed is required it is best to use them just to seed a quality PRNG like the mersenne twister or KISS. A simple example for linux: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program hwrndtest;{takes a hwrng value if present and seeds Randseed with it.}{$ifdef fpc}{$mode delphi}{$H+}{$I-}{$endif}uses sysutils,classes;varL:TFilestream;begin  if fileexists('/dev/hwrng') then   begin    L:= TFilestream.create('/dev/hwrng', fmOpenread);    try      RandSeed:=L.ReadDword;    finally      writeln(RandSeed);    L.Free;    end;   end else      writeln('no hardware rng present');end. ---------------------------------------------------------------------- sam707 · 2017-12-14 08:32 Big  thank you @thaddy, your answer was very accurate this time. After reading plenty of stuffs, I found that article of interest for my purpose and I'm going to give it a try (because i'ma crossplatfarm moron ya kno  ) https://en.wikipedia.org/wiki/Blum_Blum_Shub ---------------------------------------------------------------------- sam707 · 2017-12-14 08:36 I read on internet that blumblumshub (niggurath??) is cryptographically secure. OMG it reminds Lovecraft foolish writer LOL ... shub niggurath ---------------------------------------------------------------------- sam707 · 2017-12-14 08:40 https://github.com/foolean/blum-blum-shub should be easy to transpose in pascal ---------------------------------------------------------------------- Thaddy · 2017-12-14 08:55 Yes I already have it. Note it is only cryptographically secure because it uses the systems random functionality as seed. ---------------------------------------------------------------------- bigeno · 2017-12-14 09:09 if someone need here some quick examples for CryptGenRandom and RdRand for windows. based on https://github.com/alexpmorris/WebSocketUpgrade/blob/master/c random.pas Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Windows, Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,dynlibs; type  fWCCryptAcquireContextA = Function (phProv: Pointer; pszContainer: LPCSTR; pszProvider: LPCSTR; dwProvType: DWORD; dwFlags: DWORD): BOOL; stdcall;  fWCCryptReleaseContext = Function (hProv: Pointer; dwFlags: DWORD): BOOL; stdcall;  fWCCryptGenRandom = Function (hProv: ULONG; dwLen: DWORD; pbBuffer: PBYTE): BOOL; stdcall; type  TForm1 = class(TForm)    WinCryptGenRandom: TButton;    IntelDrng: TButton;    procedure WinCryptGenRandomClick(Sender: TObject);    procedure IntelDrngClick(Sender: TObject);  private  public  end; var  Form1: TForm1; implementation {$R *.lfm}function TryRdRand(out Value: Cardinal): Boolean;begin  {$ASMMODE intel}  asm  db   $0f  db   $c7  db   $f1  jc   @success  xor  eax,eax  ret  @success:  mov  [eax],ecx  mov  eax,1  end;end; procedure TForm1.WinCryptGenRandomClick(Sender: TObject);var  hProvider: ULONG = 0;  WinCryptHndl: THandle = 0;  WCCryptAcquireContextA: fWCCryptAcquireContextA;  WCCryptReleaseContext: fWCCryptReleaseContext;  WCCryptGenRandom: fWCCryptGenRandom;  res: boolean;  dwLength: DWORD;  bBuffer: array[0..7] of byte;  outr: string;  i: integer;begin  outr:='';  WinCryptHndl := LoadLibrary('advapi32.dll');  if WinCryptHndl = 0 then exit;  WCCryptAcquireContextA := fWCCryptAcquireContextA(GetProcAddres s(WinCryptHndl,'CryptAcquireContextA'));  WCCryptReleaseContext := fWC CryptReleaseContext(GetProcAddress(WinCryptHndl,'CryptReleaseContext') );  WCCryptGenRandom := fWCCryptGenRandom(GetProcAddress(WinCryptHndl,'CryptGenRandom'));  if WCCryptAcquireContextA = nil then exit;  if WCCryptReleaseContext = nil  then exit;  if WCCryptGenRandom = nil       then exit;   WCCryptAcquireContextA(@hProvider, Nil, Nil, 1, $F0000000);  dwLength:=8;  res:=WCCryptGenRandom(hProvider, dwLength, @bBuffer[0]);  if res then begin     for i:=0 to 7 do        outr:=outr+' '+IntToStr(bBuffer[i]);     showmessage(outr);  end;  WCCryptReleaseContext(@hProvider, 0);end; procedure TForm1.IntelDrngClick(Sender: TObject);var  rnd: Cardinal;begin  if TryRdRand(rnd) then    showmessage(inttostr(rnd));end;end. ---------------------------------------------------------------------- sam707 · 2017-12-18 23:03 source code attached  upon PCG algo I translated quickly from C http://www.pcg-random.org Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function QuickRand(IncSeed: QWord = 1): DWord;var  oldstate: QWord;  tmp0, tmp1: DWord;begin  oldstate := RndRec.state;  RndRec.Inc := IncSeed;  RndRec.state := oldstate * 6364....... download attached zip, compile the demo app, ENJOY ---------------------------------------------------------------------- sam707 · 2017-12-18 23:09 SO! for my encryption problem I guess I'm going to use a table of 1024 bytes mersenne twister generated by standard SLOW fpc random gen, and a double shuffle by selecting indices inside the renewable table, with panquakes FAST (pgcRandom that i'm going to modify a little bit to my taste). should be cryptosafe enough for one or two decades Eventually change mersennelly the SeedIncrement for PCG ---------------------------------------------------------------------- sam707 · 2017-12-18 23:21 I guess I will only need to transmit Seed and table  of Seeds' increments for the receiver to retreive passkeys  I can also encrypt these data by another way ---------------------------------------------------------------------- sam707 · 2017-12-22 10:00 a littel noisy patch to apply to my simple source code in the zip above (adjustable pseudoNoise, see comments) well, this should be cryptosafe  bytes generator (panquakes twoaster*mersenne²/(1/sqrt(-sausage)))=myShakedAss happy for 2 decades (until quantum processors rise from kangaroos' pockets) hehehe Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function QuickRand(IncSeed: QWord = 1): DWord;var  oldstate: QWord;  tmp0, tmp1: DWord;begin  oldstate := RndRec.state;  RndRec.Inc := IncSeed;  RndRec.state := oldstate * 6364136223846793005 + (RndRec.Inc or 1);  tmp0 := ((oldstate shr 18) xor oldstate) shr 27;  tmp1 := oldstate shr 59;  Result := (tmp0 shr tmp1) or (tmp0 shl ((-tmp1) and 31));end; { TForm1 } (* pseudoNoisy sausage follows *) procedure TForm1.Button1Click(Sender: TObject);var  r: DWord;  s: cardinal;begin{ following if..then breaks the 'period' by random jumps ahead }  if random(1023)< 321 then { adjustable }    s := 1  else    s := succ(random(14));  if random(255) > (15+random(17)) then { adjustable }    r := QuickRand  else    r := QuickRand(s);  Edit1.Text := Format('%U', [r]);end; initialization  Randomize;  RndRec.state := RandSeed + 6364136223846793005;{ RandSeed is the value to pass to client for that  last to retrieve same panquakes twoaster random values } end. ---------------------------------------------------------------------- sam707 · 2017-12-22 13:12 where 6364136223846793005 comes from? https://en.wikipedia.org/wiki/Linear_congruential_generator see table of parameters commonly used lines - MMIX by Donald Knuth - Newlib, Musl uncongrulethal load... sumthing  it's not that magic LOL achording to wiki, Seed Increment can be from 1 to 1442695040888963407 (even numbers 1..3...5...7...in PCG algo. ok im tired and lazy lol) make your own choices ---------------------------------------------------------------------- sam707 · 2017-12-22 13:18 i'm done with random  my 12.629 IQ hurts as I don't want to work on the chaos equation  at the moment im not drunk enough, I ain't a Edward Lorenz fan so far take care you all, have fun!!! https://en.wikipedia.org/wiki/Lorenz_system https://en.wikipedia.org/wiki/Chaos_theory ---------------------------------------------------------------------- sam707 · 2017-12-22 13:31 send me an atmo-spheric noisymodulolorenzbeer, and we'll see ---------------------------------------------------------------------- Thaddy · 2017-12-22 16:20 @sam707 That's not random. Because it relies on Randomize....... It is a PRNG, a peudo ramdom generator and it has many flaws: there is no entropy, but randomize..... (That s the really moron part (intended, moderators)) And Randomize is not a particular good Random seed.... Do the same tests as I did and published on the wiki. Ignorant. Test it for yourself: leave out randomize and you have a repeatable PRNG. Checkout the standard code for randomize... I am busy creating a real randomize...( not accepted yet, but close) I hope that everyone understands that it is not cryptographically secure in any way. This is not a case of woof, but bite. (without too much tissue ripped off if you don't move, that is, but only with my Jack Russel hybrids) Now change subject: Merry Christmas everybody and every one.... I will probably have a beer.. (more likely wine from NZ) ---------------------------------------------------------------------- sam707 · 2017-12-22 20:23 @thaddy I WAS EXPECTING with happiness such a crappy reaction from you, tiny whoopy grump  YOU CAN NOT RESIST HAHAHAHA go ahead make a random gen with software (ROFL) while it exists on new hardware processors, waste your time as you are so damn used to, while I found solutions that ARE WELL KNOWN to be cryptosecure (cf. blumblum). I've no auction to earn in the fact you can not even read  as always (at least often), you answer at first in a (minimalist) appropriate way, afterwhat, as a constant, your back to bottles and necks etcaetera blum blum shub is cryptosecure, mersenne twister is not, PCG is "hard to guess" (see table of algos on the wsite) IKR (as many) they are PRNG with uncongrulethal mechanisms , I also wrote it OMG ask fckdup Santa for glasses! c'mon! getting old is a wrecking, at least for grumpboats HEHEHE ---------------------------------------------------------------------- sam707 · 2017-12-22 20:30 OH Finally I think I just found a real pure random generator = sensors on your motherboard Celsius? Fahrenheit? coolers RPMs? Bipolar cryosystem? HAHAHAAHAHA whileever(!!) Go Go Go thagryumpiny it's a constant amazing pleasure to be picked on, by ontop scientisfinistic like ya! tons of universities documents, engineers repports and research papers are sooo wrong when your light (LMAO) apears ---------------------------------------------------------------------- Sanem · 2021-08-16 11:45 Quote from: Thaddy on December 22, 2017, 05:20:00 pm@sam707 That's not random. Because it relies on Randomize....... It is a PRNG, a peudo ramdom generator and it has many flaws: there is no entropy, but randomize..... (That s the really moron part (intended, moderators)) And Randomize is not a particular good Random seed.... Do the same tests as I did and published on the wiki. Ignorant. Test it for yourself: leave out randomize and you have a repeatable PRNG. Checkout the standard code for randomize... I am busy creating a real randomize...( not accepted yet, but close) I hope that everyone understands that it is not cryptographically secure in any way. This is not a case of woof, but bite. (without too much tissue ripped off if you don't move, that is, but only with my Jack Russel hybrids) Now change subject: Merry Christmas everybody and every one.... I will probably have a beer.. (more likely wine from NZ) Hi Thaddy, You said here you were making a true random, where can I find and use it? Regards ---------------------------------------------------------------------- Bandy · 2021-08-29 17:32 Hello, When I run this code in Lazarus 2.2.0RC1 Win 32bit is working. When I run the same code in Lazarus 2.2.0RC1 Win 64bit it returns the error in attached picture. What is the solution for the code to work in Win 64 bit, please? Thank you. function TryRdRand(out Value: Cardinal): Boolean; begin   {$ASMMODE intel}   asm   db   $0f   db   $c7   db   $f1   jc   @success   xor  eax,eax   ret   @success:   mov  [eax],rcx //here is the error in attached picture ONLY in Lazarus 2.2.0RC1 Win 64bit   mov  eax,1   end; end; procedure TForm1.IntelDrngClick(Sender: TObject); var   rnd: Cardinal; begin   if TryRdRand(rnd) then     showmessage(inttostr(rnd)); end; ---------------------------------------------------------------------- mika · 2021-08-29 21:14 Quote from: Bandy on August 29, 2021, 07:32:43 pmWhen I run this code in Lazarus 2.2.0RC1 Win 32bit is working. When I run the same code in Lazarus 2.2.0RC1 Win 64bit it returns the error in attached picture. What is the solution for the code to work in Win 64 bit, please? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function TryRdRand(out Value: Cardinal): Boolean; assembler; nostackframe;  {$ASMMODE intel}  asm  mov rdx,value  rdrand ecx  jc   @success  xor  eax,eax  ret  @success:  mov  [rdx],ecx //here is the error in attached picture ONLY in Lazarus 2.2.0RC1 Win 64bit  mov  eax,1end;  This is for 64 bit mode. For 32bit win replace rdx with edx. ---------------------------------------------------------------------- Bandy · 2021-08-31 10:45 Thank you, Mika. Let say I'm sure I have DRNG in my CPU, what is TryRdRand without the Try part? ---------------------------------------------------------------------- bytebites · 2021-08-31 11:56 Maybe this complicated? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function RdRand: Cardinal;assembler; nostackframe;  {$ASMMODE intel}asm  rdrand eaxend; ---------------------------------------------------------------------- Bandy · 2021-08-31 12:54 Thank you, bytebites. For AMD CPU 64bit what is the code for TryRdRand, please? ---------------------------------------------------------------------- Bandy · 2021-08-31 14:32 I should change {$ASMMODE intel} to {$ASMMODE amd} ? ---------------------------------------------------------------------- bytebites · 2021-08-31 16:59 No. Same code should work on AMD too assuming that rdrand works https://arstechnica.com/gadgets/2019/10/how-a-months-old-amd- microcode-bug-destroyed-my-weekend/ ---------------------------------------------------------------------- Bandy · 2021-08-31 17:20 Thank you, bytebites. I don't have AMD CPU. ---------------------------------------------------------------------- Bandy · 2021-08-31 17:29 For Android OS, there is a way to use true random number generator? I use the same rdrand instruction? What is the code for Lazarus 32/64 bit and Android OS? ---------------------------------------------------------------------- mas steindorff · 2021-08-31 22:17 Android OS is based on Linux so you should try the code from the WIKI https://wiki.freepascal.org/Dev_random ---------------------------------------------------------------------- Bandy · 2021-09-01 08:46 Thank you, mas steindorff. What is the code for retrieving true random numbers between [0; 1), with 0 included, 1 excluded, like function Random: extended. Can you explain to me how? ---------------------------------------------------------------------- Bandy · 2021-09-02 12:26 Please, someone help me with code for retrieving true random numbers between [0; 1), with 0 included, 1 excluded, like function Random: extended. Can you explain to me how? ---------------------------------------------------------------------- Sanem · 2021-09-02 12:50 There's no straight way like Random: Extended, use WinAPI or Linux seed files for seed and you'll have True random See attached project ---------------------------------------------------------------------- Bandy · 2021-09-02 16:11 Thank you, Sanem.