Search in sources :

Example 1 with StreamingAeadEncryptResponse

use of com.google.crypto.tink.proto.testing.StreamingAeadEncryptResponse 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 StreamingAeadEncryptResponse

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

the class TestingServicesTest method streamingAeadEncrypt_failsOnBadKeyset.

@Test
public void streamingAeadEncrypt_failsOnBadKeyset() throws Exception {
    byte[] badKeyset = "bad keyset".getBytes(UTF_8);
    byte[] plaintext = "The quick brown fox jumps over the lazy dog".getBytes(UTF_8);
    byte[] associatedData = "streamingAead_encrypt_fails_on_bad_keyset".getBytes(UTF_8);
    StreamingAeadEncryptResponse encResponse = streamingAeadEncrypt(streamingAeadStub, badKeyset, plaintext, associatedData);
    assertThat(encResponse.getErr()).isNotEmpty();
}
Also used : StreamingAeadEncryptResponse(com.google.crypto.tink.proto.testing.StreamingAeadEncryptResponse) Test(org.junit.Test)

Example 3 with StreamingAeadEncryptResponse

use of com.google.crypto.tink.proto.testing.StreamingAeadEncryptResponse 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)

Example 4 with StreamingAeadEncryptResponse

use of com.google.crypto.tink.proto.testing.StreamingAeadEncryptResponse 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

StreamingAeadEncryptResponse (com.google.crypto.tink.proto.testing.StreamingAeadEncryptResponse)4 Test (org.junit.Test)3 KeysetGenerateResponse (com.google.crypto.tink.proto.testing.KeysetGenerateResponse)2 StreamingAeadDecryptResponse (com.google.crypto.tink.proto.testing.StreamingAeadDecryptResponse)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 OutputStream (java.io.OutputStream)1 GeneralSecurityException (java.security.GeneralSecurityException)1