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 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 /** @file
27  *
28  * SHA-256 algorithm
29  *
30  */
31 
32 #include <stdint.h>
33 #include <string.h>
34 #include <byteswap.h>
35 #include <assert.h>
36 #include <ipxe/rotate.h>
37 #include <ipxe/crypto.h>
38 #include <ipxe/asn1.h>
39 #include <ipxe/sha256.h>
40 
41 /** SHA-256 variables */
43  /* This layout matches that of struct sha256_digest_data,
44  * allowing for efficient endianness-conversion,
45  */
55 } __attribute__ (( packed ));
56 
57 /** SHA-256 constants */
58 static const uint32_t k[SHA256_ROUNDS] = {
59  0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
60  0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
61  0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
62  0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
63  0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
64  0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
65  0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
66  0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
67  0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
68  0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
69  0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
70 };
71 
72 /** SHA-256 initial digest values */
73 static const struct sha256_digest sha256_init_digest = {
74  .h = {
75  cpu_to_be32 ( 0x6a09e667 ),
76  cpu_to_be32 ( 0xbb67ae85 ),
77  cpu_to_be32 ( 0x3c6ef372 ),
78  cpu_to_be32 ( 0xa54ff53a ),
79  cpu_to_be32 ( 0x510e527f ),
80  cpu_to_be32 ( 0x9b05688c ),
81  cpu_to_be32 ( 0x1f83d9ab ),
82  cpu_to_be32 ( 0x5be0cd19 ),
83  },
84 };
85 
86 /**
87  * Initialise SHA-256 family algorithm
88  *
89  * @v context SHA-256 context
90  * @v init Initial digest values
91  * @v digestsize Digest size
92  */
93 void sha256_family_init ( struct sha256_context *context,
94  const struct sha256_digest *init,
95  size_t digestsize ) {
96 
97  context->len = 0;
98  context->digestsize = digestsize;
99  memcpy ( &context->ddd.dd.digest, init,
100  sizeof ( context->ddd.dd.digest ) );
101 }
102 
103 /**
104  * Initialise SHA-256 algorithm
105  *
106  * @v ctx SHA-256 context
107  */
108 static void sha256_init ( void *ctx ) {
109  struct sha256_context *context = ctx;
110 
112  sizeof ( struct sha256_digest ) );
113 }
114 
115 /**
116  * Calculate SHA-256 digest of accumulated data
117  *
118  * @v context SHA-256 context
119  */
120 static void sha256_digest ( struct sha256_context *context ) {
121  union {
123  struct sha256_variables v;
124  } u;
125  uint32_t *a = &u.v.a;
126  uint32_t *b = &u.v.b;
127  uint32_t *c = &u.v.c;
128  uint32_t *d = &u.v.d;
129  uint32_t *e = &u.v.e;
130  uint32_t *f = &u.v.f;
131  uint32_t *g = &u.v.g;
132  uint32_t *h = &u.v.h;
133  uint32_t *w = u.v.w;
134  uint32_t s0;
135  uint32_t s1;
136  uint32_t maj;
137  uint32_t t1;
138  uint32_t t2;
139  uint32_t ch;
140  unsigned int i;
141 
142  /* Sanity checks */
143  assert ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 );
144  linker_assert ( &u.ddd.dd.digest.h[0] == a, sha256_bad_layout );
145  linker_assert ( &u.ddd.dd.digest.h[1] == b, sha256_bad_layout );
146  linker_assert ( &u.ddd.dd.digest.h[2] == c, sha256_bad_layout );
147  linker_assert ( &u.ddd.dd.digest.h[3] == d, sha256_bad_layout );
148  linker_assert ( &u.ddd.dd.digest.h[4] == e, sha256_bad_layout );
149  linker_assert ( &u.ddd.dd.digest.h[5] == f, sha256_bad_layout );
150  linker_assert ( &u.ddd.dd.digest.h[6] == g, sha256_bad_layout );
151  linker_assert ( &u.ddd.dd.digest.h[7] == h, sha256_bad_layout );
152  linker_assert ( &u.ddd.dd.data.dword[0] == w, sha256_bad_layout );
153 
154  DBGC ( context, "SHA256 digesting:\n" );
155  DBGC_HDA ( context, 0, &context->ddd.dd.digest,
156  sizeof ( context->ddd.dd.digest ) );
157  DBGC_HDA ( context, context->len, &context->ddd.dd.data,
158  sizeof ( context->ddd.dd.data ) );
159 
160  /* Convert h[0..7] to host-endian, and initialise a, b, c, d,
161  * e, f, g, h, and w[0..15]
162  */
163  for ( i = 0 ; i < ( sizeof ( u.ddd.dword ) /
164  sizeof ( u.ddd.dword[0] ) ) ; i++ ) {
165  be32_to_cpus ( &context->ddd.dword[i] );
166  u.ddd.dword[i] = context->ddd.dword[i];
167  }
168 
169  /* Initialise w[16..63] */
170  for ( i = 16 ; i < SHA256_ROUNDS ; i++ ) {
171  s0 = ( ror32 ( w[i-15], 7 ) ^ ror32 ( w[i-15], 18 ) ^
172  ( w[i-15] >> 3 ) );
173  s1 = ( ror32 ( w[i-2], 17 ) ^ ror32 ( w[i-2], 19 ) ^
174  ( w[i-2] >> 10 ) );
175  w[i] = ( w[i-16] + s0 + w[i-7] + s1 );
176  }
177 
178  /* Main loop */
179  for ( i = 0 ; i < SHA256_ROUNDS ; i++ ) {
180  s0 = ( ror32 ( *a, 2 ) ^ ror32 ( *a, 13 ) ^ ror32 ( *a, 22 ) );
181  maj = ( ( *a & *b ) ^ ( *a & *c ) ^ ( *b & *c ) );
182  t2 = ( s0 + maj );
183  s1 = ( ror32 ( *e, 6 ) ^ ror32 ( *e, 11 ) ^ ror32 ( *e, 25 ) );
184  ch = ( ( *e & *f ) ^ ( (~*e) & *g ) );
185  t1 = ( *h + s1 + ch + k[i] + w[i] );
186  *h = *g;
187  *g = *f;
188  *f = *e;
189  *e = ( *d + t1 );
190  *d = *c;
191  *c = *b;
192  *b = *a;
193  *a = ( t1 + t2 );
194  DBGC2 ( context, "%2d : %08x %08x %08x %08x %08x %08x %08x "
195  "%08x\n", i, *a, *b, *c, *d, *e, *f, *g, *h );
196  }
197 
198  /* Add chunk to hash and convert back to big-endian */
199  for ( i = 0 ; i < 8 ; i++ ) {
200  context->ddd.dd.digest.h[i] =
201  cpu_to_be32 ( context->ddd.dd.digest.h[i] +
202  u.ddd.dd.digest.h[i] );
203  }
204 
205  DBGC ( context, "SHA256 digested:\n" );
206  DBGC_HDA ( context, 0, &context->ddd.dd.digest,
207  sizeof ( context->ddd.dd.digest ) );
208 }
209 
210 /**
211  * Accumulate data with SHA-256 algorithm
212  *
213  * @v ctx SHA-256 context
214  * @v data Data
215  * @v len Length of data
216  */
217 void sha256_update ( void *ctx, const void *data, size_t len ) {
218  struct sha256_context *context = ctx;
219  const uint8_t *byte = data;
220  size_t offset;
221 
222  /* Accumulate data a byte at a time, performing the digest
223  * whenever we fill the data buffer
224  */
225  while ( len-- ) {
226  offset = ( context->len % sizeof ( context->ddd.dd.data ) );
227  context->ddd.dd.data.byte[offset] = *(byte++);
228  context->len++;
229  if ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 )
230  sha256_digest ( context );
231  }
232 }
233 
234 /**
235  * Generate SHA-256 digest
236  *
237  * @v ctx SHA-256 context
238  * @v out Output buffer
239  */
240 void sha256_final ( void *ctx, void *out ) {
241  struct sha256_context *context = ctx;
242  uint64_t len_bits;
243  uint8_t pad;
244 
245  /* Record length before pre-processing */
246  len_bits = cpu_to_be64 ( ( ( uint64_t ) context->len ) * 8 );
247 
248  /* Pad with a single "1" bit followed by as many "0" bits as required */
249  pad = 0x80;
250  do {
251  sha256_update ( ctx, &pad, sizeof ( pad ) );
252  pad = 0x00;
253  } while ( ( context->len % sizeof ( context->ddd.dd.data ) ) !=
254  offsetof ( typeof ( context->ddd.dd.data ), final.len ) );
255 
256  /* Append length (in bits) */
257  sha256_update ( ctx, &len_bits, sizeof ( len_bits ) );
258  assert ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 );
259 
260  /* Copy out final digest */
261  memcpy ( out, &context->ddd.dd.digest, context->digestsize );
262 }
263 
264 /** SHA-256 algorithm */
266  .name = "sha256",
267  .ctxsize = sizeof ( struct sha256_context ),
268  .blocksize = sizeof ( union sha256_block ),
269  .digestsize = sizeof ( struct sha256_digest ),
270  .init = sha256_init,
271  .update = sha256_update,
272  .final = sha256_final,
273 };
274 
275 /** "sha256" object identifier */
277 
278 /** "sha256" OID-identified algorithm */
279 struct asn1_algorithm oid_sha256_algorithm __asn1_algorithm = {
280  .name = "sha256",
281  .digest = &sha256_algorithm,
282  .oid = ASN1_OID_CURSOR ( oid_sha256 ),
283 };
static void sha256_init(void *ctx)
Initialise SHA-256 algorithm.
Definition: sha256.c:108
#define __attribute__(x)
Definition: compiler.h:10
An ASN.1 OID-identified algorithm.
Definition: asn1.h:298
struct asn1_algorithm oid_sha256_algorithm __asn1_algorithm
"sha256" OID-identified algorithm
Definition: sha256.c:279
#define ASN1_OID_SHA256
ASN.1 OID for id-sha256 (2.16.840.1.101.3.4.2.1)
Definition: asn1.h:183
static const struct sha256_digest sha256_init_digest
SHA-256 initial digest values.
Definition: sha256.c:73
static const uint32_t k[SHA256_ROUNDS]
SHA-256 constants.
Definition: sha256.c:58
SHA-256 variables.
Definition: sha256.c:42
uint32_t a
Definition: sha256.c:46
static uint8_t oid_sha256[]
"sha256" object identifier
Definition: sha256.c:276
uint32_t f
Definition: sha256.c:51
#define DBGC(...)
Definition: compiler.h:505
struct sha256_digest digest
Digest of data already processed.
Definition: sha256.h:46
unsigned long long uint64_t
Definition: stdint.h:13
Cryptographic API.
uint32_t e
Definition: sha256.c:50
static __always_inline void off_t int c
Definition: efi_uaccess.h:87
size_t digestsize
Digest size.
Definition: sha256.h:65
#define offsetof(type, field)
Get offset of a field within a structure.
Definition: stddef.h:24
u32 pad[9]
Padding.
Definition: ar9003_mac.h:90
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
static u32 ror32(u32 v, int bits)
Rotate 32-bit value right.
Definition: wpa_tkip.c:161
SHA-256 digest and data block.
Definition: sha256.h:52
uint32_t b
Definition: sha256.c:47
uint32_t h[8]
Hash output.
Definition: sha256.h:21
uint8_t ch
Definition: registers.h:83
void * memcpy(void *dest, const void *src, size_t len) __nonnull
Assertions.
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
ASN.1 encoding.
#define DBGC_HDA(...)
Definition: compiler.h:506
void sha256_update(void *ctx, const void *data, size_t len)
Accumulate data with SHA-256 algorithm.
Definition: sha256.c:217
static userptr_t size_t offset
Offset of the first segment within the content.
Definition: deflate.h:259
uint8_t h
Definition: registers.h:18
#define ASN1_OID_CURSOR(oid_value)
Define an ASN.1 cursor containing an OID.
Definition: asn1.h:292
__be32 out[4]
Definition: CIB_PRM.h:36
An SHA-256 data block.
Definition: sha256.h:25
struct sha256_digest_data dd
Digest and data block.
Definition: sha256.h:54
uint32_t w[SHA256_ROUNDS]
Definition: sha256.c:54
size_t len
Amount of accumulated data.
Definition: sha256.h:63
unsigned char uint8_t
Definition: stdint.h:10
unsigned int uint32_t
Definition: stdint.h:12
union sha256_block data
Accumulated data.
Definition: sha256.h:48
uint32_t dword[sizeof(struct sha256_digest_data)/sizeof(uint32_t)]
Raw dwords.
Definition: sha256.h:57
const char * name
Name.
Definition: asn1.h:300
#define cpu_to_be32(value)
Definition: byteswap.h:110
struct digest_algorithm sha256_algorithm
SHA-256 algorithm.
Definition: sha256.c:265
#define be32_to_cpus(ptr)
Definition: byteswap.h:128
uint32_t len
Length.
Definition: ena.h:14
#define DBGC2(...)
Definition: compiler.h:522
const char * name
Algorithm name.
Definition: crypto.h:18
uint32_t c
Definition: sha256.c:48
A message digest algorithm.
Definition: crypto.h:16
uint32_t d
Definition: sha256.c:49
#define cpu_to_be64(value)
Definition: byteswap.h:111
uint32_t g
Definition: sha256.c:52
uint32_t h
Definition: sha256.c:53
void sha256_family_init(struct sha256_context *context, const struct sha256_digest *init, size_t digestsize)
Initialise SHA-256 family algorithm.
Definition: sha256.c:93
union sha256_digest_data_dwords ddd
Digest and accumulated data.
Definition: sha256.h:67
#define SHA256_ROUNDS
SHA-256 number of rounds.
Definition: sha256.h:16
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
uint32_t digestsize
Digest size (i.e.
Definition: pccrr.h:14
An SHA-256 context.
Definition: sha256.h:61
struct arbelprm_port_state_change_st data
Message.
Definition: arbel.h:12
union @16 u
uint8_t byte[64]
Raw bytes.
Definition: sha256.h:27
SHA-256 algorithm.
#define linker_assert(condition, error_symbol)
Assert a condition at link-time.
Definition: assert.h:68
An SHA-256 digest.
Definition: sha256.h:19
String functions.
Bit operations.
static void sha256_digest(struct sha256_context *context)
Calculate SHA-256 digest of accumulated data.
Definition: sha256.c:120
void sha256_final(void *ctx, void *out)
Generate SHA-256 digest.
Definition: sha256.c:240