blob: 1b152bffd561247ce9082b47f8fdd4df6a6bf7a8 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
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.NTRUEncryptionKeyPairGenerator;
import org.spongycastle.pqc.crypto.ntru.NTRUEncryptionKeyGenerationParameters;
import org.spongycastle.pqc.crypto.ntru.NTRUEncryptionPrivateKeyParameters;
import org.spongycastle.pqc.crypto.ntru.NTRUEncryptionPublicKeyParameters;
public class EncryptionKeyTest
extends TestCase
{
public void testEncode()
throws IOException
{
for (NTRUEncryptionKeyGenerationParameters params : new NTRUEncryptionKeyGenerationParameters[]{NTRUEncryptionKeyGenerationParameters.APR2011_743, NTRUEncryptionKeyGenerationParameters.APR2011_743_FAST, NTRUEncryptionKeyGenerationParameters.EES1499EP1})
{
testEncode(params);
}
}
private void testEncode(NTRUEncryptionKeyGenerationParameters params)
throws IOException
{
NTRUEncryptionKeyPairGenerator kpGen = new NTRUEncryptionKeyPairGenerator();
kpGen.init(params);
AsymmetricCipherKeyPair kp = kpGen.generateKeyPair();
byte[] priv = ((NTRUEncryptionPrivateKeyParameters)kp.getPrivate()).getEncoded();
byte[] pub = ((NTRUEncryptionPublicKeyParameters)kp.getPublic()).getEncoded();
AsymmetricCipherKeyPair kp2 = new AsymmetricCipherKeyPair(new NTRUEncryptionPublicKeyParameters(pub, params.getEncryptionParameters()), new NTRUEncryptionPrivateKeyParameters(priv, params.getEncryptionParameters()));
assertEquals(kp.getPublic(), kp2.getPublic());
assertEquals(kp.getPrivate(), kp2.getPrivate());
ByteArrayOutputStream bos1 = new ByteArrayOutputStream();
ByteArrayOutputStream bos2 = new ByteArrayOutputStream();
((NTRUEncryptionPrivateKeyParameters)kp.getPrivate()).writeTo(bos1);
((NTRUEncryptionPublicKeyParameters)kp.getPublic()).writeTo(bos2);
ByteArrayInputStream bis1 = new ByteArrayInputStream(bos1.toByteArray());
ByteArrayInputStream bis2 = new ByteArrayInputStream(bos2.toByteArray());
AsymmetricCipherKeyPair kp3 = new AsymmetricCipherKeyPair(new NTRUEncryptionPublicKeyParameters(bis2, params.getEncryptionParameters()), new NTRUEncryptionPrivateKeyParameters(bis1, params.getEncryptionParameters()));
assertEquals(kp.getPublic(), kp3.getPublic());
assertEquals(kp.getPrivate(), kp3.getPrivate());
}
}
|