All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.unbound.provider.UBECDSASignature Maven / Gradle / Ivy

Go to download

This is a collection of JAVA libraries that implement Unbound cryptographic classes for JAVA provider, PKCS11 wrapper, cryptoki, and advapi

There is a newer version: 42761
Show newest version
package com.unbound.provider;

import com.unbound.client.*;
import java.security.*;
import java.security.interfaces.ECPublicKey;

public class UBECDSASignature extends SignatureSpi
{
  private final SignatureOper oper = Client.getInstance().newSignatureOperation();

  UBECDSASignature(HashType hashType)
  {
    oper.mode = SignatureMode.ECDSA;
    oper.hashType = hashType;
  }

// ------------------------------ interface ----------------------

  @Override
  protected void engineInitVerify(PublicKey publicKey) throws InvalidKeyException
  {
    if (!(publicKey instanceof ECPublicKey)) throw new InvalidKeyException("Invalid key type");

    try
    {
      String hashName = oper.hashType==null ? "NONE" : oper.hashType.getName();
      oper.swSignature = Signature.getInstance(hashName+"withECDSA", "SunEC");
      oper.swSignature.initVerify(publicKey);
    }
    catch (Exception e) { throw new InvalidKeyException("engineInitVerify failed"); }
  }

  @Override
  protected void engineInitSign(PrivateKey privateKey) throws InvalidKeyException
  {
    if (!(privateKey instanceof UBECPrivateKey)) throw new InvalidKeyException("Invalid key type");
    oper.keyObject = ((UBECPrivateKey)privateKey).object;
  }

  @Override
  protected void engineUpdate(byte b) throws SignatureException
  {
    oper.update(b);
  }

  @Override
  protected void engineUpdate(byte[] in, int inOffset, int inLen) throws SignatureException
  {
    oper.update(in, inOffset, inLen);
  }

  @Override
  protected byte[] engineSign() throws SignatureException
  {
    return oper.finalSign();
  }

  @Override
  protected boolean engineVerify(byte[] sigBytes) throws SignatureException
  {
    return oper.finalVerify(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");
  }

  public static final class Raw extends UBECDSASignature
  { public Raw() { super(null); } }
  public static final class SHA1 extends UBECDSASignature
  { public SHA1() { super(HashType.SHA1); } }
  public static final class SHA256 extends UBECDSASignature
  { public SHA256() { super(HashType.SHA256); } }
  public static final class SHA384 extends UBECDSASignature
  { public SHA384() { super(HashType.SHA384); } }
  public static final class SHA512 extends UBECDSASignature
  { public SHA512() { super(HashType.SHA512); } }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy