use of com.google.crypto.tink.StreamingAead in project tink by google.
the class AesGcmHkdfStreamingKeyManagerTest method testBasic.
@Test
public void testBasic() throws Exception {
// Create primitive from a given key.
AesGcmHkdfStreamingKey key = AesGcmHkdfStreamingKey.newBuilder().setVersion(0).setKeyValue(ByteString.copyFrom(Random.randBytes(20))).setParams(keyParams).build();
StreamingAead streamingAead = keyManager.getPrimitive(key);
StreamingTestUtil.testEncryptionAndDecryption(streamingAead);
// Create a key from KeyFormat, and use the key.
AesGcmHkdfStreamingKeyFormat keyFormat = AesGcmHkdfStreamingKeyFormat.newBuilder().setParams(keyParams).setKeySize(16).build();
ByteString serializedKeyFormat = ByteString.copyFrom(keyFormat.toByteArray());
key = (AesGcmHkdfStreamingKey) keyManager.newKey(serializedKeyFormat);
streamingAead = keyManager.getPrimitive(key);
StreamingTestUtil.testEncryptionAndDecryption(streamingAead);
}
use of com.google.crypto.tink.StreamingAead in project tink by google.
the class StreamingAeadCatalogueTest method testBasic.
@Test
public void testBasic() throws Exception {
StreamingAeadCatalogue catalogue = new StreamingAeadCatalogue();
// Check a single key type, incl. case-insensitve primitive name.
String keyType = "type.googleapis.com/google.crypto.tink.AesGcmHkdfStreamingKey";
{
KeyManager<StreamingAead> manager = catalogue.getKeyManager(keyType, "StreamingAead", 0);
assertThat(manager.doesSupport(keyType)).isTrue();
}
{
KeyManager<StreamingAead> manager = catalogue.getKeyManager(keyType, "STReaMIngAeAD", 0);
assertThat(manager.doesSupport(keyType)).isTrue();
}
{
KeyManager<StreamingAead> manager = catalogue.getKeyManager(keyType, "STREAMINgaEAD", 0);
assertThat(manager.doesSupport(keyType)).isTrue();
}
// Check all entries from the current StreamingAeadConfig.
RegistryConfig config = StreamingAeadConfig.TINK_1_1_0;
int count = 0;
for (KeyTypeEntry entry : config.getEntryList()) {
if ("StreamingAead".equals(entry.getPrimitiveName())) {
count = count + 1;
KeyManager<StreamingAead> manager = catalogue.getKeyManager(entry.getTypeUrl(), "streamingaead", entry.getKeyManagerVersion());
assertThat(manager.doesSupport(entry.getTypeUrl())).isTrue();
}
}
assertEquals(2, count);
}
use of com.google.crypto.tink.StreamingAead in project tink by google.
the class ReadableByteChannelDecrypter method read.
@Override
@GuardedBy("this")
public synchronized int read(ByteBuffer dst) throws IOException {
if (dst.remaining() == 0) {
return 0;
}
if (matchingChannel != null) {
return matchingChannel.read(dst);
} else {
if (attemptedMatching) {
throw new IOException("No matching key found for the ciphertext in the stream.");
}
attemptedMatching = true;
List<PrimitiveSet.Entry<StreamingAead>> entries;
try {
entries = primitives.getRawPrimitives();
} catch (GeneralSecurityException e) {
throw new IOException("Keyset failure: ", e);
}
for (PrimitiveSet.Entry<StreamingAead> entry : entries) {
try {
ReadableByteChannel attemptedChannel = entry.getPrimitive().newDecryptingChannel(ciphertextChannel, associatedData);
int retValue = attemptedChannel.read(dst);
if (retValue > 0) {
// Found a matching channel
matchingChannel = attemptedChannel;
ciphertextChannel.disableRewinding();
} else if (retValue == 0) {
// Not clear whether the channel could be matched: it might be
// that the underlying channel didn't provide sufficiently many bytes
// to check the header, or maybe the header was checked, but there
// were no actual encrypted bytes in the channel yet.
// Should try again.
ciphertextChannel.rewind();
attemptedMatching = false;
}
return retValue;
} catch (IOException e) {
// Try another key.
// IOException is thrown e.g. when MAC is incorrect, but also in case
// of I/O failures.
// TODO(b/66098906): Use a subclass of IOException.
ciphertextChannel.rewind();
continue;
} catch (GeneralSecurityException e) {
// Try another key.
ciphertextChannel.rewind();
continue;
}
}
throw new IOException("No matching key found for the ciphertext in the stream.");
}
}
use of com.google.crypto.tink.StreamingAead in project tink by google.
the class SeekableByteChannelDecrypter method read.
@Override
@GuardedBy("this")
public synchronized int read(ByteBuffer dst) throws IOException {
if (dst.remaining() == 0) {
return 0;
}
if (matchingChannel != null) {
return matchingChannel.read(dst);
} else {
if (attemptedMatching) {
throw new IOException("No matching key found for the ciphertext in the stream.");
}
attemptedMatching = true;
List<PrimitiveSet.Entry<StreamingAead>> entries;
try {
entries = primitives.getRawPrimitives();
} catch (GeneralSecurityException e) {
throw new IOException("Keyset failure: ", e);
}
for (PrimitiveSet.Entry<StreamingAead> entry : entries) {
try {
SeekableByteChannel attemptedChannel = entry.getPrimitive().newSeekableDecryptingChannel(ciphertextChannel, associatedData);
if (cachedPosition >= 0) {
// Caller did set new position before 1st read().
attemptedChannel.position(cachedPosition);
}
int retValue = attemptedChannel.read(dst);
if (retValue > 0) {
// Found a matching channel.
matchingChannel = attemptedChannel;
} else if (retValue == 0) {
// Not clear whether the channel could be matched: it might be
// that the underlying channel didn't provide sufficiently many bytes
// to check the header, or maybe the header was checked, but there
// were no actual encrypted bytes in the channel yet.
// Should try again.
ciphertextChannel.position(startingPosition);
attemptedMatching = false;
}
matchingChannel = attemptedChannel;
return retValue;
} catch (IOException e) {
// Try another key.
// IOException is thrown e.g. when MAC is incorrect, but also in case
// of I/O failures.
// TODO(b/66098906): Use a subclass of IOException.
ciphertextChannel.position(startingPosition);
continue;
} catch (GeneralSecurityException e) {
// Try another key.
ciphertextChannel.position(startingPosition);
continue;
}
}
throw new IOException("No matching key found for the ciphertext in the stream.");
}
}
use of com.google.crypto.tink.StreamingAead in project tink by google.
the class StreamingAeadIntegrationTest method testBasicAesGcmHkdfStreamingAead.
@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 = keysetHandle.getPrimitive(StreamingAead.class);
StreamingTestUtil.testEncryptionAndDecryption(streamingAead);
}
Aggregations