com.unbound.provider.RSASignature 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.provider;
import com.unbound.common.crypto.SystemProvider;
import com.unbound.provider.kmip.KMIP;
import java.io.IOException;
import java.security.*;
import java.security.interfaces.RSAPublicKey;
import java.util.Arrays;
public class RSASignature extends SignatureSpi
{
private Signature pubSignature = null;
private UBRSAPrivateKey prvKey = null;
private HashType hash;
private MessageDigest md = null;
private byte[] buffer = new byte[501]; // 512 - 11
private int bufferOffset = 0;
static String hashBitSizeToName(int hashBitSize) throws InvalidAlgorithmParameterException
{
switch (hashBitSize)
{
case 0: return "NONE";
case 160: return "SHA1";
case 256: return "SHA256";
case 384: return "SHA384";
case 512: return "SHA512";
}
throw new InvalidAlgorithmParameterException("Unsupported hash algorithm");
}
/*
static String hashBitSizeToDigestName(int hashBitSize) throws InvalidAlgorithmParameterException
{
switch (hashBitSize)
{
case 160: return "SHA-1";
case 256: return "SHA-256";
case 384: return "SHA-384";
case 512: return "SHA-512";
}
throw new InvalidAlgorithmParameterException("Unsupported hash algorithm");
}
private static int hashBitSizeToKmipAlg(int hashBitSize)
{
switch (hashBitSize)
{
case 160: return KMIP.DigitalSignatureAlgorithm.SHA_1WithRSAEncryption;
case 256: return KMIP.DigitalSignatureAlgorithm.SHA_256WithRSAEncryption;
case 384: return KMIP.DigitalSignatureAlgorithm.SHA_384WithRSAEncryption;
case 512: return KMIP.DigitalSignatureAlgorithm.SHA_512WithRSAEncryption;
}
return 0;
}
*/
RSASignature(HashType hash)
{
this.hash = hash;
}
// ------------------------- interface ---------------------------------
@Override
protected void engineInitVerify(PublicKey publicKey) throws InvalidKeyException
{
if (!(publicKey instanceof RSAPublicKey)) throw new InvalidKeyException("Invalid key type");
try
{
String hashName = hash==null ? "NONE" : hash.name;
pubSignature = SystemProvider.Signature.getInstance(hashName+"WithRSA");
}
catch (Throwable e) { throw new InvalidKeyException("engineInitVerify failed"); }
pubSignature.initVerify(publicKey);
}
@Override
protected void engineInitSign(PrivateKey privateKey) throws InvalidKeyException
{
if (!(privateKey instanceof UBRSAPrivateKey)) throw new InvalidKeyException("Invalid key type");
prvKey = (UBRSAPrivateKey)privateKey;
bufferOffset = 0;
md = (hash==null) ? null : hash.getMessageDigest();
}
@Override
protected void engineUpdate(byte b) throws SignatureException
{
if (pubSignature!=null)
{
pubSignature.update(b);
return;
}
byte[] in = {b};
engineUpdate(in, 0, 1);
}
@Override
protected void engineUpdate(byte[] in, int inOffset, int inLen) throws SignatureException
{
if (pubSignature!=null)
{
pubSignature.update(in, inOffset, inLen);
return;
}
if (md==null)
{
System.arraycopy(in, inOffset, buffer, bufferOffset, inLen);
bufferOffset += inLen;
}
else md.update(in, inOffset, inLen);
}
@Override
protected byte[] engineSign() throws SignatureException
{
byte[] in = (md==null) ? Arrays.copyOfRange(buffer, 0, bufferOffset) : md.digest();
int kmipAlg = hash==null ? 0 : hash.kmipRsaSignatureCode;
try
{
return prvKey.sign(in, KMIP.CryptographicAlgorithm.RSA, kmipAlg);
}
catch (IOException e) { throw new ProviderException(e); }
}
@Override
protected boolean engineVerify(byte[] sigBytes) throws SignatureException
{
return pubSignature.verify(sigBytes);
}
@Override
protected void engineSetParameter(String param, Object value) throws InvalidParameterException
{
throw new UnsupportedOperationException("setParameter() not supported");
}
@Override
protected Object engineGetParameter(String param) throws InvalidParameterException
{
throw new UnsupportedOperationException("getParameter() not supported");
}
// ----------------------- sub-classes ----------------------
public static final class NONEwithRSA extends RSASignature { public NONEwithRSA() { super(null); } }
public static final class SHA1withRSA extends RSASignature { public SHA1withRSA() { super(HashType.SHA1); } }
public static final class SHA256withRSA extends RSASignature { public SHA256withRSA() { super(HashType.SHA256); } }
public static final class SHA384withRSA extends RSASignature { public SHA384withRSA() { super(HashType.SHA384); } }
public static final class SHA512withRSA extends RSASignature { public SHA512withRSA() { super(HashType.SHA512); } }
}