Browse Source

[crypto] Add SHA-512/224 algorithm

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

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

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 9 years ago
parent
commit
ea3d5875cd
4 changed files with 122 additions and 0 deletions
  1. 83
    0
      src/crypto/sha512_224.c
  2. 7
    0
      src/include/ipxe/asn1.h
  3. 4
    0
      src/include/ipxe/sha512.h
  4. 28
    0
      src/tests/sha512_test.c

+ 83
- 0
src/crypto/sha512_224.c View File

@@ -0,0 +1,83 @@
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-512/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/sha512.h>
37
+
38
+/** SHA-512/224 initial digest values */
39
+static const struct sha512_digest sha512_224_init_digest = {
40
+	.h = {
41
+		cpu_to_be64 ( 0x8c3d37c819544da2ULL ),
42
+		cpu_to_be64 ( 0x73e1996689dcd4d6ULL ),
43
+		cpu_to_be64 ( 0x1dfab7ae32ff9c82ULL ),
44
+		cpu_to_be64 ( 0x679dd514582f9fcfULL ),
45
+		cpu_to_be64 ( 0x0f6d2b697bd44da8ULL ),
46
+		cpu_to_be64 ( 0x77e36f7304c48942ULL ),
47
+		cpu_to_be64 ( 0x3f9d85a86a1d36c8ULL ),
48
+		cpu_to_be64 ( 0x1112e6ad91d692a1ULL ),
49
+	},
50
+};
51
+
52
+/**
53
+ * Initialise SHA-512/224 algorithm
54
+ *
55
+ * @v ctx		SHA-512/224 context
56
+ */
57
+static void sha512_224_init ( void *ctx ) {
58
+	struct sha512_context *context = ctx;
59
+
60
+	sha512_family_init ( context, &sha512_224_init_digest,
61
+			     SHA512_224_DIGEST_SIZE );
62
+}
63
+
64
+/** SHA-512/224 algorithm */
65
+struct digest_algorithm sha512_224_algorithm = {
66
+	.name		= "sha512/224",
67
+	.ctxsize	= sizeof ( struct sha512_context ),
68
+	.blocksize	= sizeof ( union sha512_block ),
69
+	.digestsize	= SHA512_224_DIGEST_SIZE,
70
+	.init		= sha512_224_init,
71
+	.update		= sha512_update,
72
+	.final		= sha512_final,
73
+};
74
+
75
+/** "sha512_224" object identifier */
76
+static uint8_t oid_sha512_224[] = { ASN1_OID_SHA512_224 };
77
+
78
+/** "sha512_224" OID-identified algorithm */
79
+struct asn1_algorithm oid_sha512_224_algorithm __asn1_algorithm = {
80
+	.name = "sha512/224",
81
+	.digest = &sha512_224_algorithm,
82
+	.oid = ASN1_OID_CURSOR ( oid_sha512_224 ),
83
+};

+ 7
- 0
src/include/ipxe/asn1.h View File

