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_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_DIGEST_SIZE

#define SHA256_DIGEST_SIZE   sizeof ( struct sha256_digest )

SHA-256 digest size.

Definition at line 74 of file sha256.h.

◆ SHA224_DIGEST_SIZE

#define SHA224_DIGEST_SIZE   ( SHA256_DIGEST_SIZE * 224 / 256 )

SHA-224 digest size.

Definition at line 77 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 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 }
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 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:28
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
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
union sha256_digest_data_dwords ddd
Digest and accumulated data.
Definition: sha256.h:67
An SHA-256 context.
Definition: sha256.h:61
struct arbelprm_port_state_change_st data
Message.
Definition: arbel.h:12
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:120

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 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 }
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
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
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:217
__be32 out[4]
Definition: CIB_PRM.h:36
struct sha256_digest_data dd
Digest and data block.
Definition: sha256.h:54
size_t len
Amount of accumulated data.
Definition: sha256.h:63
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
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, and sha256_update().

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

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

◆ sha224_algorithm

struct digest_algorithm sha224_algorithm

SHA-224 algorithm.

Definition at line 64 of file sha224.c.

Referenced by sha256_test_exec().