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/asn1.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)
 
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

static struct sha1_step sha1_steps [4]
 SHA-1 steps. More...
 
struct digest_algorithm sha1_algorithm
 SHA-1 algorithm. More...
 
static uint8_t oid_sha1 [] = { ASN1_OID_SHA1 }
 "sha1" object identifier More...
 
struct asn1_algorithm oid_sha1_algorithm __asn1_algorithm
 "sha1" OID-identified algorithm More...
 

Detailed Description

SHA-1 algorithm.

Definition in file sha1.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ 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 60 of file sha1.c.

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

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 70 of file sha1.c.

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

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 80 of file sha1.c.

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

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 114 of file sha1.c.

114  {
115  struct sha1_context *context = ctx;
116 
117  context->ddd.dd.digest.h[0] = cpu_to_be32 ( 0x67452301 );
118  context->ddd.dd.digest.h[1] = cpu_to_be32 ( 0xefcdab89 );
119  context->ddd.dd.digest.h[2] = cpu_to_be32 ( 0x98badcfe );
120  context->ddd.dd.digest.h[3] = cpu_to_be32 ( 0x10325476 );
121  context->ddd.dd.digest.h[4] = cpu_to_be32 ( 0xc3d2e1f0 );
122  context->len = 0;
123 }
An SHA-1 context.
Definition: sha1.h:58
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
struct sha1_digest_data dd
Digest and data block.
Definition: sha1.h:51
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 130 of file sha1.c.

130  {
131  union {
133  struct sha1_variables v;
134  } u;
135  uint32_t *a = &u.v.a;
136  uint32_t *b = &u.v.b;
137  uint32_t *c = &u.v.c;
138  uint32_t *d = &u.v.d;
139  uint32_t *e = &u.v.e;
140  uint32_t *w = u.v.w;
141  uint32_t f;
142  uint32_t k;
143  uint32_t temp;
144  struct sha1_step *step;
145  unsigned int i;
146 
147  /* Sanity checks */
148  assert ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 );
149  linker_assert ( &u.ddd.dd.digest.h[0] == a, sha1_bad_layout );
150  linker_assert ( &u.ddd.dd.digest.h[1] == b, sha1_bad_layout );
151  linker_assert ( &u.ddd.dd.digest.h[2] == c, sha1_bad_layout );
152  linker_assert ( &u.ddd.dd.digest.h[3] == d, sha1_bad_layout );
153  linker_assert ( &u.ddd.dd.digest.h[4] == e, sha1_bad_layout );
154  linker_assert ( &u.ddd.dd.data.dword[0] == w, sha1_bad_layout );
155 
156  DBGC ( context, "SHA1 digesting:\n" );
157  DBGC_HDA ( context, 0, &context->ddd.dd.digest,
158  sizeof ( context->ddd.dd.digest ) );
159  DBGC_HDA ( context, context->len, &context->ddd.dd.data,
160  sizeof ( context->ddd.dd.data ) );
161 
162  /* Convert h[0..4] to host-endian, and initialise a, b, c, d,
163  * e, and w[0..15]
164  */
165  for ( i = 0 ; i < ( sizeof ( u.ddd.dword ) /
166  sizeof ( u.ddd.dword[0] ) ) ; i++ ) {
167  be32_to_cpus ( &context->ddd.dword[i] );
168  u.ddd.dword[i] = context->ddd.dword[i];
169  }
170 
171  /* Initialise w[16..79] */
172  for ( i = 16 ; i < 80 ; i++ )
173  w[i] = rol32 ( ( w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16] ), 1 );
174 
175  /* Main loop */
176  for ( i = 0 ; i < 80 ; i++ ) {
177  step = &sha1_steps[ i / 20 ];
178  f = step->f ( &u.v );
179  k = step->k;
180  temp = ( rol32 ( *a, 5 ) + f + *e + k + w[i] );
181  *e = *d;
182  *d = *c;
183  *c = rol32 ( *b, 30 );
184  *b = *a;
185  *a = temp;
186  DBGC2 ( context, "%2d : %08x %08x %08x %08x %08x\n",
187  i, *a, *b, *c, *d, *e );
188  }
189 
190  /* Add chunk to hash and convert back to big-endian */
191  for ( i = 0 ; i < 5 ; i++ ) {
192  context->ddd.dd.digest.h[i] =
193  cpu_to_be32 ( context->ddd.dd.digest.h[i] +
194  u.ddd.dd.digest.h[i] );
195  }
196 
197  DBGC ( context, "SHA1 digested:\n" );
198  DBGC_HDA ( context, 0, &context->ddd.dd.digest,
199  sizeof ( context->ddd.dd.digest ) );
200 }
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
#define DBGC(...)
Definition: compiler.h:505
static __always_inline void off_t int c
Definition: efi_uaccess.h:87
uint32_t b
Definition: sha1.c:47
uint32_t dword[sizeof(struct sha1_digest_data)/sizeof(uint32_t)]
Raw dwords.
Definition: sha1.h:54
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
uint32_t e
Definition: sha1.c:50
uint32_t(* f)(struct sha1_variables *v)
Calculate f(a,b,c,d)
Definition: sha1.c:92
uint32_t h[5]
Hash output.
Definition: sha1.h:18
An SHA-1 step function.
Definition: sha1.c:85
static const uint32_t k[64]
MD5 constants.
Definition: md5.c:54
SHA-1 digest and data block.
Definition: sha1.h:49
unsigned int uint32_t
Definition: stdint.h:12
#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
uint32_t d
Definition: sha1.c:49
uint32_t w[80]
Definition: sha1.c:51
uint32_t a
Definition: sha1.c:46
SHA-1 variables.
Definition: sha1.c:42
union @16 u
struct sha1_digest digest
Digest of data already processed.
Definition: sha1.h:43
#define linker_assert(condition, error_symbol)
Assert a condition at link-time.
Definition: assert.h:68
static struct sha1_step sha1_steps[4]
SHA-1 steps.
Definition: sha1.c:98

