0N/A/*
2362N/A * Copyright (c) 2000, 2003, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/* Copyright (c) 1988 AT&T */
0N/A/* All Rights Reserved */
0N/A
0N/A/**
0N/A * Implements the UNIX crypt(3) function, based on a direct port of the
0N/A * libc crypt function.
0N/A *
0N/A * <p>
0N/A * From the crypt man page:
0N/A * <p>
0N/A * crypt() is the password encryption routine, based on the NBS
0N/A * Data Encryption Standard, with variations intended (among
0N/A * other things) to frustrate use of hardware implementations
0N/A * of the DES for key search.
0N/A * <p>
0N/A * The first argument to crypt() is normally a user's typed
0N/A * password. The second is a 2-character string chosen from
0N/A * the set [a-zA-Z0-9./]. the salt string is used to perturb
0N/A * the DES algorithm in one
0N/A * of 4096 different ways, after which the password is used as
0N/A * the key to encrypt repeatedly a constant string. The
0N/A * returned value points to the encrypted password, in the same
0N/A * alphabet as the salt. The first two characters are the salt
0N/A * itself.
0N/A *
0N/A * @author Roland Schemers
0N/A */
0N/A
0N/Apackage com.sun.security.auth.module;
0N/A
0N/Aclass Crypt {
0N/A
0N/A/* EXPORT DELETE START */
0N/A
0N/A private static final byte[] IP = {
0N/A 58, 50, 42, 34, 26, 18, 10, 2,
0N/A 60, 52, 44, 36, 28, 20, 12, 4,
0N/A 62, 54, 46, 38, 30, 22, 14, 6,
0N/A 64, 56, 48, 40, 32, 24, 16, 8,
0N/A 57, 49, 41, 33, 25, 17, 9, 1,
0N/A 59, 51, 43, 35, 27, 19, 11, 3,
0N/A 61, 53, 45, 37, 29, 21, 13, 5,
0N/A 63, 55, 47, 39, 31, 23, 15, 7,
0N/A };
0N/A
0N/A private static final byte[] FP = {
0N/A 40, 8, 48, 16, 56, 24, 64, 32,
0N/A 39, 7, 47, 15, 55, 23, 63, 31,
0N/A 38, 6, 46, 14, 54, 22, 62, 30,
0N/A 37, 5, 45, 13, 53, 21, 61, 29,
0N/A 36, 4, 44, 12, 52, 20, 60, 28,
0N/A 35, 3, 43, 11, 51, 19, 59, 27,
0N/A 34, 2, 42, 10, 50, 18, 58, 26,
0N/A 33, 1, 41, 9, 49, 17, 57, 25,
0N/A };
0N/A
0N/A private static final byte[] PC1_C = {
0N/A 57, 49, 41, 33, 25, 17, 9,
0N/A 1, 58, 50, 42, 34, 26, 18,
0N/A 10, 2, 59, 51, 43, 35, 27,
0N/A 19, 11, 3, 60, 52, 44, 36,
0N/A };
0N/A
0N/A private static final byte[] PC1_D = {
0N/A 63, 55, 47, 39, 31, 23, 15,
0N/A 7, 62, 54, 46, 38, 30, 22,
0N/A 14, 6, 61, 53, 45, 37, 29,
0N/A 21, 13, 5, 28, 20, 12, 4,
0N/A };
0N/A
0N/A private static final byte[] shifts = { 1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1, };
0N/A
0N/A private static final byte[] PC2_C = {
0N/A 14, 17, 11, 24, 1, 5,
0N/A 3, 28, 15, 6, 21, 10,
0N/A 23, 19, 12, 4, 26, 8,
0N/A 16, 7, 27, 20, 13, 2,
0N/A };
0N/A
0N/A private static final byte[] PC2_D = {
0N/A 41,52,31,37,47,55,
0N/A 30,40,51,45,33,48,
0N/A 44,49,39,56,34,53,
0N/A 46,42,50,36,29,32,
0N/A };
0N/A
0N/A private byte[] C = new byte[28];
0N/A private byte[] D = new byte[28];
0N/A
0N/A private byte[] KS;
0N/A
0N/A private byte[] E = new byte[48];
0N/A
0N/A private static final byte[] e2 = {
0N/A 32, 1, 2, 3, 4, 5,
0N/A 4, 5, 6, 7, 8, 9,
0N/A 8, 9,10,11,12,13,
0N/A 12,13,14,15,16,17,
0N/A 16,17,18,19,20,21,
0N/A 20,21,22,23,24,25,
0N/A 24,25,26,27,28,29,
0N/A 28,29,30,31,32, 1,
0N/A };
0N/A
0N/A private void setkey(byte[] key) {
0N/A int i, j, k;
0N/A byte t;
0N/A
0N/A if (KS == null) {
0N/A KS = new byte[16*48];
0N/A }
0N/A
0N/A for (i = 0; i < 28; i++) {
0N/A C[i] = key[PC1_C[i]-1];
0N/A D[i] = key[PC1_D[i]-1];
0N/A }
0N/A for (i = 0; i < 16; i++) {
0N/A for (k = 0; k < shifts[i]; k++) {
0N/A t = C[0];
0N/A for (j = 0; j < 28-1; j++)
0N/A C[j] = C[j+1];
0N/A C[27] = t;
0N/A t = D[0];
0N/A for (j = 0; j < 28-1; j++)
0N/A D[j] = D[j+1];
0N/A D[27] = t;
0N/A }
0N/A for (j = 0; j < 24; j++) {
0N/A int index = i * 48;
0N/A
0N/A KS[index+j] = C[PC2_C[j]-1];
0N/A KS[index+j+24] = D[PC2_D[j]-28-1];
0N/A }
0N/A }
0N/A for (i = 0; i < 48; i++)
0N/A E[i] = e2[i];
0N/A }
0N/A
0N/A
0N/A private static final byte[][] S = {
0N/A {14, 4,13, 1, 2,15,11, 8, 3,10, 6,12, 5, 9, 0, 7,
0N/A 0,15, 7, 4,14, 2,13, 1,10, 6,12,11, 9, 5, 3, 8,
0N/A 4, 1,14, 8,13, 6, 2,11,15,12, 9, 7, 3,10, 5, 0,
0N/A 15,12, 8, 2, 4, 9, 1, 7, 5,11, 3,14,10, 0, 6,13},
0N/A
0N/A {15, 1, 8,14, 6,11, 3, 4, 9, 7, 2,13,12, 0, 5,10,
0N/A 3,13, 4, 7,15, 2, 8,14,12, 0, 1,10, 6, 9,11, 5,
0N/A 0,14, 7,11,10, 4,13, 1, 5, 8,12, 6, 9, 3, 2,15,
0N/A 13, 8,10, 1, 3,15, 4, 2,11, 6, 7,12, 0, 5,14, 9},
0N/A
0N/A {10, 0, 9,14, 6, 3,15, 5, 1,13,12, 7,11, 4, 2, 8,
0N/A 13, 7, 0, 9, 3, 4, 6,10, 2, 8, 5,14,12,11,15, 1,
0N/A 13, 6, 4, 9, 8,15, 3, 0,11, 1, 2,12, 5,10,14, 7,
0N/A 1,10,13, 0, 6, 9, 8, 7, 4,15,14, 3,11, 5, 2,12},
0N/A
0N/A {7,13,14, 3, 0, 6, 9,10, 1, 2, 8, 5,11,12, 4,15,
0N/A 13, 8,11, 5, 6,15, 0, 3, 4, 7, 2,12, 1,10,14, 9,
0N/A 10, 6, 9, 0,12,11, 7,13,15, 1, 3,14, 5, 2, 8, 4,
0N/A 3,15, 0, 6,10, 1,13, 8, 9, 4, 5,11,12, 7, 2,14},
0N/A
0N/A {2,12, 4, 1, 7,10,11, 6, 8, 5, 3,15,13, 0,14, 9,
0N/A 14,11, 2,12, 4, 7,13, 1, 5, 0,15,10, 3, 9, 8, 6,
0N/A 4, 2, 1,11,10,13, 7, 8,15, 9,12, 5, 6, 3, 0,14,
0N/A 11, 8,12, 7, 1,14, 2,13, 6,15, 0, 9,10, 4, 5, 3},
0N/A
0N/A {12, 1,10,15, 9, 2, 6, 8, 0,13, 3, 4,14, 7, 5,11,
0N/A 10,15, 4, 2, 7,12, 9, 5, 6, 1,13,14, 0,11, 3, 8,
0N/A 9,14,15, 5, 2, 8,12, 3, 7, 0, 4,10, 1,13,11, 6,
0N/A 4, 3, 2,12, 9, 5,15,10,11,14, 1, 7, 6, 0, 8,13},
0N/A
0N/A {4,11, 2,14,15, 0, 8,13, 3,12, 9, 7, 5,10, 6, 1,
0N/A 13, 0,11, 7, 4, 9, 1,10,14, 3, 5,12, 2,15, 8, 6,
0N/A 1, 4,11,13,12, 3, 7,14,10,15, 6, 8, 0, 5, 9, 2,
0N/A 6,11,13, 8, 1, 4,10, 7, 9, 5, 0,15,14, 2, 3,12},
0N/A
0N/A {13, 2, 8, 4, 6,15,11, 1,10, 9, 3,14, 5, 0,12, 7,
0N/A 1,15,13, 8,10, 3, 7, 4,12, 5, 6,11, 0,14, 9, 2,
0N/A 7,11, 4, 1, 9,12,14, 2, 0, 6,10,13,15, 3, 5, 8,
0N/A 2, 1,14, 7, 4,10, 8,13,15,12, 9, 0, 3, 5, 6,11},
0N/A };
0N/A
0N/A
0N/A private static final byte[] P = {
0N/A 16, 7,20,21,
0N/A 29,12,28,17,
0N/A 1,15,23,26,
0N/A 5,18,31,10,
0N/A 2, 8,24,14,
0N/A 32,27, 3, 9,
0N/A 19,13,30, 6,
0N/A 22,11, 4,25,
0N/A };
0N/A
0N/A private byte[] L = new byte[64];
0N/A private byte[] tempL = new byte[32];
0N/A private byte[] f = new byte[32];
0N/A private byte[] preS = new byte[48];
0N/A
0N/A
0N/A private void encrypt(byte[] block,int fake) {
0N/A int i;
0N/A int t, j, k;
0N/A int R = 32; // &L[32]
0N/A
0N/A if (KS == null) {
0N/A KS = new byte[16*48];
0N/A }
0N/A
0N/A for(j=0; j < 64; j++) {
0N/A L[j] = block[IP[j]-1];
0N/A }
0N/A for(i=0; i < 16; i++) {
0N/A int index = i * 48;
0N/A
0N/A for(j=0; j < 32; j++) {
0N/A tempL[j] = L[R+j];
0N/A }
0N/A for(j=0; j < 48; j++) {
0N/A preS[j] = (byte) (L[R+E[j]-1] ^ KS[index+j]);
0N/A }
0N/A for(j=0; j < 8; j++) {
0N/A t = 6*j;
0N/A k = S[j][(preS[t+0]<<5)+
0N/A (preS[t+1]<<3)+
0N/A (preS[t+2]<<2)+
0N/A (preS[t+3]<<1)+
0N/A (preS[t+4]<<0)+
0N/A (preS[t+5]<<4)];
0N/A t = 4*j;
0N/A f[t+0] = (byte) ((k>>3)&01);
0N/A f[t+1] = (byte) ((k>>2)&01);
0N/A f[t+2] = (byte) ((k>>1)&01);
0N/A f[t+3] = (byte) ((k>>0)&01);
0N/A }
0N/A for(j=0; j < 32; j++) {
0N/A L[R+j] = (byte) (L[j] ^ f[P[j]-1]);
0N/A }
0N/A for(j=0; j < 32; j++) {
0N/A L[j] = tempL[j];
0N/A }
0N/A }
0N/A for(j=0; j < 32; j++) {
0N/A t = L[j];
0N/A L[j] = L[R+j];
0N/A L[R+j] = (byte)t;
0N/A }
0N/A for(j=0; j < 64; j++) {
0N/A block[j] = L[FP[j]-1];
0N/A }
0N/A }
0N/A/* EXPORT DELETE END */
0N/A
0N/A /**
0N/A * Creates a new Crypt object for use with the crypt method.
0N/A *
0N/A */
0N/A
0N/A public Crypt()
0N/A {
0N/A // does nothing at this time
0N/A super();
0N/A }
0N/A
0N/A /**
0N/A * Implements the libc crypt(3) function.
0N/A *
0N/A * @param pw the password to "encrypt".
0N/A *
0N/A * @param salt the salt to use.
0N/A *
0N/A * @return A new byte[13] array that contains the encrypted
0N/A * password. The first two characters are the salt.
0N/A *
0N/A */
0N/A
0N/A public synchronized byte[] crypt(byte[] pw, byte[] salt) {
0N/A int c, i, j, pwi;
0N/A byte temp;
0N/A byte[] block = new byte[66];
0N/A byte[] iobuf = new byte[13];
0N/A
0N/A/* EXPORT DELETE START */
0N/A
0N/A pwi = 0;
0N/A
0N/A for(i=0; pwi < pw.length && i < 64; pwi++) {
0N/A c = pw[pwi];
0N/A for(j=0; j < 7; j++, i++) {
0N/A block[i] = (byte) ((c>>(6-j)) & 01);
0N/A }
0N/A i++;
0N/A }
0N/A
0N/A setkey(block);
0N/A
0N/A for(i=0; i < 66; i++) {
0N/A block[i] = 0;
0N/A }
0N/A
0N/A for(i=0; i < 2; i++) {
0N/A c = salt[i];
0N/A iobuf[i] = (byte)c;
0N/A if(c > 'Z')
0N/A c -= 6;
0N/A if(c > '9')
0N/A c -= 7;
0N/A c -= '.';
0N/A for(j=0; j < 6; j++) {
0N/A if( ((c>>j) & 01) != 0) {
0N/A temp = E[6*i+j];
0N/A E[6*i+j] = E[6*i+j+24];
0N/A E[6*i+j+24] = temp;
0N/A }
0N/A }
0N/A }
0N/A
0N/A for(i=0; i < 25; i++) {
0N/A encrypt(block,0);
0N/A }
0N/A
0N/A for(i=0; i < 11; i++) {
0N/A c = 0;
0N/A for(j=0; j < 6; j++) {
0N/A c <<= 1;
0N/A c |= block[6*i+j];
0N/A }
0N/A c += '.';
0N/A if(c > '9') {
0N/A c += 7;
0N/A }
0N/A if(c > 'Z') {
0N/A c += 6;
0N/A }
0N/A iobuf[i+2] = (byte)c;
0N/A }
0N/A //iobuf[i+2] = 0;
0N/A if(iobuf[1] == 0) {
0N/A iobuf[1] = iobuf[0];
0N/A }
0N/A/* EXPORT DELETE END */
0N/A return(iobuf);
0N/A }
0N/A
0N/A /**
0N/A * program to test the crypt routine.
0N/A *
0N/A * The first parameter is the cleartext password, the second is
0N/A * the salt to use. The salt should be two characters from the
0N/A * set [a-zA-Z0-9./]. Outputs the crypt result.
0N/A *
0N/A * @param arg command line arguments.
0N/A *
0N/A */
0N/A
0N/A public static void main(String arg[]) {
0N/A
0N/A if (arg.length!=2) {
0N/A System.err.println("usage: Crypt password salt");
0N/A System.exit(1);
0N/A }
0N/A
0N/A Crypt c = new Crypt();
0N/A try {
0N/A byte result[] = c.crypt
0N/A (arg[0].getBytes("ISO-8859-1"), arg[1].getBytes("ISO-8859-1"));
0N/A for (int i=0; i<result.length; i++) {
0N/A System.out.println(" "+i+" "+(char)result[i]);
0N/A }
0N/A } catch (java.io.UnsupportedEncodingException uee) {
0N/A // cannot happen
0N/A }
0N/A }
0N/A}