Search in sources :

Example 1 with HybridDecryptResponse

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

the class HybridServiceImpl method decrypt.

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

Example 2 with HybridDecryptResponse

use of com.google.crypto.tink.proto.testing.HybridDecryptResponse 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);
}
Also used : HybridEncryptResponse(com.google.crypto.tink.proto.testing.HybridEncryptResponse) HybridDecryptResponse(com.google.crypto.tink.proto.testing.HybridDecryptResponse) KeysetGenerateResponse(com.google.crypto.tink.proto.testing.KeysetGenerateResponse) KeysetPublicResponse(com.google.crypto.tink.proto.testing.KeysetPublicResponse) Test(org.junit.Test)

Example 3 with HybridDecryptResponse

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

the class AsymmetricTestingServicesTest method hybridDecrypt_failsOnBadCiphertext.

@Test
public void hybridDecrypt_failsOnBadCiphertext() throws Exception {
    byte[] template = KeyTemplateProtoConverter.toByteArray(EciesAeadHkdfPrivateKeyManager.eciesP256HkdfHmacSha256Aes128GcmTemplate());
    byte[] badCiphertext = "bad ciphertext".getBytes(UTF_8);
    byte[] contextInfo = "hybrid_decrypt_bad_ciphertext".getBytes(UTF_8);
    KeysetGenerateResponse keysetResponse = generateKeyset(keysetStub, template);
    assertThat(keysetResponse.getErr()).isEmpty();
    byte[] privateKeyset = keysetResponse.getKeyset().toByteArray();
    KeysetPublicResponse pubResponse = publicKeyset(keysetStub, privateKeyset);
    assertThat(pubResponse.getErr()).isEmpty();
    byte[] publicKeyset = pubResponse.getPublicKeyset().toByteArray();
    HybridDecryptResponse decResponse = hybridDecrypt(hybridStub, publicKeyset, badCiphertext, contextInfo);
    assertThat(decResponse.getErr()).isNotEmpty();
}
Also used : HybridDecryptResponse(com.google.crypto.tink.proto.testing.HybridDecryptResponse) KeysetGenerateResponse(com.google.crypto.tink.proto.testing.KeysetGenerateResponse) KeysetPublicResponse(com.google.crypto.tink.proto.testing.KeysetPublicResponse) Test(org.junit.Test)

Example 4 with HybridDecryptResponse

use of com.google.crypto.tink.proto.testing.HybridDecryptResponse 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();
}
Also used : HybridEncryptResponse(com.google.crypto.tink.proto.testing.HybridEncryptResponse) HybridDecryptResponse(com.google.crypto.tink.proto.testing.HybridDecryptResponse) KeysetGenerateResponse(com.google.crypto.tink.proto.testing.KeysetGenerateResponse) KeysetPublicResponse(com.google.crypto.tink.proto.testing.KeysetPublicResponse) Test(org.junit.Test)

Aggregations

HybridDecryptResponse (com.google.crypto.tink.proto.testing.HybridDecryptResponse)4 KeysetGenerateResponse (com.google.crypto.tink.proto.testing.KeysetGenerateResponse)3 KeysetPublicResponse (com.google.crypto.tink.proto.testing.KeysetPublicResponse)3 Test (org.junit.Test)3 HybridEncryptResponse (com.google.crypto.tink.proto.testing.HybridEncryptResponse)2 CleartextKeysetHandle (com.google.crypto.tink.CleartextKeysetHandle)1 HybridDecrypt (com.google.crypto.tink.HybridDecrypt)1 KeysetHandle (com.google.crypto.tink.KeysetHandle)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 IOException (java.io.IOException)1 GeneralSecurityException (java.security.GeneralSecurityException)1