@@ -181,6 +181,13 @@ struct asn1_builder_header {
181 181
 	ASN1_OID_SINGLE ( 3 ), ASN1_OID_SINGLE ( 4 ),		\
182 182
 	ASN1_OID_SINGLE ( 2 ), ASN1_OID_SINGLE ( 4 )
183 183
 
184
+/** ASN.1 OID for id-sha512-224 (2.16.840.1.101.3.4.2.5) */
185
+#define ASN1_OID_SHA512_224						\
186
+	ASN1_OID_INITIAL ( 2, 16 ), ASN1_OID_DOUBLE ( 840 ),	\
187
+	ASN1_OID_SINGLE ( 1 ), ASN1_OID_SINGLE ( 101 ),		\
188
+	ASN1_OID_SINGLE ( 3 ), ASN1_OID_SINGLE ( 4 ),		\
189
+	ASN1_OID_SINGLE ( 2 ), ASN1_OID_SINGLE ( 5 )
190
+
184 191
 /** ASN.1 OID for id-sha512-256 (2.16.840.1.101.3.4.2.6) */
185 192
 #define ASN1_OID_SHA512_256						\
186 193
 	ASN1_OID_INITIAL ( 2, 16 ), ASN1_OID_DOUBLE ( 840 ),	\

+ 4
- 0
src/include/ipxe/sha512.h View File

@@ -81,6 +81,9 @@ struct sha512_context {
81 81
 /** SHA-512/256 digest size */
82 82
 #define SHA512_256_DIGEST_SIZE ( SHA512_DIGEST_SIZE * 256 / 512 )
83 83
 
84
+/** SHA-512/224 digest size */
85
+#define SHA512_224_DIGEST_SIZE ( SHA512_DIGEST_SIZE * 224 / 512 )
86
+
84 87
 extern void sha512_family_init ( struct sha512_context *context,
85 88
 				 const struct sha512_digest *init,
86 89
 				 size_t digestsize );
@@ -90,5 +93,6 @@ extern void sha512_final ( void *ctx, void *out );
90 93
 extern struct digest_algorithm sha512_algorithm;
91 94
 extern struct digest_algorithm sha384_algorithm;
92 95
 extern struct digest_algorithm sha512_256_algorithm;
96
+extern struct digest_algorithm sha512_224_algorithm;
93 97
 
94 98
 #endif /* IPXE_SHA512_H */

+ 28
- 0
src/tests/sha512_test.c View File

@@ -32,6 +32,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
32 32
  *  http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA512.pdf
33 33
  *  http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA384.pdf
34 34
  *  http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA512_256.pdf
35
+ *  http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA512_224.pdf
35 36
  *
36 37
  */
37 38
 
@@ -124,6 +125,28 @@ DIGEST_TEST ( sha512_256_nist_abc_stu, &sha512_256_algorithm,
124 125
 		       0x9d, 0x3e, 0xf8, 0x3e, 0xe6, 0x14, 0x6f, 0xea, 0xc8,
125 126
 		       0x61, 0xe1, 0x9b, 0x56, 0x3a ) );
126 127
 
128
+/* Empty test vector (digest obtained from "shasum -a 512224 /dev/null") */
129
+DIGEST_TEST ( sha512_224_empty, &sha512_224_algorithm, DIGEST_EMPTY,
130
+	      DIGEST ( 0x6e, 0xd0, 0xdd, 0x02, 0x80, 0x6f, 0xa8, 0x9e, 0x25,
131
+		       0xde, 0x06, 0x0c, 0x19, 0xd3, 0xac, 0x86, 0xca, 0xbb,
132
+		       0x87, 0xd6, 0xa0, 0xdd, 0xd0, 0x5c, 0x33, 0x3b, 0x84,
133
+		       0xf4 ) );
134
+
135
+/* NIST test vector "abc" */
136
+DIGEST_TEST ( sha512_224_nist_abc, &sha512_224_algorithm, DIGEST_NIST_ABC,
137
+	      DIGEST ( 0x46, 0x34, 0x27, 0x0f, 0x70, 0x7b, 0x6a, 0x54, 0xda,
138
+		       0xae, 0x75, 0x30, 0x46, 0x08, 0x42, 0xe2, 0x0e, 0x37,
139
+		       0xed, 0x26, 0x5c, 0xee, 0xe9, 0xa4, 0x3e, 0x89, 0x24,
140
+		       0xaa ) );
141
+
142
+/* NIST test vector "abc...stu" */
143
+DIGEST_TEST ( sha512_224_nist_abc_stu, &sha512_224_algorithm,
144
+	      DIGEST_NIST_ABC_STU,
145
+	      DIGEST ( 0x23, 0xfe, 0xc5, 0xbb, 0x94, 0xd6, 0x0b, 0x23, 0x30,
146
+		       0x81, 0x92, 0x64, 0x0b, 0x0c, 0x45, 0x33, 0x35, 0xd6,
147
+		       0x64, 0x73, 0x4f, 0xe4, 0x0e, 0x72, 0x68, 0x67, 0x4a,
148
+		       0xf9 ) );
149
+
127 150
 /**
128 151
  * Perform SHA-512 family self-test
129 152
  *
@@ -140,6 +163,9 @@ static void sha512_test_exec ( void ) {
140 163
 	digest_ok ( &sha512_256_empty );
141 164
 	digest_ok ( &sha512_256_nist_abc );
142 165
 	digest_ok ( &sha512_256_nist_abc_stu );
166
+	digest_ok ( &sha512_224_empty );
167
+	digest_ok ( &sha512_224_nist_abc );
168
+	digest_ok ( &sha512_224_nist_abc_stu );
143 169
 
144 170
 	/* Speed tests */
145 171
 	DBG ( "SHA512 required %ld cycles per byte\n",
@@ -148,6 +174,8 @@ static void sha512_test_exec ( void ) {
148 174
 	      digest_cost ( &sha384_algorithm ) );
149 175
 	DBG ( "SHA512/256 required %ld cycles per byte\n",
150 176
 	      digest_cost ( &sha512_256_algorithm ) );
177
+	DBG ( "SHA512/224 required %ld cycles per byte\n",
178
+	      digest_cost ( &sha512_224_algorithm ) );
151 179
 }
152 180
 
153 181
 /** SHA-512 family self-test */

Loading…
Cancel
Save