dcpblockciphers.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  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. if (PQWord(@CV[0])^ = 0) then Inc(PQWord(@CV[8])^);
  431. end;
  432. class function TDCP_blockcipher128.GetBlockSize: integer;
  433. begin
  434. Result:= 128;
  435. end;
  436. procedure TDCP_blockcipher128.Init(const Key; Size: longword; InitVector: pointer);
  437. begin
  438. inherited Init(Key,Size,InitVector);
  439. InitKey(Key,Size);
  440. if InitVector= nil then
  441. begin
  442. FillChar(IV,16,{$IFDEF DCP1COMPAT}$FF{$ELSE}0{$ENDIF});
  443. EncryptECB(IV,IV);
  444. Reset;
  445. end
  446. else
  447. begin
  448. Move(InitVector^,IV,16);
  449. Reset;
  450. end;
  451. end;
  452. procedure TDCP_blockcipher128.SetIV(const Value);
  453. begin
  454. if not fInitialized then
  455. raise EDCP_blockcipher.Create('Cipher not initialized');
  456. Move(Value,IV,16);
  457. Reset;
  458. end;
  459. procedure TDCP_blockcipher128.GetIV(var Value);
  460. begin
  461. if not fInitialized then
  462. raise EDCP_blockcipher.Create('Cipher not initialized');
  463. Move(CV,Value,16);
  464. end;
  465. procedure TDCP_blockcipher128.Reset;
  466. begin
  467. if not fInitialized then
  468. raise EDCP_blockcipher.Create('Cipher not initialized')
  469. else
  470. Move(IV,CV,16);
  471. end;
  472. procedure TDCP_blockcipher128.Burn;
  473. begin
  474. FillChar(IV,16,$FF);
  475. FillChar(CV,16,$FF);
  476. inherited Burn;
  477. end;
  478. procedure TDCP_blockcipher128.EncryptCBC(const Indata; var Outdata; Size: longword);
  479. var
  480. i: longword;
  481. p1, p2: pointer;
  482. begin
  483. if not fInitialized then
  484. raise EDCP_blockcipher.Create('Cipher not initialized');
  485. p1:= @Indata;
  486. p2:= @Outdata;
  487. for i:= 1 to (Size div 16) do
  488. begin
  489. Move(p1^,p2^,16);
  490. XorBlock(p2^,CV,16);
  491. EncryptECB(p2^,p2^);
  492. Move(p2^,CV,16);
  493. p1:= pointer(p1 + 16);
  494. p2:= pointer(p2 + 16);
  495. end;
  496. if (Size mod 16)<> 0 then
  497. begin
  498. EncryptECB(CV,CV);
  499. Move(p1^,p2^,Size mod 16);
  500. XorBlock(p2^,CV,Size mod 16);
  501. end;
  502. end;
  503. procedure TDCP_blockcipher128.DecryptCBC(const Indata; var Outdata; Size: longword);
  504. var
  505. i: longword;
  506. p1, p2: pointer;
  507. Temp: array[0..15] of byte;
  508. begin
  509. if not fInitialized then
  510. raise EDCP_blockcipher.Create('Cipher not initialized');
  511. p1:= @Indata;
  512. p2:= @Outdata;
  513. dcpFillChar(Temp, SizeOf(Temp), 0);
  514. for i:= 1 to (Size div 16) do
  515. begin
  516. Move(p1^,p2^,16);
  517. Move(p1^,Temp,16);
  518. DecryptECB(p2^,p2^);
  519. XorBlock(p2^,CV,16);
  520. Move(Temp,CV,16);
  521. p1:= pointer(p1 + 16);
  522. p2:= pointer(p2 + 16);
  523. end;
  524. if (Size mod 16)<> 0 then
  525. begin
  526. EncryptECB(CV,CV);
  527. Move(p1^,p2^,Size mod 16);
  528. XorBlock(p2^,CV,Size mod 16);
  529. end;
  530. end;
  531. procedure TDCP_blockcipher128.EncryptCFB8bit(const Indata; var Outdata; Size: longword);
  532. var
  533. i: longword;
  534. p1, p2: Pbyte;
  535. Temp: array[0..15] of byte;
  536. begin
  537. if not fInitialized then
  538. raise EDCP_blockcipher.Create('Cipher not initialized');
  539. p1:= @Indata;
  540. p2:= @Outdata;
  541. dcpFillChar(Temp, SizeOf(Temp), 0);
  542. for i:= 1 to Size do
  543. begin
  544. EncryptECB(CV,Temp);
  545. p2^:= p1^ xor Temp[0];
  546. Move(CV[1],CV[0],15);
  547. CV[15]:= p2^;
  548. Inc(p1);
  549. Inc(p2);
  550. end;
  551. end;
  552. procedure TDCP_blockcipher128.DecryptCFB8bit(const Indata; var Outdata; Size: longword);
  553. var
  554. i: longword;
  555. p1, p2: Pbyte;
  556. TempByte: byte;
  557. Temp: array[0..15] of byte;
  558. begin
  559. if not fInitialized then
  560. raise EDCP_blockcipher.Create('Cipher not initialized');
  561. p1:= @Indata;
  562. p2:= @Outdata;
  563. dcpFillChar(Temp, SizeOf(Temp), 0);
  564. for i:= 1 to Size do
  565. begin
  566. TempByte:= p1^;
  567. EncryptECB(CV,Temp);
  568. p2^:= p1^ xor Temp[0];
  569. Move(CV[1],CV[0],15);
  570. CV[15]:= TempByte;
  571. Inc(p1);
  572. Inc(p2);
  573. end;
  574. end;
  575. procedure TDCP_blockcipher128.EncryptCFBblock(const Indata; var Outdata; Size: longword);
  576. var
  577. i: longword;
  578. p1, p2: Pbyte;
  579. begin
  580. if not fInitialized then
  581. raise EDCP_blockcipher.Create('Cipher not initialized');
  582. p1:= @Indata;
  583. p2:= @Outdata;
  584. for i:= 1 to (Size div 16) do
  585. begin
  586. EncryptECB(CV,CV);
  587. Move(p1^,p2^,16);
  588. XorBlock(p2^,CV,16);
  589. Move(p2^,CV,16);
  590. p1:= pointer(pointer(p1) + 16);
  591. p2:= pointer(pointer(p2) + 16);
  592. end;
  593. if (Size mod 16)<> 0 then
  594. begin
  595. EncryptECB(CV,CV);
  596. Move(p1^,p2^,Size mod 16);
  597. XorBlock(p2^,CV,Size mod 16);
  598. end;
  599. end;
  600. procedure TDCP_blockcipher128.DecryptCFBblock(const Indata; var Outdata; Size: longword);
  601. var
  602. i: longword;
  603. p1, p2: Pbyte;
  604. Temp: array[0..15] of byte;
  605. begin
  606. if not fInitialized then
  607. raise EDCP_blockcipher.Create('Cipher not initialized');
  608. p1:= @Indata;
  609. p2:= @Outdata;
  610. dcpFillChar(Temp, SizeOf(Temp), 0);
  611. for i:= 1 to (Size div 16) do
  612. begin
  613. Move(p1^,Temp,16);
  614. EncryptECB(CV,CV);
  615. Move(p1^,p2^,16);
  616. XorBlock(p2^,CV,16);
  617. Move(Temp,CV,16);
  618. p1:= pointer(pointer(p1) + 16);
  619. p2:= pointer(pointer(p2) + 16);
  620. end;
  621. if (Size mod 16)<> 0 then
  622. begin
  623. EncryptECB(CV,CV);
  624. Move(p1^,p2^,Size mod 16);
  625. XorBlock(p2^,CV,Size mod 16);
  626. end;
  627. end;
  628. procedure TDCP_blockcipher128.EncryptOFB(const Indata; var Outdata; Size: longword);
  629. var
  630. i: longword;
  631. p1, p2: pointer;
  632. begin
  633. if not fInitialized then
  634. raise EDCP_blockcipher.Create('Cipher not initialized');
  635. p1:= @Indata;
  636. p2:= @Outdata;
  637. for i:= 1 to (Size div 16) do
  638. begin
  639. EncryptECB(CV,CV);
  640. Move(p1^,p2^,16);
  641. XorBlock(p2^,CV,16);
  642. p1:= pointer(p1 + 16);
  643. p2:= pointer(p2 + 16);
  644. end;
  645. if (Size mod 16)<> 0 then
  646. begin
  647. EncryptECB(CV,CV);
  648. Move(p1^,p2^,Size mod 16);
  649. XorBlock(p2^,CV,Size mod 16);
  650. end;
  651. end;
  652. procedure TDCP_blockcipher128.DecryptOFB(const Indata; var Outdata; Size: longword);
  653. var
  654. i: longword;
  655. p1, p2: pointer;
  656. begin
  657. if not fInitialized then
  658. raise EDCP_blockcipher.Create('Cipher not initialized');
  659. p1:= @Indata;
  660. p2:= @Outdata;
  661. for i:= 1 to (Size div 16) do
  662. begin
  663. EncryptECB(CV,CV);
  664. Move(p1^,p2^,16);
  665. XorBlock(p2^,CV,16);
  666. p1:= pointer(p1 + 16);
  667. p2:= pointer(p2 + 16);
  668. end;
  669. if (Size mod 16)<> 0 then
  670. begin
  671. EncryptECB(CV,CV);
  672. Move(p1^,p2^,Size mod 16);
  673. XorBlock(p2^,CV,Size mod 16);
  674. end;
  675. end;
  676. procedure TDCP_blockcipher128.EncryptCTR(const Indata; var Outdata; Size: longword);
  677. var
  678. temp: array[0..15] of byte;
  679. i: longword;
  680. p1, p2: pointer;
  681. begin
  682. if not fInitialized then
  683. raise EDCP_blockcipher.Create('Cipher not initialized');
  684. p1:= @Indata;
  685. p2:= @Outdata;
  686. dcpFillChar(Temp, SizeOf(Temp), 0);
  687. for i:= 1 to (Size div 16) do
  688. begin
  689. EncryptECB(CV,temp);
  690. IncCounter;
  691. Move(p1^,p2^,16);
  692. XorBlock(p2^,temp,16);
  693. p1:= pointer(p1 + 16);
  694. p2:= pointer(p2 + 16);
  695. end;
  696. if (Size mod 16)<> 0 then
  697. begin
  698. EncryptECB(CV,temp);
  699. IncCounter;
  700. Move(p1^,p2^,Size mod 16);
  701. XorBlock(p2^,temp,Size mod 16);
  702. end;
  703. end;
  704. procedure TDCP_blockcipher128.DecryptCTR(const Indata; var Outdata; Size: longword);
  705. var
  706. temp: array[0..15] of byte;
  707. i: longword;
  708. p1, p2: pointer;
  709. begin
  710. if not fInitialized then
  711. raise EDCP_blockcipher.Create('Cipher not initialized');
  712. p1:= @Indata;
  713. p2:= @Outdata;
  714. dcpFillChar(Temp, SizeOf(Temp), 0);
  715. for i:= 1 to (Size div 16) do
  716. begin
  717. EncryptECB(CV,temp);
  718. IncCounter;
  719. Move(p1^,p2^,16);
  720. XorBlock(p2^,temp,16);
  721. p1:= pointer(p1 + 16);
  722. p2:= pointer(p2 + 16);
  723. end;
  724. if (Size mod 16)<> 0 then
  725. begin
  726. EncryptECB(CV,temp);
  727. IncCounter;
  728. Move(p1^,p2^,Size mod 16);
  729. XorBlock(p2^,temp,Size mod 16);
  730. end;
  731. end;
  732. end.