add more testst for EdEC in JDK

This commit is contained in:
Jörg Prante 2024-05-31 10:59:18 +02:00
parent c912fbcb92
commit 14f5bb29e8
3 changed files with 54 additions and 3 deletions

View file

@ -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;
}
}

View file

@ -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();
}
}

View file

@ -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()));
}
}