org.spongycastle.jcajce.provider.symmetric.SM4 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.jcajce.provider.symmetric;
import java.security.AlgorithmParameters;
import java.security.InvalidAlgorithmParameterException;
import java.security.SecureRandom;
import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.spec.IvParameterSpec;
import org.spongycastle.crypto.BlockCipher;
import org.spongycastle.crypto.CipherKeyGenerator;
import org.spongycastle.crypto.engines.SM4Engine;
import org.spongycastle.crypto.generators.Poly1305KeyGenerator;
import org.spongycastle.crypto.macs.CMac;
import org.spongycastle.crypto.macs.GMac;
import org.spongycastle.crypto.modes.GCMBlockCipher;
import org.spongycastle.jcajce.provider.config.ConfigurableProvider;
import org.spongycastle.jcajce.provider.symmetric.util.BaseAlgorithmParameterGenerator;
import org.spongycastle.jcajce.provider.symmetric.util.BaseBlockCipher;
import org.spongycastle.jcajce.provider.symmetric.util.BaseKeyGenerator;
import org.spongycastle.jcajce.provider.symmetric.util.BaseMac;
import org.spongycastle.jcajce.provider.symmetric.util.BlockCipherProvider;
import org.spongycastle.jcajce.provider.symmetric.util.IvAlgorithmParameters;
public final class SM4
{
private SM4()
{
}
public static class ECB
extends BaseBlockCipher
{
public ECB()
{
super(new BlockCipherProvider()
{
public BlockCipher get()
{
return new SM4Engine();
}
});
}
}
public static class KeyGen
extends BaseKeyGenerator
{
public KeyGen()
{
super("SM4", 128, new CipherKeyGenerator());
}
}
public static class CMAC
extends BaseMac
{
public CMAC()
{
super(new CMac(new SM4Engine()));
}
}
public static class GMAC
extends BaseMac
{
public GMAC()
{
super(new GMac(new GCMBlockCipher(new SM4Engine())));
}
}
public static class Poly1305
extends BaseMac
{
public Poly1305()
{
super(new org.spongycastle.crypto.macs.Poly1305(new SM4Engine()));
}
}
public static class Poly1305KeyGen
extends BaseKeyGenerator
{
public Poly1305KeyGen()
{
super("Poly1305-SM4", 256, new Poly1305KeyGenerator());
}
}
public static class AlgParamGen
extends BaseAlgorithmParameterGenerator
{
protected void engineInit(
AlgorithmParameterSpec genParamSpec,
SecureRandom random)
throws InvalidAlgorithmParameterException
{
throw new InvalidAlgorithmParameterException("No supported AlgorithmParameterSpec for SM4 parameter generation.");
}
protected AlgorithmParameters engineGenerateParameters()
{
byte[] iv = new byte[16];
if (random == null)
{
random = new SecureRandom();
}
random.nextBytes(iv);
AlgorithmParameters params;
try
{
params = createParametersInstance("SM4");
params.init(new IvParameterSpec(iv));
}
catch (Exception e)
{
throw new RuntimeException(e.getMessage());
}
return params;
}
}
public static class AlgParams
extends IvAlgorithmParameters
{
protected String engineToString()
{
return "SM4 IV";
}
}
public static class Mappings
extends SymmetricAlgorithmProvider
{
private static final String PREFIX = SM4.class.getName();
public Mappings()
{
}
public void configure(ConfigurableProvider provider)
{
provider.addAlgorithm("AlgorithmParameters.SM4", PREFIX + "$AlgParams");
provider.addAlgorithm("AlgorithmParameterGenerator.SM4", PREFIX + "$AlgParamGen");
provider.addAlgorithm("Cipher.SM4", PREFIX + "$ECB");
provider.addAlgorithm("KeyGenerator.SM4", PREFIX + "$KeyGen");
addCMacAlgorithm(provider, "SM4", PREFIX + "$CMAC", PREFIX + "$KeyGen");
addGMacAlgorithm(provider, "SM4", PREFIX + "$GMAC", PREFIX + "$KeyGen");
addPoly1305Algorithm(provider, "SM4", PREFIX + "$Poly1305", PREFIX + "$Poly1305KeyGen");
}
}
}