summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/imagemaster/util/Sha512Crypt.java
blob: c4a4b61c29e3322ebe28cb28cf9b0ac5cea1a0ae (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/*
   Sha512Crypt.java

   Created: 18 December 2007

   Java Port By: James Ratcliff, falazar@arlut.utexas.edu

   This class implements the new generation, scalable, SHA512-based
   Unix 'crypt' algorithm developed by a group of engineers from Red
   Hat, Sun, IBM, and HP for common use in the Unix and Linux
   /etc/shadow files.

   The Linux glibc library (starting at version 2.7) includes support
   for validating passwords hashed using this algorithm.

   The algorithm itself was released into the Public Domain by Ulrich
   Drepper <drepper@redhat.com>.  A discussion of the rationale and
   development of this algorithm is at

   http://people.redhat.com/drepper/sha-crypt.html

   and the specification and a sample C language implementation is at

   http://people.redhat.com/drepper/SHA-crypt.txt

   This Java Port is

     Copyright (c) 2008-2013 The University of Texas at Austin.

     All rights reserved.

     Redistribution and use in source and binary form are permitted
     provided that distributions retain this entire copyright notice
     and comment. Neither the name of the University nor the names of
     its contributors may be used to endorse or promote products
     derived from this software without specific prior written
     permission. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY
     EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE
     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
     PARTICULAR PURPOSE.

*/

package org.openslx.imagemaster.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/*------------------------------------------------------------------------------
                                                                           class
                                                                     Sha512Crypt

------------------------------------------------------------------------------*/

/**
 * <p>This class defines a method, {@link
 * Sha512Crypt#Sha512_crypt(java.lang.String, java.lang.String, int)
 * Sha512_crypt()}, which takes a password and a salt string and
 * generates a Sha512 encrypted password entry.</p>
 *
 * <p>This class implements the new generation, scalable, SHA512-based
 * Unix 'crypt' algorithm developed by a group of engineers from Red
 * Hat, Sun, IBM, and HP for common use in the Unix and Linux
 * /etc/shadow files.</p>
 *
 * <p>The Linux glibc library (starting at version 2.7) includes
 * support for validating passwords hashed using this algorithm.</p>
 *
 * <p>The algorithm itself was released into the Public Domain by
 * Ulrich Drepper &lt;drepper@redhat.com&gt;.  A discussion of the
 * rationale and development of this algorithm is at</p>
 *
 * <p>http://people.redhat.com/drepper/sha-crypt.html</p>
 *
 * <p>and the specification and a sample C language implementation is
 * at</p>
 *
 * <p>http://people.redhat.com/drepper/SHA-crypt.txt</p>
 */

public final class Sha512Crypt
{
  static private final String sha512_salt_prefix = "$6$";
  static private final String sha512_rounds_prefix = "rounds=";
  static private final int SALT_LEN_MAX = 16;
  static private final int ROUNDS_DEFAULT = 5000;
  static private final int ROUNDS_MIN = 1000;
  static private final int ROUNDS_MAX = 999999999;
  static private final String SALTCHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
  static private final String itoa64 = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  
	/**
	 * Cache of sha512 digesters
	 */
	private static final ThreadLocal<MessageDigest>
			sha1 = newThreadLocalSha512(),
			sha2 = newThreadLocalSha512();
	
	/**
	 * For instantiating TL sha512, so we don't have to copy this stuff twice
	 * @return thread local sha512 digester
	 */
	private static ThreadLocal<MessageDigest> newThreadLocalSha512() {
		return new ThreadLocal<MessageDigest>() {
			@Override
			public MessageDigest initialValue()
			{
				try {
					return MessageDigest.getInstance( "SHA-512" );
				} catch ( NoSuchAlgorithmException e ) {
					e.printStackTrace();
					System.exit(1);
					return null;
				}
			}
		};
	}

  /**
   * <p>This method actually generates an Sha512 crypted password hash
   * from a plaintext password and a salt.</p>
   *
   * <p>The resulting string will be in the form
   * '$6$&lt;rounds=n&gt;$&lt;salt&gt;$&lt;hashed mess&gt;</p>
   *
   * @param keyStr Plaintext password
   *
   * @param saltStr An encoded salt/roundes which will be consulted to determine the salt
   * and round count, if not null
   *
   * @param roundsCount If this value is not 0, this many rounds will
   * used to generate the hash text.
   *
   * @return The Sha512 Unix Crypt hash text for the keyStr
   */

  public static final String Sha512_crypt(String keyStr, String saltStr, int roundsCount)
  {
    MessageDigest ctx = sha1.get();
    MessageDigest alt_ctx = sha2.get();

    byte[] alt_result;
    byte[] temp_result;
    byte[] p_bytes = null;
    byte[] s_bytes = null;
    int cnt, cnt2;
    int rounds = ROUNDS_DEFAULT; // Default number of rounds.
    StringBuilder buffer;
    boolean include_round_count = false;

    /* -- */

    if (saltStr != null)
      {
        if (saltStr.startsWith(sha512_salt_prefix))
          {
            saltStr = saltStr.substring(sha512_salt_prefix.length());
          }

        if (saltStr.startsWith(sha512_rounds_prefix))
          {
            String num = saltStr.substring(sha512_rounds_prefix.length(), saltStr.indexOf('$'));
            int srounds = Integer.valueOf(num).intValue();
            saltStr = saltStr.substring(saltStr.indexOf('$')+1);
            rounds = Math.max(ROUNDS_MIN, Math.min(srounds, ROUNDS_MAX));
            include_round_count = true;
          }

        if (saltStr.length() > SALT_LEN_MAX)
          {
            saltStr = saltStr.substring(0, SALT_LEN_MAX);
          }

        // gnu libc's crypt(3) implementation allows the salt to end
        // in $ which is then ignored.

        if (saltStr.endsWith("$"))
          {
            saltStr = saltStr.substring(0, saltStr.length() - 1);
          }
        else
          {
            if (saltStr.indexOf("$") != -1)
              {
                saltStr = saltStr.substring(0, saltStr.indexOf("$"));
              }
          }
      }
    else
      {
        StringBuilder saltBuf = new StringBuilder();

        while (saltBuf.length() < 16)
          {
            int index = Util.randomInt(SALTCHARS.length());
            saltBuf.append(SALTCHARS.substring(index, index+1));
          }

        saltStr = saltBuf.toString();
      }

    if (roundsCount != 0)
      {
        rounds = Math.max(ROUNDS_MIN, Math.min(roundsCount, ROUNDS_MAX));
      }

    byte[] key = keyStr.getBytes();
    byte[] salt = saltStr.getBytes();

    ctx.reset();
    ctx.update(key, 0, key.length);
    ctx.update(salt, 0, salt.length);

    alt_ctx.reset();
    alt_ctx.update(key, 0, key.length);
    alt_ctx.update(salt, 0, salt.length);
    alt_ctx.update(key, 0, key.length);

    alt_result = alt_ctx.digest();

    for (cnt = key.length; cnt > 64; cnt -= 64)
      {
        ctx.update(alt_result, 0, 64);
      }

    ctx.update(alt_result, 0, cnt);

    for (cnt = key.length; cnt > 0; cnt >>= 1)
      {
        if ((cnt & 1) != 0)
          {
            ctx.update(alt_result, 0, 64);
          }
        else
          {
            ctx.update(key, 0, key.length);
          }
      }

    alt_result = ctx.digest();

    alt_ctx.reset();

    for (cnt = 0; cnt < key.length; ++cnt)
      {
        alt_ctx.update(key, 0, key.length);
      }

    temp_result = alt_ctx.digest();

    p_bytes = new byte[key.length];

    for (cnt2 = 0, cnt = p_bytes.length; cnt >= 64; cnt -= 64)
      {
        System.arraycopy(temp_result, 0, p_bytes, cnt2, 64);
        cnt2 += 64;
      }

    System.arraycopy(temp_result, 0, p_bytes, cnt2, cnt);

    alt_ctx.reset();

    for (cnt = 0; cnt < 16 + (alt_result[0]&0xFF); ++cnt)
      {
        alt_ctx.update(salt, 0, salt.length);
      }

    temp_result = alt_ctx.digest();

    s_bytes = new byte[salt.length];

    for (cnt2 = 0, cnt = s_bytes.length; cnt >= 64; cnt -= 64)
      {
        System.arraycopy(temp_result, 0, s_bytes, cnt2, 64);
        cnt2 += 64;
      }

    System.arraycopy(temp_result, 0, s_bytes, cnt2, cnt);

    /* Repeatedly run the collected hash value through SHA512 to burn
       CPU cycles.  */

    for (cnt = 0; cnt < rounds; ++cnt)
      {
        ctx.reset();

        if ((cnt & 1) != 0)
          {
            ctx.update(p_bytes, 0, key.length);
          }
        else
          {
            ctx.update (alt_result, 0, 64);
          }

        if (cnt % 3 != 0)
          {
            ctx.update(s_bytes, 0, salt.length);
          }

        if (cnt % 7 != 0)
          {
            ctx.update(p_bytes, 0, key.length);
          }

        if ((cnt & 1) != 0)
          {
            ctx.update(alt_result, 0, 64);
          }
        else
          {
            ctx.update(p_bytes, 0, key.length);
          }

        alt_result = ctx.digest();
      }

    buffer = new StringBuilder(sha512_salt_prefix);

    if (include_round_count || rounds != ROUNDS_DEFAULT)
      {
        buffer.append(sha512_rounds_prefix);
        buffer.append(rounds);
        buffer.append("$");
      }

    buffer.append(saltStr);
    buffer.append("$");

    buffer.append(b64_from_24bit (alt_result[0], alt_result[21], alt_result[42], 4));
    buffer.append(b64_from_24bit (alt_result[22], alt_result[43], alt_result[1], 4));
    buffer.append(b64_from_24bit (alt_result[44], alt_result[2], alt_result[23], 4));
    buffer.append(b64_from_24bit (alt_result[3], alt_result[24], alt_result[45], 4));
    buffer.append(b64_from_24bit (alt_result[25], alt_result[46], alt_result[4], 4));
    buffer.append(b64_from_24bit (alt_result[47], alt_result[5], alt_result[26], 4));
    buffer.append(b64_from_24bit (alt_result[6], alt_result[27], alt_result[48], 4));
    buffer.append(b64_from_24bit (alt_result[28], alt_result[49], alt_result[7], 4));
    buffer.append(b64_from_24bit (alt_result[50], alt_result[8], alt_result[29], 4));
    buffer.append(b64_from_24bit (alt_result[9], alt_result[30], alt_result[51], 4));
    buffer.append(b64_from_24bit (alt_result[31], alt_result[52], alt_result[10], 4));
    buffer.append(b64_from_24bit (alt_result[53], alt_result[11], alt_result[32], 4));
    buffer.append(b64_from_24bit (alt_result[12], alt_result[33], alt_result[54], 4));
    buffer.append(b64_from_24bit (alt_result[34], alt_result[55], alt_result[13], 4));
    buffer.append(b64_from_24bit (alt_result[56], alt_result[14], alt_result[35], 4));
    buffer.append(b64_from_24bit (alt_result[15], alt_result[36], alt_result[57], 4));
    buffer.append(b64_from_24bit (alt_result[37], alt_result[58], alt_result[16], 4));
    buffer.append(b64_from_24bit (alt_result[59], alt_result[17], alt_result[38], 4));
    buffer.append(b64_from_24bit (alt_result[18], alt_result[39], alt_result[60], 4));
    buffer.append(b64_from_24bit (alt_result[40], alt_result[61], alt_result[19], 4));
    buffer.append(b64_from_24bit (alt_result[62], alt_result[20], alt_result[41], 4));
    buffer.append(b64_from_24bit ((byte)0x00, (byte)0x00, alt_result[63], 2));

    /* Clear the buffer for the intermediate result so that people
       attaching to processes or reading core dumps cannot get any
       information. */

    ctx.reset();

    return buffer.toString();
  }

  private static final String b64_from_24bit(byte B2, byte B1, byte B0, int size)
  {
    int v = ((((int) B2) & 0xFF) << 16) | ((((int) B1) & 0xFF) << 8) | ((int)B0 & 0xff);

    StringBuilder result = new StringBuilder();

    while (--size >= 0)
      {
        result.append(itoa64.charAt((int) (v & 0x3f)));
        v >>>= 6;
      }

    return result.toString();
  }

  /**
   * <p>This method tests a plaintext password against a SHA512 Unix
   * Crypt'ed hash and returns true if the password matches the
   * hash.</p>
   *
   * @param plaintextPass The plaintext password text to test.
   * @param sha512CryptText The hash text we're testing against.
   * We'll extract the salt and the round count from this String.
   */

  static public final boolean verifyPassword(String plaintextPass, String sha512CryptText)
  {
    if (sha512CryptText.startsWith("$6$"))
      {
        return sha512CryptText.equals(Sha512_crypt(plaintextPass, sha512CryptText, 0));
      }
    else
      {
        throw new RuntimeException("Bad sha512CryptText");
      }
  }

  /**
   * <p>Returns true if sha512CryptText is a valid Sha512Crypt hashtext,
   * false if not.</p>
   */

  public static final boolean verifyHashTextFormat(String sha512CryptText)
  {
    if (!sha512CryptText.startsWith(sha512_salt_prefix))
      {
        return false;
      }

    sha512CryptText = sha512CryptText.substring(sha512_salt_prefix.length());

    if (sha512CryptText.startsWith(sha512_rounds_prefix))
      {
        String num = sha512CryptText.substring(sha512_rounds_prefix.length(), sha512CryptText.indexOf('$'));

        try
          {
            Integer.valueOf(num).intValue();
          }
        catch (NumberFormatException ex)
          {
            return false;
          }

        sha512CryptText = sha512CryptText.substring(sha512CryptText.indexOf('$')+1);
      }

    if (sha512CryptText.indexOf('$') > (SALT_LEN_MAX + 1))
      {
        return false;
      }

    sha512CryptText = sha512CryptText.substring(sha512CryptText.indexOf('$') + 1);

    for (int i = 0; i < sha512CryptText.length(); i++)
      {
        if (itoa64.indexOf(sha512CryptText.charAt(i)) == -1)
          {
            return false;
          }
      }

    return true;
  }

  /**
   * <p>Validate our implementation using test data from Ulrich
   * Drepper's C implementation.</p>
   */

  public static void selfTest()
  {
    String msgs[] =
      {
        "$6$saltstring", "Hello world!", "$6$saltstring$svn8UoSVapNtMuq1ukKS4tPQd8iKwSMHWjl/O817G3uBnIFNjnQJuesI68u4OTLiBFdcbYEdFCoEOfaS35inz1",
        "$6$xxxxxxxx",  "geheim", "$6$xxxxxxxx$wuSdyeOvQXjj/nNoWnjjo.6OxUWrQFRIj019kh1cDpun6l6cpr3ywSrBprYRYZXcm4Kv9lboCEFI3GzBkdNAz/",
        "$6$rounds=10000$saltstringsaltstring", "Hello world!", "$6$rounds=10000$saltstringsaltst$OW1/O6BYHV6BcXZu8QVeXbDWra3Oeqh0sbHbbMCVNSnCM/UrjmM0Dp8vOuZeHBy/YTBmSK6H9qs/y3RnOaw5v.",
        "$6$rounds=5000$toolongsaltstring", "This is just a test", "$6$rounds=5000$toolongsaltstrin$lQ8jolhgVRVhY4b5pZKaysCLi0QBxGoNeKQzQ3glMhwllF7oGDZxUhx1yxdYcz/e1JSbq3y6JMxxl8audkUEm0",
        "$6$rounds=1400$anotherlongsaltstring", "a very much longer text to encrypt.  This one even stretches over morethan one line.", "$6$rounds=1400$anotherlongsalts$POfYwTEok97VWcjxIiSOjiykti.o/pQs.wPvMxQ6Fm7I6IoYN3CmLs66x9t0oSwbtEW7o7UmJEiDwGqd8p4ur1",
        "$6$rounds=77777$short", "we have a short salt string but not a short password", "$6$rounds=77777$short$WuQyW2YR.hBNpjjRhpYD/ifIw05xdfeEyQoMxIXbkvr0gge1a1x3yRULJ5CCaUeOxFmtlcGZelFl5CxtgfiAc0",
        "$6$rounds=123456$asaltof16chars..", "a short string", "$6$rounds=123456$asaltof16chars..$BtCwjqMJGx5hrJhZywWvt0RLE8uZ4oPwcelCjmw2kSYu.Ec6ycULevoBK25fs2xXgMNrCzIMVcgEJAstJeonj1",
        "$6$rounds=10$roundstoolow", "the minimum number is still observed", "$6$rounds=1000$roundstoolow$kUMsbe306n21p9R.FRkW3IGn.S9NPN0x50YhH1xhLsPuWGsUSklZt58jaTfF4ZEQpyUNGc0dqbpBYYBaHHrsX.",
      };

    System.out.println("Starting Sha512Crypt tests now...");

    for (int t=0; t<(msgs.length/3); t++)
      {
        String plainText = msgs[t*3+1];
        String cryptText = msgs[t*3+2];

        String result = Sha512_crypt(plainText, cryptText, 0);

        System.out.println("test " + t + " result is:" + result);
        System.out.println("test " + t + " should be:" + cryptText);

        if (result.equals(cryptText))
          {
            System.out.println("Passed Crypt well");
          }
        else
          {
            System.out.println("Failed Crypt Badly");
            throw new RuntimeException();
          }

        if (verifyPassword(plainText, cryptText))
          {
            System.out.println("Passed verifyPassword well");
          }
        else
          {
            System.out.println("Failed verifyPassword Badly");
            throw new RuntimeException();
          }
      }
  }

  /**
   * Test rig
   */

  public static void main(String arg[])
  {
    selfTest();
  }
}