iPXE
sha1.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-1 algorithm
30 *
31 */
32
33#include <stdint.h>
34#include <string.h>
35#include <byteswap.h>
36#include <assert.h>
37#include <ipxe/rotate.h>
38#include <ipxe/crypto.h>
39#include <ipxe/sha1.h>
40
41/** SHA-1 variables */
43 /* This layout matches that of struct sha1_digest_data,
44 * allowing for efficient endianness-conversion,
45 */
52} __attribute__ (( packed ));
53
54/**
55 * f(a,b,c,d) for steps 0 to 19
56 *
57 * @v v SHA-1 variables
58 * @ret f f(a,b,c,d)
59 */
60static uint32_t sha1_f_0_19 ( struct sha1_variables *v ) {
61 return ( ( v->b & v->c ) | ( (~v->b) & v->d ) );
62}
63
64/**
65 * f(a,b,c,d) for steps 20 to 39 and 60 to 79
66 *
67 * @v v SHA-1 variables
68 * @ret f f(a,b,c,d)
69 */
71 return ( v->b ^ v->c ^ v->d );
72}
73
74/**
75 * f(a,b,c,d) for steps 40 to 59
76 *
77 * @v v SHA-1 variables
78 * @ret f f(a,b,c,d)
79 */
80static uint32_t sha1_f_40_59 ( struct sha1_variables *v ) {
81 return ( ( v->b & v->c ) | ( v->b & v->d ) | ( v->c & v->d ) );
82}
83
84/** An SHA-1 step function */
85struct sha1_step {
86 /**
87 * Calculate f(a,b,c,d)
88 *
89 * @v v SHA-1 variables
90 * @ret f f(a,b,c,d)
91 */
92 uint32_t ( * f ) ( struct sha1_variables *v );
93 /** Constant k */
95};
96
97/** SHA-1 steps */
98static struct sha1_step sha1_steps[4] = {
99 /** 0 to 19 */
100 { .f = sha1_f_0_19, .k = 0x5a827999 },
101 /** 20 to 39 */
102 { .f = sha1_f_20_39_60_79, .k = 0x6ed9eba1 },
103 /** 40 to 59 */
104 { .f = sha1_f_40_59, .k = 0x8f1bbcdc },
105 /** 60 to 79 */
106 { .f = sha1_f_20_39_60_79, .k = 0xca62c1d6 },
107};
108
109/**
110 * Initialise SHA-1 algorithm
111 *
112 * @v ctx SHA-1 context
113 */
114static void sha1_init ( void *ctx ) {
115 struct sha1_context *context = ctx;
116
117 context->ddd.dd.digest.h[0] = cpu_to_be32 ( 0x67452301 );
118 context->ddd.dd.digest.h[1] = cpu_to_be32 ( 0xefcdab89 );
119 context->ddd.dd.digest.h[2] = cpu_to_be32 ( 0x98badcfe );
120 context->ddd.dd.digest.h[3] = cpu_to_be32 ( 0x10325476 );
121 context->ddd.dd.digest.h[4] = cpu_to_be32 ( 0xc3d2e1f0 );
122 context->len = 0;
123}
124
125/**
126 * Calculate SHA-1 digest of accumulated data
127 *
128 * @v context SHA-1 context
129 */
130static void sha1_digest ( struct sha1_context *context ) {
131 union {
133 struct sha1_variables v;
134 } u;
135 uint32_t *a = &u.v.a;
136 uint32_t *b = &u.v.b;
137 uint32_t *c = &u.v.c;
138 uint32_t *d = &u.v.d;
139 uint32_t *e = &u.v.e;
140 uint32_t *w = u.v.w;
141 uint32_t f;
142 uint32_t k;
143 uint32_t temp;
144 struct sha1_step *step;
145 unsigned int i;
146
147 /* Sanity checks */
148 assert ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 );
149 build_assert ( &u.ddd.dd.digest.h[0] == a );
150 build_assert ( &u.ddd.dd.digest.h[1] == b );
151 build_assert ( &u.ddd.dd.digest.h[2] == c );
152 build_assert ( &u.ddd.dd.digest.h[3] == d );
153 build_assert ( &u.ddd.dd.digest.h[4] == e );
154 build_assert ( &u.ddd.dd.data.dword[0] == w );
155
156 DBGC ( context, "SHA1 digesting:\n" );
157 DBGC_HDA ( context, 0, &context->ddd.dd.digest,
158 sizeof ( context->ddd.dd.digest ) );
159 DBGC_HDA ( context, context->len, &context->ddd.dd.data,
160 sizeof ( context->ddd.dd.data ) );
161
162 /* Convert h[0..4] to host-endian, and initialise a, b, c, d,
163 * e, and w[0..15]
164 */
165 for ( i = 0 ; i < ( sizeof ( u.ddd.dword ) /
166 sizeof ( u.ddd.dword[0] ) ) ; i++ ) {
167 be32_to_cpus ( &context->ddd.dword[i] );
168 u.ddd.dword[i] = context->ddd.dword[i];
169 }
170
171 /* Initialise w[16..79] */
172 for ( i = 16 ; i < 80 ; i++ )
173 w[i] = rol32 ( ( w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16] ), 1 );
174
175 /* Main loop */
176 for ( i = 0 ; i < 80 ; i++ ) {
177 step = &sha1_steps[ i / 20 ];
178 f = step->f ( &u.v );
179 k = step->k;
180 temp = ( rol32 ( *a, 5 ) + f + *e + k + w[i] );
181 *e = *d;
182 *d = *c;
183 *c = rol32 ( *b, 30 );
184 *b = *a;
185 *a = temp;
186 DBGC2 ( context, "%2d : %08x %08x %08x %08x %08x\n",
187 i, *a, *b, *c, *d, *e );
188 }
189
190 /* Add chunk to hash and convert back to big-endian */
191 for ( i = 0 ; i < 5 ; i++ ) {
192 context->ddd.dd.digest.h[i] =
193 cpu_to_be32 ( context->ddd.dd.digest.h[i] +
194 u.ddd.dd.digest.h[i] );
195 }
196
197 DBGC ( context, "SHA1 digested:\n" );
198 DBGC_HDA ( context, 0, &context->ddd.dd.digest,
199 sizeof ( context->ddd.dd.digest ) );
200}
201
202/**
203 * Accumulate data with SHA-1 algorithm
204 *
205 * @v ctx SHA-1 context
206 * @v data Data
207 * @v len Length of data
208 */
209static void sha1_update ( void *ctx, const void *data, size_t len ) {
210 struct sha1_context *context = ctx;
211 const uint8_t *byte = data;
212 size_t offset;
213
214 /* Accumulate data a byte at a time, performing the digest
215 * whenever we fill the data buffer
216 */
217 while ( len-- ) {
218 offset = ( context->len % sizeof ( context->ddd.dd.data ) );
219 context->ddd.dd.data.byte[offset] = *(byte++);
220 context->len++;
221 if ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 )
222 sha1_digest ( context );
223 }
224}
225
226/**
227 * Generate SHA-1 digest
228 *
229 * @v ctx SHA-1 context
230 * @v out Output buffer
231 */
232static void sha1_final ( void *ctx, void *out ) {
233 struct sha1_context *context = ctx;
234 uint64_t len_bits;
235 uint8_t pad;
236
237 /* Record length before pre-processing */
238 len_bits = cpu_to_be64 ( ( ( uint64_t ) context->len ) * 8 );
239
240 /* Pad with a single "1" bit followed by as many "0" bits as required */
241 pad = 0x80;
242 do {
243 sha1_update ( ctx, &pad, sizeof ( pad ) );
244 pad = 0x00;
245 } while ( ( context->len % sizeof ( context->ddd.dd.data ) ) !=
246 offsetof ( typeof ( context->ddd.dd.data ), final.len ) );
247
248 /* Append length (in bits) */
249 sha1_update ( ctx, &len_bits, sizeof ( len_bits ) );
250 assert ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 );
251
252 /* Copy out final digest */
253 memcpy ( out, &context->ddd.dd.digest,
254 sizeof ( context->ddd.dd.digest ) );
255}
256
257/** SHA-1 algorithm */
259 .name = "sha1",
260 .ctxsize = sizeof ( struct sha1_context ),
261 .blocksize = sizeof ( union sha1_block ),
262 .digestsize = sizeof ( struct sha1_digest ),
263 .init = sha1_init,
264 .update = sha1_update,
265 .final = sha1_final,
266};
struct golan_eq_context ctx
Definition CIB_PRM.h:0
__be32 out[4]
Definition CIB_PRM.h:8
typeof(acpi_finder=acpi_find)
ACPI table finder.
Definition acpi.c:48
u32 pad[9]
Padding.
Definition ar9003_mac.h:23
unsigned int uint32_t
Definition stdint.h:12
unsigned long long uint64_t
Definition stdint.h:13
unsigned char uint8_t
Definition stdint.h:10
Assertions.
#define build_assert(condition)
Assert a condition at build time (after dead code elimination)
Definition assert.h:77
#define assert(condition)
Assert a condition at run-time.
Definition assert.h:50
uint16_t offset
Offset to command line.
Definition bzimage.h:3
union @104331263140136355135267063077374276003064103115 u
ring len
Length.
Definition dwmac.h:226
uint8_t data[48]
Additional event data.
Definition ena.h:11
#define DBGC2(...)
Definition compiler.h:522
#define DBGC(...)
Definition compiler.h:505
#define DBGC_HDA(...)
Definition compiler.h:506
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:926
#define be32_to_cpus(ptr)
Definition byteswap.h:129
#define cpu_to_be32(value)
Definition byteswap.h:111
#define cpu_to_be64(value)
Definition byteswap.h:112
#define __attribute__(x)
Definition compiler.h:10
Cryptographic API.
String functions.
void * memcpy(void *dest, const void *src, size_t len) __nonnull
static const uint32_t k[64]
MD5 constants.
Definition md5.c:54
uint32_t digestsize
Digest size (i.e.
Definition pccrr.h:1
void step(void)
Single-step a single process.
Definition process.c:99
Bit operations.
struct digest_algorithm sha1_algorithm
SHA-1 algorithm.
Definition sha1.c:258
static uint32_t sha1_f_40_59(struct sha1_variables *v)
f(a,b,c,d) for steps 40 to 59
Definition sha1.c:80
static uint32_t sha1_f_0_19(struct sha1_variables *v)
f(a,b,c,d) for steps 0 to 19
Definition sha1.c:60
static void sha1_final(void *ctx, void *out)
Generate SHA-1 digest.
Definition sha1.c:232
static struct sha1_step sha1_steps[4]
SHA-1 steps.
Definition sha1.c:98
static void sha1_init(void *ctx)
Initialise SHA-1 algorithm.
Definition sha1.c:114
static void sha1_digest(struct sha1_context *context)
Calculate SHA-1 digest of accumulated data.
Definition sha1.c:130
static void sha1_update(void *ctx, const void *data, size_t len)
Accumulate data with SHA-1 algorithm.
Definition sha1.c:209
static uint32_t sha1_f_20_39_60_79(struct sha1_variables *v)
f(a,b,c,d) for steps 20 to 39 and 60 to 79
Definition sha1.c:70
SHA-1 algorithm.
#define offsetof(type, field)
Get offset of a field within a structure.
Definition stddef.h:25
A message digest algorithm.
Definition crypto.h:19
An SHA-1 context.
Definition sha1.h:59
union sha1_digest_data_dwords ddd
Digest and accumulated data.
Definition sha1.h:63
size_t len
Amount of accumulated data.
Definition sha1.h:61
union sha1_block data
Accumulated data.
Definition sha1.h:46
struct sha1_digest digest
Digest of data already processed.
Definition sha1.h:44
An SHA-1 digest.
Definition sha1.h:17
uint32_t h[5]
Hash output.
Definition sha1.h:19
An SHA-1 step function.
Definition sha1.c:85
uint32_t k
Constant k.
Definition sha1.c:94
uint32_t(* f)(struct sha1_variables *v)
Calculate f(a,b,c,d)
Definition sha1.c:92
SHA-1 variables.
Definition sha1.c:42
uint32_t w[80]
Definition sha1.c:51
uint32_t d
Definition sha1.c:49
uint32_t c
Definition sha1.c:48
uint32_t e
Definition sha1.c:50
uint32_t b
Definition sha1.c:47
uint32_t a
Definition sha1.c:46
An SHA-1 data block.
Definition sha1.h:23
uint8_t byte[64]
Raw bytes.
Definition sha1.h:25
SHA-1 digest and data block.
Definition sha1.h:50
struct sha1_digest_data dd
Digest and data block.
Definition sha1.h:52
uint32_t dword[sizeof(struct sha1_digest_data)/sizeof(uint32_t)]
Raw dwords.
Definition sha1.h:55
static u32 rol32(u32 v, int bits)
Rotate 32-bit value left.
Definition wpa_tkip.c:174