Ed25519Sign implements PublicKeySign { @Override public byte[] sign(final byte[] data) throws GeneralSecurityException { return Ed25519.sign(data, publicKey, hashedPrivateKey); } Ed25519Sign(final byte[] privateKey); @Override byte[] sign(final byte[] data); static final int SECRET_KEY_LEN; }
@Test public void testSigningOneKeyWithMultipleMessages() throws Exception { Ed25519Sign.KeyPair keyPair = Ed25519Sign.KeyPair.newKeyPair(); Ed25519Sign signer = new Ed25519Sign(keyPair.getPrivateKey()); Ed25519Verify verifier = new Ed25519Verify(keyPair.getPublicKey()); for (int i = 0; i < 100; i++) { byte[] msg = Random.randBytes(20); byte[] sig = signer.sign(msg); try { verifier.verify(sig, msg); } catch (GeneralSecurityException ex) { fail( String.format( "\n\nMessage: %s\nSignature: %s\nPrivateKey: %s\nPublicKey: %s\n", TestUtil.hexEncode(msg), TestUtil.hexEncode(sig), TestUtil.hexEncode(keyPair.getPrivateKey()), TestUtil.hexEncode(keyPair.getPublicKey()))); } } }
@Test public void testSigningOneKeyWithTheSameMessage() throws Exception { Ed25519Sign.KeyPair keyPair = Ed25519Sign.KeyPair.newKeyPair(); Ed25519Sign signer = new Ed25519Sign(keyPair.getPrivateKey()); Ed25519Verify verifier = new Ed25519Verify(keyPair.getPublicKey()); byte[] msg = Random.randBytes(20); TreeSet<String> allSignatures = new TreeSet<String>(); for (int i = 0; i < 100; i++) { byte[] sig = signer.sign(msg); allSignatures.add(TestUtil.hexEncode(sig)); try { verifier.verify(sig, msg); } catch (GeneralSecurityException ex) { fail( String.format( "\n\nMessage: %s\nSignature: %s\nPrivateKey: %s\nPublicKey: %s\n", TestUtil.hexEncode(msg), TestUtil.hexEncode(sig), TestUtil.hexEncode(keyPair.getPrivateKey()), TestUtil.hexEncode(keyPair.getPublicKey()))); } } assertEquals(1, allSignatures.size()); }
@Test public void testSigningWithMultipleRandomKeysAndMessages() throws Exception { for (int i = 0; i < 100; i++) { Ed25519Sign.KeyPair keyPair = Ed25519Sign.KeyPair.newKeyPair(); Ed25519Sign signer = new Ed25519Sign(keyPair.getPrivateKey()); Ed25519Verify verifier = new Ed25519Verify(keyPair.getPublicKey()); byte[] msg = Random.randBytes(20); byte[] sig = signer.sign(msg); try { verifier.verify(sig, msg); } catch (GeneralSecurityException ex) { fail( String.format( "\n\nMessage: %s\nSignature: %s\nPrivateKey: %s\nPublicKey: %s\n", TestUtil.hexEncode(msg), TestUtil.hexEncode(sig), TestUtil.hexEncode(keyPair.getPrivateKey()), TestUtil.hexEncode(keyPair.getPublicKey()))); } } }
@Test public void testSigningWithWycheproofVectors() throws Exception { JSONObject json = WycheproofTestUtil.readJson("../wycheproof/testvectors/eddsa_test.json"); int errors = 0; JSONArray testGroups = json.getJSONArray("testGroups"); for (int i = 0; i < testGroups.length(); i++) { JSONObject group = testGroups.getJSONObject(i); JSONObject key = group.getJSONObject("key"); byte[] privateKey = Hex.decode(key.getString("sk")); JSONArray tests = group.getJSONArray("tests"); for (int j = 0; j < tests.length(); j++) { JSONObject testcase = tests.getJSONObject(j); String tcId = String.format( "testcase %d (%s)", testcase.getInt("tcId"), testcase.getString("comment")); byte[] msg = getMessage(testcase); byte[] sig = Hex.decode(testcase.getString("sig")); String result = testcase.getString("result"); if (result.equals("invalid")) { continue; } Ed25519Sign signer = new Ed25519Sign(privateKey); byte[] computedSig = signer.sign(msg); assertArrayEquals(tcId, sig, computedSig); } } assertEquals(0, errors); }