TripleDES.java 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /* TripleDES.java --
  2. Copyright (C) 2002, 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 java.util.ArrayList;
  34. import java.util.Collections;
  35. import java.util.Iterator;
  36. import java.security.InvalidKeyException;
  37. /**
  38. * Triple-DES, 3DES, or DESede is a <i>combined cipher</i> that uses three
  39. * iterations of the Data Encryption Standard cipher to theoretically improve
  40. * the security of plain DES, at the cost of speed.
  41. * <p>
  42. * Triple-DES runs the DES algorithm three times with one, two or three
  43. * independent 56-bit (DES) keys. When used with one DES key, the cipher behaves
  44. * exactly like a (slower) DES.
  45. * <p>
  46. * To encrypt:
  47. * <blockquote><i>C<sub>i</sub> = E<sub>k3</sub> ( E<sub>k2</sub><sup>-1</sup> (
  48. * E<sub>k1</sub> ( P<sub>i</sub> )))</i>
  49. * </blockquote>
  50. * <p>
  51. * And to decrypt:
  52. * <blockquote><i>P<sub>i</sub> = E<sub>k1</sub><sup>-1</sup> (
  53. * E<sub>k2</sub> ( E<sub>k3</sub><sup>-1</sup> ( C<sub>i</sub> )))</i>
  54. * </blockquote>
  55. * <p>
  56. * (The "ede" comes from the encryption operation, which runs
  57. * Encrypt-Decrypt-Encrypt)
  58. * <p>
  59. * References:
  60. * <ol>
  61. * <li>Bruce Schneier, <i>Applied Cryptography: Protocols, Algorithms, and
  62. * Source Code in C, Second Edition</i>. (1996 John Wiley and Sons) ISBN
  63. * 0-471-11709-9. Page 294--295.</li>
  64. * </ol>
  65. */
  66. public class TripleDES
  67. extends BaseCipher
  68. {
  69. /** Triple-DES only operates on 64 bit blocks. */
  70. public static final int BLOCK_SIZE = 8;
  71. /** By default, Triple-DES uses 168 bits of a parity-adjusted 192 bit key. */
  72. public static final int KEY_SIZE = 24;
  73. /** The underlying DES instance. */
  74. private DES des;
  75. /**
  76. * Default 0-arguments constructor.
  77. */
  78. public TripleDES()
  79. {
  80. super(Registry.TRIPLEDES_CIPHER, BLOCK_SIZE, KEY_SIZE);
  81. des = new DES();
  82. }
  83. /**
  84. * Convenience method which calls the method with same name and three
  85. * arguments, passing <code>3</code> as the value of the first parameter.
  86. *
  87. * @param kb The key bytes to adjust.
  88. * @param offset The starting offset into the key bytes.
  89. */
  90. public static void adjustParity(byte[] kb, int offset)
  91. {
  92. adjustParity(3, kb, offset);
  93. }
  94. /**
  95. * Adjusts, in-situ, the parity of the designated bytes, so they can be used
  96. * as DES keys for a 3-DES 1-, 2- or 3-key cipher.
  97. *
  98. * @param keyCount the number of independent DES keys. Can be either
  99. * <code>1</code>, <code>2</code> or <code>3</code>. Any other value
  100. * will cause an {@link IllegalArgumentException} to be raised.
  101. * @param kb the array containing the key bytes to adjust. MUST have at least
  102. * <code>8 * keyCount</code> bytes starting at offset position
  103. * <code>offset</code>, otherwise an
  104. * {@link ArrayIndexOutOfBoundsException} will be raised.
  105. * @param offset the starting offset into the array.
  106. * @see DES#adjustParity(byte[],int)
  107. */
  108. public static void adjustParity(int keyCount, byte[] kb, int offset)
  109. {
  110. if (keyCount < 1 || keyCount > 3)
  111. throw new IllegalArgumentException("Invalid keyCount value: " + keyCount);
  112. DES.adjustParity(kb, offset);
  113. if (keyCount > 1)
  114. DES.adjustParity(kb, offset + 8);
  115. if (keyCount > 2)
  116. DES.adjustParity(kb, offset + 16);
  117. }
  118. /**
  119. * Convenience method which calls the method with same name and three
  120. * arguments, passing <code>3</code> as the value of the first parameter.
  121. *
  122. * @param kb The key bytes to test.
  123. * @param offset The starting offset into the key bytes.
  124. * @return <code>true</code> if the bytes in <i>kb</i> starting at
  125. * <i>offset</i> are parity adjusted.
  126. * @see DES#isParityAdjusted(byte[],int)
  127. * @see #adjustParity(byte[],int)
  128. */
  129. public static boolean isParityAdjusted(byte[] kb, int offset)
  130. {
  131. return isParityAdjusted(3, kb, offset);
  132. }
  133. /**
  134. * Tests if enough bytes, expected to be used as DES keys for a 3-DES 1-, 2-
  135. * or 3-key cipher, located in a designated byte array, has already been
  136. * parity adjusted.
  137. *
  138. * @param keyCount the number of independent DES keys. Can be either
  139. * <code>1</code>, <code>2</code> or <code>3</code>. Any other value
  140. * will cause an {@link IllegalArgumentException} to be raised.
  141. * @param kb the array containing the key bytes to test. MUST have at least
  142. * <code>8 * keyCount</code> bytes starting at offset position
  143. * <code>offset</code>, otherwise an
  144. * {@link ArrayIndexOutOfBoundsException} will be raised.
  145. * @param offset the starting offset into the array.
  146. * @return <code>true</code> if the bytes in <i>kb</i> starting at
  147. * <i>offset</i> are parity adjusted.
  148. * @see DES#isParityAdjusted(byte[],int)
  149. * @see #adjustParity(int,byte[],int)
  150. */
  151. public static boolean isParityAdjusted(int keyCount, byte[] kb, int offset)
  152. {
  153. if (keyCount < 1 || keyCount > 3)
  154. throw new IllegalArgumentException("Invalid keyCount value: " + keyCount);
  155. boolean result = DES.isParityAdjusted(kb, offset);
  156. if (keyCount > 1)
  157. result = result && DES.isParityAdjusted(kb, offset + 8);
  158. if (keyCount > 2)
  159. result = result && DES.isParityAdjusted(kb, offset + 16);
  160. return result;
  161. }
  162. public Object clone()
  163. {
  164. return new TripleDES();
  165. }
  166. public Iterator blockSizes()
  167. {
  168. return Collections.singleton(Integer.valueOf(BLOCK_SIZE)).iterator();
  169. }
  170. public Iterator keySizes()
  171. {
  172. ArrayList al = new ArrayList();
  173. al.add(Integer.valueOf(8));
  174. al.add(Integer.valueOf(16));
  175. al.add(Integer.valueOf(24));
  176. return Collections.unmodifiableList(al).iterator();
  177. }
  178. public Object makeKey(byte[] kb, int bs) throws InvalidKeyException
  179. {
  180. if (kb.length != 8 && kb.length != 16 && kb.length != 24)
  181. throw new InvalidKeyException("TripleDES key must be 8, 16 or 24 bytes: "
  182. + kb.length);
  183. Context ctx = new Context();
  184. byte[] k1 = new byte[DES.KEY_SIZE];
  185. System.arraycopy(kb, 0, k1, 0, DES.KEY_SIZE);
  186. if (! DES.isParityAdjusted(k1, 0))
  187. DES.adjustParity(k1, 0);
  188. ctx.k1 = (DES.Context) des.makeKey(k1, bs);
  189. if (kb.length == 8)
  190. {
  191. ctx.k2 = (DES.Context) des.makeKey(k1, bs);
  192. ctx.k3 = (DES.Context) des.makeKey(k1, bs);
  193. }
  194. else
  195. {
  196. byte[] k2 = new byte[DES.KEY_SIZE];
  197. System.arraycopy(kb, DES.KEY_SIZE, k2, 0, DES.KEY_SIZE);
  198. if (! DES.isParityAdjusted(k2, 0))
  199. DES.adjustParity(k2, 0);
  200. ctx.k2 = (DES.Context) des.makeKey(k2, bs);
  201. byte[] k3 = new byte[DES.KEY_SIZE];
  202. if (kb.length == 16)
  203. ctx.k3 = (DES.Context) des.makeKey(k1, bs);
  204. else
  205. {
  206. System.arraycopy(kb, 2 * DES.KEY_SIZE, k3, 0, DES.KEY_SIZE);
  207. if (! DES.isParityAdjusted(k3, 0))
  208. DES.adjustParity(k3, 0);
  209. ctx.k3 = (DES.Context) des.makeKey(k3, bs);
  210. }
  211. }
  212. return ctx;
  213. }
  214. public void encrypt(byte[] in, int i, byte[] out, int o, Object K, int bs)
  215. {
  216. byte[] temp = new byte[BLOCK_SIZE];
  217. des.encrypt(in, i, temp, 0, ((Context) K).k1, bs);
  218. des.decrypt(temp, 0, temp, 0, ((Context) K).k2, bs);
  219. des.encrypt(temp, 0, out, o, ((Context) K).k3, bs);
  220. }
  221. public void decrypt(byte[] in, int i, byte[] out, int o, Object K, int bs)
  222. {
  223. byte[] temp = new byte[BLOCK_SIZE];
  224. des.decrypt(in, i, temp, 0, ((Context) K).k3, bs);
  225. des.encrypt(temp, 0, temp, 0, ((Context) K).k2, bs);
  226. des.decrypt(temp, 0, out, o, ((Context) K).k1, bs);
  227. }
  228. private final class Context
  229. {
  230. DES.Context k1, k2, k3;
  231. }
  232. }