From d0ee467262dbb76a24ddb6913fb0d86655afb0c7 Mon Sep 17 00:00:00 2001 From: sikofitt Date: Mon, 18 May 2020 17:40:57 -0700 Subject: [PATCH] Renegade -> Olive BBS --- Olive.Random.BSDRandom.pas | 107 +++++++++++++++ Olive.Random.Generic.pas | 81 ++++++++++++ ...Random.pas => Olive.Random.LinuxRandom.pas | 76 ++++++----- Olive.Random.RandomInterface.pas | 90 +++++++++++++ ...om.URandom.pas => Olive.Random.URandom.pas | 76 ++++++----- Olive.Random.WinRandom.pas | 123 ++++++++++++++++++ Olive.Random.pas | 120 +++++++++++++++++ Renegade.Random.BSDRandom.pas | 104 --------------- Renegade.Random.Generic.pas | 77 ----------- Renegade.Random.RandomInterface.pas | 86 ------------ Renegade.Random.WinRandom.pas | 119 ----------------- Renegade.Random.pas | 116 ----------------- 12 files changed, 601 insertions(+), 574 deletions(-) create mode 100644 Olive.Random.BSDRandom.pas create mode 100644 Olive.Random.Generic.pas rename Renegade.Random.LinuxRandom.pas => Olive.Random.LinuxRandom.pas (52%) create mode 100644 Olive.Random.RandomInterface.pas rename Renegade.Random.URandom.pas => Olive.Random.URandom.pas (51%) create mode 100644 Olive.Random.WinRandom.pas create mode 100644 Olive.Random.pas delete mode 100644 Renegade.Random.BSDRandom.pas delete mode 100644 Renegade.Random.Generic.pas delete mode 100644 Renegade.Random.RandomInterface.pas delete mode 100644 Renegade.Random.WinRandom.pas delete mode 100644 Renegade.Random.pas diff --git a/Olive.Random.BSDRandom.pas b/Olive.Random.BSDRandom.pas new file mode 100644 index 0000000..d8f1251 --- /dev/null +++ b/Olive.Random.BSDRandom.pas @@ -0,0 +1,107 @@ +{********************************************************} +{ } +{ Olive BBS } +{ } +{ Copyleft (ↄ) 2020 Olive BBS } +{ } +{ This file is part of Olive BBS } +{ } +{ Olive BBS is free software: you can redistribute it } +{ and/or modify it under the terms of the GNU General } +{ Public License as published by the Free Software } +{ Foundation, either version 3 of the License, or } +{ (at your option) any later version. } +{ } +{ Olive BBS is distributed in the hope that it will } +{ be useful, but WITHOUT ANY WARRANTY; without even } +{ the implied warranty of MERCHANTABILITY or FITNESS } +{ FOR A PARTICULAR PURPOSE. See the GNU General } +{ Public License for more details. } +{ } +{ You should have received a copy of the GNU General } +{ Public License along with Olive BBS. If not, see } +{ . } +{ } +{********************************************************} +{ ___ ___ ___ } +{ ( ).-. ( ) ( ) } +{ .--. | |( __)___ ___ .--. | |.-. | |.-. .--. } +{ / \| |(''"( )( / \| / \| / \ / _ \ } +{ | .-. | | | | | | | | .-. | .-. | .-. |. .' `. ; } +{ | | | | | | | | | | | | | | | | | | | || ' | | } +{ | | | | | | | | | | | |/ | | | | | | |_\_`.(___) } +{ | | | | | | | | | | | ' _.| | | | | | ( ). '. } +{ | ' | | | | | ' ' ; | .'.-| ' | | ' | || | `\ | } +{ ' `-' | | | | \ `' /' `-' ' `-' ;' `-' ; ; '._,' ' } +{ `.__.(___(___) '_.' `.__.' `.__. `.__. '.___.' } +{ } +{********************************************************} + +{$mode objfpc}{$H+} +Unit Olive.Random.BSDRandom; + +interface + +uses + CTypes, + Objects, + Classes, + SysUtils, + Olive.Random.RandomInterface, + Olive.Random.URandom; + +type + PCChar = ^CChar; + PBSDRandom = ^BSDRandom; + BSDRandom = class (RandomTrait, RandomInterface) + private + function GetSystemBytes(var RandomByteBuffer : TBytes; NBytes : SizeUint) : CInt; + public + function GetBytes(NBytes : SizeUInt) : TBytes; + function GetString(NBytes : SizeUInt) : AnsiString; + end; + +procedure arc4random_buf(var Buffer; NBytes : csize_t); + cdecl;external 'c' name 'arc4random_buf'; + +implementation + +function BSDRandom.GetSystemBytes(var RandomByteBuffer : TBytes; NBytes : SizeUint) : CInt; +var + CharBuffer : array of pcuint8; +begin + SetLength(CharBuffer, NBytes); + arc4random_buf(CharBuffer[0], NBytes); + if Length(CharBuffer) <> NBytes then + begin + RandomByteBuffer[0] := 0; + Result := -1; + end else + begin + Move(CharBuffer[Low(CharBuffer)], RandomByteBuffer[0], NBytes); + Result := High(RandomByteBuffer); + end; +end; + +function BSDRandom.GetBytes(NBytes : SizeUInt) : TBytes; +var + RandomBuffer : AnsiString; +begin + SetLength(RandomBuffer, NBytes); + SetLength(Result, NBytes); + RandomBuffer := GetString(NBytes); + Move(RandomBuffer[1], Result[0], NBytes); +end; + +function BSDRandom.GetString(NBytes : SizeUInt) : AnsiString; +var + RandomBuffer : TBytes; + B : SizeInt; +begin + SetLength(RandomBuffer, (NBytes*2)); + SetLength(Result, NBytes); + B := GetSystemBytes(RandomBuffer, (NBytes*2)); + Move(RandomBuffer[0], Result[1], NBytes); +end; + +end. diff --git a/Olive.Random.Generic.pas b/Olive.Random.Generic.pas new file mode 100644 index 0000000..80273b6 --- /dev/null +++ b/Olive.Random.Generic.pas @@ -0,0 +1,81 @@ +{********************************************************} +{ } +{ Olive BBS } +{ } +{ Copyleft (ↄ) 2020 Olive BBS } +{ } +{ This file is part of Olive BBS } +{ } +{ Olive BBS is free software: you can redistribute it } +{ and/or modify it under the terms of the GNU General } +{ Public License as published by the Free Software } +{ Foundation, either version 3 of the License, or } +{ (at your option) any later version. } +{ } +{ Olive BBS is distributed in the hope that it will } +{ be useful, but WITHOUT ANY WARRANTY; without even } +{ the implied warranty of MERCHANTABILITY or FITNESS } +{ FOR A PARTICULAR PURPOSE. See the GNU General } +{ Public License for more details. } +{ } +{ You should have received a copy of the GNU General } +{ Public License along with Olive BBS. If not, see } +{ . } +{ } +{********************************************************} +{ ___ ___ ___ } +{ ( ).-. ( ) ( ) } +{ .--. | |( __)___ ___ .--. | |.-. | |.-. .--. } +{ / \| |(''"( )( / \| / \| / \ / _ \ } +{ | .-. | | | | | | | | .-. | .-. | .-. |. .' `. ; } +{ | | | | | | | | | | | | | | | | | | | || ' | | } +{ | | | | | | | | | | | |/ | | | | | | |_\_`.(___) } +{ | | | | | | | | | | | ' _.| | | | | | ( ). '. } +{ | ' | | | | | ' ' ; | .'.-| ' | | ' | || | `\ | } +{ ' `-' | | | | \ `' /' `-' ' `-' ;' `-' ; ; '._,' ' } +{ `.__.(___(___) '_.' `.__.' `.__. `.__. '.___.' } +{ } +{********************************************************} + +{$mode objfpc}{$H+} +Unit Olive.Random.Generic; + +interface + +uses + Objects, + Classes, + SysUtils, + Olive.Random.RandomInterface; + +type + PRandomGeneric = ^RandomGeneric; + RandomGeneric = class (RandomTrait, RandomInterface) + public + function GetBytes(NBytes : SizeUInt) : TBytes; + function GetString(NBytes : SizeUInt) : AnsiString; + end; + +implementation + + +function RandomGeneric.GetBytes(NBytes : SizeUInt) : TBytes; +var + RandomBuffer : AnsiString; +begin + SetLength(Result, NBytes); + RandomBuffer := GetString(NBytes); + Move(RandomBuffer[1], Result[0], NBytes); +end; + +function RandomGeneric.GetString(NBytes : SizeUInt) : AnsiString; +var + ByteBuffer : TBytes; +begin + SetLength(Result, NBytes); + SetLength(ByteBuffer, (NBytes*2)); + ByteBuffer := MTRandomBytes((NBytes*2)); + Move(ByteBuffer[0], Result[1], NBytes); +end; + +end. diff --git a/Renegade.Random.LinuxRandom.pas b/Olive.Random.LinuxRandom.pas similarity index 52% rename from Renegade.Random.LinuxRandom.pas rename to Olive.Random.LinuxRandom.pas index 949afea..5e9c5a3 100644 --- a/Renegade.Random.LinuxRandom.pas +++ b/Olive.Random.LinuxRandom.pas @@ -1,40 +1,44 @@ -{*******************************************************} -{ } -{ Renegade BBS } -{ } -{ Copyright (c) 1990-2013 The Renegade Dev Team } -{ Copyleft (ↄ) 2016 Renegade BBS } -{ } -{ This file is part of Renegade BBS } -{ } -{ Renegade is free software: you can redistribute it } -{ and/or modify it under the terms of the GNU General } -{ Public License as published by the Free Software } -{ Foundation, either version 3 of the License, or } -{ (at your option) any later version. } -{ } -{ Renegade is distributed in the hope that it will be } -{ useful, but WITHOUT ANY WARRANTY; without even the } -{ implied warranty of MERCHANTABILITY or FITNESS FOR } -{ A PARTICULAR PURPOSE. See the GNU General Public } -{ License for more details. } -{ } -{ You should have received a copy of the GNU General } -{ Public License along with Renegade. If not, see } -{ . } -{ } -{*******************************************************} -{ _______ __ } -{ | _ .-----.-----.-----.-----.---.-.--| .-----. } -{ |. l | -__| | -__| _ | _ | _ | -__| } -{ |. _ |_____|__|__|_____|___ |___._|_____|_____| } -{ |: | | |_____| } -{ |::.|:. | } -{ `--- ---' } -{*******************************************************} +{********************************************************} +{ } +{ Olive BBS } +{ } +{ Copyleft (ↄ) 2020 Olive BBS } +{ } +{ This file is part of Olive BBS } +{ } +{ Olive BBS is free software: you can redistribute it } +{ and/or modify it under the terms of the GNU General } +{ Public License as published by the Free Software } +{ Foundation, either version 3 of the License, or } +{ (at your option) any later version. } +{ } +{ Olive BBS is distributed in the hope that it will } +{ be useful, but WITHOUT ANY WARRANTY; without even } +{ the implied warranty of MERCHANTABILITY or FITNESS } +{ FOR A PARTICULAR PURPOSE. See the GNU General } +{ Public License for more details. } +{ } +{ You should have received a copy of the GNU General } +{ Public License along with Olive BBS. If not, see } +{ . } +{ } +{********************************************************} +{ ___ ___ ___ } +{ ( ).-. ( ) ( ) } +{ .--. | |( __)___ ___ .--. | |.-. | |.-. .--. } +{ / \| |(''"( )( / \| / \| / \ / _ \ } +{ | .-. | | | | | | | | .-. | .-. | .-. |. .' `. ; } +{ | | | | | | | | | | | | | | | | | | | || ' | | } +{ | | | | | | | | | | | |/ | | | | | | |_\_`.(___) } +{ | | | | | | | | | | | ' _.| | | | | | ( ). '. } +{ | ' | | | | | ' ' ; | .'.-| ' | | ' | || | `\ | } +{ ' `-' | | | | \ `' /' `-' ' `-' ;' `-' ; ; '._,' ' } +{ `.__.(___(___) '_.' `.__.' `.__. `.__. '.___.' } +{ } +{********************************************************} {$mode objfpc}{$H+} -Unit Renegade.Random.LinuxRandom; +Unit Olive.Random.LinuxRandom; interface @@ -43,7 +47,7 @@ uses Objects, Classes, SysUtils, - Renegade.Random.RandomInterface; + Olive.Random.RandomInterface; const {$IF DEFINED(CPU64)} SYS_getrandom = 318; diff --git a/Olive.Random.RandomInterface.pas b/Olive.Random.RandomInterface.pas new file mode 100644 index 0000000..af2464f --- /dev/null +++ b/Olive.Random.RandomInterface.pas @@ -0,0 +1,90 @@ +{********************************************************} +{ } +{ Olive BBS } +{ } +{ Copyleft (ↄ) 2020 Olive BBS } +{ } +{ This file is part of Olive BBS } +{ } +{ Olive BBS is free software: you can redistribute it } +{ and/or modify it under the terms of the GNU General } +{ Public License as published by the Free Software } +{ Foundation, either version 3 of the License, or } +{ (at your option) any later version. } +{ } +{ Olive BBS is distributed in the hope that it will } +{ be useful, but WITHOUT ANY WARRANTY; without even } +{ the implied warranty of MERCHANTABILITY or FITNESS } +{ FOR A PARTICULAR PURPOSE. See the GNU General } +{ Public License for more details. } +{ } +{ You should have received a copy of the GNU General } +{ Public License along with Olive BBS. If not, see } +{ . } +{ } +{********************************************************} +{ ___ ___ ___ } +{ ( ).-. ( ) ( ) } +{ .--. | |( __)___ ___ .--. | |.-. | |.-. .--. } +{ / \| |(''"( )( / \| / \| / \ / _ \ } +{ | .-. | | | | | | | | .-. | .-. | .-. |. .' `. ; } +{ | | | | | | | | | | | | | | | | | | | || ' | | } +{ | | | | | | | | | | | |/ | | | | | | |_\_`.(___) } +{ | | | | | | | | | | | ' _.| | | | | | ( ). '. } +{ | ' | | | | | ' ' ; | .'.-| ' | | ' | || | `\ | } +{ ' `-' | | | | \ `' /' `-' ' `-' ;' `-' ; ; '._,' ' } +{ `.__.(___(___) '_.' `.__.' `.__. `.__. '.___.' } +{ } +{********************************************************} + +{$mode objfpc}{$H+} +{$interfaces corba} +{$codepage UTF8} +Unit Olive.Random.RandomInterface; + +interface + +uses + Classes, + SysUtils; + +type + RandomInterface = interface + ['{0750E585-C1D2-4C1F-A8A4-4EDC41847396}'] + function GetBytes(NBytes : SizeUInt) : TBytes; + function GetString(NBytes : SizeUInt) : AnsiString; + end; + + RandomTrait = class (TObject) + public + constructor Create; + destructor Destroy; override; + function MTRandomBytes(NBytes : SizeUInt) : TBytes; virtual; + end; + +implementation + +constructor RandomTrait.Create; +begin + inherited Create; +end; + +destructor RandomTrait.Destroy; +begin + inherited Destroy; +end; + +function RandomTrait.MTRandomBytes(NBytes : SizeUInt) : TBytes; +var + i : SizeUint; +begin + Randomize; + SetLength(Result, (NBytes*2)); + for i := 0 to (NBytes*2) do + begin + Result[i] := Random(MaxInt) mod 256; + end; + SetLength(Result, NBytes); +end; + +end. diff --git a/Renegade.Random.URandom.pas b/Olive.Random.URandom.pas similarity index 51% rename from Renegade.Random.URandom.pas rename to Olive.Random.URandom.pas index bc55c17..c6db58b 100644 --- a/Renegade.Random.URandom.pas +++ b/Olive.Random.URandom.pas @@ -1,40 +1,44 @@ -{*******************************************************} -{ } -{ Renegade BBS } -{ } -{ Copyright (c) 1990-2013 The Renegade Dev Team } -{ Copyleft (ↄ) 2016 Renegade BBS } -{ } -{ This file is part of Renegade BBS } -{ } -{ Renegade is free software: you can redistribute it } -{ and/or modify it under the terms of the GNU General } -{ Public License as published by the Free Software } -{ Foundation, either version 3 of the License, or } -{ (at your option) any later version. } -{ } -{ Renegade is distributed in the hope that it will be } -{ useful, but WITHOUT ANY WARRANTY; without even the } -{ implied warranty of MERCHANTABILITY or FITNESS FOR } -{ A PARTICULAR PURPOSE. See the GNU General Public } -{ License for more details. } -{ } -{ You should have received a copy of the GNU General } -{ Public License along with Renegade. If not, see } -{ . } -{ } -{*******************************************************} -{ _______ __ } -{ | _ .-----.-----.-----.-----.---.-.--| .-----. } -{ |. l | -__| | -__| _ | _ | _ | -__| } -{ |. _ |_____|__|__|_____|___ |___._|_____|_____| } -{ |: | | |_____| } -{ |::.|:. | } -{ `--- ---' } -{*******************************************************} +{********************************************************} +{ } +{ Olive BBS } +{ } +{ Copyleft (ↄ) 2020 Olive BBS } +{ } +{ This file is part of Olive BBS } +{ } +{ Olive BBS is free software: you can redistribute it } +{ and/or modify it under the terms of the GNU General } +{ Public License as published by the Free Software } +{ Foundation, either version 3 of the License, or } +{ (at your option) any later version. } +{ } +{ Olive BBS is distributed in the hope that it will } +{ be useful, but WITHOUT ANY WARRANTY; without even } +{ the implied warranty of MERCHANTABILITY or FITNESS } +{ FOR A PARTICULAR PURPOSE. See the GNU General } +{ Public License for more details. } +{ } +{ You should have received a copy of the GNU General } +{ Public License along with Olive BBS. If not, see } +{ . } +{ } +{********************************************************} +{ ___ ___ ___ } +{ ( ).-. ( ) ( ) } +{ .--. | |( __)___ ___ .--. | |.-. | |.-. .--. } +{ / \| |(''"( )( / \| / \| / \ / _ \ } +{ | .-. | | | | | | | | .-. | .-. | .-. |. .' `. ; } +{ | | | | | | | | | | | | | | | | | | | || ' | | } +{ | | | | | | | | | | | |/ | | | | | | |_\_`.(___) } +{ | | | | | | | | | | | ' _.| | | | | | ( ). '. } +{ | ' | | | | | ' ' ; | .'.-| ' | | ' | || | `\ | } +{ ' `-' | | | | \ `' /' `-' ' `-' ;' `-' ; ; '._,' ' } +{ `.__.(___(___) '_.' `.__.' `.__. `.__. '.___.' } +{ } +{********************************************************} {$mode objfpc}{$H+} -Unit Renegade.Random.URandom; +Unit Olive.Random.URandom; interface @@ -43,7 +47,7 @@ uses Objects, Classes, SysUtils, - Renegade.Random.RandomInterface; + Olive.Random.RandomInterface; const {$IF DEFINED(CPU64)} SYS_getrandom = 318; diff --git a/Olive.Random.WinRandom.pas b/Olive.Random.WinRandom.pas new file mode 100644 index 0000000..6919167 --- /dev/null +++ b/Olive.Random.WinRandom.pas @@ -0,0 +1,123 @@ +{********************************************************} +{ } +{ Olive BBS } +{ } +{ Copyleft (ↄ) 2020 Olive BBS } +{ } +{ This file is part of Olive BBS } +{ } +{ Olive BBS is free software: you can redistribute it } +{ and/or modify it under the terms of the GNU General } +{ Public License as published by the Free Software } +{ Foundation, either version 3 of the License, or } +{ (at your option) any later version. } +{ } +{ Olive BBS is distributed in the hope that it will } +{ be useful, but WITHOUT ANY WARRANTY; without even } +{ the implied warranty of MERCHANTABILITY or FITNESS } +{ FOR A PARTICULAR PURPOSE. See the GNU General } +{ Public License for more details. } +{ } +{ You should have received a copy of the GNU General } +{ Public License along with Olive BBS. If not, see } +{ . } +{ } +{********************************************************} +{ ___ ___ ___ } +{ ( ).-. ( ) ( ) } +{ .--. | |( __)___ ___ .--. | |.-. | |.-. .--. } +{ / \| |(''"( )( / \| / \| / \ / _ \ } +{ | .-. | | | | | | | | .-. | .-. | .-. |. .' `. ; } +{ | | | | | | | | | | | | | | | | | | | || ' | | } +{ | | | | | | | | | | | |/ | | | | | | |_\_`.(___) } +{ | | | | | | | | | | | ' _.| | | | | | ( ). '. } +{ | ' | | | | | ' ' ; | .'.-| ' | | ' | || | `\ | } +{ ' `-' | | | | \ `' /' `-' ' `-' ;' `-' ; ; '._,' ' } +{ `.__.(___(___) '_.' `.__.' `.__. `.__. '.___.' } +{ } +{********************************************************} + +{$mode objfpc}{$H+} +Unit Olive.Random.WinRandom; + +interface + +uses + Objects, + Classes, + SysUtils, + Windows, + Olive.Random.RandomInterface; + +const + CRYPT_VERIFYCONTEXT = $F0000000; + CRYPT_MACHINE_KEYSET = 32; + PROV_RSA_FULL = 1; + CRYPT_NEWKEYSET = 8; + + +type + HCRYPTPROV = ULONG_PTR; + PWinRandom = ^WinRandom; + WinRandom = class (RandomTrait, RandomInterface) + public + function GetBytes(NBytes : SizeUInt) : TBytes; + function GetString(NBytes : SizeUInt) : AnsiString; + end; + + function CryptAcquireContextW(var phProv: HCRYPTPROV; pszContainer: LPCTSTR; + pszProvider: LPCTSTR; dwProvType: DWORD; dwFlags: DWORD): BOOL;stdcall; external 'advapi32' name 'CryptAcquireContextW'; + function CryptGenRandom(hProv: HCRYPTPROV; dwLen: DWORD; + var pbBuffer: BYTE): BOOL; stdcall; external 'advapi32' name 'CryptGenRandom'; + +implementation + + +function WinRandom.GetBytes(NBytes : SizeUInt) : TBytes; +var + RandomBuffer : AnsiString; +begin + SetLength(Result, NBytes); + SetLength(RandomBuffer, NBytes); + RandomBuffer := GetString(NBytes); + Move(RandomBuffer[1], Result[0], NBytes); +end; + +function WinRandom.GetString(NBytes : SizeUInt) : AnsiString; +var + RandomBuffer : ^BYTE; + hCryptProv : ^ULONG_PTR; + WinCrypt : Boolean; + ReturnString : AnsiString; + i, Bytes : SizeInt; + ReturnBytes : TBytes; +begin + GetMem(RandomBuffer, (NBytes * 2)); + CryptAcquireContextW(hCryptProv^, nil, nil, PROV_RSA_FULL, + CRYPT_NEWKEYSET or CRYPT_MACHINE_KEYSET or CRYPT_VERIFYCONTEXT ); + WinCrypt := CryptGenRandom(hCryptProv^, (NBytes * 2), RandomBuffer^); + SetLength(ReturnString, (NBytes*2)); + if WinCrypt then + begin + for i := 1 to (NBytes*2) do + begin + ReturnString[i] := Chr(RandomBuffer[i]); + end; + end else + begin + SetLength(ReturnBytes, (NBytes*2)); + ReturnBytes := MTRandomBytes((NBytes*2)); + end; + SetLength(Result, NBytes); + FreeMem(RandomBuffer, (NBytes * 2)); + if WinCrypt then + begin + Move(ReturnString[1], Result[1], NBytes); + end else + begin + Move(ReturnBytes[0], Result[1], NBytes); + end; + +end; + +end. diff --git a/Olive.Random.pas b/Olive.Random.pas new file mode 100644 index 0000000..d56805f --- /dev/null +++ b/Olive.Random.pas @@ -0,0 +1,120 @@ +{********************************************************} +{ } +{ Olive BBS } +{ } +{ Copyleft (ↄ) 2020 Olive BBS } +{ } +{ This file is part of Olive BBS } +{ } +{ Olive BBS is free software: you can redistribute it } +{ and/or modify it under the terms of the GNU General } +{ Public License as published by the Free Software } +{ Foundation, either version 3 of the License, or } +{ (at your option) any later version. } +{ } +{ Olive BBS is distributed in the hope that it will } +{ be useful, but WITHOUT ANY WARRANTY; without even } +{ the implied warranty of MERCHANTABILITY or FITNESS } +{ FOR A PARTICULAR PURPOSE. See the GNU General } +{ Public License for more details. } +{ } +{ You should have received a copy of the GNU General } +{ Public License along with Olive BBS. If not, see } +{ . } +{ } +{********************************************************} +{ ___ ___ ___ } +{ ( ).-. ( ) ( ) } +{ .--. | |( __)___ ___ .--. | |.-. | |.-. .--. } +{ / \| |(''"( )( / \| / \| / \ / _ \ } +{ | .-. | | | | | | | | .-. | .-. | .-. |. .' `. ; } +{ | | | | | | | | | | | | | | | | | | | || ' | | } +{ | | | | | | | | | | | |/ | | | | | | |_\_`.(___) } +{ | | | | | | | | | | | ' _.| | | | | | ( ). '. } +{ | ' | | | | | ' ' ; | .'.-| ' | | ' | || | `\ | } +{ ' `-' | | | | \ `' /' `-' ' `-' ;' `-' ; ; '._,' ' } +{ `.__.(___(___) '_.' `.__.' `.__. `.__. '.___.' } +{ } +{********************************************************} + +{$mode objfpc}{$H+} +{$interfaces corba} +Unit Olive.Random; + +interface + +uses + Objects, + Classes, + SysUtils, + Olive.Random.RandomInterface, +{$IF DEFINED(LINUX)} + Olive.Random.LinuxRandom +{$ELSEIF DEFINED(WINDOWS)} + Olive.Random.WinRandom +{$ELSEIF DEFINED(BSD)} + Olive.Random.BSDRandom +{$ELSE} + Olive.Random.Generic; +{$ENDIF} + ; + +type + PRandom = ^TRandom; + TRandom = class (RandomTrait, RandomInterface) + private + FRandomGenerator : RandomInterface; + procedure SetRandomGenerator(GeneratorClass : RandomInterface); + public + constructor Init; + destructor Destroy; override; + procedure SetDefaultGenerator; + function GetBytes(NBytes : SizeUInt) : TBytes; + function GetString(NBytes : SizeUInt) : AnsiString; + property RandomGenerator : RandomInterface read FRandomGenerator write SetRandomGenerator; + end; + +implementation + + +constructor TRandom.Init; +begin + SetDefaultGenerator; +end; + +destructor TRandom.Destroy; +begin + inherited Destroy; +end; + +procedure TRandom.SetRandomGenerator(GeneratorClass : RandomInterface); +begin + FRandomGenerator := GeneratorClass; +end; + +procedure TRandom.SetDefaultGenerator; +begin + {$IF DEFINED(LINUX)} + FRandomGenerator := LinuxRandom.Create; + {$ELSEIF DEFINED(WINDOWS)} + FRandomGenerator := WinRandom.Create; + {$ELSEIF DEFINED(BSD)} + FRandomGenerator := BSDRandom.Create; + {$ELSE} + FRandomGenerator := RandomGeneric.Create; + {$ENDIF} +end; + +function TRandom.GetBytes(NBytes : SizeUInt) : TBytes; +begin + SetLength(Result, NBytes); + Result := FRandomGenerator.GetBytes(NBytes); +end; +function TRandom.GetString(NBytes : SizeUInt) : AnsiString; +begin + SetLength(Result, NBytes); + Result := FRandomGenerator.GetString(NBytes); + +end; + +end. diff --git a/Renegade.Random.BSDRandom.pas b/Renegade.Random.BSDRandom.pas deleted file mode 100644 index 376ebf8..0000000 --- a/Renegade.Random.BSDRandom.pas +++ /dev/null @@ -1,104 +0,0 @@ -{*******************************************************} -{ } -{ Renegade BBS } -{ } -{ Copyright (c) 1990-2013 The Renegade Dev Team } -{ Copyleft (ↄ) 2016 Renegade BBS } -{ } -{ This file is part of Renegade BBS } -{ } -{ Renegade is free software: you can redistribute it } -{ and/or modify it under the terms of the GNU General } -{ Public License as published by the Free Software } -{ Foundation, either version 3 of the License, or } -{ (at your option) any later version. } -{ } -{ Renegade is distributed in the hope that it will be } -{ useful, but WITHOUT ANY WARRANTY; without even the } -{ implied warranty of MERCHANTABILITY or FITNESS FOR } -{ A PARTICULAR PURPOSE. See the GNU General Public } -{ License for more details. } -{ } -{ You should have received a copy of the GNU General } -{ Public License along with Renegade. If not, see } -{ . } -{ } -{*******************************************************} -{ _______ __ } -{ | _ .-----.-----.-----.-----.---.-.--| .-----. } -{ |. l | -__| | -__| _ | _ | _ | -__| } -{ |. _ |_____|__|__|_____|___ |___._|_____|_____| } -{ |: | | |_____| } -{ |::.|:. | } -{ `--- ---' } -{*******************************************************} - -{ ???: Todo - use arc4random_buf } -{$mode objfpc}{$H+} -Unit Renegade.Random.BSDRandom; - -interface - -uses - CTypes, - Objects, - Classes, - SysUtils, - Renegade.Random.RandomInterface, - Renegade.Random.URandom; - -type - PCChar = ^CChar; - PBSDRandom = ^BSDRandom; - BSDRandom = class (RandomTrait, RandomInterface) - private - function GetSystemBytes(var RandomByteBuffer : TBytes; NBytes : SizeUint) : CInt; - public - function GetBytes(NBytes : SizeUInt) : TBytes; - function GetString(NBytes : SizeUInt) : AnsiString; - end; - -procedure arc4random_buf(var Buffer; NBytes : csize_t); - cdecl;external 'c' name 'arc4random_buf'; - -implementation - -function BSDRandom.GetSystemBytes(var RandomByteBuffer : TBytes; NBytes : SizeUint) : CInt; -var - CharBuffer : array of pcuint8; -begin - SetLength(CharBuffer, NBytes); - arc4random_buf(CharBuffer[0], NBytes); - if Length(CharBuffer) <> NBytes then - begin - RandomByteBuffer[0] := 0; - Result := -1; - end else - begin - Move(CharBuffer[Low(CharBuffer)], RandomByteBuffer[0], NBytes); - Result := High(RandomByteBuffer); - end; -end; - -function BSDRandom.GetBytes(NBytes : SizeUInt) : TBytes; -var - RandomBuffer : AnsiString; -begin - SetLength(RandomBuffer, NBytes); - SetLength(Result, NBytes); - RandomBuffer := GetString(NBytes); - Move(RandomBuffer[1], Result[0], NBytes); -end; - -function BSDRandom.GetString(NBytes : SizeUInt) : AnsiString; -var - RandomBuffer : TBytes; - B : SizeInt; -begin - SetLength(RandomBuffer, (NBytes*2)); - SetLength(Result, NBytes); - B := GetSystemBytes(RandomBuffer, (NBytes*2)); - Move(RandomBuffer[0], Result[1], NBytes); -end; - -end. diff --git a/Renegade.Random.Generic.pas b/Renegade.Random.Generic.pas deleted file mode 100644 index 668d792..0000000 --- a/Renegade.Random.Generic.pas +++ /dev/null @@ -1,77 +0,0 @@ -{*******************************************************} -{ } -{ Renegade BBS } -{ } -{ Copyright (c) 1990-2013 The Renegade Dev Team } -{ Copyleft (ↄ) 2016 Renegade BBS } -{ } -{ This file is part of Renegade BBS } -{ } -{ Renegade is free software: you can redistribute it } -{ and/or modify it under the terms of the GNU General } -{ Public License as published by the Free Software } -{ Foundation, either version 3 of the License, or } -{ (at your option) any later version. } -{ } -{ Renegade is distributed in the hope that it will be } -{ useful, but WITHOUT ANY WARRANTY; without even the } -{ implied warranty of MERCHANTABILITY or FITNESS FOR } -{ A PARTICULAR PURPOSE. See the GNU General Public } -{ License for more details. } -{ } -{ You should have received a copy of the GNU General } -{ Public License along with Renegade. If not, see } -{ . } -{ } -{*******************************************************} -{ _______ __ } -{ | _ .-----.-----.-----.-----.---.-.--| .-----. } -{ |. l | -__| | -__| _ | _ | _ | -__| } -{ |. _ |_____|__|__|_____|___ |___._|_____|_____| } -{ |: | | |_____| } -{ |::.|:. | } -{ `--- ---' } -{*******************************************************} - -{$mode objfpc}{$H+} -Unit Renegade.Random.Generic; - -interface - -uses - Objects, - Classes, - SysUtils, - Renegade.Random.RandomInterface; - -type - PRandomGeneric = ^RandomGeneric; - RandomGeneric = class (RandomTrait, RandomInterface) - public - function GetBytes(NBytes : SizeUInt) : TBytes; - function GetString(NBytes : SizeUInt) : AnsiString; - end; - -implementation - - -function RandomGeneric.GetBytes(NBytes : SizeUInt) : TBytes; -var - RandomBuffer : AnsiString; -begin - SetLength(Result, NBytes); - RandomBuffer := GetString(NBytes); - Move(RandomBuffer[1], Result[0], NBytes); -end; - -function RandomGeneric.GetString(NBytes : SizeUInt) : AnsiString; -var - ByteBuffer : TBytes; -begin - SetLength(Result, NBytes); - SetLength(ByteBuffer, (NBytes*2)); - ByteBuffer := MTRandomBytes((NBytes*2)); - Move(ByteBuffer[0], Result[1], NBytes); -end; - -end. diff --git a/Renegade.Random.RandomInterface.pas b/Renegade.Random.RandomInterface.pas deleted file mode 100644 index a9d8a12..0000000 --- a/Renegade.Random.RandomInterface.pas +++ /dev/null @@ -1,86 +0,0 @@ -{*******************************************************} -{ } -{ Renegade BBS } -{ } -{ Copyright (c) 1990-2013 The Renegade Dev Team } -{ Copyleft (ↄ) 2016 Renegade BBS } -{ } -{ This file is part of Renegade BBS } -{ } -{ Renegade is free software: you can redistribute it } -{ and/or modify it under the terms of the GNU General } -{ Public License as published by the Free Software } -{ Foundation, either version 3 of the License, or } -{ (at your option) any later version. } -{ } -{ Renegade is distributed in the hope that it will be } -{ useful, but WITHOUT ANY WARRANTY; without even the } -{ implied warranty of MERCHANTABILITY or FITNESS FOR } -{ A PARTICULAR PURPOSE. See the GNU General Public } -{ License for more details. } -{ } -{ You should have received a copy of the GNU General } -{ Public License along with Renegade. If not, see } -{ . } -{ } -{*******************************************************} -{ _______ __ } -{ | _ .-----.-----.-----.-----.---.-.--| .-----. } -{ |. l | -__| | -__| _ | _ | _ | -__| } -{ |. _ |_____|__|__|_____|___ |___._|_____|_____| } -{ |: | | |_____| } -{ |::.|:. | } -{ `--- ---' } -{*******************************************************} - -{$mode objfpc}{$H+} -{$interfaces corba} -{$codepage UTF8} -Unit Renegade.Random.RandomInterface; - -interface - -uses - Classes, - SysUtils; - -type - RandomInterface = interface - ['{0750E585-C1D2-4C1F-A8A4-4EDC41847396}'] - function GetBytes(NBytes : SizeUInt) : TBytes; - function GetString(NBytes : SizeUInt) : AnsiString; - end; - - RandomTrait = class (TObject) - public - constructor Create; - destructor Destroy; override; - function MTRandomBytes(NBytes : SizeUInt) : TBytes; virtual; - end; - -implementation - -constructor RandomTrait.Create; -begin - inherited Create; -end; - -destructor RandomTrait.Destroy; -begin - inherited Destroy; -end; - -function RandomTrait.MTRandomBytes(NBytes : SizeUInt) : TBytes; -var - i : SizeUint; -begin - Randomize; - SetLength(Result, (NBytes*2)); - for i := 0 to (NBytes*2) do - begin - Result[i] := Random(MaxInt) mod 256; - end; - SetLength(Result, NBytes); -end; - -end. diff --git a/Renegade.Random.WinRandom.pas b/Renegade.Random.WinRandom.pas deleted file mode 100644 index 54528b7..0000000 --- a/Renegade.Random.WinRandom.pas +++ /dev/null @@ -1,119 +0,0 @@ -{*******************************************************} -{ } -{ Renegade BBS } -{ } -{ Copyright (c) 1990-2013 The Renegade Dev Team } -{ Copyleft (ↄ) 2016 Renegade BBS } -{ } -{ This file is part of Renegade BBS } -{ } -{ Renegade is free software: you can redistribute it } -{ and/or modify it under the terms of the GNU General } -{ Public License as published by the Free Software } -{ Foundation, either version 3 of the License, or } -{ (at your option) any later version. } -{ } -{ Renegade is distributed in the hope that it will be } -{ useful, but WITHOUT ANY WARRANTY; without even the } -{ implied warranty of MERCHANTABILITY or FITNESS FOR } -{ A PARTICULAR PURPOSE. See the GNU General Public } -{ License for more details. } -{ } -{ You should have received a copy of the GNU General } -{ Public License along with Renegade. If not, see } -{ . } -{ } -{*******************************************************} -{ _______ __ } -{ | _ .-----.-----.-----.-----.---.-.--| .-----. } -{ |. l | -__| | -__| _ | _ | _ | -__| } -{ |. _ |_____|__|__|_____|___ |___._|_____|_____| } -{ |: | | |_____| } -{ |::.|:. | } -{ `--- ---' } -{*******************************************************} - -{$mode objfpc}{$H+} -Unit Renegade.Random.WinRandom; - -interface - -uses - Objects, - Classes, - SysUtils, - Windows, - Renegade.Random.RandomInterface; - -const - CRYPT_VERIFYCONTEXT = $F0000000; - CRYPT_MACHINE_KEYSET = 32; - PROV_RSA_FULL = 1; - CRYPT_NEWKEYSET = 8; - - -type - HCRYPTPROV = ULONG_PTR; - PWinRandom = ^WinRandom; - WinRandom = class (RandomTrait, RandomInterface) - public - function GetBytes(NBytes : SizeUInt) : TBytes; - function GetString(NBytes : SizeUInt) : AnsiString; - end; - - function CryptAcquireContextW(var phProv: HCRYPTPROV; pszContainer: LPCTSTR; - pszProvider: LPCTSTR; dwProvType: DWORD; dwFlags: DWORD): BOOL;stdcall; external 'advapi32' name 'CryptAcquireContextW'; - function CryptGenRandom(hProv: HCRYPTPROV; dwLen: DWORD; - var pbBuffer: BYTE): BOOL; stdcall; external 'advapi32' name 'CryptGenRandom'; - -implementation - - -function WinRandom.GetBytes(NBytes : SizeUInt) : TBytes; -var - RandomBuffer : AnsiString; -begin - SetLength(Result, NBytes); - SetLength(RandomBuffer, NBytes); - RandomBuffer := GetString(NBytes); - Move(RandomBuffer[1], Result[0], NBytes); -end; - -function WinRandom.GetString(NBytes : SizeUInt) : AnsiString; -var - RandomBuffer : ^BYTE; - hCryptProv : ^ULONG_PTR; - WinCrypt : Boolean; - ReturnString : AnsiString; - i, Bytes : SizeInt; - ReturnBytes : TBytes; -begin - GetMem(RandomBuffer, (NBytes * 2)); - CryptAcquireContextW(hCryptProv^, nil, nil, PROV_RSA_FULL, - CRYPT_NEWKEYSET or CRYPT_MACHINE_KEYSET or CRYPT_VERIFYCONTEXT ); - WinCrypt := CryptGenRandom(hCryptProv^, (NBytes * 2), RandomBuffer^); - SetLength(ReturnString, (NBytes*2)); - if WinCrypt then - begin - for i := 1 to (NBytes*2) do - begin - ReturnString[i] := Chr(RandomBuffer[i]); - end; - end else - begin - SetLength(ReturnBytes, (NBytes*2)); - ReturnBytes := MTRandomBytes((NBytes*2)); - end; - SetLength(Result, NBytes); - FreeMem(RandomBuffer, (NBytes * 2)); - if WinCrypt then - begin - Move(ReturnString[1], Result[1], NBytes); - end else - begin - Move(ReturnBytes[0], Result[1], NBytes); - end; - -end; - -end. diff --git a/Renegade.Random.pas b/Renegade.Random.pas deleted file mode 100644 index 74f19b2..0000000 --- a/Renegade.Random.pas +++ /dev/null @@ -1,116 +0,0 @@ -{*******************************************************} -{ } -{ Renegade BBS } -{ } -{ Copyright (c) 1990-2013 The Renegade Dev Team } -{ Copyleft (ↄ) 2016 Renegade BBS } -{ } -{ This file is part of Renegade BBS } -{ } -{ Renegade is free software: you can redistribute it } -{ and/or modify it under the terms of the GNU General } -{ Public License as published by the Free Software } -{ Foundation, either version 3 of the License, or } -{ (at your option) any later version. } -{ } -{ Renegade is distributed in the hope that it will be } -{ useful, but WITHOUT ANY WARRANTY; without even the } -{ implied warranty of MERCHANTABILITY or FITNESS FOR } -{ A PARTICULAR PURPOSE. See the GNU General Public } -{ License for more details. } -{ } -{ You should have received a copy of the GNU General } -{ Public License along with Renegade. If not, see } -{ . } -{ } -{*******************************************************} -{ _______ __ } -{ | _ .-----.-----.-----.-----.---.-.--| .-----. } -{ |. l | -__| | -__| _ | _ | _ | -__| } -{ |. _ |_____|__|__|_____|___ |___._|_____|_____| } -{ |: | | |_____| } -{ |::.|:. | } -{ `--- ---' } -{*******************************************************} - -{$mode objfpc}{$H+} -{$interfaces corba} -Unit Renegade.Random; - -interface - -uses - Objects, - Classes, - SysUtils, - Renegade.Random.RandomInterface, -{$IF DEFINED(LINUX)} - Renegade.Random.LinuxRandom -{$ELSEIF DEFINED(WINDOWS)} - Renegade.Random.WinRandom -{$ELSEIF DEFINED(BSD)} - Renegade.Random.BSDRandom -{$ELSE} - Renegade.Random.Generic; -{$ENDIF} - ; - -type - PRandom = ^TRandom; - TRandom = class (RandomTrait, RandomInterface) - private - FRandomGenerator : RandomInterface; - procedure SetRandomGenerator(GeneratorClass : RandomInterface); - public - constructor Init; - destructor Destroy; override; - procedure SetDefaultGenerator; - function GetBytes(NBytes : SizeUInt) : TBytes; - function GetString(NBytes : SizeUInt) : AnsiString; - property RandomGenerator : RandomInterface read FRandomGenerator write SetRandomGenerator; - end; - -implementation - - -constructor TRandom.Init; -begin - SetDefaultGenerator; -end; - -destructor TRandom.Destroy; -begin - inherited Destroy; -end; - -procedure TRandom.SetRandomGenerator(GeneratorClass : RandomInterface); -begin - FRandomGenerator := GeneratorClass; -end; - -procedure TRandom.SetDefaultGenerator; -begin - {$IF DEFINED(LINUX)} - FRandomGenerator := LinuxRandom.Create; - {$ELSEIF DEFINED(WINDOWS)} - FRandomGenerator := WinRandom.Create; - {$ELSEIF DEFINED(BSD)} - FRandomGenerator := BSDRandom.Create; - {$ELSE} - FRandomGenerator := RandomGeneric.Create; - {$ENDIF} -end; - -function TRandom.GetBytes(NBytes : SizeUInt) : TBytes; -begin - SetLength(Result, NBytes); - Result := FRandomGenerator.GetBytes(NBytes); -end; -function TRandom.GetString(NBytes : SizeUInt) : AnsiString; -begin - SetLength(Result, NBytes); - Result := FRandomGenerator.GetString(NBytes); - -end; - -end.