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

SHA-256 algorithm. More...

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

Go to the source code of this file.

Data Structures

struct  sha256_digest
 An SHA-256 digest. More...
union  sha256_block
 An SHA-256 data block. More...
struct  sha256_digest_data
 SHA-256 digest and data block. More...
union  sha256_digest_data_dwords
 SHA-256 digest and data block. More...
struct  sha256_context
 An SHA-256 context. More...

Defines

#define SHA256_ROUNDS   64
 SHA-256 number of rounds.
#define SHA256_CTX_SIZE   sizeof ( struct sha256_context )
 SHA-256 context size.
#define SHA256_DIGEST_SIZE   sizeof ( struct sha256_digest )
 SHA-256 digest size.
#define SHA224_DIGEST_SIZE   ( SHA256_DIGEST_SIZE * 224 / 256 )
 SHA-224 digest size.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
struct sha256_digest_data __attribute__ ((packed))
void sha256_family_init (struct sha256_context *context, const struct sha256_digest *init, size_t digestsize)
 Initialise SHA-256 family algorithm.
void sha256_update (void *ctx, const void *data, size_t len)
 Accumulate data with SHA-256 algorithm.
void sha256_final (void *ctx, void *out)
 Generate SHA-256 digest.

Variables

struct sha256_digest digest
 Digest of data already processed.
union sha256_block data
 Accumulated data.
union sha256_digest_data_dwords __attribute__
size_t len
 Amount of accumulated data.
size_t digestsize
 Digest size.
union sha256_digest_data_dwords ddd
 Digest and accumulated data.
struct digest_algorithm sha256_algorithm
 SHA-256 algorithm.
struct digest_algorithm sha224_algorithm
 SHA-224 algorithm.

Detailed Description

SHA-256 algorithm.

Definition in file sha256.h.


Define Documentation

#define SHA256_ROUNDS   64

SHA-256 number of rounds.

Definition at line 16 of file sha256.h.

Referenced by sha256_digest().

#define SHA256_CTX_SIZE   sizeof ( struct sha256_context )

SHA-256 context size.

Definition at line 71 of file sha256.h.

#define SHA256_DIGEST_SIZE   sizeof ( struct sha256_digest )

SHA-256 digest size.

Definition at line 74 of file sha256.h.

#define SHA224_DIGEST_SIZE   ( SHA256_DIGEST_SIZE * 224 / 256 )

SHA-224 digest size.

Definition at line 77 of file sha256.h.

Referenced by sha224_init().


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )
struct sha256_digest_data __attribute__ ( (packed)  )
void sha256_family_init ( struct sha256_context context,
const struct sha256_digest init,
size_t  digestsize 
)

Initialise SHA-256 family algorithm.

Parameters:
contextSHA-256 context
initInitial digest values
digestsizeDigest size

Definition at line 93 of file sha256.c.

References sha256_digest_data_dwords::dd, sha256_context::ddd, sha256_digest_data::digest, sha256_context::digestsize, digestsize, sha256_context::len, and memcpy().

Referenced by sha224_init(), and sha256_init().

                                              {

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

Accumulate data with SHA-256 algorithm.

Parameters:
ctxSHA-256 context
dataData
lenLength of data

Definition at line 217 of file sha256.c.

References sha256_block::byte, byte, context, ctx, sha256_digest_data::data, data, sha256_digest_data_dwords::dd, sha256_context::ddd, sha256_context::len, offset, and sha256_digest().

Referenced by sha256_final().

                                                               {
        struct sha256_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->ddd.dd.data ) );
                context->ddd.dd.data.byte[offset] = *(byte++);
                context->len++;
                if ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 )
                        sha256_digest ( context );
        }
}
void sha256_final ( void *  ctx,
void *  out 
)

Generate SHA-256 digest.

Parameters:
ctxSHA-256 context
outOutput buffer

Definition at line 240 of file sha256.c.

References assert, context, cpu_to_be64, ctx, sha256_digest_data::data, sha256_digest_data_dwords::dd, sha256_context::ddd, sha256_digest_data::digest, sha256_context::digestsize, sha256_context::len, memcpy(), offsetof, pad, and sha256_update().

                                           {
        struct sha256_context *context = ctx;
        uint64_t len_bits;
        uint8_t pad;

        /* Record length before pre-processing */
        len_bits = 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 {
                sha256_update ( ctx, &pad, sizeof ( pad ) );
                pad = 0x00;
        } while ( ( context->len % sizeof ( context->ddd.dd.data ) ) !=
                  offsetof ( typeof ( context->ddd.dd.data ), final.len ) );

        /* Append length (in bits) */
        sha256_update ( ctx, &len_bits, sizeof ( len_bits ) );
        assert ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 );

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

Variable Documentation

Digest of data already processed.

Definition at line 51 of file sha256.h.

Accumulated data.

Definition at line 53 of file sha256.h.

Amount of accumulated data.

Definition at line 70 of file sha256.h.

Digest size.

Definition at line 72 of file sha256.h.

Digest and accumulated data.

Definition at line 74 of file sha256.h.

SHA-256 algorithm.

Definition at line 265 of file sha256.c.

Referenced by peerdist_info_v1(), sha256_test_exec(), tls_add_handshake(), tls_prf(), and tls_restart().

SHA-224 algorithm.

Definition at line 64 of file sha224.c.

Referenced by sha256_test_exec().