Преглед изворни кода

[crypto] Add SHA-224 algorithm

SHA-224 is almost identical to SHA-256, with differing initial hash
values and a truncated output length.

This implementation has been verified using the NIST SHA-224 test
vectors.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown пре 9 година
родитељ
комит
4dbc44348c
5 измењених фајлова са 167 додато и 16 уклоњено
  1. 82
    0
      src/crypto/sha224.c
  2. 36
    13
      src/crypto/sha256.c
  3. 7
    0
      src/include/ipxe/asn1.h
  4. 12
    0
      src/include/ipxe/sha256.h
  5. 30
    3
      src/tests/sha256_test.c

+ 82
- 0
src/crypto/sha224.c Прегледај датотеку

@@ -0,0 +1,82 @@
1
+/*
2
+ * Copyright (C) 2015 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-224 algorithm
29
+ *
30
+ */
31
+
32
+#include <stdint.h>
33
+#include <byteswap.h>
34
+#include <ipxe/crypto.h>
35
+#include <ipxe/asn1.h>
36
+#include <ipxe/sha256.h>
37
+
38
+/** SHA-224 initial digest values */
39
+static const struct sha256_digest sha224_init_digest = {
40
+	.h = {
41
+		cpu_to_be32 ( 0xc1059ed8 ),
42
+		cpu_to_be32 ( 0x367cd507 ),
43
+		cpu_to_be32 ( 0x3070dd17 ),
44
+		cpu_to_be32 ( 0xf70e5939 ),
45
+		cpu_to_be32 ( 0xffc00b31 ),
46
+		cpu_to_be32 ( 0x68581511 ),
47
+		cpu_to_be32 ( 0x64f98fa7 ),
48
+		cpu_to_be32 ( 0xbefa4fa4 ),
49
+	},
50
+};
51
+
52
+/**
53
+ * Initialise SHA-224 algorithm
54
+ *
55
+ * @v ctx		SHA-224 context
56
+ */
57
+static void sha224_init ( void *ctx ) {
58
+	struct sha256_context *context = ctx;
59
+
60
+	sha256_family_init ( context, &sha224_init_digest, SHA224_DIGEST_SIZE );
61
+}
62
+
63
+/** SHA-224 algorithm */
64
+struct digest_algorithm sha224_algorithm = {
65
+	.name		= "sha224",
66
+	.ctxsize	= sizeof ( struct sha256_context ),
67
+	.blocksize	= sizeof ( union sha256_block ),
68
+	.digestsize	= SHA224_DIGEST_SIZE,
69
+	.init		= sha224_init,
70
+	.update		= sha256_update,
71
+	.final		= sha256_final,
72
+};
73
+
74
+/** "sha224" object identifier */
75
+static uint8_t oid_sha224[] = { ASN1_OID_SHA224 };
76
+
77
+/** "sha224" OID-identified algorithm */
78
+struct asn1_algorithm oid_sha224_algorithm __asn1_algorithm = {
79
+	.name = "sha224",
80
+	.digest = &sha224_algorithm,
81
+	.oid = ASN1_OID_CURSOR ( oid_sha224 ),
82
+};

+ 36
- 13
src/crypto/sha256.c Прегледај датотеку

@@ -69,6 +69,37 @@ static const uint32_t k[64] = {
69 69
 	0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
70 70
 };
71 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
+
72 103
 /**
73 104
  * Initialise SHA-256 algorithm
74 105
  *
@@ -77,15 +108,8 @@ static const uint32_t k[64] = {
77 108
 static void sha256_init ( void *ctx ) {
78 109
 	struct sha256_context *context = ctx;
79 110
 
80
-	context->ddd.dd.digest.h[0] = cpu_to_be32 ( 0x6a09e667 );
81
-	context->ddd.dd.digest.h[1] = cpu_to_be32 ( 0xbb67ae85 );
82
-	context->ddd.dd.digest.h[2] = cpu_to_be32 ( 0x3c6ef372 );
83
-	context->ddd.dd.digest.h[3] = cpu_to_be32 ( 0xa54ff53a );
84
-	context->ddd.dd.digest.h[4] = cpu_to_be32 ( 0x510e527f );
85
-	context->ddd.dd.digest.h[5] = cpu_to_be32 ( 0x9b05688c );
86
-	context->ddd.dd.digest.h[6] = cpu_to_be32 ( 0x1f83d9ab );
87
-	context->ddd.dd.digest.h[7] = cpu_to_be32 ( 0x5be0cd19 );
88
-	context->len = 0;
111
+	sha256_family_init ( context, &sha256_init_digest,
112
+			     sizeof ( struct sha256_digest ) );
89 113
 }
90 114
 
91 115
 /**
@@ -190,7 +214,7 @@ static void sha256_digest ( struct sha256_context *context ) {
190 214
  * @v data		Data
191 215
  * @v len		Length of data
192 216
  */
