dcpcrypt2.pas 21 KB

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