Skip to content

Commit

Permalink
Test getBitStrength() for Ed448/Ed25519/X448/X25519 keys
Browse files Browse the repository at this point in the history
  • Loading branch information
vanitasvitae committed Sep 10, 2024
1 parent a660daa commit bdebcde
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ public void performTest()
testV4SigningVerificationWithBcKey();

testConversionOfTestVectorKey();
testBitStrength();
}

private void testBitStrength()
throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, PGPException
{
Date date = currentTimeRounded();
KeyPairGenerator gen = KeyPairGenerator.getInstance("EDDSA", new BouncyCastleProvider());
gen.initialize(new EdDSAParameterSpec("Ed25519"));
KeyPair kp = gen.generateKeyPair();
JcaPGPKeyPair k = new JcaPGPKeyPair(PublicKeyPacket.VERSION_6, PublicKeyAlgorithmTags.Ed25519, kp, date);
isEquals("Ed25519 key size mismatch", 256, k.getPublicKey().getBitStrength());
}

private void testConversionOfJcaKeyPair()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ public void performTest()
testConversionOfBcKeyPair();
testV4SigningVerificationWithJcaKey();
testV4SigningVerificationWithBcKey();

testBitStrength();
}

private void testBitStrength()
throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, PGPException
{
Date date = currentTimeRounded();
KeyPairGenerator gen = KeyPairGenerator.getInstance("EDDSA", new BouncyCastleProvider());
gen.initialize(new EdDSAParameterSpec("Ed448"));
KeyPair kp = gen.generateKeyPair();
JcaPGPKeyPair k = new JcaPGPKeyPair(PublicKeyPacket.VERSION_6, PublicKeyAlgorithmTags.Ed448, kp, date);
isEquals("Ed448 key size mismatch", 456, k.getPublicKey().getBitStrength());
}

private void testConversionOfJcaKeyPair()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ public void performTest()
testConversionOfBcKeyPair();
testV4MessageEncryptionDecryptionWithJcaKey();
testV4MessageEncryptionDecryptionWithBcKey();

testBitStrength();
}

private void testBitStrength()
throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, PGPException
{
Date date = currentTimeRounded();
KeyPairGenerator gen = KeyPairGenerator.getInstance("XDH", new BouncyCastleProvider());
gen.initialize(new XDHParameterSpec("X25519"));
KeyPair kp = gen.generateKeyPair();
JcaPGPKeyPair k = new JcaPGPKeyPair(PublicKeyPacket.VERSION_6, PublicKeyAlgorithmTags.X25519, kp, date);
isEquals("X25519 key size mismatch", 256, k.getPublicKey().getBitStrength());
}

private void testConversionOfJcaKeyPair()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.generators.X448KeyPairGenerator;
import org.bouncycastle.crypto.params.X448KeyGenerationParameters;
import org.bouncycastle.jcajce.spec.EdDSAParameterSpec;
import org.bouncycastle.jcajce.spec.XDHParameterSpec;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.openpgp.*;
Expand Down Expand Up @@ -49,6 +50,19 @@ public void performTest()
testConversionOfBcKeyPair();
testV4MessageEncryptionDecryptionWithJcaKey();
testV4MessageEncryptionDecryptionWithBcKey();

testBitStrength();
}

private void testBitStrength()
throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, PGPException
{
Date date = currentTimeRounded();
KeyPairGenerator gen = KeyPairGenerator.getInstance("XDH", new BouncyCastleProvider());
gen.initialize(new XDHParameterSpec("X448"));
KeyPair kp = gen.generateKeyPair();
JcaPGPKeyPair k = new JcaPGPKeyPair(PublicKeyPacket.VERSION_6, PublicKeyAlgorithmTags.X448, kp, date);
isEquals("X448 key size mismatch", 448, k.getPublicKey().getBitStrength());
}

private void testConversionOfJcaKeyPair()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.bouncycastle.bcpg.EdSecretBCPGKey;
import org.bouncycastle.bcpg.HashAlgorithmTags;
import org.bouncycastle.bcpg.PublicKeyAlgorithmTags;
import org.bouncycastle.bcpg.PublicKeyPacket;
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.generators.Ed25519KeyPairGenerator;
import org.bouncycastle.crypto.params.Ed25519KeyGenerationParameters;
Expand Down Expand Up @@ -44,6 +45,19 @@ public void performTest()
testConversionOfBcKeyPair();
testV4SigningVerificationWithJcaKey();
testV4SigningVerificationWithBcKey();

