DesEdeEngine.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  7. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Engines
  8. {
  9. /// <remarks>A class that provides a basic DESede (or Triple DES) engine.</remarks>
  10. public class DesEdeEngine
  11. : DesEngine
  12. {
  13. private int[] workingKey1, workingKey2, workingKey3;
  14. private bool forEncryption;
  15. /**
  16. * initialise a DESede cipher.
  17. *
  18. * @param forEncryption whether or not we are for encryption.
  19. * @param parameters the parameters required to set up the cipher.
  20. * @exception ArgumentException if the parameters argument is
  21. * inappropriate.
  22. */
  23. public override void Init(
  24. bool forEncryption,
  25. ICipherParameters parameters)
  26. {
  27. if (!(parameters is KeyParameter))
  28. throw new ArgumentException("invalid parameter passed to DESede init - " + Org.BouncyCastle.Utilities.Platform.GetTypeName(parameters));
  29. byte[] keyMaster = ((KeyParameter)parameters).GetKey();
  30. if (keyMaster.Length != 24 && keyMaster.Length != 16)
  31. throw new ArgumentException("key size must be 16 or 24 bytes.");
  32. this.forEncryption = forEncryption;
  33. byte[] key1 = new byte[8];
  34. Array.Copy(keyMaster, 0, key1, 0, key1.Length);
  35. workingKey1 = GenerateWorkingKey(forEncryption, key1);
  36. byte[] key2 = new byte[8];
  37. Array.Copy(keyMaster, 8, key2, 0, key2.Length);
  38. workingKey2 = GenerateWorkingKey(!forEncryption, key2);
  39. if (keyMaster.Length == 24)
  40. {
  41. byte[] key3 = new byte[8];
  42. Array.Copy(keyMaster, 16, key3, 0, key3.Length);
  43. workingKey3 = GenerateWorkingKey(forEncryption, key3);
  44. }
  45. else // 16 byte key
  46. {
  47. workingKey3 = workingKey1;
  48. }
  49. }
  50. public override string AlgorithmName
  51. {
  52. get { return "DESede"; }
  53. }
  54. public override int GetBlockSize()
  55. {
  56. return BLOCK_SIZE;
  57. }
  58. public override int ProcessBlock(byte[] input, int inOff, byte[] output, int outOff)
  59. {
  60. if (workingKey1 == null)
  61. throw new InvalidOperationException("DESede engine not initialised");
  62. Check.DataLength(input, inOff, BLOCK_SIZE, "input buffer too short");
  63. Check.OutputLength(output, outOff, BLOCK_SIZE, "output buffer too short");
  64. uint hi32 = Pack.BE_To_UInt32(input, inOff);
  65. uint lo32 = Pack.BE_To_UInt32(input, inOff + 4);
  66. if (forEncryption)
  67. {
  68. DesFunc(workingKey1, ref hi32, ref lo32);
  69. DesFunc(workingKey2, ref hi32, ref lo32);
  70. DesFunc(workingKey3, ref hi32, ref lo32);
  71. }
  72. else
  73. {
  74. DesFunc(workingKey3, ref hi32, ref lo32);
  75. DesFunc(workingKey2, ref hi32, ref lo32);
  76. DesFunc(workingKey1, ref hi32, ref lo32);
  77. }
  78. Pack.UInt32_To_BE(hi32, output, outOff);
  79. Pack.UInt32_To_BE(lo32, output, outOff + 4);
  80. return BLOCK_SIZE;
  81. }
  82. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  83. public override int ProcessBlock(ReadOnlySpan<byte> input, Span<byte> output)
  84. {
  85. if (workingKey1 == null)
  86. throw new InvalidOperationException("DESede engine not initialised");
  87. Check.DataLength(input, BLOCK_SIZE, "input buffer too short");
  88. Check.OutputLength(output, BLOCK_SIZE, "output buffer too short");
  89. uint hi32 = Pack.BE_To_UInt32(input);
  90. uint lo32 = Pack.BE_To_UInt32(input[4..]);
  91. if (forEncryption)
  92. {
  93. DesFunc(workingKey1, ref hi32, ref lo32);
  94. DesFunc(workingKey2, ref hi32, ref lo32);
  95. DesFunc(workingKey3, ref hi32, ref lo32);
  96. }
  97. else
  98. {
  99. DesFunc(workingKey3, ref hi32, ref lo32);
  100. DesFunc(workingKey2, ref hi32, ref lo32);
  101. DesFunc(workingKey1, ref hi32, ref lo32);
  102. }
  103. Pack.UInt32_To_BE(hi32, output);
  104. Pack.UInt32_To_BE(lo32, output[4..]);
  105. return BLOCK_SIZE;
  106. }
  107. #endif
  108. }
  109. }
  110. #pragma warning restore
  111. #endif