KMAC - MAC with optional XOF mode. From NIST Special Publication 800-185 - SHA-3 Derived Functions:cSHAKE, KMAC, TupleHash and ParallelHash
| 20 | * </p> |
| 21 | */ |
| 22 | public class KMAC |
| 23 | implements Mac, Xof, Memoable, EncodableDigest |
| 24 | { |
| 25 | private static final byte[] padding = new byte[100]; |
| 26 | |
| 27 | private final CSHAKEDigest cshake; |
| 28 | |
| 29 | private int bitLength; |
| 30 | private int outputLength; |
| 31 | |
| 32 | private byte[] key; |
| 33 | private boolean initialised; |
| 34 | private boolean firstOutput; |
| 35 | |
| 36 | /** |
| 37 | * Base constructor. |
| 38 | * |
| 39 | * @param bitLength bit length of the underlying SHAKE function, 128 or 256. |
| 40 | * @param S the customization string - available for local use. |
| 41 | */ |
| 42 | public KMAC(int bitLength, byte[] S) |
| 43 | { |
| 44 | this.cshake = new CSHAKEDigest(bitLength, Strings.toByteArray("KMAC"), S); |
| 45 | this.bitLength = bitLength; |
| 46 | this.outputLength = bitLength * 2 / 8; |
| 47 | } |
| 48 | |
| 49 | public KMAC(KMAC original) |
| 50 | { |
| 51 | this.cshake = new CSHAKEDigest(original.cshake); |
| 52 | this.bitLength = original.bitLength; |
| 53 | this.outputLength = original.outputLength; |
| 54 | this.key = original.key; |
| 55 | this.initialised = original.initialised; |
| 56 | this.firstOutput = original.firstOutput; |
| 57 | } |
| 58 | |
| 59 | public KMAC(byte[] state) |
| 60 | { |
| 61 | this.key = new byte[state[0] & 0xff]; |
| 62 | System.arraycopy(state, 1, key, 0, key.length); |
| 63 | this.cshake = new CSHAKEDigest(Arrays.copyOfRange(state, 1 + key.length, state.length - 10)); |
| 64 | |
| 65 | this.bitLength = Pack.bigEndianToInt(state, state.length - 10); |
| 66 | this.outputLength = Pack.bigEndianToInt(state, state.length - 6); |
| 67 | this.initialised = state[state.length - 2] != 0; |
| 68 | this.firstOutput = state[state.length - 1] != 0; |
| 69 | } |
| 70 | |
| 71 | private void copyIn(KMAC original) |
| 72 | { |
| 73 | this.cshake.reset(original.cshake); |
| 74 | this.bitLength = original.bitLength; |
| 75 | this.outputLength = original.outputLength; |
| 76 | this.initialised = original.initialised; |
| 77 | this.firstOutput = original.firstOutput; |
| 78 | } |
| 79 |
nothing calls this directly
no outgoing calls
no test coverage detected