iPXE
Data Structures | Functions | Variables
sha256.c File Reference

SHA-256 algorithm. More...

#include <stdint.h>
#include <string.h>
#include <byteswap.h>
#include <assert.h>
#include <ipxe/rotate.h>
#include <ipxe/crypto.h>
#include <ipxe/sha256.h>

Go to the source code of this file.

Data Structures

struct  sha256_variables
 SHA-256 variables. More...
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
struct sha256_variables __attribute__ ((packed))
 
void sha256_family_init (struct sha256_context *context, const struct sha256_digest *init, size_t digestsize)
 Initialise SHA-256 family algorithm. More...
 
static void sha256_init (void *ctx)
 Initialise SHA-256 algorithm. More...
 
static void sha256_digest (struct sha256_context *context)
 Calculate SHA-256 digest of accumulated data. 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

uint32_t a
 
uint32_t b
 
uint32_t c
 
uint32_t d
 
uint32_t e
 
uint32_t f
 
uint32_t g
 
uint32_t h
 
uint32_t w [SHA256_ROUNDS]
 
static const uint32_t k [SHA256_ROUNDS]
 SHA-256 constants. More...
 
static const struct sha256_digest sha256_init_digest
 SHA-256 initial digest values. More...
 
struct digest_algorithm sha256_algorithm
 SHA-256 algorithm. More...
 

Detailed Description

SHA-256 algorithm.

Definition in file sha256.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ __attribute__()

struct sha256_variables __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_init()

static void sha256_init ( void *  ctx)
static

Initialise SHA-256 algorithm.

Parameters
ctxSHA-256 context

Definition at line 107 of file sha256.c.

107  {
108  struct sha256_context *context = ctx;
109 
111  sizeof ( struct sha256_digest ) );
112 }
static const struct sha256_digest sha256_init_digest
SHA-256 initial digest values.
Definition: sha256.c:72
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
void sha256_family_init(struct sha256_context *context, const struct sha256_digest *init, size_t digestsize)
Initialise SHA-256 family algorithm.
Definition: sha256.c:92
An SHA-256 context.
Definition: sha256.h:61
An SHA-256 digest.
Definition: sha256.h:19

References ctx, sha256_family_init(), and sha256_init_digest.

◆ sha256_digest()

static void sha256_digest ( struct sha256_context context)
static

Calculate SHA-256 digest of accumulated data.

Parameters
contextSHA-256 context

Definition at line 119 of file sha256.c.

