com.unbound.client.CipherOper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of unbound-java-provider Show documentation
Show all versions of unbound-java-provider Show documentation
This is a collection of JAVA libraries that implement Unbound cryptographic classes for JAVA provider, PKCS11 wrapper, cryptoki, and advapi
package com.unbound.client;
import com.unbound.common.crypto.SystemProvider;
import com.unbound.provider.KeyParameters;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.ShortBufferException;
import javax.crypto.spec.SecretKeySpec;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.ArrayList;
import java.util.Arrays;
public abstract class CipherOper extends CryptoOper
{
private ByteArrayOutputStream buffer = null;
private ByteArrayOutputStream aadBuffer = null;
public Cipher swCipher = null;
public boolean bufferMode = false;
public boolean encMode = true;
public CipherMode mode = null;
public HashType hashType = null;
public HashType mgfHashType = null;
public byte[] oaepLabel = null;
public byte[] iv = null;
public byte[] auth = null;
public int tagLen = 0;
public int dataLen = 0;
public ArrayList siv_auth = null;
public boolean padding = false;
public int speBits = 0;
public int fpeMode = 0;
public int fpeMaxLen = 0;
public String fpeFormat = null;
protected abstract byte[] hwUpdateEnc(byte[] in) ;
protected abstract byte[] hwFinalEnc(byte[] in) ;
protected abstract byte[] hwEnc(byte[] in, boolean wrapPurpose) ;
protected abstract byte[] hwUpdateDec(byte[] in) ;
protected abstract byte[] hwFinalDec(byte[] in) ;
protected abstract byte[] hwDec(byte[] in, boolean unwrapPurpose) ;
protected abstract byte[] hwWrap(KeyObject wrappedKey) ;
protected abstract KeyObject hwUnwrap(byte[] in, String name, ObjectType type, KeyParameters kp);
public void reset()
{
swCipher = null;
buffer = null;
aadBuffer = null;
super.reset();
}
public void updateAuth(byte[] in, int inOffset, int inLen)
{
if (aadBuffer==null) aadBuffer = new ByteArrayOutputStream();
aadBuffer.write(in, inOffset, inLen);
auth = aadBuffer.toByteArray();
}
public byte[] update(byte[] in, int offset, int length)
{
if (swCipher!=null) return swCipher.update(in, offset, length);
if (in!=null && (offset!=0 || length!=in.length)) in = Arrays.copyOfRange(in, offset, offset+length);
return encMode ? updateEnc(in) : updateDec(in);
}
public int update(byte[] in, int inOffset, int inLen, byte[] out, int outOffset) throws ShortBufferException
{
if (swCipher!=null) return swCipher.update(in, inOffset, inLen, out, outOffset);
if (in!=null && (inOffset!=0 || inLen!=in.length)) in = Arrays.copyOfRange(in, inOffset, inOffset+inLen);
byte[] temp = encMode ? updateEnc(in) : updateDec(in);
if (outOffset + temp.length > out.length) throw new ShortBufferException("Output buffer is too small");
System.arraycopy(temp, 0, out, outOffset, temp.length);
return temp.length;
}
public byte[] finalEncDec(byte[] in, int offset, int length) throws BadPaddingException, IllegalBlockSizeException
{
if (swCipher!=null) return swCipher.doFinal(in, offset, length);
if (in!=null && (offset!=0 || length!=in.length)) in = Arrays.copyOfRange(in, offset, offset+length);
return encMode ? finalEnc(in) : finalDec(in);
}
public int finalEncDec(byte[] in, int inOffset, int inLen, byte[] out, int outOffset) throws ShortBufferException, BadPaddingException, IllegalBlockSizeException
{
if (swCipher!=null) return swCipher.doFinal(in, inOffset, inLen, out, outOffset);
if (in!=null && (inOffset!=0 || inLen!=in.length)) in = Arrays.copyOfRange(in, inOffset, inOffset+inLen);
byte[] temp = encMode ? finalEnc(in) : finalDec(in);
if (outOffset + temp.length > out.length) throw new ShortBufferException("Output buffer is too small");
System.arraycopy(temp, 0, out, outOffset, temp.length);
return temp.length;
}
public byte[] updateEnc(byte[] in)
{
if (swCipher!=null) return swCipher.update(in);
if (in==null || in.length==0) return new byte[0];
checkSession();
return hwUpdateEnc(in);
}
public byte[] finalEnc(byte[] in) throws BadPaddingException, IllegalBlockSizeException
{
if (swCipher!=null) return swCipher.doFinal(in);
checkSession();
try { return hwFinalEnc(in); }
finally { reset(); }
}
public byte[] enc(byte[] in)
{
if (swCipher!=null)
{
try { return swCipher.doFinal(in); }
catch (IllegalBlockSizeException | BadPaddingException e) { throw new ProviderException(e); }
}
checkSession();
try { return hwEnc(in, false); }
finally { reset(); }
}
public byte[] updateDec(byte[] in)
{
if (in==null || in.length==0) return new byte[0];
if (bufferMode)
{
if (buffer==null) buffer = new ByteArrayOutputStream();
try { buffer.write(in); }
catch (IOException e) { throw new ProviderException(e); }
return new byte[0];
}
checkSession();
return hwUpdateDec(in);
}
public byte[] finalDec(byte[] in)
{
if (bufferMode && buffer!=null)
{
try
{
if (in!=null && in.length>0) buffer.write(in);
in = buffer.toByteArray();
}
catch (IOException e) { throw new ProviderException(e); }
}
checkSession();
try { return hwFinalDec(in); }
finally { reset(); }
}
public byte[] dec(byte[] in)
{
checkSession();
try { return hwDec(in, false); }
finally { reset(); }
}
public byte[] wrap(KeyObject wrappedKey)
{
checkSession();
try { return hwWrap(wrappedKey); }
finally { reset(); }
}
public KeyObject unwrap(byte[] in, String name, ObjectType type, KeyParameters kp)
{
checkSession();
try { return hwUnwrap(in, name, type, kp); }
finally { reset(); }
}
public byte[] swWrap(Key wrappedKey)
{
if (swCipher!=null)
{
try { return swCipher.wrap(wrappedKey); }
catch (IllegalBlockSizeException | InvalidKeyException e) { throw new ProviderException(e); }
}
checkSession();
try { return hwEnc(wrappedKey.getEncoded(), true); }
finally { reset(); }
}
public Key swUnwrap(byte[] in, String alg, int wrappedKeyType)
{
byte[] encoded;
checkSession();
try { encoded = hwDec(in,true); }
finally { reset(); }
if (wrappedKeyType == Cipher.SECRET_KEY) return new SecretKeySpec(encoded, alg);
KeyFactory kf = SystemProvider.KeyFactory.getInstance(alg);
try
{
if (wrappedKeyType == Cipher.PUBLIC_KEY) return kf.generatePublic(new X509EncodedKeySpec(encoded));
if (wrappedKeyType == Cipher.PRIVATE_KEY) return kf.generatePrivate(new PKCS8EncodedKeySpec(encoded));
}
catch (InvalidKeySpecException e) { throw new ProviderException(e); }
throw new ProviderException("Invalid wrapped key type");
}
}