|
| 1 | +package io.jenkins.plugins.eddsa_api.security3404; |
| 2 | + |
| 3 | +import static org.hamcrest.CoreMatchers.is; |
| 4 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 5 | + |
| 6 | +import java.security.MessageDigest; |
| 7 | +import java.security.NoSuchAlgorithmException; |
| 8 | +import java.security.spec.InvalidKeySpecException; |
| 9 | +import java.security.spec.X509EncodedKeySpec; |
| 10 | +import java.util.List; |
| 11 | +import net.i2p.crypto.eddsa.EdDSAEngine; |
| 12 | +import net.i2p.crypto.eddsa.EdDSAPublicKey; |
| 13 | +import net.i2p.crypto.eddsa.Utils; |
| 14 | +import net.i2p.crypto.eddsa.spec.EdDSANamedCurveTable; |
| 15 | +import net.i2p.crypto.eddsa.spec.EdDSAParameterSpec; |
| 16 | +import org.junit.Test; |
| 17 | +import org.junit.runner.RunWith; |
| 18 | +import org.junit.runners.Parameterized; |
| 19 | + |
| 20 | +@RunWith(Parameterized.class) |
| 21 | +public class Security3404Test { |
| 22 | + private final String messageHex; |
| 23 | + private final String publicKeyHex; |
| 24 | + private final String signatureHex; |
| 25 | + |
| 26 | + private static final EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519); |
| 27 | + |
| 28 | + @Parameterized.Parameters |
| 29 | + public static List<List<String>> parameters() { |
| 30 | + // See https://eprint.iacr.org/2020/1244.pdf Table 6 c), as well as Section 5.1 for an explanation that these |
| 31 | + // signatures are supposed to fail to ensure SUF-CMA property |
| 32 | + return List.of( |
| 33 | + List.of( |
| 34 | + "85e241a07d148b41e47d62c63f830dc7a6851a0b1f33ae4bb2f507fb6cffec40", |
| 35 | + "442aad9f089ad9e14647b1ef9099a1ff4798d78589e66f28eca69c11f582a623", |
| 36 | + "e96f66be976d82e60150baecff9906684aebb1ef181f67a7189ac78ea23b6c0e547f7690a0e2ddcd04d87dbc3490dc19b3b3052f7ff0538cb68afb369ba3a514"), |
| 37 | + List.of( |
| 38 | + "85e241a07d148b41e47d62c63f830dc7a6851a0b1f33ae4bb2f507fb6cffec40", |
| 39 | + "442aad9f089ad9e14647b1ef9099a1ff4798d78589e66f28eca69c11f582a623", |
| 40 | + "8ce5b96c8f26d0ab6c47958c9e68b937104cd36e13c33566acd2fe8d38aa19427e71f98a473474f2f13f06f97c20d58cc3f54b8bd0d272f42b695dd7e89a8c22")); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void testCases5And6() throws NoSuchAlgorithmException { |
| 45 | + assertThat(verify_i2p(), is(false)); |
| 46 | + } |
| 47 | + |
| 48 | + public Security3404Test(List<String> parameters) { |
| 49 | + messageHex = parameters.get(0); |
| 50 | + publicKeyHex = parameters.get(1); |
| 51 | + signatureHex = parameters.get(2); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Return EdDSAPublicKey object from the hex representation of the compressed Edwards public key point. |
| 56 | + **/ |
| 57 | + // Code used under Apache 2.0 license from |
| 58 | + // https://github.com/novifinancial/ed25519-speccheck/blob/main/scripts/ed25519-java/src/main/java/Ed25519TestCase.java |
| 59 | + private EdDSAPublicKey decodePublicKey() throws InvalidKeySpecException { |
| 60 | + byte[] pk = Utils.hexToBytes(this.publicKeyHex); |
| 61 | + byte[] x509pk = EncodingUtils.compressedEd25519PublicKeyToX509(pk); |
| 62 | + X509EncodedKeySpec encoded = new X509EncodedKeySpec(x509pk); |
| 63 | + return new EdDSAPublicKey(encoded); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Pure Ed25519 signature verification using the i2p lib, it returns false if it fails or if an exception occurs). |
| 68 | + **/ |
| 69 | + // Code used under Apache 2.0 license from |
| 70 | + // https://github.com/novifinancial/ed25519-speccheck/blob/main/scripts/ed25519-java/src/main/java/Ed25519TestCase.java |
| 71 | + public boolean verify_i2p() { |
| 72 | + try { |
| 73 | + EdDSAPublicKey publicKey = decodePublicKey(); |
| 74 | + byte[] messageBytes = Utils.hexToBytes(this.messageHex); |
| 75 | + byte[] signatureBytes = Utils.hexToBytes(this.signatureHex); |
| 76 | + EdDSAEngine sgr = new EdDSAEngine(MessageDigest.getInstance(spec.getHashAlgorithm())); |
| 77 | + sgr.initVerify(publicKey); |
| 78 | + return sgr.verifyOneShot(messageBytes, signatureBytes); |
| 79 | + } catch (Exception e) { |
| 80 | + return false; |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments