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

SHA-1 algorithm. More...

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

Go to the source code of this file.

Data Structures

struct  sha1_variables
 SHA-1 variables. More...
 
struct  sha1_step
 An SHA-1 step function. More...
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
struct sha1_variables __attribute__ ((packed))
 
static uint32_t sha1_f_0_19 (struct sha1_variables *v)
 f(a,b,c,d) for steps 0 to 19 More...
 
static uint32_t sha1_f_20_39_60_79 (struct sha1_variables *v)
 f(a,b,c,d) for steps 20 to 39 and 60 to 79 More...
 
static uint32_t sha1_f_40_59 (struct sha1_variables *v)
 f(a,b,c,d) for steps 40 to 59 More...
 
static void sha1_init (void *ctx)
 Initialise SHA-1 algorithm. More...
 
static void sha1_digest (struct sha1_context *context)
 Calculate SHA-1 digest of accumulated data. More...
 
static void sha1_update (void *ctx, const void *data, size_t len)
 Accumulate data with SHA-1 algorithm. More...
 
static void sha1_final (void *ctx, void *out)
 Generate SHA-1 digest. More...
 

Variables

uint32_t a
 
uint32_t b
 
uint32_t c
 
uint32_t d
 
uint32_t e
 
uint32_t w [80]
 
struct sha1_step __attribute__
 
static struct sha1_step sha1_steps [4]
 SHA-1 steps. More...
 
struct digest_algorithm sha1_algorithm
 SHA-1 algorithm. More...
 

Detailed Description

SHA-1 algorithm.

Definition in file sha1.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ __attribute__()

struct sha1_variables __attribute__ ( (packed)  )

◆ sha1_f_0_19()

static uint32_t sha1_f_0_19 ( struct sha1_variables v)
static

f(a,b,c,d) for steps 0 to 19

Parameters
vSHA-1 variables
Return values
ff(a,b,c,d)

Definition at line 59 of file sha1.c.

59  {
60  return ( ( v->b & v->c ) | ( (~v->b) & v->d ) );
61 }
uint32_t b
Definition: sha1.c:46
uint32_t c
Definition: sha1.c:47
uint32_t d
Definition: sha1.c:48

References sha1_variables::b, sha1_variables::c, and sha1_variables::d.

◆ sha1_f_20_39_60_79()

static uint32_t sha1_f_20_39_60_79 ( struct sha1_variables v)
static

f(a,b,c,d) for steps 20 to 39 and 60 to 79

Parameters
vSHA-1 variables
Return values
ff(a,b,c,d)

Definition at line 69 of file sha1.c.

69  {
70  return ( v->b ^ v->c ^ v->d );
71 }
uint32_t b
Definition: sha1.c:46
uint32_t c
Definition: sha1.c:47
uint32_t d
Definition: sha1.c:48

References sha1_variables::b, sha1_variables::c, and sha1_variables::d.

◆ sha1_f_40_59()

static uint32_t sha1_f_40_59 ( struct sha1_variables v)
static

f(a,b,c,d) for steps 40 to 59

Parameters
vSHA-1 variables
Return values
ff(a,b,c,d)

Definition at line 79 of file sha1.c.

79  {
80  return ( ( v->b & v->c ) | ( v->b & v->d ) | ( v->c & v->d ) );
81 }
uint32_t b
Definition: sha1.c:46
uint32_t c
Definition: sha1.c:47
uint32_t d
Definition: sha1.c:48

References sha1_variables::b, sha1_variables::c, and sha1_variables::d.

◆ sha1_init()

static void sha1_init ( void *  ctx)
static

Initialise SHA-1 algorithm.

Parameters
ctxSHA-1 context

Definition at line 113 of file sha1.c.

113  {
114  struct sha1_context *context = ctx;
115 
116  context->ddd.dd.digest.h[0] = cpu_to_be32 ( 0x67452301 );
117  context->ddd.dd.digest.h[1] = cpu_to_be32 ( 0xefcdab89 );
118  context->ddd.dd.digest.h[2] = cpu_to_be32 ( 0x98badcfe );
119  context->ddd.dd.digest.h[3] = cpu_to_be32 ( 0x10325476 );
120  context->ddd.dd.digest.h[4] = cpu_to_be32 ( 0xc3d2e1f0 );
121  context->len = 0;
122 }
An SHA-1 context.
Definition: sha1.h:58
struct sha1_digest_data dd
Digest and data block.
Definition: sha1.h:51
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
uint32_t h[5]
Hash output.
Definition: sha1.h:18
#define cpu_to_be32(value)
Definition: byteswap.h:110
size_t len
Amount of accumulated data.
Definition: sha1.h:60
union sha1_digest_data_dwords ddd
Digest and accumulated data.
Definition: sha1.h:62
struct sha1_digest digest
Digest of data already processed.
Definition: sha1.h:43

