use of com.google.crypto.tink.proto.testing.StreamingAeadDecryptResponse 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.StreamingAeadDecryptResponse in project tink by google.
the class StreamingAeadServiceImpl method decrypt.
/**
* Decrypts a message.
*/
@Override
public void decrypt(StreamingAeadDecryptRequest request, StreamObserver<StreamingAeadDecryptResponse> responseObserver) {
StreamingAeadDecryptResponse response;
try {
KeysetHandle keysetHandle = CleartextKeysetHandle.read(BinaryKeysetReader.withBytes(request.getKeyset().toByteArray()));
StreamingAead streamingAead = keysetHandle.getPrimitive(StreamingAead.class);
InputStream ciphertextStream = request.getCiphertext().newInput();
InputStream decryptingStream = streamingAead.newDecryptingStream(ciphertextStream, request.getAssociatedData().toByteArray());
ByteArrayOutputStream plaintextStream = new ByteArrayOutputStream();
while (true) {
int bytesRead = decryptingStream.read();
if (bytesRead == -1) {
break;
}
plaintextStream.write(bytesRead);
}
response = StreamingAeadDecryptResponse.newBuilder().setPlaintext(ByteString.copyFrom(plaintextStream.toByteArray())).build();
} catch (GeneralSecurityException | InvalidProtocolBufferException e) {
response = StreamingAeadDecryptResponse.newBuilder().setErr(e.toString()).build();
} catch (IOException e) {
response = StreamingAeadDecryptResponse.newBuilder().setErr(e.toString()).build();
}
responseObserver.onNext(response);
responseObserver.onCompleted();
}
use of com.google.crypto.tink.proto.testing.StreamingAeadDecryptResponse in project tink by google.
the class TestingServicesTest method streamingAeadDecrypt_failsOnBadCiphertext.
@Test
public void streamingAeadDecrypt_failsOnBadCiphertext() throws Exception {
byte[] template = KeyTemplateProtoConverter.toByteArray(AesGcmHkdfStreamingKeyManager.aes128GcmHkdf4KBTemplate());
byte[] badCiphertext = "bad ciphertext".getBytes(UTF_8);
byte[] associatedData = "streamingAead_decrypt_fails_on_bad_ciphertext".getBytes(UTF_8);
KeysetGenerateResponse keysetResponse = generateKeyset(keysetStub, template);
assertThat(keysetResponse.getErr()).isEmpty();
byte[] keyset = keysetResponse.getKeyset().toByteArray();
StreamingAeadDecryptResponse decResponse = streamingAeadDecrypt(streamingAeadStub, keyset, badCiphertext, associatedData);
assertThat(decResponse.getErr()).isNotEmpty();
}
use of com.google.crypto.tink.proto.testing.StreamingAeadDecryptResponse 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