test_class.declarator: HMAC_KEY_SIZE = 20
test_class.modifier: private static final
test_class.original_string: private static final int HMAC_KEY_SIZE = 20;
test_class.type: int
test_class.var_name: HMAC_KEY_SIZE
test_class.file: java_src/src/test/java/com/google/crypto/tink/mac/MacWrapperTest.java
test_class.identifier: MacWrapperTest
test_case.body: @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);
        }
  
        // Modify plaintext or tag and make sure the verifyMac failed.
        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) {
              // Expected
            }
          }
        }
  
        // mac with a non-primary RAW key, verify with the keyset
        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");
        }
  
        // mac with a random key not in the keyset, verify with the keyset should fail
        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) {
          // Expected
        }
      }
    }
test_case.class_method_signature: MacWrapperTest.testMultipleKeys()
test_case.full_signature: @Test public void testMultipleKeys()
test_case.identifier: testMultipleKeys
test_case.invocations: randBytes createKey createHmacKeyData createKey createHmacKeyData createKey createHmacKeyData createKey createHmacKeyData createKeysetHandle createKeyset wrap getPrimitives getBytes computeMac equals getOutputPrefixType copyOfRange assertArrayEquals getOutputPrefix verifyMac fail concat copyOf verifyMac copyOfRange copyOfRange fail createKeysetHandle createKeyset wrap getPrimitives computeMac verifyMac fail randBytes createKey createHmacKeyData createKeysetHandle createKeyset wrap getPrimitives computeMac verifyMac fail
test_case.modifiers: @Test public
test_case.parameters: ()
test_case.return: void
test_case.signature: void testMultipleKeys()
focal_class.declarator: logger = Logger.getLogger(MacWrapper.class.getName())
focal_class.modifier: private static final
focal_class.original_string: private static final Logger logger = Logger.getLogger(MacWrapper.class.getName());
focal_class.type: Logger
focal_class.var_name: logger
focal_class.file: java_src/src/main/java/com/google/crypto/tink/mac/MacWrapper.java
focal_class.identifier: MacWrapper
focal_class.interfaces: implements PrimitiveWrapper<Mac, Mac>
focal_class.class_method_signature: MacWrapper.MacWrapper()
focal_class.full_signature:   MacWrapper()
focal_class.identifier: MacWrapper
focal_class.parameters: ()
focal_class.signature:  MacWrapper()
focal_class.class_method_signature: MacWrapper.wrap(final PrimitiveSet<Mac> primitives)
focal_class.full_signature: @Override public Mac wrap(final PrimitiveSet<Mac> primitives)
focal_class.identifier: wrap
focal_class.modifiers: @Override public
focal_class.parameters: (final PrimitiveSet<Mac> primitives)
focal_class.return: Mac
focal_class.signature: Mac wrap(final PrimitiveSet<Mac> primitives)
focal_class.class_method_signature: MacWrapper.getPrimitiveClass()
focal_class.full_signature: @Override public Class<Mac> getPrimitiveClass()
focal_class.identifier: getPrimitiveClass
focal_class.modifiers: @Override public
focal_class.parameters: ()
focal_class.return: Class<Mac>
focal_class.signature: Class<Mac> getPrimitiveClass()
focal_class.class_method_signature: MacWrapper.getInputPrimitiveClass()
focal_class.full_signature: @Override public Class<Mac> getInputPrimitiveClass()
focal_class.identifier: getInputPrimitiveClass
focal_class.modifiers: @Override public
focal_class.parameters: ()
focal_class.return: Class<Mac>
focal_class.signature: Class<Mac> getInputPrimitiveClass()
focal_class.class_method_signature: MacWrapper.register()
focal_class.full_signature: public static void register()
focal_class.identifier: register
focal_class.modifiers: public static
focal_class.parameters: ()
focal_class.return: void
focal_class.signature: void register()
focal_method.body: @Override
    public Mac wrap(final PrimitiveSet<Mac> primitives) throws GeneralSecurityException {
      return new WrappedMac(primitives);
    }
focal_method.class_method_signature: MacWrapper.wrap(final PrimitiveSet<Mac> primitives)
focal_method.full_signature: @Override public Mac wrap(final PrimitiveSet<Mac> primitives)
focal_method.identifier: wrap
focal_method.modifiers: @Override public
focal_method.parameters: (final PrimitiveSet<Mac> primitives)
focal_method.return: Mac
focal_method.signature: Mac wrap(final PrimitiveSet<Mac> primitives)