@Test
  public void multipleKeysReadKeysetAndDeriveAesGcmKeyset_success() throws Exception {
    Assume.assumeFalse(TestUtil.isAndroid()); // some android versions don't support AesGcm
    KeysetHandle handle =
        TinkJsonProtoKeysetFormat.parseKeyset(
            JSON_AEAD_KEYSET_DERIVATION_KEYSET_WITH_MULTIPLE_KEYS, InsecureSecretKeyAccess.get());
    KeysetDeriver deriver = handle.getPrimitive(KeysetDeriver.class);

    KeysetHandle derivedHandle = deriver.deriveKeyset("salt".getBytes(UTF_8));

    // Derived keyset should only contain AEAD keys
    Aead aead = derivedHandle.getPrimitive(Aead.class);
    byte[] plaintext = "plaintext".getBytes(UTF_8);
    byte[] associatedData = "associatedData".getBytes(UTF_8);
    byte[] ciphertext = aead.encrypt(plaintext, associatedData);
    assertThat(aead.decrypt(ciphertext, associatedData)).isEqualTo(plaintext);

    // Also test that aead can decrypt messages encrypted with keyset derived from a non-primary
    // key. Use JSON_AEAD_KEYSET_DERIVATION_KEYSET, because it contains the first key from
    // JSON_AEAD_KEYSET_DERIVATION_KEYSET_WITH_MULTIPLE_KEYS.
    KeysetHandle handle1 =
        TinkJsonProtoKeysetFormat.parseKeyset(
            JSON_AEAD_KEYSET_DERIVATION_KEYSET, InsecureSecretKeyAccess.get());
    KeysetDeriver deriver1 = handle1.getPrimitive(KeysetDeriver.class);
    KeysetHandle derivedHandle1 = deriver1.deriveKeyset("salt".getBytes(UTF_8));
    Aead aead1 = derivedHandle1.getPrimitive(Aead.class);
    byte[] ciphertext1 = aead1.encrypt(plaintext, associatedData);
    assertThat(aead.decrypt(ciphertext1, associatedData)).isEqualTo(plaintext);
  }
Unit tests for the Key Derivation package. Uses only the public API.
java_src/src/test/java/com/google/crypto/tink/keyderivation/KeyDerivationTest.java