Search in sources :

Example 6 with StreamingAead

use of com.google.crypto.tink.StreamingAead in project tink by google.

the class StreamingAeadWrapperTest method testMultipleKeys.

@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);
    // Another key with a smaller segment size than the primary key
    Key otherKey = TestUtil.createKey(TestUtil.createAesCtrHmacStreamingKeyData(otherKeyValue, derivedKeySize, 256), 43, KeyStatusType.ENABLED, OutputPrefixType.RAW);
    // Another key with a larger segment size than the primary key
    Key anotherKey = TestUtil.createKey(TestUtil.createAesGcmHkdfStreamingKeyData(anotherKeyValue, derivedKeySize, 1024), 72, KeyStatusType.ENABLED, OutputPrefixType.RAW);
    PrimitiveSet<StreamingAead> primitives = TestUtil.createPrimitiveSet(TestUtil.createKeyset(primaryKey, otherKey, anotherKey), StreamingAead.class);
    StreamingAead streamingAead = new StreamingAeadWrapper().wrap(primitives);
    StreamingAead primaryAead = new StreamingAeadWrapper().wrap(TestUtil.createPrimitiveSet(TestUtil.createKeyset(primaryKey), StreamingAead.class));
    StreamingAead otherAead = new StreamingAeadWrapper().wrap(TestUtil.createPrimitiveSet(TestUtil.createKeyset(otherKey), StreamingAead.class));
    StreamingAead anotherAead = new StreamingAeadWrapper().wrap(TestUtil.createPrimitiveSet(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);
    IOException expected = assertThrows(IOException.class, () -> StreamingTestUtil.testEncryptionAndDecryption(otherAead, primaryAead));
    assertExceptionContains(expected, "No matching key");
    IOException expected2 = assertThrows(IOException.class, () -> StreamingTestUtil.testEncryptionAndDecryption(anotherAead, primaryAead));
    assertExceptionContains(expected2, "No matching key");
}
Also used : IOException(java.io.IOException) Key(com.google.crypto.tink.proto.Keyset.Key) StreamingAead(com.google.crypto.tink.StreamingAead) Test(org.junit.Test)

Example 7 with StreamingAead

use of com.google.crypto.tink.StreamingAead in project tink by google.

the class StreamingAeadIntegrationTest method testBasicAesCtrHmacStreamingAead.

@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 = keysetHandle.getPrimitive(StreamingAead.class);
    StreamingTestUtil.testEncryptionAndDecryption(streamingAead);
}
Also used : KeysetHandle(com.google.crypto.tink.KeysetHandle) StreamingAead(com.google.crypto.tink.StreamingAead) Test(org.junit.Test)

Example 8 with StreamingAead

use of com.google.crypto.tink.StreamingAead in project tink by google.

the class StreamingAeadIntegrationTest method testMultipleKeys.

@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);
    // Another key with a smaller segment size than the primary key
    Key otherKey = TestUtil.createKey(TestUtil.createAesCtrHmacStreamingKeyData(otherKeyValue, derivedKeySize, 256), 43, KeyStatusType.ENABLED, OutputPrefixType.RAW);
    // Another key with a larger segment size than the primary key
    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 = keysetHandle.getPrimitive(StreamingAead.class);
    StreamingAead primaryAead = TestUtil.createKeysetHandle(TestUtil.createKeyset(primaryKey)).getPrimitive(StreamingAead.class);
    StreamingAead otherAead = TestUtil.createKeysetHandle(TestUtil.createKeyset(otherKey)).getPrimitive(StreamingAead.class);
    StreamingAead anotherAead = TestUtil.createKeysetHandle(TestUtil.createKeyset(anotherKey)).getPrimitive(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);
    IOException expected = assertThrows(IOException.class, () -> StreamingTestUtil.testEncryptionAndDecryption(otherAead, primaryAead));
    assertExceptionContains(expected, "No matching key");
    IOException expected2 = assertThrows(IOException.class, () -> StreamingTestUtil.testEncryptionAndDecryption(anotherAead, primaryAead));
    assertExceptionContains(expected2, "No matching key");
}
Also used : KeysetHandle(com.google.crypto.tink.KeysetHandle) IOException(java.io.IOException) Key(com.google.crypto.tink.proto.Keyset.Key) StreamingAead(com.google.crypto.tink.StreamingAead) Test(org.junit.Test)

Example 9 with StreamingAead

