DESKeySpec.java 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /* DESKeySpec -- Keys for DES.
  2. Copyright (C) 2004 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package javax.crypto.spec;
  32. import java.security.InvalidKeyException;
  33. import java.security.spec.KeySpec;
  34. /**
  35. * This class is a transparent wrapper for DES keys, which are arrays
  36. * of 8 bytes.
  37. *
  38. * @author Casey Marshall (csm@gnu.org)
  39. * @since 1.4
  40. */
  41. public class DESKeySpec implements KeySpec
  42. {
  43. // Constants.
  44. // ------------------------------------------------------------------------
  45. /**
  46. * The length of a DES key, in bytes.
  47. */
  48. public static final int DES_KEY_LEN = 8;
  49. /**
  50. * The key bytes.
  51. */
  52. private byte[] key;
  53. // Constructors.
  54. // ------------------------------------------------------------------------
  55. /**
  56. * Create a new DES key spec, copying the first 8 bytes from the
  57. * byte array.
  58. *
  59. * @param key The key bytes.
  60. * @throws java.security.InvalidKeyException If there are less than 8
  61. * bytes in the array.
  62. */
  63. public DESKeySpec(byte[] key) throws InvalidKeyException
  64. {
  65. this(key, 0);
  66. }
  67. /**
  68. * Create a new DES key spec, starting at <code>offset</code> in
  69. * the byte array. The first 8 bytes starting at <code>offset</code>
  70. * are copied.
  71. *
  72. * @param key The key bytes.
  73. * @param offset The offset into the byte array at which to begin.
  74. * @throws java.security.InvalidKeyException If there are less than 8
  75. * bytes starting at <code>offset</code>.
  76. */
  77. public DESKeySpec(byte[] key, int offset) throws InvalidKeyException
  78. {
  79. if (key.length - offset < DES_KEY_LEN)
  80. {
  81. throw new InvalidKeyException("DES keys must be 8 bytes long");
  82. }
  83. this.key = new byte[DES_KEY_LEN];
  84. System.arraycopy(key, offset, this.key, 0, DES_KEY_LEN);
  85. }
  86. // Class methods.
  87. // ------------------------------------------------------------------------
  88. /**
  89. * Returns whether or not the given key is <i>parity adjusted</i>;
  90. * i.e. every byte in the key has an odd number of "1" bits.
  91. *
  92. * @param key The key bytes, considered between <code>[offset,
  93. * offset+7]</code>
  94. * @param offset The offset into the byte array at which to begin.
  95. * @return True if all bytes have an odd number of "1" bits.
  96. * @throws java.security.InvalidKeyException If there are not enough
  97. * bytes in the array.
  98. */
  99. public static boolean isParityAdjusted(byte[] key, int offset)
  100. throws InvalidKeyException
  101. {
  102. if (key.length - offset < DES_KEY_LEN)
  103. {
  104. throw new InvalidKeyException("DES keys must be 8 bytes long");
  105. }
  106. boolean parity = false;
  107. boolean oddbits = false;
  108. for (int i = 0; i < DES_KEY_LEN; i++)
  109. {
  110. oddbits = false;
  111. for (int j = 0; j < 8; j++)
  112. {
  113. oddbits ^= (key[i+offset] & 1 << j) != 0;
  114. }
  115. parity &= oddbits;
  116. }
  117. return parity;
  118. }
  119. /**
  120. * One-half of the weak and semiweak DES keys (the other half are the
  121. * complements of these).
  122. */
  123. private static final byte[][] WEAK_KEYS = new byte[][] {
  124. { 0, 0, 0, 0, 0, 0, 0, 0 }, // 0000 0000 0000 0000
  125. { -1, -1, -1, -1, 0, 0, 0, 0 }, // ffff ffff 0000 0000
  126. { 1, 1, 1, 1, 1, 1, 1, 1 }, // 0101 0101 0101 0101
  127. { 31, 31, 31, 31, 14, 14, 14, 14 }, // 1f1f 1f1f 0e0e 0e0e
  128. { 1, -2, 1, -2, 1, -2, 1, -2 }, // 01fe 01fe 01fe 01fe
  129. { 31, -32, 31, -32, -32, 31, -32, 31 }, // 1fe0 1fe0 0e1f 0e1f
  130. { 1, -32, 1, -32, 1, -15, 1, -15 }, // 01e0 01e0 01f1 01f1
  131. { 31, -2, 31, -2, 14, -2, 14, -2 }, // 1ffe 1ffe 0efe 0efe
  132. { 1, 31, 1, 31, 1, 14, 1, 14 }, // 011f 011f 010e 010e
  133. { -32, -2, -32, -2, -15, -2, -15, -2 }, // e0fe e0fe f1fe f1fe
  134. };
  135. /**
  136. * Tests if the bytes between <code>[offset, offset+7]</code>
  137. * constitute a weak or semi-weak DES key.
  138. *
  139. * @param key The key bytes to check.
  140. * @param offset The offset in the byte array to start.
  141. * @return true If the key bytes are a weak key.
  142. */
  143. public static boolean isWeak(byte[] key, int offset)
  144. throws InvalidKeyException
  145. {
  146. if (key.length - offset < DES_KEY_LEN)
  147. {
  148. throw new InvalidKeyException("DES keys must be 8 bytes long");
  149. }
  150. for (int i = 0; i < WEAK_KEYS.length; i++)
  151. {
  152. if (equalsOrComplementEquals(key, offset, WEAK_KEYS[i]))
  153. {
  154. return true;
  155. }
  156. }
  157. return false;
  158. }
  159. /**
  160. * This method returns true if the first 8 bytes starting at
  161. * <code>off</code> in <code>a</code> equal the first 8 bytes in
  162. * <code>b</code>, or equal the <i>complement</i> of the first 8 bytes
  163. * in <code>b</code>.
  164. *
  165. * @param a The first byte array.
  166. * @param off The index into the first byte array.
  167. * @param b The second byte array.
  168. * @return <code>a == b || a == ~b</code>
  169. */
  170. private static boolean equalsOrComplementEquals(byte[] a, int off, byte[] b)
  171. {
  172. boolean result = true;
  173. for (int i = 0; i < DES_KEY_LEN; i++)
  174. {
  175. result &= a[off+i] == b[i];
  176. }
  177. if (result) return true;
  178. result = true;
  179. for (int i = 0; i < DES_KEY_LEN; i++)
  180. {
  181. result &= a[off+i] == (~b[i]);
  182. }
  183. return result;
  184. }
  185. // Instance methods.
  186. // ------------------------------------------------------------------------
  187. /**
  188. * Return the key as a byte array. This method does not copy the byte
  189. * array.
  190. *
  191. * @return The key bytes.
  192. */
  193. public byte[] getKey()
  194. {
  195. return key;
  196. }
  197. }