DesEdeParameters.cs 3.7 KB

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