Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ed448 anomaly #1691

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ else if (ecdhK.getCurveOID().equals(EdECObjectIdentifiers.id_X448))
BCPGKey key = publicPk.getKey();
if (key instanceof Ed25519PublicBCPGKey)
{
return implGetPublicKeyX509(BigIntegers.asUnsignedByteArray(new BigInteger(1, publicPk.getKey().getEncoded())),
return implGetPublicKeyX509(publicPk.getKey().getEncoded(),
0, EdECObjectIdentifiers.id_Ed25519, "EdDSA");
}
else
Expand All @@ -382,7 +382,7 @@ else if (ecdhK.getCurveOID().equals(EdECObjectIdentifiers.id_X448))
// Modern Ed448 (1.3.101.113)
case PublicKeyAlgorithmTags.Ed448:
{
return implGetPublicKeyX509(BigIntegers.asUnsignedByteArray(new BigInteger(1, publicPk.getKey().getEncoded())),
return implGetPublicKeyX509(publicPk.getKey().getEncoded(),
0, EdECObjectIdentifiers.id_Ed448, "EdDSA");
}
case PublicKeyAlgorithmTags.ELGAMAL_ENCRYPT:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.bouncycastle.openpgp.test;

import org.bouncycastle.bcpg.PublicKeyAlgorithmTags;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPKeyPair;
import org.bouncycastle.openpgp.operator.jcajce.JcaPGPKeyConverter;
import org.bouncycastle.openpgp.operator.jcajce.JcaPGPKeyPair;
import org.bouncycastle.util.test.SimpleTest;

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PublicKey;
import java.security.Security;
import java.util.Date;

public class Ed448AnomalyReproducer
extends SimpleTest
{
@Override
public String getName() {
return "Ed448AnomalyReproducer";
}

@Override
public void performTest()
throws Exception
{
JcaPGPKeyConverter jcaPGPKeyConverter = new JcaPGPKeyConverter().setProvider(new BouncyCastleProvider());
int failure = 0;
for (int total = 0; total < 2000; total++)
{
Date creationDate = new Date();
KeyPairGenerator gen = KeyPairGenerator.getInstance("Ed448", "BC");
KeyPair keyPair = gen.generateKeyPair();
PGPKeyPair jcaPgpPair = new JcaPGPKeyPair(PublicKeyAlgorithmTags.Ed448, keyPair, creationDate);

try
{
PublicKey pubKey = jcaPGPKeyConverter.getPublicKey(jcaPgpPair.getPublicKey()); // fails in ~1/200 of times
}
catch (PGPException e)
{
failure++;
System.out.println(failure + "/" + total + " (" + (((double)(failure)) / total) * 100 + "%)");
}
}
}

public static void main(String[] args)
{
Security.addProvider(new BouncyCastleProvider());
runTest(new Ed448AnomalyReproducer());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.bouncycastle.jcajce.interfaces.EdDSAPublicKey;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.util.Properties;
import org.bouncycastle.util.encoders.Hex;

public class BCEdDSAPublicKey
implements EdDSAPublicKey
Expand Down Expand Up @@ -54,7 +55,9 @@ else if ((rawData.length - prefixLength) == Ed25519PublicKeyParameters.KEY_SIZE)
}
else
{
throw new InvalidKeySpecException("raw key data not recognised");
throw new InvalidKeySpecException("raw key data not recognised (prefix mismatch:\n" +
"Expected: " + Hex.toHexString(prefix) + "\n" +
"Got: " + Hex.toHexString(java.util.Arrays.copyOf(rawData, prefixLength)) + ")");
}
}

Expand Down