DesParameters.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 DesParameters
  7. : KeyParameter
  8. {
  9. public DesParameters(
  10. byte[] key)
  11. : base(key)
  12. {
  13. if (IsWeakKey(key))
  14. throw new ArgumentException("attempt to create weak DES key");
  15. }
  16. public DesParameters(
  17. byte[] key,
  18. int keyOff,
  19. int keyLen)
  20. : base(key, keyOff, keyLen)
  21. {
  22. if (IsWeakKey(key, keyOff))
  23. throw new ArgumentException("attempt to create weak DES key");
  24. }
  25. /*
  26. * DES Key Length in bytes.
  27. */
  28. public const int DesKeyLength = 8;
  29. /*
  30. * Table of weak and semi-weak keys taken from Schneier pp281
  31. */
  32. private const int N_DES_WEAK_KEYS = 16;
  33. private static readonly byte[] DES_weak_keys =
  34. {
  35. /* weak keys */
  36. (byte)0x01,(byte)0x01,(byte)0x01,(byte)0x01, (byte)0x01,(byte)0x01,(byte)0x01,(byte)0x01,
  37. (byte)0x1f,(byte)0x1f,(byte)0x1f,(byte)0x1f, (byte)0x0e,(byte)0x0e,(byte)0x0e,(byte)0x0e,
  38. (byte)0xe0,(byte)0xe0,(byte)0xe0,(byte)0xe0, (byte)0xf1,(byte)0xf1,(byte)0xf1,(byte)0xf1,
  39. (byte)0xfe,(byte)0xfe,(byte)0xfe,(byte)0xfe, (byte)0xfe,(byte)0xfe,(byte)0xfe,(byte)0xfe,
  40. /* semi-weak keys */
  41. (byte)0x01,(byte)0xfe,(byte)0x01,(byte)0xfe, (byte)0x01,(byte)0xfe,(byte)0x01,(byte)0xfe,
  42. (byte)0x1f,(byte)0xe0,(byte)0x1f,(byte)0xe0, (byte)0x0e,(byte)0xf1,(byte)0x0e,(byte)0xf1,
  43. (byte)0x01,(byte)0xe0,(byte)0x01,(byte)0xe0, (byte)0x01,(byte)0xf1,(byte)0x01,(byte)0xf1,
  44. (byte)0x1f,(byte)0xfe,(byte)0x1f,(byte)0xfe, (byte)0x0e,(byte)0xfe,(byte)0x0e,(byte)0xfe,
  45. (byte)0x01,(byte)0x1f,(byte)0x01,(byte)0x1f, (byte)0x01,(byte)0x0e,(byte)0x01,(byte)0x0e,
  46. (byte)0xe0,(byte)0xfe,(byte)0xe0,(byte)0xfe, (byte)0xf1,(byte)0xfe,(byte)0xf1,(byte)0xfe,
  47. (byte)0xfe,(byte)0x01,(byte)0xfe,(byte)0x01, (byte)0xfe,(byte)0x01,(byte)0xfe,(byte)0x01,
  48. (byte)0xe0,(byte)0x1f,(byte)0xe0,(byte)0x1f, (byte)0xf1,(byte)0x0e,(byte)0xf1,(byte)0x0e,
  49. (byte)0xe0,(byte)0x01,(byte)0xe0,(byte)0x01, (byte)0xf1,(byte)0x01,(byte)0xf1,(byte)0x01,
  50. (byte)0xfe,(byte)0x1f,(byte)0xfe,(byte)0x1f, (byte)0xfe,(byte)0x0e,(byte)0xfe,(byte)0x0e,
  51. (byte)0x1f,(byte)0x01,(byte)0x1f,(byte)0x01, (byte)0x0e,(byte)0x01,(byte)0x0e,(byte)0x01,
  52. (byte)0xfe,(byte)0xe0,(byte)0xfe,(byte)0xe0, (byte)0xfe,(byte)0xf1,(byte)0xfe,(byte)0xf1
  53. };
  54. /**
  55. * DES has 16 weak keys. This method will check
  56. * if the given DES key material is weak or semi-weak.
  57. * Key material that is too short is regarded as weak.
  58. * <p>
  59. * See <a href="http://www.counterpane.com/applied.html">"Applied
  60. * Cryptography"</a> by Bruce Schneier for more information.
  61. * </p>
  62. * @return true if the given DES key material is weak or semi-weak,
  63. * false otherwise.
  64. */
  65. public static bool IsWeakKey(
  66. byte[] key,
  67. int offset)
  68. {
  69. if (key.Length - offset < DesKeyLength)
  70. throw new ArgumentException("key material too short.");
  71. //nextkey:
  72. for (int i = 0; i < N_DES_WEAK_KEYS; i++)
  73. {
  74. bool unmatch = false;
  75. for (int j = 0; j < DesKeyLength; j++)
  76. {
  77. if (key[j + offset] != DES_weak_keys[i * DesKeyLength + j])
  78. {
  79. //continue nextkey;
  80. unmatch = true;
  81. break;
  82. }
  83. }
  84. if (!unmatch)
  85. {
  86. return true;
  87. }
  88. }
  89. return false;
  90. }
  91. public static bool IsWeakKey(
  92. byte[] key)
  93. {
  94. return IsWeakKey(key, 0);
  95. }
  96. public static byte SetOddParity(byte b)
  97. {
  98. uint parity = b ^ 1U;
  99. parity ^= (parity >> 4);
  100. parity ^= (parity >> 2);
  101. parity ^= (parity >> 1);
  102. parity &= 1U;
  103. return (byte)(b ^ parity);
  104. }
  105. /**
  106. * DES Keys use the LSB as the odd parity bit. This can
  107. * be used to check for corrupt keys.
  108. *
  109. * @param bytes the byte array to set the parity on.
  110. */
  111. public static void SetOddParity(byte[] bytes)
  112. {
  113. for (int i = 0; i < bytes.Length; i++)
  114. {
  115. bytes[i] = SetOddParity(bytes[i]);
  116. }
  117. }
  118. public static void SetOddParity(byte[] bytes, int off, int len)
  119. {
  120. for (int i = 0; i < len; i++)
  121. {
  122. bytes[off + i] = SetOddParity(bytes[off + i]);
  123. }
  124. }
  125. }
  126. }
  127. #pragma warning restore
  128. #endif