xref: /isa-l_crypto/mh_sha256/mh_sha256_update_base.c (revision f6da0bf48af4a49143c2bd4ecaa976ae453273d6)
17dc32ad5SXiaodong Liu /**********************************************************************
27dc32ad5SXiaodong Liu   Copyright(c) 2011-2017 Intel Corporation All rights reserved.
37dc32ad5SXiaodong Liu 
47dc32ad5SXiaodong Liu   Redistribution and use in source and binary forms, with or without
57dc32ad5SXiaodong Liu   modification, are permitted provided that the following conditions
67dc32ad5SXiaodong Liu   are met:
77dc32ad5SXiaodong Liu     * Redistributions of source code must retain the above copyright
87dc32ad5SXiaodong Liu       notice, this list of conditions and the following disclaimer.
97dc32ad5SXiaodong Liu     * Redistributions in binary form must reproduce the above copyright
107dc32ad5SXiaodong Liu       notice, this list of conditions and the following disclaimer in
117dc32ad5SXiaodong Liu       the documentation and/or other materials provided with the
127dc32ad5SXiaodong Liu       distribution.
137dc32ad5SXiaodong Liu     * Neither the name of Intel Corporation nor the names of its
147dc32ad5SXiaodong Liu       contributors may be used to endorse or promote products derived
157dc32ad5SXiaodong Liu       from this software without specific prior written permission.
167dc32ad5SXiaodong Liu 
177dc32ad5SXiaodong Liu   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
187dc32ad5SXiaodong Liu   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
197dc32ad5SXiaodong Liu   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
207dc32ad5SXiaodong Liu   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
217dc32ad5SXiaodong Liu   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
227dc32ad5SXiaodong Liu   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
237dc32ad5SXiaodong Liu   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
247dc32ad5SXiaodong Liu   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
257dc32ad5SXiaodong Liu   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
267dc32ad5SXiaodong Liu   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
277dc32ad5SXiaodong Liu   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
287dc32ad5SXiaodong Liu **********************************************************************/
297dc32ad5SXiaodong Liu 
307dc32ad5SXiaodong Liu /*
317dc32ad5SXiaodong Liu  * mh_sha256_update_base.c contains the prototype of mh_sha256_update_XXX.
327dc32ad5SXiaodong Liu  * Default definitions are base type which generates mh_sha256_update_base.
337dc32ad5SXiaodong Liu  * Other types are generated through different predefined macros by mh_sha256.c.
347dc32ad5SXiaodong Liu  */
357dc32ad5SXiaodong Liu #ifndef MH_SHA256_UPDATE_FUNCTION
367dc32ad5SXiaodong Liu #include "mh_sha256_internal.h"
377dc32ad5SXiaodong Liu #include <string.h>
387dc32ad5SXiaodong Liu 
39a922b2ebSMarcel Cornu #define MH_SHA256_UPDATE_FUNCTION _mh_sha256_update_base
40a922b2ebSMarcel Cornu #define MH_SHA256_BLOCK_FUNCTION  _mh_sha256_block_base
417dc32ad5SXiaodong Liu #define MH_SHA256_UPDATE_SLVER
427dc32ad5SXiaodong Liu #endif
437dc32ad5SXiaodong Liu 
4438e16e11SMarcel Cornu int
MH_SHA256_UPDATE_FUNCTION(struct isal_mh_sha256_ctx * ctx,const void * buffer,uint32_t len)45*f6da0bf4SMarcel Cornu MH_SHA256_UPDATE_FUNCTION(struct isal_mh_sha256_ctx *ctx, const void *buffer, uint32_t len)
467dc32ad5SXiaodong Liu {
477dc32ad5SXiaodong Liu 
487dc32ad5SXiaodong Liu         uint8_t *partial_block_buffer;
49122c1779SMarcel Cornu         uint32_t partial_block_len;
50122c1779SMarcel Cornu         uint32_t num_blocks;
5127316f25SMarcel Cornu         uint32_t(*mh_sha256_segs_digests)[ISAL_HASH_SEGS];
527dc32ad5SXiaodong Liu         uint8_t *aligned_frame_buffer;
537dc32ad5SXiaodong Liu         const uint8_t *input_data = (const uint8_t *) buffer;
547dc32ad5SXiaodong Liu 
557dc32ad5SXiaodong Liu         if (ctx == NULL)
5615f45959SMarcel Cornu                 return ISAL_MH_SHA256_CTX_ERROR_NULL;
577dc32ad5SXiaodong Liu 
587dc32ad5SXiaodong Liu         if (len == 0)
5915f45959SMarcel Cornu                 return ISAL_MH_SHA256_CTX_ERROR_NONE;
607dc32ad5SXiaodong Liu 
6115f45959SMarcel Cornu         partial_block_len = ctx->total_length % ISAL_MH_SHA256_BLOCK_SIZE;
627dc32ad5SXiaodong Liu         partial_block_buffer = ctx->partial_block_buffer;
637dc32ad5SXiaodong Liu         aligned_frame_buffer = (uint8_t *) ALIGN_64(ctx->frame_buffer);
6427316f25SMarcel Cornu         mh_sha256_segs_digests = (uint32_t(*)[ISAL_HASH_SEGS]) ctx->mh_sha256_interim_digests;
657dc32ad5SXiaodong Liu 
667dc32ad5SXiaodong Liu         ctx->total_length += len;
677dc32ad5SXiaodong Liu         // No enough input data for mh_sha256 calculation
6815f45959SMarcel Cornu         if (len + partial_block_len < ISAL_MH_SHA256_BLOCK_SIZE) {
697dc32ad5SXiaodong Liu                 memcpy(partial_block_buffer + partial_block_len, input_data, len);
7015f45959SMarcel Cornu                 return ISAL_MH_SHA256_CTX_ERROR_NONE;
717dc32ad5SXiaodong Liu         }
727dc32ad5SXiaodong Liu         // mh_sha256 calculation for the previous partial block
737dc32ad5SXiaodong Liu         if (partial_block_len != 0) {
747dc32ad5SXiaodong Liu                 memcpy(partial_block_buffer + partial_block_len, input_data,
7515f45959SMarcel Cornu                        ISAL_MH_SHA256_BLOCK_SIZE - partial_block_len);
767dc32ad5SXiaodong Liu                 // do one_block process
777dc32ad5SXiaodong Liu                 MH_SHA256_BLOCK_FUNCTION(partial_block_buffer, mh_sha256_segs_digests,
787dc32ad5SXiaodong Liu                                          aligned_frame_buffer, 1);
7915f45959SMarcel Cornu                 input_data += ISAL_MH_SHA256_BLOCK_SIZE - partial_block_len;
8015f45959SMarcel Cornu                 len -= ISAL_MH_SHA256_BLOCK_SIZE - partial_block_len;
8115f45959SMarcel Cornu                 memset(partial_block_buffer, 0, ISAL_MH_SHA256_BLOCK_SIZE);
827dc32ad5SXiaodong Liu         }
837dc32ad5SXiaodong Liu         // Calculate mh_sha256 for the current blocks
8415f45959SMarcel Cornu         num_blocks = len / ISAL_MH_SHA256_BLOCK_SIZE;
857dc32ad5SXiaodong Liu         if (num_blocks > 0) {
867dc32ad5SXiaodong Liu                 // do num_blocks process
8738e16e11SMarcel Cornu                 MH_SHA256_BLOCK_FUNCTION(input_data, mh_sha256_segs_digests, aligned_frame_buffer,
8838e16e11SMarcel Cornu                                          num_blocks);
8915f45959SMarcel Cornu                 len -= num_blocks * ISAL_MH_SHA256_BLOCK_SIZE;
9015f45959SMarcel Cornu                 input_data += num_blocks * ISAL_MH_SHA256_BLOCK_SIZE;
917dc32ad5SXiaodong Liu         }
927dc32ad5SXiaodong Liu         // Store the partial block
937dc32ad5SXiaodong Liu         if (len != 0) {
947dc32ad5SXiaodong Liu                 memcpy(partial_block_buffer, input_data, len);
957dc32ad5SXiaodong Liu         }
967dc32ad5SXiaodong Liu 
9715f45959SMarcel Cornu         return ISAL_MH_SHA256_CTX_ERROR_NONE;
987dc32ad5SXiaodong Liu }
997dc32ad5SXiaodong Liu 
1007dc32ad5SXiaodong Liu #ifdef MH_SHA256_UPDATE_SLVER
1017dc32ad5SXiaodong Liu struct slver {
1027dc32ad5SXiaodong Liu         uint16_t snum;
1037dc32ad5SXiaodong Liu         uint8_t ver;
1047dc32ad5SXiaodong Liu         uint8_t core;
1057dc32ad5SXiaodong Liu };
1067dc32ad5SXiaodong Liu 
1077dc32ad5SXiaodong Liu // Version info
108a922b2ebSMarcel Cornu struct slver _mh_sha256_update_base_slver_000002ba;
109a922b2ebSMarcel Cornu struct slver _mh_sha256_update_base_slver = { 0x02ba, 0x00, 0x00 };
1107dc32ad5SXiaodong Liu #endif
111