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

SHA-512 algorithm. More...

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

Go to the source code of this file.

Data Structures

struct  sha512_variables
 SHA-512 variables. More...
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
struct sha512_variables __attribute__ ((packed))
 
void sha512_family_init (struct sha512_context *context, const struct sha512_digest *init, size_t digestsize)
 Initialise SHA-512 family algorithm. More...
 
static void sha512_init (void *ctx)
 Initialise SHA-512 algorithm. More...
 
static void sha512_digest (struct sha512_context *context)
 Calculate SHA-512 digest of accumulated data. More...
 
void sha512_update (void *ctx, const void *data, size_t len)
 Accumulate data with SHA-512 algorithm. More...
 
void sha512_final (void *ctx, void *out)
 Generate SHA-512 digest. More...
 

Variables

uint64_t a
 
uint64_t b
 
uint64_t c
 
uint64_t d
 
uint64_t e
 
uint64_t f
 
uint64_t g
 
uint64_t h
 
uint64_t w [SHA512_ROUNDS]
 
static const uint64_t k [SHA512_ROUNDS]
 SHA-512 constants. More...
 
static const struct sha512_digest sha512_init_digest
 SHA-512 initial digest values. More...
 
struct digest_algorithm sha512_algorithm
 SHA-512 algorithm. More...
 

Detailed Description

SHA-512 algorithm.

Definition in file sha512.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ __attribute__()

struct sha512_variables __attribute__ ( (packed)  )

◆ sha512_family_init()

void sha512_family_init ( struct sha512_context context,
const struct sha512_digest init,
size_t  digestsize 
)

Initialise SHA-512 family algorithm.

Parameters
contextSHA-512 context
initInitial digest values
digestsizeDigest size

Definition at line 108 of file sha512.c.

110  {
111 
112  context->len = 0;
113  context->digestsize = digestsize;
114  memcpy ( &context->ddq.dd.digest, init,
115  sizeof ( context->ddq.dd.digest ) );
116 }
struct sha512_digest digest
Digest of data already processed.
Definition: sha512.h:48
void * memcpy(void *dest, const void *src, size_t len) __nonnull
struct sha512_digest_data dd
Digest and data block.
Definition: sha512.h:56
union sha512_digest_data_qwords ddq
Digest and accumulated data.
Definition: sha512.h:69
size_t len
Amount of accumulated data.
Definition: sha512.h:65
uint32_t digestsize
Digest size (i.e.
Definition: pccrr.h:14
size_t digestsize
Digest size.
Definition: sha512.h:67

References sha512_digest_data_qwords::dd, sha512_context::ddq, sha512_digest_data::digest, digestsize, sha512_context::digestsize, sha512_context::len, and memcpy().

Referenced by sha384_init(), sha512_224_init(), sha512_256_init(), and sha512_init().

◆ sha512_init()

static void sha512_init ( void *  ctx)
static

Initialise SHA-512 algorithm.

Parameters
ctxSHA-512 context

Definition at line 123 of file sha512.c.

123  {
124  struct sha512_context *context = ctx;
125 
127  sizeof ( struct sha512_digest ) );
128 }
An SHA-512 context.
Definition: sha512.h:63
An SHA-512 digest.
Definition: sha512.h:19
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
static const struct sha512_digest sha512_init_digest
SHA-512 initial digest values.
Definition: sha512.c:88
void sha512_family_init(struct sha512_context *context, const struct sha512_digest *init, size_t digestsize)
Initialise SHA-512 family algorithm.
Definition: sha512.c:108

References ctx, sha512_family_init(), and sha512_init_digest.

◆ sha512_digest()

static void sha512_digest ( struct sha512_context context)
static

Calculate SHA-512 digest of accumulated data.

Parameters
contextSHA-512 context

Definition at line 135 of file sha512.c.

