Source code: cryptix/jce/provider/cipher/TripleDES.java
1 /* $Id: TripleDES.java,v 1.7 2001/08/06 21:22:55 edwin Exp $
2 *
3 * Copyright (C) 1995-2000 The Cryptix Foundation Limited.
4 * All rights reserved.
5 *
6 * Use, modification, copying and distribution of this software is subject
7 * the terms and conditions of the Cryptix General Licence. You should have
8 * received a copy of the Cryptix General Licence along with this library;
9 * if not, you can download a copy from http://www.cryptix.org/ .
10 */
11 package cryptix.jce.provider.cipher;
12
13
14 import cryptix.jce.provider.key.RawSecretKey;
15 import java.security.InvalidKeyException;
16 import java.security.Key;
17
18
19 /**
20 * This class implements Triple DES EDE encryption with three independent
21 * keys. DES-EDE3 acts as a block cipher with an 8 byte block size.
22 * <p>
23 * The encoded form of the Triple DES key should be a 24-byte array,
24 * consisting of three 8-byte single DES keys in order - K1, K2 and K3.
25 * Encryption and decryption are done as follows:
26 * <ul>
27 * <li> C = E<sub>K3</sub>(D<sub>K2</sub>(E<sub>K1</sub>(P)))
28 * <li> P = D<sub>K1</sub>(E<sub>K2</sub>(D<sub>K3</sub>(C)))
29 * </ul>
30 * <p>
31 * The alternating encryption and decryption was designed by IBM to
32 * enable compatibility with single DES, when all three keys are equal
33 * (although it is now rare for Triple DES to be used in that way).
34 * <p>
35 * When DES-EDE3 is used with the CBC mode class (algorithm name
36 * "DES-EDE3/CBC"), the result is Outer-CBC, and only one IV is used.
37 * <p>
38 * DES was written by IBM and first released in 1976. The algorithm is
39 * freely usable for both single and triple encryption.
40 *
41 * @version $Revision: 1.7 $
42 * @author Systemics Ltd
43 * @author David Hopwood
44 * @author Eric Young
45 * @author Geoffrey Keating
46 * @author Jeroen C. van Gelderen (gelderen@cryptix.org)
47 * @author John F. Dumas (jdumas@zgs.com)
48 * @author Raif S. Naffah (raif@cryptix.org)
49 */
50 public final class TripleDES
51 extends BlockCipher
52 {
53
54 // DES-EDE3 constants and variables
55 //............................................................................
56
57 private static final int
58 BLOCK_SIZE = 8,
59 KEY_LENGTH = 24,
60 ALT_KEY_LENGTH = 21,
61 DES_KEY_LENGTH = 8;
62
63 private DES
64 des1,
65 des2,
66 des3;
67
68
69 // Constructor, ...
70 // ...................................................................
71
72 public TripleDES() {
73 super(BLOCK_SIZE);
74 des1 = new DES();
75 des2 = new DES();
76 des3 = new DES();
77 }
78
79
80
81 // BPI methods
82 // ...................................................................
83
84 protected void coreInit(Key key, boolean decrypt)
85 throws InvalidKeyException
86 {
87 byte[] userkey = key.getEncoded();
88 if (userkey == null)
89 throw new InvalidKeyException("Null user key");
90
91 int len = 0;
92
93 if (userkey.length == KEY_LENGTH) {
94 len = 8;
95 } else if (userkey.length == ALT_KEY_LENGTH) {
96 len = 7;
97 } else {
98 throw new InvalidKeyException("Invalid user key length");
99 }
100
101 byte[] k = new byte[len];
102 System.arraycopy(userkey, 0, k, 0, len);
103 RawSecretKey sk = new RawSecretKey("DES", k);
104 des1.coreInit(sk, decrypt);
105
106 System.arraycopy(userkey, len, k, 0, len);
107 sk = new RawSecretKey("DES", k);
108 des2.coreInit(sk, !decrypt);
109
110 System.arraycopy(userkey, len+len, k, 0, len);
111 sk = new RawSecretKey("DES", k);
112 des3.coreInit(sk, decrypt);
113
114 if(decrypt) {
115 DES des = des1;
116 des1 = des3;
117 des3 = des;
118 }
119 }
120
121
122
123 /**
124 * Perform a DES encryption or decryption operation (depends on subkey).
125 */
126 protected void coreCrypt(byte[] in, int inOffset, byte[] out, int outOffset) {
127 des1.coreCrypt(in, inOffset, out, outOffset);
128 des2.coreCrypt(out, outOffset, out, outOffset);
129 des3.coreCrypt(out, outOffset, out, outOffset);
130 }
131 }