java.lang.Object
net.i2p.crypto.HMACGenerator
net.i2p.crypto.HMAC256Generator
HMAC-SHA256 message authentication code generator for I2P cryptographic operations.
This class provides HMAC-SHA256 calculation using the standard JCA Mac interface, ensuring compatibility with javax.crypto.Mac.getInstance("HmacSHA256"). It offers both one-shot calculation and streaming operations for different use cases.
Key features:
- HMAC-SHA256 algorithm implementation for message authentication
- Thread-safe operation with Mac instance pooling
- Optimized for both small and large data processing
- Integration with I2P's session key management
Compatibility Note: As of 0.9.12, this class uses the standard javax.crypto.Mac implementation for improved security and compatibility.
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static final classPerformance-optimized SecretKey implementation for HMAC operations.Field Summary
FieldsModifier and TypeFieldDescriptionprivate final LinkedBlockingQueue<Mac> Initialized MACs ready for reuse.private static final booleanWhether initialized MACs are pooled.private static final intMACs held for reuse.Constructor Summary
ConstructorsMethod Summary
Public MethodsModifier and TypeMethodDescription(package private) Macacquire()Package private for HKDF.voidcalculate(byte[] key, byte[] data, int offset, int length, byte[] target, int targetOffset) Calculate the HMAC of the data with the given key.voidcalculate(SessionKey key, byte[] data, int offset, int length, byte[] target, int targetOffset) Calculate the HMAC of the data with the given key.(package private) voidRelease a Mac back to the pool.booleanverify(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 HMACGenerator
acquireTmp, releaseTmpModifier and TypeMethodDescriptionprotected byte[]32 bytes from the byte array cache.protected voidreleaseTmp(byte[] tmp) Release a temporary array back to the cache.
Field Details
_macs
Initialized MACs ready for reuse.CACHE
private static final boolean CACHEWhether initialized MACs are pooled.- See Also:
CACHE_SIZE
Constructor Details
HMAC256Generator
Create a new HMAC256Generator.- Parameters:
context- not read; the generator keeps no reference to it
Method Details
calculate
public void calculate(SessionKey key, byte[] data, int offset, int length, byte[] target, int targetOffset) Calculate the HMAC of the data with the given key. Outputs 32 bytes to target starting at targetOffset.- Specified by:
calculatein classHMACGenerator- Parameters:
key- the session keydata- the data to HMACoffset- the starting offset in datalength- how much data do we want to run the HMAC overtarget- out parameter the first 16 bytes contain the HMAC, the last 16 bytes are zerotargetOffset- offset into target to put the hmac- Throws:
UnsupportedOperationException- if the JVM does not support itIllegalArgumentException- for bad key or target too small
calculate
public void calculate(byte[] key, byte[] data, int offset, int length, byte[] target, int targetOffset) Calculate the HMAC of the data with the given key. Outputs 32 bytes to target starting at targetOffset.- Parameters:
key- first 32 bytes used as the keydata- the data to calculate the HMAC overoffset- offset into datalength- number of bytes to includetarget- receives the 32 byte MAC, so it needs 32 bytes of roomtargetOffset- where in target to write the MAC- Throws:
UnsupportedOperationException- if the JVM does not support itIllegalArgumentException- for bad key or target too small
verify
public 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.- Specified by:
verifyin classHMACGenerator- Parameters:
key- session key to verify the MAC withcurData- MAC to verifycurOffset- index into curData to MACcurLength- how much data in curData do we want to run the HMAC overorigMAC- what do we expect the MAC of curData to equalorigMACOffset- index into origMACorigMACLength- how much of the MAC do we want to verify, use 32 for HMAC256- Returns:
- true if the MAC matches
acquire
release
Release a Mac back to the pool. Mac will be reset, discarding any accumulated message state. Per the JCA contract,Mac.reset()"resets this Mac object to the state it was in when previously initialized via a call to init(Key)", so it clears the message state without re-running the ipad/opad key schedule thatMac.init(java.security.Key)does. That is all a pooled Mac needs, because every caller of acquire() must init() before use, and engineInit() overwrites all 64 bytes of both k_ipad and k_opad, so the previous key schedule cannot leak into the next operation. Package private for HKDF.- Parameters:
mac- the Mac to release