testBitStrength();
}

private void testBitStrength()
throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, PGPException
{
Date date = currentTimeRounded();
KeyPairGenerator gen = KeyPairGenerator.getInstance("EDDSA", new BouncyCastleProvider());
gen.initialize(new EdDSAParameterSpec("Ed25519"));
KeyPair kp = gen.generateKeyPair();
JcaPGPKeyPair k = new JcaPGPKeyPair(PublicKeyPacket.VERSION_6, PublicKeyAlgorithmTags.EDDSA_LEGACY, kp, date);
isEquals("Ed25519 key size mismatch", 256, k.getPublicKey().getBitStrength());
}

private void testV4SigningVerificationWithJcaKey()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.bouncycastle.bcpg.EdSecretBCPGKey;
import org.bouncycastle.bcpg.HashAlgorithmTags;
import org.bouncycastle.bcpg.PublicKeyAlgorithmTags;
import org.bouncycastle.bcpg.PublicKeyPacket;
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.generators.Ed448KeyPairGenerator;
import org.bouncycastle.crypto.params.Ed448KeyGenerationParameters;
Expand Down Expand Up @@ -48,6 +49,19 @@ public void performTest()
testConversionOfBcKeyPair();
testV4SigningVerificationWithJcaKey();
testV4SigningVerificationWithBcKey();

testBitStrength();
}

private void testBitStrength()
throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, PGPException
{
Date date = currentTimeRounded();
KeyPairGenerator gen = KeyPairGenerator.getInstance("EDDSA", new BouncyCastleProvider());
gen.initialize(new EdDSAParameterSpec("Ed448"));
KeyPair kp = gen.generateKeyPair();
JcaPGPKeyPair k = new JcaPGPKeyPair(PublicKeyPacket.VERSION_6, PublicKeyAlgorithmTags.EDDSA_LEGACY, kp, date);
isEquals("Ed448 key size mismatch", 456, k.getPublicKey().getBitStrength());
}

private void testV4SigningVerificationWithJcaKey()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.bouncycastle.bcpg.ECDHPublicBCPGKey;
import org.bouncycastle.bcpg.ECSecretBCPGKey;
import org.bouncycastle.bcpg.PublicKeyAlgorithmTags;
import org.bouncycastle.bcpg.PublicKeyPacket;
import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags;
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.generators.X25519KeyPairGenerator;
Expand Down Expand Up @@ -48,6 +49,19 @@ public void performTest()
testConversionOfBcKeyPair();
testV4MessageEncryptionDecryptionWithJcaKey();
testV4MessageEncryptionDecryptionWithBcKey();

testBitStrength();
}

private void testBitStrength()
throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, PGPException
{
Date date = currentTimeRounded();
KeyPairGenerator gen = KeyPairGenerator.getInstance("XDH", new BouncyCastleProvider());
gen.initialize(new XDHParameterSpec("X25519"));
KeyPair kp = gen.generateKeyPair();
JcaPGPKeyPair k = new JcaPGPKeyPair(PublicKeyPacket.VERSION_6, PublicKeyAlgorithmTags.ECDH, kp, date);
isEquals("X25519 key size mismatch", 256, k.getPublicKey().getBitStrength());
}

private void testConversionOfJcaKeyPair()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ public void performTest()
{
testConversionOfJcaKeyPair();
testConversionOfBcKeyPair();

testBitStrength();
}

private void testBitStrength()
throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, PGPException
{
Date date = currentTimeRounded();
KeyPairGenerator gen = KeyPairGenerator.getInstance("XDH", new BouncyCastleProvider());
gen.initialize(new XDHParameterSpec("X448"));
KeyPair kp = gen.generateKeyPair();
JcaPGPKeyPair k = new JcaPGPKeyPair(PublicKeyPacket.VERSION_6, PublicKeyAlgorithmTags.ECDH, kp, date);
isEquals("X448 key size mismatch", 448, k.getPublicKey().getBitStrength());
}

private void testConversionOfJcaKeyPair()
Expand Down

0 comments on commit bdebcde

Please sign in to comment.