Search in sources :

Example 1 with StreamingAeadDecryptResponse

use of com.google.crypto.tink.proto.testing.StreamingAeadDecryptResponse in project tink by google.

the class TestingServicesTest method streamingAeadGenerateEncryptDecrypt_success.

@Test
public void streamingAeadGenerateEncryptDecrypt_success() throws Exception {
    byte[] template = KeyTemplateProtoConverter.toByteArray(AesGcmHkdfStreamingKeyManager.aes128GcmHkdf4KBTemplate());
    byte[] plaintext = "The quick brown fox jumps over the lazy dog".getBytes(UTF_8);
    byte[] associatedData = "generate_encrypt_decrypt".getBytes(UTF_8);
    KeysetGenerateResponse keysetResponse = generateKeyset(keysetStub, template);
    assertThat(keysetResponse.getErr()).isEmpty();
    byte[] keyset = keysetResponse.getKeyset().toByteArray();
    StreamingAeadEncryptResponse encResponse = streamingAeadEncrypt(streamingAeadStub, keyset, plaintext, associatedData);
    assertThat(encResponse.getErr()).isEmpty();
    byte[] ciphertext = encResponse.getCiphertext().toByteArray();
    StreamingAeadDecryptResponse decResponse = streamingAeadDecrypt(streamingAeadStub, keyset, ciphertext, associatedData);
    assertThat(decResponse.getErr()).isEmpty();
    byte[] output = decResponse.getPlaintext().toByteArray();
    assertThat(output).isEqualTo(plaintext);
}
Also used : StreamingAeadEncryptResponse(com.google.crypto.tink.proto.testing.StreamingAeadEncryptResponse) StreamingAeadDecryptResponse(com.google.crypto.tink.proto.testing.StreamingAeadDecryptResponse) KeysetGenerateResponse(com.google.crypto.tink.proto.testing.KeysetGenerateResponse) Test(org.junit.Test)

Example 2 with StreamingAeadDecryptResponse

use of com.google.crypto.tink.proto.testing.StreamingAeadDecryptResponse in project tink by google.

the class StreamingAeadServiceImpl method decrypt.

/**
 * Decrypts a message.
 */
@Override
public void decrypt(StreamingAeadDecryptRequest request, StreamObserver<StreamingAeadDecryptResponse> responseObserver) {
    StreamingAeadDecryptResponse response;
    try {
        KeysetHandle keysetHandle = CleartextKeysetHandle.read(BinaryKeysetReader.withBytes(request.getKeyset().toByteArray()));
        StreamingAead streamingAead = keysetHandle.getPrimitive(StreamingAead.class);
        InputStream ciphertextStream = request.getCiphertext().newInput();
        InputStream decryptingStream = streamingAead.newDecryptingStream(ciphertextStream, request.getAssociatedData().toByteArray());
        ByteArrayOutputStream plaintextStream = new ByteArrayOutputStream();
        while (true) {
            int bytesRead = decryptingStream.read();
            if (bytesRead == -1) {
                break;
            }
            plaintextStream.write(bytesRead);
        }
        response = StreamingAeadDecryptResponse.newBuilder().setPlaintext(ByteString.copyFrom(plaintextStream.toByteArray())).build();
    } catch (GeneralSecurityException | InvalidProtocolBufferException e) {
        response = StreamingAeadDecryptResponse.newBuilder().setErr(e.toString()).build();
    } catch (IOException e) {
        response = StreamingAeadDecryptResponse.newBuilder().setErr(e.toString()).build();
    }
    responseObserver.onNext(response);
    responseObserver.onCompleted();
}
Also used : CleartextKeysetHandle(com.google.crypto.tink.CleartextKeysetHandle) KeysetHandle(com.google.crypto.tink.KeysetHandle) InputStream(java.io.InputStream) StreamingAeadDecryptResponse(com.google.crypto.tink.proto.testing.StreamingAeadDecryptResponse) GeneralSecurityException(java.security.GeneralSecurityException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) StreamingAead(com.google.crypto.tink.StreamingAead)

