From 14f5bb29e8d888e4943d26ad57c921f14fd5b6ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Prante?= Date: Fri, 31 May 2024 10:59:18 +0200 Subject: [PATCH] add more testst for EdEC in JDK --- .../net/security/test/Ed25519KeyTest.java | 38 +++++++++++++++++++ .../security/test/PrivateKeyReaderTest.java | 4 ++ .../test/eddsa/EdDSAPublicKeyTest.java | 15 ++++++-- 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/net-security/src/test/java/org/xbib/net/security/test/Ed25519KeyTest.java b/net-security/src/test/java/org/xbib/net/security/test/Ed25519KeyTest.java index d763127..9a7a073 100644 --- a/net-security/src/test/java/org/xbib/net/security/test/Ed25519KeyTest.java +++ b/net-security/src/test/java/org/xbib/net/security/test/Ed25519KeyTest.java @@ -4,7 +4,13 @@ import java.nio.charset.StandardCharsets; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.Signature; +import java.security.interfaces.EdECPublicKey; +import java.security.spec.X509EncodedKeySpec; import org.junit.jupiter.api.Test; +import org.xbib.net.security.eddsa.EdDSAPublicKey; +import org.xbib.net.security.eddsa.Utils; +import org.xbib.net.security.util.HexUtil; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; public class Ed25519KeyTest { @@ -21,4 +27,36 @@ public class Ed25519KeyTest { sig.update("Hello Jörg".getBytes(StandardCharsets.UTF_8)); assertTrue(sig.verify(s)); } + + @Test + public void testPublicKeyPayload() throws Exception { + String TEST_KEY = "302a300506032b65700321007d3691f280f8c8e7be7380e12bf6004a380c13d8fc1d7fed14aef4f301e4ca36"; + X509EncodedKeySpec encoded = new X509EncodedKeySpec(Utils.hexToBytes(TEST_KEY)); + EdDSAPublicKey key = new EdDSAPublicKey(encoded); + assertEquals("7d3691f280f8c8e7be7380e12bf6004a380c13d8fc1d7fed14aef4f301e4ca36", HexUtil.toHex(key.getAbyte())); + java.security.KeyFactory keyFactory = java.security.KeyFactory.getInstance("EdDSA"); + EdECPublicKey edECPublicKey = (EdECPublicKey) keyFactory.generatePublic(encoded); + byte[] b = edECPublicKey.getPoint().getY().toByteArray(); + reverse(b); + if (edECPublicKey.getPoint().isXOdd()) { + b[b.length - 1] |= (byte) 0x80; + } + assertEquals("7d3691f280f8c8e7be7380e12bf6004a380c13d8fc1d7fed14aef4f301e4ca36", HexUtil.toHex(b)); + } + + private static void reverse(byte [] arr) { + int i = 0; + int j = arr.length - 1; + while (i < j) { + swap(arr, i, j); + i++; + j--; + } + } + + private static void swap(byte[] arr, int i, int j) { + byte tmp = arr[i]; + arr[i] = arr[j]; + arr[j] = tmp; + } } diff --git a/net-security/src/test/java/org/xbib/net/security/test/PrivateKeyReaderTest.java b/net-security/src/test/java/org/xbib/net/security/test/PrivateKeyReaderTest.java index 21ccf31..6d3f268 100644 --- a/net-security/src/test/java/org/xbib/net/security/test/PrivateKeyReaderTest.java +++ b/net-security/src/test/java/org/xbib/net/security/test/PrivateKeyReaderTest.java @@ -32,6 +32,7 @@ public class PrivateKeyReaderTest { assertNotNull(publicKey); assertEquals("DSA", publicKey.getAlgorithm()); match("SHA1withDSA", privateKey, publicKey); + inputStream.close(); } } @@ -48,6 +49,7 @@ public class PrivateKeyReaderTest { assertNotNull(publicKey); assertEquals("RSA", publicKey.getAlgorithm()); match("SHA256withRSA", privateKey, publicKey); + inputStream.close(); } } @@ -64,6 +66,7 @@ public class PrivateKeyReaderTest { assertNotNull(publicKey); assertEquals("EC", publicKey.getAlgorithm()); match("SHA256withECDSA", privateKey, publicKey); + inputStream.close(); } } @@ -80,6 +83,7 @@ public class PrivateKeyReaderTest { assertNotNull(publicKey); assertEquals("EdDSA", publicKey.getAlgorithm()); match("EdDSA", privateKey, publicKey); + inputStream.close(); } } diff --git a/net-security/src/test/java/org/xbib/net/security/test/eddsa/EdDSAPublicKeyTest.java b/net-security/src/test/java/org/xbib/net/security/test/eddsa/EdDSAPublicKeyTest.java index b5dc63d..1c2b07d 100644 --- a/net-security/src/test/java/org/xbib/net/security/test/eddsa/EdDSAPublicKeyTest.java +++ b/net-security/src/test/java/org/xbib/net/security/test/eddsa/EdDSAPublicKeyTest.java @@ -3,16 +3,17 @@ package org.xbib.net.security.test.eddsa; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*; +import java.security.spec.InvalidKeySpecException; import java.security.spec.X509EncodedKeySpec; +import java.util.logging.Level; +import java.util.logging.Logger; import org.junit.jupiter.api.Test; import org.xbib.net.security.eddsa.EdDSAPublicKey; import org.xbib.net.security.eddsa.Utils; import org.xbib.net.security.eddsa.spec.EdDSAPublicKeySpec; +import org.xbib.net.security.util.HexUtil; -/** - * - */ public class EdDSAPublicKeyTest { /** * The example public key MCowBQYDK2VwAyEAGb9ECWmEzf6FQbrBZ9w7lshQhqowtrbLDFw4rXAxZuE= @@ -70,4 +71,12 @@ public class EdDSAPublicKeyTest { // Check assertThat(keyOut.getEncoded(), is(equalTo(TEST_PUBKEY))); } + + @Test + public void testPublicKeyPayload() throws InvalidKeySpecException { + String TEST_KEY = "302a300506032b65700321007d3691f280f8c8e7be7380e12bf6004a380c13d8fc1d7fed14aef4f301e4ca36"; + X509EncodedKeySpec encoded = new X509EncodedKeySpec(Utils.hexToBytes(TEST_KEY)); + EdDSAPublicKey key = new EdDSAPublicKey(encoded); + Logger.getAnonymousLogger().log(Level.INFO, "a = " + HexUtil.toHex(key.getAbyte())); + } }