forked from I2P_Developers/i2p.i2p
Add tests to check that an EdDSAEngine object can be reused with the same key
This commit is contained in:
@ -24,9 +24,10 @@ import org.junit.rules.ExpectedException;
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class EdDSAEngineTest {
|
public class EdDSAEngineTest {
|
||||||
static final byte[] ZERO_SEED = Utils.hexToBytes("0000000000000000000000000000000000000000000000000000000000000000");
|
static final byte[] TEST_SEED = Utils.hexToBytes("0000000000000000000000000000000000000000000000000000000000000000");
|
||||||
static final byte[] ZERO_PK = Utils.hexToBytes("3b6a27bcceb6a42d62a3a8d02a6f0d73653215771de243a63ac048a18b59da29");
|
static final byte[] TEST_PK = Utils.hexToBytes("3b6a27bcceb6a42d62a3a8d02a6f0d73653215771de243a63ac048a18b59da29");
|
||||||
static final byte[] ZERO_MSG_SIG = Utils.hexToBytes("94825896c7075c31bcb81f06dba2bdcd9dcf16e79288d4b9f87c248215c8468d475f429f3de3b4a2cf67fe17077ae19686020364d6d4fa7a0174bab4a123ba0f");
|
static final byte[] TEST_MSG = "This is a secret message".getBytes(Charset.forName("UTF-8"));
|
||||||
|
static final byte[] TEST_MSG_SIG = Utils.hexToBytes("94825896c7075c31bcb81f06dba2bdcd9dcf16e79288d4b9f87c248215c8468d475f429f3de3b4a2cf67fe17077ae19686020364d6d4fa7a0174bab4a123ba0f");
|
||||||
|
|
||||||
@Rule
|
@Rule
|
||||||
public ExpectedException exception = ExpectedException.none();
|
public ExpectedException exception = ExpectedException.none();
|
||||||
@ -75,16 +76,52 @@ public class EdDSAEngineTest {
|
|||||||
//Signature sgr = Signature.getInstance("EdDSA", "I2P");
|
//Signature sgr = Signature.getInstance("EdDSA", "I2P");
|
||||||
Signature sgr = new EdDSAEngine(MessageDigest.getInstance("SHA-512"));
|
Signature sgr = new EdDSAEngine(MessageDigest.getInstance("SHA-512"));
|
||||||
|
|
||||||
EdDSAPublicKeySpec pubKey = new EdDSAPublicKeySpec(ZERO_PK,
|
EdDSAPublicKeySpec pubKey = new EdDSAPublicKeySpec(TEST_PK,
|
||||||
EdDSANamedCurveTable.getByName("ed25519-sha-512"));
|
EdDSANamedCurveTable.getByName("ed25519-sha-512"));
|
||||||
PublicKey vKey = new EdDSAPublicKey(pubKey);
|
PublicKey vKey = new EdDSAPublicKey(pubKey);
|
||||||
sgr.initVerify(vKey);
|
sgr.initVerify(vKey);
|
||||||
|
|
||||||
byte[] message = "This is a secret message".getBytes(Charset.forName("UTF-8"));
|
sgr.update(TEST_MSG);
|
||||||
sgr.update(message);
|
|
||||||
|
|
||||||
exception.expect(SignatureException.class);
|
exception.expect(SignatureException.class);
|
||||||
exception.expectMessage("signature length is wrong");
|
exception.expectMessage("signature length is wrong");
|
||||||
sgr.verify(new byte[] {0});
|
sgr.verify(new byte[] {0});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSignResetsForReuse() throws Exception {
|
||||||
|
Signature sgr = new EdDSAEngine(MessageDigest.getInstance("SHA-512"));
|
||||||
|
EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName("ed25519-sha-512");
|
||||||
|
|
||||||
|
EdDSAPrivateKeySpec privKey = new EdDSAPrivateKeySpec(TEST_SEED, spec);
|
||||||
|
PrivateKey sKey = new EdDSAPrivateKey(privKey);
|
||||||
|
sgr.initSign(sKey);
|
||||||
|
|
||||||
|
// First usage
|
||||||
|
sgr.update(new byte[] {0});
|
||||||
|
sgr.sign();
|
||||||
|
|
||||||
|
// Second usage
|
||||||
|
sgr.update(TEST_MSG);
|
||||||
|
assertThat("Second sign failed", sgr.sign(), is(equalTo(TEST_MSG_SIG)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testVerifyResetsForReuse() throws Exception {
|
||||||
|
//Signature sgr = Signature.getInstance("EdDSA", "I2P");
|
||||||
|
Signature sgr = new EdDSAEngine(MessageDigest.getInstance("SHA-512"));
|
||||||
|
|
||||||
|
EdDSAPublicKeySpec pubKey = new EdDSAPublicKeySpec(TEST_PK,
|
||||||
|
EdDSANamedCurveTable.getByName("ed25519-sha-512"));
|
||||||
|
PublicKey vKey = new EdDSAPublicKey(pubKey);
|
||||||
|
sgr.initVerify(vKey);
|
||||||
|
|
||||||
|
// First usage
|
||||||
|
sgr.update(new byte[] {0});
|
||||||
|
sgr.verify(TEST_MSG_SIG);
|
||||||
|
|
||||||
|
// Second usage
|
||||||
|
sgr.update(TEST_MSG);
|
||||||
|
assertThat("Second verify failed", sgr.verify(TEST_MSG_SIG), is(true));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user