pw_kvs: Update checksum interface

This change adds a Finish() function to the checksum class to support
checksums which have a final transformation after all the input data is
processed, such as AES CMAC.

Change-Id: I86ab091c1719ab5333de59c170e58634b6e9d86f
diff --git a/pw_kvs/checksum_test.cc b/pw_kvs/checksum_test.cc
index fbf228e..1939ff3 100644
--- a/pw_kvs/checksum_test.cc
+++ b/pw_kvs/checksum_test.cc
@@ -62,8 +62,9 @@
   crc_algo.Update(as_bytes(span(kString)));
   crc_algo.Reset();
 
-  EXPECT_EQ(crc_algo.state()[0], byte{0xFF});
-  EXPECT_EQ(crc_algo.state()[1], byte{0xFF});
+  span state = crc_algo.Finish();
+  EXPECT_EQ(state[0], byte{0xFF});
+  EXPECT_EQ(state[1], byte{0xFF});
 }
 
 }  // namespace
diff --git a/pw_kvs/format.cc b/pw_kvs/format.cc
index d39fe44..44abc6f 100644
--- a/pw_kvs/format.cc
+++ b/pw_kvs/format.cc
@@ -38,7 +38,7 @@
   if (algorithm != nullptr) {
     CalculateChecksum(algorithm, key, value);
     std::memcpy(&checksum_,
-                algorithm->state().data(),
+                algorithm->Finish().data(),
                 std::min(algorithm->size_bytes(), sizeof(checksum_)));
   }
 
diff --git a/pw_kvs/public/pw_kvs/checksum.h b/pw_kvs/public/pw_kvs/checksum.h
index b1f1658..c7c883a 100644
--- a/pw_kvs/public/pw_kvs/checksum.h
+++ b/pw_kvs/public/pw_kvs/checksum.h
@@ -33,8 +33,9 @@
     return Update(span(static_cast<const std::byte*>(data), size_bytes));
   }
 
-  // Returns the current state of the checksum algorithm.
-  constexpr const span<const std::byte>& state() const { return state_; }
+  // Returns the final result of the checksum. Update() can no longer be called
+  // after this. The returned span is valid until a call to Reset().
+  virtual span<const std::byte> Finish() = 0;
 
   // Returns the size of the checksum state.
   constexpr size_t size_bytes() const { return state_.size(); }
@@ -52,6 +53,9 @@
   // class, so that it is safe to have a non-virtual destructor.
   ~ChecksumAlgorithm() = default;
 
+  // Returns the current checksum state.
+  constexpr span<const std::byte> state() const { return state_; }
+
  private:
   span<const std::byte> state_;
 };
diff --git a/pw_kvs/public/pw_kvs/crc16_checksum.h b/pw_kvs/public/pw_kvs/crc16_checksum.h
index f4e3a9b..c2a0c7c 100644
--- a/pw_kvs/public/pw_kvs/crc16_checksum.h
+++ b/pw_kvs/public/pw_kvs/crc16_checksum.h
@@ -29,6 +29,8 @@
     crc_ = checksum::CcittCrc16(data, crc_);
   }
 
+  span<const std::byte> Finish() final { return state(); }
+
  private:
   uint16_t crc_ = checksum::kCcittCrc16DefaultInitialValue;
 };