AeadWrapper implements PrimitiveWrapper<Aead, Aead> { @Override public Aead wrap(final PrimitiveSet<Aead> pset) throws GeneralSecurityException { return new WrappedAead(pset); } AeadWrapper(); @Override Aead wrap(final PrimitiveSet<Aead> pset); @Override Class<Aead> getPrimitiveClass(); @Override Class<Aead> getInputPrimitiveClass(); static void register();  }
@Test public void testBasicAesCtrHmacAead() throws Exception { byte[] aesCtrKeyValue = Random.randBytes(AES_KEY_SIZE); byte[] hmacKeyValue = Random.randBytes(HMAC_KEY_SIZE); int ivSize = 12; int tagSize = 16; KeysetHandle keysetHandle = TestUtil.createKeysetHandle( TestUtil.createKeyset( TestUtil.createKey( TestUtil.createAesCtrHmacAeadKeyData( aesCtrKeyValue, ivSize, hmacKeyValue, tagSize), 42, KeyStatusType.ENABLED, OutputPrefixType.TINK))); Aead aead = new AeadWrapper().wrap(Registry.getPrimitives(keysetHandle, Aead.class)); byte[] plaintext = Random.randBytes(20); byte[] associatedData = Random.randBytes(20); byte[] ciphertext = aead.encrypt(plaintext, associatedData); byte[] decrypted = aead.decrypt(ciphertext, associatedData); assertArrayEquals(plaintext, decrypted); }
@Test public void testMultipleKeys() throws Exception { byte[] aesCtrKeyValue = Random.randBytes(AES_KEY_SIZE); byte[] hmacKeyValue = Random.randBytes(HMAC_KEY_SIZE); int ivSize = 12; int tagSize = 16; Key primary = TestUtil.createKey( TestUtil.createAesCtrHmacAeadKeyData(aesCtrKeyValue, ivSize, hmacKeyValue, tagSize), 42, KeyStatusType.ENABLED, OutputPrefixType.TINK); Key raw = TestUtil.createKey( TestUtil.createAesCtrHmacAeadKeyData(aesCtrKeyValue, ivSize, hmacKeyValue, tagSize), 43, KeyStatusType.ENABLED, OutputPrefixType.RAW); Key legacy = TestUtil.createKey( TestUtil.createAesCtrHmacAeadKeyData(aesCtrKeyValue, ivSize, hmacKeyValue, tagSize), 44, KeyStatusType.ENABLED, OutputPrefixType.LEGACY); Key tink = TestUtil.createKey( TestUtil.createAesCtrHmacAeadKeyData(aesCtrKeyValue, ivSize, hmacKeyValue, tagSize), 45, KeyStatusType.ENABLED, OutputPrefixType.TINK); KeysetHandle keysetHandle = TestUtil.createKeysetHandle(TestUtil.createKeyset(primary, raw, legacy, tink)); Aead aead = new AeadWrapper().wrap(Registry.getPrimitives(keysetHandle, Aead.class)); byte[] plaintext = Random.randBytes(20); byte[] associatedData = Random.randBytes(20); byte[] ciphertext = aead.encrypt(plaintext, associatedData); byte[] prefix = Arrays.copyOfRange(ciphertext, 0, CryptoFormat.NON_RAW_PREFIX_SIZE); assertArrayEquals(prefix, CryptoFormat.getOutputPrefix(primary)); assertArrayEquals(plaintext, aead.decrypt(ciphertext, associatedData)); assertEquals( CryptoFormat.NON_RAW_PREFIX_SIZE + plaintext.length + ivSize + tagSize, ciphertext.length); KeysetHandle keysetHandle2 = TestUtil.createKeysetHandle(TestUtil.createKeyset(raw, legacy, tink)); Aead aead2 = new AeadWrapper().wrap(Registry.getPrimitives(keysetHandle2, Aead.class)); ciphertext = aead2.encrypt(plaintext, associatedData); assertArrayEquals(plaintext, aead.decrypt(ciphertext, associatedData)); byte[] aesCtrKeyValue2 = Random.randBytes(AES_KEY_SIZE); byte[] hmacKeyValue2 = Random.randBytes(HMAC_KEY_SIZE); Key random = TestUtil.createKey( TestUtil.createAesCtrHmacAeadKeyData(aesCtrKeyValue2, ivSize, hmacKeyValue2, tagSize), 44, KeyStatusType.ENABLED, OutputPrefixType.TINK); keysetHandle2 = TestUtil.createKeysetHandle(TestUtil.createKeyset(random)); aead2 = new AeadWrapper().wrap(Registry.getPrimitives(keysetHandle2, Aead.class)); ciphertext = aead2.encrypt(plaintext, associatedData); try { aead.decrypt(ciphertext, associatedData); fail("Expected GeneralSecurityException"); } catch (GeneralSecurityException e) { assertExceptionContains(e, "decryption failed"); } }
@Test public void testRawKeyAsPrimary() throws Exception { byte[] aesCtrKeyValue = Random.randBytes(AES_KEY_SIZE); byte[] hmacKeyValue = Random.randBytes(HMAC_KEY_SIZE); int ivSize = 12; int tagSize = 16; Key primary = TestUtil.createKey( TestUtil.createAesCtrHmacAeadKeyData(aesCtrKeyValue, ivSize, hmacKeyValue, tagSize), 42, KeyStatusType.ENABLED, OutputPrefixType.RAW); Key raw = TestUtil.createKey( TestUtil.createAesCtrHmacAeadKeyData(aesCtrKeyValue, ivSize, hmacKeyValue, tagSize), 43, KeyStatusType.ENABLED, OutputPrefixType.RAW); Key legacy = TestUtil.createKey( TestUtil.createAesCtrHmacAeadKeyData(aesCtrKeyValue, ivSize, hmacKeyValue, tagSize), 44, KeyStatusType.ENABLED, OutputPrefixType.LEGACY); KeysetHandle keysetHandle = TestUtil.createKeysetHandle(TestUtil.createKeyset(primary, raw, legacy)); Aead aead = new AeadWrapper().wrap(Registry.getPrimitives(keysetHandle, Aead.class)); byte[] plaintext = Random.randBytes(20); byte[] associatedData = Random.randBytes(20); byte[] ciphertext = aead.encrypt(plaintext, associatedData); assertArrayEquals(plaintext, aead.decrypt(ciphertext, associatedData)); assertEquals( CryptoFormat.RAW_PREFIX_SIZE + plaintext.length + ivSize + tagSize, ciphertext.length); }
@Test public void testSmallPlaintextWithRawKey() throws Exception { byte[] aesCtrKeyValue = Random.randBytes(AES_KEY_SIZE); byte[] hmacKeyValue = Random.randBytes(HMAC_KEY_SIZE); int ivSize = 12; int tagSize = 16; Key primary = TestUtil.createKey( TestUtil.createAesCtrHmacAeadKeyData(aesCtrKeyValue, ivSize, hmacKeyValue, tagSize), 42, KeyStatusType.ENABLED, OutputPrefixType.RAW); KeysetHandle keysetHandle = TestUtil.createKeysetHandle(TestUtil.createKeyset(primary)); Aead aead = new AeadWrapper().wrap(Registry.getPrimitives(keysetHandle, Aead.class)); byte[] plaintext = Random.randBytes(1); byte[] associatedData = Random.randBytes(20); byte[] ciphertext = aead.encrypt(plaintext, associatedData); assertArrayEquals(plaintext, aead.decrypt(ciphertext, associatedData)); assertEquals( CryptoFormat.RAW_PREFIX_SIZE + plaintext.length + ivSize + tagSize, ciphertext.length); }