iPXE
sha256.h File Reference

SHA-256 algorithm. More...

#include <stdint.h>
#include <endian.h>
#include <ipxe/crypto.h>
#include <ipxe/mdhash.h>

Go to the source code of this file.

Data Structures

struct  sha256_digest
 An SHA-256 digest. More...
union  sha256_block
 An SHA-256 data block. More...
struct  sha256_digest_data
 SHA-256 digest and data block. More...

Macros

#define SHA256_ROUNDS   64
 SHA-256 number of rounds.
#define SHA256_CTX_SIZE   MDHASH_CTX_SIZE ( struct sha256_digest_data )
 SHA-256 context size.
#define SHA256_BLOCK_SIZE   sizeof ( union sha256_block )
 SHA-256 block size.
#define SHA256_DIGEST_SIZE   sizeof ( struct sha256_digest )
 SHA-256 digest size.
#define SHA224_DIGEST_SIZE   ( SHA256_DIGEST_SIZE * 224 / 256 )
 SHA-224 digest size.
#define SHA256_ALGORITHM(_name, _digest, _init, _digestsize)
 Define a SHA-256 family digest algorithm.

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.

Variables

struct digest_algorithm sha256_algorithm
struct digest_algorithm sha224_algorithm

Detailed Description

SHA-256 algorithm.

Definition in file sha256.h.

Macro Definition Documentation

◆ SHA256_ROUNDS

#define SHA256_ROUNDS   64

SHA-256 number of rounds.

Definition at line 19 of file sha256.h.

Referenced by sha256_compress().

◆ SHA256_CTX_SIZE

#define SHA256_CTX_SIZE   MDHASH_CTX_SIZE ( struct sha256_digest_data )

SHA-256 context size.

Definition at line 55 of file sha256.h.

Referenced by icert_cert().

◆ SHA256_BLOCK_SIZE

#define SHA256_BLOCK_SIZE   sizeof ( union sha256_block )

SHA-256 block size.

Definition at line 58 of file sha256.h.

◆ SHA256_DIGEST_SIZE

#define SHA256_DIGEST_SIZE   sizeof ( struct sha256_digest )

SHA-256 digest size.

Definition at line 61 of file sha256.h.

◆ SHA224_DIGEST_SIZE

#define SHA224_DIGEST_SIZE   ( SHA256_DIGEST_SIZE * 224 / 256 )

SHA-224 digest size.

Definition at line 64 of file sha256.h.

Referenced by SHA256_ALGORITHM().

◆ SHA256_ALGORITHM

#define SHA256_ALGORITHM ( _name,
_digest,
_init,
_digestsize )
Value:
MDHASH_ALGORITHM( _name, _digest, sha256_compress, \
_init, _digestsize )
#define __BIG_ENDIAN
Constant representing big-endian byte order.
Definition endian.h:22
#define MDHASH_ALGORITHM(_name, _digest, _compress, _byteorder, _dd, _init, _digestsize)
Define a Merkle-Damgård hash algorithm.
Definition mdhash.h:137
void sha256_compress(struct sha256_digest_data *dd, const struct sha256_digest *digest)
Calculate SHA-256 digest of accumulated data.
Definition sha256.c:83
SHA-256 digest and data block.
Definition sha256.h:47

Define a SHA-256 family digest algorithm.

Definition at line 70 of file sha256.h.

70#define SHA256_ALGORITHM( _name, _digest, _init, _digestsize ) \
71 MDHASH_ALGORITHM( _name, _digest, sha256_compress, \
72 __BIG_ENDIAN, struct sha256_digest_data, \
73 _init, _digestsize )

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 )
extern

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
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.

Variable Documentation

◆ sha256_algorithm

◆ sha224_algorithm