org.bouncycastle.pqc.jcajce.provider.hqc.HQCKeyGeneratorSpi Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com.liferay.saml.opensaml.integration Show documentation
Show all versions of com.liferay.saml.opensaml.integration Show documentation
Liferay SAML OpenSAML Integration
package org.bouncycastle.pqc.jcajce.provider.hqc;
import java.security.InvalidAlgorithmParameterException;
import java.security.SecureRandom;
import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.KeyGeneratorSpi;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.security.auth.DestroyFailedException;
import org.bouncycastle.crypto.SecretWithEncapsulation;
import org.bouncycastle.jcajce.SecretKeyWithEncapsulation;
import org.bouncycastle.jcajce.spec.KEMExtractSpec;
import org.bouncycastle.jcajce.spec.KEMGenerateSpec;
import org.bouncycastle.pqc.crypto.hqc.HQCKEMExtractor;
import org.bouncycastle.pqc.crypto.hqc.HQCKEMGenerator;
import org.bouncycastle.util.Arrays;
public class HQCKeyGeneratorSpi
extends KeyGeneratorSpi
{
private KEMGenerateSpec genSpec;
private SecureRandom random;
private KEMExtractSpec extSpec;
protected void engineInit(SecureRandom secureRandom)
{
throw new UnsupportedOperationException("Operation not supported");
}
protected void engineInit(AlgorithmParameterSpec algorithmParameterSpec, SecureRandom secureRandom)
throws InvalidAlgorithmParameterException
{
this.random = secureRandom;
if (algorithmParameterSpec instanceof KEMGenerateSpec)
{
this.genSpec = (KEMGenerateSpec)algorithmParameterSpec;
this.extSpec = null;
}
else if (algorithmParameterSpec instanceof KEMExtractSpec)
{
this.genSpec = null;
this.extSpec = (KEMExtractSpec)algorithmParameterSpec;
}
else
{
throw new InvalidAlgorithmParameterException("unknown spec");
}
}
protected void engineInit(int i, SecureRandom secureRandom)
{
throw new UnsupportedOperationException("Operation not supported");
}
protected SecretKey engineGenerateKey()
{
if (genSpec != null)
{
BCHQCPublicKey pubKey = (BCHQCPublicKey)genSpec.getPublicKey();
HQCKEMGenerator kemGen = new HQCKEMGenerator(random);
SecretWithEncapsulation secEnc = kemGen.generateEncapsulated(pubKey.getKeyParams());
SecretKey rv = new SecretKeyWithEncapsulation(new SecretKeySpec(secEnc.getSecret(), genSpec.getKeyAlgorithmName()), secEnc.getEncapsulation());
try
{
secEnc.destroy();
}
catch (DestroyFailedException e)
{
throw new IllegalStateException("key cleanup failed");
}
return rv;
}
else
{
BCHQCPrivateKey privKey = (BCHQCPrivateKey)extSpec.getPrivateKey();
HQCKEMExtractor kemExt = new HQCKEMExtractor(privKey.getKeyParams());
byte[] encapsulation = extSpec.getEncapsulation();
byte[] secret = kemExt.extractSecret(encapsulation);
SecretKey rv = new SecretKeyWithEncapsulation(new SecretKeySpec(secret, extSpec.getKeyAlgorithmName()), encapsulation);
Arrays.clear(secret);
return rv;
}
}
}