AU: Add support for signing of update payloads after they're generated.

The change adds 2 methods -- one that takes an unsigned payload and a raw
signature size and returns the hash that needs to be signed, and another one
that takes an unsigned payload and a raw signature and generates the signed
payload.

BUG=chromium-os:10872
TEST=unit tests

Change-Id: I65bbe72a1ec67e603e78508c33893695b7de0e6a

Review URL: http://codereview.chromium.org/6265001
diff --git a/delta_diff_generator.cc b/delta_diff_generator.cc
index 4f71c49..0d1e4ce 100644
--- a/delta_diff_generator.cc
+++ b/delta_diff_generator.cc
@@ -1426,26 +1426,11 @@
   // Signatures appear at the end of the blobs. Note the offset in the
   // manifest
   if (!private_key_path.empty()) {
-    LOG(INFO) << "Making room for signature in file";
-    manifest.set_signatures_offset(next_blob_offset);
-    LOG(INFO) << "set? " << manifest.has_signatures_offset();
-    // Add a dummy op at the end to appease older clients
-    DeltaArchiveManifest_InstallOperation* dummy_op =
-        manifest.add_kernel_install_operations();
-    dummy_op->set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE);
-    dummy_op->set_data_offset(next_blob_offset);
-    manifest.set_signatures_offset(next_blob_offset);
     uint64_t signature_blob_length = 0;
     TEST_AND_RETURN_FALSE(
         PayloadSigner::SignatureBlobLength(private_key_path,
                                            &signature_blob_length));
-    dummy_op->set_data_length(signature_blob_length);
-    manifest.set_signatures_size(signature_blob_length);
-    Extent* dummy_extent = dummy_op->add_dst_extents();
-    // Tell the dummy op to write this data to a big sparse hole
-    dummy_extent->set_start_block(kSparseHole);
-    dummy_extent->set_num_blocks((signature_blob_length + kBlockSize - 1) /
-                                 kBlockSize);
+    AddSignatureOp(next_blob_offset, signature_blob_length, &manifest);
   }
 
   TEST_AND_RETURN_FALSE(InitializePartitionInfos(old_kernel_part,
@@ -1597,6 +1582,27 @@
   return true;
 }
 
+void DeltaDiffGenerator::AddSignatureOp(uint64_t signature_blob_offset,
+                                        uint64_t signature_blob_length,
+                                        DeltaArchiveManifest* manifest) {
+  LOG(INFO) << "Making room for signature in file";
+  manifest->set_signatures_offset(signature_blob_offset);
+  LOG(INFO) << "set? " << manifest->has_signatures_offset();
+  // Add a dummy op at the end to appease older clients
+  DeltaArchiveManifest_InstallOperation* dummy_op =
+      manifest->add_kernel_install_operations();
+  dummy_op->set_type(DeltaArchiveManifest_InstallOperation_Type_REPLACE);
+  dummy_op->set_data_offset(signature_blob_offset);
+  manifest->set_signatures_offset(signature_blob_offset);
+  dummy_op->set_data_length(signature_blob_length);
+  manifest->set_signatures_size(signature_blob_length);
+  Extent* dummy_extent = dummy_op->add_dst_extents();
+  // Tell the dummy op to write this data to a big sparse hole
+  dummy_extent->set_start_block(kSparseHole);
+  dummy_extent->set_num_blocks((signature_blob_length + kBlockSize - 1) /
+                               kBlockSize);
+}
+
 const char* const kBsdiffPath = "bsdiff";
 const char* const kBspatchPath = "bspatch";
 const char* const kDeltaMagic = "CrAU";