Refactor SHA2 algorithms, add basic tests

The algorithms now share most of their implementation code, and more
importantly, they generate correct output.

Test data is taken from RFC 4321: "Identifiers and Test Vectors for
HMAC-SHA-224, HMAC-SHA-256, HMAC-SHA-384, and HMAC-SHA-512"

Fixes #18221, #17733

(cherry picked from commit f8273c955e892ec21fbe10322508ddb362051a60)