[crypto] Add SHA-224 algorithm

SHA-224 is almost identical to SHA-256, with differing initial hash
values and a truncated output length.

This implementation has been verified using the NIST SHA-224 test
vectors.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
Michael Brown
2015-04-12 14:50:18 +01:00
parent a9da129122
commit 4dbc44348c
5 changed files with 167 additions and 16 deletions

View File

@@ -25,11 +25,12 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
/** @file
*
* SHA-256 tests
* SHA-256 family tests
*
* NIST test vectors are taken from
*
* http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA256.pdf
* http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA224.pdf
*
*/
@@ -61,8 +62,29 @@ DIGEST_TEST ( sha256_nist_abc_opq, &sha256_algorithm, DIGEST_NIST_ABC_OPQ,
0xe4, 0x59, 0x64, 0xff, 0x21, 0x67, 0xf6, 0xec, 0xed,
0xd4, 0x19, 0xdb, 0x06, 0xc1 ) );
/* Empty test vector (digest obtained from "sha224sum /dev/null") */
DIGEST_TEST ( sha224_empty, &sha224_algorithm, DIGEST_EMPTY,
DIGEST ( 0xd1, 0x4a, 0x02, 0x8c, 0x2a, 0x3a, 0x2b, 0xc9, 0x47,
0x61, 0x02, 0xbb, 0x28, 0x82, 0x34, 0xc4, 0x15, 0xa2,
0xb0, 0x1f, 0x82, 0x8e, 0xa6, 0x2a, 0xc5, 0xb3, 0xe4,
0x2f ) );
/* NIST test vector "abc" */
DIGEST_TEST ( sha224_nist_abc, &sha224_algorithm, DIGEST_NIST_ABC,
DIGEST ( 0x23, 0x09, 0x7d, 0x22, 0x34, 0x05, 0xd8, 0x22, 0x86,
0x42, 0xa4, 0x77, 0xbd, 0xa2, 0x55, 0xb3, 0x2a, 0xad,
0xbc, 0xe4, 0xbd, 0xa0, 0xb3, 0xf7, 0xe3, 0x6c, 0x9d,
0xa7 ) );
/* NIST test vector "abc...opq" */
DIGEST_TEST ( sha224_nist_abc_opq, &sha224_algorithm, DIGEST_NIST_ABC_OPQ,
DIGEST ( 0x75, 0x38, 0x8b, 0x16, 0x51, 0x27, 0x76, 0xcc, 0x5d,
0xba, 0x5d, 0xa1, 0xfd, 0x89, 0x01, 0x50, 0xb0, 0xc6,
0x45, 0x5c, 0xb4, 0xf5, 0x8b, 0x19, 0x52, 0x52, 0x25,
0x25 ) );
/**
* Perform SHA-256 self-test
* Perform SHA-256 family self-test
*
*/
static void sha256_test_exec ( void ) {
@@ -71,13 +93,18 @@ static void sha256_test_exec ( void ) {
digest_ok ( &sha256_empty );
digest_ok ( &sha256_nist_abc );
digest_ok ( &sha256_nist_abc_opq );
digest_ok ( &sha224_empty );
digest_ok ( &sha224_nist_abc );
digest_ok ( &sha224_nist_abc_opq );
/* Speed tests */
DBG ( "SHA256 required %ld cycles per byte\n",
digest_cost ( &sha256_algorithm ) );
DBG ( "SHA224 required %ld cycles per byte\n",
digest_cost ( &sha224_algorithm ) );
}
/** SHA-256 self-test */
/** SHA-256 family self-test */
struct self_test sha256_test __self_test = {
.name = "sha256",
.exec = sha256_test_exec,