iPXE
sha256.c File Reference

SHA-256 algorithm. More...

#include <stdint.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)
 FILE_SECBOOT (PERMITTED)
void sha256_compress (struct sha256_digest_data *dd, const struct sha256_digest *digest)
 Calculate SHA-256 digest of accumulated data.
 SHA256_ALGORITHM (sha256, sha256_algorithm, sha256_init, SHA256_DIGEST_SIZE)
 SHA-256 algorithm.

Variables

static const uint32_t k [SHA256_ROUNDS]
 SHA-256 constants.
static const struct sha256_digest sha256_init
 SHA-256 initial digest values.

Detailed Description

SHA-256 algorithm.

Definition in file sha256.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED )

◆ sha256_compress()

void sha256_compress ( struct sha256_digest_data * dd,
const struct sha256_digest * digest )

Calculate SHA-256 digest of accumulated data.

Parameters
ddDigest and data block
digestCopy of current digest value

Definition at line 83 of file sha256.c.

84 {
85 union {
86 struct sha256_digest_data dd;
87 struct sha256_variables v;
88 } *u = container_of ( dd, typeof ( *u ), dd );
89 struct sha256_variables *v = &u->v;
90 uint32_t *a = &v->a;
91 uint32_t *b = &v->b;
92 uint32_t *c = &v->c;
93 uint32_t *d = &v->d;
94 uint32_t *e = &v->e;
95 uint32_t *f = &v->f;
96 uint32_t *g = &v->g;
97 uint32_t *h = &v->h;
98 uint32_t *w = v->w;
99 uint32_t s0;
100 uint32_t s1;
101 uint32_t maj;
102 uint32_t t1;
103 uint32_t t2;
104 uint32_t ch;
105 unsigned int i;
106
107 /* Sanity checks */
108 build_assert ( &u->dd.digest.h[0] == a );
109 build_assert ( &u->dd.digest.h[1] == b );
110 build_assert ( &u->dd.digest.h[2] == c );
111 build_assert ( &u->dd.digest.h[3] == d );
112 build_assert ( &u->dd.digest.h[4] == e );
113 build_assert ( &u->dd.digest.h[5] == f );
114 build_assert ( &u->dd.digest.h[6] == g );
115 build_assert ( &u->dd.digest.h[7] == h );
116 build_assert ( &u->dd.data.dword[0] == w );
117 build_assert ( sizeof ( u->dd ) == sizeof ( u->v ) );
118
119 /* Main loop */
120 for ( i = 0 ; i < SHA256_ROUNDS ; i++ ) {
121 s0 = ( ror32 ( *a, 2 ) ^ ror32 ( *a, 13 ) ^ ror32 ( *a, 22 ) );
122 maj = ( ( *a & *b ) ^ ( *a & *c ) ^ ( *b & *c ) );
123 t2 = ( s0 + maj );
124 s1 = ( ror32 ( *e, 6 ) ^ ror32 ( *e, 11 ) ^ ror32 ( *e, 25 ) );
125 ch = ( ( *e & *f ) ^ ( (~*e) & *g ) );
126 t1 = ( *h + s1 + ch + k[i] + w[ i % 16 ] );
127 *h = *g;
128 *g = *f;
129 *f = *e;
130 *e = ( *d + t1 );
131 *d = *c;
132 *c = *b;
133 *b = *a;
134 *a = ( t1 + t2 );
135 s0 = ( ror32 ( w[ ( i - 15 ) % 16 ], 7 ) ^
136 ror32 ( w[ ( i - 15 ) % 16 ], 18 ) ^
137 ( w[ ( i - 15 ) % 16 ] >> 3 ) );
138 s1 = ( ror32 ( w[ ( i - 2 ) % 16 ], 17 ) ^
139 ror32 ( w[ ( i - 2 ) % 16 ], 19 ) ^
140 ( w[ ( i - 2 ) % 16 ] >> 10 ) );
141 w[ i % 16 ] = ( w[ ( i - 16 ) % 16 ] + s0 +
142 w[ ( i - 7 ) % 16 ] + s1 );
144 "%2d : %08x %08x %08x %08x %08x %08x %08x %08x\n",
145 i, *a, *b, *c, *d, *e, *f, *g, *h );
146 }
147
148 /* Add chunk to hash */
149 for ( i = 0 ; i < 8 ; i++ )
150 dd->digest.h[i] += digest->h[i];
151}
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
uint8_t h
Definition registers.h:4
uint8_t ch
Definition registers.h:1
#define SHA256_ROUNDS
SHA-256 number of rounds.
Definition sha256.h:19
struct digest_algorithm sha256_algorithm
#define container_of(ptr, type, field)
Get containing structure.
Definition stddef.h:36
SHA-256 digest and data block.
Definition sha256.h:47
struct sha256_digest digest
Digest of data already processed.
Definition sha256.h:49
uint32_t h[8]
Hash output.
Definition sha256.h:24
SHA-256 variables.
Definition sha256.c:40
uint32_t w[16]
Definition sha256.c:51
uint32_t f
Definition sha256.c:47
uint32_t c
Definition sha256.c:44
uint32_t e
Definition sha256.c:46
uint32_t d
Definition sha256.c:45
uint32_t a
Definition sha256.c:42
uint32_t b
Definition sha256.c:43
uint32_t h
Definition sha256.c:49
uint32_t g
Definition sha256.c:48
static u32 ror32(u32 v, int bits)
Rotate 32-bit value right.
Definition wpa_tkip.c:162

References sha256_variables::a, sha256_variables::b, build_assert, sha256_variables::c, ch, container_of, sha256_variables::d, DBGC2, sha256_digest_data::digest, sha256_variables::e, sha256_variables::f, sha256_variables::g, h, sha256_digest::h, sha256_variables::h, k, ror32(), sha256_algorithm, SHA256_ROUNDS, typeof(), u, and sha256_variables::w.

◆ SHA256_ALGORITHM()

SHA256_ALGORITHM ( sha256 ,
sha256_algorithm ,
sha256_init ,
SHA256_DIGEST_SIZE  )

SHA-256 algorithm.

References sha256_algorithm, SHA256_DIGEST_SIZE, and sha256_init.

Variable Documentation

◆ 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 55 of file sha256.c.

55 {
56 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
57 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
58 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
59 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
60 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
61 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
62 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
63 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
64 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
65 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
66 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
67};

◆ sha256_init

const struct sha256_digest sha256_init
static
Initial value:
= {
.h = {
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
}
}

SHA-256 initial digest values.

Definition at line 70 of file sha256.c.

70 {
71 .h = {
72 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
73 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
74 }
75};

Referenced by SHA256_ALGORITHM().