forked from cryptography-cafe/ed25519-elisabeth
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request cryptography-cafe#4 from cryptography-cafe/validation
Validation improvements
- Loading branch information
Showing
5 changed files
with
310 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/test/java/cafe/cryptography/ed25519/Ed25519PrivateKeyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* This file is part of ed25519-elisabeth. | ||
* Copyright (c) 2019 Jack Grigg | ||
* See LICENSE for licensing information. | ||
*/ | ||
|
||
package cafe.cryptography.ed25519; | ||
|
||
import org.junit.Test; | ||
|
||
public class Ed25519PrivateKeyTest { | ||
@Test | ||
public void fromByteArrayAcceptsAllBitsSet() { | ||
// An Ed25519 private key is only ever used as an input to a hash | ||
// function, not as a scalar, so all 32-byte strings are valid. | ||
Ed25519PrivateKey | ||
.fromByteArray(Utils.hexToBytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void fromByteArrayRejectsShortInput() { | ||
Ed25519PrivateKey.fromByteArray(Utils.hexToBytes("00")); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void fromByteArrayRejectsLongInput() { | ||
Ed25519PrivateKey | ||
.fromByteArray(Utils.hexToBytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00")); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/test/java/cafe/cryptography/ed25519/Ed25519SignatureTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* This file is part of ed25519-elisabeth. | ||
* Copyright (c) 2019 Jack Grigg | ||
* See LICENSE for licensing information. | ||
*/ | ||
|
||
package cafe.cryptography.ed25519; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.hamcrest.Matchers.is; | ||
import static org.junit.Assert.assertThat; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
public class Ed25519SignatureTest { | ||
@Test | ||
public void fromByteArrayAcceptsInvalidR() { | ||
// Validation of R happens during signature verification | ||
Ed25519Signature.fromByteArray(Utils.hexToBytes( | ||
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0000000000000000000000000000000000000000000000000000000000000000")); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void fromByteArrayRejectsShortInput() { | ||
Ed25519Signature.fromByteArray(Utils.hexToBytes("00")); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void fromByteArrayRejectsLongInput() { | ||
Ed25519Signature.fromByteArray(Utils.hexToBytes( | ||
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0000000000000000000000000000000000000000000000000000000000000000ff")); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void fromByteArrayRejectsNonCanonicalS() { | ||
Ed25519Signature.fromByteArray(Utils.hexToBytes( | ||
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")); | ||
} | ||
} |
103 changes: 103 additions & 0 deletions
103
src/test/java/cafe/cryptography/ed25519/Ed25519TestVectors.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* This file is part of ed25519-elisabeth. | ||
* Copyright (c) 2019 Jack Grigg | ||
* See LICENSE for licensing information. | ||
*/ | ||
|
||
package cafe.cryptography.ed25519; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
import cafe.cryptography.curve25519.InvalidEncodingException; | ||
import org.junit.Test; | ||
|
||
import static org.hamcrest.Matchers.is; | ||
import static org.junit.Assert.assertThat; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
/** | ||
* Test against the medium test vectors set. | ||
* | ||
* TESTVECTORS is taken from ed25519-dalek, which in turn obtained it from | ||
* sign.input.gz in agl's ed25519 Golang package. It is a selection of test | ||
* cases from https://ed25519.cr.yp.to/python/sign.input | ||
*/ | ||
public class Ed25519TestVectors { | ||
public static class TestTuple { | ||
public static int numCases; | ||
public int caseNum; | ||
public byte[] sk; | ||
public byte[] vk; | ||
public byte[] message; | ||
public byte[] signature; | ||
|
||
public TestTuple(String line) { | ||
this.caseNum = ++numCases; | ||
String[] x = line.split(":"); | ||
this.sk = Utils.hexToBytes(x[0].substring(0, 64)); | ||
this.vk = Utils.hexToBytes(x[1]); | ||
this.message = Utils.hexToBytes(x[2]); | ||
this.signature = Utils.hexToBytes(x[3].substring(0, 128)); | ||
} | ||
} | ||
|
||
public static Collection<TestTuple> testCases = getTestData("TESTVECTORS"); | ||
|
||
public static Collection<TestTuple> getTestData(String fileName) { | ||
List<TestTuple> testCases = new ArrayList<TestTuple>(); | ||
BufferedReader file = null; | ||
try { | ||
InputStream is = Ed25519TestVectors.class.getClassLoader().getResourceAsStream(fileName); | ||
if (is == null) { | ||
throw new IOException("Resource not found: " + fileName); | ||
} | ||
file = new BufferedReader(new InputStreamReader(is)); | ||
String line; | ||
while ((line = file.readLine()) != null) { | ||
testCases.add(new TestTuple(line)); | ||
} | ||
} catch (IOException e) { | ||
throw new ExceptionInInitializerError(e); | ||
} finally { | ||
if (file != null) { | ||
try { | ||
file.close(); | ||
} catch (IOException e) { | ||
} | ||
} | ||
} | ||
return testCases; | ||
} | ||
|
||
@Test | ||
public void derivePublic() { | ||
for (TestTuple testCase : testCases) { | ||
Ed25519PrivateKey sk = Ed25519PrivateKey.fromByteArray(testCase.sk); | ||
assertThat("Test case " + testCase.caseNum + " failed", sk.derivePublic().toByteArray(), is(testCase.vk)); | ||
} | ||
} | ||
|
||
@Test | ||
public void testSign() { | ||
for (TestTuple testCase : testCases) { | ||
Ed25519PrivateKey sk = Ed25519PrivateKey.fromByteArray(testCase.sk); | ||
assertThat("Test case " + testCase.caseNum + " failed", | ||
sk.expand().sign(testCase.message, sk.derivePublic()).toByteArray(), is(testCase.signature)); | ||
} | ||
} | ||
|
||
@Test | ||
public void testVerify() throws InvalidEncodingException { | ||
for (TestTuple testCase : testCases) { | ||
Ed25519PublicKey vk = Ed25519PublicKey.fromByteArray(testCase.vk); | ||
Ed25519Signature sig = Ed25519Signature.fromByteArray(testCase.signature); | ||
assertTrue("Test case " + testCase.caseNum + " failed", vk.verify(testCase.message, sig)); | ||
} | ||
} | ||
} |
Oops, something went wrong.