|
@@ -0,0 +1,270 @@
|
|
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-1 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/sha1.h>
|
|
33
|
+
|
|
34
|
+/**
|
|
35
|
+ * Rotate dword left
|
|
36
|
+ *
|
|
37
|
+ * @v dword Dword
|
|
38
|
+ * @v rotate Amount of rotation
|
|
39
|
+ */
|
|
40
|
+static inline __attribute__ (( always_inline )) uint32_t
|
|
41
|
+rol32 ( uint32_t dword, unsigned int rotate ) {
|
|
42
|
+ return ( ( dword << rotate ) | ( dword >> ( 32 - rotate ) ) );
|
|
43
|
+}
|
|
44
|
+
|
|
45
|
+/** SHA-1 variables */
|
|
46
|
+struct sha1_variables {
|
|
47
|
+ /* This layout matches that of struct sha1_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 w[80];
|
|
56
|
+} __attribute__ (( packed ));
|
|
57
|
+
|
|
58
|
+/**
|
|
59
|
+ * f(a,b,c,d) for steps 0 to 19
|
|
60
|
+ *
|
|
61
|
+ * @v v SHA-1 variables
|
|
62
|
+ * @ret f f(a,b,c,d)
|
|
63
|
+ */
|
|
64
|
+static uint32_t sha1_f_0_19 ( struct sha1_variables *v ) {
|
|
65
|
+ return ( ( v->b & v->c ) | ( (~v->b) & v->d ) );
|
|
66
|
+}
|
|
67
|
+
|
|
68
|
+/**
|
|
69
|
+ * f(a,b,c,d) for steps 20 to 39 and 60 to 79
|
|
70
|
+ *
|
|
71
|
+ * @v v SHA-1 variables
|
|
72
|
+ * @ret f f(a,b,c,d)
|
|
73
|
+ */
|
|
74
|
+static uint32_t sha1_f_20_39_60_79 ( struct sha1_variables *v ) {
|
|
75
|
+ return ( v->b ^ v->c ^ v->d );
|
|
76
|
+}
|
|
77
|
+
|
|
78
|
+/**
|
|
79
|
+ * f(a,b,c,d) for steps 40 to 59
|
|
80
|
+ *
|
|
81
|
+ * @v v SHA-1 variables
|
|
82
|
+ * @ret f f(a,b,c,d)
|
|
83
|
+ */
|
|
84
|
+static uint32_t sha1_f_40_59 ( struct sha1_variables *v ) {
|
|
85
|
+ return ( ( v->b & v->c ) | ( v->b & v->d ) | ( v->c & v->d ) );
|
|
86
|
+}
|
|
87
|
+
|
|
88
|
+/** An SHA-1 step function */
|
|
89
|
+struct sha1_step {
|
|
90
|
+ /**
|
|
91
|
+ * Calculate f(a,b,c,d)
|
|
92
|
+ *
|
|
93
|
+ * @v v SHA-1 variables
|
|
94
|
+ * @ret f f(a,b,c,d)
|
|
95
|
+ */
|
|
96
|
+ uint32_t ( * f ) ( struct sha1_variables *v );
|
|
97
|
+ /** Constant k */
|
|
98
|
+ uint32_t k;
|
|
99
|
+};
|
|
100
|
+
|
|
101
|
+/** SHA-1 steps */
|
|
102
|
+static struct sha1_step sha1_steps[4] = {
|
|
103
|
+ /** 0 to 19 */
|
|
104
|
+ { .f = sha1_f_0_19, .k = 0x5a827999 },
|
|
105
|
+ /** 20 to 39 */
|
|
106
|
+ { .f = sha1_f_20_39_60_79, .k = 0x6ed9eba1 },
|
|
107
|
+ /** 40 to 59 */
|
|
108
|
+ { .f = sha1_f_40_59, .k = 0x8f1bbcdc },
|
|
109
|
+ /** 60 to 79 */
|
|
110
|
+ { .f = sha1_f_20_39_60_79, .k = 0xca62c1d6 },
|
|
111
|
+};
|
|
112
|
+
|
|
113
|
+/**
|
|
114
|
+ * Initialise SHA-1 algorithm
|
|
115
|
+ *
|
|
116
|
+ * @v ctx SHA-1 context
|
|
117
|
+ */
|
|
118
|
+static void sha1_init ( void *ctx ) {
|
|
119
|
+ struct sha1_context *context = ctx;
|
|
120
|
+
|
|
121
|
+ context->ddd.dd.digest.h[0] = cpu_to_be32 ( 0x67452301 );
|
|
122
|
+ context->ddd.dd.digest.h[1] = cpu_to_be32 ( 0xefcdab89 );
|
|
123
|
+ context->ddd.dd.digest.h[2] = cpu_to_be32 ( 0x98badcfe );
|
|
124
|
+ context->ddd.dd.digest.h[3] = cpu_to_be32 ( 0x10325476 );
|
|
125
|
+ context->ddd.dd.digest.h[4] = cpu_to_be32 ( 0xc3d2e1f0 );
|
|
126
|
+ context->len = 0;
|
|
127
|
+}
|
|
128
|
+
|
|
129
|
+/**
|
|
130
|
+ * Calculate SHA-1 digest of accumulated data
|
|
131
|
+ *
|
|
132
|
+ * @v context SHA-1 context
|
|
133
|
+ */
|
|
134
|
+static void sha1_digest ( struct sha1_context *context ) {
|
|
135
|
+ union {
|
|
136
|
+ union sha1_digest_data_dwords ddd;
|
|
137
|
+ struct sha1_variables v;
|
|
138
|
+ } u;
|
|
139
|
+ uint32_t *a = &u.v.a;
|
|
140
|
+ uint32_t *b = &u.v.b;
|
|
141
|
+ uint32_t *c = &u.v.c;
|
|
142
|
+ uint32_t *d = &u.v.d;
|
|
143
|
+ uint32_t *e = &u.v.e;
|
|
144
|
+ uint32_t *w = u.v.w;
|
|
145
|
+ uint32_t f;
|
|
146
|
+ uint32_t k;
|
|
147
|
+ uint32_t temp;
|
|
148
|
+ struct sha1_step *step;
|
|
149
|
+ unsigned int i;
|
|
150
|
+
|
|
151
|
+ /* Sanity checks */
|
|
152
|
+ assert ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 );
|
|
153
|
+ linker_assert ( &u.ddd.dd.digest.h[0] == a, sha1_bad_layout );
|
|
154
|
+ linker_assert ( &u.ddd.dd.digest.h[1] == b, sha1_bad_layout );
|
|
155
|
+ linker_assert ( &u.ddd.dd.digest.h[2] == c, sha1_bad_layout );
|
|
156
|
+ linker_assert ( &u.ddd.dd.digest.h[3] == d, sha1_bad_layout );
|
|
157
|
+ linker_assert ( &u.ddd.dd.digest.h[4] == e, sha1_bad_layout );
|
|
158
|
+ linker_assert ( &u.ddd.dd.data.dword[0] == w, sha1_bad_layout );
|
|
159
|
+
|
|
160
|
+ DBGC ( context, "SHA1 digesting:\n" );
|
|
161
|
+ DBGC_HDA ( context, 0, &context->ddd.dd.digest,
|
|
162
|
+ sizeof ( context->ddd.dd.digest ) );
|
|
163
|
+ DBGC_HDA ( context, context->len, &context->ddd.dd.data,
|
|
164
|
+ sizeof ( context->ddd.dd.data ) );
|
|
165
|
+
|
|
166
|
+ /* Convert h[0..4] to host-endian, and initialise a, b, c, d,
|
|
167
|
+ * e, and w[0..15]
|
|
168
|
+ */
|
|
169
|
+ for ( i = 0 ; i < ( sizeof ( u.ddd.dword ) /
|
|
170
|
+ sizeof ( u.ddd.dword[0] ) ) ; i++ ) {
|
|
171
|
+ be32_to_cpus ( &context->ddd.dword[i] );
|
|
172
|
+ u.ddd.dword[i] = context->ddd.dword[i];
|
|
173
|
+ }
|
|
174
|
+
|
|
175
|
+ /* Initialise w[16..79] */
|
|
176
|
+ for ( i = 16 ; i < 80 ; i++ )
|
|
177
|
+ w[i] = rol32 ( ( w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16] ), 1 );
|
|
178
|
+
|
|
179
|
+ /* Main loop */
|
|
180
|
+ for ( i = 0 ; i < 80 ; i++ ) {
|
|
181
|
+ step = &sha1_steps[ i / 20 ];
|
|
182
|
+ f = step->f ( &u.v );
|
|
183
|
+ k = step->k;
|
|
184
|
+ temp = ( rol32 ( *a, 5 ) + f + *e + k + w[i] );
|
|
185
|
+ *e = *d;
|
|
186
|
+ *d = *c;
|
|
187
|
+ *c = rol32 ( *b, 30 );
|
|
188
|
+ *b = *a;
|
|
189
|
+ *a = temp;
|
|
190
|
+ DBGC2 ( context, "%2d : %08x %08x %08x %08x %08x\n",
|
|
191
|
+ i, *a, *b, *c, *d, *e );
|
|
192
|
+ }
|
|
193
|
+
|
|
194
|
+ /* Add chunk to hash and convert back to big-endian */
|
|
195
|
+ for ( i = 0 ; i < 5 ; i++ ) {
|
|
196
|
+ context->ddd.dd.digest.h[i] =
|
|
197
|
+ cpu_to_be32 ( context->ddd.dd.digest.h[i] +
|
|
198
|
+ u.ddd.dd.digest.h[i] );
|
|
199
|
+ }
|
|
200
|
+
|
|
201
|
+ DBGC ( context, "SHA1 digested:\n" );
|
|
202
|
+ DBGC_HDA ( context, 0, &context->ddd.dd.digest,
|
|
203
|
+ sizeof ( context->ddd.dd.digest ) );
|
|
204
|
+}
|
|
205
|
+
|
|
206
|
+/**
|
|
207
|
+ * Accumulate data with SHA-1 algorithm
|
|
208
|
+ *
|
|
209
|
+ * @v ctx SHA-1 context
|
|
210
|
+ * @v data Data
|
|
211
|
+ * @v len Length of data
|
|
212
|
+ */
|
|
213
|
+static void sha1_update ( void *ctx, const void *data, size_t len ) {
|
|
214
|
+ struct sha1_context *context = ctx;
|
|
215
|
+ const uint8_t *byte = data;
|
|
216
|
+ size_t offset;
|
|
217
|
+
|
|
218
|
+ /* Accumulate data a byte at a time, performing the digest
|
|
219
|
+ * whenever we fill the data buffer
|
|
220
|
+ */
|
|
221
|
+ while ( len-- ) {
|
|
222
|
+ offset = ( context->len % sizeof ( context->ddd.dd.data ) );
|
|
223
|
+ context->ddd.dd.data.byte[offset] = *(byte++);
|
|
224
|
+ context->len++;
|
|
225
|
+ if ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 )
|
|
226
|
+ sha1_digest ( context );
|
|
227
|
+ }
|
|
228
|
+}
|
|
229
|
+
|
|
230
|
+/**
|
|
231
|
+ * Generate SHA-1 digest
|
|
232
|
+ *
|
|
233
|
+ * @v ctx SHA-1 context
|
|
234
|
+ * @v out Output buffer
|
|
235
|
+ */
|
|
236
|
+static void sha1_final ( void *ctx, void *out ) {
|
|
237
|
+ struct sha1_context *context = ctx;
|
|
238
|
+ uint64_t len_bits;
|
|
239
|
+ uint8_t pad;
|
|
240
|
+
|
|
241
|
+ /* Record length before pre-processing */
|
|
242
|
+ len_bits = cpu_to_be64 ( ( ( uint64_t ) context->len ) * 8 );
|
|
243
|
+
|
|
244
|
+ /* Pad with a single "1" bit followed by as many "0" bits as required */
|
|
245
|
+ pad = 0x80;
|
|
246
|
+ do {
|
|
247
|
+ sha1_update ( ctx, &pad, sizeof ( pad ) );
|
|
248
|
+ pad = 0x00;
|
|
249
|
+ } while ( ( context->len % sizeof ( context->ddd.dd.data ) ) !=
|
|
250
|
+ offsetof ( typeof ( context->ddd.dd.data ), final.len ) );
|
|
251
|
+
|
|
252
|
+ /* Append length (in bits) */
|
|
253
|
+ sha1_update ( ctx, &len_bits, sizeof ( len_bits ) );
|
|
254
|
+ assert ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 );
|
|
255
|
+
|
|
256
|
+ /* Copy out final digest */
|
|
257
|
+ memcpy ( out, &context->ddd.dd.digest,
|
|
258
|
+ sizeof ( context->ddd.dd.digest ) );
|
|
259
|
+}
|
|
260
|
+
|
|
261
|
+/** SHA-1 algorithm */
|
|
262
|
+struct digest_algorithm sha1_algorithm = {
|
|
263
|
+ .name = "sha1",
|
|
264
|
+ .ctxsize = sizeof ( struct sha1_context ),
|
|
265
|
+ .blocksize = sizeof ( union sha1_block ),
|
|
266
|
+ .digestsize = sizeof ( struct sha1_digest ),
|
|
267
|
+ .init = sha1_init,
|
|
268
|
+ .update = sha1_update,
|
|
269
|
+ .final = sha1_final,
|
|
270
|
+};
|