MacWrapper implements PrimitiveWrapper<Mac, Mac> { @Override public Mac wrap(final PrimitiveSet<Mac> primitives) throws GeneralSecurityException { return new WrappedMac(primitives); } MacWrapper(); @Override Mac wrap(final PrimitiveSet<Mac> primitives); @Override Class<Mac> getPrimitiveClass(); @Override Class<Mac> getInputPrimitiveClass(); static void register();  }
@Test public void testMultipleKeys() throws Exception { byte[] keyValue = Random.randBytes(HMAC_KEY_SIZE); Key tink = TestUtil.createKey( TestUtil.createHmacKeyData(keyValue, 16), 42, KeyStatusType.ENABLED, OutputPrefixType.TINK); Key legacy = TestUtil.createKey( TestUtil.createHmacKeyData(keyValue, 16), 43, KeyStatusType.ENABLED, OutputPrefixType.LEGACY); Key raw = TestUtil.createKey( TestUtil.createHmacKeyData(keyValue, 16), 44, KeyStatusType.ENABLED, OutputPrefixType.RAW); Key crunchy = TestUtil.createKey( TestUtil.createHmacKeyData(keyValue, 16), 45, KeyStatusType.ENABLED, OutputPrefixType.CRUNCHY); Key[] keys = new Key[] {tink, legacy, raw, crunchy}; int j = keys.length; for (int i = 0; i < j; i++) { KeysetHandle keysetHandle = TestUtil.createKeysetHandle( TestUtil.createKeyset( keys[i], keys[(i + 1) % j], keys[(i + 2) % j], keys[(i + 3) % j])); Mac mac = new MacWrapper().wrap(Registry.getPrimitives(keysetHandle, Mac.class)); byte[] plaintext = "plaintext".getBytes("UTF-8"); byte[] tag = mac.computeMac(plaintext); if (!keys[i].getOutputPrefixType().equals(OutputPrefixType.RAW)) { byte[] prefix = Arrays.copyOfRange(tag, 0, CryptoFormat.NON_RAW_PREFIX_SIZE); assertArrayEquals(prefix, CryptoFormat.getOutputPrefix(keys[i])); } try { mac.verifyMac(tag, plaintext); } catch (GeneralSecurityException e) { fail("Valid MAC, should not throw exception: " + i); } byte[] plaintextAndTag = Bytes.concat(plaintext, tag); for (int b = 0; b < plaintextAndTag.length; b++) { for (int bit = 0; bit < 8; bit++) { byte[] modified = Arrays.copyOf(plaintextAndTag, plaintextAndTag.length); modified[b] ^= (byte) (1 << bit); try { mac.verifyMac(Arrays.copyOfRange(modified, plaintext.length, modified.length), Arrays.copyOfRange(modified, 0, plaintext.length)); fail("Invalid tag or plaintext, should have thrown exception"); } catch (GeneralSecurityException expected) { } } } KeysetHandle keysetHandle2 = TestUtil.createKeysetHandle( TestUtil.createKeyset(raw, legacy, tink, crunchy)); Mac mac2 = new MacWrapper().wrap(Registry.getPrimitives(keysetHandle2, Mac.class)); tag = mac2.computeMac(plaintext); try { mac.verifyMac(tag, plaintext); } catch (GeneralSecurityException e) { fail("Valid MAC, should not throw exception"); } byte[] keyValue2 = Random.randBytes(HMAC_KEY_SIZE); Key random = TestUtil.createKey( TestUtil.createHmacKeyData(keyValue2, 16), 44, KeyStatusType.ENABLED, OutputPrefixType.TINK); keysetHandle2 = TestUtil.createKeysetHandle( TestUtil.createKeyset(random)); mac2 = new MacWrapper().wrap(Registry.getPrimitives(keysetHandle2, Mac.class)); tag = mac2.computeMac(plaintext); try { mac.verifyMac(tag, plaintext); fail("Invalid MAC MAC, should have thrown exception"); } catch (GeneralSecurityException expected) { } } }
@Test public void testSmallPlaintextWithRawKey() throws Exception { byte[] keyValue = Random.randBytes(HMAC_KEY_SIZE); Key primary = TestUtil.createKey( TestUtil.createHmacKeyData(keyValue, 16), 42, KeyStatusType.ENABLED, OutputPrefixType.RAW); KeysetHandle keysetHandle = TestUtil.createKeysetHandle( TestUtil.createKeyset(primary)); Mac mac = new MacWrapper().wrap(Registry.getPrimitives(keysetHandle, Mac.class)); byte[] plaintext = "blah".getBytes("UTF-8"); byte[] tag = mac.computeMac(plaintext); assertEquals(16 , tag.length); try { mac.verifyMac(tag, plaintext); } catch (GeneralSecurityException e) { fail("Valid MAC, should not throw exception"); } }