Browse Source

[crypto] Add SHA-384 algorithm

SHA-384 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-384 test
vectors.

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

+ 82
- 0
src/crypto/sha384.c View File

@@ -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-384 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-384 initial digest values */
39
+static const struct sha512_digest sha384_init_digest = {
40
+	.h = {
41
+		cpu_to_be64 ( 0xcbbb9d5dc1059ed8ULL ),
42
+		cpu_to_be64 ( 0x629a292a367cd507ULL ),
43
+		cpu_to_be64 ( 0x9159015a3070dd17ULL ),
44
+		cpu_to_be64 ( 0x152fecd8f70e5939ULL ),
45
+		cpu_to_be64 ( 0x67332667ffc00b31ULL ),
46
+		cpu_to_be64 ( 0x8eb44a8768581511ULL ),
47
+		cpu_to_be64 ( 0xdb0c2e0d64f98fa7ULL ),
48
+		cpu_to_be64 ( 0x47b5481dbefa4fa4ULL ),
49
+	},
50
+};
51
+
52
+/**
53
+ * Initialise SHA-384 algorithm
54
+ *
55
+ * @v ctx		SHA-384 context
56
+ */
57
+static void sha384_init ( void *ctx ) {
58
+	struct sha512_context *context = ctx;
59
+
60
+	sha512_family_init ( context, &sha384_init_digest, SHA384_DIGEST_SIZE );
61
+}
62
+
63
+/** SHA-384 algorithm */
64
+struct digest_algorithm sha384_algorithm = {
65
+	.name		= "sha384",
66
+	.ctxsize	= sizeof ( struct sha512_context ),
67
+	.blocksize	= sizeof ( union sha512_block ),
68
+	.digestsize	= SHA384_DIGEST_SIZE,
69
+	.init		= sha384_init,
70
+	.update		= sha512_update,
71
+	.final		= sha512_final,
72
+};
73
+
74
+/** "sha384" object identifier */
75
+static uint8_t oid_sha384[] = { ASN1_OID_SHA384 };
76
+
77
+/** "sha384" OID-identified algorithm */
78
+struct asn1_algorithm oid_sha384_algorithm __asn1_algorithm = {
79
+	.name = "sha384",
80
+	.digest = &sha384_algorithm,
81
+	.oid = ASN1_OID_CURSOR ( oid_sha384 ),
82
+};

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

@@ -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-sha384 (2.16.840.1.101.3.4.2.2) */
164
+#define ASN1_OID_SHA384						\
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 ( 2 )
169
+
163 170
 /** ASN.1 OID for id-sha512 (2.16.840.1.101.3.4.2.3) */
164 171
 #define ASN1_OID_SHA512						\
165 172
 	ASN1_OID_INITIAL ( 2, 16 ), ASN1_OID_DOUBLE ( 840 ),	\

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

@@ -75,6 +75,9 @@ struct sha512_context {
75 75
 /** SHA-512 digest size */
76 76
 #define SHA512_DIGEST_SIZE sizeof ( struct sha512_digest )
77 77
 
78
+/** SHA-384 digest size */
79
+#define SHA384_DIGEST_SIZE ( SHA512_DIGEST_SIZE * 384 / 512 )
80
+
78 81
 extern void sha512_family_init ( struct sha512_context *context,
79 82
 				 const struct sha512_digest *init,
80 83
 				 size_t digestsize );
@@ -82,5 +85,6 @@ extern void sha512_update ( void *ctx, const void *data, size_t len );
82 85
 extern void sha512_final ( void *ctx, void *out );
83 86
 
84 87
 extern struct digest_algorithm sha512_algorithm;
88
+extern struct digest_algorithm sha384_algorithm;
85 89
 
86 90
 #endif /* IPXE_SHA512_H */

+ 36
- 3
src/tests/sha512_test.c View File

@@ -25,11 +25,12 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 25
 
26 26
 /** @file
27 27
  *
28
- * SHA-512 tests
28
+ * SHA-512 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/SHA512.pdf
33
+ *  http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA384.pdf
33 34
  *
34 35
  */
35 36
 
@@ -73,8 +74,35 @@ DIGEST_TEST ( sha512_nist_abc_stu, &sha512_algorithm, DIGEST_NIST_ABC_STU,
73 74
 		       0x26, 0x54, 0x5e, 0x96, 0xe5, 0x5b, 0x87, 0x4b, 0xe9,
74 75
 		       0x09 ) );
