Move HMac to I2PHMac, as jrandom implemented changes that make it

incompatible with the HMac in the android libraries.
This commit is contained in:
zzz
2009-03-13 18:27:29 +00:00
parent d26ac84126
commit 5a8b3eb8f3
3 changed files with 21 additions and 15 deletions

View File

@ -7,7 +7,8 @@ import net.i2p.data.Hash;
import net.i2p.data.SessionKey; import net.i2p.data.SessionKey;
import org.bouncycastle.crypto.Digest; import org.bouncycastle.crypto.Digest;
import org.bouncycastle.crypto.macs.HMac; import org.bouncycastle.crypto.Mac;
import org.bouncycastle.crypto.macs.I2PHMac;
/** /**
* Calculate the HMAC-SHA256 of a key+message. All the good stuff occurs * Calculate the HMAC-SHA256 of a key+message. All the good stuff occurs
@ -19,15 +20,15 @@ public class HMAC256Generator extends HMACGenerator {
public HMAC256Generator(I2PAppContext context) { super(context); } public HMAC256Generator(I2PAppContext context) { super(context); }
@Override @Override
protected HMac acquire() { protected Mac acquire() {
synchronized (_available) { synchronized (_available) {
if (_available.size() > 0) if (_available.size() > 0)
return (HMac)_available.remove(0); return (Mac)_available.remove(0);
} }
// the HMAC is hardcoded to use SHA256 digest size // the HMAC is hardcoded to use SHA256 digest size
// for backwards compatability. next time we have a backwards // for backwards compatability. next time we have a backwards
// incompatible change, we should update this by removing ", 32" // incompatible change, we should update this by removing ", 32"
return new HMac(new Sha256ForMAC()); return new I2PHMac(new Sha256ForMAC());
} }
private class Sha256ForMAC extends Sha256Standalone implements Digest { private class Sha256ForMAC extends Sha256Standalone implements Digest {

View File

@ -10,7 +10,8 @@ import net.i2p.data.Hash;
import net.i2p.data.SessionKey; import net.i2p.data.SessionKey;
import org.bouncycastle.crypto.digests.MD5Digest; import org.bouncycastle.crypto.digests.MD5Digest;
import org.bouncycastle.crypto.macs.HMac; import org.bouncycastle.crypto.Mac;
import org.bouncycastle.crypto.macs.I2PHMac;
/** /**
* Calculate the HMAC-MD5 of a key+message. All the good stuff occurs * Calculate the HMAC-MD5 of a key+message. All the good stuff occurs
@ -49,7 +50,7 @@ public class HMACGenerator {
if ((key == null) || (key.getData() == null) || (data == null)) if ((key == null) || (key.getData() == null) || (data == null))
throw new NullPointerException("Null arguments for HMAC"); throw new NullPointerException("Null arguments for HMAC");
HMac mac = acquire(); Mac mac = acquire();
mac.init(key.getData()); mac.init(key.getData());
mac.update(data, offset, length); mac.update(data, offset, length);
//byte rv[] = new byte[Hash.HASH_LENGTH]; //byte rv[] = new byte[Hash.HASH_LENGTH];
@ -73,7 +74,7 @@ public class HMACGenerator {
if ((key == null) || (key.getData() == null) || (curData == null)) if ((key == null) || (key.getData() == null) || (curData == null))
throw new NullPointerException("Null arguments for HMAC"); throw new NullPointerException("Null arguments for HMAC");
HMac mac = acquire(); Mac mac = acquire();
mac.init(key.getData()); mac.init(key.getData());
mac.update(curData, curOffset, curLength); mac.update(curData, curOffset, curLength);
byte rv[] = acquireTmp(); byte rv[] = acquireTmp();
@ -86,17 +87,17 @@ public class HMACGenerator {
return eq; return eq;
} }
protected HMac acquire() { protected Mac acquire() {
synchronized (_available) { synchronized (_available) {
if (_available.size() > 0) if (_available.size() > 0)
return (HMac)_available.remove(0); return (Mac)_available.remove(0);
} }
// the HMAC is hardcoded to use SHA256 digest size // the HMAC is hardcoded to use SHA256 digest size
// for backwards compatability. next time we have a backwards // for backwards compatability. next time we have a backwards
// incompatible change, we should update this by removing ", 32" // incompatible change, we should update this by removing ", 32"
return new HMac(new MD5Digest(), 32); return new I2PHMac(new MD5Digest(), 32);
} }
private void release(HMac mac) { private void release(Mac mac) {
synchronized (_available) { synchronized (_available) {
if (_available.size() < 64) if (_available.size() < 64)
_available.add(mac); _available.add(mac);

View File

@ -42,8 +42,12 @@ import org.bouncycastle.crypto.Mac;
* a frequently used buffer (called on doFinal). changes released into the public * a frequently used buffer (called on doFinal). changes released into the public
* domain in 2005. * domain in 2005.
* *
* This is renamed from HMac because the constructor HMac(digest, sz) does not exist
* in the standard bouncycastle library, thus it conflicts in JVMs that contain the
* standard library (Android).
*
*/ */
public class HMac public class I2PHMac
implements Mac implements Mac
{ {
private final static int BLOCK_LENGTH = 64; private final static int BLOCK_LENGTH = 64;
@ -56,12 +60,12 @@ implements Mac
private byte[] inputPad = new byte[BLOCK_LENGTH]; private byte[] inputPad = new byte[BLOCK_LENGTH];
private byte[] outputPad = new byte[BLOCK_LENGTH]; private byte[] outputPad = new byte[BLOCK_LENGTH];
public HMac( public I2PHMac(
Digest digest) Digest digest)
{ {
this(digest, digest.getDigestSize()); this(digest, digest.getDigestSize());
} }
public HMac( public I2PHMac(
Digest digest, int sz) Digest digest, int sz)
{ {
this.digest = digest; this.digest = digest;