|
24 | 24 | * |
25 | 25 | */ |
26 | 26 | 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"); |
30 | 31 |
|
31 | 32 | @Rule |
32 | 33 | public ExpectedException exception = ExpectedException.none(); |
@@ -75,16 +76,52 @@ public void testVerifyWrongSigLength() throws Exception { |
75 | 76 | //Signature sgr = Signature.getInstance("EdDSA", "I2P"); |
76 | 77 | Signature sgr = new EdDSAEngine(MessageDigest.getInstance("SHA-512")); |
77 | 78 |
|
78 | | - EdDSAPublicKeySpec pubKey = new EdDSAPublicKeySpec(ZERO_PK, |
| 79 | + EdDSAPublicKeySpec pubKey = new EdDSAPublicKeySpec(TEST_PK, |
79 | 80 | EdDSANamedCurveTable.getByName("ed25519-sha-512")); |
80 | 81 | PublicKey vKey = new EdDSAPublicKey(pubKey); |
81 | 82 | sgr.initVerify(vKey); |
82 | 83 |
|
83 | | - byte[] message = "This is a secret message".getBytes(Charset.forName("UTF-8")); |
84 | | - sgr.update(message); |
| 84 | + sgr.update(TEST_MSG); |
85 | 85 |
|
86 | 86 | exception.expect(SignatureException.class); |
87 | 87 | exception.expectMessage("signature length is wrong"); |
88 | 88 | sgr.verify(new byte[] {0}); |
89 | 89 | } |
| 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 | + } |
90 | 127 | } |
0 commit comments