use of com.google.crypto.tink.StreamingAead in project tink by google.

the class SeekableByteChannelDecrypter method nextAttemptingChannel.

@GuardedBy("this")
private synchronized SeekableByteChannel nextAttemptingChannel() throws IOException {
    while (!remainingPrimitives.isEmpty()) {
        ciphertextChannel.position(startingPosition);
        StreamingAead streamingAead = this.remainingPrimitives.removeFirst();
        try {
            SeekableByteChannel decChannel = streamingAead.newSeekableDecryptingChannel(ciphertextChannel, associatedData);
            if (cachedPosition >= 0) {
                // Caller already set new position.
                decChannel.position(cachedPosition);
            }
            return decChannel;
        } catch (GeneralSecurityException e) {
        // Try another primitive.
        }
    }
    throw new IOException("No matching key found for the ciphertext in the stream.");
}
Also used : SeekableByteChannel(java.nio.channels.SeekableByteChannel) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) StreamingAead(com.google.crypto.tink.StreamingAead) GuardedBy(javax.annotation.concurrent.GuardedBy)

Example 10 with StreamingAead

use of com.google.crypto.tink.StreamingAead in project tink by google.

the class StreamingAeadServiceImpl method encrypt.

/**
 * Encrypts a message.
 */
@Override
public void encrypt(StreamingAeadEncryptRequest request, StreamObserver<StreamingAeadEncryptResponse> responseObserver) {
    StreamingAeadEncryptResponse response;
    try {
        KeysetHandle keysetHandle = CleartextKeysetHandle.read(BinaryKeysetReader.withBytes(request.getKeyset().toByteArray()));
        StreamingAead streamingAead = keysetHandle.getPrimitive(StreamingAead.class);
        ByteArrayOutputStream ciphertextStream = new ByteArrayOutputStream();
        try (OutputStream encryptingStream = streamingAead.newEncryptingStream(ciphertextStream, request.getAssociatedData().toByteArray())) {
            request.getPlaintext().writeTo(encryptingStream);
        }
        response = StreamingAeadEncryptResponse.newBuilder().setCiphertext(ByteString.copyFrom(ciphertextStream.toByteArray())).build();
    } catch (GeneralSecurityException | InvalidProtocolBufferException e) {
        response = StreamingAeadEncryptResponse.newBuilder().setErr(e.toString()).build();
    } catch (IOException e) {
        responseObserver.onError(Status.UNKNOWN.withDescription(e.getMessage()).asException());
        return;
    }
    responseObserver.onNext(response);
    responseObserver.onCompleted();
}
Also used : CleartextKeysetHandle(com.google.crypto.tink.CleartextKeysetHandle) KeysetHandle(com.google.crypto.tink.KeysetHandle) StreamingAeadEncryptResponse(com.google.crypto.tink.proto.testing.StreamingAeadEncryptResponse) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GeneralSecurityException(java.security.GeneralSecurityException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) StreamingAead(com.google.crypto.tink.StreamingAead)

Aggregations

StreamingAead (com.google.crypto.tink.StreamingAead)21 Test (org.junit.Test)14 IOException (java.io.IOException)10 KeysetHandle (com.google.crypto.tink.KeysetHandle)9 GeneralSecurityException (java.security.GeneralSecurityException)7 GuardedBy (javax.annotation.concurrent.GuardedBy)4 CleartextKeysetHandle (com.google.crypto.tink.CleartextKeysetHandle)3 PrimitiveSet (com.google.crypto.tink.PrimitiveSet)3 Key (com.google.crypto.tink.proto.Keyset.Key)3 AesCtrHmacStreamingKey (com.google.crypto.tink.proto.AesCtrHmacStreamingKey)2 AesCtrHmacStreamingKeyFormat (com.google.crypto.tink.proto.AesCtrHmacStreamingKeyFormat)2 AesGcmHkdfStreamingKey (com.google.crypto.tink.proto.AesGcmHkdfStreamingKey)2 ByteString (com.google.protobuf.ByteString)2 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 InputStream (java.io.InputStream)2 SeekableByteChannel (java.nio.channels.SeekableByteChannel)2 KeyManager (com.google.crypto.tink.KeyManager)1 AesGcmHkdfStreamingKeyFormat (com.google.crypto.tink.proto.AesGcmHkdfStreamingKeyFormat)1 KeyTypeEntry (com.google.crypto.tink.proto.KeyTypeEntry)1