org.spongycastle.pqc.jcajce.provider.mceliece.McEliecePKCSCipherSpi Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of prov Show documentation
Show all versions of prov Show documentation
Spongy Castle is a package-rename (org.bouncycastle.* to org.spongycastle.*) of Bouncy Castle
intended for the Android platform. Android unfortunately ships with a stripped-down version of
Bouncy Castle, which prevents easy upgrades - Spongy Castle overcomes this and provides a full,
up-to-date version of the Bouncy Castle cryptographic libs.
The newest version!
package org.spongycastle.pqc.jcajce.provider.mceliece;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import org.spongycastle.asn1.pkcs.PKCSObjectIdentifiers;
import org.spongycastle.asn1.x509.X509ObjectIdentifiers;
import org.spongycastle.crypto.CipherParameters;
import org.spongycastle.crypto.params.ParametersWithRandom;
import org.spongycastle.pqc.crypto.mceliece.McElieceCipher;
import org.spongycastle.pqc.crypto.mceliece.McElieceKeyParameters;
import org.spongycastle.pqc.jcajce.provider.util.AsymmetricBlockCipher;
public class McEliecePKCSCipherSpi
extends AsymmetricBlockCipher
implements PKCSObjectIdentifiers, X509ObjectIdentifiers
{
private McElieceCipher cipher;
public McEliecePKCSCipherSpi(McElieceCipher cipher)
{
this.cipher = cipher;
}
protected void initCipherEncrypt(Key key, AlgorithmParameterSpec params,
SecureRandom sr)
throws InvalidKeyException,
InvalidAlgorithmParameterException
{
CipherParameters param;
param = McElieceKeysToParams.generatePublicKeyParameter((PublicKey)key);
param = new ParametersWithRandom(param, sr);
cipher.init(true, param);
this.maxPlainTextSize = cipher.maxPlainTextSize;
this.cipherTextSize = cipher.cipherTextSize;
}
protected void initCipherDecrypt(Key key, AlgorithmParameterSpec params)
throws InvalidKeyException, InvalidAlgorithmParameterException
{
CipherParameters param;
param = McElieceKeysToParams.generatePrivateKeyParameter((PrivateKey)key);
cipher.init(false, param);
this.maxPlainTextSize = cipher.maxPlainTextSize;
this.cipherTextSize = cipher.cipherTextSize;
}
protected byte[] messageEncrypt(byte[] input)
throws IllegalBlockSizeException, BadPaddingException
{
byte[] output = null;
try
{
output = cipher.messageEncrypt(input);
}
catch (Exception e)
{
e.printStackTrace();
}
return output;
}
protected byte[] messageDecrypt(byte[] input)
throws IllegalBlockSizeException, BadPaddingException
{
byte[] output = null;
try
{
output = cipher.messageDecrypt(input);
}
catch (Exception e)
{
e.printStackTrace();
}
return output;
}
public String getName()
{
return "McEliecePKCS";
}
public int getKeySize(Key key)
throws InvalidKeyException
{
McElieceKeyParameters mcElieceKeyParameters;
if (key instanceof PublicKey)
{
mcElieceKeyParameters = (McElieceKeyParameters)McElieceKeysToParams.generatePublicKeyParameter((PublicKey)key);
}
else
{
mcElieceKeyParameters = (McElieceKeyParameters)McElieceKeysToParams.generatePrivateKeyParameter((PrivateKey)key);
}
return cipher.getKeySize(mcElieceKeyParameters);
}
static public class McEliecePKCS
extends McEliecePKCSCipherSpi
{
public McEliecePKCS()
{
super(new McElieceCipher());
}
}
}