xref: /isa-l_crypto/mh_sha256/mh_sha256_finalize_base.c (revision e3f7d4fb1bd3dcbfb75b87e0c61870e751d613aa)
1 /**********************************************************************
2   Copyright(c) 2011-2017 Intel Corporation All rights reserved.
3 
4   Redistribution and use in source and binary forms, with or without
5   modification, are permitted provided that the following conditions
6   are met:
7     * Redistributions of source code must retain the above copyright
8       notice, this list of conditions and the following disclaimer.
9     * Redistributions in binary form must reproduce the above copyright
10       notice, this list of conditions and the following disclaimer in
11       the documentation and/or other materials provided with the
12       distribution.
13     * Neither the name of Intel Corporation nor the names of its
14       contributors may be used to endorse or promote products derived
15       from this software without specific prior written permission.
16 
17   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 **********************************************************************/
29 
30 /*
31  * mh_sha256_finalize_base.c contains the prototypes of mh_sha256_finalize_XXX
32  * and mh_sha256_tail_XXX. Default definitions are base type which generates
33  * mh_sha256_finalize_base and mh_sha256_tail_base. Other types are generated
34  * through different predefined macros by mh_sha256.c.
35  * mh_sha256_tail is used to calculate the last incomplete block of input
36  * data. mh_sha256_finalize is the mh_sha256_ctx wrapper of mh_sha256_tail.
37  */
38 #ifndef MH_SHA256_FINALIZE_FUNCTION
39 #include <string.h>
40 #include "mh_sha256_internal.h"
41 
42 #define MH_SHA256_FINALIZE_FUNCTION	mh_sha256_finalize_base
43 #define MH_SHA256_TAIL_FUNCTION		mh_sha256_tail_base
44 #define MH_SHA256_BLOCK_FUNCTION	mh_sha256_block_base
45 #define MH_SHA256_FINALIZE_SLVER
46 #endif
47 
48 void MH_SHA256_TAIL_FUNCTION(uint8_t * partial_buffer, uint32_t total_len,
49 			     uint32_t(*mh_sha256_segs_digests)[HASH_SEGS],
50 			     uint8_t * frame_buffer, uint32_t digests[SHA256_DIGEST_WORDS])
51 {
52 	uint64_t partial_buffer_len, len_in_bit;
53 
54 	partial_buffer_len = total_len % MH_SHA256_BLOCK_SIZE;
55 
56 	// Padding the first block
57 	partial_buffer[partial_buffer_len] = 0x80;
58 	partial_buffer_len++;
59 	memset(partial_buffer + partial_buffer_len, 0,
60 	       MH_SHA256_BLOCK_SIZE - partial_buffer_len);
61 
62 	// Calculate the first block without total_length if padding needs 2 block
63 	if (partial_buffer_len > (MH_SHA256_BLOCK_SIZE - 8)) {
64 		MH_SHA256_BLOCK_FUNCTION(partial_buffer, mh_sha256_segs_digests, frame_buffer,
65 					 1);
66 		//Padding the second block
67 		memset(partial_buffer, 0, MH_SHA256_BLOCK_SIZE);
68 	}
69 	//Padding the block
70 	len_in_bit = to_be64((uint64_t) total_len * 8);
71 	*(uint64_t *) (partial_buffer + MH_SHA256_BLOCK_SIZE - 8) = len_in_bit;
72 	MH_SHA256_BLOCK_FUNCTION(partial_buffer, mh_sha256_segs_digests, frame_buffer, 1);
73 
74 	//Calculate multi-hash SHA256 digests (segment digests as input message)
75 	sha256_for_mh_sha256((uint8_t *) mh_sha256_segs_digests, digests,
76 			     4 * SHA256_DIGEST_WORDS * HASH_SEGS);
77 
78 	return;
79 }
80 
81 int MH_SHA256_FINALIZE_FUNCTION(struct mh_sha256_ctx *ctx, void *mh_sha256_digest)
82 {
83 	uint8_t i;
84 	uint8_t *partial_block_buffer;
85 	uint64_t total_len;
86 	uint32_t(*mh_sha256_segs_digests)[HASH_SEGS];
87 	uint8_t *aligned_frame_buffer;
88 
89 	if (ctx == NULL)
90 		return MH_SHA256_CTX_ERROR_NULL;
91 
92 	total_len = ctx->total_length;
93 	partial_block_buffer = ctx->partial_block_buffer;
94 
95 	/* mh_sha256 tail */
96 	aligned_frame_buffer = (uint8_t *) ALIGN_64(ctx->frame_buffer);
97 	mh_sha256_segs_digests = (uint32_t(*)[HASH_SEGS]) ctx->mh_sha256_interim_digests;
98 
99 	MH_SHA256_TAIL_FUNCTION(partial_block_buffer, total_len, mh_sha256_segs_digests,
100 				aligned_frame_buffer, ctx->mh_sha256_digest);
101 
102 	/* Output the digests of mh_sha256 */
103 	if (mh_sha256_digest != NULL) {
104 		for (i = 0; i < SHA256_DIGEST_WORDS; i++)
105 			((uint32_t *) mh_sha256_digest)[i] = ctx->mh_sha256_digest[i];
106 	}
107 
108 	return MH_SHA256_CTX_ERROR_NONE;
109 }
110 
111 #ifdef MH_SHA256_FINALIZE_SLVER
112 struct slver {
113 	uint16_t snum;
114 	uint8_t ver;
115 	uint8_t core;
116 };
117 
118 // Version info
119 struct slver mh_sha256_finalize_base_slver_000002bb;
120 struct slver mh_sha256_finalize_base_slver = { 0x02bb, 0x00, 0x00 };
121 #endif
122