References cpu_to_be32, ctx, sha1_digest_data_dwords::dd, sha1_context::ddd, sha1_digest_data::digest, sha1_digest::h, and sha1_context::len.

◆ sha1_digest()

static void sha1_digest ( struct sha1_context context)
static

Calculate SHA-1 digest of accumulated data.

Parameters
contextSHA-1 context

Definition at line 129 of file sha1.c.

129  {
130  union {
132  struct sha1_variables v;
133  } u;
134  uint32_t *a = &u.v.a;
135  uint32_t *b = &u.v.b;
136  uint32_t *c = &u.v.c;
137  uint32_t *d = &u.v.d;
138  uint32_t *e = &u.v.e;
139  uint32_t *w = u.v.w;
140  uint32_t f;
141  uint32_t k;
142  uint32_t temp;
143  struct sha1_step *step;
144  unsigned int i;
145 
146  /* Sanity checks */
147  assert ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 );
148  build_assert ( &u.ddd.dd.digest.h[0] == a );
149  build_assert ( &u.ddd.dd.digest.h[1] == b );
150  build_assert ( &u.ddd.dd.digest.h[2] == c );
151  build_assert ( &u.ddd.dd.digest.h[3] == d );
152  build_assert ( &u.ddd.dd.digest.h[4] == e );
153  build_assert ( &u.ddd.dd.data.dword[0] == w );
154 
155  DBGC ( context, "SHA1 digesting:\n" );
156  DBGC_HDA ( context, 0, &context->ddd.dd.digest,
157  sizeof ( context->ddd.dd.digest ) );
158  DBGC_HDA ( context, context->len, &context->ddd.dd.data,
159  sizeof ( context->ddd.dd.data ) );
160 
161  /* Convert h[0..4] to host-endian, and initialise a, b, c, d,
162  * e, and w[0..15]
163  */
164  for ( i = 0 ; i < ( sizeof ( u.ddd.dword ) /
165  sizeof ( u.ddd.dword[0] ) ) ; i++ ) {
166  be32_to_cpus ( &context->ddd.dword[i] );
167  u.ddd.dword[i] = context->ddd.dword[i];
168  }
169 
170  /* Initialise w[16..79] */
171  for ( i = 16 ; i < 80 ; i++ )
172  w[i] = rol32 ( ( w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16] ), 1 );
173 
174  /* Main loop */
175  for ( i = 0 ; i < 80 ; i++ ) {
176  step = &sha1_steps[ i / 20 ];
177  f = step->f ( &u.v );
178  k = step->k;
179  temp = ( rol32 ( *a, 5 ) + f + *e + k + w[i] );
180  *e = *d;
181  *d = *c;
182  *c = rol32 ( *b, 30 );
183  *b = *a;
184  *a = temp;
185  DBGC2 ( context, "%2d : %08x %08x %08x %08x %08x\n",
186  i, *a, *b, *c, *d, *e );
187  }
188 
189  /* Add chunk to hash and convert back to big-endian */
190  for ( i = 0 ; i < 5 ; i++ ) {
191  context->ddd.dd.digest.h[i] =
192  cpu_to_be32 ( context->ddd.dd.digest.h[i] +
193  u.ddd.dd.digest.h[i] );
194  }
195 
196  DBGC ( context, "SHA1 digested:\n" );
197  DBGC_HDA ( context, 0, &context->ddd.dd.digest,
198  sizeof ( context->ddd.dd.digest ) );
199 }
static u32 rol32(u32 v, int bits)
Rotate 32-bit value left.
Definition: wpa_tkip.c:173
union sha1_block data
Accumulated data.
Definition: sha1.h:45
uint32_t a
Definition: sha1.c:28
#define DBGC(...)
Definition: compiler.h:505
uint32_t d
Definition: sha1.c:31
uint32_t dword[sizeof(struct sha1_digest_data)/sizeof(uint32_t)]
Raw dwords.
Definition: sha1.h:54
uint32_t e
Definition: sha1.c:32
struct sha1_digest_data dd
Digest and data block.
Definition: sha1.h:51
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
uint32_t b
Definition: sha1.c:29
uint32_t h[5]
Hash output.
Definition: sha1.h:18
An SHA-1 step function.
Definition: sha1.c:84
static const uint32_t k[64]
MD5 constants.
Definition: md5.c:53
SHA-1 digest and data block.
Definition: sha1.h:49
unsigned int uint32_t
Definition: stdint.h:12
uint32_t c
Definition: sha1.c:30
#define cpu_to_be32(value)
Definition: byteswap.h:110
#define be32_to_cpus(ptr)
Definition: byteswap.h:128
size_t len
Amount of accumulated data.
Definition: sha1.h:60
#define DBGC2(...)
Definition: compiler.h:522
union sha1_digest_data_dwords ddd
Digest and accumulated data.
Definition: sha1.h:62
void step(void)
Single-step a single process.
Definition: process.c:98
union @17 u
uint32_t w[80]
Definition: sha1.c:33
uint32_t f
Definition: sha256.c:33
SHA-1 variables.
Definition: sha1.c:41
struct sha1_digest digest
Digest of data already processed.
Definition: sha1.h:43
static struct sha1_step sha1_steps[4]
SHA-1 steps.
Definition: sha1.c:97

