summaryrefslogtreecommitdiffstats
path: root/libjava/classpath/gnu/javax/crypto/mac/HMac.java
diff options
context:
space:
mode:
Diffstat (limited to 'libjava/classpath/gnu/javax/crypto/mac/HMac.java')
-rw-r--r--libjava/classpath/gnu/javax/crypto/mac/HMac.java195
1 files changed, 65 insertions, 130 deletions
diff --git a/libjava/classpath/gnu/javax/crypto/mac/HMac.java b/libjava/classpath/gnu/javax/crypto/mac/HMac.java
index c1f97b54195..f0e588d1831 100644
--- a/libjava/classpath/gnu/javax/crypto/mac/HMac.java
+++ b/libjava/classpath/gnu/javax/crypto/mac/HMac.java
@@ -48,70 +48,51 @@ import java.util.HashMap;
import java.util.Map;
/**
- * <p>The implementation of the <i>HMAC</i> (Keyed-Hash Message Authentication
- * Code).</p>
- *
- * <p><i>HMAC</i> can be used in combination with any iterated cryptographic
- * hash function. <i>HMAC</i> also uses a <i>secret key</i> for calculation and
+ * The implementation of the <i>HMAC</i> (Keyed-Hash Message Authentication
+ * Code).
+ * <p>
+ * <i>HMAC</i> can be used in combination with any iterated cryptographic hash
+ * function. <i>HMAC</i> also uses a <i>secret key</i> for calculation and
* verification of the message authentication values. The main goals behind this
- * construction are</p>
- *
+ * construction are:
* <ul>
- * <li>To use, without modifications, available hash functions. In
- * particular, hash functions that perform well in software, and for which
- * code is freely and widely available.</li>
- *
- * <li>To preserve the original performance of the hash function without
- * incurring a significant degradation.</li>
- *
- * <li>To use and handle keys in a simple way.</li>
- *
- * <li>To have a well understood cryptographic analysis of the strength of
- * the authentication mechanism based on reasonable assumptions on the
- * underlying hash function.</li>
- *
- * <li>To allow for easy replaceability of the underlying hash function in
- * case that faster or more secure hash functions are found or required.</li>
+ * <li>To use, without modifications, available hash functions. In particular,
+ * hash functions that perform well in software, and for which code is freely
+ * and widely available.</li>
+ * <li>To preserve the original performance of the hash function without
+ * incurring a significant degradation.</li>
+ * <li>To use and handle keys in a simple way.</li>
+ * <li>To have a well understood cryptographic analysis of the strength of the
+ * authentication mechanism based on reasonable assumptions on the underlying
+ * hash function.</li>
+ * <li>To allow for easy replaceability of the underlying hash function in case
+ * that faster or more secure hash functions are found or required.</li>
* </ul>
- *
- * <p>References:</p>
- *
+ * <p>
+ * References:
* <ol>
- * <li><a href="http://www.ietf.org/rfc/rfc-2104.txt">RFC 2104</a>HMAC:
- * Keyed-Hashing for Message Authentication.<br>
- * H. Krawczyk, M. Bellare, and R. Canetti.</li>
+ * <li><a href="http://www.ietf.org/rfc/rfc-2104.txt">RFC 2104</a>HMAC:
+ * Keyed-Hashing for Message Authentication.<br>
+ * H. Krawczyk, M. Bellare, and R. Canetti.</li>
* </ol>
*/
-public class HMac extends BaseMac implements Cloneable
+public class HMac
+ extends BaseMac
+ implements Cloneable
{
-
- // Constants and variables
- // -------------------------------------------------------------------------
-
public static final String USE_WITH_PKCS5_V2 = "gnu.crypto.hmac.pkcs5";
-
private static final byte IPAD_BYTE = 0x36;
-
private static final byte OPAD_BYTE = 0x5C;
-
/** caches the result of the correctness test, once executed. */
private static Boolean valid;
-
protected int macSize;
-
protected int blockSize;
-
protected IMessageDigest ipadHash;
-
protected IMessageDigest opadHash;
-
protected byte[] ipad;
- // Constructor(s)
- // -------------------------------------------------------------------------
-
/**
- * <p>Trivial constructor for use by concrete subclasses.</p>
+ * Trivial constructor for use by concrete subclasses.
*
* @param underlyingHash the underlying hash algorithm instance.
*/
@@ -124,14 +105,6 @@ public class HMac extends BaseMac implements Cloneable
ipadHash = opadHash = null;
}
- // Class methods
- // -------------------------------------------------------------------------
-
- // Instance methods
- // -------------------------------------------------------------------------
-
- // java.lang.Cloneable interface implementation ----------------------------
-
public Object clone() throws CloneNotSupportedException
{
HMac result = (HMac) super.clone();
@@ -145,88 +118,65 @@ public class HMac extends BaseMac implements Cloneable
return result;
}
- // implementation of abstract methods in BaseMac ---------------------------
-
public void init(Map attributes) throws InvalidKeyException,
IllegalStateException
{
Integer ts = (Integer) attributes.get(TRUNCATED_SIZE);
truncatedSize = (ts == null ? macSize : ts.intValue());
if (truncatedSize < (macSize / 2))
- {
- throw new IllegalArgumentException("Truncated size too small");
- }
+ throw new IllegalArgumentException("Truncated size too small");
else if (truncatedSize < 10)
- {
- throw new IllegalArgumentException("Truncated size less than 80 bits");
- }
+ throw new IllegalArgumentException("Truncated size less than 80 bits");
// we dont use/save the key outside this method
byte[] K = (byte[]) attributes.get(MAC_KEY_MATERIAL);
if (K == null)
{ // take it as an indication to re-use previous key if set
if (ipadHash == null)
- {
- throw new InvalidKeyException("Null key");
- }
+ throw new InvalidKeyException("Null key");
// we already went through the motions; ie. up to step #4. re-use
underlyingHash = (IMessageDigest) ipadHash.clone();
return;
}
- // for HMACs used in key-derivation functions (e.g. PBKDF2) the key
- // material need not be >= the (output) block size of the underlying
- // algorithm
+ // for HMACs used in key-derivation functions (e.g. PBKDF2) the key material
+ // need not be >= the (output) block size of the underlying algorithm
Boolean pkcs5 = (Boolean) attributes.get(USE_WITH_PKCS5_V2);
if (pkcs5 == null)
- {
- pkcs5 = Boolean.FALSE;
- }
- if (K.length < macSize && !pkcs5.booleanValue())
- {
- throw new InvalidKeyException("Key too short");
- }
+ pkcs5 = Boolean.FALSE;
+ if (K.length < macSize && ! pkcs5.booleanValue())
+ throw new InvalidKeyException("Key too short");
if (K.length > blockSize)
{
- // (0) replace K with HASH(K) if K is larger than the hash's
- // block size. Then pad with zeros until it is the correct
- // size (the next `if').
+ // (0) replace K with HASH(K) if K is larger than the hash's block size.
+ // Then pad with zeros until it is the correct size (the next `if').
underlyingHash.update(K, 0, K.length);
K = underlyingHash.digest();
}
if (K.length < blockSize)
{
- // (1) append zeros to the end of K to create a B byte string
- // (e.g., if K is of length 20 bytes and B=64, then K will be
- // appended with 44 zero bytes 0x00)
+ // (1) append zeros to the end of K to create a B byte string (e.g., if
+ // K is of length 20 bytes and B=64, then K will be appended with 44
+ // zero bytes 0x00)
int limit = (K.length > blockSize) ? blockSize : K.length;
byte[] newK = new byte[blockSize];
System.arraycopy(K, 0, newK, 0, limit);
K = newK;
}
-
underlyingHash.reset();
opadHash = (IMessageDigest) underlyingHash.clone();
if (ipad == null)
- {
- ipad = new byte[blockSize];
- }
- // (2) XOR (bitwise exclusive-OR) the B byte string computed in step
- // (1) with ipad
- // (3) append the stream of data 'text' to the B byte string resulting
- // from step (2)
+ ipad = new byte[blockSize];
+ // (2) XOR (bitwise exclusive-OR) the B byte string computed in step (1)
+ // with ipad
+ // (3) append the stream of data 'text' to the B byte string resulting from
+ // step (2)
// (4) apply H to the stream generated in step (3)
for (int i = 0; i < blockSize; i++)
- {
- // underlyingHash.update((byte)(K[i] ^ IPAD_BYTE));
- ipad[i] = (byte) (K[i] ^ IPAD_BYTE);
- }
+ ipad[i] = (byte)(K[i] ^ IPAD_BYTE);
for (int i = 0; i < blockSize; i++)
- {
- opadHash.update((byte) (K[i] ^ OPAD_BYTE));
- }
-
+ opadHash.update((byte)(K[i] ^ OPAD_BYTE));
underlyingHash.update(ipad, 0, blockSize);
ipadHash = (IMessageDigest) underlyingHash.clone();
K = null;
@@ -245,28 +195,21 @@ public class HMac extends BaseMac implements Cloneable
public byte[] digest()
{
if (ipadHash == null)
- {
- throw new IllegalStateException("HMAC not initialised");
- }
-
+ throw new IllegalStateException("HMAC not initialised");
byte[] out = underlyingHash.digest();
- // (5) XOR (bitwise exclusive-OR) the B byte string computed in
- // step (1) with opad
+ // (5) XOR (bitwise exclusive-OR) the B byte string computed in step (1)
+ // with opad
underlyingHash = (IMessageDigest) opadHash.clone();
- // (6) append the H result from step (4) to the B byte string
- // resulting from step (5)
+ // (6) append the H result from step (4) to the B byte string resulting from
+ // step (5)
underlyingHash.update(out, 0, macSize);
- // (7) apply H to the stream generated in step (6) and output
- // the result
+ // (7) apply H to the stream generated in step (6) and output the result
out = underlyingHash.digest(); // which also resets the underlying hash
-
// truncate and return
if (truncatedSize == macSize)
return out;
-
byte[] result = new byte[truncatedSize];
System.arraycopy(out, 0, result, 0, truncatedSize);
-
return result;
}
@@ -279,31 +222,25 @@ public class HMac extends BaseMac implements Cloneable
IMac mac = new HMac(new MD5()); // use rfc-2104 test vectors
String tv1 = "9294727A3638BB1C13F48EF8158BFC9D";
String tv3 = "56BE34521D144C88DBB8C733F0E8B3F6";
- byte[] k1 = new byte[] { 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B,
- 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B,
- 0x0B, 0x0B };
- byte[] k3 = new byte[] { (byte) 0xAA, (byte) 0xAA, (byte) 0xAA,
- (byte) 0xAA, (byte) 0xAA, (byte) 0xAA,
- (byte) 0xAA, (byte) 0xAA, (byte) 0xAA,
- (byte) 0xAA, (byte) 0xAA, (byte) 0xAA,
- (byte) 0xAA, (byte) 0xAA, (byte) 0xAA,
- (byte) 0xAA };
+ byte[] k1 = new byte[] {
+ 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B,
+ 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B };
+ byte[] k3 = new byte[] {
+ (byte) 0xAA, (byte) 0xAA, (byte) 0xAA, (byte) 0xAA,
+ (byte) 0xAA, (byte) 0xAA, (byte) 0xAA, (byte) 0xAA,
+ (byte) 0xAA, (byte) 0xAA, (byte) 0xAA, (byte) 0xAA,
+ (byte) 0xAA, (byte) 0xAA, (byte) 0xAA, (byte) 0xAA };
byte[] data = new byte[50];
for (int i = 0; i < 50;)
- {
- data[i++] = (byte) 0xDD;
- }
+ data[i++] = (byte) 0xDD;
HashMap map = new HashMap();
-
// test vector #1
map.put(MAC_KEY_MATERIAL, k1);
mac.init(map);
mac.update("Hi There".getBytes("ASCII"), 0, 8);
- if (!tv1.equals(Util.toString(mac.digest())))
- {
- valid = Boolean.FALSE;
- }
+ if (! tv1.equals(Util.toString(mac.digest())))
+ valid = Boolean.FALSE;
// test #2 is not used since it causes a "Key too short" exception
@@ -311,10 +248,8 @@ public class HMac extends BaseMac implements Cloneable
map.put(MAC_KEY_MATERIAL, k3);
mac.init(map);
mac.update(data, 0, 50);
- if (!tv3.equals(Util.toString(mac.digest())))
- {
- valid = Boolean.FALSE;
- }
+ if (! tv3.equals(Util.toString(mac.digest())))
+ valid = Boolean.FALSE;
valid = Boolean.TRUE;
}
catch (Exception x)
OpenPOWER on IntegriCloud