75 76
 
77
+/* Empty test vector (digest obtained from "sha384sum /dev/null") */
78
+DIGEST_TEST ( sha384_empty, &sha384_algorithm, DIGEST_EMPTY,
79
+	      DIGEST ( 0x38, 0xb0, 0x60, 0xa7, 0x51, 0xac, 0x96, 0x38, 0x4c,
80
+		       0xd9, 0x32, 0x7e, 0xb1, 0xb1, 0xe3, 0x6a, 0x21, 0xfd,
81
+		       0xb7, 0x11, 0x14, 0xbe, 0x07, 0x43, 0x4c, 0x0c, 0xc7,
82
+		       0xbf, 0x63, 0xf6, 0xe1, 0xda, 0x27, 0x4e, 0xde, 0xbf,
83
+		       0xe7, 0x6f, 0x65, 0xfb, 0xd5, 0x1a, 0xd2, 0xf1, 0x48,
84
+		       0x98, 0xb9, 0x5b ) );
85
+
86
+/* NIST test vector "abc" */
87
+DIGEST_TEST ( sha384_nist_abc, &sha384_algorithm, DIGEST_NIST_ABC,
88
+	      DIGEST ( 0xcb, 0x00, 0x75, 0x3f, 0x45, 0xa3, 0x5e, 0x8b, 0xb5,
89
+		       0xa0, 0x3d, 0x69, 0x9a, 0xc6, 0x50, 0x07, 0x27, 0x2c,
90
+		       0x32, 0xab, 0x0e, 0xde, 0xd1, 0x63, 0x1a, 0x8b, 0x60,
91
+		       0x5a, 0x43, 0xff, 0x5b, 0xed, 0x80, 0x86, 0x07, 0x2b,
92
+		       0xa1, 0xe7, 0xcc, 0x23, 0x58, 0xba, 0xec, 0xa1, 0x34,
93
+		       0xc8, 0x25, 0xa7 ) );
94
+
95
+/* NIST test vector "abc...stu" */
96
+DIGEST_TEST ( sha384_nist_abc_stu, &sha384_algorithm, DIGEST_NIST_ABC_STU,
97
+	      DIGEST ( 0x09, 0x33, 0x0c, 0x33, 0xf7, 0x11, 0x47, 0xe8, 0x3d,
98
+		       0x19, 0x2f, 0xc7, 0x82, 0xcd, 0x1b, 0x47, 0x53, 0x11,
99
+		       0x1b, 0x17, 0x3b, 0x3b, 0x05, 0xd2, 0x2f, 0xa0, 0x80,
100
+		       0x86, 0xe3, 0xb0, 0xf7, 0x12, 0xfc, 0xc7, 0xc7, 0x1a,
101
+		       0x55, 0x7e, 0x2d, 0xb9, 0x66, 0xc3, 0xe9, 0xfa, 0x91,
102
+		       0x74, 0x60, 0x39 ) );
103
+
76 104
 /**
77
- * Perform SHA-512 self-test
105
+ * Perform SHA-512 family self-test
78 106
  *
79 107
  */
80 108
 static void sha512_test_exec ( void ) {
@@ -83,13 +111,18 @@ static void sha512_test_exec ( void ) {
83 111
 	digest_ok ( &sha512_empty );
84 112
 	digest_ok ( &sha512_nist_abc );
85 113
 	digest_ok ( &sha512_nist_abc_stu );
114
+	digest_ok ( &sha384_empty );
115
+	digest_ok ( &sha384_nist_abc );
116
+	digest_ok ( &sha384_nist_abc_stu );
86 117
 
87 118
 	/* Speed tests */
88 119
 	DBG ( "SHA512 required %ld cycles per byte\n",
89 120
 	      digest_cost ( &sha512_algorithm ) );
121
+	DBG ( "SHA384 required %ld cycles per byte\n",
122
+	      digest_cost ( &sha384_algorithm ) );
90 123
 }
91 124
 
92
-/** SHA-512 self-test */
125
+/** SHA-512 family self-test */
93 126
 struct self_test sha512_test __self_test = {
94 127
 	.name = "sha512",
95 128
 	.exec = sha512_test_exec,

Loading…
Cancel
Save