iPXE
sha1.c File Reference

SHA-1 algorithm. More...

#include <stdint.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)
 FILE_SECBOOT (PERMITTED)
static uint32_t sha1_f_0_19 (struct sha1_variables *v)
 f(a,b,c,d) for steps 0 to 19
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
static uint32_t sha1_f_40_59 (struct sha1_variables *v)
 f(a,b,c,d) for steps 40 to 59
static void sha1_compress (struct sha1_digest_data *dd, const struct sha1_digest *digest)
 Calculate SHA-1 digest of accumulated data.
 MDHASH_ALGORITHM (sha1, sha1_algorithm, sha1_compress, __BIG_ENDIAN, struct sha1_digest_data, sha1_init, SHA1_DIGEST_SIZE)
 SHA-1 algorithm.

Variables

static const struct sha1_step sha1_steps [4]
 SHA-1 steps.
static const struct sha1_digest sha1_init
 SHA-1 initial digest values.

Detailed Description

SHA-1 algorithm.

Definition in file sha1.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED )

◆ sha1_f_0_19()

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

57 {
58 return ( ( v->b & v->c ) | ( (~v->b) & v->d ) );
59}
uint32_t d
Definition sha1.c:45
uint32_t c
Definition sha1.c:44
uint32_t b
Definition sha1.c:43

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

◆ sha1_f_20_39_60_79()

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

67 {
68 return ( v->b ^ v->c ^ v->d );
69}

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

◆ sha1_f_40_59()

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

77 {
78 return ( ( v->b & v->c ) | ( v->b & v->d ) | ( v->c & v->d ) );
79}

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

◆ sha1_compress()

void sha1_compress ( struct sha1_digest_data * dd,
const struct sha1_digest * digest )
static

Calculate SHA-1 digest of accumulated data.

Parameters
ddDigest and data block
digestCopy of current digest value

Definition at line 117 of file sha1.c.

118 {
119 union {
120 struct sha1_digest_data dd;
121 struct sha1_variables v;
122 } *u = container_of ( dd, typeof ( *u ), dd );
123 struct sha1_variables *v = &u->v;
124 const struct sha1_step *step;
125 uint32_t *a = &v->a;
126 uint32_t *b = &v->b;
127 uint32_t *c = &v->c;
128 uint32_t *d = &v->d;
129 uint32_t *e = &v->e;
130 uint32_t *w = v->w;
131 uint32_t f;
132 uint32_t k;
133 uint32_t temp;
134 unsigned int i;
135
136 /* Sanity checks */
137 build_assert ( &u->dd.digest.h[0] == a );
138 build_assert ( &u->dd.digest.h[1] == b );
139 build_assert ( &u->dd.digest.h[2] == c );
140 build_assert ( &u->dd.digest.h[3] == d );
141 build_assert ( &u->dd.digest.h[4] == e );
142 build_assert ( &u->dd.data.dword[0] == w );
143 build_assert ( sizeof ( u->dd ) == sizeof ( u->v ) );
144
145 /* Main loop */
146 for ( i = 0 ; i < 80 ; i++ ) {
147 step = &sha1_steps[ i / 20 ];
148 f = step->f ( v );
149 k = step->k;
150 temp = ( rol32 ( *a, 5 ) + f + *e + k + w[ i % 16 ] );
151 *e = *d;
152 *d = *c;
153 *c = rol32 ( *b, 30 );
154 *b = *a;
155 *a = temp;
156 w[ i % 16 ] = rol32 ( ( w[ ( i - 3 ) % 16 ] ^
157 w[ ( i - 8 ) % 16 ] ^
158 w[ ( i - 14 ) % 16 ] ^
159 w[ ( i - 16 ) % 16 ] ), 1 );
160 DBGC2 ( &sha1_algorithm, "%2d : %08x %08x %08x %08x %08x\n",
161 i, *a, *b, *c, *d, *e );
162 }
163
164 /* Add chunk to hash */
165 for ( i = 0 ; i < 5 ; i++ )
166 dd->digest.h[i] += digest->h[i];
167}
typeof(acpi_finder=acpi_find)
ACPI table finder.
Definition acpi.c:48
unsigned int uint32_t
Definition stdint.h:12
#define build_assert(condition)
Assert a condition at build time (after dead code elimination).
Definition assert.h:77
union @104331263140136355135267063077374276003064103115 u
#define DBGC2(...)
Definition compiler.h:547
static const uint32_t k[64]
MD5 constants.
Definition md5.c:50
void step(void)
Single-step a single process.
Definition process.c:99
static const struct sha1_step sha1_steps[4]
SHA-1 steps.
Definition sha1.c:95
struct digest_algorithm sha1_algorithm
#define container_of(ptr, type, field)
Get containing structure.
Definition stddef.h:36
SHA-1 digest and data block.
Definition sha1.h:43
struct sha1_digest digest
Digest of data already processed.
Definition sha1.h:45
uint32_t h[5]
Hash output.
Definition sha1.h:20
An SHA-1 step function.
Definition sha1.c:82
uint32_t(* f)(struct sha1_variables *v)
Calculate f(a,b,c,d).
Definition sha1.c:89
SHA-1 variables.
Definition sha1.c:40
uint32_t e
Definition sha1.c:46
uint32_t w[16]
Definition sha1.c:48
uint32_t a
Definition sha1.c:42
static u32 rol32(u32 v, int bits)
Rotate 32-bit value left.
Definition wpa_tkip.c:174

References sha1_variables::a, sha1_variables::b, build_assert, sha1_variables::c, container_of, sha1_variables::d, DBGC2, sha1_digest_data::digest, sha1_variables::e, sha1_step::f, sha1_digest::h, k, rol32(), sha1_algorithm, sha1_steps, step(), typeof(), u, and sha1_variables::w.

Referenced by MDHASH_ALGORITHM().

◆ MDHASH_ALGORITHM()

Variable Documentation

◆ sha1_steps

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

SHA-1 steps.

Definition at line 95 of file sha1.c.

95 {
96 /** 0 to 19 */
97 { .f = sha1_f_0_19, .k = 0x5a827999 },
98 /** 20 to 39 */
99 { .f = sha1_f_20_39_60_79, .k = 0x6ed9eba1 },
100 /** 40 to 59 */
101 { .f = sha1_f_40_59, .k = 0x8f1bbcdc },
102 /** 60 to 79 */
103 { .f = sha1_f_20_39_60_79, .k = 0xca62c1d6 },
104};

Referenced by sha1_compress().

◆ sha1_init

const struct sha1_digest sha1_init
static
Initial value:
= {
.h = { 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0 }
}

SHA-1 initial digest values.

Definition at line 107 of file sha1.c.

107 {
108 .h = { 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0 }
109};

Referenced by MDHASH_ALGORITHM().