001: /*
002: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.security.krb5.internal.crypto.dk;
027:
028: import javax.crypto.Cipher;
029: import javax.crypto.Mac;
030: import javax.crypto.SecretKeyFactory;
031: import javax.crypto.SecretKey;
032: import javax.crypto.spec.SecretKeySpec;
033: import javax.crypto.spec.DESKeySpec;
034: import javax.crypto.spec.DESedeKeySpec;
035: import javax.crypto.spec.IvParameterSpec;
036: import java.security.spec.KeySpec;
037: import java.security.GeneralSecurityException;
038: import java.security.InvalidKeyException;
039: import java.util.Arrays;
040:
041: public class Des3DkCrypto extends DkCrypto {
042:
043: private static final byte[] ZERO_IV = new byte[] { 0, 0, 0, 0, 0,
044: 0, 0, 0 };
045:
046: public Des3DkCrypto() {
047: }
048:
049: protected int getKeySeedLength() {
050: return 168; // bits; 3DES key material has 21 bytes
051: }
052:
053: public byte[] stringToKey(char[] salt)
054: throws GeneralSecurityException {
055: byte[] saltUtf8 = null;
056: try {
057: saltUtf8 = charToUtf8(salt);
058: return stringToKey(saltUtf8, null);
059: } finally {
060: if (saltUtf8 != null) {
061: Arrays.fill(saltUtf8, (byte) 0);
062: }
063: // Caller responsible for clearing its own salt
064: }
065: }
066:
067: private byte[] stringToKey(byte[] secretAndSalt, byte[] opaque)
068: throws GeneralSecurityException {
069:
070: if (opaque != null && opaque.length > 0) {
071: throw new RuntimeException(
072: "Invalid parameter to stringToKey");
073: }
074:
075: byte[] tmpKey = randomToKey(nfold(secretAndSalt,
076: getKeySeedLength()));
077: return dk(tmpKey, KERBEROS_CONSTANT);
078: }
079:
080: public byte[] parityFix(byte[] value)
081: throws GeneralSecurityException {
082: // fix key parity
083: setParityBit(value);
084: return value;
085: }
086:
087: /*
088: * From RFC 3961.
089: *
090: * The 168 bits of random key data are converted to a protocol key value
091: * as follows. First, the 168 bits are divided into three groups of 56
092: * bits, which are expanded individually into 64 bits as in des3Expand().
093: * Result is a 24 byte (192-bit) key.
094: */
095: protected byte[] randomToKey(byte[] in) {
096: if (in.length != 21) {
097: throw new IllegalArgumentException("input must be 168 bits");
098: }
099:
100: byte[] one = keyCorrection(des3Expand(in, 0, 7));
101: byte[] two = keyCorrection(des3Expand(in, 7, 14));
102: byte[] three = keyCorrection(des3Expand(in, 14, 21));
103:
104: byte[] key = new byte[24];
105: System.arraycopy(one, 0, key, 0, 8);
106: System.arraycopy(two, 0, key, 8, 8);
107: System.arraycopy(three, 0, key, 16, 8);
108:
109: return key;
110: }
111:
112: private static byte[] keyCorrection(byte[] key) {
113: // check for weak key
114: try {
115: if (DESKeySpec.isWeak(key, 0)) {
116: key[7] = (byte) (key[7] ^ 0xF0);
117: }
118: } catch (InvalidKeyException ex) {
119: // swallow, since it should never happen
120: }
121: return key;
122: }
123:
124: /**
125: * From RFC 3961.
126: *
127: * Expands a 7-byte array into an 8-byte array that contains parity bits.
128: * The 56 bits are expanded into 64 bits as follows:
129: * 1 2 3 4 5 6 7 p
130: * 9 10 11 12 13 14 15 p
131: * 17 18 19 20 21 22 23 p
132: * 25 26 27 28 29 30 31 p
133: * 33 34 35 36 37 38 39 p
134: * 41 42 43 44 45 46 47 p
135: * 49 50 51 52 53 54 55 p
136: * 56 48 40 32 24 16 8 p
137: *
138: * (PI,P2,...,P8) are reserved for parity bits computed on the preceding
139: * seven independent bits and set so that the parity of the octet is odd,
140: * i.e., there is an odd number of "1" bits in the octet.
141: *
142: * @param start index of starting byte (inclusive)
143: * @param end index of ending byte (exclusive)
144: */
145: private static byte[] des3Expand(byte[] input, int start, int end) {
146: if ((end - start) != 7)
147: throw new IllegalArgumentException(
148: "Invalid length of DES Key Value:" + start + ","
149: + end);
150:
151: byte[] result = new byte[8];
152: byte last = 0;
153: System.arraycopy(input, start, result, 0, 7);
154: byte posn = 0;
155:
156: // Fill in last row
157: for (int i = start; i < end; i++) {
158: byte bit = (byte) (input[i] & 0x01);
159: if (debug) {
160: System.out.println(i + ": "
161: + Integer.toHexString(input[i]) + " bit= "
162: + Integer.toHexString(bit));
163: }
164: ++posn;
165: if (bit != 0) {
166: last |= (bit << posn);
167: }
168: }
169:
170: if (debug) {
171: System.out.println("last: " + Integer.toHexString(last));
172: }
173: result[7] = last;
174: setParityBit(result);
175: return result;
176: }
177:
178: /* Mask used to check for parity adjustment */
179: private static final byte[] PARITY_BIT_MASK = { (byte) 0x80,
180: (byte) 0x40, (byte) 0x20, (byte) 0x10, (byte) 0x08,
181: (byte) 0x04, (byte) 0x02 };
182:
183: /**
184: * Sets the parity bit (0th bit) in each byte so that each byte
185: * contains an odd number of 1's.
186: */
187: private static void setParityBit(byte[] key) {
188: for (int i = 0; i < key.length; i++) {
189: int bitCount = 0;
190: for (int maskIndex = 0; maskIndex < PARITY_BIT_MASK.length; maskIndex++) {
191: if ((key[i] & PARITY_BIT_MASK[maskIndex]) == PARITY_BIT_MASK[maskIndex]) {
192: bitCount++;
193: }
194: }
195: if ((bitCount & 0x01) == 1) {
196: // Odd number of 1 bits in the top 7 bits. Set parity bit to 0
197: key[i] = (byte) (key[i] & (byte) 0xfe);
198: } else {
199: // Even number of 1 bits in the top 7 bits. Set parity bit to 1
200: key[i] = (byte) (key[i] | 1);
201: }
202: }
203: }
204:
205: protected Cipher getCipher(byte[] key, byte[] ivec, int mode)
206: throws GeneralSecurityException {
207: // NoSuchAlgorithException
208: SecretKeyFactory factory = SecretKeyFactory
209: .getInstance("desede");
210:
211: // InvalidKeyException
212: KeySpec spec = new DESedeKeySpec(key, 0);
213:
214: // InvalidKeySpecException
215: SecretKey secretKey = factory.generateSecret(spec);
216:
217: // IV
218: if (ivec == null) {
219: ivec = ZERO_IV;
220: }
221:
222: // NoSuchAlgorithmException, NoSuchPaddingException
223: // NoSuchProviderException
224: Cipher cipher = Cipher.getInstance("DESede/CBC/NoPadding");
225: IvParameterSpec encIv = new IvParameterSpec(ivec, 0,
226: ivec.length);
227:
228: // InvalidKeyException, InvalidAlgorithParameterException
229: cipher.init(mode, secretKey, encIv);
230:
231: return cipher;
232: }
233:
234: public int getChecksumLength() {
235: return 20; // bytes
236: }
237:
238: protected byte[] getHmac(byte[] key, byte[] msg)
239: throws GeneralSecurityException {
240:
241: SecretKey keyKi = new SecretKeySpec(key, "HmacSHA1");
242: Mac m = Mac.getInstance("HmacSHA1");
243: m.init(keyKi);
244: return m.doFinal(msg);
245: }
246: }
|