iPXE
Data Structures | Macros | 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...
 

Macros

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

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. More...
 
void sha256_update (void *ctx, const void *data, size_t len)
 Accumulate data with SHA-256 algorithm. More...
 
void sha256_final (void *ctx, void *out)
 Generate SHA-256 digest. More...
 

Variables

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

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 16 of file sha256.h.

◆ SHA256_CTX_SIZE

#define SHA256_CTX_SIZE   sizeof ( struct sha256_context )

SHA-256 context size.

Definition at line 71 of file sha256.h.

◆ SHA256_BLOCK_SIZE

#define SHA256_BLOCK_SIZE   sizeof ( union sha256_block )

SHA-256 block size.

Definition at line 74 of file sha256.h.

◆ SHA256_DIGEST_SIZE

#define SHA256_DIGEST_SIZE   sizeof ( struct sha256_digest )

SHA-256 digest size.

Definition at line 77 of file sha256.h.

◆ SHA224_DIGEST_SIZE

#define SHA224_DIGEST_SIZE   ( SHA256_DIGEST_SIZE * 224 / 256 )

SHA-224 digest size.

Definition at line 80 of file sha256.h.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ __attribute__()

struct sha256_digest_data __attribute__ ( (packed)  )

◆ sha256_family_init()

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 92 of file sha256.c.

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

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 
)

Accumulate data with SHA-256 algorithm.

Parameters
ctxSHA-256 context
dataData
lenLength of data

Definition at line 216 of file sha256.c.

216  {
217  struct sha256_context *context = ctx;
218  const uint8_t *byte = data;
219  size_t offset;
220 
221  /* Accumulate data a byte at a time, performing the digest
222  * whenever we fill the data buffer
223  */
224  while ( len-- ) {
225  offset = ( context->len % sizeof ( context->ddd.dd.data ) );
226  context->ddd.dd.data.byte[offset] = *(byte++);
227  context->len++;
228  if ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 )
229  sha256_digest ( context );
230  }
231 }
static userptr_t size_t offset
Offset of the first segment within the content.
Definition: deflate.h:259
struct sha256_digest_data dd
Digest and data block.
Definition: sha256.h:54
size_t len
Amount of accumulated data.
Definition: sha256.h:63
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
unsigned char uint8_t
Definition: stdint.h:10
union sha256_block data
Accumulated data.
Definition: sha256.h:48
uint32_t len
Length.
Definition: ena.h:14
uint8_t data[48]
Additional event data.
Definition: ena.h:22
union sha256_digest_data_dwords ddd
Digest and accumulated data.
Definition: sha256.h:67
An SHA-256 context.
Definition: sha256.h:61
uint8_t byte[64]
Raw bytes.
Definition: sha256.h:27
static void sha256_digest(struct sha256_context *context)
Calculate SHA-256 digest of accumulated data.
Definition: sha256.c:119

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 
)

Generate SHA-256 digest.

Parameters
ctxSHA-256 context
outOutput buffer

Definition at line 239 of file sha256.c.

239  {
240  struct sha256_context *context = ctx;
241  uint64_t len_bits;
242  uint8_t pad;
243 
244  /* Record length before pre-processing */
245  len_bits = cpu_to_be64 ( ( ( uint64_t ) context->len ) * 8 );
246 
247  /* Pad with a single "1" bit followed by as many "0" bits as required */
248  pad = 0x80;
249  do {
250  sha256_update ( ctx, &pad, sizeof ( pad ) );
251  pad = 0x00;
252  } while ( ( context->len % sizeof ( context->ddd.dd.data ) ) !=
253  offsetof ( typeof ( context->ddd.dd.data ), final.len ) );
254 
255  /* Append length (in bits) */
256  sha256_update ( ctx, &len_bits, sizeof ( len_bits ) );
257  assert ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 );
258 
259  /* Copy out final digest */
260  memcpy ( out, &context->ddd.dd.digest, context->digestsize );
261 }
struct sha256_digest digest
Digest of data already processed.
Definition: sha256.h:46
unsigned long long uint64_t
Definition: stdint.h:13
size_t digestsize
Digest size.
Definition: sha256.h:65
#define offsetof(type, field)
Get offset of a field within a structure.
Definition: stddef.h:24
u32 pad[9]
Padding.
Definition: ar9003_mac.h:90
__be32 out[4]
Definition: CIB_PRM.h:36
void * memcpy(void *dest, const void *src, size_t len) __nonnull
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
void sha256_update(void *ctx, const void *data, size_t len)
Accumulate data with SHA-256 algorithm.
Definition: sha256.c:216
struct sha256_digest_data dd
Digest and data block.
Definition: sha256.h:54
size_t len
Amount of accumulated data.
Definition: sha256.h:63
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
unsigned char uint8_t
Definition: stdint.h:10
union sha256_block data
Accumulated data.
Definition: sha256.h:48
#define cpu_to_be64(value)
Definition: byteswap.h:111
union sha256_digest_data_dwords ddd
Digest and accumulated data.
Definition: sha256.h:67
typeof(acpi_finder=acpi_find)
ACPI table finder.
Definition: acpi.c:45
An SHA-256 context.
Definition: sha256.h:61

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

◆ digest

struct sha256_digest digest

Digest of data already processed.

Definition at line 12 of file sha256.h.

◆ data

union sha256_block data

Accumulated data.

Definition at line 14 of file sha256.h.

◆ __attribute__

◆ len

size_t len

Amount of accumulated data.

Definition at line 12 of file sha256.h.

◆ digestsize

size_t digestsize

Digest size.

Definition at line 14 of file sha256.h.

◆ ddd

Digest and accumulated data.

Definition at line 16 of file sha256.h.

◆ sha256_algorithm

struct digest_algorithm sha256_algorithm

SHA-256 algorithm.

Definition at line 264 of file sha256.c.

Referenced by icert_cert(), peerdist_info_v1(), and sha256_test_exec().

◆ sha224_algorithm

struct digest_algorithm sha224_algorithm

SHA-224 algorithm.

Definition at line 63 of file sha224.c.

Referenced by sha256_test_exec().