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