iPXE
Data Structures | Defines | Functions | Variables
sha512.h File Reference

SHA-512 algorithm. More...

#include <stdint.h>
#include <ipxe/crypto.h>

Go to the source code of this file.

Data Structures

struct  sha512_digest
 An SHA-512 digest. More...
union  sha512_block
 An SHA-512 data block. More...
struct  sha512_digest_data
 SHA-512 digest and data block. More...
union  sha512_digest_data_qwords
 SHA-512 digest and data block. More...
struct  sha512_context
 An SHA-512 context. More...

Defines

#define SHA512_ROUNDS   80
 SHA-512 number of rounds.
#define SHA512_CTX_SIZE   sizeof ( struct sha512_context )
 SHA-512 context size.
#define SHA512_DIGEST_SIZE   sizeof ( struct sha512_digest )
 SHA-512 digest size.
#define SHA384_DIGEST_SIZE   ( SHA512_DIGEST_SIZE * 384 / 512 )
 SHA-384 digest size.
#define SHA512_256_DIGEST_SIZE   ( SHA512_DIGEST_SIZE * 256 / 512 )
 SHA-512/256 digest size.
#define SHA512_224_DIGEST_SIZE   ( SHA512_DIGEST_SIZE * 224 / 512 )
 SHA-512/224 digest size.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
struct sha512_digest_data __attribute__ ((packed))
void sha512_family_init (struct sha512_context *context, const struct sha512_digest *init, size_t digestsize)
 Initialise SHA-512 family algorithm.
void sha512_update (void *ctx, const void *data, size_t len)
 Accumulate data with SHA-512 algorithm.
void sha512_final (void *ctx, void *out)
 Generate SHA-512 digest.

Variables

struct sha512_digest digest
 Digest of data already processed.
union sha512_block data
 Accumulated data.
union sha512_digest_data_qwords __attribute__
size_t len
 Amount of accumulated data.
size_t digestsize
 Digest size.
union sha512_digest_data_qwords ddq
 Digest and accumulated data.
struct digest_algorithm sha512_algorithm
 SHA-512 algorithm.
struct digest_algorithm sha384_algorithm
 SHA-384 algorithm.
struct digest_algorithm sha512_256_algorithm
 SHA-512/256 algorithm.
struct digest_algorithm sha512_224_algorithm
 SHA-512/224 algorithm.

Detailed Description

SHA-512 algorithm.

Definition in file sha512.h.


Define Documentation

#define SHA512_ROUNDS   80

SHA-512 number of rounds.

Definition at line 16 of file sha512.h.

Referenced by sha512_digest().

#define SHA512_CTX_SIZE   sizeof ( struct sha512_context )

SHA-512 context size.

Definition at line 73 of file sha512.h.

#define SHA512_DIGEST_SIZE   sizeof ( struct sha512_digest )

SHA-512 digest size.

Definition at line 76 of file sha512.h.

#define SHA384_DIGEST_SIZE   ( SHA512_DIGEST_SIZE * 384 / 512 )

SHA-384 digest size.

Definition at line 79 of file sha512.h.

#define SHA512_256_DIGEST_SIZE   ( SHA512_DIGEST_SIZE * 256 / 512 )

SHA-512/256 digest size.

Definition at line 82 of file sha512.h.

Referenced by sha512_256_init().

#define SHA512_224_DIGEST_SIZE   ( SHA512_DIGEST_SIZE * 224 / 512 )

SHA-512/224 digest size.

Definition at line 85 of file sha512.h.

Referenced by sha512_224_init().


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )
struct sha512_digest_data __attribute__ ( (packed)  )
void sha512_family_init ( struct sha512_context context,
const struct sha512_digest init,
size_t  digestsize 
)

Initialise SHA-512 family algorithm.

Parameters:
contextSHA-512 context
initInitial digest values
digestsizeDigest size

Definition at line 109 of file sha512.c.

