xref: /isa-l_crypto/mh_sha256/mh_sha256_update_base.c (revision f6da0bf48af4a49143c2bd4ecaa976ae453273d6)
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_update_base.c contains the prototype of mh_sha256_update_XXX.
32  * Default definitions are base type which generates mh_sha256_update_base.
33  * Other types are generated through different predefined macros by mh_sha256.c.
34  */
35 #ifndef MH_SHA256_UPDATE_FUNCTION
36 #include "mh_sha256_internal.h"
37 #include <string.h>
38 
39 #define MH_SHA256_UPDATE_FUNCTION _mh_sha256_update_base
40 #define MH_SHA256_BLOCK_FUNCTION  _mh_sha256_block_base
41 #define MH_SHA256_UPDATE_SLVER
42 #endif
43 
44 int
MH_SHA256_UPDATE_FUNCTION(struct isal_mh_sha256_ctx * ctx,const void * buffer,uint32_t len)45 MH_SHA256_UPDATE_FUNCTION(struct isal_mh_sha256_ctx *ctx, const void *buffer, uint32_t len)
46 {
47 
48         uint8_t *partial_block_buffer;
49         uint32_t partial_block_len;
50         uint32_t num_blocks;
51         uint32_t(*mh_sha256_segs_digests)[ISAL_HASH_SEGS];
52         uint8_t *aligned_frame_buffer;
53         const uint8_t *input_data = (const uint8_t *) buffer;
54 
55         if (ctx == NULL)
56                 return ISAL_MH_SHA256_CTX_ERROR_NULL;
57 
58         if (len == 0)
59                 return ISAL_MH_SHA256_CTX_ERROR_NONE;
60 
61         partial_block_len = ctx->total_length % ISAL_MH_SHA256_BLOCK_SIZE;
62         partial_block_buffer = ctx->partial_block_buffer;
63         aligned_frame_buffer = (uint8_t *) ALIGN_64(ctx->frame_buffer);
64         mh_sha256_segs_digests = (uint32_t(*)[ISAL_HASH_SEGS]) ctx->mh_sha256_interim_digests;
65 
66         ctx->total_length += len;
67         // No enough input data for mh_sha256 calculation
68         if (len + partial_block_len < ISAL_MH_SHA256_BLOCK_SIZE) {
69                 memcpy(partial_block_buffer + partial_block_len, input_data, len);
70                 return ISAL_MH_SHA256_CTX_ERROR_NONE;
71         }
72         // mh_sha256 calculation for the previous partial block
73         if (partial_block_len != 0) {
74                 memcpy(partial_block_buffer + partial_block_len, input_data,
75                        ISAL_MH_SHA256_BLOCK_SIZE - partial_block_len);
76                 // do one_block process
77                 MH_SHA256_BLOCK_FUNCTION(partial_block_buffer, mh_sha256_segs_digests,
78                                          aligned_frame_buffer, 1);
79                 input_data += ISAL_MH_SHA256_BLOCK_SIZE - partial_block_len;
80                 len -= ISAL_MH_SHA256_BLOCK_SIZE - partial_block_len;
81                 memset(partial_block_buffer, 0, ISAL_MH_SHA256_BLOCK_SIZE);
82         }
83         // Calculate mh_sha256 for the current blocks
84         num_blocks = len / ISAL_MH_SHA256_BLOCK_SIZE;
85         if (num_blocks > 0) {
86                 // do num_blocks process
87                 MH_SHA256_BLOCK_FUNCTION(input_data, mh_sha256_segs_digests, aligned_frame_buffer,
88                                          num_blocks);
89                 len -= num_blocks * ISAL_MH_SHA256_BLOCK_SIZE;
90                 input_data += num_blocks * ISAL_MH_SHA256_BLOCK_SIZE;
91         }
92         // Store the partial block
93         if (len != 0) {
94                 memcpy(partial_block_buffer, input_data, len);
95         }
96 
97         return ISAL_MH_SHA256_CTX_ERROR_NONE;
98 }
99 
100 #ifdef MH_SHA256_UPDATE_SLVER
101 struct slver {
102         uint16_t snum;
103         uint8_t ver;
104         uint8_t core;
105 };
106 
107 // Version info
108 struct slver _mh_sha256_update_base_slver_000002ba;
109 struct slver _mh_sha256_update_base_slver = { 0x02ba, 0x00, 0x00 };
110 #endif
111