135  {
136  union {
138  struct sha512_variables v;
139  } u;
140  uint64_t *a = &u.v.a;
141  uint64_t *b = &u.v.b;
142  uint64_t *c = &u.v.c;
143  uint64_t *d = &u.v.d;
144  uint64_t *e = &u.v.e;
145  uint64_t *f = &u.v.f;
146  uint64_t *g = &u.v.g;
147  uint64_t *h = &u.v.h;
148  uint64_t *w = u.v.w;
149  uint64_t s0;
150  uint64_t s1;
151  uint64_t maj;
152  uint64_t t1;
153  uint64_t t2;
154  uint64_t ch;
155  unsigned int i;
156 
157  /* Sanity checks */
158  assert ( ( context->len % sizeof ( context->ddq.dd.data ) ) == 0 );
159  build_assert ( &u.ddq.dd.digest.h[0] == a );
160  build_assert ( &u.ddq.dd.digest.h[1] == b );
161  build_assert ( &u.ddq.dd.digest.h[2] == c );
162  build_assert ( &u.ddq.dd.digest.h[3] == d );
163  build_assert ( &u.ddq.dd.digest.h[4] == e );
164  build_assert ( &u.ddq.dd.digest.h[5] == f );
165  build_assert ( &u.ddq.dd.digest.h[6] == g );
166  build_assert ( &u.ddq.dd.digest.h[7] == h );
167  build_assert ( &u.ddq.dd.data.qword[0] == w );
168 
169  DBGC ( context, "SHA512 digesting:\n" );
170  DBGC_HDA ( context, 0, &context->ddq.dd.digest,
171  sizeof ( context->ddq.dd.digest ) );
172  DBGC_HDA ( context, context->len, &context->ddq.dd.data,
173  sizeof ( context->ddq.dd.data ) );
174 
175  /* Convert h[0..7] to host-endian, and initialise a, b, c, d,
176  * e, f, g, h, and w[0..15]
177  */
178  for ( i = 0 ; i < ( sizeof ( u.ddq.qword ) /
179  sizeof ( u.ddq.qword[0] ) ) ; i++ ) {
180  be64_to_cpus ( &context->ddq.qword[i] );
181  u.ddq.qword[i] = context->ddq.qword[i];
182  }
183 
184  /* Initialise w[16..79] */
185  for ( i = 16 ; i < SHA512_ROUNDS ; i++ ) {
186  s0 = ( ror64 ( w[i-15], 1 ) ^ ror64 ( w[i-15], 8 ) ^
187  ( w[i-15] >> 7 ) );
188  s1 = ( ror64 ( w[i-2], 19 ) ^ ror64 ( w[i-2], 61 ) ^
189  ( w[i-2] >> 6 ) );
190  w[i] = ( w[i-16] + s0 + w[i-7] + s1 );
191  }
192 
193  /* Main loop */
194  for ( i = 0 ; i < SHA512_ROUNDS ; i++ ) {
195  s0 = ( ror64 ( *a, 28 ) ^ ror64 ( *a, 34 ) ^ ror64 ( *a, 39 ) );
196  maj = ( ( *a & *b ) ^ ( *a & *c ) ^ ( *b & *c ) );
197  t2 = ( s0 + maj );
198  s1 = ( ror64 ( *e, 14 ) ^ ror64 ( *e, 18 ) ^ ror64 ( *e, 41 ) );
199  ch = ( ( *e & *f ) ^ ( (~*e) & *g ) );
200  t1 = ( *h + s1 + ch + k[i] + w[i] );
201  *h = *g;
202  *g = *f;
203  *f = *e;
204  *e = ( *d + t1 );
205  *d = *c;
206  *c = *b;
207  *b = *a;
208  *a = ( t1 + t2 );
209  DBGC2 ( context, "%2d : %016llx %016llx %016llx %016llx "
210  "%016llx %016llx %016llx %016llx\n",
211  i, *a, *b, *c, *d, *e, *f, *g, *h );
212  }
213 
214  /* Add chunk to hash and convert back to big-endian */
215  for ( i = 0 ; i < 8 ; i++ ) {
216  context->ddq.dd.digest.h[i] =
217  cpu_to_be64 ( context->ddq.dd.digest.h[i] +
218  u.ddq.dd.digest.h[i] );
219  }
220 
221  DBGC ( context, "SHA512 digested:\n" );
222  DBGC_HDA ( context, 0, &context->ddq.dd.digest,
223  sizeof ( context->ddq.dd.digest ) );
224 }
struct sha512_digest digest
Digest of data already processed.
Definition: sha512.h:48
SHA-512 digest and data block.
Definition: sha512.h:54
SHA-512 variables.
Definition: sha512.c:41
uint64_t w[SHA512_ROUNDS]
Definition: sha512.c:36
#define DBGC(...)
Definition: compiler.h:505
unsigned long long uint64_t
Definition: stdint.h:13
uint64_t d
Definition: sha512.c:31
uint8_t ch
Definition: registers.h:83
struct sha512_digest_data dd
Digest and data block.
Definition: sha512.h:56
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
#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
uint64_t qword[sizeof(struct sha512_digest_data)/sizeof(uint64_t)]
Raw qwords.
Definition: sha512.h:59
uint64_t g
Definition: sha512.c:34
uint64_t b
Definition: sha512.c:29
uint64_t c
Definition: sha512.c:30
union sha512_digest_data_qwords ddq
Digest and accumulated data.
Definition: sha512.h:69
uint64_t h
Definition: sha512.c:35
uint64_t h[8]
Hash output.
Definition: sha512.h:21
size_t len
Amount of accumulated data.
Definition: sha512.h:65
union sha512_block data
Accumulated data.
Definition: sha512.h:50
uint64_t e
Definition: sha512.c:32
#define DBGC2(...)
Definition: compiler.h:522
uint64_t f
Definition: sha512.c:33
union @17 u
#define cpu_to_be64(value)
Definition: byteswap.h:111
static const uint64_t k[SHA512_ROUNDS]
SHA-512 constants.
Definition: sha512.c:57
#define SHA512_ROUNDS
SHA-512 number of rounds.
Definition: sha512.h:16
uint64_t a
Definition: sha512.c:28
#define be64_to_cpus(ptr)
Definition: byteswap.h:129