Example 3 with StreamingAeadDecryptResponse

use of com.google.crypto.tink.proto.testing.StreamingAeadDecryptResponse in project tink by google.

the class TestingServicesTest method streamingAeadDecrypt_failsOnBadCiphertext.

@Test
public void streamingAeadDecrypt_failsOnBadCiphertext() throws Exception {
    byte[] template = KeyTemplateProtoConverter.toByteArray(AesGcmHkdfStreamingKeyManager.aes128GcmHkdf4KBTemplate());
    byte[] badCiphertext = "bad ciphertext".getBytes(UTF_8);
    byte[] associatedData = "streamingAead_decrypt_fails_on_bad_ciphertext".getBytes(UTF_8);
    KeysetGenerateResponse keysetResponse = generateKeyset(keysetStub, template);
    assertThat(keysetResponse.getErr()).isEmpty();
    byte[] keyset = keysetResponse.getKeyset().toByteArray();
    StreamingAeadDecryptResponse decResponse = streamingAeadDecrypt(streamingAeadStub, keyset, badCiphertext, associatedData);
    assertThat(decResponse.getErr()).isNotEmpty();
}
Also used : StreamingAeadDecryptResponse(com.google.crypto.tink.proto.testing.StreamingAeadDecryptResponse) KeysetGenerateResponse(com.google.crypto.tink.proto.testing.KeysetGenerateResponse) Test(org.junit.Test)

Example 4 with StreamingAeadDecryptResponse

use of com.google.crypto.tink.proto.testing.StreamingAeadDecryptResponse in project tink by google.

the class TestingServicesTest method streamingAeadDecrypt_failsOnBadKeyset.

@Test
public void streamingAeadDecrypt_failsOnBadKeyset() throws Exception {
    byte[] template = KeyTemplateProtoConverter.toByteArray(AesGcmHkdfStreamingKeyManager.aes128GcmHkdf4KBTemplate());
    byte[] plaintext = "The quick brown fox jumps over the lazy dog".getBytes(UTF_8);
    byte[] associatedData = "generate_encrypt_decrypt".getBytes(UTF_8);
    KeysetGenerateResponse keysetResponse = generateKeyset(keysetStub, template);
    assertThat(keysetResponse.getErr()).isEmpty();
    byte[] keyset = keysetResponse.getKeyset().toByteArray();
    StreamingAeadEncryptResponse encResponse = streamingAeadEncrypt(streamingAeadStub, keyset, plaintext, associatedData);
    assertThat(encResponse.getErr()).isEmpty();
    byte[] ciphertext = encResponse.getCiphertext().toByteArray();
    byte[] badKeyset = "bad keyset".getBytes(UTF_8);
    StreamingAeadDecryptResponse decResponse = streamingAeadDecrypt(streamingAeadStub, badKeyset, ciphertext, associatedData);
    assertThat(decResponse.getErr()).isNotEmpty();
}
Also used : StreamingAeadEncryptResponse(com.google.crypto.tink.proto.testing.StreamingAeadEncryptResponse) StreamingAeadDecryptResponse(com.google.crypto.tink.proto.testing.StreamingAeadDecryptResponse) KeysetGenerateResponse(com.google.crypto.tink.proto.testing.KeysetGenerateResponse) Test(org.junit.Test)

Aggregations

StreamingAeadDecryptResponse (com.google.crypto.tink.proto.testing.StreamingAeadDecryptResponse)4 KeysetGenerateResponse (com.google.crypto.tink.proto.testing.KeysetGenerateResponse)3 Test (org.junit.Test)3 StreamingAeadEncryptResponse (com.google.crypto.tink.proto.testing.StreamingAeadEncryptResponse)2 CleartextKeysetHandle (com.google.crypto.tink.CleartextKeysetHandle)1 KeysetHandle (com.google.crypto.tink.KeysetHandle)1 StreamingAead (com.google.crypto.tink.StreamingAead)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 GeneralSecurityException (java.security.GeneralSecurityException)1