org.bouncycastle.crypto.test.X25519Test Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bcprov-ext-debug-jdk15on Show documentation
Show all versions of bcprov-ext-debug-jdk15on Show documentation
The Bouncy Castle Crypto package is a Java implementation of cryptographic algorithms. This jar contains JCE provider and lightweight API for the Bouncy Castle Cryptography APIs for JDK 1.5 to JDK 1.8. Note: this package includes the NTRU encryption algorithms.
package org.bouncycastle.crypto.test;
import java.security.SecureRandom;
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.AsymmetricCipherKeyPairGenerator;
import org.bouncycastle.crypto.agreement.X25519Agreement;
import org.bouncycastle.crypto.generators.X25519KeyPairGenerator;
import org.bouncycastle.crypto.params.X25519KeyGenerationParameters;
import org.bouncycastle.util.test.SimpleTest;
public class X25519Test
extends SimpleTest
{
private static final SecureRandom RANDOM = new SecureRandom();
public String getName()
{
return "X25519";
}
public static void main(String[] args)
{
runTest(new X25519Test());
}
public void performTest()
{
for (int i = 0; i < 10; ++i)
{
testAgreement();
}
}
private void testAgreement()
{
AsymmetricCipherKeyPairGenerator kpGen = new X25519KeyPairGenerator();
kpGen.init(new X25519KeyGenerationParameters(RANDOM));
AsymmetricCipherKeyPair kpA = kpGen.generateKeyPair();
AsymmetricCipherKeyPair kpB = kpGen.generateKeyPair();
X25519Agreement agreeA = new X25519Agreement();
agreeA.init(kpA.getPrivate());
byte[] secretA = new byte[agreeA.getAgreementSize()];
agreeA.calculateAgreement(kpB.getPublic(), secretA, 0);
X25519Agreement agreeB = new X25519Agreement();
agreeB.init(kpB.getPrivate());
byte[] secretB = new byte[agreeB.getAgreementSize()];
agreeB.calculateAgreement(kpA.getPublic(), secretB, 0);
if (!areEqual(secretA, secretB))
{
fail("X25519 agreement failed");
}
}
}