diff --git a/src/crypto/sha512_256.c b/src/crypto/sha512_256.c new file mode 100644 index 00000000..f8afaf3e --- /dev/null +++ b/src/crypto/sha512_256.c @@ -0,0 +1,83 @@ +/* + * Copyright (C) 2015 Michael Brown . + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + * + * You can also choose to distribute this program under the terms of + * the Unmodified Binary Distribution Licence (as given in the file + * COPYING.UBDL), provided that you have satisfied its requirements. + */ + +FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL ); + +/** @file + * + * SHA-512/256 algorithm + * + */ + +#include +#include +#include +#include +#include + +/** SHA-512/256 initial digest values */ +static const struct sha512_digest sha512_256_init_digest = { + .h = { + cpu_to_be64 ( 0x22312194fc2bf72cULL ), + cpu_to_be64 ( 0x9f555fa3c84c64c2ULL ), + cpu_to_be64 ( 0x2393b86b6f53b151ULL ), + cpu_to_be64 ( 0x963877195940eabdULL ), + cpu_to_be64 ( 0x96283ee2a88effe3ULL ), + cpu_to_be64 ( 0xbe5e1e2553863992ULL ), + cpu_to_be64 ( 0x2b0199fc2c85b8aaULL ), + cpu_to_be64 ( 0x0eb72ddc81c52ca2ULL ), + }, +}; + +/** + * Initialise SHA-512/256 algorithm + * + * @v ctx SHA-512/256 context + */ +static void sha512_256_init ( void *ctx ) { + struct sha512_context *context = ctx; + + sha512_family_init ( context, &sha512_256_init_digest, + SHA512_256_DIGEST_SIZE ); +} + +/** SHA-512/256 algorithm */ +struct digest_algorithm sha512_256_algorithm = { + .name = "sha512/256", + .ctxsize = sizeof ( struct sha512_context ), + .blocksize = sizeof ( union sha512_block ), + .digestsize = SHA512_256_DIGEST_SIZE, + .init = sha512_256_init, + .update = sha512_update, + .final = sha512_final, +}; + +/** "sha512_256" object identifier */ +static uint8_t oid_sha512_256[] = { ASN1_OID_SHA512_256 }; + +/** "sha512_256" OID-identified algorithm */ +struct asn1_algorithm oid_sha512_256_algorithm __asn1_algorithm = { + .name = "sha512/256", + .digest = &sha512_256_algorithm, + .oid = ASN1_OID_CURSOR ( oid_sha512_256 ), +}; diff --git a/src/include/ipxe/asn1.h b/src/include/ipxe/asn1.h index 7550c474..f9ae3112 100644 --- a/src/include/ipxe/asn1.h +++ b/src/include/ipxe/asn1.h @@ -181,6 +181,13 @@ struct asn1_builder_header { ASN1_OID_SINGLE ( 3 ), ASN1_OID_SINGLE ( 4 ), \ ASN1_OID_SINGLE ( 2 ), ASN1_OID_SINGLE ( 4 ) +/** ASN.1 OID for id-sha512-256 (2.16.840.1.101.3.4.2.6) */ +#define ASN1_OID_SHA512_256 \ + ASN1_OID_INITIAL ( 2, 16 ), ASN1_OID_DOUBLE ( 840 ), \ + ASN1_OID_SINGLE ( 1 ), ASN1_OID_SINGLE ( 101 ), \ + ASN1_OID_SINGLE ( 3 ), ASN1_OID_SINGLE ( 4 ), \ + ASN1_OID_SINGLE ( 2 ), ASN1_OID_SINGLE ( 6 ) + /** ASN.1 OID for commonName (2.5.4.3) */ #define ASN1_OID_COMMON_NAME \ ASN1_OID_INITIAL ( 2, 5 ), ASN1_OID_SINGLE ( 4 ), \ diff --git a/src/include/ipxe/sha512.h b/src/include/ipxe/sha512.h index 44a4c830..8e9892bf 100644 --- a/src/include/ipxe/sha512.h +++ b/src/include/ipxe/sha512.h @@ -78,6 +78,9 @@ struct sha512_context { /** SHA-384 digest size */ #define SHA384_DIGEST_SIZE ( SHA512_DIGEST_SIZE * 384 / 512 ) +/** SHA-512/256 digest size */ +#define SHA512_256_DIGEST_SIZE ( SHA512_DIGEST_SIZE * 256 / 512 ) + extern void sha512_family_init ( struct sha512_context *context, const struct sha512_digest *init, size_t digestsize ); @@ -86,5 +89,6 @@ extern void sha512_final ( void *ctx, void *out ); extern struct digest_algorithm sha512_algorithm; extern struct digest_algorithm sha384_algorithm; +extern struct digest_algorithm sha512_256_algorithm; #endif /* IPXE_SHA512_H */ diff --git a/src/tests/sha512_test.c b/src/tests/sha512_test.c index 80dc2f4e..f4293d6a 100644 --- a/src/tests/sha512_test.c +++ b/src/tests/sha512_test.c @@ -31,6 +31,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL ); * * http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA512.pdf * http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA384.pdf + * http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA512_256.pdf * */ @@ -101,6 +102,28 @@ DIGEST_TEST ( sha384_nist_abc_stu, &sha384_algorithm, DIGEST_NIST_ABC_STU, 0x55, 0x7e, 0x2d, 0xb9, 0x66, 0xc3, 0xe9, 0xfa, 0x91, 0x74, 0x60, 0x39 ) ); +/* Empty test vector (digest obtained from "shasum -a 512256 /dev/null") */ +DIGEST_TEST ( sha512_256_empty, &sha512_256_algorithm, DIGEST_EMPTY, + DIGEST ( 0xc6, 0x72, 0xb8, 0xd1, 0xef, 0x56, 0xed, 0x28, 0xab, + 0x87, 0xc3, 0x62, 0x2c, 0x51, 0x14, 0x06, 0x9b, 0xdd, + 0x3a, 0xd7, 0xb8, 0xf9, 0x73, 0x74, 0x98, 0xd0, 0xc0, + 0x1e, 0xce, 0xf0, 0x96, 0x7a ) ); + +/* NIST test vector "abc" */ +DIGEST_TEST ( sha512_256_nist_abc, &sha512_256_algorithm, DIGEST_NIST_ABC, + DIGEST ( 0x53, 0x04, 0x8e, 0x26, 0x81, 0x94, 0x1e, 0xf9, 0x9b, + 0x2e, 0x29, 0xb7, 0x6b, 0x4c, 0x7d, 0xab, 0xe4, 0xc2, + 0xd0, 0xc6, 0x34, 0xfc, 0x6d, 0x46, 0xe0, 0xe2, 0xf1, + 0x31, 0x07, 0xe7, 0xaf, 0x23 ) ); + +/* NIST test vector "abc...stu" */ +DIGEST_TEST ( sha512_256_nist_abc_stu, &sha512_256_algorithm, + DIGEST_NIST_ABC_STU, + DIGEST ( 0x39, 0x28, 0xe1, 0x84, 0xfb, 0x86, 0x90, 0xf8, 0x40, + 0xda, 0x39, 0x88, 0x12, 0x1d, 0x31, 0xbe, 0x65, 0xcb, + 0x9d, 0x3e, 0xf8, 0x3e, 0xe6, 0x14, 0x6f, 0xea, 0xc8, + 0x61, 0xe1, 0x9b, 0x56, 0x3a ) ); + /** * Perform SHA-512 family self-test * @@ -114,12 +137,17 @@ static void sha512_test_exec ( void ) { digest_ok ( &sha384_empty ); digest_ok ( &sha384_nist_abc ); digest_ok ( &sha384_nist_abc_stu ); + digest_ok ( &sha512_256_empty ); + digest_ok ( &sha512_256_nist_abc ); + digest_ok ( &sha512_256_nist_abc_stu ); /* Speed tests */ DBG ( "SHA512 required %ld cycles per byte\n", digest_cost ( &sha512_algorithm ) ); DBG ( "SHA384 required %ld cycles per byte\n", digest_cost ( &sha384_algorithm ) ); + DBG ( "SHA512/256 required %ld cycles per byte\n", + digest_cost ( &sha512_256_algorithm ) ); } /** SHA-512 family self-test */