Skip to content

Commit 566c055

Browse files
committed
Add tests to check that an EdDSAEngine object can be reused with the same key
1 parent cc24e94 commit 566c055

File tree

1 file changed

+43
-6
lines changed

1 file changed

+43
-6
lines changed

test/net/i2p/crypto/eddsa/EdDSAEngineTest.java

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@
2424
*
2525
*/
2626
public class EdDSAEngineTest {
27-
static final byte[] ZERO_SEED = Utils.hexToBytes("0000000000000000000000000000000000000000000000000000000000000000");
28-
static final byte[] ZERO_PK = Utils.hexToBytes("3b6a27bcceb6a42d62a3a8d02a6f0d73653215771de243a63ac048a18b59da29");
29-
static final byte[] ZERO_MSG_SIG = Utils.hexToBytes("94825896c7075c31bcb81f06dba2bdcd9dcf16e79288d4b9f87c248215c8468d475f429f3de3b4a2cf67fe17077ae19686020364d6d4fa7a0174bab4a123ba0f");
27+
static final byte[] TEST_SEED = Utils.hexToBytes("0000000000000000000000000000000000000000000000000000000000000000");
28+
static final byte[] TEST_PK = Utils.hexToBytes("3b6a27bcceb6a42d62a3a8d02a6f0d73653215771de243a63ac048a18b59da29");
29+
static final byte[] TEST_MSG = "This is a secret message".getBytes(Charset.forName("UTF-8"));
30+
static final byte[] TEST_MSG_SIG = Utils.hexToBytes("94825896c7075c31bcb81f06dba2bdcd9dcf16e79288d4b9f87c248215c8468d475f429f3de3b4a2cf67fe17077ae19686020364d6d4fa7a0174bab4a123ba0f");
3031

3132
@Rule
3233
public ExpectedException exception = ExpectedException.none();
@@ -75,16 +76,52 @@ public void testVerifyWrongSigLength() throws Exception {
7576
//Signature sgr = Signature.getInstance("EdDSA", "I2P");
7677
Signature sgr = new EdDSAEngine(MessageDigest.getInstance("SHA-512"));
7778

78-
EdDSAPublicKeySpec pubKey = new EdDSAPublicKeySpec(ZERO_PK,
79+
EdDSAPublicKeySpec pubKey = new EdDSAPublicKeySpec(TEST_PK,
7980
EdDSANamedCurveTable.getByName("ed25519-sha-512"));
8081
PublicKey vKey = new EdDSAPublicKey(pubKey);
8182
sgr.initVerify(vKey);
8283

83-
byte[] message = "This is a secret message".getBytes(Charset.forName("UTF-8"));
84-
sgr.update(message);
84+
sgr.update(TEST_MSG);
8585

8686
exception.expect(SignatureException.class);
8787
exception.expectMessage("signature length is wrong");
8888
sgr.verify(new byte[] {0});
8989
}
90+
91+
@Test
92+
public void testSignResetsForReuse() throws Exception {
93+
Signature sgr = new EdDSAEngine(MessageDigest.getInstance("SHA-512"));
94+
EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName("ed25519-sha-512");
95+
96+
EdDSAPrivateKeySpec privKey = new EdDSAPrivateKeySpec(TEST_SEED, spec);
97+
PrivateKey sKey = new EdDSAPrivateKey(privKey);
98+
sgr.initSign(sKey);
99+
100+
// First usage
101+
sgr.update(new byte[] {0});
102+
sgr.sign();
103+
104+
// Second usage
105+
sgr.update(TEST_MSG);
106+
assertThat("Second sign failed", sgr.sign(), is(equalTo(TEST_MSG_SIG)));
107+
}
108+
109+
@Test
110+
public void testVerifyResetsForReuse() throws Exception {
111+
//Signature sgr = Signature.getInstance("EdDSA", "I2P");
112+
Signature sgr = new EdDSAEngine(MessageDigest.getInstance("SHA-512"));
113+
114+
EdDSAPublicKeySpec pubKey = new EdDSAPublicKeySpec(TEST_PK,
115+
EdDSANamedCurveTable.getByName("ed25519-sha-512"));
116+
PublicKey vKey = new EdDSAPublicKey(pubKey);
117+
sgr.initVerify(vKey);
118+
119+
// First usage
120+
sgr.update(new byte[] {0});
121+
sgr.verify(TEST_MSG_SIG);
122+
123+
// Second usage
124+
sgr.update(TEST_MSG);
125+
assertThat("Second verify failed", sgr.verify(TEST_MSG_SIG), is(true));
126+
}
90127
}

0 commit comments

Comments
 (0)