Skip to content

Commit d6cff7a

Browse files
committed
Added explicit test for signing key-pair generation.
1 parent 324d9b1 commit d6cff7a

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jni: compile
3131
javah -jni -classpath build -d jni org.peergos.crypto.JniTweetNacl
3232
gcc -Wimplicit-function-declaration -fPIC -std=c11 -I${JAVA_HOME}/include -I${JAVA_HOME}/include/linux -Inative -Ijni -shared -o libtweetnacl.so jni/org_peergos_crypto_JniTweetNacl.c
3333

34-
.PHONY: jni_test
34+
.PHONY: jni_tests
3535
jni_tests: def
3636
java -Djava.library.path=. -cp "Test.jar:lib/*" test.TestRunner
3737

src/test/NativeTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,33 @@ private static byte[] copy(byte[] of) {
2323
return Arrays.copyOf(of, of.length);
2424
}
2525

26+
private static void cryptoSignKeyPairTest(int nRound) {
27+
for (int iRound = 0; iRound < nRound; iRound++) {
28+
byte[] javaPublicKey = new byte[32];
29+
byte[] javaSecretKey = new byte[64];
30+
byte[] cPublicKey = new byte[32];
31+
byte[] cSecretKey = new byte[64];
32+
33+
boolean isSeeded = true;
34+
prng.nextBytes(javaSecretKey);
35+
System.arraycopy(javaSecretKey, 0, cSecretKey, 0, javaSecretKey.length);
36+
37+
TweetNaCl.crypto_sign_keypair(javaPublicKey, javaSecretKey, isSeeded);
38+
39+
int jniRc = JniTweetNacl.crypto_sign_keypair(cPublicKey, cSecretKey);
40+
if (jniRc != 0)
41+
throw new IllegalStateException("non-zero jni return code " + jniRc);
42+
43+
if (!Arrays.equals(javaSecretKey, cSecretKey))
44+
throw new IllegalStateException("Java secret key != C secret key!");
45+
46+
if (!Arrays.equals(javaPublicKey, cPublicKey))
47+
throw new IllegalStateException("JNI unsign != original!");
48+
49+
assertTrue("crypto sign-keypair round " + iRound, Arrays.equals(javaSecretKey, cSecretKey));
50+
}
51+
}
52+
2653
private static void cryptoSignOpenTest(int nRound, int messageLength) {
2754
byte[] publicKey = new byte[32];
2855
byte[] secretKey = new byte[64];
@@ -183,6 +210,7 @@ private static void cryptoBoxOpenTest(int nRound, int messageLength) {
183210
int size = (int) Math.pow(2, exp);
184211
size += prng.nextInt(size);
185212

213+
cryptoSignKeyPairTest(N_ROUND);
186214
cryptoBoxTest(N_ROUND, size);
187215
cryptoBoxOpenTest(N_ROUND, size);
188216
cryptoSignTest(N_ROUND, size);

0 commit comments

Comments
 (0)