iPXE
sha256.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
22 */
23
24FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25FILE_SECBOOT ( PERMITTED );
26
27/** @file
28 *
29 * SHA-256 algorithm
30 *
31 */
32
33#include <stdint.h>
34#include <assert.h>
35#include <ipxe/rotate.h>
36#include <ipxe/crypto.h>
37#include <ipxe/sha256.h>
38
39/** SHA-256 variables */
41 /* This layout matches that of struct sha256_digest_data */
50 /* We reuse w[0..15] to construct w[16..63] on demand */
52} __attribute__ (( packed ));
53
54/** SHA-256 constants */
55static const uint32_t k[SHA256_ROUNDS] = {
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};
68
69/** SHA-256 initial digest values */
70static const struct sha256_digest sha256_init = {
71 .h = {
72 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
73 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
74 }
75};
76
77/**
78 * Calculate SHA-256 digest of accumulated data
79 *
80 * @v dd Digest and data block
81 * @v digest Copy of current digest value
82 */
84 const struct sha256_digest *digest ) {
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}
152
153/** SHA-256 algorithm */
#define SHA256_DIGEST_SIZE
Definition Tpm20.h:29
typeof(acpi_finder=acpi_find)
ACPI table finder.
Definition acpi.c:48
unsigned int uint32_t
Definition stdint.h:12
Assertions.
#define build_assert(condition)
Assert a condition at build time (after dead code elimination).
Definition assert.h:88
union @104331263140136355135267063077374276003064103115 u
#define DBGC2(...)
Definition compiler.h:547
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:921
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:951
#define __attribute__(x)
Definition compiler.h:10
Cryptographic API.
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
Bit operations.
static const struct sha256_digest sha256_init
SHA-256 initial digest values.
Definition sha256.c:70
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 algorithm.
#define SHA256_ALGORITHM(_name, _digest, _init, _digestsize)
Define a SHA-256 family digest algorithm.
Definition sha256.h:70
#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
An SHA-256 digest.
Definition sha256.h:22
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