DesEdeEngine.cs 3.2 KB

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