References a, assert(), b, be64_to_cpus, build_assert, c, ch, cpu_to_be64, d, sha512_digest_data::data, DBGC, DBGC2, DBGC_HDA, sha512_digest_data_qwords::dd, sha512_context::ddq, sha512_digest_data::digest, e, f, g, sha512_digest::h, h, k, sha512_context::len, sha512_digest_data_qwords::qword, SHA512_ROUNDS, u, and w.

Referenced by sha512_update().

◆ sha512_update()

void sha512_update ( void *  ctx,
const void *  data,
size_t  len 
)

Accumulate data with SHA-512 algorithm.

Parameters
ctxSHA-512 context
dataData
lenLength of data

Definition at line 233 of file sha512.c.

233  {
234  struct sha512_context *context = ctx;
235  const uint8_t *byte = data;
236  size_t offset;
237 
238  /* Accumulate data a byte at a time, performing the digest
239  * whenever we fill the data buffer
240  */
241  while ( len-- ) {
242  offset = ( context->len % sizeof ( context->ddq.dd.data ) );
243  context->ddq.dd.data.byte[offset] = *(byte++);
244  context->len++;
245  if ( ( context->len % sizeof ( context->ddq.dd.data ) ) == 0 )
246  sha512_digest ( context );
247  }
248 }
struct sha512_digest_data dd
Digest and data block.
Definition: sha512.h:56
static userptr_t size_t offset
Offset of the first segment within the content.
Definition: deflate.h:259
An SHA-512 context.
Definition: sha512.h:63
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
unsigned char uint8_t
Definition: stdint.h:10
union sha512_digest_data_qwords ddq
Digest and accumulated data.
Definition: sha512.h:69
size_t len
Amount of accumulated data.
Definition: sha512.h:65
union sha512_block data
Accumulated data.
Definition: sha512.h:50
uint32_t len
Length.
Definition: ena.h:14
uint8_t data[48]
Additional event data.
Definition: ena.h:22
static void sha512_digest(struct sha512_context *context)
Calculate SHA-512 digest of accumulated data.
Definition: sha512.c:135
uint8_t byte[128]
Raw bytes.
Definition: sha512.h:27

