Adds functionality for instantiating and using `Algorithm`

This CL moves some methods that exist in every hashing algorithm
implementation to the `Algorithm` trait; namely, `new`, `update`,
and `digest`. This allows for clients to instantiate the hashing
algorithms, and to call `update` and `digest` on objects that
have this trait. It allows a generic representation of an algorithm
that can be used.
In order to avoid users of concrete instance of `Algorithm` to have
to import `Algorithm` and every time they want to
use the functions implemented, these functions are also included in
the inherent implementation of each algorithm (as it was originally
set up, except that now the methods call the equivalent method in the
Algorithm implementation'

This CL also adds the block size as a const field in the `Algorithm`
trait.

Note: this specifically supports the hashing algorithm use in the
style of what is done in AOSP's
`packages/modules/Virtualization/libs/apkverify`, which was previously
using the rust openssl `Hasher` to instantiate algorithms specified
by their `MessageDigest`.

Redoing Ic47691ee2a4303923519b246de7d9724da90f60d which has since
been reverted.

Change-Id: Ib7c02e79952491d814bb49a1ff8f23271b716414
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/69387
Commit-Queue: Ellen Arteca <emarteca@google.com>
Reviewed-by: Adam Langley <agl@google.com>
Commit-Queue: Adam Langley <agl@google.com>
diff --git a/rust/bssl-crypto/src/digest.rs b/rust/bssl-crypto/src/digest.rs
index 645ab80..0ef0e8b 100644
--- a/rust/bssl-crypto/src/digest.rs
+++ b/rust/bssl-crypto/src/digest.rs
@@ -41,10 +41,12 @@
     type CType = bssl_sys::EVP_MD;
 }
 
-/// Used internally to parameterize other primitives.
+/// Provides the ability to hash in an algorithm-agnostic manner.
 pub trait Algorithm {
     /// The size of the resulting digest.
     const OUTPUT_LEN: usize;
+    /// The block length (in bytes).
+    const BLOCK_LEN: usize;
 
     /// Gets a reference to a message digest algorithm to be used by the HKDF implementation.
     #[doc(hidden)]
@@ -52,6 +54,15 @@
 
     /// Hashes a message.
     fn hash_to_vec(input: &[u8]) -> Vec<u8>;
+
+    /// Create a new context for incremental hashing.
+    fn new() -> Self;
+
+    /// Hash the contents of `input`.
+    fn update(&mut self, input: &[u8]);
+
+    /// Finish the hashing and return the digest.
+    fn digest_to_vec(self) -> Vec<u8>;
 }
 
 /// The insecure SHA-1 hash algorithm.
@@ -67,6 +78,7 @@
 unsafe_iuf_algo!(
     InsecureSha1,
     20,
+    64,
     EVP_sha1,
     SHA1,
     SHA1_Init,
@@ -83,6 +95,7 @@
 unsafe_iuf_algo!(
     Sha256,
     32,
+    64,
     EVP_sha256,
     SHA256,
     SHA256_Init,
@@ -99,6 +112,7 @@
 unsafe_iuf_algo!(
     Sha384,
     48,
+    128,
     EVP_sha384,
     SHA384,
     SHA384_Init,
@@ -115,6 +129,7 @@
 unsafe_iuf_algo!(
     Sha512,
     64,
+    128,
     EVP_sha512,
     SHA512,
     SHA512_Init,
@@ -131,6 +146,7 @@
 unsafe_iuf_algo!(
     Sha512_256,
     32,
+    128,
     EVP_sha512_256,
     SHA512_256,
     SHA512_256_Init,
diff --git a/rust/bssl-crypto/src/macros.rs b/rust/bssl-crypto/src/macros.rs
index 6ac3d37..194aaac 100644
--- a/rust/bssl-crypto/src/macros.rs
+++ b/rust/bssl-crypto/src/macros.rs
@@ -22,9 +22,10 @@
 // Safety: see the "Safety" sections within about the requirements for the
 // functions named in the macro parameters.
 macro_rules! unsafe_iuf_algo {
-    ($name:ident, $output_len:expr, $evp_md:ident, $one_shot:ident, $init:ident, $update:ident, $final_func:ident) => {
+    ($name:ident, $output_len:expr, $block_len:expr, $evp_md:ident, $one_shot:ident, $init:ident, $update:ident, $final_func:ident) => {
         impl Algorithm for $name {
             const OUTPUT_LEN: usize = $output_len as usize;
+            const BLOCK_LEN: usize = $block_len as usize;
 
             fn get_md(_: sealed::Sealed) -> &'static MdRef {
                 // Safety:
@@ -35,6 +36,21 @@
             fn hash_to_vec(input: &[u8]) -> Vec<u8> {
                 Self::hash(input).as_slice().to_vec()
             }
+
+            /// Create a new context for incremental hashing.
+            fn new() -> Self {
+                $name::new()
+            }
+
+            /// Hash the contents of `input`.
+            fn update(&mut self, input: &[u8]) {
+                self.update(input);
+            }
+
+            /// Finish the hashing and return the digest.
+            fn digest_to_vec(self) -> alloc::vec::Vec<u8> {
+                self.digest().to_vec()
+            }
         }
 
         impl $name {
@@ -90,7 +106,7 @@
 
         impl From<$name> for alloc::vec::Vec<u8> {
             fn from(ctx: $name) -> alloc::vec::Vec<u8> {
-                ctx.digest().into()
+                ctx.digest_to_vec()
             }
         }