1. /*
  2. * @(#)Crypt.java 1.5 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. /* Copyright (c) 1988 AT&T */
  8. /* All Rights Reserved */
  9. /* THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T */
  10. /* The copyright notice above does not evidence any */
  11. /* actual or intended publication of such source code. */
  12. /**
  13. * Implements the UNIX crypt(3) function, based on a direct port of the
  14. * libc crypt function.
  15. *
  16. * <p>
  17. * From the crypt man page:
  18. * <p>
  19. * crypt() is the password encryption routine, based on the NBS
  20. * Data Encryption Standard, with variations intended (among
  21. * other things) to frustrate use of hardware implementations
  22. * of the DES for key search.
  23. * <p>
  24. * The first argument to crypt() is normally a user's typed
  25. * password. The second is a 2-character string chosen from
  26. * the set [a-zA-Z0-9./]. the salt string is used to perturb
  27. * the DES algorithm in one
  28. * of 4096 different ways, after which the password is used as
  29. * the key to encrypt repeatedly a constant string. The
  30. * returned value points to the encrypted password, in the same
  31. * alphabet as the salt. The first two characters are the salt
  32. * itself.
  33. *
  34. * @version 1.5, 01/11/00
  35. * @author Roland Schemers
  36. */
  37. package com.sun.security.auth.module;
  38. class Crypt {
  39. /* EXPORT DELETE START */
  40. private static final byte[] IP = {
  41. 58, 50, 42, 34, 26, 18, 10, 2,
  42. 60, 52, 44, 36, 28, 20, 12, 4,
  43. 62, 54, 46, 38, 30, 22, 14, 6,
  44. 64, 56, 48, 40, 32, 24, 16, 8,
  45. 57, 49, 41, 33, 25, 17, 9, 1,
  46. 59, 51, 43, 35, 27, 19, 11, 3,
  47. 61, 53, 45, 37, 29, 21, 13, 5,
  48. 63, 55, 47, 39, 31, 23, 15, 7,
  49. };
  50. private static final byte[] FP = {
  51. 40, 8, 48, 16, 56, 24, 64, 32,
  52. 39, 7, 47, 15, 55, 23, 63, 31,
  53. 38, 6, 46, 14, 54, 22, 62, 30,
  54. 37, 5, 45, 13, 53, 21, 61, 29,
  55. 36, 4, 44, 12, 52, 20, 60, 28,
  56. 35, 3, 43, 11, 51, 19, 59, 27,
  57. 34, 2, 42, 10, 50, 18, 58, 26,
  58. 33, 1, 41, 9, 49, 17, 57, 25,
  59. };
  60. private static final byte[] PC1_C = {
  61. 57, 49, 41, 33, 25, 17, 9,
  62. 1, 58, 50, 42, 34, 26, 18,
  63. 10, 2, 59, 51, 43, 35, 27,
  64. 19, 11, 3, 60, 52, 44, 36,
  65. };
  66. private static final byte[] PC1_D = {
  67. 63, 55, 47, 39, 31, 23, 15,
  68. 7, 62, 54, 46, 38, 30, 22,
  69. 14, 6, 61, 53, 45, 37, 29,
  70. 21, 13, 5, 28, 20, 12, 4,
  71. };
  72. private static final byte[] shifts = { 1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1, };
  73. private static final byte[] PC2_C = {
  74. 14, 17, 11, 24, 1, 5,
  75. 3, 28, 15, 6, 21, 10,
  76. 23, 19, 12, 4, 26, 8,
  77. 16, 7, 27, 20, 13, 2,
  78. };
  79. private static final byte[] PC2_D = {
  80. 41,52,31,37,47,55,
  81. 30,40,51,45,33,48,
  82. 44,49,39,56,34,53,
  83. 46,42,50,36,29,32,
  84. };
  85. private byte[] C = new byte[28];
  86. private byte[] D = new byte[28];
  87. private byte[] KS;
  88. private byte[] E = new byte[48];
  89. private static final byte[] e2 = {
  90. 32, 1, 2, 3, 4, 5,
  91. 4, 5, 6, 7, 8, 9,
  92. 8, 9,10,11,12,13,
  93. 12,13,14,15,16,17,
  94. 16,17,18,19,20,21,
  95. 20,21,22,23,24,25,
  96. 24,25,26,27,28,29,
  97. 28,29,30,31,32, 1,
  98. };
  99. private void setkey(byte[] key) {
  100. int i, j, k;
  101. byte t;
  102. if (KS == null) {
  103. KS = new byte[16*48];
  104. }
  105. for (i = 0; i < 28; i++) {
  106. C[i] = key[PC1_C[i]-1];
  107. D[i] = key[PC1_D[i]-1];
  108. }
  109. for (i = 0; i < 16; i++) {
  110. for (k = 0; k < shifts[i]; k++) {
  111. t = C[0];
  112. for (j = 0; j < 28-1; j++)
  113. C[j] = C[j+1];
  114. C[27] = t;
  115. t = D[0];
  116. for (j = 0; j < 28-1; j++)
  117. D[j] = D[j+1];
  118. D[27] = t;
  119. }
  120. for (j = 0; j < 24; j++) {
  121. int index = i * 48;
  122. KS[index+j] = C[PC2_C[j]-1];
  123. KS[index+j+24] = D[PC2_D[j]-28-1];
  124. }
  125. }
  126. for (i = 0; i < 48; i++)
  127. E[i] = e2[i];
  128. }
  129. private static final byte[][] S = {
  130. {14, 4,13, 1, 2,15,11, 8, 3,10, 6,12, 5, 9, 0, 7,
  131. 0,15, 7, 4,14, 2,13, 1,10, 6,12,11, 9, 5, 3, 8,
  132. 4, 1,14, 8,13, 6, 2,11,15,12, 9, 7, 3,10, 5, 0,
  133. 15,12, 8, 2, 4, 9, 1, 7, 5,11, 3,14,10, 0, 6,13},
  134. {15, 1, 8,14, 6,11, 3, 4, 9, 7, 2,13,12, 0, 5,10,
  135. 3,13, 4, 7,15, 2, 8,14,12, 0, 1,10, 6, 9,11, 5,
  136. 0,14, 7,11,10, 4,13, 1, 5, 8,12, 6, 9, 3, 2,15,
  137. 13, 8,10, 1, 3,15, 4, 2,11, 6, 7,12, 0, 5,14, 9},
  138. {10, 0, 9,14, 6, 3,15, 5, 1,13,12, 7,11, 4, 2, 8,
  139. 13, 7, 0, 9, 3, 4, 6,10, 2, 8, 5,14,12,11,15, 1,
  140. 13, 6, 4, 9, 8,15, 3, 0,11, 1, 2,12, 5,10,14, 7,
  141. 1,10,13, 0, 6, 9, 8, 7, 4,15,14, 3,11, 5, 2,12},
  142. {7,13,14, 3, 0, 6, 9,10, 1, 2, 8, 5,11,12, 4,15,
  143. 13, 8,11, 5, 6,15, 0, 3, 4, 7, 2,12, 1,10,14, 9,
  144. 10, 6, 9, 0,12,11, 7,13,15, 1, 3,14, 5, 2, 8, 4,
  145. 3,15, 0, 6,10, 1,13, 8, 9, 4, 5,11,12, 7, 2,14},
  146. {2,12, 4, 1, 7,10,11, 6, 8, 5, 3,15,13, 0,14, 9,
  147. 14,11, 2,12, 4, 7,13, 1, 5, 0,15,10, 3, 9, 8, 6,
  148. 4, 2, 1,11,10,13, 7, 8,15, 9,12, 5, 6, 3, 0,14,
  149. 11, 8,12, 7, 1,14, 2,13, 6,15, 0, 9,10, 4, 5, 3},
  150. {12, 1,10,15, 9, 2, 6, 8, 0,13, 3, 4,14, 7, 5,11,
  151. 10,15, 4, 2, 7,12, 9, 5, 6, 1,13,14, 0,11, 3, 8,
  152. 9,14,15, 5, 2, 8,12, 3, 7, 0, 4,10, 1,13,11, 6,
  153. 4, 3, 2,12, 9, 5,15,10,11,14, 1, 7, 6, 0, 8,13},
  154. {4,11, 2,14,15, 0, 8,13, 3,12, 9, 7, 5,10, 6, 1,
  155. 13, 0,11, 7, 4, 9, 1,10,14, 3, 5,12, 2,15, 8, 6,
  156. 1, 4,11,13,12, 3, 7,14,10,15, 6, 8, 0, 5, 9, 2,
  157. 6,11,13, 8, 1, 4,10, 7, 9, 5, 0,15,14, 2, 3,12},
  158. {13, 2, 8, 4, 6,15,11, 1,10, 9, 3,14, 5, 0,12, 7,
  159. 1,15,13, 8,10, 3, 7, 4,12, 5, 6,11, 0,14, 9, 2,
  160. 7,11, 4, 1, 9,12,14, 2, 0, 6,10,13,15, 3, 5, 8,
  161. 2, 1,14, 7, 4,10, 8,13,15,12, 9, 0, 3, 5, 6,11},
  162. };
  163. private static final byte[] P = {
  164. 16, 7,20,21,
  165. 29,12,28,17,
  166. 1,15,23,26,
  167. 5,18,31,10,
  168. 2, 8,24,14,
  169. 32,27, 3, 9,
  170. 19,13,30, 6,
  171. 22,11, 4,25,
  172. };
  173. private byte[] L = new byte[64];
  174. private byte[] tempL = new byte[32];
  175. private byte[] f = new byte[32];
  176. private byte[] preS = new byte[48];
  177. private void encrypt(byte[] block,int fake) {
  178. int i;
  179. int t, j, k;
  180. int R = 32; // &L[32]
  181. if (KS == null) {
  182. KS = new byte[16*48];
  183. }
  184. for(j=0; j < 64; j++) {
  185. L[j] = block[IP[j]-1];
  186. }
  187. for(i=0; i < 16; i++) {
  188. int index = i * 48;
  189. for(j=0; j < 32; j++) {
  190. tempL[j] = L[R+j];
  191. }
  192. for(j=0; j < 48; j++) {
  193. preS[j] = (byte) (L[R+E[j]-1] ^ KS[index+j]);
  194. }
  195. for(j=0; j < 8; j++) {
  196. t = 6*j;
  197. k = S[j][(preS[t+0]<<5)+
  198. (preS[t+1]<<3)+
  199. (preS[t+2]<<2)+
  200. (preS[t+3]<<1)+
  201. (preS[t+4]<<0)+
  202. (preS[t+5]<<4)];
  203. t = 4*j;
  204. f[t+0] = (byte) ((k>>3)&01);
  205. f[t+1] = (byte) ((k>>2)&01);
  206. f[t+2] = (byte) ((k>>1)&01);
  207. f[t+3] = (byte) ((k>>0)&01);
  208. }
  209. for(j=0; j < 32; j++) {
  210. L[R+j] = (byte) (L[j] ^ f[P[j]-1]);
  211. }
  212. for(j=0; j < 32; j++) {
  213. L[j] = tempL[j];
  214. }
  215. }
  216. for(j=0; j < 32; j++) {
  217. t = L[j];
  218. L[j] = L[R+j];
  219. L[R+j] = (byte)t;
  220. }
  221. for(j=0; j < 64; j++) {
  222. block[j] = L[FP[j]-1];
  223. }
  224. }
  225. /* EXPORT DELETE END */
  226. /**
  227. * Creates a new Crypt object for use with the crypt method.
  228. *
  229. */
  230. public Crypt()
  231. {
  232. // does nothing at this time
  233. super();
  234. }
  235. /**
  236. * Implements the libc crypt(3) function.
  237. *
  238. * @param pw the password to "encrypt".
  239. *
  240. * @param salt the salt to use.
  241. *
  242. * @return A new byte[13] array that contains the encrypted
  243. * password. The first two characters are the salt.
  244. *
  245. */
  246. public synchronized byte[] crypt(byte[] pw, byte[] salt) {
  247. int c, i, j, pwi;
  248. byte temp;
  249. byte[] block = new byte[66];
  250. byte[] iobuf = new byte[13];
  251. /* EXPORT DELETE START */
  252. pwi = 0;
  253. for(i=0; pwi < pw.length && i < 64; pwi++) {
  254. c = pw[pwi];
  255. for(j=0; j < 7; j++, i++) {
  256. block[i] = (byte) ((c>>(6-j)) & 01);
  257. }
  258. i++;
  259. }
  260. setkey(block);
  261. for(i=0; i < 66; i++) {
  262. block[i] = 0;
  263. }
  264. for(i=0; i < 2; i++) {
  265. c = salt[i];
  266. iobuf[i] = (byte)c;
  267. if(c > 'Z')
  268. c -= 6;
  269. if(c > '9')
  270. c -= 7;
  271. c -= '.';
  272. for(j=0; j < 6; j++) {
  273. if( ((c>>j) & 01) != 0) {
  274. temp = E[6*i+j];
  275. E[6*i+j] = E[6*i+j+24];
  276. E[6*i+j+24] = temp;
  277. }
  278. }
  279. }
  280. for(i=0; i < 25; i++) {
  281. encrypt(block,0);
  282. }
  283. for(i=0; i < 11; i++) {
  284. c = 0;
  285. for(j=0; j < 6; j++) {
  286. c <<= 1;
  287. c |= block[6*i+j];
  288. }
  289. c += '.';
  290. if(c > '9') {
  291. c += 7;
  292. }
  293. if(c > 'Z') {
  294. c += 6;
  295. }
  296. iobuf[i+2] = (byte)c;
  297. }
  298. //iobuf[i+2] = 0;
  299. if(iobuf[1] == 0) {
  300. iobuf[1] = iobuf[0];
  301. }
  302. /* EXPORT DELETE END */
  303. return(iobuf);
  304. }
  305. /**
  306. * program to test the crypt routine.
  307. *
  308. * The first parameter is the cleartext password, the second is
  309. * the salt to use. The salt should be two characters from the
  310. * set [a-zA-Z0-9./]. Outputs the crypt result.
  311. *
  312. * @param arg command line arguments.
  313. *
  314. */
  315. public static void main(String arg[]) {
  316. if (arg.length!=2) {
  317. System.err.println("usage: Crypt password salt");
  318. System.exit(1);
  319. }
  320. Crypt c = new Crypt();
  321. byte result[] = c.crypt(arg[0].getBytes(), arg[1].getBytes());
  322. for (int i=0; i<result.length; i++) {
  323. System.out.println(" "+i+" "+(char)result[i]);
  324. }
  325. }
  326. }