Mercurial > 510Connectbot
annotate src/ch/ethz/ssh2/crypto/digest/HMAC.java @ 347:bb7d8a7babbe
compensate for SecureRandom bug on older devices
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Thu, 31 Jul 2014 18:57:57 -0700 |
parents | 071eccdff8ea |
children |
rev | line source |
---|---|
273
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
1 /* |
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
2 * Copyright (c) 2006-2011 Christian Plattner. All rights reserved. |
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
3 * Please refer to the LICENSE.txt for licensing details. |
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
4 */ |
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
5 package ch.ethz.ssh2.crypto.digest; |
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
6 |
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
7 import java.security.DigestException; |
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
8 |
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
9 /** |
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
10 * HMAC. |
307 | 11 * |
273
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
12 * @author Christian Plattner |
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
13 * @version 2.50, 03/15/10 |
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
14 */ |
307 | 15 public final class HMAC implements Digest { |
16 Digest md; | |
17 byte[] k_xor_ipad; | |
18 byte[] k_xor_opad; | |
273
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
19 |
307 | 20 byte[] tmp; |
273
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
21 |
307 | 22 int size; |
273
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
23 |
307 | 24 public HMAC(Digest md, byte[] key, int size) throws DigestException { |
25 this.md = md; | |
26 this.size = size; | |
27 tmp = new byte[md.getDigestLength()]; | |
28 final int BLOCKSIZE = 64; | |
29 k_xor_ipad = new byte[BLOCKSIZE]; | |
30 k_xor_opad = new byte[BLOCKSIZE]; | |
273
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
31 |
307 | 32 if (key.length > BLOCKSIZE) { |
33 md.reset(); | |
34 md.update(key); | |
35 md.digest(tmp); | |
36 key = tmp; | |
37 } | |
273
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
38 |
307 | 39 System.arraycopy(key, 0, k_xor_ipad, 0, key.length); |
40 System.arraycopy(key, 0, k_xor_opad, 0, key.length); | |
273
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
41 |
307 | 42 for (int i = 0; i < BLOCKSIZE; i++) { |
43 k_xor_ipad[i] ^= 0x36; | |
44 k_xor_opad[i] ^= 0x5C; | |
45 } | |
46 | |
47 md.update(k_xor_ipad); | |
48 } | |
273
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
49 |
307 | 50 public final int getDigestLength() { |
51 return size; | |
52 } | |
273
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
53 |
307 | 54 public final void update(byte b) { |
55 md.update(b); | |
56 } | |
273
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
57 |
307 | 58 public final void update(byte[] b) { |
59 md.update(b); | |
60 } | |
273
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
61 |
307 | 62 public final void update(byte[] b, int off, int len) { |
63 md.update(b, off, len); | |
64 } | |
273
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
65 |
307 | 66 public final void reset() { |
67 md.reset(); | |
68 md.update(k_xor_ipad); | |
69 } | |
273
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
70 |
307 | 71 public final void digest(byte[] out) throws DigestException { |
72 digest(out, 0); | |
73 } | |
273
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
74 |
307 | 75 public final void digest(byte[] out, int off) throws DigestException { |
76 md.digest(tmp); | |
77 md.update(k_xor_opad); | |
78 md.update(tmp); | |
79 md.digest(tmp); | |
80 System.arraycopy(tmp, 0, out, off, size); | |
81 md.update(k_xor_ipad); | |
82 } | |
273
91a31873c42a
start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
83 } |