References a, assert(), b, be32_to_cpus, build_assert, c, cpu_to_be32, d, sha1_digest_data::data, DBGC, DBGC2, DBGC_HDA, sha1_digest_data_dwords::dd, sha1_context::ddd, sha1_digest_data::digest, sha1_digest_data_dwords::dword, e, f, sha1_digest::h, k, sha1_context::len, rol32(), sha1_steps, step(), u, and w.

Referenced by sha1_update().

◆ sha1_update()

static void sha1_update ( void *  ctx,
const void *  data,
size_t  len 
)
static

Accumulate data with SHA-1 algorithm.

Parameters
ctxSHA-1 context
dataData
lenLength of data

Definition at line 208 of file sha1.c.

208  {
209  struct sha1_context *context = ctx;
210  const uint8_t *byte = data;
211  size_t offset;
212 
213  /* Accumulate data a byte at a time, performing the digest
214  * whenever we fill the data buffer
215  */
216  while ( len-- ) {
217  offset = ( context->len % sizeof ( context->ddd.dd.data ) );
218  context->ddd.dd.data.byte[offset] = *(byte++);
219  context->len++;
220  if ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 )
221  sha1_digest ( context );
222  }
223 }
An SHA-1 context.
Definition: sha1.h:58
union sha1_block data
Accumulated data.
Definition: sha1.h:45
struct sha1_digest_data dd
Digest and data block.
Definition: sha1.h:51
uint8_t byte[64]
Raw bytes.
Definition: sha1.h:24
static userptr_t size_t offset
Offset of the first segment within the content.
Definition: deflate.h:259
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
unsigned char uint8_t
Definition: stdint.h:10
static void sha1_digest(struct sha1_context *context)
Calculate SHA-1 digest of accumulated data.
Definition: sha1.c:129
uint32_t len
Length.
Definition: ena.h:14
size_t len
Amount of accumulated data.
Definition: sha1.h:60
union sha1_digest_data_dwords ddd
Digest and accumulated data.
Definition: sha1.h:62
uint8_t data[48]
Additional event data.
Definition: ena.h:22

References sha1_block::byte, ctx, data, sha1_digest_data::data, sha1_digest_data_dwords::dd, sha1_context::ddd, len, sha1_context::len, offset, and sha1_digest().

Referenced by sha1_final().

◆ sha1_final()

static void sha1_final ( void *  ctx,
void *  out 
)
static

Generate SHA-1 digest.

Parameters
ctxSHA-1 context
outOutput buffer

Definition at line 231 of file sha1.c.

