BouncySNTRUPrimeKeyPair.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.agree.GordianAgreementSpec;
import net.sourceforge.joceanus.gordianknot.api.base.GordianException;
import net.sourceforge.joceanus.gordianknot.api.keypair.GordianKeyPair;
import net.sourceforge.joceanus.gordianknot.api.keypair.GordianKeyPairSpec;
import net.sourceforge.joceanus.gordianknot.impl.bc.BouncyKeyPair.BouncyPrivateKey;
import net.sourceforge.joceanus.gordianknot.impl.bc.BouncyKeyPair.BouncyPublicKey;
import net.sourceforge.joceanus.gordianknot.impl.core.agree.GordianAgreementMessageASN1;
import net.sourceforge.joceanus.gordianknot.impl.core.agree.GordianCoreAnonymousAgreement;
import net.sourceforge.joceanus.gordianknot.impl.core.exc.GordianCryptoException;
import net.sourceforge.joceanus.gordianknot.impl.core.exc.GordianIOException;
import net.sourceforge.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.ntruprime.SNTRUPrimeKEMExtractor;
import org.bouncycastle.pqc.crypto.ntruprime.SNTRUPrimeKEMGenerator;
import org.bouncycastle.pqc.crypto.ntruprime.SNTRUPrimeKeyGenerationParameters;
import org.bouncycastle.pqc.crypto.ntruprime.SNTRUPrimeKeyPairGenerator;
import org.bouncycastle.pqc.crypto.ntruprime.SNTRUPrimeParameters;
import org.bouncycastle.pqc.crypto.ntruprime.SNTRUPrimePrivateKeyParameters;
import org.bouncycastle.pqc.crypto.ntruprime.SNTRUPrimePublicKeyParameters;
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;
/**
* SNTRUPrime KeyPair classes.
*/
public final class BouncySNTRUPrimeKeyPair {
/**
* Private constructor.
*/
private BouncySNTRUPrimeKeyPair() {
}
/**
* Bouncy SNTRUPrime PublicKey.
*/
public static class BouncySNTRUPrimePublicKey
extends BouncyPublicKey<SNTRUPrimePublicKeyParameters> {
/**
* Constructor.
* @param pKeySpec the keySpec
* @param pPublicKey the public key
*/
BouncySNTRUPrimePublicKey(final GordianKeyPairSpec pKeySpec,
final SNTRUPrimePublicKeyParameters pPublicKey) {
super(pKeySpec, pPublicKey);
}
@Override
protected boolean matchKey(final AsymmetricKeyParameter pThat) {
/* Access keys */
final SNTRUPrimePublicKeyParameters myThis = getPublicKey();
final SNTRUPrimePublicKeyParameters myThat = (SNTRUPrimePublicKeyParameters) pThat;
/* Compare keys */
return Arrays.equals(myThis.getEncoded(), myThat.getEncoded());
}
}
/**
* Bouncy SNTRUPrime PrivateKey.
*/
public static class BouncySNTRUPrimePrivateKey
extends BouncyPrivateKey<SNTRUPrimePrivateKeyParameters> {
/**
* Constructor.
* @param pKeySpec the keySpec
* @param pPrivateKey the private key
*/
BouncySNTRUPrimePrivateKey(final GordianKeyPairSpec pKeySpec,
final SNTRUPrimePrivateKeyParameters pPrivateKey) {
super(pKeySpec, pPrivateKey);
}
@Override
protected boolean matchKey(final AsymmetricKeyParameter pThat) {
/* Access keys */
final SNTRUPrimePrivateKeyParameters myThis = getPrivateKey();
final SNTRUPrimePrivateKeyParameters myThat = (SNTRUPrimePrivateKeyParameters) pThat;
/* Compare keys */
return Arrays.equals(myThis.getEncoded(), myThat.getEncoded());
}
}
/**
* BouncyCastle SNTRUPrime KeyPair generator.
*/
public static class BouncySNTRUPrimeKeyPairGenerator
extends BouncyKeyPairGenerator {
/**
* Generator.
*/
private final SNTRUPrimeKeyPairGenerator theGenerator;
/**
* Constructor.
* @param pFactory the Security Factory
* @param pKeySpec the keySpec
* @throws GordianException on error
*/
BouncySNTRUPrimeKeyPairGenerator(final BouncyFactory pFactory,
final GordianKeyPairSpec pKeySpec) throws GordianException {
/* Initialise underlying class */
super(pFactory, pKeySpec);
/* Create the generator */
theGenerator = new SNTRUPrimeKeyPairGenerator();
/* Determine the parameters */
final SNTRUPrimeParameters myParms = pKeySpec.getNTRUPrimeKeySpec().getParams().getSNTRUParameters();
/* Initialise the generator */
final SNTRUPrimeKeyGenerationParameters myParams = new SNTRUPrimeKeyGenerationParameters(getRandom(), myParms);
theGenerator.init(myParams);
}
@Override
public BouncyKeyPair generateKeyPair() {
/* Generate and return the keyPair */
final AsymmetricCipherKeyPair myPair = theGenerator.generateKeyPair();
final BouncySNTRUPrimePublicKey myPublic = new BouncySNTRUPrimePublicKey(getKeySpec(), (SNTRUPrimePublicKeyParameters) myPair.getPublic());
final BouncySNTRUPrimePrivateKey myPrivate = new BouncySNTRUPrimePrivateKey(getKeySpec(), (SNTRUPrimePrivateKeyParameters) 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 BouncySNTRUPrimePrivateKey myPrivateKey = (BouncySNTRUPrimePrivateKey) getPrivateKey(pKeyPair);
final SNTRUPrimePrivateKeyParameters 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 BouncySNTRUPrimePublicKey myPublic = derivePublicKey(pPublicKey);
final PrivateKeyInfo myInfo = PrivateKeyInfo.getInstance(pPrivateKey.getEncoded());
final SNTRUPrimePrivateKeyParameters myParms = (SNTRUPrimePrivateKeyParameters) PrivateKeyFactory.createKey(myInfo);
final BouncySNTRUPrimePrivateKey myPrivate = new BouncySNTRUPrimePrivateKey(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 BouncySNTRUPrimePublicKey myPublicKey = (BouncySNTRUPrimePublicKey) getPublicKey(pKeyPair);
final SNTRUPrimePublicKeyParameters 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 BouncySNTRUPrimePublicKey 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 BouncySNTRUPrimePublicKey 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 SNTRUPrimePublicKeyParameters myParms = (SNTRUPrimePublicKeyParameters) PublicKeyFactory.createKey(myInfo);
return new BouncySNTRUPrimePublicKey(getKeySpec(), myParms);
} catch (IOException e) {
throw new GordianCryptoException(ERROR_PARSE, e);
}
}
}
/**
* SNTRUPrime Agreement.
*/
public static class BouncySNTRUPrimeAgreement
extends GordianCoreAnonymousAgreement {
/**
* The generator.
*/
private final SNTRUPrimeKEMGenerator theGenerator;
/**
* Constructor.
* @param pFactory the security factory
* @param pSpec the agreementSpec
*/
BouncySNTRUPrimeAgreement(final BouncyFactory pFactory,
final GordianAgreementSpec pSpec) {
/* Initialise underlying class */
super(pFactory, pSpec);
/* Create Agreement */
theGenerator = new SNTRUPrimeKEMGenerator(getRandom());
}
@Override
public GordianAgreementMessageASN1 createClientHelloASN1(final GordianKeyPair pServer) throws GordianException {
/* Protect against exceptions */
try {
/* Check keyPair */
BouncyKeyPair.checkKeyPair(pServer);
checkKeyPair(pServer);
/* Create encapsulation */
final BouncySNTRUPrimePublicKey myPublic = (BouncySNTRUPrimePublicKey) getPublicKey(pServer);
final SecretWithEncapsulation myResult = theGenerator.generateEncapsulated(myPublic.getPublicKey());
/* Build the clientHello Message */
final GordianAgreementMessageASN1 myClientHello = buildClientHelloASN1(myResult.getEncapsulation());
/* Store secret and create initVector */
storeSecret(myResult.getSecret());
myResult.destroy();
/* Return the message */
return myClientHello;
} catch (DestroyFailedException e) {
throw new GordianIOException("Failed to destroy secret", e);
}
}
@Override
public void acceptClientHelloASN1(final GordianKeyPair pServer,
final GordianAgreementMessageASN1 pClientHello) throws GordianException {
/* Check keyPair */
BouncyKeyPair.checkKeyPair(pServer);
checkKeyPair(pServer);
/* Initialise Key Encapsulation */
final BouncySNTRUPrimePrivateKey myPrivate = (BouncySNTRUPrimePrivateKey) getPrivateKey(pServer);
final SNTRUPrimeKEMExtractor myExtractor = new SNTRUPrimeKEMExtractor(myPrivate.getPrivateKey());
/* Parse clientHello message and store secret */
final byte[] myMessage = pClientHello.getEncapsulated();
storeSecret(myExtractor.extractSecret(myMessage));
}
}
}