use of com.google.crypto.tink.proto.testing.DeterministicAeadEncryptResponse 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();
}
use of com.google.crypto.tink.proto.testing.DeterministicAeadEncryptResponse in project tink by google.
the class DeterministicAeadServiceImpl method encryptDeterministically.
/**
* Encrypts a message.
*/
@Override
public void encryptDeterministically(DeterministicAeadEncryptRequest request, StreamObserver<DeterministicAeadEncryptResponse> responseObserver) {
DeterministicAeadEncryptResponse response;
try {
KeysetHandle keysetHandle = CleartextKeysetHandle.read(BinaryKeysetReader.withBytes(request.getKeyset().toByteArray()));
DeterministicAead daead = keysetHandle.getPrimitive(DeterministicAead.class);
byte[] ciphertext = daead.encryptDeterministically(request.getPlaintext().toByteArray(), request.getAssociatedData().toByteArray());
response = DeterministicAeadEncryptResponse.newBuilder().setCiphertext(ByteString.copyFrom(ciphertext)).build();
} catch (GeneralSecurityException | InvalidProtocolBufferException e) {
response = DeterministicAeadEncryptResponse.newBuilder().setErr(e.toString()).build();
} catch (IOException e) {
responseObserver.onError(Status.UNKNOWN.withDescription(e.getMessage()).asException());
return;
}
responseObserver.onNext(response);
responseObserver.onCompleted();
}
use of com.google.crypto.tink.proto.testing.DeterministicAeadEncryptResponse 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);
}
use of com.google.crypto.tink.proto.testing.DeterministicAeadEncryptResponse in project tink by google.
the class TestingServicesTest method daeadEncryptDeterministically_failsOnBadKeyset.
@Test
public void daeadEncryptDeterministically_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 = "aead_encrypt_fails_on_bad_keyset".getBytes(UTF_8);
DeterministicAeadEncryptResponse encResponse = daeadEncrypt(daeadStub, badKeyset, plaintext, associatedData);
assertThat(encResponse.getErr()).isNotEmpty();
}
Aggregations