DCPrc2.pas 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. {******************************************************************************}
  2. {* DCPcrypt v2.0 written by David Barton (crypto@cityinthesky.co.uk) **********}
  3. {******************************************************************************}
  4. {* A binary compatible implementation of RC2 **********************************}
  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 DCPrc2;
  26. interface
  27. uses
  28. Classes, Sysutils, DCPcrypt2, DCPconst, DCPblockciphers;
  29. type
  30. TDCP_rc2= class(TDCP_blockcipher64)
  31. protected
  32. KeyData: array[0..63] of word;
  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 DCPrc2.inc}
  48. function LRot16(a, n: word): word;
  49. begin
  50. Result:= (a shl n) or (a shr (16-n));
  51. end;
  52. function RRot16(a, n: word): word;
  53. begin
  54. Result:= (a shr n) or (a shl (16-n));
  55. end;
  56. class function TDCP_rc2.GetMaxKeySize: integer;
  57. begin
  58. Result:= 1024;
  59. end;
  60. class function TDCP_rc2.GetID: integer;
  61. begin
  62. Result:= DCP_rc2;
  63. end;
  64. class function TDCP_rc2.GetAlgorithm: string;
  65. begin
  66. Result:= 'RC2';
  67. end;
  68. class function TDCP_rc2.SelfTest: boolean;
  69. const
  70. Key1: array[0..15] of byte=
  71. ($00,$01,$02,$03,$04,$05,$06,$07,$08,$09,$0A,$0B,$0C,$0D,$0E,$0F);
  72. InData1: array[0..7] of byte=
  73. ($00,$00,$00,$00,$00,$00,$00,$00);
  74. OutData1: array[0..7] of byte=
  75. ($50,$DC,$01,$62,$BD,$75,$7F,$31);
  76. Key2: array[0..15] of byte=
  77. ($00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$01);
  78. InData2: array[0..7] of byte=
  79. ($00,$00,$00,$00,$00,$00,$00,$00);
  80. OutData2: array[0..7] of byte=
  81. ($21,$82,$9C,$78,$A9,$F9,$C0,$74);
  82. var
  83. Cipher: TDCP_rc2;
  84. Data: array[0..7] of byte;
  85. begin
  86. Cipher:= TDCP_rc2.Create(nil);
  87. Cipher.Init(Key1,Sizeof(Key1)*8,nil);
  88. Cipher.EncryptECB(InData1,Data);
  89. Result:= boolean(CompareMem(@Data,@OutData1,Sizeof(Data)));
  90. Cipher.DecryptECB(Data,Data);
  91. Result:= boolean(CompareMem(@Data,@InData1,Sizeof(Data))) and Result;
  92. Cipher.Burn;
  93. Cipher.Init(Key2,Sizeof(Key2)*8,nil);
  94. Cipher.EncryptECB(InData2,Data);
  95. Result:= boolean(CompareMem(@Data,@OutData2,Sizeof(Data))) and Result;
  96. Cipher.DecryptECB(Data,Data);
  97. Result:= boolean(CompareMem(@Data,@InData2,Sizeof(Data))) and Result;
  98. Cipher.Burn;
  99. Cipher.Free;
  100. end;
  101. procedure TDCP_rc2.InitKey(const Key; Size: longword);
  102. var
  103. i: longword;
  104. KeyB: array[0..127] of byte;
  105. begin
  106. Move(Key,KeyB,Size div 8);
  107. for i:= (Size div 8) to 127 do
  108. KeyB[i]:= sBox[(KeyB[i-(Size div 8)]+KeyB[i-1]) and $FF];
  109. KeyB[0]:= sBox[KeyB[0]];
  110. Move(KeyB,KeyData,Sizeof(KeyData));
  111. end;
  112. procedure TDCP_rc2.Burn;
  113. begin
  114. FillChar(KeyData,Sizeof(KeyData),0);
  115. inherited Burn;
  116. end;
  117. procedure TDCP_rc2.EncryptECB(const InData; var OutData);
  118. var
  119. i, j: longword;
  120. w: array[0..3] of word;
  121. begin
  122. if not fInitialized then
  123. raise EDCP_blockcipher.Create('Cipher not initialized');
  124. Pdword(@w[0])^:= Pdword(@InData)^;
  125. Pdword(@w[2])^:= Pdword(longword(@InData)+4)^;
  126. for i:= 0 to 15 do
  127. begin
  128. j:= i*4;
  129. w[0]:= LRot16((w[0]+(w[1] and (not w[3]))+(w[2] and w[3])+KeyData[j+0]),1);
  130. w[1]:= LRot16((w[1]+(w[2] and (not w[0]))+(w[3] and w[0])+KeyData[j+1]),2);
  131. w[2]:= LRot16((w[2]+(w[3] and (not w[1]))+(w[0] and w[1])+KeyData[j+2]),3);
  132. w[3]:= LRot16((w[3]+(w[0] and (not w[2]))+(w[1] and w[2])+KeyData[j+3]),5);
  133. if (i= 4) or (i= 10) then
  134. begin
  135. w[0]:= w[0]+KeyData[w[3] and 63];
  136. w[1]:= w[1]+KeyData[w[0] and 63];
  137. w[2]:= w[2]+KeyData[w[1] and 63];
  138. w[3]:= w[3]+KeyData[w[2] and 63];
  139. end;
  140. end;
  141. Pdword(@OutData)^:= Pdword(@w[0])^;
  142. Pdword(longword(@OutData)+4)^:= Pdword(@w[2])^;
  143. end;
  144. procedure TDCP_rc2.DecryptECB(const InData; var OutData);
  145. var
  146. i, j: longword;
  147. w: array[0..3] of word;
  148. begin
  149. if not fInitialized then
  150. raise EDCP_blockcipher.Create('Cipher not initialized');
  151. Pdword(@w[0])^:= Pdword(@InData)^;
  152. Pdword(@w[2])^:= Pdword(longword(@InData)+4)^;
  153. for i:= 15 downto 0 do
  154. begin
  155. j:= i*4;
  156. w[3]:= RRot16(w[3],5)-(w[0] and (not w[2]))-(w[1] and w[2])-KeyData[j+3];
  157. w[2]:= RRot16(w[2],3)-(w[3] and (not w[1]))-(w[0] and w[1])-KeyData[j+2];
  158. w[1]:= RRot16(w[1],2)-(w[2] and (not w[0]))-(w[3] and w[0])-KeyData[j+1];
  159. w[0]:= RRot16(w[0],1)-(w[1] and (not w[3]))-(w[2] and w[3])-KeyData[j+0];
  160. if (i= 5) or (i= 11) then
  161. begin
  162. w[3]:= w[3]-KeyData[w[2] and 63];
  163. w[2]:= w[2]-KeyData[w[1] and 63];
  164. w[1]:= w[1]-KeyData[w[0] and 63];
  165. w[0]:= w[0]-KeyData[w[3] and 63];
  166. end;
  167. end;
  168. Pdword(@OutData)^:= Pdword(@w[0])^;
  169. Pdword(longword(@OutData)+4)^:= Pdword(@w[2])^;
  170. end;
  171. end.