231  {
232  struct sha1_context *context = ctx;
233  uint64_t len_bits;
234  uint8_t pad;
235 
236  /* Record length before pre-processing */
237  len_bits = cpu_to_be64 ( ( ( uint64_t ) context->len ) * 8 );
238 
239  /* Pad with a single "1" bit followed by as many "0" bits as required */
240  pad = 0x80;
241  do {
242  sha1_update ( ctx, &pad, sizeof ( pad ) );
243  pad = 0x00;
244  } while ( ( context->len % sizeof ( context->ddd.dd.data ) ) !=
245  offsetof ( typeof ( context->ddd.dd.data ), final.len ) );
246 
247  /* Append length (in bits) */
248  sha1_update ( ctx, &len_bits, sizeof ( len_bits ) );
249  assert ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 );
250 
251  /* Copy out final digest */
252  memcpy ( out, &context->ddd.dd.digest,
253  sizeof ( context->ddd.dd.digest ) );
254 }
An SHA-1 context.
Definition: sha1.h:58
union sha1_block data
Accumulated data.
Definition: sha1.h:45
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 sha1_digest_data dd
Digest and data block.
Definition: sha1.h:51
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
unsigned char uint8_t
Definition: stdint.h:10
size_t len
Amount of accumulated data.
Definition: sha1.h:60
static void sha1_update(void *ctx, const void *data, size_t len)
Accumulate data with SHA-1 algorithm.
Definition: sha1.c:208
union sha1_digest_data_dwords ddd
Digest and accumulated data.
Definition: sha1.h:62
#define cpu_to_be64(value)
Definition: byteswap.h:111
typeof(acpi_finder=acpi_find)
ACPI table finder.
Definition: acpi.c:45
struct sha1_digest digest
Digest of data already processed.
Definition: sha1.h:43

References assert(), cpu_to_be64, ctx, sha1_digest_data::data, sha1_digest_data_dwords::dd, sha1_context::ddd, sha1_digest_data::digest, sha1_context::len, memcpy(), offsetof, out, pad, sha1_update(), and typeof().

Variable Documentation

◆ a

Definition at line 28 of file sha1.c.

Referenced by sha1_digest().

◆ b

Definition at line 29 of file sha1.c.

Referenced by sha1_digest().

◆ c

Definition at line 30 of file sha1.c.

Referenced by sha1_digest().

◆ d

Definition at line 31 of file sha1.c.

Referenced by sha1_digest().

◆ e

◆ w

uint32_t w[80]

Definition at line 33 of file sha1.c.

Referenced by sha1_digest().

◆ __attribute__

◆ sha1_steps

struct sha1_step sha1_steps[4]
static
Initial value:
= {
{ .f = sha1_f_0_19, .k = 0x5a827999 },
{ .f = sha1_f_20_39_60_79, .k = 0x6ed9eba1 },
{ .f = sha1_f_40_59, .k = 0x8f1bbcdc },
{ .f = sha1_f_20_39_60_79, .k = 0xca62c1d6 },
}
static uint32_t sha1_f_40_59(struct sha1_variables *v)
f(a,b,c,d) for steps 40 to 59
Definition: sha1.c:79
static uint32_t sha1_f_0_19(struct sha1_variables *v)
f(a,b,c,d) for steps 0 to 19
Definition: sha1.c:59
static uint32_t sha1_f_20_39_60_79(struct sha1_variables *v)
f(a,b,c,d) for steps 20 to 39 and 60 to 79
Definition: sha1.c:69

SHA-1 steps.

Definition at line 97 of file sha1.c.

Referenced by sha1_digest().

◆ sha1_algorithm

struct digest_algorithm sha1_algorithm
Initial value:
= {
.name = "sha1",
.ctxsize = sizeof ( struct sha1_context ),
.blocksize = sizeof ( union sha1_block ),
.digestsize = sizeof ( struct sha1_digest ),
.init = sha1_init,
.update = sha1_update,
.final = sha1_final,
}
An SHA-1 context.
Definition: sha1.h:58
static void sha1_init(void *ctx)
Initialise SHA-1 algorithm.
Definition: sha1.c:113
An SHA-1 digest.
Definition: sha1.h:16
An SHA-1 data block.
Definition: sha1.h:22
static void sha1_final(void *ctx, void *out)
Generate SHA-1 digest.
Definition: sha1.c:231
static void sha1_update(void *ctx, const void *data, size_t len)
Accumulate data with SHA-1 algorithm.
Definition: sha1.c:208
uint32_t digestsize
Digest size (i.e.
Definition: pccrr.h:14

SHA-1 algorithm.

Definition at line 257 of file sha1.c.

Referenced by ccmp_kie_mic(), certstat(), md5_sha1_final(), md5_sha1_init(), md5_sha1_update(), mschapv2_auth(), mschapv2_challenge_hash(), ocsp_compare_responder_key_hash(), pbkdf2_sha1_f(), prf_sha1(), sha1_test_exec(), sha1sum_exec(), tls_prf(), and x509_name().