StreamingAeadWrapper implements PrimitiveWrapper<StreamingAead, StreamingAead> { @Override public StreamingAead wrap(final PrimitiveSet<StreamingAead> primitives) throws GeneralSecurityException { return new StreamingAeadHelper(primitives); } StreamingAeadWrapper(); @Override StreamingAead wrap(final PrimitiveSet<StreamingAead> primitives); @Override Class<StreamingAead> getPrimitiveClass(); @Override Class<StreamingAead> getInputPrimitiveClass(); static void register();  }
@Test public void testBasicAesCtrHmacStreamingAead() throws Exception { byte[] keyValue = Random.randBytes(KDF_KEY_SIZE); int derivedKeySize = AES_KEY_SIZE; int ciphertextSegmentSize = 128; KeysetHandle keysetHandle = TestUtil.createKeysetHandle( TestUtil.createKeyset( TestUtil.createKey( TestUtil.createAesCtrHmacStreamingKeyData( keyValue, derivedKeySize, ciphertextSegmentSize), 42, KeyStatusType.ENABLED, OutputPrefixType.RAW))); StreamingAead streamingAead = new StreamingAeadWrapper().wrap(Registry.getPrimitives(keysetHandle, StreamingAead.class)); StreamingTestUtil.testEncryptionAndDecryption(streamingAead); }
@Test public void testBasicAesGcmHkdfStreamingAead() throws Exception { byte[] keyValue = Random.randBytes(KDF_KEY_SIZE); int derivedKeySize = AES_KEY_SIZE; int ciphertextSegmentSize = 128; KeysetHandle keysetHandle = TestUtil.createKeysetHandle( TestUtil.createKeyset( TestUtil.createKey( TestUtil.createAesGcmHkdfStreamingKeyData( keyValue, derivedKeySize, ciphertextSegmentSize), 42, KeyStatusType.ENABLED, OutputPrefixType.RAW))); StreamingAead streamingAead = new StreamingAeadWrapper().wrap(Registry.getPrimitives(keysetHandle, StreamingAead.class)); StreamingTestUtil.testEncryptionAndDecryption(streamingAead); }
@Test public void testMultipleKeys() throws Exception { byte[] primaryKeyValue = Random.randBytes(KDF_KEY_SIZE); byte[] otherKeyValue = Random.randBytes(KDF_KEY_SIZE); byte[] anotherKeyValue = Random.randBytes(KDF_KEY_SIZE); int derivedKeySize = AES_KEY_SIZE; Key primaryKey = TestUtil.createKey( TestUtil.createAesGcmHkdfStreamingKeyData(primaryKeyValue, derivedKeySize, 512), 42, KeyStatusType.ENABLED, OutputPrefixType.RAW); Key otherKey = TestUtil.createKey( TestUtil.createAesCtrHmacStreamingKeyData(otherKeyValue, derivedKeySize, 256), 43, KeyStatusType.ENABLED, OutputPrefixType.RAW); Key anotherKey = TestUtil.createKey( TestUtil.createAesGcmHkdfStreamingKeyData(anotherKeyValue, derivedKeySize, 1024), 72, KeyStatusType.ENABLED, OutputPrefixType.RAW); KeysetHandle keysetHandle = TestUtil.createKeysetHandle(TestUtil.createKeyset(primaryKey, otherKey, anotherKey)); StreamingAead streamingAead = new StreamingAeadWrapper().wrap(Registry.getPrimitives(keysetHandle, StreamingAead.class)); StreamingAead primaryAead = new StreamingAeadWrapper() .wrap( Registry.getPrimitives( TestUtil.createKeysetHandle(TestUtil.createKeyset(primaryKey)), StreamingAead.class)); StreamingAead otherAead = new StreamingAeadWrapper() .wrap( Registry.getPrimitives( TestUtil.createKeysetHandle(TestUtil.createKeyset(otherKey)), StreamingAead.class)); StreamingAead anotherAead = new StreamingAeadWrapper() .wrap( Registry.getPrimitives( TestUtil.createKeysetHandle(TestUtil.createKeyset(anotherKey)), StreamingAead.class)); StreamingTestUtil.testEncryptionAndDecryption(streamingAead, streamingAead); StreamingTestUtil.testEncryptionAndDecryption(streamingAead, primaryAead); StreamingTestUtil.testEncryptionAndDecryption(primaryAead, streamingAead); StreamingTestUtil.testEncryptionAndDecryption(otherAead, streamingAead); StreamingTestUtil.testEncryptionAndDecryption(anotherAead, streamingAead); StreamingTestUtil.testEncryptionAndDecryption(primaryAead, primaryAead); StreamingTestUtil.testEncryptionAndDecryption(otherAead, otherAead); StreamingTestUtil.testEncryptionAndDecryption(anotherAead, anotherAead); try { StreamingTestUtil.testEncryptionAndDecryption(otherAead, primaryAead); fail("No matching key, should have thrown an exception"); } catch (IOException expected) { assertExceptionContains(expected, "No matching key"); } try { StreamingTestUtil.testEncryptionAndDecryption(anotherAead, primaryAead); fail("No matching key, should have thrown an exception"); } catch (IOException expected) { assertExceptionContains(expected, "No matching key"); } }