Skip to content

Commit

Permalink
CryptoKey: assorted cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
bertm committed Oct 5, 2024
1 parent 88d3fb4 commit cb127a0
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 21 deletions.
2 changes: 0 additions & 2 deletions src/freenet/crypt/DSAGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
8 changes: 4 additions & 4 deletions src/freenet/crypt/DSAPrivateKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

23 changes: 8 additions & 15 deletions src/freenet/crypt/DSAPublicKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

/**
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -158,15 +156,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);
}
}
Expand Down

0 comments on commit cb127a0

Please sign in to comment.