DesParameters.cs 4.7 KB

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