From 6f713c2d959ab3280afc83e722d6589da2029108 Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Sun, 12 Apr 2015 15:42:45 +0100 Subject: [crypto] Add SHA-512 algorithm This implementation has been verified using the NIST SHA-512 test vectors. Signed-off-by: Michael Brown --- src/include/ipxe/asn1.h | 7 ++++ src/include/ipxe/sha256.h | 3 ++ src/include/ipxe/sha512.h | 86 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+) create mode 100644 src/include/ipxe/sha512.h (limited to 'src/include/ipxe') diff --git a/src/include/ipxe/asn1.h b/src/include/ipxe/asn1.h index 4e8605fe3..21cf809fe 100644 --- a/src/include/ipxe/asn1.h +++ b/src/include/ipxe/asn1.h @@ -160,6 +160,13 @@ struct asn1_builder_header { ASN1_OID_SINGLE ( 3 ), ASN1_OID_SINGLE ( 4 ), \ ASN1_OID_SINGLE ( 2 ), ASN1_OID_SINGLE ( 1 ) +/** ASN.1 OID for id-sha512 (2.16.840.1.101.3.4.2.3) */ +#define ASN1_OID_SHA512 \ + 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 ( 3 ) + /** ASN.1 OID for id-sha224 (2.16.840.1.101.3.4.2.4) */ #define ASN1_OID_SHA224 \ ASN1_OID_INITIAL ( 2, 16 ), ASN1_OID_DOUBLE ( 840 ), \ diff --git a/src/include/ipxe/sha256.h b/src/include/ipxe/sha256.h index 811279a64..e234cce33 100644 --- a/src/include/ipxe/sha256.h +++ b/src/include/ipxe/sha256.h @@ -12,6 +12,9 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL ); #include #include +/** SHA-256 number of rounds */ +#define SHA256_ROUNDS 64 + /** An SHA-256 digest */ struct sha256_digest { /** Hash output */ diff --git a/src/include/ipxe/sha512.h b/src/include/ipxe/sha512.h new file mode 100644 index 000000000..0cfa35b9e --- /dev/null +++ b/src/include/ipxe/sha512.h @@ -0,0 +1,86 @@ +#ifndef _IPXE_SHA512_H +#define _IPXE_SHA512_H + +/** @file + * + * SHA-512 algorithm + * + */ + +FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL ); + +#include +#include + +/** SHA-512 number of rounds */ +#define SHA512_ROUNDS 80 + +/** An SHA-512 digest */ +struct sha512_digest { + /** Hash output */ + uint64_t h[8]; +}; + +/** An SHA-512 data block */ +union sha512_block { + /** Raw bytes */ + uint8_t byte[128]; + /** Raw qwords */ + uint64_t qword[16]; + /** Final block structure */ + struct { + /** Padding */ + uint8_t pad[112]; + /** High 64 bits of length in bits */ + uint64_t len_hi; + /** Low 64 bits of length in bits */ + uint64_t len_lo; + } final; +}; + +/** SHA-512 digest and data block + * + * The order of fields within this structure is designed to minimise + * code size. + */ +struct sha512_digest_data { + /** Digest of data already processed */ + struct sha512_digest digest; + /** Accumulated data */ + union sha512_block data; +} __attribute__ (( packed )); + +/** SHA-512 digest and data block */ +union sha512_digest_data_qwords { + /** Digest and data block */ + struct sha512_digest_data dd; + /** Raw qwords */ + uint64_t qword[ sizeof ( struct sha512_digest_data ) / + sizeof ( uint64_t ) ]; +}; + +/** An SHA-512 context */ +struct sha512_context { + /** Amount of accumulated data */ + size_t len; + /** Digest size */ + size_t digestsize; + /** Digest and accumulated data */ + union sha512_digest_data_qwords ddq; +} __attribute__ (( packed )); + +/** SHA-512 context size */ +#define SHA512_CTX_SIZE sizeof ( struct sha512_context ) + +/** SHA-512 digest size */ +#define SHA512_DIGEST_SIZE sizeof ( struct sha512_digest ) + +extern void sha512_family_init ( struct sha512_context *context, + const struct sha512_digest *init, + size_t digestsize ); +extern void sha512_update ( void *ctx, const void *data, size_t len ); +extern void sha512_final ( void *ctx, void *out ); + +extern struct digest_algorithm sha512_algorithm; + +#endif /* IPXE_SHA512_H */ -- cgit v1.2.3-55-g7522