Search in sources :

Example 1 with KeysetReadEncryptedResponse

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

the class KeysetServiceImpl method readEncrypted.

@Override
public void readEncrypted(KeysetReadEncryptedRequest request, StreamObserver<KeysetReadEncryptedResponse> responseObserver) {
    KeysetReadEncryptedResponse response;
    try {
        // get masterAead
        KeysetHandle masterKeysetHandle = CleartextKeysetHandle.read(BinaryKeysetReader.withBytes(request.getMasterKeyset().toByteArray()));
        Aead masterAead = masterKeysetHandle.getPrimitive(Aead.class);
        // read encrypted keyset to keysetHandle
        KeysetReader reader = BinaryKeysetReader.withBytes(request.getEncryptedKeyset().toByteArray());
        KeysetHandle keysetHandle;
        if (request.hasAssociatedData()) {
            keysetHandle = KeysetHandle.readWithAssociatedData(reader, masterAead, request.getAssociatedData().getValue().toByteArray());
        } else {
            keysetHandle = KeysetHandle.read(reader, masterAead);
        }
        // get keyset from keysetHandle
        Keyset keyset = CleartextKeysetHandle.getKeyset(keysetHandle);
        ByteArrayOutputStream keysetStream = new ByteArrayOutputStream();
        BinaryKeysetWriter.withOutputStream(keysetStream).write(keyset);
        keysetStream.close();
        response = KeysetReadEncryptedResponse.newBuilder().setKeyset(ByteString.copyFrom(keysetStream.toByteArray())).build();
    } catch (GeneralSecurityException | InvalidProtocolBufferException e) {
        response = KeysetReadEncryptedResponse.newBuilder().setErr(e.toString()).build();
    } catch (IOException e) {
        responseObserver.onError(Status.UNKNOWN.withDescription(e.getMessage()).asException());
        return;
    }
    responseObserver.onNext(response);
    responseObserver.onCompleted();
}
Also used : KeysetHandle(com.google.crypto.tink.KeysetHandle) CleartextKeysetHandle(com.google.crypto.tink.CleartextKeysetHandle) Keyset(com.google.crypto.tink.proto.Keyset) GeneralSecurityException(java.security.GeneralSecurityException) KeysetReadEncryptedResponse(com.google.crypto.tink.proto.testing.KeysetReadEncryptedResponse) Aead(com.google.crypto.tink.Aead) BinaryKeysetReader(com.google.crypto.tink.BinaryKeysetReader) JsonKeysetReader(com.google.crypto.tink.JsonKeysetReader) KeysetReader(com.google.crypto.tink.KeysetReader) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 2 with KeysetReadEncryptedResponse

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

the class TestingServicesTest method encryptDecryptInvalidKeyset_fails.

@Test
public void encryptDecryptInvalidKeyset_fails() throws Exception {
    byte[] invalidData = "invalid".getBytes(UTF_8);
    byte[] template = KeyTemplateProtoConverter.toByteArray(KeyTemplates.get("AES128_GCM"));
    KeysetGenerateResponse keysetResponse = generateKeyset(keysetStub, template);
    assertThat(keysetResponse.getErr()).isEmpty();
    byte[] keyset = keysetResponse.getKeyset().toByteArray();
    KeysetGenerateResponse masterKeysetResponse = generateKeyset(keysetStub, template);
    assertThat(masterKeysetResponse.getErr()).isEmpty();
    byte[] masterKeyset = masterKeysetResponse.getKeyset().toByteArray();
    KeysetWriteEncryptedResponse writeResponse1 = keysetWriteEncrypted(keysetStub, keyset, invalidData, /*associatedData=*/
    Optional.empty());
    assertThat(writeResponse1.getErr()).isNotEmpty();
    KeysetWriteEncryptedResponse writeResponse2 = keysetWriteEncrypted(keysetStub, invalidData, masterKeyset, /*associatedData=*/
    Optional.empty());
    assertThat(writeResponse2.getErr()).isNotEmpty();
    KeysetReadEncryptedResponse readResponse1 = keysetReadEncrypted(keysetStub, keyset, invalidData, /*associatedData=*/
    Optional.empty());
    assertThat(readResponse1.getErr()).isNotEmpty();
    KeysetReadEncryptedResponse readResponse2 = keysetReadEncrypted(keysetStub, invalidData, masterKeyset, /*associatedData=*/
    Optional.empty());
    assertThat(readResponse2.getErr()).isNotEmpty();
}
Also used : KeysetWriteEncryptedResponse(com.google.crypto.tink.proto.testing.KeysetWriteEncryptedResponse) KeysetReadEncryptedResponse(com.google.crypto.tink.proto.testing.KeysetReadEncryptedResponse) KeysetGenerateResponse(com.google.crypto.tink.proto.testing.KeysetGenerateResponse) Test(org.junit.Test)

Example 3 with KeysetReadEncryptedResponse

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

