jdk/src/jdk.security.auth/share/classes/com/sun/security/auth/module/Crypt.java
author duke
Wed, 05 Jul 2017 21:06:18 +0200
changeset 34465 41a1258588da
parent 31538 0981099a3e54
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2000, 2003, Oracle and/or its affiliates. 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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/*      Copyright (c) 1988 AT&T */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
/*        All Rights Reserved   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * Implements the UNIX crypt(3) function, based on a direct port of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * libc crypt function.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * From the crypt man page:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * crypt() is the password encryption routine, based on the NBS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Data  Encryption  Standard,  with variations intended (among
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * other things) to frustrate use of  hardware  implementations
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * of the DES for key search.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * The first argument to crypt() is  normally  a  user's  typed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * password.   The  second  is a 2-character string chosen from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * the set [a-zA-Z0-9./].  the  salt string is used to perturb
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * the DES algorithm in one
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * of 4096 different ways, after which the password is used  as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * the  key  to  encrypt  repeatedly  a  constant  string.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * returned value points to the encrypted password, in the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * alphabet as the salt.  The first two characters are the salt
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @author Roland Schemers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
package com.sun.security.auth.module;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
class Crypt {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
/* EXPORT DELETE START */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private static final byte[] IP = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        58, 50, 42, 34, 26, 18, 10, 2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        60, 52, 44, 36, 28, 20, 12, 4,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        62, 54, 46, 38, 30, 22, 14, 6,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        64, 56, 48, 40, 32, 24, 16, 8,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        57, 49, 41, 33, 25, 17, 9, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        59, 51, 43, 35, 27, 19, 11, 3,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        61, 53, 45, 37, 29, 21, 13, 5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        63, 55, 47, 39, 31, 23, 15, 7,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private static final byte[] FP = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        40, 8, 48, 16, 56, 24, 64, 32,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        39, 7, 47, 15,  55, 23, 63, 31,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        38, 6, 46, 14, 54, 22, 62, 30,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        37, 5, 45, 13, 53, 21, 61, 29,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        36, 4, 44, 12, 52, 20, 60, 28,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        35, 3, 43, 11, 51, 19, 59, 27,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        34, 2, 42, 10, 50, 18, 58, 26,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        33, 1, 41, 9, 49, 17, 57, 25,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private static final byte[] PC1_C = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        57, 49, 41, 33, 25, 17, 9,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        1, 58, 50, 42, 34, 26, 18,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        10, 2, 59, 51, 43, 35, 27,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        19, 11, 3, 60, 52, 44, 36,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private static final byte[] PC1_D = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        63, 55, 47, 39, 31, 23, 15,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        7, 62, 54, 46, 38, 30, 22,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        14, 6, 61, 53, 45, 37, 29,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        21, 13, 5, 28, 20, 12, 4,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private static final byte[] shifts = { 1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1, };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private static final byte[] PC2_C = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        14, 17, 11, 24, 1, 5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        3, 28, 15, 6, 21, 10,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        23, 19, 12, 4, 26, 8,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        16, 7, 27, 20, 13, 2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private static final byte[] PC2_D = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        41,52,31,37,47,55,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        30,40,51,45,33,48,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        44,49,39,56,34,53,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        46,42,50,36,29,32,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private byte[] C = new byte[28];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    private byte[] D = new byte[28];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private byte[] KS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    private byte[] E = new byte[48];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    private static final byte[] e2 = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        32, 1, 2, 3, 4, 5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        4, 5, 6, 7, 8, 9,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        8, 9,10,11,12,13,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        12,13,14,15,16,17,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        16,17,18,19,20,21,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        20,21,22,23,24,25,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        24,25,26,27,28,29,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        28,29,30,31,32, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    private void setkey(byte[] key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        int i, j, k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        byte t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        if (KS == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            KS = new byte[16*48];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        for (i = 0; i < 28; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                C[i] = key[PC1_C[i]-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                D[i] = key[PC1_D[i]-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        for (i = 0; i < 16; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                for (k = 0; k < shifts[i]; k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                        t = C[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                        for (j = 0; j < 28-1; j++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                                C[j] = C[j+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                        C[27] = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                        t = D[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                        for (j = 0; j < 28-1; j++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                                D[j] = D[j+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                        D[27] = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                for (j = 0; j < 24; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                        int index = i * 48;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                        KS[index+j] = C[PC2_C[j]-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                        KS[index+j+24] = D[PC2_D[j]-28-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        for (i = 0; i < 48; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                E[i] = e2[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    private static final byte[][] S = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        {14, 4,13, 1, 2,15,11, 8, 3,10, 6,12, 5, 9, 0, 7,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        0,15, 7, 4,14, 2,13, 1,10, 6,12,11, 9, 5, 3, 8,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        4, 1,14, 8,13, 6, 2,11,15,12, 9, 7, 3,10, 5, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        15,12, 8, 2, 4, 9, 1, 7, 5,11, 3,14,10, 0, 6,13},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        {15, 1, 8,14, 6,11, 3, 4, 9, 7, 2,13,12, 0, 5,10,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        3,13, 4, 7,15, 2, 8,14,12, 0, 1,10, 6, 9,11, 5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        0,14, 7,11,10, 4,13, 1, 5, 8,12, 6, 9, 3, 2,15,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        13, 8,10, 1, 3,15, 4, 2,11, 6, 7,12, 0, 5,14, 9},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        {10, 0, 9,14, 6, 3,15, 5, 1,13,12, 7,11, 4, 2, 8,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        13, 7, 0, 9, 3, 4, 6,10, 2, 8, 5,14,12,11,15, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        13, 6, 4, 9, 8,15, 3, 0,11, 1, 2,12, 5,10,14, 7,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
         1,10,13, 0, 6, 9, 8, 7, 4,15,14, 3,11, 5, 2,12},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        {7,13,14, 3, 0, 6, 9,10, 1, 2, 8, 5,11,12, 4,15,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        13, 8,11, 5, 6,15, 0, 3, 4, 7, 2,12, 1,10,14, 9,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        10, 6, 9, 0,12,11, 7,13,15, 1, 3,14, 5, 2, 8, 4,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
         3,15, 0, 6,10, 1,13, 8, 9, 4, 5,11,12, 7, 2,14},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        {2,12, 4, 1, 7,10,11, 6, 8, 5, 3,15,13, 0,14, 9,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        14,11, 2,12, 4, 7,13, 1, 5, 0,15,10, 3, 9, 8, 6,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
         4, 2, 1,11,10,13, 7, 8,15, 9,12, 5, 6, 3, 0,14,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        11, 8,12, 7, 1,14, 2,13, 6,15, 0, 9,10, 4, 5, 3},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        {12, 1,10,15, 9, 2, 6, 8, 0,13, 3, 4,14, 7, 5,11,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        10,15, 4, 2, 7,12, 9, 5, 6, 1,13,14, 0,11, 3, 8,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
         9,14,15, 5, 2, 8,12, 3, 7, 0, 4,10, 1,13,11, 6,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
         4, 3, 2,12, 9, 5,15,10,11,14, 1, 7, 6, 0, 8,13},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        {4,11, 2,14,15, 0, 8,13, 3,12, 9, 7, 5,10, 6, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        13, 0,11, 7, 4, 9, 1,10,14, 3, 5,12, 2,15, 8, 6,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
         1, 4,11,13,12, 3, 7,14,10,15, 6, 8, 0, 5, 9, 2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
         6,11,13, 8, 1, 4,10, 7, 9, 5, 0,15,14, 2, 3,12},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        {13, 2, 8, 4, 6,15,11, 1,10, 9, 3,14, 5, 0,12, 7,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
         1,15,13, 8,10, 3, 7, 4,12, 5, 6,11, 0,14, 9, 2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
         7,11, 4, 1, 9,12,14, 2, 0, 6,10,13,15, 3, 5, 8,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
         2, 1,14, 7, 4,10, 8,13,15,12, 9, 0, 3, 5, 6,11},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    private static final byte[] P = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        16, 7,20,21,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        29,12,28,17,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
         1,15,23,26,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
         5,18,31,10,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
         2, 8,24,14,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        32,27, 3, 9,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        19,13,30, 6,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        22,11, 4,25,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    private byte[]  L = new byte[64];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    private byte[] tempL = new byte[32];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    private byte[] f = new byte[32];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    private byte[] preS = new byte[48];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    private void encrypt(byte[] block,int fake) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        int     i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        int t, j, k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        int R = 32; // &L[32]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if (KS == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            KS = new byte[16*48];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        for(j=0; j < 64; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            L[j] = block[IP[j]-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        for(i=0; i < 16; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            int index = i * 48;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            for(j=0; j < 32; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                tempL[j] = L[R+j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            for(j=0; j < 48; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                preS[j] = (byte) (L[R+E[j]-1] ^ KS[index+j]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            for(j=0; j < 8; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                t = 6*j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                k = S[j][(preS[t+0]<<5)+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                         (preS[t+1]<<3)+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                         (preS[t+2]<<2)+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                         (preS[t+3]<<1)+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                         (preS[t+4]<<0)+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                         (preS[t+5]<<4)];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                t = 4*j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                f[t+0] = (byte) ((k>>3)&01);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                f[t+1] = (byte) ((k>>2)&01);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                f[t+2] = (byte) ((k>>1)&01);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                f[t+3] = (byte) ((k>>0)&01);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            for(j=0; j < 32; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                        L[R+j] = (byte) (L[j] ^ f[P[j]-1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            for(j=0; j < 32; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                        L[j] = tempL[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        for(j=0; j < 32; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            t = L[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            L[j] = L[R+j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            L[R+j] = (byte)t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        for(j=0; j < 64; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                block[j] = L[FP[j]-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
/* EXPORT DELETE END */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * Creates a new Crypt object for use with the crypt method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    public Crypt()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        // does nothing at this time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * Implements the libc crypt(3) function.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * @param pw the password to "encrypt".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @param salt the salt to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * @return A new byte[13] array that contains the encrypted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * password. The first two characters are the salt.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    public synchronized byte[] crypt(byte[] pw, byte[] salt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        int c, i, j, pwi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        byte temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        byte[] block = new byte[66];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        byte[] iobuf = new byte[13];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
/* EXPORT DELETE START */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        pwi = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        for(i=0; pwi < pw.length && i < 64; pwi++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            c = pw[pwi];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            for(j=0; j < 7; j++, i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                block[i] = (byte) ((c>>(6-j)) & 01);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        setkey(block);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        for(i=0; i < 66; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            block[i] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        for(i=0; i < 2; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            c = salt[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            iobuf[i] = (byte)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            if(c > 'Z')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                c -= 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            if(c > '9')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                c -= 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            c -= '.';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            for(j=0; j < 6; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                if( ((c>>j) & 01) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                    temp = E[6*i+j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                    E[6*i+j] = E[6*i+j+24];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    E[6*i+j+24] = temp;
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        for(i=0; i < 25; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                encrypt(block,0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        for(i=0; i < 11; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            c = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            for(j=0; j < 6; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                c <<= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                c |= block[6*i+j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            c += '.';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            if(c > '9') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                c += 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            if(c > 'Z') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                c += 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            iobuf[i+2] = (byte)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        //iobuf[i+2] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        if(iobuf[1] == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            iobuf[1] = iobuf[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
/* EXPORT DELETE END */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        return(iobuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * program to test the crypt routine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * The first parameter is the cleartext password, the second is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * the salt to use. The salt should be two characters from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * set [a-zA-Z0-9./]. Outputs the crypt result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * @param arg command line arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 25859
diff changeset
   380
    public static void main(String[] arg) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        if (arg.length!=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            System.err.println("usage: Crypt password salt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        Crypt c = new Crypt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        try {
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 25859
diff changeset
   389
            byte[] result = c.crypt
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                (arg[0].getBytes("ISO-8859-1"), arg[1].getBytes("ISO-8859-1"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            for (int i=0; i<result.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                System.out.println(" "+i+" "+(char)result[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        } catch (java.io.UnsupportedEncodingException uee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            // cannot happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
}