jdk/src/share/classes/sun/security/krb5/internal/crypto/dk/ArcFourCrypto.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 77 41259df8c6e4
child 5506 202f599c92aa
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 77
diff changeset
     2
 * Copyright 2005-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.krb5.internal.crypto.dk;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.crypto.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.crypto.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.security.krb5.EncryptedData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.security.krb5.KrbCryptoException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.krb5.Confounder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.krb5.internal.crypto.KeyUsage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Support for ArcFour in Kerberos
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * as defined in RFC 4757.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * http://www.ietf.org/rfc/rfc4757.txt
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @author Seema Malkani
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public class ArcFourCrypto extends DkCrypto {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static final boolean debug = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static final int confounderSize = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static final byte[] ZERO_IV = new byte[] {0, 0, 0, 0, 0, 0, 0, 0};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static final int hashSize = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private final int keyLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public ArcFourCrypto(int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        keyLength = length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    protected int getKeySeedLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        return keyLength;   // bits; RC4 key material
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    protected byte[] randomToKey(byte[] in) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        // simple identity operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        return in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public byte[] stringToKey(char[] passwd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        throws GeneralSecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        return stringToKey(passwd, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * String2Key(Password)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * K = MD4(UNICODE(password))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private byte[] stringToKey(char[] secret, byte[] opaque)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        throws GeneralSecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        if (opaque != null && opaque.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            throw new RuntimeException("Invalid parameter to stringToKey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        byte[] passwd = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        byte[] digest = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            // convert ascii to unicode
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            passwd = charToUtf16(secret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            // provider for MD4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            MessageDigest md = sun.security.provider.MD4.getInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            md.update(passwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            digest = md.digest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            if (passwd != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                Arrays.fill(passwd, (byte)0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        return digest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    protected Cipher getCipher(byte[] key, byte[] ivec, int mode)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        throws GeneralSecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        // IV
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        if (ivec == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
           ivec = ZERO_IV;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        SecretKeySpec secretKey = new SecretKeySpec(key, "ARCFOUR");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        Cipher cipher = Cipher.getInstance("ARCFOUR");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        IvParameterSpec encIv = new IvParameterSpec(ivec, 0, ivec.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        cipher.init(mode, secretKey, encIv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        return cipher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public int getChecksumLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        return hashSize;  // bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Get the HMAC-MD5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    protected byte[] getHmac(byte[] key, byte[] msg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        throws GeneralSecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        SecretKey keyKi = new SecretKeySpec(key, "HmacMD5");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        Mac m = Mac.getInstance("HmacMD5");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        m.init(keyKi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        // generate hash
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        byte[] hash = m.doFinal(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        return hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Calculate the checksum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public byte[] calculateChecksum(byte[] baseKey, int usage, byte[] input,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        int start, int len) throws GeneralSecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            System.out.println("ARCFOUR: calculateChecksum with usage = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                                                usage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (!KeyUsage.isValid(usage)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            throw new GeneralSecurityException("Invalid key usage number: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                                                + usage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        byte[] Ksign = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        // Derive signing key from session key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
           byte[] ss = "signaturekey".getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
           // need to append end-of-string 00
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
           byte[] new_ss = new byte[ss.length+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
           System.arraycopy(ss, 0, new_ss, 0, ss.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
           Ksign = getHmac(baseKey, new_ss);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            GeneralSecurityException gse =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                new GeneralSecurityException("Calculate Checkum Failed!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            gse.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            throw gse;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        // get the salt using key usage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        byte[] salt = getSalt(usage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        // Generate checksum of message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        MessageDigest messageDigest = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            messageDigest = MessageDigest.getInstance("MD5");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            GeneralSecurityException gse =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                new GeneralSecurityException("Calculate Checkum Failed!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            gse.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            throw gse;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        messageDigest.update(salt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        messageDigest.update(input, start, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        byte[] md5tmp = messageDigest.digest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        // Generate checksum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        byte[] hmac = getHmac(Ksign, md5tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            traceOutput("hmac", hmac, 0, hmac.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        if (hmac.length == getChecksumLength()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            return hmac;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        } else if (hmac.length > getChecksumLength()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            byte[] buf = new byte[getChecksumLength()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            System.arraycopy(hmac, 0, buf, 0, buf.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            return buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            throw new GeneralSecurityException("checksum size too short: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                        hmac.length + "; expecting : " + getChecksumLength());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * Performs encryption of Sequence Number using derived key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    public byte[] encryptSeq(byte[] baseKey, int usage,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        byte[] checksum, byte[] plaintext, int start, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        throws GeneralSecurityException, KrbCryptoException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        if (!KeyUsage.isValid(usage)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            throw new GeneralSecurityException("Invalid key usage number: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                                                + usage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        // derive encryption for sequence number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        byte[] salt = new byte[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        byte[] kSeq = getHmac(baseKey, salt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        // derive new encryption key salted with sequence number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        kSeq = getHmac(kSeq, checksum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        Cipher cipher = Cipher.getInstance("ARCFOUR");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        SecretKeySpec secretKey = new SecretKeySpec(kSeq, "ARCFOUR");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        byte[] output = cipher.doFinal(plaintext, start, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        return output;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * Performs decryption of Sequence Number using derived key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    public byte[] decryptSeq(byte[] baseKey, int usage,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        byte[] checksum, byte[] ciphertext, int start, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        throws GeneralSecurityException, KrbCryptoException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        if (!KeyUsage.isValid(usage)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            throw new GeneralSecurityException("Invalid key usage number: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                                                + usage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        // derive decryption for sequence number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        byte[] salt = new byte[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        byte[] kSeq = getHmac(baseKey, salt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        // derive new encryption key salted with sequence number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        kSeq = getHmac(kSeq, checksum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        Cipher cipher = Cipher.getInstance("ARCFOUR");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        SecretKeySpec secretKey = new SecretKeySpec(kSeq, "ARCFOUR");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        byte[] output = cipher.doFinal(ciphertext, start, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        return output;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * Performs encryption using derived key; adds confounder.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    public byte[] encrypt(byte[] baseKey, int usage,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        byte[] ivec, byte[] new_ivec, byte[] plaintext, int start, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        throws GeneralSecurityException, KrbCryptoException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        if (!KeyUsage.isValid(usage)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            throw new GeneralSecurityException("Invalid key usage number: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                                                 + usage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            System.out.println("ArcFour: ENCRYPT with key usage = " + usage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        // get the confounder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        byte[] confounder = Confounder.bytes(confounderSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        // add confounder to the plaintext for encryption
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        int plainSize = roundup(confounder.length + len, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        byte[] toBeEncrypted = new byte[plainSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        System.arraycopy(confounder, 0, toBeEncrypted, 0, confounder.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        System.arraycopy(plaintext, start, toBeEncrypted,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                                confounder.length, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        /* begin the encryption, compute K1 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        byte[] k1 = new byte[baseKey.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        System.arraycopy(baseKey, 0, k1, 0, baseKey.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        // get the salt using key usage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        byte[] salt = getSalt(usage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        // compute K2 using K1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        byte[] k2 = getHmac(k1, salt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        // generate checksum using K2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        byte[] checksum = getHmac(k2, toBeEncrypted);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        // compute K3 using K2 and checksum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        byte[] k3 = getHmac(k2, checksum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        Cipher cipher = Cipher.getInstance("ARCFOUR");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        SecretKeySpec secretKey = new SecretKeySpec(k3, "ARCFOUR");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        byte[] output = cipher.doFinal(toBeEncrypted, 0, toBeEncrypted.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        // encryptedData + HMAC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        byte[] result = new byte[hashSize + output.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        System.arraycopy(checksum, 0, result, 0, hashSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        System.arraycopy(output, 0, result, hashSize, output.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * Performs encryption using derived key; does not add confounder.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    public byte[] encryptRaw(byte[] baseKey, int usage,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        byte[] seqNum, byte[] plaintext, int start, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        throws GeneralSecurityException, KrbCryptoException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        if (!KeyUsage.isValid(usage)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            throw new GeneralSecurityException("Invalid key usage number: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                                                + usage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            System.out.println("\nARCFOUR: encryptRaw with usage = " + usage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        // Derive encryption key for data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        //   Key derivation salt = 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        byte[] klocal = new byte[baseKey.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        for (int i = 0; i <= 15; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            klocal[i] = (byte) (baseKey[i] ^ 0xF0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        byte[] salt = new byte[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        byte[] kcrypt = getHmac(klocal, salt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        // Note: When using this RC4 based encryption type, the sequence number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        // is always sent in big-endian rather than little-endian order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        // new encryption key salted with sequence number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        kcrypt = getHmac(kcrypt, seqNum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        Cipher cipher = Cipher.getInstance("ARCFOUR");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        SecretKeySpec secretKey = new SecretKeySpec(kcrypt, "ARCFOUR");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        byte[] output = cipher.doFinal(plaintext, start, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        return output;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * @param baseKey key from which keys are to be derived using usage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * @param ciphertext  E(Ke, conf | plaintext | padding, ivec) | H1[1..h]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    public byte[] decrypt(byte[] baseKey, int usage, byte[] ivec,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        byte[] ciphertext, int start, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        throws GeneralSecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        if (!KeyUsage.isValid(usage)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            throw new GeneralSecurityException("Invalid key usage number: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                                                + usage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            System.out.println("\nARCFOUR: DECRYPT using key usage = " + usage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        // compute K1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        byte[] k1 = new byte[baseKey.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        System.arraycopy(baseKey, 0, k1, 0, baseKey.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        // get the salt using key usage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        byte[] salt = getSalt(usage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        // compute K2 using K1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        byte[] k2 = getHmac(k1, salt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        // compute K3 using K2 and checksum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        byte[] checksum = new byte[hashSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        System.arraycopy(ciphertext, start, checksum, 0, hashSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        byte[] k3 = getHmac(k2, checksum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        // Decrypt [confounder | plaintext ] (without checksum)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        Cipher cipher = Cipher.getInstance("ARCFOUR");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        SecretKeySpec secretKey = new SecretKeySpec(k3, "ARCFOUR");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        byte[] plaintext = cipher.doFinal(ciphertext, start+hashSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                                                len-hashSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        // Verify checksum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        byte[] calculatedHmac = getHmac(k2, plaintext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            traceOutput("calculated Hmac", calculatedHmac, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                                calculatedHmac.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            traceOutput("message Hmac", ciphertext, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                                hashSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        boolean cksumFailed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        if (calculatedHmac.length >= hashSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            for (int i = 0; i < hashSize; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                if (calculatedHmac[i] != ciphertext[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                    cksumFailed = true;
77
41259df8c6e4 6664612: debug output leaked
weijun
parents: 2
diff changeset
   400
                    if (debug) {
41259df8c6e4 6664612: debug output leaked
weijun
parents: 2
diff changeset
   401
                        System.err.println("Checksum failed !");
41259df8c6e4 6664612: debug output leaked
weijun
parents: 2
diff changeset
   402
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        if (cksumFailed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            throw new GeneralSecurityException("Checksum failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        // Get rid of confounder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        // [ confounder | plaintext ]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        byte[] output = new byte[plaintext.length - confounderSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        System.arraycopy(plaintext, confounderSize, output, 0, output.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        return output;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * Decrypts data using specified key and initial vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * @param baseKey encryption key to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * @param ciphertext  encrypted data to be decrypted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * @param usage ignored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    public byte[] decryptRaw(byte[] baseKey, int usage, byte[] ivec,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        byte[] ciphertext, int start, int len, byte[] seqNum)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        throws GeneralSecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        if (!KeyUsage.isValid(usage)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            throw new GeneralSecurityException("Invalid key usage number: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                                                + usage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            System.out.println("\nARCFOUR: decryptRaw with usage = " + usage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        // Derive encryption key for data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        //   Key derivation salt = 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        byte[] klocal = new byte[baseKey.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        for (int i = 0; i <= 15; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            klocal[i] = (byte) (baseKey[i] ^ 0xF0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        byte[] salt = new byte[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        byte[] kcrypt = getHmac(klocal, salt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        // need only first 4 bytes of sequence number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        byte[] sequenceNum = new byte[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        System.arraycopy(seqNum, 0, sequenceNum, 0, sequenceNum.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        // new encryption key salted with sequence number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        kcrypt = getHmac(kcrypt, sequenceNum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        Cipher cipher = Cipher.getInstance("ARCFOUR");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        SecretKeySpec secretKey = new SecretKeySpec(kcrypt, "ARCFOUR");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        byte[] output = cipher.doFinal(ciphertext, start, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        return output;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    // get the salt using key usage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    private byte[] getSalt(int usage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        int ms_usage = arcfour_translate_usage(usage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        byte[] salt = new byte[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        salt[0] = (byte)(ms_usage & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        salt[1] = (byte)((ms_usage >> 8) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        salt[2] = (byte)((ms_usage >> 16) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        salt[3] = (byte)((ms_usage >> 24) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        return salt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    // Key usage translation for MS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    private int arcfour_translate_usage(int usage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        switch (usage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            case 3: return 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            case 9: return 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            case 23: return 13;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            default: return usage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
}