References sha512_block::byte, ctx, data, sha512_digest_data::data, sha512_digest_data_qwords::dd, sha512_context::ddq, len, sha512_context::len, offset, and sha512_digest().

Referenced by sha512_final().

◆ sha512_final()

void sha512_final ( void *  ctx,
void *  out 
)

Generate SHA-512 digest.

Parameters
ctxSHA-512 context
outOutput buffer

Definition at line 256 of file sha512.c.

256  {
257  struct sha512_context *context = ctx;
258  uint64_t len_bits_hi;
259  uint64_t len_bits_lo;
260  uint8_t pad;
261 
262  /* Record length before pre-processing */
263  len_bits_hi = 0;
264  len_bits_lo = cpu_to_be64 ( ( ( uint64_t ) context->len ) * 8 );
265 
266  /* Pad with a single "1" bit followed by as many "0" bits as required */
267  pad = 0x80;
268  do {
269  sha512_update ( ctx, &pad, sizeof ( pad ) );
270  pad = 0x00;
271  } while ( ( context->len % sizeof ( context->ddq.dd.data ) ) !=
272  offsetof ( typeof ( context->ddq.dd.data ), final.len_hi ) );
273 
274  /* Append length (in bits) */
275  sha512_update ( ctx, &len_bits_hi, sizeof ( len_bits_hi ) );
276  sha512_update ( ctx, &len_bits_lo, sizeof ( len_bits_lo ) );
277  assert ( ( context->len % sizeof ( context->ddq.dd.data ) ) == 0 );
278 
279  /* Copy out final digest */
280  memcpy ( out, &context->ddq.dd.digest, context->digestsize );
281 }
void sha512_update(void *ctx, const void *data, size_t len)
Accumulate data with SHA-512 algorithm.
Definition: sha512.c:233
struct sha512_digest digest
Digest of data already processed.
Definition: sha512.h:48
unsigned long long uint64_t
Definition: stdint.h:13
#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
struct sha512_digest_data dd
Digest and data block.
Definition: sha512.h:56
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
An SHA-512 context.
Definition: sha512.h:63
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
unsigned char uint8_t
Definition: stdint.h:10
union sha512_digest_data_qwords ddq
Digest and accumulated data.
Definition: sha512.h:69
size_t len
Amount of accumulated data.
Definition: sha512.h:65
union sha512_block data
Accumulated data.
Definition: sha512.h:50
#define cpu_to_be64(value)
Definition: byteswap.h:111
typeof(acpi_finder=acpi_find)
ACPI table finder.
Definition: acpi.c:45
size_t digestsize
Digest size.
Definition: sha512.h:67

References assert(), cpu_to_be64, ctx, sha512_digest_data::data, sha512_digest_data_qwords::dd, sha512_context::ddq, sha512_digest_data::digest, sha512_context::digestsize, sha512_context::len, memcpy(), offsetof, out, pad, sha512_update(), and typeof().

Variable Documentation

◆ a

Definition at line 28 of file sha512.c.

Referenced by sha512_digest().

◆ b

Definition at line 29 of file sha512.c.

Referenced by sha512_digest().

◆ c

Definition at line 30 of file sha512.c.

Referenced by sha512_digest().

◆ d

Definition at line 31 of file sha512.c.

Referenced by sha512_digest().

◆ e

Definition at line 32 of file sha512.c.

Referenced by sha512_digest().

◆ f

Definition at line 33 of file sha512.c.

Referenced by sha512_digest().

◆ g

Definition at line 34 of file sha512.c.

Referenced by sha512_digest().

