xref: /isa-l_crypto/mh_sha256/mh_sha256_finalize_base.c (revision 9999e13a5707fddf47ed4073f70fb5ea5ee542b0)
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
49 MH_SHA256_TAIL_FUNCTION(uint8_t *partial_buffer, uint32_t total_len,
50                         uint32_t (*mh_sha256_segs_digests)[ISAL_HASH_SEGS], uint8_t *frame_buffer,
51                         uint32_t digests[ISAL_SHA256_DIGEST_WORDS])
52 {
53         uint64_t partial_buffer_len, len_in_bit;
54 
55         partial_buffer_len = total_len % ISAL_MH_SHA256_BLOCK_SIZE;
56 
57         // Padding the first block
58         partial_buffer[partial_buffer_len] = 0x80;
59         partial_buffer_len++;
60         memset(partial_buffer + partial_buffer_len, 0,
61                ISAL_MH_SHA256_BLOCK_SIZE - partial_buffer_len);
62 
63         // Calculate the first block without total_length if padding needs 2 block
64         if (partial_buffer_len > (ISAL_MH_SHA256_BLOCK_SIZE - 8)) {
65                 MH_SHA256_BLOCK_FUNCTION(partial_buffer, mh_sha256_segs_digests, frame_buffer, 1);
66                 // Padding the second block
67                 memset(partial_buffer, 0, ISAL_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 + ISAL_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 * ISAL_SHA256_DIGEST_WORDS * ISAL_HASH_SEGS);
77 
78         return;
79 }
80 
81 int
82 MH_SHA256_FINALIZE_FUNCTION(struct mh_sha256_ctx *ctx, void *mh_sha256_digest)
83 {
84         uint8_t i;
85         uint8_t *partial_block_buffer;
86         uint64_t total_len;
87         uint32_t(*mh_sha256_segs_digests)[ISAL_HASH_SEGS];
88         uint8_t *aligned_frame_buffer;
89 
90         if (ctx == NULL)
91                 return ISAL_MH_SHA256_CTX_ERROR_NULL;
92 
93         total_len = ctx->total_length;
94         partial_block_buffer = ctx->partial_block_buffer;
95 
96         /* mh_sha256 tail */
97         aligned_frame_buffer = (uint8_t *) ALIGN_64(ctx->frame_buffer);
98         mh_sha256_segs_digests = (uint32_t(*)[ISAL_HASH_SEGS]) ctx->mh_sha256_interim_digests;
99 
100         MH_SHA256_TAIL_FUNCTION(partial_block_buffer, (uint32_t) total_len, mh_sha256_segs_digests,
101                                 aligned_frame_buffer, ctx->mh_sha256_digest);
102 
103         /* Output the digests of mh_sha256 */
104         if (mh_sha256_digest != NULL) {
105                 for (i = 0; i < ISAL_SHA256_DIGEST_WORDS; i++)
106                         ((uint32_t *) mh_sha256_digest)[i] = ctx->mh_sha256_digest[i];
107         }
108 
109         return ISAL_MH_SHA256_CTX_ERROR_NONE;
110 }
111 
112 #ifdef MH_SHA256_FINALIZE_SLVER
113 struct slver {
114         uint16_t snum;
115         uint8_t ver;
116         uint8_t core;
117 };
118 
119 // Version info
120 struct slver _mh_sha256_finalize_base_slver_000002bb;
121 struct slver _mh_sha256_finalize_base_slver = { 0x02bb, 0x00, 0x00 };
122 #endif
123