DCPdes.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. {******************************************************************************}
  2. {* DCPcrypt v2.0 written by David Barton (crypto@cityinthesky.co.uk) **********}
  3. {******************************************************************************}
  4. {* A binary compatible implementation of DES and Triple DES *******************}
  5. {* Based on C source code by Eric Young ***************************************}
  6. {******************************************************************************}
  7. {* Copyright (c) 1999-2002 David Barton *}
  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. {******************************************************************************}
  27. {* This implementation of DES is based on the C implementation by *}
  28. {* Eric Young (eay@mincom.oz.au) *}
  29. {******************************************************************************}
  30. {* DES takes a 64bit key and discards every 8th bit (56bit effectively) *}
  31. {* 3DES takes either a <= 128bit key and uses one key twice or takes a *}
  32. {* <= 192bit key and uses each once (again discarding every 8th bit) *}
  33. {******************************************************************************}
  34. unit DCPdes;
  35. interface
  36. uses
  37. Classes, Sysutils, DCPcrypt2, DCPconst, DCPblockciphers;
  38. type
  39. TDCP_customdes= class(TDCP_blockcipher64)
  40. protected
  41. procedure DoInit(KeyB: PByteArray; KeyData: PDWordArray);
  42. procedure EncryptBlock(const InData; var OutData; KeyData: PDWordArray);
  43. procedure DecryptBlock(const InData; var OutData; KeyData: PDWordArray);
  44. end;
  45. type
  46. TDCP_des= class(TDCP_customdes)
  47. protected
  48. KeyData: array[0..31] of dword;
  49. procedure InitKey(const Key; Size: longword); override;
  50. public
  51. class function GetId: integer; override;
  52. class function GetAlgorithm: string; override;
  53. class function GetMaxKeySize: integer; override;
  54. class function SelfTest: boolean; override;
  55. procedure Burn; override;
  56. procedure EncryptECB(const InData; var OutData); override;
  57. procedure DecryptECB(const InData; var OutData); override;
  58. end;
  59. TDCP_3des= class(TDCP_customdes)
  60. protected
  61. KeyData: array[0..2,0..31] of dword;
  62. procedure InitKey(const Key; Size: longword); override;
  63. public
  64. class function GetId: integer; override;
  65. class function GetAlgorithm: string; override;
  66. class function GetMaxKeySize: integer; override;
  67. class function SelfTest: boolean; override;
  68. procedure Burn; override;
  69. procedure EncryptECB(const InData; var OutData); override;
  70. procedure DecryptECB(const InData; var OutData); override;
  71. end;
  72. {******************************************************************************}
  73. {******************************************************************************}
  74. implementation
  75. {$R-}{$Q-}
  76. {$I DCPdes.inc}
  77. procedure hperm_op(var a, t: dword; n, m: dword);
  78. begin
  79. t:= ((a shl (16 - n)) xor a) and m;
  80. a:= a xor t xor (t shr (16 - n));
  81. end;
  82. procedure perm_op(var a, b, t: dword; n, m: dword);
  83. begin
  84. t:= ((a shr n) xor b) and m;
  85. b:= b xor t;
  86. a:= a xor (t shl n);
  87. end;
  88. procedure TDCP_customdes.DoInit(KeyB: PByteArray; KeyData: PDwordArray);
  89. var
  90. c, d, t, s, t2, i: dword;
  91. begin
  92. c:= KeyB^[0] or (KeyB^[1] shl 8) or (KeyB^[2] shl 16) or (KeyB^[3] shl 24);
  93. d:= KeyB^[4] or (KeyB^[5] shl 8) or (KeyB^[6] shl 16) or (KeyB^[7] shl 24);
  94. perm_op(d,c,t,4,$0f0f0f0f);
  95. hperm_op(c,t,dword(-2),$cccc0000);
  96. hperm_op(d,t,dword(-2),$cccc0000);
  97. perm_op(d,c,t,1,$55555555);
  98. perm_op(c,d,t,8,$00ff00ff);
  99. perm_op(d,c,t,1,$55555555);
  100. d:= ((d and $ff) shl 16) or (d and $ff00) or ((d and $ff0000) shr 16) or
  101. ((c and $f0000000) shr 4);
  102. c:= c and $fffffff;
  103. for i:= 0 to 15 do
  104. begin
  105. if shifts2[i]<> 0 then
  106. begin
  107. c:= ((c shr 2) or (c shl 26));
  108. d:= ((d shr 2) or (d shl 26));
  109. end
  110. else
  111. begin
  112. c:= ((c shr 1) or (c shl 27));
  113. d:= ((d shr 1) or (d shl 27));
  114. end;
  115. c:= c and $fffffff;
  116. d:= d and $fffffff;
  117. s:= des_skb[0,c and $3f] or
  118. des_skb[1,((c shr 6) and $03) or ((c shr 7) and $3c)] or
  119. des_skb[2,((c shr 13) and $0f) or ((c shr 14) and $30)] or
  120. des_skb[3,((c shr 20) and $01) or ((c shr 21) and $06) or ((c shr 22) and $38)];
  121. t:= des_skb[4,d and $3f] or
  122. des_skb[5,((d shr 7) and $03) or ((d shr 8) and $3c)] or
  123. des_skb[6, (d shr 15) and $3f ] or
  124. des_skb[7,((d shr 21) and $0f) or ((d shr 22) and $30)];
  125. t2:= ((t shl 16) or (s and $ffff));
  126. KeyData^[(i shl 1)]:= ((t2 shl 2) or (t2 shr 30));
  127. t2:= ((s shr 16) or (t and $ffff0000));
  128. KeyData^[(i shl 1)+1]:= ((t2 shl 6) or (t2 shr 26));
  129. end;
  130. end;
  131. procedure TDCP_customdes.EncryptBlock(const InData; var OutData; KeyData: PDWordArray);
  132. var
  133. l, r, t, u: dword;
  134. i: longint;
  135. begin
  136. r:= PDword(@InData)^;
  137. l:= PDword(dword(@InData)+4)^;
  138. t:= ((l shr 4) xor r) and $0f0f0f0f;
  139. r:= r xor t;
  140. l:= l xor (t shl 4);
  141. t:= ((r shr 16) xor l) and $0000ffff;
  142. l:= l xor t;
  143. r:= r xor (t shl 16);
  144. t:= ((l shr 2) xor r) and $33333333;
  145. r:= r xor t;
  146. l:= l xor (t shl 2);
  147. t:= ((r shr 8) xor l) and $00ff00ff;
  148. l:= l xor t;
  149. r:= r xor (t shl 8);
  150. t:= ((l shr 1) xor r) and $55555555;
  151. r:= r xor t;
  152. l:= l xor (t shl 1);
  153. r:= (r shr 29) or (r shl 3);
  154. l:= (l shr 29) or (l shl 3);
  155. i:= 0;
  156. while i< 32 do
  157. begin
  158. u:= r xor KeyData^[i ];
  159. t:= r xor KeyData^[i+1];
  160. t:= (t shr 4) or (t shl 28);
  161. l:= l xor des_SPtrans[0,(u shr 2) and $3f] xor
  162. des_SPtrans[2,(u shr 10) and $3f] xor
  163. des_SPtrans[4,(u shr 18) and $3f] xor
  164. des_SPtrans[6,(u shr 26) and $3f] xor
  165. des_SPtrans[1,(t shr 2) and $3f] xor
  166. des_SPtrans[3,(t shr 10) and $3f] xor
  167. des_SPtrans[5,(t shr 18) and $3f] xor
  168. des_SPtrans[7,(t shr 26) and $3f];
  169. u:= l xor KeyData^[i+2];
  170. t:= l xor KeyData^[i+3];
  171. t:= (t shr 4) or (t shl 28);
  172. r:= r xor des_SPtrans[0,(u shr 2) and $3f] xor
  173. des_SPtrans[2,(u shr 10) and $3f] xor
  174. des_SPtrans[4,(u shr 18) and $3f] xor
  175. des_SPtrans[6,(u shr 26) and $3f] xor
  176. des_SPtrans[1,(t shr 2) and $3f] xor
  177. des_SPtrans[3,(t shr 10) and $3f] xor
  178. des_SPtrans[5,(t shr 18) and $3f] xor
  179. des_SPtrans[7,(t shr 26) and $3f];
  180. u:= r xor KeyData^[i+4];
  181. t:= r xor KeyData^[i+5];
  182. t:= (t shr 4) or (t shl 28);
  183. l:= l xor des_SPtrans[0,(u shr 2) and $3f] xor
  184. des_SPtrans[2,(u shr 10) and $3f] xor
  185. des_SPtrans[4,(u shr 18) and $3f] xor
  186. des_SPtrans[6,(u shr 26) and $3f] xor
  187. des_SPtrans[1,(t shr 2) and $3f] xor
  188. des_SPtrans[3,(t shr 10) and $3f] xor
  189. des_SPtrans[5,(t shr 18) and $3f] xor
  190. des_SPtrans[7,(t shr 26) and $3f];
  191. u:= l xor KeyData^[i+6];
  192. t:= l xor KeyData^[i+7];
  193. t:= (t shr 4) or (t shl 28);
  194. r:= r xor des_SPtrans[0,(u shr 2) and $3f] xor
  195. des_SPtrans[2,(u shr 10) and $3f] xor
  196. des_SPtrans[4,(u shr 18) and $3f] xor
  197. des_SPtrans[6,(u shr 26) and $3f] xor
  198. des_SPtrans[1,(t shr 2) and $3f] xor
  199. des_SPtrans[3,(t shr 10) and $3f] xor
  200. des_SPtrans[5,(t shr 18) and $3f] xor
  201. des_SPtrans[7,(t shr 26) and $3f];
  202. Inc(i,8);
  203. end;
  204. r:= (r shr 3) or (r shl 29);
  205. l:= (l shr 3) or (l shl 29);
  206. t:= ((r shr 1) xor l) and $55555555;
  207. l:= l xor t;
  208. r:= r xor (t shl 1);
  209. t:= ((l shr 8) xor r) and $00ff00ff;
  210. r:= r xor t;
  211. l:= l xor (t shl 8);
  212. t:= ((r shr 2) xor l) and $33333333;
  213. l:= l xor t;
  214. r:= r xor (t shl 2);
  215. t:= ((l shr 16) xor r) and $0000ffff;
  216. r:= r xor t;
  217. l:= l xor (t shl 16);
  218. t:= ((r shr 4) xor l) and $0f0f0f0f;
  219. l:= l xor t;
  220. r:= r xor (t shl 4);
  221. PDword(@OutData)^:= l;
  222. PDword(dword(@OutData)+4)^:= r;
  223. end;
  224. procedure TDCP_customdes.DecryptBlock(const InData; var OutData; KeyData: PDWordArray);
  225. var
  226. l, r, t, u: dword;
  227. i: longint;
  228. begin
  229. r:= PDword(@InData)^;
  230. l:= PDword(dword(@InData)+4)^;
  231. t:= ((l shr 4) xor r) and $0f0f0f0f;
  232. r:= r xor t;
  233. l:= l xor (t shl 4);
  234. t:= ((r shr 16) xor l) and $0000ffff;
  235. l:= l xor t;
  236. r:= r xor (t shl 16);
  237. t:= ((l shr 2) xor r) and $33333333;
  238. r:= r xor t;
  239. l:= l xor (t shl 2);
  240. t:= ((r shr 8) xor l) and $00ff00ff;
  241. l:= l xor t;
  242. r:= r xor (t shl 8);
  243. t:= ((l shr 1) xor r) and $55555555;
  244. r:= r xor t;
  245. l:= l xor (t shl 1);
  246. r:= (r shr 29) or (r shl 3);
  247. l:= (l shr 29) or (l shl 3);
  248. i:= 30;
  249. while i> 0 do
  250. begin
  251. u:= r xor KeyData^[i ];
  252. t:= r xor KeyData^[i+1];
  253. t:= (t shr 4) or (t shl 28);
  254. l:= l xor des_SPtrans[0,(u shr 2) and $3f] xor
  255. des_SPtrans[2,(u shr 10) and $3f] xor
  256. des_SPtrans[4,(u shr 18) and $3f] xor
  257. des_SPtrans[6,(u shr 26) and $3f] xor
  258. des_SPtrans[1,(t shr 2) and $3f] xor
  259. des_SPtrans[3,(t shr 10) and $3f] xor
  260. des_SPtrans[5,(t shr 18) and $3f] xor
  261. des_SPtrans[7,(t shr 26) and $3f];
  262. u:= l xor KeyData^[i-2];
  263. t:= l xor KeyData^[i-1];
  264. t:= (t shr 4) or (t shl 28);
  265. r:= r xor des_SPtrans[0,(u shr 2) and $3f] xor
  266. des_SPtrans[2,(u shr 10) and $3f] xor
  267. des_SPtrans[4,(u shr 18) and $3f] xor
  268. des_SPtrans[6,(u shr 26) and $3f] xor
  269. des_SPtrans[1,(t shr 2) and $3f] xor
  270. des_SPtrans[3,(t shr 10) and $3f] xor
  271. des_SPtrans[5,(t shr 18) and $3f] xor
  272. des_SPtrans[7,(t shr 26) and $3f];
  273. u:= r xor KeyData^[i-4];
  274. t:= r xor KeyData^[i-3];
  275. t:= (t shr 4) or (t shl 28);
  276. l:= l xor des_SPtrans[0,(u shr 2) and $3f] xor
  277. des_SPtrans[2,(u shr 10) and $3f] xor
  278. des_SPtrans[4,(u shr 18) and $3f] xor
  279. des_SPtrans[6,(u shr 26) and $3f] xor
  280. des_SPtrans[1,(t shr 2) and $3f] xor
  281. des_SPtrans[3,(t shr 10) and $3f] xor
  282. des_SPtrans[5,(t shr 18) and $3f] xor
  283. des_SPtrans[7,(t shr 26) and $3f];
  284. u:= l xor KeyData^[i-6];
  285. t:= l xor KeyData^[i-5];
  286. t:= (t shr 4) or (t shl 28);
  287. r:= r xor des_SPtrans[0,(u shr 2) and $3f] xor
  288. des_SPtrans[2,(u shr 10) and $3f] xor
  289. des_SPtrans[4,(u shr 18) and $3f] xor
  290. des_SPtrans[6,(u shr 26) and $3f] xor
  291. des_SPtrans[1,(t shr 2) and $3f] xor
  292. des_SPtrans[3,(t shr 10) and $3f] xor
  293. des_SPtrans[5,(t shr 18) and $3f] xor
  294. des_SPtrans[7,(t shr 26) and $3f];
  295. Dec(i,8);
  296. end;
  297. r:= (r shr 3) or (r shl 29);
  298. l:= (l shr 3) or (l shl 29);
  299. t:= ((r shr 1) xor l) and $55555555;
  300. l:= l xor t;
  301. r:= r xor (t shl 1);
  302. t:= ((l shr 8) xor r) and $00ff00ff;
  303. r:= r xor t;
  304. l:= l xor (t shl 8);
  305. t:= ((r shr 2) xor l) and $33333333;
  306. l:= l xor t;
  307. r:= r xor (t shl 2);
  308. t:= ((l shr 16) xor r) and $0000ffff;
  309. r:= r xor t;
  310. l:= l xor (t shl 16);
  311. t:= ((r shr 4) xor l) and $0f0f0f0f;
  312. l:= l xor t;
  313. r:= r xor (t shl 4);
  314. PDword(@OutData)^:= l;
  315. PDword(dword(@OutData)+4)^:= r;
  316. end;
  317. class function TDCP_des.GetMaxKeySize: integer;
  318. begin
  319. Result:= 64;
  320. end;
  321. class function TDCP_des.GetID: integer;
  322. begin
  323. Result:= DCP_des;
  324. end;
  325. class function TDCP_des.GetAlgorithm: string;
  326. begin
  327. Result:= 'DES';
  328. end;
  329. class function TDCP_des.SelfTest: boolean;
  330. const
  331. InData1: array[0..7] of byte=
  332. ($07,$56,$D8,$E0,$77,$47,$61,$D2);
  333. OutData1: array[0..7] of byte=
  334. ($0C,$D3,$DA,$02,$00,$21,$DC,$09);
  335. Key1: array[0..7] of byte=
  336. ($01,$70,$F1,$75,$46,$8F,$B5,$E6);
  337. InData2: array[0..7] of byte=
  338. ($48,$0D,$39,$00,$6E,$E7,$62,$F2);
  339. OutData2: array[0..7] of byte=
  340. ($A1,$F9,$91,$55,$41,$02,$0B,$56);
  341. Key2: array[0..7] of byte=
  342. ($02,$58,$16,$16,$46,$29,$B0,$07);
  343. var
  344. Cipher: TDCP_des;
  345. Data: array[0..7] of byte;
  346. begin
  347. Cipher:= TDCP_des.Create(nil);
  348. Cipher.Init(Key1,Sizeof(Key1)*8,nil);
  349. Cipher.EncryptECB(InData1,Data);
  350. Result:= boolean(CompareMem(@Data,@OutData1,Sizeof(Data)));
  351. Cipher.DecryptECB(Data,Data);
  352. Result:= Result and boolean(CompareMem(@Data,@InData1,Sizeof(Data)));
  353. Cipher.Burn;
  354. Cipher.Init(Key2,Sizeof(Key2)*8,nil);
  355. Cipher.EncryptECB(InData2,Data);
  356. Result:= Result and boolean(CompareMem(@Data,@OutData2,Sizeof(Data)));
  357. Cipher.DecryptECB(Data,Data);
  358. Result:= Result and boolean(CompareMem(@Data,@InData2,Sizeof(Data)));
  359. Cipher.Burn;
  360. Cipher.Free;
  361. end;
  362. procedure TDCP_des.InitKey(const Key; Size: longword);
  363. var
  364. KeyB: array[0..7] of byte;
  365. begin
  366. FillChar(KeyB,Sizeof(KeyB),0);
  367. Move(Key,KeyB,Size div 8);
  368. DoInit(@KeyB,@KeyData);
  369. end;
  370. procedure TDCP_des.Burn;
  371. begin
  372. FillChar(KeyData,Sizeof(KeyData),0);
  373. inherited Burn;
  374. end;
  375. procedure TDCP_des.EncryptECB(const InData; var OutData);
  376. begin
  377. if not fInitialized then
  378. raise EDCP_blockcipher.Create('Cipher not initialized');
  379. EncryptBlock(InData,OutData,@KeyData);
  380. end;
  381. procedure TDCP_des.DecryptECB(const InData; var OutData);
  382. begin
  383. if not fInitialized then
  384. raise EDCP_blockcipher.Create('Cipher not initialized');
  385. DecryptBlock(InData,OutData,@KeyData);
  386. end;
  387. {******************************************************************************}
  388. class function TDCP_3des.GetMaxKeySize: integer;
  389. begin
  390. Result:= 192;
  391. end;
  392. class function TDCP_3des.GetID: integer;
  393. begin
  394. Result:= DCP_3des;
  395. end;
  396. class function TDCP_3des.GetAlgorithm: string;
  397. begin
  398. Result:= '3DES';
  399. end;
  400. class function TDCP_3des.SelfTest: boolean;
  401. const
  402. Key: array[0..23] of byte=
  403. ($01,$23,$45,$67,$89,$ab,$cd,$ef,$fe,$dc,$ba,$98,
  404. $76,$54,$32,$10,$89,$ab,$cd,$ef,$01,$23,$45,$67);
  405. PlainText: array[0..7] of byte=
  406. ($01,$23,$45,$67,$89,$ab,$cd,$e7);
  407. CipherText: array[0..7] of byte=
  408. ($de,$0b,$7c,$06,$ae,$5e,$0e,$d5);
  409. var
  410. Cipher: TDCP_3des;
  411. Block: array[0..7] of byte;
  412. begin
  413. Cipher:= TDCP_3des.Create(nil);
  414. Cipher.Init(Key,Sizeof(Key)*8,nil);
  415. Cipher.EncryptECB(PlainText,Block);
  416. Result:= CompareMem(@Block,@CipherText,Sizeof(CipherText));
  417. Cipher.DecryptECB(Block,Block);
  418. Result:= Result and CompareMem(@Block,@PlainText,Sizeof(PlainText));
  419. Cipher.Free;
  420. end;
  421. procedure TDCP_3des.InitKey(const Key; Size: longword);
  422. var
  423. KeyB: array[0..2,0..7] of byte;
  424. begin
  425. FillChar(KeyB,Sizeof(KeyB),0);
  426. Move(Key,KeyB,Size div 8);
  427. DoInit(@KeyB[0],@KeyData[0]);
  428. DoInit(@KeyB[1],@KeyData[1]);
  429. if Size> 128 then
  430. DoInit(@KeyB[2],@KeyData[2])
  431. else
  432. Move(KeyData[0],KeyData[2],128);
  433. end;
  434. procedure TDCP_3des.Burn;
  435. begin
  436. FillChar(KeyData,Sizeof(KeyData),0);
  437. inherited Burn;
  438. end;
  439. procedure TDCP_3des.EncryptECB(const InData; var OutData);
  440. begin
  441. if not fInitialized then
  442. raise EDCP_blockcipher.Create('Cipher not initialized');
  443. EncryptBlock(InData,OutData,@KeyData[0]);
  444. DecryptBlock(OutData,OutData,@KeyData[1]);
  445. EncryptBlock(OutData,OutData,@KeyData[2]);
  446. end;
  447. procedure TDCP_3des.DecryptECB(const InData; var OutData);
  448. begin
  449. if not fInitialized then
  450. raise EDCP_blockcipher.Create('Cipher not initialized');
  451. DecryptBlock(InData,OutData,@KeyData[2]);
  452. EncryptBlock(OutData,OutData,@KeyData[1]);
  453. DecryptBlock(OutData,OutData,@KeyData[0]);
  454. end;
  455. end.