193
-static void sha256_update ( void *ctx, const void *data, size_t len ) {
217
+void sha256_update ( void *ctx, const void *data, size_t len ) {
194 218
 	struct sha256_context *context = ctx;
195 219
 	const uint8_t *byte = data;
196 220
 	size_t offset;
@@ -213,7 +237,7 @@ static void sha256_update ( void *ctx, const void *data, size_t len ) {
213 237
  * @v ctx		SHA-256 context
214 238
  * @v out		Output buffer
215 239
  */
216
-static void sha256_final ( void *ctx, void *out ) {
240
+void sha256_final ( void *ctx, void *out ) {
217 241
 	struct sha256_context *context = ctx;
218 242
 	uint64_t len_bits;
219 243
 	uint8_t pad;
@@ -234,8 +258,7 @@ static void sha256_final ( void *ctx, void *out ) {
234 258
 	assert ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 );
235 259
 
236 260
 	/* Copy out final digest */
237
-	memcpy ( out, &context->ddd.dd.digest,
238
-		 sizeof ( context->ddd.dd.digest ) );
261
+	memcpy ( out, &context->ddd.dd.digest, context->digestsize );
239 262
 }
240 263
 
241 264
 /** SHA-256 algorithm */

+ 7
- 0
src/include/ipxe/asn1.h Прегледај датотеку

@@ -160,6 +160,13 @@ struct asn1_builder_header {
160 160
 	ASN1_OID_SINGLE ( 3 ), ASN1_OID_SINGLE ( 4 ),		\
161 161
 	ASN1_OID_SINGLE ( 2 ), ASN1_OID_SINGLE ( 1 )
162 162
 
163
+/** ASN.1 OID for id-sha224 (2.16.840.1.101.3.4.2.4) */
164
+#define ASN1_OID_SHA224						\
165
+	ASN1_OID_INITIAL ( 2, 16 ), ASN1_OID_DOUBLE ( 840 ),	\
166
+	ASN1_OID_SINGLE ( 1 ), ASN1_OID_SINGLE ( 101 ),		\
167
+	ASN1_OID_SINGLE ( 3 ), ASN1_OID_SINGLE ( 4 ),		\
168
+	ASN1_OID_SINGLE ( 2 ), ASN1_OID_SINGLE ( 4 )
169
+
163 170
 /** ASN.1 OID for commonName (2.5.4.3) */
164 171
 #define ASN1_OID_COMMON_NAME					\
165 172
 	ASN1_OID_INITIAL ( 2, 5 ), ASN1_OID_SINGLE ( 4 ),	\

+ 12
- 0
src/include/ipxe/sha256.h Прегледај датотеку

@@ -58,6 +58,8 @@ union sha256_digest_data_dwords {
58 58
 struct sha256_context {
59 59
 	/** Amount of accumulated data */
60 60
 	size_t len;
61
+	/** Digest size */
62
+	size_t digestsize;
61 63
 	/** Digest and accumulated data */
62 64
 	union sha256_digest_data_dwords ddd;
63 65
 } __attribute__ (( packed ));
@@ -68,6 +70,16 @@ struct sha256_context {
68 70
 /** SHA-256 digest size */
69 71
 #define SHA256_DIGEST_SIZE sizeof ( struct sha256_digest )
70 72
 
73
+/** SHA-224 digest size */
74
+#define SHA224_DIGEST_SIZE ( SHA256_DIGEST_SIZE * 224 / 256 )
75
+
76
+extern void sha256_family_init ( struct sha256_context *context,
77
+				 const struct sha256_digest *init,
78
+				 size_t digestsize );
79
+extern void sha256_update ( void *ctx, const void *data, size_t len );
80
+extern void sha256_final ( void *ctx, void *out );
81
+
71 82
 extern struct digest_algorithm sha256_algorithm;
83
+extern struct digest_algorithm sha224_algorithm;
72 84
 
73 85
 #endif /* _IPXE_SHA256_H */

+ 30
- 3
src/tests/sha256_test.c Прегледај датотеку

@@ -25,11 +25,12 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 25
 
26 26
 /** @file
27 27
  *
28
- * SHA-256 tests
28
+ * SHA-256 family tests
29 29
  *
30 30
  * NIST test vectors are taken from
31 31
  *
32 32
  *  http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA256.pdf
33
+ *  http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA224.pdf
33 34
  *
34 35
  */
35 36
 
@@ -61,8 +62,29 @@ DIGEST_TEST ( sha256_nist_abc_opq, &sha256_algorithm, DIGEST_NIST_ABC_OPQ,
61 62
 		       0xe4, 0x59, 0x64, 0xff, 0x21, 0x67, 0xf6, 0xec, 0xed,
62 63
 		       0xd4, 0x19, 0xdb, 0x06, 0xc1 ) );
63 64
 
65
+/* Empty test vector (digest obtained from "sha224sum /dev/null") */
66
+DIGEST_TEST ( sha224_empty, &sha224_algorithm, DIGEST_EMPTY,
67
+	      DIGEST ( 0xd1, 0x4a, 0x02, 0x8c, 0x2a, 0x3a, 0x2b, 0xc9, 0x47,
68
+		       0x61, 0x02, 0xbb, 0x28, 0x82, 0x34, 0xc4, 0x15, 0xa2,
69
+		       0xb0, 0x1f, 0x82, 0x8e, 0xa6, 0x2a, 0xc5, 0xb3, 0xe4,
70
+		       0x2f ) );
71
+
72
+/* NIST test vector "abc" */
73
+DIGEST_TEST ( sha224_nist_abc, &sha224_algorithm, DIGEST_NIST_ABC,
74
+	      DIGEST ( 0x23, 0x09, 0x7d, 0x22, 0x34, 0x05, 0xd8, 0x22, 0x86,
75
+		       0x42, 0xa4, 0x77, 0xbd, 0xa2, 0x55, 0xb3, 0x2a, 0xad,
76
+		       0xbc, 0xe4, 0xbd, 0xa0, 0xb3, 0xf7, 0xe3, 0x6c, 0x9d,
77
+		       0xa7 ) );
78
+
79
+/* NIST test vector "abc...opq" */
80
+DIGEST_TEST ( sha224_nist_abc_opq, &sha224_algorithm, DIGEST_NIST_ABC_OPQ,
81
+	      DIGEST ( 0x75, 0x38, 0x8b, 0x16, 0x51, 0x27, 0x76, 0xcc, 0x5d,
82
+		       0xba, 0x5d, 0xa1, 0xfd, 0x89, 0x01, 0x50, 0xb0, 0xc6,
83
+		       0x45, 0x5c, 0xb4, 0xf5, 0x8b, 0x19, 0x52, 0x52, 0x25,
84
+		       0x25 ) );
85
+
64 86
 /**
65
- * Perform SHA-256 self-test
87
+ * Perform SHA-256 family self-test
66 88
  *
67 89
  */
68 90
 static void sha256_test_exec ( void ) {
@@ -71,13 +93,18 @@ static void sha256_test_exec ( void ) {
71 93
 	digest_ok ( &sha256_empty );
72 94
 	digest_ok ( &sha256_nist_abc );
73 95
 	digest_ok ( &sha256_nist_abc_opq );
96
+	digest_ok ( &sha224_empty );
97
+	digest_ok ( &sha224_nist_abc );
98
+	digest_ok ( &sha224_nist_abc_opq );
74 99
 
75 100
 	/* Speed tests */
76 101
 	DBG ( "SHA256 required %ld cycles per byte\n",
77 102
 	      digest_cost ( &sha256_algorithm ) );
103
+	DBG ( "SHA224 required %ld cycles per byte\n",
104
+	      digest_cost ( &sha224_algorithm ) );
78 105
 }
79 106
 
80
-/** SHA-256 self-test */
107
+/** SHA-256 family self-test */
81 108
 struct self_test sha256_test __self_test = {
82 109
 	.name = "sha256",
83 110
 	.exec = sha256_test_exec,

Loading…
Откажи
Сачувај