java.lang.Object
net.i2p.crypto.HMACGenerator
Direct Known Subclasses:
HMAC256Generator

public abstract class HMACGeneratorextends Object
Abstract base class for HMAC (Hash-based Message Authentication Code) operations.

This class provides the foundation for HMAC algorithm implementations used throughout I2P for message authentication and integrity verification. It defines the standard interface for calculating and verifying HMAC values with various hash algorithms.

Key operations include:

  • HMAC calculation with given key and message data
  • Inline MAC verification for performance optimization
  • Support for different hash algorithms (SHA-1, SHA-256)
  • Integration with I2P's session key management

Implementation Note: As of 0.9.42, this class serves as an abstract base. See net.i2p.router.transport.udp.SSUHMACGenerator for SSU-specific HMAC implementation and SHA256Generator for Syndie-compatible HMAC operations.

  • Constructor Summary

    Constructors
    Constructor
    Description
    Initializes nothing; subclasses are called through it implicitly.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected byte[]
    32 bytes from the byte array cache.
    abstract void
    calculate(SessionKey key, byte[] data, int offset, int length, byte[] target, int targetOffset)
    Calculate the HMAC of the data with the given key.
    protected void
    releaseTmp(byte[] tmp)
    Release a temporary array back to the cache.
    abstract boolean
    verify(SessionKey key, byte[] curData, int curOffset, int curLength, byte[] origMAC, int origMACOffset, int origMACLength)
    Verify the MAC inline, reducing some unnecessary memory churn.

    Methods inherited from class Object

    clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • HMACGenerator

      public HMACGenerator()
      Initializes nothing; subclasses are called through it implicitly.
  • Method Details

    • calculate

      public abstract void calculate(SessionKey key, byte[] data, int offset, int length, byte[] target, int targetOffset)
      Calculate the HMAC of the data with the given key.
      Parameters:
      key - the session key
      data - the data to HMAC
      offset - the starting offset in data
      length - how much data do we want to run the HMAC over
      target - out parameter the first 16 bytes contain the HMAC, the last 16 bytes are zero
      targetOffset - offset into target to put the hmac
      Throws:
      IllegalArgumentException - for bad key or target too small
    • verify

      public abstract boolean verify(SessionKey key, byte[] curData, int curOffset, int curLength, byte[] origMAC, int origMACOffset, int origMACLength)
      Verify the MAC inline, reducing some unnecessary memory churn.
      Parameters:
      key - session key to verify the MAC with
      curData - MAC to verify
      curOffset - index into curData to MAC
      curLength - how much data in curData do we want to run the HMAC over
      origMAC - what do we expect the MAC of curData to equal
      origMACOffset - index into origMAC
      origMACLength - how much of the MAC do we want to verify
      Returns:
      true if the MAC matches
      Throws:
      IllegalArgumentException - for bad key
    • acquireTmp

      protected byte[] acquireTmp()
      32 bytes from the byte array cache. Does NOT zero.
      Returns:
      32-byte tmp array
    • releaseTmp

      protected void releaseTmp(byte[] tmp)
      Release a temporary array back to the cache.
      Parameters:
      tmp - a 32-byte array acquired from the byte cache, not zeroed