absl::StatusOr KmsClient::RawEncrypt()

in common/kms_client.cc [303:341]


absl::StatusOr<kms_v1::RawEncryptResponse> KmsClient::RawEncrypt(
    kms_v1::RawEncryptRequest& request) const {
  grpc::ClientContext ctx;
  AddContextSettings(&ctx, "name", request.name());

  request.mutable_plaintext_crc32c()->set_value(
      ComputeCRC32C(request.plaintext()));
  request.mutable_additional_authenticated_data_crc32c()->set_value(
      ComputeCRC32C(request.additional_authenticated_data()));
  request.mutable_initialization_vector_crc32c()->set_value(
      ComputeCRC32C(request.initialization_vector()));

  kms_v1::RawEncryptResponse response;
  absl::Status rpc_result =
      ToStatus(kms_stub_->RawEncrypt(&ctx, request, &response));
  if (!rpc_result.ok()) {
    return DecorateStatus(rpc_result);
  }

  if (!CRC32CMatches(response.ciphertext(),
                     response.ciphertext_crc32c().value())) {
    rpc_result = absl::InternalError(absl::StrFormat(
        "at %s: the response crc32c did not match the expected checksum value",
        SOURCE_LOCATION.ToString()));
    return DecorateStatus(rpc_result);
  }

  if (!response.verified_plaintext_crc32c() ||
      !response.verified_additional_authenticated_data_crc32c() ||
      !response.verified_initialization_vector_crc32c()) {
    rpc_result = absl::InternalError(
        absl::StrFormat("at %s: the server did not verify the checksum values "
                        "provided in the request",
                        SOURCE_LOCATION.ToString()));
    return DecorateStatus(rpc_result);
  }

  return response;
}