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);
}
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();
}
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();
}
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();
}
Aggregations