DCPmars.pas 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. {******************************************************************************}
  2. {* DCPcrypt v2.0 written by David Barton (crypto@cityinthesky.co.uk) **********}
  3. {******************************************************************************}
  4. {* A binary compatible implementation of Mars *********************************}
  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 DCPmars;
  26. interface
  27. uses
  28. Classes, Sysutils, DCPcrypt2, DCPconst, DCPblockciphers;
  29. type
  30. TDCP_mars= class(TDCP_blockcipher128)
  31. protected
  32. KeyData: array[0..39] of DWord;
  33. procedure InitKey(const Key; Size: longword); override;
  34. public
  35. class function GetID: integer; override;
  36. class function GetAlgorithm: string; override;
  37. class function GetMaxKeySize: integer; override;
  38. class function SelfTest: boolean; override;
  39. procedure Burn; override;
  40. procedure EncryptECB(const InData; var OutData); override;
  41. procedure DecryptECB(const InData; var OutData); override;
  42. end;
  43. {******************************************************************************}
  44. {******************************************************************************}
  45. implementation
  46. {$R-}{$Q-}
  47. {$I DCPmars.inc}
  48. function LRot32(X: DWord; c: longword): DWord;
  49. begin
  50. LRot32:= (X shl c) or (X shr (32 - c));
  51. end;
  52. function RRot32(X: DWord; c: longword): DWord;
  53. begin
  54. RRot32:= (X shr c) or (X shl (32 - c));
  55. end;
  56. class function TDCP_mars.GetID: integer;
  57. begin
  58. Result:= DCP_mars;
  59. end;
  60. class function TDCP_mars.GetAlgorithm: string;
  61. begin
  62. Result:= 'Mars';
  63. end;
  64. class function TDCP_mars.GetMaxKeySize: integer;
  65. begin
  66. Result:= 1248;
  67. end;
  68. class function TDCP_mars.SelfTest: boolean;
  69. const
  70. Key1: array[0..3] of dword=
  71. ($deb35132,$83c296de,$39069e6b,$994c2438);
  72. Key2: array[0..5] of dword=
  73. ($a5391779,$1a58048b,$a853a993,$1d41102c,$088658d1,$954d8738);
  74. Key3: array[0..7] of dword=
  75. ($9867a1fb,$22ef7a3e,$8ce27c31,$a3e1aa02,$3ccce5e8,$2aa8beed,$9ac3db99,$27725ed6);
  76. Plain1: array[0..3] of dword= ($deb35132,$83c296de,$39069e6b,$994c2438);
  77. Plain2: array[0..3] of dword= ($2dc46167,$d242613e,$adbf4fa8,$8f1583b3);
  78. Plain3: array[0..3] of dword= ($a4ab4413,$0847c4d3,$1621a7a8,$8493f4d4);
  79. Cipher1: array[0..3] of dword= ($a91245f9,$4e032db4,$042279c4,$9ba608d7);
  80. Cipher2: array[0..3] of dword= ($260334cb,$6d587f45,$e0d2bd54,$bd191c57);
  81. Cipher3: array[0..3] of dword= ($67a1acdd,$be3163e3,$5f9f1c2c,$b8a48fe3);
  82. var
  83. Cipher: TDCP_mars;
  84. Block: array[0..3] of dword;
  85. begin
  86. Cipher:= TDCP_mars.Create(nil);
  87. Cipher.Init(Key1,Sizeof(Key1)*8,nil);
  88. Cipher.EncryptECB(Plain1,Block);
  89. Result:= CompareMem(@Cipher1,@Block,Sizeof(Block));
  90. Cipher.DecryptECB(Block,Block);
  91. Result:= Result and CompareMem(@Plain1,@Block,Sizeof(Block));
  92. Cipher.Burn;
  93. Cipher.Init(Key2,Sizeof(Key2)*8,nil);
  94. Cipher.EncryptECB(Plain2,Block);
  95. Result:= Result and CompareMem(@Cipher2,@Block,Sizeof(Block));
  96. Cipher.DecryptECB(Block,Block);
  97. Result:= Result and CompareMem(@Plain2,@Block,Sizeof(Block));
  98. Cipher.Burn;
  99. Cipher.Init(Key3,Sizeof(Key3)*8,nil);
  100. Cipher.EncryptECB(Plain3,Block);
  101. Result:= Result and CompareMem(@Cipher3,@Block,Sizeof(Block));
  102. Cipher.DecryptECB(Block,Block);
  103. Result:= Result and CompareMem(@Plain3,@Block,Sizeof(Block));
  104. Cipher.Burn;
  105. Cipher.Free;
  106. end;
  107. procedure gen_mask(var x, m: DWord);
  108. var
  109. u: DWord;
  110. begin
  111. u:= x and (x shr 1); u:= u and (u shr 2);
  112. u:= u and (u shr 4); u:= u and (u shr 1) and (u shr 2);
  113. m:= u;
  114. u:= (x xor $FFFFFFFF) and ((x xor $FFFFFFFF) shr 1); u:= u and (u shr 2);
  115. u:= u and (u shr 4); u:= u and (u shr 1) and (u shr 2);
  116. u:= u or m;
  117. m:= (u shl 1) or (u shl 2) or (u shl 3)
  118. or (u shl 4) or (u shl 5) or (u shl 6)
  119. or (u shl 7) or (u shl 8);
  120. m:= (m or u or (u shl 9)) and ((x xor $FFFFFFFF) xor (x shl 1)) and ((x xor $FFFFFFFF) xor (x shr 1));
  121. m:= m and $FFFFFFFC;
  122. end;
  123. procedure TDCP_mars.InitKey(const Key; Size: longword);
  124. var
  125. i, j, m, u, w: DWord;
  126. t: array[-7..39] of DWord;
  127. KeyB: array[0..39] of DWord;
  128. begin
  129. Size:= Size div 8;
  130. FillChar(KeyB,Sizeof(KeyB),0);
  131. Move(Key,KeyB,Size);
  132. Size:= Size div 4;
  133. Move(vk,t,Sizeof(vk));
  134. for i:= 0 to 38 do
  135. begin
  136. u:= t[i-7] xor t[i-2];
  137. t[i]:= LRot32(u,3) xor KeyB[i mod DWord(Size)] xor i;
  138. end;
  139. t[39]:= Size;
  140. for j:= 0 to 6 do
  141. begin
  142. for i:= 1 to 39 do
  143. begin
  144. u:= t[i] + s_box[t[i-1] and $1FF];
  145. t[i]:= LRot32(u,9);
  146. end;
  147. u:= t[0] + s_box[t[39] and $1FF];
  148. t[0]:= LRot32(u,9);
  149. end;
  150. for i:= 0 to 39 do
  151. KeyData[(7*i) mod 40]:= t[i];
  152. i:= 5;
  153. repeat
  154. u:= s_box[265+(KeyData[i] and $3)];
  155. j:= KeyData[i+3] and $1f;
  156. w:= KeyData[i] or $3;
  157. gen_mask(w,m);
  158. KeyData[i]:= w xor (LRot32(u,j) and m);
  159. Inc(i,2);
  160. until i>= 37;
  161. end;
  162. procedure TDCP_mars.Burn;
  163. begin
  164. FillChar(KeyData,Sizeof(KeyData),$FF);
  165. inherited Burn;
  166. end;
  167. procedure TDCP_mars.EncryptECB(const InData; var OutData);
  168. var
  169. l, m, r, t: DWord;
  170. blk: array[0..3] of DWord;
  171. begin
  172. if not fInitialized then
  173. raise EDCP_blockcipher.Create('Cipher not initialized');
  174. Blk[0]:= PDWord(@InData)^;
  175. Blk[1]:= PDWord(longword(@InData)+4)^;
  176. Blk[2]:= PDWord(longword(@InData)+8)^;
  177. Blk[3]:= PDWord(longword(@InData)+12)^;
  178. blk[0]:= blk[0] + KeyData[0]; blk[1]:= blk[1] + KeyData[1];
  179. blk[2]:= blk[2] + KeyData[2]; blk[3]:= blk[3] + KeyData[3];
  180. blk[1]:= blk[1] xor s_box[ blk[0] and $FF];
  181. blk[1]:= blk[1] + s_box[((blk[0] shr 8) and $FF) + 256];
  182. blk[2]:= blk[2] + s_box[ (blk[0] shr 16) and $FF];
  183. blk[3]:= blk[3] xor s_box[((blk[0] shr 24) and $FF) + 256];
  184. blk[0]:= RRot32(blk[0], 24); blk[0]:= blk[0] + blk[3];
  185. blk[2]:= blk[2] xor s_box[ blk[1] and $FF];
  186. blk[2]:= blk[2] + s_box[((blk[1] shr 8) and $FF) + 256];
  187. blk[3]:= blk[3] + s_box[ (blk[1] shr 16) and $FF];
  188. blk[0]:= blk[0] xor s_box[((blk[1] shr 24) and $FF) + 256];
  189. blk[1]:= RRot32(blk[1], 24); blk[1]:= blk[1] + blk[2];
  190. blk[3]:= blk[3] xor s_box[ blk[2] and $FF];
  191. blk[3]:= blk[3] + s_box[((blk[2] shr 8) and $FF) + 256];
  192. blk[0]:= blk[0] + s_box[ (blk[2] shr 16) and $FF];
  193. blk[1]:= blk[1] xor s_box[((blk[2] shr 24) and $FF) + 256];
  194. blk[2]:= RRot32(blk[2], 24);
  195. blk[0]:= blk[0] xor s_box[ blk[3] and $FF];
  196. blk[0]:= blk[0] + s_box[((blk[3] shr 8) and $FF) + 256];
  197. blk[1]:= blk[1] + s_box[ (blk[3] shr 16) and $FF];
  198. blk[2]:= blk[2] xor s_box[((blk[3] shr 24) and $FF) + 256];
  199. blk[3]:= RRot32(blk[3], 24);
  200. blk[1]:= blk[1] xor s_box[ blk[0] and $FF];
  201. blk[1]:= blk[1] + s_box[((blk[0] shr 8) and $FF) + 256];
  202. blk[2]:= blk[2] + s_box[ (blk[0] shr 16) and $FF];
  203. blk[3]:= blk[3] xor s_box[((blk[0] shr 24) and $FF) + 256];
  204. blk[0]:= RRot32(blk[0], 24); blk[0]:= blk[0] + blk[3];
  205. blk[2]:= blk[2] xor s_box[ blk[1] and $FF];
  206. blk[2]:= blk[2] + s_box[((blk[1] shr 8) and $FF) + 256];
  207. blk[3]:= blk[3] + s_box[ (blk[1] shr 16) and $FF];
  208. blk[0]:= blk[0] xor s_box[((blk[1] shr 24) and $FF) + 256];
  209. blk[1]:= RRot32(blk[1], 24); blk[1]:= blk[1] + blk[2];
  210. blk[3]:= blk[3] xor s_box[ blk[2] and $FF];
  211. blk[3]:= blk[3] + s_box[((blk[2] shr 8) and $FF) + 256];
  212. blk[0]:= blk[0] + s_box[ (blk[2] shr 16) and $FF];
  213. blk[1]:= blk[1] xor s_box[((blk[2] shr 24) and $FF) + 256];
  214. blk[2]:= RRot32(blk[2], 24);
  215. blk[0]:= blk[0] xor s_box[ blk[3] and $FF];
  216. blk[0]:= blk[0] + s_box[((blk[3] shr 8) and $FF) + 256];
  217. blk[1]:= blk[1] + s_box[ (blk[3] shr 16) and $FF];
  218. blk[2]:= blk[2] xor s_box[((blk[3] shr 24) and $FF) + 256];
  219. blk[3]:= RRot32(blk[3], 24);
  220. m:= blk[0] + KeyData[4];
  221. r:= LRot32(blk[0],13) * KeyData[5];
  222. l:= s_box[m and $1FF]; r:= LRot32(r,5);
  223. t:= r and $1f; m:= LRot32(m,t);
  224. l:= l xor r; r:= LRot32(r,5); l:= l xor r;
  225. t:= r and $1f; l:= LRot32(l,t);
  226. blk[0]:= LRot32(blk[0],13);
  227. blk[1]:= blk[1] + l;
  228. blk[2]:= blk[2] + m;
  229. blk[3]:= blk[3] xor r;
  230. m:= blk[1] + KeyData[6];
  231. r:= LRot32(blk[1],13) * KeyData[7];
  232. l:= s_box[m and $1FF]; r:= LRot32(r,5);
  233. t:= r and $1f; m:= LRot32(m,t);
  234. l:= l xor r; r:= LRot32(r,5); l:= l xor r;
  235. t:= r and $1f; l:= LRot32(l,t);
  236. blk[1]:= LRot32(blk[1],13);
  237. blk[2]:= blk[2] + l;
  238. blk[3]:= blk[3] + m;
  239. blk[0]:= blk[0] xor r;
  240. m:= blk[2] + KeyData[8];
  241. r:= LRot32(blk[2],13) * KeyData[9];
  242. l:= s_box[m and $1FF]; r:= LRot32(r,5);
  243. t:= r and $1f; m:= LRot32(m,t);
  244. l:= l xor r; r:= LRot32(r,5); l:= l xor r;
  245. t:= r and $1f; l:= LRot32(l,t);
  246. blk[2]:= LRot32(blk[2],13);
  247. blk[3]:= blk[3] + l;
  248. blk[0]:= blk[0] + m;
  249. blk[1]:= blk[1] xor r;
  250. m:= blk[3] + KeyData[10];
  251. r:= LRot32(blk[3],13) * KeyData[11];
  252. l:= s_box[m and $1FF]; r:= LRot32(r,5);
  253. t:= r and $1f; m:= LRot32(m,t);
  254. l:= l xor r; r:= LRot32(r,5); l:= l xor r;
  255. t:= r and $1f; l:= LRot32(l,t);
  256. blk[3]:= LRot32(blk[3],13);
  257. blk[0]:= blk[0] + l;
  258. blk[1]:= blk[1] + m;
  259. blk[2]:= blk[2] xor r;
  260. m:= blk[0] + KeyData[12];
  261. r:= LRot32(blk[0],13) * KeyData[13];
  262. l:= s_box[m and $1FF]; r:= LRot32(r,5);
  263. t:= r and $1f; m:= LRot32(m,t);
  264. l:= l xor r; r:= LRot32(r,5); l:= l xor r;
  265. t:= r and $1f; l:= LRot32(l,t);
  266. blk[0]:= LRot32(blk[0],13);
  267. blk[1]:= blk[1] + l;
  268. blk[2]:= blk[2] + m;
  269. blk[3]:= blk[3] xor r;
  270. m:= blk[1] + KeyData[14];
  271. r:= LRot32(blk[1],13) * KeyData[15];
  272. l:= s_box[m and $1FF]; r:= LRot32(r,5);
  273. t:= r and $1f; m:= LRot32(m,t);
  274. l:= l xor r; r:= LRot32(r,5); l:= l xor r;
  275. t:= r and $1f; l:= LRot32(l,t);
  276. blk[1]:= LRot32(blk[1],13);
  277. blk[2]:= blk[2] + l;
  278. blk[3]:= blk[3] + m;
  279. blk[0]:= blk[0] xor r;
  280. m:= blk[2] + KeyData[16];
  281. r:= LRot32(blk[2],13) * KeyData[17];
  282. l:= s_box[m and $1FF]; r:= LRot32(r,5);
  283. t:= r and $1f; m:= LRot32(m,t);
  284. l:= l xor r; r:= LRot32(r,5); l:= l xor r;
  285. t:= r and $1f; l:= LRot32(l,t);
  286. blk[2]:= LRot32(blk[2],13);
  287. blk[3]:= blk[3] + l;
  288. blk[0]:= blk[0] + m;
  289. blk[1]:= blk[1] xor r;
  290. m:= blk[3] + KeyData[18];
  291. r:= LRot32(blk[3],13) * KeyData[19];
  292. l:= s_box[m and $1FF]; r:= LRot32(r,5);
  293. t:= r and $1f; m:= LRot32(m,t);
  294. l:= l xor r; r:= LRot32(r,5); l:= l xor r;
  295. t:= r and $1f; l:= LRot32(l,t);
  296. blk[3]:= LRot32(blk[3],13);
  297. blk[0]:= blk[0] + l;
  298. blk[1]:= blk[1] + m;
  299. blk[2]:= blk[2] xor r;
  300. m:= blk[0] + KeyData[20];
  301. r:= LRot32(blk[0],13) * KeyData[21];
  302. l:= s_box[m and $1FF]; r:= LRot32(r,5);
  303. t:= r and $1f; m:= LRot32(m,t);
  304. l:= l xor r; r:= LRot32(r,5); l:= l xor r;
  305. t:= r and $1f; l:= LRot32(l,t);
  306. blk[0]:= LRot32(blk[0],13);
  307. blk[3]:= blk[3] + l;
  308. blk[2]:= blk[2] + m;
  309. blk[1]:= blk[1] xor r;
  310. m:= blk[1] + KeyData[22];
  311. r:= LRot32(blk[1],13) * KeyData[23];
  312. l:= s_box[m and $1FF]; r:= LRot32(r,5);
  313. t:= r and $1f; m:= LRot32(m,t);
  314. l:= l xor r; r:= LRot32(r,5); l:= l xor r;
  315. t:= r and $1f; l:= LRot32(l,t);
  316. blk[1]:= LRot32(blk[1],13);
  317. blk[0]:= blk[0] + l;
  318. blk[3]:= blk[3] + m;
  319. blk[2]:= blk[2] xor r;
  320. m:= blk[2] + KeyData[24];
  321. r:= LRot32(blk[2],13) * KeyData[25];
  322. l:= s_box[m and $1FF]; r:= LRot32(r,5);
  323. t:= r and $1f; m:= LRot32(m,t);
  324. l:= l xor r; r:= LRot32(r,5); l:= l xor r;
  325. t:= r and $1f; l:= LRot32(l,t);
  326. blk[2]:= LRot32(blk[2],13);
  327. blk[1]:= blk[1] + l;
  328. blk[0]:= blk[0] + m;
  329. blk[3]:= blk[3] xor r;
  330. m:= blk[3] + KeyData[26];
  331. r:= LRot32(blk[3],13) * KeyData[27];
  332. l:= s_box[m and $1FF]; r:= LRot32(r,5);
  333. t:= r and $1f; m:= LRot32(m,t);
  334. l:= l xor r; r:= LRot32(r,5); l:= l xor r;
  335. t:= r and $1f; l:= LRot32(l,t);
  336. blk[3]:= LRot32(blk[3],13);
  337. blk[2]:= blk[2] + l;
  338. blk[1]:= blk[1] + m;
  339. blk[0]:= blk[0] xor r;
  340. m:= blk[0] + KeyData[28];
  341. r:= LRot32(blk[0],13) * KeyData[29];
  342. l:= s_box[m and $1FF]; r:= LRot32(r,5);
  343. t:= r and $1f; m:= LRot32(m,t);
  344. l:= l xor r; r:= LRot32(r,5); l:= l xor r;
  345. t:= r and $1f; l:= LRot32(l,t);
  346. blk[0]:= LRot32(blk[0],13);
  347. blk[3]:= blk[3] + l;
  348. blk[2]:= blk[2] + m;
  349. blk[1]:= blk[1] xor r;
  350. m:= blk[1] + KeyData[30];
  351. r:= LRot32(blk[1],13) * KeyData[31];
  352. l:= s_box[m and $1FF]; r:= LRot32(r,5);
  353. t:= r and $1f; m:= LRot32(m,t);
  354. l:= l xor r; r:= LRot32(r,5); l:= l xor r;
  355. t:= r and $1f; l:= LRot32(l,t);
  356. blk[1]:= LRot32(blk[1],13);
  357. blk[0]:= blk[0] + l;
  358. blk[3]:= blk[3] + m;
  359. blk[2]:= blk[2] xor r;
  360. m:= blk[2] + KeyData[32];
  361. r:= LRot32(blk[2],13) * KeyData[33];
  362. l:= s_box[m and $1FF]; r:= LRot32(r,5);
  363. t:= r and $1f; m:= LRot32(m,t);
  364. l:= l xor r; r:= LRot32(r,5); l:= l xor r;
  365. t:= r and $1f; l:= LRot32(l,t);
  366. blk[2]:= LRot32(blk[2],13);
  367. blk[1]:= blk[1] + l;
  368. blk[0]:= blk[0] + m;
  369. blk[3]:= blk[3] xor r;
  370. m:= blk[3] + KeyData[34];
  371. r:= LRot32(blk[3],13) * KeyData[35];
  372. l:= s_box[m and $1FF]; r:= LRot32(r,5);
  373. t:= r and $1f; m:= LRot32(m,t);
  374. l:= l xor r; r:= LRot32(r,5); l:= l xor r;
  375. t:= r and $1f; l:= LRot32(l,t);
  376. blk[3]:= LRot32(blk[3],13);
  377. blk[2]:= blk[2] + l;
  378. blk[1]:= blk[1] + m;
  379. blk[0]:= blk[0] xor r;
  380. blk[1]:= blk[1] xor s_box[ (blk[0] and $FF) + 256];
  381. blk[2]:= blk[2] - s_box[ (blk[0] shr 24) and $FF];
  382. blk[3]:= blk[3] - s_box[((blk[0] shr 16) and $FF) + 256];
  383. blk[3]:= blk[3] xor s_box[ (blk[0] shr 8) and $FF];
  384. blk[0]:= LRot32(blk[0], 24);
  385. blk[2]:= blk[2] xor s_box[ (blk[1] and $FF) + 256];
  386. blk[3]:= blk[3] - s_box[ (blk[1] shr 24) and $FF];
  387. blk[0]:= blk[0] - s_box[((blk[1] shr 16) and $FF) + 256];
  388. blk[0]:= blk[0] xor s_box[ (blk[1] shr 8) and $FF];
  389. blk[1]:= LRot32(blk[1], 24); blk[2]:= blk[2] - blk[1];
  390. blk[3]:= blk[3] xor s_box[ (blk[2] and $FF) + 256];
  391. blk[0]:= blk[0] - s_box[ (blk[2] shr 24) and $FF];
  392. blk[1]:= blk[1] - s_box[((blk[2] shr 16) and $FF) + 256];
  393. blk[1]:= blk[1] xor s_box[ (blk[2] shr 8) and $FF];
  394. blk[2]:= LRot32(blk[2], 24); blk[3]:= blk[3] - blk[0];
  395. blk[0]:= blk[0] xor s_box[ (blk[3] and $FF) + 256];
  396. blk[1]:= blk[1] - s_box[ (blk[3] shr 24) and $FF];
  397. blk[2]:= blk[2] - s_box[((blk[3] shr 16) and $FF) + 256];
  398. blk[2]:= blk[2] xor s_box[ (blk[3] shr 8) and $FF];
  399. blk[3]:= LRot32(blk[3], 24);
  400. blk[1]:= blk[1] xor s_box[ (blk[0] and $FF) + 256];
  401. blk[2]:= blk[2] - s_box[ (blk[0] shr 24) and $FF];
  402. blk[3]:= blk[3] - s_box[((blk[0] shr 16) and $FF) + 256];
  403. blk[3]:= blk[3] xor s_box[ (blk[0] shr 8) and $FF];
  404. blk[0]:= LRot32(blk[0], 24);
  405. blk[2]:= blk[2] xor s_box[ (blk[1] and $FF) + 256];
  406. blk[3]:= blk[3] - s_box[ (blk[1] shr 24) and $FF];
  407. blk[0]:= blk[0] - s_box[((blk[1] shr 16) and $FF) + 256];
  408. blk[0]:= blk[0] xor s_box[ (blk[1] shr 8) and $FF];
  409. blk[1]:= LRot32(blk[1], 24); blk[2]:= blk[2] - blk[1];
  410. blk[3]:= blk[3] xor s_box[ (blk[2] and $FF) + 256];
  411. blk[0]:= blk[0] - s_box[ (blk[2] shr 24) and $FF];
  412. blk[1]:= blk[1] - s_box[((blk[2] shr 16) and $FF) + 256];
  413. blk[1]:= blk[1] xor s_box[ (blk[2] shr 8) and $FF];
  414. blk[2]:= LRot32(blk[2], 24); blk[3]:= blk[3] - blk[0];
  415. blk[0]:= blk[0] xor s_box[ (blk[3] and $FF) + 256];
  416. blk[1]:= blk[1] - s_box[ (blk[3] shr 24) and $FF];
  417. blk[2]:= blk[2] - s_box[((blk[3] shr 16) and $FF) + 256];
  418. blk[2]:= blk[2] xor s_box[ (blk[3] shr 8) and $FF];
  419. blk[3]:= LRot32(blk[3], 24);
  420. blk[0]:= blk[0] - KeyData[36]; blk[1]:= blk[1] - KeyData[37];
  421. blk[2]:= blk[2] - KeyData[38]; blk[3]:= blk[3] - KeyData[39];
  422. PDWord(@OutData)^:= Blk[0];
  423. PDWord(longword(@OutData)+4)^:= Blk[1];
  424. PDWord(longword(@OutData)+8)^:= Blk[2];
  425. PDWord(longword(@OutData)+12)^:= Blk[3];
  426. end;
  427. procedure TDCP_mars.DecryptECB(const InData; var OutData);
  428. var
  429. l, m, r, t: DWord;
  430. blk: array[0..3] of DWord;
  431. begin
  432. if not fInitialized then
  433. raise EDCP_blockcipher.Create('Cipher not initialized');
  434. Blk[0]:= PDWord(@InData)^;
  435. Blk[1]:= PDWord(longword(@InData)+4)^;
  436. Blk[2]:= PDWord(longword(@InData)+8)^;
  437. Blk[3]:= PDWord(longword(@InData)+12)^;
  438. blk[0]:= blk[0] + KeyData[36]; blk[1]:= blk[1] + KeyData[37];
  439. blk[2]:= blk[2] + KeyData[38]; blk[3]:= blk[3] + KeyData[39];
  440. blk[3]:= RRot32(blk[3], 24);
  441. blk[2]:= blk[2] xor s_box[ (blk[3] shr 8) and $FF];
  442. blk[2]:= blk[2] + s_box[((blk[3] shr 16) and $FF) + 256];
  443. blk[1]:= blk[1] + s_box[ (blk[3] shr 24) and $FF];
  444. blk[0]:= blk[0] xor s_box[ (blk[3] and $FF) + 256];
  445. blk[3]:= blk[3] + blk[0]; blk[2]:= RRot32(blk[2], 24);
  446. blk[1]:= blk[1] xor s_box[ (blk[2] shr 8) and $FF];
  447. blk[1]:= blk[1] + s_box[((blk[2] shr 16) and $FF) + 256];
  448. blk[0]:= blk[0] + s_box[ (blk[2] shr 24) and $FF];
  449. blk[3]:= blk[3] xor s_box[ (blk[2] and $FF) + 256];
  450. blk[2]:= blk[2] + blk[1]; blk[1]:= RRot32(blk[1], 24);
  451. blk[0]:= blk[0] xor s_box[ (blk[1] shr 8) and $FF];
  452. blk[0]:= blk[0] + s_box[((blk[1] shr 16) and $FF) + 256];
  453. blk[3]:= blk[3] + s_box[ (blk[1] shr 24) and $FF];
  454. blk[2]:= blk[2] xor s_box[ (blk[1] and $FF) + 256];
  455. blk[0]:= RRot32(blk[0], 24);
  456. blk[3]:= blk[3] xor s_box[ (blk[0] shr 8) and $FF];
  457. blk[3]:= blk[3] + s_box[((blk[0] shr 16) and $FF) + 256];
  458. blk[2]:= blk[2] + s_box[ (blk[0] shr 24) and $FF];
  459. blk[1]:= blk[1] xor s_box[ (blk[0] and $FF) + 256];
  460. blk[3]:= RRot32(blk[3], 24);
  461. blk[2]:= blk[2] xor s_box[ (blk[3] shr 8) and $FF];
  462. blk[2]:= blk[2] + s_box[((blk[3] shr 16) and $FF) + 256];
  463. blk[1]:= blk[1] + s_box[ (blk[3] shr 24) and $FF];
  464. blk[0]:= blk[0] xor s_box[ (blk[3] and $FF) + 256];
  465. blk[3]:= blk[3] + blk[0]; blk[2]:= RRot32(blk[2], 24);
  466. blk[1]:= blk[1] xor s_box[ (blk[2] shr 8) and $FF];
  467. blk[1]:= blk[1] + s_box[((blk[2] shr 16) and $FF) + 256];
  468. blk[0]:= blk[0] + s_box[ (blk[2] shr 24) and $FF];
  469. blk[3]:= blk[3] xor s_box[ (blk[2] and $FF) + 256];
  470. blk[2]:= blk[2] + blk[1]; blk[1]:= RRot32(blk[1], 24);
  471. blk[0]:= blk[0] xor s_box[ (blk[1] shr 8) and $FF];
  472. blk[0]:= blk[0] + s_box[((blk[1] shr 16) and $FF) + 256];
  473. blk[3]:= blk[3] + s_box[ (blk[1] shr 24) and $FF];
  474. blk[2]:= blk[2] xor s_box[ (blk[1] and $FF) + 256];
  475. blk[0]:= RRot32(blk[0], 24);
  476. blk[3]:= blk[3] xor s_box[ (blk[0] shr 8) and $FF];
  477. blk[3]:= blk[3] + s_box[((blk[0] shr 16) and $FF) + 256];
  478. blk[2]:= blk[2] + s_box[ (blk[0] shr 24) and $FF];
  479. blk[1]:= blk[1] xor s_box[ (blk[0] and $FF) + 256];
  480. blk[3]:= RRot32(blk[3],13);
  481. m:= blk[3] + KeyData[34];
  482. r:= LRot32(blk[3],13) * KeyData[35];
  483. l:= s_box[m and $1FF]; r:= LRot32(r,5);
  484. t:= r and $1f; m:= LRot32(m,t);
  485. l:= l xor r; r:= LRot32(r,5); l:= l xor r;
  486. t:= r and $1f; l:= LRot32(l,t);
  487. blk[2]:= blk[2] - l;
  488. blk[1]:= blk[1] - m;
  489. blk[0]:= blk[0] xor r;
  490. blk[2]:= RRot32(blk[2],13);
  491. m:= blk[2] + KeyData[32];
  492. r:= LRot32(blk[2],13) * KeyData[33];
  493. l:= s_box[m and $1FF]; r:= LRot32(r,5);
  494. t:= r and $1f; m:= LRot32(m,t);
  495. l:= l xor r; r:= LRot32(r,5); l:= l xor r;
  496. t:= r and $1f; l:= LRot32(l,t);
  497. blk[1]:= blk[1] - l;
  498. blk[0]:= blk[0] - m;
  499. blk[3]:= blk[3] xor r;
  500. blk[1]:= RRot32(blk[1],13);
  501. m:= blk[1] + KeyData[30];
  502. r:= LRot32(blk[1],13) * KeyData[31];
  503. l:= s_box[m and $1FF]; r:= LRot32(r,5);
  504. t:= r and $1f; m:= LRot32(m,t);
  505. l:= l xor r; r:= LRot32(r,5); l:= l xor r;
  506. t:= r and $1f; l:= LRot32(l,t);
  507. blk[0]:= blk[0] - l;
  508. blk[3]:= blk[3] - m;
  509. blk[2]:= blk[2] xor r;
  510. blk[0]:= RRot32(blk[0],13);
  511. m:= blk[0] + KeyData[28];
  512. r:= LRot32(blk[0],13) * KeyData[29];
  513. l:= s_box[m and $1FF]; r:= LRot32(r,5);
  514. t:= r and $1f; m:= LRot32(m,t);
  515. l:= l xor r; r:= LRot32(r,5); l:= l xor r;
  516. t:= r and $1f; l:= LRot32(l,t);
  517. blk[3]:= blk[3] - l;
  518. blk[2]:= blk[2] - m;
  519. blk[1]:= blk[1] xor r;
  520. blk[3]:= RRot32(blk[3],13);
  521. m:= blk[3] + KeyData[26];
  522. r:= LRot32(blk[3],13) * KeyData[27];
  523. l:= s_box[m and $1FF]; r:= LRot32(r,5);
  524. t:= r and $1f; m:= LRot32(m,t);
  525. l:= l xor r; r:= LRot32(r,5); l:= l xor r;
  526. t:= r and $1f; l:= LRot32(l,t);
  527. blk[2]:= blk[2] - l;
  528. blk[1]:= blk[1] - m;
  529. blk[0]:= blk[0] xor r;
  530. blk[2]:= RRot32(blk[2],13);
  531. m:= blk[2] + KeyData[24];
  532. r:= LRot32(blk[2],13) * KeyData[25];
  533. l:= s_box[m and $1FF]; r:= LRot32(r,5);
  534. t:= r and $1f; m:= LRot32(m,t);
  535. l:= l xor r; r:= LRot32(r,5); l:= l xor r;
  536. t:= r and $1f; l:= LRot32(l,t);
  537. blk[1]:= blk[1] - l;
  538. blk[0]:= blk[0] - m;
  539. blk[3]:= blk[3] xor r;
  540. blk[1]:= RRot32(blk[1],13);
  541. m:= blk[1] + KeyData[22];
  542. r:= LRot32(blk[1],13) * KeyData[23];
  543. l:= s_box[m and $1FF]; r:= LRot32(r,5);
  544. t:= r and $1f; m:= LRot32(m,t);
  545. l:= l xor r; r:= LRot32(r,5); l:= l xor r;
  546. t:= r and $1f; l:= LRot32(l,t);
  547. blk[0]:= blk[0] - l;
  548. blk[3]:= blk[3] - m;
  549. blk[2]:= blk[2] xor r;
  550. blk[0]:= RRot32(blk[0],13);
  551. m:= blk[0] + KeyData[20];
  552. r:= LRot32(blk[0],13) * KeyData[21];
  553. l:= s_box[m and $1FF]; r:= LRot32(r,5);
  554. t:= r and $1f; m:= LRot32(m,t);
  555. l:= l xor r; r:= LRot32(r,5); l:= l xor r;
  556. t:= r and $1f; l:= LRot32(l,t);
  557. blk[3]:= blk[3] - l;
  558. blk[2]:= blk[2] - m;
  559. blk[1]:= blk[1] xor r;
  560. blk[3]:= RRot32(blk[3],13);
  561. m:= blk[3] + KeyData[18];
  562. r:= LRot32(blk[3],13) * KeyData[19];
  563. l:= s_box[m and $1FF]; r:= LRot32(r,5);
  564. t:= r and $1f; m:= LRot32(m,t);
  565. l:= l xor r; r:= LRot32(r,5); l:= l xor r;
  566. t:= r and $1f; l:= LRot32(l,t);
  567. blk[0]:= blk[0] - l;
  568. blk[1]:= blk[1] - m;
  569. blk[2]:= blk[2] xor r;
  570. blk[2]:= RRot32(blk[2],13);
  571. m:= blk[2] + KeyData[16];
  572. r:= LRot32(blk[2],13) * KeyData[17];
  573. l:= s_box[m and $1FF]; r:= LRot32(r,5);
  574. t:= r and $1f; m:= LRot32(m,t);
  575. l:= l xor r; r:= LRot32(r,5); l:= l xor r;
  576. t:= r and $1f; l:= LRot32(l,t);
  577. blk[3]:= blk[3] - l;
  578. blk[0]:= blk[0] - m;
  579. blk[1]:= blk[1] xor r;
  580. blk[1]:= RRot32(blk[1],13);
  581. m:= blk[1] + KeyData[14];
  582. r:= LRot32(blk[1],13) * KeyData[15];
  583. l:= s_box[m and $1FF]; r:= LRot32(r,5);
  584. t:= r and $1f; m:= LRot32(m,t);
  585. l:= l xor r; r:= LRot32(r,5); l:= l xor r;
  586. t:= r and $1f; l:= LRot32(l,t);
  587. blk[2]:= blk[2] - l;
  588. blk[3]:= blk[3] - m;
  589. blk[0]:= blk[0] xor r;
  590. blk[0]:= RRot32(blk[0],13);
  591. m:= blk[0] + KeyData[12];
  592. r:= LRot32(blk[0],13) * KeyData[13];
  593. l:= s_box[m and $1FF]; r:= LRot32(r,5);
  594. t:= r and $1f; m:= LRot32(m,t);
  595. l:= l xor r; r:= LRot32(r,5); l:= l xor r;
  596. t:= r and $1f; l:= LRot32(l,t);
  597. blk[1]:= blk[1] - l;
  598. blk[2]:= blk[2] - m;
  599. blk[3]:= blk[3] xor r;
  600. blk[3]:= RRot32(blk[3],13);
  601. m:= blk[3] + KeyData[10];
  602. r:= LRot32(blk[3],13) * KeyData[11];
  603. l:= s_box[m and $1FF]; r:= LRot32(r,5);
  604. t:= r and $1f; m:= LRot32(m,t);
  605. l:= l xor r; r:= LRot32(r,5); l:= l xor r;
  606. t:= r and $1f; l:= LRot32(l,t);
  607. blk[0]:= blk[0] - l;
  608. blk[1]:= blk[1] - m;
  609. blk[2]:= blk[2] xor r;
  610. blk[2]:= RRot32(blk[2],13);
  611. m:= blk[2] + KeyData[8];
  612. r:= LRot32(blk[2],13) * KeyData[9];
  613. l:= s_box[m and $1FF]; r:= LRot32(r,5);
  614. t:= r and $1f; m:= LRot32(m,t);
  615. l:= l xor r; r:= LRot32(r,5); l:= l xor r;
  616. t:= r and $1f; l:= LRot32(l,t);
  617. blk[3]:= blk[3] - l;
  618. blk[0]:= blk[0] - m;
  619. blk[1]:= blk[1] xor r;
  620. blk[1]:= RRot32(blk[1],13);
  621. m:= blk[1] + KeyData[6];
  622. r:= LRot32(blk[1],13) * KeyData[7];
  623. l:= s_box[m and $1FF]; r:= LRot32(r,5);
  624. t:= r and $1f; m:= LRot32(m,t);
  625. l:= l xor r; r:= LRot32(r,5); l:= l xor r;
  626. t:= r and $1f; l:= LRot32(l,t);
  627. blk[2]:= blk[2] - l;
  628. blk[3]:= blk[3] - m;
  629. blk[0]:= blk[0] xor r;
  630. blk[0]:= RRot32(blk[0],13);
  631. m:= blk[0] + KeyData[4];
  632. r:= LRot32(blk[0],13) * KeyData[5];
  633. l:= s_box[m and $1FF]; r:= LRot32(r,5);
  634. t:= r and $1f; m:= LRot32(m,t);
  635. l:= l xor r; r:= LRot32(r,5); l:= l xor r;
  636. t:= r and $1f; l:= LRot32(l,t);
  637. blk[1]:= blk[1] - l;
  638. blk[2]:= blk[2] - m;
  639. blk[3]:= blk[3] xor r;
  640. blk[3]:= LRot32(blk[3], 24);
  641. blk[2]:= blk[2] xor s_box[((blk[3] shr 24) and $FF) + 256];
  642. blk[1]:= blk[1] - s_box[ (blk[3] shr 16) and $FF];
  643. blk[0]:= blk[0] - s_box[((blk[3] shr 8) and $FF) + 256];
  644. blk[0]:= blk[0] xor s_box[ blk[3] and $FF];
  645. blk[2]:= LRot32(blk[2], 24);
  646. blk[1]:= blk[1] xor s_box[((blk[2] shr 24) and $FF) + 256];
  647. blk[0]:= blk[0] - s_box[ (blk[2] shr 16) and $FF];
  648. blk[3]:= blk[3] - s_box[((blk[2] shr 8) and $FF) + 256];
  649. blk[3]:= blk[3] xor s_box[ blk[2] and $FF];
  650. blk[1]:= blk[1] - blk[2]; blk[1]:= LRot32(blk[1], 24);
  651. blk[0]:= blk[0] xor s_box[((blk[1] shr 24) and $FF) + 256];
  652. blk[3]:= blk[3] - s_box[ (blk[1] shr 16) and $FF];
  653. blk[2]:= blk[2] - s_box[((blk[1] shr 8) and $FF) + 256];
  654. blk[2]:= blk[2] xor s_box[ blk[1] and $FF];
  655. blk[0]:= blk[0] - blk[3]; blk[0]:= LRot32(blk[0], 24);
  656. blk[3]:= blk[3] xor s_box[((blk[0] shr 24) and $FF) + 256];
  657. blk[2]:= blk[2] - s_box[ (blk[0] shr 16) and $FF];
  658. blk[1]:= blk[1] - s_box[((blk[0] shr 8) and $FF) + 256];
  659. blk[1]:= blk[1] xor s_box[ blk[0] and $FF];
  660. blk[3]:= LRot32(blk[3], 24);
  661. blk[2]:= blk[2] xor s_box[((blk[3] shr 24) and $FF) + 256];
  662. blk[1]:= blk[1] - s_box[ (blk[3] shr 16) and $FF];
  663. blk[0]:= blk[0] - s_box[((blk[3] shr 8) and $FF) + 256];
  664. blk[0]:= blk[0] xor s_box[ blk[3] and $FF];
  665. blk[2]:= LRot32(blk[2], 24);
  666. blk[1]:= blk[1] xor s_box[((blk[2] shr 24) and $FF) + 256];
  667. blk[0]:= blk[0] - s_box[ (blk[2] shr 16) and $FF];
  668. blk[3]:= blk[3] - s_box[((blk[2] shr 8) and $FF) + 256];
  669. blk[3]:= blk[3] xor s_box[ blk[2] and $FF];
  670. blk[1]:= blk[1] - blk[2]; blk[1]:= LRot32(blk[1], 24);
  671. blk[0]:= blk[0] xor s_box[((blk[1] shr 24) and $FF) + 256];
  672. blk[3]:= blk[3] - s_box[ (blk[1] shr 16) and $FF];
  673. blk[2]:= blk[2] - s_box[((blk[1] shr 8) and $FF) + 256];
  674. blk[2]:= blk[2] xor s_box[ blk[1] and $FF];
  675. blk[0]:= blk[0] - blk[3]; blk[0]:= LRot32(blk[0], 24);
  676. blk[3]:= blk[3] xor s_box[((blk[0] shr 24) and $FF) + 256];
  677. blk[2]:= blk[2] - s_box[ (blk[0] shr 16) and $FF];
  678. blk[1]:= blk[1] - s_box[((blk[0] shr 8) and $FF) + 256];
  679. blk[1]:= blk[1] xor s_box[ blk[0] and $FF];
  680. blk[0]:= blk[0] - KeyData[0]; blk[1]:= blk[1] - KeyData[1];
  681. blk[2]:= blk[2] - KeyData[2]; blk[3]:= blk[3] - KeyData[3];
  682. PDWord(@OutData)^:= Blk[0];
  683. PDWord(longword(@OutData)+4)^:= Blk[1];
  684. PDWord(longword(@OutData)+8)^:= Blk[2];
  685. PDWord(longword(@OutData)+12)^:= Blk[3];
  686. end;
  687. end.