DesEdeParameters.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters
  5. {
  6. public class DesEdeParameters
  7. : DesParameters
  8. {
  9. /*
  10. * DES-EDE Key length in bytes.
  11. */
  12. public const int DesEdeKeyLength = 24;
  13. private static byte[] FixKey(
  14. byte[] key,
  15. int keyOff,
  16. int keyLen)
  17. {
  18. byte[] tmp = new byte[24];
  19. switch (keyLen)
  20. {
  21. case 16:
  22. Array.Copy(key, keyOff, tmp, 0, 16);
  23. Array.Copy(key, keyOff, tmp, 16, 8);
  24. break;
  25. case 24:
  26. Array.Copy(key, keyOff, tmp, 0, 24);
  27. break;
  28. default:
  29. throw new ArgumentException("Bad length for DESede key: " + keyLen, "keyLen");
  30. }
  31. if (IsWeakKey(tmp))
  32. throw new ArgumentException("attempt to create weak DESede key");
  33. return tmp;
  34. }
  35. public DesEdeParameters(
  36. byte[] key)
  37. : base(FixKey(key, 0, key.Length))
  38. {
  39. }
  40. public DesEdeParameters(
  41. byte[] key,
  42. int keyOff,
  43. int keyLen)
  44. : base(FixKey(key, keyOff, keyLen))
  45. {
  46. }
  47. /**
  48. * return true if the passed in key is a DES-EDE weak key.
  49. *
  50. * @param key bytes making up the key
  51. * @param offset offset into the byte array the key starts at
  52. * @param length number of bytes making up the key
  53. */
  54. public static bool IsWeakKey(
  55. byte[] key,
  56. int offset,
  57. int length)
  58. {
  59. for (int i = offset; i < length; i += DesKeyLength)
  60. {
  61. if (DesParameters.IsWeakKey(key, i))
  62. {
  63. return true;
  64. }
  65. }
  66. return false;
  67. }
  68. /**
  69. * return true if the passed in key is a DES-EDE weak key.
  70. *
  71. * @param key bytes making up the key
  72. * @param offset offset into the byte array the key starts at
  73. */
  74. public static new bool IsWeakKey(
  75. byte[] key,
  76. int offset)
  77. {
  78. return IsWeakKey(key, offset, key.Length - offset);
  79. }
  80. public static new bool IsWeakKey(
  81. byte[] key)
  82. {
  83. return IsWeakKey(key, 0, key.Length);
  84. }
  85. /**
  86. * return true if the passed in key is a real 2/3 part DES-EDE key.
  87. *
  88. * @param key bytes making up the key
  89. * @param offset offset into the byte array the key starts at
  90. */
  91. public static bool IsRealEdeKey(byte[] key, int offset)
  92. {
  93. return key.Length == 16 ? IsReal2Key(key, offset) : IsReal3Key(key, offset);
  94. }
  95. /**
  96. * return true if the passed in key is a real 2 part DES-EDE key.
  97. *
  98. * @param key bytes making up the key
  99. * @param offset offset into the byte array the key starts at
  100. */
  101. public static bool IsReal2Key(byte[] key, int offset)
  102. {
  103. bool isValid = false;
  104. for (int i = offset; i != offset + 8; i++)
  105. {
  106. isValid |= (key[i] != key[i + 8]);
  107. }
  108. return isValid;
  109. }
  110. /**
  111. * return true if the passed in key is a real 3 part DES-EDE key.
  112. *
  113. * @param key bytes making up the key
  114. * @param offset offset into the byte array the key starts at
  115. */
  116. public static bool IsReal3Key(byte[] key, int offset)
  117. {
  118. bool diff12 = false, diff13 = false, diff23 = false;
  119. for (int i = offset; i != offset + 8; i++)
  120. {
  121. diff12 |= (key[i] != key[i + 8]);
  122. diff13 |= (key[i] != key[i + 16]);
  123. diff23 |= (key[i + 8] != key[i + 16]);
  124. }
  125. return diff12 && diff13 && diff23;
  126. }
  127. }
  128. }
  129. #pragma warning restore
  130. #endif