References sha512_digest_data_qwords::dd, sha512_context::ddq, sha512_digest_data::digest, digestsize, sha512_context::digestsize, sha512_context::len, and memcpy().

Referenced by sha384_init(), sha512_224_init(), sha512_256_init(), and sha512_init().

                                              {

        context->len = 0;
        context->digestsize = digestsize;
        memcpy ( &context->ddq.dd.digest, init,
                 sizeof ( context->ddq.dd.digest ) );
}
void sha512_update ( void *  ctx,
const void *  data,
size_t  len 
)

Accumulate data with SHA-512 algorithm.

Parameters:
ctxSHA-512 context
dataData
lenLength of data

Definition at line 234 of file sha512.c.

References sha512_block::byte, byte, context, ctx, sha512_digest_data::data, data, sha512_digest_data_qwords::dd, sha512_context::ddq, sha512_context::len, offset, and sha512_digest().

Referenced by sha512_final().

                                                               {
        struct sha512_context *context = ctx;
        const uint8_t *byte = data;
        size_t offset;

        /* Accumulate data a byte at a time, performing the digest
         * whenever we fill the data buffer
         */
        while ( len-- ) {
                offset = ( context->len % sizeof ( context->ddq.dd.data ) );
                context->ddq.dd.data.byte[offset] = *(byte++);
                context->len++;
                if ( ( context->len % sizeof ( context->ddq.dd.data ) ) == 0 )
                        sha512_digest ( context );
        }
}
void sha512_final ( void *  ctx,
void *  out 
)

Generate SHA-512 digest.

Parameters:
ctxSHA-512 context
outOutput buffer

Definition at line 257 of file sha512.c.

References assert, context, cpu_to_be64, ctx, sha512_digest_data::data, sha512_digest_data_qwords::dd, sha512_context::ddq, sha512_digest_data::digest, sha512_context::digestsize, sha512_context::len, memcpy(), offsetof, pad, and sha512_update().

                                           {
        struct sha512_context *context = ctx;
        uint64_t len_bits_hi;
        uint64_t len_bits_lo;
        uint8_t pad;

        /* Record length before pre-processing */
        len_bits_hi = 0;
        len_bits_lo = cpu_to_be64 ( ( ( uint64_t ) context->len ) * 8 );

        /* Pad with a single "1" bit followed by as many "0" bits as required */
        pad = 0x80;
        do {
                sha512_update ( ctx, &pad, sizeof ( pad ) );
                pad = 0x00;
        } while ( ( context->len % sizeof ( context->ddq.dd.data ) ) !=
                  offsetof ( typeof ( context->ddq.dd.data ), final.len_hi ) );

        /* Append length (in bits) */
        sha512_update ( ctx, &len_bits_hi, sizeof ( len_bits_hi ) );
        sha512_update ( ctx, &len_bits_lo, sizeof ( len_bits_lo ) );
        assert ( ( context->len % sizeof ( context->ddq.dd.data ) ) == 0 );

        /* Copy out final digest */
        memcpy ( out, &context->ddq.dd.digest, context->digestsize );
}

Variable Documentation

Digest of data already processed.

Definition at line 53 of file sha512.h.

Accumulated data.

Definition at line 55 of file sha512.h.

Amount of accumulated data.

Definition at line 72 of file sha512.h.

Digest size.

Definition at line 74 of file sha512.h.

Digest and accumulated data.

Definition at line 76 of file sha512.h.

SHA-512 algorithm.

Definition at line 285 of file sha512.c.

Referenced by peerdist_info_v1(), peerdist_info_v2(), and sha512_test_exec().

SHA-384 algorithm.

Definition at line 64 of file sha384.c.

Referenced by peerdist_info_v1(), and sha512_test_exec().

SHA-512/256 algorithm.

Definition at line 65 of file sha512_256.c.

Referenced by sha512_test_exec().

SHA-512/224 algorithm.

Definition at line 65 of file sha512_224.c.

Referenced by sha512_test_exec().