Pārlūkot izejas kodu

[crypto] Add SHA-512/256 algorithm

SHA-512/256 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/256 test
vectors.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 9 gadus atpakaļ
vecāks
revīzija
e5e91ab471

+ 83
- 0
src/crypto/sha512_256.c Parādīt failu

@@ -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/256 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/256 initial digest values */
39
+static const struct sha512_digest sha512_256_init_digest = {
40
+	.h = {
41
+		cpu_to_be64 ( 0x22312194fc2bf72cULL ),
42
+		cpu_to_be64 ( 0x9f555fa3c84c64c2ULL ),
43
+		cpu_to_be64 ( 0x2393b86b6f53b151ULL ),
44
+		cpu_to_be64 ( 0x963877195940eabdULL ),
45
+		cpu_to_be64 ( 0x96283ee2a88effe3ULL ),
46
+		cpu_to_be64 ( 0xbe5e1e2553863992ULL ),
47
+		cpu_to_be64 ( 0x2b0199fc2c85b8aaULL ),
48
+		cpu_to_be64 ( 0x0eb72ddc81c52ca2ULL ),
49
+	},
50
+};
51
+
52
+/**
53
+ * Initialise SHA-512/256 algorithm
54
+ *
55
+ * @v ctx		SHA-512/256 context
56
+ */
57
+static void sha512_256_init ( void *ctx ) {
58
+	struct sha512_context *context = ctx;
59
+
60
+	sha512_family_init ( context, &sha512_256_init_digest,
61
+			     SHA512_256_DIGEST_SIZE );
62
+}
63
+
64
+/** SHA-512/256 algorithm */
65
+struct digest_algorithm sha512_256_algorithm = {
66
+	.name		= "sha512/256",
67
+	.ctxsize	= sizeof ( struct sha512_context ),
68
+	.blocksize	= sizeof ( union sha512_block ),
69
+	.digestsize	= SHA512_256_DIGEST_SIZE,
70
+	.init		= sha512_256_init,
71
+	.update		= sha512_update,
72
+	.final		= sha512_final,
73
+};
74
+
75
+/** "sha512_256" object identifier */
76
+static uint8_t oid_sha512_256[] = { ASN1_OID_SHA512_256 };
77
+
78
+/** "sha512_256" OID-identified algorithm */
79
+struct asn1_algorithm oid_sha512_256_algorithm __asn1_algorithm = {
80
+	.name = "sha512/256",
81
+	.digest = &sha512_256_algorithm,
82
+	.oid = ASN1_OID_CURSOR ( oid_sha512_256 ),
83
+};

+ 7
- 0
src/include/ipxe/asn1.h Parādīt failu

@@ -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-256 (2.16.840.1.101.3.4.2.6) */
185
+#define ASN1_OID_SHA512_256						\
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 ( 6 )
190
+
184 191
 /** ASN.1 OID for commonName (2.5.4.3) */
185 192
 #define ASN1_OID_COMMON_NAME					\
186 193
 	ASN1_OID_INITIAL ( 2, 5 ), ASN1_OID_SINGLE ( 4 ),	\

+ 4
- 0
src/include/ipxe/sha512.h Parādīt failu

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

+ 28
- 0
src/tests/sha512_test.c Parādīt failu

@@ -31,6 +31,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
31 31
  *
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
+ *  http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA512_256.pdf
34 35
  *
35 36
  */
36 37
 
@@ -101,6 +102,28 @@ DIGEST_TEST ( sha384_nist_abc_stu, &sha384_algorithm, DIGEST_NIST_ABC_STU,
101 102
 		       0x55, 0x7e, 0x2d, 0xb9, 0x66, 0xc3, 0xe9, 0xfa, 0x91,
102 103
 		       0x74, 0x60, 0x39 ) );
103 104
 
105
+/* Empty test vector (digest obtained from "shasum -a 512256 /dev/null") */
106
+DIGEST_TEST ( sha512_256_empty, &sha512_256_algorithm, DIGEST_EMPTY,
107
+	      DIGEST ( 0xc6, 0x72, 0xb8, 0xd1, 0xef, 0x56, 0xed, 0x28, 0xab,
108
+		       0x87, 0xc3, 0x62, 0x2c, 0x51, 0x14, 0x06, 0x9b, 0xdd,
109
+		       0x3a, 0xd7, 0xb8, 0xf9, 0x73, 0x74, 0x98, 0xd0, 0xc0,
110
+		       0x1e, 0xce, 0xf0, 0x96, 0x7a ) );
111
+
112
+/* NIST test vector "abc" */
113
+DIGEST_TEST ( sha512_256_nist_abc, &sha512_256_algorithm, DIGEST_NIST_ABC,
114
+	      DIGEST ( 0x53, 0x04, 0x8e, 0x26, 0x81, 0x94, 0x1e, 0xf9, 0x9b,
115
+		       0x2e, 0x29, 0xb7, 0x6b, 0x4c, 0x7d, 0xab, 0xe4, 0xc2,
116
+		       0xd0, 0xc6, 0x34, 0xfc, 0x6d, 0x46, 0xe0, 0xe2, 0xf1,
117
+		       0x31, 0x07, 0xe7, 0xaf, 0x23 ) );
118
+
119
+/* NIST test vector "abc...stu" */
120
+DIGEST_TEST ( sha512_256_nist_abc_stu, &sha512_256_algorithm,
121
+	      DIGEST_NIST_ABC_STU,
122
+	      DIGEST ( 0x39, 0x28, 0xe1, 0x84, 0xfb, 0x86, 0x90, 0xf8, 0x40,
123
+		       0xda, 0x39, 0x88, 0x12, 0x1d, 0x31, 0xbe, 0x65, 0xcb,
124
+		       0x9d, 0x3e, 0xf8, 0x3e, 0xe6, 0x14, 0x6f, 0xea, 0xc8,
125
+		       0x61, 0xe1, 0x9b, 0x56, 0x3a ) );
126
+
104 127
 /**
105 128
  * Perform SHA-512 family self-test
106 129
  *
@@ -114,12 +137,17 @@ static void sha512_test_exec ( void ) {
114 137
 	digest_ok ( &sha384_empty );
115 138
 	digest_ok ( &sha384_nist_abc );
116 139
 	digest_ok ( &sha384_nist_abc_stu );
140
+	digest_ok ( &sha512_256_empty );
141
+	digest_ok ( &sha512_256_nist_abc );
142
+	digest_ok ( &sha512_256_nist_abc_stu );
117 143
 
118 144
 	/* Speed tests */
119 145
 	DBG ( "SHA512 required %ld cycles per byte\n",
120 146
 	      digest_cost ( &sha512_algorithm ) );
121 147
 	DBG ( "SHA384 required %ld cycles per byte\n",
122 148
 	      digest_cost ( &sha384_algorithm ) );
149
+	DBG ( "SHA512/256 required %ld cycles per byte\n",
150
+	      digest_cost ( &sha512_256_algorithm ) );
123 151
 }
124 152
 
125 153
 /** SHA-512 family self-test */

Notiek ielāde…
Atcelt
Saglabāt