119  {
120  union {
122  struct sha256_variables v;
123  } u;
124  uint32_t *a = &u.v.a;
125  uint32_t *b = &u.v.b;
126  uint32_t *c = &u.v.c;
127  uint32_t *d = &u.v.d;
128  uint32_t *e = &u.v.e;
129  uint32_t *f = &u.v.f;
130  uint32_t *g = &u.v.g;
131  uint32_t *h = &u.v.h;
132  uint32_t *w = u.v.w;
133  uint32_t s0;
134  uint32_t s1;
135  uint32_t maj;
136  uint32_t t1;
137  uint32_t t2;
138  uint32_t ch;
139  unsigned int i;
140 
141  /* Sanity checks */
142  assert ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 );
143  build_assert ( &u.ddd.dd.digest.h[0] == a );
144  build_assert ( &u.ddd.dd.digest.h[1] == b );
145  build_assert ( &u.ddd.dd.digest.h[2] == c );
146  build_assert ( &u.ddd.dd.digest.h[3] == d );
147  build_assert ( &u.ddd.dd.digest.h[4] == e );
148  build_assert ( &u.ddd.dd.digest.h[5] == f );
149  build_assert ( &u.ddd.dd.digest.h[6] == g );
150  build_assert ( &u.ddd.dd.digest.h[7] == h );
151  build_assert ( &u.ddd.dd.data.dword[0] == w );
152 
153  DBGC ( context, "SHA256 digesting:\n" );
154  DBGC_HDA ( context, 0, &context->ddd.dd.digest,
155  sizeof ( context->ddd.dd.digest ) );
156  DBGC_HDA ( context, context->len, &context->ddd.dd.data,
157  sizeof ( context->ddd.dd.data ) );
158 
159  /* Convert h[0..7] to host-endian, and initialise a, b, c, d,
160  * e, f, g, h, and w[0..15]
161  */
162  for ( i = 0 ; i < ( sizeof ( u.ddd.dword ) /
163  sizeof ( u.ddd.dword[0] ) ) ; i++ ) {
164  be32_to_cpus ( &context->ddd.dword[i] );
165  u.ddd.dword[i] = context->ddd.dword[i];
166  }
167 
168  /* Initialise w[16..63] */
169  for ( i = 16 ; i < SHA256_ROUNDS ; i++ ) {
170  s0 = ( ror32 ( w[i-15], 7 ) ^ ror32 ( w[i-15], 18 ) ^
171  ( w[i-15] >> 3 ) );
172  s1 = ( ror32 ( w[i-2], 17 ) ^ ror32 ( w[i-2], 19 ) ^
173  ( w[i-2] >> 10 ) );
174  w[i] = ( w[i-16] + s0 + w[i-7] + s1 );
175  }
176 
177  /* Main loop */
178  for ( i = 0 ; i < SHA256_ROUNDS ; i++ ) {
179  s0 = ( ror32 ( *a, 2 ) ^ ror32 ( *a, 13 ) ^ ror32 ( *a, 22 ) );
180  maj = ( ( *a & *b ) ^ ( *a & *c ) ^ ( *b & *c ) );
181  t2 = ( s0 + maj );
182  s1 = ( ror32 ( *e, 6 ) ^ ror32 ( *e, 11 ) ^ ror32 ( *e, 25 ) );
183  ch = ( ( *e & *f ) ^ ( (~*e) & *g ) );
184  t1 = ( *h + s1 + ch + k[i] + w[i] );
185  *h = *g;
186  *g = *f;
187  *f = *e;
188  *e = ( *d + t1 );
189  *d = *c;
190  *c = *b;
191  *b = *a;
192  *a = ( t1 + t2 );
193  DBGC2 ( context, "%2d : %08x %08x %08x %08x %08x %08x %08x "
194  "%08x\n", i, *a, *b, *c, *d, *e, *f, *g, *h );
195  }
196 
197  /* Add chunk to hash and convert back to big-endian */
198  for ( i = 0 ; i < 8 ; i++ ) {
199  context->ddd.dd.digest.h[i] =
200  cpu_to_be32 ( context->ddd.dd.digest.h[i] +
201  u.ddd.dd.digest.h[i] );
202  }
203 
204  DBGC ( context, "SHA256 digested:\n" );
205  DBGC_HDA ( context, 0, &context->ddd.dd.digest,
206  sizeof ( context->ddd.dd.digest ) );
207 }
uint32_t d
Definition: sha256.c:31
static const uint32_t k[SHA256_ROUNDS]
SHA-256 constants.
Definition: sha256.c:57
SHA-256 variables.
Definition: sha256.c:41
uint32_t c
Definition: sha256.c:30
uint32_t g
Definition: sha256.c:34
#define DBGC(...)
Definition: compiler.h:505
struct sha256_digest digest
Digest of data already processed.
Definition: sha256.h:46
static u32 ror32(u32 v, int bits)
Rotate 32-bit value right.
Definition: wpa_tkip.c:161
SHA-256 digest and data block.
Definition: sha256.h:52
uint32_t h[8]
Hash output.
Definition: sha256.h:21
uint8_t ch
Definition: registers.h:83
uint32_t w[SHA256_ROUNDS]
Definition: sha256.c:36
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
uint32_t h
Definition: sha256.c:35
#define DBGC_HDA(...)
Definition: compiler.h:506
#define build_assert(condition)
Assert a condition at build time (after dead code elimination)
Definition: assert.h:76
struct sha256_digest_data dd
Digest and data block.
Definition: sha256.h:54
uint32_t b
Definition: sha256.c:29
uint32_t a
Definition: sha256.c:28
size_t len
Amount of accumulated data.
Definition: sha256.h:63
unsigned int uint32_t
Definition: stdint.h:12
union sha256_block data
Accumulated data.
Definition: sha256.h:48
uint32_t dword[sizeof(struct sha256_digest_data)/sizeof(uint32_t)]
Raw dwords.
Definition: sha256.h:57
#define cpu_to_be32(value)
Definition: byteswap.h:110
#define be32_to_cpus(ptr)
Definition: byteswap.h:128
#define DBGC2(...)
Definition: compiler.h:522
uint32_t e
Definition: sha256.c:32
union @17 u
uint32_t f
Definition: sha256.c:33
union sha256_digest_data_dwords ddd
Digest and accumulated data.
Definition: sha256.h:67
#define SHA256_ROUNDS
SHA-256 number of rounds.
Definition: sha256.h:16

