DesEdeEngine.cs 3.4 KB

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