the class TestingServicesTest method generateEncryptDecryptKeyset.

@Test
public void generateEncryptDecryptKeyset() throws Exception {
    byte[] template = KeyTemplateProtoConverter.toByteArray(KeyTemplates.get("AES128_GCM"));
    KeysetGenerateResponse keysetResponse = generateKeyset(keysetStub, template);
    assertThat(keysetResponse.getErr()).isEmpty();
    byte[] keyset = keysetResponse.getKeyset().toByteArray();
    KeysetGenerateResponse masterKeysetResponse = generateKeyset(keysetStub, template);
    assertThat(masterKeysetResponse.getErr()).isEmpty();
    byte[] masterKeyset = masterKeysetResponse.getKeyset().toByteArray();
    KeysetWriteEncryptedResponse writeResponse = keysetWriteEncrypted(keysetStub, keyset, masterKeyset, /*associatedData=*/
    Optional.empty());
    assertThat(writeResponse.getErr()).isEmpty();
    byte[] encryptedKeyset = writeResponse.getEncryptedKeyset().toByteArray();
    assertThat(encryptedKeyset).isNotEqualTo(keyset);
    KeysetReadEncryptedResponse readResponse = keysetReadEncrypted(keysetStub, encryptedKeyset, masterKeyset, /*associatedData=*/
    Optional.empty());
    assertThat(readResponse.getErr()).isEmpty();
    byte[] output = readResponse.getKeyset().toByteArray();
    assertThat(output).isEqualTo(keyset);
}
Also used : KeysetWriteEncryptedResponse(com.google.crypto.tink.proto.testing.KeysetWriteEncryptedResponse) KeysetReadEncryptedResponse(com.google.crypto.tink.proto.testing.KeysetReadEncryptedResponse) KeysetGenerateResponse(com.google.crypto.tink.proto.testing.KeysetGenerateResponse) Test(org.junit.Test)

Example 4 with KeysetReadEncryptedResponse

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

the class TestingServicesTest method generateEncryptDecryptKeysetWithAssociatedData.

@Test
public void generateEncryptDecryptKeysetWithAssociatedData() throws Exception {
    byte[] template = KeyTemplateProtoConverter.toByteArray(KeyTemplates.get("AES128_GCM"));
    byte[] associatedData = "a".getBytes(UTF_8);
    KeysetGenerateResponse keysetResponse = generateKeyset(keysetStub, template);
    assertThat(keysetResponse.getErr()).isEmpty();
    byte[] keyset = keysetResponse.getKeyset().toByteArray();
    KeysetGenerateResponse masterKeysetResponse = generateKeyset(keysetStub, template);
    assertThat(masterKeysetResponse.getErr()).isEmpty();
    byte[] masterKeyset = masterKeysetResponse.getKeyset().toByteArray();
    KeysetWriteEncryptedResponse writeResponse = keysetWriteEncrypted(keysetStub, keyset, masterKeyset, Optional.of(associatedData));
    assertThat(writeResponse.getErr()).isEmpty();
    byte[] encryptedKeyset = writeResponse.getEncryptedKeyset().toByteArray();
    assertThat(encryptedKeyset).isNotEqualTo(keyset);
    KeysetReadEncryptedResponse readResponse = keysetReadEncrypted(keysetStub, encryptedKeyset, masterKeyset, Optional.of(associatedData));
    assertThat(readResponse.getErr()).isEmpty();
    byte[] output = readResponse.getKeyset().toByteArray();
    assertThat(output).isEqualTo(keyset);
}
Also used : KeysetWriteEncryptedResponse(com.google.crypto.tink.proto.testing.KeysetWriteEncryptedResponse) KeysetReadEncryptedResponse(com.google.crypto.tink.proto.testing.KeysetReadEncryptedResponse) KeysetGenerateResponse(com.google.crypto.tink.proto.testing.KeysetGenerateResponse) Test(org.junit.Test)

Aggregations

KeysetReadEncryptedResponse (com.google.crypto.tink.proto.testing.KeysetReadEncryptedResponse)4 KeysetGenerateResponse (com.google.crypto.tink.proto.testing.KeysetGenerateResponse)3 KeysetWriteEncryptedResponse (com.google.crypto.tink.proto.testing.KeysetWriteEncryptedResponse)3 Test (org.junit.Test)3 Aead (com.google.crypto.tink.Aead)1 BinaryKeysetReader (com.google.crypto.tink.BinaryKeysetReader)1 CleartextKeysetHandle (com.google.crypto.tink.CleartextKeysetHandle)1 JsonKeysetReader (com.google.crypto.tink.JsonKeysetReader)1 KeysetHandle (com.google.crypto.tink.KeysetHandle)1 KeysetReader (com.google.crypto.tink.KeysetReader)1 Keyset (com.google.crypto.tink.proto.Keyset)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 GeneralSecurityException (java.security.GeneralSecurityException)1