J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 3 | * |
| 4 | * This code is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of the GNU General Public License version 2 only, as |
| 6 | * published by the Free Software Foundation. Sun designates this |
| 7 | * particular file as subject to the "Classpath" exception as provided |
| 8 | * by Sun in the LICENSE file that accompanied this code. |
| 9 | * |
| 10 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 | * version 2 for more details (a copy is included in the LICENSE file that |
| 14 | * accompanied this code). |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License version |
| 17 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 18 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | * |
| 20 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 21 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 22 | * have any questions. |
| 23 | */ |
| 24 | |
| 25 | /* |
| 26 | * (C) Copyright IBM Corp. 1999 All Rights Reserved. |
| 27 | * Copyright 1997 The Open Group Research Institute. All rights reserved. |
| 28 | */ |
| 29 | |
| 30 | package sun.security.krb5.internal.crypto; |
| 31 | |
| 32 | import sun.security.krb5.Checksum; |
| 33 | import sun.security.krb5.KrbCryptoException; |
| 34 | import sun.security.krb5.internal.*; |
| 35 | import javax.crypto.spec.DESKeySpec; |
| 36 | import java.security.InvalidKeyException; |
| 37 | |
| 38 | public class DesMacKCksumType extends CksumType { |
| 39 | |
| 40 | public DesMacKCksumType() { |
| 41 | } |
| 42 | |
| 43 | public int confounderSize() { |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | public int cksumType() { |
| 48 | return Checksum.CKSUMTYPE_DES_MAC_K; |
| 49 | } |
| 50 | |
| 51 | public boolean isSafe() { |
| 52 | return true; |
| 53 | } |
| 54 | |
| 55 | public int cksumSize() { |
| 56 | return 16; |
| 57 | } |
| 58 | |
| 59 | public int keyType() { |
| 60 | return Krb5.KEYTYPE_DES; |
| 61 | } |
| 62 | |
| 63 | public int keySize() { |
| 64 | return 8; |
| 65 | } |
| 66 | |
| 67 | public byte[] calculateChecksum(byte[] data, int size) { |
| 68 | return null; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Calculates keyed checksum. |
| 73 | * @param data the data used to generate the checksum. |
| 74 | * @param size length of the data. |
| 75 | * @param key the key used to encrypt the checksum. |
| 76 | * @return keyed checksum. |
| 77 | * |
| 78 | * @modified by Yanni Zhang, 12/08/99. |
| 79 | */ |
| 80 | public byte[] calculateKeyedChecksum(byte[] data, int size, byte[] key, |
| 81 | int usage) throws KrbCryptoException { |
| 82 | //check for weak keys |
| 83 | try { |
| 84 | if (DESKeySpec.isWeak(key, 0)) { |
| 85 | key[7] = (byte)(key[7] ^ 0xF0); |
| 86 | } |
| 87 | } catch (InvalidKeyException ex) { |
| 88 | // swallow, since it should never happen |
| 89 | } |
| 90 | byte[] ivec = new byte[key.length]; |
| 91 | System.arraycopy(key, 0, ivec, 0, key.length); |
| 92 | byte[] cksum = Des.des_cksum(ivec, data, key); |
| 93 | return cksum; |
| 94 | } |
| 95 | |
| 96 | public boolean verifyKeyedChecksum(byte[] data, int size, |
| 97 | byte[] key, byte[] checksum, int usage) throws KrbCryptoException { |
| 98 | byte[] new_cksum = calculateKeyedChecksum(data, data.length, key, usage); |
| 99 | return isChecksumEqual(checksum, new_cksum); |
| 100 | } |
| 101 | |
| 102 | } |