1/*
2 * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package sun.security.krb5.internal.crypto;
27
28import sun.security.krb5.Checksum;
29import sun.security.krb5.KrbCryptoException;
30import sun.security.krb5.internal.*;
31import javax.crypto.spec.DESKeySpec;
32import java.security.InvalidKeyException;
33import java.security.GeneralSecurityException;
34
35/**
36 * This class encapsulates the checksum type for HMAC RC4
37 *
38 * @author Seema Malkani
39 */
40
41public class HmacMd5ArcFourCksumType extends CksumType {
42
43    public HmacMd5ArcFourCksumType() {
44    }
45
46    public int confounderSize() {
47        return 8;
48    }
49
50    public int cksumType() {
51        return Checksum.CKSUMTYPE_HMAC_MD5_ARCFOUR;
52    }
53
54    public boolean isSafe() {
55        return true;
56    }
57
58    public int cksumSize() {
59        return 16;  // bytes
60    }
61
62    public int keyType() {
63        return Krb5.KEYTYPE_ARCFOUR_HMAC;
64    }
65
66    public int keySize() {
67        return 16;   // bytes
68    }
69
70    public byte[] calculateChecksum(byte[] data, int size) {
71        return null;
72    }
73
74    /**
75     * Calculates keyed checksum.
76     * @param data the data used to generate the checksum.
77     * @param size length of the data.
78     * @param key the key used to encrypt the checksum.
79     * @return keyed checksum.
80     */
81    public byte[] calculateKeyedChecksum(byte[] data, int size, byte[] key,
82        int usage) throws KrbCryptoException {
83
84         try {
85             return ArcFourHmac.calculateChecksum(key, usage, data, 0, size);
86         } catch (GeneralSecurityException e) {
87             KrbCryptoException ke = new KrbCryptoException(e.getMessage());
88             ke.initCause(e);
89             throw ke;
90         }
91    }
92
93    /**
94     * Verifies keyed checksum.
95     * @param data the data.
96     * @param size the length of data.
97     * @param key the key used to encrypt the checksum.
98     * @param checksum the checksum.
99     * @return true if verification is successful.
100     */
101    public boolean verifyKeyedChecksum(byte[] data, int size,
102        byte[] key, byte[] checksum, int usage) throws KrbCryptoException {
103
104         try {
105             byte[] newCksum = ArcFourHmac.calculateChecksum(key, usage,
106                 data, 0, size);
107
108             return isChecksumEqual(checksum, newCksum);
109         } catch (GeneralSecurityException e) {
110             KrbCryptoException ke = new KrbCryptoException(e.getMessage());
111             ke.initCause(e);
112             throw ke;
113         }
114     }
115}
116