◆ h

Definition at line 35 of file sha512.c.

Referenced by sha512_digest().

◆ w

Definition at line 36 of file sha512.c.

Referenced by sha512_digest().

◆ k

const uint64_t k[SHA512_ROUNDS]
static
Initial value:
= {
0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, 0xb5c0fbcfec4d3b2fULL,
0xe9b5dba58189dbbcULL, 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL,
0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL, 0xd807aa98a3030242ULL,
0x12835b0145706fbeULL, 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,
0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL, 0x9bdc06a725c71235ULL,
0xc19bf174cf692694ULL, 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL,
0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL, 0x2de92c6f592b0275ULL,
0x4a7484aa6ea6e483ULL, 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL, 0xb00327c898fb213fULL,
0xbf597fc7beef0ee4ULL, 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL,
0x06ca6351e003826fULL, 0x142929670a0e6e70ULL, 0x27b70a8546d22ffcULL,
0x2e1b21385c26c926ULL, 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL, 0x81c2c92e47edaee6ULL,
0x92722c851482353bULL, 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL,
0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL, 0xd192e819d6ef5218ULL,
0xd69906245565a910ULL, 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL, 0x2748774cdf8eeb99ULL,
0x34b0bcb5e19b48a8ULL, 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL,
0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL, 0x748f82ee5defb2fcULL,
0x78a5636f43172f60ULL, 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL, 0xbef9a3f7b2c67915ULL,
0xc67178f2e372532bULL, 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL,
0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL, 0x06f067aa72176fbaULL,
0x0a637dc5a2c898a6ULL, 0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
0x28db77f523047d84ULL, 0x32caab7b40c72493ULL, 0x3c9ebe0a15c9bebcULL,
0x431d67c49c100d4cULL, 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL,
0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL
}

SHA-512 constants.

Definition at line 57 of file sha512.c.

Referenced by sha512_digest().

◆ sha512_init_digest

const struct sha512_digest sha512_init_digest
static
Initial value:
= {
.h = {
cpu_to_be64 ( 0x6a09e667f3bcc908ULL ),
cpu_to_be64 ( 0xbb67ae8584caa73bULL ),
cpu_to_be64 ( 0x3c6ef372fe94f82bULL ),
cpu_to_be64 ( 0xa54ff53a5f1d36f1ULL ),
cpu_to_be64 ( 0x510e527fade682d1ULL ),
cpu_to_be64 ( 0x9b05688c2b3e6c1fULL ),
cpu_to_be64 ( 0x1f83d9abfb41bd6bULL ),
cpu_to_be64 ( 0x5be0cd19137e2179ULL ),
},
}
#define cpu_to_be64(value)
Definition: byteswap.h:111

SHA-512 initial digest values.

Definition at line 88 of file sha512.c.

Referenced by sha512_init().

◆ sha512_algorithm

struct digest_algorithm sha512_algorithm
Initial value:
= {
.name = "sha512",
.ctxsize = sizeof ( struct sha512_context ),
.blocksize = sizeof ( union sha512_block ),
.digestsize = sizeof ( struct sha512_digest ),
.init = sha512_init,
.update = sha512_update,
.final = sha512_final,
}
void sha512_update(void *ctx, const void *data, size_t len)
Accumulate data with SHA-512 algorithm.
Definition: sha512.c:233
void sha512_final(void *ctx, void *out)
Generate SHA-512 digest.
Definition: sha512.c:256
An SHA-512 context.
Definition: sha512.h:63
An SHA-512 digest.
Definition: sha512.h:19
An SHA-512 data block.
Definition: sha512.h:25
static void sha512_init(void *ctx)
Initialise SHA-512 algorithm.
Definition: sha512.c:123
uint32_t digestsize
Digest size (i.e.
Definition: pccrr.h:14

SHA-512 algorithm.

Definition at line 284 of file sha512.c.

Referenced by peerdist_info_v1(), peerdist_info_v2(), and sha512_test_exec().