References a, assert(), b, be32_to_cpus, build_assert, c, ch, cpu_to_be32, d, sha256_digest_data::data, DBGC, DBGC2, DBGC_HDA, sha256_digest_data_dwords::dd, sha256_context::ddd, sha256_digest_data::digest, sha256_digest_data_dwords::dword, e, f, g, sha256_digest::h, h, k, sha256_context::len, ror32(), SHA256_ROUNDS, u, and w.

Referenced by sha256_update().

◆ 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

◆ a

Definition at line 28 of file sha256.c.

Referenced by sha256_digest().

◆ b

Definition at line 29 of file sha256.c.

Referenced by sha256_digest().

◆ c

Definition at line 30 of file sha256.c.

Referenced by sha256_digest().

◆ d

Definition at line 31 of file sha256.c.

Referenced by sha256_digest().

◆ e

Definition at line 32 of file sha256.c.

Referenced by sha256_digest().

◆ f

◆ g

Definition at line 34 of file sha256.c.

Referenced by arbel_complete(), hermon_complete(), md5_digest(), and sha256_digest().

◆ h

◆ w

Definition at line 36 of file sha256.c.

Referenced by sha256_digest().

◆ k

const uint32_t k[SHA256_ROUNDS]
static
Initial value:
= {
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
}

SHA-256 constants.

Definition at line 57 of file sha256.c.

Referenced by sha256_digest().

◆ sha256_init_digest

const struct sha256_digest sha256_init_digest
static
Initial value:
= {
.h = {
cpu_to_be32 ( 0x6a09e667 ),
cpu_to_be32 ( 0xbb67ae85 ),
cpu_to_be32 ( 0x3c6ef372 ),
cpu_to_be32 ( 0xa54ff53a ),
cpu_to_be32 ( 0x510e527f ),
cpu_to_be32 ( 0x9b05688c ),
cpu_to_be32 ( 0x1f83d9ab ),
cpu_to_be32 ( 0x5be0cd19 ),
},
}
#define cpu_to_be32(value)
Definition: byteswap.h:110

SHA-256 initial digest values.

Definition at line 72 of file sha256.c.

Referenced by sha256_init().

◆ sha256_algorithm

struct digest_algorithm sha256_algorithm
Initial value:
= {
.name = "sha256",
.ctxsize = sizeof ( struct sha256_context ),
.blocksize = sizeof ( union sha256_block ),
.digestsize = sizeof ( struct sha256_digest ),
.init = sha256_init,
.update = sha256_update,
.final = sha256_final,
}
static void sha256_init(void *ctx)
Initialise SHA-256 algorithm.
Definition: sha256.c:107
void sha256_update(void *ctx, const void *data, size_t len)
Accumulate data with SHA-256 algorithm.
Definition: sha256.c:216
An SHA-256 data block.
Definition: sha256.h:25
uint32_t digestsize
Digest size (i.e.
Definition: pccrr.h:14
An SHA-256 context.
Definition: sha256.h:61
An SHA-256 digest.
Definition: sha256.h:19
void sha256_final(void *ctx, void *out)
Generate SHA-256 digest.
Definition: sha256.c:239

SHA-256 algorithm.

Definition at line 264 of file sha256.c.

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