DCPrc6.pas 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. {******************************************************************************}
  2. {* DCPcrypt v2.0 written by David Barton (crypto@cityinthesky.co.uk) **********}
  3. {******************************************************************************}
  4. {* A binary compatible implementation of RC6 **********************************}
  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 DCPrc6;
  26. interface
  27. uses
  28. Classes, Sysutils, DCPcrypt2, DCPconst, DCPblockciphers;
  29. const
  30. NUMROUNDS= 20; { number of rounds must be between 16-24 }
  31. type
  32. TDCP_rc6= class(TDCP_blockcipher128)
  33. protected
  34. KeyData: array[0..((NUMROUNDS*2)+3)] of DWord;
  35. procedure InitKey(const Key; Size: longword); override;
  36. public
  37. class function GetId: integer; override;
  38. class function GetAlgorithm: string; override;
  39. class function GetMaxKeySize: integer; override;
  40. class function SelfTest: boolean; override;
  41. procedure Burn; override;
  42. procedure EncryptECB(const InData; var OutData); override;
  43. procedure DecryptECB(const InData; var OutData); override;
  44. end;
  45. {******************************************************************************}
  46. {******************************************************************************}
  47. implementation
  48. {$R-}{$Q-}
  49. const
  50. sBox: array[0..51] of DWord= (
  51. $B7E15163,$5618CB1C,$F45044D5,$9287BE8E,$30BF3847,$CEF6B200,
  52. $6D2E2BB9,$0B65A572,$A99D1F2B,$47D498E4,$E60C129D,$84438C56,
  53. $227B060F,$C0B27FC8,$5EE9F981,$FD21733A,$9B58ECF3,$399066AC,
  54. $D7C7E065,$75FF5A1E,$1436D3D7,$B26E4D90,$50A5C749,$EEDD4102,
  55. $8D14BABB,$2B4C3474,$C983AE2D,$67BB27E6,$05F2A19F,$A42A1B58,
  56. $42619511,$E0990ECA,$7ED08883,$1D08023C,$BB3F7BF5,$5976F5AE,
  57. $F7AE6F67,$95E5E920,$341D62D9,$D254DC92,$708C564B,$0EC3D004,
  58. $ACFB49BD,$4B32C376,$E96A3D2F,$87A1B6E8,$25D930A1,$C410AA5A,
  59. $62482413,$007F9DCC,$9EB71785,$3CEE913E);
  60. function LRot32(X: DWord; c: longword): DWord;
  61. begin
  62. LRot32:= (X shl c) or (X shr (32 - c));
  63. end;
  64. function RRot32(X: DWord; c: longword): DWord;
  65. begin
  66. RRot32:= (X shr c) or (X shl (32 - c));
  67. end;
  68. class function TDCP_rc6.GetID: integer;
  69. begin
  70. Result:= DCP_rc6;
  71. end;
  72. class function TDCP_rc6.GetAlgorithm: string;
  73. begin
  74. Result:= 'RC6';
  75. end;
  76. class function TDCP_rc6.GetMaxKeySize: integer;
  77. begin
  78. Result:= 2048;
  79. end;
  80. class function TDCP_rc6.SelfTest: boolean;
  81. const
  82. Key1: array[0..15] of byte=
  83. ($01,$23,$45,$67,$89,$ab,$cd,$ef,$01,$12,$23,$34,$45,$56,$67,$78);
  84. Plain1: array[0..15] of byte=
  85. ($02,$13,$24,$35,$46,$57,$68,$79,$8a,$9b,$ac,$bd,$ce,$df,$e0,$f1);
  86. Cipher1: array[0..15] of byte=
  87. ($52,$4e,$19,$2f,$47,$15,$c6,$23,$1f,$51,$f6,$36,$7e,$a4,$3f,$18);
  88. Key2: array[0..31] of byte=
  89. ($01,$23,$45,$67,$89,$ab,$cd,$ef,$01,$12,$23,$34,$45,$56,$67,$78,
  90. $89,$9a,$ab,$bc,$cd,$de,$ef,$f0,$10,$32,$54,$76,$98,$ba,$dc,$fe);
  91. Plain2: array[0..15] of byte=
  92. ($02,$13,$24,$35,$46,$57,$68,$79,$8a,$9b,$ac,$bd,$ce,$df,$e0,$f1);
  93. Cipher2: array[0..15] of byte=
  94. ($c8,$24,$18,$16,$f0,$d7,$e4,$89,$20,$ad,$16,$a1,$67,$4e,$5d,$48);
  95. var
  96. Cipher: TDCP_rc6;
  97. Data: array[0..15] of byte;
  98. begin
  99. Cipher:= TDCP_rc6.Create(nil);
  100. Cipher.Init(Key1,Sizeof(Key1)*8,nil);
  101. Cipher.EncryptECB(Plain1,Data);
  102. Result:= boolean(CompareMem(@Data,@Cipher1,Sizeof(Data)));
  103. Cipher.DecryptECB(Data,Data);
  104. Result:= Result and boolean(CompareMem(@Data,@Plain1,Sizeof(Data)));
  105. Cipher.Burn;
  106. Cipher.Init(Key2,Sizeof(Key2)*8,nil);
  107. Cipher.EncryptECB(Plain2,Data);
  108. Result:= Result and boolean(CompareMem(@Data,@Cipher2,Sizeof(Data)));
  109. Cipher.DecryptECB(Data,Data);
  110. Result:= Result and boolean(CompareMem(@Data,@Plain2,Sizeof(Data)));
  111. Cipher.Burn;
  112. Cipher.Free;
  113. end;
  114. procedure TDCP_rc6.InitKey(const Key; Size: longword);
  115. var
  116. xKeyD: array[0..63] of DWord;
  117. i, j, k, xKeyLen: longword;
  118. A, B: DWord;
  119. begin
  120. Size:= Size div 8;
  121. FillChar(xKeyD,Sizeof(xKeyD),0);
  122. Move(Key,xKeyD,Size);
  123. xKeyLen:= Size div 4;
  124. if (Size mod 4)<> 0 then
  125. Inc(xKeyLen);
  126. Move(sBox,KeyData,((NUMROUNDS*2)+4)*4);
  127. i:= 0; j:= 0;
  128. A:= 0; B:= 0;
  129. if xKeyLen> ((NUMROUNDS*2)+4) then
  130. k:= xKeyLen*3
  131. else
  132. k:= ((NUMROUNDS*2)+4)*3;
  133. for k:= 1 to k do
  134. begin
  135. A:= LRot32(KeyData[i]+A+B,3);
  136. KeyData[i]:= A;
  137. B:= LRot32(xKeyD[j]+A+B,A+B);
  138. xKeyD[j]:= B;
  139. i:= (i+1) mod ((NUMROUNDS*2)+4);
  140. j:= (j+1) mod xKeyLen;
  141. end;
  142. FillChar(xKeyD,Sizeof(xKeyD),0);
  143. end;
  144. procedure TDCP_rc6.Burn;
  145. begin
  146. FillChar(KeyData,Sizeof(KeyData),$FF);
  147. inherited Burn;
  148. end;
  149. procedure TDCP_rc6.EncryptECB(const InData; var OutData);
  150. var
  151. x0, x1, x2, x3: DWord;
  152. u, t: DWord;
  153. i: longword;
  154. begin
  155. if not fInitialized then
  156. raise EDCP_blockcipher.Create('Cipher not initialized');
  157. x0:= PDword(@InData)^;
  158. x1:= PDword(longword(@InData)+4)^;
  159. x2:= PDword(longword(@InData)+8)^;
  160. x3:= PDword(longword(@InData)+12)^;
  161. x1:= x1 + KeyData[0];
  162. x3:= x3 + KeyData[1];
  163. for i:= 1 to NUMROUNDS do
  164. begin
  165. t:= Lrot32(x1 * (2*x1 + 1),5);
  166. u:= Lrot32(x3 * (2*x3 + 1),5);
  167. x0:= Lrot32(x0 xor t,u) + KeyData[2*i];
  168. x2:= Lrot32(x2 xor u,t) + KeyData[2*i+1];
  169. t:= x0; x0:= x1; x1:= x2; x2:= x3; x3:= t;
  170. end;
  171. x0:= x0 + KeyData[(2*NUMROUNDS)+2];
  172. x2:= x2 + KeyData[(2*NUMROUNDS)+3];
  173. PDword(@OutData)^:= x0;
  174. PDword(longword(@OutData)+4)^:= x1;
  175. PDword(longword(@OutData)+8)^:= x2;
  176. PDword(longword(@OutData)+12)^:= x3;
  177. end;
  178. procedure TDCP_rc6.DecryptECB(const InData; var OutData);
  179. var
  180. x0, x1, x2, x3: DWord;
  181. u, t: DWord;
  182. i: longword;
  183. begin
  184. if not fInitialized then
  185. raise EDCP_blockcipher.Create('Cipher not initialized');
  186. x0:= PDword(@InData)^;
  187. x1:= PDword(longword(@InData)+4)^;
  188. x2:= PDword(longword(@InData)+8)^;
  189. x3:= PDword(longword(@InData)+12)^;
  190. x2:= x2 - KeyData[(2*NUMROUNDS)+3];
  191. x0:= x0 - KeyData[(2*NUMROUNDS)+2];
  192. for i:= NUMROUNDS downto 1 do
  193. begin
  194. t:= x0; x0:= x3; x3:= x2; x2:= x1; x1:= t;
  195. u:= Lrot32(x3 * (2*x3 + 1),5);
  196. t:= Lrot32(x1 * (2*x1 + 1),5);
  197. x2:= Rrot32(x2 - KeyData[2*i+1],t) xor u;
  198. x0:= Rrot32(x0 - KeyData[2*i],u) xor t;
  199. end;
  200. x3:= x3 - KeyData[1];
  201. x1:= x1 - KeyData[0];
  202. PDword(@OutData)^:= x0;
  203. PDword(longword(@OutData)+4)^:= x1;
  204. PDword(longword(@OutData)+8)^:= x2;
  205. PDword(longword(@OutData)+12)^:= x3;
  206. end;
  207. end.