Search in sources :

Example 1 with DeterministicAeadDecryptResponse

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

the class TestingServicesTest method daeadDecryptDeterministically_failsOnBadKeyset.

@Test
public void daeadDecryptDeterministically_failsOnBadKeyset() throws Exception {
    byte[] template = KeyTemplateProtoConverter.toByteArray(AesSivKeyManager.aes256SivTemplate());
    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();
    DeterministicAeadEncryptResponse encResponse = daeadEncrypt(daeadStub, keyset, plaintext, associatedData);
    assertThat(encResponse.getErr()).isEmpty();
    byte[] ciphertext = encResponse.getCiphertext().toByteArray();
    byte[] badKeyset = "bad keyset".getBytes(UTF_8);
    DeterministicAeadDecryptResponse decResponse = daeadDecrypt(daeadStub, badKeyset, ciphertext, associatedData);
    assertThat(decResponse.getErr()).isNotEmpty();
}
Also used : DeterministicAeadEncryptResponse(com.google.crypto.tink.proto.testing.DeterministicAeadEncryptResponse) DeterministicAeadDecryptResponse(com.google.crypto.tink.proto.testing.DeterministicAeadDecryptResponse) KeysetGenerateResponse(com.google.crypto.tink.proto.testing.KeysetGenerateResponse) Test(org.junit.Test)

Example 2 with DeterministicAeadDecryptResponse

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

the class DeterministicAeadServiceImpl method decryptDeterministically.

/**
 * Decrypts a message.
 */
@Override
public void decryptDeterministically(DeterministicAeadDecryptRequest request, StreamObserver<DeterministicAeadDecryptResponse> responseObserver) {
    DeterministicAeadDecryptResponse response;
    try {
        KeysetHandle keysetHandle = CleartextKeysetHandle.read(BinaryKeysetReader.withBytes(request.getKeyset().toByteArray()));
        DeterministicAead daead = keysetHandle.getPrimitive(DeterministicAead.class);
        byte[] plaintext = daead.decryptDeterministically(request.getCiphertext().toByteArray(), request.getAssociatedData().toByteArray());
        response = DeterministicAeadDecryptResponse.newBuilder().setPlaintext(ByteString.copyFrom(plaintext)).build();
    } catch (GeneralSecurityException | InvalidProtocolBufferException e) {
        response = DeterministicAeadDecryptResponse.newBuilder().setErr(e.toString()).build();
    } catch (IOException e) {
        responseObserver.onError(Status.UNKNOWN.withDescription(e.getMessage()).asException());
        return;
    }
    responseObserver.onNext(response);
    responseObserver.onCompleted();
}
Also used : DeterministicAeadDecryptResponse(com.google.crypto.tink.proto.testing.DeterministicAeadDecryptResponse) CleartextKeysetHandle(com.google.crypto.tink.CleartextKeysetHandle) KeysetHandle(com.google.crypto.tink.KeysetHandle) DeterministicAead(com.google.crypto.tink.DeterministicAead) GeneralSecurityException(java.security.GeneralSecurityException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) IOException(java.io.IOException)

Example 3 with DeterministicAeadDecryptResponse

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

the class TestingServicesTest method daeadDecryptDeterministically_failsOnBadCiphertext.

@Test
public void daeadDecryptDeterministically_failsOnBadCiphertext() throws Exception {
    byte[] template = KeyTemplateProtoConverter.toByteArray(AesSivKeyManager.aes256SivTemplate());
    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();
    DeterministicAeadDecryptResponse decResponse = daeadDecrypt(daeadStub, keyset, badCiphertext, associatedData);
    assertThat(decResponse.getErr()).isNotEmpty();
}
Also used : DeterministicAeadDecryptResponse(com.google.crypto.tink.proto.testing.DeterministicAeadDecryptResponse) KeysetGenerateResponse(com.google.crypto.tink.proto.testing.KeysetGenerateResponse) Test(org.junit.Test)

Example 4 with DeterministicAeadDecryptResponse

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

the class TestingServicesTest method daeadGenerateEncryptDecryptDeterministically_success.

@Test
public void daeadGenerateEncryptDecryptDeterministically_success() throws Exception {
    byte[] template = KeyTemplateProtoConverter.toByteArray(AesSivKeyManager.aes256SivTemplate());
    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();
    DeterministicAeadEncryptResponse encResponse = daeadEncrypt(daeadStub, keyset, plaintext, associatedData);
    assertThat(encResponse.getErr()).isEmpty();
    byte[] ciphertext = encResponse.getCiphertext().toByteArray();
    DeterministicAeadDecryptResponse decResponse = daeadDecrypt(daeadStub, keyset, ciphertext, associatedData);
    assertThat(decResponse.getErr()).isEmpty();
    byte[] output = decResponse.getPlaintext().toByteArray();
    assertThat(output).isEqualTo(plaintext);
}
Also used : DeterministicAeadEncryptResponse(com.google.crypto.tink.proto.testing.DeterministicAeadEncryptResponse) DeterministicAeadDecryptResponse(com.google.crypto.tink.proto.testing.DeterministicAeadDecryptResponse) KeysetGenerateResponse(com.google.crypto.tink.proto.testing.KeysetGenerateResponse) Test(org.junit.Test)

Aggregations

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