BouncyDSAKeyPair.java
/*******************************************************************************
* GordianKnot: Security Suite
* Copyright 2012,2025 Tony Washer
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
******************************************************************************/
package net.sourceforge.joceanus.gordianknot.impl.bc;
import net.sourceforge.joceanus.gordianknot.api.base.GordianException;
import net.sourceforge.joceanus.gordianknot.api.keypair.GordianDSAKeyType;
import net.sourceforge.joceanus.gordianknot.api.keypair.GordianKeyPair;
import net.sourceforge.joceanus.gordianknot.api.keypair.GordianKeyPairSpec;
import net.sourceforge.joceanus.gordianknot.api.sign.GordianSignatureSpec;
import net.sourceforge.joceanus.gordianknot.impl.bc.BouncyKeyPair.BouncyPrivateKey;
import net.sourceforge.joceanus.gordianknot.impl.bc.BouncyKeyPair.BouncyPublicKey;
import net.sourceforge.joceanus.gordianknot.impl.bc.BouncySignature.BouncyDERCoder;
import net.sourceforge.joceanus.gordianknot.impl.bc.BouncySignature.BouncyDigestSignature;
import net.sourceforge.joceanus.gordianknot.impl.core.exc.GordianCryptoException;
import net.sourceforge.joceanus.gordianknot.impl.core.keypair.GordianKeyPairValidity;
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.DSA;
import org.bouncycastle.crypto.digests.SHA256Digest;
import org.bouncycastle.crypto.generators.DSAKeyPairGenerator;
import org.bouncycastle.crypto.generators.DSAParametersGenerator;
import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
import org.bouncycastle.crypto.params.DSAKeyGenerationParameters;
import org.bouncycastle.crypto.params.DSAParameterGenerationParameters;
import org.bouncycastle.crypto.params.DSAPrivateKeyParameters;
import org.bouncycastle.crypto.params.DSAPublicKeyParameters;
import org.bouncycastle.crypto.params.ParametersWithRandom;
import org.bouncycastle.crypto.util.PrivateKeyFactory;
import org.bouncycastle.crypto.util.PrivateKeyInfoFactory;
import org.bouncycastle.crypto.util.PublicKeyFactory;
import org.bouncycastle.crypto.util.SubjectPublicKeyInfoFactory;
import java.io.IOException;
import java.math.BigInteger;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
/**
* DSA KeyPair classes.
*/
public final class BouncyDSAKeyPair {
/**
* Private constructor.
*/
private BouncyDSAKeyPair() {
}
/**
* Bouncy DSA PublicKey.
*/
public static class BouncyDSAPublicKey
extends BouncyPublicKey<DSAPublicKeyParameters> {
/**
* Constructor.
* @param pKeySpec the keySpec
* @param pPublicKey the public key
*/
BouncyDSAPublicKey(final GordianKeyPairSpec pKeySpec,
final DSAPublicKeyParameters pPublicKey) {
super(pKeySpec, pPublicKey);
}
@Override
protected boolean matchKey(final AsymmetricKeyParameter pThat) {
/* Access keys */
final DSAPublicKeyParameters myThis = getPublicKey();
final DSAPublicKeyParameters myThat = (DSAPublicKeyParameters) pThat;
/* Compare keys */
return compareKeys(myThis, myThat);
}
/**
* Is the private key valid for this public key?
* @param pPrivate the private key
* @return true/false
*/
public boolean validPrivate(final BouncyDSAPrivateKey pPrivate) {
final DSAPrivateKeyParameters myPrivate = pPrivate.getPrivateKey();
return getPublicKey().getParameters().equals(myPrivate.getParameters());
}
/**
* CompareKeys.
* @param pFirst the first key
* @param pSecond the second key
* @return true/false
*/
private static boolean compareKeys(final DSAPublicKeyParameters pFirst,
final DSAPublicKeyParameters pSecond) {
return pFirst.getY().equals(pSecond.getY())
&& pFirst.getParameters().equals(pSecond.getParameters());
}
}
/**
* Bouncy DSA PrivateKey.
*/
public static class BouncyDSAPrivateKey
extends BouncyPrivateKey<DSAPrivateKeyParameters> {
/**
* Constructor.
* @param pKeySpec the keySpec
* @param pPrivateKey the private key
*/
BouncyDSAPrivateKey(final GordianKeyPairSpec pKeySpec,
final DSAPrivateKeyParameters pPrivateKey) {
super(pKeySpec, pPrivateKey);
}
@Override
protected boolean matchKey(final AsymmetricKeyParameter pThat) {
/* Access keys */
final DSAPrivateKeyParameters myThis = getPrivateKey();
final DSAPrivateKeyParameters myThat = (DSAPrivateKeyParameters) pThat;
/* Compare keys */
return compareKeys(myThis, myThat);
}
/**
* CompareKeys.
* @param pFirst the first key
* @param pSecond the second key
* @return true/false
*/
private static boolean compareKeys(final DSAPrivateKeyParameters pFirst,
final DSAPrivateKeyParameters pSecond) {
return pFirst.getX().equals(pSecond.getX())
&& pFirst.getParameters().equals(pSecond.getParameters());
}
}
/**
* BouncyCastle DSA KeyPair generator.
*/
public static class BouncyDSAKeyPairGenerator
extends BouncyKeyPairGenerator {
/**
* Generator.
*/
private final DSAKeyPairGenerator theGenerator;
/**
* Constructor.
* @param pFactory the Security Factory
* @param pKeySpec the keySpec
*/
BouncyDSAKeyPairGenerator(final BouncyFactory pFactory,
final GordianKeyPairSpec pKeySpec) {
/* Initialise underlying class */
super(pFactory, pKeySpec);
/* Create the parameter generator */
final GordianDSAKeyType myKeyType = pKeySpec.getDSAKeyType();
final DSAParameterGenerationParameters myGenParms = new DSAParameterGenerationParameters(myKeyType.getKeySize(),
myKeyType.getHashSize(), PRIME_CERTAINTY, getRandom());
final DSAParametersGenerator myParmGenerator = new DSAParametersGenerator(new SHA256Digest());
myParmGenerator.init(myGenParms);
/* Create and initialise the generator */
theGenerator = new DSAKeyPairGenerator();
final DSAKeyGenerationParameters myParams = new DSAKeyGenerationParameters(getRandom(), myParmGenerator.generateParameters());
theGenerator.init(myParams);
}
@Override
public BouncyKeyPair generateKeyPair() {
/* Generate and return the keyPair */
final AsymmetricCipherKeyPair myPair = theGenerator.generateKeyPair();
final BouncyDSAPublicKey myPublic = new BouncyDSAPublicKey(getKeySpec(), (DSAPublicKeyParameters) myPair.getPublic());
final BouncyDSAPrivateKey myPrivate = new BouncyDSAPrivateKey(getKeySpec(), (DSAPrivateKeyParameters) myPair.getPrivate());
return new BouncyKeyPair(myPublic, myPrivate);
}
@Override
public PKCS8EncodedKeySpec getPKCS8Encoding(final GordianKeyPair pKeyPair) throws GordianException {
/* Protect against exceptions */
try {
/* Check the keyPair type and keySpecs */
BouncyKeyPair.checkKeyPair(pKeyPair, getKeySpec());
/* build and return the encoding */
final BouncyDSAPrivateKey myPrivateKey = (BouncyDSAPrivateKey) getPrivateKey(pKeyPair);
final DSAPrivateKeyParameters myParms = myPrivateKey.getPrivateKey();
final PrivateKeyInfo myInfo = PrivateKeyInfoFactory.createPrivateKeyInfo(myParms);
return new PKCS8EncodedKeySpec(myInfo.getEncoded());
} catch (IOException e) {
throw new GordianCryptoException(ERROR_PARSE, e);
}
}
@Override
public BouncyKeyPair deriveKeyPair(final X509EncodedKeySpec pPublicKey,
final PKCS8EncodedKeySpec pPrivateKey) throws GordianException {
/* Protect against exceptions */
try {
/* Check the keySpecs */
checkKeySpec(pPrivateKey);
/* derive keyPair */
final BouncyDSAPublicKey myPublic = derivePublicKey(pPublicKey);
final PrivateKeyInfo myInfo = PrivateKeyInfo.getInstance(pPrivateKey.getEncoded());
final DSAPrivateKeyParameters myParms = (DSAPrivateKeyParameters) PrivateKeyFactory.createKey(myInfo);
final BouncyDSAPrivateKey myPrivate = new BouncyDSAPrivateKey(getKeySpec(), myParms);
final BouncyKeyPair myPair = new BouncyKeyPair(myPublic, myPrivate);
/* Check that we have a matching pair */
GordianKeyPairValidity.checkValidity(getFactory(), myPair);
/* Return the keyPair */
return myPair;
} catch (IOException e) {
throw new GordianCryptoException(ERROR_PARSE, e);
}
}
@Override
public X509EncodedKeySpec getX509Encoding(final GordianKeyPair pKeyPair) throws GordianException {
/* Protect against exceptions */
try {
/* Check the keyPair type and keySpecs */
BouncyKeyPair.checkKeyPair(pKeyPair, getKeySpec());
/* build and return the encoding */
final BouncyDSAPublicKey myPublicKey = (BouncyDSAPublicKey) getPublicKey(pKeyPair);
final DSAPublicKeyParameters myParms = myPublicKey.getPublicKey();
final SubjectPublicKeyInfo myInfo = SubjectPublicKeyInfoFactory.createSubjectPublicKeyInfo(myParms);
return new X509EncodedKeySpec(myInfo.getEncoded());
} catch (IOException e) {
throw new GordianCryptoException(ERROR_PARSE, e);
}
}
@Override
public BouncyKeyPair derivePublicOnlyKeyPair(final X509EncodedKeySpec pEncodedKey) throws GordianException {
final BouncyDSAPublicKey myPublic = derivePublicKey(pEncodedKey);
return new BouncyKeyPair(myPublic);
}
/**
* Derive public key from encoded.
* @param pEncodedKey the encoded key
* @return the public key
* @throws GordianException on error
*/
private BouncyDSAPublicKey derivePublicKey(final X509EncodedKeySpec pEncodedKey) throws GordianException {
/* Protect against exceptions */
try {
/* Check the keySpecs */
checkKeySpec(pEncodedKey);
/* derive publicKey */
final SubjectPublicKeyInfo myInfo = SubjectPublicKeyInfo.getInstance(pEncodedKey.getEncoded());
final DSAPublicKeyParameters myParms = (DSAPublicKeyParameters) PublicKeyFactory.createKey(myInfo);
return new BouncyDSAPublicKey(getKeySpec(), myParms);
} catch (IOException e) {
throw new GordianCryptoException(ERROR_PARSE, e);
}
}
}
/**
* DSA signer.
*/
public static class BouncyDSASignature
extends BouncyDigestSignature {
/**
* The Signer.
*/
private final DSA theSigner;
/**
* The Coder.
*/
private final BouncyDERCoder theCoder;
/**
* Constructor.
* @param pFactory the factory
* @param pSpec the signatureSpec.
* @throws GordianException on error
*/
BouncyDSASignature(final BouncyFactory pFactory,
final GordianSignatureSpec pSpec) throws GordianException {
/* Initialise underlying class */
super(pFactory, pSpec);
/* Create the signer */
theSigner = BouncySignature.getDSASigner(pFactory, pSpec);
theCoder = new BouncyDERCoder();
}
@Override
public void initForSigning(final GordianKeyPair pKeyPair) throws GordianException {
/* Initialise detail */
BouncyKeyPair.checkKeyPair(pKeyPair);
super.initForSigning(pKeyPair);
/* Initialise and set the signer */
final BouncyDSAPrivateKey myPrivate = (BouncyDSAPrivateKey) getKeyPair().getPrivateKey();
final ParametersWithRandom myParms = new ParametersWithRandom(myPrivate.getPrivateKey(), getRandom());
theSigner.init(true, myParms);
}
@Override
public void initForVerify(final GordianKeyPair pKeyPair) throws GordianException {
/* Initialise detail */
BouncyKeyPair.checkKeyPair(pKeyPair);
super.initForVerify(pKeyPair);
/* Initialise and set the signer */
final BouncyDSAPublicKey myPublic = (BouncyDSAPublicKey) getKeyPair().getPublicKey();
theSigner.init(false, myPublic.getPublicKey());
}
@Override
public byte[] sign() throws GordianException {
/* Check that we are in signing mode */
checkMode(GordianSignatureMode.SIGN);
/* Sign the message */
final BigInteger[] myValues = theSigner.generateSignature(getDigest());
return theCoder.dsaEncode(myValues[0], myValues[1]);
}
@Override
public boolean verify(final byte[] pSignature) throws GordianException {
/* Check that we are in verify mode */
checkMode(GordianSignatureMode.VERIFY);
/* Verify the message */
final BigInteger[] myValues = theCoder.dsaDecode(pSignature);
return theSigner.verifySignature(getDigest(), myValues[0], myValues[1]);
}
}
}