|
@@ -0,0 +1,254 @@
|
|
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., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
17
|
+ */
|
|
18
|
+
|
|
19
|
+FILE_LICENCE ( GPL2_OR_LATER );
|
|
20
|
+
|
|
21
|
+/** @file
|
|
22
|
+ *
|
|
23
|
+ * SHA-256 algorithm
|
|
24
|
+ *
|
|
25
|
+ */
|
|
26
|
+
|
|
27
|
+#include <stdint.h>
|
|
28
|
+#include <string.h>
|
|
29
|
+#include <byteswap.h>
|
|
30
|
+#include <assert.h>
|
|
31
|
+#include <ipxe/crypto.h>
|
|
32
|
+#include <ipxe/sha256.h>
|
|
33
|
+
|
|
34
|
+/**
|
|
35
|
+ * Rotate dword right
|
|
36
|
+ *
|
|
37
|
+ * @v dword Dword
|
|
38
|
+ * @v rotate Amount of rotation
|
|
39
|
+ */
|
|
40
|
+static inline __attribute__ (( always_inline )) uint32_t
|
|
41
|
+ror32 ( uint32_t dword, unsigned int rotate ) {
|
|
42
|
+ return ( ( dword >> rotate ) | ( dword << ( 32 - rotate ) ) );
|
|
43
|
+}
|
|
44
|
+
|
|
45
|
+/** SHA-256 variables */
|
|
46
|
+struct sha256_variables {
|
|
47
|
+ /* This layout matches that of struct sha256_digest_data,
|
|
48
|
+ * allowing for efficient endianness-conversion,
|
|
49
|
+ */
|
|
50
|
+ uint32_t a;
|
|
51
|
+ uint32_t b;
|
|
52
|
+ uint32_t c;
|
|
53
|
+ uint32_t d;
|
|
54
|
+ uint32_t e;
|
|
55
|
+ uint32_t f;
|
|
56
|
+ uint32_t g;
|
|
57
|
+ uint32_t h;
|
|
58
|
+ uint32_t w[64];
|
|
59
|
+} __attribute__ (( packed ));
|
|
60
|
+
|
|
61
|
+/** SHA-256 constants */
|
|
62
|
+static const uint32_t k[64] = {
|
|
63
|
+ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
|
|
64
|
+ 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
|
|
65
|
+ 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
|
|
66
|
+ 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
|
67
|
+ 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
|
|
68
|
+ 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
|
|
69
|
+ 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
|
|
70
|
+ 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
|
71
|
+ 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
|
|
72
|
+ 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
|
|
73
|
+ 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
|
|
74
|
+};
|
|
75
|
+
|
|
76
|
+/**
|
|
77
|
+ * Initialise SHA-256 algorithm
|
|
78
|
+ *
|
|
79
|
+ * @v ctx SHA-256 context
|
|
80
|
+ */
|
|
81
|
+static void sha256_init ( void *ctx ) {
|
|
82
|
+ struct sha256_context *context = ctx;
|
|
83
|
+
|
|
84
|
+ context->ddd.dd.digest.h[0] = cpu_to_be32 ( 0x6a09e667 );
|
|
85
|
+ context->ddd.dd.digest.h[1] = cpu_to_be32 ( 0xbb67ae85 );
|
|
86
|
+ context->ddd.dd.digest.h[2] = cpu_to_be32 ( 0x3c6ef372 );
|
|
87
|
+ context->ddd.dd.digest.h[3] = cpu_to_be32 ( 0xa54ff53a );
|
|
88
|
+ context->ddd.dd.digest.h[4] = cpu_to_be32 ( 0x510e527f );
|
|
89
|
+ context->ddd.dd.digest.h[5] = cpu_to_be32 ( 0x9b05688c );
|
|
90
|
+ context->ddd.dd.digest.h[6] = cpu_to_be32 ( 0x1f83d9ab );
|
|
91
|
+ context->ddd.dd.digest.h[7] = cpu_to_be32 ( 0x5be0cd19 );
|
|
92
|
+ context->len = 0;
|
|
93
|
+}
|
|
94
|
+
|
|
95
|
+/**
|
|
96
|
+ * Calculate SHA-256 digest of accumulated data
|
|
97
|
+ *
|
|
98
|
+ * @v context SHA-256 context
|
|
99
|
+ */
|
|
100
|
+static void sha256_digest ( struct sha256_context *context ) {
|
|
101
|
+ union {
|
|
102
|
+ union sha256_digest_data_dwords ddd;
|
|
103
|
+ struct sha256_variables v;
|
|
104
|
+ } u;
|
|
105
|
+ uint32_t *a = &u.v.a;
|
|
106
|
+ uint32_t *b = &u.v.b;
|
|
107
|
+ uint32_t *c = &u.v.c;
|
|
108
|
+ uint32_t *d = &u.v.d;
|
|
109
|
+ uint32_t *e = &u.v.e;
|
|
110
|
+ uint32_t *f = &u.v.f;
|
|
111
|
+ uint32_t *g = &u.v.g;
|
|
112
|
+ uint32_t *h = &u.v.h;
|
|
113
|
+ uint32_t *w = u.v.w;
|
|
114
|
+ uint32_t s0;
|
|
115
|
+ uint32_t s1;
|
|
116
|
+ uint32_t maj;
|
|
117
|
+ uint32_t t1;
|
|
118
|
+ uint32_t t2;
|
|
119
|
+ uint32_t ch;
|
|
120
|
+ unsigned int i;
|
|
121
|
+
|
|
122
|
+ /* Sanity checks */
|
|
123
|
+ assert ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 );
|
|
124
|
+ linker_assert ( &u.ddd.dd.digest.h[0] == a, sha256_bad_layout );
|
|
125
|
+ linker_assert ( &u.ddd.dd.digest.h[1] == b, sha256_bad_layout );
|
|
126
|
+ linker_assert ( &u.ddd.dd.digest.h[2] == c, sha256_bad_layout );
|
|
127
|
+ linker_assert ( &u.ddd.dd.digest.h[3] == d, sha256_bad_layout );
|
|
128
|
+ linker_assert ( &u.ddd.dd.digest.h[4] == e, sha256_bad_layout );
|
|
129
|
+ linker_assert ( &u.ddd.dd.digest.h[5] == f, sha256_bad_layout );
|
|
130
|
+ linker_assert ( &u.ddd.dd.digest.h[6] == g, sha256_bad_layout );
|
|
131
|
+ linker_assert ( &u.ddd.dd.digest.h[7] == h, sha256_bad_layout );
|
|
132
|
+ linker_assert ( &u.ddd.dd.data.dword[0] == w, sha256_bad_layout );
|
|
133
|
+
|
|
134
|
+ DBGC ( context, "SHA256 digesting:\n" );
|
|
135
|
+ DBGC_HDA ( context, 0, &context->ddd.dd.digest,
|
|
136
|
+ sizeof ( context->ddd.dd.digest ) );
|
|
137
|
+ DBGC_HDA ( context, context->len, &context->ddd.dd.data,
|
|
138
|
+ sizeof ( context->ddd.dd.data ) );
|
|
139
|
+
|
|
140
|
+ /* Convert h[0..7] to host-endian, and initialise a, b, c, d,
|
|
141
|
+ * e, f, g, h, and w[0..15]
|
|
142
|
+ */
|
|
143
|
+ for ( i = 0 ; i < ( sizeof ( u.ddd.dword ) /
|
|
144
|
+ sizeof ( u.ddd.dword[0] ) ) ; i++ ) {
|
|
145
|
+ be32_to_cpus ( &context->ddd.dword[i] );
|
|
146
|
+ u.ddd.dword[i] = context->ddd.dword[i];
|
|
147
|
+ }
|
|
148
|
+
|
|
149
|
+ /* Initialise w[16..63] */
|
|
150
|
+ for ( i = 16 ; i < 64 ; i++ ) {
|
|
151
|
+ s0 = ( ror32 ( w[i-15], 7 ) ^ ror32 ( w[i-15], 18 ) ^
|
|
152
|
+ ( w[i-15] >> 3 ) );
|
|
153
|
+ s1 = ( ror32 ( w[i-2], 17 ) ^ ror32 ( w[i-2], 19 ) ^
|
|
154
|
+ ( w[i-2] >> 10 ) );
|
|
155
|
+ w[i] = ( w[i-16] + s0 + w[i-7] + s1 );
|
|
156
|
+ }
|
|
157
|
+
|
|
158
|
+ /* Main loop */
|
|
159
|
+ for ( i = 0 ; i < 64 ; i++ ) {
|
|
160
|
+ s0 = ( ror32 ( *a, 2 ) ^ ror32 ( *a, 13 ) ^ ror32 ( *a, 22 ) );
|
|
161
|
+ maj = ( ( *a & *b ) ^ ( *a & *c ) ^ ( *b & *c ) );
|
|
162
|
+ t2 = ( s0 + maj );
|
|
163
|
+ s1 = ( ror32 ( *e, 6 ) ^ ror32 ( *e, 11 ) ^ ror32 ( *e, 25 ) );
|
|
164
|
+ ch = ( ( *e & *f ) ^ ( (~*e) & *g ) );
|
|
165
|
+ t1 = ( *h + s1 + ch + k[i] + w[i] );
|
|
166
|
+ *h = *g;
|
|
167
|
+ *g = *f;
|
|
168
|
+ *f = *e;
|
|
169
|
+ *e = ( *d + t1 );
|
|
170
|
+ *d = *c;
|
|
171
|
+ *c = *b;
|
|
172
|
+ *b = *a;
|
|
173
|
+ *a = ( t1 + t2 );
|
|
174
|
+ DBGC2 ( context, "%2d : %08x %08x %08x %08x %08x %08x %08x "
|
|
175
|
+ "%08x\n", i, *a, *b, *c, *d, *e, *f, *g, *h );
|
|
176
|
+ }
|
|
177
|
+
|
|
178
|
+ /* Add chunk to hash and convert back to big-endian */
|
|
179
|
+ for ( i = 0 ; i < 8 ; i++ ) {
|
|
180
|
+ context->ddd.dd.digest.h[i] =
|
|
181
|
+ cpu_to_be32 ( context->ddd.dd.digest.h[i] +
|
|
182
|
+ u.ddd.dd.digest.h[i] );
|
|
183
|
+ }
|
|
184
|
+
|
|
185
|
+ DBGC ( context, "SHA256 digested:\n" );
|
|
186
|
+ DBGC_HDA ( context, 0, &context->ddd.dd.digest,
|
|
187
|
+ sizeof ( context->ddd.dd.digest ) );
|
|
188
|
+}
|
|
189
|
+
|
|
190
|
+/**
|
|
191
|
+ * Accumulate data with SHA-256 algorithm
|
|
192
|
+ *
|
|
193
|
+ * @v ctx SHA-256 context
|
|
194
|
+ * @v data Data
|
|
195
|
+ * @v len Length of data
|
|
196
|
+ */
|
|
197
|
+static void sha256_update ( void *ctx, const void *data, size_t len ) {
|
|
198
|
+ struct sha256_context *context = ctx;
|
|
199
|
+ const uint8_t *byte = data;
|
|
200
|
+ size_t offset;
|
|
201
|
+
|
|
202
|
+ /* Accumulate data a byte at a time, performing the digest
|
|
203
|
+ * whenever we fill the data buffer
|
|
204
|
+ */
|
|
205
|
+ while ( len-- ) {
|
|
206
|
+ offset = ( context->len % sizeof ( context->ddd.dd.data ) );
|
|
207
|
+ context->ddd.dd.data.byte[offset] = *(byte++);
|
|
208
|
+ context->len++;
|
|
209
|
+ if ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 )
|
|
210
|
+ sha256_digest ( context );
|
|
211
|
+ }
|
|
212
|
+}
|
|
213
|
+
|
|
214
|
+/**
|
|
215
|
+ * Generate SHA-256 digest
|
|
216
|
+ *
|
|
217
|
+ * @v ctx SHA-256 context
|
|
218
|
+ * @v out Output buffer
|
|
219
|
+ */
|
|
220
|
+static void sha256_final ( void *ctx, void *out ) {
|
|
221
|
+ struct sha256_context *context = ctx;
|
|
222
|
+ uint64_t len_bits;
|
|
223
|
+ uint8_t pad;
|
|
224
|
+
|
|
225
|
+ /* Record length before pre-processing */
|
|
226
|
+ len_bits = cpu_to_be64 ( ( ( uint64_t ) context->len ) * 8 );
|
|
227
|
+
|
|
228
|
+ /* Pad with a single "1" bit followed by as many "0" bits as required */
|
|
229
|
+ pad = 0x80;
|
|
230
|
+ do {
|
|
231
|
+ sha256_update ( ctx, &pad, sizeof ( pad ) );
|
|
232
|
+ pad = 0x00;
|
|
233
|
+ } while ( ( context->len % sizeof ( context->ddd.dd.data ) ) !=
|
|
234
|
+ offsetof ( typeof ( context->ddd.dd.data ), final.len ) );
|
|
235
|
+
|
|
236
|
+ /* Append length (in bits) */
|
|
237
|
+ sha256_update ( ctx, &len_bits, sizeof ( len_bits ) );
|
|
238
|
+ assert ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 );
|
|
239
|
+
|
|
240
|
+ /* Copy out final digest */
|
|
241
|
+ memcpy ( out, &context->ddd.dd.digest,
|
|
242
|
+ sizeof ( context->ddd.dd.digest ) );
|
|
243
|
+}
|
|
244
|
+
|
|
245
|
+/** SHA-256 algorithm */
|
|
246
|
+struct digest_algorithm sha256_algorithm = {
|
|
247
|
+ .name = "sha256",
|
|
248
|
+ .ctxsize = sizeof ( struct sha256_context ),
|
|
249
|
+ .blocksize = sizeof ( union sha256_block ),
|
|
250
|
+ .digestsize = sizeof ( struct sha256_digest ),
|
|
251
|
+ .init = sha256_init,
|
|
252
|
+ .update = sha256_update,
|
|
253
|
+ .final = sha256_final,
|
|
254
|
+};
|