Skip to content

Commit 94b91e1

Browse files
author
gefeili
committed
Add EdECObjectIdentifiers.id_X25519 to Bc/JcePublicKeyKeyEncryptionMethodGenerator. Remove providedKeyAlgorithm from PGPSignatureGenerator and PGPV3SignatureGenerator.
1 parent 2ff8410 commit 94b91e1

File tree

4 files changed

+28
-25
lines changed

4 files changed

+28
-25
lines changed

pg/src/main/java/org/bouncycastle/openpgp/PGPSignatureGenerator.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class PGPSignatureGenerator
2828
private SignatureSubpacket[] hashed = new SignatureSubpacket[0];
2929
private PGPContentSignerBuilder contentSignerBuilder;
3030
private PGPContentSigner contentSigner;
31-
private int providedKeyAlgorithm = -1;
31+
//private int providedKeyAlgorithm = -1;
3232

3333
/**
3434
* Create a signature generator built on the passed in contentSignerBuilder.
@@ -58,10 +58,10 @@ public void init(
5858
sigType = contentSigner.getType();
5959
lastb = 0;
6060

61-
if (providedKeyAlgorithm >= 0 && providedKeyAlgorithm != contentSigner.getKeyAlgorithm())
62-
{
63-
throw new PGPException("key algorithm mismatch");
64-
}
61+
// if (providedKeyAlgorithm >= 0 && providedKeyAlgorithm != contentSigner.getKeyAlgorithm())
62+
// {
63+
// throw new PGPException("key algorithm mismatch");
64+
// }
6565
}
6666

6767
public void setHashedSubpackets(
@@ -167,34 +167,35 @@ public PGPSignature generate()
167167
throw new PGPException("exception encoding hashed data.", e);
168168
}
169169

170-
171170
byte[] trailer = sOut.toByteArray();
172171

173172
blockUpdate(trailer, 0, trailer.length);
174-
175-
if (contentSigner.getKeyAlgorithm() == PublicKeyAlgorithmTags.RSA_SIGN
176-
|| contentSigner.getKeyAlgorithm() == PublicKeyAlgorithmTags.RSA_GENERAL) // an RSA signature
173+
switch (contentSigner.getKeyAlgorithm())
174+
{
175+
case PublicKeyAlgorithmTags.RSA_SIGN:
176+
case PublicKeyAlgorithmTags.RSA_GENERAL:
177177
{
178178
sigValues = new MPInteger[1];
179179
sigValues[0] = new MPInteger(new BigInteger(1, contentSigner.getSignature()));
180+
break;
180181
}
181-
else if (contentSigner.getKeyAlgorithm() == PublicKeyAlgorithmTags.EDDSA_LEGACY)
182+
case PublicKeyAlgorithmTags.EDDSA_LEGACY:
182183
{
183184
byte[] enc = contentSigner.getSignature();
184185
sigValues = new MPInteger[]{
185186
new MPInteger(new BigInteger(1, Arrays.copyOfRange(enc, 0, enc.length / 2))),
186187
new MPInteger(new BigInteger(1, Arrays.copyOfRange(enc, enc.length / 2, enc.length)))
187188
};
189+
break;
188190
}
189-
else if (contentSigner.getKeyAlgorithm() == PublicKeyAlgorithmTags.Ed25519 ||
190-
contentSigner.getKeyAlgorithm() == PublicKeyAlgorithmTags.Ed448)
191-
{
191+
case PublicKeyAlgorithmTags.Ed25519:
192+
case PublicKeyAlgorithmTags.Ed448:
192193
// Contrary to EDDSA_LEGACY, the new PK algorithms Ed25519, Ed448 do not use MPI encoding
193194
sigValues = null;
194-
}
195-
else
196-
{
195+
break;
196+
default:
197197
sigValues = PGPUtil.dsaSigToMpi(contentSigner.getSignature());
198+
break;
198199
}
199200

200201
byte[] digest = contentSigner.getDigest();
@@ -206,13 +207,13 @@ else if (contentSigner.getKeyAlgorithm() == PublicKeyAlgorithmTags.Ed25519 ||
206207
if (sigValues != null)
207208
{
208209
return new PGPSignature(new SignaturePacket(sigType, contentSigner.getKeyID(), contentSigner.getKeyAlgorithm(),
209-
contentSigner.getHashAlgorithm(), hPkts, unhPkts, fingerPrint, sigValues));
210+
contentSigner.getHashAlgorithm(), hPkts, unhPkts, fingerPrint, sigValues));
210211
}
211212
else
212213
{
213214
// Ed25519, Ed448 use raw encoding instead of MPI
214215
return new PGPSignature(new SignaturePacket(4, sigType, contentSigner.getKeyID(), contentSigner.getKeyAlgorithm(),
215-
contentSigner.getHashAlgorithm(), hPkts, unhPkts, fingerPrint, contentSigner.getSignature()));
216+
contentSigner.getHashAlgorithm(), hPkts, unhPkts, fingerPrint, contentSigner.getSignature()));
216217
}
217218
}
218219

pg/src/main/java/org/bouncycastle/openpgp/PGPV3SignatureGenerator.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class PGPV3SignatureGenerator
1919
{
2020
private PGPContentSignerBuilder contentSignerBuilder;
2121
private PGPContentSigner contentSigner;
22-
private int providedKeyAlgorithm = -1;
22+
// private int providedKeyAlgorithm = -1;
2323

2424
/**
2525
* Create a signature generator built on the passed in contentSignerBuilder.
@@ -49,10 +49,10 @@ public void init(
4949
sigType = contentSigner.getType();
5050
lastb = 0;
5151

52-
if (providedKeyAlgorithm >= 0 && providedKeyAlgorithm != contentSigner.getKeyAlgorithm())
53-
{
54-
throw new PGPException("key algorithm mismatch");
55-
}
52+
// if (providedKeyAlgorithm >= 0 && providedKeyAlgorithm != contentSigner.getKeyAlgorithm())
53+
// {
54+
// throw new PGPException("key algorithm mismatch");
55+
// }
5656
}
5757

5858
/**

pg/src/main/java/org/bouncycastle/openpgp/operator/bc/BcPublicKeyKeyEncryptionMethodGenerator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.bouncycastle.crypto.params.X25519PublicKeyParameters;
3333
import org.bouncycastle.crypto.params.X448KeyGenerationParameters;
3434
import org.bouncycastle.crypto.params.X448PublicKeyParameters;
35+
import org.bouncycastle.internal.asn1.edec.EdECObjectIdentifiers;
3536
import org.bouncycastle.openpgp.PGPException;
3637
import org.bouncycastle.openpgp.PGPPublicKey;
3738
import org.bouncycastle.openpgp.operator.PGPPad;
@@ -85,7 +86,7 @@ protected byte[] encryptSessionInfo(PGPPublicKey pubKey, byte[] sessionInfo)
8586
{
8687
ECDHPublicBCPGKey ecPubKey = (ECDHPublicBCPGKey)pubKeyPacket.getKey();
8788
byte[] userKeyingMaterial = RFC6637Utils.createUserKeyingMaterial(pubKeyPacket, new BcKeyFingerprintCalculator());
88-
if (ecPubKey.getCurveOID().equals(CryptlibObjectIdentifiers.curvey25519))
89+
if (ecPubKey.getCurveOID().equals(CryptlibObjectIdentifiers.curvey25519) || ecPubKey.getCurveOID().equals(EdECObjectIdentifiers.id_X25519))
8990
{
9091
AsymmetricCipherKeyPair ephKp = getAsymmetricCipherKeyPair(new X25519KeyPairGenerator(), new X25519KeyGenerationParameters(random));
9192

pg/src/main/java/org/bouncycastle/openpgp/operator/jcajce/JcePublicKeyKeyEncryptionMethodGenerator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.bouncycastle.bcpg.PublicKeyAlgorithmTags;
2626
import org.bouncycastle.bcpg.PublicKeyPacket;
2727
import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags;
28+
import org.bouncycastle.internal.asn1.edec.EdECObjectIdentifiers;
2829
import org.bouncycastle.jcajce.spec.HybridValueParameterSpec;
2930
import org.bouncycastle.jcajce.spec.UserKeyingMaterialSpec;
3031
import org.bouncycastle.jcajce.util.DefaultJcaJceHelper;
@@ -100,7 +101,7 @@ protected byte[] encryptSessionInfo(PGPPublicKey pubKey, byte[] sessionInfo)
100101
ECDHPublicBCPGKey ecKey = (ECDHPublicBCPGKey)pubKey.getPublicKeyPacket().getKey();
101102
String keyEncryptionOID = RFC6637Utils.getKeyEncryptionOID(ecKey.getSymmetricKeyAlgorithm()).getId();
102103
PublicKeyPacket pubKeyPacket = pubKey.getPublicKeyPacket();
103-
if (ecKey.getCurveOID().equals(CryptlibObjectIdentifiers.curvey25519))
104+
if (ecKey.getCurveOID().equals(CryptlibObjectIdentifiers.curvey25519) || ecKey.getCurveOID().equals(EdECObjectIdentifiers.id_X25519))
104105
{
105106
return getEncryptSessionInfo(pubKeyPacket, "X25519", cryptoPublicKey, keyEncryptionOID,
106107
ecKey.getSymmetricKeyAlgorithm(), sessionInfo, RFC6637Utils.getXDHAlgorithm(pubKeyPacket),

0 commit comments

Comments
 (0)