DCPtea.pas 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. {******************************************************************************}
  2. {* DCPcrypt v2.0 written by David Barton (crypto@cityinthesky.co.uk) **********}
  3. {******************************************************************************}
  4. {* A binary compatible implementation of Tea **********************************}
  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 DCPtea;
  26. interface
  27. uses
  28. Classes, Sysutils, DCPcrypt2, DCPconst, DCPblockciphers;
  29. type
  30. TDCP_tea= class(TDCP_blockcipher64)
  31. protected
  32. KeyData: array[0..3] 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. const
  48. Delta= $9e3779b9;
  49. Rounds= 32;
  50. function SwapDword(a: dword): dword;
  51. begin
  52. Result:= ((a and $FF) shl 24) or ((a and $FF00) shl 8) or ((a and $FF0000) shr 8) or ((a and $FF000000) shr 24);
  53. end;
  54. class function TDCP_tea.GetID: integer;
  55. begin
  56. Result:= DCP_tea;
  57. end;
  58. class function TDCP_tea.GetAlgorithm: string;
  59. begin
  60. Result:= 'Tea';
  61. end;
  62. class function TDCP_tea.GetMaxKeySize: integer;
  63. begin
  64. Result:= 128;
  65. end;
  66. class function TDCP_tea.SelfTest: boolean;
  67. const
  68. Key: array[0..3] of dword= ($12345678,$9ABCDEF0,$0FEDCBA9,$87654321);
  69. PT: array[0..1] of dword= ($12345678,$9ABCDEF0);
  70. var
  71. Data: array[0..1] of dword;
  72. Cipher: TDCP_tea;
  73. begin
  74. Cipher:= TDCP_tea.Create(nil);
  75. Cipher.Init(Key,Sizeof(Key)*8,nil);
  76. Cipher.EncryptECB(PT,Data);
  77. Result:= not CompareMem(@Data,@PT,Sizeof(PT));
  78. Cipher.DecryptECB(Data,Data);
  79. Result:= Result and CompareMem(@Data,@PT,Sizeof(PT));
  80. Cipher.Burn;
  81. Cipher.Free;
  82. end;
  83. procedure TDCP_tea.InitKey(const Key; Size: longword);
  84. begin
  85. FillChar(KeyData,Sizeof(KeyData),0);
  86. Move(Key,KeyData,Size div 8);
  87. KeyData[0]:= SwapDWord(KeyData[0]); KeyData[1]:= SwapDWord(KeyData[1]);
  88. KeyData[2]:= SwapDWord(KeyData[2]); KeyData[3]:= SwapDWord(KeyData[3]);
  89. end;
  90. procedure TDCP_tea.Burn;
  91. begin
  92. FillChar(KeyData,Sizeof(KeyData),0);
  93. inherited Burn;
  94. end;
  95. procedure TDCP_tea.EncryptECB(const InData; var OutData);
  96. var
  97. a, b, c, d, x, y, n, sum: dword;
  98. begin
  99. if not fInitialized then
  100. raise EDCP_blockcipher.Create('Cipher not initialized');
  101. x:= SwapDWord(pdword(@InData)^);
  102. y:= SwapDWord(pdword(longword(@InData)+4)^);
  103. sum:= 0; a:= KeyData[0]; b:= KeyData[1]; c:= KeyData[2]; d:= KeyData[3];
  104. for n:= 1 to Rounds do
  105. begin
  106. Inc(sum,Delta);
  107. Inc(x,(y shl 4) + (a xor y) + (sum xor (y shr 5)) + b);
  108. Inc(y,(x shl 4) + (c xor x) + (sum xor (x shr 5)) + d);
  109. end;
  110. pdword(@OutData)^:= SwapDWord(x);
  111. pdword(longword(@OutData)+4)^:= SwapDWord(y);
  112. end;
  113. procedure TDCP_tea.DecryptECB(const InData; var OutData);
  114. var
  115. a, b, c, d, x, y, n, sum: dword;
  116. begin
  117. if not fInitialized then
  118. raise EDCP_blockcipher.Create('Cipher not initialized');
  119. x:= SwapDWord(pdword(@InData)^);
  120. y:= SwapDWord(pdword(longword(@InData)+4)^);
  121. sum:= Delta shl 5; a:= KeyData[0]; b:= KeyData[1]; c:= KeyData[2]; d:= KeyData[3];
  122. for n:= 1 to Rounds do
  123. begin
  124. Dec(y,(x shl 4) + (c xor x) + (sum xor (x shr 5)) + d);
  125. Dec(x,(y shl 4) + (a xor y) + (sum xor (y shr 5)) + b);
  126. Dec(sum,Delta);
  127. end;
  128. pdword(@OutData)^:= SwapDWord(x);
  129. pdword(longword(@OutData)+4)^:= SwapDWord(y);
  130. end;
  131. end.