iPXE
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...

Macros

#define SHA256_ROUNDS   64
 SHA-256 number of rounds.
#define SHA256_CTX_SIZE   sizeof ( struct sha256_context )
 SHA-256 context size.
#define SHA256_BLOCK_SIZE   sizeof ( union sha256_block )
 SHA-256 block 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)
 FILE_SECBOOT (PERMITTED)
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 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.

Macro Definition Documentation

◆ SHA256_ROUNDS

#define SHA256_ROUNDS   64

SHA-256 number of rounds.

Definition at line 17 of file sha256.h.

Referenced by sha256_digest().

◆ SHA256_CTX_SIZE

#define SHA256_CTX_SIZE   sizeof ( struct sha256_context )

SHA-256 context size.

Definition at line 72 of file sha256.h.

Referenced by icert_cert().

◆ SHA256_BLOCK_SIZE

#define SHA256_BLOCK_SIZE   sizeof ( union sha256_block )

SHA-256 block size.

Definition at line 75 of file sha256.h.

◆ SHA256_DIGEST_SIZE

#define SHA256_DIGEST_SIZE   sizeof ( struct sha256_digest )

SHA-256 digest size.

Definition at line 78 of file sha256.h.

◆ SHA224_DIGEST_SIZE

#define SHA224_DIGEST_SIZE   ( SHA256_DIGEST_SIZE * 224 / 256 )

SHA-224 digest size.

Definition at line 81 of file sha256.h.

Referenced by sha224_init().

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED )

◆ sha256_family_init()

void sha256_family_init ( struct sha256_context * context,
const struct sha256_digest * init,
size_t digestsize )
extern

Initialise SHA-256 family algorithm.

Parameters
contextSHA-256 context
initInitial digest values
digestsizeDigest size

Definition at line 93 of file sha256.c.

95 {
96
97 context->len = 0;
98 context->digestsize = digestsize;
99 memcpy ( &context->ddd.dd.digest, init,
100 sizeof ( context->ddd.dd.digest ) );
101}
void * memcpy(void *dest, const void *src, size_t len) __nonnull
uint32_t digestsize
Digest size (i.e.
Definition pccrr.h:1
size_t digestsize
Digest size.
Definition sha256.h:66
size_t len
Amount of accumulated data.
Definition sha256.h:64
union sha256_digest_data_dwords ddd
Digest and accumulated data.
Definition sha256.h:68
struct sha256_digest digest
Digest of data already processed.
Definition sha256.h:47
struct sha256_digest_data dd
Digest and data block.
Definition sha256.h:55

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

Referenced by sha224_init(), and sha256_init().

◆ sha256_update()

void sha256_update ( void * ctx,
const void * data,
size_t len )
extern

Accumulate data with SHA-256 algorithm.

Parameters
ctxSHA-256 context
dataData
lenLength of data

Definition at line 217 of file sha256.c.

217 {
218 struct sha256_context *context = ctx;
219 const uint8_t *byte = data;
220 size_t offset;
221
222 /* Accumulate data a byte at a time, performing the digest
223 * whenever we fill the data buffer
224 */
225 while ( len-- ) {
226 offset = ( context->len % sizeof ( context->ddd.dd.data ) );
227 context->ddd.dd.data.byte[offset] = *(byte++);
228 context->len++;
229 if ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 )
230 sha256_digest ( context );
231 }
232}
struct golan_eq_context ctx
Definition CIB_PRM.h:0
unsigned char uint8_t
Definition stdint.h:10
uint16_t offset
Offset to command line.
Definition bzimage.h:3
ring len
Length.
Definition dwmac.h:226
uint8_t data[48]
Additional event data.
Definition ena.h:11
static void sha256_digest(struct sha256_context *context)
Calculate SHA-256 digest of accumulated data.
Definition sha256.c:120
An SHA-256 context.
Definition sha256.h:62
union sha256_block data
Accumulated data.
Definition sha256.h:49
uint8_t byte[64]
Raw bytes.
Definition sha256.h:28

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

Referenced by sha256_final().

◆ sha256_final()

void sha256_final ( void * ctx,
void * out )
extern

Generate SHA-256 digest.

Parameters
ctxSHA-256 context
outOutput buffer

Definition at line 240 of file sha256.c.

