Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ public void testStartupNonFips() throws Throwable {
}

@Test
public void testStartupFips() throws Throwable {
public void testStartupFips() {
rjr.javaOptions("-Xmx128M", "-Djenkins.security.FIPS140.COMPLIANCE=true");
JenkinsStartupException jse = assertThrows(JenkinsStartupException.class, () -> {
rjr.then(r -> {
Jenkins.get().getPluginManager().uberClassLoader.loadClass("net.i2p.crypto.eddsa.EdDSAEngine");
fail("should not get here!");
});
});
JenkinsStartupException jse = assertThrows(
JenkinsStartupException.class,
() -> rjr.then(r -> {
Jenkins.get().getPluginManager().uberClassLoader.loadClass("net.i2p.crypto.eddsa.EdDSAEngine");
fail("should not get here!");
}));
assertThat(
jse.getMessage(),
containsString(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,60 +4,48 @@
import static org.hamcrest.MatcherAssert.assertThat;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.X509EncodedKeySpec;
import java.util.List;
import java.util.stream.Stream;
import net.i2p.crypto.eddsa.EdDSAEngine;
import net.i2p.crypto.eddsa.EdDSAPublicKey;
import net.i2p.crypto.eddsa.Utils;
import net.i2p.crypto.eddsa.spec.EdDSANamedCurveTable;
import net.i2p.crypto.eddsa.spec.EdDSAParameterSpec;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

@RunWith(Parameterized.class)
public class Security3404Test {
private final String messageHex;
private final String publicKeyHex;
private final String signatureHex;
class Security3404Test {

private static final EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519);

@Parameterized.Parameters
public static List<List<String>> parameters() {
static Stream<Arguments> parameters() {
// See https://eprint.iacr.org/2020/1244.pdf Table 6 c), as well as Section 5.1 for an explanation that these
// signatures are supposed to fail to ensure SUF-CMA property
return List.of(
List.of(
return Stream.of(
Arguments.arguments(
"85e241a07d148b41e47d62c63f830dc7a6851a0b1f33ae4bb2f507fb6cffec40",
"442aad9f089ad9e14647b1ef9099a1ff4798d78589e66f28eca69c11f582a623",
"e96f66be976d82e60150baecff9906684aebb1ef181f67a7189ac78ea23b6c0e547f7690a0e2ddcd04d87dbc3490dc19b3b3052f7ff0538cb68afb369ba3a514"),
List.of(
Arguments.arguments(
"85e241a07d148b41e47d62c63f830dc7a6851a0b1f33ae4bb2f507fb6cffec40",
"442aad9f089ad9e14647b1ef9099a1ff4798d78589e66f28eca69c11f582a623",
"8ce5b96c8f26d0ab6c47958c9e68b937104cd36e13c33566acd2fe8d38aa19427e71f98a473474f2f13f06f97c20d58cc3f54b8bd0d272f42b695dd7e89a8c22"));
}

@Test
public void testCases5And6() throws NoSuchAlgorithmException {
assertThat(verify_i2p(), is(false));
@ParameterizedTest
@MethodSource("parameters")
void testCases5And6(String messageHex, String publicKeyHex, String signatureHex) {
assertThat(verify_i2p(messageHex, publicKeyHex, signatureHex), is(false));
}

public Security3404Test(List<String> parameters) {
messageHex = parameters.get(0);
publicKeyHex = parameters.get(1);
signatureHex = parameters.get(2);
}

/**
* Return EdDSAPublicKey object from the hex representation of the compressed Edwards public key point.
**/
// Code used under Apache 2.0 license from
// https://github.com/novifinancial/ed25519-speccheck/blob/main/scripts/ed25519-java/src/main/java/Ed25519TestCase.java
private EdDSAPublicKey decodePublicKey() throws InvalidKeySpecException {
byte[] pk = Utils.hexToBytes(this.publicKeyHex);
private EdDSAPublicKey decodePublicKey(String publicKeyHex) throws InvalidKeySpecException {
byte[] pk = Utils.hexToBytes(publicKeyHex);
byte[] x509pk = EncodingUtils.compressedEd25519PublicKeyToX509(pk);
X509EncodedKeySpec encoded = new X509EncodedKeySpec(x509pk);
return new EdDSAPublicKey(encoded);
Expand All @@ -68,11 +56,11 @@ private EdDSAPublicKey decodePublicKey() throws InvalidKeySpecException {
**/
// Code used under Apache 2.0 license from
// https://github.com/novifinancial/ed25519-speccheck/blob/main/scripts/ed25519-java/src/main/java/Ed25519TestCase.java
public boolean verify_i2p() {
public boolean verify_i2p(String messageHex, String publicKeyHex, String signatureHex) {
try {
EdDSAPublicKey publicKey = decodePublicKey();
byte[] messageBytes = Utils.hexToBytes(this.messageHex);
byte[] signatureBytes = Utils.hexToBytes(this.signatureHex);
EdDSAPublicKey publicKey = decodePublicKey(publicKeyHex);
byte[] messageBytes = Utils.hexToBytes(messageHex);
byte[] signatureBytes = Utils.hexToBytes(signatureHex);
EdDSAEngine sgr = new EdDSAEngine(MessageDigest.getInstance(spec.getHashAlgorithm()));
sgr.initVerify(publicKey);
return sgr.verifyOneShot(messageBytes, signatureBytes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public TestTuple(String line) {
public static Collection<TestTuple> testCases = getTestData("test.data");

public static Collection<TestTuple> getTestData(String fileName) {
List<TestTuple> testCases = new ArrayList<TestTuple>();
List<TestTuple> testCases = new ArrayList<>();
BufferedReader file = null;
try {
InputStream is = Ed25519TestVectors.class.getResourceAsStream(fileName);
Expand Down
61 changes: 27 additions & 34 deletions src/test/java/net/i2p/crypto/eddsa/EdDSAEngineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
*/
package net.i2p.crypto.eddsa;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.lang.reflect.InvocationTargetException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.PrivateKey;
import java.security.PublicKey;
Expand All @@ -26,29 +28,25 @@
import net.i2p.crypto.eddsa.spec.EdDSAParameterSpec;
import net.i2p.crypto.eddsa.spec.EdDSAPrivateKeySpec;
import net.i2p.crypto.eddsa.spec.EdDSAPublicKeySpec;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.jupiter.api.Test;

// import sun.security.util.DerValue;
// import sun.security.x509.X509Key;

/**
* @author str4d
*
*/
public class EdDSAEngineTest {
class EdDSAEngineTest {
static final byte[] TEST_SEED =
Utils.hexToBytes("0000000000000000000000000000000000000000000000000000000000000000");
static final byte[] TEST_PK = Utils.hexToBytes("3b6a27bcceb6a42d62a3a8d02a6f0d73653215771de243a63ac048a18b59da29");
static final byte[] TEST_MSG = "This is a secret message".getBytes(Charset.forName("UTF-8"));
static final byte[] TEST_MSG = "This is a secret message".getBytes(StandardCharsets.UTF_8);
static final byte[] TEST_MSG_SIG = Utils.hexToBytes(
"94825896c7075c31bcb81f06dba2bdcd9dcf16e79288d4b9f87c248215c8468d475f429f3de3b4a2cf67fe17077ae19686020364d6d4fa7a0174bab4a123ba0f");

@Rule
public ExpectedException exception = ExpectedException.none();

@Test
public void testSign() throws Exception {
void testSign() throws Exception {
EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519);
// Signature sgr = Signature.getInstance("EdDSA", "I2P");
Signature sgr = new EdDSAEngine(MessageDigest.getInstance(spec.getHashAlgorithm()));
Expand All @@ -65,7 +63,7 @@ public void testSign() throws Exception {
}

@Test
public void testVerify() throws Exception {
void testVerify() throws Exception {
EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519);
// Signature sgr = Signature.getInstance("EdDSA", "I2P");
Signature sgr = new EdDSAEngine(MessageDigest.getInstance(spec.getHashAlgorithm()));
Expand All @@ -84,7 +82,8 @@ public void testVerify() throws Exception {
* Checks that a wrong-length signature throws an IAE.
*/
@Test
public void testVerifyWrongSigLength() throws Exception {
void testVerifyWrongSigLength() throws Exception {

EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519);
// Signature sgr = Signature.getInstance("EdDSA", "I2P");
Signature sgr = new EdDSAEngine(MessageDigest.getInstance(spec.getHashAlgorithm()));
Expand All @@ -93,14 +92,12 @@ public void testVerifyWrongSigLength() throws Exception {
sgr.initVerify(vKey);

sgr.update(TEST_MSG);

exception.expect(SignatureException.class);
exception.expectMessage("signature length is wrong");
sgr.verify(new byte[] {0});
SignatureException exception = assertThrows(SignatureException.class, () -> sgr.verify(new byte[] {0}));
assertTrue(exception.getMessage().contains("signature length is wrong"));
}

@Test
public void testSignResetsForReuse() throws Exception {
void testSignResetsForReuse() throws Exception {
EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519);
Signature sgr = new EdDSAEngine(MessageDigest.getInstance(spec.getHashAlgorithm()));
EdDSAPrivateKeySpec privKey = new EdDSAPrivateKeySpec(TEST_SEED, spec);
Expand All @@ -117,7 +114,7 @@ public void testSignResetsForReuse() throws Exception {
}

@Test
public void testVerifyResetsForReuse() throws Exception {
void testVerifyResetsForReuse() throws Exception {
EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519);
Signature sgr = new EdDSAEngine(MessageDigest.getInstance(spec.getHashAlgorithm()));
EdDSAPublicKeySpec pubKey = new EdDSAPublicKeySpec(TEST_PK, spec);
Expand All @@ -134,7 +131,7 @@ public void testVerifyResetsForReuse() throws Exception {
}

@Test
public void testSignOneShotMode() throws Exception {
void testSignOneShotMode() throws Exception {
EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519);
Signature sgr = new EdDSAEngine(MessageDigest.getInstance(spec.getHashAlgorithm()));
EdDSAPrivateKeySpec privKey = new EdDSAPrivateKeySpec(TEST_SEED, spec);
Expand All @@ -148,7 +145,7 @@ public void testSignOneShotMode() throws Exception {
}

@Test
public void testVerifyOneShotMode() throws Exception {
void testVerifyOneShotMode() throws Exception {
EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519);
Signature sgr = new EdDSAEngine(MessageDigest.getInstance(spec.getHashAlgorithm()));
EdDSAPublicKeySpec pubKey = new EdDSAPublicKeySpec(TEST_PK, spec);
Expand All @@ -162,7 +159,7 @@ public void testVerifyOneShotMode() throws Exception {
}

@Test
public void testSignOneShotModeMultipleUpdates() throws Exception {
void testSignOneShotModeMultipleUpdates() throws Exception {
EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519);
Signature sgr = new EdDSAEngine(MessageDigest.getInstance(spec.getHashAlgorithm()));
EdDSAPrivateKeySpec privKey = new EdDSAPrivateKeySpec(TEST_SEED, spec);
Expand All @@ -171,14 +168,12 @@ public void testSignOneShotModeMultipleUpdates() throws Exception {
sgr.setParameter(EdDSAEngine.ONE_SHOT_MODE);

sgr.update(TEST_MSG);

exception.expect(SignatureException.class);
exception.expectMessage("update() already called");
sgr.update(TEST_MSG);
SignatureException exception = assertThrows(SignatureException.class, () -> sgr.update(TEST_MSG));
assertTrue(exception.getMessage().contains("update() already called"));
}

@Test
public void testVerifyOneShotModeMultipleUpdates() throws Exception {
void testVerifyOneShotModeMultipleUpdates() throws Exception {
EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519);
EdDSAPublicKeySpec pubKey = new EdDSAPublicKeySpec(TEST_PK, spec);
Signature sgr = new EdDSAEngine(MessageDigest.getInstance(spec.getHashAlgorithm()));
Expand All @@ -187,14 +182,12 @@ public void testVerifyOneShotModeMultipleUpdates() throws Exception {
sgr.setParameter(EdDSAEngine.ONE_SHOT_MODE);

sgr.update(TEST_MSG);

exception.expect(SignatureException.class);
exception.expectMessage("update() already called");
sgr.update(TEST_MSG);
SignatureException exception = assertThrows(SignatureException.class, () -> sgr.update(TEST_MSG));
assertTrue(exception.getMessage().contains("update() already called"));
}

@Test
public void testSignOneShot() throws Exception {
void testSignOneShot() throws Exception {
EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519);
EdDSAPrivateKeySpec privKey = new EdDSAPrivateKeySpec(TEST_SEED, spec);
EdDSAEngine sgr = new EdDSAEngine(MessageDigest.getInstance(spec.getHashAlgorithm()));
Expand All @@ -205,7 +198,7 @@ public void testSignOneShot() throws Exception {
}

@Test
public void testVerifyOneShot() throws Exception {
void testVerifyOneShot() throws Exception {
EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519);
EdDSAPublicKeySpec pubKey = new EdDSAPublicKeySpec(TEST_PK, spec);
EdDSAEngine sgr = new EdDSAEngine(MessageDigest.getInstance(spec.getHashAlgorithm()));
Expand All @@ -216,7 +209,7 @@ public void testVerifyOneShot() throws Exception {
}

@Test
public void testVerifyX509PublicKeyInfo() throws Exception {
void testVerifyX509PublicKeyInfo() throws Exception {
EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName("Ed25519");
Signature sgr = new EdDSAEngine(MessageDigest.getInstance(spec.getHashAlgorithm()));
for (Ed25519TestVectors.TestTuple testCase : Ed25519TestVectors.testCases) {
Expand Down
12 changes: 6 additions & 6 deletions src/test/java/net/i2p/crypto/eddsa/EdDSAPrivateKeyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@
*/
package net.i2p.crypto.eddsa;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;

import java.security.spec.PKCS8EncodedKeySpec;
import net.i2p.crypto.eddsa.spec.EdDSAPrivateKeySpec;
import org.junit.Test;
import org.junit.jupiter.api.Test;

/**
* @author str4d
*
*/
public class EdDSAPrivateKeyTest {
class EdDSAPrivateKeyTest {
/**
* The example private key MC4CAQAwBQYDK2VwBCIEINTuctv5E1hK1bbY8fdp+K06/nwoy/HU++CXqI9EdVhC
* from https://tools.ietf.org/html/draft-ietf-curdle-pkix-04#section-10.3
Expand All @@ -36,7 +36,7 @@ public class EdDSAPrivateKeyTest {
"302f020100300806032b65640a01010420d4ee72dbf913584ad5b6d8f1f769f8ad3afe7c28cbf1d4fbe097a88f44755842");

@Test
public void testDecodeAndEncode() throws Exception {
void testDecodeAndEncode() throws Exception {
// Decode
PKCS8EncodedKeySpec encoded = new PKCS8EncodedKeySpec(TEST_PRIVKEY);
EdDSAPrivateKey keyIn = new EdDSAPrivateKey(encoded);
Expand All @@ -51,7 +51,7 @@ public void testDecodeAndEncode() throws Exception {
}

@Test
public void testDecodeWithNullAndEncode() throws Exception {
void testDecodeWithNullAndEncode() throws Exception {
// Decode
PKCS8EncodedKeySpec encoded = new PKCS8EncodedKeySpec(TEST_PRIVKEY_NULL_PARAMS);
EdDSAPrivateKey keyIn = new EdDSAPrivateKey(encoded);
Expand All @@ -66,7 +66,7 @@ public void testDecodeWithNullAndEncode() throws Exception {
}

@Test
public void testReEncodeOldEncoding() throws Exception {
void testReEncodeOldEncoding() throws Exception {
// Decode
PKCS8EncodedKeySpec encoded = new PKCS8EncodedKeySpec(TEST_PRIVKEY_OLD);
EdDSAPrivateKey keyIn = new EdDSAPrivateKey(encoded);
Expand Down
12 changes: 6 additions & 6 deletions src/test/java/net/i2p/crypto/eddsa/EdDSAPublicKeyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@
*/
package net.i2p.crypto.eddsa;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;

import java.security.spec.X509EncodedKeySpec;
import net.i2p.crypto.eddsa.spec.EdDSAPublicKeySpec;
import org.junit.Test;
import org.junit.jupiter.api.Test;

/**
* @author str4d
*
*/
public class EdDSAPublicKeyTest {
class EdDSAPublicKeyTest {
/**
* The example public key MCowBQYDK2VwAyEAGb9ECWmEzf6FQbrBZ9w7lshQhqowtrbLDFw4rXAxZuE=
* from https://tools.ietf.org/html/draft-ietf-curdle-pkix-04#section-10.1
Expand All @@ -36,7 +36,7 @@ public class EdDSAPublicKeyTest {
"302d300806032b65640a010103210019bf44096984cdfe8541bac167dc3b96c85086aa30b6b6cb0c5c38ad703166e1");

@Test
public void testDecodeAndEncode() throws Exception {
void testDecodeAndEncode() throws Exception {
// Decode
X509EncodedKeySpec encoded = new X509EncodedKeySpec(TEST_PUBKEY);
EdDSAPublicKey keyIn = new EdDSAPublicKey(encoded);
Expand All @@ -50,7 +50,7 @@ public void testDecodeAndEncode() throws Exception {
}

@Test
public void testDecodeWithNullAndEncode() throws Exception {
void testDecodeWithNullAndEncode() throws Exception {
// Decode
X509EncodedKeySpec encoded = new X509EncodedKeySpec(TEST_PUBKEY_NULL_PARAMS);
EdDSAPublicKey keyIn = new EdDSAPublicKey(encoded);
Expand All @@ -64,7 +64,7 @@ public void testDecodeWithNullAndEncode() throws Exception {
}

@Test
public void testReEncodeOldEncoding() throws Exception {
void testReEncodeOldEncoding() throws Exception {
// Decode
X509EncodedKeySpec encoded = new X509EncodedKeySpec(TEST_PUBKEY_OLD);
EdDSAPublicKey keyIn = new EdDSAPublicKey(encoded);
Expand Down
Loading
Loading