BouncyBIKEKeyPair.java
/*
* GordianKnot: Security Suite
* Copyright 2012-2026. 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 io.github.tonywasher.joceanus.gordianknot.impl.bc;
import io.github.tonywasher.joceanus.gordianknot.api.agree.GordianAgreementSpec;
import io.github.tonywasher.joceanus.gordianknot.api.base.GordianException;
import io.github.tonywasher.joceanus.gordianknot.api.keypair.GordianKeyPair;
import io.github.tonywasher.joceanus.gordianknot.api.keypair.GordianKeyPairSpec;
import io.github.tonywasher.joceanus.gordianknot.impl.bc.BouncyKeyPair.BouncyPrivateKey;
import io.github.tonywasher.joceanus.gordianknot.impl.bc.BouncyKeyPair.BouncyPublicKey;
import io.github.tonywasher.joceanus.gordianknot.impl.core.agree.GordianCoreAgreementFactory;
import io.github.tonywasher.joceanus.gordianknot.impl.core.base.GordianBaseFactory;
import io.github.tonywasher.joceanus.gordianknot.impl.core.exc.GordianCryptoException;
import io.github.tonywasher.joceanus.gordianknot.impl.core.exc.GordianIOException;
import io.github.tonywasher.joceanus.gordianknot.impl.core.keypair.GordianKeyPairValidity;
import org.bouncycastle.asn1.ASN1Encoding;
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.SecretWithEncapsulation;
import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
import org.bouncycastle.pqc.crypto.bike.BIKEKEMExtractor;
import org.bouncycastle.pqc.crypto.bike.BIKEKEMGenerator;
import org.bouncycastle.pqc.crypto.bike.BIKEKeyGenerationParameters;
import org.bouncycastle.pqc.crypto.bike.BIKEKeyPairGenerator;
import org.bouncycastle.pqc.crypto.bike.BIKEParameters;
import org.bouncycastle.pqc.crypto.bike.BIKEPrivateKeyParameters;
import org.bouncycastle.pqc.crypto.bike.BIKEPublicKeyParameters;
import org.bouncycastle.pqc.crypto.util.PrivateKeyFactory;
import org.bouncycastle.pqc.crypto.util.PrivateKeyInfoFactory;
import org.bouncycastle.pqc.crypto.util.PublicKeyFactory;
import org.bouncycastle.pqc.crypto.util.SubjectPublicKeyInfoFactory;
import javax.security.auth.DestroyFailedException;
import java.io.IOException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Arrays;
/**
* BIKE KeyPair classes.
*/
public final class BouncyBIKEKeyPair {
/**
* Private constructor.
*/
private BouncyBIKEKeyPair() {
}
/**
* Bouncy BIKE PublicKey.
*/
public static class BouncyBIKEPublicKey
extends BouncyPublicKey<BIKEPublicKeyParameters> {
/**
* Constructor.
*
* @param pKeySpec the keySpec
* @param pPublicKey the public key
*/
BouncyBIKEPublicKey(final GordianKeyPairSpec pKeySpec,
final BIKEPublicKeyParameters pPublicKey) {
super(pKeySpec, pPublicKey);
}
@Override
protected boolean matchKey(final AsymmetricKeyParameter pThat) {
/* Access keys */
final BIKEPublicKeyParameters myThis = getPublicKey();
final BIKEPublicKeyParameters myThat = (BIKEPublicKeyParameters) pThat;
/* Compare keys */
return Arrays.equals(myThis.getEncoded(), myThat.getEncoded());
}
}
/**
* Bouncy BIKE PrivateKey.
*/
public static class BouncyBIKEPrivateKey
extends BouncyPrivateKey<BIKEPrivateKeyParameters> {
/**
* Constructor.
*
* @param pKeySpec the keySpec
* @param pPrivateKey the private key
*/
BouncyBIKEPrivateKey(final GordianKeyPairSpec pKeySpec,
final BIKEPrivateKeyParameters pPrivateKey) {
super(pKeySpec, pPrivateKey);
}
@Override
protected boolean matchKey(final AsymmetricKeyParameter pThat) {
/* Access keys */
final BIKEPrivateKeyParameters myThis = getPrivateKey();
final BIKEPrivateKeyParameters myThat = (BIKEPrivateKeyParameters) pThat;
/* Compare keys */
return Arrays.equals(myThis.getEncoded(), myThat.getEncoded());
}
}
/**
* BouncyCastle BIKE KeyPair generator.
*/
public static class BouncyBIKEKeyPairGenerator
extends BouncyKeyPairGenerator {
/**
* Generator.
*/
private final BIKEKeyPairGenerator theGenerator;
/**
* Constructor.
*
* @param pFactory the Security Factory
* @param pKeySpec the keySpec
* @throws GordianException on error
*/
BouncyBIKEKeyPairGenerator(final GordianBaseFactory pFactory,
final GordianKeyPairSpec pKeySpec) throws GordianException {
/* Initialise underlying class */
super(pFactory, pKeySpec);
/* Create the generator */
theGenerator = new BIKEKeyPairGenerator();
/* Determine the parameters */
final BIKEParameters myParms = pKeySpec.getBIKEKeySpec().getParameters();
/* Initialise the generator */
final BIKEKeyGenerationParameters myParams = new BIKEKeyGenerationParameters(getRandom(), myParms);
theGenerator.init(myParams);
}
@Override
public BouncyKeyPair generateKeyPair() {
/* Generate and return the keyPair */
final AsymmetricCipherKeyPair myPair = theGenerator.generateKeyPair();
final BouncyBIKEPublicKey myPublic = new BouncyBIKEPublicKey(getKeySpec(), (BIKEPublicKeyParameters) myPair.getPublic());
final BouncyBIKEPrivateKey myPrivate = new BouncyBIKEPrivateKey(getKeySpec(), (BIKEPrivateKeyParameters) 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 BouncyBIKEPrivateKey myPrivateKey = (BouncyBIKEPrivateKey) getPrivateKey(pKeyPair);
final BIKEPrivateKeyParameters 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 BouncyBIKEPublicKey myPublic = derivePublicKey(pPublicKey);
final PrivateKeyInfo myInfo = PrivateKeyInfo.getInstance(pPrivateKey.getEncoded());
final BIKEPrivateKeyParameters myParms = (BIKEPrivateKeyParameters) PrivateKeyFactory.createKey(myInfo);
final BouncyBIKEPrivateKey myPrivate = new BouncyBIKEPrivateKey(getKeySpec(), myParms);
final BouncyKeyPair myPair = new BouncyKeyPair(myPublic, myPrivate);
/* Check that we have a matching pair */
GordianKeyPairValidity.checkValidity(getFactory(), myPair);
/* Return the derived 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 BouncyBIKEPublicKey myPublicKey = (BouncyBIKEPublicKey) getPublicKey(pKeyPair);
final BIKEPublicKeyParameters myParms = myPublicKey.getPublicKey();
final SubjectPublicKeyInfo myInfo = SubjectPublicKeyInfoFactory.createSubjectPublicKeyInfo(myParms);
final byte[] myBytes = myInfo.getEncoded(ASN1Encoding.DER);
return new X509EncodedKeySpec(myBytes);
} catch (IOException e) {
throw new GordianCryptoException(ERROR_PARSE, e);
}
}
@Override
public BouncyKeyPair derivePublicOnlyKeyPair(final X509EncodedKeySpec pEncodedKey) throws GordianException {
final BouncyBIKEPublicKey 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 BouncyBIKEPublicKey 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 BIKEPublicKeyParameters myParms = (BIKEPublicKeyParameters) PublicKeyFactory.createKey(myInfo);
return new BouncyBIKEPublicKey(getKeySpec(), myParms);
} catch (IOException e) {
throw new GordianCryptoException(ERROR_PARSE, e);
}
}
}
/**
* BIKE Agreement Engine.
*/
public static class BouncyBIKEAgreementEngine
extends BouncyAgreementBase {
/**
* Constructor.
*
* @param pFactory the security factory
* @param pSpec the agreementSpec
* @throws GordianException on error
*/
BouncyBIKEAgreementEngine(final GordianCoreAgreementFactory pFactory,
final GordianAgreementSpec pSpec) throws GordianException {
/* Initialize underlying class */
super(pFactory, pSpec);
}
@Override
public void buildClientHello() throws GordianException {
/* Protect against exceptions */
try {
/* Create encapsulation */
final BouncyBIKEPublicKey myPublic = (BouncyBIKEPublicKey) getPublicKey(getServerKeyPair());
final BIKEKEMGenerator myGenerator = new BIKEKEMGenerator(getRandom());
final SecretWithEncapsulation myResult = myGenerator.generateEncapsulated(myPublic.getPublicKey());
/* Store the encapsulation */
setEncapsulated(myResult.getEncapsulation());
/* Store secret */
storeSecret(myResult.getSecret());
myResult.destroy();
} catch (DestroyFailedException e) {
throw new GordianIOException("Failed to destroy secret", e);
}
}
@Override
public void processClientHello() throws GordianException {
/* Create extractor */
final BouncyBIKEPrivateKey myPrivate = (BouncyBIKEPrivateKey) getPrivateKey(getServerKeyPair());
final BIKEKEMExtractor myExtractor = new BIKEKEMExtractor(myPrivate.getPrivateKey());
/* Parse encapsulated message and store secret */
final byte[] myMessage = getEncapsulated();
storeSecret(myExtractor.extractSecret(myMessage));
}
}
}