package org.bouncycastle.pqc.crypto.qtesla;
import java.security.SecureRandom;
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.AsymmetricCipherKeyPairGenerator;
import org.bouncycastle.crypto.KeyGenerationParameters;
public final class QTESLAKeyPairGenerator
implements AsymmetricCipherKeyPairGenerator
{
private int securityCategory;
private SecureRandom secureRandom;
public void init(
KeyGenerationParameters param)
{
QTESLAKeyGenerationParameters parameters = (QTESLAKeyGenerationParameters)param;
this.secureRandom = parameters.getRandom();
this.securityCategory = parameters.getSecurityCategory();
}
public AsymmetricCipherKeyPair generateKeyPair()
{
byte[] privateKey = allocatePrivate(securityCategory);
byte[] publicKey = allocatePublic(securityCategory);
switch (securityCategory)
{
case QTESLASecurityCategory.HEURISTIC_I:
QTESLA.generateKeyPairI(publicKey, privateKey, secureRandom);
break;
case QTESLASecurityCategory.HEURISTIC_III_SIZE:
QTESLA.generateKeyPairIIISize(publicKey, privateKey, secureRandom);
break;
case QTESLASecurityCategory.HEURISTIC_III_SPEED:
QTESLA.generateKeyPairIIISpeed(publicKey, privateKey, secureRandom);
break;
case QTESLASecurityCategory.PROVABLY_SECURE_I:
QTESLA.generateKeyPairIP(publicKey, privateKey, secureRandom);
break;
case QTESLASecurityCategory.PROVABLY_SECURE_III:
QTESLA.generateKeyPairIIIP(publicKey, privateKey, secureRandom);
break;
default:
throw new IllegalArgumentException("unknown security category: " + securityCategory);
}
return new AsymmetricCipherKeyPair(new QTESLAPublicKeyParameters(securityCategory, publicKey), new QTESLAPrivateKeyParameters(securityCategory, privateKey));
}
private byte[] allocatePrivate(int securityCategory)
{
return new byte[QTESLASecurityCategory.getPrivateSize(securityCategory)];
}
private byte[] allocatePublic(int securityCategory)
{
return new byte[QTESLASecurityCategory.getPublicSize(securityCategory)];
}
}