iPXE
sha256.h
Go to the documentation of this file.
00001 #ifndef _IPXE_SHA256_H
00002 #define _IPXE_SHA256_H
00003 
00004 /** @file
00005  *
00006  * SHA-256 algorithm
00007  *
00008  */
00009 
00010 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00011 
00012 #include <stdint.h>
00013 #include <ipxe/crypto.h>
00014 
00015 /** SHA-256 number of rounds */
00016 #define SHA256_ROUNDS 64
00017 
00018 /** An SHA-256 digest */
00019 struct sha256_digest {
00020         /** Hash output */
00021         uint32_t h[8];
00022 };
00023 
00024 /** An SHA-256 data block */
00025 union sha256_block {
00026         /** Raw bytes */
00027         uint8_t byte[64];
00028         /** Raw dwords */
00029         uint32_t dword[16];
00030         /** Final block structure */
00031         struct {
00032                 /** Padding */
00033                 uint8_t pad[56];
00034                 /** Length in bits */
00035                 uint64_t len;
00036         } final;
00037 };
00038 
00039 /** SHA-256 digest and data block
00040  *
00041  * The order of fields within this structure is designed to minimise
00042  * code size.
00043  */
00044 struct sha256_digest_data {
00045         /** Digest of data already processed */
00046         struct sha256_digest digest;
00047         /** Accumulated data */
00048         union sha256_block data;
00049 } __attribute__ (( packed ));
00050 
00051 /** SHA-256 digest and data block */
00052 union sha256_digest_data_dwords {
00053         /** Digest and data block */
00054         struct sha256_digest_data dd;
00055         /** Raw dwords */
00056         uint32_t dword[ sizeof ( struct sha256_digest_data ) /
00057                         sizeof ( uint32_t ) ];
00058 };
00059 
00060 /** An SHA-256 context */
00061 struct sha256_context {
00062         /** Amount of accumulated data */
00063         size_t len;
00064         /** Digest size */
00065         size_t digestsize;
00066         /** Digest and accumulated data */
00067         union sha256_digest_data_dwords ddd;
00068 } __attribute__ (( packed ));
00069 
00070 /** SHA-256 context size */
00071 #define SHA256_CTX_SIZE sizeof ( struct sha256_context )
00072 
00073 /** SHA-256 digest size */
00074 #define SHA256_DIGEST_SIZE sizeof ( struct sha256_digest )
00075 
00076 /** SHA-224 digest size */
00077 #define SHA224_DIGEST_SIZE ( SHA256_DIGEST_SIZE * 224 / 256 )
00078 
00079 extern void sha256_family_init ( struct sha256_context *context,
00080                                  const struct sha256_digest *init,
00081                                  size_t digestsize );
00082 extern void sha256_update ( void *ctx, const void *data, size_t len );
00083 extern void sha256_final ( void *ctx, void *out );
00084 
00085 extern struct digest_algorithm sha256_algorithm;
00086 extern struct digest_algorithm sha224_algorithm;
00087 
00088 #endif /* _IPXE_SHA256_H */