DES.java 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. /* DES.java --
  2. Copyright (C) 2002, 2003, 2006 Free Software Foundation, Inc.
  3. This file is a 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 of the License, or (at
  7. your option) 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; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
  15. 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 gnu.javax.crypto.cipher;
  32. import gnu.java.security.Registry;
  33. import gnu.java.security.Properties;
  34. import gnu.java.security.util.Util;
  35. import java.security.InvalidKeyException;
  36. import java.util.Arrays;
  37. import java.util.Collections;
  38. import java.util.Iterator;
  39. /**
  40. * The Data Encryption Standard. DES is a 64-bit block cipher with a 56-bit
  41. * key, developed by IBM in the 1970's for the standardization process begun by
  42. * the National Bureau of Standards (now NIST).
  43. * <p>
  44. * New applications should not use DES except for compatibility.
  45. * <p>
  46. * This version is based upon the description and sample implementation in
  47. * [1].
  48. * <p>
  49. * References:
  50. * <ol>
  51. * <li>Bruce Schneier, <i>Applied Cryptography: Protocols, Algorithms, and
  52. * Source Code in C, Second Edition</i>. (1996 John Wiley and Sons) ISBN
  53. * 0-471-11709-9. Pages 265--301, 623--632.</li>
  54. * </ol>
  55. */
  56. public class DES
  57. extends BaseCipher
  58. {
  59. /** DES operates on 64 bit blocks. */
  60. public static final int BLOCK_SIZE = 8;
  61. /** DES uses 56 bits of a 64 bit parity-adjusted key. */
  62. public static final int KEY_SIZE = 8;
  63. // S-Boxes 1 through 8.
  64. private static final int[] SP1 = new int[] {
  65. 0x01010400, 0x00000000, 0x00010000, 0x01010404, 0x01010004, 0x00010404,
  66. 0x00000004, 0x00010000, 0x00000400, 0x01010400, 0x01010404, 0x00000400,
  67. 0x01000404, 0x01010004, 0x01000000, 0x00000004, 0x00000404, 0x01000400,
  68. 0x01000400, 0x00010400, 0x00010400, 0x01010000, 0x01010000, 0x01000404,
  69. 0x00010004, 0x01000004, 0x01000004, 0x00010004, 0x00000000, 0x00000404,
  70. 0x00010404, 0x01000000, 0x00010000, 0x01010404, 0x00000004, 0x01010000,
  71. 0x01010400, 0x01000000, 0x01000000, 0x00000400, 0x01010004, 0x00010000,
  72. 0x00010400, 0x01000004, 0x00000400, 0x00000004, 0x01000404, 0x00010404,
  73. 0x01010404, 0x00010004, 0x01010000, 0x01000404, 0x01000004, 0x00000404,
  74. 0x00010404, 0x01010400, 0x00000404, 0x01000400, 0x01000400, 0x00000000,
  75. 0x00010004, 0x00010400, 0x00000000, 0x01010004 };
  76. private static final int[] SP2 = new int[] {
  77. 0x80108020, 0x80008000, 0x00008000, 0x00108020, 0x00100000, 0x00000020,
  78. 0x80100020, 0x80008020, 0x80000020, 0x80108020, 0x80108000, 0x80000000,
  79. 0x80008000, 0x00100000, 0x00000020, 0x80100020, 0x00108000, 0x00100020,
  80. 0x80008020, 0x00000000, 0x80000000, 0x00008000, 0x00108020, 0x80100000,
  81. 0x00100020, 0x80000020, 0x00000000, 0x00108000, 0x00008020, 0x80108000,
  82. 0x80100000, 0x00008020, 0x00000000, 0x00108020, 0x80100020, 0x00100000,
  83. 0x80008020, 0x80100000, 0x80108000, 0x00008000, 0x80100000, 0x80008000,
  84. 0x00000020, 0x80108020, 0x00108020, 0x00000020, 0x00008000, 0x80000000,
  85. 0x00008020, 0x80108000, 0x00100000, 0x80000020, 0x00100020, 0x80008020,
  86. 0x80000020, 0x00100020, 0x00108000, 0x00000000, 0x80008000, 0x00008020,
  87. 0x80000000, 0x80100020, 0x80108020, 0x00108000 };
  88. private static final int[] SP3 = new int[] {
  89. 0x00000208, 0x08020200, 0x00000000, 0x08020008, 0x08000200, 0x00000000,
  90. 0x00020208, 0x08000200, 0x00020008, 0x08000008, 0x08000008, 0x00020000,
  91. 0x08020208, 0x00020008, 0x08020000, 0x00000208, 0x08000000, 0x00000008,
  92. 0x08020200, 0x00000200, 0x00020200, 0x08020000, 0x08020008, 0x00020208,
  93. 0x08000208, 0x00020200, 0x00020000, 0x08000208, 0x00000008, 0x08020208,
  94. 0x00000200, 0x08000000, 0x08020200, 0x08000000, 0x00020008, 0x00000208,
  95. 0x00020000, 0x08020200, 0x08000200, 0x00000000, 0x00000200, 0x00020008,
  96. 0x08020208, 0x08000200, 0x08000008, 0x00000200, 0x00000000, 0x08020008,
  97. 0x08000208, 0x00020000, 0x08000000, 0x08020208, 0x00000008, 0x00020208,
  98. 0x00020200, 0x08000008, 0x08020000, 0x08000208, 0x00000208, 0x08020000,
  99. 0x00020208, 0x00000008, 0x08020008, 0x00020200 };
  100. private static final int[] SP4 = new int[] {
  101. 0x00802001, 0x00002081, 0x00002081, 0x00000080, 0x00802080, 0x00800081,
  102. 0x00800001, 0x00002001, 0x00000000, 0x00802000, 0x00802000, 0x00802081,
  103. 0x00000081, 0x00000000, 0x00800080, 0x00800001, 0x00000001, 0x00002000,
  104. 0x00800000, 0x00802001, 0x00000080, 0x00800000, 0x00002001, 0x00002080,
  105. 0x00800081, 0x00000001, 0x00002080, 0x00800080, 0x00002000, 0x00802080,
  106. 0x00802081, 0x00000081, 0x00800080, 0x00800001, 0x00802000, 0x00802081,
  107. 0x00000081, 0x00000000, 0x00000000, 0x00802000, 0x00002080, 0x00800080,
  108. 0x00800081, 0x00000001, 0x00802001, 0x00002081, 0x00002081, 0x00000080,
  109. 0x00802081, 0x00000081, 0x00000001, 0x00002000, 0x00800001, 0x00002001,
  110. 0x00802080, 0x00800081, 0x00002001, 0x00002080, 0x00800000, 0x00802001,
  111. 0x00000080, 0x00800000, 0x00002000, 0x00802080 };
  112. private static final int[] SP5 = new int[] {
  113. 0x00000100, 0x02080100, 0x02080000, 0x42000100, 0x00080000, 0x00000100,
  114. 0x40000000, 0x02080000, 0x40080100, 0x00080000, 0x02000100, 0x40080100,
  115. 0x42000100, 0x42080000, 0x00080100, 0x40000000, 0x02000000, 0x40080000,
  116. 0x40080000, 0x00000000, 0x40000100, 0x42080100, 0x42080100, 0x02000100,
  117. 0x42080000, 0x40000100, 0x00000000, 0x42000000, 0x02080100, 0x02000000,
  118. 0x42000000, 0x00080100, 0x00080000, 0x42000100, 0x00000100, 0x02000000,
  119. 0x40000000, 0x02080000, 0x42000100, 0x40080100, 0x02000100, 0x40000000,
  120. 0x42080000, 0x02080100, 0x40080100, 0x00000100, 0x02000000, 0x42080000,
  121. 0x42080100, 0x00080100, 0x42000000, 0x42080100, 0x02080000, 0x00000000,
  122. 0x40080000, 0x42000000, 0x00080100, 0x02000100, 0x40000100, 0x00080000,
  123. 0x00000000, 0x40080000, 0x02080100, 0x40000100 };
  124. private static final int[] SP6 = new int[] {
  125. 0x20000010, 0x20400000, 0x00004000, 0x20404010, 0x20400000, 0x00000010,
  126. 0x20404010, 0x00400000, 0x20004000, 0x00404010, 0x00400000, 0x20000010,
  127. 0x00400010, 0x20004000, 0x20000000, 0x00004010, 0x00000000, 0x00400010,
  128. 0x20004010, 0x00004000, 0x00404000, 0x20004010, 0x00000010, 0x20400010,
  129. 0x20400010, 0x00000000, 0x00404010, 0x20404000, 0x00004010, 0x00404000,
  130. 0x20404000, 0x20000000, 0x20004000, 0x00000010, 0x20400010, 0x00404000,
  131. 0x20404010, 0x00400000, 0x00004010, 0x20000010, 0x00400000, 0x20004000,
  132. 0x20000000, 0x00004010, 0x20000010, 0x20404010, 0x00404000, 0x20400000,
  133. 0x00404010, 0x20404000, 0x00000000, 0x20400010, 0x00000010, 0x00004000,
  134. 0x20400000, 0x00404010, 0x00004000, 0x00400010, 0x20004010, 0x00000000,
  135. 0x20404000, 0x20000000, 0x00400010, 0x20004010 };
  136. private static final int[] SP7 = new int[] {
  137. 0x00200000, 0x04200002, 0x04000802, 0x00000000, 0x00000800, 0x04000802,
  138. 0x00200802, 0x04200800, 0x04200802, 0x00200000, 0x00000000, 0x04000002,
  139. 0x00000002, 0x04000000, 0x04200002, 0x00000802, 0x04000800, 0x00200802,
  140. 0x00200002, 0x04000800, 0x04000002, 0x04200000, 0x04200800, 0x00200002,
  141. 0x04200000, 0x00000800, 0x00000802, 0x04200802, 0x00200800, 0x00000002,
  142. 0x04000000, 0x00200800, 0x04000000, 0x00200800, 0x00200000, 0x04000802,
  143. 0x04000802, 0x04200002, 0x04200002, 0x00000002, 0x00200002, 0x04000000,
  144. 0x04000800, 0x00200000, 0x04200800, 0x00000802, 0x00200802, 0x04200800,
  145. 0x00000802, 0x04000002, 0x04200802, 0x04200000, 0x00200800, 0x00000000,
  146. 0x00000002, 0x04200802, 0x00000000, 0x00200802, 0x04200000, 0x00000800,
  147. 0x04000002, 0x04000800, 0x00000800, 0x00200002 };
  148. private static final int[] SP8 = new int[] {
  149. 0x10001040, 0x00001000, 0x00040000, 0x10041040, 0x10000000, 0x10001040,
  150. 0x00000040, 0x10000000, 0x00040040, 0x10040000, 0x10041040, 0x00041000,
  151. 0x10041000, 0x00041040, 0x00001000, 0x00000040, 0x10040000, 0x10000040,
  152. 0x10001000, 0x00001040, 0x00041000, 0x00040040, 0x10040040, 0x10041000,
  153. 0x00001040, 0x00000000, 0x00000000, 0x10040040, 0x10000040, 0x10001000,
  154. 0x00041040, 0x00040000, 0x00041040, 0x00040000, 0x10041000, 0x00001000,
  155. 0x00000040, 0x10040040, 0x00001000, 0x00041040, 0x10001000, 0x00000040,
  156. 0x10000040, 0x10040000, 0x10040040, 0x10000000, 0x00040000, 0x10001040,
  157. 0x00000000, 0x10041040, 0x00040040, 0x10000040, 0x10040000, 0x10001000,
  158. 0x10001040, 0x00000000, 0x10041040, 0x00041000, 0x00041000, 0x00001040,
  159. 0x00001040, 0x00040040, 0x10000000, 0x10041000 };
  160. /**
  161. * Constants that help in determining whether or not a byte array is parity
  162. * adjusted.
  163. */
  164. private static final byte[] PARITY = {
  165. 8, 1, 0, 8, 0, 8, 8, 0, 0, 8, 8, 0, 8, 0, 2, 8,
  166. 0, 8, 8, 0, 8, 0, 0, 8, 8, 0, 0, 8, 0, 8, 8, 3,
  167. 0, 8, 8, 0, 8, 0, 0, 8, 8, 0, 0, 8, 0, 8, 8, 0,
  168. 8, 0, 0, 8, 0, 8, 8, 0, 0, 8, 8, 0, 8, 0, 0, 8,
  169. 0, 8, 8, 0, 8, 0, 0, 8, 8, 0, 0, 8, 0, 8, 8, 0,
  170. 8, 0, 0, 8, 0, 8, 8, 0, 0, 8, 8, 0, 8, 0, 0, 8,
  171. 8, 0, 0, 8, 0, 8, 8, 0, 0, 8, 8, 0, 8, 0, 0, 8,
  172. 0, 8, 8, 0, 8, 0, 0, 8, 8, 0, 0, 8, 0, 8, 8, 0,
  173. 0, 8, 8, 0, 8, 0, 0, 8, 8, 0, 0, 8, 0, 8, 8, 0,
  174. 8, 0, 0, 8, 0, 8, 8, 0, 0, 8, 8, 0, 8, 0, 0, 8,
  175. 8, 0, 0, 8, 0, 8, 8, 0, 0, 8, 8, 0, 8, 0, 0, 8,
  176. 0, 8, 8, 0, 8, 0, 0, 8, 8, 0, 0, 8, 0, 8, 8, 0,
  177. 8, 0, 0, 8, 0, 8, 8, 0, 0, 8, 8, 0, 8, 0, 0, 8,
  178. 0, 8, 8, 0, 8, 0, 0, 8, 8, 0, 0, 8, 0, 8, 8, 0,
  179. 4, 8, 8, 0, 8, 0, 0, 8, 8, 0, 0, 8, 0, 8, 8, 0,
  180. 8, 5, 0, 8, 0, 8, 8, 0, 0, 8, 8, 0, 8, 0, 6, 8 };
  181. // Key schedule constants.
  182. private static final byte[] ROTARS = {
  183. 1, 2, 4, 6, 8, 10, 12, 14, 15, 17, 19, 21, 23, 25, 27, 28 };
  184. private static final byte[] PC1 = {
  185. 56, 48, 40, 32, 24, 16, 8, 0, 57, 49, 41, 33, 25, 17, 9, 1,
  186. 58, 50, 42, 34, 26, 18, 10, 2, 59, 51, 43, 35, 62, 54, 46, 38,
  187. 30, 22, 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 60, 52, 44, 36,
  188. 28, 20, 12, 4, 27, 19, 11, 3 };
  189. private static final byte[] PC2 = {
  190. 13, 16, 10, 23, 0, 4, 2, 27, 14, 5, 20, 9, 22, 18, 11, 3,
  191. 25, 7, 15, 6, 26, 19, 12, 1, 40, 51, 30, 36, 46, 54, 29, 39,
  192. 50, 44, 32, 47, 43, 48, 38, 55, 33, 52, 45, 41, 49, 35, 28, 31 };
  193. /**
  194. * Weak keys (parity adjusted): If all the bits in each half are either 0
  195. * or 1, then the key used for any cycle of the algorithm is the same as
  196. * all other cycles.
  197. */
  198. public static final byte[][] WEAK_KEYS = {
  199. Util.toBytesFromString("0101010101010101"),
  200. Util.toBytesFromString("01010101FEFEFEFE"),
  201. Util.toBytesFromString("FEFEFEFE01010101"),
  202. Util.toBytesFromString("FEFEFEFEFEFEFEFE") };
  203. /**
  204. * Semi-weak keys (parity adjusted): Some pairs of keys encrypt plain text
  205. * to identical cipher text. In other words, one key in the pair can decrypt
  206. * messages that were encrypted with the other key. These keys are called
  207. * semi-weak keys. This occurs because instead of 16 different sub-keys being
  208. * generated, these semi-weak keys produce only two different sub-keys.
  209. */
  210. public static final byte[][] SEMIWEAK_KEYS = {
  211. Util.toBytesFromString("01FE01FE01FE01FE"),
  212. Util.toBytesFromString("FE01FE01FE01FE01"),
  213. Util.toBytesFromString("1FE01FE00EF10EF1"),
  214. Util.toBytesFromString("E01FE01FF10EF10E"),
  215. Util.toBytesFromString("01E001E001F101F1"),
  216. Util.toBytesFromString("E001E001F101F101"),
  217. Util.toBytesFromString("1FFE1FFE0EFE0EFE"),
  218. Util.toBytesFromString("FE1FFE1FFE0EFE0E"),
  219. Util.toBytesFromString("011F011F010E010E"),
  220. Util.toBytesFromString("1F011F010E010E01"),
  221. Util.toBytesFromString("E0FEE0FEF1FEF1FE"),
  222. Util.toBytesFromString("FEE0FEE0FEF1FEF1") };
  223. /** Possible weak keys (parity adjusted) --produce 4 instead of 16 subkeys. */
  224. public static final byte[][] POSSIBLE_WEAK_KEYS = {
  225. Util.toBytesFromString("1F1F01010E0E0101"),
  226. Util.toBytesFromString("011F1F01010E0E01"),
  227. Util.toBytesFromString("1F01011F0E01010E"),
  228. Util.toBytesFromString("01011F1F01010E0E"),
  229. Util.toBytesFromString("E0E00101F1F10101"),
  230. Util.toBytesFromString("FEFE0101FEFE0101"),
  231. Util.toBytesFromString("FEE01F01FEF10E01"),
  232. Util.toBytesFromString("E0FE1F01F1FE0E01"),
  233. Util.toBytesFromString("FEE0011FFEF1010E"),
  234. Util.toBytesFromString("E0FE011FF1FE010E"),
  235. Util.toBytesFromString("E0E01F1FF1F10E0E"),
  236. Util.toBytesFromString("FEFE1F1FFEFE0E0E"),
  237. Util.toBytesFromString("1F1F01010E0E0101"),
  238. Util.toBytesFromString("011F1F01010E0E01"),
  239. Util.toBytesFromString("1F01011F0E01010E"),
  240. Util.toBytesFromString("01011F1F01010E0E"),
  241. Util.toBytesFromString("01E0E00101F1F101"),
  242. Util.toBytesFromString("1FFEE0010EFEF001"),
  243. Util.toBytesFromString("1FE0FE010EF1FE01"),
  244. Util.toBytesFromString("01FEFE0101FEFE01"),
  245. Util.toBytesFromString("1FE0E01F0EF1F10E"),
  246. Util.toBytesFromString("01FEE01F01FEF10E"),
  247. Util.toBytesFromString("01E0FE1F01F1FE0E"),
  248. Util.toBytesFromString("1FFEFE1F0EFEFE0E"),
  249. Util.toBytesFromString("E00101E0F10101F1"),
  250. Util.toBytesFromString("FE1F01E0FE0E0EF1"),
  251. Util.toBytesFromString("FE011FE0FE010EF1"),
  252. Util.toBytesFromString("E01F1FE0F10E0EF1"),
  253. Util.toBytesFromString("FE0101FEFE0101FE"),
  254. Util.toBytesFromString("E01F01FEF10E01FE"),
  255. Util.toBytesFromString("E0011FFEF1010EFE"),
  256. Util.toBytesFromString("FE1F1FFEFE0E0EFE"),
  257. Util.toBytesFromString("1FFE01E00EFE01F1"),
  258. Util.toBytesFromString("01FE1FE001FE0EF1"),
  259. Util.toBytesFromString("1FE001FE0EF101FE"),
  260. Util.toBytesFromString("01E01FFE01F10EFE"),
  261. Util.toBytesFromString("0101E0E00101F1F1"),
  262. Util.toBytesFromString("1F1FE0E00E0EF1F1"),
  263. Util.toBytesFromString("1F01FEE00E01FEF1"),
  264. Util.toBytesFromString("011FFEE0010EFEF1"),
  265. Util.toBytesFromString("1F01E0FE0E01F1FE"),
  266. Util.toBytesFromString("011FE0FE010EF1FE"),
  267. Util.toBytesFromString("0101FEFE0001FEFE"),
  268. Util.toBytesFromString("1F1FFEFE0E0EFEFE"),
  269. Util.toBytesFromString("FEFEE0E0FEFEF1F1"),
  270. Util.toBytesFromString("E0FEFEE0F1FEFEF1"),
  271. Util.toBytesFromString("FEE0E0FEFEF1F1FE"),
  272. Util.toBytesFromString("E0E0FEFEF1F1FEFE") };
  273. /** Default 0-argument constructor. */
  274. public DES()
  275. {
  276. super(Registry.DES_CIPHER, BLOCK_SIZE, KEY_SIZE);
  277. }
  278. /**
  279. * Adjust the parity for a raw key array. This essentially means that each
  280. * byte in the array will have an odd number of '1' bits (the last bit in
  281. * each byte is unused.
  282. *
  283. * @param kb The key array, to be parity-adjusted.
  284. * @param offset The starting index into the key bytes.
  285. */
  286. public static void adjustParity(byte[] kb, int offset)
  287. {
  288. for (int i = offset; i < offset + KEY_SIZE; i++)
  289. kb[i] ^= (PARITY[kb[i] & 0xff] == 8) ? 1 : 0;
  290. }
  291. /**
  292. * Test if a byte array, which must be at least 8 bytes long, is parity
  293. * adjusted.
  294. *
  295. * @param kb The key bytes.
  296. * @param offset The starting index into the key bytes.
  297. * @return <code>true</code> if the first 8 bytes of <i>kb</i> have been
  298. * parity adjusted. <code>false</code> otherwise.
  299. */
  300. public static boolean isParityAdjusted(byte[] kb, int offset)
  301. {
  302. int w = 0x88888888;
  303. int n = PARITY[kb[offset + 0] & 0xff];
  304. n <<= 4;
  305. n |= PARITY[kb[offset + 1] & 0xff];
  306. n <<= 4;
  307. n |= PARITY[kb[offset + 2] & 0xff];
  308. n <<= 4;
  309. n |= PARITY[kb[offset + 3] & 0xff];
  310. n <<= 4;
  311. n |= PARITY[kb[offset + 4] & 0xff];
  312. n <<= 4;
  313. n |= PARITY[kb[offset + 5] & 0xff];
  314. n <<= 4;
  315. n |= PARITY[kb[offset + 6] & 0xff];
  316. n <<= 4;
  317. n |= PARITY[kb[offset + 7] & 0xff];
  318. return (n & w) == 0;
  319. }
  320. /**
  321. * Test if a key is a weak key.
  322. *
  323. * @param kb The key to test.
  324. * @return <code>true</code> if the key is weak.
  325. */
  326. public static boolean isWeak(byte[] kb)
  327. {
  328. for (int i = 0; i < WEAK_KEYS.length; i++)
  329. if (Arrays.equals(WEAK_KEYS[i], kb))
  330. return true;
  331. return false;
  332. }
  333. /**
  334. * Test if a key is a semi-weak key.
  335. *
  336. * @param kb The key to test.
  337. * @return <code>true</code> if this key is semi-weak.
  338. */
  339. public static boolean isSemiWeak(byte[] kb)
  340. {
  341. for (int i = 0; i < SEMIWEAK_KEYS.length; i++)
  342. if (Arrays.equals(SEMIWEAK_KEYS[i], kb))
  343. return true;
  344. return false;
  345. }
  346. /**
  347. * Test if the designated byte array represents a possibly weak key.
  348. *
  349. * @param kb the byte array to test.
  350. * @return <code>true</code> if <code>kb</code>represents a possibly weak key.
  351. * Returns <code>false</code> otherwise.
  352. */
  353. public static boolean isPossibleWeak(byte[] kb)
  354. {
  355. for (int i = 0; i < POSSIBLE_WEAK_KEYS.length; i++)
  356. if (Arrays.equals(POSSIBLE_WEAK_KEYS[i], kb))
  357. return true;
  358. return false;
  359. }
  360. /**
  361. * The core DES function. This is used for both encryption and decryption,
  362. * the only difference being the key.
  363. *
  364. * @param in The input bytes.
  365. * @param i The starting offset into the input bytes.
  366. * @param out The output bytes.
  367. * @param o The starting offset into the output bytes.
  368. * @param key The working key.
  369. */
  370. private static void desFunc(byte[] in, int i, byte[] out, int o, int[] key)
  371. {
  372. int right, left, work;
  373. // Load.
  374. left = (in[i++] & 0xff) << 24
  375. | (in[i++] & 0xff) << 16
  376. | (in[i++] & 0xff) << 8
  377. | in[i++] & 0xff;
  378. right = (in[i++] & 0xff) << 24
  379. | (in[i++] & 0xff) << 16
  380. | (in[i++] & 0xff) << 8
  381. | in[i ] & 0xff;
  382. // Initial permutation.
  383. work = ((left >>> 4) ^ right) & 0x0F0F0F0F;
  384. left ^= work << 4;
  385. right ^= work;
  386. work = ((left >>> 16) ^ right) & 0x0000FFFF;
  387. left ^= work << 16;
  388. right ^= work;
  389. work = ((right >>> 2) ^ left) & 0x33333333;
  390. right ^= work << 2;
  391. left ^= work;
  392. work = ((right >>> 8) ^ left) & 0x00FF00FF;
  393. right ^= work << 8;
  394. left ^= work;
  395. right = ((right << 1) | ((right >>> 31) & 1)) & 0xFFFFFFFF;
  396. work = (left ^ right) & 0xAAAAAAAA;
  397. left ^= work;
  398. right ^= work;
  399. left = ((left << 1) | ((left >>> 31) & 1)) & 0xFFFFFFFF;
  400. int k = 0, t;
  401. for (int round = 0; round < 8; round++)
  402. {
  403. work = right >>> 4 | right << 28;
  404. work ^= key[k++];
  405. t = SP7[work & 0x3F];
  406. work >>>= 8;
  407. t |= SP5[work & 0x3F];
  408. work >>>= 8;
  409. t |= SP3[work & 0x3F];
  410. work >>>= 8;
  411. t |= SP1[work & 0x3F];
  412. work = right ^ key[k++];
  413. t |= SP8[work & 0x3F];
  414. work >>>= 8;
  415. t |= SP6[work & 0x3F];
  416. work >>>= 8;
  417. t |= SP4[work & 0x3F];
  418. work >>>= 8;
  419. t |= SP2[work & 0x3F];
  420. left ^= t;
  421. work = left >>> 4 | left << 28;
  422. work ^= key[k++];
  423. t = SP7[work & 0x3F];
  424. work >>>= 8;
  425. t |= SP5[work & 0x3F];
  426. work >>>= 8;
  427. t |= SP3[work & 0x3F];
  428. work >>>= 8;
  429. t |= SP1[work & 0x3F];
  430. work = left ^ key[k++];
  431. t |= SP8[work & 0x3F];
  432. work >>>= 8;
  433. t |= SP6[work & 0x3F];
  434. work >>>= 8;
  435. t |= SP4[work & 0x3F];
  436. work >>>= 8;
  437. t |= SP2[work & 0x3F];
  438. right ^= t;
  439. }
  440. // The final permutation.
  441. right = (right << 31) | (right >>> 1);
  442. work = (left ^ right) & 0xAAAAAAAA;
  443. left ^= work;
  444. right ^= work;
  445. left = (left << 31) | (left >>> 1);
  446. work = ((left >>> 8) ^ right) & 0x00FF00FF;
  447. left ^= work << 8;
  448. right ^= work;
  449. work = ((left >>> 2) ^ right) & 0x33333333;
  450. left ^= work << 2;
  451. right ^= work;
  452. work = ((right >>> 16) ^ left) & 0x0000FFFF;
  453. right ^= work << 16;
  454. left ^= work;
  455. work = ((right >>> 4) ^ left) & 0x0F0F0F0F;
  456. right ^= work << 4;
  457. left ^= work;
  458. out[o++] = (byte)(right >>> 24);
  459. out[o++] = (byte)(right >>> 16);
  460. out[o++] = (byte)(right >>> 8);
  461. out[o++] = (byte) right;
  462. out[o++] = (byte)(left >>> 24);
  463. out[o++] = (byte)(left >>> 16);
  464. out[o++] = (byte)(left >>> 8);
  465. out[o ] = (byte) left;
  466. }
  467. public Object clone()
  468. {
  469. return new DES();
  470. }
  471. public Iterator blockSizes()
  472. {
  473. return Collections.singleton(Integer.valueOf(BLOCK_SIZE)).iterator();
  474. }
  475. public Iterator keySizes()
  476. {
  477. return Collections.singleton(Integer.valueOf(KEY_SIZE)).iterator();
  478. }
  479. public Object makeKey(byte[] kb, int bs) throws InvalidKeyException
  480. {
  481. if (kb == null || kb.length != KEY_SIZE)
  482. throw new InvalidKeyException("DES keys must be 8 bytes long");
  483. if (Properties.checkForWeakKeys()
  484. && (isWeak(kb) || isSemiWeak(kb) || isPossibleWeak(kb)))
  485. throw new WeakKeyException();
  486. int i, j, l, m, n;
  487. long pc1m = 0, pcr = 0;
  488. for (i = 0; i < 56; i++)
  489. {
  490. l = PC1[i];
  491. pc1m |= ((kb[l >>> 3] & (0x80 >>> (l & 7))) != 0) ? (1L << (55 - i))
  492. : 0;
  493. }
  494. Context ctx = new Context();
  495. // Encryption key first.
  496. for (i = 0; i < 16; i++)
  497. {
  498. pcr = 0;
  499. m = i << 1;
  500. n = m + 1;
  501. for (j = 0; j < 28; j++)
  502. {
  503. l = j + ROTARS[i];
  504. if (l < 28)
  505. pcr |= ((pc1m & 1L << (55 - l)) != 0) ? (1L << (55 - j)) : 0;
  506. else
  507. pcr |= ((pc1m & 1L << (55 - (l - 28))) != 0) ? (1L << (55 - j))
  508. : 0;
  509. }
  510. for (j = 28; j < 56; j++)
  511. {
  512. l = j + ROTARS[i];
  513. if (l < 56)
  514. pcr |= ((pc1m & 1L << (55 - l)) != 0) ? (1L << (55 - j)) : 0;
  515. else
  516. pcr |= ((pc1m & 1L << (55 - (l - 28))) != 0) ? (1L << (55 - j))
  517. : 0;
  518. }
  519. for (j = 0; j < 24; j++)
  520. {
  521. if ((pcr & 1L << (55 - PC2[j])) != 0)
  522. ctx.ek[m] |= 1 << (23 - j);
  523. if ((pcr & 1L << (55 - PC2[j + 24])) != 0)
  524. ctx.ek[n] |= 1 << (23 - j);
  525. }
  526. }
  527. // The decryption key is the same, but in reversed order.
  528. for (i = 0; i < Context.EXPANDED_KEY_SIZE; i += 2)
  529. {
  530. ctx.dk[30 - i] = ctx.ek[i];
  531. ctx.dk[31 - i] = ctx.ek[i + 1];
  532. }
  533. // "Cook" the keys.
  534. for (i = 0; i < 32; i += 2)
  535. {
  536. int x, y;
  537. x = ctx.ek[i];
  538. y = ctx.ek[i + 1];
  539. ctx.ek[i ] = ((x & 0x00FC0000) << 6)
  540. | ((x & 0x00000FC0) << 10)
  541. | ((y & 0x00FC0000) >>> 10)
  542. | ((y & 0x00000FC0) >>> 6);
  543. ctx.ek[i + 1] = ((x & 0x0003F000) << 12)
  544. | ((x & 0x0000003F) << 16)
  545. | ((y & 0x0003F000) >>> 4)
  546. | (y & 0x0000003F);
  547. x = ctx.dk[i];
  548. y = ctx.dk[i + 1];
  549. ctx.dk[i ] = ((x & 0x00FC0000) << 6)
  550. | ((x & 0x00000FC0) << 10)
  551. | ((y & 0x00FC0000) >>> 10)
  552. | ((y & 0x00000FC0) >>> 6);
  553. ctx.dk[i + 1] = ((x & 0x0003F000) << 12)
  554. | ((x & 0x0000003F) << 16)
  555. | ((y & 0x0003F000) >>> 4)
  556. | (y & 0x0000003F);
  557. }
  558. return ctx;
  559. }
  560. public void encrypt(byte[] in, int i, byte[] out, int o, Object K, int bs)
  561. {
  562. desFunc(in, i, out, o, ((Context) K).ek);
  563. }
  564. public void decrypt(byte[] in, int i, byte[] out, int o, Object K, int bs)
  565. {
  566. desFunc(in, i, out, o, ((Context) K).dk);
  567. }
  568. /**
  569. * Simple wrapper class around the session keys. Package-private so TripleDES
  570. * can see it.
  571. */
  572. final class Context
  573. {
  574. private static final int EXPANDED_KEY_SIZE = 32;
  575. /** The encryption key. */
  576. int[] ek;
  577. /** The decryption key. */
  578. int[] dk;
  579. /** Default 0-arguments constructor. */
  580. Context()
  581. {
  582. ek = new int[EXPANDED_KEY_SIZE];
  583. dk = new int[EXPANDED_KEY_SIZE];
  584. }
  585. byte[] getEncryptionKeyBytes()
  586. {
  587. return toByteArray(ek);
  588. }
  589. byte[] getDecryptionKeyBytes()
  590. {
  591. return toByteArray(dk);
  592. }
  593. byte[] toByteArray(int[] k)
  594. {
  595. byte[] result = new byte[4 * k.length];
  596. for (int i = 0, j = 0; i < k.length; i++)
  597. {
  598. result[j++] = (byte)(k[i] >>> 24);
  599. result[j++] = (byte)(k[i] >>> 16);
  600. result[j++] = (byte)(k[i] >>> 8);
  601. result[j++] = (byte) k[i];
  602. }
  603. return result;
  604. }
  605. }
  606. }