From a3ab8a99b26d8ef53c665e1c4a718a18544c70a4 Mon Sep 17 00:00:00 2001 From: Bert Massop Date: Sat, 5 Oct 2024 19:51:04 +0200 Subject: [PATCH 01/10] CryptoKey: remove commented out code --- src/freenet/crypt/CryptoKey.java | 6 ------ src/freenet/crypt/DSAGroup.java | 13 ------------- src/freenet/crypt/DSAPrivateKey.java | 12 ------------ 3 files changed, 31 deletions(-) diff --git a/src/freenet/crypt/CryptoKey.java b/src/freenet/crypt/CryptoKey.java index a31414996a6..b866634403f 100644 --- a/src/freenet/crypt/CryptoKey.java +++ b/src/freenet/crypt/CryptoKey.java @@ -40,8 +40,6 @@ public static CryptoKey read(InputStream i) throws IOException, CryptFormatExcep } } -// public abstract void write(OutputStream o) throws IOException; - public abstract String keyType(); public abstract byte[] fingerprint(); public abstract byte[] asBytes(); @@ -69,10 +67,6 @@ public String toString() { return b.toString(); } -// protected void write(OutputStream o, String clazz) throws IOException { -// UTF8.writeWithLength(o, clazz); -// } -// public String fingerprintToString() { String fphex = HexUtil.bytesToHex(fingerprint()); StringBuilder b = new StringBuilder(40 + 10); diff --git a/src/freenet/crypt/DSAGroup.java b/src/freenet/crypt/DSAGroup.java index eba84170c8a..0552382fd31 100644 --- a/src/freenet/crypt/DSAGroup.java +++ b/src/freenet/crypt/DSAGroup.java @@ -45,19 +45,6 @@ protected DSAGroup() { g = null; } - /** - * Parses a DSA Group from a string, where p, q, and g are in unsigned - * hex-strings, separated by a commas - */ - // see readFromField() below - //public static DSAGroup parse(String grp) { - // StringTokenizer str=new StringTokenizer(grp, ","); - // BigInteger p,q,g; - // p = new BigInteger(str.nextToken(), 16); - // q = new BigInteger(str.nextToken(), 16); - // g = new BigInteger(str.nextToken(), 16); - // return new DSAGroup(p,q,g); - //} public static CryptoKey read(InputStream i) throws IOException, CryptFormatException { BigInteger p, q, g; p = Util.readMPI(i); diff --git a/src/freenet/crypt/DSAPrivateKey.java b/src/freenet/crypt/DSAPrivateKey.java index 1fa66251ed0..75beab534a2 100644 --- a/src/freenet/crypt/DSAPrivateKey.java +++ b/src/freenet/crypt/DSAPrivateKey.java @@ -24,12 +24,6 @@ public DSAPrivateKey(BigInteger x, DSAGroup g) { throw new IllegalArgumentException(); } - // this is dangerous... better to force people to construct the - // BigInteger themselves so they know what is going on with the sign - //public DSAPrivateKey(byte[] x) { - // this.x = new BigInteger(1, x); - //} - public DSAPrivateKey(DSAGroup g, Random r) { BigInteger tempX; do { @@ -61,12 +55,6 @@ public String toLongString() { return "x="+HexUtil.biToHex(x); } - // what? why is DSAGroup passed in? - //public static CryptoKey readFromField(DSAGroup group, String field) { - // //BigInteger x=Util.byteArrayToMPI(Util.hexToBytes(field)); - // return new DSAPrivateKey(new BigInteger(field, 16)); - //} - @Override public byte[] asBytes() { return Util.MPIbytes(x); From 3cca620c91f0be05137e0481308fe36e13962c02 Mon Sep 17 00:00:00 2001 From: Bert Massop Date: Sat, 5 Oct 2024 19:53:32 +0200 Subject: [PATCH 02/10] CryptoKey: remove testing code --- src/freenet/crypt/CryptoKey.java | 60 ++------------------------------ 1 file changed, 2 insertions(+), 58 deletions(-) diff --git a/src/freenet/crypt/CryptoKey.java b/src/freenet/crypt/CryptoKey.java index b866634403f..b347b051ee7 100644 --- a/src/freenet/crypt/CryptoKey.java +++ b/src/freenet/crypt/CryptoKey.java @@ -3,16 +3,11 @@ * http://www.gnu.org/ for further details of the GPL. */ package freenet.crypt; -import java.io.DataInputStream; -import java.io.IOException; -import java.io.InputStream; import java.io.Serializable; -import java.lang.reflect.Method; import java.math.BigInteger; import java.security.MessageDigest; import freenet.support.HexUtil; -import freenet.support.Logger; public abstract class CryptoKey implements CryptoElement, Serializable { @@ -21,44 +16,19 @@ public abstract class CryptoKey implements CryptoElement, Serializable { CryptoKey() { } - public static CryptoKey read(InputStream i) throws IOException, CryptFormatException { - DataInputStream dis = new DataInputStream(i); - String type = dis.readUTF(); - try { - Class keyClass = Class.forName(type); - Method m = - keyClass.getMethod("read", new Class[] { InputStream.class }); - return (CryptoKey) m.invoke(null, dis); - } catch (Exception e) { - e.printStackTrace(); - if (e instanceof CryptFormatException) - throw (CryptFormatException) e; - if (e instanceof IOException) - throw (IOException) e; - Logger.error(CryptoKey.class, "Unknown exception while reading CryptoKey", e); - return null; - } - } - public abstract String keyType(); public abstract byte[] fingerprint(); public abstract byte[] asBytes(); - protected byte[] fingerprint(BigInteger[] quantities) { + static byte[] fingerprint(BigInteger[] quantities) { MessageDigest shactx = HashType.SHA1.get(); - for (BigInteger quantity: quantities) { + for (BigInteger quantity : quantities) { byte[] mpi = Util.MPIbytes(quantity); shactx.update(mpi, 0, mpi.length); } return shactx.digest(); } - public String verboseToString() { - StringBuilder b = new StringBuilder(); - b.append(toString()).append('\t').append(fingerprintToString()); - return b.toString(); - } - @Override public String toString() { StringBuilder b = new StringBuilder(keyType().length() + 1 + 4); @@ -67,30 +37,4 @@ public String toString() { return b.toString(); } - public String fingerprintToString() { - String fphex = HexUtil.bytesToHex(fingerprint()); - StringBuilder b = new StringBuilder(40 + 10); - b - .append(fphex.substring(0, 4)) - .append(' ') - .append(fphex.substring(4, 8)) - .append(' ') - .append(fphex.substring(8, 12)) - .append(' ') - .append(fphex.substring(12, 16)) - .append(' ') - .append(fphex.substring(16, 20)) - .append(" ") - .append(fphex.substring(20, 24)) - .append(' ') - .append(fphex.substring(24, 28)) - .append(' ') - .append(fphex.substring(28, 32)) - .append(' ') - .append(fphex.substring(32, 36)) - .append(' ') - .append(fphex.substring(36, 40)); - return b.toString(); - } - } From 360dec52bdd6e3b9cd090a61258e28cfb5822856 Mon Sep 17 00:00:00 2001 From: Bert Massop Date: Sat, 5 Oct 2024 20:18:55 +0200 Subject: [PATCH 03/10] CryptoKey: stop making defensive copies of immutable data --- src/freenet/crypt/DSAGroup.java | 11 ----------- src/freenet/crypt/DSAPublicKey.java | 12 ------------ src/freenet/keys/ClientSSK.java | 5 +---- test/freenet/store/PubkeyStoreTest.java | 7 +++---- test/freenet/store/SimplePubkeyCacheTest.java | 7 +++---- 5 files changed, 7 insertions(+), 35 deletions(-) diff --git a/src/freenet/crypt/DSAGroup.java b/src/freenet/crypt/DSAGroup.java index 0552382fd31..6fbaef88f0b 100644 --- a/src/freenet/crypt/DSAGroup.java +++ b/src/freenet/crypt/DSAGroup.java @@ -32,12 +32,6 @@ public DSAGroup(BigInteger p, BigInteger q, BigInteger g) { throw new IllegalArgumentException(); } - private DSAGroup(DSAGroup group) { - this.p = new BigInteger(1, group.p.toByteArray()); - this.q = new BigInteger(1, group.q.toByteArray()); - this.g = new BigInteger(1, group.g.toByteArray()); - } - protected DSAGroup() { // For serialization. p = null; @@ -151,9 +145,4 @@ public String toLongString() { return "p="+HexUtil.biToHex(p)+", q="+HexUtil.biToHex(q)+", g="+HexUtil.biToHex(g); } - public DSAGroup cloneKey() { - if(this == Global.DSAgroupBigA) return this; - return new DSAGroup(this); - } - } diff --git a/src/freenet/crypt/DSAPublicKey.java b/src/freenet/crypt/DSAPublicKey.java index c978c6fb820..dbaaa5bc718 100644 --- a/src/freenet/crypt/DSAPublicKey.java +++ b/src/freenet/crypt/DSAPublicKey.java @@ -63,14 +63,6 @@ public DSAPublicKey(byte[] pubkeyBytes) throws IOException, CryptFormatException this(new ByteArrayInputStream(pubkeyBytes)); } - private DSAPublicKey(DSAPublicKey key) { - fingerprint = null; // regen when needed - this.y = new BigInteger(1, key.y.toByteArray()); - DSAGroup g = key.group; - if(g != null) g = g.cloneKey(); - this.group = g; - } - public static DSAPublicKey create(byte[] pubkeyAsBytes) throws CryptFormatException { try { return new DSAPublicKey(new ByteArrayInputStream(pubkeyAsBytes)); @@ -216,8 +208,4 @@ public byte[] getRoutingKey() { return asBytesHash(); } - public DSAPublicKey cloneKey() { - return new DSAPublicKey(this); - } - } diff --git a/src/freenet/keys/ClientSSK.java b/src/freenet/keys/ClientSSK.java index 811c4e0a775..96af26c7d7c 100644 --- a/src/freenet/keys/ClientSSK.java +++ b/src/freenet/keys/ClientSSK.java @@ -44,10 +44,7 @@ public class ClientSSK extends ClientKey { private ClientSSK(ClientSSK key) { this.cryptoAlgorithm = key.cryptoAlgorithm; this.docName = key.docName; - if(key.pubKey != null) - this.pubKey = key.pubKey.cloneKey(); - else - this.pubKey = null; + this.pubKey = key.pubKey; pubKeyHash = key.pubKeyHash.clone(); cryptoKey = key.cryptoKey.clone(); ehDocname = key.ehDocname.clone(); diff --git a/test/freenet/store/PubkeyStoreTest.java b/test/freenet/store/PubkeyStoreTest.java index 4145cfffd19..16a2357e781 100644 --- a/test/freenet/store/PubkeyStoreTest.java +++ b/test/freenet/store/PubkeyStoreTest.java @@ -1,20 +1,19 @@ package freenet.store; -import static org.junit.Assert.*; +import static org.junit.Assert.assertTrue; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.Random; -import org.junit.Test; - import freenet.crypt.DSAGroup; import freenet.crypt.DSAPrivateKey; import freenet.crypt.DSAPublicKey; import freenet.crypt.Global; import freenet.support.ByteArrayWrapper; import freenet.support.math.MersenneTwister; +import org.junit.Test; public class PubkeyStoreTest { @@ -31,7 +30,7 @@ public void testSimple() throws IOException { DSAPublicKey key = new DSAPublicKey(group, privKey); byte[] hash = key.asBytesHash(); ByteArrayWrapper w = new ByteArrayWrapper(hash); - map.put(w, key.cloneKey()); + map.put(w, key); pk.put(hash, key, false); assertTrue(pk.fetch(hash, false, false, null).equals(key)); } diff --git a/test/freenet/store/SimplePubkeyCacheTest.java b/test/freenet/store/SimplePubkeyCacheTest.java index 7034299d4f7..39a28fd6da0 100644 --- a/test/freenet/store/SimplePubkeyCacheTest.java +++ b/test/freenet/store/SimplePubkeyCacheTest.java @@ -1,19 +1,18 @@ package freenet.store; -import static org.junit.Assert.*; +import static org.junit.Assert.assertTrue; import java.util.HashMap; import java.util.Map; import java.util.Random; -import org.junit.Test; - import freenet.crypt.DSAGroup; import freenet.crypt.DSAPrivateKey; import freenet.crypt.DSAPublicKey; import freenet.crypt.Global; import freenet.support.ByteArrayWrapper; import freenet.support.math.MersenneTwister; +import org.junit.Test; public class SimplePubkeyCacheTest { @@ -31,7 +30,7 @@ public void testSimple() { DSAPublicKey key = new DSAPublicKey(group, privKey); byte[] hash = key.asBytesHash(); ByteArrayWrapper w = new ByteArrayWrapper(hash); - map.put(w, key.cloneKey()); + map.put(w, key); pubkeys.cacheKey(hash, key, false, false, false, false, false); assertTrue(pubkeys.getKey(hash, false, false, null).equals(key)); } From 7324107a2a2e18ea6d7a56b580e5b3ef6ef19e25 Mon Sep 17 00:00:00 2001 From: Bert Massop Date: Sat, 5 Oct 2024 20:31:39 +0200 Subject: [PATCH 04/10] CryptoKey: simplify fingerprint code --- src/freenet/crypt/CryptoKey.java | 2 +- src/freenet/crypt/DSAGroup.java | 6 +----- src/freenet/crypt/DSAPrivateKey.java | 2 +- src/freenet/crypt/DSAPublicKey.java | 10 ++-------- 4 files changed, 5 insertions(+), 15 deletions(-) diff --git a/src/freenet/crypt/CryptoKey.java b/src/freenet/crypt/CryptoKey.java index b347b051ee7..d1d7ec07487 100644 --- a/src/freenet/crypt/CryptoKey.java +++ b/src/freenet/crypt/CryptoKey.java @@ -20,7 +20,7 @@ public abstract class CryptoKey implements CryptoElement, Serializable { public abstract byte[] fingerprint(); public abstract byte[] asBytes(); - static byte[] fingerprint(BigInteger[] quantities) { + static byte[] fingerprint(BigInteger... quantities) { MessageDigest shactx = HashType.SHA1.get(); for (BigInteger quantity : quantities) { byte[] mpi = Util.MPIbytes(quantity); diff --git a/src/freenet/crypt/DSAGroup.java b/src/freenet/crypt/DSAGroup.java index 6fbaef88f0b..29c92a45300 100644 --- a/src/freenet/crypt/DSAGroup.java +++ b/src/freenet/crypt/DSAGroup.java @@ -72,11 +72,7 @@ public BigInteger getG() { @Override public byte[] fingerprint() { - BigInteger fp[] = new BigInteger[3]; - fp[0] = p; - fp[1] = q; - fp[2] = g; - return fingerprint(fp); + return fingerprint(p, q, g); } @Override diff --git a/src/freenet/crypt/DSAPrivateKey.java b/src/freenet/crypt/DSAPrivateKey.java index 75beab534a2..90fc642aecb 100644 --- a/src/freenet/crypt/DSAPrivateKey.java +++ b/src/freenet/crypt/DSAPrivateKey.java @@ -62,7 +62,7 @@ public byte[] asBytes() { @Override public byte[] fingerprint() { - return fingerprint(new BigInteger[] {x}); + return fingerprint(x); } public SimpleFieldSet asFieldSet() { diff --git a/src/freenet/crypt/DSAPublicKey.java b/src/freenet/crypt/DSAPublicKey.java index dbaaa5bc718..91a0dc3525c 100644 --- a/src/freenet/crypt/DSAPublicKey.java +++ b/src/freenet/crypt/DSAPublicKey.java @@ -22,8 +22,7 @@ public class DSAPublicKey extends CryptoKey implements StorableBlock { public static final int HASH_LENGTH = 32; /** Null means use Global.DSAgroupBigA. This makes persistence simpler. */ private final DSAGroup group; - private volatile byte[] fingerprint; - + public DSAPublicKey(DSAGroup g, BigInteger y) { if(y.signum() != 1) throw new IllegalArgumentException(); @@ -143,12 +142,7 @@ public byte[] asPaddedBytes() { @Override public byte[] fingerprint() { - byte[] fingerprint = this.fingerprint; - if (fingerprint == null) { - fingerprint = fingerprint(new BigInteger[]{y}); - this.fingerprint = fingerprint; - } - return fingerprint; + return fingerprint(y); } public boolean equals(DSAPublicKey o) { From fd14db514c9d186d03d732558cab4fd91b681c66 Mon Sep 17 00:00:00 2001 From: Bert Massop Date: Sat, 5 Oct 2024 20:34:29 +0200 Subject: [PATCH 05/10] CryptoKey: remove unnecessary serialization constructors --- src/freenet/crypt/DSAGroup.java | 7 ------- src/freenet/crypt/DSAPrivateKey.java | 5 ----- src/freenet/crypt/DSAPublicKey.java | 6 ------ 3 files changed, 18 deletions(-) diff --git a/src/freenet/crypt/DSAGroup.java b/src/freenet/crypt/DSAGroup.java index 29c92a45300..d3112f235b9 100644 --- a/src/freenet/crypt/DSAGroup.java +++ b/src/freenet/crypt/DSAGroup.java @@ -32,13 +32,6 @@ public DSAGroup(BigInteger p, BigInteger q, BigInteger g) { throw new IllegalArgumentException(); } - protected DSAGroup() { - // For serialization. - p = null; - q = null; - g = null; - } - public static CryptoKey read(InputStream i) throws IOException, CryptFormatException { BigInteger p, q, g; p = Util.readMPI(i); diff --git a/src/freenet/crypt/DSAPrivateKey.java b/src/freenet/crypt/DSAPrivateKey.java index 90fc642aecb..3fdf84ef96f 100644 --- a/src/freenet/crypt/DSAPrivateKey.java +++ b/src/freenet/crypt/DSAPrivateKey.java @@ -31,11 +31,6 @@ public DSAPrivateKey(DSAGroup g, Random r) { } while (tempX.compareTo(g.getQ()) > -1 || tempX.compareTo(BigInteger.ZERO) < 1); this.x = tempX; } - - protected DSAPrivateKey() { - // For serialization. - x = null; - } @Override public String keyType() { diff --git a/src/freenet/crypt/DSAPublicKey.java b/src/freenet/crypt/DSAPublicKey.java index 91a0dc3525c..0f7d070390a 100644 --- a/src/freenet/crypt/DSAPublicKey.java +++ b/src/freenet/crypt/DSAPublicKey.java @@ -69,12 +69,6 @@ public static DSAPublicKey create(byte[] pubkeyAsBytes) throws CryptFormatExcept throw new CryptFormatException(e); } } - - protected DSAPublicKey() { - // For serialization. - y = null; - group = null; - } public BigInteger getY() { return y; From bd3927df4c3a7e4ffaf2689e6eabba48d426b953 Mon Sep 17 00:00:00 2001 From: Bert Massop Date: Sat, 5 Oct 2024 20:43:58 +0200 Subject: [PATCH 06/10] CryptoKey: simplify equals and hashCode --- src/freenet/crypt/DSAGroup.java | 15 +++++++-------- src/freenet/crypt/DSAPublicKey.java | 14 ++++++-------- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/src/freenet/crypt/DSAGroup.java b/src/freenet/crypt/DSAGroup.java index d3112f235b9..239ebd7da01 100644 --- a/src/freenet/crypt/DSAGroup.java +++ b/src/freenet/crypt/DSAGroup.java @@ -6,6 +6,7 @@ import java.io.IOException; import java.io.InputStream; import java.math.BigInteger; +import java.util.Objects; import freenet.node.FSParseException; import freenet.support.Base64; @@ -82,21 +83,19 @@ public byte[] asBytes() { @Override public boolean equals(Object o) { - if (this == o) // Not necessary, but a very cheap optimization - return true; - return (o instanceof DSAGroup) && p.equals(((DSAGroup) o).p) - && q.equals(((DSAGroup) o).q) && g.equals(((DSAGroup) o).g); + return (o instanceof DSAGroup) && equals((DSAGroup) o); } public boolean equals(DSAGroup o) { - if (this == o) // Not necessary, but a very cheap optimization - return true; - return p.equals(o.p) && q.equals(o.q) && g.equals(o.g); + if (this == o) { + return true; + } + return Objects.equals(p, o.p) && Objects.equals(q, o.q) && Objects.equals(g, o.g); } @Override public int hashCode() { - return p.hashCode() ^ q.hashCode() ^ g.hashCode(); + return Objects.hash(p, q, g); } public SimpleFieldSet asFieldSet() { diff --git a/src/freenet/crypt/DSAPublicKey.java b/src/freenet/crypt/DSAPublicKey.java index 0f7d070390a..4daa4b9287d 100644 --- a/src/freenet/crypt/DSAPublicKey.java +++ b/src/freenet/crypt/DSAPublicKey.java @@ -6,6 +6,7 @@ import java.io.InputStream; import java.math.BigInteger; import java.util.Arrays; +import java.util.Objects; import freenet.node.FSParseException; import freenet.store.StorableBlock; @@ -140,23 +141,20 @@ public byte[] fingerprint() { } public boolean equals(DSAPublicKey o) { - if(this == o) // Not necessary, but a very cheap optimization + if (this == o) { return true; - return y.equals(o.y) && getGroup().equals(o.getGroup()); + } + return Objects.equals(y, o.y) && Objects.equals(getGroup(), o.getGroup()); } @Override public int hashCode() { - return y.hashCode() ^ getGroup().hashCode(); + return Objects.hash(y, getGroup()); } @Override public boolean equals(Object o) { - if(this == o) // Not necessary, but a very cheap optimization - return true; - else if((o == null) || (o.getClass() != this.getClass())) - return false; - return y.equals(((DSAPublicKey) o).y) && getGroup().equals(((DSAPublicKey) o).getGroup()); + return o instanceof DSAPublicKey && equals((DSAPublicKey) o); } public int compareTo(Object other) { From d5a9891855d9d1c6ede1c42c62f563093c098f2c Mon Sep 17 00:00:00 2001 From: Bert Massop Date: Sat, 5 Oct 2024 20:53:22 +0200 Subject: [PATCH 07/10] CryptoKey: cache lazily computed key hash in DSAPublicKey This value is used relatively frequently for various routing related purposes, keep the computed hash around for future use. --- src/freenet/crypt/DSAPublicKey.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/freenet/crypt/DSAPublicKey.java b/src/freenet/crypt/DSAPublicKey.java index 4daa4b9287d..ec7eee0baac 100644 --- a/src/freenet/crypt/DSAPublicKey.java +++ b/src/freenet/crypt/DSAPublicKey.java @@ -24,6 +24,11 @@ public class DSAPublicKey extends CryptoKey implements StorableBlock { /** Null means use Global.DSAgroupBigA. This makes persistence simpler. */ private final DSAGroup group; + /** + * Cached key hash, computed lazily on first access to {@link #asBytesHash()}. + */ + private transient volatile byte[] bytesHash; + public DSAPublicKey(DSAGroup g, BigInteger y) { if(y.signum() != 1) throw new IllegalArgumentException(); @@ -122,8 +127,12 @@ public byte[] asBytes() { } public byte[] asBytesHash() { - byte[] hash = SHA256.digest(asBytes()); - return hash; + byte[] result = bytesHash; + if (result == null) { + result = SHA256.digest(asBytes()); + bytesHash = result; + } + return Arrays.copyOf(result, result.length); } public byte[] asPaddedBytes() { From 437bad7a5d5a3cb8d570a74ab07a9d88835d62f4 Mon Sep 17 00:00:00 2001 From: Bert Massop Date: Sat, 5 Oct 2024 20:58:37 +0200 Subject: [PATCH 08/10] CryptoKey: ensure same checks for all DSAPublicKey constructors --- src/freenet/crypt/DSAGroup.java | 2 +- src/freenet/crypt/DSAPublicKey.java | 13 ++----------- 2 files changed, 3 insertions(+), 12 deletions(-) diff --git a/src/freenet/crypt/DSAGroup.java b/src/freenet/crypt/DSAGroup.java index 239ebd7da01..92b6e8571ca 100644 --- a/src/freenet/crypt/DSAGroup.java +++ b/src/freenet/crypt/DSAGroup.java @@ -33,7 +33,7 @@ public DSAGroup(BigInteger p, BigInteger q, BigInteger g) { throw new IllegalArgumentException(); } - public static CryptoKey read(InputStream i) throws IOException, CryptFormatException { + public static DSAGroup read(InputStream i) throws IOException, CryptFormatException { BigInteger p, q, g; p = Util.readMPI(i); q = Util.readMPI(i); diff --git a/src/freenet/crypt/DSAPublicKey.java b/src/freenet/crypt/DSAPublicKey.java index ec7eee0baac..0ebcbc8958d 100644 --- a/src/freenet/crypt/DSAPublicKey.java +++ b/src/freenet/crypt/DSAPublicKey.java @@ -44,11 +44,7 @@ public DSAPublicKey(DSAGroup g, BigInteger y) { * available, will save some conversions and string allocations. */ public DSAPublicKey(DSAGroup g, String yAsHexString) throws NumberFormatException { - this.y = new BigInteger(yAsHexString, 16); - if(y.signum() != 1) - throw new IllegalArgumentException(); - if(g == Global.DSAgroupBigA) g = null; - this.group = g; + this(g, new BigInteger(yAsHexString, 16)); } public DSAPublicKey(DSAGroup g, DSAPrivateKey p) { @@ -56,12 +52,7 @@ public DSAPublicKey(DSAGroup g, DSAPrivateKey p) { } public DSAPublicKey(InputStream is) throws IOException, CryptFormatException { - DSAGroup g = (DSAGroup) DSAGroup.read(is); - if(g == Global.DSAgroupBigA) g = null; - group = g; - y = Util.readMPI(is); - if(y.compareTo(getGroup().getP()) > 0) - throw new IllegalArgumentException("y must be < p but y=" + y + " p=" + getGroup().getP()); + this(DSAGroup.read(is), Util.readMPI(is)); } public DSAPublicKey(byte[] pubkeyBytes) throws IOException, CryptFormatException { From 660f43c7de80ac5b9bd245d67892f59d8cd26745 Mon Sep 17 00:00:00 2001 From: Bert Massop Date: Sat, 5 Oct 2024 21:01:00 +0200 Subject: [PATCH 09/10] CryptoKey: remove odd unused methods from DSAPublicKey The `keyId` method has no reasonable use, whereas `compareTo` exists without implementing the Comparable interface. Remove both. --- src/freenet/crypt/DSAPublicKey.java | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/src/freenet/crypt/DSAPublicKey.java b/src/freenet/crypt/DSAPublicKey.java index 0ebcbc8958d..90cfa2d602b 100644 --- a/src/freenet/crypt/DSAPublicKey.java +++ b/src/freenet/crypt/DSAPublicKey.java @@ -98,10 +98,6 @@ public static CryptoKey read(InputStream i) throws IOException, CryptFormatExcep return new DSAPublicKey(i); } - public int keyId() { - return y.intValue(); - } - @Override public String toLongString() { return "y=" + HexUtil.biToHex(y); @@ -157,13 +153,6 @@ public boolean equals(Object o) { return o instanceof DSAPublicKey && equals((DSAPublicKey) o); } - public int compareTo(Object other) { - if(other instanceof DSAPublicKey) - return getY().compareTo(((DSAPublicKey) other).getY()); - else - return -1; - } - public SimpleFieldSet asFieldSet() { SimpleFieldSet fs = new SimpleFieldSet(true); fs.putSingle("y", Base64.encode(y.toByteArray())); From 4466dbf99e661f68b7b4685892be6aefe0962fe1 Mon Sep 17 00:00:00 2001 From: Bert Massop Date: Sat, 5 Oct 2024 21:12:53 +0200 Subject: [PATCH 10/10] CryptoKey: assorted cleanup --- src/freenet/crypt/DSAGroup.java | 2 -- src/freenet/crypt/DSAPrivateKey.java | 8 ++++---- src/freenet/crypt/DSAPublicKey.java | 23 ++++++++--------------- 3 files changed, 12 insertions(+), 21 deletions(-) diff --git a/src/freenet/crypt/DSAGroup.java b/src/freenet/crypt/DSAGroup.java index 92b6e8571ca..cb3a0487a9f 100644 --- a/src/freenet/crypt/DSAGroup.java +++ b/src/freenet/crypt/DSAGroup.java @@ -20,8 +20,6 @@ */ public class DSAGroup extends CryptoKey { private static final long serialVersionUID = -1; - - protected static final int Q_BIT_LENGTH = 256; private final BigInteger p, q, g; diff --git a/src/freenet/crypt/DSAPrivateKey.java b/src/freenet/crypt/DSAPrivateKey.java index 3fdf84ef96f..e139d0dc73d 100644 --- a/src/freenet/crypt/DSAPrivateKey.java +++ b/src/freenet/crypt/DSAPrivateKey.java @@ -67,10 +67,10 @@ public SimpleFieldSet asFieldSet() { } public static DSAPrivateKey create(SimpleFieldSet fs, DSAGroup group) throws IllegalBase64Exception { - BigInteger y = new BigInteger(1, Base64.decode(fs.get("x"))); - if(y.bitLength() > 512) + BigInteger x = new BigInteger(1, Base64.decode(fs.get("x"))); + if (x.bitLength() > 512) { throw new IllegalBase64Exception("Probably a pubkey"); - return new DSAPrivateKey(y, group); + } + return new DSAPrivateKey(x, group); } } - diff --git a/src/freenet/crypt/DSAPublicKey.java b/src/freenet/crypt/DSAPublicKey.java index 90cfa2d602b..a32ea0a7455 100644 --- a/src/freenet/crypt/DSAPublicKey.java +++ b/src/freenet/crypt/DSAPublicKey.java @@ -33,10 +33,10 @@ public DSAPublicKey(DSAGroup g, BigInteger y) { if(y.signum() != 1) throw new IllegalArgumentException(); this.y = y; - if(g == Global.DSAgroupBigA) g = null; - this.group = g; - if(y.compareTo(getGroup().getP()) > 0) - throw new IllegalArgumentException("y must be < p but y=" + y + " p=" + g.getP()); + this.group = g == Global.DSAgroupBigA ? null : g; + if (y.compareTo(getGroup().getP()) > 0) { + throw new IllegalArgumentException("y must be < p but y=" + y + " p=" + getGroup().getP()); + } } /** @@ -88,10 +88,8 @@ public String keyType() { return "DSA.p"; } - // Nope, this is fine public final DSAGroup getGroup() { - if(group == null) return Global.DSAgroupBigA; - else return group; + return group == null ? Global.DSAgroupBigA : group; } public static CryptoKey read(InputStream i) throws IOException, CryptFormatException { @@ -160,15 +158,10 @@ public SimpleFieldSet asFieldSet() { } public static DSAPublicKey create(SimpleFieldSet set, DSAGroup group) throws FSParseException { - BigInteger x; - try { - x = new BigInteger(1, Base64.decode(set.get("y"))); - } catch (IllegalBase64Exception e) { - throw new FSParseException(e); - } try { - return new DSAPublicKey(group, x); - } catch (IllegalArgumentException e) { + BigInteger y = new BigInteger(1, Base64.decode(set.get("y"))); + return new DSAPublicKey(group, y); + } catch (IllegalBase64Exception | IllegalArgumentException e) { throw new FSParseException(e); } }