dcpblockciphers.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. {******************************************************************************}
  2. {* DCPcrypt v2.0 written by David Barton ([email protected]) **********}
  3. {******************************************************************************}
  4. {* Block cipher component definitions *****************************************}
  5. {******************************************************************************}
  6. {* Copyright (c) 1999-2002 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 DCPblockciphers;
  26. {$MODE Delphi}
  27. interface
  28. uses
  29. Classes, Sysutils, DCPcrypt2;
  30. {******************************************************************************}
  31. { Base type definition for 64 bit block ciphers }
  32. type
  33. TDCP_blockcipher64= class(TDCP_blockcipher)
  34. private
  35. IV, CV: array[0..7] of byte;
  36. procedure IncCounter;
  37. public
  38. class function GetBlockSize: integer; override;
  39. { Get the block size of the cipher (in bits) }
  40. procedure Reset; override;
  41. { Reset any stored chaining information }
  42. procedure Burn; override;
  43. { Clear all stored key information and chaining information }
  44. procedure SetIV(const Value); override;
  45. { Sets the IV to Value and performs a reset }
  46. procedure GetIV(var Value); override;
  47. { Returns the current chaining information, not the actual IV }
  48. procedure Init(const Key; Size: longword; InitVector: pointer); override;
  49. { Do key setup based on the data in Key, size is in bits }
  50. procedure EncryptCBC(const Indata; var Outdata; Size: longword); override;
  51. { Encrypt size bytes of data using the CBC method of encryption }
  52. procedure DecryptCBC(const Indata; var Outdata; Size: longword); override;
  53. { Decrypt size bytes of data using the CBC method of decryption }
  54. procedure EncryptCFB8bit(const Indata; var Outdata; Size: longword); override;
  55. { Encrypt size bytes of data using the CFB (8 bit) method of encryption }
  56. procedure DecryptCFB8bit(const Indata; var Outdata; Size: longword); override;
  57. { Decrypt size bytes of data using the CFB (8 bit) method of decryption }
  58. procedure EncryptCFBblock(const Indata; var Outdata; Size: longword); override;
  59. { Encrypt size bytes of data using the CFB (block) method of encryption }
  60. procedure DecryptCFBblock(const Indata; var Outdata; Size: longword); override;
  61. { Decrypt size bytes of data using the CFB (block) method of decryption }
  62. procedure EncryptOFB(const Indata; var Outdata; Size: longword); override;
  63. { Encrypt size bytes of data using the OFB method of encryption }
  64. procedure DecryptOFB(const Indata; var Outdata; Size: longword); override;
  65. { Decrypt size bytes of data using the OFB method of decryption }
  66. procedure EncryptCTR(const Indata; var Outdata; Size: longword); override;
  67. { Encrypt size bytes of data using the CTR method of encryption }
  68. procedure DecryptCTR(const Indata; var Outdata; Size: longword); override;
  69. { Decrypt size bytes of data using the CTR method of decryption }
  70. end;
  71. {******************************************************************************}
  72. { Base type definition for 128 bit block ciphers }
  73. type
  74. TDCP_blockcipher128= class(TDCP_blockcipher)
  75. private
  76. IV, CV: array[0..15] of byte;
  77. procedure IncCounter;
  78. public
  79. class function GetBlockSize: integer; override;
  80. { Get the block size of the cipher (in bits) }
  81. procedure Reset; override;
  82. { Reset any stored chaining information }
  83. procedure Burn; override;
  84. { Clear all stored key information and chaining information }
  85. procedure SetIV(const Value); override;
  86. { Sets the IV to Value and performs a reset }
  87. procedure GetIV(var Value); override;
  88. { Returns the current chaining information, not the actual IV }
  89. procedure Init(const Key; Size: longword; InitVector: pointer); override;
  90. { Do key setup based on the data in Key, size is in bits }
  91. procedure EncryptCBC(const Indata; var Outdata; Size: longword); override;
  92. { Encrypt size bytes of data using the CBC method of encryption }
  93. procedure DecryptCBC(const Indata; var Outdata; Size: longword); override;
  94. { Decrypt size bytes of data using the CBC method of decryption }
  95. procedure EncryptCFB8bit(const Indata; var Outdata; Size: longword); override;
  96. { Encrypt size bytes of data using the CFB (8 bit) method of encryption }
  97. procedure DecryptCFB8bit(const Indata; var Outdata; Size: longword); override;
  98. { Decrypt size bytes of data using the CFB (8 bit) method of decryption }
  99. procedure EncryptCFBblock(const Indata; var Outdata; Size: longword); override;
  100. { Encrypt size bytes of data using the CFB (block) method of encryption }
  101. procedure DecryptCFBblock(const Indata; var Outdata; Size: longword); override;
  102. { Decrypt size bytes of data using the CFB (block) method of decryption }
  103. procedure EncryptOFB(const Indata; var Outdata; Size: longword); override;
  104. { Encrypt size bytes of data using the OFB method of encryption }
  105. procedure DecryptOFB(const Indata; var Outdata; Size: longword); override;
  106. { Decrypt size bytes of data using the OFB method of decryption }
  107. procedure EncryptCTR(const Indata; var Outdata; Size: longword); override;
  108. { Encrypt size bytes of data using the CTR method of encryption }
  109. procedure DecryptCTR(const Indata; var Outdata; Size: longword); override;
  110. { Decrypt size bytes of data using the CTR method of decryption }
  111. end;
  112. implementation
  113. {** TDCP_blockcipher64 ********************************************************}
  114. procedure TDCP_blockcipher64.IncCounter;
  115. var
  116. i: integer;
  117. begin
  118. Inc(CV[7]);
  119. i:= 7;
  120. while (i> 0) and (CV[i] = 0) do
  121. begin
  122. Inc(CV[i-1]);
  123. Dec(i);
  124. end;
  125. end;
  126. class function TDCP_blockcipher64.GetBlockSize: integer;
  127. begin
  128. Result:= 64;
  129. end;
  130. procedure TDCP_blockcipher64.Init(const Key; Size: longword; InitVector: pointer);
  131. begin
  132. inherited Init(Key,Size,InitVector);
  133. InitKey(Key,Size);
  134. if InitVector= nil then
  135. begin
  136. FillChar(IV,8,{$IFDEF DCP1COMPAT}$FF{$ELSE}0{$ENDIF});
  137. EncryptECB(IV,IV);
  138. Reset;
  139. end
  140. else
  141. begin
  142. Move(InitVector^,IV,8);
  143. Reset;
  144. end;
  145. end;
  146. procedure TDCP_blockcipher64.SetIV(const Value);
  147. begin
  148. if not fInitialized then
  149. raise EDCP_blockcipher.Create('Cipher not initialized');
  150. Move(Value,IV,8);
  151. Reset;
  152. end;
  153. procedure TDCP_blockcipher64.GetIV(var Value);
  154. begin
  155. if not fInitialized then
  156. raise EDCP_blockcipher.Create('Cipher not initialized');
  157. Move(CV,Value,8);
  158. end;
  159. procedure TDCP_blockcipher64.Reset;
  160. begin
  161. if not fInitialized then
  162. raise EDCP_blockcipher.Create('Cipher not initialized')
  163. else
  164. Move(IV,CV,8);
  165. end;
  166. procedure TDCP_blockcipher64.Burn;
  167. begin
  168. FillChar(IV,8,$FF);
  169. FillChar(CV,8,$FF);
  170. inherited Burn;
  171. end;
  172. procedure TDCP_blockcipher64.EncryptCBC(const Indata; var Outdata; Size: longword);
  173. var
  174. i: longword;
  175. p1, p2: pointer;
  176. begin
  177. if not fInitialized then
  178. raise EDCP_blockcipher.Create('Cipher not initialized');
  179. p1:= @Indata;
  180. p2:= @Outdata;
  181. for i:= 1 to (Size div 8) do
  182. begin
  183. Move(p1^,p2^,8);
  184. XorBlock(p2^,CV,8);
  185. EncryptECB(p2^,p2^);
  186. Move(p2^,CV,8);
  187. p1:= pointer(p1 + 8);
  188. p2:= pointer(p2 + 8);
  189. end;
  190. if (Size mod 8)<> 0 then
  191. begin
  192. EncryptECB(CV,CV);
  193. Move(p1^,p2^,Size mod 8);
  194. XorBlock(p2^,CV,Size mod 8);
  195. end;
  196. end;
  197. procedure TDCP_blockcipher64.DecryptCBC(const Indata; var Outdata; Size: longword);
  198. var
  199. i: longword;
  200. p1, p2: pointer;
  201. Temp: array[0..7] of byte;
  202. begin
  203. if not fInitialized then
  204. raise EDCP_blockcipher.Create('Cipher not initialized');
  205. dcpFillChar(Temp, SizeOf(Temp), 0);
  206. p1:= @Indata;
  207. p2:= @Outdata;
  208. for i:= 1 to (Size div 8) do
  209. begin
  210. Move(p1^,p2^,8);
  211. Move(p1^,Temp,8);
  212. DecryptECB(p2^,p2^);
  213. XorBlock(p2^,CV,8);
  214. Move(Temp,CV,8);
  215. p1:= pointer(p1 + 8);
  216. p2:= pointer(p2 + 8);
  217. end;
  218. if (Size mod 8)<> 0 then
  219. begin
  220. EncryptECB(CV,CV);
  221. Move(p1^,p2^,Size mod 8);
  222. XorBlock(p2^,CV,Size mod 8);
  223. end;
  224. end;
  225. procedure TDCP_blockcipher64.EncryptCFB8bit(const Indata; var Outdata; Size: longword);
  226. var
  227. i: longword;
  228. p1, p2: Pbyte;
  229. Temp: array[0..7] of byte;
  230. begin
  231. if not fInitialized then
  232. raise EDCP_blockcipher.Create('Cipher not initialized');
  233. p1:= @Indata;
  234. p2:= @Outdata;
  235. dcpFillChar(Temp, SizeOf(Temp), 0);
  236. for i:= 1 to Size do
  237. begin
  238. EncryptECB(CV,Temp);
  239. p2^:= p1^ xor Temp[0];
  240. Move(CV[1],CV[0],8-1);
  241. CV[7]:= p2^;
  242. Inc(p1);
  243. Inc(p2);
  244. end;
  245. end;
  246. procedure TDCP_blockcipher64.DecryptCFB8bit(const Indata; var Outdata; Size: longword);
  247. var
  248. i: longword;
  249. p1, p2: Pbyte;
  250. TempByte: byte;
  251. Temp: array[0..7] of byte;
  252. begin
  253. if not fInitialized then
  254. raise EDCP_blockcipher.Create('Cipher not initialized');
  255. p1:= @Indata;
  256. p2:= @Outdata;
  257. dcpFillChar(Temp, SizeOf(Temp), 0);
  258. for i:= 1 to Size do
  259. begin
  260. TempByte:= p1^;
  261. EncryptECB(CV,Temp);
  262. p2^:= p1^ xor Temp[0];
  263. Move(CV[1],CV[0],8-1);
  264. CV[7]:= TempByte;
  265. Inc(p1);
  266. Inc(p2);
  267. end;
  268. end;
  269. procedure TDCP_blockcipher64.EncryptCFBblock(const Indata; var Outdata; Size: longword);
  270. var
  271. i: longword;
  272. p1, p2: Pbyte;
  273. begin
  274. if not fInitialized then
  275. raise EDCP_blockcipher.Create('Cipher not initialized');
  276. p1:= @Indata;
  277. p2:= @Outdata;
  278. for i:= 1 to (Size div 8) do
  279. begin
  280. EncryptECB(CV,CV);
  281. Move(p1^,p2^,8);
  282. XorBlock(p2^,CV,8);
  283. Move(p2^,CV,8);
  284. p1:= pointer(pointer(p1) + 8);
  285. p2:= pointer(pointer(p2) + 8);
  286. end;
  287. if (Size mod 8)<> 0 then
  288. begin
  289. EncryptECB(CV,CV);
  290. Move(p1^,p2^,Size mod 8);
  291. XorBlock(p2^,CV,Size mod 8);
  292. end;
  293. end;
  294. procedure TDCP_blockcipher64.DecryptCFBblock(const Indata; var Outdata; Size: longword);
  295. var
  296. i: longword;
  297. p1, p2: Pbyte;
  298. Temp: array[0..7] of byte;
  299. begin
  300. if not fInitialized then
  301. raise EDCP_blockcipher.Create('Cipher not initialized');
  302. p1:= @Indata;
  303. p2:= @Outdata;
  304. dcpFillChar(Temp, SizeOf(Temp), 0);
  305. for i:= 1 to (Size div 8) do
  306. begin
  307. Move(p1^,Temp,8);
  308. EncryptECB(CV,CV);
  309. Move(p1^,p2^,8);
  310. XorBlock(p2^,CV,8);
  311. Move(Temp,CV,8);
  312. p1:= pointer(pointer(p1) + 8);
  313. p2:= pointer(pointer(p2) + 8);
  314. end;
  315. if (Size mod 8)<> 0 then
  316. begin
  317. EncryptECB(CV,CV);
  318. Move(p1^,p2^,Size mod 8);
  319. XorBlock(p2^,CV,Size mod 8);
  320. end;
  321. end;
  322. procedure TDCP_blockcipher64.EncryptOFB(const Indata; var Outdata; Size: longword);
  323. var
  324. i: longword;
  325. p1, p2: pointer;
  326. begin
  327. if not fInitialized then
  328. raise EDCP_blockcipher.Create('Cipher not initialized');
  329. p1:= @Indata;
  330. p2:= @Outdata;
  331. for i:= 1 to (Size div 8) do
  332. begin
  333. EncryptECB(CV,CV);
  334. Move(p1^,p2^,8);
  335. XorBlock(p2^,CV,8);
  336. p1:= pointer(p1 + 8);
  337. p2:= pointer(p2 + 8);
  338. end;
  339. if (Size mod 8)<> 0 then
  340. begin
  341. EncryptECB(CV,CV);
  342. Move(p1^,p2^,Size mod 8);
  343. XorBlock(p2^,CV,Size mod 8);
  344. end;
  345. end;
  346. procedure TDCP_blockcipher64.DecryptOFB(const Indata; var Outdata; Size: longword);
  347. var
  348. i: longword;
  349. p1, p2: pointer;
  350. begin
  351. if not fInitialized then
  352. raise EDCP_blockcipher.Create('Cipher not initialized');
  353. p1:= @Indata;
  354. p2:= @Outdata;
  355. for i:= 1 to (Size div 8) do
  356. begin
  357. EncryptECB(CV,CV);
  358. Move(p1^,p2^,8);
  359. XorBlock(p2^,CV,8);
  360. p1:= pointer(p1 + 8);
  361. p2:= pointer(p2 + 8);
  362. end;
  363. if (Size mod 8)<> 0 then
  364. begin
  365. EncryptECB(CV,CV);
  366. Move(p1^,p2^,Size mod 8);
  367. XorBlock(p2^,CV,Size mod 8);
  368. end;
  369. end;
  370. procedure TDCP_blockcipher64.EncryptCTR(const Indata; var Outdata; Size: longword);
  371. var
  372. temp: array[0..7] of byte;
  373. i: longword;
  374. p1, p2: pointer;
  375. begin
  376. if not fInitialized then
  377. raise EDCP_blockcipher.Create('Cipher not initialized');
  378. p1:= @Indata;
  379. p2:= @Outdata;
  380. dcpFillChar(Temp, SizeOf(Temp), 0);
  381. for i:= 1 to (Size div 8) do
  382. begin
  383. EncryptECB(CV,temp);
  384. IncCounter;
  385. Move(p1^,p2^,8);
  386. XorBlock(p2^,temp,8);
  387. p1:= pointer(p1 + 8);
  388. p2:= pointer(p2 + 8);
  389. end;
  390. if (Size mod 8)<> 0 then
  391. begin
  392. EncryptECB(CV,temp);
  393. IncCounter;
  394. Move(p1^,p2^,Size mod 8);
  395. XorBlock(p2^,temp,Size mod 8);
  396. end;
  397. end;
  398. procedure TDCP_blockcipher64.DecryptCTR(const Indata; var Outdata; Size: longword);
  399. var
  400. temp: array[0..7] of byte;
  401. i: longword;
  402. p1, p2: pointer;
  403. begin
  404. if not fInitialized then
  405. raise EDCP_blockcipher.Create('Cipher not initialized');
  406. p1:= @Indata;
  407. p2:= @Outdata;
  408. dcpFillChar(Temp, SizeOf(Temp), 0);
  409. for i:= 1 to (Size div 8) do
  410. begin
  411. EncryptECB(CV,temp);
  412. IncCounter;
  413. Move(p1^,p2^,8);
  414. XorBlock(p2^,temp,8);
  415. p1:= pointer(p1 + 8);
  416. p2:= pointer(p2 + 8);
  417. end;
  418. if (Size mod 8)<> 0 then
  419. begin
  420. EncryptECB(CV,temp);
  421. IncCounter;
  422. Move(p1^,p2^,Size mod 8);
  423. XorBlock(p2^,temp,Size mod 8);
  424. end;
  425. end;
  426. {** TDCP_blockcipher128 ********************************************************}
  427. procedure TDCP_blockcipher128.IncCounter;
  428. begin
  429. Inc(PQWord(@CV[0])^);
  430. end;
  431. class function TDCP_blockcipher128.GetBlockSize: integer;
  432. begin
  433. Result:= 128;
  434. end;
  435. procedure TDCP_blockcipher128.Init(const Key; Size: longword; InitVector: pointer);
  436. begin
  437. inherited Init(Key,Size,InitVector);
  438. InitKey(Key,Size);
  439. if InitVector= nil then
  440. begin
  441. FillChar(IV,16,{$IFDEF DCP1COMPAT}$FF{$ELSE}0{$ENDIF});
  442. EncryptECB(IV,IV);
  443. Reset;
  444. end
  445. else
  446. begin
  447. Move(InitVector^,IV,16);
  448. Reset;
  449. end;
  450. end;
  451. procedure TDCP_blockcipher128.SetIV(const Value);
  452. begin
  453. if not fInitialized then
  454. raise EDCP_blockcipher.Create('Cipher not initialized');
  455. Move(Value,IV,16);
  456. Reset;
  457. end;
  458. procedure TDCP_blockcipher128.GetIV(var Value);
  459. begin
  460. if not fInitialized then
  461. raise EDCP_blockcipher.Create('Cipher not initialized');
  462. Move(CV,Value,16);
  463. end;
  464. procedure TDCP_blockcipher128.Reset;
  465. begin
  466. if not fInitialized then
  467. raise EDCP_blockcipher.Create('Cipher not initialized')
  468. else
  469. Move(IV,CV,16);
  470. end;
  471. procedure TDCP_blockcipher128.Burn;
  472. begin
  473. FillChar(IV,16,$FF);
  474. FillChar(CV,16,$FF);
  475. inherited Burn;
  476. end;
  477. procedure TDCP_blockcipher128.EncryptCBC(const Indata; var Outdata; Size: longword);
  478. var
  479. i: longword;
  480. p1, p2: pointer;
  481. begin
  482. if not fInitialized then
  483. raise EDCP_blockcipher.Create('Cipher not initialized');
  484. p1:= @Indata;
  485. p2:= @Outdata;
  486. for i:= 1 to (Size div 16) do
  487. begin
  488. Move(p1^,p2^,16);
  489. XorBlock(p2^,CV,16);
  490. EncryptECB(p2^,p2^);
  491. Move(p2^,CV,16);
  492. p1:= pointer(p1 + 16);
  493. p2:= pointer(p2 + 16);
  494. end;
  495. if (Size mod 16)<> 0 then
  496. begin
  497. EncryptECB(CV,CV);
  498. Move(p1^,p2^,Size mod 16);
  499. XorBlock(p2^,CV,Size mod 16);
  500. end;
  501. end;
  502. procedure TDCP_blockcipher128.DecryptCBC(const Indata; var Outdata; Size: longword);
  503. var
  504. i: longword;
  505. p1, p2: pointer;
  506. Temp: array[0..15] of byte;
  507. begin
  508. if not fInitialized then
  509. raise EDCP_blockcipher.Create('Cipher not initialized');
  510. p1:= @Indata;
  511. p2:= @Outdata;
  512. dcpFillChar(Temp, SizeOf(Temp), 0);
  513. for i:= 1 to (Size div 16) do
  514. begin
  515. Move(p1^,p2^,16);
  516. Move(p1^,Temp,16);
  517. DecryptECB(p2^,p2^);
  518. XorBlock(p2^,CV,16);
  519. Move(Temp,CV,16);
  520. p1:= pointer(p1 + 16);
  521. p2:= pointer(p2 + 16);
  522. end;
  523. if (Size mod 16)<> 0 then
  524. begin
  525. EncryptECB(CV,CV);
  526. Move(p1^,p2^,Size mod 16);
  527. XorBlock(p2^,CV,Size mod 16);
  528. end;
  529. end;
  530. procedure TDCP_blockcipher128.EncryptCFB8bit(const Indata; var Outdata; Size: longword);
  531. var
  532. i: longword;
  533. p1, p2: Pbyte;
  534. Temp: array[0..15] of byte;
  535. begin
  536. if not fInitialized then
  537. raise EDCP_blockcipher.Create('Cipher not initialized');
  538. p1:= @Indata;
  539. p2:= @Outdata;
  540. dcpFillChar(Temp, SizeOf(Temp), 0);
  541. for i:= 1 to Size do
  542. begin
  543. EncryptECB(CV,Temp);
  544. p2^:= p1^ xor Temp[0];
  545. Move(CV[1],CV[0],15);
  546. CV[15]:= p2^;
  547. Inc(p1);
  548. Inc(p2);
  549. end;
  550. end;
  551. procedure TDCP_blockcipher128.DecryptCFB8bit(const Indata; var Outdata; Size: longword);
  552. var
  553. i: longword;
  554. p1, p2: Pbyte;
  555. TempByte: byte;
  556. Temp: array[0..15] of byte;
  557. begin
  558. if not fInitialized then
  559. raise EDCP_blockcipher.Create('Cipher not initialized');
  560. p1:= @Indata;
  561. p2:= @Outdata;
  562. dcpFillChar(Temp, SizeOf(Temp), 0);
  563. for i:= 1 to Size do
  564. begin
  565. TempByte:= p1^;
  566. EncryptECB(CV,Temp);
  567. p2^:= p1^ xor Temp[0];
  568. Move(CV[1],CV[0],15);
  569. CV[15]:= TempByte;
  570. Inc(p1);
  571. Inc(p2);
  572. end;
  573. end;
  574. procedure TDCP_blockcipher128.EncryptCFBblock(const Indata; var Outdata; Size: longword);
  575. var
  576. i: longword;
  577. p1, p2: Pbyte;
  578. begin
  579. if not fInitialized then
  580. raise EDCP_blockcipher.Create('Cipher not initialized');
  581. p1:= @Indata;
  582. p2:= @Outdata;
  583. for i:= 1 to (Size div 16) do
  584. begin
  585. EncryptECB(CV,CV);
  586. Move(p1^,p2^,16);
  587. XorBlock(p2^,CV,16);
  588. Move(p2^,CV,16);
  589. p1:= pointer(pointer(p1) + 16);
  590. p2:= pointer(pointer(p2) + 16);
  591. end;
  592. if (Size mod 16)<> 0 then
  593. begin
  594. EncryptECB(CV,CV);
  595. Move(p1^,p2^,Size mod 16);
  596. XorBlock(p2^,CV,Size mod 16);
  597. end;
  598. end;
  599. procedure TDCP_blockcipher128.DecryptCFBblock(const Indata; var Outdata; Size: longword);
  600. var
  601. i: longword;
  602. p1, p2: Pbyte;
  603. Temp: array[0..15] of byte;
  604. begin
  605. if not fInitialized then
  606. raise EDCP_blockcipher.Create('Cipher not initialized');
  607. p1:= @Indata;
  608. p2:= @Outdata;
  609. dcpFillChar(Temp, SizeOf(Temp), 0);
  610. for i:= 1 to (Size div 16) do
  611. begin
  612. Move(p1^,Temp,16);
  613. EncryptECB(CV,CV);
  614. Move(p1^,p2^,16);
  615. XorBlock(p2^,CV,16);
  616. Move(Temp,CV,16);
  617. p1:= pointer(pointer(p1) + 16);
  618. p2:= pointer(pointer(p2) + 16);
  619. end;
  620. if (Size mod 16)<> 0 then
  621. begin
  622. EncryptECB(CV,CV);
  623. Move(p1^,p2^,Size mod 16);
  624. XorBlock(p2^,CV,Size mod 16);
  625. end;
  626. end;
  627. procedure TDCP_blockcipher128.EncryptOFB(const Indata; var Outdata; Size: longword);
  628. var
  629. i: longword;
  630. p1, p2: pointer;
  631. begin
  632. if not fInitialized then
  633. raise EDCP_blockcipher.Create('Cipher not initialized');
  634. p1:= @Indata;
  635. p2:= @Outdata;
  636. for i:= 1 to (Size div 16) do
  637. begin
  638. EncryptECB(CV,CV);
  639. Move(p1^,p2^,16);
  640. XorBlock(p2^,CV,16);
  641. p1:= pointer(p1 + 16);
  642. p2:= pointer(p2 + 16);
  643. end;
  644. if (Size mod 16)<> 0 then
  645. begin
  646. EncryptECB(CV,CV);
  647. Move(p1^,p2^,Size mod 16);
  648. XorBlock(p2^,CV,Size mod 16);
  649. end;
  650. end;
  651. procedure TDCP_blockcipher128.DecryptOFB(const Indata; var Outdata; Size: longword);
  652. var
  653. i: longword;
  654. p1, p2: pointer;
  655. begin
  656. if not fInitialized then
  657. raise EDCP_blockcipher.Create('Cipher not initialized');
  658. p1:= @Indata;
  659. p2:= @Outdata;
  660. for i:= 1 to (Size div 16) do
  661. begin
  662. EncryptECB(CV,CV);
  663. Move(p1^,p2^,16);
  664. XorBlock(p2^,CV,16);
  665. p1:= pointer(p1 + 16);
  666. p2:= pointer(p2 + 16);
  667. end;
  668. if (Size mod 16)<> 0 then
  669. begin
  670. EncryptECB(CV,CV);
  671. Move(p1^,p2^,Size mod 16);
  672. XorBlock(p2^,CV,Size mod 16);
  673. end;
  674. end;
  675. procedure TDCP_blockcipher128.EncryptCTR(const Indata; var Outdata; Size: longword);
  676. var
  677. temp: array[0..15] of byte;
  678. i: longword;
  679. p1, p2: pointer;
  680. begin
  681. if not fInitialized then
  682. raise EDCP_blockcipher.Create('Cipher not initialized');
  683. p1:= @Indata;
  684. p2:= @Outdata;
  685. dcpFillChar(Temp, SizeOf(Temp), 0);
  686. for i:= 1 to (Size div 16) do
  687. begin
  688. EncryptECB(CV,temp);
  689. IncCounter;
  690. Move(p1^,p2^,16);
  691. XorBlock(p2^,temp,16);
  692. p1:= pointer(p1 + 16);
  693. p2:= pointer(p2 + 16);
  694. end;
  695. if (Size mod 16)<> 0 then
  696. begin
  697. EncryptECB(CV,temp);
  698. IncCounter;
  699. Move(p1^,p2^,Size mod 16);
  700. XorBlock(p2^,temp,Size mod 16);
  701. end;
  702. end;
  703. procedure TDCP_blockcipher128.DecryptCTR(const Indata; var Outdata; Size: longword);
  704. var
  705. temp: array[0..15] of byte;
  706. i: longword;
  707. p1, p2: pointer;
  708. begin
  709. if not fInitialized then
  710. raise EDCP_blockcipher.Create('Cipher not initialized');
  711. p1:= @Indata;
  712. p2:= @Outdata;
  713. dcpFillChar(Temp, SizeOf(Temp), 0);
  714. for i:= 1 to (Size div 16) do
  715. begin
  716. EncryptECB(CV,temp);
  717. IncCounter;
  718. Move(p1^,p2^,16);
  719. XorBlock(p2^,temp,16);
  720. p1:= pointer(p1 + 16);
  721. p2:= pointer(p2 + 16);
  722. end;
  723. if (Size mod 16)<> 0 then
  724. begin
  725. EncryptECB(CV,temp);
  726. IncCounter;
  727. Move(p1^,p2^,Size mod 16);
  728. XorBlock(p2^,temp,Size mod 16);
  729. end;
  730. end;
  731. end.