Search in sources :

Example 1 with AeadDecryptResponse

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

the class TestingServicesTest method aeadGenerateEncryptDecrypt_success.

@Test
public void aeadGenerateEncryptDecrypt_success() throws Exception {
    byte[] template = KeyTemplateProtoConverter.toByteArray(KeyTemplates.get("AES128_GCM"));
    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();
    AeadEncryptResponse encResponse = aeadEncrypt(aeadStub, keyset, plaintext, associatedData);
    assertThat(encResponse.getErr()).isEmpty();
    byte[] ciphertext = encResponse.getCiphertext().toByteArray();
    AeadDecryptResponse decResponse = aeadDecrypt(aeadStub, keyset, ciphertext, associatedData);
    assertThat(decResponse.getErr()).isEmpty();
    byte[] output = decResponse.getPlaintext().toByteArray();
    assertThat(output).isEqualTo(plaintext);
}
Also used : DeterministicAeadDecryptResponse(com.google.crypto.tink.proto.testing.DeterministicAeadDecryptResponse) StreamingAeadDecryptResponse(com.google.crypto.tink.proto.testing.StreamingAeadDecryptResponse) AeadDecryptResponse(com.google.crypto.tink.proto.testing.AeadDecryptResponse) AeadEncryptResponse(com.google.crypto.tink.proto.testing.AeadEncryptResponse) DeterministicAeadEncryptResponse(com.google.crypto.tink.proto.testing.DeterministicAeadEncryptResponse) StreamingAeadEncryptResponse(com.google.crypto.tink.proto.testing.StreamingAeadEncryptResponse) KeysetGenerateResponse(com.google.crypto.tink.proto.testing.KeysetGenerateResponse) Test(org.junit.Test)

Example 2 with AeadDecryptResponse

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

the class TestingServicesTest method aeadDecrypt_failsOnBadKeyset.

@Test
public void aeadDecrypt_failsOnBadKeyset() throws Exception {
    byte[] template = KeyTemplateProtoConverter.toByteArray(KeyTemplates.get("AES128_GCM"));
    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();
    AeadEncryptResponse encResponse = aeadEncrypt(aeadStub, keyset, plaintext, associatedData);
    assertThat(encResponse.getErr()).isEmpty();
    byte[] ciphertext = encResponse.getCiphertext().toByteArray();
    byte[] badKeyset = "bad keyset".getBytes(UTF_8);
    AeadDecryptResponse decResponse = aeadDecrypt(aeadStub, badKeyset, ciphertext, associatedData);
    assertThat(decResponse.getErr()).isNotEmpty();
}
Also used : DeterministicAeadDecryptResponse(com.google.crypto.tink.proto.testing.DeterministicAeadDecryptResponse) StreamingAeadDecryptResponse(com.google.crypto.tink.proto.testing.StreamingAeadDecryptResponse) AeadDecryptResponse(com.google.crypto.tink.proto.testing.AeadDecryptResponse) AeadEncryptResponse(com.google.crypto.tink.proto.testing.AeadEncryptResponse) DeterministicAeadEncryptResponse(com.google.crypto.tink.proto.testing.DeterministicAeadEncryptResponse) StreamingAeadEncryptResponse(com.google.crypto.tink.proto.testing.StreamingAeadEncryptResponse) KeysetGenerateResponse(com.google.crypto.tink.proto.testing.KeysetGenerateResponse) Test(org.junit.Test)

Example 3 with AeadDecryptResponse

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

the class AeadServiceImpl method decrypt.

/**
 * Decrypts a message.
 */
@Override
public void decrypt(AeadDecryptRequest request, StreamObserver<AeadDecryptResponse> responseObserver) {
    AeadDecryptResponse response;
    try {
        KeysetHandle keysetHandle = CleartextKeysetHandle.read(BinaryKeysetReader.withBytes(request.getKeyset().toByteArray()));
        Aead aead = keysetHandle.getPrimitive(Aead.class);
        byte[] plaintext = aead.decrypt(request.getCiphertext().toByteArray(), request.getAssociatedData().toByteArray());
        response = AeadDecryptResponse.newBuilder().setPlaintext(ByteString.copyFrom(plaintext)).build();
    } catch (GeneralSecurityException | InvalidProtocolBufferException e) {
        response = AeadDecryptResponse.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) AeadDecryptResponse(com.google.crypto.tink.proto.testing.AeadDecryptResponse) GeneralSecurityException(java.security.GeneralSecurityException) Aead(com.google.crypto.tink.Aead) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) IOException(java.io.IOException)

Example 4 with AeadDecryptResponse

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

the class TestingServicesTest method aeadDecrypt_failsOnBadCiphertext.

@Test
public void aeadDecrypt_failsOnBadCiphertext() throws Exception {
    byte[] template = KeyTemplateProtoConverter.toByteArray(KeyTemplates.get("AES128_GCM"));
    byte[] badCiphertext = "bad ciphertext".getBytes(UTF_8);
    byte[] associatedData = "aead_decrypt_fails_on_bad_ciphertext".getBytes(UTF_8);
    KeysetGenerateResponse keysetResponse = generateKeyset(keysetStub, template);
    assertThat(keysetResponse.getErr()).isEmpty();
    byte[] keyset = keysetResponse.getKeyset().toByteArray();
    AeadDecryptResponse decResponse = aeadDecrypt(aeadStub, keyset, badCiphertext, associatedData);
    assertThat(decResponse.getErr()).isNotEmpty();
}
Also used : DeterministicAeadDecryptResponse(com.google.crypto.tink.proto.testing.DeterministicAeadDecryptResponse) StreamingAeadDecryptResponse(com.google.crypto.tink.proto.testing.StreamingAeadDecryptResponse) AeadDecryptResponse(com.google.crypto.tink.proto.testing.AeadDecryptResponse) KeysetGenerateResponse(com.google.crypto.tink.proto.testing.KeysetGenerateResponse) Test(org.junit.Test)

Aggregations

AeadDecryptResponse (com.google.crypto.tink.proto.testing.AeadDecryptResponse)4 DeterministicAeadDecryptResponse (com.google.crypto.tink.proto.testing.DeterministicAeadDecryptResponse)3 KeysetGenerateResponse (com.google.crypto.tink.proto.testing.KeysetGenerateResponse)3 StreamingAeadDecryptResponse (com.google.crypto.tink.proto.testing.StreamingAeadDecryptResponse)3 Test (org.junit.Test)3 AeadEncryptResponse (com.google.crypto.tink.proto.testing.AeadEncryptResponse)2 DeterministicAeadEncryptResponse (com.google.crypto.tink.proto.testing.DeterministicAeadEncryptResponse)2 StreamingAeadEncryptResponse (com.google.crypto.tink.proto.testing.StreamingAeadEncryptResponse)2 Aead (com.google.crypto.tink.Aead)1 CleartextKeysetHandle (com.google.crypto.tink.CleartextKeysetHandle)1 KeysetHandle (com.google.crypto.tink.KeysetHandle)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 IOException (java.io.IOException)1 GeneralSecurityException (java.security.GeneralSecurityException)1