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

com.unbound.client.MacOper 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.client;

import com.unbound.common.Converter;

import java.util.Arrays;

public abstract class MacOper extends CryptoOper
{
  public MacMode mode = null;
  public HashType hashType = null;
  public byte[] iv = null;
  public byte[] auth = null;
  public int tagLen = 0;
  public int dataLen = 0;
  private byte[] cache = new byte[64];
  private int cacheLen = 0;

  private byte[] oneByte = null;

  protected abstract void   hwUpdateMac(byte[] in);
  protected abstract byte[] hwFinalMac(byte[] in);
  protected abstract byte[] hwMac(byte[] in);

  @Override
  public void reset()
  {
    cacheLen=0;
    super.reset();
  }

  public void update(byte in)
  {
    if (oneByte==null) oneByte = new byte[1];
    oneByte[0] = in;
    update(oneByte);
  }
  public void update(byte[] in, int offset, int length)
  {
    if (length==0) return;
    if (in!=null && (offset!=0 || length!=in.length)) in = Arrays.copyOfRange(in, offset, offset+length);
    update(in);
  }

  public void update(byte[] in)
  {
    if (in==null || in.length==0) return;
    checkSession();
    if (cacheLen+in.length<=64)
    {
      System.arraycopy(in, 0, cache, cacheLen, in.length);
      cacheLen+=in.length;
      return;
    }

    if (cacheLen>0) { in = Converter.concat(Arrays.copyOf(cache, cacheLen), in);  cacheLen = 0; }
    hwUpdateMac(in);
  }

  public byte[] finalMac(byte[] in)
  {
    checkSession();

    if (cacheLen>0) { in = Converter.concat(Arrays.copyOf(cache, cacheLen), in); cacheLen = 0; }
    try { return hwFinalMac(in); }
    finally { reset(); }
  }

  public byte[] mac(byte[] in)
  {
    checkSession();
    try { return hwMac(in); }
    finally { reset(); }
  }

  public int getMacLen()
  {
    if (hashType!=null) return hashType.getBitSize()/8;
    return keyObject.getType().getBlockSize();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy