src/java.security.jgss/share/classes/sun/security/krb5/internal/crypto/Des.java
author weijun
Tue, 14 Aug 2018 22:39:34 +0800
changeset 51398 3c389a284345
parent 47216 71c04702a3d5
permissions -rw-r--r--
8209416: Refactoring GetPropertyAction calls in security libs Reviewed-by: xuelei, rriggs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3949
diff changeset
     6
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3949
diff changeset
     8
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3949
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3949
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3949
diff changeset
    22
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *  (C) Copyright IBM Corp. 1999 All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *  Copyright 1997 The Open Group Research Institute.  All rights reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
package sun.security.krb5.internal.crypto;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.crypto.Cipher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.crypto.spec.SecretKeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.crypto.SecretKeyFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.crypto.SecretKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.GeneralSecurityException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.crypto.spec.IvParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.security.krb5.KrbCryptoException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.Arrays;
3949
00603a93b589 6862679: ESC: AD Authentication with user with umlauts fails
weijun
parents: 2
diff changeset
    41
import sun.security.action.GetPropertyAction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public final class Des {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
3949
00603a93b589 6862679: ESC: AD Authentication with user with umlauts fails
weijun
parents: 2
diff changeset
    45
    // RFC 3961 demands that UTF-8 encoding be used in DES's
00603a93b589 6862679: ESC: AD Authentication with user with umlauts fails
weijun
parents: 2
diff changeset
    46
    // string-to-key function. For historical reasons, some
00603a93b589 6862679: ESC: AD Authentication with user with umlauts fails
weijun
parents: 2
diff changeset
    47
    // implementations use a locale-specific encoding. Even
00603a93b589 6862679: ESC: AD Authentication with user with umlauts fails
weijun
parents: 2
diff changeset
    48
    // so, when the client and server use different locales,
00603a93b589 6862679: ESC: AD Authentication with user with umlauts fails
weijun
parents: 2
diff changeset
    49
    // they must agree on a common value, normally the one
00603a93b589 6862679: ESC: AD Authentication with user with umlauts fails
weijun
parents: 2
diff changeset
    50
    // used when the password is set/reset.
00603a93b589 6862679: ESC: AD Authentication with user with umlauts fails
weijun
parents: 2
diff changeset
    51
    //
00603a93b589 6862679: ESC: AD Authentication with user with umlauts fails
weijun
parents: 2
diff changeset
    52
    // The following system property is provided to perform the
00603a93b589 6862679: ESC: AD Authentication with user with umlauts fails
weijun
parents: 2
diff changeset
    53
    // string-to-key encoding. When set, the specified charset
00603a93b589 6862679: ESC: AD Authentication with user with umlauts fails
weijun
parents: 2
diff changeset
    54
    // name is used. Otherwise, the system default charset.
00603a93b589 6862679: ESC: AD Authentication with user with umlauts fails
weijun
parents: 2
diff changeset
    55
51398
3c389a284345 8209416: Refactoring GetPropertyAction calls in security libs
weijun
parents: 47216
diff changeset
    56
    private final static String CHARSET = GetPropertyAction
3c389a284345 8209416: Refactoring GetPropertyAction calls in security libs
weijun
parents: 47216
diff changeset
    57
            .privilegedGetProperty("sun.security.krb5.msinterop.des.s2kcharset");
3949
00603a93b589 6862679: ESC: AD Authentication with user with umlauts fails
weijun
parents: 2
diff changeset
    58
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private static final long[] bad_keys = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        0x0101010101010101L, 0xfefefefefefefefeL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        0x1f1f1f1f1f1f1f1fL, 0xe0e0e0e0e0e0e0e0L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        0x01fe01fe01fe01feL, 0xfe01fe01fe01fe01L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        0x1fe01fe00ef10ef1L, 0xe01fe01ff10ef10eL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        0x01e001e001f101f1L, 0xe001e001f101f101L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        0x1ffe1ffe0efe0efeL, 0xfe1ffe1ffe0efe0eL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        0x011f011f010e010eL, 0x1f011f010e010e01L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        0xe0fee0fef1fef1feL, 0xfee0fee0fef1fef1L
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private static final byte[] good_parity = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        1,       1,   2,   2,   4,   4,   7,   7,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        8,   8,   11,  11,  13,  13,  14,  14,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        16,  16,  19,  19,  21,  21,  22,  22,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        25,  25,  26,  26,  28,  28,  31,  31,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        32,  32,  35,  35,  37,  37,  38,  38,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        41,  41,  42,  42,  44,  44,  47,  47,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        49,  49,  50,  50,  52,  52,  55,  55,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        56,  56,  59,  59,  61,  61,  62,  62,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        64,  64,  67,  67,  69,  69,  70,  70,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        73,  73,  74,  74,  76,  76,  79,  79,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        81,  81,  82,  82,  84,  84,  87,  87,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        88,  88,  91,  91,  93,  93,  94,  94,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        97,  97,  98,  98,  100, 100, 103, 103,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        104, 104, 107, 107, 109, 109, 110, 110,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        112, 112, 115, 115, 117, 117, 118, 118,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        121, 121, 122, 122, 124, 124, 127, 127,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        (byte)128, (byte)128, (byte)131, (byte)131,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        (byte)133, (byte)133, (byte)134, (byte)134,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        (byte)137, (byte)137, (byte)138, (byte)138,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        (byte)140, (byte)140, (byte)143, (byte)143,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        (byte)145, (byte)145, (byte)146, (byte)146,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        (byte)148, (byte)148, (byte)151, (byte)151,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        (byte)152, (byte)152, (byte)155, (byte)155,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        (byte)157, (byte)157, (byte)158, (byte)158,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        (byte)161, (byte)161, (byte)162, (byte)162,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        (byte)164, (byte)164, (byte)167, (byte)167,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        (byte)168, (byte)168, (byte)171, (byte)171,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        (byte)173, (byte)173, (byte)174, (byte)174,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        (byte)176, (byte)176, (byte)179, (byte)179,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        (byte)181, (byte)181, (byte)182, (byte)182,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        (byte)185, (byte)185, (byte)186, (byte)186,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        (byte)188, (byte)188, (byte)191, (byte)191,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        (byte)193, (byte)193, (byte)194, (byte)194,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        (byte)196, (byte)196, (byte)199, (byte)199,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        (byte)200, (byte)200, (byte)203, (byte)203,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        (byte)205, (byte)205, (byte)206, (byte)206,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        (byte)208, (byte)208, (byte)211, (byte)211,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        (byte)213, (byte)213, (byte)214, (byte)214,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        (byte)217, (byte)217, (byte)218, (byte)218,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        (byte)220, (byte)220, (byte)223, (byte)223,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        (byte)224, (byte)224, (byte)227, (byte)227,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        (byte)229, (byte)229, (byte)230, (byte)230,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        (byte)233, (byte)233, (byte)234, (byte)234,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        (byte)236, (byte)236, (byte)239, (byte)239,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        (byte)241, (byte)241, (byte)242, (byte)242,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        (byte)244, (byte)244, (byte)247, (byte)247,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        (byte)248, (byte)248, (byte)251, (byte)251,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        (byte)253, (byte)253, (byte)254, (byte)254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    public static final byte[] set_parity(byte[] key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        for (int i=0; i < 8; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            key[i] = good_parity[key[i] & 0xff];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    public static final long set_parity(long key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        return octet2long(set_parity(long2octet(key)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public static final boolean bad_key(long key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        for (int i = 0; i < bad_keys.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            if (bad_keys[i] == key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    public static final boolean bad_key(byte[] key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        return bad_key(octet2long(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    public static long octet2long(byte[] input) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        return octet2long(input, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    public static long octet2long(byte[] input, int offset) {   //convert a 8-byte to a long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        long result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        for (int i = 0; i < 8; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            if (i + offset < input.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                result |= (((long)input[i + offset]) & 0xffL) << ((7 - i) * 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public static byte[] long2octet(long input) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        byte[] output = new byte[8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        for (int i = 0; i < 8; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            output[i] = (byte)((input >>> ((7 - i) * 8)) & 0xffL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        return output;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    public static void long2octet(long input, byte[] output) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        long2octet(input, output, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    public static void long2octet(long input, byte[] output, int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        for (int i = 0; i < 8; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            if (i + offset < output.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                output[i + offset] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    (byte)((input >>> ((7 - i) * 8)) & 0xffL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * Creates a DES cipher in Electronic Codebook mode, with no padding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @param input plain text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @param output the buffer for the result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @param key DES the key to encrypt the text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @param ivec initialization vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @created by Yanni Zhang, Dec 6 99.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    public static void cbc_encrypt (
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                                    byte[] input,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                                    byte[] output,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                                    byte[] key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                                    byte[] ivec,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                                    boolean encrypt) throws KrbCryptoException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        Cipher cipher = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            cipher = Cipher.getInstance("DES/CBC/NoPadding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            KrbCryptoException ke = new KrbCryptoException("JCE provider may not be installed. "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                                                           + e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            ke.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            throw ke;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        IvParameterSpec params = new IvParameterSpec(ivec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        SecretKeySpec skSpec = new SecretKeySpec(key, "DES");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            //                  SecretKey sk = skf.generateSecret(skSpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            SecretKey sk = (SecretKey) skSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            if (encrypt)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                cipher.init(Cipher.ENCRYPT_MODE, sk, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                cipher.init(Cipher.DECRYPT_MODE, sk, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            byte[] result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            result = cipher.doFinal(input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            System.arraycopy(result, 0, output, 0, result.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            KrbCryptoException ke = new KrbCryptoException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            ke.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            throw ke;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * Generates DES key from the password.
32003
acb12269398a 8132130: some docs cleanup
avstepan
parents: 25859
diff changeset
   228
     * @param passwdChars a char[] used to create the key.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @return DES key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * @modified by Yanni Zhang, Dec 6, 99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    public static long char_to_key(char[] passwdChars) throws KrbCryptoException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        long key = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        long octet, octet1, octet2 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        byte[] cbytes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        // Convert password to byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        try {
3949
00603a93b589 6862679: ESC: AD Authentication with user with umlauts fails
weijun
parents: 2
diff changeset
   240
            if (CHARSET == null) {
00603a93b589 6862679: ESC: AD Authentication with user with umlauts fails
weijun
parents: 2
diff changeset
   241
                cbytes = (new String(passwdChars)).getBytes();
00603a93b589 6862679: ESC: AD Authentication with user with umlauts fails
weijun
parents: 2
diff changeset
   242
            } else {
00603a93b589 6862679: ESC: AD Authentication with user with umlauts fails
weijun
parents: 2
diff changeset
   243
                cbytes = (new String(passwdChars)).getBytes(CHARSET);
00603a93b589 6862679: ESC: AD Authentication with user with umlauts fails
weijun
parents: 2
diff changeset
   244
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            // clear-up sensitive information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            if (cbytes != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                Arrays.fill(cbytes, 0, cbytes.length, (byte) 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            KrbCryptoException ce =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                new KrbCryptoException("Unable to convert passwd, " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            ce.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            throw ce;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        // pad data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        byte[] passwdBytes = pad(cbytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        byte[] newkey = new byte[8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        int length = (passwdBytes.length / 8) + (passwdBytes.length % 8  == 0 ? 0 : 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        for (int i = 0; i < length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            octet = octet2long(passwdBytes, i * 8) & 0x7f7f7f7f7f7f7f7fL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            if (i % 2 == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                octet1 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                for (int j = 0; j < 64; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                    octet1 |= ((octet & (1L << j)) >>> j) << (63 - j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                octet = octet1 >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            key ^= (octet << 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        key = set_parity(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        if (bad_key(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            byte [] temp = long2octet(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            temp[7] ^= 0xf0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            key = octet2long(temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        newkey = des_cksum(long2octet(key), passwdBytes, long2octet(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        key = octet2long(set_parity(newkey));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        if (bad_key(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            byte [] temp = long2octet(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            temp[7] ^= 0xf0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            key = octet2long(temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        // clear-up sensitive information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        if (cbytes != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            Arrays.fill(cbytes, 0, cbytes.length, (byte) 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        if (passwdBytes != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            Arrays.fill(passwdBytes, 0, passwdBytes.length, (byte) 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * Encrypts the message blocks using DES CBC and output the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * final block of 8-byte ciphertext.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @param ivec Initialization vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @param msg Input message as an byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @param key DES key to encrypt the message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * @return the last block of ciphertext.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * @created by Yanni Zhang, Dec 6, 99.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    public static byte[] des_cksum(byte[] ivec, byte[] msg, byte[] key) throws KrbCryptoException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        Cipher cipher = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        byte[] result = new byte[8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        try{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            cipher = Cipher.getInstance("DES/CBC/NoPadding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            KrbCryptoException ke = new KrbCryptoException("JCE provider may not be installed. "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                                                           + e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            ke.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            throw ke;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        IvParameterSpec params = new IvParameterSpec(ivec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        SecretKeySpec skSpec = new SecretKeySpec(key, "DES");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            // SecretKey sk = skf.generateSecret(skSpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            SecretKey sk = (SecretKey) skSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            cipher.init(Cipher.ENCRYPT_MODE, sk, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            for (int i = 0; i < msg.length / 8; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                result = cipher.doFinal(msg, i * 8, 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                cipher.init(Cipher.ENCRYPT_MODE, sk, (new IvParameterSpec(result)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            KrbCryptoException ke = new KrbCryptoException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            ke.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            throw ke;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * Pads the data so that its length is a multiple of 8 bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * @param data the raw data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * @return the data being padded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * @created by Yanni Zhang, Dec 6 99. //Kerberos does not use PKCS5 padding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    static byte[] pad(byte[] data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        int len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        if (data.length < 8) len = data.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        else len = data.length % 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        if (len == 0) return data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            byte[] padding = new byte[ 8 - len + data.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            for (int i = padding.length - 1; i > data.length - 1; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                padding[i] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            System.arraycopy(data, 0, padding, 0, data.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            return padding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    // Caller is responsible for clearing password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    public static byte[] string_to_key_bytes(char[] passwdChars)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        throws KrbCryptoException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        return long2octet(char_to_key(passwdChars));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
}