KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > kernel > security > impl > ldap > management > Crypt


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is Shirley Sasson.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  */

46
47 package org.mr.kernel.security.impl.ldap.management;
48
49 /**
50  * Crypt
51  *
52  * @version 1.0
53  * @since Mar 22, 2006
54  * @author Shirley Sasson
55  *
56  */

57 public class Crypt {
58
59 /* EXPORT DELETE START */
60
61     private static final byte[] IP = {
62     58, 50, 42, 34, 26, 18, 10, 2,
63     60, 52, 44, 36, 28, 20, 12, 4,
64     62, 54, 46, 38, 30, 22, 14, 6,
65     64, 56, 48, 40, 32, 24, 16, 8,
66     57, 49, 41, 33, 25, 17, 9, 1,
67     59, 51, 43, 35, 27, 19, 11, 3,
68     61, 53, 45, 37, 29, 21, 13, 5,
69     63, 55, 47, 39, 31, 23, 15, 7,
70     };
71
72     private static final byte[] FP = {
73     40, 8, 48, 16, 56, 24, 64, 32,
74     39, 7, 47, 15, 55, 23, 63, 31,
75     38, 6, 46, 14, 54, 22, 62, 30,
76     37, 5, 45, 13, 53, 21, 61, 29,
77     36, 4, 44, 12, 52, 20, 60, 28,
78     35, 3, 43, 11, 51, 19, 59, 27,
79     34, 2, 42, 10, 50, 18, 58, 26,
80     33, 1, 41, 9, 49, 17, 57, 25,
81     };
82
83     private static final byte[] PC1_C = {
84     57, 49, 41, 33, 25, 17, 9,
85     1, 58, 50, 42, 34, 26, 18,
86     10, 2, 59, 51, 43, 35, 27,
87     19, 11, 3, 60, 52, 44, 36,
88     };
89
90     private static final byte[] PC1_D = {
91     63, 55, 47, 39, 31, 23, 15,
92     7, 62, 54, 46, 38, 30, 22,
93     14, 6, 61, 53, 45, 37, 29,
94     21, 13, 5, 28, 20, 12, 4,
95     };
96
97     private static final byte[] shifts = { 1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1, };
98
99     private static final byte[] PC2_C = {
100     14, 17, 11, 24, 1, 5,
101     3, 28, 15, 6, 21, 10,
102     23, 19, 12, 4, 26, 8,
103     16, 7, 27, 20, 13, 2,
104     };
105
106     private static final byte[] PC2_D = {
107     41,52,31,37,47,55,
108     30,40,51,45,33,48,
109     44,49,39,56,34,53,
110     46,42,50,36,29,32,
111     };
112
113     private byte[] C = new byte[28];
114     private byte[] D = new byte[28];
115
116     private byte[] KS;
117
118     private byte[] E = new byte[48];
119
120     private static final byte[] e2 = {
121     32, 1, 2, 3, 4, 5,
122     4, 5, 6, 7, 8, 9,
123     8, 9,10,11,12,13,
124     12,13,14,15,16,17,
125     16,17,18,19,20,21,
126     20,21,22,23,24,25,
127     24,25,26,27,28,29,
128     28,29,30,31,32, 1,
129     };
130
131     private void setkey(byte[] key) {
132     int i, j, k;
133     byte t;
134
135     if (KS == null) {
136         KS = new byte[16*48];
137     }
138
139     for (i = 0; i < 28; i++) {
140         C[i] = key[PC1_C[i]-1];
141         D[i] = key[PC1_D[i]-1];
142     }
143     for (i = 0; i < 16; i++) {
144         for (k = 0; k < shifts[i]; k++) {
145             t = C[0];
146             for (j = 0; j < 28-1; j++)
147                 C[j] = C[j+1];
148             C[27] = t;
149             t = D[0];
150             for (j = 0; j < 28-1; j++)
151                 D[j] = D[j+1];
152             D[27] = t;
153         }
154         for (j = 0; j < 24; j++) {
155             int index = i * 48;
156
157             KS[index+j] = C[PC2_C[j]-1];
158             KS[index+j+24] = D[PC2_D[j]-28-1];
159         }
160     }
161     for (i = 0; i < 48; i++)
162         E[i] = e2[i];
163     }
164
165
166     private static final byte[][] S = {
167     {14, 4,13, 1, 2,15,11, 8, 3,10, 6,12, 5, 9, 0, 7,
168     0,15, 7, 4,14, 2,13, 1,10, 6,12,11, 9, 5, 3, 8,
169     4, 1,14, 8,13, 6, 2,11,15,12, 9, 7, 3,10, 5, 0,
170     15,12, 8, 2, 4, 9, 1, 7, 5,11, 3,14,10, 0, 6,13},
171
172     {15, 1, 8,14, 6,11, 3, 4, 9, 7, 2,13,12, 0, 5,10,
173     3,13, 4, 7,15, 2, 8,14,12, 0, 1,10, 6, 9,11, 5,
174     0,14, 7,11,10, 4,13, 1, 5, 8,12, 6, 9, 3, 2,15,
175     13, 8,10, 1, 3,15, 4, 2,11, 6, 7,12, 0, 5,14, 9},
176
177     {10, 0, 9,14, 6, 3,15, 5, 1,13,12, 7,11, 4, 2, 8,
178     13, 7, 0, 9, 3, 4, 6,10, 2, 8, 5,14,12,11,15, 1,
179     13, 6, 4, 9, 8,15, 3, 0,11, 1, 2,12, 5,10,14, 7,
180      1,10,13, 0, 6, 9, 8, 7, 4,15,14, 3,11, 5, 2,12},
181
182     {7,13,14, 3, 0, 6, 9,10, 1, 2, 8, 5,11,12, 4,15,
183     13, 8,11, 5, 6,15, 0, 3, 4, 7, 2,12, 1,10,14, 9,
184     10, 6, 9, 0,12,11, 7,13,15, 1, 3,14, 5, 2, 8, 4,
185      3,15, 0, 6,10, 1,13, 8, 9, 4, 5,11,12, 7, 2,14},
186
187     {2,12, 4, 1, 7,10,11, 6, 8, 5, 3,15,13, 0,14, 9,
188     14,11, 2,12, 4, 7,13, 1, 5, 0,15,10, 3, 9, 8, 6,
189      4, 2, 1,11,10,13, 7, 8,15, 9,12, 5, 6, 3, 0,14,
190     11, 8,12, 7, 1,14, 2,13, 6,15, 0, 9,10, 4, 5, 3},
191
192     {12, 1,10,15, 9, 2, 6, 8, 0,13, 3, 4,14, 7, 5,11,
193     10,15, 4, 2, 7,12, 9, 5, 6, 1,13,14, 0,11, 3, 8,
194      9,14,15, 5, 2, 8,12, 3, 7, 0, 4,10, 1,13,11, 6,
195      4, 3, 2,12, 9, 5,15,10,11,14, 1, 7, 6, 0, 8,13},
196
197     {4,11, 2,14,15, 0, 8,13, 3,12, 9, 7, 5,10, 6, 1,
198     13, 0,11, 7, 4, 9, 1,10,14, 3, 5,12, 2,15, 8, 6,
199      1, 4,11,13,12, 3, 7,14,10,15, 6, 8, 0, 5, 9, 2,
200      6,11,13, 8, 1, 4,10, 7, 9, 5, 0,15,14, 2, 3,12},
201
202     {13, 2, 8, 4, 6,15,11, 1,10, 9, 3,14, 5, 0,12, 7,
203      1,15,13, 8,10, 3, 7, 4,12, 5, 6,11, 0,14, 9, 2,
204      7,11, 4, 1, 9,12,14, 2, 0, 6,10,13,15, 3, 5, 8,
205      2, 1,14, 7, 4,10, 8,13,15,12, 9, 0, 3, 5, 6,11},
206     };
207
208
209     private static final byte[] P = {
210     16, 7,20,21,
211     29,12,28,17,
212      1,15,23,26,
213      5,18,31,10,
214      2, 8,24,14,
215     32,27, 3, 9,
216     19,13,30, 6,
217     22,11, 4,25,
218     };
219
220     private byte[] L = new byte[64];
221     private byte[] tempL = new byte[32];
222     private byte[] f = new byte[32];
223     private byte[] preS = new byte[48];
224
225
226     private void encrypt(byte[] block,int fake) {
227     int i;
228     int t, j, k;
229     int R = 32; // &L[32]
230

231     if (KS == null) {
232         KS = new byte[16*48];
233     }
234
235     for(j=0; j < 64; j++) {
236         L[j] = block[IP[j]-1];
237     }
238     for(i=0; i < 16; i++) {
239         int index = i * 48;
240
241         for(j=0; j < 32; j++) {
242         tempL[j] = L[R+j];
243         }
244         for(j=0; j < 48; j++) {
245         preS[j] = (byte) (L[R+E[j]-1] ^ KS[index+j]);
246         }
247         for(j=0; j < 8; j++) {
248         t = 6*j;
249         k = S[j][(preS[t+0]<<5)+
250              (preS[t+1]<<3)+
251              (preS[t+2]<<2)+
252              (preS[t+3]<<1)+
253              (preS[t+4]<<0)+
254              (preS[t+5]<<4)];
255         t = 4*j;
256         f[t+0] = (byte) ((k>>3)&01);
257         f[t+1] = (byte) ((k>>2)&01);
258         f[t+2] = (byte) ((k>>1)&01);
259         f[t+3] = (byte) ((k>>0)&01);
260         }
261         for(j=0; j < 32; j++) {
262             L[R+j] = (byte) (L[j] ^ f[P[j]-1]);
263         }
264         for(j=0; j < 32; j++) {
265             L[j] = tempL[j];
266         }
267     }
268     for(j=0; j < 32; j++) {
269         t = L[j];
270         L[j] = L[R+j];
271         L[R+j] = (byte)t;
272     }
273     for(j=0; j < 64; j++) {
274         block[j] = L[FP[j]-1];
275     }
276     }
277
278     /**
279      * Creates a new Crypt object for use with the crypt method.
280      *
281      */

282     public Crypt()
283     {
284     // does nothing at this time
285
super();
286     }
287
288     /**
289      * Implements the libc crypt(3) function.
290      *
291      * @param pw the password to "encrypt".
292      *
293      * @param salt the salt to use.
294      *
295      * @return A new byte[13] array that contains the encrypted
296      * password. The first two characters are the salt.
297      *
298      */

299     public synchronized byte[] crypt(byte[] pw, byte[] salt) {
300     int c, i, j, pwi;
301     byte temp;
302     byte[] block = new byte[66];
303     byte[] iobuf = new byte[13];
304
305 /* EXPORT DELETE START */
306
307     pwi = 0;
308
309     for(i=0; pwi < pw.length && i < 64; pwi++) {
310         c = pw[pwi];
311         for(j=0; j < 7; j++, i++) {
312         block[i] = (byte) ((c>>(6-j)) & 01);
313         }
314         i++;
315     }
316
317     setkey(block);
318
319     for(i=0; i < 66; i++) {
320         block[i] = 0;
321     }
322
323     for(i=0; i < 2; i++) {
324         c = salt[i];
325         iobuf[i] = (byte)c;
326         if(c > 'Z')
327         c -= 6;
328         if(c > '9')
329         c -= 7;
330         c -= '.';
331         for(j=0; j < 6; j++) {
332         if( ((c>>j) & 01) != 0) {
333             temp = E[6*i+j];
334             E[6*i+j] = E[6*i+j+24];
335             E[6*i+j+24] = temp;
336         }
337         }
338     }
339
340     for(i=0; i < 25; i++) {
341         encrypt(block,0);
342     }
343
344     for(i=0; i < 11; i++) {
345         c = 0;
346         for(j=0; j < 6; j++) {
347         c <<= 1;
348         c |= block[6*i+j];
349         }
350         c += '.';
351         if(c > '9') {
352         c += 7;
353         }
354         if(c > 'Z') {
355         c += 6;
356         }
357         iobuf[i+2] = (byte)c;
358     }
359     //iobuf[i+2] = 0;
360
if(iobuf[1] == 0) {
361         iobuf[1] = iobuf[0];
362     }
363 /* EXPORT DELETE END */
364     return(iobuf);
365     }
366 }
367
Popular Tags