aboutsummaryrefslogtreecommitdiffstats
path: root/libraries/spongycastle/core/src/test/java/org/spongycastle/pqc/crypto/test/NTRUSignatureKeyTest.java
diff options
context:
space:
mode:
Diffstat (limited to 'libraries/spongycastle/core/src/test/java/org/spongycastle/pqc/crypto/test/NTRUSignatureKeyTest.java')
-rw-r--r--libraries/spongycastle/core/src/test/java/org/spongycastle/pqc/crypto/test/NTRUSignatureKeyTest.java58
1 files changed, 58 insertions, 0 deletions
diff --git a/libraries/spongycastle/core/src/test/java/org/spongycastle/pqc/crypto/test/NTRUSignatureKeyTest.java b/libraries/spongycastle/core/src/test/java/org/spongycastle/pqc/crypto/test/NTRUSignatureKeyTest.java
new file mode 100644
index 000000000..f741532ce
--- /dev/null
+++ b/libraries/spongycastle/core/src/test/java/org/spongycastle/pqc/crypto/test/NTRUSignatureKeyTest.java
@@ -0,0 +1,58 @@
+package org.spongycastle.pqc.crypto.test;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+
+import junit.framework.TestCase;
+import org.spongycastle.crypto.AsymmetricCipherKeyPair;
+import org.spongycastle.pqc.crypto.ntru.NTRUSigner;
+import org.spongycastle.pqc.crypto.ntru.NTRUSigningKeyGenerationParameters;
+import org.spongycastle.pqc.crypto.ntru.NTRUSigningKeyPairGenerator;
+import org.spongycastle.pqc.crypto.ntru.NTRUSigningPrivateKeyParameters;
+import org.spongycastle.pqc.crypto.ntru.NTRUSigningPublicKeyParameters;
+
+public class NTRUSignatureKeyTest
+ extends TestCase
+{
+ public void testEncode()
+ throws IOException
+ {
+ for (NTRUSigningKeyGenerationParameters params : new NTRUSigningKeyGenerationParameters[]{NTRUSigningKeyGenerationParameters.TEST157, NTRUSigningKeyGenerationParameters.TEST157_PROD})
+ {
+ testEncode(params);
+ }
+ }
+
+ private void testEncode(NTRUSigningKeyGenerationParameters params)
+ throws IOException
+ {
+ NTRUSigner ntru = new NTRUSigner(params.getSigningParameters());
+ NTRUSigningKeyPairGenerator kGen = new NTRUSigningKeyPairGenerator();
+
+ kGen.init(params);
+
+ AsymmetricCipherKeyPair kp = kGen.generateKeyPair();
+
+ NTRUSigningPrivateKeyParameters kPriv = (NTRUSigningPrivateKeyParameters)kp.getPrivate();
+ NTRUSigningPublicKeyParameters kPub = (NTRUSigningPublicKeyParameters)kp.getPublic();
+
+ // encode to byte[] and reconstruct
+ byte[] priv = kPriv.getEncoded();
+ byte[] pub = kPub.getEncoded();
+ AsymmetricCipherKeyPair kp2 = new AsymmetricCipherKeyPair(new NTRUSigningPublicKeyParameters(pub, params.getSigningParameters()), new NTRUSigningPrivateKeyParameters(priv, params));
+ assertEquals(kPub, kp2.getPublic());
+ assertEquals(kPriv, kp2.getPrivate());
+
+ // encode to OutputStream and reconstruct
+ ByteArrayOutputStream bos1 = new ByteArrayOutputStream();
+ ByteArrayOutputStream bos2 = new ByteArrayOutputStream();
+ kPriv.writeTo(bos1);
+ kPub.writeTo(bos2);
+ ByteArrayInputStream bis1 = new ByteArrayInputStream(bos1.toByteArray());
+ ByteArrayInputStream bis2 = new ByteArrayInputStream(bos2.toByteArray());
+ AsymmetricCipherKeyPair kp3 = new AsymmetricCipherKeyPair(new NTRUSigningPublicKeyParameters(bis2, params.getSigningParameters()), new NTRUSigningPrivateKeyParameters(bis1, params));
+ assertEquals(kPub, kp3.getPublic());
+ assertEquals(kPriv, kp3.getPrivate());
+ }
+} \ No newline at end of file