Skip to content

Commit 486ac81

Browse files
committed
Enforce correct seed and hash lengths in EdDSAPrivateKeySpec
1 parent dd2a28b commit 486ac81

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/net/i2p/crypto/eddsa/spec/EdDSAPrivateKeySpec.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,12 @@ public class EdDSAPrivateKeySpec implements KeySpec {
3030
private final EdDSAParameterSpec spec;
3131

3232
/**
33-
* @throws IllegalArgumentException if hash algorithm is unsupported
33+
* @throws IllegalArgumentException if seed length is wrong or hash algorithm is unsupported
3434
*/
3535
public EdDSAPrivateKeySpec(byte[] seed, EdDSAParameterSpec spec) {
36+
if (seed.length != spec.getCurve().getField().getb()/8)
37+
throw new IllegalArgumentException("seed length is wrong");
38+
3639
this.spec = spec;
3740
this.seed = seed;
3841

@@ -65,9 +68,13 @@ public EdDSAPrivateKeySpec(byte[] seed, EdDSAParameterSpec spec) {
6568
* getSeed() will return null if this constructor is used.
6669
*
6770
* @param h the private key
71+
* @throws IllegalArgumentException if hash length is wrong
6872
* @since 0.1.1
6973
*/
7074
public EdDSAPrivateKeySpec(EdDSAParameterSpec spec, byte[] h) {
75+
if (h.length != spec.getCurve().getField().getb()/4)
76+
throw new IllegalArgumentException("hash length is wrong");
77+
7178
this.seed = null;
7279
this.h = h;
7380
this.spec = spec;

test/net/i2p/crypto/eddsa/spec/EdDSAPrivateKeySpecTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
import static org.junit.Assert.*;
1616
import net.i2p.crypto.eddsa.Utils;
1717

18+
import org.junit.Rule;
1819
import org.junit.Test;
20+
import org.junit.rules.ExpectedException;
1921

2022
/**
2123
* @author str4d
@@ -28,6 +30,9 @@ public class EdDSAPrivateKeySpecTest {
2830

2931
static final EdDSANamedCurveSpec ed25519 = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.CURVE_ED25519_SHA512);
3032

33+
@Rule
34+
public ExpectedException exception = ExpectedException.none();
35+
3136
/**
3237
* Test method for {@link net.i2p.crypto.eddsa.spec.EdDSAPrivateKeySpec#EdDSAPrivateKeySpec(byte[], net.i2p.crypto.eddsa.spec.EdDSAParameterSpec)}.
3338
*/
@@ -39,6 +44,13 @@ public void testEdDSAPrivateKeySpecFromSeed() {
3944
assertThat(key.getA().toByteArray(), is(equalTo(ZERO_PK)));
4045
}
4146

47+
@Test
48+
public void incorrectSeedLengthThrows() {
49+
exception.expect(IllegalArgumentException.class);
50+
exception.expectMessage("seed length is wrong");
51+
EdDSAPrivateKeySpec key = new EdDSAPrivateKeySpec(new byte[2], ed25519);
52+
}
53+
4254
/**
4355
* Test method for {@link net.i2p.crypto.eddsa.spec.EdDSAPrivateKeySpec#EdDSAPrivateKeySpec(net.i2p.crypto.eddsa.spec.EdDSAParameterSpec, byte[])}.
4456
*/
@@ -50,4 +62,10 @@ public void testEdDSAPrivateKeySpecFromH() {
5062
assertThat(key.getA().toByteArray(), is(equalTo(ZERO_PK)));
5163
}
5264

65+
@Test
66+
public void incorrectHashLengthThrows() {
67+
exception.expect(IllegalArgumentException.class);
68+
exception.expectMessage("hash length is wrong");
69+
EdDSAPrivateKeySpec key = new EdDSAPrivateKeySpec(ed25519, new byte[2]);
70+
}
5371
}

0 commit comments

Comments
 (0)