240 {
241 struct sha256_context *context = ctx;
242 uint64_t len_bits;
243 uint8_t pad;
244
245 /* Record length before pre-processing */
246 len_bits = cpu_to_be64 ( ( ( uint64_t ) context->len ) * 8 );
247
248 /* Pad with a single "1" bit followed by as many "0" bits as required */
249 pad = 0x80;
250 do {
251 sha256_update ( ctx, &pad, sizeof ( pad ) );
252 pad = 0x00;
253 } while ( ( context->len % sizeof ( context->ddd.dd.data ) ) !=
254 offsetof ( typeof ( context->ddd.dd.data ), final.len ) );
255
256 /* Append length (in bits) */
257 sha256_update ( ctx, &len_bits, sizeof ( len_bits ) );
258 assert ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 );
259
260 /* Copy out final digest */
261 memcpy ( out, &context->ddd.dd.digest, context->digestsize );
262}
__be32 out[4]
Definition CIB_PRM.h:8
typeof(acpi_finder=acpi_find)
ACPI table finder.
Definition acpi.c:48
u32 pad[9]
Padding.
Definition ar9003_mac.h:23
unsigned long long uint64_t
Definition stdint.h:13
#define assert(condition)
Assert a condition at run-time.
Definition assert.h:50
#define cpu_to_be64(value)
Definition byteswap.h:112
void sha256_update(void *ctx, const void *data, size_t len)
Accumulate data with SHA-256 algorithm.
Definition sha256.c:217
#define offsetof(type, field)
Get offset of a field within a structure.
Definition stddef.h:25

References assert, 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, out, pad, sha256_update(), and typeof().

Variable Documentation

◆ sha256_algorithm

struct digest_algorithm sha256_algorithm
extern

SHA-256 algorithm.

Definition at line 265 of file sha256.c.

265 {
266 .name = "sha256",
267 .ctxsize = sizeof ( struct sha256_context ),
268 .blocksize = sizeof ( union sha256_block ),
269 .digestsize = sizeof ( struct sha256_digest ),
270 .init = sha256_init,
271 .update = sha256_update,
272 .final = sha256_final,
273};
static void sha256_init(void *ctx)
Initialise SHA-256 algorithm.
Definition sha256.c:108
void sha256_final(void *ctx, void *out)
Generate SHA-256 digest.
Definition sha256.c:240
An SHA-256 digest.
Definition sha256.h:20
An SHA-256 data block.
Definition sha256.h:26

Referenced by __tls_cipher_suite(), __tls_cipher_suite(), __tls_cipher_suite(), __tls_cipher_suite(), __tls_cipher_suite(), __tls_cipher_suite(), __tls_cipher_suite(), __tls_cipher_suite(), __tls_cipher_suite(), __tls_cipher_suite(), __tls_cipher_suite(), __tls_cipher_suite(), __tls_cipher_suite(), __tls_cipher_suite(), DIGEST_TEST(), DIGEST_TEST(), DIGEST_TEST(), HASH_DF_TEST(), HASH_DF_TEST(), HASH_DF_TEST(), HASH_DF_TEST(), HASH_DF_TEST(), HASH_DF_TEST(), HASH_DF_TEST(), HASH_DF_TEST(), HASH_DF_TEST(), HASH_DF_TEST(), HASH_DF_TEST(), HASH_DF_TEST(), HASH_DF_TEST(), HASH_DF_TEST(), HASH_DF_TEST(), HASH_DF_TEST(), HASH_DF_TEST(), HASH_DF_TEST(), HASH_DF_TEST(), HASH_DF_TEST(), HMAC_TEST(), HMAC_TEST(), HMAC_TEST(), HMAC_TEST(), icert_cert(), PEERDIST_INFO_TEST(), peerdist_info_v1(), PUBKEY_SIGN_TEST(), PUBKEY_SIGN_TEST(), PUBKEY_SIGN_TEST(), sha256_test_exec(), and sha256sum_exec().

◆ sha224_algorithm

struct digest_algorithm sha224_algorithm
extern

SHA-224 algorithm.

Definition at line 64 of file sha224.c.

64 {
65 .name = "sha224",
66 .ctxsize = sizeof ( struct sha256_context ),
67 .blocksize = sizeof ( union sha256_block ),
69 .init = sha224_init,
70 .update = sha256_update,
71 .final = sha256_final,
72};
static void sha224_init(void *ctx)
Initialise SHA-224 algorithm.
Definition sha224.c:57
#define SHA224_DIGEST_SIZE
SHA-224 digest size.
Definition sha256.h:81

Referenced by DIGEST_TEST(), DIGEST_TEST(), DIGEST_TEST(), sha224sum_exec(), and sha256_test_exec().