Ed25519Verify implements PublicKeyVerify { @Override public void verify(byte[] signature, byte[] data) throws GeneralSecurityException { if (signature.length != SIGNATURE_LEN) { throw new GeneralSecurityException( String.format("The length of the signature is not %s.", SIGNATURE_LEN)); } if (!Ed25519.verify(data, signature, publicKey.getBytes())) { throw new GeneralSecurityException("Signature check failed."); } } Ed25519Verify(final byte[] publicKey); @Override void verify(byte[] signature, byte[] data); static final int PUBLIC_KEY_LEN; static final int SIGNATURE_LEN; }
@Test public void testVerificationWithWycheproofVectors() 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[] publicKey = Hex.decode(key.getString("pk")); 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"); Ed25519Verify verifier = new Ed25519Verify(publicKey); try { verifier.verify(sig, msg); if (result.equals("invalid")) { System.out.printf("FAIL %s: accepting invalid signature%n", tcId); errors++; } } catch (GeneralSecurityException ex) { if (result.equals("valid")) { System.out.printf("FAIL %s: rejecting valid signature, exception: %s%n", tcId, ex); errors++; } } } } assertEquals(0, errors); }