DCPrc5.pas 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. {******************************************************************************}
  2. {* DCPcrypt v2.0 written by David Barton (crypto@cityinthesky.co.uk) **********}
  3. {******************************************************************************}
  4. {* A binary compatible implementation of RC5 **********************************}
  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 DCPrc5;
  26. interface
  27. uses
  28. Classes, Sysutils, DCPcrypt2, DCPconst, DCPblockciphers;
  29. const
  30. NUMROUNDS= 12; { number of rounds must be between 12-16 }
  31. type
  32. TDCP_rc5= class(TDCP_blockcipher64)
  33. protected
  34. KeyData: array[0..((NUMROUNDS*2)+1)] 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..33] 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);
  57. function LRot32(a, b: longword): longword;
  58. begin
  59. Result:= (a shl b) or (a shr (32-b));
  60. end;
  61. function RRot32(a, b: longword): longword;
  62. begin
  63. Result:= (a shr b) or (a shl (32-b));
  64. end;
  65. class function TDCP_rc5.GetID: integer;
  66. begin
  67. Result:= DCP_rc5;
  68. end;
  69. class function TDCP_rc5.GetAlgorithm: string;
  70. begin
  71. Result:= 'RC5';
  72. end;
  73. class function TDCP_rc5.GetMaxKeySize: integer;
  74. begin
  75. Result:= 2048;
  76. end;
  77. class function TDCP_rc5.SelfTest: boolean;
  78. const
  79. Key1: array[0..15] of byte=
  80. ($DC,$49,$DB,$13,$75,$A5,$58,$4F,$64,$85,$B4,$13,$B5,$F1,$2B,$AF);
  81. Plain1: array[0..1] of dword=
  82. ($B7B3422F,$92FC6903);
  83. Cipher1: array[0..1] of dword=
  84. ($B278C165,$CC97D184);
  85. Key2: array[0..15] of byte=
  86. ($52,$69,$F1,$49,$D4,$1B,$A0,$15,$24,$97,$57,$4D,$7F,$15,$31,$25);
  87. Plain2: array[0..1] of dword=
  88. ($B278C165,$CC97D184);
  89. Cipher2: array[0..1] of dword=
  90. ($15E444EB,$249831DA);
  91. var
  92. Cipher: TDCP_rc5;
  93. Data: array[0..1] of dword;
  94. begin
  95. Cipher:= TDCP_rc5.Create(nil);
  96. Cipher.Init(Key1,Sizeof(Key1)*8,nil);
  97. Cipher.EncryptECB(Plain1,Data);
  98. Result:= boolean(CompareMem(@Data,@Cipher1,Sizeof(Data)));
  99. Cipher.DecryptECB(Data,Data);
  100. Result:= Result and boolean(CompareMem(@Data,@Plain1,Sizeof(Data)));
  101. Cipher.Burn;
  102. Cipher.Init(Key2,Sizeof(Key2)*8,nil);
  103. Cipher.EncryptECB(Plain2,Data);
  104. Result:= Result and boolean(CompareMem(@Data,@Cipher2,Sizeof(Data)));
  105. Cipher.DecryptECB(Data,Data);
  106. Result:= Result and boolean(CompareMem(@Data,@Plain2,Sizeof(Data)));
  107. Cipher.Burn;
  108. Cipher.Free;
  109. end;
  110. procedure TDCP_rc5.InitKey(const Key; Size: longword);
  111. var
  112. xKeyD: array[0..63] of DWord;
  113. i, j, k, xKeyLen: longword;
  114. A, B: DWord;
  115. begin
  116. FillChar(xKeyD,Sizeof(xKeyD),0);
  117. Size:= Size div 8;
  118. Move(Key,xKeyD,Size);
  119. xKeyLen:= Size div 4;
  120. if (Size mod 4)<> 0 then
  121. Inc(xKeyLen);
  122. Move(sBox,KeyData,(NUMROUNDS+1)*8);
  123. i:= 0; j:= 0;
  124. A:= 0; B:= 0;
  125. if xKeyLen> ((NUMROUNDS+1)*2) then
  126. k:= xKeyLen*3
  127. else
  128. k:= (NUMROUNDS+1)*6;
  129. for k:= k downto 1 do
  130. begin
  131. A:= LRot32(KeyData[i]+A+B,3);
  132. KeyData[i]:= A;
  133. B:= LRot32(xKeyD[j]+A+B,A+B);
  134. xKeyD[j]:= B;
  135. i:= (i+1) mod ((NUMROUNDS+1)*2);
  136. j:= (j+1) mod xKeyLen;
  137. end;
  138. FillChar(xKeyD,Sizeof(xKeyD),0);
  139. end;
  140. procedure TDCP_rc5.Burn;
  141. begin
  142. FillChar(KeyData,Sizeof(KeyData),$FF);
  143. inherited Burn;
  144. end;
  145. procedure TDCP_rc5.EncryptECB(const InData; var OutData);
  146. var
  147. A, B: DWord;
  148. i: longword;
  149. begin
  150. if not fInitialized then
  151. raise EDCP_blockcipher.Create('Cipher not initialized');
  152. A:= PDword(@InData)^ + KeyData[0];
  153. B:= PDword(longword(@InData)+4)^ + KeyData[1];
  154. for i:= 1 to NUMROUNDS do
  155. begin
  156. A:= A xor B;
  157. A:= LRot32(A,B)+KeyData[2*i];
  158. B:= B xor A;
  159. B:= LRot32(B,A)+KeyData[(2*i)+1];
  160. end;
  161. PDword(@OutData)^:= A;
  162. PDword(longword(@OutData)+4)^:= B;
  163. end;
  164. procedure TDCP_rc5.DecryptECB(const InData; var OutData);
  165. var
  166. A, B: DWord;
  167. i: longword;
  168. begin
  169. if not fInitialized then
  170. raise EDCP_blockcipher.Create('Cipher not initialized');
  171. A:= PDword(@InData)^;
  172. B:= PDword(longword(@InData)+4)^;
  173. for i:= NUMROUNDS downto 1 do
  174. begin
  175. B:= RRot32(B-KeyData[(2*i)+1],A);
  176. B:= B xor A;
  177. A:= RRot32(A-KeyData[2*i],B);
  178. A:= A xor B;
  179. end;
  180. PDword(@OutData)^:= A - KeyData[0];
  181. PDword(longword(@OutData)+4)^:= B - KeyData[1];
  182. end;
  183. end.