diff --git a/Demos/Cipher_Console/Cipher_Console.deployproj b/Demos/Cipher_Console/Cipher_Console.deployproj deleted file mode 100644 index 16a3cbf5..00000000 --- a/Demos/Cipher_Console/Cipher_Console.deployproj +++ /dev/null @@ -1,261 +0,0 @@ - - - - 12 - - - CB512EA59X - - - - - Cipher_Console\ - Cipher_Console.exe - ProjectOutput - 0 - - - True - True - - - - - - Cipher_Console\res\drawable-xxhdpi\ - ic_notification.png - Android_NotificationIcon72 - 1 - - - True - - - Cipher_Console\res\drawable-ldpi\ - ic_launcher.png - Android_LauncherIcon36 - 1 - - - True - - - Cipher_Console\ - AndroidManifest.xml - ProjectAndroidManifest - 1 - - - True - - - Cipher_Console\res\values\ - colors.xml - Android_Colors - 1 - - - True - - - Cipher_Console\res\drawable-hdpi\ - ic_launcher.png - Android_LauncherIcon72 - 1 - - - True - - - Cipher_Console\res\drawable-large\ - splash_image.png - Android_SplashImage640 - 1 - - - True - - - Cipher_Console\res\drawable-xlarge\ - splash_image.png - Android_SplashImage960 - 1 - - - True - - - Cipher_Console\res\drawable-small\ - splash_image.png - Android_SplashImage426 - 1 - - - True - - - Cipher_Console\res\values\ - styles.xml - AndroidSplashStyles - 1 - - - True - - - Cipher_Console\classes\ - classes.dex - AndroidClassesDexFile - 1 - - - True - - - Cipher_Console\res\drawable-hdpi\ - ic_notification.png - Android_NotificationIcon36 - 1 - - - True - - - Cipher_Console\res\drawable-mdpi\ - ic_launcher.png - Android_LauncherIcon48 - 1 - - - True - - - Cipher_Console\library\lib\mips\ - libCipher_Console.so - AndroidLibnativeMipsFile - 1 - - - True - - - Cipher_Console\library\lib\armeabi-v7a\ - gdbserver - AndroidGDBServer - 1 - - - True - - - Cipher_Console\library\lib\armeabi-v7a\ - libCipher_Console.so - ProjectOutput - 1 - - - True - True - - - Cipher_Console\res\values\ - strings.xml - Android_Strings - 1 - - - True - - - Cipher_Console\res\drawable-xxxhdpi\ - ic_notification.png - Android_NotificationIcon96 - 1 - - - True - - - Cipher_Console\res\drawable-xxhdpi\ - ic_launcher.png - Android_LauncherIcon144 - 1 - - - True - - - Cipher_Console\res\values-v21\ - styles.xml - AndroidSplashStylesV21 - 1 - - - True - - - Cipher_Console\library\lib\armeabi\ - libCipher_Console.so - AndroidLibnativeArmeabiFile - 1 - - - True - - - Cipher_Console\res\drawable-normal\ - splash_image.png - Android_SplashImage470 - 1 - - - True - - - Cipher_Console\res\drawable\ - splash_image_def.xml - AndroidSplashImageDef - 1 - - - True - - - Cipher_Console\res\drawable-mdpi\ - ic_notification.png - Android_NotificationIcon24 - 1 - - - True - - - Cipher_Console\res\drawable-xhdpi\ - ic_launcher.png - Android_LauncherIcon96 - 1 - - - True - - - Cipher_Console\res\drawable-xhdpi\ - ic_notification.png - Android_NotificationIcon48 - 1 - - - True - - - Cipher_Console\res\drawable-xxxhdpi\ - ic_launcher.png - Android_LauncherIcon192 - 1 - - - True - - - - diff --git a/Demos/Cipher_Console/Cipher_Console.dpr b/Demos/Cipher_Console/Cipher_Console.dpr deleted file mode 100644 index 45e89280..00000000 --- a/Demos/Cipher_Console/Cipher_Console.dpr +++ /dev/null @@ -1,96 +0,0 @@ -{***************************************************************************** - The DEC team (see file NOTICE.txt) licenses this file - to you under the Apache License, Version 2.0 (the - "License"); you may not use this file except in compliance - with the License. A copy of this licence is found in the root directory of - this project in the file LICENCE.txt or alternatively at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, - software distributed under the License is distributed on an - "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - KIND, either express or implied. See the License for the - specific language governing permissions and limitations - under the License. -*****************************************************************************} - -/// -/// Most simple demonstration of using a DEC cipher -/// -program Cipher_Console; - -{$APPTYPE CONSOLE} - -{$R *.res} - -uses - System.SysUtils, - DECCipherBase, - DECCipherModes, - DECCipherFormats, - DECCiphers; - -var - Cipher : TCipher_1DES; - // We use raw byte string here since Unicode handling of Windows console - // is not given - SourceText : RawByteString; - // Key for the initialization of our encryption run - CipherKey : RawByteString; - IV : RawByteString; - Input, - Output : TBytes; - i : Integer; -begin - Cipher := TCipher_1DES.Create; - - try - try - // Init our encryption - CipherKey := 'Passwort'; - IV := #0#0#0#0#0#0#0#0; - Cipher.Init(CipherKey, IV, 0); - Cipher.Mode := cmCBCx; - - SourceText := 'Beispielklartext'; - WriteLn('Source text: ' + SourceText); - Input := System.SysUtils.BytesOf(SourceText); - - // Encrypt - Output := Cipher.EncodeBytes(Input); - - Write('Encrypted data in hex: '); - for i := 0 to high(Output) do - Write(IntToHex(Output[i], 2), ' '); - - WriteLn; - - // Decrypt - Cipher.Init(CipherKey, IV, 0); - Output := Cipher.DecodeBytes(Output); - - SourceText := RawByteString(System.SysUtils.StringOf(Output)); - - WriteLn('Decrypted data: ' + SourceText); - - // Show that using a different key results in a different output - WriteLn; - - CipherKey := 'Password'; - Cipher.Init(CipherKey, IV, 0); - Output := Cipher.DecodeBytes(Output); - - SourceText := RawByteString(System.SysUtils.StringOf(Output)); - - WriteLn('Decrypted with different key: ' + SourceText); - - ReadLn; - except - on E: Exception do - Writeln(E.ClassName, ': ', E.Message); - end; - finally - Cipher.Free; - end; -end. diff --git a/Demos/Cipher_Console/Cipher_Console.dproj b/Demos/Cipher_Console/Cipher_Console.dproj deleted file mode 100644 index 84ee874c..00000000 --- a/Demos/Cipher_Console/Cipher_Console.dproj +++ /dev/null @@ -1,1403 +0,0 @@ - - - {BC13B21E-5E59-450C-A11D-EA58C8E05628} - 19.2 - None - Cipher_Console.dpr - True - Debug - Win32 - 3 - Console - - - true - - - true - Base - true - - - true - Base - true - - - true - Base - true - - - true - Cfg_1 - true - true - - - true - Base - true - - - .\..\..\Compiled\DCU_IDE$(ProductVersion)_$(Platform)__Demos - .\..\..\Compiled\BIN_IDExx.x_$(Platform)__Demos - .\..\..\Compiled\DCP_IDE$(ProductVersion)_$(Platform)_$(Config) - .\..\..\Compiled\DCU_IDE$(ProductVersion)_$(Platform)__Demos - .\..\..\Compiled\DCP_IDE$(ProductVersion)_$(Platform)_$(Config) - .\..\..\Compiled\DCU_IDE$(ProductVersion)_$(Platform)_$(Config);$(DCC_UnitSearchPath) - .\..\..\Compiled\BIN_IDExx.x_$(Platform)__Demos - .\..\..\Compiled\DCP_IDE$(ProductVersion)_$(Platform)_$(Config) - 1031 - CompanyName=;FileDescription=$(MSBuildProjectName);FileVersion=1.0.0.0;InternalName=;LegalCopyright=;LegalTrademarks=;OriginalFilename=;ProgramID=com.embarcadero.$(MSBuildProjectName);ProductName=$(MSBuildProjectName);ProductVersion=1.0.0.0;Comments= - Cipher_Console - - - Winapi;System.Win;Data.Win;Datasnap.Win;Web.Win;Soap.Win;Xml.Win;Bde;$(DCC_Namespace) - Debug - DBXSqliteDriver;RESTComponents;DataSnapServerMidas;DBXDb2Driver;DBXInterBaseDriver;TwoThumbTrackbarDesign;vclactnband;vclFireDAC;emsclientfiredac;DataSnapFireDAC;svnui;tethering;JvGlobus;FireDACADSDriver;JvPluginSystem;DBXMSSQLDriver;JvMM;DatasnapConnectorsFreePascal;FireDACMSSQLDriver;vcltouch;JvBands;vcldb;bindcompfmx;svn;Intraweb;DBXOracleDriver;JvJans;JvNet;inetdb;JvAppFrm;VirtualTreesDR;FmxTeeUI;emsedge;JvDotNetCtrls;FireDACIBDriver;fmx;fmxdae;TwoThumbTrackbarRuntime;JvWizards;IcsCommonD101Run;FireDACDBXDriver;dbexpress;IndyCore;vclx;JvPageComps;dsnap;DataSnapCommon;emsclient;IcsVclD101Run;FireDACCommon;fmxinfopower;JvDB;RESTBackendComponents;DataSnapConnectors;VCLRESTComponents;soapserver;JclDeveloperTools;vclie;RDesignLAB;bindengine;DBXMySQLDriver;FireDACOracleDriver;CloudService;FireDACMySQLDriver;DBXFirebirdDriver;JvCmp;JvHMI;FireDACCommonODBC;FireDACCommonDriver;DataSnapClient;inet;bindcompdbx;IndyIPCommon;JvCustom;vcl;DBXSybaseASEDriver;IndyIPServer;JvXPCtrls;IndySystem;FireDACDb2Driver;dsnapcon;FireDACMSAccDriver;fmxFireDAC;FireDACInfxDriver;vclimg;TeeDB;FireDAC;Jcl;JvCore;emshosting;IcsFmxD101Run;JvCrypt;FireDACSqliteDriver;FireDACPgDriver;FireDACASADriver;DBXOdbcDriver;FireDACTDataDriver;FMXTee;soaprtl;DbxCommonDriver;JvDlgs;JvRuntimeDesign;JvManagedThreads;Tee;DataSnapServer;xmlrtl;soapmidas;DataSnapNativeClient;fmxobj;vclwinx;FireDACDSDriver;rtl;JvTimeFramework;DbxClientDriver;DBXSybaseASADriver;CustomIPTransport;vcldsnap;CodeSiteExpressPkg;DOSCommandDR;JvSystem;JvStdCtrls;TMSFMXPackPkgDXE10;bindcomp;appanalytics;DBXInformixDriver;IndyIPClient;bindcompvcl;TeeUI;JvDocking;dbxcds;VclSmp;JvPascalInterpreter;adortl;FireDACODBCDriver;JclVcl;DataSnapIndy10ServerTransport;dsnapxml;DataSnapProviderClient;dbrtl;inetdbxpress;FireDACMongoDBDriver;IndyProtocols;JvControls;JvPrintPreview;TMSFMXPackPkgDEDXE10;JclContainers;PowerPDFDR;fmxase;$(DCC_UsePackage) - true - CompanyName=;FileDescription=$(MSBuildProjectName);FileVersion=1.0.0.0;InternalName=;LegalCopyright=;LegalTrademarks=;OriginalFilename=;ProgramID=com.embarcadero.$(MSBuildProjectName);ProductName=$(MSBuildProjectName);ProductVersion=1.0.0.0;Comments= - 1033 - $(BDS)\bin\Artwork\Windows\UWP\delphi_UwpDefault_150.png - $(BDS)\bin\Artwork\Windows\UWP\delphi_UwpDefault_44.png - - - true - DBXSqliteDriver;RESTComponents;DataSnapServerMidas;DBXDb2Driver;DBXInterBaseDriver;vclactnband;vclFireDAC;emsclientfiredac;DataSnapFireDAC;tethering;FireDACADSDriver;DBXMSSQLDriver;DatasnapConnectorsFreePascal;FireDACMSSQLDriver;vcltouch;vcldb;bindcompfmx;Intraweb;DBXOracleDriver;inetdb;VirtualTreesDR;FmxTeeUI;emsedge;FireDACIBDriver;fmx;fmxdae;IcsCommonD101Run;FireDACDBXDriver;dbexpress;IndyCore;vclx;dsnap;DataSnapCommon;emsclient;IcsVclD101Run;FireDACCommon;RESTBackendComponents;DataSnapConnectors;VCLRESTComponents;soapserver;vclie;bindengine;DBXMySQLDriver;FireDACOracleDriver;CloudService;FireDACMySQLDriver;DBXFirebirdDriver;FireDACCommonODBC;FireDACCommonDriver;DataSnapClient;inet;bindcompdbx;IndyIPCommon;vcl;DBXSybaseASEDriver;IndyIPServer;IndySystem;FireDACDb2Driver;dsnapcon;FireDACMSAccDriver;fmxFireDAC;FireDACInfxDriver;vclimg;TeeDB;FireDAC;emshosting;IcsFmxD101Run;FireDACSqliteDriver;FireDACPgDriver;FireDACASADriver;DBXOdbcDriver;FireDACTDataDriver;FMXTee;soaprtl;DbxCommonDriver;Tee;DataSnapServer;xmlrtl;soapmidas;DataSnapNativeClient;fmxobj;vclwinx;FireDACDSDriver;rtl;DbxClientDriver;DBXSybaseASADriver;CustomIPTransport;vcldsnap;DOSCommandDR;bindcomp;appanalytics;DBXInformixDriver;IndyIPClient;bindcompvcl;TeeUI;dbxcds;VclSmp;adortl;FireDACODBCDriver;DataSnapIndy10ServerTransport;dsnapxml;DataSnapProviderClient;dbrtl;inetdbxpress;FireDACMongoDBDriver;IndyProtocols;PowerPDFDR;fmxase;$(DCC_UsePackage) - $(BDS)\bin\Artwork\Windows\UWP\delphi_UwpDefault_44.png - $(BDS)\bin\Artwork\Windows\UWP\delphi_UwpDefault_150.png - Winapi;System.Win;Data.Win;Datasnap.Win;Web.Win;Soap.Win;Xml.Win;$(DCC_Namespace) - Debug - 1033 - - - DEBUG;$(DCC_Define) - false - true - true - true - - - false - 1033 - (Ohne) - - - false - RELEASE;$(DCC_Define) - 0 - 0 - - - - MainSource - - - Cfg_2 - Base - - - Base - - - Cfg_1 - Base - - - - Delphi.Personality.12 - Application - - - - Cipher_Console.dpr - - - - - - ic_notification.png - true - - - - - ic_launcher.png - true - - - - - true - - - - - true - - - - - ic_launcher.png - true - - - - - splash_image.png - true - - - - - splash_image.png - true - - - - - splash_image.png - true - - - - - true - - - - - classes.dex - true - - - - - ic_notification.png - true - - - - - ic_launcher.png - true - - - - - true - - - - - libCipher_Console.so - true - - - - - true - - - - - libCipher_Console.so - true - - - - - true - - - - - Cipher_Console.exe - true - - - - - ic_notification.png - true - - - - - ic_launcher.png - true - - - - - styles.xml - true - - - - - libCipher_Console.so - true - - - - - true - - - - - true - - - - - true - - - - - splash_image.png - true - - - - - true - - - - - ic_notification.png - true - - - - - true - - - - - ic_launcher.png - true - - - - - ic_notification.png - true - - - - - ic_launcher.png - true - - - - - true - - - - - 1 - - - Contents\MacOS - 1 - - - 0 - - - - - classes - 1 - - - classes - 1 - - - - - res\xml - 1 - - - res\xml - 1 - - - - - library\lib\armeabi-v7a - 1 - - - - - library\lib\armeabi - 1 - - - library\lib\armeabi - 1 - - - - - library\lib\armeabi-v7a - 1 - - - - - library\lib\mips - 1 - - - library\lib\mips - 1 - - - - - library\lib\armeabi-v7a - 1 - - - library\lib\arm64-v8a - 1 - - - - - library\lib\armeabi-v7a - 1 - - - - - res\drawable - 1 - - - res\drawable - 1 - - - - - res\values - 1 - - - res\values - 1 - - - - - res\values-v21 - 1 - - - res\values-v21 - 1 - - - - - res\values - 1 - - - res\values - 1 - - - - - res\drawable - 1 - - - res\drawable - 1 - - - - - res\drawable-xxhdpi - 1 - - - res\drawable-xxhdpi - 1 - - - - - res\drawable-xxxhdpi - 1 - - - res\drawable-xxxhdpi - 1 - - - - - res\drawable-ldpi - 1 - - - res\drawable-ldpi - 1 - - - - - res\drawable-mdpi - 1 - - - res\drawable-mdpi - 1 - - - - - res\drawable-hdpi - 1 - - - res\drawable-hdpi - 1 - - - - - res\drawable-xhdpi - 1 - - - res\drawable-xhdpi - 1 - - - - - res\drawable-mdpi - 1 - - - res\drawable-mdpi - 1 - - - - - res\drawable-hdpi - 1 - - - res\drawable-hdpi - 1 - - - - - res\drawable-xhdpi - 1 - - - res\drawable-xhdpi - 1 - - - - - res\drawable-xxhdpi - 1 - - - res\drawable-xxhdpi - 1 - - - - - res\drawable-xxxhdpi - 1 - - - res\drawable-xxxhdpi - 1 - - - - - res\drawable-small - 1 - - - res\drawable-small - 1 - - - - - res\drawable-normal - 1 - - - res\drawable-normal - 1 - - - - - res\drawable-large - 1 - - - res\drawable-large - 1 - - - - - res\drawable-xlarge - 1 - - - res\drawable-xlarge - 1 - - - - - res\values - 1 - - - res\values - 1 - - - - - 1 - - - Contents\MacOS - 1 - - - 0 - - - - - Contents\MacOS - 1 - .framework - - - Contents\MacOS - 1 - .framework - - - 0 - - - - - 1 - .dylib - - - 1 - .dylib - - - 1 - .dylib - - - Contents\MacOS - 1 - .dylib - - - Contents\MacOS - 1 - .dylib - - - 0 - .dll;.bpl - - - - - 1 - .dylib - - - 1 - .dylib - - - 1 - .dylib - - - Contents\MacOS - 1 - .dylib - - - Contents\MacOS - 1 - .dylib - - - 0 - .bpl - - - - - 0 - - - 0 - - - 0 - - - 0 - - - 0 - - - Contents\Resources\StartUp\ - 0 - - - Contents\Resources\StartUp\ - 0 - - - 0 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset - 1 - - - - - 1 - - - 1 - - - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - 1 - - - 1 - - - - - ..\$(PROJECTNAME).app.dSYM\Contents\Resources\DWARF - 1 - - - ..\$(PROJECTNAME).app.dSYM\Contents\Resources\DWARF - 1 - - - - - 1 - - - 1 - - - - - ..\ - 1 - - - ..\ - 1 - - - - - 1 - - - 1 - - - 1 - - - - - ..\$(PROJECTNAME).launchscreen - 64 - - - ..\$(PROJECTNAME).launchscreen - 64 - - - - - 1 - - - 1 - - - 1 - - - - - ..\$(PROJECTNAME).app.dSYM\Contents\Resources\DWARF - 1 - - - - - ..\ - 1 - - - ..\ - 1 - - - - - Contents - 1 - - - Contents - 1 - - - - - Contents\Resources - 1 - - - Contents\Resources - 1 - - - - - library\lib\armeabi-v7a - 1 - - - library\lib\arm64-v8a - 1 - - - 1 - - - 1 - - - 1 - - - 1 - - - Contents\MacOS - 1 - - - Contents\MacOS - 1 - - - 0 - - - - - library\lib\armeabi-v7a - 1 - - - - - 1 - - - 1 - - - - - Assets - 1 - - - Assets - 1 - - - - - Assets - 1 - - - Assets - 1 - - - - - - - - - - - - - - - False - False - True - True - - - 12 - - - - - diff --git a/Demos/Cipher_Console/Cipher_Console.res b/Demos/Cipher_Console/Cipher_Console.res deleted file mode 100644 index 36f26e23..00000000 Binary files a/Demos/Cipher_Console/Cipher_Console.res and /dev/null differ diff --git a/Demos/Cipher_Console/Cipher_Console_project.tvsconfig b/Demos/Cipher_Console/Cipher_Console_project.tvsconfig deleted file mode 100644 index d4307801..00000000 --- a/Demos/Cipher_Console/Cipher_Console_project.tvsconfig +++ /dev/null @@ -1,2 +0,0 @@ - - \ No newline at end of file diff --git a/Demos/Cipher_FMX/AndroidManifest.template.xml b/Demos/Cipher_FMX/AndroidManifest.template.xml deleted file mode 100644 index b5936845..00000000 --- a/Demos/Cipher_FMX/AndroidManifest.template.xml +++ /dev/null @@ -1,42 +0,0 @@ - - - - - - -<%uses-permission%> - - - -<%application-meta-data%> - <%services%> - - - - - - - - - - <%activity%> - <%receivers%> - - - diff --git a/Demos/Cipher_FMX/Cipher_FMX.deployproj b/Demos/Cipher_FMX/Cipher_FMX.deployproj deleted file mode 100644 index ed3ae190..00000000 --- a/Demos/Cipher_FMX/Cipher_FMX.deployproj +++ /dev/null @@ -1,434 +0,0 @@ - - - - 12 - - - CB512EA59X - - - - - Cipher_FMX\ - Cipher_FMX.exe - ProjectOutput - 0 - - - True - True - - - - - - Cipher_FMX\res\drawable-mdpi\ - ic_launcher.png - Android_LauncherIcon48 - 1 - - - True - - - Cipher_FMX\classes\ - classes.dex - AndroidClassesDexFile - 1 - - - True - - - Cipher_FMX\res\drawable-hdpi\ - ic_notification.png - Android_NotificationIcon36 - 1 - - - True - - - Cipher_FMX\library\lib\mips\ - libCipher_FMX.so - AndroidLibnativeMipsFile - 1 - - - True - - - Cipher_FMX\res\drawable\ - splash_image_def.xml - AndroidSplashImageDef - 1 - - - True - - - Cipher_FMX\res\drawable-xhdpi\ - ic_launcher.png - Android_LauncherIcon96 - 1 - - - True - - - Cipher_FMX\res\drawable-ldpi\ - ic_launcher.png - Android_LauncherIcon36 - 1 - - - True - - - Cipher_FMX\res\drawable-large\ - splash_image.png - Android_SplashImage640 - 1 - - - True - - - Cipher_FMX\res\values\ - strings.xml - Android_Strings - 1 - - - True - - - Cipher_FMX\res\drawable-xxxhdpi\ - ic_notification.png - Android_NotificationIcon96 - 1 - - - True - - - Cipher_FMX\ - AndroidManifest.xml - ProjectAndroidManifest - 1 - - - True - - - Cipher_FMX\library\lib\armeabi\ - libCipher_FMX.so - AndroidLibnativeArmeabiFile - 1 - - - True - - - Cipher_FMX\res\values-v21\ - styles.xml - AndroidSplashStylesV21 - 1 - - - True - - - Cipher_FMX\res\drawable-mdpi\ - ic_notification.png - Android_NotificationIcon24 - 1 - - - True - - - Cipher_FMX\res\drawable-normal\ - splash_image.png - Android_SplashImage470 - 1 - - - True - - - Cipher_FMX\res\drawable-xhdpi\ - ic_notification.png - Android_NotificationIcon48 - 1 - - - True - - - Cipher_FMX\res\drawable-hdpi\ - ic_launcher.png - Android_LauncherIcon72 - 1 - - - True - - - Cipher_FMX\res\drawable-xxhdpi\ - ic_notification.png - Android_NotificationIcon72 - 1 - - - True - - - Cipher_FMX\res\drawable-xxhdpi\ - ic_launcher.png - Android_LauncherIcon144 - 1 - - - True - - - Cipher_FMX\res\values\ - colors.xml - Android_Colors - 1 - - - True - - - Cipher_FMX\res\drawable-xxxhdpi\ - ic_launcher.png - Android_LauncherIcon192 - 1 - - - True - - - Cipher_FMX\library\lib\armeabi-v7a\ - libCipher_FMX.so - ProjectOutput - 1 - - - True - True - - - Cipher_FMX\res\drawable-small\ - splash_image.png - Android_SplashImage426 - 1 - - - True - - - Cipher_FMX\classes\ - classes.dex - AndroidClassesDexFile - 1 - - - True - - - Cipher_FMX\library\lib\armeabi-v7a\ - gdbserver - AndroidGDBServer - 1 - - - True - - - Cipher_FMX\res\values\ - styles.xml - AndroidSplashStyles - 1 - - - True - - - Cipher_FMX\res\drawable-xlarge\ - splash_image.png - Android_SplashImage960 - 1 - - - True - - - - - Cipher_FMX\res\drawable\ - splash_image_def.xml - AndroidSplashImageDef - 1 - - - True - - - Cipher_FMX\library\lib\arm64-v8a\ - libCipher_FMX.so - ProjectOutput - 1 - - - True - True - - - Cipher_FMX\classes\ - classes.dex - AndroidClassesDexFile - 1 - - - True - - - Cipher_FMX\res\drawable-xlarge\ - splash_image.png - Android_SplashImage960 - 1 - - - True - - - Cipher_FMX\res\values\ - styles.xml - AndroidSplashStyles - 1 - - - True - - - Cipher_FMX\res\drawable-mdpi\ - ic_launcher.png - Android_LauncherIcon48 - 1 - - - True - - - Cipher_FMX\res\drawable-small\ - splash_image.png - Android_SplashImage426 - 1 - - - True - - - Cipher_FMX\ - AndroidManifest.xml - ProjectAndroidManifest - 1 - - - True - - - Cipher_FMX\res\drawable-normal\ - splash_image.png - Android_SplashImage470 - 1 - - - True - - - Cipher_FMX\res\drawable-large\ - splash_image.png - Android_SplashImage640 - 1 - - - True - - - Cipher_FMX\res\drawable-xhdpi\ - ic_launcher.png - Android_LauncherIcon96 - 1 - - - True - - - Cipher_FMX\res\drawable-xxxhdpi\ - ic_launcher.png - Android_LauncherIcon192 - 1 - - - True - - - Cipher_FMX\res\drawable-ldpi\ - ic_launcher.png - Android_LauncherIcon36 - 1 - - - True - - - Cipher_FMX\res\values-v21\ - styles.xml - AndroidSplashStylesV21 - 1 - - - True - - - Cipher_FMX\res\values\ - colors.xml - Android_Colors - 1 - - - True - - - Cipher_FMX\res\drawable-hdpi\ - ic_launcher.png - Android_LauncherIcon72 - 1 - - - True - - - Cipher_FMX\res\drawable-xxhdpi\ - ic_launcher.png - Android_LauncherIcon144 - 1 - - - True - - - Cipher_FMX\res\values\ - strings.xml - Android_Strings - 1 - - - True - - - diff --git a/Demos/Cipher_FMX/Cipher_FMX.dpr b/Demos/Cipher_FMX/Cipher_FMX.dpr deleted file mode 100644 index 2344c78f..00000000 --- a/Demos/Cipher_FMX/Cipher_FMX.dpr +++ /dev/null @@ -1,14 +0,0 @@ -program Cipher_FMX; - -uses - System.StartUpCopy, - FMX.Forms, - MainForm in 'MainForm.pas' {FormMain}; - -{$R *.res} - -begin - Application.Initialize; - Application.CreateForm(TFormMain, FormMain); - Application.Run; -end. diff --git a/Demos/Cipher_FMX/Cipher_FMX.dproj b/Demos/Cipher_FMX/Cipher_FMX.dproj deleted file mode 100644 index 79f33e53..00000000 --- a/Demos/Cipher_FMX/Cipher_FMX.dproj +++ /dev/null @@ -1,1677 +0,0 @@ - - - {F90DC8C6-DC04-46FE-8D5C-2260589FBEF1} - 19.2 - FMX - Cipher_FMX.dpr - True - Debug - Win32 - 32787 - Application - AndroidSDK25.2.5_64bit.sdk - - - true - - - true - Base - true - - - true - Base - true - - - true - Base - true - - - true - Base - true - - - true - Base - true - - - true - Cfg_1 - true - true - - - true - Cfg_1 - true - true - - - true - Cfg_1 - true - true - - - true - Cfg_1 - true - true - - - true - Base - true - - - true - Cfg_2 - true - true - - - true - Cfg_2 - true - true - - - true - Cfg_2 - true - true - - - true - Cfg_2 - true - true - - - .\..\..\Compiled\DCU_IDE$(ProductVersion)_$(Platform)__Demos - .\..\..\Compiled\BIN_IDExx.x_$(Platform)__Demos - .\..\..\Compiled\DCP_IDE$(ProductVersion)_$(Platform)_$(Config) - .\..\..\Compiled\DCU_IDE$(ProductVersion)_$(Platform)__Demos - .\..\..\Compiled\DCP_IDE$(ProductVersion)_$(Platform)_$(Config) - .\..\..\Compiled\DCU_IDE$(ProductVersion)_$(Platform)_$(Config);$(DCC_UnitSearchPath) - .\..\..\Compiled\BIN_IDExx.x_$(Platform)__Demos - .\..\..\Compiled\DCP_IDE$(ProductVersion)_$(Platform)_$(Config) - Cipher_FMX - - - fill_horizontal - 3 - CryptoIcon_36.png - Crypto_470_320.png - CryptoIcon_96.png - DBXSqliteDriver;RESTComponents;DBXInterBaseDriver;emsclientfiredac;DataSnapFireDAC;tethering;bindcompfmx;FmxTeeUI;FireDACIBDriver;fmx;FireDACDBXDriver;dbexpress;IndyCore;dsnap;DataSnapCommon;emsclient;FireDACCommon;RESTBackendComponents;soapserver;RDesignLAB;bindengine;CloudService;FireDACCommonDriver;DataSnapClient;inet;bindcompdbx;IndyIPCommon;IndyIPServer;IndySystem;fmxFireDAC;FireDAC;FireDACSqliteDriver;FMXTee;soaprtl;DbxCommonDriver;xmlrtl;soapmidas;DataSnapNativeClient;FireDACDSDriver;rtl;DbxClientDriver;CustomIPTransport;bindcomp;IndyIPClient;dbxcds;dsnapxml;DataSnapProviderClient;dbrtl;IndyProtocols;$(DCC_UsePackage) - true - CryptoIcon_48.png - android-support-v4.dex.jar;cloud-messaging.dex.jar;fmx.dex.jar;google-analytics-v2.dex.jar;google-play-billing.dex.jar;google-play-licensing.dex.jar;google-play-services.dex.jar - CryptoIcon_144.png - Crypto_640_480.png - Crypto_426_320.png - CryptoIcon_72.png - Crypto_960_720.png - package=com.dec.Cipher_FMX;label=DEC cipher demo;versionCode=3;versionName=1.2.0;persistent=False;restoreAnyVersion=False;installLocation=auto;largeHeap=False;theme=TitleBar;hardwareAccelerated=true;apiKey= - Debug - $(BDS)\bin\Artwork\Android\FM_NotificationIcon_24x24.png - $(BDS)\bin\Artwork\Android\FM_NotificationIcon_36x36.png - $(BDS)\bin\Artwork\Android\FM_NotificationIcon_48x48.png - $(BDS)\bin\Artwork\Android\FM_NotificationIcon_72x72.png - $(BDS)\bin\Artwork\Android\FM_NotificationIcon_96x96.png - CryptoIcon_192.png - #000000 - ..\..\..\AndroidAppKeystore\mh_Keystore.keystore - 850847C65F90078DCF7DE530A4627BB3E44F08447EB9ECFA9F547732D85CA8FFDB06A389E3001C4DC1B85774FAD1C339BAFD36020437228116714592E5A6228DC953F307D56AFAE2BFAAAF1007411D1B92433E551DCB0925E3F016B44439788F - - - package=com.dec.Cipher_FMX;label=DEC cipher demo;versionCode=3;versionName=1.2.0;persistent=False;restoreAnyVersion=False;installLocation=auto;largeHeap=False;theme=TitleBar;hardwareAccelerated=true;apiKey= - Debug - true - true - Base - true - fill_horizontal - 3 - CryptoIcon_36.png - Crypto_470_320.png - CryptoIcon_96.png - DBXSqliteDriver;RESTComponents;DBXInterBaseDriver;emsclientfiredac;DataSnapFireDAC;tethering;bindcompfmx;FmxTeeUI;FireDACIBDriver;fmx;FireDACDBXDriver;dbexpress;IndyCore;dsnap;DataSnapCommon;emsclient;FireDACCommon;RESTBackendComponents;soapserver;RDesignLAB;bindengine;CloudService;FireDACCommonDriver;DataSnapClient;inet;bindcompdbx;IndyIPCommon;IndyIPServer;IndySystem;fmxFireDAC;FireDAC;FireDACSqliteDriver;FMXTee;soaprtl;DbxCommonDriver;xmlrtl;soapmidas;DataSnapNativeClient;FireDACDSDriver;rtl;DbxClientDriver;CustomIPTransport;bindcomp;IndyIPClient;dbxcds;dsnapxml;DataSnapProviderClient;dbrtl;IndyProtocols;$(DCC_UsePackage);$(DCC_UsePackage) - CryptoIcon_48.png - android-support-v4.dex.jar;cloud-messaging.dex.jar;fmx.dex.jar;google-analytics-v2.dex.jar;google-play-billing.dex.jar;google-play-licensing.dex.jar;google-play-services.dex.jar - CryptoIcon_144.png - Crypto_640_480.png - Crypto_426_320.png - CryptoIcon_72.png - Crypto_960_720.png - CryptoIcon_192.png - true - #000000 - ..\..\..\AndroidAppKeystore\mh_Keystore.keystore - 850847C65F90078DCF7DE530A4627BB3E44F08447EB9ECFA9F547732D85CA8FFDB06A389E3001C4DC1B85774FAD1C339BAFD36020437228116714592E5A6228DC953F307D56AFAE2BFAAAF1007411D1B92433E551DCB0925E3F016B44439788F - - - $(BDS)\bin\Artwork\Windows\UWP\delphi_UwpDefault_150.png - $(BDS)\bin\Artwork\Windows\UWP\delphi_UwpDefault_44.png - true - DBXSqliteDriver;RESTComponents;DataSnapServerMidas;DBXDb2Driver;DBXInterBaseDriver;TwoThumbTrackbarDesign;vclactnband;vclFireDAC;emsclientfiredac;DataSnapFireDAC;svnui;tethering;JvGlobus;FireDACADSDriver;JvPluginSystem;DBXMSSQLDriver;JvMM;DatasnapConnectorsFreePascal;FireDACMSSQLDriver;vcltouch;JvBands;vcldb;bindcompfmx;svn;Intraweb;DBXOracleDriver;JvJans;JvNet;inetdb;JvAppFrm;VirtualTreesDR;FmxTeeUI;emsedge;JvDotNetCtrls;FireDACIBDriver;fmx;fmxdae;TwoThumbTrackbarRuntime;JvWizards;IcsCommonD101Run;FireDACDBXDriver;dbexpress;IndyCore;vclx;JvPageComps;dsnap;DataSnapCommon;emsclient;IcsVclD101Run;FireDACCommon;fmxinfopower;JvDB;RESTBackendComponents;DataSnapConnectors;VCLRESTComponents;soapserver;JclDeveloperTools;vclie;RDesignLAB;bindengine;DBXMySQLDriver;FireDACOracleDriver;CloudService;FireDACMySQLDriver;DBXFirebirdDriver;JvCmp;JvHMI;FireDACCommonODBC;FireDACCommonDriver;DataSnapClient;inet;bindcompdbx;IndyIPCommon;JvCustom;vcl;DBXSybaseASEDriver;IndyIPServer;JvXPCtrls;IndySystem;FireDACDb2Driver;dsnapcon;FireDACMSAccDriver;fmxFireDAC;FireDACInfxDriver;vclimg;TeeDB;FireDAC;Jcl;JvCore;emshosting;IcsFmxD101Run;JvCrypt;FireDACSqliteDriver;FireDACPgDriver;FireDACASADriver;DBXOdbcDriver;FireDACTDataDriver;FMXTee;soaprtl;DbxCommonDriver;JvDlgs;JvRuntimeDesign;JvManagedThreads;Tee;DataSnapServer;xmlrtl;soapmidas;DataSnapNativeClient;fmxobj;vclwinx;FireDACDSDriver;rtl;JvTimeFramework;DbxClientDriver;DBXSybaseASADriver;CustomIPTransport;vcldsnap;CodeSiteExpressPkg;DOSCommandDR;JvSystem;JvStdCtrls;TMSFMXPackPkgDXE10;bindcomp;appanalytics;DBXInformixDriver;IndyIPClient;bindcompvcl;TeeUI;JvDocking;dbxcds;VclSmp;JvPascalInterpreter;adortl;FireDACODBCDriver;JclVcl;DataSnapIndy10ServerTransport;dsnapxml;DataSnapProviderClient;dbrtl;inetdbxpress;FireDACMongoDBDriver;IndyProtocols;JvControls;JvPrintPreview;TMSFMXPackPkgDEDXE10;JclContainers;PowerPDFDR;fmxase;$(DCC_UsePackage) - 1033 - $(BDS)\bin\default_app.manifest - Debug - Winapi;System.Win;Data.Win;Datasnap.Win;Web.Win;Soap.Win;Xml.Win;Bde;$(DCC_Namespace) - CompanyName=Team DEC;FileDescription=$(MSBuildProjectName);FileVersion=1.2.0.0;InternalName=;LegalCopyright=© 2018-2021 Team DEC;LegalTrademarks=;OriginalFilename=;ProgramID=com.embarcadero.$(MSBuildProjectName);ProductName=$(MSBuildProjectName);ProductVersion=1.2.0.0;Comments=Delphi Encryption Compendium demo application - $(BDS)\bin\delphi_PROJECTICON.ico - 2 - - - $(BDS)\bin\Artwork\Windows\UWP\delphi_UwpDefault_150.png - $(BDS)\bin\Artwork\Windows\UWP\delphi_UwpDefault_44.png - true - DBXSqliteDriver;RESTComponents;DataSnapServerMidas;DBXDb2Driver;DBXInterBaseDriver;TwoThumbTrackbarDesign;vclactnband;vclFireDAC;emsclientfiredac;DataSnapFireDAC;svnui;tethering;JvGlobus;FireDACADSDriver;JvPluginSystem;DBXMSSQLDriver;JvMM;DatasnapConnectorsFreePascal;FireDACMSSQLDriver;vcltouch;JvBands;vcldb;bindcompfmx;svn;Intraweb;DBXOracleDriver;JvJans;JvNet;inetdb;JvAppFrm;VirtualTreesDR;FmxTeeUI;emsedge;JvDotNetCtrls;FireDACIBDriver;fmx;fmxdae;TwoThumbTrackbarRuntime;JvWizards;IcsCommonD101Run;FireDACDBXDriver;dbexpress;IndyCore;vclx;JvPageComps;dsnap;DataSnapCommon;emsclient;IcsVclD101Run;FireDACCommon;fmxinfopower;JvDB;RESTBackendComponents;DataSnapConnectors;VCLRESTComponents;soapserver;JclDeveloperTools;vclie;RDesignLAB;bindengine;DBXMySQLDriver;FireDACOracleDriver;CloudService;FireDACMySQLDriver;DBXFirebirdDriver;JvCmp;JvHMI;FireDACCommonODBC;FireDACCommonDriver;DataSnapClient;inet;bindcompdbx;IndyIPCommon;JvCustom;vcl;DBXSybaseASEDriver;IndyIPServer;JvXPCtrls;IndySystem;FireDACDb2Driver;dsnapcon;FireDACMSAccDriver;fmxFireDAC;FireDACInfxDriver;vclimg;TeeDB;FireDAC;Jcl;JvCore;emshosting;IcsFmxD101Run;JvCrypt;FireDACSqliteDriver;FireDACPgDriver;FireDACASADriver;DBXOdbcDriver;FireDACTDataDriver;FMXTee;soaprtl;DbxCommonDriver;JvDlgs;JvRuntimeDesign;JvManagedThreads;Tee;DataSnapServer;xmlrtl;soapmidas;DataSnapNativeClient;fmxobj;vclwinx;FireDACDSDriver;rtl;JvTimeFramework;DbxClientDriver;DBXSybaseASADriver;CustomIPTransport;vcldsnap;CodeSiteExpressPkg;DOSCommandDR;JvSystem;JvStdCtrls;TMSFMXPackPkgDXE10;bindcomp;appanalytics;DBXInformixDriver;IndyIPClient;bindcompvcl;TeeUI;JvDocking;dbxcds;VclSmp;JvPascalInterpreter;adortl;FireDACODBCDriver;JclVcl;DataSnapIndy10ServerTransport;dsnapxml;DataSnapProviderClient;dbrtl;inetdbxpress;FireDACMongoDBDriver;IndyProtocols;JvControls;JvPrintPreview;TMSFMXPackPkgDEDXE10;JclContainers;PowerPDFDR;fmxase;$(DCC_UsePackage) - 1033 - $(BDS)\bin\default_app.manifest - Debug - Winapi;System.Win;Data.Win;Datasnap.Win;Web.Win;Soap.Win;Xml.Win;Bde;$(DCC_Namespace) - CompanyName=Team DEC;FileDescription=$(MSBuildProjectName);FileVersion=1.2.0.0;InternalName=;LegalCopyright=© 2018-2021 Team DEC;LegalTrademarks=;OriginalFilename=;ProgramID=com.embarcadero.$(MSBuildProjectName);ProductName=$(MSBuildProjectName);ProductVersion=1.2.0.0;Comments=Delphi Encryption Compendium demo application - $(BDS)\bin\delphi_PROJECTICON.ico - 2 - - - DEBUG;$(DCC_Define) - false - true - true - true - - - package=com.DEC.$(MSBuildProjectName);label=DEC cipher demo;versionCode=3;versionName=1.2.0;persistent=False;restoreAnyVersion=False;installLocation=auto;largeHeap=False;theme=TitleBar;hardwareAccelerated=true;apiKey= - 3 - -cleaninstall - - - true - Cfg_1 - true - package=com.DEC.$(MSBuildProjectName);label=DEC cipher demo;versionCode=3;versionName=1.2.0;persistent=False;restoreAnyVersion=False;installLocation=auto;largeHeap=False;theme=TitleBar;hardwareAccelerated=true;apiKey= - 3 - - - $(BDS)\bin\Artwork\Windows\UWP\delphi_UwpDefault_150.png - $(BDS)\bin\Artwork\Windows\UWP\delphi_UwpDefault_44.png - true - 1033 - Debug - 2 - - - true - PerMonitor - true - 1033 - 2 - - - false - RELEASE;$(DCC_Define) - 0 - 0 - - - 3 - mh - 850847C65F90078DCF7DE530A4627BB3E44F08447EB9EC899F08776AD805A8ADA5B85D4B7F2AE2278730577EFAC2C320BA95360A046D229E16794598E5F422DEC959F355D508FACCBFF9AF4807581D76922F3E161DF8097AE3AF16A1442878BD65819A9822A67D524670 - - - 3 - mh - 850847C65F90078DCF7DE530A4627BB3E44F08447EB9EC899F08776AD805A8ADA5B85D4B7F2AE2278730577EFAC2C320BA95360A046D229E16794598E5F422DEC959F355D508FACCBFF9AF4807581D76922F3E161DF8097AE3AF16A1442878BD65819A9822A67D524670 - - - true - PerMonitor - true - 2 - 1033 - - - true - PerMonitor - true - 2 - 1033 - - - - MainSource - - -
FormMain
- fmx -
- - Cfg_2 - Base - - - Base - - - Cfg_1 - Base - -
- - Delphi.Personality.12 - Application - - - - Cipher_FMX.dpr - - - Microsoft Office 2000 Beispiele für gekapselte Komponenten für Automatisierungsserver - Microsoft Office XP Beispiele für gekapselte Komponenten für Automation Server - - - - - - true - - - - - ic_launcher.png - true - - - - - libCipher_FMX.so - true - - - - - true - - - - - classes.dex - true - - - - - true - - - - - splash_image.png - true - - - - - classes.dex - true - - - - - true - - - - - ic_notification.png - true - - - - - ic_launcher.png - true - - - - - libCipher_FMX.so - true - - - - - splash_image.png - true - - - - - libCipher_FMX.so - true - - - - - true - - - - - ic_launcher.png - true - - - - - ic_launcher.png - true - - - - - splash_image.png - true - - - - - ic_notification.png - true - - - - - libCipher_FMX.so - true - - - - - styles.xml - true - - - - - true - - - - - true - - - - - true - - - - - ic_notification.png - true - - - - - true - - - - - splash_image.png - true - - - - - splash_image.png - true - - - - - ic_notification.png - true - - - - - splash_image.png - true - - - - - ic_launcher.png - true - - - - - ic_launcher.png - true - - - - - ic_launcher.png - true - - - - - ic_notification.png - true - - - - - ic_launcher.png - true - - - - - ic_launcher.png - true - - - - - styles.xml - true - - - - - true - - - - - ic_launcher.png - true - - - - - true - - - - - ic_launcher.png - true - - - - - splash_image.png - true - - - - - true - - - - - classes.dex - true - - - - - true - - - - - splash_image.png - true - - - - - ic_launcher.png - true - - - - - true - - - - - true - - - - - Cipher_FMX.exe - true - - - - - true - - - - - true - - - - - 1 - - - Contents\MacOS - 1 - - - 0 - - - - - classes - 1 - - - classes - 1 - - - - - res\xml - 1 - - - res\xml - 1 - - - - - library\lib\armeabi-v7a - 1 - - - - - library\lib\armeabi - 1 - - - library\lib\armeabi - 1 - - - - - library\lib\armeabi-v7a - 1 - - - - - library\lib\mips - 1 - - - library\lib\mips - 1 - - - - - library\lib\armeabi-v7a - 1 - - - library\lib\arm64-v8a - 1 - - - - - library\lib\armeabi-v7a - 1 - - - - - res\drawable - 1 - - - res\drawable - 1 - - - - - res\values - 1 - - - res\values - 1 - - - - - res\values-v21 - 1 - - - res\values-v21 - 1 - - - - - res\values - 1 - - - res\values - 1 - - - - - res\drawable - 1 - - - res\drawable - 1 - - - - - res\drawable-xxhdpi - 1 - - - res\drawable-xxhdpi - 1 - - - - - res\drawable-xxxhdpi - 1 - - - res\drawable-xxxhdpi - 1 - - - - - res\drawable-ldpi - 1 - - - res\drawable-ldpi - 1 - - - - - res\drawable-mdpi - 1 - - - res\drawable-mdpi - 1 - - - - - res\drawable-hdpi - 1 - - - res\drawable-hdpi - 1 - - - - - res\drawable-xhdpi - 1 - - - res\drawable-xhdpi - 1 - - - - - res\drawable-mdpi - 1 - - - res\drawable-mdpi - 1 - - - - - res\drawable-hdpi - 1 - - - res\drawable-hdpi - 1 - - - - - res\drawable-xhdpi - 1 - - - res\drawable-xhdpi - 1 - - - - - res\drawable-xxhdpi - 1 - - - res\drawable-xxhdpi - 1 - - - - - res\drawable-xxxhdpi - 1 - - - res\drawable-xxxhdpi - 1 - - - - - res\drawable-small - 1 - - - res\drawable-small - 1 - - - - - res\drawable-normal - 1 - - - res\drawable-normal - 1 - - - - - res\drawable-large - 1 - - - res\drawable-large - 1 - - - - - res\drawable-xlarge - 1 - - - res\drawable-xlarge - 1 - - - - - res\values - 1 - - - res\values - 1 - - - - - 1 - - - Contents\MacOS - 1 - - - 0 - - - - - Contents\MacOS - 1 - .framework - - - Contents\MacOS - 1 - .framework - - - 0 - - - - - 1 - .dylib - - - 1 - .dylib - - - 1 - .dylib - - - Contents\MacOS - 1 - .dylib - - - Contents\MacOS - 1 - .dylib - - - 0 - .dll;.bpl - - - - - 1 - .dylib - - - 1 - .dylib - - - 1 - .dylib - - - Contents\MacOS - 1 - .dylib - - - Contents\MacOS - 1 - .dylib - - - 0 - .bpl - - - - - 0 - - - 0 - - - 0 - - - 0 - - - 0 - - - Contents\Resources\StartUp\ - 0 - - - Contents\Resources\StartUp\ - 0 - - - 0 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset - 1 - - - - - 1 - - - 1 - - - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - 1 - - - 1 - - - - - ..\$(PROJECTNAME).app.dSYM\Contents\Resources\DWARF - 1 - - - ..\$(PROJECTNAME).app.dSYM\Contents\Resources\DWARF - 1 - - - - - 1 - - - 1 - - - - - ..\ - 1 - - - ..\ - 1 - - - - - 1 - - - 1 - - - 1 - - - - - ..\$(PROJECTNAME).launchscreen - 64 - - - ..\$(PROJECTNAME).launchscreen - 64 - - - - - 1 - - - 1 - - - 1 - - - - - ..\$(PROJECTNAME).app.dSYM\Contents\Resources\DWARF - 1 - - - - - ..\ - 1 - - - ..\ - 1 - - - - - Contents - 1 - - - Contents - 1 - - - - - Contents\Resources - 1 - - - Contents\Resources - 1 - - - - - library\lib\armeabi-v7a - 1 - - - library\lib\arm64-v8a - 1 - - - 1 - - - 1 - - - 1 - - - 1 - - - Contents\MacOS - 1 - - - Contents\MacOS - 1 - - - 0 - - - - - library\lib\armeabi-v7a - 1 - - - - - 1 - - - 1 - - - - - Assets - 1 - - - Assets - 1 - - - - - Assets - 1 - - - Assets - 1 - - - - - - - - - - - - - - - True - True - True - True - - - 12 - - - - -
diff --git a/Demos/Cipher_FMX/Cipher_FMX.res b/Demos/Cipher_FMX/Cipher_FMX.res deleted file mode 100644 index 3ceec678..00000000 Binary files a/Demos/Cipher_FMX/Cipher_FMX.res and /dev/null differ diff --git a/Demos/Cipher_FMX/CryptoIcon_144.png b/Demos/Cipher_FMX/CryptoIcon_144.png deleted file mode 100644 index 256dffc0..00000000 Binary files a/Demos/Cipher_FMX/CryptoIcon_144.png and /dev/null differ diff --git a/Demos/Cipher_FMX/CryptoIcon_192.png b/Demos/Cipher_FMX/CryptoIcon_192.png deleted file mode 100644 index 6555d53d..00000000 Binary files a/Demos/Cipher_FMX/CryptoIcon_192.png and /dev/null differ diff --git a/Demos/Cipher_FMX/CryptoIcon_36.png b/Demos/Cipher_FMX/CryptoIcon_36.png deleted file mode 100644 index 28c51f2a..00000000 Binary files a/Demos/Cipher_FMX/CryptoIcon_36.png and /dev/null differ diff --git a/Demos/Cipher_FMX/CryptoIcon_48.png b/Demos/Cipher_FMX/CryptoIcon_48.png deleted file mode 100644 index f3e8ec7b..00000000 Binary files a/Demos/Cipher_FMX/CryptoIcon_48.png and /dev/null differ diff --git a/Demos/Cipher_FMX/CryptoIcon_72.png b/Demos/Cipher_FMX/CryptoIcon_72.png deleted file mode 100644 index fadfc312..00000000 Binary files a/Demos/Cipher_FMX/CryptoIcon_72.png and /dev/null differ diff --git a/Demos/Cipher_FMX/CryptoIcon_96.png b/Demos/Cipher_FMX/CryptoIcon_96.png deleted file mode 100644 index 105ea2a2..00000000 Binary files a/Demos/Cipher_FMX/CryptoIcon_96.png and /dev/null differ diff --git a/Demos/Cipher_FMX/Crypto_426_320.png b/Demos/Cipher_FMX/Crypto_426_320.png deleted file mode 100644 index a9b50930..00000000 Binary files a/Demos/Cipher_FMX/Crypto_426_320.png and /dev/null differ diff --git a/Demos/Cipher_FMX/Crypto_470_320.png b/Demos/Cipher_FMX/Crypto_470_320.png deleted file mode 100644 index 79c076be..00000000 Binary files a/Demos/Cipher_FMX/Crypto_470_320.png and /dev/null differ diff --git a/Demos/Cipher_FMX/Crypto_640_480.png b/Demos/Cipher_FMX/Crypto_640_480.png deleted file mode 100644 index fd36c817..00000000 Binary files a/Demos/Cipher_FMX/Crypto_640_480.png and /dev/null differ diff --git a/Demos/Cipher_FMX/Crypto_960_720.png b/Demos/Cipher_FMX/Crypto_960_720.png deleted file mode 100644 index b74accc2..00000000 Binary files a/Demos/Cipher_FMX/Crypto_960_720.png and /dev/null differ diff --git a/Demos/Cipher_FMX/MainForm.LgXhdpiPh.fmx b/Demos/Cipher_FMX/MainForm.LgXhdpiPh.fmx deleted file mode 100644 index 31993d1c..00000000 --- a/Demos/Cipher_FMX/MainForm.LgXhdpiPh.fmx +++ /dev/null @@ -1,91 +0,0 @@ -inherited MainForm_LgXhdpiPh: TMainForm_LgXhdpiPh - ClientHeight = 695 - ClientWidth = 450 - DesignerMasterStyle = 0 - inherited VertScrollBox1: TVertScrollBox - Size.Width = 450.000000000000000000 - Size.Height = 695.000000000000000000 - Viewport.Width = 445.000000000000000000 - Viewport.Height = 695.000000000000000000 - inherited LayoutTop: TLayout - inherited Label2: TLabel - Size.Width = 124.000000000000000000 - Size.Height = 22.000000000000000000 - end - inherited ComboBoxHashFunction: TComboBox - TabOrder = 3 - end - inherited Label5: TLabel - Size.Width = 139.000000000000000000 - Size.Height = 22.000000000000000000 - end - inherited ComboBoxInputFormatting: TComboBox - TabOrder = 4 - end - inherited Label6: TLabel - Size.Width = 178.000000000000000000 - Size.Height = 22.000000000000000000 - end - inherited ComboBoxOutputFormatting: TComboBox - TabOrder = 6 - end - inherited Label1: TLabel - Size.Width = 119.000000000000000000 - Size.Height = 22.000000000000000000 - end - inherited EditKey: TEdit - TabOrder = 15 - Size.Height = 32.000000000000000000 - end - inherited Label3: TLabel - Size.Width = 81.000000000000000000 - Size.Height = 22.000000000000000000 - end - inherited Edit1: TEdit - TabOrder = 14 - Size.Height = 32.000000000000000000 - end - inherited Label4: TLabel - Size.Width = 79.000000000000000000 - Size.Height = 22.000000000000000000 - end - inherited EditFiller: TEdit - TabOrder = 13 - Size.Height = 32.000000000000000000 - end - inherited Label7: TLabel - Size.Width = 103.000000000000000000 - Size.Height = 22.000000000000000000 - end - inherited Label8: TLabel - Size.Width = 139.000000000000000000 - Size.Height = 22.000000000000000000 - end - inherited StringGrid1: TStringGrid - Viewport.Width = 380.000000000000000000 - Viewport.Height = 68.000000000000000000 - inherited StringColumn1: TStringColumn - Size.Width = 250.000000000000000000 - end - end - inherited Label9: TLabel - Size.Width = 139.000000000000000000 - Size.Height = 22.000000000000000000 - end - inherited Label10: TLabel - Size.Width = 139.000000000000000000 - Size.Height = 22.000000000000000000 - end - inherited EditPlainText: TEdit - Size.Height = 32.000000000000000000 - end - inherited EditCipherText: TEdit - Size.Height = 32.000000000000000000 - end - inherited Label11: TLabel - Size.Width = 88.000000000000000000 - Size.Height = 22.000000000000000000 - end - end - end -end diff --git a/Demos/Cipher_FMX/MainForm.fmx b/Demos/Cipher_FMX/MainForm.fmx deleted file mode 100644 index e486d6b5..00000000 --- a/Demos/Cipher_FMX/MainForm.fmx +++ /dev/null @@ -1,304 +0,0 @@ -object FormMain: TFormMain - Left = 0 - Top = 0 - ActiveControl = ComboBoxCipherAlgorithm - Caption = 'FMX Cipher Demo' - ClientHeight = 711 - ClientWidth = 425 - FormFactor.Width = 320 - FormFactor.Height = 480 - FormFactor.Devices = [Desktop] - OnCreate = FormCreate - OnResize = FormResize - DesignerMasterStyle = 0 - object VertScrollBox1: TVertScrollBox - Align = Client - Size.Width = 425.000000000000000000 - Size.Height = 711.000000000000000000 - Size.PlatformDefault = False - StyleLookup = 'scrollboxstyle' - TabOrder = 0 - Viewport.Width = 409.000000000000000000 - Viewport.Height = 711.000000000000000000 - object LayoutTop: TLayout - Size.Width = 425.000000000000000000 - Size.Height = 1170.000000000000000000 - Size.PlatformDefault = False - TabOrder = 0 - object Label2: TLabel - AutoSize = True - Position.X = 16.000000000000000000 - Position.Y = 16.000000000000000000 - Size.Width = 82.000000000000000000 - Size.Height = 16.000000000000000000 - Size.PlatformDefault = False - StyleLookup = 'labelstyle' - TextSettings.WordWrap = False - Text = 'Cipher function' - end - object ComboBoxCipherAlgorithm: TComboBox - Anchors = [akLeft, akTop, akRight] - Position.X = 16.000000000000000000 - Position.Y = 44.000000000000000000 - Size.Width = 385.000000000000000000 - Size.Height = 32.000000000000000000 - Size.PlatformDefault = False - StyleLookup = 'comboboxstyle' - TabOrder = 0 - OnChange = ComboBoxCipherAlgorithmChange - end - object Label5: TLabel - AutoSize = True - Position.X = 16.000000000000000000 - Position.Y = 88.000000000000000000 - Size.Width = 91.000000000000000000 - Size.Height = 16.000000000000000000 - Size.PlatformDefault = False - StyleLookup = 'labelstyle' - TextSettings.WordWrap = False - Text = 'Input is in format' - end - object ComboBoxInputFormatting: TComboBox - Anchors = [akLeft, akTop, akRight] - Position.X = 16.000000000000000000 - Position.Y = 116.000000000000000000 - Size.Width = 385.000000000000000000 - Size.Height = 32.000000000000000000 - Size.PlatformDefault = False - StyleLookup = 'comboboxstyle' - TabOrder = 1 - end - object Label6: TLabel - AutoSize = True - Position.X = 16.000000000000000000 - Position.Y = 160.000000000000000000 - Size.Width = 119.000000000000000000 - Size.Height = 16.000000000000000000 - Size.PlatformDefault = False - StyleLookup = 'labelstyle' - TextSettings.WordWrap = False - Text = 'Desired output format' - end - object ComboBoxOutputFormatting: TComboBox - Anchors = [akLeft, akTop, akRight] - Position.X = 16.000000000000000000 - Position.Y = 188.000000000000000000 - Size.Width = 385.000000000000000000 - Size.Height = 32.000000000000000000 - Size.PlatformDefault = False - StyleLookup = 'comboboxstyle' - TabOrder = 2 - end - object Label1: TLabel - AutoSize = True - Position.X = 16.000000000000000000 - Position.Y = 240.000000000000000000 - Size.Width = 117.000000000000000000 - Size.Height = 16.000000000000000000 - Size.PlatformDefault = False - StyleLookup = 'labelstyle' - TextSettings.WordWrap = False - Text = 'Encryption key (ASCII)' - end - object EditKey: TEdit - Touch.InteractiveGestures = [LongTap, DoubleTap] - Anchors = [akLeft, akTop, akRight] - StyleLookup = 'editstyle' - TabOrder = 3 - Position.X = 16.000000000000000000 - Position.Y = 272.000000000000000000 - Size.Width = 385.000000000000000000 - Size.Height = 22.000000000000000000 - Size.PlatformDefault = False - end - object Label3: TLabel - AutoSize = True - Position.X = 16.000000000000000000 - Position.Y = 312.000000000000000000 - Size.Width = 130.000000000000000000 - Size.Height = 16.000000000000000000 - Size.PlatformDefault = False - StyleLookup = 'labelstyle' - TextSettings.WordWrap = False - Text = 'Init vector (hexadecimal)' - end - object EditInitVector: TEdit - Touch.InteractiveGestures = [LongTap, DoubleTap] - Anchors = [akLeft, akTop, akRight] - StyleLookup = 'editstyle' - TabOrder = 4 - Position.X = 16.000000000000000000 - Position.Y = 344.000000000000000000 - Size.Width = 385.000000000000000000 - Size.Height = 22.000000000000000000 - Size.PlatformDefault = False - end - object Label4: TLabel - AutoSize = True - Position.X = 16.000000000000000000 - Position.Y = 384.000000000000000000 - Size.Width = 128.000000000000000000 - Size.Height = 16.000000000000000000 - Size.PlatformDefault = False - StyleLookup = 'labelstyle' - TextSettings.WordWrap = False - Text = 'Filler byte (hexadecimal)' - end - object EditFiller: TEdit - Touch.InteractiveGestures = [LongTap, DoubleTap] - Anchors = [akLeft, akTop, akRight] - StyleLookup = 'editstyle' - TabOrder = 5 - Position.X = 16.000000000000000000 - Position.Y = 408.000000000000000000 - Size.Width = 385.000000000000000000 - Size.Height = 22.000000000000000000 - Size.PlatformDefault = False - end - object Label7: TLabel - AutoSize = True - Position.X = 16.000000000000000000 - Position.Y = 448.000000000000000000 - Size.Width = 69.000000000000000000 - Size.Height = 16.000000000000000000 - Size.PlatformDefault = False - StyleLookup = 'labelstyle' - TextSettings.WordWrap = False - Text = 'Cipher mode' - end - object ComboBoxChainingMethod: TComboBox - Anchors = [akLeft, akTop, akRight] - Position.X = 16.000000000000000000 - Position.Y = 476.000000000000000000 - Size.Width = 385.000000000000000000 - Size.Height = 32.000000000000000000 - Size.PlatformDefault = False - StyleLookup = 'comboboxstyle' - TabOrder = 6 - end - object CheckBoxLiveCalc: TCheckBox - Anchors = [akLeft, akTop, akRight] - Position.X = 16.000000000000000000 - Position.Y = 528.000000000000000000 - Size.Width = 385.000000000000000000 - Size.Height = 19.000000000000000000 - Size.PlatformDefault = False - StyleLookup = 'checkboxstyle' - TabOrder = 7 - Text = 'Live calculation' - end - object Label8: TLabel - AutoSize = True - Position.X = 16.000000000000000000 - Position.Y = 568.000000000000000000 - Size.Width = 93.000000000000000000 - Size.Height = 16.000000000000000000 - Size.PlatformDefault = False - StyleLookup = 'labelstyle' - TextSettings.WordWrap = False - Text = 'Cipher properties' - end - object StringGridContext: TStringGrid - Anchors = [akLeft, akTop, akRight] - CanFocus = True - ClipChildren = True - Position.X = 16.000000000000000000 - Position.Y = 600.000000000000000000 - Size.Width = 385.000000000000000000 - Size.Height = 100.000000000000000000 - Size.PlatformDefault = False - StyleLookup = 'gridstyle' - TabOrder = 8 - Viewport.Width = 365.000000000000000000 - Viewport.Height = 75.000000000000000000 - object StringColumn1: TStringColumn - Header = 'Property' - Size.Width = 250.000000000000000000 - end - object StringColumn2: TStringColumn - Header = 'Value' - end - end - object Label9: TLabel - AutoSize = True - Position.X = 16.000000000000000000 - Position.Y = 720.000000000000000000 - Size.Width = 49.000000000000000000 - Size.Height = 16.000000000000000000 - Size.PlatformDefault = False - StyleLookup = 'labelstyle' - TextSettings.WordWrap = False - Text = 'Plain text' - end - object Label10: TLabel - AutoSize = True - Position.X = 16.000000000000000000 - Position.Y = 784.000000000000000000 - Size.Width = 58.000000000000000000 - Size.Height = 16.000000000000000000 - Size.PlatformDefault = False - StyleLookup = 'labelstyle' - TextSettings.WordWrap = False - Text = 'Cipher text' - end - object EditPlainText: TEdit - Touch.InteractiveGestures = [LongTap, DoubleTap] - Anchors = [akLeft, akTop, akRight] - StyleLookup = 'editstyle' - TabOrder = 9 - Position.X = 16.000000000000000000 - Position.Y = 752.000000000000000000 - Size.Width = 385.000000000000000000 - Size.Height = 22.000000000000000000 - Size.PlatformDefault = False - OnChangeTracking = EditPlainTextChangeTracking - end - object EditCipherText: TEdit - Touch.InteractiveGestures = [LongTap, DoubleTap] - Anchors = [akLeft, akTop, akRight] - StyleLookup = 'editstyle' - TabOrder = 10 - Position.X = 16.000000000000000000 - Position.Y = 816.000000000000000000 - Size.Width = 385.000000000000000000 - Size.Height = 22.000000000000000000 - Size.PlatformDefault = False - end - object ButtonEncrypt: TButton - Anchors = [akLeft, akTop, akRight] - Position.X = 16.000000000000000000 - Position.Y = 864.000000000000000000 - Size.Width = 385.000000000000000000 - Size.Height = 33.000000000000000000 - Size.PlatformDefault = False - StyleLookup = 'buttonstyle' - TabOrder = 11 - Text = 'Encrypt' - OnClick = ButtonEncryptClick - end - object ButtonDecrypt: TButton - Anchors = [akLeft, akTop, akRight] - Position.X = 16.000000000000000000 - Position.Y = 920.000000000000000000 - Size.Width = 385.000000000000000000 - Size.Height = 33.000000000000000000 - Size.PlatformDefault = False - StyleLookup = 'buttonstyle' - TabOrder = 12 - Text = 'Decrypt' - OnClick = ButtonDecryptClick - end - object LabelVersion: TLabel - AutoSize = True - Position.X = 16.000000000000000000 - Position.Y = 968.000000000000000000 - Size.Width = 178.000000000000000000 - Size.Height = 16.000000000000000000 - Size.PlatformDefault = False - StyleLookup = 'labelstyle' - TextSettings.WordWrap = False - Text = #169' 2018-2021 by Team DEC V%0:s' - end - end - end -end diff --git a/Demos/Cipher_FMX/MainForm.pas b/Demos/Cipher_FMX/MainForm.pas deleted file mode 100644 index 1485c466..00000000 --- a/Demos/Cipher_FMX/MainForm.pas +++ /dev/null @@ -1,397 +0,0 @@ -{***************************************************************************** - The DEC team (see file NOTICE.txt) licenses this file - to you under the Apache License, Version 2.0 (the - "License"); you may not use this file except in compliance - with the License. A copy of this licence is found in the root directory of - this project in the file LICENCE.txt or alternatively at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, - software distributed under the License is distributed on an - "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - KIND, either express or implied. See the License for the - specific language governing permissions and limitations - under the License. -*****************************************************************************} - -unit MainForm; - -interface - -uses - System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, - FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Layouts, - FMX.StdCtrls, FMX.ListBox, FMX.Controls.Presentation, FMX.Edit, System.Rtti, - {$IF RTLVersion < 31} - {$ELSE} - FMX.Grid.Style, - {$ENDIF} - FMX.Grid, FMX.ScrollBox, DECCipherBase, DECFormatBase; - -type - /// - /// Form of the cross platform FMX Cipher demo - /// - TFormMain = class(TForm) - VertScrollBox1: TVertScrollBox; - LayoutTop: TLayout; - Label2: TLabel; - ComboBoxCipherAlgorithm: TComboBox; - Label5: TLabel; - ComboBoxInputFormatting: TComboBox; - Label6: TLabel; - ComboBoxOutputFormatting: TComboBox; - Label1: TLabel; - EditKey: TEdit; - Label3: TLabel; - EditInitVector: TEdit; - Label4: TLabel; - EditFiller: TEdit; - Label7: TLabel; - ComboBoxChainingMethod: TComboBox; - CheckBoxLiveCalc: TCheckBox; - Label8: TLabel; - StringGridContext: TStringGrid; - StringColumn1: TStringColumn; - StringColumn2: TStringColumn; - Label9: TLabel; - Label10: TLabel; - EditPlainText: TEdit; - EditCipherText: TEdit; - ButtonEncrypt: TButton; - ButtonDecrypt: TButton; - LabelVersion: TLabel; - procedure FormResize(Sender: TObject); - procedure FormCreate(Sender: TObject); - procedure ComboBoxCipherAlgorithmChange(Sender: TObject); - procedure ButtonEncryptClick(Sender: TObject); - procedure EditPlainTextChangeTracking(Sender: TObject); - procedure ButtonDecryptClick(Sender: TObject); - private - procedure InitFormatCombos; - procedure InitCipherCombo; - procedure InitCipherModes; - procedure ShowErrorMessage(ErrorMsg: string); - function GetSelectedCipherMode: TCipherMode; - function GetSettings(var InputFormatting : TDECFormatClass; - var OutputFormatting : TDECFormatClass): Boolean; - function GetCipherAlgorithm(var Cipher: TDECCipher): Boolean; - public - end; - -var - FormMain: TFormMain; - -implementation - -uses - System.TypInfo, Generics.Collections, FMX.Platform, - DECBaseClass, DECFormat, DECCipherModes, - DECCipherFormats, DECCiphers, DECUtil - {$IFDEF Android} - , - Androidapi.JNI.GraphicsContentViewText, - Androidapi.Helpers, - Androidapi.JNI.App - {$ENDIF}; - -{$R *.fmx} - -procedure TFormMain.ButtonDecryptClick(Sender: TObject); -var - Cipher : TDECCipher; - InputFormatting : TDECFormatClass; - OutputFormatting : TDECFormatClass; - InputBuffer : TBytes; - OutputBuffer : TBytes; -begin - if not GetSettings(InputFormatting, OutputFormatting) then - exit; - - if ComboBoxCipherAlgorithm.ItemIndex >= 0 then - begin - if not GetCipherAlgorithm(Cipher) then - exit; - - try - InputBuffer := System.SysUtils.BytesOf(EditCipherText.Text); - - if InputFormatting.IsValid(InputBuffer) then - begin - OutputBuffer := (Cipher as TDECFormattedCipher).DecodeBytes(InputFormatting.Decode(InputBuffer)); - - EditPlainText.Text := string(DECUtil.BytesToRawString(OutputFormatting.Encode(OutputBuffer))); - end - else - ShowErrorMessage('Input has wrong format'); - finally - Cipher.Free; - end; - end - else - ShowErrorMessage('No cipher algorithm selected'); -end; - -procedure TFormMain.ButtonEncryptClick(Sender: TObject); -var - Cipher : TDECCipher; - InputFormatting : TDECFormatClass; - OutputFormatting : TDECFormatClass; - InputBuffer : TBytes; - OutputBuffer : TBytes; -begin - if not GetSettings(InputFormatting, OutputFormatting) then - exit; - - if ComboBoxCipherAlgorithm.ItemIndex >= 0 then - begin - if not GetCipherAlgorithm(Cipher) then - exit; - - try - InputBuffer := System.SysUtils.BytesOf(EditPlainText.Text); - - if InputFormatting.IsValid(InputBuffer) then - begin - OutputBuffer := (Cipher as TDECFormattedCipher).EncodeBytes(InputFormatting.Decode(InputBuffer)); - - EditCipherText.Text := string(DECUtil.BytesToRawString(OutputFormatting.Encode(OutputBuffer))); - end - else - ShowErrorMessage('Input has wrong format'); - finally - Cipher.Free; - end; - end - else - ShowErrorMessage('No cipher algorithm selected'); -end; - -function TFormMain.GetSettings(var InputFormatting : TDECFormatClass; - var OutputFormatting : TDECFormatClass): Boolean; -begin - result := false; - - if ComboBoxInputFormatting.ItemIndex >= 0 then - begin - // Find the class type of the selected formatting class and create an instance of it - InputFormatting := TDECFormat.ClassByName( - ComboBoxInputFormatting.Items[ComboBoxInputFormatting.ItemIndex]); - end - else - begin - ShowErrorMessage('No input format selected'); - exit; - end; - - if ComboBoxOutputFormatting.ItemIndex >= 0 then - begin - // Find the class type of the selected formatting class and create an instance of it - OutputFormatting := TDECFormat.ClassByName( - ComboBoxOutputFormatting.Items[ComboBoxOutputFormatting.ItemIndex]); - end - else - begin - ShowErrorMessage('No output format selected'); - exit; - end; - - if EditKey.Text.IsEmpty or EditInitVector.Text.IsEmpty or EditFiller.Text.IsEmpty then - begin - ShowErrorMessage('No key, initialization vector or filler byte given'); - exit; - end; - - result := true; -end; - -function TFormMain.GetCipherAlgorithm(var Cipher : TDECCipher):Boolean; -begin - result := false; - - // Find the class type of the selected cipher class and create an instance of it - Cipher := TDECCipher.ClassByName( - ComboBoxCipherAlgorithm.Items[ComboBoxCipherAlgorithm.ItemIndex]).Create; - - if TFormat_HEX.IsValid(RawByteString(EditInitVector.Text)) and - TFormat_HEX.IsValid(RawByteString(EditFiller.Text)) then - begin - Cipher.Init(RawByteString(EditKey.Text), - TFormat_HEX.Decode(RawByteString(EditInitVector.Text)), - StrToInt('0x' + EditFiller.Text)); - - Cipher.Mode := GetSelectedCipherMode; - end - else - begin - ShowErrorMessage('Init vector or filler byte not given in hexadecimal representation'); - exit; - end; - - result := true; -end; - -function TFormMain.GetSelectedCipherMode:TCipherMode; -begin - // Determine selected block chaining method via RTTI (runtime type information) - result := TCipherMode(System.TypInfo.GetEnumValue( - TypeInfo(TCipherMode), - ComboBoxChainingMethod.Items[ComboBoxChainingMethod.ItemIndex])); -end; - -procedure TFormMain.ShowErrorMessage(ErrorMsg: string); -{$IF RTLVersion > 30} -var - AsyncDlg : IFMXDialogServiceASync; -{$ENDIF} -begin - {$IF RTLVersion > 30} - if TPlatformServices.Current.SupportsPlatformService(IFMXDialogServiceAsync, - IInterface(AsyncDlg)) then - AsyncDlg.MessageDialogAsync(Translate(ErrorMsg), - TMsgDlgType.mtError, [TMsgDlgBtn.mbOk], TMsgDlgBtn.mbOk, 0, - procedure (const AResult: TModalResult) - begin - end); - {$ELSE} - MessageDlg(Translate(ErrorMsg), - TMsgDlgType.mtError, [TMsgDlgBtn.mbOk], 0); - {$ENDIF} -end; - -procedure TFormMain.ComboBoxCipherAlgorithmChange(Sender: TObject); -var - Context : TCipherContext; -begin - Context := TDECCipher.ClassByName( - ComboBoxCipherAlgorithm.Items[ComboBoxCipherAlgorithm.ItemIndex]).Context; - - StringGridContext.RowCount := 7; - StringGridContext.Cells[0, 0] := 'Key size (bit)'; - StringGridContext.Cells[0, 1] := 'Block size (bit)'; - StringGridContext.Cells[0, 2] := 'Buffer size (bit)'; - StringGridContext.Cells[0, 3] := 'User size (bit)'; - StringGridContext.Cells[0, 4] := 'User save'; - StringGridContext.Cells[0, 5] := 'Cipher mode'; - StringGridContext.Cells[0, 6] := 'Cipher key'; - - StringGridContext.Cells[1, 0] := IntToStr(Context.KeySize*8); - StringGridContext.Cells[1, 1] := IntToStr(Context.BlockSize*8); - StringGridContext.Cells[1, 2] := IntToStr(Context.BufferSize*8); - StringGridContext.Cells[1, 3] := IntToStr(Context.AdditionalBufferSize*8); - StringGridContext.Cells[1, 4] := BoolToStr(Context.NeedsAdditionalBufferBackup, true); - - if ctBlock in Context.CipherType then - StringGridContext.Cells[1, 5] := 'block cipher' - else - StringGridContext.Cells[1, 5] := 'stream cipher'; - - if ctSymmetric in Context.CipherType then - StringGridContext.Cells[1, 6] := 'symmetric' - else - StringGridContext.Cells[1, 6] := 'asymmetric'; -end; - -procedure TFormMain.EditPlainTextChangeTracking(Sender: TObject); -begin - if CheckBoxLiveCalc.IsChecked then - ButtonEncryptClick(self) -end; - -procedure TFormMain.FormCreate(Sender: TObject); -var - AppService : IFMXApplicationService; -begin - if TPlatformServices.Current.SupportsPlatformService(IFMXApplicationService, - IInterface(AppService)) then - LabelVersion.Text := format(LabelVersion.Text, [AppService.AppVersion]) - else - LabelVersion.Text := format(LabelVersion.Text, ['']); - - InitFormatCombos; - InitCipherCombo; - InitCipherModes; -end; - -procedure TFormMain.FormResize(Sender: TObject); -begin - LayoutTop.Width := VertScrollBox1.Width; -end; - -procedure TFormMain.InitFormatCombos; -var - MyClass : TPair; - Formats : TStringList; - CopyIdx : Integer; -begin - Formats := TStringList.Create; - - try - for MyClass in TDECFormat.ClassList do - Formats.Add(MyClass.Value.ClassName); - - Formats.Sort; - ComboBoxInputFormatting.Items.AddStrings(Formats); - ComboBoxOutputFormatting.Items.AddStrings(Formats); - - if Formats.Count > 0 then - begin - if Formats.Find('TFormat_Copy', CopyIdx) then - begin - ComboBoxInputFormatting.ItemIndex := CopyIdx; - ComboBoxOutputFormatting.ItemIndex := CopyIdx; - end - else - begin - ComboBoxInputFormatting.ItemIndex := 0; - ComboBoxOutputFormatting.ItemIndex := 0; - end; - end; - finally - Formats.Free; - end; -end; - -procedure TFormMain.InitCipherCombo; -var - MyClass : TPair; - Ciphers : TStringList; -begin - Ciphers := TStringList.Create; - - try - // Alternatively you can use TDECCipher.ClassList.GetClassList(Ciphers); but - // then it's harder to remove TCipher_Null from the list - for MyClass in TDECCipher.ClassList do - begin - if (MyClass.Value <> TCipher_Null) then - Ciphers.Add(MyClass.Value.ClassName); - end; - - Ciphers.Sort; - ComboBoxCipherAlgorithm.Items.AddStrings(Ciphers); - - if Ciphers.Count > 0 then - ComboBoxCipherAlgorithm.ItemIndex := 0; - finally - Ciphers.Free; - end; -end; - -procedure TFormMain.InitCipherModes; -var - Mode : TCipherMode; -begin - for Mode := low(TCipherMode) to high(TCipherMode) do - begin - ComboBoxChainingMethod.Items.Add(System.TypInfo.GetEnumName( - TypeInfo(TCipherMode), - Integer(Mode))); - end; - - if ComboBoxChainingMethod.Items.Count > 0 then - ComboBoxChainingMethod.ItemIndex := 0; -end; - -end. diff --git a/Demos/Progress_VCL/MainForm.dfm b/Demos/Progress_VCL/MainForm.dfm deleted file mode 100644 index 4c45001f..00000000 --- a/Demos/Progress_VCL/MainForm.dfm +++ /dev/null @@ -1,73 +0,0 @@ -object FormMain: TFormMain - Left = 0 - Top = 0 - Caption = 'ProgressTest' - ClientHeight = 187 - ClientWidth = 635 - Color = clBtnFace - Constraints.MinHeight = 226 - Constraints.MinWidth = 350 - Font.Charset = DEFAULT_CHARSET - Font.Color = clWindowText - Font.Height = -11 - Font.Name = 'Tahoma' - Font.Style = [] - OldCreateOrder = False - DesignSize = ( - 635 - 187) - PixelsPerInch = 96 - TextHeight = 13 - object Button1: TButton - Left = 8 - Top = 16 - Width = 75 - Height = 25 - Caption = 'Encrypt' - TabOrder = 0 - OnClick = Button1Click - end - object Edit1: TEdit - Left = 104 - Top = 18 - Width = 523 - Height = 21 - Anchors = [akLeft, akTop, akRight] - TabOrder = 1 - Text = 'D:\Test.txt' - end - object ProgressBar1: TProgressBar - Left = 8 - Top = 64 - Width = 619 - Height = 17 - Anchors = [akLeft, akTop, akRight] - TabOrder = 2 - end - object RadioButtonMethod: TRadioButton - Left = 8 - Top = 96 - Width = 193 - Height = 17 - Caption = 'Use method as progress event' - Checked = True - TabOrder = 3 - TabStop = True - end - object RadioButtonProcedure: TRadioButton - Left = 8 - Top = 128 - Width = 193 - Height = 17 - Caption = 'Use procedure as progress event' - TabOrder = 4 - end - object RadioButtonAnonMethod: TRadioButton - Left = 8 - Top = 162 - Width = 233 - Height = 17 - Caption = 'Use anonymous method as progress event' - TabOrder = 5 - end -end diff --git a/Demos/Progress_VCL/MainForm.pas b/Demos/Progress_VCL/MainForm.pas deleted file mode 100644 index 027a77e5..00000000 --- a/Demos/Progress_VCL/MainForm.pas +++ /dev/null @@ -1,130 +0,0 @@ -{***************************************************************************** - The DEC team (see file NOTICE.txt) licenses this file - to you under the Apache License, Version 2.0 (the - "License"); you may not use this file except in compliance - with the License. A copy of this licence is found in the root directory of - this project in the file LICENCE.txt or alternatively at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, - software distributed under the License is distributed on an - "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - KIND, either express or implied. See the License for the - specific language governing permissions and limitations - under the License. -*****************************************************************************} - -/// -/// Simple demonstration of using the IDECProgress interface for displaying -/// progress of an operation -/// -unit MainForm; - -interface - -uses - Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, - Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ComCtrls, Vcl.StdCtrls, DECUtil; - -type - TFormMain = class(TForm) - Button1: TButton; - Edit1: TEdit; - ProgressBar1: TProgressBar; - RadioButtonMethod: TRadioButton; - RadioButtonProcedure: TRadioButton; - RadioButtonAnonMethod: TRadioButton; - procedure Button1Click(Sender: TObject); - public - procedure OnProgress(Size, Pos: Int64; State: TDECProgressState); - end; - -var - FormMain: TFormMain; - -implementation - -uses - System.UITypes, DECCiphers, DECCipherBase; - -{$R *.dfm} - -resourcestring - rFileNameEmptyFailure = 'No input file specified!'; - -procedure OnProgressProc(Size, Pos: Int64; State: TDECProgressState); -begin - FormMain.ProgressBar1.Min := 0; - FormMain.ProgressBar1.Max := Size; - - if (State = Finished) then - FormMain.ProgressBar1.Position := FormMain.ProgressBar1.Max - else - FormMain.ProgressBar1.Position := Pos; -end; - -procedure TFormMain.Button1Click(Sender: TObject); -var - Cipher : TCipher_AES; - TargetFile : string; -begin - if Edit1.Text = '' then - begin - MessageDlg(rFileNameEmptyFailure, mtError, [mbOK], -1); - exit; - end; - - Cipher := TCipher_AES.Create; - - try - try - // Init encryption - Cipher.Init(RawByteString('Passwort1234567890'), RawByteString(#1#2#3#4#5#6#7#99), 0); - Cipher.Mode := cmCBCx; - - // replace file extension of input file - TargetFile := Edit1.Text; - Delete(TargetFile, pos('.', TargetFile), length(TargetFile)); - TargetFile := TargetFile + '.enc'; - - // depending on selected radio button demo a different progress event technique - if RadioButtonMethod.Checked then - Cipher.EncodeFile(Edit1.Text, TargetFile, OnProgress) - else - if RadioButtonProcedure.Checked then - Cipher.EncodeFile(Edit1.Text, TargetFile, OnProgressProc) - else - if RadioButtonAnonMethod.Checked then - Cipher.EncodeFile(Edit1.Text, TargetFile, - procedure(Size, Pos: Int64; State: TDECProgressState) - begin - ProgressBar1.Min := 0; - ProgressBar1.Max := Size; - - if (State = Finished) then - ProgressBar1.Position := ProgressBar1.Max - else - ProgressBar1.Position := Pos; - end); - except - on E: Exception do - MessageDlg(E.Message, mtError, [mbOK], -1); - end; - finally - Cipher.Free; - end; -end; - -procedure TFormMain.OnProgress(Size, Pos: Int64; State: TDECProgressState); -begin - ProgressBar1.Min := 0; - ProgressBar1.Max := Size; - - if (State = Finished) then - ProgressBar1.Position := ProgressBar1.Max - else - ProgressBar1.Position := Pos; -end; - -end. diff --git a/Demos/Progress_VCL/ProgressDemoVCL.dpr b/Demos/Progress_VCL/ProgressDemoVCL.dpr deleted file mode 100644 index 90f626b1..00000000 --- a/Demos/Progress_VCL/ProgressDemoVCL.dpr +++ /dev/null @@ -1,27 +0,0 @@ -program ProgressDemoVCL; - -uses - Vcl.Forms, - MainForm in 'MainForm.pas' {Form1}, - DECBaseClass in '..\..\Source\DECBaseClass.pas', - DECCipherBase in '..\..\Source\DECCipherBase.pas', - DECCipherFormats in '..\..\Source\DECCipherFormats.pas', - DECCipherInterface in '..\..\Source\DECCipherInterface.pas', - DECCipherModes in '..\..\Source\DECCipherModes.pas', - DECCiphers in '..\..\Source\DECCiphers.pas', - DECDataCipher in '..\..\Source\DECDataCipher.pas', - DECCRC in '..\..\Source\DECCRC.pas', - DECUtil in '..\..\Source\DECUtil.pas', - DECTypes in '..\..\Source\DECTypes.pas', - DECUtilRawByteStringHelper in '..\..\Source\DECUtilRawByteStringHelper.pas', - DECFormatBase in '..\..\Source\DECFormatBase.pas', - DECData in '..\..\Source\DECData.pas'; - -{$R *.res} - -begin - Application.Initialize; - Application.MainFormOnTaskbar := True; - Application.CreateForm(TFormMain, FormMain); - Application.Run; -end. diff --git a/Demos/Progress_VCL/ProgressDemoVCL.dproj b/Demos/Progress_VCL/ProgressDemoVCL.dproj deleted file mode 100644 index f024cc1f..00000000 --- a/Demos/Progress_VCL/ProgressDemoVCL.dproj +++ /dev/null @@ -1,966 +0,0 @@ - - - {B6DE5711-AD3F-44B0-8206-FB26B8B82A31} - 19.2 - VCL - True - Debug - Win32 - 3 - Application - ProgressDemoVCL.dpr - - - true - - - true - Base - true - - - true - Base - true - - - true - Base - true - - - true - Cfg_1 - true - true - - - true - Cfg_1 - true - true - - - true - Base - true - - - true - Cfg_2 - true - true - - - true - Cfg_2 - true - true - - - .\$(Platform)\$(Config) - .\$(Platform)\$(Config) - false - false - false - false - false - System;Xml;Data;Datasnap;Web;Soap;Vcl;Vcl.Imaging;Vcl.Touch;Vcl.Samples;Vcl.Shell;$(DCC_Namespace) - $(BDS)\bin\delphi_PROJECTICON.ico - $(BDS)\bin\Artwork\Windows\UWP\delphi_UwpDefault_44.png - $(BDS)\bin\Artwork\Windows\UWP\delphi_UwpDefault_150.png - ProgressDemoVCL - - - DBXSqliteDriver;RESTComponents;fmxase;DBXDb2Driver;DBXInterBaseDriver;vclactnband;vclFireDAC;bindcompvclsmp;emsclientfiredac;tethering;svnui;DataSnapFireDAC;JvGlobus;FireDACADSDriver;JvPluginSystem;frx27;DBXMSSQLDriver;JvMM;DatasnapConnectorsFreePascal;FireDACMSSQLDriver;vcltouch;JvBands;vcldb;bindcompfmx;svn;JvJans;DBXOracleDriver;JvNet;inetdb;JvAppFrm;VirtualTreesDR;FmxTeeUI;emsedge;JvDotNetCtrls;fmx;FireDACIBDriver;fmxdae;vcledge;JvWizards;FireDACDBXDriver;dbexpress;IndyCore;vclx;frxTee27;JvPageComps;dsnap;emsclient;DataSnapCommon;SVGIconImageListFMX;FireDACCommon;JvDB;RESTBackendComponents;DataSnapConnectors;VCLRESTComponents;soapserver;JclDeveloperTools;vclie;bindengine;DBXMySQLDriver;CloudService;FireDACOracleDriver;FireDACMySQLDriver;DBXFirebirdDriver;JvCmp;JvHMI;SVGIconPackage;FireDACCommonODBC;FireDACCommonDriver;DataSnapClient;inet;IndyIPCommon;bindcompdbx;frxDB27;JvCustom;vcl;IndyIPServer;DBXSybaseASEDriver;JvXPCtrls;IndySystem;FireDACDb2Driver;bindcompvclwinx;dsnapcon;FireDACMSAccDriver;fmxFireDAC;FireDACInfxDriver;vclimg;TeeDB;FireDAC;Jcl;JvCore;emshosting;JvCrypt;FireDACSqliteDriver;FireDACPgDriver;FireDACASADriver;DBXOdbcDriver;FireDACTDataDriver;FMXTee;soaprtl;DbxCommonDriver;JvDlgs;JvRuntimeDesign;JvManagedThreads;Tee;DataSnapServer;xmlrtl;soapmidas;DataSnapNativeClient;fmxobj;vclwinx;FireDACDSDriver;rtl;emsserverresource;DbxClientDriver;JvTimeFramework;DBXSybaseASADriver;CustomIPTransport;vcldsnap;CodeSiteExpressPkg;JvSystem;JvStdCtrls;bindcomp;appanalytics;DBXInformixDriver;IndyIPClient;bindcompvcl;frxe27;SVGIconImageList;TeeUI;JvDocking;dbxcds;VclSmp;JvPascalInterpreter;adortl;FireDACODBCDriver;JclVcl;DataSnapIndy10ServerTransport;dsnapxml;DataSnapProviderClient;dbrtl;IndyProtocols;inetdbxpress;FireDACMongoDBDriver;JvControls;JvPrintPreview;JclContainers;DataSnapServerMidas;$(DCC_UsePackage) - Winapi;System.Win;Data.Win;Datasnap.Win;Web.Win;Soap.Win;Xml.Win;Bde;$(DCC_Namespace) - Debug - true - CompanyName=;FileDescription=$(MSBuildProjectName);FileVersion=1.0.0.0;InternalName=;LegalCopyright=;LegalTrademarks=;OriginalFilename=;ProgramID=com.embarcadero.$(MSBuildProjectName);ProductName=$(MSBuildProjectName);ProductVersion=1.0.0.0;Comments= - 1033 - $(BDS)\bin\default_app.manifest - .\..\..\Compiled\BIN_IDExx.x_$(Platform)__Demos - .\..\..\Compiled\DCU_IDE$(ProductVersion)_$(Platform)__Demos - .\..\..\Compiled\DCP_IDE$(ProductVersion)_$(Platform)_$(Config) - .\..\..\Compiled\BIN_IDExx.x_$(Platform)__Demos - .\..\..\Compiled\DCU_IDE$(ProductVersion)_$(Platform)_$(Config);$(DCC_UnitSearchPath) - - - DBXSqliteDriver;RESTComponents;fmxase;DBXDb2Driver;DBXInterBaseDriver;vclactnband;vclFireDAC;bindcompvclsmp;emsclientfiredac;tethering;DataSnapFireDAC;FireDACADSDriver;DBXMSSQLDriver;DatasnapConnectorsFreePascal;FireDACMSSQLDriver;vcltouch;vcldb;bindcompfmx;DBXOracleDriver;inetdb;VirtualTreesDR;FmxTeeUI;emsedge;fmx;FireDACIBDriver;fmxdae;vcledge;FireDACDBXDriver;dbexpress;IndyCore;vclx;dsnap;emsclient;DataSnapCommon;FireDACCommon;RESTBackendComponents;DataSnapConnectors;VCLRESTComponents;soapserver;vclie;bindengine;DBXMySQLDriver;CloudService;FireDACOracleDriver;FireDACMySQLDriver;DBXFirebirdDriver;FireDACCommonODBC;FireDACCommonDriver;DataSnapClient;inet;IndyIPCommon;bindcompdbx;vcl;IndyIPServer;DBXSybaseASEDriver;IndySystem;FireDACDb2Driver;bindcompvclwinx;dsnapcon;FireDACMSAccDriver;fmxFireDAC;FireDACInfxDriver;vclimg;TeeDB;FireDAC;emshosting;FireDACSqliteDriver;FireDACPgDriver;FireDACASADriver;DBXOdbcDriver;FireDACTDataDriver;FMXTee;soaprtl;DbxCommonDriver;Tee;DataSnapServer;xmlrtl;soapmidas;DataSnapNativeClient;fmxobj;vclwinx;FireDACDSDriver;rtl;emsserverresource;DbxClientDriver;DBXSybaseASADriver;CustomIPTransport;vcldsnap;bindcomp;appanalytics;DBXInformixDriver;IndyIPClient;bindcompvcl;TeeUI;dbxcds;VclSmp;adortl;FireDACODBCDriver;DataSnapIndy10ServerTransport;dsnapxml;DataSnapProviderClient;dbrtl;IndyProtocols;inetdbxpress;FireDACMongoDBDriver;DataSnapServerMidas;$(DCC_UsePackage) - Winapi;System.Win;Data.Win;Datasnap.Win;Web.Win;Soap.Win;Xml.Win;$(DCC_Namespace) - Debug - true - CompanyName=;FileDescription=$(MSBuildProjectName);FileVersion=1.0.0.0;InternalName=;LegalCopyright=;LegalTrademarks=;OriginalFilename=;ProgramID=com.embarcadero.$(MSBuildProjectName);ProductName=$(MSBuildProjectName);ProductVersion=1.0.0.0;Comments= - 1033 - $(BDS)\bin\default_app.manifest - - - DEBUG;$(DCC_Define) - true - false - true - true - true - - - false - true - PerMonitorV2 - true - 1033 - - - true - PerMonitorV2 - - - false - RELEASE;$(DCC_Define) - 0 - 0 - - - true - PerMonitorV2 - true - 1033 - - - true - PerMonitorV2 - - - - MainSource - - -
Form1
- dfm -
- - - - - - - - - - - - - - - Cfg_2 - Base - - - Base - - - Cfg_1 - Base - -
- - Delphi.Personality.12 - Application - - - - ProgressDemoVCL.dpr - - - Microsoft Office 2000 Beispiele für gekapselte Komponenten für Automatisierungsserver - Microsoft Office XP Beispiele für gekapselte Komponenten für Automation Server - - - - - - ProgressDemoVCL.exe - true - - - - - 1 - - - Contents\MacOS - 1 - - - 0 - - - - - classes - 1 - - - classes - 1 - - - - - res\xml - 1 - - - res\xml - 1 - - - - - library\lib\armeabi-v7a - 1 - - - - - library\lib\armeabi - 1 - - - library\lib\armeabi - 1 - - - - - library\lib\armeabi-v7a - 1 - - - - - library\lib\mips - 1 - - - library\lib\mips - 1 - - - - - library\lib\armeabi-v7a - 1 - - - library\lib\arm64-v8a - 1 - - - - - library\lib\armeabi-v7a - 1 - - - - - res\drawable - 1 - - - res\drawable - 1 - - - - - res\values - 1 - - - res\values - 1 - - - - - res\values-v21 - 1 - - - res\values-v21 - 1 - - - - - res\values - 1 - - - res\values - 1 - - - - - res\drawable - 1 - - - res\drawable - 1 - - - - - res\drawable-xxhdpi - 1 - - - res\drawable-xxhdpi - 1 - - - - - res\drawable-xxxhdpi - 1 - - - res\drawable-xxxhdpi - 1 - - - - - res\drawable-ldpi - 1 - - - res\drawable-ldpi - 1 - - - - - res\drawable-mdpi - 1 - - - res\drawable-mdpi - 1 - - - - - res\drawable-hdpi - 1 - - - res\drawable-hdpi - 1 - - - - - res\drawable-xhdpi - 1 - - - res\drawable-xhdpi - 1 - - - - - res\drawable-mdpi - 1 - - - res\drawable-mdpi - 1 - - - - - res\drawable-hdpi - 1 - - - res\drawable-hdpi - 1 - - - - - res\drawable-xhdpi - 1 - - - res\drawable-xhdpi - 1 - - - - - res\drawable-xxhdpi - 1 - - - res\drawable-xxhdpi - 1 - - - - - res\drawable-xxxhdpi - 1 - - - res\drawable-xxxhdpi - 1 - - - - - res\drawable-small - 1 - - - res\drawable-small - 1 - - - - - res\drawable-normal - 1 - - - res\drawable-normal - 1 - - - - - res\drawable-large - 1 - - - res\drawable-large - 1 - - - - - res\drawable-xlarge - 1 - - - res\drawable-xlarge - 1 - - - - - res\values - 1 - - - res\values - 1 - - - - - 1 - - - Contents\MacOS - 1 - - - 0 - - - - - Contents\MacOS - 1 - .framework - - - Contents\MacOS - 1 - .framework - - - 0 - - - - - 1 - .dylib - - - 1 - .dylib - - - 1 - .dylib - - - Contents\MacOS - 1 - .dylib - - - Contents\MacOS - 1 - .dylib - - - 0 - .dll;.bpl - - - - - 1 - .dylib - - - 1 - .dylib - - - 1 - .dylib - - - Contents\MacOS - 1 - .dylib - - - Contents\MacOS - 1 - .dylib - - - 0 - .bpl - - - - - 0 - - - 0 - - - 0 - - - 0 - - - 0 - - - Contents\Resources\StartUp\ - 0 - - - Contents\Resources\StartUp\ - 0 - - - 0 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset - 1 - - - - - 1 - - - 1 - - - - - ..\$(PROJECTNAME).app.dSYM\Contents\Resources\DWARF - 1 - - - ..\$(PROJECTNAME).app.dSYM\Contents\Resources\DWARF - 1 - - - - - ..\ - 1 - - - ..\ - 1 - - - - - 1 - - - 1 - - - 1 - - - - - ..\$(PROJECTNAME).launchscreen - 64 - - - ..\$(PROJECTNAME).launchscreen - 64 - - - - - 1 - - - 1 - - - 1 - - - - - ..\$(PROJECTNAME).app.dSYM\Contents\Resources\DWARF - 1 - - - - - ..\ - 1 - - - ..\ - 1 - - - - - Contents - 1 - - - Contents - 1 - - - - - Contents\Resources - 1 - - - Contents\Resources - 1 - - - - - library\lib\armeabi-v7a - 1 - - - library\lib\arm64-v8a - 1 - - - 1 - - - 1 - - - 1 - - - 1 - - - Contents\MacOS - 1 - - - Contents\MacOS - 1 - - - 0 - - - - - library\lib\armeabi-v7a - 1 - - - - - 1 - - - 1 - - - - - Assets - 1 - - - Assets - 1 - - - - - Assets - 1 - - - Assets - 1 - - - - - - - - - - - - - - - True - True - - - 12 - - - - -
diff --git a/Demos/Progress_VCL/ProgressDemoVCL.res b/Demos/Progress_VCL/ProgressDemoVCL.res deleted file mode 100644 index b8b47e3a..00000000 Binary files a/Demos/Progress_VCL/ProgressDemoVCL.res and /dev/null differ diff --git a/Source/DECCipherBase.pas b/Source/DECCipherBase.pas deleted file mode 100644 index 9d002830..00000000 --- a/Source/DECCipherBase.pas +++ /dev/null @@ -1,1114 +0,0 @@ -{***************************************************************************** - The DEC team (see file NOTICE.txt) licenses this file - to you under the Apache License, Version 2.0 (the - "License"); you may not use this file except in compliance - with the License. A copy of this licence is found in the root directory - of this project in the file LICENCE.txt or alternatively at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, - software distributed under the License is distributed on an - "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - KIND, either express or implied. See the License for the - specific language governing permissions and limitations - under the License. -*****************************************************************************} -unit DECCipherBase; - -interface - -{$INCLUDE DECOptions.inc} - -uses - {$IFDEF FPC} - SysUtils, Classes, - {$ELSE} - System.SysUtils, System.Classes, - {$ENDIF} - DECBaseClass, DECFormatBase; - -type - /// - /// Possible kindes of cipher algorithms - /// - /// ctNull = special "do nothing cipher" - /// - /// - /// ctStream = cipher operating on a stream of bytes instead of blocks - /// - /// - /// ctBlock = cipher operating on blocks of bytes with a fixed size - /// - /// - /// ctSymmetric = cipher where the same key encrypts and decrypts - /// - /// - /// ctAsymetric = cipher where encryption and decryption requires - /// different keys - /// - /// - TCipherTypes = (ctNull, ctStream, ctBlock, ctSymmetric, ctAsymmetric); - - /// - /// Actual kind of cipher algorithm - /// - TCipherType = set of TCipherTypes; - - /// - /// Padding used to fill the last incomplete block of a block encryption - /// algorithm. To be expanded in a future version - /// - TBlockFillMode = (fmByte); - - /// - /// Record containing meta data about a certain cipher - /// - TCipherContext = packed record - /// - /// maximal key size in bytes - /// - KeySize : Integer; - /// - /// mininmal block size in bytes, e.g. 1 = Streamcipher - /// - BlockSize : Integer; - /// - /// internal buffersize in bytes - /// - BufferSize : Integer; - /// - /// Size in bytes of the FAdditionalBuffer used by some of the cipher algorithms - /// - AdditionalBufferSize : Integer; - /// - /// When true the memory a certain internal pointer (FAdditionalBuffer) - /// points to needs to be backuped during key initialization if no init - /// vector is specified and restored at the end of that init method. - /// Same in Done method as well. - /// - NeedsAdditionalBufferBackup : Boolean; - /// - /// Minimum number of rounds allowed for any block cipher having a rounds - /// property. In all other cases it will be set to 1. - /// - MinRounds : UInt16; - /// - /// Maximum number of rounds allowed for any block cipher having a rounds - /// property. In all other cases it will be set to 1. - /// - MaxRounds : UInt16; - - /// - /// Specifies the kind of cipher - /// - CipherType : TCipherType; - end; - - /// - /// TCipher.State represents the internal state of processing - /// - /// csNew : cipher isn't initialized, .Init() must be called before en/decode - /// - /// - /// csNew : cipher isn't initialized, .Init() must be called before en/decode - /// - /// - /// csInitialized : cipher is initialized by .Init(), i.e. Keysetup was processed - /// - /// - /// csEncode : Encoding was started, and more chunks can be encoded, but not decoded - /// - /// - /// csDecode : Decoding was started, and more chunks can be decoded, but not encoded - /// - /// - /// csPadded : trough En/Decoding the messagechunks are padded, no more chunks can - /// be processed, the cipher is blocked - /// - /// - /// csDone : Processing is finished and Cipher.Done was called. Now new En/Decoding - /// can be started without calling .Init() before. csDone is basically - /// identical to csInitialized, except Cipher.Buffer holds the encrypted - /// last state of Cipher.Feedback, thus Cipher.Buffer can be used as C-MAC. - /// - /// - TCipherState = (csNew, csInitialized, csEncode, csDecode, csPadded, csDone); - /// - /// Set of cipher states, representing the internal state of processing - /// - TCipherStates = set of TCipherState; - - /// - /// This defines how the individual blocks of the data to be processed are - /// linked with each other. - /// - /// Modes cmCBCx, cmCTSx, cmCTSxx, cmCFBx, cmOFBx, cmCFSx, cmECBx are working - /// on Blocks of Cipher.BufferSize bytes, when using a Blockcipher that's equal - /// to Cipher.BlockSize. - /// - /// Modes cmCFB8, cmOFB8, cmCFS8 work on 8 bit Feedback Shift Registers. - /// - /// Modes cmCTSx, cmCFSx, cmCFS8 are proprietary modes developed by Hagen - /// Reddmann. These modes work like cmCBCx, cmCFBx, cmCFB8 but with double - /// XOR'ing of the inputstream into the feedback register. - /// - /// Mode cmECBx needs message padding to be a multiple of Cipher.BlockSize and - /// should be used only in 1-byte Streamciphers. - /// - /// Modes cmCFB8, cmCFBx, cmOFB8, cmOFBx, cmCFS8 and cmCFSx need no padding. - /// - /// Modes cmCTSx, cmCBCx need no external padding, because internally the last - /// truncated block is padded by cmCFS8 or cmCFB8. After padding these Modes - /// cannot be used to process any more data. If needed to process chunks of - /// data then each chunk must be algined to Cipher.BufferSize bytes. - /// - /// Mode cmCTS3 is a proprietary mode developed by Frederik Winkelsdorf. It - /// replaces the CFS8 padding of the truncated final block with a CFSx padding. - /// Useful when converting projects that previously used the old DEC v3.0. It - /// has the same restrictions for external padding and chunk processing as - /// cmCTSx has. - /// - TCipherMode = ( - cmCTSx, // double CBC, with CFS8 padding of truncated final block - cmCBCx, // Cipher Block Chaining, with CFB8 padding of truncated final block - cmCFB8, // 8bit Cipher Feedback mode - cmCFBx, // CFB on Blocksize of Cipher - cmOFB8, // 8bit Output Feedback mode - cmOFBx, // OFB on Blocksize bytes - cmCFS8, // 8Bit CFS, double CFB - cmCFSx, // CFS on Blocksize bytes - cmECBx // Electronic Code Book - {$IFDEF DEC3_CMCTS} - ,cmCTS3 // double CBC, with less secure padding of truncated final block - // for DEC 3.0 compatibility only (see DECOptions.inc) - {$ENDIF DEC3_CMCTS} - ); - - /// - /// Each cipher algorithm has to implement a Encode and a Decode method which - /// has the same signature as this type. The CipherFormats get these - /// encode/decode methods passed to do their work. - /// - /// - /// Contains the data to be encoded or decoded - /// - /// - /// Contains the data after encoding or decoding - /// - /// - /// Number of bytes to encode or decode - /// - TDECCipherCodeEvent = procedure(const Source; var Dest; DataSize: Integer) of object; - - /// - /// Class type of the cipher base class - /// - TDECCipherClass = class of TDECCipher; - - /// - /// Base class for all implemented cipher algorithms - /// - /// - /// When adding new block ciphers do never directly inherit from this class! - /// Inherit from TDECCipherFormats. - /// - TDECCipher = class(TDECObject) - strict private - /// - /// This is the complete memory block containing FInitializationVector, - /// FFeedback, FBuffer and FAdditionalBuffer - /// - FData : PByteArray; - /// - /// This is the size of FData in byte - /// - FDataSize : Integer; - strict protected - /// - /// Padding mode used to concatenate/connect blocks in a block cipher - /// - FMode : TCipherMode; - /// - /// Mode used for filling up an incomplete last block in a block cipher - /// - FFillMode : TBlockFillMode; - /// - /// Current processing state - /// - FState: TCipherState; - /// - /// Size of the internally used processing buffer in byte - /// - FBufferSize: Integer; - /// - /// At which position of the buffer are we currently operating? - /// - FBufferIndex: Integer; - - /// - /// Some algorithms, mostly the cipher mode ones, need a temporary buffer - /// to work with. Some other methods like Done or Valid cipher need to pass - /// a buffer as parameter as that is ecpected by the called method. - /// - FBuffer: PByteArray; - - /// - /// Initialization vector. When using cipher modes to derive a stream - /// cipher from a block cipher algorithm some data from each encrypted block - /// is fed into the encryption of the next block. For the first block there - /// is no such encrypted data yet, so this initialization vector fills this - /// "gap". - /// - FInitializationVector: PByteArray; - - /// - /// Cipher modes are used to derive a stream cipher from block cipher - /// algorithms. For this something from the last entrypted block (or for - /// the first block from the vector) is used in the encryption of the next - /// block. It may be XORed with the next block cipher text for isntance. - /// That data "going into the next block encryption" is this feedback array - /// - FFeedback: PByteArray; - - /// - /// Size of FAdditionalBuffer in Byte - /// - FAdditionalBufferSize: Integer; - /// - /// A buffer some of the cipher algorithms need to operate on. It is - /// some part of FBuffer like FInitializationVector and FFeedback as well. - /// - FAdditionalBuffer: Pointer; - - /// - /// If a user does not specify an init vector (IV) during key setup - /// (IV length = 0) the init method generates an IV by encrypting the - /// complete memory reserved for IV. Within this memory block is the memory - /// FAdditionalBuffer points to as well, and for some algorithms this part - /// of the memory may not be altered during initialization so it is - /// backupped to this memory location and restored after the IV got encrypted. - /// In DoDone it needs to be restored as well to prevent any unwanted - /// leftovers which might pose a security issue. - /// - FAdditionalBufferBackup: Pointer; - - /// - /// Checks whether the state machine is in one of the states specified as - /// parameter. If not a EDECCipherException will be raised. - /// - /// - /// List of states the state machine should be at currently - /// - procedure CheckState(States: TCipherStates); - - /// - /// Initialize the key, based on the key passed in - /// - /// - /// Encryption/Decryption key to be used - /// - /// - /// Size of the key passed in bytes. - /// - procedure DoInit(const Key; Size: Integer); virtual; abstract; - - /// - /// This abstract method needs to be overwritten by each concrete encryption - /// algorithm as this is the routine used internally to encrypt a single - /// block of data. - /// - /// - /// Data to be encrypted - /// - /// - /// In this memory the encrypted result will be written - /// - /// - /// Size of source in byte - /// - procedure DoEncode(Source, Dest: Pointer; Size: Integer); virtual; abstract; - /// - /// This abstract method needs to be overwritten by each concrete encryption - /// algorithm as this is the routine used internally to decrypt a single - /// block of data. - /// - /// - /// Data to be decrypted - /// - /// - /// In this memory the decrypted result will be written - /// - /// - /// Size of source in byte - /// - procedure DoDecode(Source, Dest: Pointer; Size: Integer); virtual; abstract; - /// - /// Securely fills the processing buffer with zeroes to make stealing data - /// from memory harder. - /// - procedure SecureErase; virtual; - - /// - /// Returns the currently set cipher block mode, means how blocks are - /// linked to each other in order to avoid certain attacks. - /// - function GetMode: TCipherMode; - - /// - /// Sets the cipher mode, means how each block is being linked with his - /// predecessor to avoid certain attacks - /// - procedure SetMode(Value: TCipherMode); - public - /// - /// List of registered DEC classes. Key is the Identity of the class. - /// - class var ClassList : TDECClassList; - - /// - /// Tries to find a class type by its name - /// - /// - /// Name to look for in the list - /// - /// - /// Returns the class type if found. if it could not be found a - /// EDECClassNotRegisteredException will be thrown - /// - class function ClassByName(const Name: string): TDECCipherClass; - - /// - /// Tries to find a class type by its numeric identity DEC assigned to it. - /// Useful for file headers, so they can easily encode numerically which - /// cipher class was being used. - /// - /// - /// Identity to look for - /// - /// - /// Returns the class type of the class with the specified identity value - /// or throws an EDECClassNotRegisteredException exception if no class - /// with the given identity has been found - /// - class function ClassByIdentity(Identity: Int64): TDECCipherClass; - - /// - /// Initializes the instance. Relies in parts on information given by the - /// Context class function. - /// - constructor Create; override; - /// - /// Frees internal structures and where necessary does so in a save way so - /// that data in those structures cannot be "stolen". - /// - destructor Destroy; override; - - /// - /// Provides meta data about the cipher algorithm used like key size. - /// To be overidden in the concrete cipher classes. - /// - /// - /// C++ does not support virtual static functions thus the base cannot be - /// marked 'abstract'. Calling this version of the method will lead to an - /// EDECAbstractError - /// - class function Context: TCipherContext; virtual; - - /// - /// Initializes the cipher with the necessary encryption/decryption key - /// - /// - /// Encryption/decryption key. Recommended/required key length is dependant - /// on the concrete algorithm. - /// - /// - /// Size of the key in bytes - /// - /// - /// Initialization vector. This contains the values the first block of - /// data to be processed is linked with. This is being done the same way - /// as the 2nd block of the data to be processed will be linked with the - /// first block and so on and this is dependant on the cypher mode set via - /// Mode property - /// - /// - /// Size of the initialization vector in bytes - /// - /// - /// optional parameter defining the value with which the last block will - /// be filled up if the size of the data to be processed cannot be divided - /// by block size without reminder. Means: if the last block is not - /// completely filled with data. - /// - procedure Init(const Key; Size: Integer; const IVector; IVectorSize: Integer; IFiller: Byte = $FF); overload; - /// - /// Initializes the cipher with the necessary encryption/decryption key - /// - /// - /// Encryption/decryption key. Recommended/required key length is dependant - /// on the concrete algorithm. - /// - /// - /// Initialization vector. This contains the values the first block of - /// data to be processed is linked with. This is being done the same way - /// as the 2nd block of the data to be processed will be linked with the - /// first block and so on and this is dependant on the cypher mode set via - /// Mode property - /// - /// - /// optional parameter defining the value with which the last block will - /// be filled up if the size of the data to be processed cannot be divided - /// by block size without reminder. Means: if the last block is not - /// completely filled with data. - /// - procedure Init(const Key: TBytes; const IVector: TBytes; IFiller: Byte = $FF); overload; - /// - /// Initializes the cipher with the necessary encryption/decryption key - /// - /// - /// Encryption/decryption key. Recommended/required key length is dependant - /// on the concrete algorithm. - /// - /// - /// Initialization vector. This contains the values the first block of - /// data to be processed is linked with. This is being done the same way - /// as the 2nd block of the data to be processed will be linked with the - /// first block and so on and this is dependant on the cypher mode set via - /// Mode property - /// - /// - /// optional parameter defining the value with which the last block will - /// be filled up if the size of the data to be processed cannot be divided - /// by block size without reminder. Means: if the last block is not - /// completely filled with data. - /// - procedure Init(const Key: RawByteString; const IVector: RawByteString = ''; IFiller: Byte = $FF); overload; - {$IFDEF ANSISTRINGSUPPORTED} - /// - /// Initializes the cipher with the necessary encryption/decryption key. - /// Only for use with the classic desktop compilers. - /// - /// - /// Encryption/decryption key. Recommended/required key length is dependant - /// on the concrete algorithm. - /// - /// - /// Initialization vector. This contains the values the first block of - /// data to be processed is linked with. This is being done the same way - /// as the 2nd block of the data to be processed will be linked with the - /// first block and so on and this is dependant on the cypher mode set via - /// Mode property - /// - /// - /// optional parameter defining the value with which the last block will - /// be filled up if the size of the data to be processed cannot be divided - /// by block size without reminder. Means: if the last block is not - /// completely filled with data. - /// - procedure Init(const Key: AnsiString; const IVector: AnsiString = ''; IFiller: Byte = $FF); overload; - {$ENDIF} - {$IFNDEF NEXTGEN} - /// - /// Initializes the cipher with the necessary encryption/decryption key. - /// Only for use with the classic desktop compilers. - /// - /// - /// Encryption/decryption key. Recommended/required key length is dependant - /// on the concrete algorithm. - /// - /// - /// Initialization vector. This contains the values the first block of - /// data to be processed is linked with. This is being done the same way - /// as the 2nd block of the data to be processed will be linked with the - /// first block and so on and this is dependant on the cypher mode set via - /// Mode property - /// - /// - /// optional parameter defining the value with which the last block will - /// be filled up if the size of the data to be processed cannot be divided - /// by block size without reminder. Means: if the last block is not - /// completely filled with data. - /// - procedure Init(const Key: WideString; const IVector: WideString = ''; IFiller: Byte = $FF); overload; - {$ENDIF} - - /// -{ TODO : Description needs to be revised } - /// Properly finishes the cryptographic operation. It needs to be called - /// at the end of encrypting or decrypting data, otherwise the last block - /// or last byte of the data will not be properly processed. - /// - procedure Done; - - // Encoding / Decoding Routines - // Do not add further methods of that kind here! If needed add them to - // TDECFormattedCipher in DECCipherFormats or inherit from that one. - - /// - /// Encrypts the contents of a RawByteString. This method is deprecated - /// and should be replaced by a variant expecting TBytes as source in - /// order to not support mistreating strings as binary buffers. - /// - /// - /// This is the direct successor of the EncodeBinary method from DEC 5.2. - /// When block chaining mode ECBx is used - /// (not recommended!), the size of the data passed via this parameter - /// needs to be a multiple of the block size of the algorithm used, - /// otherwise a EDECCipherException exception will be raised! - /// - /// - /// The data to be encrypted - /// - /// - /// Optional parameter. Here a formatting method can be passed. The - /// resulting encrypted data will be formatted with this function, if one - /// has been passed. Examples are hex or base 64 formatting. - /// - /// - /// Encrypted data. Init must have been called previously. - /// - function EncodeRawByteString(const Source: RawByteString; - Format: TDECFormatClass = nil): RawByteString; deprecated; // please use EncodeBytes functions now - /// - /// Decrypts the contents of a RawByteString. This method is deprecated - /// and should be replaced by a variant expecting TBytes as source in - /// order to not support mistreating strings as binary buffers. - /// - /// - /// This is the direct successor of the DecodeBinary method from DEC 5.2 - /// When block chaining mode ECBx is used - /// (not recommended!), the size of the data passed via this parameter - /// needs to be a multiple of the block size of the algorithm used, - /// otherwise a EDECCipherException exception will be raised! - /// - /// - /// The data to be decrypted - /// - /// - /// Optional parameter. Here a formatting method can be passed. The - /// data to be decrypted will be formatted with this function, if one - /// has been passed. Examples are hex or base 64 formatting. - /// This is used for removing a formatting applied by the EncodeRawByteString - /// method. - /// - /// - /// Decrypted data. Init must have been called previously. - /// - function DecodeRawByteString(const Source: RawByteString; - Format: TDECFormatClass = nil): RawByteString; deprecated; // please use DecodeBytes functions now - - /// - /// Encrypts the contents of a ByteArray. - /// - /// - /// The data to be encrypted. When block chaining mode ECBx is used - /// (not recommended!), the size of the data passed via this parameter - /// needs to be a multiple of the block size of the algorithm used, - /// otherwise a EDECCipherException exception will be raised! - /// - /// - /// Optional parameter. Here a formatting method can be passed. The - /// resulting encrypted data will be formatted with this function, if one - /// has been passed. Examples are hex or base 64 formatting. - /// - /// - /// Encrypted data. Init must have been called previously. - /// - function EncodeBytes(const Source: TBytes; Format: TDECFormatClass = nil): TBytes; - /// - /// Decrypts the contents of a ByteArray. - /// - /// - /// The data to be decrypted. When block chaining mode ECBx is used - /// (not recommended!), the size of the data passed via this parameter - /// needs to be a multiple of the block size of the algorithm used, - /// otherwise a EDECCipherException exception will be raised! - /// - /// - /// Optional parameter. Here a formatting method can be passed. The - /// data to be decrypted will be formatted with this function, if one - /// has been passed. Examples are hex or base 64 formatting. - /// This is used for removing a formatting applied by the EncodeRawByteString - /// method. - /// - /// - /// Decrypted data. Init must have been called previously. - /// - function DecodeBytes(const Source: TBytes; Format: TDECFormatClass): TBytes; - - // CalcMACBytes deferred since the current implementation would neither be - // performant (that would require another TFormatBase.Encode variant from - // pointer to TBytes and that would require a new method name as overloads - // may not differ in return values only and it would require a lot of unit - // tests to get implemented. Deferred in particular also due to not yet - // really understanding the purpose of CalcMAC -// function CalcMACByte(Format: TDECFormatClass = nil): TBytes; overload; - - // Deprecated directive commented out, as replacement CalcMACByte has not - // been implemented yet, see remark above. Use case for CalcMAC is not clear - // yet either. - function CalcMAC(Format: TDECFormatClass = nil): RawByteString; overload; //deprecated; // please use the TBytes based overload; - - // properties - - /// - /// Provides the size of the initialization vector in bytes. - /// - property InitVectorSize: Integer - read FBufferSize; - /// - /// Provides access to the contents of the initialization vector - /// - property InitVector: PByteArray - read FInitializationVector; - - /// - /// Cipher modes are used to derive a stream cipher from block cipher - /// algorithms. For this something from the last entrypted block (or for - /// the first block from the vector) is used in the encryption of the next - /// block. It may be XORed with the next block cipher text for instance. - /// That data "going into the next block encryption" is stored in this - /// feedback array. The size usually depends on the block size of the - /// cipher algorithm. - /// - property Feedback: PByteArray - read FFeedback; - /// - /// Allows to query the current internal processing state - /// - property State: TCipherState - read FState; - /// - /// Mode used for padding data to be encrypted/decrypted. See TCipherMode. - /// - property Mode: TCipherMode - read GetMode - write SetMode; - - /// - /// Mode used for filling up an incomplete last block in a block cipher - /// - property FillMode: TBlockFillMode - read FFillMode - write FFillMode; - end; - -/// -/// Returns the passed cipher class type if it is not nil. Otherwise the -/// class type class set per SetDefaultCipherClass is being returned. If using -/// the DECCiphers unit that one registers TCipher_Null in the initialization -/// -/// -/// Class type of a cipher class like TCipher_Blowfish or nil, if no -/// encryption/decryption is desired. -/// -/// -/// Passed class type or defined default cipher class type, depending on -/// CipherClass parameter value. -/// -function ValidCipher(CipherClass: TDECCipherClass = nil): TDECCipherClass; - -/// -/// Defines which cipher class to return by ValidCipher if passing nil to that -/// -/// -/// Class type of a cipher class to return by ValidCipher if passing nil to -/// that one. This parameter should not be nil! -/// -procedure SetDefaultCipherClass(CipherClass: TDECCipherClass); - -implementation - -uses - {$IFDEF FPC} - TypInfo, - {$ELSE} - System.TypInfo, - {$ENDIF} - DECUtil; - -{$IFOPT Q+}{$DEFINE RESTORE_OVERFLOWCHECKS}{$Q-}{$ENDIF} -{$IFOPT R+}{$DEFINE RESTORE_RANGECHECKS}{$R-}{$ENDIF} - -resourcestring - sAlreadyPadded = 'Cipher has already been padded, cannot process message'; - sInvalidState = 'Cipher is not in valid state for this action'; - sNoKeyMaterialGiven = 'No Keymaterial given (Security Issue)'; - sKeyMaterialTooLarge = 'Keymaterial is too large for use (Security Issue)'; - sIVMaterialTooLarge = 'Initvector is too large for use (Security Issue)'; - sInvalidMACMode = 'Invalid Cipher mode to compute MAC'; - sCipherNoDefault = 'No default cipher has been registered'; - -var - /// - /// Cipher class returned by ValidCipher if nil is passed as parameter to it - /// - FDefaultCipherClass: TDECCipherClass = nil; - -function ValidCipher(CipherClass: TDECCipherClass): TDECCipherClass; -begin - if CipherClass <> nil then - Result := CipherClass - else - Result := FDefaultCipherClass; - - if Result = nil then - raise EDECCipherException.CreateRes(@sCipherNoDefault); -end; - -procedure SetDefaultCipherClass(CipherClass: TDECCipherClass); -begin - Assert(Assigned(CipherClass), 'Do not set a nil default cipher class!'); - - FDefaultCipherClass := CipherClass; -end; - -{ TDECCipher } - -constructor TDECCipher.Create; -var - MustAdditionalBufferSave: Boolean; -begin - inherited Create; - - FBufferSize := Context.BufferSize; - FAdditionalBufferSize := Context.AdditionalBufferSize; - MustAdditionalBufferSave := Context.NeedsAdditionalBufferBackup; - - // Initialization vector, feedback, buffer, additional buffer - FDataSize := FBufferSize * 3 + FAdditionalBufferSize; - - if MustAdditionalBufferSave then - // if contents of the FAdditionalBuffer needs to be saved increase buffer size - // by FAdditionalBufferSize so FAdditionalBuffer and then FAdditionalBufferBackup - // fit in the buffer - Inc(FDataSize, FAdditionalBufferSize); - - // ReallocMemory instead of ReallocMem due to C++ compatibility as per 10.1 help - FData := ReallocMemory(FData, FDataSize); - FInitializationVector := @FData[0]; - FFeedback := @FInitializationVector[FBufferSize]; - FBuffer := @FFeedback[FBufferSize]; - FAdditionalBuffer := @FBuffer[FBufferSize]; - - if MustAdditionalBufferSave then - // buffer contents: FData, then FFeedback, then FBuffer then FAdditionalBuffer - FAdditionalBufferBackup := @PByteArray(FAdditionalBuffer)[FAdditionalBufferSize] - else - FAdditionalBufferBackup := nil; - - FFillMode := fmByte; - FState := csNew; - - SecureErase; -end; - -destructor TDECCipher.Destroy; -begin - SecureErase; - // FreeMem instead of ReallocMemory which produced a memory leak. ReallocMemory - // was used instead of ReallocMem due to C++ compatibility as per 10.1 help - FreeMem(FData, FDataSize); - FInitializationVector := nil; - FFeedback := nil; - FBuffer := nil; - FAdditionalBuffer := nil; - FAdditionalBufferBackup := nil; - inherited Destroy; -end; - -procedure TDECCipher.SetMode(Value: TCipherMode); -begin - if Value <> FMode then - begin - if not (FState in [csNew, csInitialized, csDone]) then - Done; - - FMode := Value; - end; -end; - -procedure TDECCipher.CheckState(States: TCipherStates); -begin - if not (FState in States) then - begin - if FState = csPadded then - raise EDECCipherException.CreateRes(@sAlreadyPadded) - else - raise EDECCipherException.CreateRes(@sInvalidState); - end; -end; - -class function TDECCipher.ClassByIdentity(Identity: Int64): TDECCipherClass; -begin - result := TDECCipherClass(ClassList.ClassByIdentity(Identity)); -end; - -class function TDECCipher.ClassByName(const Name: string): TDECCipherClass; -begin - result := TDECCipherClass(ClassList.ClassByName(Name)); -end; - -class function TDECCipher.Context: TCipherContext; -begin - // C++ does not support virtual static functions thus the base cannot be - // marked 'abstract'. This is our workaround: - raise EDECAbstractError.Create(GetShortClassName); -end; - -procedure TDECCipher.Init(const Key; Size: Integer; const IVector; IVectorSize: Integer; IFiller: Byte); -begin - FState := csNew; - SecureErase; - - if (Size > Context.KeySize) and (not (ctNull in Context.CipherType)) then - raise EDECCipherException.CreateRes(@sKeyMaterialTooLarge); - - if IVectorSize > FBufferSize then - raise EDECCipherException.CreateRes(@sIVMaterialTooLarge); - - DoInit(Key, Size); - if FAdditionalBufferBackup <> nil then - // create backup of FBuffer - Move(FAdditionalBuffer^, FAdditionalBufferBackup^, FAdditionalBufferSize); - - FillChar(FInitializationVector^, FBufferSize, IFiller); - if IVectorSize = 0 then - begin - DoEncode(FInitializationVector, FInitializationVector, FBufferSize); - if FAdditionalBufferBackup <> nil then - // Restore backup fo FBuffer - Move(FAdditionalBufferBackup^, FAdditionalBuffer^, FAdditionalBufferSize); - end - else - Move(IVector, FInitializationVector^, IVectorSize); - - Move(FInitializationVector^, FFeedback^, FBufferSize); - - FState := csInitialized; -end; - -procedure TDECCipher.Init(const Key: TBytes; const IVector: TBytes; IFiller: Byte = $FF); -begin - if (Length(Key) = 0) and (not (ctNull in Context.CipherType)) then - raise EDECCipherException.CreateRes(@sNoKeyMaterialGiven); - - if IVector <> nil then - Init(Key[0], Length(Key), IVector[0], Length(IVector), IFiller) - else - Init(Key[0], Length(Key), NullStr, 0, IFiller); -end; - -procedure TDECCipher.Init(const Key: RawByteString; const IVector: RawByteString = ''; IFiller: Byte = $FF); -begin - if (Length(Key) = 0) and (not (ctNull in Context.CipherType)) then - raise EDECCipherException.CreateRes(@sNoKeyMaterialGiven); - - if Length(IVector) > 0 then - {$IF CompilerVersion >= 24.0} - Init(Key[Low(Key)], Length(Key) * SizeOf(Key[Low(Key)]), - IVector[Low(IVector)], Length(IVector) * SizeOf(IVector[Low(IVector)]), IFiller) - {$ELSE} - Init(Key[1], Length(Key) * SizeOf(Key[1]), - IVector[1], Length(IVector) * SizeOf(IVector[1]), IFiller) - {$IFEND} - else - {$IF CompilerVersion >= 24.0} - Init(Key[Low(Key)], Length(Key) * SizeOf(Key[Low(Key)]), NullStr, 0, IFiller); - {$ELSE} - Init(Key[1], Length(Key) * SizeOf(Key[1]), NullStr, 0, IFiller); - {$IFEND} -end; - - -{$IFDEF ANSISTRINGSUPPORTED} -procedure TDECCipher.Init(const Key, IVector: AnsiString; IFiller: Byte); -begin - if (Length(Key) = 0) and (not (ctNull in Context.CipherType)) then - raise EDECCipherException.Create(sNoKeyMaterialGiven); - - if Length(IVector) > 0 then - {$IF CompilerVersion >= 24.0} - Init(Key[Low(Key)], Length(Key) * SizeOf(Key[Low(Key)]), - IVector[Low(IVector)], Length(IVector) * SizeOf(Low(IVector)), IFiller) - {$ELSE} - Init(Key[1], Length(Key) * SizeOf(Key[Low(Key)]), - IVector[IVector[1]], Length(IVector) * SizeOf(IVector[1]), IFiller) - {$IFEND} - else - {$IF CompilerVersion >= 24.0} - Init(Key[Low(Key)], Length(Key) * SizeOf(Key[Low(Key)]), NullStr, 0, IFiller); - {$ELSE} - Init(Key[1], Length(Key) * SizeOf(Key[1]), NullStr, 0, IFiller); - {$IFEND} -end; -{$ENDIF} - - -{$IFNDEF NEXTGEN} -procedure TDECCipher.Init(const Key, IVector: WideString; IFiller: Byte); -begin - if (Length(Key) = 0) and (not (ctNull in Context.CipherType)) then - raise EDECCipherException.CreateRes(@sNoKeyMaterialGiven); - - if Length(IVector) > 0 then - {$IF CompilerVersion >= 24.0} - Init(Key[Low(Key)], Length(Key) * SizeOf(Key[Low(Key)]), - IVector[Low(IVector)], Length(IVector) * SizeOf(IVector[Low(IVector)]), IFiller) - {$ELSE} - Init(Key[1], Length(Key) * SizeOf(Key[1]), - IVector[1], Length(IVector) * SizeOf(IVector[1]), IFiller) - {$IFEND} - else - {$IF CompilerVersion >= 24.0} - Init(Key[Low(Key)], Length(Key) * SizeOf(Key[Low(Key)]), NullStr, 0, IFiller); - {$ELSE} - Init(Key[1], Length(Key) * SizeOf(Key[1]), NullStr, 0, IFiller); - {$IFEND} -end; -{$ENDIF} - -procedure TDECCipher.Done; -begin - if FState <> csDone then - begin - FState := csDone; - FBufferIndex := 0; - DoEncode(FFeedback, FBuffer, FBufferSize); - Move(FInitializationVector^, FFeedback^, FBufferSize); - if FAdditionalBufferBackup <> nil then - Move(FAdditionalBufferBackup^, FAdditionalBuffer^, FAdditionalBufferSize); - end; -end; - -procedure TDECCipher.SecureErase; -begin - ProtectBuffer(FData[0], FDataSize); -end; - -function TDECCipher.EncodeRawByteString(const Source: RawByteString; Format: TDECFormatClass): RawByteString; -var - b : TBytes; -begin - SetLength(b, 0); - if Length(Source) > 0 then - begin - {$IF CompilerVersion >= 24.0} - SetLength(b, Length(Source) * SizeOf(Source[Low(Source)])); - DoEncode(@Source[low(Source)], @b[0], Length(Source) * SizeOf(Source[low(Source)])); - {$ELSE} - SetLength(b, Length(Source) * SizeOf(Source[1])); - DoEncode(@Source[1], @b[0], Length(Source) * SizeOf(Source[1])); - {$IFEND} - Result := BytesToRawString(ValidFormat(Format).Encode(b)); - end; -end; - -function TDECCipher.GetMode: TCipherMode; -begin - Result := FMode; -end; - -function TDECCipher.EncodeBytes(const Source: TBytes; Format: TDECFormatClass = nil): TBytes; -begin - SetLength(Result, 0); - if Length(Source) > 0 then - begin - SetLength(Result, Length(Source) * SizeOf(Source[0])); - DoEncode(@Source[0], @Result[0], Length(Source) * SizeOf(Source[0])); - Result := ValidFormat(Format).Encode(Result); - end; -end; - -function TDECCipher.DecodeRawByteString(const Source: RawByteString; Format: TDECFormatClass): RawByteString; -var - b : TBytes; -begin - SetLength(Result, 0); - if Length(Source) > 0 then - begin - // Delphi 10.1 Berlin and 10.2 Tokyo will issue a W1057 implicit string - // conversion warning here because the RawByteString BytesOf function is by - // mistake in a $IFNDEF NEXTGEN block. See QP report: - // https://quality.embarcadero.com/browse/RSP-20574 - // This has been fixed in 10.3.0 Rio - b := ValidFormat(Format).Decode(BytesOf(Source)); - - {$IF CompilerVersion >= 24.0} - DoDecode(@b[0], @Result[Low(Result)], Length(Result) * SizeOf(Result[Low(Result)])); - {$ELSE} - DoDecode(@b[0], @Result[1], Length(Result) * SizeOf(Result[1])); - {$IFEND} - end; -end; - -function TDECCipher.DecodeBytes(const Source: TBytes; Format: TDECFormatClass): TBytes; -begin - SetLength(Result, 0); - if Length(Source) > 0 then - begin - Result := ValidFormat(Format).Decode(Source); - DoDecode(@Result[0], @Result[0], Length(Result) * SizeOf(Result[0])); - end; -end; - - -function TDECCipher.CalcMAC(Format: TDECFormatClass): RawByteString; -begin - Done; - if FMode in [cmECBx] then - raise EDECException.CreateRes(@sInvalidMACMode) - else - Result := ValidFormat(Format).Encode(FBuffer^, FBufferSize); - { TODO : How to rewrite? EncodeBytes cannot be called directly like that } -end; - -//function TDECCipher.CalcMACByte(Format: TDECFormatClass): TBytes; -//begin -// Done; -// if FMode in [cmECBx] then -// raise EDECCipherException.Create(sInvalidMACMode) -// else -// begin -// Result := System.SysUtils.BytesOf(ValidFormat(Format).Encode(FBuffer^, FBufferSize)); -// end; -//end; - -{$IFDEF RESTORE_RANGECHECKS}{$R+}{$ENDIF} -{$IFDEF RESTORE_OVERFLOWCHECKS}{$Q+}{$ENDIF} - -{$IFDEF DELPHIORBCB} -procedure ModuleUnload(Instance: NativeInt); -var // automaticaly deregistration/releasing - i: Integer; -begin - if TDECCipher.ClassList <> nil then - begin - for i := TDECCipher.ClassList.Count - 1 downto 0 do - begin - if NativeInt(FindClassHInstance(TClass(TDECCipher.ClassList[i]))) = Instance then - TDECCipher.ClassList.Remove(TDECCipher.ClassList[i].Identity); - end; - end; -end; -{$ENDIF DELPHIORBCB} - -initialization - // Code for packages and dynamic extension of the class registration list - {$IFDEF DELPHIORBCB} - AddModuleUnloadProc(ModuleUnload); - {$ENDIF DELPHIORBCB} - - TDECCipher.ClassList := TDECClassList.Create; - -finalization - // Ensure no further instances of classes registered in the registraiotn list - // are possible through the list after this unit has been unloaded by unloding - // the package this unit is in - {$IFDEF DELPHIORBCB} - RemoveModuleUnloadProc(ModuleUnload); - {$ENDIF DELPHIORBCB} - - TDECCipher.ClassList.Free; -end. diff --git a/Source/DECCipherFormats.pas b/Source/DECCipherFormats.pas deleted file mode 100644 index a65be5c5..00000000 --- a/Source/DECCipherFormats.pas +++ /dev/null @@ -1,1037 +0,0 @@ -{***************************************************************************** - The DEC team (see file NOTICE.txt) licenses this file - to you under the Apache License, Version 2.0 (the - "License"); you may not use this file except in compliance - with the License. A copy of this licence is found in the root directory - of this project in the file LICENCE.txt or alternatively at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, - software distributed under the License is distributed on an - "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - KIND, either express or implied. See the License for the - specific language governing permissions and limitations - under the License. -*****************************************************************************} -unit DECCipherFormats; - -interface - -uses - {$IFDEF FPC} - SysUtils, Classes, - {$ELSE} - System.SysUtils, System.Classes, - {$ENDIF} - DECCipherBase, DECCipherModes, DECUtil, DECFormatBase, DECCipherInterface; - -type - /// - /// Class in which the various encode/decode variants provided have been - /// moved in order to keep the base cipher class small and clean. - /// - TDECFormattedCipher = class(TDECCipherModes, IDECCipher) - private - /// - /// Encrypts or decrypts the data contained in a given stream - /// - /// - /// Source stream containing the data to encrypt or to decrypt - /// - /// - /// Destination stream, where the encrypted or decrypted data shall be put in - /// - /// - /// Number of bytes of Source to be encrypted or decrypted - /// - /// - /// Callback which either encrypts or decrypts the stream, depending on - /// which one is being passed - /// - /// - /// optional callback for reporting progress of the operation - /// - procedure DoEncodeDecodeStream(const Source, Dest: TStream; DataSize: Int64; - const CipherProc: TDECCipherCodeEvent; - const OnProgress: TDECProgressEvent); - - /// - /// Encrypts or decrypts a file and stores the result in another file - /// - /// - /// Path and name of the file to encrypt - /// - /// - /// Path and name of the file the encrypted data shall be stored in - /// - /// - /// This method does the actual encrypting or decrypting of the data. - /// Usually the Encode or Decode method is being passed here which is - /// declared in TDECCipherBase as virtual abstract method and - /// implemented in the individual cipher class inheriting from this one - /// - /// - /// Optional event which can be passed to get information about the - /// progress of the encryption operation - /// - procedure DoEncodeDecodeFile(const SourceFileName, DestFileName: string; - const Proc: TDECCipherCodeEvent; - const OnProgress: TDECProgressEvent); - public - /// - /// Encrypts the contents of a given byte array - /// - /// - /// Byte array with data to be encrypted. When block chaining mode ECBx - /// is used (not recommended!), the size of the data passed via this - /// parameter needs to be a multiple of the block size of the algorithm used, - /// otherwise a EDECCipherException exception will be raised! - /// - /// - /// Byte array with encrypted data - /// - function EncodeBytes(const Source: TBytes): TBytes; - - /// - /// Decrypts the contents of a given byte array - /// - /// - /// Byte array with data to be decrypted. When block chaining mode ECBx - /// is used (not recommended!), the size of the data passed via this - /// parameter needs to be a multiple of the block size of the algorithm used, - /// otherwise a EDECCipherException exception will be raised! - /// - /// - /// Byte array with decrypted data - /// - function DecodeBytes(const Source: TBytes): TBytes; - - /// - /// Encrypts the data contained in a given stream - /// - /// - /// Source stream containing the data to encrypt. When block chaining mode ECBx - /// is used (not recommended!), the size of the data passed via this - /// parameter needs to be a multiple of the block size of the algorithm used, - /// otherwise a EDECCipherException exception will be raised! - /// - /// - /// Destination stream, where the encrypted data shall be put in - /// - /// - /// Number of bytes of Source to be encrypted - /// - /// - /// optional callback for reporting progress of the operation - /// - procedure EncodeStream(const Source, Dest: TStream; DataSize: Int64; - const OnProgress: TDECProgressEvent = nil); - - /// - /// Decrypts the data contained in a given stream - /// - /// - /// Source stream containing the data to decrypt. When block chaining mode ECBx - /// is used (not recommended!), the size of the data passed via this - /// parameter needs to be a multiple of the block size of the algorithm used, - /// otherwise a EDECCipherException exception will be raised! - /// - /// - /// Destination stream, where the decrypted data shall be put in - /// - /// - /// Number of bytes of Source to be decrypted - /// - /// - /// optional callback for reporting progress of the operation - /// - procedure DecodeStream(const Source, Dest: TStream; DataSize: Int64; - const OnProgress: TDECProgressEvent = nil); - - /// - /// Reads the contents of one file, encrypts it and stores it in another file - /// - /// - /// Path and name of the file to encrypt. When block chaining mode ECBx - /// is used (not recommended!), the size of the data passed via this - /// parameter needs to be a multiple of the block size of the algorithm - /// used, otherwise a EDECCipherException exception will be raised! - /// - /// - /// Path and name of the file the encrypted data shall be stored in - /// - /// - /// Optional event which can be passed to get information about the - /// progress of the encryption operation - /// - procedure EncodeFile(const SourceFileName, DestFileName: string; - const OnProgress: TDECProgressEvent = nil); - - /// - /// Reads the contents of one file, decrypts it and stores it in another file - /// - /// - /// Path and name of the file to decrypt. When block chaining mode ECBx - /// is used (not recommended!), the size of the data passed via this - /// parameter needs to be a multiple of the block size of the algorithm - /// used, otherwise a EDECCipherException exception will be raised! - /// - /// - /// Path and name of the file the decrypted data shall be stored in - /// - /// - /// Optional event which can be passed to get information about the - /// progress of the decryption operation - /// - procedure DecodeFile(const SourceFileName, DestFileName: string; - const OnProgress: TDECProgressEvent = nil); - - /// - /// Encrypts the contents of the passed unicode string - /// - /// - /// String to encrypt. When block chaining mode ECBx - /// is used (not recommended!), the size of the data passed via this - /// parameter needs to be a multiple of the block size of the algorithm - /// used, otherwise a EDECCipherException exception will be raised! - /// - /// - /// Optional parameter. One can pass a class reference of one of the - /// concrete data formatting classes here which will be internally used - /// to convert the data. Encoded will be the encrypted data, not the - /// source data. Formattings can be used to convert data into a format - /// suitable for the transport medium the data shall be transported with. - /// - /// - /// Encrypted string as a byte array - /// - function EncodeStringToBytes(const Source: string; - Format: TDECFormatClass = nil): TBytes; overload; - - /// - /// Encrypts the contents of the passed RawByteString - /// - /// - /// String to encrypt. When block chaining mode ECBx - /// is used (not recommended!), the size of the data passed via this - /// parameter needs to be a multiple of the block size of the algorithm - /// used, otherwise a EDECCipherException exception will be raised! - /// - /// - /// Optional parameter. One can pass a class reference of one of the - /// concrete data formatting classes here which will be internally used - /// to convert the data. Encoded will be the encrypted data, not the - /// source data. Formattings can be used to convert data into a format - /// suitable for the transport medium the data shall be transported with. - /// - /// - /// Encrypted string as a byte array - /// - function EncodeStringToBytes(const Source: RawByteString; - Format: TDECFormatClass = nil): TBytes; overload; - - /// - /// Encrypts the contents of the passed unicode string - /// - /// - /// String to encrypt. When block chaining mode ECBx - /// is used (not recommended!), the size of the data passed via this - /// parameter needs to be a multiple of the block size of the algorithm - /// used, otherwise a EDECCipherException exception will be raised! - /// - /// - /// Optional parameter. One can pass a class reference of one of the - /// concrete data formatting classes here which will be internally used - /// to convert the data. Encoded will be the encrypted data, not the - /// source data. Formattings can be used to convert data into a format - /// suitable for the transport medium the data shall be transported with. - /// - /// - /// Encrypted string - /// - /// - /// The use of this method is only recommended if a formatting is passed - /// which will result in an 7-bit ASCII compatible string as we cannot - /// ensure that Unicode string processing will not alter/interpret some - /// byte combinations in a destructive way, making the encrypted string - /// un-decryptable. - /// - function EncodeStringToString(const Source: string; - Format: TDECFormatClass = nil): string; overload; - - /// - /// Encrypts the contents of the passed unicode string - /// - /// - /// String to encrypt. When block chaining mode ECBx - /// is used (not recommended!), the size of the data passed via this - /// parameter needs to be a multiple of the block size of the algorithm - /// used, otherwise a EDECCipherException exception will be raised! - /// - /// - /// Optional parameter. One can pass a class reference of one of the - /// concrete data formatting classes here which will be internally used - /// to convert the data. Encoded will be the encrypted data, not the - /// source data. Formattings can be used to convert data into a format - /// suitable for the transport medium the data shall be transported with. - /// - /// - /// Encrypted string - /// - /// - /// The use of this method is only recommended if a formatting is passed - /// which will result in an 7-bit ASCII compatible string as we cannot - /// ensure that string processing will not alter/interpret some - /// byte combinations in a destructive way, making the encrypted string - /// un-decryptable. - /// - function EncodeStringToString(const Source: RawByteString; - Format: TDECFormatClass = nil): RawByteString; overload; - - /// - /// Decrypts the contents of the passed encrypted unicode string - /// - /// - /// String to decrypt. When block chaining mode ECBx - /// is used (not recommended!), the size of the data passed via this - /// parameter needs to be a multiple of the block size of the algorithm - /// used, otherwise a EDECCipherException exception will be raised! - /// - /// - /// Optional parameter. One can pass a class reference of one of the - /// concrete data formatting classes here which will be internally used - /// to convert the data. Decoded will be the still encrypted data, not the - /// encrypted data. Formattings can be used to convert data into a format - /// suitable for the transport medium the data shall be transported with. - /// - /// - /// Decrypted string as a byte array - /// - function DecodeStringToBytes(const Source: string; - Format: TDECFormatClass = nil): TBytes; overload; - - /// - /// Decrypts the contents of the passed encrypted RawByteString - /// - /// - /// String to decrypt. When block chaining mode ECBx - /// is used (not recommended!), the size of the data passed via this - /// parameter needs to be a multiple of the block size of the algorithm - /// used, otherwise a EDECCipherException exception will be raised! - /// - /// - /// Optional parameter. One can pass a class reference of one of the - /// concrete data formatting classes here which will be internally used - /// to convert the data. Decoded will be the still encrypted data, not the - /// encrypted data. Formattings can be used to convert data into a format - /// suitable for the transport medium the data shall be transported with. - /// - /// - /// Decrypted string as a byte array - /// - function DecodeStringToBytes(const Source: RawByteString; - Format: TDECFormatClass = nil): TBytes; overload; - - /// - /// Decrypts the contents of the passed Unicode string - /// - /// - /// String to decrypt. When block chaining mode ECBx - /// is used (not recommended!), the size of the data passed via this - /// parameter needs to be a multiple of the block size of the algorithm - /// used, otherwise a EDECCipherException exception will be raised! - /// - /// - /// Optional parameter. One can pass a class reference of one of the - /// concrete data formatting classes here which will be internally used - /// to convert the data. Decoded will be the encrypted data, not the - /// decrypted data. Formattings can be used to convert data into a format - /// suitable for the transport medium the data shall be transported with. - /// - /// - /// Decrypted string - /// - /// - /// The use of this method is only recommended if a formatting is passed - /// which uses an 7-bit ASCII compatible string as input so that it - /// didn't get altered by Unicode string processing in some hafrmful way - /// - function DecodeStringToString(const Source: string; - Format: TDECFormatClass = nil): string; overload; - - /// - /// Decrypts the contents of the passed RawByteString string - /// - /// - /// String to decrypt. When block chaining mode ECBx - /// is used (not recommended!), the size of the data passed via this - /// parameter needs to be a multiple of the block size of the algorithm - /// used, otherwise a EDECCipherException exception will be raised! - /// - /// - /// Optional parameter. One can pass a class reference of one of the - /// concrete data formatting classes here which will be internally used - /// to convert the data. Decoded will be the encrypted data, not the - /// decrypted data. Formattings can be used to convert data into a format - /// suitable for the transport medium the data shall be transported with. - /// - /// - /// Decrypted string - /// - /// - /// The use of this method is only recommended if a formatting is passed - /// which uses an 7-bit ASCII compatible string as input so that it - /// didn't get altered by string processing in some hafrmful way - /// - function DecodeStringToString(const Source: RawByteString; - Format: TDECFormatClass = nil): RawByteString; overload; - -{$IFDEF ANSISTRINGSUPPORTED} - /// - /// Encrypts the contents of the passed Ansistring - /// - /// - /// String to encrypt. When block chaining mode ECBx - /// is used (not recommended!), the size of the data passed via this - /// parameter needs to be a multiple of the block size of the algorithm - /// used, otherwise a EDECCipherException exception will be raised! - /// - /// - /// Optional parameter. One can pass a class reference of one of the - /// concrete data formatting classes here which will be internally used - /// to convert the data. Encoded will be the encrypted data, not the - /// source data. Formattings can be used to convert data into a format - /// suitable for the transport medium the data shall be transported with. - /// - /// - /// Encrypted string as a byte array - /// - function EncodeStringToBytes(const Source: AnsiString; - Format: TDECFormatClass = nil): TBytes; overload; - - /// - /// Encrypts the contents of the passed Ansistring - /// - /// - /// String to encrypt. When block chaining mode ECBx - /// is used (not recommended!), the size of the data passed via this - /// parameter needs to be a multiple of the block size of the algorithm - /// used, otherwise a EDECCipherException exception will be raised! - /// - /// - /// Optional parameter. One can pass a class reference of one of the - /// concrete data formatting classes here which will be internally used - /// to convert the data. Encoded will be the encrypted data, not the - /// source data. Formattings can be used to convert data into a format - /// suitable for the transport medium the data shall be transported with. - /// - /// - /// Encrypted string as an AnsiString - /// - /// - /// The use of this method is only recommended if a formatting is passed - /// which will result in an 7-bit ASCII compatible string as we cannot - /// ensure that string processing will not alter/interpret some - /// byte combinations in a destructive way, making the encrypted string - /// un-decryptable. - /// - function EncodeStringToString(const Source: AnsiString; - Format: TDECFormatClass = nil): AnsiString; overload; - - /// - /// Decrypts the contents of the passed encrypted Ansistring - /// - /// - /// String to decrypt. When block chaining mode ECBx - /// is used (not recommended!), the size of the data passed via this - /// parameter needs to be a multiple of the block size of the algorithm - /// used, otherwise a EDECCipherException exception will be raised! - /// - /// - /// Optional parameter. One can pass a class reference of one of the - /// concrete data formatting classes here which will be internally used - /// to convert the data. Decoded will be the still encrypted data, not the - /// encrypted data. Formattings can be used to convert data into a format - /// suitable for the transport medium the data shall be transported with. - /// - /// - /// Decrypted string as a byte array - /// - function DecodeStringToBytes(const Source: AnsiString; - Format: TDECFormatClass = nil): TBytes; overload; - - /// - /// Decrypts the contents of the passed AnsiString string - /// - /// - /// String to decrypt. When block chaining mode ECBx - /// is used (not recommended!), the size of the data passed via this - /// parameter needs to be a multiple of the block size of the algorithm - /// used, otherwise a EDECCipherException exception will be raised! - /// - /// - /// Optional parameter. One can pass a class reference of one of the - /// concrete data formatting classes here which will be internally used - /// to convert the data. Decoded will be the encrypted data, not the - /// decrypted data. Formattings can be used to convert data into a format - /// suitable for the transport medium the data shall be transported with. - /// - /// - /// Decrypted string - /// - /// - /// The use of this method is only recommended if a formatting is passed - /// which uses an 7-bit ASCII compatible string as input so that it - /// didn't get altered by string processing in some hafrmful way - /// - function DecodeStringToString(const Source: AnsiString; - Format: TDECFormatClass = nil): AnsiString; overload; -{$ENDIF} - -{$IFNDEF NEXTGEN} - /// - /// Encrypts the contents of the passed Widestring - /// - /// - /// String to encrypt. When block chaining mode ECBx - /// is used (not recommended!), the size of the data passed via this - /// parameter needs to be a multiple of the block size of the algorithm - /// used, otherwise a EDECCipherException exception will be raised! - /// - /// - /// Optional parameter. One can pass a class reference of one of the - /// concrete data formatting classes here which will be internally used - /// to convert the data. Encoded will be the encrypted data, not the - /// source data. Formattings can be used to convert data into a format - /// suitable for the transport medium the data shall be transported with. - /// - /// - /// Encrypted string as a byte array - /// - function EncodeStringToBytes(const Source: WideString; - Format: TDECFormatClass = nil): TBytes; overload; - - /// - /// Encrypts the contents of the passed Widestring - /// - /// - /// String to encrypt. When block chaining mode ECBx - /// is used (not recommended!), the size of the data passed via this - /// parameter needs to be a multiple of the block size of the algorithm - /// used, otherwise a EDECCipherException exception will be raised! - /// - /// - /// Optional parameter. One can pass a class reference of one of the - /// concrete data formatting classes here which will be internally used - /// to convert the data. Encoded will be the encrypted data, not the - /// source data. Formattings can be used to convert data into a format - /// suitable for the transport medium the data shall be transported with. - /// - /// - /// Encrypted string as an WideString - /// - /// - /// The use of this method is only recommended if a formatting is passed - /// which will result in an 7-bit ASCII compatible string as we cannot - /// ensure that string processing will not alter/interpret some - /// byte combinations in a destructive way, making the encrypted string - /// un-decryptable. - /// - function EncodeStringToString(const Source: WideString; - Format: TDECFormatClass = nil): WideString; overload; - - /// - /// Decrypts the contents of the passed encrypted Widestring - /// - /// - /// String to decrypt. When block chaining mode ECBx - /// is used (not recommended!), the size of the data passed via this - /// parameter needs to be a multiple of the block size of the algorithm - /// used, otherwise a EDECCipherException exception will be raised! - /// - /// - /// Optional parameter. One can pass a class reference of one of the - /// concrete data formatting classes here which will be internally used - /// to convert the data. Decoded will be the still encrypted data, not the - /// encrypted data. Formattings can be used to convert data into a format - /// suitable for the transport medium the data shall be transported with. - /// - /// - /// Decrypted string as a byte array - /// - function DecodeStringToBytes(const Source: WideString; - Format: TDECFormatClass = nil): TBytes; overload; - - /// - /// Decrypts the contents of the passed WideString string - /// - /// - /// String to decrypt. When block chaining mode ECBx - /// is used (not recommended!), the size of the data passed via this - /// parameter needs to be a multiple of the block size of the algorithm - /// used, otherwise a EDECCipherException exception will be raised! - /// - /// - /// Optional parameter. One can pass a class reference of one of the - /// concrete data formatting classes here which will be internally used - /// to convert the data. Decoded will be the encrypted data, not the - /// decrypted data. Formattings can be used to convert data into a format - /// suitable for the transport medium the data shall be transported with. - /// - /// - /// Decrypted string - /// - /// - /// The use of this method is only recommended if a formatting is passed - /// which uses an 7-bit ASCII compatible string as input so that it - /// didn't get altered by string processing in some hafrmful way - /// - function DecodeStringToString(const Source: WideString; - Format: TDECFormatClass = nil): WideString; overload; -{$ENDIF} - end; - -implementation - -uses - DECBaseClass; - -function TDECFormattedCipher.EncodeBytes(const Source: TBytes): TBytes; -begin - SetLength(Result, Length(Source)); - if Length(Result) > 0 then - Encode(Source[0], Result[0], Length(Source)); -end; - -function TDECFormattedCipher.DecodeBytes(const Source: TBytes): TBytes; -begin - Result := Source; - if Length(Result) > 0 then - Decode(Result[0], Result[0], Length(Source)); -end; - -procedure TDECFormattedCipher.DoEncodeDecodeStream(const Source, Dest: TStream; - DataSize: Int64; - const CipherProc: TDECCipherCodeEvent; - const OnProgress: TDECProgressEvent); -var - Buffer: TBytes; - BufferSize, Bytes: Integer; - Max, StartPos, Pos: Int64; -begin - Pos := Source.Position; - if DataSize < 0 then - DataSize := Source.Size - Pos; - - Max := Pos + DataSize; - StartPos := Pos; - - if DataSize > 0 then - try - if Assigned(OnProgress) then - OnProgress(Max, 0, Started); - - if StreamBufferSize <= 0 then - StreamBufferSize := 8192; - BufferSize := StreamBufferSize mod Context.BlockSize; - if BufferSize = 0 then - BufferSize := StreamBufferSize - else - BufferSize := StreamBufferSize + Context.BlockSize - BufferSize; - if DataSize > BufferSize then - SetLength(Buffer, BufferSize) - else - SetLength(Buffer, DataSize); - while DataSize > 0 do - begin - Bytes := BufferSize; - if Bytes > DataSize then - Bytes := DataSize; - Source.ReadBuffer(Buffer[0], Bytes); - - // The real encryption or decryption routine - CipherProc(Buffer[0], Buffer[0], Bytes); - Dest.WriteBuffer(Buffer[0], Bytes); - Dec(DataSize, Bytes); - Inc(Pos, Bytes); - - if Assigned(OnProgress) then - OnProgress(Max, Pos - StartPos, Processing); - end; - finally - ProtectBytes(Buffer); - if Assigned(OnProgress) then - OnProgress(Max, Max, Finished); - end; -end; - -procedure TDECFormattedCipher.EncodeStream(const Source, Dest: TStream; DataSize: Int64; - const OnProgress: TDECProgressEvent); -begin - DoEncodeDecodeStream(Source, Dest, DataSize, - Encode, OnProgress); -end; - -procedure TDECFormattedCipher.DecodeStream(const Source, Dest: TStream; DataSize: Int64; - const OnProgress: TDECProgressEvent); -begin - DoEncodeDecodeStream(Source, Dest, DataSize, - Decode, OnProgress); -end; - -procedure TDECFormattedCipher.DoEncodeDecodeFile(const SourceFileName, DestFileName: string; - const Proc: TDECCipherCodeEvent; - const OnProgress: TDECProgressEvent); -var - S, D: TStream; -begin - Assert(SourceFileName <> DestFileName, 'Source and Dest file name may not be equal'); - - S := TFileStream.Create(SourceFileName, fmOpenRead or fmShareDenyNone); - try - D := TFileStream.Create(DestFileName, fmCreate); - try - DoEncodeDecodeStream(S, D, S.Size, Proc, OnProgress); - finally - D.Free; - end; - finally - S.Free; - end; -end; - -procedure TDECFormattedCipher.EncodeFile(const SourceFileName, DestFileName: string; - const OnProgress: TDECProgressEvent); -begin - DoEncodeDecodeFile(SourceFileName, DestFileName, Encode, OnProgress); -end; - -procedure TDECFormattedCipher.DecodeFile(const SourceFileName, DestFileName: string; - const OnProgress: TDECProgressEvent); -begin - DoEncodeDecodeFile(SourceFileName, DestFileName, Decode, OnProgress); -end; - -function TDECFormattedCipher.EncodeStringToBytes(const Source: string; - Format: TDECFormatClass = nil): TBytes; -var - Len: Integer; -begin - if Length(Source) > 0 then - begin - {$IF CompilerVersion >= 24.0} - Len := Length(Source) * SizeOf(Source[low(Source)]); - SetLength(Result, Len); - Encode(Source[low(Source)], Result[0], Len); - {$ELSE} - Len := Length(Source) * SizeOf(Source[1]); - SetLength(Result, Len); - Encode(Source[1], Result[0], Len); - {$IFEND} - - Result := ValidFormat(Format).Encode(Result); - end - else - SetLength(Result, 0); -end; - -function TDECFormattedCipher.EncodeStringToBytes(const Source: RawByteString; Format: TDECFormatClass): TBytes; -var - Len: Integer; -begin - if Length(Source) > 0 then - begin - {$IF CompilerVersion >= 24.0} - Len := Length(Source) * SizeOf(Source[low(Source)]); - SetLength(Result, Len); - Encode(Source[low(Source)], Result[0], Len); - {$ELSE} - Len := Length(Source) * SizeOf(Source[1]); - SetLength(Result, Len); - Encode(Source[1], Result[0], Len); - {$IFEND} - - Result := ValidFormat(Format).Encode(Result); - end - else - SetLength(Result, 0); -end; - -function TDECFormattedCipher.DecodeStringToBytes(const Source: string; Format: TDECFormatClass): TBytes; -var - Len: Integer; - Src: TBytes; -begin - if Length(Source) > 0 then - begin - Src := ValidFormat(Format).Decode(BytesOf(Source)); - - Len := Length(Src); - Result := Src; - Decode(Result[0], Result[0], Len); - end - else - SetLength(Result, 0); -end; - -function TDECFormattedCipher.DecodeStringToBytes(const Source: RawByteString; Format: TDECFormatClass): TBytes; -var - Len: Integer; - Src: TBytes; -begin - if Length(Source) > 0 then - begin - Src := ValidFormat(Format).Decode(BytesOf(Source)); - - Len := Length(Src); - Result := Src; - Decode(Result[0], Result[0], Len); - end - else - SetLength(Result, 0); -end; - -{$IFDEF ANSISTRINGSUPPORTED} -function TDECFormattedCipher.EncodeStringToBytes(const Source: AnsiString; Format: TDECFormatClass): TBytes; -var - Len: Integer; -begin - if Length(Source) > 0 then - begin - Len := Length(Source) * SizeOf(Source[1]); - SetLength(Result, Len); - Encode(Source[1], Result[0], Len); - - Result := ValidFormat(Format).Encode(Result); - end - else - SetLength(Result, 0); -end; -{$ENDIF} - -{$IFDEF ANSISTRINGSUPPORTED} -function TDECFormattedCipher.DecodeStringToBytes(const Source: AnsiString; Format: TDECFormatClass): TBytes; -var - Len: Integer; - Src: TBytes; -begin - if Length(Source) > 0 then - begin - Src := ValidFormat(Format).Decode(SysUtils.BytesOf(Source)); - - Len := Length(Src); - SetLength(Result, Len); - Decode(Src[0], Result[0], Len); - end - else - SetLength(Result, 0); -end; -{$ENDIF} - -{$IFNDEF NEXTGEN} -function TDECFormattedCipher.EncodeStringToBytes(const Source: WideString; Format: TDECFormatClass): TBytes; -var - Len: Integer; -begin - if Length(Source) > 0 then - begin - Len := Length(Source) * SizeOf(Source[1]); - SetLength(Result, Len); - Encode(Source[1], Result[0], Len); - - Result := ValidFormat(Format).Encode(Result); - end - else - SetLength(Result, 0); -end; - -function TDECFormattedCipher.EncodeStringToString(const Source: WideString; - Format: TDECFormatClass): WideString; -begin - result := WideString(EncodeStringToString(string(Source), Format)); -end; -{$ENDIF} - -{$IFDEF ANSISTRINGSUPPORTED} -function TDECFormattedCipher.EncodeStringToString(const Source: AnsiString; - Format: TDECFormatClass): AnsiString; -var - Len : Integer; - EncryptedBuffer : TBytes; - Temp : TBytes; -begin - if Length(Source) > 0 then - begin - Len := Length(Source) * SizeOf(Source[1]); - SetLength(EncryptedBuffer, Len); - Encode(Source[1], EncryptedBuffer[0], Len); - - Temp := ValidFormat(Format).Encode(EncryptedBuffer); - SetLength(Result, length(Temp)); - Move(Temp[0], Result[1], length(Temp)); - end - else - SetLength(Result, 0); -end; -{$ENDIF} - -function TDECFormattedCipher.EncodeStringToString(const Source: string; - Format: TDECFormatClass): string; -var - SourceSize : Integer; - EncryptedBuffer : TBytes; -begin - if Length(Source) > 0 then - begin - {$IF CompilerVersion >= 24.0} - SourceSize := Length(Source) * SizeOf(Source[low(Source)]); - SetLength(EncryptedBuffer, SourceSize); - Encode(Source[low(Source)], EncryptedBuffer[0], SourceSize); - {$ELSE} - SourceSize := Length(Source) * SizeOf(Source[1]); - SetLength(EncryptedBuffer, SourceSize); - Encode(Source[1], EncryptedBuffer[0], SourceSize); - {$IFEND} - - Result := StringOf(ValidFormat(Format).Encode(EncryptedBuffer)); - end - else - Result := ''; -end; - -function TDECFormattedCipher.EncodeStringToString(const Source: RawByteString; - Format: TDECFormatClass): RawByteString; -var - SourceSize : Integer; - EncryptedBuffer : TBytes; - Temp : TBytes; -begin - if Length(Source) > 0 then - begin - {$IF CompilerVersion >= 24.0} - SourceSize := Length(Source) * SizeOf(Source[low(Source)]); - SetLength(EncryptedBuffer, SourceSize); - Encode(Source[low(Source)], EncryptedBuffer[0], SourceSize); - {$ELSE} - SourceSize := Length(Source) * SizeOf(Source[1]); - SetLength(EncryptedBuffer, SourceSize); - Encode(Source[1], EncryptedBuffer[0], SourceSize); - {$IFEND} - - Temp := ValidFormat(Format).Encode(EncryptedBuffer); - SetLength(Result, length(Temp)); - {$IF CompilerVersion >= 24.0} - Move(Temp[0], Result[low(Result)], length(Temp)) - {$ELSE} - Move(Temp[0], Result[1], length(Temp)) - {$IFEND} - end - else - Result := ''; -end; - -{$IFNDEF NEXTGEN} -function TDECFormattedCipher.DecodeStringToBytes(const Source: WideString; Format: TDECFormatClass): TBytes; -var - Len: Integer; - Src: TBytes; -begin - if Length(Source) > 0 then - begin - Src := ValidFormat(Format).Decode(BytesOf(Source)); - - Len := Length(Src); - SetLength(Result, Len); - Decode(Src[0], Result[0], Len); - end - else - SetLength(Result, 0); -end; -{$ENDIF} - -{$IFDEF ANSISTRINGSUPPORTED} -function TDECFormattedCipher.DecodeStringToString(const Source: AnsiString; - Format: TDECFormatClass): AnsiString; -var - Len : Integer; - Src : TBytes; - Tmp : TBytes; -begin - if Length(Source) > 0 then - begin - Src := ValidFormat(Format).Decode(SysUtils.BytesOf(Source)); - - Len := Length(Src); - SetLength(Tmp, Len); - Decode(Src[0], Tmp[0], Len); - - SetLength(Result, length(Tmp)); - - {$IF CompilerVersion >= 24.0} - Move(Tmp[0], Result[low(Result)], length(Tmp)) - {$ELSE} - Move(Tmp[0], Result[1], length(Tmp)) - {$IFEND} - end - else - SetLength(Result, 0); -end; -{$ENDIF} - -{$IFNDEF NEXTGEN} -function TDECFormattedCipher.DecodeStringToString(const Source: WideString; - Format: TDECFormatClass): WideString; -begin - Result := WideString(DecodeStringToString(string(Source), Format)); -end; -{$ENDIF} - -function TDECFormattedCipher.DecodeStringToString(const Source: RawByteString; - Format: TDECFormatClass): RawByteString; -var - Len : Integer; - Src : TBytes; - Tmp : TBytes; -begin - if Length(Source) > 0 then - begin - Src := ValidFormat(Format).Decode(BytesOf(Source)); - - Len := Length(Src); - SetLength(Tmp, Len); - Decode(Src[0], Tmp[0], Len); - - SetLength(Result, length(Tmp)); - - {$IF CompilerVersion >= 24.0} - Move(Tmp[0], Result[low(Result)], length(Tmp)) - {$ELSE} - Move(Tmp[0], Result[1], length(Tmp)) - {$IFEND} - end - else - SetLength(Result, 0); -end; - -function TDECFormattedCipher.DecodeStringToString(const Source: string; - Format: TDECFormatClass): string; -var - Len : Integer; - Src : TBytes; - Tmp : TBytes; -begin - if Length(Source) > 0 then - begin - Src := ValidFormat(Format).Decode(BytesOf(Source)); - - Len := Length(Src); - SetLength(Tmp, Len); - Decode(Src[0], Tmp[0], Len); - Result := WideStringOf(Tmp); - end - else - SetLength(Result, 0); -end; - -end. diff --git a/Source/DECCipherInterface.pas b/Source/DECCipherInterface.pas deleted file mode 100644 index 1b55126e..00000000 --- a/Source/DECCipherInterface.pas +++ /dev/null @@ -1,605 +0,0 @@ -{***************************************************************************** - The DEC team (see file NOTICE.txt) licenses this file - to you under the Apache License, Version 2.0 (the - "License"); you may not use this file except in compliance - with the License. A copy of this licence is found in the root directory - of this project in the file LICENCE.txt or alternatively at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, - software distributed under the License is distributed on an - "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - KIND, either express or implied. See the License for the - specific language governing permissions and limitations - under the License. -*****************************************************************************} -unit DECCipherInterface; - -interface - -uses - {$IFDEF FPC} - SysUtils, Classes, - {$ELSE} - System.SysUtils, System.Classes, - {$ENDIF} - DECUtil, DECCipherBase, DECFormatBase; - -type - /// - /// Common interface for all ciphers. Some ciphers may have additional - /// methods/properties though! - /// - IDECCipher = Interface - /// - /// Encrypts the contents of a given byte array - /// - /// - /// Byte array with data to be encrypted - /// - /// - /// Byte array with encrypted data - /// - function EncodeBytes(const Source: TBytes): TBytes; - - /// - /// Decrypts the contents of a given byte array - /// - /// - /// Byte array with data to be decrypted - /// - /// - /// Byte array with decrypted data - /// - function DecodeBytes(const Source: TBytes): TBytes; - - /// - /// Encrypts the data contained in a given stream - /// - /// - /// Source stream containing the data to encrypt - /// - /// - /// Destination stream, where the encrypted data shall be put in - /// - /// - /// Number of bytes of Source to be encrypted - /// - /// - /// optional callback for reporting progress of the operation - /// - procedure EncodeStream(const Source, Dest: TStream; DataSize: Int64; - const OnProgress: TDECProgressEvent = nil); - - /// - /// Decrypts the data contained in a given stream - /// - /// - /// Source stream containing the data to decrypt - /// - /// - /// Destination stream, where the decrypted data shall be put in - /// - /// - /// Number of bytes of Source to be decrypted - /// - /// - /// optional callback for reporting progress of the operation - /// - procedure DecodeStream(const Source, Dest: TStream; DataSize: Int64; - const OnProgress: TDECProgressEvent = nil); - - /// - /// Reads the contents of one file, encrypts it and stores it in another file - /// - /// - /// Path and name of the file to encrypt - /// - /// - /// Path and name of the file the encrypted data shall be stored in - /// - /// - /// Optional event which can be passed to get information about the - /// progress of the encryption operation - /// - procedure EncodeFile(const SourceFileName, DestFileName: string; - const OnProgress: TDECProgressEvent = nil); - - /// - /// Reads the contents of one file, decrypts it and stores it in another file - /// - /// - /// Path and name of the file to decrypt - /// - /// - /// Path and name of the file the decrypted data shall be stored in - /// - /// - /// Optional event which can be passed to get information about the - /// progress of the decryption operation - /// - procedure DecodeFile(const SourceFileName, DestFileName: string; - const OnProgress: TDECProgressEvent = nil); - - /// - /// Encrypts the contents of the passed unicode string - /// - /// - /// String to encrypt - /// - /// - /// Optional parameter. One can pass a class reference of one of the - /// concrete data formatting classes here which will be internally used - /// to convert the data. Encoded will be the encrypted data, not the - /// source data. Formattings can be used to convert data into a format - /// suitable for the transport medium the data shall be transported with. - /// - /// - /// Encrypted string as a byte array - /// - function EncodeStringToBytes(const Source: string; Format: TDECFormatClass = nil): TBytes; overload; - - /// - /// Encrypts the contents of the passed RawByteString - /// - /// - /// String to encrypt - /// - /// - /// Optional parameter. One can pass a class reference of one of the - /// concrete data formatting classes here which will be internally used - /// to convert the data. Encoded will be the encrypted data, not the - /// source data. Formattings can be used to convert data into a format - /// suitable for the transport medium the data shall be transported with. - /// - /// - /// Encrypted string as a byte array - /// - function EncodeStringToBytes(const Source: RawByteString; Format: TDECFormatClass = nil): TBytes; overload; - - /// - /// Encrypts the contents of the passed unicode string - /// - /// - /// String to encrypt - /// - /// - /// Optional parameter. One can pass a class reference of one of the - /// concrete data formatting classes here which will be internally used - /// to convert the data. Encoded will be the encrypted data, not the - /// source data. Formattings can be used to convert data into a format - /// suitable for the transport medium the data shall be transported with. - /// - /// - /// Encrypted string - /// - /// - /// The use of this method is only recommended if a formatting is passed - /// which will result in an 7-bit ASCII compatible string as we cannot - /// ensure that Unicode string processing will not alter/interpret some - /// byte combinations in a destructive way, making the encrypted string - /// un-decryptable. - /// - function EncodeStringToString(const Source: string; Format: TDECFormatClass = nil): string; overload; - - /// - /// Encrypts the contents of the passed unicode string - /// - /// - /// String to encrypt - /// - /// - /// Optional parameter. One can pass a class reference of one of the - /// concrete data formatting classes here which will be internally used - /// to convert the data. Encoded will be the encrypted data, not the - /// source data. Formattings can be used to convert data into a format - /// suitable for the transport medium the data shall be transported with. - /// - /// - /// Encrypted string - /// - /// - /// The use of this method is only recommended if a formatting is passed - /// which will result in an 7-bit ASCII compatible string as we cannot - /// ensure that string processing will not alter/interpret some - /// byte combinations in a destructive way, making the encrypted string - /// un-decryptable. - /// - function EncodeStringToString(const Source: RawByteString; Format: TDECFormatClass = nil): RawByteString; overload; - - /// - /// Decrypts the contents of the passed encrypted unicode string - /// - /// - /// String to decrypt - /// - /// - /// Optional parameter. One can pass a class reference of one of the - /// concrete data formatting classes here which will be internally used - /// to convert the data. Decoded will be the still encrypted data, not the - /// encrypted data. Formattings can be used to convert data into a format - /// suitable for the transport medium the data shall be transported with. - /// - /// - /// Decrypted string as a byte array - /// - function DecodeStringToBytes(const Source: string; Format: TDECFormatClass = nil): TBytes; overload; - - /// - /// Decrypts the contents of the passed encrypted RawByteString - /// - /// - /// String to decrypt - /// - /// - /// Optional parameter. One can pass a class reference of one of the - /// concrete data formatting classes here which will be internally used - /// to convert the data. Decoded will be the still encrypted data, not the - /// encrypted data. Formattings can be used to convert data into a format - /// suitable for the transport medium the data shall be transported with. - /// - /// - /// Decrypted string as a byte array - /// - function DecodeStringToBytes(const Source: RawByteString; Format: TDECFormatClass = nil): TBytes; overload; - - /// - /// Decrypts the contents of the passed Unicode string - /// - /// - /// String to decrypt - /// - /// - /// Optional parameter. One can pass a class reference of one of the - /// concrete data formatting classes here which will be internally used - /// to convert the data. Decoded will be the encrypted data, not the - /// decrypted data. Formattings can be used to convert data into a format - /// suitable for the transport medium the data shall be transported with. - /// - /// - /// Decrypted string - /// - /// - /// The use of this method is only recommended if a formatting is passed - /// which uses an 7-bit ASCII compatible string as input so that it - /// didn't get altered by Unicode string processing in some hafrmful way - /// - function DecodeStringToString(const Source: string; Format: TDECFormatClass = nil): string; overload; - - /// - /// Decrypts the contents of the passed RawByteString string - /// - /// - /// String to decrypt - /// - /// - /// Optional parameter. One can pass a class reference of one of the - /// concrete data formatting classes here which will be internally used - /// to convert the data. Decoded will be the encrypted data, not the - /// decrypted data. Formattings can be used to convert data into a format - /// suitable for the transport medium the data shall be transported with. - /// - /// - /// Decrypted string - /// - /// - /// The use of this method is only recommended if a formatting is passed - /// which uses an 7-bit ASCII compatible string as input so that it - /// didn't get altered by string processing in some hafrmful way - /// - function DecodeStringToString(const Source: RawByteString; Format: TDECFormatClass = nil): RawByteString; overload; - -{$IFDEF ANSISTRINGSUPPORTED} - /// - /// Encrypts the contents of the passed Ansistring - /// - /// - /// String to encrypt - /// - /// - /// Optional parameter. One can pass a class reference of one of the - /// concrete data formatting classes here which will be internally used - /// to convert the data. Encoded will be the encrypted data, not the - /// source data. Formattings can be used to convert data into a format - /// suitable for the transport medium the data shall be transported with. - /// - /// - /// Encrypted string as a byte array - /// - function EncodeStringToBytes(const Source: AnsiString; Format: TDECFormatClass = nil): TBytes; overload; - - /// - /// Encrypts the contents of the passed Ansistring - /// - /// - /// String to encrypt - /// - /// - /// Optional parameter. One can pass a class reference of one of the - /// concrete data formatting classes here which will be internally used - /// to convert the data. Encoded will be the encrypted data, not the - /// source data. Formattings can be used to convert data into a format - /// suitable for the transport medium the data shall be transported with. - /// - /// - /// Encrypted string as an AnsiString - /// - /// - /// The use of this method is only recommended if a formatting is passed - /// which will result in an 7-bit ASCII compatible string as we cannot - /// ensure that string processing will not alter/interpret some - /// byte combinations in a destructive way, making the encrypted string - /// un-decryptable. - /// - function EncodeStringToString(const Source: AnsiString; Format: TDECFormatClass = nil): AnsiString; overload; - - /// - /// Decrypts the contents of the passed encrypted Ansistring - /// - /// - /// String to decrypt - /// - /// - /// Optional parameter. One can pass a class reference of one of the - /// concrete data formatting classes here which will be internally used - /// to convert the data. Decoded will be the still encrypted data, not the - /// encrypted data. Formattings can be used to convert data into a format - /// suitable for the transport medium the data shall be transported with. - /// - /// - /// Decrypted string as a byte array - /// - function DecodeStringToBytes(const Source: AnsiString; Format: TDECFormatClass = nil): TBytes; overload; - - /// - /// Decrypts the contents of the passed AnsiString string - /// - /// - /// String to decrypt - /// - /// - /// Optional parameter. One can pass a class reference of one of the - /// concrete data formatting classes here which will be internally used - /// to convert the data. Decoded will be the encrypted data, not the - /// decrypted data. Formattings can be used to convert data into a format - /// suitable for the transport medium the data shall be transported with. - /// - /// - /// Decrypted string - /// - /// - /// The use of this method is only recommended if a formatting is passed - /// which uses an 7-bit ASCII compatible string as input so that it - /// didn't get altered by string processing in some hafrmful way - /// - function DecodeStringToString(const Source: AnsiString; Format: TDECFormatClass = nil): AnsiString; overload; -{$ENDIF} - -{$IFNDEF NEXTGEN} - /// - /// Encrypts the contents of the passed Widestring - /// - /// - /// String to encrypt - /// - /// - /// Optional parameter. One can pass a class reference of one of the - /// concrete data formatting classes here which will be internally used - /// to convert the data. Encoded will be the encrypted data, not the - /// source data. Formattings can be used to convert data into a format - /// suitable for the transport medium the data shall be transported with. - /// - /// - /// Encrypted string as a byte array - /// - function EncodeStringToBytes(const Source: WideString; Format: TDECFormatClass = nil): TBytes; overload; - - /// - /// Encrypts the contents of the passed Widestring - /// - /// - /// String to encrypt - /// - /// - /// Optional parameter. One can pass a class reference of one of the - /// concrete data formatting classes here which will be internally used - /// to convert the data. Encoded will be the encrypted data, not the - /// source data. Formattings can be used to convert data into a format - /// suitable for the transport medium the data shall be transported with. - /// - /// - /// Encrypted string as an WideString - /// - /// - /// The use of this method is only recommended if a formatting is passed - /// which will result in an 7-bit ASCII compatible string as we cannot - /// ensure that string processing will not alter/interpret some - /// byte combinations in a destructive way, making the encrypted string - /// un-decryptable. - /// - function EncodeStringToString(const Source: WideString; Format: TDECFormatClass = nil): WideString; overload; - - /// - /// Decrypts the contents of the passed encrypted Widestring - /// - /// - /// String to decrypt - /// - /// - /// Optional parameter. One can pass a class reference of one of the - /// concrete data formatting classes here which will be internally used - /// to convert the data. Decoded will be the still encrypted data, not the - /// encrypted data. Formattings can be used to convert data into a format - /// suitable for the transport medium the data shall be transported with. - /// - /// - /// Decrypted string as a byte array - /// - function DecodeStringToBytes(const Source: WideString; Format: TDECFormatClass = nil): TBytes; overload; - - /// - /// Decrypts the contents of the passed WideString string - /// - /// - /// String to decrypt - /// - /// - /// Optional parameter. One can pass a class reference of one of the - /// concrete data formatting classes here which will be internally used - /// to convert the data. Decoded will be the encrypted data, not the - /// decrypted data. Formattings can be used to convert data into a format - /// suitable for the transport medium the data shall be transported with. - /// - /// - /// Decrypted string - /// - /// - /// The use of this method is only recommended if a formatting is passed - /// which uses an 7-bit ASCII compatible string as input so that it - /// didn't get altered by string processing in some hafrmful way - /// - function DecodeStringToString(const Source: WideString; Format: TDECFormatClass = nil): WideString; overload; -{$ENDIF} - - /// - /// Initializes the cipher with the necessary encryption/decryption key - /// - /// - /// Encryption/decryption key. Recommended/required key length is dependant - /// on the concrete algorithm. - /// - /// - /// Size of the key in bytes - /// - /// - /// Initialization vector. This contains the values the first block of - /// data to be processed is linked with. This is being done the same way - /// as the 2nd block of the data to be processed will be linked with the - /// first block and so on and this is dependant on the cypher mode set via - /// Mode property - /// - /// - /// Size of the initialization vector in bytes - /// - /// - /// optional parameter defining the value with which the last block will - /// be filled up if the size of the data to be processed cannot be divided - /// by block size without reminder. Means: if the last block is not - /// completely filled with data. - /// - procedure Init(const Key; Size: Integer; const IVector; IVectorSize: Integer; IFiller: Byte = $FF); overload; - /// - /// Initializes the cipher with the necessary encryption/decryption key - /// - /// - /// Encryption/decryption key. Recommended/required key length is dependant - /// on the concrete algorithm. - /// - /// - /// Initialization vector. This contains the values the first block of - /// data to be processed is linked with. This is being done the same way - /// as the 2nd block of the data to be processed will be linked with the - /// first block and so on and this is dependant on the cypher mode set via - /// Mode property - /// - /// - /// optional parameter defining the value with which the last block will - /// be filled up if the size of the data to be processed cannot be divided - /// by block size without reminder. Means: if the last block is not - /// completely filled with data. - /// - procedure Init(const Key: TBytes; const IVector: TBytes; IFiller: Byte = $FF); overload; - /// - /// Initializes the cipher with the necessary encryption/decryption key - /// - /// - /// Encryption/decryption key. Recommended/required key length is dependant - /// on the concrete algorithm. - /// - /// - /// Initialization vector. This contains the values the first block of - /// data to be processed is linked with. This is being done the same way - /// as the 2nd block of the data to be processed will be linked with the - /// first block and so on and this is dependant on the cypher mode set via - /// Mode property - /// - /// - /// optional parameter defining the value with which the last block will - /// be filled up if the size of the data to be processed cannot be divided - /// by block size without reminder. Means: if the last block is not - /// completely filled with data. - /// - procedure Init(const Key: RawByteString; const IVector: RawByteString = ''; IFiller: Byte = $FF); overload; - {$IFDEF ANSISTRINGSUPPORTED} - /// - /// Initializes the cipher with the necessary encryption/decryption key. - /// Only for use with the classic desktop compilers. - /// - /// - /// Encryption/decryption key. Recommended/required key length is dependant - /// on the concrete algorithm. - /// - /// - /// Initialization vector. This contains the values the first block of - /// data to be processed is linked with. This is being done the same way - /// as the 2nd block of the data to be processed will be linked with the - /// first block and so on and this is dependant on the cypher mode set via - /// Mode property - /// - /// - /// optional parameter defining the value with which the last block will - /// be filled up if the size of the data to be processed cannot be divided - /// by block size without reminder. Means: if the last block is not - /// completely filled with data. - /// - procedure Init(const Key: AnsiString; const IVector: AnsiString = ''; IFiller: Byte = $FF); overload; - {$ENDIF} - {$IFNDEF NEXTGEN} - /// - /// Initializes the cipher with the necessary encryption/decryption key. - /// Only for use with the classic desktop compilers. - /// - /// - /// Encryption/decryption key. Recommended/required key length is dependant - /// on the concrete algorithm. - /// - /// - /// Initialization vector. This contains the values the first block of - /// data to be processed is linked with. This is being done the same way - /// as the 2nd block of the data to be processed will be linked with the - /// first block and so on and this is dependant on the cypher mode set via - /// Mode property - /// - /// - /// optional parameter defining the value with which the last block will - /// be filled up if the size of the data to be processed cannot be divided - /// by block size without reminder. Means: if the last block is not - /// completely filled with data. - /// - procedure Init(const Key: WideString; const IVector: WideString = ''; IFiller: Byte = $FF); overload; - {$ENDIF} - - /// - /// Returns the currently set cipher block mode, means how blocks are - /// linked to each other in order to avoid certain attacks. - /// - function GetMode: TCipherMode; - - /// - /// Sets the cipher mode, means how each block is being linked with his - /// predecessor to avoid certain attacks - /// - procedure SetMode(Value: TCipherMode); - - /// - /// Mode used for padding data to be encrypted/decrypted. See TCipherMode. - /// - property Mode: TCipherMode - read GetMode - write SetMode; - end; - -implementation - -end. diff --git a/Source/DECCipherModes.pas b/Source/DECCipherModes.pas deleted file mode 100644 index fd171c0a..00000000 --- a/Source/DECCipherModes.pas +++ /dev/null @@ -1,919 +0,0 @@ -{***************************************************************************** - The DEC team (see file NOTICE.txt) licenses this file - to you under the Apache License, Version 2.0 (the - "License"); you may not use this file except in compliance - with the License. A copy of this licence is found in the root directory - of this project in the file LICENCE.txt or alternatively at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, - software distributed under the License is distributed on an - "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - KIND, either express or implied. See the License for the - specific language governing permissions and limitations - under the License. -*****************************************************************************} -unit DECCipherModes; - -interface - -{$INCLUDE DECOptions.inc} - -uses - {$IFDEF FPC} - SysUtils, - {$ELSE} - System.SysUtils, - {$ENDIF} - DECCipherBase; - -{$I DECOptions.inc} - -type - /// - /// Most ciphers are block oriented and thus work on blocks of a fixed size. - /// In order to not encrypt each block separately without any link to his - /// predecessor and sucessor, which would make attacks on the encrypted data - /// easier, each block should be linked with his predecessor (or the - /// initialization vector). This class implements the various supported - /// algorithms for linking blocks. - /// - TDECCipherModes = class(TDECCipher) - strict protected - /// - /// Raises an EDECCipherException exception and provides the correct values - /// for message lenght and block size - /// - procedure ReportInvalidMessageLength(Cipher: TDECCipher); - /// - /// Electronic Code Book - /// Mode cmECBx needs message padding to be a multiple of Cipher.BlockSize - /// and should be used only in 1-byte Streamciphers. - /// This one works on Blocks of Cipher.BufferSize bytes, when using a - /// Blockcipher that's equal to Cipher.BlockSize. - /// - /// - /// This mode should not be used in practice, as it makes the encrypted - /// message vulnerable to certain attacks without knowing the encryption key - /// - procedure EncodeECBx(Source, Dest: PByteArray; Size: Integer); virtual; - /// - /// 8bit Output Feedback mode, needs no padding - /// - procedure EncodeOFB8(Source, Dest: PByteArray; Size: Integer); virtual; - /// - /// 8 bit Cipher Feedback mode, needs no padding and works on 8 bit - /// Feedback Shift Registers. - /// - procedure EncodeCFB8(Source, Dest: PByteArray; Size: Integer); virtual; - /// - /// 8Bit CFS, double Cipher Feedback mode (CFB), needs no padding and - /// works on 8 bit Feedback Shift Registers. - /// This one is a proprietary mode developed by Hagen Reddmann. This mode - /// works as cmCBCx, cmCFBx, cmCFB8 but with double XOR'ing of the - /// inputstream into Feedback register. - /// - procedure EncodeCFS8(Source, Dest: PByteArray; Size: Integer); virtual; - /// - /// Cipher Feedback mode (CFB) on Blocksize of Cipher, needs no padding - /// This one works on Blocks of Cipher.BufferSize bytes, when using a - /// Blockcipher that's equal to Cipher.BlockSize. - /// - procedure EncodeCFBx(Source, Dest: PByteArray; Size: Integer); virtual; - /// - /// Output Feedback mode on Blocksize of Cipher, needs no padding and - /// works on 8 bit Feedback Shift Registers. - /// This one works on Blocks of Cipher.BufferSize bytes, when using a - /// Blockcipher that's equal to Cipher.BlockSize. - /// - procedure EncodeOFBx(Source, Dest: PByteArray; Size: Integer); virtual; - /// - /// double Cipher Feedback mode (CFB) on Blocksize of Cipher, needs no padding. - /// This one works on Blocks of Cipher.BufferSize bytes, when using a - /// Blockcipher that's equal to Cipher.BlockSize. - /// This one is a proprietary mode developed by Hagen Reddmann. This mode - /// works as cmCBCx, cmCFBx, cmCFB8 but with double XOR'ing of the - /// inputstream into Feedback register. - /// - procedure EncodeCFSx(Source, Dest: PByteArray; Size: Integer); virtual; - /// - /// Cipher Block Chaining, with CFB8 padding of truncated final block - /// It needs no external padding, because internally the last - /// truncated block is padded by cmCFS8 or cmCFB8. After padding these Modes - /// cannot be used to process any more data. If needed to process chunks of - /// data then each chunk must be aligned to Cipher.BufferSize bytes. - /// This one works on Blocks of Cipher.BufferSize bytes, when using a - /// Blockcipher that's equal to Cipher.BlockSize. - /// - procedure EncodeCBCx(Source, Dest: PByteArray; Size: Integer); virtual; - /// - /// double CBC, with CFS8 padding of truncated final block - /// It needs no external padding, because internally the last - /// truncated block is padded by cmCFS8 or cmCFB8. After padding these Modes - /// cannot be used to process any more data. If needed to process chunks of - /// data then each chunk must be aligned to Cipher.BufferSize bytes. - /// This one works on Blocks of Cipher.BufferSize bytes, when using a - /// Blockcipher that's equal to Cipher.BlockSize. - /// This one is a proprietary mode developed by Hagen Reddmann. This mode - /// works as cmCBCx, cmCFBx, cmCFB8 but with double XOR'ing of the - /// inputstream into Feedback register. - /// - procedure EncodeCTSx(Source, Dest: PByteArray; Size: Integer); virtual; - {$IFDEF DEC3_CMCTS} - /// - /// double CBC, with - /// for DEC 3.0 compatibility only - /// This is a proprietary mode developed by Frederik Winkelsdorf. It - /// replaces the CFS8 padding of the truncated final block with a CFSx padding. - /// Useful when converting projects that previously used the old DEC v3.0. It - /// has the same restrictions for external padding and chunk processing as - /// cmCTSx has. It has a less secure padding of the truncated final block. - /// (to enable it see DECOptions.inc) - /// - procedure EncodeCTS3(Source, Dest: PByteArray; Size: Integer); virtual; - {$ENDIF} - /// - /// Electronic Code Book - /// Mode cmECBx needs message padding to be a multiple of Cipher.BlockSize - /// and should be used only in 1-byte Streamciphers. - /// This one works on blocks of Cipher.BufferSize bytes, when using a - /// blockcipher that's equal to Cipher.BlockSize. - /// - procedure DecodeECBx(Source, Dest: PByteArray; Size: Integer); virtual; - /// - /// 8 bit Output Feedback mode, needs no padding - /// - procedure DecodeOFB8(Source, Dest: PByteArray; Size: Integer); virtual; - /// - /// 8 bit Cipher Feedback mode, needs no padding and works on 8 bit - /// Feedback Shift Registers. - /// - procedure DecodeCFB8(Source, Dest: PByteArray; Size: Integer); virtual; - /// - /// 8 Bit CFS, double Cipher Feedback mode (CFB), needs no padding and - /// works on 8 bit Feedback Shift Registers. - /// This one is a proprietary mode developed by Hagen Reddmann. This mode - /// works as cmCBCx, cmCFBx, cmCFB8 but with double XOR'ing of the - /// inputstream into Feedback register. - /// - procedure DecodeCFS8(Source, Dest: PByteArray; Size: Integer); virtual; - /// - /// Cipher Feedback mode (CFB) on Blocksize of Cipher, needs no padding - /// This one works on blocks of Cipher.BufferSize bytes, when using a - /// blockcipher that's equal to Cipher.BlockSize. - /// - procedure DecodeCFBx(Source, Dest: PByteArray; Size: Integer); virtual; - /// - /// Output Feedback mode on Blocksize of Cipher, needs no padding and - /// works on 8 bit Feedback Shift Registers. - /// This one works on blocks of Cipher.BufferSize bytes, when using a - /// blockcipher that's equal to Cipher.BlockSize. - /// - procedure DecodeOFBx(Source, Dest: PByteArray; Size: Integer); virtual; - /// - /// double Cipher Feedback mode (CFB) on Blocksize of Cipher, needs no padding. - /// This one works on blocks of Cipher.BufferSize bytes, when using a - /// blockcipher that's equal to Cipher.BlockSize. - /// This one is a proprietary mode developed by Hagen Reddmann. This mode - /// works as cmCBCx, cmCFBx, cmCFB8 but with double XOR'ing of the - /// inputstream into Feedback register. - /// - procedure DecodeCFSx(Source, Dest: PByteArray; Size: Integer); virtual; - /// - /// Cipher Block Chaining, with CFB8 padding of truncated final block. - /// It needs no external padding, because internally the last - /// truncated block is padded by cmCFS8 or cmCFB8. After padding these modes - /// cannot be used to process any more data. If needed to process chunks of - /// data then each chunk must be algined to Cipher.BufferSize bytes. - /// This one works on blocks of Cipher.BufferSize bytes, when using a - /// blockcipher that's equal to Cipher.BlockSize. - /// - procedure DecodeCBCx(Source, Dest: PByteArray; Size: Integer); virtual; - /// - /// double CBC, with CFS8 padding of truncated final block - /// It needs no external padding, because internally the last - /// truncated block is padded by cmCFS8 or cmCFB8. After padding these Modes - /// cannot be used to process any more data. If needed to process chunks of - /// data then each chunk must be algined to Cipher.BufferSize bytes. - /// This one works on blocks of Cipher.BufferSize bytes, when using a - /// blockcipher that's equal to Cipher.BlockSize. - /// This one is a proprietary mode developed by Hagen Reddmann. This mode - /// works as cmCBCx, cmCFBx, cmCFB8 but with double XOR'ing of the - /// inputstream into feedback register. - /// - procedure DecodeCTSx(Source, Dest: PByteArray; Size: Integer); virtual; - {$IFDEF DEC3_CMCTS} - /// - /// double CBC - /// This is a proprietary mode developed by Frederik Winkelsdorf. It - /// replaces the CFS8 padding of the truncated final block with a CFSx padding. - /// Useful when converting projects that previously used the old DEC v3.0. It - /// has the same restrictions for external padding and chunk processing as - /// cmCTSx has. It has a less secure padding of the truncated final block. - /// (to enable it see DECOptions.inc) - /// - /// - /// For DEC 3.0 compatibility only - /// - procedure DecodeCTS3(Source, Dest: PByteArray; Size: Integer); virtual; - {$ENDIF} - public - /// - /// Encrypts a given block of data - /// - /// - /// Data to be encrypted - /// - /// - /// Data after encryption - /// - /// - /// Size of the data the Source parameter points to in byte - /// - procedure Encode(const Source; var Dest; DataSize: Integer); - /// - /// Decrypts a given block of data - /// - /// - /// Data to be Decrypted - /// - /// - /// Data after decryption - /// - /// - /// Size of the data the Source parameter points to in byte - /// - procedure Decode(const Source; var Dest; DataSize: Integer); - end; - -implementation - -uses - {$IFDEF FPC} - TypInfo, - {$ELSE} - System.TypInfo, - {$ENDIF} - DECUtil; - -resourcestring - sInvalidMessageLength = 'Message length for mode %0:s must be a multiple of %1:d bytes'; - -procedure TDECCipherModes.ReportInvalidMessageLength(Cipher: TDECCipher); -begin - raise EDECCipherException.CreateResFmt(@sInvalidMessageLength, - [System.TypInfo.GetEnumName(TypeInfo(TCipherMode), - Integer(Cipher.Mode)), - Cipher.Context.BlockSize]); -end; - -procedure TDECCipherModes.Encode(const Source; var Dest; DataSize: Integer); -begin - CheckState([csInitialized, csEncode, csDone]); - - case FMode of - cmECBx: EncodeECBx(@Source, @Dest, DataSize); - cmCBCx: EncodeCBCx(@Source, @Dest, DataSize); - cmCTSx: EncodeCTSx(@Source, @Dest, DataSize); - {$IFDEF DEC3_CMCTS} - cmCTS3: EncodeCTS3(@Source, @Dest, DataSize); - {$ENDIF DEC3_CMCTS} - cmCFB8: EncodeCFB8(@Source, @Dest, DataSize); - cmCFBx: EncodeCFBx(@Source, @Dest, DataSize); - cmOFB8: EncodeOFB8(@Source, @Dest, DataSize); - cmOFBx: EncodeOFBx(@Source, @Dest, DataSize); - cmCFS8: EncodeCFS8(@Source, @Dest, DataSize); - cmCFSx: EncodeCFSx(@Source, @Dest, DataSize); - end; -end; - -procedure TDECCipherModes.EncodeECBx(Source, Dest: PByteArray; Size: Integer); -var - I: Integer; -begin - if Context.BlockSize = 1 then - begin - DoEncode(Source, Dest, Size); - FState := csEncode; - end - else - begin - Dec(Size, FBufferSize); - I := 0; - while I <= Size do - begin - DoEncode(@Source[I], @Dest[I], FBufferSize); - Inc(I, FBufferSize); - end; - Dec(Size, I - FBufferSize); - if Size > 0 then - begin - if Size mod Context.BlockSize = 0 then - begin - DoEncode(@Source[I], @Dest[I], Size); - FState := csEncode; - end - else - begin - FState := csPadded; - ReportInvalidMessageLength(Self); - end; - end; - end; -end; - -procedure TDECCipherModes.EncodeOFB8(Source, Dest: PByteArray; Size: Integer); -var - I: Integer; -begin - I := 0; - while I < Size do - begin - DoEncode(FFeedback, FBuffer, FBufferSize); - Move(FFeedback[1], FFeedback[0], FBufferSize - 1); - FFeedback[FBufferSize - 1] := FBuffer[0]; - Dest[I] := Source[I] xor FBuffer[0]; - Inc(I); - end; - FState := csEncode; -end; - -procedure TDECCipherModes.EncodeCFB8(Source, Dest: PByteArray; Size: Integer); -// CFB-8 -var - I: Integer; -begin - I := 0; - while I < Size do - begin - DoEncode(FFeedback, FBuffer, FBufferSize); - Move(FFeedback[1], FFeedback[0], FBufferSize - 1); - Dest[I] := Source[I] xor FBuffer[0]; - FFeedback[FBufferSize - 1] := Dest[I]; - Inc(I); - end; - FState := csEncode; -end; - -procedure TDECCipherModes.EncodeCFS8(Source, Dest: PByteArray; Size: Integer); -// CFS-8, CTS as CFB -var - I: Integer; -begin - I := 0; - while I < Size do - begin - DoEncode(FFeedback, FBuffer, FBufferSize); - Dest[I] := Source[I] xor FBuffer[0]; - Move(FFeedback[1], FFeedback[0], FBufferSize - 1); - FFeedback[FBufferSize - 1] := FFeedback[FBufferSize - 1] xor Dest[I]; - Inc(I); - end; - FState := csEncode; -end; - -procedure TDECCipherModes.EncodeCFBx(Source, Dest: PByteArray; Size: Integer); -// CFB-BlockSize -var - I: Integer; - F: PByteArray; -begin - FState := csEncode; - if FBufferIndex > 0 then - begin - I := FBufferSize - FBufferIndex; - if I > Size then - I := Size; - XORBuffers(Source[0], FBuffer[FBufferIndex], I, Dest[0]); - Move(Dest[0], FFeedback[FBufferIndex], I); - Inc(FBufferIndex, I); - if FBufferIndex < FBufferSize then - Exit; - Dec(Size, I); - Source := @Source[I]; - Dest := @Dest[I]; - FBufferIndex := 0 - end; - Dec(Size, FBufferSize); - F := FFeedback; - I := 0; - while I < Size do - begin - DoEncode(F, FBuffer, FBufferSize); - XORBuffers(Source[I], FBuffer[0], FBufferSize, Dest[I]); - F := @Dest[I]; - Inc(I, FBufferSize); - end; - if F <> FFeedback then - Move(F^, FFeedback^, FBufferSize); - Dec(Size, I - FBufferSize); - if Size > 0 then - begin - DoEncode(FFeedback, FBuffer, FBufferSize); - XORBuffers(Source[I], FBuffer[0], Size, Dest[I]); - Move(Dest[I], FFeedback[0], Size); - FBufferIndex := Size; - end; -end; - -procedure TDECCipherModes.EncodeOFBx(Source, Dest: PByteArray; Size: Integer); -// OFB-BlockSize -var - I: Integer; -begin - FState := csEncode; - if FBufferIndex > 0 then - begin - I := FBufferSize - FBufferIndex; - if I > Size then - I := Size; - XORBuffers(Source[0], FFeedback[FBufferIndex], I, Dest[0]); - Inc(FBufferIndex, I); - if FBufferIndex < FBufferSize then - Exit; - Dec(Size, I); - Source := @Source[I]; - Dest := @Dest[I]; - FBufferIndex := 0 - end; - Dec(Size, FBufferSize); - I := 0; - while I < Size do - begin - DoEncode(FFeedback, FFeedback, FBufferSize); - XORBuffers(Source[I], FFeedback[0], FBufferSize, Dest[I]); - Inc(I, FBufferSize); - end; - Dec(Size, I - FBufferSize); - if Size > 0 then - begin - DoEncode(FFeedback, FFeedback, FBufferSize); - XORBuffers(Source[I], FFeedback[0], Size, Dest[I]); - FBufferIndex := Size; - end; -end; - -procedure TDECCipherModes.EncodeCFSx(Source, Dest: PByteArray; Size: Integer); -// CFS-BlockSize -var - I: Integer; -begin - FState := csEncode; - if FBufferIndex > 0 then - begin - I := FBufferSize - FBufferIndex; - if I > Size then - I := Size; - XORBuffers(Source[0], FBuffer[FBufferIndex], I, Dest[0]); - XORBuffers(Dest[0], FFeedback[FBufferIndex], I, FFeedback[FBufferIndex]); - Inc(FBufferIndex, I); - if FBufferIndex < FBufferSize then - Exit; - Dec(Size, I); - Source := @Source[I]; - Dest := @Dest[I]; - FBufferIndex := 0 - end; - Dec(Size, FBufferSize); - I := 0; - while I < Size do - begin - DoEncode(FFeedback, FBuffer, FBufferSize); - XORBuffers(Source[I], FBuffer[0], FBufferSize, Dest[I]); - XORBuffers(Dest[I], FFeedback[0], FBufferSize, FFeedback[0]); - Inc(I, FBufferSize); - end; - Dec(Size, I - FBufferSize); - if Size > 0 then - begin - DoEncode(FFeedback, FBuffer, FBufferSize); - XORBuffers(Source[I], FBuffer[0], Size, Dest[I]); - XORBuffers(Dest[I], FFeedback[0], Size, FFeedback[0]); - FBufferIndex := Size; - end; -end; - -procedure TDECCipherModes.EncodeCBCx(Source, Dest: PByteArray; Size: Integer); -var - F: PByteArray; - I: Integer; -begin - Dec(Size, FBufferSize); - F := FFeedback; - I := 0; - while I <= Size do - begin - XORBuffers(Source[I], F[0], FBufferSize, Dest[I]); - F := @Dest[I]; - DoEncode(F, F, FBufferSize); - Inc(I, FBufferSize); - end; - if F <> FFeedback then - Move(F[0], FFeedback[0], FBufferSize); - Dec(Size, I - FBufferSize); - if Size > 0 then - begin // padding - EncodeCFB8(@Source[I], @Dest[I], Size); - FState := csPadded; - end - else - FState := csEncode; -end; - -procedure TDECCipherModes.EncodeCTSx(Source, Dest: PByteArray; Size: Integer); -var - I: Integer; -begin - Dec(Size, FBufferSize); - I := 0; - while I <= Size do - begin - XORBuffers(Source[I], FFeedback[0], FBufferSize, Dest[I]); - DoEncode(@Dest[I], @Dest[I], FBufferSize); - XORBuffers(Dest[I], FFeedback[0], FBufferSize, FFeedback[0]); - Inc(I, FBufferSize); - end; - Dec(Size, I - FBufferSize); - if Size > 0 then - begin // padding - EncodeCFS8(@Source[I], @Dest[I], Size); - FState := csPadded; - end - else - FState := csEncode; -end; - -{$IFDEF DEC3_CMCTS} -procedure TDECCipherModes.EncodeCTS3(Source, Dest: PByteArray; Size: Integer); -var - I: Integer; -begin - Dec(Size, FBufferSize); - I := 0; - while I <= Size do - begin - XORBuffers(Source[I], FFeedback[0], FBufferSize, Dest[I]); - DoEncode(@Dest[I], @Dest[I], FBufferSize); - XORBuffers(Dest[I], FFeedback[0], FBufferSize, FFeedback[0]); - Inc(I, FBufferSize); - end; - Dec(Size, I - FBufferSize); - if Size > 0 then - begin // padding - EncodeCFSx(@Source[I], @Dest[I], Size); // use the padding implemented in CFSx - FState := csPadded; - end - else - FState := csEncode; -end; -{$ENDIF DEC3_CMCTS} - -procedure TDECCipherModes.Decode(const Source; var Dest; DataSize: Integer); -begin - CheckState([csInitialized, csDecode, csDone]); - - case FMode of - cmECBx: DecodeECBx(@Source, @Dest, DataSize); - cmCBCx: DecodeCBCx(@Source, @Dest, DataSize); - cmCTSx: DecodeCTSx(@Source, @Dest, DataSize); - {$IFDEF DEC3_CMCTS} - cmCTS3: DecodeCTS3(@Source, @Dest, DataSize); - {$ENDIF DEC3_CMCTS} - cmCFB8: DecodeCFB8(@Source, @Dest, DataSize); - cmCFBx: DecodeCFBx(@Source, @Dest, DataSize); - cmOFB8: DecodeOFB8(@Source, @Dest, DataSize); - cmOFBx: DecodeOFBx(@Source, @Dest, DataSize); - cmCFS8: DecodeCFS8(@Source, @Dest, DataSize); - cmCFSx: DecodeCFSx(@Source, @Dest, DataSize); - end; -end; - -procedure TDECCipherModes.DecodeECBx(Source, Dest: PByteArray; Size: Integer); -var - I: Integer; -begin - if Context.BlockSize = 1 then - begin - DoDecode(Source, Dest, Size); - FState := csDecode; - end - else - begin - Dec(Size, FBufferSize); - I := 0; - while I <= Size do - begin - DoDecode(@Source[I], @Dest[I], FBufferSize); - Inc(I, FBufferSize); - end; - Dec(Size, I - FBufferSize); - if Size > 0 then - begin - if Size mod Context.BlockSize = 0 then - begin - DoDecode(@Source[I], @Dest[I], Size); - FState := csDecode; - end - else - begin - FState := csPadded; - ReportInvalidMessageLength(Self); - end; - end; - end; -end; - -procedure TDECCipherModes.DecodeCFB8(Source, Dest: PByteArray; Size: Integer); -// CFB-8 -var - I: Integer; -begin - I := 0; - while I < Size do - begin - DoEncode(FFeedback, FBuffer, FBufferSize); - Move(FFeedback[1], FFeedback[0], FBufferSize - 1); - FFeedback[FBufferSize - 1] := Source[I]; - Dest[I] := Source[I] xor FBuffer[0]; - Inc(I); - end; - FState := csDecode; -end; - -procedure TDECCipherModes.DecodeOFB8(Source, Dest: PByteArray; Size: Integer); -// same as EncodeOFB -var - I: Integer; -begin - I := 0; - while I < Size do - begin - DoEncode(FFeedback, FBuffer, FBufferSize); - Move(FFeedback[1], FFeedback[0], FBufferSize - 1); - FFeedback[FBufferSize - 1] := FBuffer[0]; - Dest[I] := Source[I] xor FBuffer[0]; - Inc(I); - end; - FState := csDecode; -end; - -procedure TDECCipherModes.DecodeCFS8(Source, Dest: PByteArray; Size: Integer); -var - I: Integer; -begin - I := 0; - while I < Size do - begin - DoEncode(FFeedback, FBuffer, FBufferSize); - Move(FFeedback[1], FFeedback[0], FBufferSize - 1); - FFeedback[FBufferSize - 1] := FFeedback[FBufferSize - 1] xor Source[I]; - Dest[I] := Source[I] xor FBuffer[0]; - Inc(I); - end; - FState := csDecode; -end; - -procedure TDECCipherModes.DecodeCFBx(Source, Dest: PByteArray; Size: Integer); -// CFB-BlockSize -var - I: Integer; - F: PByteArray; -begin - FState := csDecode; - if FBufferIndex > 0 then - begin // remaining bytes of last decode - I := FBufferSize - FBufferIndex; - if I > Size then - I := Size; - Move(Source[0], FFeedback[FBufferIndex], I); - XORBuffers(Source[0], FBuffer[FBufferIndex], I, Dest[0]); - Inc(FBufferIndex, I); - if FBufferIndex < FBufferSize then - Exit; - Dec(Size, I); - Source := @Source[I]; - Dest := @Dest[I]; - FBufferIndex := 0 - end; - // process chunks of FBufferSize bytes - Dec(Size, FBufferSize); - I := 0; - if Source <> Dest then - begin - F := FFeedback; - while I < Size do - begin - DoEncode(F, FBuffer, FBufferSize); - XORBuffers(Source[I], FBuffer[0], FBufferSize, Dest[I]); - F := @Source[I]; - Inc(I, FBufferSize); - end; - if F <> FFeedback then - Move(F^, FFeedback^, FBufferSize); - end - else - while I < Size do - begin - DoEncode(FFeedback, FBuffer, FBufferSize); - Move(Source[I], FFeedback[0], FBufferSize); - XORBuffers(Source[I], FBuffer[0], FBufferSize, Dest[I]); - Inc(I, FBufferSize); - end; - Dec(Size, I - FBufferSize); - if Size > 0 then - begin // remaining bytes - DoEncode(FFeedback, FBuffer, FBufferSize); - Move(Source[I], FFeedback[0], Size); - XORBuffers(Source[I], FBuffer[0], Size, Dest[I]); - FBufferIndex := Size; - end; -end; - -procedure TDECCipherModes.DecodeOFBx(Source, Dest: PByteArray; Size: Integer); -// OFB-BlockSize, same as EncodeOFBx -var - I: Integer; -begin - FState := csDecode; - if FBufferIndex > 0 then - begin - I := FBufferSize - FBufferIndex; - if I > Size then - I := Size; - XORBuffers(Source[0], FFeedback[FBufferIndex], I, Dest[0]); - Inc(FBufferIndex, I); - if FBufferIndex < FBufferSize then - Exit; - Dec(Size, I); - Source := @Source[I]; - Dest := @Dest[I]; - FBufferIndex := 0 - end; - Dec(Size, FBufferSize); - I := 0; - while I < Size do - begin - DoEncode(FFeedback, FFeedback, FBufferSize); - XORBuffers(Source[I], FFeedback[0], FBufferSize, Dest[I]); - Inc(I, FBufferSize); - end; - Dec(Size, I - FBufferSize); - if Size > 0 then - begin - DoEncode(FFeedback, FFeedback, FBufferSize); - XORBuffers(Source[I], FFeedback[0], Size, Dest[I]); - FBufferIndex := Size; - end; -end; - -procedure TDECCipherModes.DecodeCFSx(Source, Dest: PByteArray; Size: Integer); -// CFS-BlockSize -var - I: Integer; -begin - FState := csDecode; - if FBufferIndex > 0 then - begin // remaining bytes of last decode - I := FBufferSize - FBufferIndex; - if I > Size then - I := Size; - XORBuffers(Source[0], FFeedback[FBufferIndex], I, FFeedback[FBufferIndex]); - XORBuffers(Source[0], FBuffer[FBufferIndex], I, Dest[0]); - Inc(FBufferIndex, I); - if FBufferIndex < FBufferSize then - Exit; - Dec(Size, I); - Source := @Source[I]; - Dest := @Dest[I]; - FBufferIndex := 0 - end; - // process chunks of FBufferSize bytes - Dec(Size, FBufferSize); - I := 0; - while I < Size do - begin - DoEncode(FFeedback, FBuffer, FBufferSize); - XORBuffers(Source[I], FFeedback[0], FBufferSize, FFeedback[0]); - XORBuffers(Source[I], FBuffer[0], FBufferSize, Dest[I]); - Inc(I, FBufferSize); - end; - Dec(Size, I - FBufferSize); - if Size > 0 then - begin // remaining bytes - DoEncode(FFeedback, FBuffer, FBufferSize); - XORBuffers(Source[I], FFeedback[0], Size, FFeedback[0]); - XORBuffers(Source[I], FBuffer[0], Size, Dest[I]); - FBufferIndex := Size; - end; -end; - -procedure TDECCipherModes.DecodeCBCx(Source, Dest: PByteArray; Size: Integer); -var - I: Integer; - F, B, T: PByteArray; -begin - Dec(Size, FBufferSize); - F := FFeedback; - I := 0; - if Source = Dest then - begin - B := FBuffer; - while I <= Size do - begin - Move(Source[I], B[0], FBufferSize); - DoDecode(@Source[I], @Source[I], FBufferSize); - XORBuffers(Source[I], F[0], FBufferSize, Source[I]); - T := F; - F := B; - B := T; - Inc(I, FBufferSize); - end; - end - else - begin - while I <= Size do - begin - DoDecode(@Source[I], @Dest[I], FBufferSize); - XORBuffers(F[0], Dest[I], FBufferSize, Dest[I]); - F := @Source[I]; - Inc(I, FBufferSize); - end; - end; - if F <> FFeedback then - Move(F[0], FFeedback[0], FBufferSize); - Dec(Size, I - FBufferSize); - if Size > 0 then - begin - DecodeCFB8(@Source[I], @Dest[I], Size); - FState := csPadded; - end - else - FState := csDecode; -end; - -procedure TDECCipherModes.DecodeCTSx(Source, Dest: PByteArray; Size: Integer); -var - I: Integer; - F, B, T: PByteArray; -begin - Dec(Size, FBufferSize); - F := FFeedback; - B := FBuffer; - I := 0; - while I <= Size do - begin - XORBuffers(Source[I], F[0], FBufferSize, B[0]); - DoDecode(@Source[I], @Dest[I], FBufferSize); - XORBuffers(Dest[I], F[0], FBufferSize, Dest[I]); - T := B; - B := F; - F := T; - Inc(I, FBufferSize); - end; - if F <> FFeedback then - Move(F[0], FFeedback[0], FBufferSize); - Dec(Size, I - FBufferSize); - if Size > 0 then - begin - DecodeCFS8(@Source[I], @Dest[I], Size); - FState := csPadded; - end - else - FState := csDecode; -end; - -{$IFDEF DEC3_CMCTS} -procedure DecodeCTS3(Source, Dest: PByteArray; Size: Integer); -var - I: Integer; - F, B, T: PByteArray; -begin - Dec(Size, FBufferSize); - F := FFeedback; - B := FBuffer; - I := 0; - while I <= Size do - begin - XORBuffers(Source[I], F[0], FBufferSize, B[0]); - DoDecode(@Source[I], @Dest[I], FBufferSize); - XORBuffers(Dest[I], F[0], FBufferSize, Dest[I]); - T := B; - B := F; - F := T; - Inc(I, FBufferSize); - end; - if F <> FFeedback then - Move(F[0], FFeedback[0], FBufferSize); - Dec(Size, I - FBufferSize); - if Size > 0 then - begin - DecodeCFSx(@Source[I], @Dest[I], Size); // use the padding implemented in CFSx - FState := csPadded; - end - else - FState := csDecode; -end; -{$ENDIF DEC3_CMCTS} - -end. diff --git a/Source/DECCiphers.pas b/Source/DECCiphers.pas deleted file mode 100644 index 8f74ea9d..00000000 --- a/Source/DECCiphers.pas +++ /dev/null @@ -1,6612 +0,0 @@ -{***************************************************************************** - The DEC team (see file NOTICE.txt) licenses this file - to you under the Apache License, Version 2.0 (the - "License"); you may not use this file except in compliance - with the License. A copy of this licence is found in the root directory - of this project in the file LICENCE.txt or alternatively at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, - software distributed under the License is distributed on an - "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - KIND, either express or implied. See the License for the - specific language governing permissions and limitations - under the License. -*****************************************************************************} -unit DECCiphers; - -interface - -{$INCLUDE DECOptions.inc} - -uses - DECCipherBase, DECCipherFormats, DECUtil, DECTypes; - -type - // Cipher Classes - - /// - /// Null cipher, doesn't encrypt, only copy - /// - TCipher_Null = class; - /// - /// A block based encryption algorithm with 32 to 448 bit key length - /// - TCipher_Blowfish = class; - /// - /// AES Round 2 Final Candidate - /// - TCipher_Twofish = class; - /// - /// International Data Encryption Algorithm, formerly patentet, - /// now patent free. The algorithm is no longer to be really recommended due - /// to some classes of weak keys and other successfull attacks. - /// - TCipher_IDEA = class; - /// - /// Carlisle Adams and Stafford Tavares, 256 bit key length - /// - TCipher_Cast256 = class; - /// - /// AES Round 2 Final Candidate - /// - TCipher_Mars = class; - /// - /// Streamcipher in as Block Cipher - /// - TCipher_RC4 = class; - /// - /// AES Round 2 Final Candidate - /// - TCipher_RC6 = class; - /// - /// AES Round 2 Final Candidate - /// - TCipher_Rijndael = class; - /// - /// AES winner = TCipher_Rijndael - /// - TCipher_AES = class; - /// - /// A block cipher invented by Joan Daemen and Vincent Rijmen. The design, - /// published in 1997, is a forerunner to Rijndael, which has been adopted - /// as the Advanced Encryption Standard. Square was introduced together with - /// a new form of cryptanalysis discovered by Lars Knudsen, called the - /// "Square attack". - /// The structure of Square is a substitution-permutation network with eight - /// rounds, operating on 128-bit blocks and using a 128-bit key. - /// - /// - /// If possible use TCipher_AES instead - /// - TCipher_Square = class; - /// - /// Stream Cipher in Blockmode (on UInt32), very fast - /// - TCipher_SCOP = class; - /// - /// Stream Cipher in Blockmode (on UInt32), very fast. - /// Wrong old version from DEC 5.2. Use only for backwards compatibility! - /// - TCipher_SCOP_DEC52 = class; - /// - /// Stream Cipher, eq. design from German ENIGMA Machine - /// - TCipher_Sapphire = class; - /// - /// Single DES 8 byte Blocksize, 8 byte Keysize, 56 bits relevant. - /// Considered to be too weak nowadays. Included for compatibility reasons. - /// - TCipher_1DES = class; - /// - /// Double DES 8 byte Blocksize, 16 byte Keysize, 112 bits relevant - /// - TCipher_2DES = class; - /// - /// Triple DES 8 byte Blocksize, 24 byte Keysize, 168 bits relevant - /// - TCipher_3DES = class; - /// - /// Triple DES 16 byte Blocksize, 16 byte Keysize, 112 bits relevant - /// - TCipher_2DDES = class; - /// - /// Triple DES 16 byte Blocksize, 24 byte Keysize, 168 bits relevant - /// - TCipher_3DDES = class; - /// - /// Triple DES 24 byte Blocksize, 24 byte Keysize, 168 bits relevant - /// - TCipher_3TDES = class; - /// - /// A 1994 developed block cipher using a 96 bit key. 3-Way, is vulnerable - /// to related key cryptanalysis. - /// - TCipher_3Way = class; - /// - /// Carlisle Adams and Stafford Tavares, 128 bit key length - /// - TCipher_Cast128 = class; - /// - /// Russian Cipher - /// - TCipher_Gost = class; - /// - /// Alias/new name for Gost cipher - /// - TCipher_Magma = class; - /// - /// Misty1 is a block cipher developed 1995 by Mitsubishi. It is free only for - /// academical and non-profit works in RFC 2994. it is otherwise patented. - /// In 2015 it got broken via integral cryptoanalysis. - /// - TCipher_Misty = class; - /// - /// A 1996 block cipher with a key length of 120 bit. It can be broken with - /// a relatively low number of ciphertext/plaintext queries. - /// - TCipher_NewDES = class; - /// - /// Camelia, a 128 bit block cipher. - /// Specification: https://info.isl.ntt.co.jp/crypt/eng/camellia/dl/01espec.pdf - /// - TCipher_Q128 = class; - /// - /// Rivest Cipher 2, a 1987 developed cipher with a default keysize of 64 bit - /// - TCipher_RC2 = class; - /// - /// Rivest Cipher 5, a 1994 developed cipher with emphasis on speed and low - /// size in order to make it efficient on embedded hardware as well. Key sizes - /// of up to 2048 bits are possible but 128 bits are suggested. The algorithm - /// was patented in the US up to 2015. - /// - TCipher_RC5 = class; - /// - /// SAFER = Secure And Fast Encryption Routine - /// - TCipher_SAFER = class; - /// - /// A 1996 published block cipher with a key size of 128 bits. It was - /// identified as one of the predecessors of Rijndael - /// - TCipher_Shark = class; - /// - /// A 1996 published block cipher with a key size of 128 bits. It was - /// identified as one of the predecessors of Rijndael - /// Wrong old version from DEC 5.2. Use only for backwards compatibility! - /// - TCipher_Shark_DEC52 = class; - /// - /// A NSA developed and 1998 published block cipher with a key length of - /// 80 bit. Soon after publication various weaknesses have been identified. - /// - TCipher_Skipjack = class; - /// - /// Tiny Encryption Algorithm - /// - TCipher_TEA = class; - /// - /// Tiny Encryption Algorithm, 1st extended Version - /// - TCipher_XTEA = class; - /// - /// = TCipher_XTEA (kept for backward compatibility) - /// - TCipher_TEAN = class; - /// - /// Tiny Encryption Algorithm, 1st extended Version. - /// Wrong old version from DEC 5.2. Use only for backwards compatibility! - /// - TCipher_XTEA_DEC52 = class; - - // Definitions needed for Skipjack algorithm - PSkipjackTab = ^TSkipjackTab; - TSkipjackTab = array[0..255] of Byte; - - /// - /// A do nothing cipher, usefull for debugging and development purposes. Do - /// not use it for actual encryption as it will not encrypt anything at all! - /// - TCipher_Null = class(TDECFormattedCipher) - protected - procedure DoInit(const Key; Size: Integer); override; - procedure DoEncode(Source, Dest: Pointer; Size: Integer); override; - procedure DoDecode(Source, Dest: Pointer; Size: Integer); override; - public - /// - /// Provides meta data about the cipher algorithm used like key size. - /// - class function Context: TCipherContext; override; - end; - - TCipher_Blowfish = class(TDECFormattedCipher) - protected - /// - /// Initialize the key, based on the key passed in - /// - /// - /// Encryption/Decryption key to be used - /// - /// - /// Size of the key passed in bytes. - /// - procedure DoInit(const Key; Size: Integer); override; - procedure DoEncode(Source, Dest: Pointer; Size: Integer); override; - procedure DoDecode(Source, Dest: Pointer; Size: Integer); override; - public - class function Context: TCipherContext; override; - end; - - TCipher_Twofish = class(TDECFormattedCipher) - protected - /// - /// Initialize the key, based on the key passed in - /// - /// - /// Encryption/Decryption key to be used - /// - /// - /// Size of the key passed in bytes. - /// - procedure DoInit(const Key; Size: Integer); override; - procedure DoEncode(Source, Dest: Pointer; Size: Integer); override; - procedure DoDecode(Source, Dest: Pointer; Size: Integer); override; - public - class function Context: TCipherContext; override; - end; - - TCipher_IDEA = class(TDECFormattedCipher) - protected - /// - /// Initialize the key, based on the key passed in - /// - /// - /// Encryption/Decryption key to be used - /// - /// - /// Size of the key passed in bytes. - /// - procedure DoInit(const Key; Size: Integer); override; - procedure DoEncode(Source, Dest: Pointer; Size: Integer); override; - procedure DoDecode(Source, Dest: Pointer; Size: Integer); override; - public - class function Context: TCipherContext; override; - end; - - TCipher_Cast256 = class(TDECFormattedCipher) - protected - /// - /// Initialize the key, based on the key passed in - /// - /// - /// Encryption/Decryption key to be used - /// - /// - /// Size of the key passed in bytes. - /// - procedure DoInit(const Key; Size: Integer); override; - procedure DoEncode(Source, Dest: Pointer; Size: Integer); override; - procedure DoDecode(Source, Dest: Pointer; Size: Integer); override; - public - class function Context: TCipherContext; override; - end; - - TCipher_Mars = class(TDECFormattedCipher) - protected - /// - /// Initialize the key, based on the key passed in - /// - /// - /// Encryption/Decryption key to be used - /// - /// - /// Size of the key passed in bytes. - /// - procedure DoInit(const Key; Size: Integer); override; - procedure DoEncode(Source, Dest: Pointer; Size: Integer); override; - procedure DoDecode(Source, Dest: Pointer; Size: Integer); override; - public - class function Context: TCipherContext; override; - end; - - /// - /// This is a well known stream cipher. In February 2015 its use in context - /// of TLS has been forbidden due to severe security issues. - /// - TCipher_RC4 = class(TDECFormattedCipher) - protected - /// - /// Initialize the key, based on the key passed in - /// - /// - /// Encryption/Decryption key to be used - /// - /// - /// Size of the key passed in bytes. - /// - procedure DoInit(const Key; Size: Integer); override; - procedure DoEncode(Source, Dest: Pointer; Size: Integer); override; - procedure DoDecode(Source, Dest: Pointer; Size: Integer); override; - public - class function Context: TCipherContext; override; - end; - - TCipher_RC6 = class(TDECFormattedCipher) - private - FRounds: Integer; - procedure SetRounds(Value: Integer); - /// - /// Limits the number of rounds used to a minimum or maximum value, - /// depending on the current value. If FRounds is 0 it will be set to 20. - /// - procedure LimitRounds; inline; - protected - /// - /// Initialize the key, based on the key passed in - /// - /// - /// Encryption/Decryption key to be used - /// - /// - /// Size of the key passed in bytes. - /// - procedure DoInit(const Key; Size: Integer); override; - procedure DoEncode(Source, Dest: Pointer; Size: Integer); override; - procedure DoDecode(Source, Dest: Pointer; Size: Integer); override; - public - class function Context: TCipherContext; override; - /// - /// Sets the number of rounds/times the algorithm is being applied to the - /// data. Range should be 16-24 and default is 20 rounds. - /// - property Rounds: Integer read FRounds write SetRounds; - end; - - TCipher_Rijndael = class(TDECFormattedCipher) - private - FRounds: Integer; - protected - /// - /// Initialize the key, based on the key passed in - /// - /// - /// Encryption/Decryption key to be used - /// - /// - /// Size of the key passed in bytes. - /// - procedure DoInit(const Key; Size: Integer); override; - procedure DoEncode(Source, Dest: Pointer; Size: Integer); override; - procedure DoDecode(Source, Dest: Pointer; Size: Integer); override; - public - class function Context: TCipherContext; override; - /// - /// Gets the number of rounds/times the algorithm is being applied to the - /// data. The number of rounds depends on the key size. - /// - property Rounds: Integer read FRounds; - end; - - TCipher_AES = class(TCipher_Rijndael); - - TCipher_Square = class(TDECFormattedCipher) - protected - /// - /// Initialize the key, based on the key passed in - /// - /// - /// Encryption/Decryption key to be used - /// - /// - /// Size of the key passed in bytes. - /// - procedure DoInit(const Key; Size: Integer); override; - procedure DoEncode(Source, Dest: Pointer; Size: Integer); override; - procedure DoDecode(Source, Dest: Pointer; Size: Integer); override; - public - class function Context: TCipherContext; override; - end; - - TCipher_SCOP = class(TDECFormattedCipher) - protected - /// - /// Initialize the key, based on the key passed in - /// - /// - /// Encryption/Decryption key to be used - /// - /// - /// Size of the key passed in bytes. - /// - procedure DoInit(const Key; Size: Integer); override; - procedure DoEncode(Source, Dest: Pointer; Size: Integer); override; - procedure DoDecode(Source, Dest: Pointer; Size: Integer); override; - public - class function Context: TCipherContext; override; - end; - - /// - /// Do only use if backwards compatibility with old code is necessary as - /// this implementation is faulty! - /// - TCipher_SCOP_DEC52 = class(TDECFormattedCipher) - protected - /// - /// Initialize the key, based on the key passed in - /// - /// - /// Encryption/Decryption key to be used - /// - /// - /// Size of the key passed in bytes. - /// - procedure DoInit(const Key; Size: Integer); override; - procedure DoEncode(Source, Dest: Pointer; Size: Integer); override; - procedure DoDecode(Source, Dest: Pointer; Size: Integer); override; - public - class function Context: TCipherContext; override; - end; - - TCipher_Sapphire = class(TDECFormattedCipher) - protected - /// - /// Initialize the key, based on the key passed in - /// - /// - /// Encryption/Decryption key to be used - /// - /// - /// Size of the key passed in bytes. - /// - procedure DoInit(const Key; Size: Integer); override; - procedure DoEncode(Source, Dest: Pointer; Size: Integer); override; - procedure DoDecode(Source, Dest: Pointer; Size: Integer); override; - public - class function Context: TCipherContext; override; - end; - - TCipher_1DES = class(TDECFormattedCipher) - protected - /// - /// Initialize the key, based on the key passed in - /// - /// - /// Key for the current block to be encrypted/decrypted? - /// - /// - /// Encryption/Decryption key to be used - /// - /// - /// Size of the key passed in bytes. - /// - /// - /// Defines whether some internal calculation needs to be based from the - /// start index or the highest index (= reverse) - /// - procedure DoInitKey(const Data: array of Byte; Key: PUInt32Array; Reverse: Boolean); - procedure DoInit(const Key; Size: Integer); override; - procedure DoEncode(Source, Dest: Pointer; Size: Integer); override; - procedure DoDecode(Source, Dest: Pointer; Size: Integer); override; - public - class function Context: TCipherContext; override; - end; - - TCipher_2DES = class(TCipher_1DES) - protected - /// - /// Initialize the key, based on the key passed in - /// - /// - /// Encryption/Decryption key to be used - /// - /// - /// Size of the key passed in bytes. - /// - procedure DoInit(const Key; Size: Integer); override; - procedure DoEncode(Source, Dest: Pointer; Size: Integer); override; - procedure DoDecode(Source, Dest: Pointer; Size: Integer); override; - public - class function Context: TCipherContext; override; - end; - - TCipher_3DES = class(TCipher_1DES) - protected - /// - /// Initialize the key, based on the key passed in - /// - /// - /// Encryption/Decryption key to be used - /// - /// - /// Size of the key passed in bytes. - /// - procedure DoInit(const Key; Size: Integer); override; - procedure DoEncode(Source, Dest: Pointer; Size: Integer); override; - procedure DoDecode(Source, Dest: Pointer; Size: Integer); override; - public - class function Context: TCipherContext; override; - end; - - TCipher_2DDES = class(TCipher_2DES) - protected - procedure DoEncode(Source, Dest: Pointer; Size: Integer); override; - procedure DoDecode(Source, Dest: Pointer; Size: Integer); override; - public - class function Context: TCipherContext; override; - end; - - TCipher_3DDES = class(TCipher_3DES) - protected - procedure DoEncode(Source, Dest: Pointer; Size: Integer); override; - procedure DoDecode(Source, Dest: Pointer; Size: Integer); override; - public - class function Context: TCipherContext; override; - end; - - TCipher_3TDES = class(TCipher_3DES) - protected - procedure DoEncode(Source, Dest: Pointer; Size: Integer); override; - procedure DoDecode(Source, Dest: Pointer; Size: Integer); override; - public - class function Context: TCipherContext; override; - end; - - TCipher_3Way = class(TDECFormattedCipher) - protected - /// - /// Initialize the key, based on the key passed in - /// - /// - /// Encryption/Decryption key to be used - /// - /// - /// Size of the key passed in bytes. - /// - procedure DoInit(const Key; Size: Integer); override; - procedure DoEncode(Source, Dest: Pointer; Size: Integer); override; - procedure DoDecode(Source, Dest: Pointer; Size: Integer); override; - public - class function Context: TCipherContext; override; - end; - - TCipher_Cast128 = class(TDECFormattedCipher) - private - FRounds: Integer; - procedure SetRounds(Value: Integer); - protected - /// - /// Initialize the key, based on the key passed in - /// - /// - /// Encryption/Decryption key to be used - /// - /// - /// Size of the key passed in bytes. - /// - procedure DoInit(const Key; Size: Integer); override; - procedure DoEncode(Source, Dest: Pointer; Size: Integer); override; - procedure DoDecode(Source, Dest: Pointer; Size: Integer); override; - public - class function Context: TCipherContext; override; - - /// - /// Sets the number of rounds/times the algorithm is being applied to the - /// data. Default value is 16 rounds. - /// - property Rounds: Integer read FRounds write SetRounds; - end; - - TCipher_Gost = class(TDECFormattedCipher) - protected - /// - /// Initialize the key, based on the key passed in - /// - /// - /// Encryption/Decryption key to be used - /// - /// - /// Size of the key passed in bytes. - /// - procedure DoInit(const Key; Size: Integer); override; - procedure DoEncode(Source, Dest: Pointer; Size: Integer); override; - procedure DoDecode(Source, Dest: Pointer; Size: Integer); override; - public - class function Context: TCipherContext; override; - end; - - /// - /// Alias for Gost - /// - TCipher_Magma = class(TCipher_Gost); - - /// - /// Do no longer use this algorithm if possible, as it got broken in 2015 - /// by crypto analysis. - /// - TCipher_Misty = class(TDECFormattedCipher) - protected - /// - /// Initialize the key, based on the key passed in - /// - /// - /// Encryption/Decryption key to be used - /// - /// - /// Size of the key passed in bytes. - /// - procedure DoInit(const Key; Size: Integer); override; - procedure DoEncode(Source, Dest: Pointer; Size: Integer); override; - procedure DoDecode(Source, Dest: Pointer; Size: Integer); override; - public - class function Context: TCipherContext; override; - end; - - /// - /// While this algorithm resembles the Data Encryption Standard (DES), - /// it is easier to implement in software and is supposed to be more secure. - /// It is not to be confused with another algorithm - known by the same - /// name - which is simply DES without the initial and final permutations. - /// The NewDES here is a completely different algorithm. - /// - /// Be aware though that recent crypto analysis shows that this algorithm is - /// less safe than DES and thus not to be recommended for use! - /// - TCipher_NewDES = class(TDECFormattedCipher) - protected - /// - /// Initialize the key, based on the key passed in - /// - /// - /// Encryption/Decryption key to be used - /// - /// - /// Size of the key passed in bytes. - /// - procedure DoInit(const Key; Size: Integer); override; - procedure DoEncode(Source, Dest: Pointer; Size: Integer); override; - procedure DoDecode(Source, Dest: Pointer; Size: Integer); override; - public - class function Context: TCipherContext; override; - end; - - TCipher_Q128 = class(TDECFormattedCipher) - protected - /// - /// Initialize the key, based on the key passed in - /// - /// - /// Encryption/Decryption key to be used - /// - /// - /// Size of the key passed in bytes. - /// - procedure DoInit(const Key; Size: Integer); override; - procedure DoEncode(Source, Dest: Pointer; Size: Integer); override; - procedure DoDecode(Source, Dest: Pointer; Size: Integer); override; - public - class function Context: TCipherContext; override; - end; - - TCipher_RC2 = class(TDECFormattedCipher) - protected - /// - /// Initialize the key, based on the key passed in - /// - /// - /// Encryption/Decryption key to be used - /// - /// - /// Size of the key passed in bytes. - /// - procedure DoInit(const Key; Size: Integer); override; - procedure DoEncode(Source, Dest: Pointer; Size: Integer); override; - procedure DoDecode(Source, Dest: Pointer; Size: Integer); override; - public - class function Context: TCipherContext; override; - end; - - TCipher_RC5 = class(TDECFormattedCipher) - private - FRounds: Integer; - procedure SetRounds(Value: Integer); - protected - /// - /// Initialize the key, based on the key passed in - /// - /// - /// Encryption/Decryption key to be used - /// - /// - /// Size of the key passed in bytes. - /// - procedure DoInit(const Key; Size: Integer); override; - procedure DoEncode(Source, Dest: Pointer; Size: Integer); override; - procedure DoDecode(Source, Dest: Pointer; Size: Integer); override; - public - class function Context: TCipherContext; override; - - /// - /// Sets the number of rounds/times the algorithm is being applied to the - /// data. Range should be 8-16 and default is 12 rounds. - /// - property Rounds: Integer read FRounds write SetRounds; - end; - - /// - /// svK40 SAFER K-40 Keysize is 40bit -> 5 Byte - /// svK64 SAFER K-64 Keysize is 64bit -> 8 Byte - /// svK128 SAFER K-128 KeySize is 128bit -> 16 Byte - /// svSK40 SAFER SK-40 Stronger Version from K-40 with better Key Scheduling - /// svSK64 SAFER SK-64 Stronger Version from K-64 with better Key Scheduling - /// svSK128 SAFER SK-128 Stronger Version from K-128 with better Key Scheduling - /// - TSAFERVersion = (svSK128, svSK64, svSK40, svK128, svK64, svK40); - - TCipher_SAFER = class(TDECFormattedCipher) - private - FRounds: Integer; - FVersion: TSAFERVersion; - procedure SetRounds(Value: Integer); - procedure SetVersion(Value: TSAFERVersion); - protected - /// - /// Initialize the key, based on the key passed in - /// - /// - /// Encryption/Decryption key to be used - /// - /// - /// Size of the key passed in bytes. - /// - procedure DoInit(const Key; Size: Integer); override; - procedure DoEncode(Source, Dest: Pointer; Size: Integer); override; - procedure DoDecode(Source, Dest: Pointer; Size: Integer); override; - public - class function Context: TCipherContext; override; - - /// - /// Sets the number of rounds/times the algorithm is being applied to the - /// data. Range should be 4-13 and default is 5, 6, 10 or 8 rounds - /// depending on the version - /// - property Rounds: Integer read FRounds write SetRounds; - property Version: TSAFERVersion read FVersion write SetVersion; - end; - - {$IFNDEF CPU64BITS} - PLong64 = ^TLong64; - TLong64 = packed record - L, R: UInt32; - end; - - PLong64Array = ^TLong64Array; - TLong64Array = array[0..1023] of TLong64; - {$ENDIF} - - TLogArray = array[0..255] of Byte; - - /// - /// Base class for both Shark implementations - /// - TCipher_SharkBase = class(TDECFormattedCipher) - strict protected - {$IFNDEF CPU64BITS} - function Transform(A: TLong64; Log, ALog: TLogArray): TLong64; - function Shark(D: TLong64; K: PLong64): TLong64; - {$ELSE} - function Transform(A: UInt64; Log, ALog: TLogArray): UInt64; - {$ENDIF} - end; - - TCipher_Shark = class(TCipher_SharkBase) - protected - /// - /// Initialize the key, based on the key passed in - /// - /// - /// Encryption/Decryption key to be used - /// - /// - /// Size of the key passed in bytes. - /// - procedure DoInit(const Key; Size: Integer); override; - procedure DoEncode(Source, Dest: Pointer; Size: Integer); override; - procedure DoDecode(Source, Dest: Pointer; Size: Integer); override; - public - class function Context: TCipherContext; override; - end; - - /// - /// Do only use if backwards compatibility with old code is necessary as - /// this implementation is faulty! - /// - TCipher_Shark_DEC52 = class(TCipher_Shark) - protected - /// - /// Initialize the key, based on the key passed in - /// - /// - /// Encryption/Decryption key to be used - /// - /// - /// Size of the key passed in bytes. - /// - procedure DoInit(const Key; Size: Integer); override; - end; - - TCipher_Skipjack = class(TDECFormattedCipher) - strict private - procedure SkipjackIncCheck(var ATab: PSkipjackTab; AMin: PSkipjackTab; AMax: PByte); inline; - procedure SkipjackDecCheck(var ATab: PSkipjackTab; AMin: PByte; AMax: PSkipjackTab); inline; - protected - /// - /// Initialize the key, based on the key passed in - /// - /// - /// Encryption/Decryption key to be used - /// - /// - /// Size of the key passed in bytes. - /// - procedure DoInit(const Key; Size: Integer); override; - procedure DoEncode(Source, Dest: Pointer; Size: Integer); override; - procedure DoDecode(Source, Dest: Pointer; Size: Integer); override; - public - class function Context: TCipherContext; override; - end; - - TCipher_TEA = class(TDECFormattedCipher) - private - FRounds: Integer; - procedure SetRounds(Value: Integer); - protected - /// - /// Initialize the key, based on the key passed in - /// - /// - /// Encryption/Decryption key to be used - /// - /// - /// Size of the key passed in bytes. - /// - procedure DoInit(const Key; Size: Integer); override; - procedure DoEncode(Source, Dest: Pointer; Size: Integer); override; - procedure DoDecode(Source, Dest: Pointer; Size: Integer); override; - public - class function Context: TCipherContext; override; - - /// - /// 16 - 256 Rounds, 16 (default) is sufficient, 64 is the official - /// recommendation. If a value outside the range of 16 to 256 is assigned - /// it will be limited to that range. - /// - property Rounds: Integer read FRounds write SetRounds; - end; - - /// - /// XTEA is an improved version of the TEA algorithm. - /// - /// - /// In DEC V5.2 at least and in former commits of DEC 6.0 development version - /// this algorithm was broken due to differences in brackets and thus returned - /// a different result. It is unclear why nobody reported this as bug yet - /// but be aware that if you need the old variant for compatibility reasons - /// you need a commit from before 3rd December 2020. - /// - TCipher_XTEA = class(TCipher_TEA) - protected - procedure DoEncode(Source, Dest: Pointer; Size: Integer); override; - procedure DoDecode(Source, Dest: Pointer; Size: Integer); override; - end; - - TCipher_TEAN = class(TCipher_XTEA); - - /// - /// XTEA is an improved version of the TEA algorithm. This version is the - /// old faulty one from DEC 5.2. Use only if necessary for compatibility - /// reasons! - /// - /// - /// In DEC V5.2 at least and in former commits of DEC 6.0 development version - /// this algorithm was broken due to differences in brackets and thus returned - /// a different result. It is unclear why nobody reported this as bug yet - /// but be aware that if you need the old variant for compatibility reasons - /// you need a commit from before 3rd December 2020. - /// - TCipher_XTEA_DEC52 = class(TCipher_TEA) - protected - procedure DoEncode(Source, Dest: Pointer; Size: Integer); override; - procedure DoDecode(Source, Dest: Pointer; Size: Integer); override; - end; - -implementation - -{$IFOPT Q+}{$DEFINE RESTORE_OVERFLOWCHECKS}{$Q-}{$ENDIF} -{$IFOPT R+}{$DEFINE RESTORE_RANGECHECKS}{$R-}{$ENDIF} - -uses - {$IFDEF FPC} - SysUtils, - {$ELSE} - System.SysUtils, - {$ENDIF} - DECData, DECDataCipher; - -{ TCipher_Null } - -class function TCipher_Null.Context: TCipherContext; -begin - Result.KeySize := 0; - Result.BlockSize := 1; - Result.BufferSize := 8; - Result.AdditionalBufferSize := 0; - Result.NeedsAdditionalBufferBackup := False; - Result.MinRounds := 1; - Result.MaxRounds := 1; - Result.CipherType := [ctNull, ctSymmetric]; -end; - -procedure TCipher_Null.DoInit(const Key; Size: Integer); -begin - // dummy -end; - -procedure TCipher_Null.DoEncode(Source, Dest: Pointer; Size: Integer); -begin - if Source <> Dest then - Move(Source^, Dest^, Size); -end; - -procedure TCipher_Null.DoDecode(Source, Dest: Pointer; Size: Integer); -begin - if Source <> Dest then - Move(Source^, Dest^, Size); -end; - -{ TCipher_Blowfish } - -type - PBlowfish = ^TBlowfish; - TBlowfish = array[0..3, 0..255] of UInt32; - -class function TCipher_Blowfish.Context: TCipherContext; -begin - Result.KeySize := 56; - Result.BufferSize := 8; - Result.BlockSize := 8; - Result.AdditionalBufferSize := SizeOf(Blowfish_Data) + SizeOf(Blowfish_Key); - Result.NeedsAdditionalBufferBackup := False; - Result.MinRounds := 1; - Result.MaxRounds := 1; - Result.CipherType := [ctSymmetric, ctBlock]; -end; - -procedure TCipher_Blowfish.DoInit(const Key; Size: Integer); -var - I, J: Integer; - B: array[0..1] of UInt32; - K: PByteArray; - P: PUInt32Array; - S: PBlowfish; -begin - K := @Key; - S := FAdditionalBuffer; - P := Pointer(PByte(FAdditionalBuffer) + SizeOf(Blowfish_Data)); // for Pointer Math - - Move(Blowfish_Data, S^, SizeOf(Blowfish_Data)); - Move(Blowfish_Key, P^, Sizeof(Blowfish_Key)); - J := 0; - if Size > 0 then - for I := 0 to 17 do - begin - P[I] := P[I] xor (K[(J + 0) mod Size] shl 24 + - K[(J + 1) mod Size] shl 16 + - K[(J + 2) mod Size] shl 8 + - K[(J + 3) mod Size] shl 0); - J := (J + 4) mod Size; - end; - FillChar(B, SizeOf(B), 0); - - for I := 0 to 8 do - begin - DoEncode(@B, @B, SizeOf(B)); - P[I * 2 + 0] := SwapUInt32(B[0]); - P[I * 2 + 1] := SwapUInt32(B[1]); - end; - for I := 0 to 3 do - for J := 0 to 127 do - begin - DoEncode(@B, @B, SizeOf(B)); - S[I, J * 2 + 0] := SwapUInt32(B[0]); - S[I, J * 2 + 1] := SwapUInt32(B[1]); - end; - FillChar(B, SizeOf(B), 0); -end; - -procedure TCipher_Blowfish.DoEncode(Source, Dest: Pointer; Size: Integer); -{$IFDEF X86ASM} -// Source = EDX, Dest = ECX, Size on Stack -asm - PUSH EDI - PUSH ESI - PUSH EBX - PUSH EBP - PUSH ECX - MOV ESI,[EAX].TCipher_Blowfish.FAdditionalBuffer - MOV EBX,[EDX + 0] // A - MOV EBP,[EDX + 4] // B - BSWAP EBX // CPU >= 486 - BSWAP EBP - XOR EBX,[ESI + 4 * 256 * 4] - XOR EDI,EDI -@@1: MOV EAX,EBX - SHR EBX,16 - MOVZX ECX,BH - AND EBX,0FFh - MOV ECX,[ESI + ECX * 4 + 1024 * 0] - MOV EBX,[ESI + EBX * 4 + 1024 * 1] - MOVZX EDX,AH - ADD EBX,ECX - MOVZX ECX,AL - MOV EDX,[ESI + EDX * 4 + 1024 * 2] - MOV ECX,[ESI + ECX * 4 + 1024 * 3] - XOR EBX,EDX - XOR EBP,[ESI + 4 * 256 * 4 + 4 + EDI * 4] - ADD EBX,ECX - INC EDI - XOR EBX,EBP - TEST EDI,010h - MOV EBP,EAX - JZ @@1 - POP EAX - XOR EBP,[ESI + 4 * 256 * 4 + 17 * 4] - BSWAP EBX - BSWAP EBP - MOV [EAX + 4],EBX - MOV [EAX + 0],EBP - POP EBP - POP EBX - POP ESI - POP EDI -end; -{$ELSE !X86ASM} -var - I, A, B: UInt32; - P: PUInt32Array; - D: PBlowfish; -begin - Assert(Size = Context.BlockSize, 'Size of ' + IntToStr(Size) + ' does not equal '+ - 'block size of ' + IntToStr(Context.BlockSize)); - - D := Pointer(FAdditionalBuffer); - P := Pointer(PByte(FAdditionalBuffer) + SizeOf(Blowfish_Data)); // for Pointer Math - A := SwapUInt32(PUInt32Array(Source)[0]) xor P[0]; P := @P[1]; - B := SwapUInt32(PUInt32Array(Source)[1]); - for I := 0 to 7 do - begin - B := B xor P[0] xor (D[0, A shr 24 ] + - D[1, A shr 16 and $FF] xor - D[2, A shr 8 and $FF] + - D[3, A and $FF]); - - A := A xor P[1] xor (D[0, B shr 24 ] + - D[1, B shr 16 and $FF] xor - D[2, B shr 8 and $FF] + - D[3, B and $FF]); - P := @P[2]; - end; - PUInt32Array(Dest)[0] := SwapUInt32(B xor P[0]); - PUInt32Array(Dest)[1] := SwapUInt32(A); -end; -{$ENDIF !X86ASM} - -procedure TCipher_Blowfish.DoDecode(Source, Dest: Pointer; Size: Integer); -{$IFDEF X86ASM} -asm - PUSH EDI - PUSH ESI - PUSH EBX - PUSH EBP - PUSH ECX - MOV ESI,[EAX].TCipher_Blowfish.FAdditionalBuffer - MOV EBX,[EDX + 0] // A - MOV EBP,[EDX + 4] // B - BSWAP EBX - BSWAP EBP - XOR EBX,[ESI + 4 * 256 * 4 + 17 * 4] - MOV EDI,16 -@@1: MOV EAX,EBX - SHR EBX,16 - MOVZX ECX,BH - MOVZX EDX,BL - MOV EBX,[ESI + ECX * 4 + 1024 * 0] - MOV EDX,[ESI + EDX * 4 + 1024 * 1] - MOVZX ECX,AH - LEA EBX,[EBX + EDX] - MOVZX EDX,AL - MOV ECX,[ESI + ECX * 4 + 1024 * 2] - MOV EDX,[ESI + EDX * 4 + 1024 * 3] - XOR EBX,ECX - XOR EBP,[ESI + 4 * 256 * 4 + EDI * 4] - LEA EBX,[EBX + EDX] - XOR EBX,EBP - DEC EDI - MOV EBP,EAX - JNZ @@1 - POP EAX - XOR EBP,[ESI + 4 * 256 * 4] - BSWAP EBX - BSWAP EBP - MOV [EAX + 0],EBP - MOV [EAX + 4],EBX - POP EBP - POP EBX - POP ESI - POP EDI -end; -{$ELSE !X86ASM} -var - I, A, B: UInt32; - P: PUInt32Array; - D: PBlowfish; -begin - Assert(Size = Context.BlockSize); - - D := Pointer(FAdditionalBuffer); - P := Pointer(PByte(FAdditionalBuffer) + SizeOf(Blowfish_Data) + SizeOf(Blowfish_Key) - SizeOf(Int32)); - A := SwapUInt32(PUInt32Array(Source)[0]) xor P[0]; - B := SwapUInt32(PUInt32Array(Source)[1]); - for I := 0 to 7 do - begin - Dec(PUInt32(P), 2); - B := B xor P[1] xor (D[0, A shr 24 ] + - D[1, A shr 16 and $FF] xor - D[2, A shr 8 and $FF] + - D[3, A and $FF]); - A := A xor P[0] xor (D[0, B shr 24 ] + - D[1, B shr 16 and $FF] xor - D[2, B shr 8 and $FF] + - D[3, B and $FF]); - end; - Dec(PUInt32(P)); - PUInt32Array(Dest)[0] := SwapUInt32(B xor P[0]); - PUInt32Array(Dest)[1] := SwapUInt32(A); -end; -{$ENDIF !X86ASM} - -{ TCipher_Twofish } - -type - PTwofishBox = ^TTwofishBox; - TTwofishBox = array[0..3, 0..255] of UInt32; - - TLongRec = record - case Integer of - 0: (L: UInt32); - 1: (A, B, C, D: Byte); - end; - -class function TCipher_Twofish.Context: TCipherContext; -begin - Result.KeySize := 32; - Result.BufferSize := 16; - Result.BlockSize := 16; - Result.AdditionalBufferSize := 4256; - Result.NeedsAdditionalBufferBackup := False; - Result.MinRounds := 1; - Result.MaxRounds := 1; - Result.CipherType := [ctSymmetric, ctBlock]; -end; - -procedure TCipher_Twofish.DoInit(const Key; Size: Integer); -var - BoxKey: array[0..3] of TLongRec; - SubKey: PUInt32Array; - Box: PTwofishBox; - - procedure SetupKey; - - function Encode(K0, K1: Integer): Integer; - var - R, I, J, G2, G3: Integer; - B: byte; - begin - R := 0; - for I := 0 to 1 do - begin - if I <> 0 then - R := R xor K0 - else - R := R xor K1; - for J := 0 to 3 do - begin - B := R shr 24; - if B and $80 <> 0 then - G2 := (B shl 1 xor $014D) and $FF - else - G2 := B shl 1 and $FF; - if B and 1 <> 0 then - G3 := (B shr 1 and $7F) xor $014D shr 1 xor G2 - else - G3 := (B shr 1 and $7F) xor G2; - R := R shl 8 xor G3 shl 24 xor G2 shl 16 xor G3 shl 8 xor B; - end; - end; - Result := R; - end; - - function F32(X: Integer; K: array of Integer): Integer; - var - A, B, C, D: UInt32; - begin - A := X and $FF; - B := X shr 8 and $FF; - C := X shr 16 and $FF; - D := X shr 24; - if Size = 32 then - begin - A := Twofish_8x8[1, A] xor K[3] and $FF; - B := Twofish_8x8[0, B] xor K[3] shr 8 and $FF; - C := Twofish_8x8[0, C] xor K[3] shr 16 and $FF; - D := Twofish_8x8[1, D] xor K[3] shr 24; - end; - if Size >= 24 then - begin - A := Twofish_8x8[1, A] xor K[2] and $FF; - B := Twofish_8x8[1, B] xor K[2] shr 8 and $FF; - C := Twofish_8x8[0, C] xor K[2] shr 16 and $FF; - D := Twofish_8x8[0, D] xor K[2] shr 24; - end; - A := Twofish_8x8[0, A] xor K[1] and $FF; - B := Twofish_8x8[1, B] xor K[1] shr 8 and $FF; - C := Twofish_8x8[0, C] xor K[1] shr 16 and $FF; - D := Twofish_8x8[1, D] xor K[1] shr 24; - - A := Twofish_8x8[0, A] xor K[0] and $FF; - B := Twofish_8x8[0, B] xor K[0] shr 8 and $FF; - C := Twofish_8x8[1, C] xor K[0] shr 16 and $FF; - D := Twofish_8x8[1, D] xor K[0] shr 24; - - Result := Twofish_Data[0, A] xor Twofish_Data[1, B] xor - Twofish_Data[2, C] xor Twofish_Data[3, D]; - end; - - var - I, J, A, B: Integer; - E, O: array[0..3] of Integer; - K: array[0..7] of Integer; - begin - FillChar(K, SizeOf(K), 0); - Move(Key, K, Size); - if Size <= 16 then - Size := 16 - else - if Size <= 24 then - Size := 24 - else - Size := 32; - J := Size shr 3 - 1; - for I := 0 to J do - begin - E[I] := K[I shl 1]; - O[I] := K[I shl 1 + 1]; - BoxKey[J].L := Encode(E[I], O[I]); - Dec(J); - end; - J := 0; - for I := 0 to 19 do - begin - A := F32(J, E); - B := F32(J + $01010101, O); - B := B shl 8 or B shr 24; - SubKey[I shl 1] := A + B; - B := A + B shl 1; // here buggy instead shr 1 it's correct shl 1 - SubKey[I shl 1 + 1] := B shl 9 or B shr 23; - Inc(J, $02020202); - end; - end; - - procedure DoXOR(D, S: PUInt32Array; Value: UInt32); - var - I: UInt32; - begin - Value := (Value and $FF) * $01010101; - for I := 0 to 63 do - D[I] := S[I] xor Value; - end; - - procedure SetupBox128; - var - L: array[0..255] of Byte; - A, I: Integer; - begin - DoXOR(@L, @Twofish_8x8[0], BoxKey[1].L); - A := BoxKey[0].A; - for I := 0 to 255 do - Box[0, I] := Twofish_Data[0, Twofish_8x8[0, L[I]] xor A]; - DoXOR(@L, @Twofish_8x8[1], BoxKey[1].L shr 8); - A := BoxKey[0].B; - for I := 0 to 255 do - Box[1, I] := Twofish_Data[1, Twofish_8x8[0, L[I]] xor A]; - DoXOR(@L, @Twofish_8x8[0], BoxKey[1].L shr 16); - A := BoxKey[0].C; - for I := 0 to 255 do - Box[2, I] := Twofish_Data[2, Twofish_8x8[1, L[I]] xor A]; - DoXOR(@L, @Twofish_8x8[1], BoxKey[1].L shr 24); - A := BoxKey[0].D; - for I := 0 to 255 do - Box[3, I] := Twofish_Data[3, Twofish_8x8[1, L[I]] xor A]; - end; - - procedure SetupBox192; - var - L: array[0..255] of Byte; - A, B, I: Integer; - begin - DoXOR(@L, @Twofish_8x8[1], BoxKey[2].L); - A := BoxKey[0].A; - B := BoxKey[1].A; - for I := 0 to 255 do - Box[0, I] := Twofish_Data[0, Twofish_8x8[0, Twofish_8x8[0, L[I]] xor B] xor A]; - DoXOR(@L, @Twofish_8x8[1], BoxKey[2].L shr 8); - A := BoxKey[0].B; - B := BoxKey[1].B; - for I := 0 to 255 do - Box[1, I] := Twofish_Data[1, Twofish_8x8[0, Twofish_8x8[1, L[I]] xor B] xor A]; - DoXOR(@L, @Twofish_8x8[0], BoxKey[2].L shr 16); - A := BoxKey[0].C; - B := BoxKey[1].C; - for I := 0 to 255 do - Box[2, I] := Twofish_Data[2, Twofish_8x8[1, Twofish_8x8[0, L[I]] xor B] xor A]; - DoXOR(@L ,@Twofish_8x8[0], BoxKey[2].L shr 24); - A := BoxKey[0].D; - B := BoxKey[1].D; - for I := 0 to 255 do - Box[3, I] := Twofish_Data[3, Twofish_8x8[1, Twofish_8x8[1, L[I]] xor B] xor A]; - end; - - procedure SetupBox256; - var - L: array[0..255] of Byte; - K: array[0..255] of Byte; - A, B, I: Integer; - begin - DoXOR(@K, @Twofish_8x8[1], BoxKey[3].L); - for I := 0 to 255 do - L[I] := Twofish_8x8[1, K[I]]; - DoXOR(@L, @L, BoxKey[2].L); - A := BoxKey[0].A; - B := BoxKey[1].A; - for I := 0 to 255 do - Box[0, I] := Twofish_Data[0, Twofish_8x8[0, Twofish_8x8[0, L[I]] xor B] xor A]; - DoXOR(@K, @Twofish_8x8[0], BoxKey[3].L shr 8); - for I := 0 to 255 do - L[I] := Twofish_8x8[1, K[I]]; - DoXOR(@L, @L, BoxKey[2].L shr 8); - A := BoxKey[0].B; - B := BoxKey[1].B; - for I := 0 to 255 do - Box[1, I] := Twofish_Data[1, Twofish_8x8[0, Twofish_8x8[1, L[I]] xor B] xor A]; - DoXOR(@K, @Twofish_8x8[0], BoxKey[3].L shr 16); - for I := 0 to 255 do - L[I] := Twofish_8x8[0, K[I]]; - DoXOR(@L, @L, BoxKey[2].L shr 16); - A := BoxKey[0].C; - B := BoxKey[1].C; - for I := 0 to 255 do - Box[2, I] := Twofish_Data[2, Twofish_8x8[1, Twofish_8x8[0, L[I]] xor B] xor A]; - DoXOR(@K, @Twofish_8x8[1], BoxKey[3].L shr 24); - for I := 0 to 255 do - L[I] := Twofish_8x8[0, K[I]]; - DoXOR(@L, @L, BoxKey[2].L shr 24); - A := BoxKey[0].D; - B := BoxKey[1].D; - for I := 0 to 255 do - Box[3, I] := Twofish_Data[3, Twofish_8x8[1, Twofish_8x8[1, L[I]] xor B] xor A]; - end; - -begin - SubKey := FAdditionalBuffer; - Box := @SubKey[40]; - SetupKey; - if Size = 16 then - SetupBox128 - else - if Size = 24 then - SetupBox192 - else - SetupBox256; -end; - -procedure TCipher_Twofish.DoEncode(Source, Dest: Pointer; Size: Integer); -var - S: PUInt32Array; - Box: PTwofishBox; - I, X, Y: UInt32; - A, B, C, D: TLongRec; -begin - Assert(Size = Context.BlockSize); - - S := FAdditionalBuffer; - A.L := PUInt32Array(Source)[0] xor S[0]; - B.L := PUInt32Array(Source)[1] xor S[1]; - C.L := PUInt32Array(Source)[2] xor S[2]; - D.L := PUInt32Array(Source)[3] xor S[3]; - - Box := @S[40]; - S := @S[8]; - for I := 0 to 7 do - begin - X := Box[0, A.A] xor Box[1, A.B] xor Box[2, A.C] xor Box[3, A.D]; - Y := Box[1, B.A] xor Box[2, B.B] xor Box[3, B.C] xor Box[0, B.D]; - D.L := D.L shl 1 or D.L shr 31; - C.L := C.L xor (X + Y + S[0]); - D.L := D.L xor (X + Y shl 1 + S[1]); - C.L := C.L shr 1 or C.L shl 31; - - X := Box[0, C.A] xor Box[1, C.B] xor Box[2, C.C] xor Box[3, C.D]; - Y := Box[1, D.A] xor Box[2, D.B] xor Box[3, D.C] xor Box[0, D.D]; - B.L := B.L shl 1 or B.L shr 31; - A.L := A.L xor (X + Y + S[2]); - B.L := B.L xor (X + Y shl 1 + S[3]); - A.L := A.L shr 1 or A.L shl 31; - - S := @S[4]; - end; - S := FAdditionalBuffer; - PUInt32Array(Dest)[0] := C.L xor S[4]; - PUInt32Array(Dest)[1] := D.L xor S[5]; - PUInt32Array(Dest)[2] := A.L xor S[6]; - PUInt32Array(Dest)[3] := B.L xor S[7]; -end; - -procedure TCipher_Twofish.DoDecode(Source, Dest: Pointer; Size: Integer); -var - S: PUInt32Array; - Box: PTwofishBox; - I, X, Y: UInt32; - A, B, C, D: TLongRec; -begin - Assert(Size = Context.BlockSize); - - S := FAdditionalBuffer; - Box := @S[40]; - C.L := PUInt32Array(Source)[0] xor S[4]; - D.L := PUInt32Array(Source)[1] xor S[5]; - A.L := PUInt32Array(Source)[2] xor S[6]; - B.L := PUInt32Array(Source)[3] xor S[7]; - S := @S[36]; - for I := 0 to 7 do - begin - X := Box[0, C.A] xor Box[1, C.B] xor Box[2, C.C] xor Box[3, C.D]; - Y := Box[0, D.D] xor Box[1, D.A] xor Box[2, D.B] xor Box[3, D.C]; - A.L := A.L shl 1 or A.L shr 31; - B.L := B.L xor (X + Y shl 1 + S[3]); - A.L := A.L xor (X + Y + S[2]); - B.L := B.L shr 1 or B.L shl 31; - - X := Box[0, A.A] xor Box[1, A.B] xor Box[2, A.C] xor Box[3, A.D]; - Y := Box[0, B.D] xor Box[1, B.A] xor Box[2, B.B] xor Box[3, B.C]; - C.L := C.L shl 1 or C.L shr 31; - D.L := D.L xor (X + Y shl 1 + S[1]); - C.L := C.L xor (X + Y + S[0]); - D.L := D.L shr 1 or D.L shl 31; - - Dec(PUInt32(S), 4); - end; - S := FAdditionalBuffer; - PUInt32Array(Dest)[0] := A.L xor S[0]; - PUInt32Array(Dest)[1] := B.L xor S[1]; - PUInt32Array(Dest)[2] := C.L xor S[2]; - PUInt32Array(Dest)[3] := D.L xor S[3]; -end; - -{ TCipher_IDEA } - -class function TCipher_IDEA.Context: TCipherContext; -begin - Result.KeySize := 16; - Result.BufferSize := 8; - Result.BlockSize := 8; - Result.AdditionalBufferSize := 208; - Result.NeedsAdditionalBufferBackup := False; - Result.MinRounds := 1; - Result.MaxRounds := 1; - Result.CipherType := [ctSymmetric, ctBlock]; -end; - -procedure TCipher_IDEA.DoInit(const Key; Size: Integer); - - function IDEAInv(X: Word): Word; - var - A, B, C, D: Word; - begin - if X <= 1 then - begin - Result := X; - Exit; - end; - A := 1; - B := $10001 div X; - C := $10001 mod X; - while C <> 1 do - begin - D := X div C; - X := X mod C; - Inc(A, B * D); - if X = 1 then - begin - Result := A; - Exit; - end; - D := C div X; - C := C mod X; - Inc(B, A * D); - end; - Result := 1 - B; - end; - -var - I: Integer; - E: PWordArray; - A, B, C: Word; - K, D: PWordArray; -begin - E := FAdditionalBuffer; - Move(Key, E^, Size); - for I := 0 to 7 do - E[I] := Swap(E[I]); - for I := 0 to 39 do - E[I + 8] := E[I and not 7 + (I + 1) and 7] shl 9 or - E[I and not 7 + (I + 2) and 7] shr 7; - for I := 41 to 44 do - E[I + 7] := E[I] shl 9 or E[I + 1] shr 7; - K := E; - D := @E[100]; - A := IDEAInv(K[0]); - B := 0 - K[1]; - C := 0 - K[2]; - D[3] := IDEAInv(K[3]); - D[2] := C; - D[1] := B; - D[0] := A; - Inc(PWord(K), 4); - for I := 1 to 8 do - begin - Dec(PWord(D), 6); - A := K[0]; - D[5] := K[1]; - D[4] := A; - A := IDEAInv(K[2]); - B := 0 - K[3]; - C := 0 - K[4]; - D[3] := IDEAInv(K[5]); - D[2] := B; - D[1] := C; - D[0] := A; - Inc(PWord(K), 6); - end; - A := D[2]; - D[2] := D[1]; - D[1] := A; -end; - -function IDEAMul(X, Y: UInt32): UInt32; -{$IF defined(X86ASM) or defined(X64ASM)} -asm - {$IFDEF X64ASM} - MOV EAX,ECX - {$ENDIF X64ASM} - AND EAX,0FFFFh - JZ @@1 - AND EDX,0FFFFh - JZ @@1 - MUL EDX - MOV EDX,EAX - MOV ECX,EAX - SHR EDX,16 - SUB EAX,EDX - SUB CX,AX - ADC EAX,0 - RET -@@1: LEA EAX,[EAX + EDX - 1] - NEG EAX -end; -{$ELSE} -begin - X := X and $FFFF; - if X <> 0 then - begin - Y := Y and $FFFF; - if Y <> 0 then - begin - X := X * Y; - Result := X - (X shr 16); - if Word(X) < Word(Result) then // carry flag check for "sub cx,ax" - Inc(Result); - Exit; - end; - end; - Result := -(X + Y - 1); -end; -{$IFEND} - -procedure IDEACipher(Source, Dest: PUInt32Array; Key: PWordArray); -var - I: UInt32; - X, Y, A, B, C, D: UInt32; -begin - I := SwapUInt32(Source[0]); - A := I shr 16; - B := I and $FFFF; - I := SwapUInt32(Source[1]); - C := I shr 16; - D := I and $FFFF; - for I := 0 to 7 do - begin - A := IDEAMul(A, Key[0]); - Inc(B, Key[1]); - Inc(C, Key[2]); - D := IDEAMul(D, Key[3]); - Y := C xor A; - Y := IDEAMul(Y, Key[4]); - X := B xor D + Y; - X := IDEAMul(X, Key[5]); - Inc(Y, X); - A := A xor X; - D := D xor Y; - Y := B xor Y; - B := C xor X; - C := Y; - Key := @Key[6]; - end; - Dest[0] := SwapUInt32(IDEAMul(A, Key[0]) shl 16 or (C + Key[1]) and $FFFF); - Dest[1] := SwapUInt32((B + Key[2]) shl 16 or IDEAMul(D, Key[3]) and $FFFF); -end; - -procedure TCipher_IDEA.DoEncode(Source, Dest: Pointer; Size: Integer); -begin - Assert(Size = Context.BlockSize); - - IDEACipher(Source, Dest, FAdditionalBuffer); -end; - -procedure TCipher_IDEA.DoDecode(Source, Dest: Pointer; Size: Integer); -begin - Assert(Size = Context.BlockSize); - - IDEACipher(Source, Dest, @PUInt32Array(FAdditionalBuffer)[26]); -end; - -{ TCipher_Cast256 } - -class function TCipher_Cast256.Context: TCipherContext; -begin - Result.KeySize := 32; - Result.BlockSize := 16; - Result.BufferSize := 16; - Result.AdditionalBufferSize := 384; - Result.NeedsAdditionalBufferBackup := False; - Result.MinRounds := 1; - Result.MaxRounds := 1; - Result.CipherType := [ctSymmetric, ctBlock]; -end; - -procedure TCipher_Cast256.DoInit(const Key; Size: Integer); -var - X: array[0..7] of UInt32; - M, R, I, J, T: UInt32; - K: PUInt32Array; -begin - FillChar(X, SizeOf(X), 0); - Move(Key, X, Size); - SwapUInt32Buffer(X, X, 8); - K := FAdditionalBuffer; - M := $5A827999; - R := 19; - for I := 0 to 11 do - begin - for J := 0 to 1 do - begin - T := M + X[7]; - T := T shl R or T shr (32 - R); - X[6] := X[6] xor (Cast256_Data[0, T shr 24] xor - Cast256_Data[1, T shr 16 and $FF] - - Cast256_Data[2, T shr 8 and $FF] + - Cast256_Data[3, T and $FF]); - Inc(M, $6ED9EBA1); - Inc(R, 17); - T := M xor X[6]; - T := T shl R or T shr (32 - R); - X[5] := X[5] xor (Cast256_Data[0, T shr 24] - - Cast256_Data[1, T shr 16 and $FF] + - Cast256_Data[2, T shr 8 and $FF] xor - Cast256_Data[3, T and $FF]); - Inc(M, $6ED9EBA1); - Inc(R, 17); - T := M - X[5]; - T := T shl R or T shr (32 - R); - X[4] := X[4] xor (Cast256_Data[0, T shr 24] + - Cast256_Data[1, T shr 16 and $FF] xor - Cast256_Data[2, T shr 8 and $FF] - - Cast256_Data[3, T and $FF]); - Inc(M, $6ED9EBA1); - Inc(R, 17); - T := M + X[4]; - T := T shl R or T shr (32 - R); - X[3] := X[3] xor (Cast256_Data[0, T shr 24] xor - Cast256_Data[1, T shr 16 and $FF] - - Cast256_Data[2, T shr 8 and $FF] + - Cast256_Data[3, T and $FF]); - Inc(M, $6ED9EBA1); - Inc(R, 17); - T := M xor X[3]; - T := T shl R or T shr (32 - R); - X[2] := X[2] xor (Cast256_Data[0, T shr 24] - - Cast256_Data[1, T shr 16 and $FF] + - Cast256_Data[2, T shr 8 and $FF] xor - Cast256_Data[3, T and $FF]); - Inc(M, $6ED9EBA1); - Inc(R, 17); - T := M - X[2]; - T := T shl R or T shr (32 - R); - X[1] := X[1] xor (Cast256_Data[0, T shr 24] + - Cast256_Data[1, T shr 16 and $FF] xor - Cast256_Data[2, T shr 8 and $FF] - - Cast256_Data[3, T and $FF]); - Inc(M, $6ED9EBA1); - Inc(R, 17); - T := M + X[1]; - T := T shl R or T shr (32 - R); - X[0] := X[0] xor (Cast256_Data[0, T shr 24] xor - Cast256_Data[1, T shr 16 and $FF] - - Cast256_Data[2, T shr 8 and $FF] + - Cast256_Data[3, T and $FF]); - Inc(M, $6ED9EBA1); - Inc(R, 17); - T := M xor X[0]; - T := T shl R or T shr (32 - R); - X[7] := X[7] xor (Cast256_Data[0, T shr 24] - - Cast256_Data[1, T shr 16 and $FF] + - Cast256_Data[2, T shr 8 and $FF] xor - Cast256_Data[3, T and $FF]); - Inc(M, $6ED9EBA1); - Inc(R, 17); - end; - if I < 6 then - begin - K[48] := X[0] and $1F; - K[49] := X[2] and $1F; - K[50] := X[4] and $1F; - K[51] := X[6] and $1F; - K[0] := X[7]; - K[1] := X[5]; - K[2] := X[3]; - K[3] := X[1]; - end - else - begin - K[48] := X[6] and $1F; - K[49] := X[4] and $1F; - K[50] := X[2] and $1F; - K[51] := X[0] and $1F; - K[0] := X[1]; - K[1] := X[3]; - K[2] := X[5]; - K[3] := X[7]; - end; - K := @K[4]; - end; - ProtectBuffer(X, SizeOf(X)); -end; - -procedure TCipher_Cast256.DoEncode(Source, Dest: Pointer; Size: Integer); -var - I, T, A, B, C, D: UInt32; - K: PUInt32Array; -begin - Assert(Size = Context.BlockSize); - - K := FAdditionalBuffer; - SwapUInt32Buffer(Source^, Dest^, 4); - A := PUInt32Array(Dest)[0]; - B := PUInt32Array(Dest)[1]; - C := PUInt32Array(Dest)[2]; - D := PUInt32Array(Dest)[3]; - for I := 0 to 5 do - begin - T := K[0] + D; - T := T shl K[48] or T shr (32 - K[48]); - C := C xor (Cast256_Data[0, T shr 24] xor - Cast256_Data[1, T shr 16 and $FF] - - Cast256_Data[2, T shr 8 and $FF] + - Cast256_Data[3, T and $FF]); - T := K[1] xor C; - T := T shl K[49] or T shr (32 - K[49]); - B := B xor (Cast256_Data[0, T shr 24] - - Cast256_Data[1, T shr 16 and $FF] + - Cast256_Data[2, T shr 8 and $FF] xor - Cast256_Data[3, T and $FF]); - T := K[2] - B; - T := T shl K[50] or T shr (32 - K[50]); - A := A xor (Cast256_Data[0, T shr 24] + - Cast256_Data[1, T shr 16 and $FF] xor - Cast256_Data[2, T shr 8 and $FF] - - Cast256_Data[3, T and $FF]); - T := K[3] + A; - T := T shl K[51] or T shr (32 - K[51]); - D := D xor (Cast256_Data[0, T shr 24] xor - Cast256_Data[1, T shr 16 and $FF] - - Cast256_Data[2, T shr 8 and $FF] + - Cast256_Data[3, T and $FF]); - K := @K[4]; - end; - for I := 0 to 5 do - begin - T := K[0] + A; - T := T shl K[48] or T shr (32 - K[48]); - D := D xor (Cast256_Data[0, T shr 24] xor - Cast256_Data[1, T shr 16 and $FF] - - Cast256_Data[2, T shr 8 and $FF] + - Cast256_Data[3, T and $FF]); - T := K[1] - B; - T := T shl K[49] or T shr (32 - K[49]); - A := A xor (Cast256_Data[0, T shr 24] + - Cast256_Data[1, T shr 16 and $FF] xor - Cast256_Data[2, T shr 8 and $FF] - - Cast256_Data[3, T and $FF]); - T := K[2] xor C; - T := T shl K[50] or T shr (32 - K[50]); - B := B xor (Cast256_Data[0, T shr 24] - - Cast256_Data[1, T shr 16 and $FF] + - Cast256_Data[2, T shr 8 and $FF] xor - Cast256_Data[3, T and $FF]); - T := K[3] + D; - T := T shl K[51] or T shr (32 - K[51]); - C := C xor (Cast256_Data[0, T shr 24] xor - Cast256_Data[1, T shr 16 and $FF] - - Cast256_Data[2, T shr 8 and $FF] + - Cast256_Data[3, T and $FF]); - K := @K[4]; - end; - PUInt32Array(Dest)[0] := A; - PUInt32Array(Dest)[1] := B; - PUInt32Array(Dest)[2] := C; - PUInt32Array(Dest)[3] := D; - SwapUInt32Buffer(Dest^, Dest^, 4); -end; - -procedure TCipher_Cast256.DoDecode(Source, Dest: Pointer; Size: Integer); -var - I, T, A, B, C, D: UInt32; - K: PUInt32Array; -begin - Assert(Size = Context.BlockSize); - - K := @PUInt32Array(FAdditionalBuffer)[44]; - SwapUInt32Buffer(Source^, Dest^, 4); - A := PUInt32Array(Dest)[0]; - B := PUInt32Array(Dest)[1]; - C := PUInt32Array(Dest)[2]; - D := PUInt32Array(Dest)[3]; - for I := 0 to 5 do - begin - T := K[3] + D; - T := T shl K[51] or T shr (32 - K[51]); - C := C xor (Cast256_Data[0, T shr 24] xor - Cast256_Data[1, T shr 16 and $FF] - - Cast256_Data[2, T shr 8 and $FF] + - Cast256_Data[3, T and $FF]); - T := K[2] xor C; - T := T shl K[50] or T shr (32 - K[50]); - B := B xor (Cast256_Data[0, T shr 24] - - Cast256_Data[1, T shr 16 and $FF] + - Cast256_Data[2, T shr 8 and $FF] xor - Cast256_Data[3, T and $FF]); - T := K[1] - B; - T := T shl K[49] or T shr (32 - K[49]); - A := A xor (Cast256_Data[0, T shr 24] + - Cast256_Data[1, T shr 16 and $FF] xor - Cast256_Data[2, T shr 8 and $FF] - - Cast256_Data[3, T and $FF]); - T := K[0] + A; - T := T shl K[48] or T shr (32 - K[48]); - D := D xor (Cast256_Data[0, T shr 24] xor - Cast256_Data[1, T shr 16 and $FF] - - Cast256_Data[2, T shr 8 and $FF] + - Cast256_Data[3, T and $FF]); - Dec(PUInt32(K), 4); - end; - for I := 0 to 5 do - begin - T := K[3] + A; - T := T shl K[51] or T shr (32 - K[51]); - D := D xor (Cast256_Data[0, T shr 24] xor - Cast256_Data[1, T shr 16 and $FF] - - Cast256_Data[2, T shr 8 and $FF] + - Cast256_Data[3, T and $FF]); - T := K[2] - B; - T := T shl K[50] or T shr (32 - K[50]); - A := A xor (Cast256_Data[0, T shr 24] + - Cast256_Data[1, T shr 16 and $FF] xor - Cast256_Data[2, T shr 8 and $FF] - - Cast256_Data[3, T and $FF]); - T := K[1] xor C; - T := T shl K[49] or T shr (32 - K[49]); - B := B xor (Cast256_Data[0, T shr 24] - - Cast256_Data[1, T shr 16 and $FF] + - Cast256_Data[2, T shr 8 and $FF] xor - Cast256_Data[3, T and $FF]); - T := K[0] + D; - T := T shl K[48] or T shr (32 - K[48]); - C := C xor (Cast256_Data[0, T shr 24] xor - Cast256_Data[1, T shr 16 and $FF] - - Cast256_Data[2, T shr 8 and $FF] + - Cast256_Data[3, T and $FF]); - Dec(PUInt32(K), 4); - end; - PUInt32Array(Dest)[0] := A; - PUInt32Array(Dest)[1] := B; - PUInt32Array(Dest)[2] := C; - PUInt32Array(Dest)[3] := D; - SwapUInt32Buffer(Dest^, Dest^, 4); -end; - -{ TCipher_Mars } - -class function TCipher_Mars.Context: TCipherContext; -begin - Result.KeySize := 56; - Result.BlockSize := 16; - Result.BufferSize := 16; - Result.AdditionalBufferSize := 160; - Result.NeedsAdditionalBufferBackup := False; - Result.MinRounds := 1; - Result.MaxRounds := 1; - Result.CipherType := [ctSymmetric, ctBlock]; -end; - -procedure TCipher_Mars.DoInit(const Key; Size: Integer); -var - B: PUInt32Array; - - function FixKey(K, R: UInt32): UInt32; - var - M1, M2: UInt32; - I: UInt32; - begin - I := K and 3; - K := K or 3; - M1 := not K xor (K shl 1); - M2 := M1 and (M1 shl 1); - M2 := M2 and (M2 shl 2); - M2 := M2 and (M2 shl 4); - M2 := M2 and (M1 shl 8); - M2 := M2 and $FFFFFE00; - if M2 = 0 then - begin - Result := K; - Exit; - end; - M1 := M2 or (M2 shr 1); - M1 := M1 or (M1 shr 2); - M1 := M1 or (M2 shr 4); - M1 := M1 or (M1 shr 5); - M1 := M1 and ((not K xor (K shl 1)) and (not K xor (K shr 1)) and $7FFFFFFC); - Result := K xor ((B[265 + I] shl R or B[265 + I] shr (32 - R)) and M1); - end; - -var - T: array[0..14] of UInt32; - I, J, L: UInt32; - U: UInt32; - K: PUInt32Array; -begin - K := FAdditionalBuffer; - B := @Mars_Data; - FillChar(T, SizeOf(T), 0); - Move(Key, T, Size); - Size := Size div 4; - T[Size] := Size; - for J := 0 to 3 do - begin - for I := 0 to 14 do - begin - U := T[(I + 8) mod 15] xor T[(I + 13) mod 15]; - T[I] := T[I] xor (U shl 3 or U shr 29) xor (I * 4 + J); - end; - for L := 0 to 3 do - begin - for I := 0 to 14 do - begin - Inc(T[I], B[T[(I + 14) mod 15] and $1FF]); - T[I] := T[I] shl 9 or T[I] shr 23; - end; - end; - for I := 0 to 9 do - K[(J * 10) + I] := T[(I * 4) mod 15]; - end; - I := 5; - repeat - K[I] := FixKey(K[I], K[I - 1]); - Inc(I, 2); - until I >= 37; -end; - -procedure TCipher_Mars.DoEncode(Source, Dest: Pointer; Size: Integer); -var - K: PUInt32Array; - I, L, R, A, B, C, D: UInt32; -begin - Assert(Size = Context.BlockSize); - - K := FAdditionalBuffer; - A := PUInt32Array(Source)[0] + K[0]; - B := PUInt32Array(Source)[1] + K[1]; - C := PUInt32Array(Source)[2] + K[2]; - D := PUInt32Array(Source)[3] + K[3]; - K := @K[4]; - for I := 0 to 1 do - begin - B := B xor Mars_Data[A and $FF] + Mars_Data[A shr 8 and $FF + 256]; - Inc(C, Mars_Data[A shr 16 and $FF]); - D := D xor Mars_Data[A shr 24 + 256]; - A := (A shr 24 or A shl 8) + D; - - C := C xor Mars_Data[B and $FF] + Mars_Data[B shr 8 and $FF + 256]; - Inc(D, Mars_Data[B shr 16 and $FF]); - A := A xor Mars_Data[B shr 24 + 256]; - B := (B shr 24 or B shl 8) + C; - - D := D xor Mars_Data[C and $FF] + Mars_Data[C shr 8 and $FF + 256]; - Inc(A, Mars_Data[C shr 16 and $FF]); - B := B xor Mars_Data[C shr 24 + 256]; - C := C shr 24 or C shl 8; - - A := A xor Mars_Data[D and $FF] + Mars_Data[D shr 8 and $FF + 256]; - Inc(B, Mars_Data[D shr 16 and $FF]); - C := C xor Mars_Data[D shr 24 + 256]; - D := D shr 24 or D shl 8; - end; - - for I := 0 to 3 do - begin - L := A + K[0]; - A := A shl 13 or A shr 19; - R := A * K[1]; - R := R shl 5 or R shr 27; - Inc(C, L shl R or L shr (32 - R)); - L := Mars_Data[L and $1FF] xor R; - R := R shl 5 or R shr 27; - L := L xor R; - L := L shl R or L shr (32 - R); - - if I <= 1 then - begin - Inc(B, L); - D := D xor R; - end - else - begin - Inc(D, L); - B := B xor R; - end; - L := B + K[2]; - B := B shl 13 or B shr 19; - R := B * K[3]; - R := R shl 5 or R shr 27; - Inc(D, L shl R or L shr (32 - R)); - L := Mars_Data[L and $1FF] xor R; - R := R shl 5 or R shr 27; - L := L xor R; - L := L shl R or L shr (32 - R); - if I <= 1 then - begin - Inc(C, L); - A := A xor R; - end - else - begin - Inc(A, L); - C := C xor R; - end; - L := C + K[4]; - C := C shl 13 or C shr 19; - R := C * K[5]; - R := R shl 5 or R shr 27; - Inc(A, L shl R or L shr (32 - R)); - L := Mars_Data[L and $1FF] xor R; - R := R shl 5 or R shr 27; - L := L xor R; - L := L shl R or L shr (32 - R); - if I <= 1 then - begin - Inc(D, L); - B := B xor R; - end - else - begin - Inc(B, L); - D := D xor R; - end; - L := D + K[6]; - D := D shl 13 or D shr 19; - R := D * K[7]; - R := R shl 5 or R shr 27; - Inc(B, L shl R or L shr (32 - R)); - L := Mars_Data[L and $1FF] xor R; - R := R shl 5 or R shr 27; - L := L xor R; - L := L shl R or L shr (32 - R); - if I <= 1 then - begin - Inc(A, L); - C := C xor R; - end - else - begin - Inc(C, L); - A := A xor R; - end; - K := @K[8]; - end; - for I := 0 to 1 do - begin - B := B xor Mars_Data[A and $FF + 256]; - Dec(C, Mars_Data[A shr 24]); - D := D - Mars_Data[A shr 16 and $FF + 256] xor Mars_Data[A shr 8 and $FF]; - A := A shl 24 or A shr 8; - C := C xor Mars_Data[B and $FF + 256]; - Dec(D, Mars_Data[B shr 24]); - A := A - Mars_Data[B shr 16 and $FF + 256] xor Mars_Data[B shr 8 and $FF]; - B := B shl 24 or B shr 8; - Dec(C, B); - D := D xor Mars_Data[C and $FF + 256]; - Dec(A, Mars_Data[C shr 24]); - B := B - Mars_Data[C shr 16 and $FF + 256] xor Mars_Data[C shr 8 and $FF]; - C := C shl 24 or C shr 8; - Dec(D, A); - A := A xor Mars_Data[D and $FF + 256]; - Dec(B, Mars_Data[D shr 24]); - C := C - Mars_Data[D shr 16 and $FF + 256] xor Mars_Data[D shr 8 and $FF]; - D := D shl 24 or D shr 8; - end; - PUInt32Array(Dest)[0] := A - K[0]; - PUInt32Array(Dest)[1] := B - K[1]; - PUInt32Array(Dest)[2] := C - K[2]; - PUInt32Array(Dest)[3] := D - K[3]; -end; - -procedure TCipher_Mars.DoDecode(Source, Dest: Pointer; Size: Integer); -var - K: PUInt32Array; - I, L, R, A, B, C, D: UInt32; -begin - Assert(Size = Context.BlockSize); - - K := @PUInt32Array(FAdditionalBuffer)[28]; - A := PUInt32Array(Source)[0] + K[8]; - B := PUInt32Array(Source)[1] + K[9]; - C := PUInt32Array(Source)[2] + K[10]; - D := PUInt32Array(Source)[3] + K[11]; - for I := 0 to 1 do - begin - D := D shr 24 or D shl 8; - C := C xor Mars_Data[D shr 8 and $FF] + Mars_Data[D shr 16 and $FF + 256]; - Inc(B, Mars_Data[D shr 24]); - A := A xor Mars_Data[D and $FF + 256]; - Inc(D, A); - C := C shr 24 or C shl 8; - B := B xor Mars_Data[C shr 8 and $FF] + Mars_Data[C shr 16 and $FF + 256]; - Inc(A, Mars_Data[C shr 24]); - D := D xor Mars_Data[C and $FF + 256]; - Inc(C, B); - B := B shr 24 or B shl 8; - A := A xor Mars_Data[B shr 8 and $FF] + Mars_Data[B shr 16 and $FF + 256]; - Inc(D, Mars_Data[B shr 24]); - C := C xor Mars_Data[B and $FF + 256]; - A := A shr 24 or A shl 8; - D := D xor Mars_Data[A shr 8 and $FF] + Mars_Data[A shr 16 and $FF + 256]; - Inc(C, Mars_Data[A shr 24]); - B := B xor Mars_Data[A and $FF + 256]; - end; - for I := 0 to 3 do - begin - R := D * K[7]; - R := R shl 5 or R shr 27; - D := D shr 13 or D shl 19; - L := D + K[6]; - Dec(B, L shl R or L shr (32 - R)); - L := Mars_Data[L and $1FF] xor R; - R := R shl 5 or R shr 27; - L := L xor R; - L := L shl R or L shr (32 - R); - if I <= 1 then - begin - Dec(C, L); - A := A xor R; - end - else - begin - Dec(A, L); - C := C xor R; - end; - R := C * K[5]; - R := R shl 5 or R shr 27; - C := C shr 13 or C shl 19; - L := C + K[4]; - Dec(A, L shl R or L shr (32 - R)); - L := Mars_Data[L and $1FF] xor R; - R := R shl 5 or R shr 27; - L := L xor R; - L := L shl R or L shr (32 - R); - if I <= 1 then - begin - Dec(B, L); - D := D xor R; - end - else - begin - Dec(D, L); - B := B xor R; - end; - R := B * K[3]; - R := R shl 5 or R shr 27; - B := B shr 13 or B shl 19; - L := B + K[2]; - Dec(D, L shl R or L shr (32 - R)); - L := Mars_Data[L and $1FF] xor R; - R := R shl 5 or R shr 27; - L := L xor R; - L := L shl R or L shr (32 - R); - if I <= 1 then - begin - Dec(A, L); - C := C xor R; - end - else - begin - Dec(C, L); - A := A xor R; - end; - R := A * K[1]; - R := R shl 5 or R shr 27; - A := A shr 13 or A shl 19; - L := A + K[0]; - Dec(C, L shl R or L shr (32 - R)); - L := Mars_Data[L and $1FF] xor R; - R := R shl 5 or R shr 27; - L := L xor R; - L := L shl R or L shr (32 - R); - if I <= 1 then - begin - Dec(D, L); - B := B xor R; - end - else - begin - Dec(B, L); - D := D xor R; - end; - Dec(PUInt32(K), 8); - end; - for I := 0 to 1 do - begin - D := D shl 24 or D shr 8; - C := C xor Mars_Data[D shr 24 + 256]; - Dec(B, Mars_Data[D shr 16 and $FF]); - A := A - Mars_Data[D shr 8 and $FF + 256] xor Mars_Data[D and $FF]; - C := C shl 24 or C shr 8; - B := B xor Mars_Data[C shr 24 + 256]; - Dec(A, Mars_Data[C shr 16 and $FF]); - D := D - Mars_Data[C shr 8 and $FF + 256] xor Mars_Data[C and $FF]; - Dec(B, C); - B := B shl 24 or B shr 8; - A := A xor Mars_Data[B shr 24 + 256]; - Dec(D, Mars_Data[B shr 16 and $FF]); - C := C - Mars_Data[B shr 8 and $FF + 256] xor Mars_Data[B and $FF]; - Dec(A, D); - A := A shl 24 or A shr 8; - D := D xor Mars_Data[A shr 24 + 256]; - Dec(C, Mars_Data[A shr 16 and $FF]); - B := B - Mars_Data[A shr 8 and $FF + 256] xor Mars_Data[A and $FF]; - end; - PUInt32Array(Dest)[0] := A - K[4]; - PUInt32Array(Dest)[1] := B - K[5]; - PUInt32Array(Dest)[2] := C - K[6]; - PUInt32Array(Dest)[3] := D - K[7]; -end; - -{ TCipher_RC4 } - -class function TCipher_RC4.Context: TCipherContext; -begin - Result.KeySize := 256; - Result.BlockSize := 1; - Result.BufferSize := 16; - Result.AdditionalBufferSize := 256 + 2; - Result.NeedsAdditionalBufferBackup := true; - Result.MinRounds := 1; - Result.MaxRounds := 1; - Result.CipherType := [ctSymmetric, ctStream]; -end; - -procedure TCipher_RC4.DoInit(const Key; Size: Integer); -var - K: array[0..255] of Byte; - D: PByteArray; - I, J, T: Byte; -begin - D := FAdditionalBuffer; - for I := 0 to 255 do - begin - D[I] := I; - if Size > 0 then - K[I] := TByteArray(Key)[I mod Size]; - end; - J := 0; - for I := 0 to 255 do - begin - J := J + D[I] + K[I]; - T := D[I]; - D[I] := D[J]; - D[J] := T; - end; - D[256] := 0; - D[257] := 0; - ProtectBuffer(K, SizeOf(K)); -end; - -procedure TCipher_RC4.DoEncode(Source, Dest: Pointer; Size: Integer); -var - D: PByteArray; - S: Integer; - T, I, J: Byte; -begin - D := FAdditionalBuffer; - I := D[256]; - J := D[257]; - for S := 0 to Size - 1 do - begin - Inc(I); - T := D[I]; - Inc(J, T); - D[I] := D[J]; - D[J] := T; - PByteArray(Dest)[S] := PByteArray(Source)[S] xor D[Byte(D[I] + T)]; - end; - D[256] := I; - D[257] := J; -end; - -procedure TCipher_RC4.DoDecode(Source, Dest: Pointer; Size: Integer); -begin - DoEncode(Source, Dest, Size); -end; - -{ TCipher_RC6 } - -class function TCipher_RC6.Context: TCipherContext; -begin - Result.KeySize := 256; - Result.BlockSize := 16; - Result.BufferSize := 16; - Result.AdditionalBufferSize := 272; - Result.NeedsAdditionalBufferBackup := False; - Result.MinRounds := 16; - Result.MaxRounds := 24; - Result.CipherType := [ctSymmetric, ctBlock]; -end; - -procedure TCipher_RC6.SetRounds(Value: Integer); -begin - if Value < Context.MinRounds then - Value := Context.MinRounds - else - if Value > Context.MaxRounds then - Value := Context.MaxRounds; - if Value <> FRounds then - begin - if not (FState in [csNew, csInitialized, csDone]) then - Done; - FRounds := Value; - end; -end; - -procedure TCipher_RC6.DoInit(const Key; Size: Integer); -var - K: array[0..63] of UInt32; - D: PUInt32Array; - I, J, L, A, B, Z, T: UInt32; -begin - LimitRounds; - - D := FAdditionalBuffer; - FillChar(K, SizeOf(K), 0); - Move(Key, K, Size); - L := Size shr 2; - if Size and 3 <> 0 then - Inc(L); - if L <= 0 then - L := 1; - J := $B7E15163; - for I := 0 to (FRounds + 2) * 2 do - begin - D[I] := J; - Inc(J, $9E3779B9); - end; - if L > UInt32(FRounds + 2) * 2 then - Z := L * 3 - else - Z := (FRounds + 2) * 6; - I := 0; - J := 0; - A := 0; - B := 0; - for Z := Z downto 1 do - begin - A := A + B + D[I]; - A := A shl 3 or A shr 29; - D[I] := A; - T := A + B; - B := T + K[J]; - B := B shl T or B shr (32 - T); - K[J] := B; - I := (I + 1) mod (UInt32(FRounds + 2) * 2); - J := (J + 1) mod L; - end; - ProtectBuffer(K, SizeOf(K)); -end; - -procedure TCipher_RC6.LimitRounds; -begin - if FRounds = 0 then - FRounds := 20 - else - if FRounds < 16 then - FRounds := 16 - else - if FRounds > 24 then - FRounds := 24; -end; - -procedure TCipher_RC6.DoEncode(Source, Dest: Pointer; Size: Integer); -{$IFDEF X86ASM} -asm - PUSH EBX - PUSH ESI - PUSH EDI - PUSH EBP - PUSH ECX - MOV EBP,[EAX].TCipher_RC6.FRounds // Rounds - MOV ESI,[EAX].TCipher_RC6.FAdditionalBuffer // Key - MOV EAX,[EDX + 0] // A - MOV EBX,[EDX + 4] // B - MOV EDI,[EDX + 8] // C - MOV EDX,[EDX + 12] // D - ADD EBX,[ESI + 0] // Inc(B, K[0]) - ADD EDX,[ESI + 4] // Inc(D, K[1]) - ADD ESI,8 // Inc(PInteger(K), 2) -@@1: LEA ECX,[EBX * 2 + 1] // ECX := B * 2 + 1 - IMUL ECX,EBX // ECX := ECX * B - ROL ECX,5 // T := ROL(B * (B * 2 + 1), 5) - PUSH ECX // save T - XOR EAX,ECX // A := A xor T - LEA ECX,[EDX * 2 + 1] // ECX := D * 2 + 1 - IMUL ECX,EDX // ECX := ECX * D - ROL ECX,5 // U := ROL(D * (D * 2 + 1), 5) - XOR EDI,ECX // C := C xor U - ROL EAX,CL // A := ROL(A xor T, U) - POP ECX // restore T - ADD EAX,[ESI + 0] // Inc(A, K[0]) - ROL EDI,CL // C := ROL(C xor U, T) - MOV ECX,EAX // T := A - ADD EDI,[ESI + 4] // Inc(C, K[1]) - MOV EAX,EBX // A := B - MOV EBX,EDI // B := C - MOV EDI,EDX // C := D - DEC EBP - MOV EDX,ECX // D := T; - LEA ESI,[ESI + 8] // Inc(PInteger(K), 2) - JNZ @@1 - ADD EAX,[ESI + 0] // Inc(A, K[0]) - ADD EDI,[ESI + 4] // Inc(C, K[1]) - POP ECX - MOV [ECX + 0],EAX // A - MOV [ECX + 4],EBX // B - MOV [ECX + 8],EDI // C - MOV [ECX + 12],EDX // D - POP EBP - POP EDI - POP ESI - POP EBX -end; -{$ELSE !X86ASM} -var - K: PUInt32Array; - I, T, U, A, B, C, D: UInt32; -begin - Assert(Size = Context.BlockSize); - - K := Pointer(FAdditionalBuffer); - A := PUInt32Array(Source)[0]; - B := PUInt32Array(Source)[1] + K[0]; - C := PUInt32Array(Source)[2]; - D := PUInt32Array(Source)[3] + K[1]; - for I := 1 to FRounds do - begin - K := @K[2]; - T := B * (B + B + 1); - T := T shl 5 or T shr 27; - U := D * (D + D + 1); - U := U shl 5 or U shr 27; - A := A xor T; - A := A shl U or A shr (32 - U) + K[0]; - C := C xor U; - C := C shl T or C shr (32 - T) + K[1]; - T := A; A := B; B := C; C := D; D := T; - end; - PUInt32Array(Dest)[0] := A + K[2]; - PUInt32Array(Dest)[1] := B; - PUInt32Array(Dest)[2] := C + K[3]; - PUInt32Array(Dest)[3] := D; -end; -{$ENDIF !X86ASM} - -procedure TCipher_RC6.DoDecode(Source, Dest: Pointer; Size: Integer); -{$IFDEF X86ASM} -asm - PUSH EBX - PUSH ESI - PUSH EDI - PUSH EBP - PUSH ECX - MOV EBP,[EAX].TCipher_RC6.FRounds // Rounds - MOV ESI,[EAX].TCipher_RC6.FAdditionalBuffer // Key - LEA ESI,[ESI + EBP * 8] // Key[FRounds * 2] - MOV EAX,[EDX + 0] // A - MOV EBX,[EDX + 4] // B - MOV EDI,[EDX + 8] // C - MOV EDX,[EDX + 12] // D - SUB EDI,[ESI + 12] // Dec(C, K[3]) - SUB EAX,[ESI + 8] // Dec(A, K[2]) -@@1: MOV ECX,EAX // T := A - SUB EDX,[ESI + 0] // Dec(A, K[0]) - MOV EAX,EDX // A := D - MOV EDX,EDI // D := C - SUB EBX,[ESI + 4] // Dec(C, K[1]) - MOV EDI,EBX // C := B - MOV EBX,ECX // B := T; - LEA ECX,[EDX * 2 + 1] // ECX := D * 2 + 1 - IMUL ECX,EDX // ECX := ECX * D - ROL ECX,5 // U := ROL(D * (D * 2 + 1), 5) - PUSH ECX // save U - ROR EAX,CL // A := ROR(A - K[0], U) - LEA ECX,[EBX * 2 + 1] // ECX := B * 2 + 1 - IMUL ECX,EBX // ECX := ECX * B - ROL ECX,5 // T := ROL(B * (B * 2 + 1), 5) - XOR EAX,ECX // A := A xor T - ROR EDI,CL // C := ROR(C - K[1], T) - POP ECX // restore U - XOR EDI,ECX // C := C xor U - DEC EBP - LEA ESI,[ESI - 8] // Dec(PInteger(K), 2) - JNZ @@1 - SUB EBX,[ESI + 0] // Dec(B, K[0]) - SUB EDX,[ESI + 4] // Inc(D, K[1]) - POP ECX - MOV [ECX + 0],EAX // A - MOV [ECX + 4],EBX // B - MOV [ECX + 8],EDI // C - MOV [ECX + 12],EDX // D - POP EBP - POP EDI - POP ESI - POP EBX -end; -{$ELSE !X86ASM} -var - I, U, T, A, B, C, D: UInt32; - K: PUInt32Array; -begin - Assert(Size = Context.BlockSize); - - K := @PUInt32Array(FAdditionalBuffer)[FRounds * 2]; - A := PUInt32Array(Source)[0] - K[2]; - B := PUInt32Array(Source)[1]; - C := PUInt32Array(Source)[2] - K[3]; - D := PUInt32Array(Source)[3]; - - for I := 1 to FRounds do - begin - T := A; A := D; D := C; C := B; B := T; - U := D * (D + D + 1); - U := U shl 5 or U shr 27; - T := B * (B + B + 1); - T := T shl 5 or T shr 27; - C := C - K[1]; - C := C shr T or C shl (32 - T) xor U; - A := A - K[0]; - A := A shr U or A shl (32 - U) xor T; - Dec(PUInt32(K), 2); - end; - - PUInt32Array(Dest)[0] := A; - PUInt32Array(Dest)[1] := B - K[0]; - PUInt32Array(Dest)[2] := C; - PUInt32Array(Dest)[3] := D - K[1]; -end; -{$ENDIF !X86ASM} - -{ TCipher_Rijndael } - -class function TCipher_Rijndael.Context: TCipherContext; -const - // don't change this! - Rijndael_Blocks = 4; - Rijndael_Rounds = 14; -begin - Result.KeySize := 32; - Result.BlockSize := Rijndael_Blocks * 4; - Result.BufferSize := Rijndael_Blocks * 4; - Result.AdditionalBufferSize := (Rijndael_Rounds + 1) * Rijndael_Blocks * SizeOf(UInt32) * 2; - Result.NeedsAdditionalBufferBackup := False; - Result.MinRounds := 1; - Result.MaxRounds := 1; - Result.CipherType := [ctSymmetric, ctBlock]; -end; - -procedure TCipher_Rijndael.DoInit(const Key; Size: Integer); -{$REGION OldKeyShedule} -{ - // Old Rijndael Key Scheduling: - - procedure BuildEncodeKey; - const - RND_Data: array[0..29] of Byte = ( - $01, $02, $04, $08, $10, $20, $40, $80, $1B, $36, $6C, $D8, $AB, $4D, $9A, - $2F, $5E, $BC, $63, $C6, $97, $35, $6A, $D4, $B3, $7D, $FA, $EF, $C5, $91 - ); - var - T, R: Integer; - - procedure NextRounds; - var - J: Integer; - begin - J := 0; - while (J < FRounds - 6) and (R <= FRounds) do - begin - while (J < FRounds - 6) and (T < Rijndael_Blocks) do - begin - PUInt32Array(FBuffer)[R * Rijndael_Blocks + T] := K[J]; - Inc(J); - Inc(T); - end; - if T = Rijndael_Blocks then - begin - T := 0; - Inc(R); - end; - end; - end; - - var - RND: PByte; - B: PByte; - I: Integer; - begin - R := 0; - T := 0; - RND := @RND_Data; - NextRounds; - while R <= FRounds do - begin - B := @K; - B^ := B^ xor Rijndael_S[0, K[FRounds - 7] shr 8 and $FF] xor RND^; Inc(B); - B^ := B^ xor Rijndael_S[0, K[FRounds - 7] shr 16 and $FF]; Inc(B); - B^ := B^ xor Rijndael_S[0, K[FRounds - 7] shr 24]; Inc(B); - B^ := B^ xor Rijndael_S[0, K[FRounds - 7] and $FF]; - Inc(RND); - if FRounds = 14 then - begin - for I := 1 to 7 do - K[I] := K[I] xor K[I - 1]; - B := @K[4]; - B^ := B^ xor Rijndael_S[0, K[3] and $FF]; Inc(B); - B^ := B^ xor Rijndael_S[0, K[3] shr 8 and $FF]; Inc(B); - B^ := B^ xor Rijndael_S[0, K[3] shr 16 and $FF]; Inc(B); - B^ := B^ xor Rijndael_S[0, K[3] shr 24]; - for I := 5 to 7 do - K[I] := K[I] xor K[I - 1]; - end - else - for I := 1 to FRounds - 7 do - K[I] := K[I] xor K[I - 1]; - NextRounds; - end; - end; - - procedure BuildDecodeKey; - var - I: Integer; - D: PUInt32; - begin - D := Pointer(PAnsiChar(FBuffer) + FBufferSize shr 1); // for Pointer Math - Move(FBuffer^, D^, FBufferSize shr 1); - Inc(D, 4); - for I := 0 to FRounds * 4 - 5 do - begin - D^ := Rijndael_Key[D^ and $FF] xor - (Rijndael_Key[D^ shr 8 and $FF] shl 8 or Rijndael_Key[D^ shr 8 and $FF] shr 24) xor - (Rijndael_Key[D^ shr 16 and $FF] shl 16 or Rijndael_Key[D^ shr 16 and $FF] shr 16) xor - (Rijndael_Key[D^ shr 24] shl 24 or Rijndael_Key[D^ shr 24] shr 8); - Inc(D); - end; - end; } -{$ENDREGION} - - // New AES conform Key Scheduling - - procedure BuildEncodeKey; - const - RCon: array[0..9] of UInt32 = ($01, $02, $04, $08, $10, $20, $40, $80, $1b, $36); - var - I: Integer; - T: UInt32; - P: PUInt32Array; - begin - P := FAdditionalBuffer; - if Size <= 16 then - begin - for I := 0 to 9 do - begin - T := P[3]; - P[4] := Rijndael_S[0, T shr 8 and $FF] xor - Rijndael_S[0, T shr 16 and $FF] shl 8 xor - Rijndael_S[0, T shr 24 ] shl 16 xor - Rijndael_S[0, T and $FF] shl 24 xor P[0] xor RCon[I]; - P[5] := P[1] xor P[4]; - P[6] := P[2] xor P[5]; - P[7] := P[3] xor P[6]; - P := @P[4]; - end; - end - else - if Size <= 24 then - begin - for I := 0 to 7 do - begin - T := P[5]; - P[6] := Rijndael_S[0, T shr 8 and $FF] xor - Rijndael_S[0, T shr 16 and $FF] shl 8 xor - Rijndael_S[0, T shr 24 ] shl 16 xor - Rijndael_S[0, T and $FF] shl 24 xor P[0] xor RCon[I]; - P[7] := P[1] xor P[6]; - P[8] := P[2] xor P[7]; - P[9] := P[3] xor P[8]; - if I = 7 then - Break; - P[10] := P[4] xor P[9]; - P[11] := P[5] xor P[10]; - P := @P[6]; - end; - end - else - begin - for I :=0 to 6 do - begin - T := P[7]; - P[8] := Rijndael_S[0, T shr 8 and $FF] xor - Rijndael_S[0, T shr 16 and $FF] shl 8 xor - Rijndael_S[0, T shr 24 ] shl 16 xor - Rijndael_S[0, T and $FF] shl 24 xor P[0] xor RCon[I]; - P[9] := P[1] xor P[8]; - P[10] := P[2] xor P[9]; - P[11] := P[3] xor P[10]; - if I = 6 then - Break; - T := P[11]; - P[12] := Rijndael_S[0, T and $FF] xor - Rijndael_S[0, T shr 8 and $FF] shl 8 xor - Rijndael_S[0, T shr 16 and $FF] shl 16 xor - Rijndael_S[0, T shr 24 ] shl 24 xor P[4]; - P[13] := P[5] xor P[12]; - P[14] := P[6] xor P[13]; - P[15] := P[7] xor P[14]; - P := @P[8]; - end; - end; - end; - - procedure BuildDecodeKey; - var - P: PUInt32; - I: Integer; - begin - P := Pointer(PByte(FAdditionalBuffer) + FAdditionalBufferSize shr 1); // for Pointer Math - Move(FAdditionalBuffer^, P^, FAdditionalBufferSize shr 1); - Inc(P, 4); - for I := 0 to FRounds * 4 - 5 do - begin - P^ := Rijndael_T[4, Rijndael_S[0, P^ and $FF]] xor - Rijndael_T[5, Rijndael_S[0, P^ shr 8 and $FF]] xor - Rijndael_T[6, Rijndael_S[0, P^ shr 16 and $FF]] xor - Rijndael_T[7, Rijndael_S[0, P^ shr 24 ]]; - Inc(P); - end; - end; - -begin - if Size <= 16 then - FRounds := 10 - else - if Size <= 24 then - FRounds := 12 - else - FRounds := 14; - FillChar(FAdditionalBuffer^, 32, 0); - Move(Key, FAdditionalBuffer^, Size); - BuildEncodeKey; - BuildDecodeKey; -end; - -procedure TCipher_Rijndael.DoEncode(Source, Dest: Pointer; Size: Integer); -var - P: PUInt32Array; - I: Integer; - A2, B2, C2, D2: UInt32; - A1, B1, C1, D1: UInt32; -begin - Assert(Size = Context.BlockSize); - - P := FAdditionalBuffer; - A1 := PUInt32Array(Source)[0]; - B1 := PUInt32Array(Source)[1]; - C1 := PUInt32Array(Source)[2]; - D1 := PUInt32Array(Source)[3]; - - for I := 2 to FRounds do - begin - A2 := A1 xor P[0]; - B2 := B1 xor P[1]; - C2 := C1 xor P[2]; - D2 := D1 xor P[3]; - - A1 := Rijndael_T[0, A2 and $FF] xor - Rijndael_T[1, B2 shr 8 and $FF] xor - Rijndael_T[2, C2 shr 16 and $FF] xor - Rijndael_T[3, D2 shr 24 ]; - B1 := Rijndael_T[0, B2 and $FF] xor - Rijndael_T[1, C2 shr 8 and $FF] xor - Rijndael_T[2, D2 shr 16 and $FF] xor - Rijndael_T[3, A2 shr 24 ]; - C1 := Rijndael_T[0, C2 and $FF] xor - Rijndael_T[1, D2 shr 8 and $FF] xor - Rijndael_T[2, A2 shr 16 and $FF] xor - Rijndael_T[3, B2 shr 24 ]; - D1 := Rijndael_T[0, D2 and $FF] xor - Rijndael_T[1, A2 shr 8 and $FF] xor - Rijndael_T[2, B2 shr 16 and $FF] xor - Rijndael_T[3, C2 shr 24 ]; - - P := @P[4]; - end; - - A2 := A1 xor P[0]; - B2 := B1 xor P[1]; - C2 := C1 xor P[2]; - D2 := D1 xor P[3]; - - PUInt32Array(Dest)[0] := (Rijndael_S[0, A2 and $FF] or - Rijndael_S[0, B2 shr 8 and $FF] shl 8 or - Rijndael_S[0, C2 shr 16 and $FF] shl 16 or - Rijndael_S[0, D2 shr 24 ] shl 24) xor P[4]; - PUInt32Array(Dest)[1] := (Rijndael_S[0, B2 and $FF] or - Rijndael_S[0, C2 shr 8 and $FF] shl 8 or - Rijndael_S[0, D2 shr 16 and $FF] shl 16 or - Rijndael_S[0, A2 shr 24 ] shl 24) xor P[5]; - PUInt32Array(Dest)[2] := (Rijndael_S[0, C2 and $FF] or - Rijndael_S[0, D2 shr 8 and $FF] shl 8 or - Rijndael_S[0, A2 shr 16 and $FF] shl 16 or - Rijndael_S[0, B2 shr 24 ] shl 24) xor P[6]; - PUInt32Array(Dest)[3] := (Rijndael_S[0, D2 and $FF] or - Rijndael_S[0, A2 shr 8 and $FF] shl 8 or - Rijndael_S[0, B2 shr 16 and $FF] shl 16 or - Rijndael_S[0, C2 shr 24 ] shl 24) xor P[7]; -end; - -procedure TCipher_Rijndael.DoDecode(Source, Dest: Pointer; Size: Integer); -var - P: PUInt32Array; - I: Integer; - A2, B2, C2, D2: UInt32; - A1, B1, C1, D1: UInt32; -begin - Assert(Size = Context.BlockSize); - - P := Pointer(PByte(FAdditionalBuffer) + FAdditionalBufferSize shr 1 + FRounds * 16); // for Pointer Math - A1 := PUInt32Array(Source)[0]; - B1 := PUInt32Array(Source)[1]; - C1 := PUInt32Array(Source)[2]; - D1 := PUInt32Array(Source)[3]; - - for I := 2 to FRounds do - begin - A2 := A1 xor P[0]; - B2 := B1 xor P[1]; - C2 := C1 xor P[2]; - D2 := D1 xor P[3]; - - A1 := Rijndael_T[4, A2 and $FF] xor - Rijndael_T[5, D2 shr 8 and $FF] xor - Rijndael_T[6, C2 shr 16 and $FF] xor - Rijndael_T[7, B2 shr 24 ]; - B1 := Rijndael_T[4, B2 and $FF] xor - Rijndael_T[5, A2 shr 8 and $FF] xor - Rijndael_T[6, D2 shr 16 and $FF] xor - Rijndael_T[7, C2 shr 24 ]; - C1 := Rijndael_T[4, C2 and $FF] xor - Rijndael_T[5, B2 shr 8 and $FF] xor - Rijndael_T[6, A2 shr 16 and $FF] xor - Rijndael_T[7, D2 shr 24 ]; - D1 := Rijndael_T[4, D2 and $FF] xor - Rijndael_T[5, C2 shr 8 and $FF] xor - Rijndael_T[6, B2 shr 16 and $FF] xor - Rijndael_T[7, A2 shr 24 ]; - - Dec(PUInt32(P), 4); - end; - - A2 := A1 xor P[0]; - B2 := B1 xor P[1]; - C2 := C1 xor P[2]; - D2 := D1 xor P[3]; - - Dec(PUInt32(P), 4); - - PUInt32Array(Dest)[0] := (Rijndael_S[1, A2 and $FF] or - Rijndael_S[1, D2 shr 8 and $FF] shl 8 or - Rijndael_S[1, C2 shr 16 and $FF] shl 16 or - Rijndael_S[1, B2 shr 24] shl 24) xor P[0]; - PUInt32Array(Dest)[1] := (Rijndael_S[1, B2 and $FF] or - Rijndael_S[1, A2 shr 8 and $FF] shl 8 or - Rijndael_S[1, D2 shr 16 and $FF] shl 16 or - Rijndael_S[1, C2 shr 24] shl 24) xor P[1]; - PUInt32Array(Dest)[2] := (Rijndael_S[1, C2 and $FF] or - Rijndael_S[1, B2 shr 8 and $FF] shl 8 or - Rijndael_S[1, A2 shr 16 and $FF] shl 16 or - Rijndael_S[1, D2 shr 24] shl 24) xor P[2]; - PUInt32Array(Dest)[3] := (Rijndael_S[1, D2 and $FF] or - Rijndael_S[1, C2 shr 8 and $FF] shl 8 or - Rijndael_S[1, B2 shr 16 and $FF] shl 16 or - Rijndael_S[1, A2 shr 24] shl 24) xor P[3]; -end; - -{ TCipher_Square } - -class function TCipher_Square.Context: TCipherContext; -begin - Result.KeySize := 16; - Result.BlockSize := 16; - Result.BufferSize := 16; - Result.AdditionalBufferSize := 9 * 4 * 2 * SizeOf(UInt32); - Result.NeedsAdditionalBufferBackup := False; - Result.MinRounds := 1; - Result.MaxRounds := 1; - Result.CipherType := [ctSymmetric, ctBlock]; -end; - -procedure TCipher_Square.DoInit(const Key; Size: Integer); -type - PSquare_Key = ^TSquare_Key; - TSquare_Key = array[0..8, 0..3] of UInt32; -var - E, D: PSquare_Key; - S, T, R: UInt32; - I, J: Integer; -begin - E := FAdditionalBuffer; - D := FAdditionalBuffer; Inc(D); - Move(Key, E^, Size); - - for I := 1 to 8 do - begin - T := E[I - 1, 3]; - T := T shr 8 or T shl 24; - E[I, 0] := E[I - 1, 0] xor T xor 1 shl (I - 1); - E[I, 1] := E[I - 1, 1] xor E[I, 0]; - E[I, 2] := E[I - 1, 2] xor E[I, 1]; - E[I, 3] := E[I - 1, 3] xor E[I, 2]; - - D[8 - I, 0] := E[I, 0]; - D[8 - I, 1] := E[I, 1]; - D[8 - I, 2] := E[I, 2]; - D[8 - I, 3] := E[I, 3]; - - for J := 0 to 3 do - begin - R := E[I - 1, J]; - S := Square_PHI[R and $FF]; - T := Square_PHI[R shr 8 and $FF]; - T := T shl 8 or T shr 24; - S := S xor T; - T := Square_PHI[R shr 16 and $FF]; - T := T shl 16 or T shr 16; - S := S xor T; - T := Square_PHI[R shr 24]; - T := T shl 24 or T shr 8; - S := S xor T; - E[I - 1, J] := S; - end; - end; - - D[8] := E[0]; -end; - -procedure TCipher_Square.DoEncode(Source, Dest: Pointer; Size: Integer); -var - Key: PUInt32Array; - A, B, C, D: UInt32; - AA, BB, CC: UInt32; - I: Integer; -begin - Key := FAdditionalBuffer; - A := PUInt32Array(Source)[0] xor Key[0]; - B := PUInt32Array(Source)[1] xor Key[1]; - C := PUInt32Array(Source)[2] xor Key[2]; - D := PUInt32Array(Source)[3] xor Key[3]; - Key := @Key[4]; - - for I := 0 to 6 do - begin - AA := Square_TE[0, A and $FF] xor - Square_TE[1, B and $FF] xor - Square_TE[2, C and $FF] xor - Square_TE[3, D and $FF] xor Key[0]; - BB := Square_TE[0, A shr 8 and $FF] xor - Square_TE[1, B shr 8 and $FF] xor - Square_TE[2, C shr 8 and $FF] xor - Square_TE[3, D shr 8 and $FF] xor Key[1]; - CC := Square_TE[0, A shr 16 and $FF] xor - Square_TE[1, B shr 16 and $FF] xor - Square_TE[2, C shr 16 and $FF] xor - Square_TE[3, D shr 16 and $FF] xor Key[2]; - D := Square_TE[0, A shr 24 ] xor - Square_TE[1, B shr 24 ] xor - Square_TE[2, C shr 24 ] xor - Square_TE[3, D shr 24 ] xor Key[3]; - - A := AA; B := BB; C := CC; - - Key := @Key[4]; - end; - - PUInt32Array(Dest)[0] := UInt32(Square_SE[A and $FF]) xor - UInt32(Square_SE[B and $FF]) shl 8 xor - UInt32(Square_SE[C and $FF]) shl 16 xor - UInt32(Square_SE[D and $FF]) shl 24 xor Key[0]; - PUInt32Array(Dest)[1] := UInt32(Square_SE[A shr 8 and $FF]) xor - UInt32(Square_SE[B shr 8 and $FF]) shl 8 xor - UInt32(Square_SE[C shr 8 and $FF]) shl 16 xor - UInt32(Square_SE[D shr 8 and $FF]) shl 24 xor Key[1]; - PUInt32Array(Dest)[2] := UInt32(Square_SE[A shr 16 and $FF]) xor - UInt32(Square_SE[B shr 16 and $FF]) shl 8 xor - UInt32(Square_SE[C shr 16 and $FF]) shl 16 xor - UInt32(Square_SE[D shr 16 and $FF]) shl 24 xor Key[2]; - PUInt32Array(Dest)[3] := UInt32(Square_SE[A shr 24 ]) xor - UInt32(Square_SE[B shr 24 ]) shl 8 xor - UInt32(Square_SE[C shr 24 ]) shl 16 xor - UInt32(Square_SE[D shr 24 ]) shl 24 xor Key[3]; -end; - -procedure TCipher_Square.DoDecode(Source, Dest: Pointer; Size: Integer); -var - Key: PUInt32Array; - A, B, C, D: UInt32; - AA, BB, CC: UInt32; - I: Integer; -begin - Key := @PUInt32Array(FAdditionalBuffer)[9 * 4]; - A := PUInt32Array(Source)[0] xor Key[0]; - B := PUInt32Array(Source)[1] xor Key[1]; - C := PUInt32Array(Source)[2] xor Key[2]; - D := PUInt32Array(Source)[3] xor Key[3]; - Key := @Key[4]; - - for I := 0 to 6 do - begin - AA := Square_TD[0, A and $FF] xor - Square_TD[1, B and $FF] xor - Square_TD[2, C and $FF] xor - Square_TD[3, D and $FF] xor Key[0]; - BB := Square_TD[0, A shr 8 and $FF] xor - Square_TD[1, B shr 8 and $FF] xor - Square_TD[2, C shr 8 and $FF] xor - Square_TD[3, D shr 8 and $FF] xor Key[1]; - CC := Square_TD[0, A shr 16 and $FF] xor - Square_TD[1, B shr 16 and $FF] xor - Square_TD[2, C shr 16 and $FF] xor - Square_TD[3, D shr 16 and $FF] xor Key[2]; - D := Square_TD[0, A shr 24 ] xor - Square_TD[1, B shr 24 ] xor - Square_TD[2, C shr 24 ] xor - Square_TD[3, D shr 24 ] xor Key[3]; - - A := AA; B := BB; C := CC; - Key := @Key[4]; - end; - - PUInt32Array(Dest)[0] := UInt32(Square_SD[A and $FF]) xor - UInt32(Square_SD[B and $FF]) shl 8 xor - UInt32(Square_SD[C and $FF]) shl 16 xor - UInt32(Square_SD[D and $FF]) shl 24 xor Key[0]; - PUInt32Array(Dest)[1] := UInt32(Square_SD[A shr 8 and $FF]) xor - UInt32(Square_SD[B shr 8 and $FF]) shl 8 xor - UInt32(Square_SD[C shr 8 and $FF]) shl 16 xor - UInt32(Square_SD[D shr 8 and $FF]) shl 24 xor Key[1]; - PUInt32Array(Dest)[2] := UInt32(Square_SD[A shr 16 and $FF]) xor - UInt32(Square_SD[B shr 16 and $FF]) shl 8 xor - UInt32(Square_SD[C shr 16 and $FF]) shl 16 xor - UInt32(Square_SD[D shr 16 and $FF]) shl 24 xor Key[2]; - PUInt32Array(Dest)[3] := UInt32(Square_SD[A shr 24 ]) xor - UInt32(Square_SD[B shr 24 ]) shl 8 xor - UInt32(Square_SD[C shr 24 ]) shl 16 xor - UInt32(Square_SD[D shr 24 ]) shl 24 xor Key[3]; -end; - -{ TCipher_SCOP } - -class function TCipher_SCOP.Context: TCipherContext; -begin - Result.KeySize := 48; - Result.BlockSize := 4; - Result.BufferSize := 32; - Result.AdditionalBufferSize := 384 * 4 + 3 * SizeOf(UInt32); - Result.NeedsAdditionalBufferBackup := True; - Result.MinRounds := 1; - Result.MaxRounds := 1; - Result.CipherType := [ctSymmetric, ctStream]; -end; - -procedure TCipher_SCOP.DoInit(const Key; Size: Integer); -var - Init_State: packed record - Coef: array[0..7, 0..3] of Byte; - X: array[0..3] of UInt32; - end; - - procedure ExpandKey; - var - P: PByteArray; - I, C: Integer; - begin - C := 1; - P := @Init_State; - Move(Key, P^, Size); - for I := Size to 47 do - P[I] := P[I - Size] + P[I - Size + 1]; - for I := 0 to 31 do - if P[I] = 0 then - begin - P[I] := C; - Inc(C); - end; - end; - - procedure GP8(Data: PUInt32Array); - var - I, I2: Integer; - NewX: array[0..3] of UInt32; - X1, X2, X3, X4: UInt32; - Y1, Y2: UInt32; - begin - I := 0; - I2 := 0; - while I < 8 do - begin - X1 := Init_State.X[I2] shr 16; - X2 := X1 * X1; - X3 := X2 * X1; - X4 := X3 * X1; - Y1 := Init_State.Coef[I][0] * X4 + - Init_State.Coef[I][1] * X3 + - Init_State.Coef[I][2] * X2 + - Init_State.Coef[I][3] * X1 + 1; - X1 := Init_State.X[I2] and $FFFF; - X2 := X1 * X1; - X3 := X2 * X1; - X4 := X3 * X1; - Y2 := Init_State.Coef[I + 1][0] * X4 + - Init_State.Coef[I + 1][1] * X3 + - Init_State.Coef[I + 1][2] * X2 + - Init_State.Coef[I + 1][3] * X1 + 1; - Data[I2] := Y1 shl 16 or Y2 and $FFFF; - NewX[I2] := Y1 and $FFFF0000 or Y2 shr 16; - Inc(I2); - Inc(I, 2); - end; - Init_State.X[0] := NewX[0] shr 16 or NewX[3] shl 16; - Init_State.X[1] := NewX[0] shl 16 or NewX[1] shr 16; - Init_State.X[2] := NewX[1] shl 16 or NewX[2] shr 16; - Init_State.X[3] := NewX[2] shl 16 or NewX[3] shr 16; - end; - -var - I, J: Integer; - T: array[0..3] of UInt32; - P: PUInt32Array; -begin - FillChar(Init_State, SizeOf(Init_State), 0); - FillChar(T, SizeOf(T), 0); - P := Pointer(PByte(FAdditionalBuffer) + 12); // for Pointer Math - ExpandKey; - for I := 0 to 7 do - GP8(@T); - for I := 0 to 11 do - begin - for J := 0 to 7 do - GP8(@P[I * 32 + J * 4]); - GP8(@T); - end; - GP8(@T); - I := T[3] and $7F; - P[I] := P[I] or 1; - P := FAdditionalBuffer; - P[0] := T[3] shr 24 and $FF; - P[1] := T[3] shr 16 and $FF; - P[2] := T[3] shr 8 and $FF; - ProtectBuffer(Init_State, SizeOf(Init_State)); -end; - -procedure TCipher_SCOP.DoEncode(Source, Dest: Pointer; Size: Integer); -var - I, J: Byte; - T2, T3, T1: UInt32; - P: PUInt32Array; - W: Integer; -begin - P := FAdditionalBuffer; - I := P[0]; - J := P[1]; - T3 := P[2]; - for W := 0 to Size div 4 - 1 do - begin - T1 := P[J + 3 + 128]; Inc(J, T3); - T2 := P[J + 3 + 128]; - PUInt32Array(Dest)[W] := PUInt32Array(Source)[W] + T1 + T2; - T3 := T2 + P[I + 3]; Inc(I); - P[J + 3 + 128] := T3; - Inc(J, T2); - end; - P[0] := I; - P[1] := J; - P[2] := T3; -end; - -procedure TCipher_SCOP.DoDecode(Source, Dest: Pointer; Size: Integer); -var - I, J: Byte; - T1, T2, T3: UInt32; - P: PUInt32Array; - W: Integer; -begin - P := FAdditionalBuffer; - I := P[0]; - J := P[1]; - T3 := P[2]; - for W := 0 to Size div 4 - 1 do - begin - T1 := P[J + 3 + 128]; Inc(J, T3); - T2 := P[J + 3 + 128]; - PUInt32Array(Dest)[W] := PUInt32Array(Source)[W] - T1 - T2; - T3 := T2 + P[I + 3]; - Inc(I); - P[J + 3 + 128] := T3; - Inc(J, T2); - end; - P[0] := I; - P[1] := J; - P[2] := T3; -end; - -{ TCipher_SCOP_DEC52 } - -{ TODO : The old failure needs to be restored again } - -class function TCipher_SCOP_DEC52.Context: TCipherContext; -begin - Result.KeySize := 48; - Result.BlockSize := 4; - Result.BufferSize := 32; - Result.AdditionalBufferSize := 384 * 4 + 3 * SizeOf(UInt32); - Result.NeedsAdditionalBufferBackup := True; - Result.MinRounds := 1; - Result.MaxRounds := 1; - Result.CipherType := [ctSymmetric, ctStream]; -end; - -procedure TCipher_SCOP_DEC52.DoInit(const Key; Size: Integer); -var - Init_State: packed record - Coef: array[0..7, 0..3] of Byte; - X: array[0..3] of UInt32; - end; - - procedure ExpandKey; - var - P: PByteArray; - I, C: Integer; - begin - C := 1; - P := @Init_State; - Move(Key, P^, Size); - for I := Size to 47 do - P[I] := P[I - Size] + P[I - Size + 1]; - for I := 0 to 31 do - if P[I] = 0 then - begin - P[I] := C; - Inc(C); - end; - end; - - procedure GP8(Data: PUInt32Array); - var - I, I2: Integer; - NewX: array[0..3] of UInt32; - X1, X2, X3, X4: UInt32; - Y1, Y2: UInt32; - begin - I := 0; - I2 := 0; - while I < 8 do - begin - X1 := Init_State.X[I2] shr 16; - X2 := X1 * X1; - X3 := X2 * X1; - X4 := X3 * X1; - Y1 := Init_State.Coef[I][0] * X4 + - Init_State.Coef[I][1] * X3 + - Init_State.Coef[I][2] * X2 + - Init_State.Coef[I][3] * X1 + 1; - X1 := Init_State.X[I2] and $FFFF; - X2 := X1 * X1; - X3 := X2 * X1; - X4 := X3 * X1; - Y2 := Init_State.Coef[I + 1][0] * X4 + - Init_State.Coef[I + 2][1] * X3 + - Init_State.Coef[I + 3][2] * X2 + - Init_State.Coef[I + 4][3] * X1 + 1; - Data[I2] := Y1 shl 16 or Y2 and $FFFF; - NewX[I2] := Y1 and $FFFF0000 or Y2 shr 16; - Inc(I2); - Inc(I, 2); - end; - Init_State.X[0] := NewX[0] shr 16 or NewX[3] shl 16; - Init_State.X[1] := NewX[0] shl 16 or NewX[1] shr 16; - Init_State.X[2] := NewX[1] shl 16 or NewX[2] shr 16; - Init_State.X[3] := NewX[2] shl 16 or NewX[3] shr 16; - end; - -var - I, J: Integer; - T: array[0..3] of Integer; - P: PUInt32Array; -begin - FillChar(Init_State, SizeOf(Init_State), 0); - FillChar(T, SizeOf(T), 0); - P := Pointer(PByte(FAdditionalBuffer) + 12); // for Pointer Math - ExpandKey; - for I := 0 to 7 do - GP8(@T); - for I := 0 to 11 do - begin - for J := 0 to 7 do - GP8(@P[I * 32 + J * 4]); - GP8(@T); - end; - GP8(@T); - I := T[3] and $7F; - P[I + 3] := P[I + 3] or 1; - P := FAdditionalBuffer; - P[0] := T[3] shr 24 and $FF; - P[1] := T[3] shr 16 and $FF; - P[2] := T[3] shr 8 and $FF; - ProtectBuffer(Init_State, SizeOf(Init_State)); -end; - -procedure TCipher_SCOP_DEC52.DoEncode(Source, Dest: Pointer; Size: Integer); -var - I, J: Byte; - T2, T3, T1: UInt32; - P: PUInt32Array; - W: Integer; -begin - P := FAdditionalBuffer; - I := P[0]; - J := P[1]; - T3 := P[2]; - for W := 0 to Size div 4 - 1 do - begin - T1 := P[J + 3 + 128]; Inc(J, T3); - T2 := P[J + 3 + 128]; - PUInt32Array(Dest)[W] := PUInt32Array(Source)[W] + T1 + T2; - T3 := T2 + P[I + 3]; Inc(I); - P[J + 3 + 128] := T3; - Inc(J, T2); - end; - P[0] := I; - P[1] := J; - P[2] := T3; -end; - -procedure TCipher_SCOP_DEC52.DoDecode(Source, Dest: Pointer; Size: Integer); -var - I, J: Byte; - T1, T2, T3: UInt32; - P: PUInt32Array; - W: Integer; -begin - P := FAdditionalBuffer; - I := P[0]; - J := P[1]; - T3 := P[2]; - for W := 0 to Size div 4 - 1 do - begin - T1 := P[J + 3 + 128]; Inc(J, T3); - T2 := P[J + 3 + 128]; - PUInt32Array(Dest)[W] := PUInt32Array(Source)[W] - T1 - T2; - T3 := T2 + P[I + 3]; - Inc(I); - P[J + 3 + 128] := T3; - Inc(J, T2); - end; - P[0] := I; - P[1] := J; - P[2] := T3; -end; - -{ TCipher_Sapphire } - -type - PSapphireKey = ^TSapphireKey; - TSapphireKey = packed record - Cards: array[0..255] of UInt32; - Rotor: UInt32; - Ratchet: UInt32; - Avalanche: UInt32; - Plain: UInt32; - Cipher: UInt32; - end; - -class function TCipher_Sapphire.Context: TCipherContext; -begin - Result.KeySize := 1024; - Result.BlockSize := 1; - Result.BufferSize := 32; - Result.AdditionalBufferSize := SizeOf(TSapphireKey); - Result.NeedsAdditionalBufferBackup := True; - Result.MinRounds := 1; - Result.MaxRounds := 1; - Result.CipherType := [ctSymmetric, ctStream]; -end; - -procedure TCipher_Sapphire.DoInit(const Key; Size: Integer); -var - Sum: Byte; - P: Integer; - - function KeyRand(Max: UInt32): Byte; - var - I, M: UInt32; - begin - Result := 0; - if Max = 0 then - Exit; - I := 0; - M := 1; - - while M < Max do - Inc(M, M or 1); - - repeat - Inc(Sum, TByteArray(Key)[P]); - Inc(P); - if P >= Size then - begin - P := 0; - Inc(Sum, Size); - end; - Result := M and Sum; - Inc(I); - if I > 11 then - Result := Result mod Max; - until Result <= Max; - end; - -var - I, S, T: Integer; - SKey : PSapphireKey; -begin - SKey := PSapphireKey(FAdditionalBuffer); - if Size <= 0 then - begin - SKey.Rotor := 1; - SKey.Ratchet := 3; - SKey.Avalanche := 5; - SKey.Plain := 7; - SKey.Cipher := 11; - for I := 0 to 255 do - SKey.Cards[I] := 255 - I; - end - else - begin - for I := 0 to 255 do - SKey.Cards[I] := I; - P := 0; - Sum := 0; - for I := 255 downto 1 do - begin - S := KeyRand(I); - T := SKey.Cards[I]; - SKey.Cards[I] := SKey.Cards[S]; - SKey.Cards[S] := T; - end; - SKey.Rotor := SKey.Cards[1]; - SKey.Ratchet := SKey.Cards[3]; - SKey.Avalanche := SKey.Cards[5]; - SKey.Plain := SKey.Cards[7]; - SKey.Cipher := SKey.Cards[Sum]; - end; -end; - -procedure TCipher_Sapphire.DoEncode(Source, Dest: Pointer; Size: Integer); -var - T: UInt32; - I: Integer; - SKey: PSapphireKey; -begin - SKey := PSapphireKey(FAdditionalBuffer); - for I := 0 to Size - 1 do - begin - SKey.Ratchet := (SKey.Ratchet + SKey.Cards[SKey.Rotor]) and $FF; - SKey.Rotor := (SKey.Rotor + 1) and $FF; - T := SKey.Cards[SKey.Cipher]; - SKey.Cards[SKey.Cipher] := SKey.Cards[SKey.Ratchet]; - SKey.Cards[SKey.Ratchet] := SKey.Cards[SKey.Plain]; - SKey.Cards[SKey.Plain] := SKey.Cards[SKey.Rotor]; - SKey.Cards[SKey.Rotor] := T; - SKey.Avalanche := (SKey.Avalanche + SKey.Cards[T]) and $FF; - T := (SKey.Cards[SKey.Plain] + SKey.Cards[SKey.Cipher] + SKey.Cards[SKey.Avalanche]) and $FF; - SKey.Plain := PByteArray(Source)[I]; - SKey.Cipher := SKey.Plain xor SKey.Cards[SKey.Cards[T]] xor - SKey.Cards[(SKey.Cards[SKey.Ratchet] + - SKey.Cards[SKey.Rotor]) and $FF]; - PByteArray(Dest)[I] := SKey.Cipher; - end; -end; - -procedure TCipher_Sapphire.DoDecode(Source, Dest: Pointer; Size: Integer); -var - T: UInt32; - I: Integer; - SKey: PSapphireKey; -begin - SKey := PSapphireKey(FAdditionalBuffer); - for I := 0 to Size - 1 do - begin - SKey.Ratchet := (SKey.Ratchet + SKey.Cards[SKey.Rotor]) and $FF; - SKey.Rotor := (SKey.Rotor + 1) and $FF; - T := SKey.Cards[SKey.Cipher]; - SKey.Cards[SKey.Cipher] := SKey.Cards[SKey.Ratchet]; - SKey.Cards[SKey.Ratchet] := SKey.Cards[SKey.Plain]; - SKey.Cards[SKey.Plain] := SKey.Cards[SKey.Rotor]; - SKey.Cards[SKey.Rotor] := T; - SKey.Avalanche := (SKey.Avalanche + SKey.Cards[T]) and $FF; - T := (SKey.Cards[SKey.Plain] + SKey.Cards[SKey.Cipher] + SKey.Cards[SKey.Avalanche]) and $FF; - SKey.Cipher := PByteArray(Source)[I]; - SKey.Plain := SKey.Cipher xor SKey.Cards[SKey.Cards[T]] xor - SKey.Cards[(SKey.Cards[SKey.Ratchet] + - SKey.Cards[SKey.Rotor]) and $FF]; - PByteArray(Dest)[I] := SKey.Plain; - end; -end; - -{ TCipher_1DES } - -procedure DES_Func(Source, Dest, Key: PUInt32Array); -var - L, R, X, Y, I: UInt32; -begin - L := SwapUInt32(Source[0]); - R := SwapUInt32(Source[1]); - - X := (L shr 4 xor R) and $0F0F0F0F; R := R xor X; L := L xor X shl 4; - X := (L shr 16 xor R) and $0000FFFF; R := R xor X; L := L xor X shl 16; - X := (R shr 2 xor L) and $33333333; L := L xor X; R := R xor X shl 2; - X := (R shr 8 xor L) and $00FF00FF; L := L xor X; R := R xor X shl 8; - - R := R shl 1 or R shr 31; - X := (L xor R) and $AAAAAAAA; - R := R xor X; - L := L xor X; - L := L shl 1 or L shr 31; - - for I := 0 to 7 do - begin - X := (R shl 28 or R shr 4) xor Key[0]; - Y := R xor Key[1]; - L := L xor (DES_Data[0, X and $3F] or DES_Data[1, X shr 8 and $3F] or - DES_Data[2, X shr 16 and $3F] or DES_Data[3, X shr 24 and $3F] or - DES_Data[4, Y and $3F] or DES_Data[5, Y shr 8 and $3F] or - DES_Data[6, Y shr 16 and $3F] or DES_Data[7, Y shr 24 and $3F]); - - X := (L shl 28 or L shr 4) xor Key[2]; - Y := L xor Key[3]; - R := R xor (DES_Data[0, X and $3F] or DES_Data[1, X shr 8 and $3F] or - DES_Data[2, X shr 16 and $3F] or DES_Data[3, X shr 24 and $3F] or - DES_Data[4, Y and $3F] or DES_Data[5, Y shr 8 and $3F] or - DES_Data[6, Y shr 16 and $3F] or DES_Data[7, Y shr 24 and $3F]); - Key := @Key[4]; - end; - - R := R shl 31 or R shr 1; - X := (L xor R) and $AAAAAAAA; - R := R xor X; - L := L xor X; - L := L shl 31 or L shr 1; - - X := (L shr 8 xor R) and $00FF00FF; R := R xor X; L := L xor X shl 8; - X := (L shr 2 xor R) and $33333333; R := R xor X; L := L xor X shl 2; - X := (R shr 16 xor L) and $0000FFFF; L := L xor X; R := R xor X shl 16; - X := (R shr 4 xor L) and $0F0F0F0F; L := L xor X; R := R xor X shl 4; - - Dest[0] := SwapUInt32(R); - Dest[1] := SwapUInt32(L); -end; - -class function TCipher_1DES.Context: TCipherContext; -begin - Result.KeySize := 8; - Result.BlockSize := 8; - Result.BufferSize := 8; - Result.AdditionalBufferSize := 32 * 4 * 2; - Result.NeedsAdditionalBufferBackup := False; - Result.MinRounds := 1; - Result.MaxRounds := 1; - Result.CipherType := [ctSymmetric, ctBlock]; -end; - -procedure TCipher_1DES.DoInitKey(const Data: array of Byte; Key: PUInt32Array; Reverse: Boolean); -const - ROT: array[0..15] of Byte = (1, 2, 4, 6, 8, 10, 12, 14, 15, 17, 19, 21, 23, 25, 27, 28); -var - I, J, L, M, N: UInt32; - PC_M, PC_R: array[0..55] of Byte; - K: array[0..31] of UInt32; -begin - FillChar(K, SizeOf(K), 0); - for I := 0 to 55 do - if Data[DES_PC1[I] shr 3] and ($80 shr (DES_PC1[I] and $07)) <> 0 then - PC_M[I] := 1 - else - PC_M[I] := 0; - for I := 0 to 15 do - begin - if Reverse then - M := (15 - I) shl 1 - else - M := I shl 1; - N := M + 1; - for J := 0 to 27 do - begin - L := J + ROT[I]; - if L < 28 then - PC_R[J] := PC_M[L] - else - PC_R[J] := PC_M[L - 28]; - end; - for J := 28 to 55 do - begin - L := J + ROT[I]; - if L < 56 then - PC_R[J] := PC_M[L] - else - PC_R[J] := PC_M[L - 28]; - end; - L := $1000000; - for J := 0 to 23 do - begin - L := L shr 1; - if PC_R[DES_PC2[J ]] <> 0 then - K[M] := K[M] or L; - if PC_R[DES_PC2[J + 24]] <> 0 then - K[N] := K[N] or L; - end; - end; - for I := 0 to 15 do - begin - M := I shl 1; - N := M + 1; - Key[0] := K[M] and $00FC0000 shl 6 or - K[M] and $00000FC0 shl 10 or - K[N] and $00FC0000 shr 10 or - K[N] and $00000FC0 shr 6; - Key[1] := K[M] and $0003F000 shl 12 or - K[M] and $0000003F shl 16 or - K[N] and $0003F000 shr 4 or - K[N] and $0000003F; - Key := @Key[2]; - end; - ProtectBuffer(K, SizeOf(K)); - ProtectBuffer(PC_M, SizeOf(PC_M)); - ProtectBuffer(PC_R, SizeOf(PC_R)); -end; - -procedure TCipher_1DES.DoInit(const Key; Size: Integer); -var - K: array[0..7] of Byte; -begin - FillChar(K, SizeOf(K), 0); - Move(Key, K, Size); - DoInitKey(K, FAdditionalBuffer, False); - DoInitKey(K, @PUInt32Array(FAdditionalBuffer)[32], True); - ProtectBuffer(K, SizeOf(K)); -end; - -procedure TCipher_1DES.DoEncode(Source, Dest: Pointer; Size: Integer); -begin - Assert(Size = Context.BlockSize); - DES_Func(Source, Dest, FAdditionalBuffer); -end; - -procedure TCipher_1DES.DoDecode(Source, Dest: Pointer; Size: Integer); -begin - Assert(Size = Context.BlockSize); - DES_Func(Source, Dest, @PUInt32Array(FAdditionalBuffer)[32]); -end; - -{ TCipher_2DES } - -class function TCipher_2DES.Context: TCipherContext; -begin - Result.KeySize := 16; - Result.BlockSize := 8; - Result.BufferSize := 8; - Result.AdditionalBufferSize := 32 * 4 * 2 * 2; - Result.NeedsAdditionalBufferBackup := False; - Result.MinRounds := 1; - Result.MaxRounds := 1; - Result.CipherType := [ctSymmetric, ctBlock]; -end; - -procedure TCipher_2DES.DoInit(const Key; Size: Integer); -var - K: array[0..15] of Byte; - P: PUInt32Array; -begin - FillChar(K, SizeOf(K), 0); - Move(Key, K, Size); - P := FAdditionalBuffer; - DoInitKey(K[0], @P[ 0], False); - DoInitKey(K[8], @P[32], True); - DoInitKey(K[0], @P[64], True); - DoInitKey(K[8], @P[96], False); - ProtectBuffer(K, SizeOf(K)); -end; - -procedure TCipher_2DES.DoEncode(Source, Dest: Pointer; Size: Integer); -begin - Assert(Size = Context.BlockSize); - DES_Func(Source, Dest, FAdditionalBuffer); - DES_Func(Source, Dest, @PUInt32Array(FAdditionalBuffer)[32]); - DES_Func(Source, Dest, FAdditionalBuffer); -end; - -procedure TCipher_2DES.DoDecode(Source, Dest: Pointer; Size: Integer); -begin - Assert(Size = Context.BlockSize); - DES_Func(Source, Dest, @PUInt32Array(FAdditionalBuffer)[64]); - DES_Func(Source, Dest, @PUInt32Array(FAdditionalBuffer)[96]); - DES_Func(Source, Dest, @PUInt32Array(FAdditionalBuffer)[64]); -end; - -{ TCipher_3DES } - -class function TCipher_3DES.Context: TCipherContext; -begin - Result.KeySize := 24; - Result.BlockSize := 8; - Result.BufferSize := 8; - Result.AdditionalBufferSize := 32 * 4 * 2 * 3; - Result.NeedsAdditionalBufferBackup := False; - Result.MinRounds := 1; - Result.MaxRounds := 1; - Result.CipherType := [ctSymmetric, ctBlock]; -end; - -procedure TCipher_3DES.DoInit(const Key; Size: Integer); -var - K: array[0..23] of Byte; - P: PUInt32Array; -begin - FillChar(K, SizeOf(K), 0); - Move(Key, K, Size); - P := FAdditionalBuffer; - DoInitKey(K[ 0], @P[ 0], False); - DoInitKey(K[ 8], @P[ 32], True); - DoInitKey(K[16], @P[ 64], False); - DoInitKey(K[16], @P[ 96], True); - DoInitKey(K[ 8], @P[128], False); - DoInitKey(K[ 0], @P[160], True); - ProtectBuffer(K, SizeOf(K)); -end; - -procedure TCipher_3DES.DoEncode(Source, Dest: Pointer; Size: Integer); -begin - Assert(Size = Context.BlockSize); - DES_Func(Source, Dest, @PUInt32Array(FAdditionalBuffer)[ 0]); - DES_Func(Source, Dest, @PUInt32Array(FAdditionalBuffer)[32]); - DES_Func(Source, Dest, @PUInt32Array(FAdditionalBuffer)[64]); -end; - -procedure TCipher_3DES.DoDecode(Source, Dest: Pointer; Size: Integer); -begin - Assert(Size = Context.BlockSize); - DES_Func(Source, Dest, @PUInt32Array(FAdditionalBuffer)[96]); - DES_Func(Source, Dest, @PUInt32Array(FAdditionalBuffer)[128]); - DES_Func(Source, Dest, @PUInt32Array(FAdditionalBuffer)[160]); -end; - -{ TCipher_2DDES } - -class function TCipher_2DDES.Context: TCipherContext; -begin - Result := inherited Context; - Result.BlockSize := 16; - Result.BufferSize := 16; - Result.MinRounds := 1; - Result.MaxRounds := 1; - Result.CipherType := [ctSymmetric, ctBlock]; -end; - -procedure TCipher_2DDES.DoEncode(Source, Dest: Pointer; Size: Integer); -var - T: UInt32; -begin - Assert(Size = Context.BlockSize); - - DES_Func(@PUInt32Array(Source)[0], @PUInt32Array(Dest)[0], FAdditionalBuffer); - DES_Func(@PUInt32Array(Source)[2], @PUInt32Array(Dest)[2], FAdditionalBuffer); - T := PUInt32Array(Dest)[1]; - PUInt32Array(Dest)[1] := PUInt32Array(Dest)[2]; - PUInt32Array(Dest)[2] := T; - DES_Func(@PUInt32Array(Dest)[0], @PUInt32Array(Dest)[0], @PUInt32Array(FAdditionalBuffer)[32]); - DES_Func(@PUInt32Array(Dest)[2], @PUInt32Array(Dest)[2], @PUInt32Array(FAdditionalBuffer)[32]); - T := PUInt32Array(Dest)[1]; - PUInt32Array(Dest)[1] := PUInt32Array(Dest)[2]; - PUInt32Array(Dest)[2] := T; - DES_Func(@PUInt32Array(Dest)[0], @PUInt32Array(Dest)[0], FAdditionalBuffer); - DES_Func(@PUInt32Array(Dest)[2], @PUInt32Array(Dest)[2], FAdditionalBuffer); -end; - -procedure TCipher_2DDES.DoDecode(Source, Dest: Pointer; Size: Integer); -var - T: UInt32; -begin - Assert(Size = Context.BlockSize); - - DES_Func(@PUInt32Array(Source)[0], @PUInt32Array(Dest)[0], @PUInt32Array(FAdditionalBuffer)[64]); - DES_Func(@PUInt32Array(Source)[2], @PUInt32Array(Dest)[2], @PUInt32Array(FAdditionalBuffer)[64]); - T := PUInt32Array(Dest)[1]; - PUInt32Array(Dest)[1] := PUInt32Array(Dest)[2]; - PUInt32Array(Dest)[2] := T; - DES_Func(@PUInt32Array(Dest)[0], @PUInt32Array(Dest)[0], @PUInt32Array(FAdditionalBuffer)[96]); - DES_Func(@PUInt32Array(Dest)[2], @PUInt32Array(Dest)[2], @PUInt32Array(FAdditionalBuffer)[96]); - T := PUInt32Array(Dest)[1]; - PUInt32Array(Dest)[1] := PUInt32Array(Dest)[2]; - PUInt32Array(Dest)[2] := T; - DES_Func(@PUInt32Array(Dest)[0], @PUInt32Array(Dest)[0], @PUInt32Array(FAdditionalBuffer)[64]); - DES_Func(@PUInt32Array(Dest)[2], @PUInt32Array(Dest)[2], @PUInt32Array(FAdditionalBuffer)[64]); -end; - -{ TCipher_3DDES } - -class function TCipher_3DDES.Context: TCipherContext; -begin - Result := inherited Context; - Result.BlockSize := 16; - Result.BufferSize := 16; - Result.MinRounds := 1; - Result.MaxRounds := 1; - Result.CipherType := [ctSymmetric, ctBlock]; -end; - -procedure TCipher_3DDES.DoEncode(Source, Dest: Pointer; Size: Integer); -var - T: UInt32; -begin - Assert(Size = Context.BlockSize); - - DES_Func(@PUInt32Array(Source)[0], @PUInt32Array(Dest)[0], FAdditionalBuffer); - DES_Func(@PUInt32Array(Source)[2], @PUInt32Array(Dest)[2], FAdditionalBuffer); - T := PUInt32Array(Dest)[1]; - PUInt32Array(Dest)[1] := PUInt32Array(Dest)[2]; - PUInt32Array(Dest)[2] := T; - DES_Func(@PUInt32Array(Dest)[0], @PUInt32Array(Dest)[0], @PUInt32Array(FAdditionalBuffer)[32]); - DES_Func(@PUInt32Array(Dest)[2], @PUInt32Array(Dest)[2], @PUInt32Array(FAdditionalBuffer)[32]); - T := PUInt32Array(Dest)[1]; - PUInt32Array(Dest)[1] := PUInt32Array(Dest)[2]; - PUInt32Array(Dest)[2] := T; - DES_Func(@PUInt32Array(Dest)[0], @PUInt32Array(Dest)[0], @PUInt32Array(FAdditionalBuffer)[64]); - DES_Func(@PUInt32Array(Dest)[2], @PUInt32Array(Dest)[2], @PUInt32Array(FAdditionalBuffer)[64]); -end; - -procedure TCipher_3DDES.DoDecode(Source, Dest: Pointer; Size: Integer); -var - T: UInt32; -begin - Assert(Size = Context.BlockSize); - - DES_Func(@PUInt32Array(Source)[0], @PUInt32Array(Dest)[0], @PUInt32Array(FAdditionalBuffer)[96]); - DES_Func(@PUInt32Array(Source)[2], @PUInt32Array(Dest)[2], @PUInt32Array(FAdditionalBuffer)[96]); - T := PUInt32Array(Dest)[1]; - PUInt32Array(Dest)[1] := PUInt32Array(Dest)[2]; - PUInt32Array(Dest)[2] := T; - DES_Func(@PUInt32Array(Dest)[0], @PUInt32Array(Dest)[0], @PUInt32Array(FAdditionalBuffer)[128]); - DES_Func(@PUInt32Array(Dest)[2], @PUInt32Array(Dest)[2], @PUInt32Array(FAdditionalBuffer)[128]); - T := PUInt32Array(Dest)[1]; - PUInt32Array(Dest)[1] := PUInt32Array(Dest)[2]; - PUInt32Array(Dest)[2] := T; - DES_Func(@PUInt32Array(Dest)[0], @PUInt32Array(Dest)[0], @PUInt32Array(FAdditionalBuffer)[160]); - DES_Func(@PUInt32Array(Dest)[2], @PUInt32Array(Dest)[2], @PUInt32Array(FAdditionalBuffer)[160]); -end; - -{ TCipher_3TDES } - -class function TCipher_3TDES.Context: TCipherContext; -begin - Result := inherited Context; - Result.BlockSize := 24; - Result.BufferSize := 24; - Result.MinRounds := 1; - Result.MaxRounds := 1; - Result.CipherType := [ctSymmetric, ctBlock]; -end; - -procedure TCipher_3TDES.DoEncode(Source, Dest: Pointer; Size: Integer); -var - T: UInt32; -begin - Assert(Size = Context.BlockSize); - - DES_Func(@PUInt32Array(Source)[0], @PUInt32Array(Dest)[0], FAdditionalBuffer); - DES_Func(@PUInt32Array(Source)[2], @PUInt32Array(Dest)[2], FAdditionalBuffer); - DES_Func(@PUInt32Array(Source)[4], @PUInt32Array(Dest)[4], FAdditionalBuffer); - T := PUInt32Array(Dest)[1]; - PUInt32Array(Dest)[1] := PUInt32Array(Dest)[2]; - PUInt32Array(Dest)[2] := T; - T := PUInt32Array(Dest)[3]; - PUInt32Array(Dest)[3] := PUInt32Array(Dest)[4]; - PUInt32Array(Dest)[4] := T; - DES_Func(@PUInt32Array(Dest)[0], @PUInt32Array(Dest)[0], @PUInt32Array(FAdditionalBuffer)[32]); - DES_Func(@PUInt32Array(Dest)[2], @PUInt32Array(Dest)[2], @PUInt32Array(FAdditionalBuffer)[32]); - DES_Func(@PUInt32Array(Dest)[4], @PUInt32Array(Dest)[4], @PUInt32Array(FAdditionalBuffer)[32]); - T := PUInt32Array(Dest)[1]; - PUInt32Array(Dest)[1] := PUInt32Array(Dest)[2]; - PUInt32Array(Dest)[2] := T; - T := PUInt32Array(Dest)[3]; - PUInt32Array(Dest)[3] := PUInt32Array(Dest)[4]; - PUInt32Array(Dest)[4] := T; - DES_Func(@PUInt32Array(Dest)[0], @PUInt32Array(Dest)[0], @PUInt32Array(FAdditionalBuffer)[64]); - DES_Func(@PUInt32Array(Dest)[2], @PUInt32Array(Dest)[2], @PUInt32Array(FAdditionalBuffer)[64]); - DES_Func(@PUInt32Array(Dest)[4], @PUInt32Array(Dest)[4], @PUInt32Array(FAdditionalBuffer)[64]); -end; - -procedure TCipher_3TDES.DoDecode(Source, Dest: Pointer; Size: Integer); -var - T: UInt32; -begin - Assert(Size = Context.BlockSize); - - DES_Func(@PUInt32Array(Source)[0], @PUInt32Array(Dest)[0], @PUInt32Array(FAdditionalBuffer)[96]); - DES_Func(@PUInt32Array(Source)[2], @PUInt32Array(Dest)[2], @PUInt32Array(FAdditionalBuffer)[96]); - DES_Func(@PUInt32Array(Source)[4], @PUInt32Array(Dest)[4], @PUInt32Array(FAdditionalBuffer)[96]); - T := PUInt32Array(Dest)[1]; - PUInt32Array(Dest)[1] := PUInt32Array(Dest)[2]; - PUInt32Array(Dest)[2] := T; - T := PUInt32Array(Dest)[3]; - PUInt32Array(Dest)[3] := PUInt32Array(Dest)[4]; - PUInt32Array(Dest)[4] := T; - DES_Func(@PUInt32Array(Dest)[0], @PUInt32Array(Dest)[0], @PUInt32Array(FAdditionalBuffer)[128]); - DES_Func(@PUInt32Array(Dest)[2], @PUInt32Array(Dest)[2], @PUInt32Array(FAdditionalBuffer)[128]); - DES_Func(@PUInt32Array(Dest)[4], @PUInt32Array(Dest)[4], @PUInt32Array(FAdditionalBuffer)[128]); - T := PUInt32Array(Dest)[1]; - PUInt32Array(Dest)[1] := PUInt32Array(Dest)[2]; - PUInt32Array(Dest)[2] := T; - T := PUInt32Array(Dest)[3]; - PUInt32Array(Dest)[3] := PUInt32Array(Dest)[4]; - PUInt32Array(Dest)[4] := T; - DES_Func(@PUInt32Array(Dest)[0], @PUInt32Array(Dest)[0], @PUInt32Array(FAdditionalBuffer)[160]); - DES_Func(@PUInt32Array(Dest)[2], @PUInt32Array(Dest)[2], @PUInt32Array(FAdditionalBuffer)[160]); - DES_Func(@PUInt32Array(Dest)[4], @PUInt32Array(Dest)[4], @PUInt32Array(FAdditionalBuffer)[160]); -end; - -{ TCipher_3Way } - -type - P3Way_Key = ^T3Way_Key; - T3Way_Key = packed record - E_Key: array[0..2] of UInt32; - E_Data: array[0..11] of UInt32; - D_Key: array[0..2] of UInt32; - D_Data: array[0..11] of UInt32; - end; - -class function TCipher_3Way.Context: TCipherContext; -begin - Result.KeySize := 12; - Result.BlockSize := 12; - Result.BufferSize := 12; - Result.AdditionalBufferSize := SizeOf(T3Way_Key); - Result.NeedsAdditionalBufferBackup := False; - Result.MinRounds := 1; - Result.MaxRounds := 1; - Result.CipherType := [ctSymmetric, ctBlock]; -end; - -procedure TCipher_3Way.DoInit(const Key; Size: Integer); - - procedure RANDGenerate(Start: UInt32; var P: array of UInt32); - var - I: Integer; - begin - for I := 0 to 11 do - begin - P[I] := Start; - Start := Start shl 1; - if Start and $10000 <> 0 then - Start := Start xor $11011; - end; - end; - -var - A0, A1, A2: UInt32; - B0, B1, B2: UInt32; - P3WayKey: P3Way_Key; -begin - P3WayKey := P3Way_Key(FAdditionalBuffer); - - Move(Key, P3WayKey.E_Key, Size); - Move(Key, P3WayKey.D_Key, Size); - RANDGenerate($0B0B, P3WayKey.E_Data); - RANDGenerate($B1B1, P3WayKey.D_Data); - A0 := P3WayKey.D_Key[0]; - A1 := P3WayKey.D_Key[1]; - A2 := P3WayKey.D_Key[2]; - B0 := A0 xor A0 shr 16 xor A1 shl 16 xor A1 shr 16 xor A2 shl 16 xor - A1 shr 24 xor A2 shl 8 xor A2 shr 8 xor A0 shl 24 xor - A2 shr 16 xor A0 shl 16 xor A2 shr 24 xor A0 shl 8; - B1 := A1 xor A1 shr 16 xor A2 shl 16 xor A2 shr 16 xor A0 shl 16 xor - A2 shr 24 xor A0 shl 8 xor A0 shr 8 xor A1 shl 24 xor - A0 shr 16 xor A1 shl 16 xor A0 shr 24 xor A1 shl 8; - B2 := A2 xor A2 shr 16 xor A0 shl 16 xor A0 shr 16 xor A1 shl 16 xor - A0 shr 24 xor A1 shl 8 xor A1 shr 8 xor A2 shl 24 xor - A1 shr 16 xor A2 shl 16 xor A1 shr 24 xor A2 shl 8; - P3WayKey.D_Key[2] := ReverseBits(B0); - P3WayKey.D_Key[1] := ReverseBits(B1); - P3WayKey.D_Key[0] := ReverseBits(B2); -end; - -procedure TCipher_3Way.DoEncode(Source, Dest: Pointer; Size: Integer); -var - I: Integer; - A0, A1, A2: UInt32; - B0, B1, B2: UInt32; - K0, K1, K2: UInt32; - E: PUInt32; - P3WayKey: P3Way_Key; -begin - Assert(Size = Context.BlockSize); - P3WayKey := P3Way_Key(FAdditionalBuffer); - - K0 := P3WayKey.E_Key[0]; - K1 := P3WayKey.E_Key[1]; - K2 := P3WayKey.E_Key[2]; - E := @P3WayKey.E_Data; - - A0 := PUInt32Array(Source)[0]; - A1 := PUInt32Array(Source)[1]; - A2 := PUInt32Array(Source)[2]; - for I := 0 to 10 do - begin - A0 := A0 xor K0 xor E^ shl 16; - A1 := A1 xor K1; - A2 := A2 xor K2 xor E^; - Inc(E); - - B0 := A0 xor A0 shr 16 xor A1 shl 16 xor A1 shr 16 xor A2 shl 16 xor - A1 shr 24 xor A2 shl 8 xor A2 shr 8 xor A0 shl 24 xor - A2 shr 16 xor A0 shl 16 xor A2 shr 24 xor A0 shl 8; - B1 := A1 xor A1 shr 16 xor A2 shl 16 xor A2 shr 16 xor A0 shl 16 xor - A2 shr 24 xor A0 shl 8 xor A0 shr 8 xor A1 shl 24 xor - A0 shr 16 xor A1 shl 16 xor A0 shr 24 xor A1 shl 8; - B2 := A2 xor A2 shr 16 xor A0 shl 16 xor A0 shr 16 xor A1 shl 16 xor - A0 shr 24 xor A1 shl 8 xor A1 shr 8 xor A2 shl 24 xor - A1 shr 16 xor A2 shl 16 xor A1 shr 24 xor A2 shl 8; - B0 := B0 shr 10 or B0 shl 22; - B2 := B2 shl 1 or B2 shr 31; - A0 := B0 xor (B1 or not B2); - A1 := B1 xor (B2 or not B0); - A2 := B2 xor (B0 or not B1); - A0 := A0 shl 1 or A0 shr 31; - A2 := A2 shr 10 or A2 shl 22; - end; - A0 := A0 xor K0 xor E^ shl 16; - A1 := A1 xor K1; - A2 := A2 xor K2 xor E^; - PUInt32Array(Dest)[0] := A0 xor A0 shr 16 xor A1 shl 16 xor A1 shr 16 xor A2 shl 16 xor - A1 shr 24 xor A2 shl 8 xor A2 shr 8 xor A0 shl 24 xor - A2 shr 16 xor A0 shl 16 xor A2 shr 24 xor A0 shl 8; - PUInt32Array(Dest)[1] := A1 xor A1 shr 16 xor A2 shl 16 xor A2 shr 16 xor A0 shl 16 xor - A2 shr 24 xor A0 shl 8 xor A0 shr 8 xor A1 shl 24 xor - A0 shr 16 xor A1 shl 16 xor A0 shr 24 xor A1 shl 8; - PUInt32Array(Dest)[2] := A2 xor A2 shr 16 xor A0 shl 16 xor A0 shr 16 xor A1 shl 16 xor - A0 shr 24 xor A1 shl 8 xor A1 shr 8 xor A2 shl 24 xor - A1 shr 16 xor A2 shl 16 xor A1 shr 24 xor A2 shl 8; -end; - -procedure TCipher_3Way.DoDecode(Source, Dest: Pointer; Size: Integer); -var - I: Integer; - A0, A1, A2: UInt32; - B0, B1, B2: UInt32; - K0, K1, K2: UInt32; - E: PUInt32; - P3WayKey: P3Way_Key; -begin - Assert(Size = Context.BlockSize); - P3WayKey := P3Way_Key(FAdditionalBuffer); - - K0 := P3WayKey.D_Key[0]; - K1 := P3WayKey.D_Key[1]; - K2 := P3WayKey.D_Key[2]; - E := @P3WayKey.D_Data; - - A0 := ReverseBits(PUInt32Array(Source)[2]); - A1 := ReverseBits(PUInt32Array(Source)[1]); - A2 := ReverseBits(PUInt32Array(Source)[0]); - for I := 0 to 10 do - begin - A0 := A0 xor K0 xor E^ shl 16; - A1 := A1 xor K1; - A2 := A2 xor K2 xor E^; - Inc(E); - - B0 := A0 xor A0 shr 16 xor A1 shl 16 xor A1 shr 16 xor A2 shl 16 xor - A1 shr 24 xor A2 shl 8 xor A2 shr 8 xor A0 shl 24 xor - A2 shr 16 xor A0 shl 16 xor A2 shr 24 xor A0 shl 8; - B1 := A1 xor A1 shr 16 xor A2 shl 16 xor A2 shr 16 xor A0 shl 16 xor - A2 shr 24 xor A0 shl 8 xor A0 shr 8 xor A1 shl 24 xor - A0 shr 16 xor A1 shl 16 xor A0 shr 24 xor A1 shl 8; - B2 := A2 xor A2 shr 16 xor A0 shl 16 xor A0 shr 16 xor A1 shl 16 xor - A0 shr 24 xor A1 shl 8 xor A1 shr 8 xor A2 shl 24 xor - A1 shr 16 xor A2 shl 16 xor A1 shr 24 xor A2 shl 8; - B0 := B0 shr 10 or B0 shl 22; - B2 := B2 shl 1 or B2 shr 31; - A0 := B0 xor (B1 or not B2); - A1 := B1 xor (B2 or not B0); - A2 := B2 xor (B0 or not B1); - A0 := A0 shl 1 or A0 shr 31; - A2 := A2 shr 10 or A2 shl 22; - end; - A0 := A0 xor K0 xor E^ shl 16; - A1 := A1 xor K1; - A2 := A2 xor K2 xor E^; - B0 := A0 xor A0 shr 16 xor A1 shl 16 xor A1 shr 16 xor A2 shl 16 xor - A1 shr 24 xor A2 shl 8 xor A2 shr 8 xor A0 shl 24 xor - A2 shr 16 xor A0 shl 16 xor A2 shr 24 xor A0 shl 8; - B1 := A1 xor A1 shr 16 xor A2 shl 16 xor A2 shr 16 xor A0 shl 16 xor - A2 shr 24 xor A0 shl 8 xor A0 shr 8 xor A1 shl 24 xor - A0 shr 16 xor A1 shl 16 xor A0 shr 24 xor A1 shl 8; - B2 := A2 xor A2 shr 16 xor A0 shl 16 xor A0 shr 16 xor A1 shl 16 xor - A0 shr 24 xor A1 shl 8 xor A1 shr 8 xor A2 shl 24 xor - A1 shr 16 xor A2 shl 16 xor A1 shr 24 xor A2 shl 8; - - PUInt32Array(Dest)[2] := ReverseBits(B0); - PUInt32Array(Dest)[1] := ReverseBits(B1); - PUInt32Array(Dest)[0] := ReverseBits(B2); -end; - -{ TCipher_Cast128 } - -class function TCipher_Cast128.Context: TCipherContext; -begin - Result.KeySize := 16; - Result.BlockSize := 8; - Result.BufferSize := 8; - Result.AdditionalBufferSize := 128; - Result.NeedsAdditionalBufferBackup := false; - Result.MinRounds := 1; - Result.MaxRounds := 256; - Result.CipherType := [ctSymmetric, ctBlock]; -end; - -procedure TCipher_Cast128.SetRounds(Value: Integer); -begin - if Value <> FRounds then - begin - if not (FState in [csNew, csInitialized, csDone]) then - Done; - if (FState <> csNew) and (Value <= 0) then - Value := 16; - FRounds := Value; - end; -end; - -procedure TCipher_Cast128.DoInit(const Key; Size: Integer); -var - Z, X, T: array[0..3] of UInt32; - K: PUInt32Array; - I: UInt32; -begin - if FRounds <= 0 then - begin - if Size <= 10 then - FRounds := 12 - else - FRounds := 16; - end; - K := FAdditionalBuffer; - FillChar(X, SizeOf(X), 0); - Move(Key, X, Size); - SwapUInt32Buffer(X, X, 4); - I := 0; - while I < 32 do - begin - if I and 4 = 0 then - begin - Z[0] := X[0] xor Cast128_Key[0, X[3] shr 16 and $FF] xor - Cast128_Key[1, X[3] and $FF] xor - Cast128_Key[2, X[3] shr 24] xor - Cast128_Key[3, X[3] shr 8 and $FF] xor - Cast128_Key[2, X[2] shr 24]; - T[0] := Z[0]; - Z[1] := X[2] xor Cast128_Key[0, Z[0] shr 24] xor - Cast128_Key[1, Z[0] shr 8 and $FF] xor - Cast128_Key[2, Z[0] shr 16 and $FF] xor - Cast128_Key[3, Z[0] and $FF] xor - Cast128_Key[3, X[2] shr 8 and $FF]; - T[1] := Z[1]; - Z[2] := X[3] xor Cast128_Key[0, Z[1] and $FF] xor - Cast128_Key[1, Z[1] shr 8 and $FF] xor - Cast128_Key[2, Z[1] shr 16 and $FF] xor - Cast128_Key[3, Z[1] shr 24] xor - Cast128_Key[0, X[2] shr 16 and $FF]; - T[2] := Z[2]; - Z[3] := X[1] xor Cast128_Key[0, Z[2] shr 8 and $FF] xor - Cast128_Key[1, Z[2] shr 16 and $FF] xor - Cast128_Key[2, Z[2] and $FF] xor - Cast128_Key[3, Z[2] shr 24] xor - Cast128_Key[1, X[2] and $FF]; - T[3] := Z[3]; - end - else - begin - X[0] := Z[2] xor Cast128_Key[0, Z[1] shr 16 and $FF] xor - Cast128_Key[1, Z[1] and $FF] xor - Cast128_Key[2, Z[1] shr 24] xor - Cast128_Key[3, Z[1] shr 8 and $FF] xor - Cast128_Key[2, Z[0] shr 24]; - T[0] := X[0]; - X[1] := Z[0] xor Cast128_Key[0, X[0] shr 24] xor - Cast128_Key[1, X[0] shr 8 and $FF] xor - Cast128_Key[2, X[0] shr 16 and $FF] xor - Cast128_Key[3, X[0] and $FF] xor - Cast128_Key[3, Z[0] shr 8 and $FF]; - T[1] := X[1]; - X[2] := Z[1] xor Cast128_Key[0, X[1] and $FF] xor - Cast128_Key[1, X[1] shr 8 and $FF] xor - Cast128_Key[2, X[1] shr 16 and $FF] xor - Cast128_Key[3, X[1] shr 24] xor - Cast128_Key[0, Z[0] shr 16 and $FF]; - T[2] := X[2]; - X[3] := Z[3] xor Cast128_Key[0, X[2] shr 8 and $FF] xor - Cast128_Key[1, X[2] shr 16 and $FF] xor - Cast128_Key[2, X[2] and $FF] xor - Cast128_Key[3, X[2] shr 24] xor - Cast128_Key[1, Z[0] and $FF]; - T[3] := X[3]; - end; - case I and 12 of - 0,12: - begin - K[I + 0] := Cast128_Key[0, T[2] shr 24] xor - Cast128_Key[1, T[2] shr 16 and $FF] xor - Cast128_Key[2, T[1] and $FF] xor - Cast128_Key[3, T[1] shr 8 and $FF]; - K[I + 1] := Cast128_Key[0, T[2] shr 8 and $FF] xor - Cast128_Key[1, T[2] and $FF] xor - Cast128_Key[2, T[1] shr 16 and $FF] xor - Cast128_Key[3, T[1] shr 24]; - K[I + 2] := Cast128_Key[0, T[3] shr 24] xor - Cast128_Key[1, T[3] shr 16 and $FF] xor - Cast128_Key[2, T[0] and $FF] xor - Cast128_Key[3, T[0] shr 8 and $FF]; - K[I + 3] := Cast128_Key[0, T[3] shr 8 and $FF] xor - Cast128_Key[1, T[3] and $FF] xor - Cast128_Key[2, T[0] shr 16 and $FF] xor - Cast128_Key[3, T[0] shr 24]; - end; - 4,8: - begin - K[I + 0] := Cast128_Key[0, T[0] and $FF] xor - Cast128_Key[1, T[0] shr 8 and $FF] xor - Cast128_Key[2, T[3] shr 24] xor - Cast128_Key[3, T[3] shr 16 and $FF]; - K[I + 1] := Cast128_Key[0, T[0] shr 16 and $FF] xor - Cast128_Key[1, T[0] shr 24] xor - Cast128_Key[2, T[3] shr 8 and $FF] xor - Cast128_Key[3, T[3] and $FF]; - K[I + 2] := Cast128_Key[0, T[1] and $FF] xor - Cast128_Key[1, T[1] shr 8 and $FF] xor - Cast128_Key[2, T[2] shr 24] xor - Cast128_Key[3, T[2] shr 16 and $FF]; - K[I + 3] := Cast128_Key[0, T[1] shr 16 and $FF] xor - Cast128_Key[1, T[1] shr 24] xor - Cast128_Key[2, T[2] shr 8 and $FF] xor - Cast128_Key[3, T[2] and $FF]; - end; - end; - case I and 12 of - 0: begin - K[I + 0] := K[I + 0] xor Cast128_Key[0, Z[0] shr 8 and $FF]; - K[I + 1] := K[I + 1] xor Cast128_Key[1, Z[1] shr 8 and $FF]; - K[I + 2] := K[I + 2] xor Cast128_Key[2, Z[2] shr 16 and $FF]; - K[I + 3] := K[I + 3] xor Cast128_Key[3, Z[3] shr 24]; - end; - 4: begin - K[I + 0] := K[I + 0] xor Cast128_Key[0, X[2] shr 24]; - K[I + 1] := K[I + 1] xor Cast128_Key[1, X[3] shr 16 and $FF]; - K[I + 2] := K[I + 2] xor Cast128_Key[2, X[0] and $FF]; - K[I + 3] := K[I + 3] xor Cast128_Key[3, X[1] and $FF]; - end; - 8: begin - K[I + 0] := K[I + 0] xor Cast128_Key[0, Z[2] shr 16 and $FF]; - K[I + 1] := K[I + 1] xor Cast128_Key[1, Z[3] shr 24]; - K[I + 2] := K[I + 2] xor Cast128_Key[2, Z[0] shr 8 and $FF]; - K[I + 3] := K[I + 3] xor Cast128_Key[3, Z[1] shr 8 and $FF]; - end; - 12: begin - K[I + 0] := K[I + 0] xor Cast128_Key[0, X[0] and $FF]; - K[I + 1] := K[I + 1] xor Cast128_Key[1, X[1] and $FF]; - K[I + 2] := K[I + 2] xor Cast128_Key[2, X[2] shr 24]; - K[I + 3] := K[I + 3] xor Cast128_Key[3, X[3] shr 16 and $FF]; - end; - end; - if I >= 16 then - begin - K[I + 0] := K[I + 0] and $1F; - K[I + 1] := K[I + 1] and $1F; - K[I + 2] := K[I + 2] and $1F; - K[I + 3] := K[I + 3] and $1F; - end; - Inc(I, 4); - end; - ProtectBuffer(X, SizeOf(X)); - ProtectBuffer(Z, SizeOf(Z)); - ProtectBuffer(T, SizeOf(T)); -end; - -procedure TCipher_Cast128.DoEncode(Source, Dest: Pointer; Size: Integer); -var - T, I, A, B: UInt32; - K: PUInt32Array; -begin - Assert(Size = Context.BlockSize); - - K := FAdditionalBuffer; - A := SwapUInt32(PUInt32Array(Source)[0]); - B := SwapUInt32(PUInt32Array(Source)[1]); - for I := 0 to 2 do - begin - T := K[0] + B; - T := T shl K[16] or T shr (32 - K[16]); - A := A xor (Cast128_Data[0, T shr 24] xor - Cast128_Data[1, T shr 16 and $FF] - - Cast128_Data[2, T shr 8 and $FF] + - Cast128_Data[3, T and $FF]); - T := K[1] xor A; - T := T shl K[17] or T shr (32 - K[17]); - B := B xor (Cast128_Data[0, T shr 24] - - Cast128_Data[1, T shr 16 and $FF] + - Cast128_Data[2, T shr 8 and $FF] xor - Cast128_Data[3, T and $FF]); - T := K[2] - B; - T := T shl K[18] or T shr (32 - K[18]); - A := A xor (Cast128_Data[0, T shr 24] + - Cast128_Data[1, T shr 16 and $FF] xor - Cast128_Data[2, T shr 8 and $FF] - - Cast128_Data[3, T and $FF]); - T := K[3] + A; - T := T shl K[19] or T shr (32 - K[19]); - B := B xor (Cast128_Data[0, T shr 24] xor - Cast128_Data[1, T shr 16 and $FF] - - Cast128_Data[2, T shr 8 and $FF] + - Cast128_Data[3, T and $FF]); - if I = 2 then - Break; - T := K[4] xor B; - T := T shl K[20] or T shr (32 - K[20]); - A := A xor (Cast128_Data[0, T shr 24] - - Cast128_Data[1, T shr 16 and $FF] + - Cast128_Data[2, T shr 8 and $FF] xor - Cast128_Data[3, T and $FF]); - T := K[5] - A; - T := T shl K[21] or T shr (32 - K[21]); - B := B xor (Cast128_Data[0, T shr 24] + - Cast128_Data[1, T shr 16 and $FF] xor - Cast128_Data[2, T shr 8 and $FF] - - Cast128_Data[3, T and $FF]); - if (I = 1) and (FRounds <= 12) then - Break; - K := @K[6]; - end; - PUInt32Array(Dest)[0] := SwapUInt32(B); - PUInt32Array(Dest)[1] := SwapUInt32(A); -end; - -procedure TCipher_Cast128.DoDecode(Source, Dest: Pointer; Size: Integer); -var - T, I, A, B: UInt32; - K: PUInt32Array; - JumpStart: Boolean; -begin - Assert(Size = Context.BlockSize); - JumpStart := False; - - K := @PUInt32Array(FAdditionalBuffer)[12]; - B := SwapUInt32(PUInt32Array(Source)[0]); - A := SwapUInt32(PUInt32Array(Source)[1]); - I := 2; - - if FRounds <= 12 then - Dec(PUInt32(K), 6) - else - JumpStart := True; - - while I > 0 do - begin - if not JumpStart then - begin - Dec(I); - T := K[5] - A; - T := T shl K[21] or T shr (32 - K[21]); - B := B xor (Cast128_Data[0, T shr 24] + - Cast128_Data[1, T shr 16 and $FF] xor - Cast128_Data[2, T shr 8 and $FF] - - Cast128_Data[3, T and $FF]); - T := K[4] xor B; - T := T shl K[20] or T shr (32 - K[20]); - A := A xor (Cast128_Data[0, T shr 24] - - Cast128_Data[1, T shr 16 and $FF] + - Cast128_Data[2, T shr 8 and $FF] xor - Cast128_Data[3, T and $FF]); - end - else - JumpStart := False; - - T := K[3] + A; - T := T shl K[19] or T shr (32 - K[19]); - B := B xor (Cast128_Data[0, T shr 24] xor - Cast128_Data[1, T shr 16 and $FF] - - Cast128_Data[2, T shr 8 and $FF] + - Cast128_Data[3, T and $FF]); - T := K[2] - B; - T := T shl K[18] or T shr (32 - K[18]); - A := A xor (Cast128_Data[0, T shr 24] + - Cast128_Data[1, T shr 16 and $FF] xor - Cast128_Data[2, T shr 8 and $FF] - - Cast128_Data[3, T and $FF]); - T := K[1] xor A; - T := T shl K[17] or T shr (32 - K[17]); - B := B xor (Cast128_Data[0, T shr 24] - - Cast128_Data[1, T shr 16 and $FF] + - Cast128_Data[2, T shr 8 and $FF] xor - Cast128_Data[3, T and $FF]); - T := K[0] + B; - T := T shl K[16] or T shr (32 - K[16]); - A := A xor (Cast128_Data[0, T shr 24] xor - Cast128_Data[1, T shr 16 and $FF] - - Cast128_Data[2, T shr 8 and $FF] + - Cast128_Data[3, T and $FF]); - Dec(PUInt32(K), 6); - end; - - PUInt32Array(Dest)[0] := SwapUInt32(A); - PUInt32Array(Dest)[1] := SwapUInt32(B); -end; - -{ TCipher_Gost } - -class function TCipher_Gost.Context: TCipherContext; -begin - Result.KeySize := 32; - Result.BlockSize := 8; - Result.BufferSize := 8; - Result.AdditionalBufferSize := 32; - Result.NeedsAdditionalBufferBackup := false; - Result.MinRounds := 1; - Result.MaxRounds := 1; - Result.CipherType := [ctSymmetric, ctBlock]; -end; - -procedure TCipher_Gost.DoInit(const Key; Size: Integer); -begin - Move(Key, FAdditionalBuffer^, Size); -end; - -procedure TCipher_Gost.DoEncode(Source, Dest: Pointer; Size: Integer); -var - I, A, B, T: UInt32; - K: PUInt32Array; -begin - Assert(Size = Context.BlockSize); - - K := FAdditionalBuffer; - A := PUInt32Array(Source)[0]; - B := PUInt32Array(Source)[1]; - - for I := 0 to 11 do - begin - if I and 3 = 0 then - K := FAdditionalBuffer; - T := A + K[0]; - B := B xor Gost_Data[0, T and $FF] xor - Gost_Data[1, T shr 8 and $FF] xor - Gost_Data[2, T shr 16 and $FF] xor - Gost_Data[3, T shr 24 ]; - T := B + K[1]; - A := A xor Gost_Data[0, T and $FF] xor - Gost_Data[1, T shr 8 and $FF] xor - Gost_Data[2, T shr 16 and $FF] xor - Gost_Data[3, T shr 24 ]; - K := @K[2]; - end; - - K := @PUInt32Array(FAdditionalBuffer)[6]; - - for I := 0 to 3 do - begin - T := A + K[1]; - B := B xor Gost_Data[0, T and $FF] xor - Gost_Data[1, T shr 8 and $FF] xor - Gost_Data[2, T shr 16 and $FF] xor - Gost_Data[3, T shr 24 ]; - T := B + K[0]; - A := A xor Gost_Data[0, T and $FF] xor - Gost_Data[1, T shr 8 and $FF] xor - Gost_Data[2, T shr 16 and $FF] xor - Gost_Data[3, T shr 24 ]; - Dec(PUInt32(K), 2); - end; - - PUInt32Array(Dest)[0] := B; - PUInt32Array(Dest)[1] := A; -end; - -procedure TCipher_Gost.DoDecode(Source, Dest: Pointer; Size: Integer); -var - I, A, B, T: UInt32; - K: PUInt32Array; -begin - Assert(Size = Context.BlockSize); - - A := PUInt32Array(Source)[0]; - B := PUInt32Array(Source)[1]; - K := FAdditionalBuffer; - - for I := 0 to 3 do - begin - T := A + K[0]; - B := B xor Gost_Data[0, T and $FF] xor - Gost_Data[1, T shr 8 and $FF] xor - Gost_Data[2, T shr 16 and $FF] xor - Gost_Data[3, T shr 24]; - T := B + K[1]; - A := A xor Gost_Data[0, T and $FF] xor - Gost_Data[1, T shr 8 and $FF] xor - Gost_Data[2, T shr 16 and $FF] xor - Gost_Data[3, T shr 24]; - K := @K[2]; - end; - - for I := 0 to 11 do - begin - if I and 3 = 0 then - K := @PUInt32Array(FAdditionalBuffer)[6]; - T := A + K[1]; - B := B xor Gost_Data[0, T and $FF] xor - Gost_Data[1, T shr 8 and $FF] xor - Gost_Data[2, T shr 16 and $FF] xor - Gost_Data[3, T shr 24]; - T := B + K[0]; - A := A xor Gost_Data[0, T and $FF] xor - Gost_Data[1, T shr 8 and $FF] xor - Gost_Data[2, T shr 16 and $FF] xor - Gost_Data[3, T shr 24]; - Dec(PUInt32(K), 2); - end; - - PUInt32Array(Dest)[0] := B; - PUInt32Array(Dest)[1] := A; -end; - -{ TCipher_Misty } - -class function TCipher_Misty.Context: TCipherContext; -begin - Result.KeySize := 16; - Result.BlockSize := 8; - Result.BufferSize := 8; - Result.AdditionalBufferSize := 128; - Result.NeedsAdditionalBufferBackup := False; - Result.MinRounds := 1; - Result.MaxRounds := 1; - Result.CipherType := [ctSymmetric, ctBlock]; -end; - -function Misty_I(Value, Key: UInt32): UInt32; -begin - Result := Misty_Data9[Value shr 7 and $1FF] xor (Value and $7F); - Value := (Misty_Data7[Value and $7F] xor Result and $7F) xor (Key shr 9 and $7F); - Result := Misty_Data9[Result xor (Key and $1FF)] xor Value or Value shl 9; -end; - -function Misty_O(Value, K: UInt32; Key: PUInt32Array): UInt32; -begin - Result := Misty_I((Value shr 16) xor Key[K], Key[(K + 5) and 7 + 8]) xor (Value and $FFFF); - Value := Misty_I((Value and $FFFF) xor Key[(K + 2) and 7], Key[(K + 1) and 7 + 8]) xor Result; - Result := Misty_I(Result xor Key[(K + 7) and 7], Key[(K + 3) and 7 + 8]) xor Value; - Result := Result or (Value xor Key[(K + 4) and 7]) shl 16; -end; - -function Misty_E(Value, K: UInt32; Key: PUInt32Array): UInt32; -begin - Result := Value shr 16; - Value := Value and $FFFF; - - if K and 1 <> 0 then - begin - K := K shr 1; - Value := Value xor (Result and Key[(K + 2) and 7 + 8]); - Result := Result xor (Value or Key[(K + 4) and 7]); - end - else - begin - K := K shr 1; - Value := Value xor (Result and Key[K]); - Result := Result xor (Value or Key[(K + 6) and 7 + 8]); - end; - - Result:= (Result shl 16) or Value; -end; - -function Misty_D(Value, K: UInt32; Key: PUInt32Array): UInt32; -begin - Result := Value shr 16; - Value := Value and $FFFF; - - if K and 1 <> 0 then - begin - K := K shr 1; - Result := Result xor (Value or Key[(K + 4) and 7]); - Value := Value xor (Result and Key[(K + 2) and 7 + 8]); - end - else - begin - K := K shr 1; - Result := Result xor (Value or Key[(K + 6) and 7 + 8]); - Value := Value xor (Result and Key[K]); - end; - - Result:= (Result shl 16) or Value; -end; - -procedure TCipher_Misty.DoInit(const Key; Size: Integer); -var - K: array[0..15] of Byte; - D: PUInt32Array; - I: Integer; -begin - FillChar(K, SizeOf(K), 0); - Move(Key, K, Size); - D := FAdditionalBuffer; - - for I := 0 to 7 do - D[I] := K[I * 2] * 256 + K[I * 2 + 1]; - - for I := 0 to 7 do - begin - D[I + 8] := Misty_I(D[I], D[(I + 1) and 7]); - D[I + 16] := D[I + 8] and $1FF; - D[I + 24] := D[I + 8] shr 9; - end; - - ProtectBuffer(K, SizeOf(K)); -end; - -procedure TCipher_Misty.DoEncode(Source, Dest: Pointer; Size: Integer); -var - A, B: UInt32; -begin - Assert(Size = Context.BlockSize); - - A := PUInt32Array(Source)[0]; - B := PUInt32Array(Source)[1]; - A := Misty_E(A, 0, FAdditionalBuffer); - B := Misty_E(B, 1, FAdditionalBuffer) xor Misty_O(A, 0, FAdditionalBuffer); - A := A xor Misty_O(B, 1, FAdditionalBuffer); - A := Misty_E(A, 2, FAdditionalBuffer); - B := Misty_E(B, 3, FAdditionalBuffer) xor Misty_O(A, 2, FAdditionalBuffer); - A := A xor Misty_O(B, 3, FAdditionalBuffer); - A := Misty_E(A, 4, FAdditionalBuffer); - B := Misty_E(B, 5, FAdditionalBuffer) xor Misty_O(A, 4, FAdditionalBuffer); - A := A xor Misty_O(B, 5, FAdditionalBuffer); - A := Misty_E(A, 6, FAdditionalBuffer); - B := Misty_E(B, 7, FAdditionalBuffer) xor Misty_O(A, 6, FAdditionalBuffer); - A := A xor Misty_O(B, 7, FAdditionalBuffer); - - PUInt32Array(Dest)[0] := Misty_E(B, 9, FAdditionalBuffer); - PUInt32Array(Dest)[1] := Misty_E(A, 8, FAdditionalBuffer); -end; - -procedure TCipher_Misty.DoDecode(Source, Dest: Pointer; Size: Integer); -var - A, B: UInt32; -begin - Assert(Size = Context.BlockSize); - - B := Misty_D(PUInt32Array(Source)[0], 9, FAdditionalBuffer); - A := Misty_D(PUInt32Array(Source)[1], 8, FAdditionalBuffer); - A := A xor Misty_O(B, 7, FAdditionalBuffer); - B := Misty_D(B xor Misty_O(A, 6, FAdditionalBuffer), 7, FAdditionalBuffer); - A := Misty_D(A, 6, FAdditionalBuffer); - A := A xor Misty_O(B, 5, FAdditionalBuffer); - B := Misty_D(B xor Misty_O(A, 4, FAdditionalBuffer), 5, FAdditionalBuffer); - A := Misty_D(A, 4, FAdditionalBuffer); - A := A xor Misty_O(B, 3, FAdditionalBuffer); - B := Misty_D(B xor Misty_O(A, 2, FAdditionalBuffer), 3, FAdditionalBuffer); - A := Misty_D(A, 2, FAdditionalBuffer); - A := A xor Misty_O(B, 1, FAdditionalBuffer); - - PUInt32Array(Dest)[0] := Misty_D(A, 0, FAdditionalBuffer); - PUInt32Array(Dest)[1] := Misty_D(B xor Misty_O(A, 0, FAdditionalBuffer), 1, FAdditionalBuffer); -end; - -{ TCipher_NewDES } - -procedure NewDES_Func(Source, Dest, Key: PByteArray); -var - I: Integer; - A, B, C, D, E, F, G, H: Byte; -begin - A := Source[0]; - B := Source[1]; - C := Source[2]; - D := Source[3]; - E := Source[4]; - F := Source[5]; - G := Source[6]; - H := Source[7]; - - for I := 0 to 7 do - begin - E := E xor NewDES_Data[A xor Key[0]]; - F := F xor NewDES_Data[B xor Key[1]]; - G := G xor NewDES_Data[C xor Key[2]]; - H := H xor NewDES_Data[D xor Key[3]]; - B := B xor NewDES_Data[E xor Key[4]]; - C := C xor NewDES_Data[F xor E]; - D := D xor NewDES_Data[G xor Key[5]]; - A := A xor NewDES_Data[H xor Key[6]]; - Key := @Key[7]; - end; - - E := E xor NewDES_Data[A xor Key[0]]; - F := F xor NewDES_Data[B xor Key[1]]; - G := G xor NewDES_Data[C xor Key[2]]; - H := H xor NewDES_Data[D xor Key[3]]; - - Dest[0] := A; - Dest[1] := B; - Dest[2] := C; - Dest[3] := D; - Dest[4] := E; - Dest[5] := F; - Dest[6] := G; - Dest[7] := H; -end; - -class function TCipher_NewDES.Context: TCipherContext; -begin - Result.KeySize := 15; - Result.BlockSize := 8; - Result.BufferSize := 8; - Result.AdditionalBufferSize := 60 * 2; - Result.NeedsAdditionalBufferBackup := true; - Result.MinRounds := 1; - Result.MaxRounds := 1; - Result.CipherType := [ctSymmetric, ctBlock]; -end; - -procedure TCipher_NewDES.DoInit(const Key; Size: Integer); -var - K: array[0..14] of Byte; - E: PByteArray; - I: Integer; -begin - FillChar(K, SizeOf(K), 0); - Move(Key, K, Size); - E := FAdditionalBuffer; - Move(K, E[ 0], 15); - Move(K, E[15], 15); - Move(K, E[30], 15); - Move(K, E[45], 15); - E := @E[60]; - I := 11; - - repeat - E[0] := K[I]; I := (I + 1) mod 15; - E[1] := K[I]; I := (I + 1) mod 15; - E[2] := K[I]; I := (I + 1) mod 15; - E[3] := K[I]; I := (I + 9) mod 15; - if I = 12 then - Break; - E[4] := K[I]; Inc(I); - E[5] := K[I]; Inc(I); - E[6] := K[I]; I := (I + 9) mod 15; - E := @E[7]; - until False; - - ProtectBuffer(K, SizeOf(K)); -end; - -procedure TCipher_NewDES.DoEncode(Source, Dest: Pointer; Size: Integer); -begin - Assert(Size = Context.BlockSize); - NewDES_Func(Source, Dest, FAdditionalBuffer); -end; - -procedure TCipher_NewDES.DoDecode(Source, Dest: Pointer; Size: Integer); -begin - Assert(Size = Context.BlockSize); - NewDES_Func(Source, Dest, @PByteArray(FAdditionalBuffer)[60]); -end; - -{ TCipher_Q128 } - -class function TCipher_Q128.Context: TCipherContext; -begin - Result.KeySize := 16; - Result.BlockSize := 16; - Result.BufferSize := 16; - Result.AdditionalBufferSize := 256; - Result.NeedsAdditionalBufferBackup := false; - Result.MinRounds := 1; - Result.MaxRounds := 1; - Result.CipherType := [ctSymmetric, ctBlock]; -end; - -procedure TCipher_Q128.DoInit(const Key; Size: Integer); -var - K: array[0..3] of UInt32; - D: PUInt32Array; - I: Integer; -begin - FillChar(K, SizeOf(K), 0); - Move(Key, K, Size); - D := FAdditionalBuffer; - - for I := 19 downto 1 do - begin - K[1] := K[1] xor Q128_Data[K[0] and $03FF]; K[0] := K[0] shr 10 or K[0] shl 22; - K[2] := K[2] xor Q128_Data[K[1] and $03FF]; K[1] := K[1] shr 10 or K[1] shl 22; - K[3] := K[3] xor Q128_Data[K[2] and $03FF]; K[2] := K[2] shr 10 or K[2] shl 22; - K[0] := K[0] xor Q128_Data[K[3] and $03FF]; K[3] := K[3] shr 10 or K[3] shl 22; - if I <= 16 then - begin - D[0] := K[0]; - D[1] := K[1]; - D[2] := K[2]; - D[3] := K[3]; - D := @D[4]; - end; - end; - - ProtectBuffer(K, SizeOf(K)); -end; - -procedure TCipher_Q128.DoEncode(Source, Dest: Pointer; Size: Integer); -{$IFDEF X86ASM} -asm - PUSH ESI - PUSH EDI - PUSH EBX - PUSH EBP - PUSH ECX - MOV EDI,[EAX].TCipher_Q128.FAdditionalBuffer - MOV EAX,[EDX + 0] // B0 - MOV EBX,[EDX + 4] // B1 - MOV ECX,[EDX + 8] // B2 - MOV EDX,[EDX + 12] // B3 - MOV EBP,16 -@@1: MOV ESI,EAX - ROL ESI,10 - AND EAX,03FFh - MOV EAX,[EAX * 4 + OFFSET Q128_DATA] - ADD EAX,[EDI + 0] - XOR EAX,EBX - MOV EBX,EAX - ROL EBX,10 - AND EAX,03FFh - MOV EAX,[EAX * 4 + OFFSET Q128_DATA] - ADD EAX,[EDI + 4] - XOR EAX,ECX - MOV ECX,EAX - ROL ECX,10 - AND EAX,03FFh - MOV EAX,[EAX * 4 + OFFSET Q128_DATA] - ADD EAX,[EDI + 8] - XOR EAX,EDX - MOV EDX,EAX - ROL EDX,10 - AND EAX,03FFh - MOV EAX,[EAX * 4 + OFFSET Q128_DATA] - ADD EAX,[EDI + 12] - XOR EAX,ESI - DEC EBP - LEA EDI,[EDI + 16] - JNZ @@1 - POP ESI - MOV [ESI + 0],EAX // B0 - MOV [ESI + 4],EBX // B1 - MOV [ESI + 8],ECX // B2 - MOV [ESI + 12],EDX // B3 - POP EBP - POP EBX - POP EDI - POP ESI -end; -{$ELSE !X86ASM} -var - D: PUInt32Array; - B0, B1, B2, B3, I: UInt32; -begin - Assert(Size = Context.BlockSize); - - D := Pointer(FAdditionalBuffer); - B0 := PUInt32Array(Source)[0]; - B1 := PUInt32Array(Source)[1]; - B2 := PUInt32Array(Source)[2]; - B3 := PUInt32Array(Source)[3]; - for I := 0 to 15 do - begin - B1 := B1 xor (Q128_Data[B0 and $03FF] + D[0]); B0 := B0 shl 10 or B0 shr 22; - B2 := B2 xor (Q128_Data[B1 and $03FF] + D[1]); B1 := B1 shl 10 or B1 shr 22; - B3 := B3 xor (Q128_Data[B2 and $03FF] + D[2]); B2 := B2 shl 10 or B2 shr 22; - B0 := B0 xor (Q128_Data[B3 and $03FF] + D[3]); B3 := B3 shl 10 or B3 shr 22; - D := @D[4]; - end; - PUInt32Array(Dest)[0] := B0; - PUInt32Array(Dest)[1] := B1; - PUInt32Array(Dest)[2] := B2; - PUInt32Array(Dest)[3] := B3; -end; -{$ENDIF !X86ASM} - -procedure TCipher_Q128.DoDecode(Source, Dest: Pointer; Size: Integer); -{$IFDEF X86ASM} -asm - PUSH ESI - PUSH EDI - PUSH EBX - PUSH EBP - PUSH ECX - MOV EDI,[EAX].TCipher_Q128.FAdditionalBuffer - LEA EDI,[EDI + 64 * 4] - MOV ESI,[EDX + 0] // B0 - MOV EBX,[EDX + 4] // B1 - MOV ECX,[EDX + 8] // B2 - MOV EDX,[EDX + 12] // B3 - MOV EBP,16 -@@1: SUB EDI,16 - ROR EDX,10 - MOV EAX,EDX - AND EAX,03FFh - MOV EAX,[EAX * 4 + OFFSET Q128_DATA] - ADD EAX,[EDI + 12] - XOR ESI,EAX - ROR ECX,10 - MOV EAX,ECX - AND EAX,03FFh - MOV EAX,[EAX * 4 + OFFSET Q128_DATA] - ADD EAX,[EDI + 8] - XOR EDX,EAX - ROR EBX,10 - MOV EAX,EBX - AND EAX,03FFh - MOV EAX,[EAX * 4 + OFFSET Q128_DATA] - ADD EAX,[EDI + 4] - XOR ECX,EAX - ROR ESI,10 - MOV EAX,ESI - AND EAX,03FFh - MOV EAX,[EAX * 4 + OFFSET Q128_DATA] - ADD EAX,[EDI] - XOR EBX,EAX - DEC EBP - JNZ @@1 - POP EAX - MOV [EAX + 0],ESI // B0 - MOV [EAX + 4],EBX // B1 - MOV [EAX + 8],ECX // B2 - MOV [EAX + 12],EDX // B3 - POP EBP - POP EBX - POP EDI - POP ESI -end; -{$ELSE !X86ASM} -var - D: PUInt32Array; - B0, B1, B2, B3, I: UInt32; -begin - Assert(Size = Context.BlockSize); - - D := @PUInt32Array(FAdditionalBuffer)[60]; - B0 := PUInt32Array(Source)[0]; - B1 := PUInt32Array(Source)[1]; - B2 := PUInt32Array(Source)[2]; - B3 := PUInt32Array(Source)[3]; - for I := 0 to 15 do - begin - B3 := B3 shr 10 or B3 shl 22; B0 := B0 xor (Q128_Data[B3 and $03FF] + D[3]); - B2 := B2 shr 10 or B2 shl 22; B3 := B3 xor (Q128_Data[B2 and $03FF] + D[2]); - B1 := B1 shr 10 or B1 shl 22; B2 := B2 xor (Q128_Data[B1 and $03FF] + D[1]); - B0 := B0 shr 10 or B0 shl 22; B1 := B1 xor (Q128_Data[B0 and $03FF] + D[0]); - Dec(PUInt32(D), 4); - end; - PUInt32Array(Dest)[0] := B0; - PUInt32Array(Dest)[1] := B1; - PUInt32Array(Dest)[2] := B2; - PUInt32Array(Dest)[3] := B3; -end; -{$ENDIF !X86ASM} - -{ TCipher_RC2 } - -class function TCipher_RC2.Context: TCipherContext; -begin - Result.KeySize := 128; - Result.BlockSize := 8; - Result.BufferSize := 8; - Result.AdditionalBufferSize := 128; - Result.NeedsAdditionalBufferBackup := False; - Result.MinRounds := 1; - Result.MaxRounds := 1; - Result.CipherType := [ctSymmetric, ctBlock]; -end; - -procedure TCipher_RC2.DoInit(const Key; Size: Integer); -// New keyscheduling according to RFC2268 and it's testcases. The V3 keysetup -// was using an older, inferior version. Special thanks to Brendan Bosnan for -// pointing that out. -var - I, L, Mask, KeyEffectiveBits: Integer; - K: PByteArray; -begin - if Size <= 0 then - Exit; - KeyEffectiveBits := Size * 8; - L := KeyEffectiveBits and 7; - if L = 0 then - Mask := $FF - else - Mask := $FF shr (8 - L); - L := (KeyEffectiveBits + 7) shr 3; - K := FAdditionalBuffer; - Move(Key, K[0], Size); - for I := Size to 127 do - K[I] := RC2_Data[(K[I - Size] + K[I - 1]) and $FF]; - K[128 - L] := RC2_Data[K[128 - L] and Mask]; - for I := 127 - L downto 0 do - K[I] := RC2_Data[K[I + 1] xor K[I + L]]; -end; - -procedure TCipher_RC2.DoEncode(Source, Dest: Pointer; Size: Integer); -var - I: Integer; - K: PWordArray; - A, B, C, D: Word; -begin - Assert(Size = Context.BlockSize); - - K := FAdditionalBuffer; - A := PWordArray(Source)[0]; - B := PWordArray(Source)[1]; - C := PWordArray(Source)[2]; - D := PWordArray(Source)[3]; - for I := 0 to 15 do - begin - Inc(A, (B and not D) + (C and D) + K[I * 4 + 0]); A := A shl 1 or A shr 15; - Inc(B, (C and not A) + (D and A) + K[I * 4 + 1]); B := B shl 2 or B shr 14; - Inc(C, (D and not B) + (A and B) + K[I * 4 + 2]); C := C shl 3 or C shr 13; - Inc(D, (A and not C) + (B and C) + K[I * 4 + 3]); D := D shl 5 or D shr 11; - if I in [4, 10] then - begin - Inc(A, K[D and $3F]); - Inc(B, K[A and $3F]); - Inc(C, K[B and $3F]); - Inc(D, K[C and $3F]); - end; - end; - PWordArray(Dest)[0] := A; - PWordArray(Dest)[1] := B; - PWordArray(Dest)[2] := C; - PWordArray(Dest)[3] := D; -end; - -procedure TCipher_RC2.DoDecode(Source, Dest: Pointer; Size: Integer); -var - I: Integer; - K: PWordArray; - A, B, C, D: Word; -begin - Assert(Size = Context.BlockSize); - - K := FAdditionalBuffer; - A := PWordArray(Source)[0]; - B := PWordArray(Source)[1]; - C := PWordArray(Source)[2]; - D := PWordArray(Source)[3]; - for I := 15 downto 0 do - begin - D := D shr 5 or D shl 11 - (A and not C) - (B and C) - K[I * 4 + 3]; - C := C shr 3 or C shl 13 - (D and not B) - (A and B) - K[I * 4 + 2]; - B := B shr 2 or B shl 14 - (C and not A) - (D and A) - K[I * 4 + 1]; - A := A shr 1 or A shl 15 - (B and not D) - (C and D) - K[I * 4 + 0]; - if I in [5, 11] then - begin - Dec(D, K[C and $3F]); - Dec(C, K[B and $3F]); - Dec(B, K[A and $3F]); - Dec(A, K[D and $3F]); - end; - end; - PWordArray(Dest)[0] := A; - PWordArray(Dest)[1] := B; - PWordArray(Dest)[2] := C; - PWordArray(Dest)[3] := D; -end; - -{ TCipher_RC5 } - -class function TCipher_RC5.Context: TCipherContext; -begin - Result.KeySize := 256; - Result.BlockSize := 8; - Result.BufferSize := 8; - Result.AdditionalBufferSize := 136; - Result.NeedsAdditionalBufferBackup := false; - Result.MinRounds := 1; - Result.MaxRounds := 256; - Result.CipherType := [ctSymmetric, ctBlock]; -end; - -procedure TCipher_RC5.SetRounds(Value: Integer); -begin - if Value <> FRounds then - begin - if not (FState in [csNew, csInitialized, csDone]) then - Done; - if Value <= 0 then - Value := 12; - FRounds := Value; - end; -end; - -procedure TCipher_RC5.DoInit(const Key; Size: Integer); -var - K: array[0..63] of UInt32; - L, Z, I, J: Integer; - D: PUInt32Array; - A, B, T: UInt32; -begin - if FRounds <= 0 then - FRounds := 12; - FillChar(K, SizeOf(K), 0); - Move(Key, K, Size); - D := FAdditionalBuffer; - L := (Size + 3) shr 2; - if L <= 0 then - L := 1; - T := $B7E15163; - for I := 0 to (FRounds + 1) * 2 do - begin - D[I] := T; - Inc(T, $9E3779B9); - end; - if L > (FRounds + 1) * 2 then - Z := L * 3 - else - Z := (FRounds + 1) * 6; - I := 0; - J := 0; - A := 0; - B := 0; - for Z := Z downto 1 do - begin - A := D[I] + A + B; - A := A shl 3 or A shr 29; - D[I] := A; - T := A + B; - B := K[J] + T; - B := B shl T or B shr (32 - T); - K[J] := B; - I := (I + 1) mod ((FRounds + 1) * 2); - J := (J + 1) mod L; - end; - ProtectBuffer(K, SizeOf(K)); -end; - -procedure TCipher_RC5.DoEncode(Source, Dest: Pointer; Size: Integer); -var - K: PUInt32Array; - I: Integer; - A, B: UInt32; -begin - Assert(Size = Context.BlockSize); - - K := FAdditionalBuffer; - A := PUInt32Array(Source)[0] + K[0]; - B := PUInt32Array(Source)[1] + K[1]; - for I := 1 to FRounds do - begin - A := A xor B; A := A shl B or A shr (32 - B) + K[I * 2 + 0]; - B := B xor A; B := B shl A or B shr (32 - A) + K[I * 2 + 1]; - end; - PUInt32Array(Dest)[0] := A; - PUInt32Array(Dest)[1] := B; -end; - -procedure TCipher_RC5.DoDecode(Source, Dest: Pointer; Size: Integer); -var - K: PUInt32Array; - I: Integer; - A, B: UInt32; -begin - Assert(Size = Context.BlockSize); - - K := @PUInt32Array(FAdditionalBuffer)[0]; - A := PUInt32Array(Source)[0]; - B := PUInt32Array(Source)[1]; - for I := FRounds downto 1 do - begin - B := B - K[I * 2 + 1]; B := B shr A or B shl (32 - A) xor A; - A := A - K[I * 2 + 0]; A := A shr B or A shl (32 - B) xor B; - end; - PUInt32Array(Dest)[0] := A - K[0]; - PUInt32Array(Dest)[1] := B - K[1]; -end; - -{ TCipher_SAFER } - -class function TCipher_SAFER.Context: TCipherContext; -begin - Result.KeySize := 16; - Result.BlockSize := 8; - Result.BufferSize := 8; - Result.AdditionalBufferSize := 768; - Result.NeedsAdditionalBufferBackup := false; - Result.MinRounds := 4; - Result.MaxRounds := 13; - Result.CipherType := [ctSymmetric, ctBlock]; -end; - -procedure TCipher_SAFER.SetRounds(Value: Integer); -begin - if not (FState in [csNew, csInitialized, csDone]) then - Done; - if (Value < 4) or (Value > 13) then - case FVersion of // Default Rounds - svK40, svSK40: Value := 5; - svK64, svSK64: Value := 6; - svK128, svSK128: Value := 10; - else - Value := 8; - end; - FRounds := Value; -end; - -procedure TCipher_SAFER.SetVersion(Value: TSAFERVersion); -begin - if Value <> FVersion then - begin - if not (FState in [csNew, csInitialized, csDone]) then - Done; - FVersion := Value; - SetRounds(0); - end; -end; - -procedure TCipher_SAFER.DoInit(const Key; Size: Integer); - - procedure InitTab; - var - I, E: Integer; - Exp: PByteArray; - Log: PByteArray; - begin - Exp := FAdditionalBuffer; - Log := @Exp[256]; - E := 1; - for I := 0 to 255 do - begin - Exp[I] := E and $FF; - Log[E and $FF] := I; - E := (E * 45) mod 257; - end; - end; - - procedure InitKey; - var - D: PByte; - Exp: PByteArray; - Strong: Boolean; - K: array[Boolean, 0..8] of Byte; - I, J: Integer; - begin - Strong := FVersion in [svSK40, svSK64, svSK128]; - Exp := FAdditionalBuffer; - D := @Exp[512]; - FillChar(K, SizeOf(K), 0); - // Setup Key A - I := Size; - if I > 8 then - I := 8; - Move(Key, K[False], I); - // Setup the Key for K-40, SK-40 - if FVersion in [svK40, svSK40] then - begin - K[False, 5] := K[False, 0] xor K[False, 2] xor 129; - K[False, 6] := K[False, 0] xor K[False, 3] xor K[False, 4] xor 66; - K[False, 7] := K[False, 1] xor K[False, 2] xor K[False, 4] xor 36; - K[False, 8] := K[False, 1] xor K[False, 3] xor 24; - Move(K[False], K[True], SizeOf(K[False])); - end - else - begin - if Size > 8 then - begin - I := Size - 8; - if I > 8 then - I := 8; - Move(TByteArray(Key)[8], K[True], I); - end - else - Move(K[False], K[True], 9); - for I := 0 to 7 do - begin - K[False, 8] := K[False, 8] xor K[False, I]; - K[True, 8] := K[True, 8] xor K[True, I]; - end; - end; - // Setup the KeyData - Move(K[True], D^, 8); - Inc(D, 8); - - for I := 0 to 8 do - K[False, I] := K[False, I] shr 3 or K[False, I] shl 5; - - for I := 1 to FRounds do - begin - for J := 0 to 8 do - begin - K[False, J] := K[False, J] shl 6 or K[False, J] shr 2; - K[True, J] := K[True, J] shl 6 or K[True, J] shr 2; - end; - for J := 0 to 7 do - begin - if Strong then - D^ := K[False, (J + I * 2 - 1) mod 9] + Exp[Exp[18 * I + J + 1]] - else - D^ := K[False, J] + Exp[Exp[18 * I + J + 1]]; - Inc(D); - end; - for J := 0 to 7 do - begin - if Strong then - D^ := K[True, (J + I * 2) mod 9] + Exp[Exp[18 * I + J + 10]] - else - D^ := K[True, J] + Exp[Exp[18 * I + J + 10]]; - Inc(D); - end; - end; - ProtectBuffer(K, SizeOf(K)); - end; - -begin - if (FRounds < 4) or (FRounds > 13) then - case FVersion of - svK40, svSK40: FRounds := 5; - svK64, svSK64: FRounds := 6; - svK128, svSK128: FRounds := 10; - else - FRounds := 8; - end; - InitTab; - InitKey; -end; - -procedure TCipher_SAFER.DoEncode(Source, Dest: Pointer; Size: Integer); -var - Exp, Log, Key: PByteArray; - I: Integer; - A, B, C, D, E, F, G, H, T: Byte; -begin - Assert(Size = Context.BlockSize); - - Exp := FAdditionalBuffer; - Log := @Exp[256]; - Key := @Exp[512]; - - A := PByteArray(Source)[0]; - B := PByteArray(Source)[1]; - C := PByteArray(Source)[2]; - D := PByteArray(Source)[3]; - E := PByteArray(Source)[4]; - F := PByteArray(Source)[5]; - G := PByteArray(Source)[6]; - H := PByteArray(Source)[7]; - - for I := 0 to FRounds - 1 do - begin - A := A xor Key[0]; - B := B + Key[1]; - C := C + Key[2]; - D := D xor Key[3]; - E := E xor Key[4]; - F := F + Key[5]; - G := G + Key[6]; - H := H xor Key[7]; - - A := Exp[A] + Key[8]; - B := Log[B] xor Key[9]; - C := Log[C] xor Key[10]; - D := Exp[D] + Key[11]; - E := Exp[E] + Key[12]; - F := Log[F] xor Key[13]; - G := Log[G] xor Key[14]; - H := Exp[H] + Key[15]; - Inc(B, A); Inc(A, B); - Inc(D, C); Inc(C, D); - Inc(F, E); Inc(E, F); - Inc(H, G); Inc(G, H); - Inc(C, A); Inc(A, C); - Inc(G, E); Inc(E, G); - Inc(D, B); Inc(B, D); - Inc(H, F); Inc(F, H); - Inc(E, A); Inc(A, E); - Inc(F, B); Inc(B, F); - Inc(G, C); Inc(C, G); - Inc(H, D); Inc(D, H); - T := B; B := E; E := C; C := T; - T := D; D := F; F := G; G := T; - Key := @Key[16]; - end; - - PByteArray(Dest)[0] := A xor Key[0]; - PByteArray(Dest)[1] := B + Key[1]; - PByteArray(Dest)[2] := C + Key[2]; - PByteArray(Dest)[3] := D xor Key[3]; - PByteArray(Dest)[4] := E xor Key[4]; - PByteArray(Dest)[5] := F + Key[5]; - PByteArray(Dest)[6] := G + Key[6]; - PByteArray(Dest)[7] := H xor Key[7]; -end; - -procedure TCipher_SAFER.DoDecode(Source, Dest: Pointer; Size: Integer); -var - Exp, Log, Key: PByteArray; - I: Integer; - A, B, C, D, E, F, G, H, T: Byte; -begin - Assert(Size = Context.BlockSize); - - Exp := FAdditionalBuffer; - Log := @Exp[256]; - Key := @Exp[504 + 8 * (FRounds * 2 + 1)]; - - A := PByteArray(Source)[0] xor Key[0]; - B := PByteArray(Source)[1] - Key[1]; - C := PByteArray(Source)[2] - Key[2]; - D := PByteArray(Source)[3] xor Key[3]; - E := PByteArray(Source)[4] xor Key[4]; - F := PByteArray(Source)[5] - Key[5]; - G := PByteArray(Source)[6] - Key[6]; - H := PByteArray(Source)[7] xor Key[7]; - - for I := 0 to FRounds - 1 do - begin - Dec(PByte(Key), 16); - T := E; E := B; B := C; C := T; - T := F; F := D; D := G; G := T; - Dec(A, E); Dec(E, A); - Dec(B, F); Dec(F, B); - Dec(C, G); Dec(G, C); - Dec(D, H); Dec(H, D); - Dec(A, C); Dec(C, A); - Dec(E, G); Dec(G, E); - Dec(B, D); Dec(D, B); - Dec(F, H); Dec(H, F); - Dec(A, B); Dec(B, A); - Dec(C, D); Dec(D, C); - Dec(E, F); Dec(F, E); - Dec(G, H); Dec(H, G); - H := H - Key[15]; - G := G xor Key[14]; - F := F xor Key[13]; - E := E - Key[12]; - D := D - Key[11]; - C := C xor Key[10]; - B := B xor Key[9]; - A := A - Key[8]; - H := Log[H] xor Key[7]; - G := Exp[G] - Key[6]; - F := Exp[F] - Key[5]; - E := Log[E] xor Key[4]; - D := Log[D] xor Key[3]; - C := Exp[C] - Key[2]; - B := Exp[B] - Key[1]; - A := Log[A] xor Key[0]; - end; - - PByteArray(Dest)[0] := A; - PByteArray(Dest)[1] := B; - PByteArray(Dest)[2] := C; - PByteArray(Dest)[3] := D; - PByteArray(Dest)[4] := E; - PByteArray(Dest)[5] := F; - PByteArray(Dest)[6] := G; - PByteArray(Dest)[7] := H; -end; - -{ TCipher_SharkBase } - -{$IFNDEF CPU64BITS} -function TCipher_SharkBase.Shark(D: TLong64; K: PLong64): TLong64; -var - R, T: Integer; -begin - for R := 0 to 4 do - begin - D.L := D.L xor K.L; - D.R := D.R xor K.R; - Inc(K); - T := Shark_CE[0, D.R shr 23 and $1FE] xor - Shark_CE[1, D.R shr 15 and $1FE] xor - Shark_CE[2, D.R shr 7 and $1FE] xor - Shark_CE[3, D.R shl 1 and $1FE] xor - Shark_CE[4, D.L shr 23 and $1FE] xor - Shark_CE[5, D.L shr 15 and $1FE] xor - Shark_CE[6, D.L shr 7 and $1FE] xor - Shark_CE[7, D.L shl 1 and $1FE]; - - D.R := Shark_CE[0, D.R shr 23 and $1FE or 1] xor - Shark_CE[1, D.R shr 15 and $1FE or 1] xor - Shark_CE[2, D.R shr 7 and $1FE or 1] xor - Shark_CE[3, D.R shl 1 and $1FE or 1] xor - Shark_CE[4, D.L shr 23 and $1FE or 1] xor - Shark_CE[5, D.L shr 15 and $1FE or 1] xor - Shark_CE[6, D.L shr 7 and $1FE or 1] xor - Shark_CE[7, D.L shl 1 and $1FE or 1]; - D.L := T; - end; - D.L := D.L xor K.L; - D.R := D.R xor K.R; - Inc(K); - D.L := UInt32(Shark_SE[D.L shr 24 and $FF]) shl 24 xor - UInt32(Shark_SE[D.L shr 16 and $FF]) shl 16 xor - UInt32(Shark_SE[D.L shr 8 and $FF]) shl 8 xor - UInt32(Shark_SE[D.L and $FF]); - D.R := UInt32(Shark_SE[D.R shr 24 and $FF]) shl 24 xor - UInt32(Shark_SE[D.R shr 16 and $FF]) shl 16 xor - UInt32(Shark_SE[D.R shr 8 and $FF]) shl 8 xor - UInt32(Shark_SE[D.R and $FF]); - Result.L := D.L xor K.L; - Result.R := D.R xor K.R; -end; - -function TCipher_SharkBase.Transform(A: TLong64; Log, ALog: TLogArray): TLong64; - function Mul(A, B: Integer): Byte; - begin - Result := ALog[(Log[A] + Log[B]) mod 255]; - end; - -var - I, J: Byte; - K, T: array[0..7] of Byte; -begin - Move(A.R, K[0], 4); - Move(A.L, K[4], 4); - SwapUInt32Buffer(K, K, 2); - for I := 0 to 7 do - begin - T[I] := Mul(Shark_I[I, 0], K[0]); - for J := 1 to 7 do - T[I] := T[I] xor Mul(Shark_I[I, J], K[J]); - end; - Result.L := T[0]; - Result.R := 0; - for I := 1 to 7 do - begin - Result.R := Result.R shl 8 or Result.L shr 24; - Result.L := Result.L shl 8 xor T[I]; - end; -end; -{$ELSE CPU64BITS} -function TCipher_SharkBase.Transform(A: UInt64; Log, ALog: TLogArray): UInt64; -var - I, J: Integer; - K, T: array[0..7] of Byte; - - function Mul(A, B: Byte): Byte; - begin - // GF(256) multiplication via logarithm tables - Result := ALog[(Log[A] + Log[B]) mod 255]; - end; -begin - for I := 0 to 7 do - K[I] := A shr (56 - 8 * i); - for I := 0 to 7 do - begin - T[I] := Mul(Shark_I[I, 0], K[0]); - for J := 1 to 7 do - T[I] := T[I] xor Mul(Shark_I[I, J], K[J]); - end; - Result := T[0]; - for I := 1 to 7 do - Result := (Result shl 8) xor T[I]; -end; -{$ENDIF} - -{ TCipher_Shark } - -class function TCipher_Shark.Context: TCipherContext; -begin - Result.KeySize := 16; - Result.BlockSize := 8; - Result.BufferSize := 8; - Result.AdditionalBufferSize := 112; - Result.NeedsAdditionalBufferBackup := False; - Result.MinRounds := 1; - Result.MaxRounds := 1; - Result.CipherType := [ctSymmetric, ctBlock]; -end; - -{$IFNDEF CPU64BITS} -procedure TCipher_Shark.DoInit(const Key; Size: Integer); -var - Log, ALog: TLogArray; - - procedure InitLog; - var - I, J: Word; - begin - ALog[0] := 1; - for I := 1 to 255 do - begin - J := ALog[I - 1] shl 1; - if J and $100 <> 0 then - J := J xor $01F5; - ALog[I] := J; - end; - Log[0] := 0; - for I := 0 to 254 do - Log[ALog[I]] := I; - end; - -var - T: array[0..6] of TLong64; - A: array[0..6] of TLong64; - K: array[0..15] of Byte; - I, J, R: Byte; - E, D: PLong64Array; - L: TLong64; -begin - FillChar(K, SizeOf(K), 0); - Move(Key, K, Size); - InitLog; - E := FAdditionalBuffer; - D := @E[7]; - Move(Shark_CE[0], T, SizeOf(T)); - T[6] := Transform(T[6], Log, ALog); - I := 0; - for R := 0 to 6 do - begin - A[R].L := K[I and $F]; - A[R].R := 0; - Inc(I); - for J := 1 to 7 do - begin - A[R].R := A[R].R shl 8 or A[R].L shr 24; - A[R].L := A[R].L shl 8 or K[I and $F]; - Inc(I); - end; - end; - L.L := 0; - L.R := 0; - L := Shark(L, @T); - E[0].L := A[0].L xor L.L; - E[0].R := A[0].R xor L.R; - for R := 1 to 6 do - begin - L := Shark(E[R - 1], @T); - E[R].L := A[R].L xor L.L; - E[R].R := A[R].R xor L.R; - end; - E[6] := Transform(E[6], Log, ALog); - D[0] := E[6]; - D[6] := E[0]; - for R := 1 to 5 do - D[R] := Transform(E[6-R], Log, ALog); - ProtectBuffer(T, SizeOf(T)); - ProtectBuffer(A, SizeOf(A)); - ProtectBuffer(K, SizeOf(K)); -end; - -procedure TCipher_Shark.DoEncode(Source, Dest: Pointer; Size: Integer); -var - I: Integer; - T, L, R: UInt32; - K: PUInt32Array; -begin - Assert(Size = Context.BlockSize); - - K := FAdditionalBuffer; - L := PLong64(Source).L; - R := PLong64(Source).R; - for I := 0 to 4 do - begin - L := L xor K[I * 2 + 0]; - R := R xor K[I * 2 + 1]; - T := Shark_CE[0, R shr 23 and $1FE] xor - Shark_CE[1, R shr 15 and $1FE] xor - Shark_CE[2, R shr 7 and $1FE] xor - Shark_CE[3, R shl 1 and $1FE] xor - Shark_CE[4, L shr 23 and $1FE] xor - Shark_CE[5, L shr 15 and $1FE] xor - Shark_CE[6, L shr 7 and $1FE] xor - Shark_CE[7, L shl 1 and $1FE]; - R := Shark_CE[0, R shr 23 and $1FE or 1] xor - Shark_CE[1, R shr 15 and $1FE or 1] xor - Shark_CE[2, R shr 7 and $1FE or 1] xor - Shark_CE[3, R shl 1 and $1FE or 1] xor - Shark_CE[4, L shr 23 and $1FE or 1] xor - Shark_CE[5, L shr 15 and $1FE or 1] xor - Shark_CE[6, L shr 7 and $1FE or 1] xor - Shark_CE[7, L shl 1 and $1FE or 1]; - L := T; - end; - L := L xor K[10]; - R := R xor K[11]; - L := UInt32(Shark_SE[L shr 24 ]) shl 24 xor - UInt32(Shark_SE[L shr 16 and $FF]) shl 16 xor - UInt32(Shark_SE[L shr 8 and $FF]) shl 8 xor - UInt32(Shark_SE[L and $FF]); - R := UInt32(Shark_SE[R shr 24 ]) shl 24 xor - UInt32(Shark_SE[R shr 16 and $FF]) shl 16 xor - UInt32(Shark_SE[R shr 8 and $FF]) shl 8 xor - UInt32(Shark_SE[R and $FF]); - PLong64(Dest).L := L xor K[12]; - PLong64(Dest).R := R xor K[13]; -end; - -procedure TCipher_Shark.DoDecode(Source, Dest: Pointer; Size: Integer); -var - I: Integer; - T, R, L: UInt32; - K: PUInt32Array; -begin - Assert(Size = Context.BlockSize); - - K := @PUInt32Array(FAdditionalBuffer)[14]; - L := PLong64(Source).L; - R := PLong64(Source).R; - for I := 0 to 4 do - begin - L := L xor K[I * 2 + 0]; - R := R xor K[I * 2 + 1]; - T := Shark_CD[0, R shr 23 and $1FE] xor - Shark_CD[1, R shr 15 and $1FE] xor - Shark_CD[2, R shr 7 and $1FE] xor - Shark_CD[3, R shl 1 and $1FE] xor - Shark_CD[4, L shr 23 and $1FE] xor - Shark_CD[5, L shr 15 and $1FE] xor - Shark_CD[6, L shr 7 and $1FE] xor - Shark_CD[7, L shl 1 and $1FE]; - R := Shark_CD[0, R shr 23 and $1FE or 1] xor - Shark_CD[1, R shr 15 and $1FE or 1] xor - Shark_CD[2, R shr 7 and $1FE or 1] xor - Shark_CD[3, R shl 1 and $1FE or 1] xor - Shark_CD[4, L shr 23 and $1FE or 1] xor - Shark_CD[5, L shr 15 and $1FE or 1] xor - Shark_CD[6, L shr 7 and $1FE or 1] xor - Shark_CD[7, L shl 1 and $1FE or 1]; - L := T; - end; - L := L xor K[10]; - R := R xor K[11]; - L := UInt32(Shark_SD[L shr 24 ]) shl 24 xor - UInt32(Shark_SD[L shr 16 and $FF]) shl 16 xor - UInt32(Shark_SD[L shr 8 and $FF]) shl 8 xor - UInt32(Shark_SD[L and $FF]); - R := UInt32(Shark_SD[R shr 24 ]) shl 24 xor - UInt32(Shark_SD[R shr 16 and $FF]) shl 16 xor - UInt32(Shark_SD[R shr 8 and $FF]) shl 8 xor - UInt32(Shark_SD[R and $FF]); - PLong64(Dest).L := L xor K[12]; - PLong64(Dest).R := R xor K[13]; -end; - -{$ELSE CPU64BITS} - -const - SHARK_ROUNDS = 6; - SHARK_ROUNDKEYS = SHARK_ROUNDS + 1; - SHARK_ROOT = $1F5; // GF(256) polynomial x^8 + x^7 + x^6 + x^5 + x^4 + x^2 + 1 - -function SharkEncode(D: UInt64; K: PUInt64): UInt64; -var - R: Integer; -begin - for R := 1 to SHARK_ROUNDS - 1 do - begin - D := D xor K^; - Inc(K); - D := Shark_CE[0, D shr 56 and $FF] xor - Shark_CE[1, D shr 48 and $FF] xor - Shark_CE[2, D shr 40 and $FF] xor - Shark_CE[3, D shr 32 and $FF] xor - Shark_CE[4, D shr 24 and $FF] xor - Shark_CE[5, D shr 16 and $FF] xor - Shark_CE[6, D shr 8 and $FF] xor - Shark_CE[7, D and $FF]; - end; - D := D xor K^; - Inc(K); - D := UInt64(Shark_SE[D shr 56 and $FF]) shl 56 xor - UInt64(Shark_SE[D shr 48 and $FF]) shl 48 xor - UInt64(Shark_SE[D shr 40 and $FF]) shl 40 xor - UInt64(Shark_SE[D shr 32 and $FF]) shl 32 xor - UInt64(Shark_SE[D shr 24 and $FF]) shl 24 xor - UInt64(Shark_SE[D shr 16 and $FF]) shl 16 xor - UInt64(Shark_SE[D shr 8 and $FF]) shl 8 xor - UInt64(Shark_SE[D and $FF]); - Result := D xor K^; -end; - -procedure TCipher_Shark.DoInit(const Key; Size: Integer); -var - Log, ALog: TLogArray; - - procedure InitLog; - var - I, J: Word; - begin - // Generate GF(256) anti-logarithm and logarithm tables - ALog[0] := 1; - for I := 1 to 255 do - begin - J := ALog[I - 1] shl 1; - if J and $100 <> 0 then - J := J xor SHARK_ROOT; - ALog[I] := J; - end; - Log[0] := 0; - for I := 0 to 254 do - Log[ALog[I]] := I; - end; - -var - T: array[0..SHARK_ROUNDS] of UInt64; - A: array[0..SHARK_ROUNDKEYS-1] of UInt64; - K: array[0..15] of Byte; - I, J, R: Integer; - E, D: PUInt64Array; -begin - FillChar(K, SizeOf(K), 0); - Move(Key, K, Size); - InitLog; - E := FAdditionalBuffer; // encryption round key - D := @E[SHARK_ROUNDS + 1]; // decryption round key - - Move(Shark_CE[0], T, SizeOf(T)); - T[SHARK_ROUNDS] := Transform(T[SHARK_ROUNDS], Log, ALog); - - I := 0; - for R := 0 to High(A) do - begin - A[R] := K[I and $F]; - Inc(I); - for J := 1 to 7 do - begin - A[R] := A[R] shl 8 or K[I and $F]; - Inc(I); - end; - end; - - E[0] := A[0] xor SharkEncode(0, @T); - for R := 1 to High(A) do - E[R] := A[R] xor SharkEncode(E[R - 1], @T); - - E[SHARK_ROUNDS] := Transform(E[SHARK_ROUNDS], Log, ALog); - D[0] := E[SHARK_ROUNDS]; - D[SHARK_ROUNDS] := E[0]; - for R := 1 to SHARK_ROUNDS - 1 do - D[R] := Transform(E[SHARK_ROUNDS - R], Log, ALog); - - ProtectBuffer(T, SizeOf(T)); - ProtectBuffer(A, SizeOf(A)); - ProtectBuffer(K, SizeOf(K)); -end; - -procedure TCipher_Shark.DoEncode(Source, Dest: Pointer; Size: Integer); -begin - Assert(Size = Context.BufferSize); - - PUInt64(Dest)^ := SharkEncode(PUInt64(Source)^, FAdditionalBuffer); -end; - -procedure TCipher_Shark.DoDecode(Source, Dest: Pointer; Size: Integer); -var - R: Integer; - D: UInt64; - K: PUInt64; -begin - Assert(Size = Context.BufferSize); - - D := PUInt64(Source)^; - K := @PUInt64Array(FAdditionalBuffer)[SHARK_ROUNDS + 1]; // decryption round key - for R := 1 to SHARK_ROUNDS - 1 do - begin - D := D xor K^; - Inc(K); - D := Shark_CD[0, D shr 56 and $FF] xor - Shark_CD[1, D shr 48 and $FF] xor - Shark_CD[2, D shr 40 and $FF] xor - Shark_CD[3, D shr 32 and $FF] xor - Shark_CD[4, D shr 24 and $FF] xor - Shark_CD[5, D shr 16 and $FF] xor - Shark_CD[6, D shr 8 and $FF] xor - Shark_CD[7, D and $FF]; - end; - D := D xor K^; - Inc(K); - D := UInt64(Shark_SD[D shr 56 and $FF]) shl 56 xor - UInt64(Shark_SD[D shr 48 and $FF]) shl 48 xor - UInt64(Shark_SD[D shr 40 and $FF]) shl 40 xor - UInt64(Shark_SD[D shr 32 and $FF]) shl 32 xor - UInt64(Shark_SD[D shr 24 and $FF]) shl 24 xor - UInt64(Shark_SD[D shr 16 and $FF]) shl 16 xor - UInt64(Shark_SD[D shr 8 and $FF]) shl 8 xor - UInt64(Shark_SD[D and $FF]); - - PUInt64(Dest)^ := D xor K^; -end; -{$ENDIF CPU64BITS} - -{ TCipher_Shark_DEC52 } - -{$IFNDEF CPU64BITS} -procedure TCipher_Shark_DEC52.DoInit(const Key; Size: Integer); -var - Log, ALog: TLogArray; - - procedure InitLog; - var - I, J: Word; - begin - ALog[0] := 1; - for I := 1 to 255 do - begin - J := ALog[I - 1] shl 1; - if J and $100 <> 0 then - J := J xor $01F5; - ALog[I] := J; - end; - for I := 1 to 254 do - Log[ALog[I]] := I; - end; - -var - T: array[0..6] of TLong64; - A: array[0..6] of TLong64; - K: array[0..15] of Byte; - I, J, R: Byte; - E, D: PLong64Array; - L: TLong64; -begin - FillChar(K, SizeOf(K), 0); - Move(Key, K, Size); - InitLog; - E := FAdditionalBuffer; - D := @E[7]; - Move(Shark_CE[0], T, SizeOf(T)); - T[6] := Transform(T[6], Log, ALog); - I := 0; - for R := 0 to 6 do - begin - Inc(I); - A[R].L := K[I and $F]; - A[R].R := 0; - for J := 1 to 7 do - begin - Inc(I); - A[R].R := A[R].R shl 8 or A[R].L shr 24; - A[R].L := A[R].L shl 8 or K[I and $F]; - end; - end; - L.L := 0; - L.R := 0; - L := Shark(L, @T); - E[0].L := A[0].L xor L.L; - E[0].R := A[0].R xor L.R; - for R := 1 to 6 do - begin - L := Shark(E[R - 1], @T); - E[R].L := A[R].L xor L.L; - E[R].R := A[R].R xor L.R; - end; - E[6] := Transform(E[6], Log, ALog); - D[0] := E[6]; - D[6] := E[0]; - for R := 1 to 5 do - D[R] := Transform(E[6-R], Log, ALog); - ProtectBuffer(T, SizeOf(T)); - ProtectBuffer(A, SizeOf(A)); - ProtectBuffer(K, SizeOf(K)); -end; - -{$ELSE CPU64BITS} - -procedure TCipher_Shark_DEC52.DoInit(const Key; Size: Integer); -var - Log, ALog: TLogArray; - - procedure InitLog; - var - I, J: Word; - begin - // Generate GF(256) anti-logarithm and logarithm tables - ALog[0] := 1; - for I := 1 to 255 do - begin - J := ALog[I - 1] shl 1; - if J and $100 <> 0 then - J := J xor SHARK_ROOT; - ALog[I] := J; - end; - for I := 1 to 254 do - Log[ALog[I]] := I; - end; - -var - T: array[0..SHARK_ROUNDS] of UInt64; - A: array[0..SHARK_ROUNDKEYS-1] of UInt64; - K: array[0..15] of Byte; - I, J, R: Integer; - E, D: PUInt64Array; -begin - FillChar(K, SizeOf(K), 0); - Move(Key, K, Size); - InitLog; - E := FAdditionalBuffer; // encryption round key - D := @E[SHARK_ROUNDS + 1]; // decryption round key - - Move(Shark_CE[0], T, SizeOf(T)); - T[SHARK_ROUNDS] := Transform(T[SHARK_ROUNDS], Log, ALog); - - I := 0; - for R := 0 to High(A) do - begin - Inc(I); - A[R] := K[I and $F]; - for J := 1 to 7 do - begin - Inc(I); - A[R] := A[R] shl 8 or K[I and $F]; - end; - end; - - E[0] := A[0] xor SharkEncode(0, @T); - for R := 1 to High(A) do - E[R] := A[R] xor SharkEncode(E[R - 1], @T); - - E[SHARK_ROUNDS] := Transform(E[SHARK_ROUNDS], Log, ALog); - D[0] := E[SHARK_ROUNDS]; - D[SHARK_ROUNDS] := E[0]; - for R := 1 to SHARK_ROUNDS - 1 do - D[R] := Transform(E[SHARK_ROUNDS - R], Log, ALog); - - ProtectBuffer(T, SizeOf(T)); - ProtectBuffer(A, SizeOf(A)); - ProtectBuffer(K, SizeOf(K)); -end; -{$ENDIF} - -{ TCipher_Skipjack } - -class function TCipher_Skipjack.Context: TCipherContext; -begin - Result.KeySize := 10; - Result.BlockSize := 8; - Result.BufferSize := 8; - Result.AdditionalBufferSize := $A00; - Result.NeedsAdditionalBufferBackup := false; - Result.MinRounds := 1; - Result.MaxRounds := 1; - Result.CipherType := [ctSymmetric, ctBlock]; -end; - -procedure TCipher_Skipjack.DoInit(const Key; Size: Integer); -var - K: array[0..9] of Byte; - D: PByte; - I, J: Integer; -begin - FillChar(K, SizeOf(K), 0); - Move(Key, K, Size); - D := FAdditionalBuffer; - for I := 0 to 9 do - for J := 0 to 255 do - begin - D^ := Skipjack_Data[J xor K[I]]; - Inc(D); - end; - ProtectBuffer(K, SizeOf(K)); -end; - -procedure TCipher_Skipjack.DoEncode(Source, Dest: Pointer; Size: Integer); -var - Tab, Min: PSkipjackTab; - Max: PByte; - K, T, A, B, C, D: UInt32; - -begin - Assert(Size = Context.BlockSize); - - Min := FAdditionalBuffer; - Max := PByte(Min) + 9 * 256; // for Pointer Math - Tab := Min; - A := Swap(PWordArray(Source)[0]); - B := Swap(PWordArray(Source)[1]); - C := Swap(PWordArray(Source)[2]); - D := Swap(PWordArray(Source)[3]); - K := 0; - - repeat - Inc(K); - T := A; - T := T xor Tab[T and $FF] shl 8; SkipjackIncCheck(Tab, Min, Max); - T := T xor Tab[T shr 8]; SkipjackIncCheck(Tab, Min, Max); - T := T xor Tab[T and $FF] shl 8; SkipjackIncCheck(Tab, Min, Max); - T := T xor Tab[T shr 8]; SkipjackIncCheck(Tab, Min, Max); - A := T xor D xor K; - D := C; - C := B; - B := T; - until K = 8; - - repeat - Inc(K); - T := A; - A := D; - D := C; - C := T xor B xor K; - T := T xor Tab[T and $FF] shl 8; SkipjackIncCheck(Tab, Min, Max); - T := T xor Tab[T shr 8]; SkipjackIncCheck(Tab, Min, Max); - T := T xor Tab[T and $FF] shl 8; SkipjackIncCheck(Tab, Min, Max); - T := T xor Tab[T shr 8]; SkipjackIncCheck(Tab, Min, Max); - B := T; - until K = 16; - - repeat - Inc(K); - T := A; - T := T xor Tab[T and $FF] shl 8; SkipjackIncCheck(Tab, Min, Max); - T := T xor Tab[T shr 8]; SkipjackIncCheck(Tab, Min, Max); - T := T xor Tab[T and $FF] shl 8; SkipjackIncCheck(Tab, Min, Max); - T := T xor Tab[T shr 8]; SkipjackIncCheck(Tab, Min, Max); - A := T xor D xor K; - D := C; - C := B; - B := T; - until K = 24; - - repeat - Inc(K); - T := A; - A := D; - D := C; - C := T xor B xor K; - T := T xor Tab[T and $FF] shl 8; SkipjackIncCheck(Tab, Min, Max); - T := T xor Tab[T shr 8]; SkipjackIncCheck(Tab, Min, Max); - T := T xor Tab[T and $FF] shl 8; SkipjackIncCheck(Tab, Min, Max); - T := T xor Tab[T shr 8]; SkipjackIncCheck(Tab, Min, Max); - B := T; - until K = 32; - - PWordArray(Dest)[0] := Swap(A); - PWordArray(Dest)[1] := Swap(B); - PWordArray(Dest)[2] := Swap(C); - PWordArray(Dest)[3] := Swap(D); -end; - -procedure TCipher_Skipjack.SkipjackIncCheck(var ATab: PSkipjackTab; AMin: PSkipjackTab; AMax: PByte); -begin - Inc(ATab); - - if PByte(ATab) > AMax then - ATab := AMin; -end; - -procedure TCipher_Skipjack.DoDecode(Source, Dest: Pointer; Size: Integer); -var - Tab, Max: PSkipjackTab; - Min: PByte; // for Pointer Math - K, T, A, B, C, D: UInt32; - -begin - Assert(Size = Context.BlockSize); - - Min := FAdditionalBuffer; - Max := Pointer(Min + 9 * 256); - Tab := Pointer(Min + 7 * 256); - A := Swap(PWordArray(Source)[0]); // holds an Integer, Compiler makes faster Code - B := Swap(PWordArray(Source)[1]); - C := Swap(PWordArray(Source)[2]); - D := Swap(PWordArray(Source)[3]); - K := 32; - - repeat - T := B; - T := T xor Tab[T shr 8]; SkipjackDecCheck(Tab, Min, Max); - T := T xor Tab[T and $FF] shl 8; SkipjackDecCheck(Tab, Min, Max); - T := T xor Tab[T shr 8]; SkipjackDecCheck(Tab, Min, Max); - T := T xor Tab[T and $FF] shl 8; SkipjackDecCheck(Tab, Min, Max); - B := T xor C xor K; - C := D; - D := A; - A := T; - Dec(K); - until K = 24; - - repeat - T := B; - B := C; - C := D; - D := T xor A xor K; - T := T xor Tab[T shr 8]; SkipjackDecCheck(Tab, Min, Max); - T := T xor Tab[T and $FF] shl 8; SkipjackDecCheck(Tab, Min, Max); - T := T xor Tab[T shr 8]; SkipjackDecCheck(Tab, Min, Max); - T := T xor Tab[T and $FF] shl 8; SkipjackDecCheck(Tab, Min, Max); - A := T; - Dec(K); - until K = 16; - - repeat - T := B; - T := T xor Tab[T shr 8]; SkipjackDecCheck(Tab, Min, Max); - T := T xor Tab[T and $FF] shl 8; SkipjackDecCheck(Tab, Min, Max); - T := T xor Tab[T shr 8]; SkipjackDecCheck(Tab, Min, Max); - T := T xor Tab[T and $FF] shl 8; SkipjackDecCheck(Tab, Min, Max); - B := C xor T xor K; - C := D; - D := A; - A := T; - Dec(K); - until K = 8; - - repeat - T := B; - B := C; - C := D; - D := T xor A xor K; - T := T xor Tab[T shr 8]; SkipjackDecCheck(Tab, Min, Max); - T := T xor Tab[T and $FF] shl 8; SkipjackDecCheck(Tab, Min, Max); - T := T xor Tab[T shr 8]; SkipjackDecCheck(Tab, Min, Max); - T := T xor Tab[T and $FF] shl 8; SkipjackDecCheck(Tab, Min, Max); - A := T; - Dec(K); - until K = 0; - - PWordArray(Dest)[0] := Swap(A); - PWordArray(Dest)[1] := Swap(B); - PWordArray(Dest)[2] := Swap(C); - PWordArray(Dest)[3] := Swap(D); -end; - -procedure TCipher_Skipjack.SkipjackDecCheck(var ATab: PSkipjackTab; AMin: PByte; AMax: PSkipjackTab); -begin - Dec(ATab); -// {$IFDEF DELPHIORBCB} -// if ATab < AMin then -// {$ELSE !DELPHIORBCB} -{ TODO : Prüfen ob so korrekt, da ATab auf PByte umgestellt wurde, außerdem sollte -diese interne procedure eher zu einer strict private methode werden} - if PByte(ATab) < AMin then -// {$ENDIF !DELPHIORBCB} - ATab := AMax; -end; - -{ TCipher_TEA } - -const - TEA_Delta = $9E3779B9; // magic constant, decimal 2654435769 - -class function TCipher_TEA.Context: TCipherContext; -begin - Result.KeySize := 16; // 128 bits - Result.BlockSize := 8; // 64 bits - Result.BufferSize := 8; // 64 bits - Result.AdditionalBufferSize := 32; // 256 bits - Result.NeedsAdditionalBufferBackup := false; - Result.MinRounds := 16; - Result.MaxRounds := 256; - Result.CipherType := [ctSymmetric, ctBlock]; -end; - -procedure TCipher_TEA.SetRounds(Value: Integer); -begin - if not (FState in [csNew, csInitialized, csDone]) then - Done; - if Value < 16 then - Value := 16 - else - if Value > 256 then - Value := 256; - FRounds := Value; -end; - -procedure TCipher_TEA.DoInit(const Key; Size: Integer); -begin - Move(Key, FAdditionalBuffer^, Size); - SetRounds(FRounds); -end; - -procedure TCipher_TEA.DoEncode(Source, Dest: Pointer; Size: Integer); -var - I: Integer; - Sum, - X, Y, A, B, C, D: UInt32; -begin - Assert(Size = Context.BlockSize); - - Sum := 0; - - A := PUInt32Array(FAdditionalBuffer)[0]; - B := PUInt32Array(FAdditionalBuffer)[1]; - C := PUInt32Array(FAdditionalBuffer)[2]; - D := PUInt32Array(FAdditionalBuffer)[3]; - X := PUInt32Array(Source)[0]; - Y := PUInt32Array(Source)[1]; - - for I := 0 to FRounds - 1 do - begin - Inc(Sum, TEA_Delta); - Inc(X, (((Y shl 4 + A) xor Y) + Sum) xor (Y shr 5 + B)); - Inc(Y, (((X shl 4 + C) xor X) + Sum) xor (X shr 5 + D)); - end; - - PUInt32Array(Dest)[0] := X; - PUInt32Array(Dest)[1] := Y; -end; - -procedure TCipher_TEA.DoDecode(Source, Dest: Pointer; Size: Integer); -var - I: Integer; - Sum, - X, Y, A, B, C, D: UInt32; -begin - Assert(Size = Context.BlockSize); - - Sum := TEA_Delta * UInt32(FRounds); - - A := PUInt32Array(FAdditionalBuffer)[0]; - B := PUInt32Array(FAdditionalBuffer)[1]; - C := PUInt32Array(FAdditionalBuffer)[2]; - D := PUInt32Array(FAdditionalBuffer)[3]; - X := PUInt32Array(Source)[0]; - Y := PUInt32Array(Source)[1]; - - for I := 0 to FRounds - 1 do - begin - Dec(Y, (X shl 4 + C) xor X + Sum xor (X shr 5 + D)); - Dec(X, (Y shl 4 + A) xor Y + Sum xor (Y shr 5 + B)); - Dec(Sum, TEA_Delta); - end; - - PUInt32Array(Dest)[0] := X; - PUInt32Array(Dest)[1] := Y; -end; - -{ TCipher_XTEA } - -procedure TCipher_XTEA.DoEncode(Source, Dest: Pointer; Size: Integer); -var - Sum, - I, X, Y: UInt32; - K: PUInt32Array; -begin - Assert(Size = Context.BlockSize); - - Sum := 0; - - X := PUInt32Array(Source)[0]; - Y := PUInt32Array(Source)[1]; - K := FAdditionalBuffer; - - for I := 0 to FRounds - 1 do - begin - Inc(X, (((Y shl 4) xor (Y shr 5)) + Y) xor (Sum + K[Sum and 3])); - Inc(Sum, TEA_Delta); - Inc(Y, (((X shl 4) xor (X shr 5)) + X) xor (Sum + K[Sum shr 11 and 3])); - end; - - PUInt32Array(Dest)[0] := X; - PUInt32Array(Dest)[1] := Y; -end; - -procedure TCipher_XTEA.DoDecode(Source, Dest: Pointer; Size: Integer); -var - I: Integer; - Sum, - X, Y: UInt32; - K: PUInt32Array; -begin - Assert(Size = Context.BlockSize); - - Sum := TEA_Delta * UInt32(FRounds); - - X := PUInt32Array(Source)[0]; - Y := PUInt32Array(Source)[1]; - K := FAdditionalBuffer; - - for I := 0 to FRounds - 1 do - begin - Dec(Y, (((X shl 4) xor (X shr 5)) + X) xor (Sum + K[Sum shr 11 and 3])); - Dec(Sum, TEA_Delta); - Dec(X, (((Y shl 4) xor (Y shr 5)) + Y) xor (Sum + K[Sum and 3])); - end; - - PUInt32Array(Dest)[0] := X; - PUInt32Array(Dest)[1] := Y; -end; - -{ TCipher_XTEA_DEC52 } - -{ TODO : The old failure needs to be restored again } - -procedure TCipher_XTEA_DEC52.DoEncode(Source, Dest: Pointer; Size: Integer); -var - Sum, - I, X, Y: UInt32; - K: PUInt32Array; -begin - Assert(Size = Context.BlockSize); - - Sum := 0; - - X := PUInt32Array(Source)[0]; - Y := PUInt32Array(Source)[1]; - K := FAdditionalBuffer; - - for I := 0 to FRounds - 1 do - begin - Inc(X, (Y shl 4 xor Y shr 5) + (Y xor Sum) + K[Sum and 3]); - Inc(Sum, TEA_Delta); - Inc(Y, (X shl 4 xor X shr 5) + (X xor Sum) + K[Sum shr 11 and 3]); - end; - - PUInt32Array(Dest)[0] := X; - PUInt32Array(Dest)[1] := Y; -end; - -procedure TCipher_XTEA_DEC52.DoDecode(Source, Dest: Pointer; Size: Integer); -var - I: Integer; - Sum, - X, Y: UInt32; - K: PUInt32Array; -begin - Assert(Size = Context.BlockSize); - - Sum := TEA_Delta * UInt32(FRounds); - - X := PUInt32Array(Source)[0]; - Y := PUInt32Array(Source)[1]; - K := FAdditionalBuffer; - - for I := 0 to FRounds - 1 do - begin - Dec(Y, (X shl 4 xor X shr 5) + (X xor Sum) + K[Sum shr 11 and 3]); - Dec(Sum, TEA_Delta); - Dec(X, (Y shl 4 xor Y shr 5) + (Y xor Sum) + K[Sum and 3]); - end; - - PUInt32Array(Dest)[0] := X; - PUInt32Array(Dest)[1] := Y; -end; - -{$IFDEF RESTORE_RANGECHECKS}{$R+}{$ENDIF} -{$IFDEF RESTORE_OVERFLOWCHECKS}{$Q+}{$ENDIF} - -initialization - SetDefaultCipherClass(TCipher_Null); - - {$IFNDEF ManualRegisterClasses} - TCipher_Null.RegisterClass(TDECCipher.ClassList); - TCipher_Blowfish.RegisterClass(TDECCipher.ClassList); - TCipher_Twofish.RegisterClass(TDECCipher.ClassList); - TCipher_IDEA.RegisterClass(TDECCipher.ClassList); - TCipher_Cast256.RegisterClass(TDECCipher.ClassList); - TCipher_Mars.RegisterClass(TDECCipher.ClassList); - TCipher_RC4.RegisterClass(TDECCipher.ClassList); - TCipher_RC6.RegisterClass(TDECCipher.ClassList); -// Explicitely not registered, as Rijndael is 1:1 the same as AES and AES is the -// more common name -// TCipher_Rijndael.RegisterClass(TDECCipher.ClassList); - TCipher_AES.RegisterClass(TDECCipher.ClassList); - TCipher_Square.RegisterClass(TDECCipher.ClassList); - TCipher_SCOP.RegisterClass(TDECCipher.ClassList); - TCipher_Sapphire.RegisterClass(TDECCipher.ClassList); - TCipher_1DES.RegisterClass(TDECCipher.ClassList); - TCipher_2DES.RegisterClass(TDECCipher.ClassList); - TCipher_3DES.RegisterClass(TDECCipher.ClassList); - TCipher_2DDES.RegisterClass(TDECCipher.ClassList); - TCipher_3DDES.RegisterClass(TDECCipher.ClassList); - TCipher_3TDES.RegisterClass(TDECCipher.ClassList); - TCipher_3Way.RegisterClass(TDECCipher.ClassList); - TCipher_Cast128.RegisterClass(TDECCipher.ClassList); - TCipher_Gost.RegisterClass(TDECCipher.ClassList); -// Explicitely not registered, as this is an alias for Gost only -// TCipher_Magma.RegisterClass(TDECCipher.ClassList); - TCipher_Misty.RegisterClass(TDECCipher.ClassList); - TCipher_NewDES.RegisterClass(TDECCipher.ClassList); - TCipher_Q128.RegisterClass(TDECCipher.ClassList); - TCipher_RC2.RegisterClass(TDECCipher.ClassList); - TCipher_RC5.RegisterClass(TDECCipher.ClassList); - TCipher_SAFER.RegisterClass(TDECCipher.ClassList); - TCipher_Shark.RegisterClass(TDECCipher.ClassList); - TCipher_Skipjack.RegisterClass(TDECCipher.ClassList); - TCipher_TEA.RegisterClass(TDECCipher.ClassList); - TCipher_XTEA.RegisterClass(TDECCipher.ClassList); - TCipher_TEAN.RegisterClass(TDECCipher.ClassList); - - {$IFDEF OLD_REGISTER_FAULTY_CIPHERS} - // Those classes are only there for those who might have relied on the - // faulty implementation - TCipher_SCOP_DEC52.RegisterClass(TDECCipher.ClassList); - TCipher_Shark_DEC52.RegisterClass(TDECCipher.ClassList); - TCipher_XTEA_DEC52.RegisterClass(TDECCipher.ClassList); - {$ENDIF} - {$ENDIF} - -finalization - -end. diff --git a/Unit Tests/Tests/TestDECCipher.pas b/Unit Tests/Tests/TestDECCipher.pas deleted file mode 100644 index 0b246bd9..00000000 --- a/Unit Tests/Tests/TestDECCipher.pas +++ /dev/null @@ -1,4100 +0,0 @@ -{***************************************************************************** - The DEC team (see file NOTICE.txt) licenses this file - to you under the Apache License, Version 2.0 (the - "License"); you may not use this file except in compliance - with the License. A copy of this licence is found in the root directory of - this project in the file LICENCE.txt or alternatively at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, - software distributed under the License is distributed on an - "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - KIND, either express or implied. See the License for the - specific language governing permissions and limitations - under the License. -*****************************************************************************} - -{$M+} // DUnitX would add it anyway -unit TestDECCipher; - -interface - -// Needs to be included before any other statements -{$INCLUDE TestDefines.inc} - -uses - System.SysUtils, System.Classes, - {$IFDEF DUnitX} - DUnitX.TestFramework,DUnitX.DUnitCompatibility, - {$ELSE} - TestFramework, - {$ENDIF} - DECBaseClass, DECCipherBase, DECCiphers, DECUtil, DECFormatBase, DECFormat; - -type - // A function with these parameters has to be passed to DoTestEncode/Decode to - // make that one generic - TEncodeDecodeFunc = function (const Source: RawByteString; Format: TDECFormatClass = nil): TBytes of Object; - - /// - /// All known testvectors use the same filler byte and the same cmCTSx mode - /// - TCipherTestData = record - InputData : RawByteString; - OutputData : RawByteString; - - Key : RawByteString; - InitVector : RawByteString; - Filler : Byte; - Mode : TCipherMode; - end; - - /// - /// Init method called before conducting a test. Sets up the concrete - /// cipher password, mode, initialization vector etc. - /// - /// - /// Record with the data for the current test, including encryption key, - /// initialization vector etc. - /// - TInitProc = procedure(TestData: TCipherTestData) Of Object; - /// - /// Caqllback method for cleaning up after each test - /// - TDoneProc = procedure of Object; - - // Basic class all Cipher test classes should inherit from - TCipherBasis = class(TTestCase) - strict protected - FTestData : array of TCipherTestData; - /// - /// FTestData gets put into this memory stream in tests which test the - /// stream oriented encryption/decryption methods - /// - FTestStream : TMemoryStream; - - /// - /// Converts a test vector with the follwing syntax to a byte array: - /// \x30\x31\x41 where \ is the delimiter and x means that the following - /// two chars are the hex ordinal number of an ANSI char - /// - function ConvertHexVectorToBytes(Vector: string): TBytes; - - /// - /// Ensures that a given key is not longer then the KeySize passed - /// - /// - /// Key to be checked. if it is longer than KeySize it will be cut off. - /// - /// - /// Maximum size of a key for the given cipher algorithm - /// - procedure LimitKeyLength(var Key:RawByteString; KeySize: Integer); - - procedure DoTestEncode(EncodeFunc: TEncodeDecodeFunc; InitProc: TInitProc; DoneProc: TDoneProc); - procedure DoTestDecode(DecodeFunct: TEncodeDecodeFunc; InitProc: TInitProc; DoneProc: TDoneProc); - end; - - // Testmethods for class TDECCipher - {$IFDEF DUnitX} [TestFixture] {$ENDIF} - TestTDECCipher = class(TCipherBasis) - strict private - public - procedure SetUp; override; - procedure TearDown; override; - published - procedure TestIsClassListCreated; - procedure TestValidCipherSetDefaultCipherClass; - end; - - // Testmethoden for Klasse TCipher_Null - {$IFDEF DUnitX} [TestFixture] {$ENDIF} - TestTCipher_Null = class(TTestCase) - strict private - FCipher_Null: TCipher_Null; - public - procedure SetUp; override; - procedure TearDown; override; - published - procedure TestIdentity; - procedure TestContext; - procedure TestClassByName; - end; - - // Testmethods for class TCipher_Blowfish - {$IFDEF DUnitX} [TestFixture] {$ENDIF} - TestTCipher_Blowfish = class(TCipherBasis) - strict private - FCipher_Blowfish: TCipher_Blowfish; - - procedure Init(TestData: TCipherTestData); - procedure Done; - public - procedure SetUp; override; - procedure TearDown; override; - published - procedure TestIdentity; - procedure TestContext; - procedure TestEncode; - procedure TestDecode; - procedure TestClassByName; - end; - - // Testmethods for class TCipher_Twofish - {$IFDEF DUnitX} [TestFixture] {$ENDIF} - TestTCipher_Twofish = class(TCipherBasis) - strict private - FCipher_Twofish: TCipher_Twofish; - - procedure Init(TestData: TCipherTestData); - procedure Done; - public - procedure SetUp; override; - procedure TearDown; override; - published - procedure TestIdentity; - procedure TestContext; - procedure TestEncode; - procedure TestDecode; - procedure TestClassByName; - end; - - // Testmethods for class TCipher_IDEA - {$IFDEF DUnitX} [TestFixture] {$ENDIF} - TestTCipher_IDEA = class(TCipherBasis) - strict private - FCipher_IDEA: TCipher_IDEA; - - procedure Init(TestData: TCipherTestData); - procedure Done; - public - procedure SetUp; override; - procedure TearDown; override; - published - procedure TestIdentity; - procedure TestContext; - procedure TestEncode; - procedure TestDecode; - procedure TestClassByName; - end; - - // Testmethods for class TCipher_Cast256 - {$IFDEF DUnitX} [TestFixture] {$ENDIF} - TestTCipher_Cast256 = class(TCipherBasis) - strict private - FCipher_Cast256: TCipher_Cast256; - - procedure Init(TestData: TCipherTestData); - procedure Done; - public - procedure SetUp; override; - procedure TearDown; override; - published - procedure TestIdentity; - procedure TestContext; - procedure TestEncode; - procedure TestDecode; - procedure TestClassByName; - end; - - // Testmethods for class TCipher_Mars - {$IFDEF DUnitX} [TestFixture] {$ENDIF} - TestTCipher_Mars = class(TCipherBasis) - strict private - FCipher_Mars: TCipher_Mars; - - procedure Init(TestData: TCipherTestData); - procedure Done; - private - public - procedure SetUp; override; - procedure TearDown; override; - published - procedure TestIdentity; - procedure TestContext; - procedure TestEncode; - procedure TestDecode; - procedure TestClassByName; - end; - - // Testmethods for class TCipher_RC4 - {$IFDEF DUnitX} [TestFixture] {$ENDIF} - TestTCipher_RC4 = class(TCipherBasis) - strict private - FCipher_RC4: TCipher_RC4; - - procedure Init(TestData: TCipherTestData); - procedure Done; - public - procedure SetUp; override; - procedure TearDown; override; - published - procedure TestIdentity; - procedure TestContext; - procedure TestEncode; - procedure TestDecode; - procedure TestClassByName; - end; - - // Testmethods for class TCipher_RC6 - {$IFDEF DUnitX} [TestFixture] {$ENDIF} - TestTCipher_RC6 = class(TCipherBasis) - strict private - FCipher_RC6: TCipher_RC6; - - procedure Init(TestData: TCipherTestData); - procedure Done; - public - procedure SetUp; override; - procedure TearDown; override; - published - procedure TestIdentity; - procedure TestContext; - procedure TestEncode; - procedure TestDecode; - procedure TestClassByName; - end; - - // Testmethods for class TCipher_AES - {$IFDEF DUnitX} [TestFixture] {$ENDIF} - TestTCipher_AES = class(TCipherBasis) - strict private - FCipher_AES: TCipher_AES; - - procedure Init(TestData: TCipherTestData); - procedure Done; - public - procedure SetUp; override; - procedure TearDown; override; - published - procedure TestIdentity; - procedure TestContext; - procedure TestEncode; - procedure TestDecode; - procedure TestClassByName; - end; - - // Testmethods for class TCipher_Rijndael which is an alias for AES as it's - // the original name of that algorithm - {$IFDEF DUnitX} [TestFixture] {$ENDIF} - TestTCipher_Rijndael = class(TCipherBasis) - strict private - FCipher_Rijndael: TCipher_Rijndael; - - procedure Init(TestData: TCipherTestData); - procedure Done; - - procedure DoTestClassByName; - public - procedure SetUp; override; - procedure TearDown; override; - published - procedure TestIdentity; - procedure TestContext; - procedure TestEncode; - procedure TestDecode; - procedure TestClassByName; - end; - - // Testmethods for class TCipher_Square - {$IFDEF DUnitX} [TestFixture] {$ENDIF} - TestTCipher_Square = class(TCipherBasis) - strict private - FCipher_Square: TCipher_Square; - - procedure Init(TestData: TCipherTestData); - procedure Done; - public - procedure SetUp; override; - procedure TearDown; override; - published - procedure TestIdentity; - procedure TestContext; - procedure TestEncode; - procedure TestDecode; - procedure TestClassByName; - end; - - // Testmethods for class TCipher_SCOP - {$IFDEF DUnitX} [TestFixture] {$ENDIF} - TestTCipher_SCOP = class(TCipherBasis) - strict private - FCipher_SCOP: TCipher_SCOP; - - procedure Init(TestData: TCipherTestData); - procedure Done; - public - procedure SetUp; override; - procedure TearDown; override; - published - procedure TestIdentity; - procedure TestContext; - procedure TestEncode; - procedure TestDecode; - procedure TestClassByName; - end; - - // Testmethods for class TCipher_SCOP_DEC52 - {$IFDEF DUnitX} [TestFixture] {$ENDIF} - TestTCipher_SCOP_DEC52 = class(TCipherBasis) - strict private - FCipher_SCOP_DEC52: TCipher_SCOP_DEC52; - - procedure DoTestClassByName; - - procedure Init(TestData: TCipherTestData); - procedure Done; - public - procedure SetUp; override; - procedure TearDown; override; - published - procedure TestIdentity; - procedure TestContext; - procedure TestEncode; - procedure TestDecode; - procedure TestClassByName; - end; - - // Testmethods for class TCipher_Sapphire - {$IFDEF DUnitX} [TestFixture] {$ENDIF} - TestTCipher_Sapphire = class(TCipherBasis) - strict private - FCipher_Sapphire: TCipher_Sapphire; - - procedure Init(TestData: TCipherTestData); - procedure Done; - public - procedure SetUp; override; - procedure TearDown; override; - published - procedure TestIdentity; - procedure TestContext; - procedure TestEncode; - procedure TestDecode; - procedure TestClassByName; - end; - - // Testmethods for class TCipher_1DES - {$IFDEF DUnitX} [TestFixture] {$ENDIF} - TestTCipher_1DES = class(TCipherBasis) - strict private - FCipher_1DES: TCipher_1DES; - - procedure Init(TestData: TCipherTestData); - procedure Done; - public - procedure SetUp; override; - procedure TearDown; override; - published - procedure TestIdentity; - procedure TestContext; - procedure TestEncode; - procedure TestDecode; - procedure TestClassByName; - end; - - // Testmethods for class TCipher_2DES - {$IFDEF DUnitX} [TestFixture] {$ENDIF} - TestTCipher_2DES = class(TCipherBasis) - strict private - FCipher_2DES: TCipher_2DES; - - procedure Init(TestData: TCipherTestData); - procedure Done; - public - procedure SetUp; override; - procedure TearDown; override; - published - procedure TestIdentity; - procedure TestContext; - procedure TestEncode; - procedure TestDecode; - procedure TestClassByName; - end; - - // Testmethods for class TCipher_3DES - {$IFDEF DUnitX} [TestFixture] {$ENDIF} - TestTCipher_3DES = class(TCipherBasis) - strict private - FCipher_3DES: TCipher_3DES; - - procedure Init(TestData: TCipherTestData); - procedure Done; - public - procedure SetUp; override; - procedure TearDown; override; - published - procedure TestIdentity; - procedure TestContext; - procedure TestEncode; - procedure TestDecode; - procedure TestClassByName; - end; - - // Testmethods for class TCipher_2DDES - {$IFDEF DUnitX} [TestFixture] {$ENDIF} - TestTCipher_2DDES = class(TCipherBasis) - strict private - FCipher_2DDES: TCipher_2DDES; - - procedure Init(TestData: TCipherTestData); - procedure Done; - public - procedure SetUp; override; - procedure TearDown; override; - published - procedure TestIdentity; - procedure TestContext; - procedure TestEncode; - procedure TestDecode; - procedure TestClassByName; - end; - - // Testmethods for class TCipher_3DDES - {$IFDEF DUnitX} [TestFixture] {$ENDIF} - TestTCipher_3DDES = class(TCipherBasis) - strict private - FCipher_3DDES: TCipher_3DDES; - - procedure Init(TestData: TCipherTestData); - procedure Done; - public - procedure SetUp; override; - procedure TearDown; override; - published - procedure TestIdentity; - procedure TestContext; - procedure TestEncode; - procedure TestDecode; - procedure TestClassByName; - end; - - // Testmethods for class TCipher_3TDES - {$IFDEF DUnitX} [TestFixture] {$ENDIF} - TestTCipher_3TDES = class(TCipherBasis) - strict private - FCipher_3TDES: TCipher_3TDES; - - procedure Init(TestData: TCipherTestData); - procedure Done; - public - procedure SetUp; override; - procedure TearDown; override; - published - procedure TestIdentity; - procedure TestContext; - procedure TestEncode; - procedure TestDecode; - procedure TestClassByName; - end; - - // Testmethods for class TCipher_3Way - {$IFDEF DUnitX} [TestFixture] {$ENDIF} - TestTCipher_3Way = class(TCipherBasis) - strict private - FCipher_3Way: TCipher_3Way; - - procedure Init(TestData: TCipherTestData); - procedure Done; - public - procedure SetUp; override; - procedure TearDown; override; - published - procedure TestIdentity; - procedure TestContext; - procedure TestEncode; - procedure TestDecode; - procedure TestClassByName; - end; - - // Testmethods for class TCipher_Cast128 - {$IFDEF DUnitX} [TestFixture] {$ENDIF} - TestTCipher_Cast128 = class(TCipherBasis) - strict private - FCipher_Cast128: TCipher_Cast128; - - procedure Init(TestData: TCipherTestData); - procedure Done; - public - procedure SetUp; override; - procedure TearDown; override; - published - procedure TestIdentity; - procedure TestContext; - procedure TestEncode; - procedure TestDecode; - procedure TestClassByName; - end; - - // Testmethods for class TCipher_Gost - {$IFDEF DUnitX} [TestFixture] {$ENDIF} - TestTCipher_Gost = class(TCipherBasis) - strict private - FCipher_Gost: TCipher_Gost; - - procedure Init(TestData: TCipherTestData); - procedure Done; - public - procedure SetUp; override; - procedure TearDown; override; - published - procedure TestIdentity; - procedure TestContext; - procedure TestEncode; - procedure TestDecode; - procedure TestClassByName; - end; - - // Testmethods for class TCipher_Magma, which is an alias for Ghost - {$IFDEF DUnitX} [TestFixture] {$ENDIF} - TestTCipher_Magma = class(TCipherBasis) - strict private - FCipher_Magma: TCipher_Magma; - - procedure Init(TestData: TCipherTestData); - procedure Done; - - procedure DoTestClassByName; - public - procedure SetUp; override; - procedure TearDown; override; - published - procedure TestIdentity; - procedure TestContext; - procedure TestEncode; - procedure TestDecode; - procedure TestClassByName; - end; - - // Testmethods for class TCipher_Misty - {$IFDEF DUnitX} [TestFixture] {$ENDIF} - TestTCipher_Misty = class(TCipherBasis) - strict private - FCipher_Misty: TCipher_Misty; - - procedure Init(TestData: TCipherTestData); - procedure Done; - public - procedure SetUp; override; - procedure TearDown; override; - published - procedure TestIdentity; - procedure TestContext; - procedure TestEncode; - procedure TestDecode; - procedure TestClassByName; - end; - - // Testmethods for class TCipher_NewDES - {$IFDEF DUnitX} [TestFixture] {$ENDIF} - TestTCipher_NewDES = class(TCipherBasis) - strict private - FCipher_NewDES: TCipher_NewDES; - - procedure Init(TestData: TCipherTestData); - procedure Done; - public - procedure SetUp; override; - procedure TearDown; override; - published - procedure TestIdentity; - procedure TestContext; - procedure TestEncode; - procedure TestDecode; - procedure TestClassByName; - end; - - // Testmethods for class TCipher_Q128 - {$IFDEF DUnitX} [TestFixture] {$ENDIF} - TestTCipher_Q128 = class(TCipherBasis) - strict private - FCipher_Q128: TCipher_Q128; - - procedure Init(TestData: TCipherTestData); - procedure Done; - public - procedure SetUp; override; - procedure TearDown; override; - published - procedure TestIdentity; - procedure TestContext; - procedure TestEncode; - procedure TestDecode; - procedure TestClassByName; - end; - - // Testmethods for class TCipher_RC2 - {$IFDEF DUnitX} [TestFixture] {$ENDIF} - TestTCipher_RC2 = class(TCipherBasis) - strict private - FCipher_RC2: TCipher_RC2; - - procedure Init(TestData: TCipherTestData); - procedure Done; - public - procedure SetUp; override; - procedure TearDown; override; - published - procedure TestIdentity; - procedure TestContext; - procedure TestEncode; - procedure TestDecode; - procedure TestClassByName; - end; - - // Testmethods for class TCipher_RC5 - {$IFDEF DUnitX} [TestFixture] {$ENDIF} - TestTCipher_RC5 = class(TCipherBasis) - strict private - FCipher_RC5: TCipher_RC5; - - procedure Init(TestData: TCipherTestData); - procedure Done; - public - procedure SetUp; override; - procedure TearDown; override; - published - procedure TestIdentity; - procedure TestContext; - procedure TestEncode; - procedure TestDecode; - procedure TestClassByName; - end; - - // Testmethods for class TCipher_SAFER - {$IFDEF DUnitX} [TestFixture] {$ENDIF} - TestTCipher_SAFER = class(TCipherBasis) - strict private - FCipher_SAFER: TCipher_SAFER; - - procedure Init(TestData: TCipherTestData); - procedure Done; - public - procedure SetUp; override; - procedure TearDown; override; - published - procedure TestIdentity; - procedure TestContext; - procedure TestEncode; - procedure TestDecode; - procedure TestClassByName; - end; - - // Testmethods for class TCipher_Shark - {$IFDEF DUnitX} [TestFixture] {$ENDIF} - TestTCipher_Shark = class(TCipherBasis) - strict private - FCipher_Shark: TCipher_Shark; - - procedure Init(TestData: TCipherTestData); - procedure Done; - public - procedure SetUp; override; - procedure TearDown; override; - published - procedure TestIdentity; - procedure TestContext; - procedure TestEncode; - procedure TestDecode; - procedure TestClassByName; - end; - - // Testmethods for class TCipher_Shark_DEC52 - {$IFDEF DUnitX} [TestFixture] {$ENDIF} - TestTCipher_Shark_DEC52 = class(TCipherBasis) - strict private - FCipher_Shark_DEC52: TCipher_Shark_DEC52; - - procedure DoTestClassByName; - - procedure Init(TestData: TCipherTestData); - procedure Done; - public - procedure SetUp; override; - procedure TearDown; override; - published - procedure TestIdentity; - procedure TestContext; - procedure TestEncode; - procedure TestDecode; - procedure TestClassByName; - end; - - // Testmethods for class TCipher_Skipjack - {$IFDEF DUnitX} [TestFixture] {$ENDIF} - TestTCipher_Skipjack = class(TCipherBasis) - strict private - FCipher_Skipjack: TCipher_Skipjack; - - procedure Init(TestData: TCipherTestData); - procedure Done; - public - procedure SetUp; override; - procedure TearDown; override; - published - procedure TestIdentity; - procedure TestContext; - procedure TestEncode; - procedure TestDecode; - procedure TestClassByName; - end; - - // Testmethods for class TCipher_TEA - {$IFDEF DUnitX} [TestFixture] {$ENDIF} - TestTCipher_TEA = class(TCipherBasis) - strict private - FCipher_TEA: TCipher_TEA; - - procedure Init(TestData: TCipherTestData); - procedure Done; - public - procedure SetUp; override; - procedure TearDown; override; - published - procedure TestIdentity; - procedure TestContext; - procedure TestEncode; - procedure TestDecode; - procedure TestClassByName; - end; - - // Testmethods for class TCipher_XTEA - {$IFDEF DUnitX} [TestFixture] {$ENDIF} - TestTCipher_XTEA = class(TCipherBasis) - strict private - FCipher_XTEA: TCipher_XTEA; - - procedure Init(TestData: TCipherTestData); - procedure Done; - public - procedure SetUp; override; - procedure TearDown; override; - published - procedure TestIdentity; - procedure TestContext; - procedure TestEncode; - procedure TestDecode; - procedure TestClassByName; - end; - - // Testmethods for class TCipher_XTEA_DEC52 - {$IFDEF DUnitX} [TestFixture] {$ENDIF} - TestTCipher_XTEA_DEC52 = class(TCipherBasis) - strict private - FCipher_XTEA_DEC52: TCipher_XTEA_DEC52; - - procedure DoTestClassByName; - - procedure Init(TestData: TCipherTestData); - procedure Done; - public - procedure SetUp; override; - procedure TearDown; override; - published - procedure TestIdentity; - procedure TestContext; - procedure TestEncode; - procedure TestDecode; - procedure TestClassByName; - end; - -implementation - -const - cZeroBlock8 = #$00#$00#$00#$00#$00#$00#$00#$00; - cFFBlock8 = 'FFFFFFFFFFFFFFFF'; - cZeroBlock16 = #$00#$00#$00#$00#$00#$00#$00#$00#$00#$00#$00#$00#$00#$00#$00#$00; - cFFBlock16 = 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF'; - -procedure TestTCipher_Null.SetUp; -begin - FCipher_Null := TCipher_Null.Create; -end; - -procedure TestTCipher_Null.TearDown; -begin - FCipher_Null.Free; - FCipher_Null := nil; -end; - -procedure TestTCipher_Null.TestClassByName; -var - ReturnValue : TDECCipherClass; -begin - ReturnValue := FCipher_Null.ClassByName('TCipher_Null'); - CheckEquals(TCipher_Null, ReturnValue, 'Class is not registered'); -end; - -procedure TestTCipher_Null.TestContext; -var - ReturnValue: TCipherContext; -begin - ReturnValue := FCipher_Null.Context; - - CheckEquals( 0, ReturnValue.KeySize); - CheckEquals( 1, ReturnValue.BlockSize); - CheckEquals( 8, ReturnValue.BufferSize); - CheckEquals( 0, ReturnValue.AdditionalBufferSize); - CheckEquals( 1, ReturnValue.MinRounds); - CheckEquals( 1, ReturnValue.MaxRounds); - CheckEquals(false, ReturnValue.NeedsAdditionalBufferBackup); - CheckEquals(true, [ctNull, ctSymmetric] = ReturnValue.CipherType); -end; - -procedure TestTCipher_Null.TestIdentity; -begin - CheckEquals($1678C79D, FCipher_Null.Identity); -end; - -procedure TestTCipher_Blowfish.Done; -begin - FCipher_Blowfish.Done; -end; - -procedure TestTCipher_Blowfish.Init(TestData: TCipherTestData); -begin - LimitKeyLength(TestData.Key, FCipher_Blowfish.Context.KeySize); - - FCipher_Blowfish.Mode := TestData.Mode; - FCipher_Blowfish.Init(BytesOf(TestData.Key), - BytesOf(TestData.InitVector), - TestData.Filler); -end; - -procedure TestTCipher_Blowfish.SetUp; -begin - FCipher_Blowfish := TCipher_Blowfish.Create; - - SetLength(FTestData, 1); - - FTestData[0].OutputData := '1971cacd2b9c8529da8147b7ebce16c6910e1dc840123e3570edbc964c13d0b8'; - FTestData[0].InputData := TFormat_ESCAPE.Decode('\x30\x44\xED\x6E\x45\xA4' + - '\x96\xF5\xF6\x35\xA2\xEB' + - '\x3D\x1A\x5D\xD6\xCB\x1D' + - '\x09\x82\x2D\xBD\xF5\x60' + - '\xC2\xB8\x58\xA1\x91\xF9' + - '\x81\xB1'); - - FTestData[0].Key := 'TCipher_Blowfish'; - FTestData[0].InitVector := ''; - FTestData[0].Filler := $FF; - FTestData[0].Mode := cmCTSx; -end; - -procedure TestTCipher_Blowfish.TearDown; -begin - FCipher_Blowfish.Free; - FCipher_Blowfish := nil; -end; - -procedure TestTCipher_Blowfish.TestContext; -var - ReturnValue: TCipherContext; -begin - ReturnValue := FCipher_Blowfish.Context; - - CheckEquals( 56, ReturnValue.KeySize); - CheckEquals( 8, ReturnValue.BlockSize); - CheckEquals( 8, ReturnValue.BufferSize); - CheckEquals(4168, ReturnValue.AdditionalBufferSize); - CheckEquals( 1, ReturnValue.MinRounds); - CheckEquals( 1, ReturnValue.MaxRounds); - CheckEquals(false, ReturnValue.NeedsAdditionalBufferBackup); - CheckEquals(true, [ctBlock, ctSymmetric] = ReturnValue.CipherType); -end; - -procedure TestTCipher_Blowfish.TestDecode; -begin - DoTestDecode(FCipher_Blowfish.DecodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_Blowfish.TestEncode; -begin - DoTestEncode(FCipher_Blowfish.EncodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_Blowfish.TestIdentity; -begin - CheckEquals($54E9A294, FCipher_Blowfish.Identity); -end; - -procedure TestTCipher_Blowfish.TestClassByName; -var - ReturnValue : TDECCipherClass; -begin - ReturnValue := FCipher_Blowfish.ClassByName('TCipher_Blowfish'); - CheckEquals(TCipher_Blowfish, ReturnValue, 'Class is not registered'); -end; - -procedure TestTCipher_Twofish.Done; -begin - FCipher_Twofish.Done; -end; - -procedure TestTCipher_Twofish.Init(TestData: TCipherTestData); -begin - LimitKeyLength(TestData.Key, FCipher_Twofish.Context.KeySize); - - FCipher_Twofish.Mode := TestData.Mode; - FCipher_Twofish.Init(BytesOf(TestData.Key), - BytesOf(TestData.InitVector), - TestData.Filler); -end; - -procedure TestTCipher_Twofish.SetUp; -begin - FCipher_Twofish := TCipher_Twofish.Create; - - SetLength(FTestData, 1); - - FTestData[0].OutputData := 'e81674f9bc69442188c949bb52e1e47874171177e99dbbe9880875094f8dfe21'; - FTestData[0].InputData := TFormat_ESCAPE.Decode('\x30\x44\xED\x6E\x45\xA4' + - '\x96\xF5\xF6\x35\xA2\xEB' + - '\x3D\x1A\x5D\xD6\xCB\x1D' + - '\x09\x82\x2D\xBD\xF5\x60' + - '\xC2\xB8\x58\xA1\x91\xF9' + - '\x81\xB1'); - - FTestData[0].Key := 'TCipher_Twofish'; - FTestData[0].InitVector := ''; - FTestData[0].Filler := $FF; - FTestData[0].Mode := cmCTSx; -end; - -procedure TestTCipher_Twofish.TearDown; -begin - FCipher_Twofish.Free; - FCipher_Twofish := nil; -end; - -procedure TestTCipher_Twofish.TestContext; -var - ReturnValue: TCipherContext; -begin - ReturnValue := FCipher_Twofish.Context; - - CheckEquals( 32, ReturnValue.KeySize); - CheckEquals( 16, ReturnValue.BlockSize); - CheckEquals( 16, ReturnValue.BufferSize); - CheckEquals(4256, ReturnValue.AdditionalBufferSize); - CheckEquals( 1, ReturnValue.MinRounds); - CheckEquals( 1, ReturnValue.MaxRounds); - CheckEquals(false, ReturnValue.NeedsAdditionalBufferBackup); - CheckEquals(true, [ctBlock, ctSymmetric] = ReturnValue.CipherType); -end; - -procedure TestTCipher_Twofish.TestDecode; -begin - DoTestDecode(FCipher_Twofish.DecodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_Twofish.TestEncode; -begin - DoTestEncode(FCipher_Twofish.EncodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_Twofish.TestIdentity; -begin - CheckEquals($B38AB3E6, FCipher_Twofish.Identity); -end; - -procedure TestTCipher_Twofish.TestClassByName; -var - ReturnValue : TDECCipherClass; -begin - ReturnValue := FCipher_Twofish.ClassByName('TCipher_Twofish'); - CheckEquals(TCipher_Twofish, ReturnValue, 'Class is not registered'); -end; - -procedure TestTCipher_IDEA.Done; -begin - FCipher_IDEA.Done; -end; - -procedure TestTCipher_IDEA.Init(TestData: TCipherTestData); -begin - LimitKeyLength(TestData.Key, FCipher_IDEA.Context.KeySize); - - FCipher_IDEA.Mode := TestData.Mode; - FCipher_IDEA.Init(BytesOf(TestData.Key), - BytesOf(TestData.InitVector), - TestData.Filler); -end; - -procedure TestTCipher_IDEA.SetUp; -begin - FCipher_IDEA := TCipher_IDEA.Create; - - SetLength(FTestData, 1); - - FTestData[0].OutputData := '8c65cad843e79993ed41ea48fd665094a2256dd7b1d09a233dd2e8ecc9457f7e'; - FTestData[0].InputData := TFormat_ESCAPE.Decode('\x30\x44\xED\x6E\x45\xA4' + - '\x96\xF5\xF6\x35\xA2\xEB' + - '\x3D\x1A\x5D\xD6\xCB\x1D' + - '\x09\x82\x2D\xBD\xF5\x60' + - '\xC2\xB8\x58\xA1\x91\xF9'+ - '\x81\xB1'); - - FTestData[0].Key := 'TCipher_IDEA'; - FTestData[0].InitVector := ''; - FTestData[0].Filler := $FF; - FTestData[0].Mode := cmCTSx; -end; - -procedure TestTCipher_IDEA.TearDown; -begin - FCipher_IDEA.Free; - FCipher_IDEA := nil; -end; - -procedure TestTCipher_IDEA.TestContext; -var - ReturnValue: TCipherContext; -begin - ReturnValue := FCipher_IDEA.Context; - - CheckEquals( 16, ReturnValue.KeySize); - CheckEquals( 8, ReturnValue.BlockSize); - CheckEquals( 8, ReturnValue.BufferSize); - CheckEquals( 208, ReturnValue.AdditionalBufferSize); - CheckEquals( 1, ReturnValue.MinRounds); - CheckEquals( 1, ReturnValue.MaxRounds); - CheckEquals(false, ReturnValue.NeedsAdditionalBufferBackup); - CheckEquals(true, [ctBlock, ctSymmetric] = ReturnValue.CipherType); -end; - -procedure TestTCipher_IDEA.TestDecode; -begin - DoTestDecode(FCipher_IDEA.DecodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_IDEA.TestEncode; -begin - DoTestEncode(FCipher_IDEA.EncodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_IDEA.TestIdentity; -begin - CheckEquals($3938F197, FCipher_IDEA.Identity); -end; - -procedure TestTCipher_IDEA.TestClassByName; -var - ReturnValue : TDECCipherClass; -begin - ReturnValue := FCipher_IDEA.ClassByName('TCipher_IDEA'); - CheckEquals(TCipher_IDEA, ReturnValue, 'Class is not registered'); -end; - -procedure TestTCipher_Cast256.Done; -begin - FCipher_Cast256.Done; -end; - -procedure TestTCipher_Cast256.Init(TestData: TCipherTestData); -begin - LimitKeyLength(TestData.Key, FCipher_Cast256.Context.KeySize); - - FCipher_Cast256.Mode := TestData.Mode; - FCipher_Cast256.Init(BytesOf(TestData.Key), - BytesOf(TestData.InitVector), - TestData.Filler); -end; - -procedure TestTCipher_Cast256.SetUp; -begin - FCipher_Cast256 := TCipher_Cast256.Create; - - SetLength(FTestData, 1); - - FTestData[0].OutputData := '45820e97772071993e2945d5594feca5cd583875469ca7c5faa6339c82fb9254'; - FTestData[0].InputData := TFormat_ESCAPE.Decode('\x30\x44\xED\x6E\x45\xA4' + - '\x96\xF5\xF6\x35\xA2\xEB' + - '\x3D\x1A\x5D\xD6\xCB\x1D' + - '\x09\x82\x2D\xBD\xF5\x60' + - '\xC2\xB8\x58\xA1\x91\xF9' + - '\x81\xB1'); - - FTestData[0].Key := 'TCipher_CAST256'; - FTestData[0].InitVector := ''; - FTestData[0].Filler := $FF; - FTestData[0].Mode := cmCTSx; -end; - -procedure TestTCipher_Cast256.TearDown; -begin - FCipher_Cast256.Free; - FCipher_Cast256 := nil; -end; - -procedure TestTCipher_Cast256.TestContext; -var - ReturnValue: TCipherContext; -begin - ReturnValue := FCipher_Cast256.Context; - - CheckEquals( 32, ReturnValue.KeySize); - CheckEquals( 16, ReturnValue.BlockSize); - CheckEquals( 16, ReturnValue.BufferSize); - CheckEquals( 384, ReturnValue.AdditionalBufferSize); - CheckEquals( 1, ReturnValue.MinRounds); - CheckEquals( 1, ReturnValue.MaxRounds); - CheckEquals(false, ReturnValue.NeedsAdditionalBufferBackup); - CheckEquals(true, [ctBlock, ctSymmetric] = ReturnValue.CipherType); -end; - -procedure TestTCipher_Cast256.TestDecode; -begin - DoTestDecode(FCipher_Cast256.DecodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_Cast256.TestEncode; -begin - DoTestEncode(FCipher_Cast256.EncodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_Cast256.TestIdentity; -begin - CheckEquals($47C2021C, FCipher_Cast256.Identity); -end; - -procedure TestTCipher_Cast256.TestClassByName; -var - ReturnValue : TDECCipherClass; -begin - ReturnValue := FCipher_Cast256.ClassByName('TCipher_Cast256'); - CheckEquals(TCipher_Cast256, ReturnValue, 'Class is not registered'); -end; - -procedure TestTCipher_Mars.Done; -begin - FCipher_Mars.Done; -end; - -procedure TestTCipher_Mars.Init(TestData: TCipherTestData); -begin - LimitKeyLength(TestData.Key, FCipher_Mars.Context.KeySize); - - FCipher_Mars.Mode := TestData.Mode; - FCipher_Mars.Init(BytesOf(TestData.Key), - BytesOf(TestData.InitVector), - TestData.Filler); -end; - -procedure TestTCipher_Mars.SetUp; -begin - FCipher_Mars := TCipher_Mars.Create; - - SetLength(FTestData, 1); - - FTestData[0].OutputData := 'fda54d3c1d79739ceaf668675595e210b145bfa9e4ab65efaa68c88ea34ab09d'; - FTestData[0].InputData := TFormat_ESCAPE.Decode('\x30\x44\xED\x6E\x45\xA4' + - '\x96\xF5\xF6\x35\xA2\xEB' + - '\x3D\x1A\x5D\xD6\xCB\x1D' + - '\x09\x82\x2D\xBD\xF5\x60' + - '\xC2\xB8\x58\xA1\x91\xF9' + - '\x81\xB1'); - - FTestData[0].Key := 'TCipher_Mars'; - FTestData[0].InitVector := ''; - FTestData[0].Filler := $FF; - FTestData[0].Mode := cmCTSx; -end; - -procedure TestTCipher_Mars.TearDown; -begin - FCipher_Mars.Free; - FCipher_Mars := nil; -end; - -procedure TestTCipher_Mars.TestContext; -var - ReturnValue: TCipherContext; -begin - ReturnValue := FCipher_Mars.Context; - - CheckEquals( 56, ReturnValue.KeySize); - CheckEquals( 16, ReturnValue.BlockSize); - CheckEquals( 16, ReturnValue.BufferSize); - CheckEquals( 160, ReturnValue.AdditionalBufferSize); - CheckEquals( 1, ReturnValue.MinRounds); - CheckEquals( 1, ReturnValue.MaxRounds); - CheckEquals(false, ReturnValue.NeedsAdditionalBufferBackup); - CheckEquals(true, [ctBlock, ctSymmetric] = ReturnValue.CipherType); -end; - -procedure TestTCipher_Mars.TestDecode; -begin - DoTestDecode(FCipher_Mars.DecodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_Mars.TestEncode; -begin - DoTestEncode(FCipher_Mars.EncodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_Mars.TestIdentity; -begin - CheckEquals($46AB51F5, FCipher_Mars.Identity); -end; - -procedure TestTCipher_Mars.TestClassByName; -// ClassByName Tests for die restlichen Ciphers umsetzen! -var - ReturnValue : TDECCipherClass; -begin - ReturnValue := FCipher_Mars.ClassByName('TCipher_Mars'); - CheckEquals(TCipher_Mars, ReturnValue, 'Class is not registered'); -end; - -procedure TestTCipher_RC4.Done; -begin - FCipher_RC4.Done; -end; - -procedure TestTCipher_RC4.Init(TestData: TCipherTestData); -begin - LimitKeyLength(TestData.Key, FCipher_RC4.Context.KeySize); - - FCipher_RC4.Mode := TestData.Mode; - FCipher_RC4.Init(BytesOf(TestData.Key), - BytesOf(TestData.InitVector), - TestData.Filler); -end; - -procedure TestTCipher_RC4.SetUp; -begin - FCipher_RC4 := TCipher_RC4.Create; - - SetLength(FTestData, 1); - - FTestData[0].OutputData := 'cfbb1291ba5b690a09ca5d14c2e5a229196d183f5539a4edc56c2bfb7c12d630'; - FTestData[0].InputData := TFormat_ESCAPE.Decode('\x30\x44\xED\x6E\x45\xA4' + - '\x96\xF5\xF6\x35\xA2\xEB' + - '\x3D\x1A\x5D\xD6\xCB\x1D' + - '\x09\x82\x2D\xBD\xF5\x60' + - '\xC2\xB8\x58\xA1\x91\xF9' + - '\x81\xB1'); - - FTestData[0].Key := 'TCipher_RC4'; - FTestData[0].InitVector := ''; - FTestData[0].Filler := $FF; - FTestData[0].Mode := cmCTSx; -end; - -procedure TestTCipher_RC4.TearDown; -begin - FCipher_RC4.Free; - FCipher_RC4 := nil; -end; - -procedure TestTCipher_RC4.TestClassByName; -var - ReturnValue : TDECCipherClass; -begin - ReturnValue := FCipher_RC4.ClassByName('TCipher_RC4'); - CheckEquals(TCipher_RC4, ReturnValue, 'Class is not registered'); -end; - -procedure TestTCipher_RC4.TestContext; -var - ReturnValue: TCipherContext; -begin - ReturnValue := FCipher_RC4.Context; - - CheckEquals( 256, ReturnValue.KeySize); - CheckEquals( 1, ReturnValue.BlockSize); - CheckEquals( 16, ReturnValue.BufferSize); - CheckEquals( 258, ReturnValue.AdditionalBufferSize); - CheckEquals( 1, ReturnValue.MinRounds); - CheckEquals( 1, ReturnValue.MaxRounds); - CheckEquals(true, ReturnValue.NeedsAdditionalBufferBackup); - CheckEquals(true, [ctStream, ctSymmetric] = ReturnValue.CipherType); -end; - -procedure TestTCipher_RC4.TestDecode; -begin - DoTestDecode(FCipher_RC4.DecodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_RC4.TestEncode; -begin - DoTestEncode(FCipher_RC4.EncodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_RC4.TestIdentity; -begin - CheckEquals($73A3DF5A, FCipher_RC4.Identity); -end; - -procedure TestTCipher_RC6.Done; -begin - FCipher_RC6.Done; -end; - -procedure TestTCipher_RC6.Init(TestData: TCipherTestData); -begin - LimitKeyLength(TestData.Key, FCipher_RC6.Context.KeySize); - - FCipher_RC6.Mode := TestData.Mode; - FCipher_RC6.Init(BytesOf(TestData.Key), - BytesOf(TestData.InitVector), - TestData.Filler); -end; - -procedure TestTCipher_RC6.SetUp; -begin - FCipher_RC6 := TCipher_RC6.Create; - - SetLength(FTestData, 1); - - FTestData[0].OutputData := '987165a110febdf907853efc21dbfca18f5f8bf74528810def9a227af0622cc6'; - FTestData[0].InputData := TFormat_ESCAPE.Decode('\x30\x44\xED\x6E\x45\xA4' + - '\x96\xF5\xF6\x35\xA2\xEB' + - '\x3D\x1A\x5D\xD6\xCB\x1D' + - '\x09\x82\x2D\xBD\xF5\x60' + - '\xC2\xB8\x58\xA1\x91\xF9' + - '\x81\xB1'); - - FTestData[0].Key := 'TCipher_RC6'; - FTestData[0].InitVector := ''; - FTestData[0].Filler := $FF; - FTestData[0].Mode := cmCTSx; -end; - -procedure TestTCipher_RC6.TearDown; -begin - FCipher_RC6.Free; - FCipher_RC6 := nil; -end; - -procedure TestTCipher_RC6.TestClassByName; -var - ReturnValue : TDECCipherClass; -begin - ReturnValue := FCipher_RC6.ClassByName('TCipher_RC6'); - CheckEquals(TCipher_RC6, ReturnValue, 'Class is not registered'); -end; - -procedure TestTCipher_RC6.TestContext; -var - ReturnValue: TCipherContext; -begin - ReturnValue := FCipher_RC6.Context; - - CheckEquals( 256, ReturnValue.KeySize); - CheckEquals( 16, ReturnValue.BlockSize); - CheckEquals( 16, ReturnValue.BufferSize); - CheckEquals( 272, ReturnValue.AdditionalBufferSize); - CheckEquals( 16, ReturnValue.MinRounds); - CheckEquals( 24, ReturnValue.MaxRounds); - CheckEquals(false, ReturnValue.NeedsAdditionalBufferBackup); - CheckEquals(true, [ctBlock, ctSymmetric] = ReturnValue.CipherType); -end; - -procedure TestTCipher_RC6.TestDecode; -begin - DoTestDecode(FCipher_RC6.DecodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_RC6.TestEncode; -begin - DoTestEncode(FCipher_RC6.EncodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_RC6.TestIdentity; -begin - CheckEquals($9DADBE76, FCipher_RC6.Identity); -end; - -procedure TestTCipher_Square.Done; -begin - FCipher_Square.Done; -end; - -procedure TestTCipher_Square.Init(TestData: TCipherTestData); -begin - LimitKeyLength(TestData.Key, FCipher_Square.Context.KeySize); - - FCipher_Square.Mode := TestData.Mode; - FCipher_Square.Init(BytesOf(TestData.Key), - BytesOf(TestData.InitVector), - TestData.Filler); -end; - -procedure TestTCipher_Square.SetUp; -begin - FCipher_Square := TCipher_Square.Create; - - SetLength(FTestData, 1); - - FTestData[0].OutputData := '439ca6c467e82e472295668506396ac9182120f74436f1617d1490b1a96856c7'; - FTestData[0].InputData := TFormat_ESCAPE.Decode('\x30\x44\xED\x6E\x45\xA4' + - '\x96\xF5\xF6\x35\xA2\xEB' + - '\x3D\x1A\x5D\xD6\xCB\x1D' + - '\x09\x82\x2D\xBD\xF5\x60' + - '\xC2\xB8\x58\xA1\x91\xF9' + - '\x81\xB1'); - - FTestData[0].Key := 'TCipher_Square'; - FTestData[0].InitVector := ''; - FTestData[0].Filler := $FF; - FTestData[0].Mode := cmCTSx; -end; - -procedure TestTCipher_Square.TearDown; -begin - FCipher_Square.Free; - FCipher_Square := nil; -end; - -procedure TestTCipher_Square.TestClassByName; -var - ReturnValue : TDECCipherClass; -begin - ReturnValue := FCipher_Square.ClassByName('TCipher_Square'); - CheckEquals(TCipher_Square, ReturnValue, 'Class is not registered'); -end; - -procedure TestTCipher_Square.TestContext; -var - ReturnValue: TCipherContext; -begin - ReturnValue := FCipher_Square.Context; - - CheckEquals( 16, ReturnValue.KeySize); - CheckEquals( 16, ReturnValue.BlockSize); - CheckEquals( 16, ReturnValue.BufferSize); - CheckEquals( 288, ReturnValue.AdditionalBufferSize); - CheckEquals( 1, ReturnValue.MinRounds); - CheckEquals( 1, ReturnValue.MaxRounds); - CheckEquals(false, ReturnValue.NeedsAdditionalBufferBackup); - CheckEquals(true, [ctBlock, ctSymmetric] = ReturnValue.CipherType); -end; - -procedure TestTCipher_Square.TestDecode; -begin - DoTestDecode(FCipher_Square.DecodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_Square.TestEncode; -begin - DoTestEncode(FCipher_Square.EncodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_Square.TestIdentity; -begin - CheckEquals($2954C319, FCipher_Square.Identity); -end; - -procedure TestTCipher_SCOP.Done; -begin - FCipher_SCOP.Done; -end; - -procedure TestTCipher_SCOP.Init(TestData: TCipherTestData); -begin - LimitKeyLength(TestData.Key, FCipher_SCOP.Context.KeySize); - - FCipher_SCOP.Mode := TestData.Mode; - FCipher_SCOP.Init(BytesOf(TestData.Key), - BytesOf(TestData.InitVector), - TestData.Filler); -end; - -procedure TestTCipher_SCOP.SetUp; -begin - FCipher_SCOP := TCipher_SCOP.Create; - - SetLength(FTestData, 18); - - // Standard test vector - FTestData[0].OutputData := 'ca29853fb7eec7f958931ff185c0b415c944c22f13e34423aba1a84fb3101f19'; - FTestData[0].InputData := TFormat_ESCAPE.Decode('\x30\x44\xED\x6E\x45\xA4' + - '\x96\xF5\xF6\x35\xA2\xEB' + - '\x3D\x1A\x5D\xD6\xCB\x1D' + - '\x09\x82\x2D\xBD\xF5\x60' + - '\xC2\xB8\x58\xA1\x91\xF9' + - '\x81\xB1'); - FTestData[0].Key := 'TCipher_SCOP'; - FTestData[0].InitVector := ''; - FTestData[0].Filler := $FF; - FTestData[0].Mode := cmECBx; - - // Standard test vector, key with 'odd' bit set - FTestData[1].OutputData := '18be1fff893de279b5768b21307c5c52436835c83ed5c96ea589884b61c69dc4'; - FTestData[1].InputData := TFormat_ESCAPE.Decode('\x30\x44\xED\x6E\x45\xA4' + - '\x96\xF5\xF6\x35\xA2\xEB' + - '\x3D\x1A\x5D\xD6\xCB\x1D' + - '\x09\x82\x2D\xBD\xF5\x60' + - '\xC2\xB8\x58\xA1\x91\xF9' + - '\x81\xB1'); - FTestData[1].Key := 'TCipher_SCOPa'; - FTestData[1].InitVector := ''; - FTestData[1].Filler := $FF; - FTestData[1].Mode := cmECBx; - - // Full 48 bytes key length - FTestData[2].OutputData := '8dbc6579ac264ccfbb0f7aea'; - FTestData[2].InputData := '12bytesbytes'; - FTestData[2].Key := 'TCipher_SCOPTCipher_SCOPTCipher_SCOPTCipher_SCOP'; - FTestData[2].InitVector := ''; - FTestData[2].Filler := $FF; - FTestData[2].Mode := cmECBx; - - // Source until SourceEnd: Test data as generated from the original SCOP test - // program written in C - FTestData[3].OutputData := 'ce5d5f193d3b9d41f06c6135c3a3f66dbe0d798d58200d5d21a2727ba6b998afe04bb0de8a3cbb7d'; - FTestData[3].InputData := #$0#$0#$0#$0#$0#$0#$0#$0#$0#$0#$0#$0#$0#$0#$0#$0#$0#$0#$0#$0 + - #$0#$0#$0#$0#$0#$0#$0#$0#$0#$0#$0#$0#$0#$0#$0#$0#$0#$0#$0#$0; - FTestData[3].Key := #0#1#2#3#4#5#6#7#8#9#$A#$B#$C#$D#$E#$F; - FTestData[3].InitVector := ''; - FTestData[3].Filler := $FF; - FTestData[3].Mode := cmECBx; - - FTestData[4].OutputData := 'cf5e601a'; - FTestData[4].InputData := #$1#$1#$1#$1; - FTestData[4].Key := #0#1#2#3#4#5#6#7#8#9#$A#$B#$C#$D#$E#$F; - FTestData[4].InitVector := ''; - FTestData[4].Filler := $FF; - FTestData[4].Mode := cmECBx; - - FTestData[5].OutputData := 'cf5e601b'; - FTestData[5].InputData := #$1#$1#$1#$2; - FTestData[5].Key := #0#1#2#3#4#5#6#7#8#9#$A#$B#$C#$D#$E#$F; - FTestData[5].InitVector := ''; - FTestData[5].Filler := $FF; - FTestData[5].Mode := cmECBx; - - FTestData[6].OutputData := 'cf5e611b'; - FTestData[6].InputData := #$1#$1#$2#$2; - FTestData[6].Key := #0#1#2#3#4#5#6#7#8#9#$A#$B#$C#$D#$E#$F; - FTestData[6].InitVector := ''; - FTestData[6].Filler := $FF; - FTestData[6].Mode := cmECBx; - - FTestData[7].OutputData := 'cf5f611b'; - FTestData[7].InputData := #$1#$2#$2#$2; - FTestData[7].Key := #0#1#2#3#4#5#6#7#8#9#$A#$B#$C#$D#$E#$F; - FTestData[7].InitVector := ''; - FTestData[7].Filler := $FF; - FTestData[7].Mode := cmECBx; - - FTestData[8].OutputData := 'd05f611b'; - FTestData[8].InputData := #$2#$2#$2#$2; - FTestData[8].Key := #0#1#2#3#4#5#6#7#8#9#$A#$B#$C#$D#$E#$F; - FTestData[8].InitVector := ''; - FTestData[8].Filler := $FF; - FTestData[8].Mode := cmECBx; - - FTestData[9].OutputData := 'cf5e601a3e3c9e42'; - FTestData[9].InputData := #$1#$1#$1#$1#$1#$1#$1#$1; - FTestData[9].Key := #0#1#2#3#4#5#6#7#8#9#$A#$B#$C#$D#$E#$F; - FTestData[9].InitVector := ''; - FTestData[9].Filler := $FF; - FTestData[9].Mode := cmECBx; - - FTestData[10].OutputData := 'cf5e601a3e3c9e43'; - FTestData[10].InputData := #$1#$1#$1#$1#$1#$1#$1#$2; - FTestData[10].Key := #0#1#2#3#4#5#6#7#8#9#$A#$B#$C#$D#$E#$F; - FTestData[10].InitVector := ''; - FTestData[10].Filler := $FF; - FTestData[10].Mode := cmECBx; - - FTestData[11].OutputData := 'cf5e601a3e3c9f43'; - FTestData[11].InputData := #$1#$1#$1#$1#$1#$1#$2#$2; - FTestData[11].Key := #0#1#2#3#4#5#6#7#8#9#$A#$B#$C#$D#$E#$F; - FTestData[11].InitVector := ''; - FTestData[11].Filler := $FF; - FTestData[11].Mode := cmECBx; - - FTestData[12].OutputData := 'cf5e601a3e3d9f43'; - FTestData[12].InputData := #$1#$1#$1#$1#$1#$2#$2#$2; - FTestData[12].Key := #0#1#2#3#4#5#6#7#8#9#$A#$B#$C#$D#$E#$F; - FTestData[12].InitVector := ''; - FTestData[12].Filler := $FF; - FTestData[12].Mode := cmECBx; - - FTestData[13].OutputData := 'cf5e601a3f3d9f43'; - FTestData[13].InputData := #$1#$1#$1#$1#$2#$2#$2#$2; - FTestData[13].Key := #0#1#2#3#4#5#6#7#8#9#$A#$B#$C#$D#$E#$F; - FTestData[13].InitVector := ''; - FTestData[13].Filler := $FF; - FTestData[13].Mode := cmECBx; - - FTestData[14].OutputData := 'cf5e601b3f3d9f43'; - FTestData[14].InputData := #$1#$1#$1#$2#$2#$2#$2#$2; - FTestData[14].Key := #0#1#2#3#4#5#6#7#8#9#$A#$B#$C#$D#$E#$F; - FTestData[14].InitVector := ''; - FTestData[14].Filler := $FF; - FTestData[14].Mode := cmECBx; - - FTestData[15].OutputData := 'cf5e611b3f3d9f43'; - FTestData[15].InputData := #$1#$1#$2#$2#$2#$2#$2#$2; - FTestData[15].Key := #0#1#2#3#4#5#6#7#8#9#$A#$B#$C#$D#$E#$F; - FTestData[15].InitVector := ''; - FTestData[15].Filler := $FF; - FTestData[15].Mode := cmECBx; - - FTestData[16].OutputData := 'cf5f611b3f3d9f43'; - FTestData[16].InputData := #$1#$2#$2#$2#$2#$2#$2#$2; - FTestData[16].Key := #0#1#2#3#4#5#6#7#8#9#$A#$B#$C#$D#$E#$F; - FTestData[16].InitVector := ''; - FTestData[16].Filler := $FF; - FTestData[16].Mode := cmECBx; - - FTestData[17].OutputData := 'd05f611b3f3d9f43'; - FTestData[17].InputData := #$2#$2#$2#$2#$2#$2#$2#$2; - FTestData[17].Key := #0#1#2#3#4#5#6#7#8#9#$A#$B#$C#$D#$E#$F; - FTestData[17].InitVector := ''; - FTestData[17].Filler := $FF; - FTestData[17].Mode := cmECBx; - // SourceEnd -end; - -procedure TestTCipher_SCOP.TearDown; -begin - FCipher_SCOP.Free; - FCipher_SCOP := nil; -end; - -procedure TestTCipher_SCOP.TestClassByName; -var - ReturnValue : TDECCipherClass; -begin - ReturnValue := FCipher_SCOP.ClassByName('TCipher_SCOP'); - CheckEquals(TCipher_SCOP, ReturnValue, 'Class is not registered'); -end; - -procedure TestTCipher_SCOP.TestContext; -var - ReturnValue: TCipherContext; -begin - ReturnValue := FCipher_SCOP.Context; - - CheckEquals( 48, ReturnValue.KeySize); - CheckEquals( 4, ReturnValue.BlockSize); - CheckEquals( 32, ReturnValue.BufferSize); - CheckEquals(1548, ReturnValue.AdditionalBufferSize); - CheckEquals( 1, ReturnValue.MinRounds); - CheckEquals( 1, ReturnValue.MaxRounds); - CheckEquals(true, ReturnValue.NeedsAdditionalBufferBackup); - CheckEquals(true, [ctStream, ctSymmetric] = ReturnValue.CipherType); -end; - -procedure TestTCipher_SCOP.TestDecode; -begin - DoTestDecode(FCipher_SCOP.DecodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_SCOP.TestEncode; -begin - DoTestEncode(FCipher_SCOP.EncodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_SCOP.TestIdentity; -begin - CheckEquals($938C9891, FCipher_Scop.Identity); -end; - -procedure TestTCipher_SCOP_DEC52.Done; -begin - FCipher_SCOP_DEC52.Done; -end; - -procedure TestTCipher_SCOP_DEC52.DoTestClassByName; -var - ReturnValue : TDECCipherClass; -begin - ReturnValue := FCipher_SCOP_DEC52.ClassByName('TCipher_SCOP_DEC52'); - // This line should never be executed due to ClassByName rising an exception - // but it suppresses a ReturnValue is not being used compiler warning - CheckEquals(TCipher_SCOP_DEC52, ReturnValue, 'Class is not registered'); -end; - -procedure TestTCipher_SCOP_DEC52.Init(TestData: TCipherTestData); -begin - LimitKeyLength(TestData.Key, FCipher_SCOP_DEC52.Context.KeySize); - - FCipher_SCOP_DEC52.Mode := TestData.Mode; - FCipher_SCOP_DEC52.Init(BytesOf(TestData.Key), - BytesOf(TestData.InitVector), - TestData.Filler); -end; - -procedure TestTCipher_SCOP_DEC52.SetUp; -begin - FCipher_SCOP_DEC52 := TCipher_SCOP_DEC52.Create; - - SetLength(FTestData, 1); - - FTestData[0].OutputData := 'b1a7ee707aab160af9b9c3ebc2db5ee814a28995d2c1f994c53ca159ee052632'; - FTestData[0].InputData := TFormat_ESCAPE.Decode('\x30\x44\xED\x6E\x45\xA4' + - '\x96\xF5\xF6\x35\xA2\xEB' + - '\x3D\x1A\x5D\xD6\xCB\x1D' + - '\x09\x82\x2D\xBD\xF5\x60' + - '\xC2\xB8\x58\xA1\x91\xF9' + - '\x81\xB1'); - - FTestData[0].Key := 'TCipher_SCOP'; - FTestData[0].InitVector := ''; - FTestData[0].Filler := $FF; - FTestData[0].Mode := cmCTSx; -end; - -procedure TestTCipher_SCOP_DEC52.TearDown; -begin - FCipher_SCOP_DEC52.Free; - FCipher_SCOP_DEC52 := nil; -end; - -procedure TestTCipher_SCOP_DEC52.TestClassByName; -begin - // Class shall not be registered by default - CheckException(DoTestClassByName, EDECClassNotRegisteredException); -end; - -procedure TestTCipher_SCOP_DEC52.TestContext; -var - ReturnValue: TCipherContext; -begin - ReturnValue := FCipher_SCOP_DEC52.Context; - - CheckEquals( 48, ReturnValue.KeySize); - CheckEquals( 4, ReturnValue.BlockSize); - CheckEquals( 32, ReturnValue.BufferSize); - CheckEquals(1548, ReturnValue.AdditionalBufferSize); - CheckEquals( 1, ReturnValue.MinRounds); - CheckEquals( 1, ReturnValue.MaxRounds); - CheckEquals(true, ReturnValue.NeedsAdditionalBufferBackup); - CheckEquals(true, [ctStream, ctSymmetric] = ReturnValue.CipherType); -end; - -procedure TestTCipher_SCOP_DEC52.TestDecode; -begin - DoTestDecode(FCipher_SCOP_DEC52.DecodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_SCOP_DEC52.TestEncode; -begin - DoTestEncode(FCipher_SCOP_DEC52.EncodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_SCOP_DEC52.TestIdentity; -begin - CheckEquals($398EE1E3, FCipher_SCOP_DEC52.Identity); -end; - -procedure TestTCipher_Sapphire.Done; -begin - FCipher_Sapphire.Done; -end; - -procedure TestTCipher_Sapphire.Init(TestData: TCipherTestData); -begin - LimitKeyLength(TestData.Key, FCipher_Sapphire.Context.KeySize); - - FCipher_Sapphire.Mode := TestData.Mode; - FCipher_Sapphire.Init(BytesOf(TestData.Key), - BytesOf(TestData.InitVector), - TestData.Filler); -end; - -procedure TestTCipher_Sapphire.SetUp; -begin - FCipher_Sapphire := TCipher_Sapphire.Create; - - SetLength(FTestData, 1); - - FTestData[0].OutputData := 'cff8d04e8e0d42e6aef37afaacbe9b08850c4d0c75ac54c0b9388e54e5609650'; - FTestData[0].InputData := TFormat_ESCAPE.Decode('\x30\x44\xED\x6E\x45\xA4' + - '\x96\xF5\xF6\x35\xA2\xEB' + - '\x3D\x1A\x5D\xD6\xCB\x1D' + - '\x09\x82\x2D\xBD\xF5\x60' + - '\xC2\xB8\x58\xA1\x91\xF9' + - '\x81\xB1'); - - FTestData[0].Key := 'TCipher_Sapphire'; - FTestData[0].InitVector := ''; - FTestData[0].Filler := $FF; - FTestData[0].Mode := cmCTSx; -end; - -procedure TestTCipher_Sapphire.TearDown; -begin - FCipher_Sapphire.Free; - FCipher_Sapphire := nil; -end; - -procedure TestTCipher_Sapphire.TestClassByName; -var - ReturnValue : TDECCipherClass; -begin - ReturnValue := FCipher_Sapphire.ClassByName('TCipher_Sapphire'); - CheckEquals(TCipher_Sapphire, ReturnValue, 'Class is not registered'); -end; - -procedure TestTCipher_Sapphire.TestContext; -var - ReturnValue: TCipherContext; -begin - ReturnValue := FCipher_Sapphire.Context; - - CheckEquals(1024, ReturnValue.KeySize); - CheckEquals( 1, ReturnValue.BlockSize); - CheckEquals( 32, ReturnValue.BufferSize); - CheckEquals(1044, ReturnValue.AdditionalBufferSize); - CheckEquals( 1, ReturnValue.MinRounds); - CheckEquals( 1, ReturnValue.MaxRounds); - CheckEquals(true, ReturnValue.NeedsAdditionalBufferBackup); - CheckEquals(true, [ctStream, ctSymmetric] = ReturnValue.CipherType); -end; - -procedure TestTCipher_Sapphire.TestDecode; -begin - DoTestDecode(FCipher_Sapphire.DecodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_Sapphire.TestEncode; -begin - DoTestEncode(FCipher_Sapphire.EncodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_Sapphire.TestIdentity; -begin - CheckEquals($42FAA470, FCipher_Sapphire.Identity); -end; - -procedure TestTCipher_1DES.Done; -begin - FCipher_1DES.Done; -end; - -procedure TestTCipher_1DES.Init(TestData: TCipherTestData); -begin - LimitKeyLength(TestData.Key, FCipher_1DES.Context.KeySize); - - FCipher_1DES.Mode := TestData.Mode; - FCipher_1DES.Init(BytesOf(TestData.Key), - BytesOf(TestData.InitVector), - TestData.Filler); -end; - -procedure TestTCipher_1DES.SetUp; -begin - FCipher_1DES := TCipher_1DES.Create; - - SetLength(FTestData, 1); - - FTestData[0].OutputData := 'ad6942bbf668204d53cdc762139398c0300d850be2aa72096fdb5f8ed3e4cf8a'; - FTestData[0].InputData := TFormat_ESCAPE.Decode('\x30\x44\xED\x6E\x45\xA4' + - '\x96\xF5\xF6\x35\xA2\xEB' + - '\x3D\x1A\x5D\xD6\xCB\x1D' + - '\x09\x82\x2D\xBD\xF5\x60' + - '\xC2\xB8\x58\xA1\x91\xF9' + - '\x81\xB1'); - - FTestData[0].Key := 'TCipher_1DES'; - FTestData[0].InitVector := ''; - FTestData[0].Filler := $FF; - FTestData[0].Mode := cmCTSx; -end; - -procedure TestTCipher_1DES.TearDown; -begin - FCipher_1DES.Free; - FCipher_1DES := nil; -end; - -procedure TestTCipher_1DES.TestClassByName; -var - ReturnValue : TDECCipherClass; -begin - ReturnValue := FCipher_1DES.ClassByName('TCipher_1DES'); - CheckEquals(TCipher_1DES, ReturnValue, 'Class is not registered'); -end; - -procedure TestTCipher_1DES.TestContext; -var - ReturnValue: TCipherContext; -begin - ReturnValue := FCipher_1DES.Context; - - CheckEquals( 8, ReturnValue.KeySize); - CheckEquals( 8, ReturnValue.BlockSize); - CheckEquals( 8, ReturnValue.BufferSize); - CheckEquals( 256, ReturnValue.AdditionalBufferSize); - CheckEquals( 1, ReturnValue.MinRounds); - CheckEquals( 1, ReturnValue.MaxRounds); - CheckEquals(false, ReturnValue.NeedsAdditionalBufferBackup); - CheckEquals(true, [ctBlock, ctSymmetric] = ReturnValue.CipherType); -end; - -procedure TestTCipher_1DES.TestDecode; -begin - DoTestDecode(FCipher_1DES.DecodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_1DES.TestEncode; -begin - DoTestEncode(FCipher_1DES.EncodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_1DES.TestIdentity; -begin - CheckEquals($640A08AC, FCipher_1DES.Identity); -end; - -procedure TestTCipher_2DES.Done; -begin - FCipher_2DES.Done; -end; - -procedure TestTCipher_2DES.Init(TestData: TCipherTestData); -begin - LimitKeyLength(TestData.Key, FCipher_2DES.Context.KeySize); - - FCipher_2DES.Mode := TestData.Mode; - FCipher_2DES.Init(BytesOf(TestData.Key), - BytesOf(TestData.InitVector), - TestData.Filler); -end; - -procedure TestTCipher_2DES.SetUp; -begin - FCipher_2DES := TCipher_2DES.Create; - - SetLength(FTestData, 1); - - FTestData[0].OutputData := '665c7927e91c8ba0a9e4995a158cbd465c9c75913c38069d75b47e68e947fdab'; - FTestData[0].InputData := TFormat_ESCAPE.Decode('\x30\x44\xED\x6E\x45\xA4' + - '\x96\xF5\xF6\x35\xA2\xEB' + - '\x3D\x1A\x5D\xD6\xCB\x1D' + - '\x09\x82\x2D\xBD\xF5\x60' + - '\xC2\xB8\x58\xA1\x91\xF9' + - '\x81\xB1'); - - FTestData[0].Key := 'TCipher_2DES'; - FTestData[0].InitVector := ''; - FTestData[0].Filler := $FF; - FTestData[0].Mode := cmCTSx; -end; - -procedure TestTCipher_2DES.TearDown; -begin - FCipher_2DES.Free; - FCipher_2DES := nil; -end; - -procedure TestTCipher_2DES.TestClassByName; -var - ReturnValue : TDECCipherClass; -begin - ReturnValue := FCipher_2DES.ClassByName('TCipher_2DES'); - CheckEquals(TCipher_2DES, ReturnValue, 'Class is not registered'); -end; - -procedure TestTCipher_2DES.TestContext; -var - ReturnValue: TCipherContext; -begin - ReturnValue := FCipher_2DES.Context; - - CheckEquals( 16, ReturnValue.KeySize); - CheckEquals( 8, ReturnValue.BlockSize); - CheckEquals( 8, ReturnValue.BufferSize); - CheckEquals( 512, ReturnValue.AdditionalBufferSize); - CheckEquals( 1, ReturnValue.MinRounds); - CheckEquals( 1, ReturnValue.MaxRounds); - CheckEquals(false, ReturnValue.NeedsAdditionalBufferBackup); - CheckEquals(true, [ctBlock, ctSymmetric] = ReturnValue.CipherType); -end; - -procedure TestTCipher_2DES.TestDecode; -begin - DoTestDecode(FCipher_2DES.DecodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_2DES.TestEncode; -begin - DoTestEncode(FCipher_2DES.EncodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_2DES.TestIdentity; -begin - CheckEquals($76BFA742, FCipher_2DES.Identity); -end; - -procedure TestTCipher_3DES.Done; -begin - FCipher_3DES.Done; -end; - -procedure TestTCipher_3DES.Init(TestData: TCipherTestData); -begin - LimitKeyLength(TestData.Key, FCipher_3DES.Context.KeySize); - - FCipher_3DES.Mode := TestData.Mode; - FCipher_3DES.Init(BytesOf(TestData.Key), - BytesOf(TestData.InitVector), - TestData.Filler); -end; - -procedure TestTCipher_3DES.SetUp; -begin - FCipher_3DES := TCipher_3DES.Create; - - SetLength(FTestData, 1); - - FTestData[0].OutputData := '074c14f3e22e08d964bf6f82b5dff0a22f2d3bdb17db25b6b51efa71372fd172'; - FTestData[0].InputData := TFormat_ESCAPE.Decode('\x30\x44\xED\x6E\x45\xA4' + - '\x96\xF5\xF6\x35\xA2\xEB' + - '\x3D\x1A\x5D\xD6\xCB\x1D' + - '\x09\x82\x2D\xBD\xF5\x60' + - '\xC2\xB8\x58\xA1\x91\xF9' + - '\x81\xB1'); - - FTestData[0].Key := 'TCipher_3DES'; - FTestData[0].InitVector := ''; - FTestData[0].Filler := $FF; - FTestData[0].Mode := cmCTSx; -end; - -procedure TestTCipher_3DES.TearDown; -begin - FCipher_3DES.Free; - FCipher_3DES := nil; -end; - -procedure TestTCipher_3DES.TestClassByName; -var - ReturnValue : TDECCipherClass; -begin - ReturnValue := FCipher_3DES.ClassByName('TCipher_3DES'); - CheckEquals(TCipher_3DES, ReturnValue, 'Class is not registered'); -end; - -procedure TestTCipher_3DES.TestContext; -var - ReturnValue: TCipherContext; -begin - ReturnValue := FCipher_3DES.Context; - - CheckEquals( 24, ReturnValue.KeySize); - CheckEquals( 8, ReturnValue.BlockSize); - CheckEquals( 8, ReturnValue.BufferSize); - CheckEquals( 768, ReturnValue.AdditionalBufferSize); - CheckEquals( 1, ReturnValue.MinRounds); - CheckEquals( 1, ReturnValue.MaxRounds); - CheckEquals(false, ReturnValue.NeedsAdditionalBufferBackup); - CheckEquals(true, [ctBlock, ctSymmetric] = ReturnValue.CipherType); -end; - -procedure TestTCipher_3DES.TestDecode; -begin - DoTestDecode(FCipher_3DES.DecodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_3DES.TestEncode; -begin - DoTestEncode(FCipher_3DES.EncodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_3DES.TestIdentity; -begin - CheckEquals($CE03C027, FCipher_3DES.Identity); -end; - -procedure TestTCipher_2DDES.Done; -begin - FCipher_2DDES.Done; -end; - -procedure TestTCipher_2DDES.Init(TestData: TCipherTestData); -begin - LimitKeyLength(TestData.Key, FCipher_2DDES.Context.KeySize); - - FCipher_2DDES.Mode := TestData.Mode; - FCipher_2DDES.Init(BytesOf(TestData.Key), - BytesOf(TestData.InitVector), - TestData.Filler); -end; - -procedure TestTCipher_2DDES.SetUp; -begin - FCipher_2DDES := TCipher_2DDES.Create; - - SetLength(FTestData, 1); - - FTestData[0].OutputData := '936cf643c6a77fed4db4704ae2a6068b751319afe182ed354e13f688a46b3326'; - FTestData[0].InputData := TFormat_ESCAPE.Decode('\x30\x44\xED\x6E\x45\xA4' + - '\x96\xF5\xF6\x35\xA2\xEB' + - '\x3D\x1A\x5D\xD6\xCB\x1D' + - '\x09\x82\x2D\xBD\xF5\x60' + - '\xC2\xB8\x58\xA1\x91\xF9' + - '\x81\xB1'); - - FTestData[0].Key := 'TCipher_2DDES'; - FTestData[0].InitVector := ''; - FTestData[0].Filler := $FF; - FTestData[0].Mode := cmCTSx; -end; - -procedure TestTCipher_2DDES.TearDown; -begin - FCipher_2DDES.Free; - FCipher_2DDES := nil; -end; - -procedure TestTCipher_2DDES.TestClassByName; -var - ReturnValue : TDECCipherClass; -begin - ReturnValue := FCipher_2DDES.ClassByName('TCipher_2DDES'); - CheckEquals(TCipher_2DDES, ReturnValue, 'Class is not registered'); -end; - -procedure TestTCipher_2DDES.TestContext; -var - ReturnValue: TCipherContext; -begin - ReturnValue := FCipher_2DDES.Context; - - CheckEquals( 16, ReturnValue.KeySize); - CheckEquals( 16, ReturnValue.BlockSize); - CheckEquals( 16, ReturnValue.BufferSize); - CheckEquals( 512, ReturnValue.AdditionalBufferSize); - CheckEquals( 1, ReturnValue.MinRounds); - CheckEquals( 1, ReturnValue.MaxRounds); - CheckEquals(false, ReturnValue.NeedsAdditionalBufferBackup); - CheckEquals(true, [ctBlock, ctSymmetric] = ReturnValue.CipherType); -end; - -procedure TestTCipher_2DDES.TestDecode; -begin - DoTestDecode(FCipher_2DDES.DecodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_2DDES.TestEncode; -begin - DoTestEncode(FCipher_2DDES.EncodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_2DDES.TestIdentity; -begin - CheckEquals($70C155BD, FCipher_2DDES.Identity); -end; - -procedure TestTCipher_3DDES.Done; -begin - FCipher_3DDES.Done; -end; - -procedure TestTCipher_3DDES.Init(TestData: TCipherTestData); -begin - LimitKeyLength(TestData.Key, FCipher_3DDES.Context.KeySize); - - FCipher_3DDES.Mode := TestData.Mode; - FCipher_3DDES.Init(BytesOf(TestData.Key), - BytesOf(TestData.InitVector), - TestData.Filler); -end; - -procedure TestTCipher_3DDES.SetUp; -begin - FCipher_3DDES := TCipher_3DDES.Create; - - SetLength(FTestData, 1); - - FTestData[0].OutputData := '2f5a5ed45e8aaa4ed26659481de195942a9fcc1f4de614f050040364669a778e'; - FTestData[0].InputData := TFormat_ESCAPE.Decode('\x30\x44\xED\x6E\x45\xA4' + - '\x96\xF5\xF6\x35\xA2\xEB' + - '\x3D\x1A\x5D\xD6\xCB\x1D' + - '\x09\x82\x2D\xBD\xF5\x60' + - '\xC2\xB8\x58\xA1\x91\xF9' + - '\x81\xB1'); - - FTestData[0].Key := 'TCipher_3DDES'; - FTestData[0].InitVector := ''; - FTestData[0].Filler := $FF; - FTestData[0].Mode := cmCTSx; -end; - -procedure TestTCipher_3DDES.TearDown; -begin - FCipher_3DDES.Free; - FCipher_3DDES := nil; -end; - -procedure TestTCipher_3DDES.TestClassByName; -var - ReturnValue : TDECCipherClass; -begin - ReturnValue := FCipher_3DDES.ClassByName('TCipher_3DDES'); - CheckEquals(TCipher_3DDES, ReturnValue, 'Class is not registered'); -end; - -procedure TestTCipher_3DDES.TestContext; -var - ReturnValue: TCipherContext; -begin - ReturnValue := FCipher_3DDES.Context; - - CheckEquals( 24, ReturnValue.KeySize); - CheckEquals( 16, ReturnValue.BlockSize); - CheckEquals( 16, ReturnValue.BufferSize); - CheckEquals( 768, ReturnValue.AdditionalBufferSize); - CheckEquals( 1, ReturnValue.MinRounds); - CheckEquals( 1, ReturnValue.MaxRounds); - CheckEquals(false, ReturnValue.NeedsAdditionalBufferBackup); - CheckEquals(true, [ctBlock, ctSymmetric] = ReturnValue.CipherType); -end; - -procedure TestTCipher_3DDES.TestDecode; -begin - DoTestDecode(FCipher_3DDES.DecodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_3DDES.TestEncode; -begin - DoTestEncode(FCipher_3DDES.EncodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_3DDES.TestIdentity; -begin - CheckEquals($4DA17C0D, FCipher_3DDES.Identity); -end; - -procedure TestTCipher_3TDES.Done; -begin - FCipher_3TDES.Done; -end; - -procedure TestTCipher_3TDES.Init(TestData: TCipherTestData); -begin - LimitKeyLength(TestData.Key, FCipher_3TDES.Context.KeySize); - - FCipher_3TDES.Mode := TestData.Mode; - FCipher_3TDES.Init(BytesOf(TestData.Key), - BytesOf(TestData.InitVector), - TestData.Filler); -end; - -procedure TestTCipher_3TDES.SetUp; -begin - FCipher_3TDES := TCipher_3TDES.Create; - - SetLength(FTestData, 1); - - FTestData[0].OutputData := '899e748e57060649fc7436b21a538bb8d64b57c6a0863bf6b5f18468c0f6466e'; - FTestData[0].InputData := TFormat_ESCAPE.Decode('\x30\x44\xED\x6E\x45\xA4' + - '\x96\xF5\xF6\x35\xA2\xEB' + - '\x3D\x1A\x5D\xD6\xCB\x1D' + - '\x09\x82\x2D\xBD\xF5\x60' + - '\xC2\xB8\x58\xA1\x91\xF9' + - '\x81\xB1'); - - FTestData[0].Key := 'TCipher_3TDES'; - FTestData[0].InitVector := ''; - FTestData[0].Filler := $FF; - FTestData[0].Mode := cmCTSx; -end; - -procedure TestTCipher_3TDES.TearDown; -begin - FCipher_3TDES.Free; - FCipher_3TDES := nil; -end; - -procedure TestTCipher_3TDES.TestClassByName; -var - ReturnValue : TDECCipherClass; -begin - ReturnValue := FCipher_3TDES.ClassByName('TCipher_3TDES'); - CheckEquals(TCipher_3TDES, ReturnValue, 'Class is not registered'); -end; - -procedure TestTCipher_3TDES.TestContext; -var - ReturnValue: TCipherContext; -begin - ReturnValue := FCipher_3TDES.Context; - - CheckEquals( 24, ReturnValue.KeySize); - CheckEquals( 24, ReturnValue.BlockSize); - CheckEquals( 24, ReturnValue.BufferSize); - CheckEquals( 768, ReturnValue.AdditionalBufferSize); - CheckEquals( 1, ReturnValue.MinRounds); - CheckEquals( 1, ReturnValue.MaxRounds); - CheckEquals(false, ReturnValue.NeedsAdditionalBufferBackup); - CheckEquals(true, [ctBlock, ctSymmetric] = ReturnValue.CipherType); -end; - -procedure TestTCipher_3TDES.TestDecode; -begin - DoTestEncode(FCipher_3TDES.EncodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_3TDES.TestEncode; -begin - DoTestDecode(FCipher_3TDES.DecodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_3TDES.TestIdentity; -begin - CheckEquals($1DB82B92, FCipher_3TDES.Identity); -end; - -procedure TestTCipher_3Way.Done; -begin - FCipher_3Way.Done; -end; - -procedure TestTCipher_3Way.Init(TestData: TCipherTestData); -begin - LimitKeyLength(TestData.Key, FCipher_3Way.Context.KeySize); - - FCipher_3Way.Mode := TestData.Mode; - FCipher_3Way.Init(BytesOf(TestData.Key), - BytesOf(TestData.InitVector), - TestData.Filler); -end; - -procedure TestTCipher_3Way.SetUp; -begin - FCipher_3Way := TCipher_3Way.Create; - - SetLength(FTestData, 1); - - FTestData[0].OutputData := '77fc77947c8fde21e981df2ab1bc7ef8a3b6444bb6fc79c49b4058cee8959e12'; - FTestData[0].InputData := TFormat_ESCAPE.Decode('\x30\x44\xED\x6E\x45\xA4' + - '\x96\xF5\xF6\x35\xA2\xEB' + - '\x3D\x1A\x5D\xD6\xCB\x1D' + - '\x09\x82\x2D\xBD\xF5\x60' + - '\xC2\xB8\x58\xA1\x91\xF9' + - '\x81\xB1'); - - FTestData[0].Key := 'TCipher_3Way'; - FTestData[0].InitVector := ''; - FTestData[0].Filler := $FF; - FTestData[0].Mode := cmCTSx; -end; - -procedure TestTCipher_3Way.TearDown; -begin - FCipher_3Way.Free; - FCipher_3Way := nil; -end; - -procedure TestTCipher_3Way.TestClassByName; -var - ReturnValue : TDECCipherClass; -begin - ReturnValue := FCipher_3Way.ClassByName('TCipher_3Way'); - CheckEquals(TCipher_3Way, ReturnValue, 'Class is not registered'); -end; - -procedure TestTCipher_3Way.TestContext; -var - ReturnValue: TCipherContext; -begin - ReturnValue := FCipher_3Way.Context; - - CheckEquals( 12, ReturnValue.KeySize); - CheckEquals( 12, ReturnValue.BlockSize); - CheckEquals( 12, ReturnValue.BufferSize); - CheckEquals( 120, ReturnValue.AdditionalBufferSize); - CheckEquals( 1, ReturnValue.MinRounds); - CheckEquals( 1, ReturnValue.MaxRounds); - CheckEquals(false, ReturnValue.NeedsAdditionalBufferBackup); - CheckEquals(true, [ctBlock, ctSymmetric] = ReturnValue.CipherType); -end; - -procedure TestTCipher_3Way.TestDecode; -begin - DoTestDecode(FCipher_3Way.DecodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_3Way.TestEncode; -begin - DoTestEncode(FCipher_3Way.EncodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_3Way.TestIdentity; -begin - CheckEquals($54DAF114, FCipher_3Way.Identity); -end; - -procedure TestTCipher_Cast128.Done; -begin - FCipher_Cast128.Done; -end; - -procedure TestTCipher_Cast128.Init(TestData: TCipherTestData); -begin - LimitKeyLength(TestData.Key, FCipher_Cast128.Context.KeySize); - - FCipher_Cast128.Mode := TestData.Mode; - FCipher_Cast128.Init(BytesOf(TestData.Key), - BytesOf(TestData.InitVector), - TestData.Filler); -end; - -procedure TestTCipher_Cast128.SetUp; -begin - FCipher_Cast128 := TCipher_Cast128.Create; - - SetLength(FTestData, 1); - - FTestData[0].OutputData := '6c27d14cf6ba76e7a4781c20188c30bcd29af62a631ffd04893fc70e07a9949b'; - FTestData[0].InputData := TFormat_ESCAPE.Decode('\x30\x44\xED\x6E\x45\xA4' + - '\x96\xF5\xF6\x35\xA2\xEB' + - '\x3D\x1A\x5D\xD6\xCB\x1D' + - '\x09\x82\x2D\xBD\xF5\x60' + - '\xC2\xB8\x58\xA1\x91\xF9' + - '\x81\xB1'); - - FTestData[0].Key := 'TCipher_Cast128'; - FTestData[0].InitVector := ''; - FTestData[0].Filler := $FF; - FTestData[0].Mode := cmCTSx; -end; - -procedure TestTCipher_Cast128.TearDown; -begin - FCipher_Cast128.Free; - FCipher_Cast128 := nil; -end; - -procedure TestTCipher_Cast128.TestClassByName; -var - ReturnValue : TDECCipherClass; -begin - ReturnValue := FCipher_Cast128.ClassByName('TCipher_Cast128'); - CheckEquals(TCipher_Cast128, ReturnValue, 'Class is not registered'); -end; - -procedure TestTCipher_Cast128.TestContext; -var - ReturnValue: TCipherContext; -begin - ReturnValue := FCipher_Cast128.Context; - - CheckEquals( 16, ReturnValue.KeySize); - CheckEquals( 8, ReturnValue.BlockSize); - CheckEquals( 8, ReturnValue.BufferSize); - CheckEquals( 128, ReturnValue.AdditionalBufferSize); - CheckEquals( 1, ReturnValue.MinRounds); - CheckEquals( 256, ReturnValue.MaxRounds); - CheckEquals(false, ReturnValue.NeedsAdditionalBufferBackup); - CheckEquals(true, [ctBlock, ctSymmetric] = ReturnValue.CipherType); -end; - -procedure TestTCipher_Cast128.TestDecode; -begin - DoTestDecode(FCipher_Cast128.DecodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_Cast128.TestEncode; -begin - DoTestEncode(FCipher_Cast128.EncodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_Cast128.TestIdentity; -begin - CheckEquals($ED7D0785, FCipher_Cast128.Identity); -end; - -procedure TestTCipher_Gost.Done; -begin - FCipher_Gost.Done; -end; - -procedure TestTCipher_Gost.Init(TestData: TCipherTestData); -begin - LimitKeyLength(TestData.Key, FCipher_Gost.Context.KeySize); - - FCipher_Gost.Mode := TestData.Mode; - FCipher_Gost.Init(BytesOf(TestData.Key), - BytesOf(TestData.InitVector), - TestData.Filler); -end; - -procedure TestTCipher_Gost.SetUp; -begin - FCipher_Gost := TCipher_Gost.Create; - - SetLength(FTestData, 1); - - FTestData[0].OutputData := 'b303a03fb57b914d97512440bdcf251534059cf8ab10869ff2804784479b1ad1'; - FTestData[0].InputData := TFormat_ESCAPE.Decode('\x30\x44\xED\x6E\x45\xA4' + - '\x96\xF5\xF6\x35\xA2\xEB' + - '\x3D\x1A\x5D\xD6\xCB\x1D' + - '\x09\x82\x2D\xBD\xF5\x60' + - '\xC2\xB8\x58\xA1\x91\xF9' + - '\x81\xB1'); - - FTestData[0].Key := 'TCipher_Gost'; - FTestData[0].InitVector := ''; - FTestData[0].Filler := $FF; - FTestData[0].Mode := cmCTSx; -end; - -procedure TestTCipher_Gost.TearDown; -begin - FCipher_Gost.Free; - FCipher_Gost := nil; -end; - -procedure TestTCipher_Gost.TestClassByName; -var - ReturnValue : TDECCipherClass; -begin - ReturnValue := FCipher_Gost.ClassByName('TCipher_Gost'); - CheckEquals(TCipher_Gost, ReturnValue, 'Class is not registered'); -end; - -procedure TestTCipher_Gost.TestContext; -var - ReturnValue: TCipherContext; -begin - ReturnValue := FCipher_Gost.Context; - - CheckEquals( 32, ReturnValue.KeySize); - CheckEquals( 8, ReturnValue.BlockSize); - CheckEquals( 8, ReturnValue.BufferSize); - CheckEquals( 32, ReturnValue.AdditionalBufferSize); - CheckEquals( 1, ReturnValue.MinRounds); - CheckEquals( 1, ReturnValue.MaxRounds); - CheckEquals(false, ReturnValue.NeedsAdditionalBufferBackup); - CheckEquals(true, [ctBlock, ctSymmetric] = ReturnValue.CipherType); -end; - -procedure TestTCipher_Gost.TestDecode; -begin - DoTestDecode(FCipher_Gost.DecodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_Gost.TestEncode; -begin - DoTestEncode(FCipher_Gost.EncodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_Gost.TestIdentity; -begin - CheckEquals($A4F73879, FCipher_Gost.Identity); -end; - -procedure TestTCipher_Magma.Done; -begin - FCipher_Magma.Done; -end; - -procedure TestTCipher_Magma.Init(TestData: TCipherTestData); -begin - LimitKeyLength(TestData.Key, FCipher_Magma.Context.KeySize); - - FCipher_Magma.Mode := TestData.Mode; - FCipher_Magma.Init(BytesOf(TestData.Key), - BytesOf(TestData.InitVector), - TestData.Filler); -end; - -procedure TestTCipher_Magma.SetUp; -begin - FCipher_Magma := TCipher_Magma.Create; - - SetLength(FTestData, 1); - - FTestData[0].OutputData := 'b303a03fb57b914d97512440bdcf251534059cf8ab10869ff2804784479b1ad1'; - FTestData[0].InputData := TFormat_ESCAPE.Decode('\x30\x44\xED\x6E\x45\xA4' + - '\x96\xF5\xF6\x35\xA2\xEB' + - '\x3D\x1A\x5D\xD6\xCB\x1D' + - '\x09\x82\x2D\xBD\xF5\x60' + - '\xC2\xB8\x58\xA1\x91\xF9' + - '\x81\xB1'); - - FTestData[0].Key := 'TCipher_Gost'; - FTestData[0].InitVector := ''; - FTestData[0].Filler := $FF; - FTestData[0].Mode := cmCTSx; -end; - -procedure TestTCipher_Magma.TearDown; -begin - FCipher_Magma.Free; - FCipher_Magma := nil; -end; - -procedure TestTCipher_Magma.DoTestClassByName; -var - ReturnValue : TDECCipherClass; -begin - ReturnValue := FCipher_Magma.ClassByName('TCipher_Magma'); - // This line should never be executed due to ClassByName rising an exception - // but it suppresses a ReturnValue is not being used compiler warning - CheckEquals(TCipher_Magma, ReturnValue, 'Class is not registered'); -end; - -procedure TestTCipher_Magma.TestClassByName; -begin - CheckException(DoTestClassByName, EDECClassNotRegisteredException); -end; - -procedure TestTCipher_Magma.TestContext; -var - ReturnValue: TCipherContext; -begin - ReturnValue := FCipher_Magma.Context; - - CheckEquals( 32, ReturnValue.KeySize); - CheckEquals( 8, ReturnValue.BlockSize); - CheckEquals( 8, ReturnValue.BufferSize); - CheckEquals( 32, ReturnValue.AdditionalBufferSize); - CheckEquals( 1, ReturnValue.MinRounds); - CheckEquals( 1, ReturnValue.MaxRounds); - CheckEquals(false, ReturnValue.NeedsAdditionalBufferBackup); - CheckEquals(true, [ctBlock, ctSymmetric] = ReturnValue.CipherType); -end; - -procedure TestTCipher_Magma.TestDecode; -begin - DoTestDecode(FCipher_Magma.DecodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_Magma.TestEncode; -begin - DoTestEncode(FCipher_Magma.EncodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_Magma.TestIdentity; -begin - CheckEquals($5BB9788, FCipher_Magma.Identity); -end; - -procedure TestTCipher_Misty.Done; -begin - FCipher_Misty.Done; -end; - -procedure TestTCipher_Misty.Init(TestData: TCipherTestData); -begin - LimitKeyLength(TestData.Key, FCipher_Misty.Context.KeySize); - - FCipher_Misty.Mode := TestData.Mode; - FCipher_Misty.Init(BytesOf(TestData.Key), - BytesOf(TestData.InitVector), - TestData.Filler); -end; - -procedure TestTCipher_Misty.SetUp; -begin - FCipher_Misty := TCipher_Misty.Create; - - SetLength(FTestData, 1); - - FTestData[0].OutputData := '647bc5c64945aa955d64cd567c6cb6478157fe8cf48419bc27600ca679850fc9'; - FTestData[0].InputData := TFormat_ESCAPE.Decode('\x30\x44\xED\x6E\x45\xA4' + - '\x96\xF5\xF6\x35\xA2\xEB' + - '\x3D\x1A\x5D\xD6\xCB\x1D' + - '\x09\x82\x2D\xBD\xF5\x60' + - '\xC2\xB8\x58\xA1\x91\xF9' + - '\x81\xB1'); - - FTestData[0].Key := 'TCipher_Misty'; - FTestData[0].InitVector := ''; - FTestData[0].Filler := $FF; - FTestData[0].Mode := cmCTSx; -end; - -procedure TestTCipher_Misty.TearDown; -begin - FCipher_Misty.Free; - FCipher_Misty := nil; -end; - -procedure TestTCipher_Misty.TestClassByName; -var - ReturnValue : TDECCipherClass; -begin - ReturnValue := FCipher_Misty.ClassByName('TCipher_Misty'); - CheckEquals(TCipher_Misty, ReturnValue, 'Class is not registered'); -end; - -procedure TestTCipher_Misty.TestContext; -var - ReturnValue: TCipherContext; -begin - ReturnValue := FCipher_Misty.Context; - - CheckEquals( 16, ReturnValue.KeySize); - CheckEquals( 8, ReturnValue.BlockSize); - CheckEquals( 8, ReturnValue.BufferSize); - CheckEquals( 128, ReturnValue.AdditionalBufferSize); - CheckEquals( 1, ReturnValue.MinRounds); - CheckEquals( 1, ReturnValue.MaxRounds); - CheckEquals(false, ReturnValue.NeedsAdditionalBufferBackup); - CheckEquals(true, [ctBlock, ctSymmetric] = ReturnValue.CipherType); -end; - -procedure TestTCipher_Misty.TestDecode; -begin - DoTestDecode(FCipher_Misty.DecodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_Misty.TestEncode; -begin - DoTestEncode(FCipher_Misty.EncodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_Misty.TestIdentity; -begin - CheckEquals($534C8585, FCipher_Misty.Identity); -end; - -procedure TestTCipher_NewDES.Done; -begin - FCipher_NewDES.Done; -end; - -procedure TestTCipher_NewDES.Init(TestData: TCipherTestData); -begin - LimitKeyLength(TestData.Key, FCipher_NewDES.Context.KeySize); - - FCipher_NewDES.Mode := TestData.Mode; - FCipher_NewDES.Init(BytesOf(TestData.Key), - BytesOf(TestData.InitVector), - TestData.Filler); -end; - -procedure TestTCipher_NewDES.SetUp; -begin - FCipher_NewDES := TCipher_NewDES.Create; - - SetLength(FTestData, 1); - - FTestData[0].OutputData := 'd5914f9c743546fbd5ad9131751464fea779216a29994789d20d760c739ccd17'; - FTestData[0].InputData := TFormat_ESCAPE.Decode('\x30\x44\xED\x6E\x45\xA4' + - '\x96\xF5\xF6\x35\xA2\xEB' + - '\x3D\x1A\x5D\xD6\xCB\x1D' + - '\x09\x82\x2D\xBD\xF5\x60' + - '\xC2\xB8\x58\xA1\x91\xF9' + - '\x81\xB1'); - - FTestData[0].Key := 'TCipher_NewDES'; - FTestData[0].InitVector := ''; - FTestData[0].Filler := $FF; - FTestData[0].Mode := cmCTSx; -end; - -procedure TestTCipher_NewDES.TearDown; -begin - FCipher_NewDES.Free; - FCipher_NewDES := nil; -end; - -procedure TestTCipher_NewDES.TestClassByName; -var - ReturnValue : TDECCipherClass; -begin - ReturnValue := FCipher_NewDES.ClassByName('TCipher_NewDES'); - CheckEquals(TCipher_NewDES, ReturnValue, 'Class is not registered'); -end; - -procedure TestTCipher_NewDES.TestContext; -var - ReturnValue: TCipherContext; -begin - ReturnValue := FCipher_NewDES.Context; - - CheckEquals( 15, ReturnValue.KeySize); - CheckEquals( 8, ReturnValue.BlockSize); - CheckEquals( 8, ReturnValue.BufferSize); - CheckEquals( 120, ReturnValue.AdditionalBufferSize); - CheckEquals( 1, ReturnValue.MinRounds); - CheckEquals( 1, ReturnValue.MaxRounds); - CheckEquals(true, ReturnValue.NeedsAdditionalBufferBackup); - CheckEquals(true, [ctBlock, ctSymmetric] = ReturnValue.CipherType); -end; - -procedure TestTCipher_NewDES.TestDecode; -begin - DoTestDecode(FCipher_NewDES.DecodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_NewDES.TestEncode; -begin - DoTestEncode(FCipher_NewDES.EncodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_NewDES.TestIdentity; -begin - CheckEquals($5EE9D8B9, FCipher_NewDES.Identity); -end; - -procedure TestTCipher_Q128.Done; -begin - FCipher_Q128.Done; -end; - -procedure TestTCipher_Q128.Init(TestData: TCipherTestData); -begin - LimitKeyLength(TestData.Key, FCipher_Q128.Context.KeySize); - - FCipher_Q128.Mode := TestData.Mode; - FCipher_Q128.Init(BytesOf(TestData.Key), - BytesOf(TestData.InitVector), - TestData.Filler); -end; - -procedure TestTCipher_Q128.SetUp; -begin - FCipher_Q128 := TCipher_Q128.Create; - - SetLength(FTestData, 1); - - FTestData[0].OutputData := '99aad03dca144e2af81e01a0eaab9f48232d5954547e2b128680e833ebe15eae'; - FTestData[0].InputData := TFormat_ESCAPE.Decode('\x30\x44\xED\x6E\x45\xA4' + - '\x96\xF5\xF6\x35\xA2\xEB' + - '\x3D\x1A\x5D\xD6\xCB\x1D' + - '\x09\x82\x2D\xBD\xF5\x60' + - '\xC2\xB8\x58\xA1\x91\xF9' + - '\x81\xB1'); - - FTestData[0].Key := 'TCipher_Q128'; - FTestData[0].InitVector := ''; - FTestData[0].Filler := $FF; - FTestData[0].Mode := cmCTSx; -end; - -procedure TestTCipher_Q128.TearDown; -begin - FCipher_Q128.Free; - FCipher_Q128 := nil; -end; - -procedure TestTCipher_Q128.TestClassByName; -var - ReturnValue : TDECCipherClass; -begin - ReturnValue := FCipher_Q128.ClassByName('TCipher_Q128'); - CheckEquals(TCipher_Q128, ReturnValue, 'Class is not registered'); -end; - -procedure TestTCipher_Q128.TestContext; -var - ReturnValue: TCipherContext; -begin - ReturnValue := FCipher_Q128.Context; - - CheckEquals( 16, ReturnValue.KeySize); - CheckEquals( 16, ReturnValue.BlockSize); - CheckEquals( 16, ReturnValue.BufferSize); - CheckEquals( 256, ReturnValue.AdditionalBufferSize); - CheckEquals( 1, ReturnValue.MinRounds); - CheckEquals( 1, ReturnValue.MaxRounds); - CheckEquals(false, ReturnValue.NeedsAdditionalBufferBackup); - CheckEquals(true, [ctBlock, ctSymmetric] = ReturnValue.CipherType); -end; - -procedure TestTCipher_Q128.TestDecode; -begin - DoTestDecode(FCipher_Q128.DecodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_Q128.TestEncode; -begin - DoTestEncode(FCipher_Q128.EncodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_Q128.TestIdentity; -begin - CheckEquals($B70802F5, FCipher_Q128.Identity); -end; - -procedure TestTCipher_RC2.Done; -begin - FCipher_RC2.Done; -end; - -procedure TestTCipher_RC2.Init(TestData: TCipherTestData); -begin - LimitKeyLength(TestData.Key, FCipher_RC2.Context.KeySize); - - FCipher_RC2.Mode := TestData.Mode; - FCipher_RC2.Init(BytesOf(TestData.Key), - BytesOf(TestData.InitVector), - TestData.Filler); -end; - -procedure TestTCipher_RC2.SetUp; -begin - FCipher_RC2 := TCipher_RC2.Create; - - SetLength(FTestData, 1); - - FTestData[0].OutputData := '71a2f0fdc2f93c871064b779d3fcdd1153364fd71153775d8d53a72e8b8af9e7'; - FTestData[0].InputData := TFormat_ESCAPE.Decode('\x30\x44\xED\x6E\x45\xA4' + - '\x96\xF5\xF6\x35\xA2\xEB' + - '\x3D\x1A\x5D\xD6\xCB\x1D' + - '\x09\x82\x2D\xBD\xF5\x60' + - '\xC2\xB8\x58\xA1\x91\xF9' + - '\x81\xB1'); - - FTestData[0].Key := 'TCipher_RC2'; - FTestData[0].InitVector := ''; - FTestData[0].Filler := $FF; - FTestData[0].Mode := cmCTSx; -end; - -procedure TestTCipher_RC2.TearDown; -begin - FCipher_RC2.Free; - FCipher_RC2 := nil; -end; - -procedure TestTCipher_RC2.TestClassByName; -var - ReturnValue : TDECCipherClass; -begin - ReturnValue := FCipher_RC2.ClassByName('TCipher_RC2'); - CheckEquals(TCipher_RC2, ReturnValue, 'Class is not registered'); -end; - -procedure TestTCipher_RC2.TestContext; -var - ReturnValue: TCipherContext; -begin - ReturnValue := FCipher_RC2.Context; - - CheckEquals( 128, ReturnValue.KeySize); - CheckEquals( 8, ReturnValue.BlockSize); - CheckEquals( 8, ReturnValue.BufferSize); - CheckEquals( 128, ReturnValue.AdditionalBufferSize); - CheckEquals( 1, ReturnValue.MinRounds); - CheckEquals( 1, ReturnValue.MaxRounds); - CheckEquals(false, ReturnValue.NeedsAdditionalBufferBackup); - CheckEquals(true, [ctBlock, ctSymmetric] = ReturnValue.CipherType); -end; - -procedure TestTCipher_RC2.TestDecode; -begin - DoTestDecode(FCipher_RC2.DecodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_RC2.TestEncode; -begin - DoTestEncode(FCipher_RC2.EncodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_RC2.TestIdentity; -begin - CheckEquals($9AC07A6F, FCipher_RC2.Identity); -end; - -procedure TestTCipher_RC5.Done; -begin - FCipher_RC5.Done; -end; - -procedure TestTCipher_RC5.Init(TestData: TCipherTestData); -begin - LimitKeyLength(TestData.Key, FCipher_RC5.Context.KeySize); - - FCipher_RC5.Mode := TestData.Mode; - FCipher_RC5.Init(BytesOf(TestData.Key), - BytesOf(TestData.InitVector), - TestData.Filler); -end; - -procedure TestTCipher_RC5.SetUp; -begin - FCipher_RC5 := TCipher_RC5.Create; - - SetLength(FTestData, 1); - - FTestData[0].OutputData := '10392ce00b5f097fd6b16c0eb975d5ccfcbeb58d41ac547c8330269daccb0a69'; - FTestData[0].InputData := TFormat_ESCAPE.Decode('\x30\x44\xED\x6E\x45\xA4' + - '\x96\xF5\xF6\x35\xA2\xEB' + - '\x3D\x1A\x5D\xD6\xCB\x1D' + - '\x09\x82\x2D\xBD\xF5\x60' + - '\xC2\xB8\x58\xA1\x91\xF9' + - '\x81\xB1'); - - FTestData[0].Key := 'TCipher_RC5'; - FTestData[0].InitVector := ''; - FTestData[0].Filler := $FF; - FTestData[0].Mode := cmCTSx; -end; - -procedure TestTCipher_RC5.TearDown; -begin - FCipher_RC5.Free; - FCipher_RC5 := nil; -end; - -procedure TestTCipher_RC5.TestClassByName; -var - ReturnValue : TDECCipherClass; -begin - ReturnValue := FCipher_RC5.ClassByName('TCipher_RC5'); - CheckEquals(TCipher_RC5, ReturnValue, 'Class is not registered'); -end; - -procedure TestTCipher_RC5.TestContext; -var - ReturnValue: TCipherContext; -begin - ReturnValue := FCipher_RC5.Context; - - CheckEquals( 256, ReturnValue.KeySize); - CheckEquals( 8, ReturnValue.BlockSize); - CheckEquals( 8, ReturnValue.BufferSize); - CheckEquals( 136, ReturnValue.AdditionalBufferSize); - CheckEquals( 1, ReturnValue.MinRounds); - CheckEquals( 256, ReturnValue.MaxRounds); - CheckEquals(false, ReturnValue.NeedsAdditionalBufferBackup); - CheckEquals(true, [ctBlock, ctSymmetric] = ReturnValue.CipherType); -end; - -procedure TestTCipher_RC5.TestDecode; -begin - DoTestDecode(FCipher_RC5.DecodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_RC5.TestEncode; -begin - DoTestEncode(FCipher_RC5.EncodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_RC5.TestIdentity; -begin - CheckEquals($04A4EFCC, FCipher_RC5.Identity); -end; - -procedure TestTCipher_SAFER.Done; -begin - FCipher_SAFER.Done; -end; - -procedure TestTCipher_SAFER.Init(TestData: TCipherTestData); -begin - LimitKeyLength(TestData.Key, FCipher_SAFER.Context.KeySize); - - FCipher_SAFER.Mode := TestData.Mode; - FCipher_SAFER.Init(BytesOf(TestData.Key), - BytesOf(TestData.InitVector), - TestData.Filler); -end; - -procedure TestTCipher_SAFER.SetUp; -begin - FCipher_SAFER := TCipher_SAFER.Create; - - SetLength(FTestData, 1); - - FTestData[0].OutputData := '003d4920736385aad9c20ade7e9ee9ab24d07434477e211d55f935289884a875'; - FTestData[0].InputData := TFormat_ESCAPE.Decode('\x30\x44\xED\x6E\x45\xA4' + - '\x96\xF5\xF6\x35\xA2\xEB' + - '\x3D\x1A\x5D\xD6\xCB\x1D' + - '\x09\x82\x2D\xBD\xF5\x60' + - '\xC2\xB8\x58\xA1\x91\xF9' + - '\x81\xB1'); - - FTestData[0].Key := 'TCipher_SAFER'; - FTestData[0].InitVector := ''; - FTestData[0].Filler := $FF; - FTestData[0].Mode := cmCTSx; -end; - -procedure TestTCipher_SAFER.TearDown; -begin - FCipher_SAFER.Free; - FCipher_SAFER := nil; -end; - -procedure TestTCipher_SAFER.TestClassByName; -var - ReturnValue : TDECCipherClass; -begin - ReturnValue := FCipher_SAFER.ClassByName('TCipher_SAFER'); - CheckEquals(TCipher_SAFER, ReturnValue, 'Class is not registered'); -end; - -procedure TestTCipher_SAFER.TestContext; -var - ReturnValue: TCipherContext; -begin - ReturnValue := FCipher_SAFER.Context; - - CheckEquals( 16, ReturnValue.KeySize); - CheckEquals( 8, ReturnValue.BlockSize); - CheckEquals( 8, ReturnValue.BufferSize); - CheckEquals( 768, ReturnValue.AdditionalBufferSize); - CheckEquals( 4, ReturnValue.MinRounds); - CheckEquals( 13, ReturnValue.MaxRounds); - CheckEquals(false, ReturnValue.NeedsAdditionalBufferBackup); - CheckEquals(true, [ctBlock, ctSymmetric] = ReturnValue.CipherType); -end; - -procedure TestTCipher_SAFER.TestDecode; -begin - DoTestDecode(FCipher_SAFER.DecodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_SAFER.TestEncode; -begin - DoTestEncode(FCipher_SAFER.EncodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_SAFER.TestIdentity; -begin - CheckEquals($97CE1F8A, FCipher_SAFER.Identity); -end; - -procedure TestTCipher_Shark.Done; -begin - FCipher_Shark.Done; -end; - -procedure TestTCipher_Shark.Init(TestData: TCipherTestData); -begin - LimitKeyLength(TestData.Key, FCipher_Shark.Context.KeySize); - - FCipher_Shark.Mode := TestData.Mode; - FCipher_Shark.Init(BytesOf(TestData.Key), - BytesOf(TestData.InitVector), - TestData.Filler); -end; - -procedure TestTCipher_Shark.SetUp; -begin - FCipher_Shark := TCipher_Shark.Create; - - SetLength(FTestData, 2); - - FTestData[0].OutputData := 'e97af38e7c8c56d0426597162c4e68ad867ac9540fe9a2cf7b2fd33e7df8919c'; - FTestData[0].InputData := TFormat_ESCAPE.Decode('\x30\x44\xED\x6E\x45\xA4' + - '\x96\xF5\xF6\x35\xA2\xEB' + - '\x3D\x1A\x5D\xD6\xCB\x1D' + - '\x09\x82\x2D\xBD\xF5\x60' + - '\xC2\xB8\x58\xA1\x91\xF9' + - '\x81\xB1'); - FTestData[0].Key := 'TCipher_Shark'; - FTestData[0].InitVector := ''; - FTestData[0].Filler := $FF; - FTestData[0].Mode := cmECBx; - - FTestData[1].OutputData := '3968bf331e8ca5ed'; - FTestData[1].InputData := #0#0#0#0#0#0#0#0; - FTestData[1].Key := 'TCipher_Shark'; - FTestData[1].InitVector := ''; - FTestData[1].Filler := $FF; - FTestData[1].Mode := cmECBx; -end; - -procedure TestTCipher_Shark.TearDown; -begin - FCipher_Shark.Free; - FCipher_Shark := nil; -end; - -procedure TestTCipher_Shark.TestClassByName; -var - ReturnValue : TDECCipherClass; -begin - ReturnValue := FCipher_Shark.ClassByName('TCipher_Shark'); - CheckEquals(TCipher_Shark, ReturnValue, 'Class is not registered'); -end; - -procedure TestTCipher_Shark.TestContext; -var - ReturnValue: TCipherContext; -begin - ReturnValue := FCipher_Shark.Context; - - CheckEquals( 16, ReturnValue.KeySize); - CheckEquals( 8, ReturnValue.BlockSize); - CheckEquals( 8, ReturnValue.BufferSize); - CheckEquals( 112, ReturnValue.AdditionalBufferSize); - CheckEquals( 1, ReturnValue.MinRounds); - CheckEquals( 1, ReturnValue.MaxRounds); - CheckEquals(false, ReturnValue.NeedsAdditionalBufferBackup); - CheckEquals(true, [ctBlock, ctSymmetric] = ReturnValue.CipherType); -end; - -procedure TestTCipher_Shark.TestDecode; -begin - DoTestDecode(FCipher_Shark.DecodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_Shark.TestEncode; -begin - DoTestEncode(FCipher_Shark.EncodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_Shark.TestIdentity; -begin - CheckEquals($8E616AD3, FCipher_Shark.Identity); -end; - -procedure TestTCipher_Shark_DEC52.Done; -begin - FCipher_Shark_DEC52.Done; -end; - -procedure TestTCipher_Shark_DEC52.DoTestClassByName; -var - ReturnValue : TDECCipherClass; -begin - ReturnValue := FCipher_Shark_DEC52.ClassByName('TCipher_Shark_DEC52'); - // This line should never be executed due to ClassByName rising an exception - // but it suppresses a ReturnValue is not being used compiler warning - CheckEquals(TCipher_Shark_DEC52, ReturnValue, 'Class is not registered'); -end; - -procedure TestTCipher_Shark_DEC52.Init(TestData: TCipherTestData); -begin - LimitKeyLength(TestData.Key, FCipher_Shark_DEC52.Context.KeySize); - - FCipher_Shark_DEC52.Mode := TestData.Mode; - FCipher_Shark_DEC52.Init(BytesOf(TestData.Key), - BytesOf(TestData.InitVector), - TestData.Filler); -end; - -procedure TestTCipher_Shark_DEC52.SetUp; -begin - FCipher_Shark_DEC52 := TCipher_Shark_DEC52.Create; - - SetLength(FTestData, 1); - - FTestData[0].OutputData := 'd96521aac0c384609dce1f8bfbab183fa121acf85349c06f273a8915d37ae90b'; - FTestData[0].InputData := TFormat_ESCAPE.Decode('\x30\x44\xED\x6E\x45\xA4' + - '\x96\xF5\xF6\x35\xA2\xEB' + - '\x3D\x1A\x5D\xD6\xCB\x1D' + - '\x09\x82\x2D\xBD\xF5\x60' + - '\xC2\xB8\x58\xA1\x91\xF9' + - '\x81\xB1'); - FTestData[0].Key := 'TCipher_Shark'; - FTestData[0].InitVector := ''; - FTestData[0].Filler := $FF; - FTestData[0].Mode := cmCTSx; -end; - -procedure TestTCipher_Shark_DEC52.TearDown; -begin - FCipher_Shark_DEC52.Free; - FCipher_Shark_DEC52 := nil; -end; - -procedure TestTCipher_Shark_DEC52.TestClassByName; -begin - // Class shall not be registered by default - CheckException(DoTestClassByName, EDECClassNotRegisteredException); -end; - -procedure TestTCipher_Shark_DEC52.TestContext; -var - ReturnValue: TCipherContext; -begin - ReturnValue := FCipher_Shark_DEC52.Context; - - CheckEquals( 16, ReturnValue.KeySize); - CheckEquals( 8, ReturnValue.BlockSize); - CheckEquals( 8, ReturnValue.BufferSize); - CheckEquals( 112, ReturnValue.AdditionalBufferSize); - CheckEquals( 1, ReturnValue.MinRounds); - CheckEquals( 1, ReturnValue.MaxRounds); - CheckEquals(false, ReturnValue.NeedsAdditionalBufferBackup); - CheckEquals(true, [ctBlock, ctSymmetric] = ReturnValue.CipherType); -end; - -procedure TestTCipher_Shark_DEC52.TestDecode; -begin - DoTestDecode(FCipher_Shark_DEC52.DecodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_Shark_DEC52.TestEncode; -begin - DoTestEncode(FCipher_Shark_DEC52.EncodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_Shark_DEC52.TestIdentity; -begin - CheckEquals($7901E07F, FCipher_Shark_DEC52.Identity); -end; - -procedure TestTCipher_Skipjack.Done; -begin - FCipher_Skipjack.Done; -end; - -procedure TestTCipher_Skipjack.Init(TestData: TCipherTestData); -begin - LimitKeyLength(TestData.Key, FCipher_Skipjack.Context.KeySize); - - FCipher_Skipjack.Mode := TestData.Mode; - FCipher_Skipjack.Init(BytesOf(TestData.Key), - BytesOf(TestData.InitVector), - TestData.Filler); -end; - -procedure TestTCipher_Skipjack.SetUp; -begin - FCipher_Skipjack := TCipher_Skipjack.Create; - - SetLength(FTestData, 1); - - FTestData[0].OutputData := 'd513a692ec2435e8174e2b555e8d27dac99aa9b9213da0011802b30eb7b551ea'; - FTestData[0].InputData := TFormat_ESCAPE.Decode('\x30\x44\xED\x6E\x45\xA4' + - '\x96\xF5\xF6\x35\xA2\xEB' + - '\x3D\x1A\x5D\xD6\xCB\x1D' + - '\x09\x82\x2D\xBD\xF5\x60' + - '\xC2\xB8\x58\xA1\x91\xF9' + - '\x81\xB1'); - - FTestData[0].Key := 'TCipher_Skipjack'; - FTestData[0].InitVector := ''; - FTestData[0].Filler := $FF; - FTestData[0].Mode := cmCTSx; -end; - -procedure TestTCipher_Skipjack.TearDown; -begin - FCipher_Skipjack.Free; - FCipher_Skipjack := nil; -end; - -procedure TestTCipher_Skipjack.TestClassByName; -var - ReturnValue : TDECCipherClass; -begin - ReturnValue := FCipher_Skipjack.ClassByName('TCipher_Skipjack'); - CheckEquals(TCipher_Skipjack, ReturnValue, 'Class is not registered'); -end; - -procedure TestTCipher_Skipjack.TestContext; -var - ReturnValue: TCipherContext; -begin - ReturnValue := FCipher_Skipjack.Context; - - CheckEquals( 10, ReturnValue.KeySize); - CheckEquals( 8, ReturnValue.BlockSize); - CheckEquals( 8, ReturnValue.BufferSize); - CheckEquals(2560, ReturnValue.AdditionalBufferSize); - CheckEquals( 1, ReturnValue.MinRounds); - CheckEquals( 1, ReturnValue.MaxRounds); - CheckEquals(false, ReturnValue.NeedsAdditionalBufferBackup); - CheckEquals(true, [ctBlock, ctSymmetric] = ReturnValue.CipherType); -end; - -procedure TestTCipher_Skipjack.TestDecode; -begin - DoTestDecode(FCipher_Skipjack.DecodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_Skipjack.TestEncode; -begin - DoTestEncode(FCipher_Skipjack.EncodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_Skipjack.TestIdentity; -begin - CheckEquals($D2283F49, FCipher_Skipjack.Identity); -end; - -procedure TestTCipher_TEA.Done; -begin - FCipher_TEA.Done; -end; - -procedure TestTCipher_TEA.Init(TestData: TCipherTestData); -begin - LimitKeyLength(TestData.Key, FCipher_TEA.Context.KeySize); - - FCipher_TEA.Mode := TestData.Mode; - FCipher_TEA.Init(BytesOf(TestData.Key), - BytesOf(TestData.InitVector), - TestData.Filler); -end; - -procedure TestTCipher_TEA.SetUp; -begin - FCipher_TEA := TCipher_TEA.Create; - - SetLength(FTestData, 1); - - FTestData[0].OutputData := 'b7b8aabb264b06f97086b0e4560429ccbf55ea4eef59261819b0037c298ce277'; - FTestData[0].InputData := TFormat_ESCAPE.Decode('\x30\x44\xED\x6E\x45\xA4' + - '\x96\xF5\xF6\x35\xA2\xEB' + - '\x3D\x1A\x5D\xD6\xCB\x1D' + - '\x09\x82\x2D\xBD\xF5\x60' + - '\xC2\xB8\x58\xA1\x91\xF9' + - '\x81\xB1'); - - FTestData[0].Key := 'TCipher_TEA'; - FTestData[0].InitVector := ''; - FTestData[0].Filler := $FF; - FTestData[0].Mode := cmCTSx; -end; - -procedure TestTCipher_TEA.TearDown; -begin - FCipher_TEA.Free; - FCipher_TEA := nil; -end; - -procedure TestTCipher_TEA.TestClassByName; -var - ReturnValue : TDECCipherClass; -begin - ReturnValue := FCipher_TEA.ClassByName('TCipher_TEA'); - CheckEquals(TCipher_TEA, ReturnValue, 'Class is not registered'); -end; - -procedure TestTCipher_TEA.TestContext; -var - ReturnValue: TCipherContext; -begin - ReturnValue := FCipher_TEA.Context; - - CheckEquals( 16, ReturnValue.KeySize); - CheckEquals( 8, ReturnValue.BlockSize); - CheckEquals( 8, ReturnValue.BufferSize); - CheckEquals( 32, ReturnValue.AdditionalBufferSize); - CheckEquals( 16, ReturnValue.MinRounds); - CheckEquals( 256, ReturnValue.MaxRounds); - CheckEquals(false, ReturnValue.NeedsAdditionalBufferBackup); - CheckEquals(true, [ctBlock, ctSymmetric] = ReturnValue.CipherType); -end; - -procedure TestTCipher_TEA.TestDecode; -begin - DoTestDecode(FCipher_TEA.DecodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_TEA.TestEncode; -begin - DoTestEncode(FCipher_TEA.EncodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_TEA.TestIdentity; -begin - CheckEquals($011B81DD, FCipher_TEA.Identity); -end; - -procedure TestTCipher_XTEA.Done; -begin - FCipher_XTEA.Done; -end; - -procedure TestTCipher_XTEA.Init(TestData: TCipherTestData); -begin - LimitKeyLength(TestData.Key, FCipher_XTEA.Context.KeySize); - - FCipher_XTEA.Mode := TestData.Mode; - FCipher_XTEA.Init(BytesOf(TestData.Key), - BytesOf(TestData.InitVector), - TestData.Filler); -end; - -procedure TestTCipher_XTEA.SetUp; -begin - // Source of the test data used: - // https://github.com/froydnj/ironclad/blob/master/testing/test-vectors/xtea.testvec - FCipher_XTEA := TCipher_XTEA.Create; -{ TODO : Should be specified via FTestData? But how to apply? } - FCipher_XTEA.Rounds := 32; - - SetLength(FTestData, 4); - FTestData[0].OutputData := 'd8d4e9ded91e13f7'; - FTestData[0].InputData := TFormat_HEX.Decode('0000000000000000'); - - FTestData[0].Key := #$00#$00#$00#$00#$00#$00#$00#$00#$00#$00#$00#$00#$00#$00#$00#$00; - FTestData[0].InitVector := ''; - FTestData[0].Filler := $00; - FTestData[0].Mode := cmECBx; - - FTestData[1].OutputData := '058c7e0537191550'; - FTestData[1].InputData := TFormat_HEX.Decode('0000000000000000'); - - FTestData[1].Key := RawByteString(#$00#$00#$00#$80#$00#$00#$00#$00#$00#$00#$00#$00#$00#$00#$00#$00); - FTestData[1].InitVector := ''; - FTestData[1].Filler := $00; - FTestData[1].Mode := cmECBx; - - FTestData[2].OutputData := 'ef175e2818e3d22f'; - FTestData[2].InputData := TFormat_HEX.Decode('1a1a1a1a1a1a1a1a'); - - FTestData[2].Key := #$1a#$1a#$1a#$1a#$1a#$1a#$1a#$1a#$1a#$1a#$1a#$1a#$1a#$1a#$1a#$1a; - FTestData[2].InitVector := ''; - FTestData[2].Filler := $00; - FTestData[2].Mode := cmECBx; - - FTestData[3].Key := TFormat_BigEndian32.Decode( - RawByteString(#$2B#$D6#$45#$9F#$82#$C5#$B3#$00 + - #$95#$2C#$49#$10#$48#$81#$FF#$48)); - FTestData[3].InitVector := ''; - FTestData[3].Filler := $00; - FTestData[3].Mode := cmECBx; - - FTestData[3].OutputData := '0a1eb4673a595fa0'; - FTestData[3].InputData := TFormat_HEX.Decode('144702ea844d5cad'); -end; - -procedure TestTCipher_XTEA.TearDown; -begin - FCipher_XTEA.Free; - FCipher_XTEA := nil; -end; - -procedure TestTCipher_XTEA.TestClassByName; -var - ReturnValue : TDECCipherClass; -begin - ReturnValue := FCipher_XTEA.ClassByName('TCipher_XTEA'); - CheckEquals(TCipher_XTEA, ReturnValue, 'Class is not registered'); -end; - -procedure TestTCipher_XTEA.TestContext; -var - ReturnValue: TCipherContext; -begin - ReturnValue := FCipher_XTEA.Context; - - CheckEquals( 16, ReturnValue.KeySize); - CheckEquals( 8, ReturnValue.BlockSize); - CheckEquals( 8, ReturnValue.BufferSize); - CheckEquals( 32, ReturnValue.AdditionalBufferSize); - CheckEquals( 16, ReturnValue.MinRounds); - CheckEquals( 256, ReturnValue.MaxRounds); - CheckEquals(false, ReturnValue.NeedsAdditionalBufferBackup); - CheckEquals(true, [ctBlock, ctSymmetric] = ReturnValue.CipherType); -end; - -procedure TestTCipher_XTEA.TestDecode; -begin - DoTestDecode(FCipher_XTEA.DecodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_XTEA.TestEncode; -begin - DoTestEncode(FCipher_XTEA.EncodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_XTEA.TestIdentity; -begin - CheckEquals($CDBB621D, FCipher_XTEA.Identity); -end; - -procedure TestTCipher_XTEA_DEC52.Done; -begin - FCipher_XTEA_DEC52.Done; -end; - -procedure TestTCipher_XTEA_DEC52.DoTestClassByName; -var - ReturnValue : TDECCipherClass; -begin - ReturnValue := FCipher_XTEA_DEC52.ClassByName('TCipher_XTEA_DEC52'); - // This line should never be executed due to ClassByName rising an exception - // but it suppresses a ReturnValue is not being used compiler warning - CheckEquals(TCipher_XTEA_DEC52, ReturnValue, 'Class is not registered'); -end; - -procedure TestTCipher_XTEA_DEC52.Init(TestData: TCipherTestData); -begin - LimitKeyLength(TestData.Key, FCipher_XTEA_DEC52.Context.KeySize); - - FCipher_XTEA_DEC52.Mode := TestData.Mode; - FCipher_XTEA_DEC52.Init(BytesOf(TestData.Key), - BytesOf(TestData.InitVector), - TestData.Filler); -end; - -procedure TestTCipher_XTEA_DEC52.SetUp; -begin - // Source of the test data used: Hagen's original test vectors - FCipher_XTEA_DEC52 := TCipher_XTEA_DEC52.Create; - FCipher_XTEA_DEC52.Rounds := 16; - - SetLength(FTestData, 1); - FTestData[0].OutputData := 'cd7ebba2921a4b3be29e62cff71da5df63339429e2367c663ff81af90278bfa1'; - FTestData[0].InputData := TFormat_ESCAPE.Decode('\x30\x44\xED\x6E\x45\xA4' + - '\x96\xF5\xF6\x35\xA2\xEB' + - '\x3D\x1A\x5D\xD6\xCB\x1D' + - '\x09\x82\x2D\xBD\xF5\x60' + - '\xC2\xB8\x58\xA1\x91\xF9' + - '\x81\xB1'); - - // The key for this test vector is the old name of the class which was - // TCipher_TEAN as the vector already existed in DEC 5.2 but the class got - // renamed later on - FTestData[0].Key := 'TCipher_TEAN'; - FTestData[0].InitVector := ''; - FTestData[0].Filler := $FF; - FTestData[0].Mode := cmCTSx; -end; - -procedure TestTCipher_XTEA_DEC52.TearDown; -begin - FCipher_XTEA_DEC52.Free; - FCipher_XTEA_DEC52 := nil; -end; - -procedure TestTCipher_XTEA_DEC52.TestClassByName; -begin - // Class shall not be registered by default - CheckException(DoTestClassByName, EDECClassNotRegisteredException); -end; - -procedure TestTCipher_XTEA_DEC52.TestContext; -var - ReturnValue: TCipherContext; -begin - ReturnValue := FCipher_XTEA_DEC52.Context; - - CheckEquals( 16, ReturnValue.KeySize); - CheckEquals( 8, ReturnValue.BlockSize); - CheckEquals( 8, ReturnValue.BufferSize); - CheckEquals( 32, ReturnValue.AdditionalBufferSize); - CheckEquals( 16, ReturnValue.MinRounds); - CheckEquals( 256, ReturnValue.MaxRounds); - CheckEquals(false, ReturnValue.NeedsAdditionalBufferBackup); - CheckEquals(true, [ctBlock, ctSymmetric] = ReturnValue.CipherType); -end; - -procedure TestTCipher_XTEA_DEC52.TestDecode; -begin - DoTestDecode(FCipher_XTEA_DEC52.DecodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_XTEA_DEC52.TestEncode; -begin - DoTestEncode(FCipher_XTEA_DEC52.EncodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_XTEA_DEC52.TestIdentity; -begin - CheckEquals($59A6BE1E, FCipher_XTEA_DEC52.Identity); -end; - -{ TCipherBasis } - -function TCipherBasis.ConvertHexVectorToBytes(Vector: string): TBytes; -var - sl: TStringList; - i : Integer; - s : string; -begin - System.Assert(Length(Vector) mod 4 = 0, 'Char count of ' + Vector + ' is not integral'); - - SetLength(Result, Length(Vector) div 4); - - if (Vector <> '') then - begin - sl := TStringList.Create; - try - sl.Delimiter := '\'; - sl.DelimitedText := StringReplace(Vector, 'x', '', [rfReplaceAll]); - - // first element is always empty - sl.Delete(0); - s := ''; - for i := 0 to sl.Count - 1 do - begin - sl[i] := '0x' + sl[i]; - Result[i] := StrToInt(sl[i]); - end; - finally - sl.Free; - end; - end; -end; - -procedure TCipherBasis.DoTestDecode(DecodeFunct: TEncodeDecodeFunc; InitProc: TInitProc; DoneProc: TDoneProc); -var - Data : TCipherTestData; - Result : TBytes; - TempResultHex : RawByteString; -begin -{ TODO : -Das Problem ist hier: dass wir zu low level testen, da die bisherigen Textvektoren -ja immer von einem bestimmten CipherModus ausgehen, und nicht die -einzelnen DoEncode/DoDecode primitive. Diese sind später zu testen, wenn -wir die bisherigen Vektoren testen können. Dann können wir die nötigen -Daten synthetisieren. } - for Data in FTestData do - begin - InitProc(Data); - Result := DecodeFunct(RawByteString(Data.OutputData), TFormat_HEXL); - DoneProc; - - TempResultHex := RawByteString(StringOf(Result)); - - CheckEquals(TFormat_HEXL.Encode(Data.InputData), TFormat_HEXL.Encode(TempResultHex)); - end; -end; - -procedure TCipherBasis.DoTestEncode(EncodeFunc: TEncodeDecodeFunc; InitProc: TInitProc; DoneProc: TDoneProc); -var - Data : TCipherTestData; - Result : TBytes; - TempResultHex : RawByteString; -begin -{ TODO : -Das Problem ist hier: dass wir zu low level testen, da die bisherigen Testvektoren -ja immer von einem bestimmten CipherModus ausgehen, und nicht die -einzelnen DoEncode/DoDecode primitive. Diese sind später zu testen, wenn -wir die bisherigen Vektoren testen können. Dann können wir die nötigen -Daten synthetisieren. } - for Data in FTestData do - begin - InitProc(Data); - Result := EncodeFunc(RawByteString(Data.InputData), TFormat_COPY); - DoneProc; - - TempResultHex := TFormat_HEXL.Encode(Result[0], length(Result)); - - CheckEquals(Data.OutputData, TempResultHex); - end; -end; - -procedure TCipherBasis.LimitKeyLength(var Key: RawByteString; KeySize: Integer); -begin - if Length(Key) > KeySize then - Delete(Key, KeySize + 1, length(Key)); -end; - -{ TestTDECCipher } - -procedure TestTDECCipher.SetUp; -begin - inherited; -end; - -procedure TestTDECCipher.TearDown; -begin - inherited; -end; - -procedure TestTDECCipher.TestIsClassListCreated; -begin - CheckEquals(true, assigned(TDECCipher.ClassList), 'Class list has not been created in initialization'); -end; - -procedure TestTDECCipher.TestValidCipherSetDefaultCipherClass; -var - result : Boolean; -begin - // Asumption: nobody has called SetDefaultCipher yet - result := ValidCipher(nil) = TCipher_Null; - CheckEquals(true, result, 'Initial default cipher is not TCipher_Null'); - - try - SetDefaultCipherClass(TCipher_AES); - result := ValidCipher(nil) = TCipher_AES; - CheckEquals(true, result, 'Changed default cipher is not TCipher_AES'); - - SetDefaultCipherClass(TCipher_TEA); - result := ValidCipher(nil) = TCipher_TEA; - CheckEquals(true, result, 'Changed default cipher is not TCipher_TEA'); - - result := ValidCipher(TCipher_XTEA) = TCipher_XTEA; - CheckEquals(true, result, 'Passed cipher is not TCipher_XTEA'); - finally - SetDefaultCipherClass(TCipher_Null); - end; -end; - -{ TestTCipher_AES } - -procedure TestTCipher_AES.Done; -begin - FCipher_AES.Done; -end; - -procedure TestTCipher_AES.Init(TestData: TCipherTestData); -begin - LimitKeyLength(TestData.Key, FCipher_AES.Context.KeySize); - - FCipher_AES.Mode := TestData.Mode; - FCipher_AES.Init(BytesOf(TestData.Key), - BytesOf(TestData.InitVector), - TestData.Filler); -end; - -procedure TestTCipher_AES.SetUp; -begin - FCipher_AES := TCipher_AES.Create; - - SetLength(FTestData, 5); - FTestData[0].OutputData := '946d2b5ee0ad1b5ca523a513958b3d2d9387f3374551f6589be7901b3687f9a9'; - FTestData[0].InputData := TFormat_ESCAPE.Decode('\x30\x44\xED\x6E\x45\xA4' + - '\x96\xF5\xF6\x35\xA2\xEB' + - '\x3D\x1A\x5D\xD6\xCB\x1D' + - '\x09\x82\x2D\xBD\xF5\x60' + - '\xC2\xB8\x58\xA1\x91\xF9' + - '\x81\xB1'); - - FTestData[0].Key := 'TCipher_Rijndael'; - FTestData[0].InitVector := ''; - FTestData[0].Filler := $FF; - FTestData[0].Mode := cmCTSx; - - // Original test vectors from Nist FIPS 197 AES standard description - // AES 128 - FTestData[1].OutputData := '69c4e0d86a7b0430d8cdb78070b4c55a'; - FTestData[1].InputData := TFormat_HEXL.Decode('00112233445566778899aabbccddeeff'); - - FTestData[1].Key := TFormat_HEXL.Decode('000102030405060708090a0b0c0d0e0f'); - FTestData[1].InitVector := ''; - FTestData[1].Filler := $FF; - FTestData[1].Mode := cmECBx; - - // AES 192 - FTestData[2].OutputData := 'dda97ca4864cdfe06eaf70a0ec0d7191'; - FTestData[2].InputData := TFormat_HEXL.Decode('00112233445566778899aabbccddeeff'); - - FTestData[2].Key := TFormat_HEXL.Decode('000102030405060708090a0b0c0d0e0f1011121314151617'); - FTestData[2].InitVector := ''; - FTestData[2].Filler := $FF; - FTestData[2].Mode := cmECBx; - - // AES 256 - FTestData[3].OutputData := '8ea2b7ca516745bfeafc49904b496089'; - FTestData[3].InputData := TFormat_HEXL.Decode('00112233445566778899aabbccddeeff'); - - FTestData[3].Key := TFormat_HEXL.Decode('000102030405060708090a0b0c0d0e0f'+ - '101112131415161718191a1b1c1d1e1f'); - FTestData[3].InitVector := ''; - FTestData[3].Filler := $FF; - FTestData[3].Mode := cmECBx; - - // CBC - FTestData[4].OutputData := '8859653cb4c4e4ca3add490015ac8860fa59d1e233301563b184fcca95790c8c'; - FTestData[4].InputData := 'abcdefghijklmnopqrstuv0123456789'; - - FTestData[4].Key := TFormat_HEXL.Decode('30313233343536373839303132333435'); - FTestData[4].InitVector := TFormat_HEXL.Decode('30313233343536373839303132333435'); - FTestData[4].Filler := $FF; - FTestData[4].Mode := cmCBCx; -end; - -procedure TestTCipher_AES.TearDown; -begin - FCipher_AES.Free; - FCipher_AES := nil; -end; - -procedure TestTCipher_AES.TestClassByName; -var - ReturnValue : TDECCipherClass; -begin - ReturnValue := FCipher_AES.ClassByName('TCipher_AES'); - CheckEquals(TCipher_AES, ReturnValue, 'Class is not registered'); -end; - -procedure TestTCipher_AES.TestContext; -var - ReturnValue: TCipherContext; -begin - ReturnValue := FCipher_AES.Context; - - CheckEquals( 32, ReturnValue.KeySize); - CheckEquals( 16, ReturnValue.BlockSize); - CheckEquals( 16, ReturnValue.BufferSize); - CheckEquals( 480, ReturnValue.AdditionalBufferSize); - CheckEquals( 1, ReturnValue.MinRounds); - CheckEquals( 1, ReturnValue.MaxRounds); - CheckEquals(false, ReturnValue.NeedsAdditionalBufferBackup); - CheckEquals(true, [ctBlock, ctSymmetric] = ReturnValue.CipherType); -end; - -procedure TestTCipher_AES.TestDecode; -begin - DoTestDecode(FCipher_AES.DecodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_AES.TestEncode; -begin - DoTestEncode(FCipher_AES.EncodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_AES.TestIdentity; -begin - CheckEquals($E84F910E, FCipher_AES.Identity); -end; - -{ TestTCipher_Rijndael } - -procedure TestTCipher_Rijndael.Done; -begin - FCipher_Rijndael.Done; -end; - -procedure TestTCipher_Rijndael.Init(TestData: TCipherTestData); -begin - LimitKeyLength(TestData.Key, FCipher_Rijndael.Context.KeySize); - - FCipher_Rijndael.Mode := TestData.Mode; - FCipher_Rijndael.Init(BytesOf(TestData.Key), - BytesOf(TestData.InitVector), - TestData.Filler); -end; - -procedure TestTCipher_Rijndael.SetUp; -begin - FCipher_Rijndael := TCipher_Rijndael.Create; - - SetLength(FTestData, 1); - FTestData[0].OutputData := '946d2b5ee0ad1b5ca523a513958b3d2d9387f3374551f6589be7901b3687f9a9'; - FTestData[0].InputData := TFormat_ESCAPE.Decode('\x30\x44\xED\x6E\x45\xA4' + - '\x96\xF5\xF6\x35\xA2\xEB' + - '\x3D\x1A\x5D\xD6\xCB\x1D' + - '\x09\x82\x2D\xBD\xF5\x60' + - '\xC2\xB8\x58\xA1\x91\xF9' + - '\x81\xB1'); - - FTestData[0].Key := 'TCipher_Rijndael'; - FTestData[0].InitVector := ''; - FTestData[0].Filler := $FF; - FTestData[0].Mode := cmCTSx; -end; - -procedure TestTCipher_Rijndael.TearDown; -begin - FCipher_Rijndael.Free; - FCipher_Rijndael := nil; -end; - -procedure TestTCipher_Rijndael.DoTestClassByName; -var - ReturnValue : TDECCipherClass; -begin - ReturnValue := FCipher_Rijndael.ClassByName('TCipher_Rijndael'); - // This line should never be executed due to ClassByName rising an exception - // but it suppresses a ReturnValue is not being used compiler warning - CheckEquals(TCipher_Rijndael, ReturnValue, 'Class is not registered'); -end; - -procedure TestTCipher_Rijndael.TestClassByName; -begin - CheckException(DoTestClassByName, EDECClassNotRegisteredException); -end; - -procedure TestTCipher_Rijndael.TestContext; -var - ReturnValue: TCipherContext; -begin - ReturnValue := FCipher_Rijndael.Context; - - CheckEquals( 32, ReturnValue.KeySize); - CheckEquals( 16, ReturnValue.BlockSize); - CheckEquals( 16, ReturnValue.BufferSize); - CheckEquals( 480, ReturnValue.AdditionalBufferSize); - CheckEquals( 1, ReturnValue.MinRounds); - CheckEquals( 1, ReturnValue.MaxRounds); - CheckEquals(false, ReturnValue.NeedsAdditionalBufferBackup); - CheckEquals(true, [ctBlock, ctSymmetric] = ReturnValue.CipherType); -end; - -procedure TestTCipher_Rijndael.TestDecode; -begin - DoTestDecode(FCipher_Rijndael.DecodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_Rijndael.TestEncode; -begin - DoTestEncode(FCipher_Rijndael.EncodeStringToBytes, self.Init, self.Done); -end; - -procedure TestTCipher_Rijndael.TestIdentity; -begin - CheckEquals($F8B830A5, FCipher_Rijndael.Identity); -end; - -initialization - // Register all test classes - {$IFDEF DUnitX} - TDUnitX.RegisterTestFixture(TestTDECCipher); - TDUnitX.RegisterTestFixture(TestTCipher_Null); - TDUnitX.RegisterTestFixture(TestTCipher_Blowfish); - TDUnitX.RegisterTestFixture(TestTCipher_Twofish); - TDUnitX.RegisterTestFixture(TestTCipher_IDEA); - TDUnitX.RegisterTestFixture(TestTCipher_Cast256); - TDUnitX.RegisterTestFixture(TestTCipher_Mars); - TDUnitX.RegisterTestFixture(TestTCipher_RC4); - TDUnitX.RegisterTestFixture(TestTCipher_RC6); - TDUnitX.RegisterTestFixture(TestTCipher_AES); - TDUnitX.RegisterTestFixture(TestTCipher_Rijndael); - TDUnitX.RegisterTestFixture(TestTCipher_Square); - TDUnitX.RegisterTestFixture(TestTCipher_SCOP); - TDUnitX.RegisterTestFixture(TestTCipher_SCOP_DEC52); - TDUnitX.RegisterTestFixture(TestTCipher_Sapphire); - TDUnitX.RegisterTestFixture(TestTCipher_1DES); - TDUnitX.RegisterTestFixture(TestTCipher_2DES); - TDUnitX.RegisterTestFixture(TestTCipher_3DES); - TDUnitX.RegisterTestFixture(TestTCipher_2DDES); - TDUnitX.RegisterTestFixture(TestTCipher_3DDES);; - TDUnitX.RegisterTestFixture(TestTCipher_3TDES);; - TDUnitX.RegisterTestFixture(TestTCipher_3Way); - TDUnitX.RegisterTestFixture(TestTCipher_Cast128); - TDUnitX.RegisterTestFixture(TestTCipher_Gost); - TDUnitX.RegisterTestFixture(TestTCipher_Magma); - TDUnitX.RegisterTestFixture(TestTCipher_Misty); - TDUnitX.RegisterTestFixture(TestTCipher_NewDES); - TDUnitX.RegisterTestFixture(TestTCipher_Q128); - TDUnitX.RegisterTestFixture(TestTCipher_RC2); - TDUnitX.RegisterTestFixture(TestTCipher_RC5); - TDUnitX.RegisterTestFixture(TestTCipher_SAFER); - TDUnitX.RegisterTestFixture(TestTCipher_Shark); - TDUnitX.RegisterTestFixture(TestTCipher_Shark_DEC52); - TDUnitX.RegisterTestFixture(TestTCipher_Skipjack); - TDUnitX.RegisterTestFixture(TestTCipher_TEA); - TDUnitX.RegisterTestFixture(TestTCipher_XTEA); - TDUnitX.RegisterTestFixture(TestTCipher_XTEA_DEC52); - {$ELSE} - RegisterTests('DECCipher', [TestTDECCipher.Suite, - TestTCipher_Null.Suite, - TestTCipher_Blowfish.Suite, - TestTCipher_Twofish.Suite, - TestTCipher_IDEA.Suite, - TestTCipher_Cast256.Suite, - TestTCipher_Mars.Suite, - TestTCipher_RC4.Suite, - TestTCipher_RC6.Suite, - TestTCipher_AES.Suite, - TestTCipher_Rijndael.Suite, - TestTCipher_Square.Suite, - TestTCipher_SCOP.Suite, - TestTCipher_SCOP_DEC52.Suite, - TestTCipher_Sapphire.Suite, - TestTCipher_1DES.Suite, - TestTCipher_2DES.Suite, - TestTCipher_3DES.Suite, - TestTCipher_2DDES.Suite, - TestTCipher_3DDES.Suite, - TestTCipher_3TDES.Suite, - TestTCipher_3Way.Suite, - TestTCipher_Cast128.Suite, - TestTCipher_Gost.Suite, - TestTCipher_Magma.Suite, - TestTCipher_Misty.Suite, - TestTCipher_NewDES.Suite, - TestTCipher_Q128.Suite, - TestTCipher_RC2.Suite, - TestTCipher_RC5.Suite, - TestTCipher_SAFER.Suite, - TestTCipher_Shark.Suite, - TestTCipher_Shark_DEC52.Suite, - TestTCipher_Skipjack.Suite, - TestTCipher_TEA.Suite, - TestTCipher_XTEA.Suite, - TestTCipher_XTEA_DEC52.Suite]); - {$ENDIF} -end. - diff --git a/Unit Tests/Tests/TestDECCipherFormats.pas b/Unit Tests/Tests/TestDECCipherFormats.pas deleted file mode 100644 index 2d486a42..00000000 --- a/Unit Tests/Tests/TestDECCipherFormats.pas +++ /dev/null @@ -1,690 +0,0 @@ -{***************************************************************************** - The DEC team (see file NOTICE.txt) licenses this file - to you under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - A copy of this licence is found in the root directory of - this project in the file LICENCE.txt or alternatively at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, - software distributed under the License is distributed on an - "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - KIND, either express or implied. See the License for the - specific language governing permissions and limitations - under the License. -*****************************************************************************} - -{$M+} // DUnitX would add it anyway -unit TestDECCipherFormats; - -// Needs to be included before any other statements -{$INCLUDE TestDefines.inc} - -interface - -uses - {$IFDEF DUnitX} - DUnitX.TestFramework,DUnitX.DUnitCompatibility, - {$ELSE} - TestFramework, - {$ENDIF} - System.Classes, System.SysUtils, - DECCipherBase, DECCiphers, DECCipherFormats; - -type - /// - /// All known testvectors use the same filler byte and the same cmCTSx mode - /// - TCipherTestData = record - PlainTextData : RawByteString; - EncryptedTextData : RawByteString; - EncryptedUTF16TextData : string; - - Key : RawByteString; - InitVector : RawByteString; - Filler : Byte; - Mode : TCipherMode; - end; - - // Test methods for class TDECClassList - {$IFDEF DUnitX} [TestFixture] {$ENDIF} - TestTDECCipherFormats = class(TTestCase) - strict private - FCipherTwoFish : TDECFormattedCipher; - - /// - /// Array with the test data - /// - FTestData : array of TCipherTestData; - - /// - /// Ensures that a given key is not longer then the KeySize passed - /// - /// - /// Key to be checked. if it is longer than KeySize it will be cut off. - /// - /// - /// Maximum size of a key for the given cipher algorithm - /// - procedure LimitKeyLength(var Key:RawByteString; KeySize: Integer); - - /// - /// Initialization routine which sets the properties of the crypto object - /// as specified in the test data record with the given index. - /// - /// - /// Index of the test data record to be used for this test run initialization - /// - procedure Init(Index: Integer); - - - {$IFDEF ANSISTRINGSUPPORTED} - /// - /// Copies the bytes of the buffer into an AnsiString - /// - /// - /// Byte buffer to be converted to an AnsiString - /// - /// - /// AnsiString converted buffer. If the buffer passed has a length of 0 - /// an empty string will be returned - /// - function AnsiStringOf(const Bytes: TBytes): AnsiString; - {$ENDIF} - public - procedure SetUp; override; - procedure TearDown; override; - published - procedure TestEncodeBytes; - procedure TestDecodeBytes; - - procedure TestEncodeStream; - procedure TestDecodeStream; - -// procedure TestCalculateStringData; - -// Currently commented out because it would require a file as external dependency -// procedure TestEncodeFile(const SourceFileName, DestFileName: string; -// const Progress: IDECProgress = nil); -// procedure TestDecodeFile(const SourceFileName, DestFileName: string; -// const Progress: IDECProgress = nil); - - procedure TestEncodeStringToBytes; - procedure TestEncodeRawByteStringToBytes; - - procedure TestEncodeStringToString; - procedure TestEncodeRawByteStringToString; - - procedure TestDecodeStringToBytes; - procedure TestDecodeRawByteStringToBytes; - - procedure TestDecodeStringToString; - procedure TestDecodeRawByteStringToString; - - -{$IFDEF ANSISTRINGSUPPORTED} - procedure TestEncodeAnsiStringToBytes; - procedure TestEncodeAnsiStringToString; - - procedure TestDecodeAnsiStringToBytes; - procedure TestDecodeAnsiStringToString; -{$ENDIF} - -{$IFNDEF NEXTGEN} - procedure TestEncodeWideStringToBytes; - procedure TestEncodeWideStringToString; - - procedure TestDecodeWideStringToBytes; - procedure TestDecodeWideStringToString; -{$ENDIF} - end; - -implementation - -uses - DECBaseClass, DECFormat, DECUtil; - -{ TestTDECCipherFormats } - -procedure TestTDECCipherFormats.LimitKeyLength(var Key: RawByteString; - KeySize: Integer); -begin - if Length(Key) > KeySize then - Delete(Key, KeySize + 1, length(Key)); -end; - -{$IFDEF ANSISTRINGSUPPORTED} -function TestTDECCipherFormats.AnsiStringOf(const Bytes: TBytes): AnsiString; -begin - if Assigned(Bytes) then - begin - SetLength(Result, length(Bytes)); - {$IF CompilerVersion >= 24.0} - Move(Bytes[0], Result[low(Result)], length(Bytes)); - {$ELSE} - Move(Bytes[0], Result[1], length(Bytes)); - {$IFEND} - end - else - Result := ''; -end; -{$ENDIF} - -procedure TestTDECCipherFormats.TestDecodeRawByteStringToBytes; -var - i : Integer; - result : TBytes; -begin - for i := 0 to High(FTestData) do - begin - Init(i); - - result := FCipherTwoFish.DecodeStringToBytes(TFormat_HexL.Decode(FTestData[i].EncryptedTextData)); - - CheckEquals(FTestData[i].PlainTextData, - RawByteString(StringOf(result)), - 'Failure in TestDecodeRawByteStringToBytes ' + IntToStr(i)); - end; -end; - -procedure TestTDECCipherFormats.TestDecodeStringToBytes; -var - i : Integer; - result : TBytes; - InputStr : string; - ExpStr : string; - ResStr : string; -begin - for i := 0 to High(FTestData) do - begin - Init(i); - - result := TFormat_HexL.Decode(BytesOf(FTestData[i].EncryptedUTF16TextData)); - InputStr := StringOf(result); - - result := FCipherTwoFish.DecodeStringToBytes(InputStr); - ResStr := WideStringOf(result); - - ExpStr := string(FTestData[i].PlainTextData); - CheckEquals(ExpStr, ResStr, 'Failure in TestDecodeStringToBytes ' + IntToStr(i)); - end; -end; - -procedure TestTDECCipherFormats.Init(Index: Integer); -begin - LimitKeyLength(FTestData[Index].Key, FCipherTwoFish.Context.KeySize); - - FCipherTwoFish.Mode := FTestData[Index].Mode; - FCipherTwoFish.Init(BytesOf(FTestData[Index].Key), - BytesOf(FTestData[Index].InitVector), - FTestData[Index].Filler); -end; - -procedure TestTDECCipherFormats.SetUp; -begin - FCipherTwoFish := TCipher_Twofish.Create; - - SetLength(FTestData, 1); - - FTestData[0].EncryptedTextData := 'e81674f9bc69442188c949bb52e1e47874171177e99' + - 'dbbe9880875094f8dfe21'; - FTestData[0].PlainTextData := TFormat_ESCAPE.Decode('\x30\x44\xED\x6E\x45\xA4' + - '\x96\xF5\xF6\x35\xA2\xEB' + - '\x3D\x1A\x5D\xD6\xCB\x1D' + - '\x09\x82\x2D\xBD\xF5\x60' + - '\xC2\xB8\x58\xA1\x91\xF9' + - '\x81\xB1'); - // In this first test case simply the RawByteString based test data filled up - // with a 0 in each char to form a UTF16 char - FTestData[0].EncryptedUTF16TextData := 'ebc6a21d2a7d8341f643a0bf494057d5a5c38f' + - '0ae72bd4ced90b5e6467de24c7d06b88207a41' + - 'f9d32126e38ab49024c98788b8619c3cbeb7fa' + - 'ad2cd9b7e40480'; - - FTestData[0].Key := 'TCipher_Twofish'; - FTestData[0].InitVector := ''; - FTestData[0].Filler := $FF; - FTestData[0].Mode := cmCTSx; -end; - -procedure TestTDECCipherFormats.TearDown; -begin - FCipherTwoFish.Free; -end; - -{$IFDEF ANSISTRINGSUPPORTED} -procedure TestTDECCipherFormats.TestDecodeAnsiStringToBytes; -var - i : Integer; - result : TBytes; -begin - for i := 0 to High(FTestData) do - begin - Init(i); - - result := FCipherTwoFish.DecodeStringToBytes(AnsiString(TFormat_HexL.Decode(FTestData[i].EncryptedTextData))); - - CheckEquals(FTestData[i].PlainTextData, - AnsiStringOf(result), - 'Failure in TestDecodeAnsiStringToBytes ' + IntToStr(i)); - end; -end; - -procedure TestTDECCipherFormats.TestDecodeAnsiStringToString; -var - i : Integer; - result : AnsiString; - InputStr : AnsiString; - StrArr : TBytes; -begin - for i := 0 to High(FTestData) do - begin - Init(i); - - StrArr := BytesOf(FTestData[i].EncryptedTextData); - StrArr := TFormat_HexL.Decode(StrArr); - InputStr := AnsiStringOf(StrArr); - - result := FCipherTwoFish.DecodeStringToString(InputStr); - - CheckEquals(AnsiString(FTestData[i].PlainTextData), - result, - 'Failure in TestDecodeAnsiStringToString ' + IntToStr(i)); - end; -end; -{$ENDIF} - -procedure TestTDECCipherFormats.TestDecodeBytes; -var - i : Integer; - result : TBytes; -begin - for i := 0 to High(FTestData) do - begin - Init(i); - - result := FCipherTwoFish.DecodeBytes( - BytesOf(TFormat_HexL.Decode(FTestData[i].EncryptedTextData))); - - CheckEquals(FTestData[i].PlainTextData, - RawByteString(StringOf(result)), - 'Failure in TestDecodeBytes ' + IntToStr(i)); - end; -end; - -procedure TestTDECCipherFormats.TestDecodeRawByteStringToString; -var - i : Integer; - result : RawByteString; - InputStr : RawByteString; - StrArr : TBytes; -begin - for i := 0 to High(FTestData) do - begin - Init(i); - - StrArr := BytesOf(FTestData[i].EncryptedTextData); - StrArr := TFormat_HexL.Decode(StrArr); - InputStr := BytesToRawString(StrArr); - - result := FCipherTwoFish.DecodeStringToString(InputStr); - - CheckEquals(FTestData[i].PlainTextData, - result, - 'Failure in TestDecodeRawByteStringToString ' + IntToStr(i)); - end; -end; - -procedure TestTDECCipherFormats.TestDecodeStream; -var - Src, Dest : TMemoryStream; - SrcBuf : TBytes; - i : Integer; - result : TBytes; -begin - Src := TMemoryStream.Create; - try - - Dest := TMemoryStream.Create; - try - - for i := 0 to High(FTestData) do - begin - Init(i); - - SrcBuf := BytesOf(TFormat_HexL.Decode(FTestData[i].EncryptedTextData)); - - Src.Clear; - {$IF CompilerVersion >= 25.0} - Src.WriteData(SrcBuf, length(SrcBuf)); - {$ELSE} - Src.Write(SrcBuf[0], Length(SrcBuf)); - {$IFEND} - Src.Seek(0, TSeekOrigin.soBeginning); - - FCipherTwoFish.DecodeStream(Src, Dest, Src.Size, nil); - - Dest.Seek(0, TSeekOrigin.soBeginning); - SetLength(result, Dest.Size); - {$IF CompilerVersion >= 25.0} - Dest.Read(result, 0, Dest.Size); - {$ELSE} - Dest.Read(Result[0], Dest.Size); - {$IFEND} - - CheckEquals(FTestData[i].PlainTextData, - RawByteString(StringOf(result)), - 'Failure in TestDecodeStream ' + IntToStr(i)); - end; - - finally - Dest.Free; - end; - - finally - Src.Free; - end; -end; - -procedure TestTDECCipherFormats.TestDecodeStringToString; -var - i : Integer; - result : string; - InputStr : string; - StrArr : TBytes; -begin - for i := 0 to High(FTestData) do - begin - Init(i); - - StrArr := BytesOf(FTestData[i].EncryptedUTF16TextData); - StrArr := TFormat_HexL.Decode(StrArr); - InputStr := StringOf(StrArr); - - result := FCipherTwoFish.DecodeStringToString(InputStr); - - CheckEquals(string(FTestData[i].PlainTextData), - result, - 'Failure in TestDecodeStringToString ' + IntToStr(i)); - end; -end; - -{$IFNDEF NEXTGEN} -procedure TestTDECCipherFormats.TestDecodeWideStringToBytes; -var - i : Integer; - result : TBytes; - InputStr : WideString; - ExpStr : WideString; - ResStr : WideString; -begin - for i := 0 to High(FTestData) do - begin - Init(i); - - result := TFormat_HexL.Decode(BytesOf(FTestData[i].EncryptedUTF16TextData)); - InputStr := StringOf(result); - - result := FCipherTwoFish.DecodeStringToBytes(InputStr); - ResStr := WideStringOf(result); - - ExpStr := string(FTestData[i].PlainTextData); - CheckEquals(ExpStr, ResStr, 'Failure in TestDecodeWideStringToBytes ' + IntToStr(i)); - end; -end; - -procedure TestTDECCipherFormats.TestDecodeWideStringToString; -var - i : Integer; - result : WideString; - InputStr : WideString; - StrArr : TBytes; -begin - for i := 0 to High(FTestData) do - begin - Init(i); - - StrArr := BytesOf(FTestData[i].EncryptedUTF16TextData); - StrArr := TFormat_HexL.Decode(StrArr); - InputStr := StringOf(StrArr); - - result := FCipherTwoFish.DecodeStringToString(InputStr); - - CheckEquals(string(FTestData[i].PlainTextData), - string(result), - 'Failure in TestDecodeWideStringToString ' + IntToStr(i)); - end; -end; -{$ENDIF} - -{$IFDEF NEXTGEN} -procedure TestTDECCipherFormats.TestEncodeAnsiStringToBytes; -var - i : Integer; - result : TBytes; - InputStr : AnsiString; -begin - for i := 0 to High(FTestData) do - begin - Init(i); - - InputStr := AnsiString(FTestData[i].PlainTextData); - result := FCipherTwoFish.EncodeStringToBytes(InputStr); - - CheckEquals(FTestData[i].EncryptedTextData, - AnsiStringOf(TFormat_HexL.Encode(result)), - 'Failure in TestEncodeAnsiStringToBytes ' + IntToString(i)); - end; -end; - -procedure TestTDECCipherFormats.TestEncodeAnsiStringToString; -var - i : Integer; - result : AnsiString; - InputStr : AnsiString; -begin - for i := 0 to High(FTestData) do - begin - Init(i); - - InputStr := FTestData[i].PlainTextData; - result := FCipherTwoFish.EncodeStringToString(InputStr); - - CheckEquals(AnsiString(FTestData[i].EncryptedTextData), - AnsiStringOf(TFormat_HexL.Encode(BytesOf(result))), - 'Failure in TestEncodeAnsiStringToString ' + IntToString(i)); - end; -end; -{$ENDIF} - -procedure TestTDECCipherFormats.TestEncodeBytes; -var - i : Integer; - result : TBytes; -begin - for i := 0 to High(FTestData) do - begin - Init(i); - - result := FCipherTwoFish.EncodeBytes(BytesOf(FTestData[i].PlainTextData)); - - CheckEquals(FTestData[i].EncryptedTextData, - RawByteString(StringOf(TFormat_HexL.Encode(result))), - 'Failure in TestEncodeBytes ' + IntToStr(i)); - end; -end; - -procedure TestTDECCipherFormats.TestEncodeRawByteStringToBytes; -var - i : Integer; - result : TBytes; -begin - for i := 0 to High(FTestData) do - begin - Init(i); - - result := FCipherTwoFish.EncodeStringToBytes(FTestData[i].PlainTextData); - - CheckEquals(FTestData[i].EncryptedTextData, - RawByteString(StringOf(TFormat_HexL.Encode(result))), - 'Failure in TestEncodeRawByteStringToBytes ' + IntToStr(i)); - end; -end; - -procedure TestTDECCipherFormats.TestEncodeRawByteStringToString; -var - i : Integer; - result : RawByteString; - InputStr : RawByteString; -begin - for i := 0 to High(FTestData) do - begin - Init(i); - - InputStr := FTestData[i].PlainTextData; - result := FCipherTwoFish.EncodeStringToString(InputStr); - - CheckEquals(FTestData[i].EncryptedTextData, - BytesToRawString(TFormat_HexL.Encode(BytesOf(result))), - 'Failure in TestEncodeRawByteStringToString ' + IntToStr(i)); - end; -end; - -procedure TestTDECCipherFormats.TestEncodeStream; -var - Src, Dest : TMemoryStream; - SrcBuf : TBytes; - i : Integer; - result : TBytes; -begin - Src := TMemoryStream.Create; - try - - Dest := TMemoryStream.Create; - try - - for i := 0 to High(FTestData) do - begin - Init(i); - - SrcBuf := BytesOf(FTestData[i].PlainTextData); - - Src.Clear; - {$IF CompilerVersion >= 25.0} - Src.WriteData(SrcBuf, length(SrcBuf)); - {$ELSE} - Src.Write(SrcBuf[0], Length(SrcBuf)); - {$IFEND} - Src.Seek(0, TSeekOrigin.soBeginning); - - FCipherTwoFish.EncodeStream(Src, Dest, Src.Size, nil); - - Dest.Seek(0, TSeekOrigin.soBeginning); - SetLength(result, Dest.Size); - {$IF CompilerVersion >= 25.0} - Dest.Read(result, 0, Dest.Size); - {$ELSE} - Dest.Read(Result[0], Dest.Size); - {$IFEND} - - CheckEquals(FTestData[i].EncryptedTextData, - RawByteString(StringOf(TFormat_HexL.Encode(result))), - 'Failure in TestEncodeStream ' + IntToStr(i)); - end; - - finally - Dest.Free; - end; - - finally - Src.Free; - end; -end; - -procedure TestTDECCipherFormats.TestEncodeStringToBytes; -var - i : Integer; - result : TBytes; - InputStr : string; -begin - for i := 0 to High(FTestData) do - begin - Init(i); - - InputStr := string(FTestData[i].PlainTextData); - result := FCipherTwoFish.EncodeStringToBytes(InputStr); - - CheckEquals(FTestData[i].EncryptedUTF16TextData, - string(RawByteString(StringOf(TFormat_HexL.Encode(result)))), - 'Failure in TestEncodeStringToBytes ' + IntToStr(i)); - end; -end; - -procedure TestTDECCipherFormats.TestEncodeStringToString; -var - i : Integer; - result : string; - InputStr : string; -begin - for i := 0 to High(FTestData) do - begin - Init(i); - - InputStr := string(FTestData[i].PlainTextData); - result := FCipherTwoFish.EncodeStringToString(InputStr); - - CheckEquals(FTestData[i].EncryptedUTF16TextData, - StringOf(TFormat_HexL.Encode(BytesOf(result))), - 'Failure in TestEncodeStringToString ' + IntToStr(i)); - end; -end; - -{$IFNDEF NEXTGEN} -procedure TestTDECCipherFormats.TestEncodeWideStringToBytes; -var - i : Integer; - result : TBytes; - InputStr : WideString; -begin - for i := 0 to High(FTestData) do - begin - Init(i); - - InputStr := WideString(FTestData[i].PlainTextData); - result := FCipherTwoFish.EncodeStringToBytes(InputStr); - - CheckEquals(FTestData[i].EncryptedUTF16TextData, - string(RawByteString(StringOf(TFormat_HexL.Encode(result)))), - 'Failure in TestEncodeWideStringToBytes ' + IntToStr(i)); - end; -end; - -procedure TestTDECCipherFormats.TestEncodeWideStringToString; -var - i : Integer; - result : WideString; - InputStr : WideString; -begin - for i := 0 to High(FTestData) do - begin - Init(i); - - InputStr := WideString(FTestData[i].PlainTextData); - result := FCipherTwoFish.EncodeStringToString(InputStr); - - CheckEquals(FTestData[i].EncryptedUTF16TextData, - StringOf(TFormat_HexL.Encode(BytesOf(result))), - 'Failure in TestEncodeWideStringToString ' + IntToStr(i)); - end; -end; -{$ENDIF} - -initialization - // Register any test cases with the test runner - {$IFNDEF DUnitX} - RegisterTest(TestTDECCipherFormats.Suite); - {$ELSE} - TDUnitX.RegisterTestFixture(TestTDECCipherFormats); - {$ENDIF} -end. diff --git a/Unit Tests/Tests/TestDECCipherModes.pas b/Unit Tests/Tests/TestDECCipherModes.pas deleted file mode 100644 index 4613da43..00000000 --- a/Unit Tests/Tests/TestDECCipherModes.pas +++ /dev/null @@ -1,510 +0,0 @@ -{***************************************************************************** - The DEC team (see file NOTICE.txt) licenses this file - to you under the Apache License, Version 2.0 (the - "License"); you may not use this file except in compliance - with the License. A copy of this licence is found in the root directory of - this project in the file LICENCE.txt or alternatively at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, - software distributed under the License is distributed on an - "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - KIND, either express or implied. See the License for the - specific language governing permissions and limitations - under the License. -*****************************************************************************} - -{$M+} // DUnitX would add it anyway -unit TestDECCipherModes; - -interface - -// Needs to be included before any other statements -{$INCLUDE TestDefines.inc} - -uses - {$IFDEF DUnitX} - DUnitX.TestFramework,DUnitX.DUnitCompatibility, - {$ELSE} - TestFramework, - {$ENDIF} - System.SysUtils, - DECCipherBase, DECCipherModes, DECCipherFormats, DECCiphers; - -type - /// - /// Class reference to be abe to specify duifferent cipher classes for - /// carrying out the different tests. - /// - TFormattedCipherClass = class of TDECFormattedCipher; - - /// - /// One entry in a list of tests - /// - TTestEntry = record - /// - /// Input value, needs to be of block size length or a multiple of it - /// - Input : RawByteString; - /// - /// Expected output value, needs to be of block size length or a multiple of it - /// - Output : RawByteString; - /// - /// Expected output value which is used if Output is empty. Contains the - /// output in hexadecimal notation. - /// - OutputHex : RawByteString; - /// - /// Init Vektor für den ersten Test - /// - InitVector : RawByteString; - /// - /// Class reference for the cipher class used for this test. - /// - TestClass : TFormattedCipherClass; - /// - /// Block concatenating/padding mode - /// - Mode : TCipherMode; - end; - - /// - /// Prototype for a function to be passed to the generic test method - /// - TTestFunction = procedure(Source, Dest: PByteArray; Size: Integer) of object; - - /// - /// Testmethoden für Klasse TDECCipherModes - /// - {$IFDEF DUnitX} [TestFixture] {$ENDIF} - TestTDECCipherModes = class(TTestCase) - strict private - const - Data: array[1..27] of TTestEntry = ((Input: 'ABCDEFGHIJKLMNOPQRSTUVWX'; - Output: 'ABCDEFGHIJKLMNOPQRSTUVWX'; - TestClass: TCipher_Null; - Mode: TCipherMode.cmECBx), - (Input: '000000000000000000000000'; - Output: '000000000000000000000000'; - TestClass: TCipher_Null; - Mode: TCipherMode.cmECBx), - (Input: '12345678'; - Output: '12345678'; - TestClass: TCipher_Null; - Mode: TCipherMode.cmECBx), - (Input: 'ABCDEFGHIJKLMNOPQRSTUVWX'; - Output: ''; - OutputHex: 'FE5A89A7A1F4BD29DFFADFCF2239E1F581106DA64C0AE704'; - TestClass: TCipher_1DES; - Mode: TCipherMode.cmOFB8), - (Input: '000000000000000000000000'; - Output: ''; - OutputHex: '8F28FAD3D482CA51A680A4B35F479E95E0720EC2296C806C'; - TestClass: TCipher_1DES; - Mode: TCipherMode.cmOFB8), - (Input: '12345678'; - Output: ''; - OutputHex: '8E2AF9D7D184CD59'; - TestClass: TCipher_1DES; - Mode: TCipherMode.cmOFB8), - (Input: 'ABCDEFGHIJKLMNOPQRSTUVWX'; - Output: ''; - OutputHex: 'FE604D3DF9C2AE3D7839AF5BDEE8FD9078544A1996EC4F1C'; - TestClass: TCipher_1DES; - Mode: TCipherMode.cmCFB8), - (Input: '000000000000000000000000'; - Output: ''; - OutputHex: '8FD637FC449CF89F1E5EEBB66BED15C7F8C63B4481F74C5A'; - TestClass: TCipher_1DES; - Mode: TCipherMode.cmCFB8), - (Input: '12345678'; - Output: ''; - OutputHex: '8EF08D6414063543'; - TestClass: TCipher_1DES; - Mode: TCipherMode.cmCFB8), - (Input: 'ABCDEFGHIJKLMNOPQRSTUVWX'; - Output: ''; - OutputHex: 'FEAB3839BBA059FC1FECBF798CEF537803F10F15967E3ABD'; - TestClass: TCipher_1DES; - Mode: TCipherMode.cmCFS8), - (Input: '000000000000000000000000'; - Output: ''; - OutputHex: '8F9661B53B06D611BA916562F4420DA4B6EFD550BF01DA2C'; - TestClass: TCipher_1DES; - Mode: TCipherMode.cmCFS8), - (Input: '12345678'; - Output: ''; - OutputHex: '8EEA2F2F86159953'; - TestClass: TCipher_1DES; - Mode: TCipherMode.cmCFS8), - (Input: 'ABCDEFGHIJKLMNOPQRSTUVWX'; - Output: ''; - OutputHex: 'FED41297FD52669B4221F913AF978D77292C958B2A9E289A'; - TestClass: TCipher_1DES; - Mode: TCipherMode.cmOFBx), - (Input: '000000000000000000000000'; - Output: ''; - OutputHex: '8FA661E3882411E33B5B826FD2E9F217484EF6EF4FF84FF2'; - TestClass: TCipher_1DES; - Mode: TCipherMode.cmOFBx), - (Input: '12345678'; - Output: ''; - OutputHex: '8EA462E78D2216EB'; - TestClass: TCipher_1DES; - Mode: TCipherMode.cmOFBx), - (Input: 'ABCDEFGHIJKLMNOPQRSTUVWX'; - Output: ''; - OutputHex: 'FED41297FD52669B0DEC818A383ADA358E469BE634B7AFBC'; - TestClass: TCipher_1DES; - Mode: TCipherMode.cmCFSx), - (Input: '000000000000000000000000'; - Output: ''; - OutputHex: '8FA661E3882411E3DB7258A29424D11F2BB6B4607D24D5DB'; - TestClass: TCipher_1DES; - Mode: TCipherMode.cmCFSx), - (Input: '12345678'; - Output: ''; - OutputHex: '8EA462E78D2216EB'; - TestClass: TCipher_1DES; - Mode: TCipherMode.cmCFSx), - (Input : 'ABCDEFGHIJKLMNOPQRSTUVWX'; - Output : 'qsqwqsq'+#$7f+'89:;<=>/ikioikiw'; - InitVector: '01234567'; - TestClass : TCipher_NULL; - Mode : TCipherMode.cmCBCx), - (Input : '000000000000000000000000'; - Output : '00000000' + #0#0#0#0#0#0#0#0 + '00000000'; - InitVector: #0#0#0#0#0#0#0#0; - TestClass : TCipher_NULL; - Mode : TCipherMode.cmCBCx), - (Input : '000000000000000000000000'; - Output : #0#1#2#3#4#5#6#7 + '01234567' + #0#1#2#3#4#5#6#7; - InitVector: '01234567'; - TestClass : TCipher_NULL; - Mode : TCipherMode.cmCBCx), - (Input: 'ABCDEFGHIJKLMNOPQRSTUVWX'; - Output: ''; - OutputHex: 'FD73DA2F279926A19A65EFA8EBA5EEB67A778C6CD73294F5'; - TestClass: TCipher_1DES; - Mode: TCipherMode.cmCTSx), - (Input: '000000000000000000000000'; - Output: ''; - OutputHex: '1D538CCCF38138A6BD4655272CC67443A0E32865EB422745'; - TestClass: TCipher_1DES; - Mode: TCipherMode.cmCTSx), - (Input: '12345678'; - Output: ''; - OutputHex: '8EE274B893296F9E'; - TestClass: TCipher_1DES; - Mode: TCipherMode.cmCTSx), - (Input: 'ABCDEFGHIJKLMNOPQRSTUVWX'; - Output: ''; - OutputHex: 'FED41297FD52669BF5361295F3BD937EF0644802ED92DC21'; - TestClass: TCipher_1DES; - Mode: TCipherMode.cmCFBx), - (Input: '000000000000000000000000'; - Output: ''; - OutputHex: '8FA661E3882411E35337C15BAE99B7CBDD988AC4FABB3368'; - TestClass: TCipher_1DES; - Mode: TCipherMode.cmCFBx), - (Input: '12345678'; - Output: ''; - OutputHex: '8EA462E78D2216EB'; - TestClass: TCipher_1DES; - Mode: TCipherMode.cmCFBx)); - - /// - /// Carries out the actual encode test - /// - /// - /// Array with the data definint inputs and outputs for the tests - /// - /// - /// Cipher mode which shall be tested - /// - /// - /// if true parameter Mode will be ignored and tests for all modes be - /// carried out - /// - procedure DoTestEncode(Data: array of TTestEntry; Mode: TCipherMode; TestAllModes: Boolean = false); - /// - /// Carries out the actual decode test - /// - /// - /// Array with the data definint inputs and outputs for the tests - /// - /// - /// Cipher mode which shall be tested - /// - /// - /// if true parameter Mode will be ignored and tests for all modes be - /// carried out - /// - procedure DoTestDecode(Data: array of TTestEntry; Mode: TCipherMode; TestAllModes: Boolean = false); - published - procedure TestEncodeECBx; - procedure TestEncodeOFB8; - procedure TestEncodeCFB8; - procedure TestEncodeCFS8; - procedure TestEncodeCFBx; - procedure TestEncodeOFBx; - procedure TestEncodeCFSx; - procedure TestEncodeCBCx; - procedure TestEncodeCTSx; - procedure TestDecodeECBx; - procedure TestDecodeOFB8; - procedure TestDecodeCFB8; - procedure TestDecodeCFS8; - procedure TestDecodeCFBx; - procedure TestDecodeOFBx; - procedure TestDecodeCFSx; - procedure TestDecodeCBCx; - procedure TestDecodeCTSx; - procedure TestEncode; - procedure TestDecode; - end; - -implementation - -uses - DECUtil; - -procedure TestTDECCipherModes.DoTestEncode(Data: array of TTestEntry; Mode: TCipherMode; TestAllModes: Boolean = false); -var - Dest : TBytes; - Source : TBytes; - i, n : Integer; - Result : string; - - Cipher : TDECCipherModes; -begin - for i := Low(Data) to High(Data) do - begin - if not TestAllModes then - // Skip data for other modes - if Data[i].Mode <> Mode then - Continue; - - Cipher := Data[i].TestClass.Create; - Cipher.Mode := Data[i].Mode; - - try - Cipher.Init(BytesOf(RawByteString('ABCDEFGH')), BytesOf(Data[i].InitVector), $FF); - - SetLength(Source, Length(Data[i].Input)); - FillChar(Source[0], Length(Source), $FF); - - Move(Data[i].Input[1], Source[0], Length(Data[i].Input)); - - SetLength(Dest, length(Source)); - Cipher.Encode(Source[0], Dest[0], length(Source)); - - // Output is noted non hexadecimal - if Data[i].Output <> '' then - begin - for n := Low(Dest) to High(Dest) do - begin - CheckEquals(Ord(Data[i].Output[n+1]), Dest[n], - IntToStr(n+1) + '. position is wrong. ' + - IntToStr(i) + '. test series. Expected: ' + - string(Data[i].Output) + ' was: ' + string(DECUtil.BytesToRawString(Dest))); - end; - end - else - begin - // Output is noted in hex - Result := ''; - for n := Low(Dest) to High(Dest) do - Result := Result + IntToHex(Dest[n], 2); - - {$IF CompilerVersion >= 24.0} - for n := Low(Result) to High(Result) do - CheckEquals(char(Data[i].OutputHex[n]), Result[n], - IntToStr(n+1) + '. position is wrong. ' + - IntToStr(i) + '. test series. Expected: ' + - string(Data[i].OutputHex) + ' was: ' + Result); - {$ELSE} - for n := 1 to Length(Result) do - CheckEquals(char(Data[i].OutputHex[n]), Result[n], - IntToStr(n+1) + '. position is wrong. ' + - IntToStr(i) + '. test series. Expected: ' + - string(Data[i].OutputHex) + ' was: ' + Result); - {$IFEND} - end; - - finally - Cipher.Free; - end; - end; -end; - -procedure TestTDECCipherModes.DoTestDecode(Data: array of TTestEntry; Mode: TCipherMode; TestAllModes: Boolean = false); -var - Dest : TBytes; - Source : TBytes; - i, n, m : Integer; - - Cipher : TDECCipherModes; -begin - for i := Low(Data) to High(Data) do - begin - if not TestAllModes then - // Skip data for other modes - if Data[i].Mode <> Mode then - Continue; - - Cipher := Data[i].TestClass.Create; - Cipher.Mode := Data[i].Mode; - - try - Cipher.Init(BytesOf(RawByteString('ABCDEFGH')), BytesOf(Data[i].InitVector), $FF); - - if (Data[i].Output <> '') then - begin - SetLength(Source, Length(Data[i].Output)); - FillChar(Source[0], Length(Source), $FF); - - Move(Data[i].Output[1], Source[0], Length(Data[i].Output)); - end - else - begin - SetLength(Source, Length(Data[i].OutputHex) div 2); - FillChar(Source[0], Length(Source), $FF); - - n := 1; m := 0; - - repeat - Source[m] := StrToInt('$' + char(Data[i].OutputHex[n]) + char(Data[i].OutputHex[n +1])); - - inc(n, 2); - inc(m); - until (n > Length(Data[i].OutputHex)); - end; - - SetLength(Dest, length(Source)); - Cipher.Decode(Source[0], Dest[0], length(Source)); - - for n := Low(Dest) to High(Dest) do - begin - CheckEquals(Ord(Data[i].Input[n+1]), Dest[n], - IntToStr(n+1) + '. position is wrong. ' + - IntToStr(i) + '. test series. Expected: ' + - string(Data[i].Input) + ' was: ' + string(DECUtil.BytesToRawString(Dest))); - end; - - finally - Cipher.Free; - end; - end; -end; - -procedure TestTDECCipherModes.TestEncodeECBx; -begin - DoTestEncode(Data, TCipherMode.cmECBx); -end; - -procedure TestTDECCipherModes.TestEncodeOFB8; -begin - DoTestEncode(Data, TCipherMode.cmOFB8); -end; - -procedure TestTDECCipherModes.TestEncodeCFB8; -begin - DoTestEncode(Data, TCipherMode.cmCFB8); -end; - -procedure TestTDECCipherModes.TestEncodeCFS8; -begin - DoTestEncode(Data, TCipherMode.cmCFS8); -end; - -procedure TestTDECCipherModes.TestEncodeCFBx; -begin - DoTestEncode(Data, TCipherMode.cmCFBx); -end; - -procedure TestTDECCipherModes.TestEncodeOFBx; -begin - DoTestEncode(Data, TCipherMode.cmOFBx); -end; - -procedure TestTDECCipherModes.TestEncodeCFSx; -begin - DoTestEncode(Data, TCipherMode.cmCFSx); -end; - -procedure TestTDECCipherModes.TestEncodeCBCx; -begin - DoTestEncode(Data, TCipherMode.cmCBCx); -end; - -procedure TestTDECCipherModes.TestEncodeCTSx; -begin - DoTestEncode(Data, TCipherMode.cmCTSx); -end; - -procedure TestTDECCipherModes.TestDecodeECBx; -begin - DoTestDecode(Data, TCipherMode.cmECBx); -end; - -procedure TestTDECCipherModes.TestDecodeOFB8; -begin - DoTestDecode(Data, TCipherMode.cmOFB8); -end; - -procedure TestTDECCipherModes.TestDecodeCFB8; -begin - DoTestDecode(Data, TCipherMode.cmCFB8); -end; - -procedure TestTDECCipherModes.TestDecodeCFS8; -begin - DoTestDecode(Data, TCipherMode.cmCFS8); -end; - -procedure TestTDECCipherModes.TestDecodeCFBx; -begin - DoTestDecode(Data, TCipherMode.cmCFBx); -end; - -procedure TestTDECCipherModes.TestDecodeOFBx; -begin - DoTestDecode(Data, TCipherMode.cmOFBx); -end; - -procedure TestTDECCipherModes.TestDecodeCFSx; -begin - DoTestDecode(Data, TCipherMode.cmCFSx); -end; - -procedure TestTDECCipherModes.TestDecodeCBCx; -begin - DoTestDecode(Data, TCipherMode.cmCBCx); -end; - -procedure TestTDECCipherModes.TestDecodeCTSx; -begin - DoTestDecode(Data, TCipherMode.cmCTSx); -end; - -procedure TestTDECCipherModes.TestEncode; -begin - DoTestEncode(Data, TCipherMode.cmCTSx, true); -end; - -procedure TestTDECCipherModes.TestDecode; -begin - DoTestDecode(Data, TCipherMode.cmCTSx, true); -end; - -initialization - // Register all test cases to be run - {$IFDEF DUnitX} - TDUnitX.RegisterTestFixture(TestTDECCipherModes); - {$ELSE} - RegisterTest(TestTDECCipherModes.Suite); - {$ENDIF} -end.