use of com.google.crypto.tink.proto.testing.HybridEncryptResponse in project tink by google.
the class HybridServiceImpl method encrypt.
/**
* Encrypts a message.
*/
@Override
public void encrypt(HybridEncryptRequest request, StreamObserver<HybridEncryptResponse> responseObserver) {
HybridEncryptResponse response;
try {
KeysetHandle publicKeysetHandle = CleartextKeysetHandle.read(BinaryKeysetReader.withBytes(request.getPublicKeyset().toByteArray()));
HybridEncrypt hybridEncrypt = publicKeysetHandle.getPrimitive(HybridEncrypt.class);
byte[] ciphertext = hybridEncrypt.encrypt(request.getPlaintext().toByteArray(), request.getContextInfo().toByteArray());
response = HybridEncryptResponse.newBuilder().setCiphertext(ByteString.copyFrom(ciphertext)).build();
} catch (GeneralSecurityException | InvalidProtocolBufferException e) {
response = HybridEncryptResponse.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.HybridEncryptResponse in project tink by google.
the class AsymmetricTestingServicesTest method hybridGenerateEncryptDecrypt_success.
@Test
public void hybridGenerateEncryptDecrypt_success() throws Exception {
byte[] template = KeyTemplateProtoConverter.toByteArray(EciesAeadHkdfPrivateKeyManager.eciesP256HkdfHmacSha256Aes128GcmTemplate());
byte[] plaintext = "The quick brown fox jumps over the lazy dog".getBytes(UTF_8);
byte[] associatedData = "generate_encrypt_decrypt".getBytes(UTF_8);
KeysetGenerateResponse genResponse = generateKeyset(keysetStub, template);
assertThat(genResponse.getErr()).isEmpty();
byte[] privateKeyset = genResponse.getKeyset().toByteArray();
KeysetPublicResponse pubResponse = publicKeyset(keysetStub, privateKeyset);
assertThat(pubResponse.getErr()).isEmpty();
byte[] publicKeyset = pubResponse.getPublicKeyset().toByteArray();
HybridEncryptResponse encResponse = hybridEncrypt(hybridStub, publicKeyset, plaintext, associatedData);
assertThat(encResponse.getErr()).isEmpty();
byte[] ciphertext = encResponse.getCiphertext().toByteArray();
HybridDecryptResponse decResponse = hybridDecrypt(hybridStub, privateKeyset, ciphertext, associatedData);
assertThat(decResponse.getErr()).isEmpty();
byte[] output = decResponse.getPlaintext().toByteArray();
assertThat(output).isEqualTo(plaintext);
}
use of com.google.crypto.tink.proto.testing.HybridEncryptResponse in project tink by google.
the class AsymmetricTestingServicesTest method hybridDecrypt_failsOnBadKeyset.
@Test
public void hybridDecrypt_failsOnBadKeyset() throws Exception {
byte[] template = KeyTemplateProtoConverter.toByteArray(EciesAeadHkdfPrivateKeyManager.eciesP256HkdfHmacSha256Aes128GcmTemplate());
byte[] plaintext = "The quick brown fox jumps over the lazy dog".getBytes(UTF_8);
byte[] contextInfo = "hybrid_decrypt_bad_keyset".getBytes(UTF_8);
KeysetGenerateResponse privateKeysetResponse = generateKeyset(keysetStub, template);
assertThat(privateKeysetResponse.getErr()).isEmpty();
byte[] privateKeyset = privateKeysetResponse.getKeyset().toByteArray();
KeysetPublicResponse pubResponse = publicKeyset(keysetStub, privateKeyset);
assertThat(pubResponse.getErr()).isEmpty();
byte[] publicKeyset = pubResponse.getPublicKeyset().toByteArray();
HybridEncryptResponse encResponse = hybridEncrypt(hybridStub, publicKeyset, plaintext, contextInfo);
assertThat(encResponse.getErr()).isEmpty();
byte[] ciphertext = encResponse.getCiphertext().toByteArray();
byte[] badKeyset = "bad keyset".getBytes(UTF_8);
HybridDecryptResponse decResponse = hybridDecrypt(hybridStub, badKeyset, ciphertext, contextInfo);
assertThat(decResponse.getErr()).isNotEmpty();
}
use of com.google.crypto.tink.proto.testing.HybridEncryptResponse in project tink by google.
the class AsymmetricTestingServicesTest method hybridEncrypt_failsOnBadKeyset.
@Test
public void hybridEncrypt_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[] contextInfo = "hybrid_encrypt_bad_keyset".getBytes(UTF_8);
HybridEncryptResponse encResponse = hybridEncrypt(hybridStub, badKeyset, plaintext, contextInfo);
assertThat(encResponse.getErr()).isNotEmpty();
}
Aggregations