modify scram mechanisms to use guava hashing

This commit is contained in:
Daniel Gultsch 2022-10-24 13:11:30 +02:00
parent e2b9f0e77a
commit a29c7c725e
7 changed files with 47 additions and 57 deletions

View file

@ -1,15 +1,13 @@
package eu.siacs.conversations.crypto.sasl; package eu.siacs.conversations.crypto.sasl;
import android.util.Base64; import android.util.Base64;
import android.util.Log;
import com.google.common.base.CaseFormat; import com.google.common.base.CaseFormat;
import com.google.common.base.Objects; import com.google.common.base.Objects;
import com.google.common.cache.Cache; import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheBuilder;
import com.google.common.hash.HashFunction;
import org.bouncycastle.crypto.Digest;
import org.bouncycastle.crypto.macs.HMac;
import org.bouncycastle.crypto.params.KeyParameter;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.security.InvalidKeyException; import java.security.InvalidKeyException;
@ -17,6 +15,7 @@ import java.util.concurrent.ExecutionException;
import javax.net.ssl.SSLSocket; import javax.net.ssl.SSLSocket;
import eu.siacs.conversations.Config;
import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.utils.CryptoHelper; import eu.siacs.conversations.utils.CryptoHelper;
@ -54,14 +53,14 @@ abstract class ScramMechanism extends SaslMechanism {
clientFirstMessageBare = ""; clientFirstMessageBare = "";
} }
protected abstract HMac getHMAC(); protected abstract HashFunction getHMac(final byte[] key);
protected abstract Digest getDigest(); protected abstract HashFunction getDigest();
private KeyPair getKeyPair(final String password, final String salt, final int iterations) private KeyPair getKeyPair(final String password, final String salt, final int iterations)
throws ExecutionException { throws ExecutionException {
return CACHE.get( return CACHE.get(
new CacheKey(getHMAC().getAlgorithmName(), password, salt, iterations), new CacheKey(getMechanism(), password, salt, iterations),
() -> { () -> {
final byte[] saltedPassword, serverKey, clientKey; final byte[] saltedPassword, serverKey, clientKey;
saltedPassword = saltedPassword =
@ -76,21 +75,11 @@ abstract class ScramMechanism extends SaslMechanism {
} }
private byte[] hmac(final byte[] key, final byte[] input) throws InvalidKeyException { private byte[] hmac(final byte[] key, final byte[] input) throws InvalidKeyException {
final HMac hMac = getHMAC(); return getHMac(key).hashBytes(input).asBytes();
hMac.init(new KeyParameter(key));
hMac.update(input, 0, input.length);
final byte[] out = new byte[hMac.getMacSize()];
hMac.doFinal(out, 0);
return out;
} }
public byte[] digest(final byte[] bytes) { private byte[] digest(final byte[] bytes) {
final Digest digest = getDigest(); return getDigest().hashBytes(bytes).asBytes();
digest.reset();
digest.update(bytes, 0, bytes.length);
final byte[] out = new byte[digest.getDigestSize()];
digest.doFinal(out, 0);
return out;
} }
/* /*

View file

@ -1,8 +1,7 @@
package eu.siacs.conversations.crypto.sasl; package eu.siacs.conversations.crypto.sasl;
import org.bouncycastle.crypto.Digest; import com.google.common.hash.HashFunction;
import org.bouncycastle.crypto.digests.SHA1Digest; import com.google.common.hash.Hashing;
import org.bouncycastle.crypto.macs.HMac;
import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.entities.Account;
@ -15,13 +14,13 @@ public class ScramSha1 extends ScramMechanism {
} }
@Override @Override
protected HMac getHMAC() { protected HashFunction getHMac(final byte[] key) {
return new HMac(new SHA1Digest()); return Hashing.hmacSha1(key);
} }
@Override @Override
protected Digest getDigest() { protected HashFunction getDigest() {
return new SHA1Digest(); return Hashing.sha1();
} }
@Override @Override

View file

@ -1,8 +1,7 @@
package eu.siacs.conversations.crypto.sasl; package eu.siacs.conversations.crypto.sasl;
import org.bouncycastle.crypto.Digest; import com.google.common.hash.HashFunction;
import org.bouncycastle.crypto.digests.SHA1Digest; import com.google.common.hash.Hashing;
import org.bouncycastle.crypto.macs.HMac;
import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.entities.Account;
@ -15,13 +14,13 @@ public class ScramSha1Plus extends ScramPlusMechanism {
} }
@Override @Override
protected HMac getHMAC() { protected HashFunction getHMac(final byte[] key) {
return new HMac(new SHA1Digest()); return Hashing.hmacSha1(key);
} }
@Override @Override
protected Digest getDigest() { protected HashFunction getDigest() {
return new SHA1Digest(); return Hashing.sha1();
} }
@Override @Override

View file

@ -1,5 +1,8 @@
package eu.siacs.conversations.crypto.sasl; package eu.siacs.conversations.crypto.sasl;
import com.google.common.hash.HashFunction;
import com.google.common.hash.Hashing;
import org.bouncycastle.crypto.Digest; import org.bouncycastle.crypto.Digest;
import org.bouncycastle.crypto.digests.SHA256Digest; import org.bouncycastle.crypto.digests.SHA256Digest;
import org.bouncycastle.crypto.macs.HMac; import org.bouncycastle.crypto.macs.HMac;
@ -15,15 +18,14 @@ public class ScramSha256 extends ScramMechanism {
} }
@Override @Override
protected HMac getHMAC() { protected HashFunction getHMac(final byte[] key) {
return new HMac(new SHA256Digest()); return Hashing.hmacSha256(key);
} }
@Override @Override
protected Digest getDigest() { protected HashFunction getDigest() {
return new SHA256Digest(); return Hashing.sha256();
} }
@Override @Override
public int getPriority() { public int getPriority() {
return 25; return 25;

View file

@ -1,8 +1,7 @@
package eu.siacs.conversations.crypto.sasl; package eu.siacs.conversations.crypto.sasl;
import org.bouncycastle.crypto.Digest; import com.google.common.hash.HashFunction;
import org.bouncycastle.crypto.digests.SHA256Digest; import com.google.common.hash.Hashing;
import org.bouncycastle.crypto.macs.HMac;
import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.entities.Account;
@ -15,13 +14,13 @@ public class ScramSha256Plus extends ScramPlusMechanism {
} }
@Override @Override
protected HMac getHMAC() { protected HashFunction getHMac(final byte[] key) {
return new HMac(new SHA256Digest()); return Hashing.hmacSha256(key);
} }
@Override @Override
protected Digest getDigest() { protected HashFunction getDigest() {
return new SHA256Digest(); return Hashing.sha256();
} }
@Override @Override

View file

@ -1,5 +1,8 @@
package eu.siacs.conversations.crypto.sasl; package eu.siacs.conversations.crypto.sasl;
import com.google.common.hash.HashFunction;
import com.google.common.hash.Hashing;
import org.bouncycastle.crypto.Digest; import org.bouncycastle.crypto.Digest;
import org.bouncycastle.crypto.digests.SHA512Digest; import org.bouncycastle.crypto.digests.SHA512Digest;
import org.bouncycastle.crypto.macs.HMac; import org.bouncycastle.crypto.macs.HMac;
@ -15,13 +18,13 @@ public class ScramSha512 extends ScramMechanism {
} }
@Override @Override
protected HMac getHMAC() { protected HashFunction getHMac(final byte[] key) {
return new HMac(new SHA512Digest()); return Hashing.hmacSha512(key);
} }
@Override @Override
protected Digest getDigest() { protected HashFunction getDigest() {
return new SHA512Digest(); return Hashing.sha512();
} }
@Override @Override

View file

@ -1,8 +1,7 @@
package eu.siacs.conversations.crypto.sasl; package eu.siacs.conversations.crypto.sasl;
import org.bouncycastle.crypto.Digest; import com.google.common.hash.HashFunction;
import org.bouncycastle.crypto.digests.SHA512Digest; import com.google.common.hash.Hashing;
import org.bouncycastle.crypto.macs.HMac;
import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.entities.Account;
@ -15,13 +14,13 @@ public class ScramSha512Plus extends ScramPlusMechanism {
} }
@Override @Override
protected HMac getHMAC() { protected HashFunction getHMac(final byte[] key) {
return new HMac(new SHA512Digest()); return Hashing.hmacSha512(key);
} }
@Override @Override
protected Digest getDigest() { protected HashFunction getDigest() {
return new SHA512Digest(); return Hashing.sha512();
} }
@Override @Override