| 1 |
{******************************************************************************} |
| 2 |
{* DCPcrypt v2.0 written by David Barton ([email protected]) **********} |
| 3 |
{******************************************************************************} |
| 4 |
{* Main component definitions *************************************************} |
| 5 |
{******************************************************************************} |
| 6 |
{* Copyright (C) 1999-2003 David Barton *} |
| 7 |
{* Copyright (C) 2018 Alexander Koblov ([email protected]) *} |
| 8 |
{* Permission is hereby granted, free of charge, to any person obtaining a *} |
| 9 |
{* copy of this software and associated documentation files (the "Software"), *} |
| 10 |
{* to deal in the Software without restriction, including without limitation *} |
| 11 |
{* the rights to use, copy, modify, merge, publish, distribute, sublicense, *} |
| 12 |
{* and/or sell copies of the Software, and to permit persons to whom the *} |
| 13 |
{* Software is furnished to do so, subject to the following conditions: *} |
| 14 |
{* *} |
| 15 |
{* The above copyright notice and this permission notice shall be included in *} |
| 16 |
{* all copies or substantial portions of the Software. *} |
| 17 |
{* *} |
| 18 |
{* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *} |
| 19 |
{* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *} |
| 20 |
{* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *} |
| 21 |
{* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *} |
| 22 |
{* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *} |
| 23 |
{* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *} |
| 24 |
{* DEALINGS IN THE SOFTWARE. *} |
| 25 |
{******************************************************************************} |
| 26 |
unit DCPcrypt2; |
| 27 |
|
| 28 |
{$MODE Delphi} |
| 29 |
|
| 30 |
interface |
| 31 |
uses |
| 32 |
Classes, Sysutils, DCPbase64; |
| 33 |
|
| 34 |
//{$DEFINE DCP1COMPAT} { DCPcrypt v1.31 compatiblity mode - see documentation } |
| 35 |
|
| 36 |
{******************************************************************************} |
| 37 |
{ A few predefined types to help out } |
| 38 |
|
| 39 |
type |
| 40 |
{$IFNDEF FPC} |
| 41 |
Pbyte= ^byte; |
| 42 |
Pword= ^word; |
| 43 |
Pdword= ^dword; |
| 44 |
Pint64= ^int64; |
| 45 |
dword= longword; |
| 46 |
Pwordarray= ^Twordarray; |
| 47 |
Twordarray= array[0..19383] of word; |
| 48 |
{$ENDIF} |
| 49 |
Pdwordarray= ^Tdwordarray; |
| 50 |
Tdwordarray= array[0..8191] of dword; |
| 51 |
|
| 52 |
|
| 53 |
{******************************************************************************} |
| 54 |
{ The base class from which all hash algorithms are to be derived } |
| 55 |
|
| 56 |
type |
| 57 |
EDCP_hash= class(Exception); |
| 58 |
TDCP_hash= class(TComponent) |
| 59 |
protected |
| 60 |
fInitialized: boolean; { Whether or not the algorithm has been initialized } |
| 61 |
|
| 62 |
procedure DeadInt(Value: integer); { Knudge to display vars in the object inspector } |
| 63 |
procedure DeadStr(Value: string); { Knudge to display vars in the object inspector } |
| 64 |
|
| 65 |
private |
| 66 |
function _GetId: integer; |
| 67 |
function _GetAlgorithm: string; |
| 68 |
function _GetHashSize: integer; |
| 69 |
|
| 70 |
public |
| 71 |
property Initialized: boolean |
| 72 |
read fInitialized; |
| 73 |
|
| 74 |
class function GetId: integer; virtual; |
| 75 |
{ Get the algorithm id } |
| 76 |
class function GetAlgorithm: string; virtual; |
| 77 |
{ Get the algorithm name } |
| 78 |
class function GetHashSize: integer; virtual; |
| 79 |
{ Get the size of the digest produced - in bits } |
| 80 |
class function SelfTest: boolean; virtual; |
| 81 |
{ Tests the implementation with several test vectors } |
| 82 |
|
| 83 |
procedure Init; virtual; |
| 84 |
{ Initialize the hash algorithm } |
| 85 |
procedure Final(var Digest); virtual; |
| 86 |
{ Create the final digest and clear the stored information. |
| 87 |
The size of the Digest var must be at least equal to the hash size } |
| 88 |
procedure Burn; virtual; |
| 89 |
{ Clear any stored information with out creating the final digest } |
| 90 |
|
| 91 |
procedure Update(const Buffer; Size: longword); virtual; |
| 92 |
{ Update the hash buffer with Size bytes of data from Buffer } |
| 93 |
procedure UpdateStream(Stream: TStream; Size: QWord); |
| 94 |
{ Update the hash buffer with Size bytes of data from the stream } |
| 95 |
procedure UpdateStr(const Str: string); |
| 96 |
{ Update the hash buffer with the string } |
| 97 |
|
| 98 |
destructor Destroy; override; |
| 99 |
|
| 100 |
published |
| 101 |
property Id: integer |
| 102 |
read _GetId write DeadInt; |
| 103 |
property Algorithm: string |
| 104 |
read _GetAlgorithm write DeadStr; |
| 105 |
property HashSize: integer |
| 106 |
read _GetHashSize write DeadInt; |
| 107 |
end; |
| 108 |
TDCP_hashclass= class of TDCP_hash; |
| 109 |
|
| 110 |
|
| 111 |
{******************************************************************************} |
| 112 |
{ The base class from which all encryption components will be derived. } |
| 113 |
{ Stream ciphers will be derived directly from this class where as } |
| 114 |
{ Block ciphers will have a further foundation class TDCP_blockcipher. } |
| 115 |
|
| 116 |
type |
| 117 |
EDCP_cipher= class(Exception); |
| 118 |
TDCP_cipher= class(TComponent) |
| 119 |
protected |
| 120 |
fInitialized: boolean; { Whether or not the key setup has been done yet } |
| 121 |
|
| 122 |
procedure DeadInt(Value: integer); { Knudge to display vars in the object inspector } |
| 123 |
procedure DeadStr(Value: string); { Knudge to display vars in the object inspector } |
| 124 |
|
| 125 |
private |
| 126 |
function _GetId: integer; |
| 127 |
function _GetAlgorithm: string; |
| 128 |
function _GetMaxKeySize: integer; |
| 129 |
|
| 130 |
public |
| 131 |
property Initialized: boolean |
| 132 |
read fInitialized; |
| 133 |
|
| 134 |
class function GetId: integer; virtual; |
| 135 |
{ Get the algorithm id } |
| 136 |
class function GetAlgorithm: string; virtual; |
| 137 |
{ Get the algorithm name } |
| 138 |
class function GetMaxKeySize: integer; virtual; |
| 139 |
{ Get the maximum key size (in bits) } |
| 140 |
class function SelfTest: boolean; virtual; |
| 141 |
{ Tests the implementation with several test vectors } |
| 142 |
|
| 143 |
procedure Init(const Key; Size: longword; InitVector: pointer); virtual; |
| 144 |
{ Do key setup based on the data in Key, size is in bits } |
| 145 |
procedure InitStr(const Key: string; HashType: TDCP_hashclass); |
| 146 |
{ Do key setup based on a hash of the key string } |
| 147 |
procedure Burn; virtual; |
| 148 |
{ Clear all stored key information } |
| 149 |
procedure Reset; virtual; |
| 150 |
{ Reset any stored chaining information } |
| 151 |
procedure Encrypt(const Indata; var Outdata; Size: longword); virtual; |
| 152 |
{ Encrypt size bytes of data and place in Outdata } |
| 153 |
procedure Decrypt(const Indata; var Outdata; Size: longword); virtual; |
| 154 |
{ Decrypt size bytes of data and place in Outdata } |
| 155 |
function EncryptStream(InStream, OutStream: TStream; Size: longword): longword; |
| 156 |
{ Encrypt size bytes of data from InStream and place in OutStream } |
| 157 |
function DecryptStream(InStream, OutStream: TStream; Size: longword): longword; |
| 158 |
{ Decrypt size bytes of data from InStream and place in OutStream } |
| 159 |
function EncryptString(const Str: string): string; virtual; |
| 160 |
{ Encrypt a string and return Base64 encoded } |
| 161 |
function DecryptString(const Str: string): string; virtual; |
| 162 |
{ Decrypt a Base64 encoded string } |
| 163 |
|
| 164 |
constructor Create(AOwner: TComponent); override; |
| 165 |
destructor Destroy; override; |
| 166 |
|
| 167 |
published |
| 168 |
property Id: integer |
| 169 |
read _GetId write DeadInt; |
| 170 |
property Algorithm: string |
| 171 |
read _GetAlgorithm write DeadStr; |
| 172 |
property MaxKeySize: integer |
| 173 |
read _GetMaxKeySize write DeadInt; |
| 174 |
end; |
| 175 |
TDCP_cipherclass= class of TDCP_cipher; |
| 176 |
|
| 177 |
|
| 178 |
{******************************************************************************} |
| 179 |
{ The base class from which all block ciphers are to be derived, this } |
| 180 |
{ extra class takes care of the different block encryption modes. } |
| 181 |
|
| 182 |
type |
| 183 |
TDCP_ciphermode= (cmCBC, cmCFB8bit, cmCFBblock, cmOFB, cmCTR); // cmCFB8bit is equal to DCPcrypt v1.xx's CFB mode |
| 184 |
EDCP_blockcipher= class(EDCP_cipher); |
| 185 |
TDCP_blockcipher= class(TDCP_cipher) |
| 186 |
protected |
| 187 |
fCipherMode: TDCP_ciphermode; { The cipher mode the encrypt method uses } |
| 188 |
|
| 189 |
procedure InitKey(const Key; Size: longword); virtual; |
| 190 |
|
| 191 |
private |
| 192 |
function _GetBlockSize: integer; |
| 193 |
|
| 194 |
public |
| 195 |
class function GetBlockSize: integer; virtual; |
| 196 |
{ Get the block size of the cipher (in bits) } |
| 197 |
|
| 198 |
procedure SetIV(const Value); virtual; |
| 199 |
{ Sets the IV to Value and performs a reset } |
| 200 |
procedure GetIV(var Value); virtual; |
| 201 |
{ Returns the current chaining information, not the actual IV } |
| 202 |
|
| 203 |
procedure Encrypt(const Indata; var Outdata; Size: longword); override; |
| 204 |
{ Encrypt size bytes of data and place in Outdata using CipherMode } |
| 205 |
procedure Decrypt(const Indata; var Outdata; Size: longword); override; |
| 206 |
{ Decrypt size bytes of data and place in Outdata using CipherMode } |
| 207 |
function EncryptString(const Str: string): string; override; |
| 208 |
{ Encrypt a string and return Base64 encoded } |
| 209 |
function DecryptString(const Str: string): string; override; |
| 210 |
{ Decrypt a Base64 encoded string } |
| 211 |
procedure EncryptECB(const Indata; var Outdata); virtual; |
| 212 |
{ Encrypt a block of data using the ECB method of encryption } |
| 213 |
procedure DecryptECB(const Indata; var Outdata); virtual; |
| 214 |
{ Decrypt a block of data using the ECB method of decryption } |
| 215 |
procedure EncryptCBC(const Indata; var Outdata; Size: longword); virtual; |
| 216 |
{ Encrypt size bytes of data using the CBC method of encryption } |
| 217 |
procedure DecryptCBC(const Indata; var Outdata; Size: longword); virtual; |
| 218 |
{ Decrypt size bytes of data using the CBC method of decryption } |
| 219 |
procedure EncryptCFB8bit(const Indata; var Outdata; Size: longword); virtual; |
| 220 |
{ Encrypt size bytes of data using the CFB (8 bit) method of encryption } |
| 221 |
procedure DecryptCFB8bit(const Indata; var Outdata; Size: longword); virtual; |
| 222 |
{ Decrypt size bytes of data using the CFB (8 bit) method of decryption } |
| 223 |
procedure EncryptCFBblock(const Indata; var Outdata; Size: longword); virtual; |
| 224 |
{ Encrypt size bytes of data using the CFB (block) method of encryption } |
| 225 |
procedure DecryptCFBblock(const Indata; var Outdata; Size: longword); virtual; |
| 226 |
{ Decrypt size bytes of data using the CFB (block) method of decryption } |
| 227 |
procedure EncryptOFB(const Indata; var Outdata; Size: longword); virtual; |
| 228 |
{ Encrypt size bytes of data using the OFB method of encryption } |
| 229 |
procedure DecryptOFB(const Indata; var Outdata; Size: longword); virtual; |
| 230 |
{ Decrypt size bytes of data using the OFB method of decryption } |
| 231 |
procedure EncryptCTR(const Indata; var Outdata; Size: longword); virtual; |
| 232 |
{ Encrypt size bytes of data using the CTR method of encryption } |
| 233 |
procedure DecryptCTR(const Indata; var Outdata; Size: longword); virtual; |
| 234 |
{ Decrypt size bytes of data using the CTR method of decryption } |
| 235 |
|
| 236 |
constructor Create(AOwner: TComponent); override; |
| 237 |
|
| 238 |
published |
| 239 |
property BlockSize: integer |
| 240 |
read _GetBlockSize write DeadInt; |
| 241 |
property CipherMode: TDCP_ciphermode |
| 242 |
read fCipherMode write fCipherMode default cmCBC; |
| 243 |
end; |
| 244 |
TDCP_blockcipherclass= class of TDCP_blockcipher; |
| 245 |
|
| 246 |
|
| 247 |
{******************************************************************************} |
| 248 |
{ Helper functions } |
| 249 |
|
| 250 |
procedure XorBlock(var InData1, InData2; Size: longword); |
| 251 |
// Supposed to be an optimized version of XorBlock() using 32-bit xor |
| 252 |
procedure XorBlockEx(var InData1, InData2; Size: longword); |
| 253 |
// removes the compiler hint due to first param being 'var' instead of 'out' |
| 254 |
procedure dcpFillChar(out x; count: SizeInt; Value: Byte); overload; |
| 255 |
procedure dcpFillChar(out x; count: SizeInt; Value: Char); overload; |
| 256 |
procedure ZeroMemory(Destination: Pointer; Length: PtrUInt); |
| 257 |
|
| 258 |
{$IF DEFINED(CPUX86_64)} |
| 259 |
|
| 260 |
function BMI2Support: LongBool; |
| 261 |
function SSSE3Support: LongBool; |
| 262 |
|
| 263 |
{$ENDIF} |
| 264 |
|
| 265 |
implementation |
| 266 |
|
| 267 |
{$Q-}{$R-} |
| 268 |
|
| 269 |
|
| 270 |
{** TDCP_hash *****************************************************************} |
| 271 |
|
| 272 |
procedure TDCP_hash.DeadInt(Value: integer); |
| 273 |
begin |
| 274 |
end; |
| 275 |
|
| 276 |
procedure TDCP_hash.DeadStr(Value: string); |
| 277 |
begin |
| 278 |
end; |
| 279 |
|
| 280 |
function TDCP_hash._GetId: integer; |
| 281 |
begin |
| 282 |
Result:= GetId; |
| 283 |
end; |
| 284 |
|
| 285 |
function TDCP_hash._GetAlgorithm: string; |
| 286 |
begin |
| 287 |
Result:= GetAlgorithm; |
| 288 |
end; |
| 289 |
|
| 290 |
function TDCP_hash._GetHashSize: integer; |
| 291 |
begin |
| 292 |
Result:= GetHashSize; |
| 293 |
end; |
| 294 |
|
| 295 |
class function TDCP_hash.GetId: integer; |
| 296 |
begin |
| 297 |
Result:= -1; |
| 298 |
end; |
| 299 |
|
| 300 |
class function TDCP_hash.GetAlgorithm: string; |
| 301 |
begin |
| 302 |
Result:= ''; |
| 303 |
end; |
| 304 |
|
| 305 |
class function TDCP_hash.GetHashSize: integer; |
| 306 |
begin |
| 307 |
Result:= -1; |
| 308 |
end; |
| 309 |
|
| 310 |
class function TDCP_hash.SelfTest: boolean; |
| 311 |
begin |
| 312 |
Result:= false; |
| 313 |
end; |
| 314 |
|
| 315 |
procedure TDCP_hash.Init; |
| 316 |
begin |
| 317 |
end; |
| 318 |
|
| 319 |
procedure TDCP_hash.Final(var Digest); |
| 320 |
begin |
| 321 |
end; |
| 322 |
|
| 323 |
procedure TDCP_hash.Burn; |
| 324 |
begin |
| 325 |
end; |
| 326 |
|
| 327 |
procedure TDCP_hash.Update(const Buffer; Size: longword); |
| 328 |
begin |
| 329 |
end; |
| 330 |
|
| 331 |
procedure TDCP_hash.UpdateStream(Stream: TStream; Size: QWord); |
| 332 |
var |
| 333 |
Buffer: array[0..8191] of byte; |
| 334 |
i, read: integer; |
| 335 |
begin |
| 336 |
dcpFillChar(Buffer, SizeOf(Buffer), 0); |
| 337 |
for i:= 1 to (Size div Sizeof(Buffer)) do |
| 338 |
begin |
| 339 |
read:= Stream.Read(Buffer,Sizeof(Buffer)); |
| 340 |
Update(Buffer,read); |
| 341 |
end; |
| 342 |
if (Size mod Sizeof(Buffer))<> 0 then |
| 343 |
begin |
| 344 |
read:= Stream.Read(Buffer,Size mod Sizeof(Buffer)); |
| 345 |
Update(Buffer,read); |
| 346 |
end; |
| 347 |
end; |
| 348 |
|
| 349 |
procedure TDCP_hash.UpdateStr(const Str: string); |
| 350 |
begin |
| 351 |
Update(Str[1],Length(Str)); |
| 352 |
end; |
| 353 |
|
| 354 |
destructor TDCP_hash.Destroy; |
| 355 |
begin |
| 356 |
if fInitialized then |
| 357 |
Burn; |
| 358 |
inherited Destroy; |
| 359 |
end; |
| 360 |
|
| 361 |
|
| 362 |
{** TDCP_cipher ***************************************************************} |
| 363 |
|
| 364 |
procedure TDCP_cipher.DeadInt(Value: integer); |
| 365 |
begin |
| 366 |
end; |
| 367 |
|
| 368 |
procedure TDCP_cipher.DeadStr(Value: string); |
| 369 |
begin |
| 370 |
end; |
| 371 |
|
| 372 |
function TDCP_cipher._GetId: integer; |
| 373 |
begin |
| 374 |
Result:= GetId; |
| 375 |
end; |
| 376 |
|
| 377 |
function TDCP_cipher._GetAlgorithm: string; |
| 378 |
begin |
| 379 |
Result:= GetAlgorithm; |
| 380 |
end; |
| 381 |
|
| 382 |
function TDCP_cipher._GetMaxKeySize: integer; |
| 383 |
begin |
| 384 |
Result:= GetMaxKeySize; |
| 385 |
end; |
| 386 |
|
| 387 |
class function TDCP_cipher.GetId: integer; |
| 388 |
begin |
| 389 |
Result:= -1; |
| 390 |
end; |
| 391 |
|
| 392 |
class function TDCP_cipher.GetAlgorithm: string; |
| 393 |
begin |
| 394 |
Result:= ''; |
| 395 |
end; |
| 396 |
|
| 397 |
class function TDCP_cipher.GetMaxKeySize: integer; |
| 398 |
begin |
| 399 |
Result:= -1; |
| 400 |
end; |
| 401 |
|
| 402 |
class function TDCP_cipher.SelfTest: boolean; |
| 403 |
begin |
| 404 |
Result:= false; |
| 405 |
end; |
| 406 |
|
| 407 |
procedure TDCP_cipher.Init(const Key; Size: longword; InitVector: pointer); |
| 408 |
begin |
| 409 |
if fInitialized then |
| 410 |
Burn; |
| 411 |
if (Size <= 0) or ((Size and 3)<> 0) or (Size> longword(GetMaxKeySize)) then |
| 412 |
raise EDCP_cipher.Create('Invalid key size') |
| 413 |
else |
| 414 |
fInitialized:= true; |
| 415 |
end; |
| 416 |
|
| 417 |
procedure TDCP_cipher.InitStr(const Key: string; HashType: TDCP_hashclass); |
| 418 |
var |
| 419 |
Hash: TDCP_hash; |
| 420 |
Digest: pointer; |
| 421 |
begin |
| 422 |
if fInitialized then |
| 423 |
Burn; |
| 424 |
try |
| 425 |
GetMem(Digest,HashType.GetHashSize div 8); |
| 426 |
Hash:= HashType.Create(Self); |
| 427 |
Hash.Init; |
| 428 |
Hash.UpdateStr(Key); |
| 429 |
Hash.Final(Digest^); |
| 430 |
Hash.Free; |
| 431 |
if MaxKeySize< HashType.GetHashSize then |
| 432 |
begin |
| 433 |
Init(Digest^,MaxKeySize,nil); |
| 434 |
end |
| 435 |
else |
| 436 |
begin |
| 437 |
Init(Digest^,HashType.GetHashSize,nil); |
| 438 |
end; |
| 439 |
FillChar(Digest^,HashType.GetHashSize div 8,$FF); |
| 440 |
FreeMem(Digest); |
| 441 |
except |
| 442 |
raise EDCP_cipher.Create('Unable to allocate sufficient memory for hash digest'); |
| 443 |
end; |
| 444 |
end; |
| 445 |
|
| 446 |
procedure TDCP_cipher.Burn; |
| 447 |
begin |
| 448 |
fInitialized:= false; |
| 449 |
end; |
| 450 |
|
| 451 |
procedure TDCP_cipher.Reset; |
| 452 |
begin |
| 453 |
end; |
| 454 |
|
| 455 |
procedure TDCP_cipher.Encrypt(const Indata; var Outdata; Size: longword); |
| 456 |
begin |
| 457 |
end; |
| 458 |
|
| 459 |
procedure TDCP_cipher.Decrypt(const Indata; var Outdata; Size: longword); |
| 460 |
begin |
| 461 |
end; |
| 462 |
|
| 463 |
function TDCP_cipher.EncryptStream(InStream, OutStream: TStream; Size: longword): longword; |
| 464 |
var |
| 465 |
Buffer: array[0..8191] of byte; |
| 466 |
i, Read: longword; |
| 467 |
begin |
| 468 |
dcpFillChar(Buffer, SizeOf(Buffer), 0); |
| 469 |
Result:= 0; |
| 470 |
for i:= 1 to (Size div Sizeof(Buffer)) do |
| 471 |
begin |
| 472 |
Read:= InStream.Read(Buffer,Sizeof(Buffer)); |
| 473 |
Inc(Result,Read); |
| 474 |
Encrypt(Buffer,Buffer,Read); |
| 475 |
OutStream.Write(Buffer,Read); |
| 476 |
end; |
| 477 |
if (Size mod Sizeof(Buffer))<> 0 then |
| 478 |
begin |
| 479 |
Read:= InStream.Read(Buffer,Size mod Sizeof(Buffer)); |
| 480 |
Inc(Result,Read); |
| 481 |
Encrypt(Buffer,Buffer,Read); |
| 482 |
OutStream.Write(Buffer,Read); |
| 483 |
end; |
| 484 |
end; |
| 485 |
|
| 486 |
function TDCP_cipher.DecryptStream(InStream, OutStream: TStream; Size: longword): longword; |
| 487 |
var |
| 488 |
Buffer: array[0..8191] of byte; |
| 489 |
i, Read: longword; |
| 490 |
begin |
| 491 |
dcpFillChar(Buffer, SizeOf(Buffer), 0); |
| 492 |
Result:= 0; |
| 493 |
for i:= 1 to (Size div Sizeof(Buffer)) do |
| 494 |
begin |
| 495 |
Read:= InStream.Read(Buffer,Sizeof(Buffer)); |
| 496 |
Inc(Result,Read); |
| 497 |
Decrypt(Buffer,Buffer,Read); |
| 498 |
OutStream.Write(Buffer,Read); |
| 499 |
end; |
| 500 |
if (Size mod Sizeof(Buffer))<> 0 then |
| 501 |
begin |
| 502 |
Read:= InStream.Read(Buffer,Size mod Sizeof(Buffer)); |
| 503 |
Inc(Result,Read); |
| 504 |
Decrypt(Buffer,Buffer,Read); |
| 505 |
OutStream.Write(Buffer,Read); |
| 506 |
end; |
| 507 |
end; |
| 508 |
|
| 509 |
function TDCP_cipher.EncryptString(const Str: string): string; |
| 510 |
begin |
| 511 |
SetLength(Result,Length(Str)); |
| 512 |
Encrypt(Str[1],Result[1],Length(Str)); |
| 513 |
Result:= Base64EncodeStr(Result); |
| 514 |
end; |
| 515 |
|
| 516 |
function TDCP_cipher.DecryptString(const Str: string): string; |
| 517 |
begin |
| 518 |
Result:= Base64DecodeStr(Str); |
| 519 |
Decrypt(Result[1],Result[1],Length(Result)); |
| 520 |
end; |
| 521 |
|
| 522 |
constructor TDCP_cipher.Create(AOwner: TComponent); |
| 523 |
begin |
| 524 |
inherited Create(AOwner); |
| 525 |
Burn; |
| 526 |
end; |
| 527 |
|
| 528 |
destructor TDCP_cipher.Destroy; |
| 529 |
begin |
| 530 |
if fInitialized then |
| 531 |
Burn; |
| 532 |
inherited Destroy; |
| 533 |
end; |
| 534 |
|
| 535 |
|
| 536 |
{** TDCP_blockcipher **********************************************************} |
| 537 |
|
| 538 |
procedure TDCP_blockcipher.InitKey(const Key; Size: longword); |
| 539 |
begin |
| 540 |
end; |
| 541 |
|
| 542 |
function TDCP_blockcipher._GetBlockSize: integer; |
| 543 |
begin |
| 544 |
Result:= GetBlockSize; |
| 545 |
end; |
| 546 |
|
| 547 |
class function TDCP_blockcipher.GetBlockSize: integer; |
| 548 |
begin |
| 549 |
Result:= -1; |
| 550 |
end; |
| 551 |
|
| 552 |
procedure TDCP_blockcipher.SetIV(const Value); |
| 553 |
begin |
| 554 |
end; |
| 555 |
|
| 556 |
procedure TDCP_blockcipher.GetIV(var Value); |
| 557 |
begin |
| 558 |
end; |
| 559 |
|
| 560 |
procedure TDCP_blockcipher.Encrypt(const Indata; var Outdata; Size: longword); |
| 561 |
begin |
| 562 |
case fCipherMode of |
| 563 |
cmCBC: EncryptCBC(Indata,Outdata,Size); |
| 564 |
cmCFB8bit: EncryptCFB8bit(Indata,Outdata,Size); |
| 565 |
cmCFBblock: EncryptCFBblock(Indata,Outdata,Size); |
| 566 |
cmOFB: EncryptOFB(Indata,Outdata,Size); |
| 567 |
cmCTR: EncryptCTR(Indata,Outdata,Size); |
| 568 |
end; |
| 569 |
end; |
| 570 |
|
| 571 |
function TDCP_blockcipher.EncryptString(const Str: string): string; |
| 572 |
begin |
| 573 |
SetLength(Result,Length(Str)); |
| 574 |
EncryptCFB8bit(Str[1],Result[1],Length(Str)); |
| 575 |
Result:= Base64EncodeStr(Result); |
| 576 |
end; |
| 577 |
|
| 578 |
function TDCP_blockcipher.DecryptString(const Str: string): string; |
| 579 |
begin |
| 580 |
Result:= Base64DecodeStr(Str); |
| 581 |
DecryptCFB8bit(Result[1],Result[1],Length(Result)); |
| 582 |
end; |
| 583 |
|
| 584 |
procedure TDCP_blockcipher.Decrypt(const Indata; var Outdata; Size: longword); |
| 585 |
begin |
| 586 |
case fCipherMode of |
| 587 |
cmCBC: DecryptCBC(Indata,Outdata,Size); |
| 588 |
cmCFB8bit: DecryptCFB8bit(Indata,Outdata,Size); |
| 589 |
cmCFBblock: DecryptCFBblock(Indata,Outdata,Size); |
| 590 |
cmOFB: DecryptOFB(Indata,Outdata,Size); |
| 591 |
cmCTR: DecryptCTR(Indata,Outdata,Size); |
| 592 |
end; |
| 593 |
end; |
| 594 |
|
| 595 |
procedure TDCP_blockcipher.EncryptECB(const Indata; var Outdata); |
| 596 |
begin |
| 597 |
end; |
| 598 |
|
| 599 |
procedure TDCP_blockcipher.DecryptECB(const Indata; var Outdata); |
| 600 |
begin |
| 601 |
end; |
| 602 |
|
| 603 |
procedure TDCP_blockcipher.EncryptCBC(const Indata; var Outdata; Size: longword); |
| 604 |
begin |
| 605 |
end; |
| 606 |
|
| 607 |
procedure TDCP_blockcipher.DecryptCBC(const Indata; var Outdata; Size: longword); |
| 608 |
begin |
| 609 |
end; |
| 610 |
|
| 611 |
procedure TDCP_blockcipher.EncryptCFB8bit(const Indata; var Outdata; Size: longword); |
| 612 |
begin |
| 613 |
end; |
| 614 |
|
| 615 |
procedure TDCP_blockcipher.DecryptCFB8bit(const Indata; var Outdata; Size: longword); |
| 616 |
begin |
| 617 |
end; |
| 618 |
|
| 619 |
procedure TDCP_blockcipher.EncryptCFBblock(const Indata; var Outdata; Size: longword); |
| 620 |
begin |
| 621 |
end; |
| 622 |
|
| 623 |
procedure TDCP_blockcipher.DecryptCFBblock(const Indata; var Outdata; Size: longword); |
| 624 |
begin |
| 625 |
end; |
| 626 |
|
| 627 |
procedure TDCP_blockcipher.EncryptOFB(const Indata; var Outdata; Size: longword); |
| 628 |
begin |
| 629 |
end; |
| 630 |
|
| 631 |
procedure TDCP_blockcipher.DecryptOFB(const Indata; var Outdata; Size: longword); |
| 632 |
begin |
| 633 |
end; |
| 634 |
|
| 635 |
procedure TDCP_blockcipher.EncryptCTR(const Indata; var Outdata; Size: longword); |
| 636 |
begin |
| 637 |
end; |
| 638 |
|
| 639 |
procedure TDCP_blockcipher.DecryptCTR(const Indata; var Outdata; Size: longword); |
| 640 |
begin |
| 641 |
end; |
| 642 |
|
| 643 |
constructor TDCP_blockcipher.Create(AOwner: TComponent); |
| 644 |
begin |
| 645 |
inherited Create(AOwner); |
| 646 |
fCipherMode:= cmCBC; |
| 647 |
end; |
| 648 |
|
| 649 |
|
| 650 |
{** Helpher functions *********************************************************} |
| 651 |
procedure XorBlock(var InData1, InData2; Size: longword); |
| 652 |
var |
| 653 |
b1: PByteArray; |
| 654 |
b2: PByteArray; |
| 655 |
i: longword; |
| 656 |
begin |
| 657 |
b1 := @InData1; |
| 658 |
b2 := @InData2; |
| 659 |
for i := 0 to size-1 do |
| 660 |
b1[i] := b1[i] xor b2[i]; |
| 661 |
end; |
| 662 |
|
| 663 |
procedure dcpFillChar(out x; count: SizeInt; Value: Byte); |
| 664 |
begin |
| 665 |
{$HINTS OFF} |
| 666 |
FillChar(x, count, value); |
| 667 |
{$HINTS ON} |
| 668 |
end; |
| 669 |
|
| 670 |
procedure ZeroMemory(Destination: Pointer; Length: PtrUInt); |
| 671 |
begin |
| 672 |
FillChar(Destination^, Length, 0); |
| 673 |
end; |
| 674 |
|
| 675 |
procedure dcpFillChar(out x; count: SizeInt; Value: Char); |
| 676 |
begin |
| 677 |
{$HINTS OFF} |
| 678 |
FillChar(x, count, Value); |
| 679 |
{$HINTS ON} |
| 680 |
end; |
| 681 |
|
| 682 |
// Supposed to be an optimized version of XorBlock() using 32-bit xor |
| 683 |
procedure XorBlockEx(var InData1, InData2; Size: longword); |
| 684 |
var |
| 685 |
l1: PIntegerArray; |
| 686 |
l2: PIntegerArray; |
| 687 |
b1: PByteArray; |
| 688 |
b2: PByteArray; |
| 689 |
i: integer; |
| 690 |
c: integer; |
| 691 |
begin |
| 692 |
l1 := @inData1; |
| 693 |
l2 := @inData2; |
| 694 |
for i := 0 to size div sizeof(LongWord)-1 do |
| 695 |
l1[i] := l1[i] xor l2[i]; |
| 696 |
|
| 697 |
// the rest of the buffer (3 bytes) |
| 698 |
c := size mod sizeof(longWord); |
| 699 |
if c > 0 then begin |
| 700 |
b1 := @InData1; |
| 701 |
b2 := @InData2; |
| 702 |
for i := (size-c) to size-1 do |
| 703 |
b1[i] := b1[i] xor b2[i]; |
| 704 |
end; |
| 705 |
end; |
| 706 |
|
| 707 |
{$IF DEFINED(CPUX86_64)} |
| 708 |
|
| 709 |
function BMI2Support: LongBool; assembler; nostackframe; |
| 710 |
asm |
| 711 |
pushq %rbx |
| 712 |
movl $7,%eax |
| 713 |
movl $0,%ecx |
| 714 |
cpuid |
| 715 |
andl $256,%ebx |
| 716 |
movl %ebx,%eax |
| 717 |
popq %rbx |
| 718 |
end; |
| 719 |
|
| 720 |
function SSSE3Support: LongBool; assembler; nostackframe; |
| 721 |
asm |
| 722 |
pushq %rbx |
| 723 |
movl $1,%eax |
| 724 |
cpuid |
| 725 |
andl $512,%ecx |
| 726 |
movl %ecx,%eax |
| 727 |
popq %rbx |
| 728 |
end; |
| 729 |
|
| 730 |
{$ENDIF} |
| 731 |
|
| 732 |
end. |
| 733 |
|