Browse code

core: crypto add support for SHA-512/256 for RFCs 8760/7616

S-P Chan authored on 07/07/2022 02:31:00 • Daniel-Constantin Mierla committed on 11/07/2022 12:42:21
Showing 2 changed files
... ...
@@ -301,6 +301,18 @@ const static sha2_word64 sha512_initial_hash_value[8] = {
301 301
 	0x5be0cd19137e2179ULL
302 302
 };
303 303
 
304
+/* Initial hash value H for SHA-512/256 */
305
+const static sha2_word64 sha512_256_initial_hash_value[8] = {
306
+	0x22312194FC2BF72CULL,
307
+	0x9F555FA3C84C64C2ULL,
308
+	0x2393B86B6F53B151ULL,
309
+	0x963877195940EABDULL,
310
+	0x96283EE2A88EFFE3ULL,
311
+	0xBE5E1E2553863992ULL,
312
+	0x2B0199FC2C85B8AAULL,
313
+	0x0EB72DDC81C52CA2ULL
314
+};
315
+
304 316
 /*
305 317
  * Constant used by SHA256/384/512_End() functions for converting the
306 318
  * digest to a readable hexadecimal character string:
... ...
@@ -636,7 +648,7 @@ char* sr_SHA256_Data(const sha2_byte* data, size_t len, char digest[SHA256_DIGES
636 648
 }
637 649
 
638 650
 
639
-/*** SHA-512: *********************************************************/
651
+/*** SHA-512 SHA-512/256: *********************************************************/
640 652
 void sr_SHA512_Init(SHA512_CTX* context) {
641 653
 	if (context == (SHA512_CTX*)0) {
642 654
 		return;
... ...
@@ -646,6 +658,15 @@ void sr_SHA512_Init(SHA512_CTX* context) {
646 658
 	context->bitcount[0] = context->bitcount[1] =  0;
647 659
 }
648 660
 
661
+void sr_SHA512_256_Init(SHA512_CTX* context) {
662
+	if (context == (SHA512_CTX*)0) {
663
+		return;
664
+	}
665
+	MEMCPY_BCOPY(context->state, sha512_256_initial_hash_value, SHA512_DIGEST_LENGTH);
666
+	MEMSET_BZERO(context->buffer, SHA512_BLOCK_LENGTH);
667
+	context->bitcount[0] = context->bitcount[1] =  0;
668
+}
669
+
649 670
 #ifdef SHA2_UNROLL_TRANSFORM
650 671
 
651 672
 /* Unrolled SHA-512 round macros: */
... ...
@@ -97,6 +97,7 @@ char* sr_SHA384_End(SHA384_CTX*, char[SHA384_DIGEST_STRING_LENGTH]);
97 97
 char* sr_SHA384_Data(const uint8_t*, size_t, char[SHA384_DIGEST_STRING_LENGTH]);
98 98
 
99 99
 void sr_SHA512_Init(SHA512_CTX*);
100
+void sr_SHA512_256_Init(SHA512_CTX*);
100 101
 void sr_SHA512_Update(SHA512_CTX*, const uint8_t*, size_t);
101 102
 void sr_SHA512_Final(sha2_byte[SHA512_DIGEST_LENGTH], SHA512_CTX*);
102 103
 char* sr_SHA512_End(SHA512_CTX*, char[SHA512_DIGEST_STRING_LENGTH]);
... ...
@@ -117,6 +118,7 @@ char* sr_SHA384_End();
117 118
 char* sr_SHA384_Data();
118 119
 
119 120
 void sr_SHA512_Init();
121
+void sr_SHA512_256_Init();
120 122
 void sr_SHA512_Update();
121 123
 void sr_SHA512_Final();
122 124
 char* sr_SHA512_End();