References sha1_variables::a, assert(), sha1_variables::b, be32_to_cpus, c, cpu_to_be32, sha1_variables::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, sha1_variables::e, sha1_step::f, sha1_digest::h, k, sha1_context::len, linker_assert, rol32(), sha1_steps, step(), u, and sha1_variables::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 209 of file sha1.c.

209  {
210  struct sha1_context *context = ctx;
211  const uint8_t *byte = data;
212  size_t offset;
213 
214  /* Accumulate data a byte at a time, performing the digest
215  * whenever we fill the data buffer
216  */
217  while ( len-- ) {
218  offset = ( context->len % sizeof ( context->ddd.dd.data ) );
219  context->ddd.dd.data.byte[offset] = *(byte++);
220  context->len++;
221  if ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 )
222  sha1_digest ( context );
223  }
224 }
An SHA-1 context.
Definition: sha1.h:58
union sha1_block data
Accumulated data.
Definition: sha1.h:45
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
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
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:130
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
struct arbelprm_port_state_change_st data
Message.
Definition: arbel.h:12

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 232 of file sha1.c.

232  {
233  struct sha1_context *context = ctx;
234  uint64_t len_bits;
235  uint8_t pad;
236 
237  /* Record length before pre-processing */
238  len_bits = cpu_to_be64 ( ( ( uint64_t ) context->len ) * 8 );
239 
240  /* Pad with a single "1" bit followed by as many "0" bits as required */
241  pad = 0x80;
242  do {
243  sha1_update ( ctx, &pad, sizeof ( pad ) );
244  pad = 0x00;
245  } while ( ( context->len % sizeof ( context->ddd.dd.data ) ) !=
246  offsetof ( typeof ( context->ddd.dd.data ), final.len ) );
247 
248  /* Append length (in bits) */
249  sha1_update ( ctx, &len_bits, sizeof ( len_bits ) );
250  assert ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 );
251 
252  /* Copy out final digest */
253  memcpy ( out, &context->ddd.dd.digest,
254  sizeof ( context->ddd.dd.digest ) );
255 }
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
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
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)
__be32 out[4]
Definition: CIB_PRM.h:36
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:209
union sha1_digest_data_dwords ddd
Digest and accumulated data.
Definition: sha1.h:62
#define cpu_to_be64(value)
Definition: byteswap.h:111
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, and sha1_update().

Variable Documentation

◆ 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:80
static uint32_t sha1_f_0_19(struct sha1_variables *v)
f(a,b,c,d) for steps 0 to 19
Definition: sha1.c:60
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:70

SHA-1 steps.

Definition at line 98 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:114
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:232
static void sha1_update(void *ctx, const void *data, size_t len)
Accumulate data with SHA-1 algorithm.
Definition: sha1.c:209
uint32_t digestsize
Digest size (i.e.
Definition: pccrr.h:14

SHA-1 algorithm.

Definition at line 258 of file sha1.c.

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

◆ oid_sha1

uint8_t oid_sha1[] = { ASN1_OID_SHA1 }
static

"sha1" object identifier

Definition at line 269 of file sha1.c.

◆ __asn1_algorithm

struct asn1_algorithm oid_sha1_algorithm __asn1_algorithm
Initial value:
= {
.name = "sha1",
.digest = &sha1_algorithm,
}
static uint8_t oid_sha1[]
"sha1" object identifier
Definition: sha1.c:269
#define ASN1_OID_CURSOR(oid_value)
Define an ASN.1 cursor containing an OID.
Definition: asn1.h:292
struct digest_algorithm sha1_algorithm
SHA-1 algorithm.
Definition: sha1.c:258

"sha1" OID-identified algorithm

Definition at line 272 of file sha1.c.